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.7 2003/11/10 06:12:09 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/linker_set.h>
43 #include <sys/device.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 lwkt_port *vidcdevsw_ini;
69 static struct lwkt_port **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 lwkt_port **new_cdevsw;
89 newsize = ((adapters + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
90 new_adp = malloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK);
91 new_vidsw = malloc(sizeof(*new_vidsw)*newsize, M_DEVBUF, M_WAITOK);
92 #ifdef FB_INSTALL_CDEV
93 new_cdevsw = malloc(sizeof(*new_cdevsw)*newsize, M_DEVBUF, M_WAITOK);
95 bzero(new_adp, sizeof(*new_adp)*newsize);
96 bzero(new_vidsw, sizeof(*new_vidsw)*newsize);
97 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
98 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
99 #ifdef FB_INSTALL_CDEV
100 bzero(new_cdevsw, sizeof(*new_cdevsw)*newsize);
101 bcopy(vidcdevsw, new_cdevsw, sizeof(*vidcdevsw)*adapters);
104 free(adapter, M_DEVBUF);
105 free(vidsw, M_DEVBUF);
106 #ifdef FB_INSTALL_CDEV
107 free(vidcdevsw, M_DEVBUF);
112 #ifdef FB_INSTALL_CDEV
113 vidcdevsw = new_cdevsw;
119 printf("fb: new array size %d\n", adapters);
125 vid_malloc_init(void *arg)
130 SYSINIT(vid_mem, SI_SUB_KMEM, SI_ORDER_ANY, vid_malloc_init, NULL);
133 * Low-level frame buffer driver functions
134 * frame buffer subdrivers, such as the VGA driver, call these functions
135 * to initialize the video_adapter structure and register it to the virtual
136 * frame buffer driver `fb'.
139 /* initialize the video_adapter_t structure */
141 vid_init_struct(video_adapter_t *adp, char *name, int type, int unit)
149 /* Register a video adapter */
151 vid_register(video_adapter_t *adp)
153 const video_driver_t **list;
154 const video_driver_t *p;
157 for (index = 0; index < adapters; ++index) {
158 if (adapter[index] == NULL)
161 if (index >= adapters) {
162 if (vid_realloc_array())
166 adp->va_index = index;
167 adp->va_token = NULL;
168 SET_FOREACH(list, videodriver_set) {
170 if (strcmp(p->name, adp->va_name) == 0) {
171 adapter[index] = adp;
172 vidsw[index] = p->vidsw;
181 vid_unregister(video_adapter_t *adp)
183 if ((adp->va_index < 0) || (adp->va_index >= adapters))
185 if (adapter[adp->va_index] != adp)
188 adapter[adp->va_index] = NULL;
189 vidsw[adp->va_index] = NULL;
193 /* Get video I/O function table */
195 *vid_get_switch(char *name)
197 const video_driver_t **list;
198 const video_driver_t *p;
200 SET_FOREACH(list, videodriver_set) {
202 if (strcmp(p->name, name) == 0)
210 * Video card client functions
211 * Video card clients, such as the console driver `syscons' and the frame
212 * buffer cdev driver, use these functions to claim and release a card for
216 /* find the video card specified by a driver name and a unit number */
218 vid_find_adapter(char *driver, int unit)
222 for (i = 0; i < adapters; ++i) {
223 if (adapter[i] == NULL)
225 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
227 if ((unit != -1) && (adapter[i]->va_unit != unit))
234 /* allocate a video card */
236 vid_allocate(char *driver, int unit, void *id)
242 index = vid_find_adapter(driver, unit);
244 if (adapter[index]->va_token) {
248 adapter[index]->va_token = id;
255 vid_release(video_adapter_t *adp, void *id)
261 if (adp->va_token == NULL) {
263 } else if (adp->va_token != id) {
266 adp->va_token = NULL;
273 /* Get a video adapter structure */
275 *vid_get_adapter(int index)
277 if ((index < 0) || (index >= adapters))
279 return adapter[index];
282 /* Configure drivers: this is a backdoor for the console driver XXX */
284 vid_configure(int flags)
286 const video_driver_t **list;
287 const video_driver_t *p;
289 SET_FOREACH(list, videodriver_set) {
291 if (p->configure != NULL)
292 (*p->configure)(flags);
299 * Virtual frame buffer cdev driver functions
300 * The virtual frame buffer driver dispatches driver functions to
301 * appropriate subdrivers.
304 #define FB_DRIVER_NAME "fb"
306 #ifdef FB_INSTALL_CDEV
310 static devclass_t fb_devclass;
312 static int fbprobe(device_t dev);
313 static int fbattach(device_t dev);
315 static device_method_t fb_methods[] = {
316 DEVMETHOD(device_probe, fbprobe),
317 DEVMETHOD(device_attach, fbattach),
319 DEVMETHOD(bus_print_child, bus_generic_print_child),
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);
352 #endif /* experimental */
354 #define FB_UNIT(dev) minor(dev)
355 #define FB_MKMINOR(unit) (u)
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;
364 #define CDEV_MAJOR 123 /* XXX */
366 static struct cdevsw fb_cdevsw = {
367 /* name */ FB_DRIVER_NAME,
368 /* maj */ CDEV_MAJOR,
380 /* strategy */ nostrategy,
388 static int fb_devsw_installed = FALSE;
390 if (!fb_devsw_installed) {
391 cdevsw_add(&fb_cdevsw);
392 fb_devsw_installed = TRUE;
396 PSEUDO_SET(vfbattach, fb);
399 * Note: dev represents the actual video device, not the frame buffer
402 fb_attach(dev_t dev, video_adapter_t *adp)
406 if (adp->va_index >= adapters)
408 if (adapter[adp->va_index] != adp)
412 adp->va_minor = minor(dev);
413 vidcdevsw[adp->va_index] = dev_dport(dev);
416 printf("fb%d at %s%d\n", adp->va_index, adp->va_name, adp->va_unit);
420 #if 0 /* never seems to be called */
422 * Note: dev represents the actual video device, not the frame buffer
425 fb_detach(dev_t dev, video_adapter_t *adp)
429 if (adp->va_index >= adapters)
431 if (adapter[adp->va_index] != adp)
433 if (vidcdevsw[adp->va_index] != port)
437 vidcdevsw[adp->va_index] = NULL;
444 fbopen(dev_t dev, int flag, int mode, struct thread *td)
449 if (unit >= adapters)
451 if (vidcdevsw[unit] == NULL)
453 return dev_port_dopen(vidcdevsw[unit],
454 makedev(0, adapter[unit]->va_minor),
459 fbclose(dev_t dev, int flag, int mode, struct thread *td)
464 if (vidcdevsw[unit] == NULL)
466 return dev_port_dclose(vidcdevsw[unit],
467 makedev(0, adapter[unit]->va_minor),
472 fbread(dev_t dev, struct uio *uio, int flag)
477 if (vidcdevsw[unit] == NULL)
479 return dev_port_dread(vidcdevsw[unit],
480 makedev(0, adapter[unit]->va_minor),
485 fbwrite(dev_t dev, struct uio *uio, int flag)
490 if (vidcdevsw[unit] == NULL)
492 return dev_port_dwrite(vidcdevsw[unit],
493 makedev(0, adapter[unit]->va_minor),
498 fbioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
503 if (vidcdevsw[unit] == NULL)
505 return dev_port_dioctl(vidcdevsw[unit],
506 makedev(0, adapter[unit]->va_minor),
511 fbmmap(dev_t dev, vm_offset_t offset, int nprot)
516 if (vidcdevsw[unit] == NULL)
518 return (dev_port_dmmap(vidcdevsw[unit],
519 makedev(0, adapter[unit]->va_minor),
524 DEV_DRIVER_MODULE(fb, ???, fb_driver, fb_devclass, fb_cdevsw, 0, 0);
528 * Generic frame buffer cdev driver functions
529 * Frame buffer subdrivers may call these functions to implement common
533 int genfbopen(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
539 if (!(sc->gfb_flags & FB_OPEN))
540 sc->gfb_flags |= FB_OPEN;
545 int genfbclose(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
551 sc->gfb_flags &= ~FB_OPEN;
556 int genfbread(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
565 size = adp->va_buffer_size/adp->va_info.vi_planes;
566 while (uio->uio_resid > 0) {
567 if (uio->uio_offset >= size)
569 offset = uio->uio_offset%adp->va_window_size;
570 len = imin(uio->uio_resid, size - uio->uio_offset);
571 len = imin(len, adp->va_window_size - offset);
574 (*vidsw[adp->va_index]->set_win_org)(adp, uio->uio_offset);
575 error = uiomove((caddr_t)(adp->va_window + offset), len, uio);
582 int genfbwrite(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
588 int genfbioctl(genfb_softc_t *sc, video_adapter_t *adp, u_long cmd,
589 caddr_t arg, int flag, struct thread *td)
593 if (adp == NULL) /* XXX */
595 error = (*vidsw[adp->va_index]->ioctl)(adp, cmd, arg);
596 if (error == ENOIOCTL)
601 int genfbmmap(genfb_softc_t *sc, video_adapter_t *adp, vm_offset_t offset,
604 return (*vidsw[adp->va_index]->mmap)(adp, offset, prot);
607 #endif /* FB_INSTALL_CDEV */
610 *adapter_name(int type)
617 { KD_HERCULES, "Hercules" },
621 { KD_PC98, "PC-98x1" },
627 for (i = 0; names[i].type != -1; ++i)
628 if (names[i].type == type)
630 return names[i].name;
634 * Generic low-level frame buffer functions
635 * The low-level functions in the frame buffer subdriver may use these
640 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
645 printf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
646 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
647 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
648 printf("%s%d: port:0x%x-0x%x, crtc:0x%x, mem:0x%x 0x%x\n",
649 FB_DRIVER_NAME, adp->va_index,
650 adp->va_io_base, adp->va_io_base + adp->va_io_size - 1,
651 adp->va_crtc_addr, adp->va_mem_base, adp->va_mem_size);
652 printf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
653 FB_DRIVER_NAME, adp->va_index,
654 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
655 printf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
656 FB_DRIVER_NAME, adp->va_index,
657 (void *)adp->va_window, (int)adp->va_window_size/1024,
658 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
659 (int)adp->va_buffer_size/1024);
663 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
669 printf("%s%d: %s, mode:%d, flags:0x%x ",
670 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
671 if (info->vi_flags & V_INFO_GRAPHICS)
672 printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
673 info->vi_width, info->vi_height,
674 info->vi_depth, info->vi_planes,
675 info->vi_cwidth, info->vi_cheight);
677 printf("T %dx%d, font:%dx%d, ",
678 info->vi_width, info->vi_height,
679 info->vi_cwidth, info->vi_cheight);
680 printf("win:0x%x\n", info->vi_window);
684 fb_type(int adp_type)
690 { FBTYPE_MDA, KD_MONO },
691 { FBTYPE_HERCULES, KD_HERCULES },
692 { FBTYPE_CGA, KD_CGA },
693 { FBTYPE_EGA, KD_EGA },
694 { FBTYPE_VGA, KD_VGA },
695 { FBTYPE_PC98, KD_PC98 },
696 { FBTYPE_TGA, KD_TGA },
700 for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i) {
701 if (types[i].va_type == adp_type)
702 return types[i].fb_type;
708 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
713 /* assert(adp != NULL) */
720 case FBIO_ADAPTER: /* get video adapter index */
721 *(int *)arg = adp->va_index;
724 case FBIO_ADPTYPE: /* get video adapter type */
725 *(int *)arg = adp->va_type;
728 case FBIO_ADPINFO: /* get video adapter info */
729 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
730 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
731 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
732 imin(strlen(adp->va_name) + 1,
733 sizeof(((video_adapter_info_t *)arg)->va_name)));
734 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
735 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
736 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
737 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
738 ((video_adapter_info_t *)arg)->va_crtc_addr = adp->va_crtc_addr;
739 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
740 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
741 ((video_adapter_info_t *)arg)->va_window
743 = vtophys(adp->va_window);
747 ((video_adapter_info_t *)arg)->va_window_size
748 = adp->va_window_size;
749 ((video_adapter_info_t *)arg)->va_window_gran
750 = adp->va_window_gran;
751 ((video_adapter_info_t *)arg)->va_window_orig
752 = adp->va_window_orig;
753 ((video_adapter_info_t *)arg)->va_unused0
755 = (adp->va_buffer) ? vtophys(adp->va_buffer) : 0;
759 ((video_adapter_info_t *)arg)->va_buffer_size
760 = adp->va_buffer_size;
761 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
762 ((video_adapter_info_t *)arg)->va_initial_mode
763 = adp->va_initial_mode;
764 ((video_adapter_info_t *)arg)->va_initial_bios_mode
765 = adp->va_initial_bios_mode;
766 ((video_adapter_info_t *)arg)->va_line_width
767 = adp->va_line_width;
768 ((video_adapter_info_t *)arg)->va_disp_start.x
769 = adp->va_disp_start.x;
770 ((video_adapter_info_t *)arg)->va_disp_start.y
771 = adp->va_disp_start.y;
774 case FBIO_MODEINFO: /* get mode information */
775 error = (*vidsw[adp->va_index]->get_info)(adp,
776 ((video_info_t *)arg)->vi_mode,
777 (video_info_t *)arg);
782 case FBIO_FINDMODE: /* find a matching video mode */
783 error = (*vidsw[adp->va_index]->query_mode)(adp,
784 (video_info_t *)arg);
787 case FBIO_GETMODE: /* get video mode */
788 *(int *)arg = adp->va_mode;
791 case FBIO_SETMODE: /* set video mode */
792 error = (*vidsw[adp->va_index]->set_mode)(adp, *(int *)arg);
794 error = ENODEV; /* EINVAL? */
797 case FBIO_GETWINORG: /* get frame buffer window origin */
798 *(u_int *)arg = adp->va_window_orig;
801 case FBIO_GETDISPSTART: /* get display start address */
802 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
803 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
806 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
807 *(u_int *)arg = adp->va_line_width;
810 case FBIO_GETPALETTE: /* get color palette */
811 case FBIO_SETPALETTE: /* set color palette */
818 case FBIO_SETWINORG: /* set frame buffer window origin */
819 case FBIO_SETDISPSTART: /* set display start address */
820 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */