2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 The FreeBSD Foundation
7 * This software was developed by Aleksandr Rybalko under sponsorship from the
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* Generic framebuffer */
35 /* TODO unlink from VT(9) */
36 /* TODO done normal /dev/fb methods */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/queue.h>
51 #include <machine/bus.h>
53 #include <dev/vt/vt.h>
54 #include <dev/vt/hw/fb/vt_fb.h>
61 LIST_HEAD(fb_list_head_t, fb_list_entry) fb_list_head =
62 LIST_HEAD_INITIALIZER(fb_list_head);
63 struct fb_list_entry {
64 struct fb_info *fb_info;
66 LIST_ENTRY(fb_list_entry) fb_list;
71 struct fb_info *sc_info;
74 static void fbd_evh_init(void *);
75 /* SI_ORDER_SECOND, just after EVENTHANDLERs initialized. */
76 SYSINIT(fbd_evh_init, SI_SUB_CONFIGURE, SI_ORDER_SECOND, fbd_evh_init, NULL);
78 static d_open_t fb_open;
79 static d_close_t fb_close;
80 static d_read_t fb_read;
81 static d_write_t fb_write;
82 static d_ioctl_t fb_ioctl;
83 static d_mmap_t fb_mmap;
85 static struct cdevsw fb_cdevsw = {
86 .d_version = D_VERSION,
87 .d_flags = D_NEEDGIANT,
97 static int framebuffer_dev_unit = 0;
100 fb_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
107 fb_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
114 fb_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
117 struct fb_info *info;
125 bcopy(info, (struct fbtype *)data, sizeof(struct fbtype));
128 case FBIO_GETWINORG: /* get frame buffer window origin */
132 case FBIO_GETDISPSTART: /* get display start address */
133 ((video_display_start_t *)data)->x = 0;
134 ((video_display_start_t *)data)->y = 0;
137 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
138 *(u_int *)data = info->fb_stride;
141 case FBIO_BLANK: /* blank display */
142 if (info->setblankmode != NULL)
143 error = info->setblankmode(info->fb_priv, *(int *)data);
154 fb_read(struct cdev *dev, struct uio *uio, int ioflag)
157 return (0); /* XXX nothing to read, yet */
161 fb_write(struct cdev *dev, struct uio *uio, int ioflag)
164 return (0); /* XXX nothing written */
168 fb_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
169 vm_memattr_t *memattr)
171 struct fb_info *info;
175 if (info->fb_flags & FB_FLAG_NOMMAP)
178 if (offset >= 0 && offset < info->fb_size) {
179 if (info->fb_pbase == 0)
180 *paddr = vtophys((uint8_t *)info->fb_vbase + offset);
182 *paddr = info->fb_pbase + offset;
183 if (info->fb_flags & FB_FLAG_MEMATTR)
184 *memattr = info->fb_memattr;
191 fb_init(struct fb_list_entry *entry, int unit)
193 struct fb_info *info;
195 info = entry->fb_info;
196 entry->fb_si = make_dev(&fb_cdevsw, unit, UID_ROOT, GID_WHEEL,
198 entry->fb_si->si_drv1 = info;
199 info->fb_cdev = entry->fb_si;
207 struct fb_list_entry *entry;
209 if (LIST_EMPTY(&fb_list_head))
212 LIST_FOREACH(entry, &fb_list_head, fb_list) {
213 printf("FB %s @%p\n", entry->fb_info->fb_name,
214 (void *)entry->fb_info->fb_pbase);
220 static struct fb_list_entry *
221 fbd_find(struct fb_info* info)
223 struct fb_list_entry *entry, *tmp;
225 LIST_FOREACH_SAFE(entry, &fb_list_head, fb_list, tmp) {
226 if (entry->fb_info == info) {
235 fbd_register(struct fb_info* info)
237 struct fb_list_entry *entry;
241 if (LIST_EMPTY(&fb_list_head))
244 entry = fbd_find(info);
246 /* XXX Update framebuffer params */
250 entry = malloc(sizeof(struct fb_list_entry), M_DEVBUF, M_WAITOK|M_ZERO);
251 entry->fb_info = info;
253 LIST_INSERT_HEAD(&fb_list_head, entry, fb_list);
255 err = fb_init(entry, framebuffer_dev_unit++);
260 err = vt_fb_attach(info);
269 fbd_unregister(struct fb_info* info)
271 struct fb_list_entry *entry, *tmp;
273 LIST_FOREACH_SAFE(entry, &fb_list_head, fb_list, tmp) {
274 if (entry->fb_info == info) {
275 LIST_REMOVE(entry, fb_list);
276 if (LIST_EMPTY(&fb_list_head))
278 free(entry, M_DEVBUF);
287 register_fb_wrap(void *arg, void *ptr)
290 fbd_register((struct fb_info *)ptr);
294 unregister_fb_wrap(void *arg, void *ptr)
297 fbd_unregister((struct fb_info *)ptr);
301 fbd_evh_init(void *ctx)
304 EVENTHANDLER_REGISTER(register_framebuffer, register_fb_wrap, NULL,
305 EVENTHANDLER_PRI_ANY);
306 EVENTHANDLER_REGISTER(unregister_framebuffer, unregister_fb_wrap, NULL,
307 EVENTHANDLER_PRI_ANY);
310 /* Newbus methods. */
312 fbd_probe(device_t dev)
315 return (BUS_PROBE_NOWILDCARD);
319 fbd_attach(device_t dev)
321 struct fbd_softc *sc;
324 sc = device_get_softc(dev);
327 sc->sc_info = FB_GETINFO(device_get_parent(dev));
328 if (sc->sc_info == NULL)
330 err = fbd_register(sc->sc_info);
336 fbd_detach(device_t dev)
338 struct fbd_softc *sc;
341 sc = device_get_softc(dev);
343 err = fbd_unregister(sc->sc_info);
348 static device_method_t fbd_methods[] = {
349 /* Device interface */
350 DEVMETHOD(device_probe, fbd_probe),
351 DEVMETHOD(device_attach, fbd_attach),
352 DEVMETHOD(device_detach, fbd_detach),
354 DEVMETHOD(device_shutdown, bus_generic_shutdown),
359 driver_t fbd_driver = {
362 sizeof(struct fbd_softc)
365 devclass_t fbd_devclass;
367 DRIVER_MODULE(fbd, fb, fbd_driver, fbd_devclass, 0, 0);
368 DRIVER_MODULE(fbd, drmn, fbd_driver, fbd_devclass, 0, 0);
369 DRIVER_MODULE(fbd, udl, fbd_driver, fbd_devclass, 0, 0);
370 MODULE_VERSION(fbd, 1);