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