2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
43 #include <sys/linker_set.h>
48 #include <dev/fb/fbreg.h>
50 SET_DECLARE(videodriver_set, const video_driver_t);
55 * We need at least one entry each in order to initialize a video card
56 * for the kernel console. The arrays will be increased dynamically
60 static int vid_malloc;
61 static int adapters = 1;
62 static video_adapter_t *adp_ini;
63 static video_adapter_t **adapter = &adp_ini;
64 static video_switch_t *vidsw_ini;
65 video_switch_t **vidsw = &vidsw_ini;
67 #ifdef FB_INSTALL_CDEV
68 static struct cdevsw *vidcdevsw_ini;
69 static struct cdevsw **vidcdevsw = &vidcdevsw_ini;
75 vid_realloc_array(void)
77 video_adapter_t **new_adp;
78 video_switch_t **new_vidsw;
79 #ifdef FB_INSTALL_CDEV
80 struct cdevsw **new_cdevsw;
89 newsize = ((adapters + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
90 new_adp = malloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
91 new_vidsw = malloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
93 #ifdef FB_INSTALL_CDEV
94 new_cdevsw = malloc(sizeof(*new_cdevsw)*newsize, M_DEVBUF,
97 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
98 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
99 #ifdef FB_INSTALL_CDEV
100 bcopy(vidcdevsw, new_cdevsw, sizeof(*vidcdevsw)*adapters);
103 free(adapter, M_DEVBUF);
104 free(vidsw, M_DEVBUF);
105 #ifdef FB_INSTALL_CDEV
106 free(vidcdevsw, M_DEVBUF);
111 #ifdef FB_INSTALL_CDEV
112 vidcdevsw = new_cdevsw;
118 printf("fb: new array size %d\n", adapters);
124 vid_malloc_init(void *arg)
129 SYSINIT(vid_mem, SI_SUB_KMEM, SI_ORDER_ANY, vid_malloc_init, NULL);
132 * Low-level frame buffer driver functions
133 * frame buffer subdrivers, such as the VGA driver, call these functions
134 * to initialize the video_adapter structure and register it to the virtual
135 * frame buffer driver `fb'.
138 /* initialize the video_adapter_t structure */
140 vid_init_struct(video_adapter_t *adp, char *name, int type, int unit)
148 /* Register a video adapter */
150 vid_register(video_adapter_t *adp)
152 const video_driver_t **list;
153 const video_driver_t *p;
156 for (index = 0; index < adapters; ++index) {
157 if (adapter[index] == NULL)
160 if (index >= adapters) {
161 if (vid_realloc_array())
165 adp->va_index = index;
166 adp->va_token = NULL;
167 SET_FOREACH(list, videodriver_set) {
169 if (strcmp(p->name, adp->va_name) == 0) {
170 adapter[index] = adp;
171 vidsw[index] = p->vidsw;
180 vid_unregister(video_adapter_t *adp)
182 if ((adp->va_index < 0) || (adp->va_index >= adapters))
184 if (adapter[adp->va_index] != adp)
187 adapter[adp->va_index] = NULL;
188 vidsw[adp->va_index] = NULL;
192 /* Get video I/O function table */
194 *vid_get_switch(char *name)
196 const video_driver_t **list;
197 const video_driver_t *p;
199 SET_FOREACH(list, videodriver_set) {
201 if (strcmp(p->name, name) == 0)
209 * Video card client functions
210 * Video card clients, such as the console driver `syscons' and the frame
211 * buffer cdev driver, use these functions to claim and release a card for
215 /* find the video card specified by a driver name and a unit number */
217 vid_find_adapter(char *driver, int unit)
221 for (i = 0; i < adapters; ++i) {
222 if (adapter[i] == NULL)
224 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
226 if ((unit != -1) && (adapter[i]->va_unit != unit))
233 /* allocate a video card */
235 vid_allocate(char *driver, int unit, void *id)
241 index = vid_find_adapter(driver, unit);
243 if (adapter[index]->va_token) {
247 adapter[index]->va_token = id;
254 vid_release(video_adapter_t *adp, void *id)
260 if (adp->va_token == NULL) {
262 } else if (adp->va_token != id) {
265 adp->va_token = NULL;
272 /* Get a video adapter structure */
274 *vid_get_adapter(int index)
276 if ((index < 0) || (index >= adapters))
278 return adapter[index];
281 /* Configure drivers: this is a backdoor for the console driver XXX */
283 vid_configure(int flags)
285 const video_driver_t **list;
286 const video_driver_t *p;
288 SET_FOREACH(list, videodriver_set) {
290 if (p->configure != NULL)
291 (*p->configure)(flags);
298 * Virtual frame buffer cdev driver functions
299 * The virtual frame buffer driver dispatches driver functions to
300 * appropriate subdrivers.
303 #define FB_DRIVER_NAME "fb"
305 #ifdef FB_INSTALL_CDEV
307 #if 0 /* experimental */
309 static devclass_t fb_devclass;
311 static int fbprobe(device_t dev);
312 static int fbattach(device_t dev);
314 static device_method_t fb_methods[] = {
315 DEVMETHOD(device_probe, fbprobe),
316 DEVMETHOD(device_attach, fbattach),
318 DEVMETHOD(bus_print_child, bus_generic_print_child),
322 static driver_t fb_driver = {
329 fbprobe(device_t dev)
333 unit = device_get_unit(dev);
334 if (unit >= adapters)
336 if (adapter[unit] == NULL)
339 device_set_desc(dev, "generic frame buffer");
344 fbattach(device_t dev)
346 printf("fbattach: about to attach children\n");
347 bus_generic_attach(dev);
353 #define FB_UNIT(dev) dev2unit(dev)
354 #define FB_MKMINOR(unit) (u)
356 #if 0 /* experimental */
357 static d_open_t fbopen;
358 static d_close_t fbclose;
359 static d_read_t fbread;
360 static d_write_t fbwrite;
361 static d_ioctl_t fbioctl;
362 static d_mmap_t fbmmap;
365 static struct cdevsw fb_cdevsw = {
366 .d_version = D_VERSION,
367 .d_flags = D_NEEDGIANT,
374 .d_name = FB_DRIVER_NAME,
380 fb_modevent(module_t mod, int type, void *data)
387 printf("fb module unload - not possible for this module type\n");
395 static moduledata_t fb_mod = {
401 DECLARE_MODULE(fb, fb_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
404 fb_attach(int unit, video_adapter_t *adp, struct cdevsw *cdevsw)
408 if (adp->va_index >= adapters)
410 if (adapter[adp->va_index] != adp)
414 adp->va_minor = unit;
415 vidcdevsw[adp->va_index] = cdevsw;
418 printf("fb%d at %s%d\n", adp->va_index, adp->va_name, adp->va_unit);
423 fb_detach(int unit, video_adapter_t *adp, struct cdevsw *cdevsw)
427 if (adp->va_index >= adapters)
429 if (adapter[adp->va_index] != adp)
431 if (vidcdevsw[adp->va_index] != cdevsw)
435 vidcdevsw[adp->va_index] = NULL;
441 * Generic frame buffer cdev driver functions
442 * Frame buffer subdrivers may call these functions to implement common
446 int genfbopen(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
452 if (!(sc->gfb_flags & FB_OPEN))
453 sc->gfb_flags |= FB_OPEN;
458 int genfbclose(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
464 sc->gfb_flags &= ~FB_OPEN;
469 int genfbread(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
478 size = adp->va_buffer_size/adp->va_info.vi_planes;
479 while (uio->uio_resid > 0) {
480 if (uio->uio_offset >= size)
482 offset = uio->uio_offset%adp->va_window_size;
483 len = imin(uio->uio_resid, size - uio->uio_offset);
484 len = imin(len, adp->va_window_size - offset);
487 vidd_set_win_org(adp, uio->uio_offset);
488 error = uiomove((caddr_t)(adp->va_window + offset), len, uio);
495 int genfbwrite(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
501 int genfbioctl(genfb_softc_t *sc, video_adapter_t *adp, u_long cmd,
502 caddr_t arg, int flag, struct thread *td)
506 if (adp == NULL) /* XXX */
508 error = vidd_ioctl(adp, cmd, arg);
509 if (error == ENOIOCTL)
514 int genfbmmap(genfb_softc_t *sc, video_adapter_t *adp, vm_offset_t offset,
515 vm_offset_t *paddr, int prot)
517 return vidd_mmap(adp, offset, paddr, prot);
520 #endif /* FB_INSTALL_CDEV */
523 *adapter_name(int type)
530 { KD_HERCULES, "Hercules" },
534 { KD_PC98, "PC-98x1" },
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_PC98, KD_PC98 },
610 { FBTYPE_TGA, KD_TGA },
614 for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i) {
615 if (types[i].va_type == adp_type)
616 return types[i].fb_type;
622 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
627 /* assert(adp != NULL) */
634 case FBIO_ADAPTER: /* get video adapter index */
635 *(int *)arg = adp->va_index;
638 case FBIO_ADPTYPE: /* get video adapter type */
639 *(int *)arg = adp->va_type;
642 case FBIO_ADPINFO: /* get video adapter info */
643 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
644 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
645 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
646 imin(strlen(adp->va_name) + 1,
647 sizeof(((video_adapter_info_t *)arg)->va_name)));
648 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
649 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
650 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
651 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
652 ((video_adapter_info_t *)arg)->va_crtc_addr = adp->va_crtc_addr;
653 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
654 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
655 ((video_adapter_info_t *)arg)->va_window
657 = vtophys(adp->va_window);
661 ((video_adapter_info_t *)arg)->va_window_size
662 = adp->va_window_size;
663 ((video_adapter_info_t *)arg)->va_window_gran
664 = adp->va_window_gran;
665 ((video_adapter_info_t *)arg)->va_window_orig
666 = adp->va_window_orig;
667 ((video_adapter_info_t *)arg)->va_unused0
669 = (adp->va_buffer) ? vtophys(adp->va_buffer) : 0;
673 ((video_adapter_info_t *)arg)->va_buffer_size
674 = adp->va_buffer_size;
675 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
676 ((video_adapter_info_t *)arg)->va_initial_mode
677 = adp->va_initial_mode;
678 ((video_adapter_info_t *)arg)->va_initial_bios_mode
679 = adp->va_initial_bios_mode;
680 ((video_adapter_info_t *)arg)->va_line_width
681 = adp->va_line_width;
682 ((video_adapter_info_t *)arg)->va_disp_start.x
683 = adp->va_disp_start.x;
684 ((video_adapter_info_t *)arg)->va_disp_start.y
685 = adp->va_disp_start.y;
688 case FBIO_MODEINFO: /* get mode information */
689 error = vidd_get_info(adp,
690 ((video_info_t *)arg)->vi_mode,
691 (video_info_t *)arg);
696 case FBIO_FINDMODE: /* find a matching video mode */
697 error = vidd_query_mode(adp, (video_info_t *)arg);
700 case FBIO_GETMODE: /* get video mode */
701 *(int *)arg = adp->va_mode;
704 case FBIO_SETMODE: /* set video mode */
705 error = vidd_set_mode(adp, *(int *)arg);
707 error = ENODEV; /* EINVAL? */
710 case FBIO_GETWINORG: /* get frame buffer window origin */
711 *(u_int *)arg = adp->va_window_orig;
714 case FBIO_GETDISPSTART: /* get display start address */
715 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
716 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
719 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
720 *(u_int *)arg = adp->va_line_width;
723 case FBIO_BLANK: /* blank display */
724 error = vidd_blank_display(adp, *(int *)arg);
727 case FBIO_GETPALETTE: /* get color palette */
728 case FBIO_SETPALETTE: /* set color palette */
737 case FBIO_SETWINORG: /* set frame buffer window origin */
738 case FBIO_SETDISPSTART: /* set display start address */
739 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */