xref: /netbsd-src/sys/dev/wsfb/genfb.c (revision ca453df649ce9db45b64d73678ba06cbccf9aa11)
1 /*	$NetBSD: genfb.c,v 1.42 2011/07/13 22:47:29 macallan Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Michael Lorenz
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.42 2011/07/13 22:47:29 macallan Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/mutex.h>
38 #include <sys/ioctl.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/kmem.h>
42 
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsdisplayvar.h>
45 #include <dev/rasops/rasops.h>
46 #include <dev/wsfont/wsfont.h>
47 
48 #include <dev/wscons/wsdisplay_vconsvar.h>
49 
50 #include <dev/wsfb/genfbvar.h>
51 
52 #ifdef GENFB_DISABLE_TEXT
53 #include <sys/reboot.h>
54 #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \
55 		AB_VERBOSE | AB_DEBUG) )
56 #endif
57 
58 #include "opt_genfb.h"
59 #include "opt_wsfb.h"
60 
61 #ifdef GENFB_DEBUG
62 #define GPRINTF panic
63 #else
64 #define GPRINTF aprint_verbose
65 #endif
66 
67 #define GENFB_BRIGHTNESS_STEP 15
68 
69 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
70 static paddr_t	genfb_mmap(void *, void *, off_t, int);
71 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
72 
73 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
74 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
75 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
76 			    uint8_t, uint8_t);
77 
78 static void	genfb_brightness_up(device_t);
79 static void	genfb_brightness_down(device_t);
80 
81 extern const u_char rasops_cmap[768];
82 
83 static int genfb_cnattach_called = 0;
84 static int genfb_enabled = 1;
85 
86 static struct genfb_softc *genfb_softc = NULL;
87 
88 void
89 genfb_init(struct genfb_softc *sc)
90 {
91 	prop_dictionary_t dict;
92 	uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
93 	uint32_t fboffset;
94 	bool console;
95 
96 	dict = device_properties(sc->sc_dev);
97 #ifdef GENFB_DEBUG
98 	printf(prop_dictionary_externalize(dict));
99 #endif
100 	prop_dictionary_get_bool(dict, "is_console", &console);
101 
102 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
103 		GPRINTF("no width property\n");
104 		return;
105 	}
106 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
107 		GPRINTF("no height property\n");
108 		return;
109 	}
110 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
111 		GPRINTF("no depth property\n");
112 		return;
113 	}
114 
115 	/* XXX should be a 64bit value */
116 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
117 		GPRINTF("no address property\n");
118 		return;
119 	}
120 
121 	sc->sc_fboffset = fboffset;
122 
123 	sc->sc_fbaddr = NULL;
124 	if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
125 		sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
126 	}
127 
128 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
129 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
130 
131 	/*
132 	 * deal with a bug in the Raptor firmware which always sets
133 	 * stride = width even when depth != 8
134 	 */
135 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
136 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
137 
138 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
139 
140 	/* optional colour map callback */
141 	sc->sc_cmcb = NULL;
142 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
143 		if (cmap_cb != 0)
144 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
145 	}
146 
147 	/* optional pmf callback */
148 	sc->sc_pmfcb = NULL;
149 	if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
150 		if (pmf_cb != 0)
151 			sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
152 	}
153 
154 	/* optional mode callback */
155 	sc->sc_modecb = NULL;
156 	if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
157 		if (mode_cb != 0)
158 			sc->sc_modecb = (void *)(vaddr_t)mode_cb;
159 	}
160 
161 	/* optional backlight control callback */
162 	sc->sc_backlight = NULL;
163 	if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
164 		if (bl_cb != 0) {
165 			sc->sc_backlight = (void *)(vaddr_t)bl_cb;
166 			aprint_naive_dev(sc->sc_dev,
167 			    "enabling backlight control\n");
168 		}
169 	}
170 
171 	/* optional brightness control callback */
172 	sc->sc_brightness = NULL;
173 	if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
174 		if (br_cb != 0) {
175 			sc->sc_brightness = (void *)(vaddr_t)br_cb;
176 			aprint_naive_dev(sc->sc_dev,
177 			    "enabling brightness control\n");
178 			if (console &&
179 			    sc->sc_brightness->gpc_upd_parameter != NULL) {
180 				pmf_event_register(sc->sc_dev,
181 				    PMFE_DISPLAY_BRIGHTNESS_UP,
182 				    genfb_brightness_up, TRUE);
183 				pmf_event_register(sc->sc_dev,
184 				    PMFE_DISPLAY_BRIGHTNESS_DOWN,
185 				    genfb_brightness_down, TRUE);
186 			}
187 		}
188 	}
189 }
190 
191 int
192 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
193 {
194 	struct wsemuldisplaydev_attach_args aa;
195 	prop_dictionary_t dict;
196 	struct rasops_info *ri;
197 	uint16_t crow;
198 	long defattr;
199 	int i, j;
200 	bool console;
201 #ifdef SPLASHSCREEN
202 	int error = ENXIO;
203 #endif
204 
205 	dict = device_properties(sc->sc_dev);
206 	prop_dictionary_get_bool(dict, "is_console", &console);
207 
208 	if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
209 		crow = 0;
210 	if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
211 	    == false)
212 		sc->sc_want_clear = true;
213 
214 	aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
215 	    "stride %d\n",
216 	    sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
217 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
218 
219 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
220 		"default",
221 		0, 0,
222 		NULL,
223 		8, 16,
224 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
225 		NULL
226 	};
227 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
228 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
229 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
230 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
231 	if (sc->sc_modecb != NULL)
232 		sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
233 
234 	sc->sc_accessops.ioctl = genfb_ioctl;
235 	sc->sc_accessops.mmap = genfb_mmap;
236 
237 #ifdef GENFB_SHADOWFB
238 	sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
239 	if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
240 		memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
241 #endif
242 
243 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
244 	    &sc->sc_accessops);
245 	sc->vd.init_screen = genfb_init_screen;
246 
247 	/* Do not print anything between this point and the screen
248 	 * clear operation below.  Otherwise it will be lost. */
249 
250 	ri = &sc->sc_console_screen.scr_ri;
251 
252 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
253 	    &defattr);
254 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
255 
256 #ifdef SPLASHSCREEN
257 /*
258  * If system isn't going to go multiuser, or user has requested to see
259  * boot text, don't render splash screen immediately
260  */
261 	if (DISABLESPLASH)
262 #endif
263 		vcons_redraw_screen(&sc->sc_console_screen);
264 
265 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
266 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
267 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
268 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
269 
270 	if (console)
271 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
272 		    defattr);
273 
274 	/* Clear the whole screen to bring it to a known state. */
275 	if (sc->sc_want_clear)
276 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
277 
278 	j = 0;
279 	for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
280 #ifndef SPLASHSCREEN
281 		sc->sc_cmap_red[i] = rasops_cmap[j];
282 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
283 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
284 		j += 3;
285 #else
286 		if (i >= SPLASH_CMAP_OFFSET &&
287 		    i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
288 			splash_get_cmap(i,
289 			    &sc->sc_cmap_red[i],
290 			    &sc->sc_cmap_green[i],
291 			    &sc->sc_cmap_blue[i]);
292 		} else {
293 			sc->sc_cmap_red[i] = rasops_cmap[j];
294 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
295 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
296 		}
297 		j += 3;
298 #endif
299 	}
300 	genfb_restore_palette(sc);
301 
302 #ifdef SPLASHSCREEN
303 	sc->sc_splash.si_depth = sc->sc_depth;
304 	sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_bits;
305 	sc->sc_splash.si_hwbits = sc->sc_fbaddr;
306 	sc->sc_splash.si_width = sc->sc_width;
307 	sc->sc_splash.si_height = sc->sc_height;
308 	sc->sc_splash.si_stride = sc->sc_stride;
309 	sc->sc_splash.si_fillrect = NULL;
310 	if (!DISABLESPLASH) {
311 		error = splash_render(&sc->sc_splash,
312 		    SPLASH_F_CENTER|SPLASH_F_FILL);
313 		if (error) {
314 			SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
315 			vcons_replay_msgbuf(&sc->sc_console_screen);
316 		}
317 	}
318 #else
319 	vcons_replay_msgbuf(&sc->sc_console_screen);
320 #endif
321 
322 	if (genfb_softc == NULL)
323 		genfb_softc = sc;
324 
325 	aa.console = console;
326 	aa.scrdata = &sc->sc_screenlist;
327 	aa.accessops = &sc->sc_accessops;
328 	aa.accesscookie = &sc->vd;
329 
330 #ifdef GENFB_DISABLE_TEXT
331 	if (!DISABLESPLASH && error == 0)
332 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
333 #endif
334 
335 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
336 
337 	return 0;
338 }
339 
340 static int
341 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
342 	struct lwp *l)
343 {
344 	struct vcons_data *vd = v;
345 	struct genfb_softc *sc = vd->cookie;
346 	struct wsdisplay_fbinfo *wdf;
347 	struct vcons_screen *ms = vd->active;
348 	struct wsdisplay_param *param;
349 	int new_mode, error, val;
350 
351 	switch (cmd) {
352 		case WSDISPLAYIO_GINFO:
353 			if (ms == NULL)
354 				return ENODEV;
355 			wdf = (void *)data;
356 			wdf->height = ms->scr_ri.ri_height;
357 			wdf->width = ms->scr_ri.ri_width;
358 			wdf->depth = ms->scr_ri.ri_depth;
359 			wdf->cmsize = 256;
360 			return 0;
361 
362 		case WSDISPLAYIO_GETCMAP:
363 			return genfb_getcmap(sc,
364 			    (struct wsdisplay_cmap *)data);
365 
366 		case WSDISPLAYIO_PUTCMAP:
367 			return genfb_putcmap(sc,
368 			    (struct wsdisplay_cmap *)data);
369 
370 		case WSDISPLAYIO_LINEBYTES:
371 			*(u_int *)data = sc->sc_stride;
372 			return 0;
373 
374 		case WSDISPLAYIO_SMODE:
375 			new_mode = *(int *)data;
376 
377 			/* notify the bus backend */
378 			error = 0;
379 			if (sc->sc_ops.genfb_ioctl)
380 				error = sc->sc_ops.genfb_ioctl(sc, vs,
381 					    cmd, data, flag, l);
382 			if (error)
383 				return error;
384 
385 			if (new_mode != sc->sc_mode) {
386 				sc->sc_mode = new_mode;
387 				if (sc->sc_modecb != NULL)
388 					sc->sc_modecb->gmc_setmode(sc,
389 					    sc->sc_mode);
390 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
391 					genfb_restore_palette(sc);
392 					vcons_redraw_screen(ms);
393 				}
394 			}
395 			return 0;
396 		case WSDISPLAYIO_SSPLASH:
397 #if defined(SPLASHSCREEN)
398 			if(*(int *)data == 1) {
399 				SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
400 				splash_render(&sc->sc_splash,
401 						SPLASH_F_CENTER|SPLASH_F_FILL);
402 			} else {
403 				SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
404 			}
405 			vcons_redraw_screen(ms);
406 			return 0;
407 #else
408 			return ENODEV;
409 #endif
410 		case WSDISPLAYIO_GETPARAM:
411 			param = (struct wsdisplay_param *)data;
412 			switch (param->param) {
413 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
414 				if (sc->sc_brightness == NULL)
415 					return EPASSTHROUGH;
416 				param->min = 0;
417 				param->max = 255;
418 				return sc->sc_brightness->gpc_get_parameter(
419 				    sc->sc_brightness->gpc_cookie,
420 				    &param->curval);
421 			case WSDISPLAYIO_PARAM_BACKLIGHT:
422 				if (sc->sc_backlight == NULL)
423 					return EPASSTHROUGH;
424 				param->min = 0;
425 				param->max = 1;
426 				return sc->sc_backlight->gpc_get_parameter(
427 				    sc->sc_backlight->gpc_cookie,
428 				    &param->curval);
429 			}
430 			return EPASSTHROUGH;
431 
432 		case WSDISPLAYIO_SETPARAM:
433 			param = (struct wsdisplay_param *)data;
434 			switch (param->param) {
435 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
436 				if (sc->sc_brightness == NULL)
437 					return EPASSTHROUGH;
438 				val = param->curval;
439 				if (val < 0) val = 0;
440 				if (val > 255) val = 255;
441 				return sc->sc_brightness->gpc_set_parameter(
442 				    sc->sc_brightness->gpc_cookie, val);
443 			case WSDISPLAYIO_PARAM_BACKLIGHT:
444 				if (sc->sc_backlight == NULL)
445 					return EPASSTHROUGH;
446 				val = param->curval;
447 				if (val < 0) val = 0;
448 				if (val > 1) val = 1;
449 				return sc->sc_backlight->gpc_set_parameter(
450 				    sc->sc_backlight->gpc_cookie, val);
451 			}
452 			return EPASSTHROUGH;
453 		case WSDISPLAYIO_GET_EDID: {
454 			struct wsdisplayio_edid_info *d = data;
455 			return wsdisplayio_get_edid(sc->sc_dev, d);
456 		}
457 		default:
458 			if (sc->sc_ops.genfb_ioctl)
459 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
460 				    data, flag, l);
461 	}
462 	return EPASSTHROUGH;
463 }
464 
465 static paddr_t
466 genfb_mmap(void *v, void *vs, off_t offset, int prot)
467 {
468 	struct vcons_data *vd = v;
469 	struct genfb_softc *sc = vd->cookie;
470 
471 	if (sc->sc_ops.genfb_mmap)
472 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
473 
474 	return -1;
475 }
476 
477 static void
478 genfb_init_screen(void *cookie, struct vcons_screen *scr,
479     int existing, long *defattr)
480 {
481 	struct genfb_softc *sc = cookie;
482 	struct rasops_info *ri = &scr->scr_ri;
483 
484 	ri->ri_depth = sc->sc_depth;
485 	ri->ri_width = sc->sc_width;
486 	ri->ri_height = sc->sc_height;
487 	ri->ri_stride = sc->sc_stride;
488 	ri->ri_flg = RI_CENTER;
489 	if (sc->sc_want_clear)
490 		ri->ri_flg |= RI_FULLCLEAR;
491 
492 #ifdef GENFB_SHADOWFB
493 	if (sc->sc_shadowfb != NULL) {
494 
495 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
496 		ri->ri_bits = (char *)sc->sc_shadowfb;
497 	} else
498 #endif
499 	{
500 		ri->ri_bits = (char *)sc->sc_fbaddr;
501 		scr->scr_flags |= VCONS_DONT_READ;
502 	}
503 
504 	if (existing && sc->sc_want_clear) {
505 		ri->ri_flg |= RI_CLEAR;
506 	}
507 
508 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
509 	ri->ri_caps = WSSCREEN_WSCOLORS;
510 
511 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
512 		    sc->sc_width / ri->ri_font->fontwidth);
513 
514 	/* TODO: actually center output */
515 	ri->ri_hw = scr;
516 
517 #ifdef GENFB_DISABLE_TEXT
518 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
519 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
520 #endif
521 }
522 
523 static int
524 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
525 {
526 	u_char *r, *g, *b;
527 	u_int index = cm->index;
528 	u_int count = cm->count;
529 	int i, error;
530 	u_char rbuf[256], gbuf[256], bbuf[256];
531 
532 #ifdef GENFB_DEBUG
533 	aprint_debug("putcmap: %d %d\n",index, count);
534 #endif
535 	if (cm->index >= 256 || cm->count > 256 ||
536 	    (cm->index + cm->count) > 256)
537 		return EINVAL;
538 	error = copyin(cm->red, &rbuf[index], count);
539 	if (error)
540 		return error;
541 	error = copyin(cm->green, &gbuf[index], count);
542 	if (error)
543 		return error;
544 	error = copyin(cm->blue, &bbuf[index], count);
545 	if (error)
546 		return error;
547 
548 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
549 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
550 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
551 
552 	r = &sc->sc_cmap_red[index];
553 	g = &sc->sc_cmap_green[index];
554 	b = &sc->sc_cmap_blue[index];
555 
556 	for (i = 0; i < count; i++) {
557 		genfb_putpalreg(sc, index, *r, *g, *b);
558 		index++;
559 		r++, g++, b++;
560 	}
561 	return 0;
562 }
563 
564 static int
565 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
566 {
567 	u_int index = cm->index;
568 	u_int count = cm->count;
569 	int error;
570 
571 	if (index >= 255 || count > 256 || index + count > 256)
572 		return EINVAL;
573 
574 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
575 	if (error)
576 		return error;
577 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
578 	if (error)
579 		return error;
580 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
581 	if (error)
582 		return error;
583 
584 	return 0;
585 }
586 
587 void
588 genfb_restore_palette(struct genfb_softc *sc)
589 {
590 	int i;
591 
592 	if (sc->sc_depth <= 8) {
593 		for (i = 0; i < (1 << sc->sc_depth); i++) {
594 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
595 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
596 		}
597 	}
598 }
599 
600 static int
601 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
602     uint8_t b)
603 {
604 
605 	if (sc->sc_cmcb) {
606 
607 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
608 		    idx, r, g, b);
609 	}
610 	return 0;
611 }
612 
613 void
614 genfb_cnattach(void)
615 {
616 	genfb_cnattach_called = 1;
617 }
618 
619 void
620 genfb_disable(void)
621 {
622 	genfb_enabled = 0;
623 }
624 
625 int
626 genfb_is_console(void)
627 {
628 	return genfb_cnattach_called;
629 }
630 
631 int
632 genfb_is_enabled(void)
633 {
634 	return genfb_enabled;
635 }
636 
637 int
638 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
639 {
640 	struct genfb_softc *sc = genfb_softc;
641 
642 	if (sc && sc->sc_ops.genfb_borrow)
643 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
644 	return 0;
645 }
646 
647 static void
648 genfb_brightness_up(device_t dev)
649 {
650 	struct genfb_softc *sc = device_private(dev);
651 
652 	KASSERT(sc->sc_brightness != NULL &&
653 		sc->sc_brightness->gpc_upd_parameter != NULL);
654 
655 	(void)sc->sc_brightness->gpc_upd_parameter(
656 	    sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
657 }
658 
659 static void
660 genfb_brightness_down(device_t dev)
661 {
662 	struct genfb_softc *sc = device_private(dev);
663 
664 	KASSERT(sc->sc_brightness != NULL &&
665 		sc->sc_brightness->gpc_upd_parameter != NULL);
666 
667 	(void)sc->sc_brightness->gpc_upd_parameter(
668 	    sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
669 }
670 
671 void
672 genfb_enable_polling(device_t dev)
673 {
674 	struct genfb_softc *sc = device_private(dev);
675 
676 	if (sc->sc_console_screen.scr_vd) {
677 		SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
678 		vcons_hard_switch(&sc->sc_console_screen);
679 		vcons_enable_polling(&sc->vd);
680 	}
681 }
682 
683 void
684 genfb_disable_polling(device_t dev)
685 {
686 	struct genfb_softc *sc = device_private(dev);
687 
688 	if (sc->sc_console_screen.scr_vd) {
689 		vcons_disable_polling(&sc->vd);
690 	}
691 }
692