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.
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
42 #include <sys/linker_set.h>
47 #include <dev/fb/fbreg.h>
49 SET_DECLARE(videodriver_set, const video_driver_t);
54 * We need at least one entry each in order to initialize a video card
55 * for the kernel console. The arrays will be increased dynamically
59 static int vid_malloc;
60 static int adapters = 1;
61 static video_adapter_t *adp_ini;
62 static video_adapter_t **adapter = &adp_ini;
63 static video_switch_t *vidsw_ini;
64 video_switch_t **vidsw = &vidsw_ini;
66 #ifdef FB_INSTALL_CDEV
67 static struct cdevsw *vidcdevsw_ini;
68 static struct cdevsw **vidcdevsw = &vidcdevsw_ini;
74 vid_realloc_array(void)
76 video_adapter_t **new_adp;
77 video_switch_t **new_vidsw;
78 #ifdef FB_INSTALL_CDEV
79 struct cdevsw **new_cdevsw;
88 newsize = ((adapters + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
89 new_adp = malloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
90 new_vidsw = malloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
92 #ifdef FB_INSTALL_CDEV
93 new_cdevsw = malloc(sizeof(*new_cdevsw)*newsize, M_DEVBUF,
96 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
97 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
98 #ifdef FB_INSTALL_CDEV
99 bcopy(vidcdevsw, new_cdevsw, sizeof(*vidcdevsw)*adapters);
102 free(adapter, M_DEVBUF);
103 free(vidsw, M_DEVBUF);
104 #ifdef FB_INSTALL_CDEV
105 free(vidcdevsw, M_DEVBUF);
110 #ifdef FB_INSTALL_CDEV
111 vidcdevsw = new_cdevsw;
117 printf("fb: new array size %d\n", adapters);
123 vid_malloc_init(void *arg)
128 SYSINIT(vid_mem, SI_SUB_KMEM, SI_ORDER_ANY, vid_malloc_init, NULL);
131 * Low-level frame buffer driver functions
132 * frame buffer subdrivers, such as the VGA driver, call these functions
133 * to initialize the video_adapter structure and register it to the virtual
134 * frame buffer driver `fb'.
137 /* initialize the video_adapter_t structure */
139 vid_init_struct(video_adapter_t *adp, char *name, int type, int unit)
147 /* Register a video adapter */
149 vid_register(video_adapter_t *adp)
151 const video_driver_t **list;
152 const video_driver_t *p;
155 for (index = 0; index < adapters; ++index) {
156 if (adapter[index] == NULL)
159 if (index >= adapters) {
160 if (vid_realloc_array())
164 adp->va_index = index;
165 adp->va_token = NULL;
166 SET_FOREACH(list, videodriver_set) {
168 if (strcmp(p->name, adp->va_name) == 0) {
169 adapter[index] = adp;
170 vidsw[index] = p->vidsw;
179 vid_unregister(video_adapter_t *adp)
181 if ((adp->va_index < 0) || (adp->va_index >= adapters))
183 if (adapter[adp->va_index] != adp)
186 adapter[adp->va_index] = NULL;
187 vidsw[adp->va_index] = NULL;
191 /* Get video I/O function table */
193 *vid_get_switch(char *name)
195 const video_driver_t **list;
196 const video_driver_t *p;
198 SET_FOREACH(list, videodriver_set) {
200 if (strcmp(p->name, name) == 0)
208 * Video card client functions
209 * Video card clients, such as the console driver `syscons' and the frame
210 * buffer cdev driver, use these functions to claim and release a card for
214 /* find the video card specified by a driver name and a unit number */
216 vid_find_adapter(char *driver, int unit)
220 for (i = 0; i < adapters; ++i) {
221 if (adapter[i] == NULL)
223 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
225 if ((unit != -1) && (adapter[i]->va_unit != unit))
232 /* allocate a video card */
234 vid_allocate(char *driver, int unit, void *id)
240 index = vid_find_adapter(driver, unit);
242 if (adapter[index]->va_token) {
246 adapter[index]->va_token = id;
253 vid_release(video_adapter_t *adp, void *id)
259 if (adp->va_token == NULL) {
261 } else if (adp->va_token != id) {
264 adp->va_token = NULL;
271 /* Get a video adapter structure */
273 *vid_get_adapter(int index)
275 if ((index < 0) || (index >= adapters))
277 return adapter[index];
280 /* Configure drivers: this is a backdoor for the console driver XXX */
282 vid_configure(int flags)
284 const video_driver_t **list;
285 const video_driver_t *p;
287 SET_FOREACH(list, videodriver_set) {
289 if (p->configure != NULL)
290 (*p->configure)(flags);
297 * Virtual frame buffer cdev driver functions
298 * The virtual frame buffer driver dispatches driver functions to
299 * appropriate subdrivers.
302 #define FB_DRIVER_NAME "fb"
304 #ifdef FB_INSTALL_CDEV
308 static devclass_t fb_devclass;
310 static int fbprobe(device_t dev);
311 static int fbattach(device_t dev);
313 static device_method_t fb_methods[] = {
314 DEVMETHOD(device_probe, fbprobe),
315 DEVMETHOD(device_attach, fbattach),
317 DEVMETHOD(bus_print_child, bus_generic_print_child),
321 static driver_t fb_driver = {
328 fbprobe(device_t dev)
332 unit = device_get_unit(dev);
333 if (unit >= adapters)
335 if (adapter[unit] == NULL)
338 device_set_desc(dev, "generic frame buffer");
343 fbattach(device_t dev)
345 printf("fbattach: about to attach children\n");
346 bus_generic_attach(dev);
350 #endif /* experimental */
352 #define FB_UNIT(dev) minor(dev)
353 #define FB_MKMINOR(unit) (u)
355 static d_open_t fbopen;
356 static d_close_t fbclose;
357 static d_read_t fbread;
358 static d_write_t fbwrite;
359 static d_ioctl_t fbioctl;
360 static d_mmap_t fbmmap;
362 #define CDEV_MAJOR 123 /* XXX */
364 static struct cdevsw fb_cdevsw = {
372 /* strategy */ nostrategy,
373 /* name */ FB_DRIVER_NAME,
374 /* maj */ CDEV_MAJOR,
382 fb_modevent(module_t mod, int type, void *data)
387 cdevsw_add(&fb_cdevsw);
390 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(dev_t dev, video_adapter_t *adp, struct cdevsw *cdevsw)
409 if (adp->va_index >= adapters)
411 if (adapter[adp->va_index] != adp)
415 adp->va_minor = minor(dev);
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(dev_t dev, 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 fbopen(dev_t dev, int flag, int mode, struct thread *td)
447 if (unit >= adapters)
449 if (vidcdevsw[unit] == NULL)
451 return (*vidcdevsw[unit]->d_open)(makedev(0, adapter[unit]->va_minor),
456 fbclose(dev_t dev, int flag, int mode, struct thread *td)
461 if (vidcdevsw[unit] == NULL)
463 return (*vidcdevsw[unit]->d_close)(makedev(0, adapter[unit]->va_minor),
468 fbread(dev_t dev, struct uio *uio, int flag)
473 if (vidcdevsw[unit] == NULL)
475 return (*vidcdevsw[unit]->d_read)(makedev(0, adapter[unit]->va_minor),
480 fbwrite(dev_t dev, struct uio *uio, int flag)
485 if (vidcdevsw[unit] == NULL)
487 return (*vidcdevsw[unit]->d_write)(makedev(0, adapter[unit]->va_minor),
492 fbioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
497 if (vidcdevsw[unit] == NULL)
499 return (*vidcdevsw[unit]->d_ioctl)(makedev(0, adapter[unit]->va_minor),
504 fbmmap(dev_t dev, vm_offset_t offset, int nprot)
509 if (vidcdevsw[unit] == NULL)
511 return (*vidcdevsw[unit]->d_mmap)(makedev(0, adapter[unit]->va_minor),
516 DEV_DRIVER_MODULE(fb, ???, fb_driver, fb_devclass, fb_cdevsw, 0, 0);
520 * Generic frame buffer cdev driver functions
521 * Frame buffer subdrivers may call these functions to implement common
525 int genfbopen(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
531 if (!(sc->gfb_flags & FB_OPEN))
532 sc->gfb_flags |= FB_OPEN;
537 int genfbclose(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
543 sc->gfb_flags &= ~FB_OPEN;
548 int genfbread(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
557 size = adp->va_buffer_size/adp->va_info.vi_planes;
558 while (uio->uio_resid > 0) {
559 if (uio->uio_offset >= size)
561 offset = uio->uio_offset%adp->va_window_size;
562 len = imin(uio->uio_resid, size - uio->uio_offset);
563 len = imin(len, adp->va_window_size - offset);
566 (*vidsw[adp->va_index]->set_win_org)(adp, uio->uio_offset);
567 error = uiomove((caddr_t)(adp->va_window + offset), len, uio);
574 int genfbwrite(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
580 int genfbioctl(genfb_softc_t *sc, video_adapter_t *adp, u_long cmd,
581 caddr_t arg, int flag, struct thread *td)
585 if (adp == NULL) /* XXX */
587 error = (*vidsw[adp->va_index]->ioctl)(adp, cmd, arg);
588 if (error == ENOIOCTL)
593 int genfbmmap(genfb_softc_t *sc, video_adapter_t *adp, vm_offset_t offset,
596 return (*vidsw[adp->va_index]->mmap)(adp, offset, prot);
599 #endif /* FB_INSTALL_CDEV */
602 *adapter_name(int type)
609 { KD_HERCULES, "Hercules" },
613 { KD_PC98, "PC-98x1" },
619 for (i = 0; names[i].type != -1; ++i)
620 if (names[i].type == type)
622 return names[i].name;
626 * Generic low-level frame buffer functions
627 * The low-level functions in the frame buffer subdriver may use these
632 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
637 printf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
638 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
639 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
640 printf("%s%d: port:0x%lx-0x%lx, crtc:0x%lx, mem:0x%lx 0x%x\n",
641 FB_DRIVER_NAME, adp->va_index, (u_long)adp->va_io_base,
642 (u_long)adp->va_io_base + adp->va_io_size - 1,
643 (u_long)adp->va_crtc_addr, (u_long)adp->va_mem_base,
645 printf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
646 FB_DRIVER_NAME, adp->va_index,
647 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
648 printf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
649 FB_DRIVER_NAME, adp->va_index,
650 (void *)adp->va_window, (int)adp->va_window_size/1024,
651 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
652 (int)adp->va_buffer_size/1024);
656 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
662 printf("%s%d: %s, mode:%d, flags:0x%x ",
663 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
664 if (info->vi_flags & V_INFO_GRAPHICS)
665 printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
666 info->vi_width, info->vi_height,
667 info->vi_depth, info->vi_planes,
668 info->vi_cwidth, info->vi_cheight);
670 printf("T %dx%d, font:%dx%d, ",
671 info->vi_width, info->vi_height,
672 info->vi_cwidth, info->vi_cheight);
673 printf("win:0x%lx\n", (u_long)info->vi_window);
677 fb_type(int adp_type)
683 { FBTYPE_MDA, KD_MONO },
684 { FBTYPE_HERCULES, KD_HERCULES },
685 { FBTYPE_CGA, KD_CGA },
686 { FBTYPE_EGA, KD_EGA },
687 { FBTYPE_VGA, KD_VGA },
688 { FBTYPE_PC98, KD_PC98 },
689 { FBTYPE_TGA, KD_TGA },
693 for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i) {
694 if (types[i].va_type == adp_type)
695 return types[i].fb_type;
701 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
706 /* assert(adp != NULL) */
713 case FBIO_ADAPTER: /* get video adapter index */
714 *(int *)arg = adp->va_index;
717 case FBIO_ADPTYPE: /* get video adapter type */
718 *(int *)arg = adp->va_type;
721 case FBIO_ADPINFO: /* get video adapter info */
722 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
723 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
724 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
725 imin(strlen(adp->va_name) + 1,
726 sizeof(((video_adapter_info_t *)arg)->va_name)));
727 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
728 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
729 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
730 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
731 ((video_adapter_info_t *)arg)->va_crtc_addr = adp->va_crtc_addr;
732 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
733 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
734 ((video_adapter_info_t *)arg)->va_window
736 = vtophys(adp->va_window);
740 ((video_adapter_info_t *)arg)->va_window_size
741 = adp->va_window_size;
742 ((video_adapter_info_t *)arg)->va_window_gran
743 = adp->va_window_gran;
744 ((video_adapter_info_t *)arg)->va_window_orig
745 = adp->va_window_orig;
746 ((video_adapter_info_t *)arg)->va_unused0
748 = (adp->va_buffer) ? vtophys(adp->va_buffer) : 0;
752 ((video_adapter_info_t *)arg)->va_buffer_size
753 = adp->va_buffer_size;
754 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
755 ((video_adapter_info_t *)arg)->va_initial_mode
756 = adp->va_initial_mode;
757 ((video_adapter_info_t *)arg)->va_initial_bios_mode
758 = adp->va_initial_bios_mode;
759 ((video_adapter_info_t *)arg)->va_line_width
760 = adp->va_line_width;
761 ((video_adapter_info_t *)arg)->va_disp_start.x
762 = adp->va_disp_start.x;
763 ((video_adapter_info_t *)arg)->va_disp_start.y
764 = adp->va_disp_start.y;
767 case FBIO_MODEINFO: /* get mode information */
768 error = (*vidsw[adp->va_index]->get_info)(adp,
769 ((video_info_t *)arg)->vi_mode,
770 (video_info_t *)arg);
775 case FBIO_FINDMODE: /* find a matching video mode */
776 error = (*vidsw[adp->va_index]->query_mode)(adp,
777 (video_info_t *)arg);
780 case FBIO_GETMODE: /* get video mode */
781 *(int *)arg = adp->va_mode;
784 case FBIO_SETMODE: /* set video mode */
785 error = (*vidsw[adp->va_index]->set_mode)(adp, *(int *)arg);
787 error = ENODEV; /* EINVAL? */
790 case FBIO_GETWINORG: /* get frame buffer window origin */
791 *(u_int *)arg = adp->va_window_orig;
794 case FBIO_GETDISPSTART: /* get display start address */
795 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
796 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
799 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
800 *(u_int *)arg = adp->va_line_width;
803 case FBIO_BLANK: /* blank display */
804 error = (*vidsw[adp->va_index]->blank_display)(adp, *(int *)arg);
807 case FBIO_GETPALETTE: /* get color palette */
808 case FBIO_SETPALETTE: /* set color palette */
817 case FBIO_SETWINORG: /* set frame buffer window origin */
818 case FBIO_SETDISPSTART: /* set display start address */
819 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */