4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sys/dev/fb/fb.c,v 1.11.2.2 2000/08/02 22:35:22 peter Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
43 #include <sys/linker_set.h>
44 #include <sys/device.h>
45 #include <sys/thread2.h>
52 SET_DECLARE(videodriver_set, const video_driver_t);
57 * We need at least one entry each in order to initialize a video card
58 * for the kernel console. The arrays will be increased dynamically
62 static int vid_malloc;
63 static int adapters = 1;
64 static video_adapter_t *adp_ini;
65 static video_adapter_t **adapter = &adp_ini;
66 static video_switch_t *vidsw_ini;
67 video_switch_t **vidsw = &vidsw_ini;
69 #ifdef FB_INSTALL_CDEV
70 static cdev_t vidcdevsw_ini;
71 static cdev_t *vidcdevsw = &vidcdevsw_ini;
77 vid_realloc_array(void)
79 video_adapter_t **new_adp;
80 video_switch_t **new_vidsw;
81 #ifdef FB_INSTALL_CDEV
90 newsize = ((adapters + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
91 new_adp = kmalloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
92 new_vidsw = kmalloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
94 #ifdef FB_INSTALL_CDEV
95 new_cdevsw = kmalloc(sizeof(*new_cdevsw)*newsize, M_DEVBUF,
98 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
99 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
100 #ifdef FB_INSTALL_CDEV
101 bcopy(vidcdevsw, new_cdevsw, sizeof(*vidcdevsw)*adapters);
104 kfree(adapter, M_DEVBUF);
105 kfree(vidsw, M_DEVBUF);
106 #ifdef FB_INSTALL_CDEV
107 kfree(vidcdevsw, M_DEVBUF);
112 #ifdef FB_INSTALL_CDEV
113 vidcdevsw = new_cdevsw;
119 kprintf("fb: new array size %d\n", adapters);
125 vid_malloc_init(void *arg)
130 SYSINIT(vid_mem, SI_BOOT1_POST, 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 lwkt_gettoken(&tty_token);
169 SET_FOREACH(list, videodriver_set) {
171 if (strcmp(p->name, adp->va_name) == 0) {
172 adapter[index] = adp;
173 vidsw[index] = p->vidsw;
174 lwkt_reltoken(&tty_token);
179 lwkt_reltoken(&tty_token);
184 vid_unregister(video_adapter_t *adp)
186 lwkt_gettoken(&tty_token);
187 if ((adp->va_index < 0) || (adp->va_index >= adapters)) {
188 lwkt_reltoken(&tty_token);
191 if (adapter[adp->va_index] != adp) {
192 lwkt_reltoken(&tty_token);
196 adapter[adp->va_index] = NULL;
197 vidsw[adp->va_index] = NULL;
198 lwkt_reltoken(&tty_token);
202 /* Get video I/O function table */
204 vid_get_switch(char *name)
206 const video_driver_t **list;
207 const video_driver_t *p;
209 lwkt_gettoken(&tty_token);
210 SET_FOREACH(list, videodriver_set) {
212 if (strcmp(p->name, name) == 0) {
213 lwkt_reltoken(&tty_token);
218 lwkt_reltoken(&tty_token);
223 * Video card client functions
224 * Video card clients, such as the console driver `syscons' and the frame
225 * buffer cdev driver, use these functions to claim and release a card for
229 /* find the video card specified by a driver name and a unit number */
231 vid_find_adapter(char *driver, int unit)
235 for (i = 0; i < adapters; ++i) {
236 if (adapter[i] == NULL)
238 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
240 if ((unit != -1) && (adapter[i]->va_unit != unit))
247 /* allocate a video card */
249 vid_allocate(char *driver, int unit, void *id)
254 index = vid_find_adapter(driver, unit);
256 if (adapter[index]->va_token) {
260 adapter[index]->va_token = id;
267 vid_release(video_adapter_t *adp, void *id)
272 if (adp->va_token == NULL) {
274 } else if (adp->va_token != id) {
277 adp->va_token = NULL;
284 /* Get a video adapter structure */
286 vid_get_adapter(int index)
288 if ((index < 0) || (index >= adapters))
290 return adapter[index];
293 /* Configure drivers: this is a backdoor for the console driver XXX */
295 vid_configure(int flags)
297 const video_driver_t **list;
298 const video_driver_t *p;
300 SET_FOREACH(list, videodriver_set) {
302 if (p->configure != NULL)
303 (*p->configure)(flags);
310 * Virtual frame buffer cdev driver functions
311 * The virtual frame buffer driver dispatches driver functions to
312 * appropriate subdrivers.
315 #define FB_DRIVER_NAME "fb"
317 #ifdef FB_INSTALL_CDEV
319 #if 0 /* experimental */
321 static devclass_t fb_devclass;
323 static int fbprobe(device_t dev);
324 static int fbattach(device_t dev);
326 static device_method_t fb_methods[] = {
327 DEVMETHOD(device_probe, fbprobe),
328 DEVMETHOD(device_attach, fbattach),
330 DEVMETHOD(bus_print_child, bus_generic_print_child),
334 static driver_t fb_driver = {
341 fbprobe(device_t dev)
345 unit = device_get_unit(dev);
346 if (unit >= adapters)
348 if (adapter[unit] == NULL)
351 device_set_desc(dev, "generic frame buffer");
356 fbattach(device_t dev)
358 kprintf("fbattach: about to attach children\n");
359 bus_generic_attach(dev);
363 #endif /* experimental */
365 #define FB_UNIT(dev) minor(dev)
366 #define FB_MKMINOR(unit) (u)
369 static d_default_t fboperate;
370 static d_open_t fbopen;
372 static struct dev_ops fb_ops = {
373 { FB_DRIVER_NAME, 0, 0 },
374 .d_default = fboperate,
382 static int fb_devsw_installed = FALSE;
384 if (!fb_devsw_installed) {
386 dev_ops_add(&fb_ops, 0, 0);
388 fb_devsw_installed = TRUE;
392 PSEUDO_SET(vfbattach, fb);
395 * Note: dev represents the actual video device, not the frame buffer
398 fb_attach(cdev_t dev, video_adapter_t *adp)
400 if (adp->va_index >= adapters)
402 if (adapter[adp->va_index] != adp)
407 adp->va_minor = minor(dev);
408 vidcdevsw[adp->va_index] = dev;
411 kprintf("fb%d at %s%d\n", adp->va_index, adp->va_name, adp->va_unit);
416 * Note: dev represents the actual video device, not the frame buffer
419 fb_detach(cdev_t dev, video_adapter_t *adp)
421 if (adp->va_index >= adapters)
423 if (adapter[adp->va_index] != adp)
425 if (vidcdevsw[adp->va_index] != dev)
429 vidcdevsw[adp->va_index] = NULL;
437 fbopen(struct dev_open_args *ap)
439 cdev_t dev = ap->a_head.a_dev;
444 if (unit < 0 || unit >= adapters)
446 if ((fdev = vidcdevsw[unit]) == NULL)
448 return dev_dopen(fdev, ap->a_oflags, ap->a_devtype, ap->a_cred, NULL);
452 fboperate(struct dev_generic_args *ap)
454 cdev_t dev = ap->a_dev;
459 if ((fdev = vidcdevsw[unit]) == NULL)
462 return dev_doperate(ap);
467 * Generic frame buffer cdev driver functions
468 * Frame buffer subdrivers may call these functions to implement common
472 int genfbopen(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode,
476 if (!(sc->gfb_flags & FB_OPEN))
477 sc->gfb_flags |= FB_OPEN;
482 int genfbclose(genfb_softc_t *sc, video_adapter_t *adp, int flag, int mode)
485 sc->gfb_flags &= ~FB_OPEN;
490 int genfbread(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
498 lwkt_gettoken(&tty_token);
500 size = adp->va_buffer_size/adp->va_info.vi_planes;
501 while (uio->uio_resid > 0) {
502 if (uio->uio_offset >= size)
504 offset = uio->uio_offset%adp->va_window_size;
505 len = (int)szmin(uio->uio_resid, size - uio->uio_offset);
506 len = imin(len, adp->va_window_size - offset);
509 (*vidsw[adp->va_index]->set_win_org)(adp, uio->uio_offset);
510 error = uiomove((caddr_t)(adp->va_window + offset),
515 lwkt_reltoken(&tty_token);
519 int genfbwrite(genfb_softc_t *sc, video_adapter_t *adp, struct uio *uio,
525 int genfbioctl(genfb_softc_t *sc, video_adapter_t *adp, u_long cmd,
526 caddr_t arg, int flag, struct ucred *cred)
530 if (adp == NULL) /* XXX */
532 lwkt_gettoken(&tty_token);
533 error = (*vidsw[adp->va_index]->ioctl)(adp, cmd, arg);
534 if (error == ENOIOCTL)
536 lwkt_reltoken(&tty_token);
540 int genfbmmap(genfb_softc_t *sc, video_adapter_t *adp, vm_offset_t offset,
545 lwkt_gettoken(&tty_token);
546 error = (*vidsw[adp->va_index]->mmap)(adp, offset, prot);
547 lwkt_reltoken(&tty_token);
551 #endif /* FB_INSTALL_CDEV */
554 adapter_name(int type)
561 { KD_HERCULES, "Hercules" },
570 for (i = 0; names[i].type != -1; ++i)
571 if (names[i].type == type)
573 return names[i].name;
577 * Generic low-level frame buffer functions
578 * The low-level functions in the frame buffer subdriver may use these
583 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
588 kprintf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
589 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
590 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
591 kprintf("%s%d: port:0x%x-0x%x, mem:0x%x 0x%x\n",
592 FB_DRIVER_NAME, adp->va_index,
593 adp->va_io_base, adp->va_io_base + adp->va_io_size - 1,
594 adp->va_mem_base, adp->va_mem_size);
595 kprintf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
596 FB_DRIVER_NAME, adp->va_index,
597 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
598 kprintf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
599 FB_DRIVER_NAME, adp->va_index,
600 (void *)adp->va_window, (int)adp->va_window_size/1024,
601 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
602 (int)adp->va_buffer_size/1024);
606 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
612 kprintf("%s%d: %s, mode:%d, flags:0x%x ",
613 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
614 if (info->vi_flags & V_INFO_GRAPHICS)
615 kprintf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
616 info->vi_width, info->vi_height,
617 info->vi_depth, info->vi_planes,
618 info->vi_cwidth, info->vi_cheight);
620 kprintf("T %dx%d, font:%dx%d, ",
621 info->vi_width, info->vi_height,
622 info->vi_cwidth, info->vi_cheight);
623 kprintf("win:0x%x\n", info->vi_window);
627 fb_type(int adp_type)
633 { FBTYPE_MDA, KD_MONO },
634 { FBTYPE_HERCULES, KD_HERCULES },
635 { FBTYPE_CGA, KD_CGA },
636 { FBTYPE_EGA, KD_EGA },
637 { FBTYPE_VGA, KD_VGA },
638 { FBTYPE_TGA, KD_TGA },
642 for (i = 0; i < NELEM(types); ++i) {
643 if (types[i].va_type == adp_type)
644 return types[i].fb_type;
650 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
654 /* assert(adp != NULL) */
659 lwkt_gettoken(&tty_token);
662 case FBIO_ADAPTER: /* get video adapter index */
663 *(int *)arg = adp->va_index;
666 case FBIO_ADPTYPE: /* get video adapter type */
667 *(int *)arg = adp->va_type;
670 case FBIO_ADPINFO: /* get video adapter info */
671 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
672 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
673 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
674 imin(strlen(adp->va_name) + 1,
675 sizeof(((video_adapter_info_t *)arg)->va_name)));
676 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
677 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
678 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
679 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
680 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
681 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
682 ((video_adapter_info_t *)arg)->va_window
684 = vtophys(adp->va_window);
688 ((video_adapter_info_t *)arg)->va_window_size
689 = adp->va_window_size;
690 ((video_adapter_info_t *)arg)->va_window_gran
691 = adp->va_window_gran;
692 ((video_adapter_info_t *)arg)->va_window_orig
693 = adp->va_window_orig;
694 ((video_adapter_info_t *)arg)->va_unused0
696 = (adp->va_buffer) ? vtophys(adp->va_buffer) : 0;
700 ((video_adapter_info_t *)arg)->va_buffer_size
701 = adp->va_buffer_size;
702 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
703 ((video_adapter_info_t *)arg)->va_initial_mode
704 = adp->va_initial_mode;
705 ((video_adapter_info_t *)arg)->va_initial_bios_mode
706 = adp->va_initial_bios_mode;
707 ((video_adapter_info_t *)arg)->va_line_width
708 = adp->va_line_width;
709 ((video_adapter_info_t *)arg)->va_disp_start.x
710 = adp->va_disp_start.x;
711 ((video_adapter_info_t *)arg)->va_disp_start.y
712 = adp->va_disp_start.y;
715 case FBIO_MODEINFO: /* get mode information */
716 error = (*vidsw[adp->va_index]->get_info)(adp,
717 ((video_info_t *)arg)->vi_mode,
718 (video_info_t *)arg);
723 case FBIO_FINDMODE: /* find a matching video mode */
724 error = (*vidsw[adp->va_index]->query_mode)(adp,
725 (video_info_t *)arg);
728 case FBIO_GETMODE: /* get video mode */
729 *(int *)arg = adp->va_mode;
732 case FBIO_SETMODE: /* set video mode */
733 error = (*vidsw[adp->va_index]->set_mode)(adp, *(int *)arg);
735 error = ENODEV; /* EINVAL? */
738 case FBIO_GETWINORG: /* get frame buffer window origin */
739 *(u_int *)arg = adp->va_window_orig;
742 case FBIO_GETDISPSTART: /* get display start address */
743 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
744 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
747 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
748 *(u_int *)arg = adp->va_line_width;
751 case FBIO_GETPALETTE: /* get color palette */
752 case FBIO_SETPALETTE: /* set color palette */
759 case FBIO_SETWINORG: /* set frame buffer window origin */
760 case FBIO_SETDISPSTART: /* set display start address */
761 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */
779 lwkt_reltoken(&tty_token);