xref: /netbsd-src/sys/dev/wsfb/genfb.c (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 /*	$NetBSD: genfb.c,v 1.36 2011/02/12 17:15:27 phx 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.36 2011/02/12 17:15:27 phx 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 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
68 static paddr_t	genfb_mmap(void *, void *, off_t, int);
69 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
70 
71 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
72 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
73 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
74 			    uint8_t, uint8_t);
75 
76 static void	genfb_brightness_up(device_t);
77 static void	genfb_brightness_down(device_t);
78 /* set backlight level */
79 static void	genfb_set_backlight(struct genfb_softc *, int);
80 /* turn backlight on and off without messing with the level */
81 static void	genfb_switch_backlight(struct genfb_softc *, int);
82 
83 extern const u_char rasops_cmap[768];
84 
85 static int genfb_cnattach_called = 0;
86 static int genfb_enabled = 1;
87 
88 struct wsdisplay_accessops genfb_accessops = {
89 	genfb_ioctl,
90 	genfb_mmap,
91 	NULL,	/* alloc_screen */
92 	NULL,	/* free_screen */
93 	NULL,	/* show_screen */
94 	NULL, 	/* load_font */
95 	NULL,	/* pollc */
96 	NULL	/* scroll */
97 };
98 
99 static struct genfb_softc *genfb_softc = NULL;
100 
101 void
102 genfb_init(struct genfb_softc *sc)
103 {
104 	prop_dictionary_t dict;
105 	uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb;
106 	uint32_t fboffset;
107 	bool console;
108 
109 	dict = device_properties(sc->sc_dev);
110 #ifdef GENFB_DEBUG
111 	printf(prop_dictionary_externalize(dict));
112 #endif
113 	prop_dictionary_get_bool(dict, "is_console", &console);
114 
115 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
116 		GPRINTF("no width property\n");
117 		return;
118 	}
119 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
120 		GPRINTF("no height property\n");
121 		return;
122 	}
123 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
124 		GPRINTF("no depth property\n");
125 		return;
126 	}
127 
128 	/* XXX should be a 64bit value */
129 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
130 		GPRINTF("no address property\n");
131 		return;
132 	}
133 
134 	sc->sc_fboffset = fboffset;
135 
136 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
137 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
138 
139 	/*
140 	 * deal with a bug in the Raptor firmware which always sets
141 	 * stride = width even when depth != 8
142 	 */
143 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
144 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
145 
146 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
147 
148 	/* optional colour map callback */
149 	sc->sc_cmcb = NULL;
150 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
151 		if (cmap_cb != 0)
152 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
153 	}
154 
155 	/* optional pmf callback */
156 	sc->sc_pmfcb = NULL;
157 	if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
158 		if (pmf_cb != 0)
159 			sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
160 	}
161 
162 	/* optional mode callback */
163 	sc->sc_modecb = NULL;
164 	if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
165 		if (mode_cb != 0)
166 			sc->sc_modecb = (void *)(vaddr_t)mode_cb;
167 	}
168 
169 	/* optional backlight control callback */
170 	sc->sc_backlight = NULL;
171 	if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
172 		if (bl_cb != 0) {
173 			sc->sc_backlight = (void *)(vaddr_t)bl_cb;
174 			sc->sc_backlight_on = 1;
175 			aprint_naive_dev(sc->sc_dev,
176 			    "enabling backlight control\n");
177 			sc->sc_backlight_level =
178 			    sc->sc_backlight->gpc_get_parameter(
179 			    sc->sc_backlight->gpc_cookie);
180 			if (console) {
181 				pmf_event_register(sc->sc_dev,
182 				    PMFE_DISPLAY_BRIGHTNESS_UP,
183 				    genfb_brightness_up, TRUE);
184 				pmf_event_register(sc->sc_dev,
185 				    PMFE_DISPLAY_BRIGHTNESS_DOWN,
186 				    genfb_brightness_down, TRUE);
187 			}
188 		}
189 	}
190 }
191 
192 int
193 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
194 {
195 	struct wsemuldisplaydev_attach_args aa;
196 	prop_dictionary_t dict;
197 	struct rasops_info *ri;
198 	uint16_t crow;
199 	long defattr;
200 	int i, j;
201 	bool console;
202 #ifdef SPLASHSCREEN
203 	int error = ENXIO;
204 #endif
205 
206 	dict = device_properties(sc->sc_dev);
207 	prop_dictionary_get_bool(dict, "is_console", &console);
208 
209 	if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
210 		crow = 0;
211 	if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
212 	    == false)
213 		sc->sc_want_clear = true;
214 
215 	/* do not attach when we're not console */
216 	if (!console) {
217 		aprint_normal_dev(sc->sc_dev, "no console, unable to continue\n");
218 		return -1;
219 	}
220 
221 	aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
222 	    "stride %d\n",
223 	    sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
224 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
225 
226 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
227 		"default",
228 		0, 0,
229 		NULL,
230 		8, 16,
231 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
232 		NULL
233 	};
234 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
235 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
236 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
237 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
238 	if (sc->sc_modecb != NULL)
239 		sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
240 
241 #ifdef GENFB_SHADOWFB
242 	sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
243 	if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
244 		memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
245 #endif
246 
247 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
248 	    &genfb_accessops);
249 	sc->vd.init_screen = genfb_init_screen;
250 
251 	/* Do not print anything between this point and the screen
252 	 * clear operation below.  Otherwise it will be lost. */
253 
254 	ri = &sc->sc_console_screen.scr_ri;
255 
256 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
257 	    &defattr);
258 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
259 
260 #ifdef SPLASHSCREEN
261 /*
262  * If system isn't going to go multiuser, or user has requested to see
263  * boot text, don't render splash screen immediately
264  */
265 	if (DISABLESPLASH)
266 #endif
267 		vcons_redraw_screen(&sc->sc_console_screen);
268 
269 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
270 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
271 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
272 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
273 	wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
274 	    defattr);
275 
276 	/* Clear the whole screen to bring it to a known state. */
277 	if (sc->sc_want_clear)
278 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
279 
280 	j = 0;
281 	for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
282 #ifndef SPLASHSCREEN
283 		sc->sc_cmap_red[i] = rasops_cmap[j];
284 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
285 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
286 		j += 3;
287 #else
288 		if (i >= SPLASH_CMAP_OFFSET &&
289 		    i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
290 			splash_get_cmap(i,
291 			    &sc->sc_cmap_red[i],
292 			    &sc->sc_cmap_green[i],
293 			    &sc->sc_cmap_blue[i]);
294 		} else {
295 			sc->sc_cmap_red[i] = rasops_cmap[j];
296 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
297 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
298 		}
299 		j += 3;
300 #endif
301 	}
302 	genfb_restore_palette(sc);
303 
304 #ifdef SPLASHSCREEN
305 	sc->sc_splash.si_depth = sc->sc_depth;
306 	sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_bits;
307 	sc->sc_splash.si_hwbits = sc->sc_fbaddr;
308 	sc->sc_splash.si_width = sc->sc_width;
309 	sc->sc_splash.si_height = sc->sc_height;
310 	sc->sc_splash.si_stride = sc->sc_stride;
311 	sc->sc_splash.si_fillrect = NULL;
312 	if (!DISABLESPLASH) {
313 		error = splash_render(&sc->sc_splash,
314 		    SPLASH_F_CENTER|SPLASH_F_FILL);
315 		if (error) {
316 			SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
317 			vcons_replay_msgbuf(&sc->sc_console_screen);
318 		}
319 	}
320 #else
321 	vcons_replay_msgbuf(&sc->sc_console_screen);
322 #endif
323 
324 	if (genfb_softc == NULL)
325 		genfb_softc = sc;
326 
327 	aa.console = console;
328 	aa.scrdata = &sc->sc_screenlist;
329 	aa.accessops = &genfb_accessops;
330 	aa.accesscookie = &sc->vd;
331 
332 #ifdef GENFB_DISABLE_TEXT
333 	if (!DISABLESPLASH && error == 0)
334 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
335 #endif
336 
337 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
338 
339 	return 0;
340 }
341 
342 static int
343 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
344 	struct lwp *l)
345 {
346 	struct vcons_data *vd = v;
347 	struct genfb_softc *sc = vd->cookie;
348 	struct wsdisplay_fbinfo *wdf;
349 	struct vcons_screen *ms = vd->active;
350 	struct wsdisplay_param *param;
351 	int new_mode, error;
352 
353 	switch (cmd) {
354 		case WSDISPLAYIO_GINFO:
355 			if (ms == NULL)
356 				return ENODEV;
357 			wdf = (void *)data;
358 			wdf->height = ms->scr_ri.ri_height;
359 			wdf->width = ms->scr_ri.ri_width;
360 			wdf->depth = ms->scr_ri.ri_depth;
361 			wdf->cmsize = 256;
362 			return 0;
363 
364 		case WSDISPLAYIO_GETCMAP:
365 			return genfb_getcmap(sc,
366 			    (struct wsdisplay_cmap *)data);
367 
368 		case WSDISPLAYIO_PUTCMAP:
369 			return genfb_putcmap(sc,
370 			    (struct wsdisplay_cmap *)data);
371 
372 		case WSDISPLAYIO_LINEBYTES:
373 			*(u_int *)data = sc->sc_stride;
374 			return 0;
375 
376 		case WSDISPLAYIO_SMODE:
377 			new_mode = *(int *)data;
378 
379 			/* notify the bus backend */
380 			error = 0;
381 			if (sc->sc_ops.genfb_ioctl)
382 				error = sc->sc_ops.genfb_ioctl(sc, vs,
383 					    cmd, data, flag, l);
384 			if (error)
385 				return error;
386 
387 			if (new_mode != sc->sc_mode) {
388 				sc->sc_mode = new_mode;
389 				if (sc->sc_modecb != NULL)
390 					sc->sc_modecb->gmc_setmode(sc,
391 					    sc->sc_mode);
392 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
393 					genfb_restore_palette(sc);
394 					vcons_redraw_screen(ms);
395 				}
396 			}
397 			return 0;
398 		case WSDISPLAYIO_SSPLASH:
399 #if defined(SPLASHSCREEN)
400 			if(*(int *)data == 1) {
401 				SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
402 				splash_render(&sc->sc_splash,
403 						SPLASH_F_CENTER|SPLASH_F_FILL);
404 			} else {
405 				SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
406 			}
407 			vcons_redraw_screen(ms);
408 			return 0;
409 #else
410 			return ENODEV;
411 #endif
412 		case WSDISPLAYIO_GETPARAM:
413 			param = (struct wsdisplay_param *)data;
414 			if (sc->sc_backlight == NULL)
415 				return EPASSTHROUGH;
416 			switch (param->param) {
417 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
418 				param->min = 0;
419 				param->max = 255;
420 				param->curval = sc->sc_backlight_level;
421 				return 0;
422 			case WSDISPLAYIO_PARAM_BACKLIGHT:
423 				param->min = 0;
424 				param->max = 1;
425 				param->curval = sc->sc_backlight_on;
426 				return 0;
427 			}
428 			return EPASSTHROUGH;
429 
430 		case WSDISPLAYIO_SETPARAM:
431 			param = (struct wsdisplay_param *)data;
432 			if (sc->sc_backlight == NULL)
433 				return EPASSTHROUGH;
434 			switch (param->param) {
435 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
436 				genfb_set_backlight(sc, param->curval);
437 				return 0;
438 			case WSDISPLAYIO_PARAM_BACKLIGHT:
439 				genfb_switch_backlight(sc,  param->curval);
440 				return 0;
441 			}
442 			return EPASSTHROUGH;
443 		default:
444 			if (sc->sc_ops.genfb_ioctl)
445 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
446 				    data, flag, l);
447 	}
448 	return EPASSTHROUGH;
449 }
450 
451 static paddr_t
452 genfb_mmap(void *v, void *vs, off_t offset, int prot)
453 {
454 	struct vcons_data *vd = v;
455 	struct genfb_softc *sc = vd->cookie;
456 
457 	if (sc->sc_ops.genfb_mmap)
458 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
459 
460 	return -1;
461 }
462 
463 static void
464 genfb_init_screen(void *cookie, struct vcons_screen *scr,
465     int existing, long *defattr)
466 {
467 	struct genfb_softc *sc = cookie;
468 	struct rasops_info *ri = &scr->scr_ri;
469 
470 	ri->ri_depth = sc->sc_depth;
471 	ri->ri_width = sc->sc_width;
472 	ri->ri_height = sc->sc_height;
473 	ri->ri_stride = sc->sc_stride;
474 	ri->ri_flg = RI_CENTER;
475 	if (sc->sc_want_clear)
476 		ri->ri_flg |= RI_FULLCLEAR;
477 
478 #ifdef GENFB_SHADOWFB
479 	if (sc->sc_shadowfb != NULL) {
480 
481 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
482 		ri->ri_bits = (char *)sc->sc_shadowfb;
483 	} else
484 #endif
485 	{
486 		ri->ri_bits = (char *)sc->sc_fbaddr;
487 		scr->scr_flags |= VCONS_DONT_READ;
488 	}
489 
490 	if (existing && sc->sc_want_clear) {
491 		ri->ri_flg |= RI_CLEAR;
492 	}
493 
494 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
495 	ri->ri_caps = WSSCREEN_WSCOLORS;
496 
497 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
498 		    sc->sc_width / ri->ri_font->fontwidth);
499 
500 	/* TODO: actually center output */
501 	ri->ri_hw = scr;
502 
503 #ifdef GENFB_DISABLE_TEXT
504 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
505 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
506 #endif
507 }
508 
509 static int
510 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
511 {
512 	u_char *r, *g, *b;
513 	u_int index = cm->index;
514 	u_int count = cm->count;
515 	int i, error;
516 	u_char rbuf[256], gbuf[256], bbuf[256];
517 
518 #ifdef GENFB_DEBUG
519 	aprint_debug("putcmap: %d %d\n",index, count);
520 #endif
521 	if (cm->index >= 256 || cm->count > 256 ||
522 	    (cm->index + cm->count) > 256)
523 		return EINVAL;
524 	error = copyin(cm->red, &rbuf[index], count);
525 	if (error)
526 		return error;
527 	error = copyin(cm->green, &gbuf[index], count);
528 	if (error)
529 		return error;
530 	error = copyin(cm->blue, &bbuf[index], count);
531 	if (error)
532 		return error;
533 
534 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
535 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
536 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
537 
538 	r = &sc->sc_cmap_red[index];
539 	g = &sc->sc_cmap_green[index];
540 	b = &sc->sc_cmap_blue[index];
541 
542 	for (i = 0; i < count; i++) {
543 		genfb_putpalreg(sc, index, *r, *g, *b);
544 		index++;
545 		r++, g++, b++;
546 	}
547 	return 0;
548 }
549 
550 static int
551 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
552 {
553 	u_int index = cm->index;
554 	u_int count = cm->count;
555 	int error;
556 
557 	if (index >= 255 || count > 256 || index + count > 256)
558 		return EINVAL;
559 
560 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
561 	if (error)
562 		return error;
563 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
564 	if (error)
565 		return error;
566 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
567 	if (error)
568 		return error;
569 
570 	return 0;
571 }
572 
573 void
574 genfb_restore_palette(struct genfb_softc *sc)
575 {
576 	int i;
577 
578 	if (sc->sc_depth <= 8) {
579 		for (i = 0; i < (1 << sc->sc_depth); i++) {
580 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
581 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
582 		}
583 	}
584 }
585 
586 static int
587 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
588     uint8_t b)
589 {
590 
591 	if (sc->sc_cmcb) {
592 
593 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
594 		    idx, r, g, b);
595 	}
596 	return 0;
597 }
598 
599 void
600 genfb_cnattach(void)
601 {
602 	genfb_cnattach_called = 1;
603 }
604 
605 void
606 genfb_disable(void)
607 {
608 	genfb_enabled = 0;
609 }
610 
611 int
612 genfb_is_console(void)
613 {
614 	return genfb_cnattach_called;
615 }
616 
617 int
618 genfb_is_enabled(void)
619 {
620 	return genfb_enabled;
621 }
622 
623 int
624 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
625 {
626 	struct genfb_softc *sc = genfb_softc;
627 
628 	if (sc && sc->sc_ops.genfb_borrow)
629 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
630 	return 0;
631 }
632 
633 static void
634 genfb_set_backlight(struct genfb_softc *sc, int level)
635 {
636 
637 	KASSERT(sc->sc_backlight != NULL);
638 
639 	/*
640 	 * should we do nothing when backlight is off, should we just store the
641 	 * level and use it when turning back on or should we just flip sc_bl_on
642 	 * and turn the backlight on?
643 	 * For now turn it on so a crashed screensaver can't get the user stuck
644 	 * with a dark screen as long as hotkeys work
645 	 */
646 	if (level > 255) level = 255;
647 	if (level < 0) level = 0;
648 	if (level == sc->sc_backlight_level)
649 		return;
650 	sc->sc_backlight_level = level;
651 	if (sc->sc_backlight_on == 0)
652 		sc->sc_backlight_on = 1;
653 	sc->sc_backlight->gpc_set_parameter(
654 	    sc->sc_backlight->gpc_cookie, level);
655 }
656 
657 static void
658 genfb_switch_backlight(struct genfb_softc *sc, int on)
659 {
660 	int level;
661 
662 	KASSERT(sc->sc_backlight != NULL);
663 
664 	if (on == sc->sc_backlight_on)
665 		return;
666 	sc->sc_backlight_on = on;
667 	level = on ? sc->sc_backlight_level : 0;
668 	sc->sc_backlight->gpc_set_parameter(
669 	    sc->sc_backlight->gpc_cookie, level);
670 }
671 
672 
673 static void
674 genfb_brightness_up(device_t dev)
675 {
676 	struct genfb_softc *sc = device_private(dev);
677 
678 	genfb_set_backlight(sc, sc->sc_backlight_level + 8);
679 }
680 
681 static void
682 genfb_brightness_down(device_t dev)
683 {
684 	struct genfb_softc *sc = device_private(dev);
685 
686 	genfb_set_backlight(sc, sc->sc_backlight_level - 8);
687 }
688 
689 void
690 genfb_enable_polling(device_t dev)
691 {
692 	struct genfb_softc *sc = device_private(dev);
693 
694 	SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
695 	vcons_enable_polling(&sc->vd);
696 }
697 
698 void
699 genfb_disable_polling(device_t dev)
700 {
701 	struct genfb_softc *sc = device_private(dev);
702 
703 	vcons_disable_polling(&sc->vd);
704 }
705