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>
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
43 #include <sys/linker_set.h>
48 #include <dev/fb/fbreg.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;
70 vid_realloc_array(void)
72 video_adapter_t **new_adp;
73 video_switch_t **new_vidsw;
81 newsize = rounddown(adapters + ARRAY_DELTA, ARRAY_DELTA);
82 new_adp = malloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
83 new_vidsw = malloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
85 bcopy(adapter, new_adp, sizeof(*adapter)*adapters);
86 bcopy(vidsw, new_vidsw, sizeof(*vidsw)*adapters);
88 free(adapter, M_DEVBUF);
89 free(vidsw, M_DEVBUF);
97 printf("fb: new array size %d\n", adapters);
103 vid_malloc_init(void *arg)
108 SYSINIT(vid_mem, SI_SUB_KMEM, SI_ORDER_ANY, vid_malloc_init, NULL);
111 * Low-level frame buffer driver functions
112 * frame buffer subdrivers, such as the VGA driver, call these functions
113 * to initialize the video_adapter structure and register it to the virtual
114 * frame buffer driver `fb'.
117 /* initialize the video_adapter_t structure */
119 vid_init_struct(video_adapter_t *adp, char *name, int type, int unit)
127 /* Register a video adapter */
129 vid_register(video_adapter_t *adp)
131 const video_driver_t **list;
132 const video_driver_t *p;
135 for (index = 0; index < adapters; ++index) {
136 if (adapter[index] == NULL)
139 if (index >= adapters) {
140 if (vid_realloc_array())
144 adp->va_index = index;
145 adp->va_token = NULL;
146 SET_FOREACH(list, videodriver_set) {
148 if (strcmp(p->name, adp->va_name) == 0) {
149 adapter[index] = adp;
150 vidsw[index] = p->vidsw;
159 vid_unregister(video_adapter_t *adp)
161 if ((adp->va_index < 0) || (adp->va_index >= adapters))
163 if (adapter[adp->va_index] != adp)
166 adapter[adp->va_index] = NULL;
167 vidsw[adp->va_index] = NULL;
171 /* Get video I/O function table */
173 *vid_get_switch(char *name)
175 const video_driver_t **list;
176 const video_driver_t *p;
178 SET_FOREACH(list, videodriver_set) {
180 if (strcmp(p->name, name) == 0)
188 * Video card client functions
189 * Video card clients, such as the console driver `syscons' and the frame
190 * buffer cdev driver, use these functions to claim and release a card for
194 /* find the video card specified by a driver name and a unit number */
196 vid_find_adapter(char *driver, int unit)
200 for (i = 0; i < adapters; ++i) {
201 if (adapter[i] == NULL)
203 if (strcmp("*", driver) && strcmp(adapter[i]->va_name, driver))
205 if ((unit != -1) && (adapter[i]->va_unit != unit))
212 /* allocate a video card */
214 vid_allocate(char *driver, int unit, void *id)
220 index = vid_find_adapter(driver, unit);
222 if (adapter[index]->va_token) {
226 adapter[index]->va_token = id;
233 vid_release(video_adapter_t *adp, void *id)
239 if (adp->va_token == NULL) {
241 } else if (adp->va_token != id) {
244 adp->va_token = NULL;
251 /* Get a video adapter structure */
253 *vid_get_adapter(int index)
255 if ((index < 0) || (index >= adapters))
257 return adapter[index];
260 /* Configure drivers: this is a backdoor for the console driver XXX */
262 vid_configure(int flags)
264 const video_driver_t **list;
265 const video_driver_t *p;
267 SET_FOREACH(list, videodriver_set) {
269 if (p->configure != NULL)
270 (*p->configure)(flags);
276 #define FB_DRIVER_NAME "fb"
279 *adapter_name(int type)
286 { KD_HERCULES, "Hercules" },
295 for (i = 0; names[i].type != -1; ++i)
296 if (names[i].type == type)
298 return names[i].name;
302 * Generic low-level frame buffer functions
303 * The low-level functions in the frame buffer subdriver may use these
308 fb_dump_adp_info(char *driver, video_adapter_t *adp, int level)
313 printf("%s%d: %s%d, %s, type:%s (%d), flags:0x%x\n",
314 FB_DRIVER_NAME, adp->va_index, driver, adp->va_unit, adp->va_name,
315 adapter_name(adp->va_type), adp->va_type, adp->va_flags);
316 printf("%s%d: port:0x%lx-0x%lx, crtc:0x%lx, mem:0x%lx 0x%x\n",
317 FB_DRIVER_NAME, adp->va_index, (u_long)adp->va_io_base,
318 (u_long)adp->va_io_base + adp->va_io_size - 1,
319 (u_long)adp->va_crtc_addr, (u_long)adp->va_mem_base,
321 printf("%s%d: init mode:%d, bios mode:%d, current mode:%d\n",
322 FB_DRIVER_NAME, adp->va_index,
323 adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
324 printf("%s%d: window:%p size:%dk gran:%dk, buf:%p size:%dk\n",
325 FB_DRIVER_NAME, adp->va_index,
326 (void *)adp->va_window, (int)adp->va_window_size/1024,
327 (int)adp->va_window_gran/1024, (void *)adp->va_buffer,
328 (int)adp->va_buffer_size/1024);
332 fb_dump_mode_info(char *driver, video_adapter_t *adp, video_info_t *info,
338 printf("%s%d: %s, mode:%d, flags:0x%x ",
339 driver, adp->va_unit, adp->va_name, info->vi_mode, info->vi_flags);
340 if (info->vi_flags & V_INFO_GRAPHICS)
341 printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
342 info->vi_width, info->vi_height,
343 info->vi_depth, info->vi_planes,
344 info->vi_cwidth, info->vi_cheight);
346 printf("T %dx%d, font:%dx%d, ",
347 info->vi_width, info->vi_height,
348 info->vi_cwidth, info->vi_cheight);
349 printf("win:0x%lx\n", (u_long)info->vi_window);
353 fb_type(int adp_type)
359 { FBTYPE_MDA, KD_MONO },
360 { FBTYPE_HERCULES, KD_HERCULES },
361 { FBTYPE_CGA, KD_CGA },
362 { FBTYPE_EGA, KD_EGA },
363 { FBTYPE_VGA, KD_VGA },
364 { FBTYPE_TGA, KD_TGA },
368 for (i = 0; i < nitems(types); ++i) {
369 if (types[i].va_type == adp_type)
370 return types[i].fb_type;
376 fb_commonioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
381 /* assert(adp != NULL) */
388 case FBIO_ADAPTER: /* get video adapter index */
389 *(int *)arg = adp->va_index;
392 case FBIO_ADPTYPE: /* get video adapter type */
393 *(int *)arg = adp->va_type;
396 case FBIO_ADPINFO: /* get video adapter info */
397 ((video_adapter_info_t *)arg)->va_index = adp->va_index;
398 ((video_adapter_info_t *)arg)->va_type = adp->va_type;
399 bcopy(adp->va_name, ((video_adapter_info_t *)arg)->va_name,
400 imin(strlen(adp->va_name) + 1,
401 sizeof(((video_adapter_info_t *)arg)->va_name)));
402 ((video_adapter_info_t *)arg)->va_unit = adp->va_unit;
403 ((video_adapter_info_t *)arg)->va_flags = adp->va_flags;
404 ((video_adapter_info_t *)arg)->va_io_base = adp->va_io_base;
405 ((video_adapter_info_t *)arg)->va_io_size = adp->va_io_size;
406 ((video_adapter_info_t *)arg)->va_crtc_addr = adp->va_crtc_addr;
407 ((video_adapter_info_t *)arg)->va_mem_base = adp->va_mem_base;
408 ((video_adapter_info_t *)arg)->va_mem_size = adp->va_mem_size;
409 ((video_adapter_info_t *)arg)->va_window
410 #if defined(__amd64__) || defined(__i386__)
411 = vtophys(adp->va_window);
415 ((video_adapter_info_t *)arg)->va_window_size
416 = adp->va_window_size;
417 ((video_adapter_info_t *)arg)->va_window_gran
418 = adp->va_window_gran;
419 ((video_adapter_info_t *)arg)->va_window_orig
420 = adp->va_window_orig;
421 ((video_adapter_info_t *)arg)->va_unused0
422 #if defined(__amd64__) || defined(__i386__)
423 = adp->va_buffer != 0 ? vtophys(adp->va_buffer) : 0;
427 ((video_adapter_info_t *)arg)->va_buffer_size
428 = adp->va_buffer_size;
429 ((video_adapter_info_t *)arg)->va_mode = adp->va_mode;
430 ((video_adapter_info_t *)arg)->va_initial_mode
431 = adp->va_initial_mode;
432 ((video_adapter_info_t *)arg)->va_initial_bios_mode
433 = adp->va_initial_bios_mode;
434 ((video_adapter_info_t *)arg)->va_line_width
435 = adp->va_line_width;
436 ((video_adapter_info_t *)arg)->va_disp_start.x
437 = adp->va_disp_start.x;
438 ((video_adapter_info_t *)arg)->va_disp_start.y
439 = adp->va_disp_start.y;
442 case FBIO_MODEINFO: /* get mode information */
443 error = vidd_get_info(adp,
444 ((video_info_t *)arg)->vi_mode,
445 (video_info_t *)arg);
450 case FBIO_FINDMODE: /* find a matching video mode */
451 error = vidd_query_mode(adp, (video_info_t *)arg);
454 case FBIO_GETMODE: /* get video mode */
455 *(int *)arg = adp->va_mode;
458 case FBIO_SETMODE: /* set video mode */
459 error = vidd_set_mode(adp, *(int *)arg);
461 error = ENODEV; /* EINVAL? */
464 case FBIO_GETWINORG: /* get frame buffer window origin */
465 *(u_int *)arg = adp->va_window_orig;
468 case FBIO_GETDISPSTART: /* get display start address */
469 ((video_display_start_t *)arg)->x = adp->va_disp_start.x;
470 ((video_display_start_t *)arg)->y = adp->va_disp_start.y;
473 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
474 *(u_int *)arg = adp->va_line_width;
477 case FBIO_BLANK: /* blank display */
478 error = vidd_blank_display(adp, *(int *)arg);
481 case FBIO_GETPALETTE: /* get color palette */
482 case FBIO_SETPALETTE: /* set color palette */
489 case FBIO_SETWINORG: /* set frame buffer window origin */
490 case FBIO_SETDISPSTART: /* set display start address */
491 case FBIO_SETLINEWIDTH: /* set scan line width in pixel */