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.16 2006/09/10 01:26:37 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>
44 #include <sys/thread2.h>
51 SET_DECLARE(videodriver_set, const video_driver_t);
56 * We need at least one entry each in order to initialize a video card
57 * for the kernel console. The arrays will be increased dynamically
61 static int vid_malloc;
62 static int adapters = 1;
63 static video_adapter_t *adp_ini;
64 static video_adapter_t **adapter = &adp_ini;
65 static video_switch_t *vidsw_ini;
66 video_switch_t **vidsw = &vidsw_ini;
68 #ifdef FB_INSTALL_CDEV
69 static cdev_t vidcdevsw_ini;
70 static cdev_t *vidcdevsw = &vidcdevsw_ini;
76 vid_realloc_array(void)
78 video_adapter_t **new_adp;
79 video_switch_t **new_vidsw;
80 #ifdef FB_INSTALL_CDEV
89 newsize = ((adapters + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
90 new_adp = kmalloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
91 new_vidsw = kmalloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
93 #ifdef FB_INSTALL_CDEV
94 new_cdevsw = kmalloc(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 kfree(adapter, M_DEVBUF);
104 kfree(vidsw, M_DEVBUF);
105 #ifdef FB_INSTALL_CDEV
106 kfree(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)
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)
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 SET_FOREACH(list, videodriver_set) {
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_default_t fboperate;
355 static d_open_t fbopen;
357 #define CDEV_MAJOR 123 /* XXX */
359 static struct dev_ops fb_ops = {
360 { FB_DRIVER_NAME, CDEV_MAJOR, 0 },
361 .d_default = fboperate,
368 static int fb_devsw_installed = FALSE;
370 if (!fb_devsw_installed) {
371 dev_ops_add(&fb_ops, 0, 0);
372 fb_devsw_installed = TRUE;
376 PSEUDO_SET(vfbattach, fb);
379 * Note: dev represents the actual video device, not the frame buffer
382 fb_attach(cdev_t dev, video_adapter_t *adp)
384 if (adp->va_index >= adapters)
386 if (adapter[adp->va_index] != adp)
391 adp->va_minor = minor(dev);
392 vidcdevsw[adp->va_index] = dev;
395 printf("fb%d at %s%d\n", adp->va_index, adp->va_name, adp->va_unit);
400 * Note: dev represents the actual video device, not the frame buffer
403 fb_detach(cdev_t dev, video_adapter_t *adp)
405 if (adp->va_index >= adapters)
407 if (adapter[adp->va_index] != adp)
409 if (vidcdevsw[adp->va_index] != dev)
413 vidcdevsw[adp->va_index] = NULL;
420 fbopen(struct dev_open_args *ap)
422 cdev_t dev = ap->a_head.a_dev;
427 if (unit < 0 || unit >= adapters)
429 if ((fdev = vidcdevsw[unit]) == NULL)
431 return dev_dopen(fdev, ap->a_oflags, ap->a_devtype, ap->a_cred);
435 fboperate(struct dev_generic_args *ap)
437 cdev_t dev = ap->a_dev;
442 if ((fdev = vidcdevsw[unit]) == NULL)
445 return dev_doperate(ap);
449 * Generic frame buffer cdev driver functions
450 * Frame buffer subdrivers may call these functions to implement common
454 int genfbopen(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
458 if (!(sc->gfb_flags & FB_OPEN))
459 sc->gfb_flags |= FB_OPEN;
464 int genfbclose(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode)
467 sc->gfb_flags &= ~FB_OPEN;
472 int genfbread(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
481 size = adp->va_buffer_size/adp->va_info.vi_planes;
482 while (uio->uio_resid > 0) {
483 if (uio->uio_offset >= size)
485 offset = uio->uio_offset%adp->va_window_size;
486 len = imin(uio->uio_resid, size - uio->uio_offset);
487 len = imin(len, adp->va_window_size - offset);
490 (*vidsw[adp->va_index]->set_win_org)(adp, uio->uio_offset);
491 error = uiomove((caddr_t)(adp->va_window + offset), len, uio);
498 int genfbwrite(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
504 int genfbioctl(genfb_softc_t *sc, video_adapter_t *adp, u_long cmd,
505 caddr_t arg, int flag, struct ucred *cred)
509 if (adp == NULL) /* XXX */
511 error = (*vidsw[adp->va_index]->ioctl)(adp, cmd, arg);
512 if (error == ENOIOCTL)
517 int genfbmmap(genfb_softc_t *sc, video_adapter_t *adp, vm_offset_t offset,
520 return (*vidsw[adp->va_index]->mmap)(adp, offset, prot);
523 #endif /* FB_INSTALL_CDEV */
526 adapter_name(int type)
533 { KD_HERCULES, "Hercules" },
542 for (i = 0; names[i].type != -1; ++i)
543 if (names[i].type == type)
545 return names[i].name;
549 * Generic low-level frame buffer functions
550 * The low-level functions in the frame buffer subdriver may use these
555 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
560 printf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
561 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
562 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
563 printf("%s%d: port:0x%x-0x%x, crtc:0x%x, mem:0x%x 0x%x\n",
564 FB_DRIVER_NAME, adp->va_index,
565 adp->va_io_base, adp->va_io_base + adp->va_io_size - 1,
566 adp->va_crtc_addr, adp->va_mem_base, adp->va_mem_size);
567 printf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
568 FB_DRIVER_NAME, adp->va_index,
569 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
570 printf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
571 FB_DRIVER_NAME, adp->va_index,
572 (void *)adp->va_window, (int)adp->va_window_size/1024,
573 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
574 (int)adp->va_buffer_size/1024);
578 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
584 printf("%s%d: %s, mode:%d, flags:0x%x ",
585 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
586 if (info->vi_flags & V_INFO_GRAPHICS)
587 printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
588 info->vi_width, info->vi_height,
589 info->vi_depth, info->vi_planes,
590 info->vi_cwidth, info->vi_cheight);
592 printf("T %dx%d, font:%dx%d, ",
593 info->vi_width, info->vi_height,
594 info->vi_cwidth, info->vi_cheight);
595 printf("win:0x%x\n", info->vi_window);
599 fb_type(int adp_type)
605 { FBTYPE_MDA, KD_MONO },
606 { FBTYPE_HERCULES, KD_HERCULES },
607 { FBTYPE_CGA, KD_CGA },
608 { FBTYPE_EGA, KD_EGA },
609 { FBTYPE_VGA, KD_VGA },
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)
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
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
668 = (adp->va_buffer) ? 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 = (*vidsw[adp->va_index]->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 = (*vidsw[adp->va_index]->query_mode)(adp,
697 (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 = (*vidsw[adp->va_index]->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_GETPALETTE: /* get color palette */
724 case FBIO_SETPALETTE: /* set color palette */
731 case FBIO_SETWINORG: /* set frame buffer window origin */
732 case FBIO_SETDISPSTART: /* set display start address */
733 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */