2 * SPDX-License-Identifier: BSD-3-Clause
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
45 #include <sys/linker_set.h>
50 #include <dev/fb/fbreg.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;
72 vid_realloc_array(void)
74 video_adapter_t **new_adp;
75 video_switch_t **new_vidsw;
83 newsize = rounddown(adapters + ARRAY_DELTA, ARRAY_DELTA);
84 new_adp = malloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
85 new_vidsw = malloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
87 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
88 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
90 free(adapter, M_DEVBUF);
91 free(vidsw, M_DEVBUF);
99 printf("fb: new array size %d\n", adapters);
105 vid_malloc_init(void *arg)
110 SYSINIT(vid_mem, SI_SUB_KMEM, SI_ORDER_ANY, vid_malloc_init, NULL);
113 * Low-level frame buffer driver functions
114 * frame buffer subdrivers, such as the VGA driver, call these functions
115 * to initialize the video_adapter structure and register it to the virtual
116 * frame buffer driver `fb'.
119 /* initialize the video_adapter_t structure */
121 vid_init_struct(video_adapter_t *adp, char *name, int type, int unit)
129 /* Register a video adapter */
131 vid_register(video_adapter_t *adp)
133 const video_driver_t **list;
134 const video_driver_t *p;
137 for (index = 0; index < adapters; ++index) {
138 if (adapter[index] == NULL)
141 if (index >= adapters) {
142 if (vid_realloc_array())
146 adp->va_index = index;
147 adp->va_token = NULL;
148 SET_FOREACH(list, videodriver_set) {
150 if (strcmp(p->name, adp->va_name) == 0) {
151 adapter[index] = adp;
152 vidsw[index] = p->vidsw;
161 vid_unregister(video_adapter_t *adp)
163 if ((adp->va_index < 0) || (adp->va_index >= adapters))
165 if (adapter[adp->va_index] != adp)
168 adapter[adp->va_index] = NULL;
169 vidsw[adp->va_index] = NULL;
173 /* Get video I/O function table */
175 *vid_get_switch(char *name)
177 const video_driver_t **list;
178 const video_driver_t *p;
180 SET_FOREACH(list, videodriver_set) {
182 if (strcmp(p->name, name) == 0)
190 * Video card client functions
191 * Video card clients, such as the console driver `syscons' and the frame
192 * buffer cdev driver, use these functions to claim and release a card for
196 /* find the video card specified by a driver name and a unit number */
198 vid_find_adapter(char *driver, int unit)
202 for (i = 0; i < adapters; ++i) {
203 if (adapter[i] == NULL)
205 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
207 if ((unit != -1) && (adapter[i]->va_unit != unit))
214 /* allocate a video card */
216 vid_allocate(char *driver, int unit, void *id)
222 index = vid_find_adapter(driver, unit);
224 if (adapter[index]->va_token) {
228 adapter[index]->va_token = id;
235 vid_release(video_adapter_t *adp, void *id)
241 if (adp->va_token == NULL) {
243 } else if (adp->va_token != id) {
246 adp->va_token = NULL;
253 /* Get a video adapter structure */
255 *vid_get_adapter(int index)
257 if ((index < 0) || (index >= adapters))
259 return adapter[index];
262 /* Configure drivers: this is a backdoor for the console driver XXX */
264 vid_configure(int flags)
266 const video_driver_t **list;
267 const video_driver_t *p;
269 SET_FOREACH(list, videodriver_set) {
271 if (p->configure != NULL)
272 (*p->configure)(flags);
278 #define FB_DRIVER_NAME "fb"
281 *adapter_name(int type)
288 { KD_HERCULES, "Hercules" },
297 for (i = 0; names[i].type != -1; ++i)
298 if (names[i].type == type)
300 return names[i].name;
304 * Generic low-level frame buffer functions
305 * The low-level functions in the frame buffer subdriver may use these
310 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
315 printf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
316 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
317 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
318 printf("%s%d: port:0x%lx-0x%lx, crtc:0x%lx, mem:0x%lx 0x%x\n",
319 FB_DRIVER_NAME, adp->va_index, (u_long)adp->va_io_base,
320 (u_long)adp->va_io_base + adp->va_io_size - 1,
321 (u_long)adp->va_crtc_addr, (u_long)adp->va_mem_base,
323 printf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
324 FB_DRIVER_NAME, adp->va_index,
325 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
326 printf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
327 FB_DRIVER_NAME, adp->va_index,
328 (void *)adp->va_window, (int)adp->va_window_size/1024,
329 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
330 (int)adp->va_buffer_size/1024);
334 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
340 printf("%s%d: %s, mode:%d, flags:0x%x ",
341 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
342 if (info->vi_flags & V_INFO_GRAPHICS)
343 printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
344 info->vi_width, info->vi_height,
345 info->vi_depth, info->vi_planes,
346 info->vi_cwidth, info->vi_cheight);
348 printf("T %dx%d, font:%dx%d, ",
349 info->vi_width, info->vi_height,
350 info->vi_cwidth, info->vi_cheight);
351 printf("win:0x%lx\n", (u_long)info->vi_window);
355 fb_type(int adp_type)
361 { FBTYPE_MDA, KD_MONO },
362 { FBTYPE_HERCULES, KD_HERCULES },
363 { FBTYPE_CGA, KD_CGA },
364 { FBTYPE_EGA, KD_EGA },
365 { FBTYPE_VGA, KD_VGA },
366 { FBTYPE_TGA, KD_TGA },
370 for (i = 0; i < nitems(types); ++i) {
371 if (types[i].va_type == adp_type)
372 return types[i].fb_type;
378 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
383 /* assert(adp != NULL) */
390 case FBIO_ADAPTER: /* get video adapter index */
391 *(int *)arg = adp->va_index;
394 case FBIO_ADPTYPE: /* get video adapter type */
395 *(int *)arg = adp->va_type;
398 case FBIO_ADPINFO: /* get video adapter info */
399 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
400 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
401 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
402 imin(strlen(adp->va_name) + 1,
403 sizeof(((video_adapter_info_t *)arg)->va_name)));
404 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
405 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
406 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
407 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
408 ((video_adapter_info_t *)arg)->va_crtc_addr = adp->va_crtc_addr;
409 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
410 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
411 ((video_adapter_info_t *)arg)->va_window
412 #if defined(__amd64__) || defined(__i386__)
413 = vtophys(adp->va_window);
417 ((video_adapter_info_t *)arg)->va_window_size
418 = adp->va_window_size;
419 ((video_adapter_info_t *)arg)->va_window_gran
420 = adp->va_window_gran;
421 ((video_adapter_info_t *)arg)->va_window_orig
422 = adp->va_window_orig;
423 ((video_adapter_info_t *)arg)->va_unused0
424 #if defined(__amd64__) || defined(__i386__)
425 = adp->va_buffer != 0 ? vtophys(adp->va_buffer) : 0;
429 ((video_adapter_info_t *)arg)->va_buffer_size
430 = adp->va_buffer_size;
431 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
432 ((video_adapter_info_t *)arg)->va_initial_mode
433 = adp->va_initial_mode;
434 ((video_adapter_info_t *)arg)->va_initial_bios_mode
435 = adp->va_initial_bios_mode;
436 ((video_adapter_info_t *)arg)->va_line_width
437 = adp->va_line_width;
438 ((video_adapter_info_t *)arg)->va_disp_start.x
439 = adp->va_disp_start.x;
440 ((video_adapter_info_t *)arg)->va_disp_start.y
441 = adp->va_disp_start.y;
444 case FBIO_MODEINFO: /* get mode information */
445 error = vidd_get_info(adp,
446 ((video_info_t *)arg)->vi_mode,
447 (video_info_t *)arg);
452 case FBIO_FINDMODE: /* find a matching video mode */
453 error = vidd_query_mode(adp, (video_info_t *)arg);
456 case FBIO_GETMODE: /* get video mode */
457 *(int *)arg = adp->va_mode;
460 case FBIO_SETMODE: /* set video mode */
461 error = vidd_set_mode(adp, *(int *)arg);
463 error = ENODEV; /* EINVAL? */
466 case FBIO_GETWINORG: /* get frame buffer window origin */
467 *(u_int *)arg = adp->va_window_orig;
470 case FBIO_GETDISPSTART: /* get display start address */
471 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
472 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
475 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
476 *(u_int *)arg = adp->va_line_width;
479 case FBIO_BLANK: /* blank display */
480 error = vidd_blank_display(adp, *(int *)arg);
483 case FBIO_GETPALETTE: /* get color palette */
484 case FBIO_SETPALETTE: /* set color palette */
491 case FBIO_SETWINORG: /* set frame buffer window origin */
492 case FBIO_SETDISPSTART: /* set display start address */
493 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */