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.
28 * $FreeBSD: src/sys/dev/fb/fb.c,v 1.11.2.2 2000/08/02 22:35:22 peter Exp $
29 * $DragonFly: src/sys/dev/video/fb/fb.c,v 1.6 2003/08/07 21:17:15 dillon Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
42 #include <sys/device.h>
52 * We need at least one entry each in order to initialize a video card
53 * for the kernel console. The arrays will be increased dynamically
57 static int vid_malloc;
58 static int adapters = 1;
59 static video_adapter_t *adp_ini;
60 static video_adapter_t **adapter = &adp_ini;
61 static video_switch_t *vidsw_ini;
62 video_switch_t **vidsw = &vidsw_ini;
64 #ifdef FB_INSTALL_CDEV
65 static struct lwkt_port *vidcdevsw_ini;
66 static struct lwkt_port **vidcdevsw = &vidcdevsw_ini;
72 vid_realloc_array(void)
74 video_adapter_t **new_adp;
75 video_switch_t **new_vidsw;
76 #ifdef FB_INSTALL_CDEV
77 struct lwkt_port **new_cdevsw;
86 newsize = ((adapters + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
87 new_adp = malloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK);
88 new_vidsw = malloc(sizeof(*new_vidsw)*newsize, M_DEVBUF, M_WAITOK);
89 #ifdef FB_INSTALL_CDEV
90 new_cdevsw = malloc(sizeof(*new_cdevsw)*newsize, M_DEVBUF, M_WAITOK);
92 bzero(new_adp, sizeof(*new_adp)*newsize);
93 bzero(new_vidsw, sizeof(*new_vidsw)*newsize);
94 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
95 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
96 #ifdef FB_INSTALL_CDEV
97 bzero(new_cdevsw, sizeof(*new_cdevsw)*newsize);
98 bcopy(vidcdevsw, new_cdevsw, sizeof(*vidcdevsw)*adapters);
101 free(adapter, M_DEVBUF);
102 free(vidsw, M_DEVBUF);
103 #ifdef FB_INSTALL_CDEV
104 free(vidcdevsw, M_DEVBUF);
109 #ifdef FB_INSTALL_CDEV
110 vidcdevsw = new_cdevsw;
116 printf("fb: new array size %d\n", adapters);
122 vid_malloc_init(void *arg)
127 SYSINIT(vid_mem, SI_SUB_KMEM, SI_ORDER_ANY, vid_malloc_init, NULL);
130 * Low-level frame buffer driver functions
131 * frame buffer subdrivers, such as the VGA driver, call these functions
132 * to initialize the video_adapter structure and register it to the virtual
133 * frame buffer driver `fb'.
136 /* initialize the video_adapter_t structure */
138 vid_init_struct(video_adapter_t *adp, char *name, int type, int unit)
146 /* Register a video adapter */
148 vid_register(video_adapter_t *adp)
150 const video_driver_t **list;
151 const video_driver_t *p;
154 for (index = 0; index < adapters; ++index) {
155 if (adapter[index] == NULL)
158 if (index >= adapters) {
159 if (vid_realloc_array())
163 adp->va_index = index;
164 adp->va_token = NULL;
165 list = (const video_driver_t **)videodriver_set.ls_items;
166 while ((p = *list++) != NULL) {
167 if (strcmp(p->name, adp->va_name) == 0) {
168 adapter[index] = adp;
169 vidsw[index] = p->vidsw;
178 vid_unregister(video_adapter_t *adp)
180 if ((adp->va_index < 0) || (adp->va_index >= adapters))
182 if (adapter[adp->va_index] != adp)
185 adapter[adp->va_index] = NULL;
186 vidsw[adp->va_index] = NULL;
190 /* Get video I/O function table */
192 *vid_get_switch(char *name)
194 const video_driver_t **list;
195 const video_driver_t *p;
197 list = (const video_driver_t **)videodriver_set.ls_items;
198 while ((p = *list++) != NULL) {
199 if (strcmp(p->name, name) == 0)
207 * Video card client functions
208 * Video card clients, such as the console driver `syscons' and the frame
209 * buffer cdev driver, use these functions to claim and release a card for
213 /* find the video card specified by a driver name and a unit number */
215 vid_find_adapter(char *driver, int unit)
219 for (i = 0; i < adapters; ++i) {
220 if (adapter[i] == NULL)
222 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
224 if ((unit != -1) && (adapter[i]->va_unit != unit))
231 /* allocate a video card */
233 vid_allocate(char *driver, int unit, void *id)
239 index = vid_find_adapter(driver, unit);
241 if (adapter[index]->va_token) {
245 adapter[index]->va_token = id;
252 vid_release(video_adapter_t *adp, void *id)
258 if (adp->va_token == NULL) {
260 } else if (adp->va_token != id) {
263 adp->va_token = NULL;
270 /* Get a video adapter structure */
272 *vid_get_adapter(int index)
274 if ((index < 0) || (index >= adapters))
276 return adapter[index];
279 /* Configure drivers: this is a backdoor for the console driver XXX */
281 vid_configure(int flags)
283 const video_driver_t **list;
284 const video_driver_t *p;
286 list = (const video_driver_t **)videodriver_set.ls_items;
287 while ((p = *list++) != NULL) {
288 if (p->configure != NULL)
289 (*p->configure)(flags);
296 * Virtual frame buffer cdev driver functions
297 * The virtual frame buffer driver dispatches driver functions to
298 * appropriate subdrivers.
301 #define FB_DRIVER_NAME "fb"
303 #ifdef FB_INSTALL_CDEV
307 static devclass_t fb_devclass;
309 static int fbprobe(device_t dev);
310 static int fbattach(device_t dev);
312 static device_method_t fb_methods[] = {
313 DEVMETHOD(device_probe, fbprobe),
314 DEVMETHOD(device_attach, fbattach),
316 DEVMETHOD(bus_print_child, bus_generic_print_child),
320 static driver_t fb_driver = {
327 fbprobe(device_t dev)
331 unit = device_get_unit(dev);
332 if (unit >= adapters)
334 if (adapter[unit] == NULL)
337 device_set_desc(dev, "generic frame buffer");
342 fbattach(device_t dev)
344 printf("fbattach: about to attach children\n");
345 bus_generic_attach(dev);
349 #endif /* experimental */
351 #define FB_UNIT(dev) minor(dev)
352 #define FB_MKMINOR(unit) (u)
354 static d_open_t fbopen;
355 static d_close_t fbclose;
356 static d_read_t fbread;
357 static d_write_t fbwrite;
358 static d_ioctl_t fbioctl;
359 static d_mmap_t fbmmap;
361 #define CDEV_MAJOR 123 /* XXX */
363 static struct cdevsw fb_cdevsw = {
364 /* name */ FB_DRIVER_NAME,
365 /* maj */ CDEV_MAJOR,
377 /* strategy */ nostrategy,
385 static int fb_devsw_installed = FALSE;
387 if (!fb_devsw_installed) {
388 cdevsw_add(&fb_cdevsw);
389 fb_devsw_installed = TRUE;
393 PSEUDO_SET(vfbattach, fb);
396 * Note: dev represents the actual video device, not the frame buffer
399 fb_attach(dev_t dev, video_adapter_t *adp)
403 if (adp->va_index >= adapters)
405 if (adapter[adp->va_index] != adp)
409 adp->va_minor = minor(dev);
410 vidcdevsw[adp->va_index] = dev_dport(dev);
413 printf("fb%d at %s%d\n", adp->va_index, adp->va_name, adp->va_unit);
417 #if 0 /* never seems to be called */
419 * Note: dev represents the actual video device, not the frame buffer
422 fb_detach(dev_t dev, video_adapter_t *adp)
426 if (adp->va_index >= adapters)
428 if (adapter[adp->va_index] != adp)
430 if (vidcdevsw[adp->va_index] != port)
434 vidcdevsw[adp->va_index] = NULL;
441 fbopen(dev_t dev, int flag, int mode, struct thread *td)
446 if (unit >= adapters)
448 if (vidcdevsw[unit] == NULL)
450 return dev_port_dopen(vidcdevsw[unit],
451 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 dev_port_dclose(vidcdevsw[unit],
464 makedev(0, adapter[unit]->va_minor),
469 fbread(dev_t dev, struct uio *uio, int flag)
474 if (vidcdevsw[unit] == NULL)
476 return dev_port_dread(vidcdevsw[unit],
477 makedev(0, adapter[unit]->va_minor),
482 fbwrite(dev_t dev, struct uio *uio, int flag)
487 if (vidcdevsw[unit] == NULL)
489 return dev_port_dwrite(vidcdevsw[unit],
490 makedev(0, adapter[unit]->va_minor),
495 fbioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
500 if (vidcdevsw[unit] == NULL)
502 return dev_port_dioctl(vidcdevsw[unit],
503 makedev(0, adapter[unit]->va_minor),
508 fbmmap(dev_t dev, vm_offset_t offset, int nprot)
513 if (vidcdevsw[unit] == NULL)
515 return (dev_port_dmmap(vidcdevsw[unit],
516 makedev(0, adapter[unit]->va_minor),
521 DEV_DRIVER_MODULE(fb, ???, fb_driver, fb_devclass, fb_cdevsw, 0, 0);
525 * Generic frame buffer cdev driver functions
526 * Frame buffer subdrivers may call these functions to implement common
530 int genfbopen(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
536 if (!(sc->gfb_flags & FB_OPEN))
537 sc->gfb_flags |= FB_OPEN;
542 int genfbclose(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
548 sc->gfb_flags &= ~FB_OPEN;
553 int genfbread(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
562 size = adp->va_buffer_size/adp->va_info.vi_planes;
563 while (uio->uio_resid > 0) {
564 if (uio->uio_offset >= size)
566 offset = uio->uio_offset%adp->va_window_size;
567 len = imin(uio->uio_resid, size - uio->uio_offset);
568 len = imin(len, adp->va_window_size - offset);
571 (*vidsw[adp->va_index]->set_win_org)(adp, uio->uio_offset);
572 error = uiomove((caddr_t)(adp->va_window + offset), len, uio);
579 int genfbwrite(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
585 int genfbioctl(genfb_softc_t *sc, video_adapter_t *adp, u_long cmd,
586 caddr_t arg, int flag, struct thread *td)
590 if (adp == NULL) /* XXX */
592 error = (*vidsw[adp->va_index]->ioctl)(adp, cmd, arg);
593 if (error == ENOIOCTL)
598 int genfbmmap(genfb_softc_t *sc, video_adapter_t *adp, vm_offset_t offset,
601 return (*vidsw[adp->va_index]->mmap)(adp, offset, prot);
604 #endif /* FB_INSTALL_CDEV */
607 *adapter_name(int type)
614 { KD_HERCULES, "Hercules" },
618 { KD_PC98, "PC-98x1" },
624 for (i = 0; names[i].type != -1; ++i)
625 if (names[i].type == type)
627 return names[i].name;
631 * Generic low-level frame buffer functions
632 * The low-level functions in the frame buffer subdriver may use these
637 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
642 printf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
643 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
644 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
645 printf("%s%d: port:0x%x-0x%x, crtc:0x%x, mem:0x%x 0x%x\n",
646 FB_DRIVER_NAME, adp->va_index,
647 adp->va_io_base, adp->va_io_base + adp->va_io_size - 1,
648 adp->va_crtc_addr, adp->va_mem_base, adp->va_mem_size);
649 printf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
650 FB_DRIVER_NAME, adp->va_index,
651 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
652 printf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
653 FB_DRIVER_NAME, adp->va_index,
654 (void *)adp->va_window, (int)adp->va_window_size/1024,
655 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
656 (int)adp->va_buffer_size/1024);
660 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
666 printf("%s%d: %s, mode:%d, flags:0x%x ",
667 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
668 if (info->vi_flags & V_INFO_GRAPHICS)
669 printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
670 info->vi_width, info->vi_height,
671 info->vi_depth, info->vi_planes,
672 info->vi_cwidth, info->vi_cheight);
674 printf("T %dx%d, font:%dx%d, ",
675 info->vi_width, info->vi_height,
676 info->vi_cwidth, info->vi_cheight);
677 printf("win:0x%x\n", info->vi_window);
681 fb_type(int adp_type)
687 { FBTYPE_MDA, KD_MONO },
688 { FBTYPE_HERCULES, KD_HERCULES },
689 { FBTYPE_CGA, KD_CGA },
690 { FBTYPE_EGA, KD_EGA },
691 { FBTYPE_VGA, KD_VGA },
692 { FBTYPE_PC98, KD_PC98 },
693 { FBTYPE_TGA, KD_TGA },
697 for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i) {
698 if (types[i].va_type == adp_type)
699 return types[i].fb_type;
705 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
710 /* assert(adp != NULL) */
717 case FBIO_ADAPTER: /* get video adapter index */
718 *(int *)arg = adp->va_index;
721 case FBIO_ADPTYPE: /* get video adapter type */
722 *(int *)arg = adp->va_type;
725 case FBIO_ADPINFO: /* get video adapter info */
726 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
727 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
728 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
729 imin(strlen(adp->va_name) + 1,
730 sizeof(((video_adapter_info_t *)arg)->va_name)));
731 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
732 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
733 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
734 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
735 ((video_adapter_info_t *)arg)->va_crtc_addr = adp->va_crtc_addr;
736 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
737 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
738 ((video_adapter_info_t *)arg)->va_window
740 = vtophys(adp->va_window);
744 ((video_adapter_info_t *)arg)->va_window_size
745 = adp->va_window_size;
746 ((video_adapter_info_t *)arg)->va_window_gran
747 = adp->va_window_gran;
748 ((video_adapter_info_t *)arg)->va_window_orig
749 = adp->va_window_orig;
750 ((video_adapter_info_t *)arg)->va_unused0
752 = (adp->va_buffer) ? vtophys(adp->va_buffer) : 0;
756 ((video_adapter_info_t *)arg)->va_buffer_size
757 = adp->va_buffer_size;
758 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
759 ((video_adapter_info_t *)arg)->va_initial_mode
760 = adp->va_initial_mode;
761 ((video_adapter_info_t *)arg)->va_initial_bios_mode
762 = adp->va_initial_bios_mode;
763 ((video_adapter_info_t *)arg)->va_line_width
764 = adp->va_line_width;
765 ((video_adapter_info_t *)arg)->va_disp_start.x
766 = adp->va_disp_start.x;
767 ((video_adapter_info_t *)arg)->va_disp_start.y
768 = adp->va_disp_start.y;
771 case FBIO_MODEINFO: /* get mode information */
772 error = (*vidsw[adp->va_index]->get_info)(adp,
773 ((video_info_t *)arg)->vi_mode,
774 (video_info_t *)arg);
779 case FBIO_FINDMODE: /* find a matching video mode */
780 error = (*vidsw[adp->va_index]->query_mode)(adp,
781 (video_info_t *)arg);
784 case FBIO_GETMODE: /* get video mode */
785 *(int *)arg = adp->va_mode;
788 case FBIO_SETMODE: /* set video mode */
789 error = (*vidsw[adp->va_index]->set_mode)(adp, *(int *)arg);
791 error = ENODEV; /* EINVAL? */
794 case FBIO_GETWINORG: /* get frame buffer window origin */
795 *(u_int *)arg = adp->va_window_orig;
798 case FBIO_GETDISPSTART: /* get display start address */
799 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
800 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
803 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
804 *(u_int *)arg = adp->va_line_width;
807 case FBIO_GETPALETTE: /* get color palette */
808 case FBIO_SETPALETTE: /* set color palette */
815 case FBIO_SETWINORG: /* set frame buffer window origin */
816 case FBIO_SETDISPSTART: /* set display start address */
817 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */