2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
45 #include <sys/linker_set.h>
50 #include <dev/fb/fbreg.h>
52 SET_DECLARE(videodriver_set, const video_driver_t);
57 * We need at least one entry each in order to initialize a video card
58 * for the kernel console. The arrays will be increased dynamically
62 static int vid_malloc;
63 static int adapters = 1;
64 static video_adapter_t *adp_ini;
65 static video_adapter_t **adapter = &adp_ini;
66 static video_switch_t *vidsw_ini;
67 video_switch_t **vidsw = &vidsw_ini;
69 #ifdef FB_INSTALL_CDEV
70 static struct cdevsw *vidcdevsw_ini;
71 static struct cdevsw **vidcdevsw = &vidcdevsw_ini;
77 vid_realloc_array(void)
79 video_adapter_t **new_adp;
80 video_switch_t **new_vidsw;
81 #ifdef FB_INSTALL_CDEV
82 struct cdevsw **new_cdevsw;
91 newsize = rounddown(adapters + ARRAY_DELTA, ARRAY_DELTA);
92 new_adp = malloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
93 new_vidsw = malloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
95 #ifdef FB_INSTALL_CDEV
96 new_cdevsw = malloc(sizeof(*new_cdevsw)*newsize, M_DEVBUF,
99 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
100 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
101 #ifdef FB_INSTALL_CDEV
102 bcopy(vidcdevsw, new_cdevsw, sizeof(*vidcdevsw)*adapters);
105 free(adapter, M_DEVBUF);
106 free(vidsw, M_DEVBUF);
107 #ifdef FB_INSTALL_CDEV
108 free(vidcdevsw, M_DEVBUF);
113 #ifdef FB_INSTALL_CDEV
114 vidcdevsw = new_cdevsw;
120 printf("fb: new array size %d\n", adapters);
126 vid_malloc_init(void *arg)
131 SYSINIT(vid_mem, SI_SUB_KMEM, SI_ORDER_ANY, vid_malloc_init, NULL);
134 * Low-level frame buffer driver functions
135 * frame buffer subdrivers, such as the VGA driver, call these functions
136 * to initialize the video_adapter structure and register it to the virtual
137 * frame buffer driver `fb'.
140 /* initialize the video_adapter_t structure */
142 vid_init_struct(video_adapter_t *adp, char *name, int type, int unit)
150 /* Register a video adapter */
152 vid_register(video_adapter_t *adp)
154 const video_driver_t **list;
155 const video_driver_t *p;
158 for (index = 0; index < adapters; ++index) {
159 if (adapter[index] == NULL)
162 if (index >= adapters) {
163 if (vid_realloc_array())
167 adp->va_index = index;
168 adp->va_token = NULL;
169 SET_FOREACH(list, videodriver_set) {
171 if (strcmp(p->name, adp->va_name) == 0) {
172 adapter[index] = adp;
173 vidsw[index] = p->vidsw;
182 vid_unregister(video_adapter_t *adp)
184 if ((adp->va_index < 0) || (adp->va_index >= adapters))
186 if (adapter[adp->va_index] != adp)
189 adapter[adp->va_index] = NULL;
190 vidsw[adp->va_index] = NULL;
194 /* Get video I/O function table */
196 *vid_get_switch(char *name)
198 const video_driver_t **list;
199 const video_driver_t *p;
201 SET_FOREACH(list, videodriver_set) {
203 if (strcmp(p->name, name) == 0)
211 * Video card client functions
212 * Video card clients, such as the console driver `syscons' and the frame
213 * buffer cdev driver, use these functions to claim and release a card for
217 /* find the video card specified by a driver name and a unit number */
219 vid_find_adapter(char *driver, int unit)
223 for (i = 0; i < adapters; ++i) {
224 if (adapter[i] == NULL)
226 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
228 if ((unit != -1) && (adapter[i]->va_unit != unit))
235 /* allocate a video card */
237 vid_allocate(char *driver, int unit, void *id)
243 index = vid_find_adapter(driver, unit);
245 if (adapter[index]->va_token) {
249 adapter[index]->va_token = id;
256 vid_release(video_adapter_t *adp, void *id)
262 if (adp->va_token == NULL) {
264 } else if (adp->va_token != id) {
267 adp->va_token = NULL;
274 /* Get a video adapter structure */
276 *vid_get_adapter(int index)
278 if ((index < 0) || (index >= adapters))
280 return adapter[index];
283 /* Configure drivers: this is a backdoor for the console driver XXX */
285 vid_configure(int flags)
287 const video_driver_t **list;
288 const video_driver_t *p;
290 SET_FOREACH(list, videodriver_set) {
292 if (p->configure != NULL)
293 (*p->configure)(flags);
300 * Virtual frame buffer cdev driver functions
301 * The virtual frame buffer driver dispatches driver functions to
302 * appropriate subdrivers.
305 #define FB_DRIVER_NAME "fb"
307 #ifdef FB_INSTALL_CDEV
309 #if 0 /* experimental */
311 static devclass_t fb_devclass;
313 static int fbprobe(device_t dev);
314 static int fbattach(device_t dev);
316 static device_method_t fb_methods[] = {
317 DEVMETHOD(device_probe, fbprobe),
318 DEVMETHOD(device_attach, fbattach),
323 static driver_t fb_driver = {
330 fbprobe(device_t dev)
334 unit = device_get_unit(dev);
335 if (unit >= adapters)
337 if (adapter[unit] == NULL)
340 device_set_desc(dev, "generic frame buffer");
345 fbattach(device_t dev)
347 printf("fbattach: about to attach children\n");
348 bus_generic_attach(dev);
354 #define FB_UNIT(dev) dev2unit(dev)
355 #define FB_MKMINOR(unit) (u)
357 #if 0 /* experimental */
358 static d_open_t fbopen;
359 static d_close_t fbclose;
360 static d_read_t fbread;
361 static d_write_t fbwrite;
362 static d_ioctl_t fbioctl;
363 static d_mmap_t fbmmap;
366 static struct cdevsw fb_cdevsw = {
367 .d_version = D_VERSION,
368 .d_flags = D_NEEDGIANT,
375 .d_name = FB_DRIVER_NAME,
381 fb_modevent(module_t mod, int type, void *data)
388 printf("fb module unload - not possible for this module type\n");
396 static moduledata_t fb_mod = {
402 DECLARE_MODULE(fb, fb_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
405 fb_attach(int unit, video_adapter_t *adp, struct cdevsw *cdevsw)
409 if (adp->va_index >= adapters)
411 if (adapter[adp->va_index] != adp)
415 adp->va_minor = unit;
416 vidcdevsw[adp->va_index] = cdevsw;
419 printf("fb%d at %s%d\n", adp->va_index, adp->va_name, adp->va_unit);
424 fb_detach(int unit, video_adapter_t *adp, struct cdevsw *cdevsw)
428 if (adp->va_index >= adapters)
430 if (adapter[adp->va_index] != adp)
432 if (vidcdevsw[adp->va_index] != cdevsw)
436 vidcdevsw[adp->va_index] = NULL;
442 * Generic frame buffer cdev driver functions
443 * Frame buffer subdrivers may call these functions to implement common
447 int genfbopen(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
453 if (!(sc->gfb_flags & FB_OPEN))
454 sc->gfb_flags |= FB_OPEN;
459 int genfbclose(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
465 sc->gfb_flags &= ~FB_OPEN;
470 int genfbread(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
479 size = adp->va_buffer_size/adp->va_info.vi_planes;
480 while (uio->uio_resid > 0) {
481 if (uio->uio_offset >= size)
483 offset = uio->uio_offset%adp->va_window_size;
484 len = imin(uio->uio_resid, size - uio->uio_offset);
485 len = imin(len, adp->va_window_size - offset);
488 vidd_set_win_org(adp, uio->uio_offset);
489 error = uiomove((caddr_t)(adp->va_window + offset), len, uio);
496 int genfbwrite(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
502 int genfbioctl(genfb_softc_t *sc, video_adapter_t *adp, u_long cmd,
503 caddr_t arg, int flag, struct thread *td)
507 if (adp == NULL) /* XXX */
509 error = vidd_ioctl(adp, cmd, arg);
510 if (error == ENOIOCTL)
515 int genfbmmap(genfb_softc_t *sc, video_adapter_t *adp, vm_ooffset_t offset,
516 vm_paddr_t *paddr, int prot, vm_memattr_t *memattr)
518 return vidd_mmap(adp, offset, paddr, prot, memattr);
521 #endif /* FB_INSTALL_CDEV */
524 *adapter_name(int type)
531 { KD_HERCULES, "Hercules" },
540 for (i = 0; names[i].type != -1; ++i)
541 if (names[i].type == type)
543 return names[i].name;
547 * Generic low-level frame buffer functions
548 * The low-level functions in the frame buffer subdriver may use these
553 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
558 printf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
559 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
560 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
561 printf("%s%d: port:0x%lx-0x%lx, crtc:0x%lx, mem:0x%lx 0x%x\n",
562 FB_DRIVER_NAME, adp->va_index, (u_long)adp->va_io_base,
563 (u_long)adp->va_io_base + adp->va_io_size - 1,
564 (u_long)adp->va_crtc_addr, (u_long)adp->va_mem_base,
566 printf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
567 FB_DRIVER_NAME, adp->va_index,
568 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
569 printf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
570 FB_DRIVER_NAME, adp->va_index,
571 (void *)adp->va_window, (int)adp->va_window_size/1024,
572 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
573 (int)adp->va_buffer_size/1024);
577 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
583 printf("%s%d: %s, mode:%d, flags:0x%x ",
584 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
585 if (info->vi_flags & V_INFO_GRAPHICS)
586 printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
587 info->vi_width, info->vi_height,
588 info->vi_depth, info->vi_planes,
589 info->vi_cwidth, info->vi_cheight);
591 printf("T %dx%d, font:%dx%d, ",
592 info->vi_width, info->vi_height,
593 info->vi_cwidth, info->vi_cheight);
594 printf("win:0x%lx\n", (u_long)info->vi_window);
598 fb_type(int adp_type)
604 { FBTYPE_MDA, KD_MONO },
605 { FBTYPE_HERCULES, KD_HERCULES },
606 { FBTYPE_CGA, KD_CGA },
607 { FBTYPE_EGA, KD_EGA },
608 { FBTYPE_VGA, KD_VGA },
609 { FBTYPE_TGA, KD_TGA },
613 for (i = 0; i < nitems(types); ++i) {
614 if (types[i].va_type == adp_type)
615 return types[i].fb_type;
621 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
626 /* assert(adp != NULL) */
633 case FBIO_ADAPTER: /* get video adapter index */
634 *(int *)arg = adp->va_index;
637 case FBIO_ADPTYPE: /* get video adapter type */
638 *(int *)arg = adp->va_type;
641 case FBIO_ADPINFO: /* get video adapter info */
642 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
643 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
644 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
645 imin(strlen(adp->va_name) + 1,
646 sizeof(((video_adapter_info_t *)arg)->va_name)));
647 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
648 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
649 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
650 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
651 ((video_adapter_info_t *)arg)->va_crtc_addr = adp->va_crtc_addr;
652 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
653 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
654 ((video_adapter_info_t *)arg)->va_window
655 #if defined(__amd64__) || defined(__i386__)
656 = vtophys(adp->va_window);
660 ((video_adapter_info_t *)arg)->va_window_size
661 = adp->va_window_size;
662 ((video_adapter_info_t *)arg)->va_window_gran
663 = adp->va_window_gran;
664 ((video_adapter_info_t *)arg)->va_window_orig
665 = adp->va_window_orig;
666 ((video_adapter_info_t *)arg)->va_unused0
667 #if defined(__amd64__) || defined(__i386__)
668 = adp->va_buffer != 0 ? vtophys(adp->va_buffer) : 0;
672 ((video_adapter_info_t *)arg)->va_buffer_size
673 = adp->va_buffer_size;
674 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
675 ((video_adapter_info_t *)arg)->va_initial_mode
676 = adp->va_initial_mode;
677 ((video_adapter_info_t *)arg)->va_initial_bios_mode
678 = adp->va_initial_bios_mode;
679 ((video_adapter_info_t *)arg)->va_line_width
680 = adp->va_line_width;
681 ((video_adapter_info_t *)arg)->va_disp_start.x
682 = adp->va_disp_start.x;
683 ((video_adapter_info_t *)arg)->va_disp_start.y
684 = adp->va_disp_start.y;
687 case FBIO_MODEINFO: /* get mode information */
688 error = vidd_get_info(adp,
689 ((video_info_t *)arg)->vi_mode,
690 (video_info_t *)arg);
695 case FBIO_FINDMODE: /* find a matching video mode */
696 error = vidd_query_mode(adp, (video_info_t *)arg);
699 case FBIO_GETMODE: /* get video mode */
700 *(int *)arg = adp->va_mode;
703 case FBIO_SETMODE: /* set video mode */
704 error = vidd_set_mode(adp, *(int *)arg);
706 error = ENODEV; /* EINVAL? */
709 case FBIO_GETWINORG: /* get frame buffer window origin */
710 *(u_int *)arg = adp->va_window_orig;
713 case FBIO_GETDISPSTART: /* get display start address */
714 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
715 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
718 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
719 *(u_int *)arg = adp->va_line_width;
722 case FBIO_BLANK: /* blank display */
723 error = vidd_blank_display(adp, *(int *)arg);
726 case FBIO_GETPALETTE: /* get color palette */
727 case FBIO_SETPALETTE: /* set color palette */
736 case FBIO_SETWINORG: /* set frame buffer window origin */
737 case FBIO_SETDISPSTART: /* set display start address */
738 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */