xref: /netbsd-src/sys/dev/wsfb/genfb.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: genfb.c,v 1.51 2013/10/09 17:20:54 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.51 2013/10/09 17:20:54 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_debug
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 static void	genfb_init_palette(struct genfb_softc *);
78 
79 static void	genfb_brightness_up(device_t);
80 static void	genfb_brightness_down(device_t);
81 
82 extern const u_char rasops_cmap[768];
83 
84 static int genfb_cnattach_called = 0;
85 static int genfb_enabled = 1;
86 
87 static struct genfb_softc *genfb_softc = NULL;
88 
89 void
90 genfb_init(struct genfb_softc *sc)
91 {
92 	prop_dictionary_t dict;
93 	uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
94 	uint32_t fboffset;
95 	bool console;
96 
97 	dict = device_properties(sc->sc_dev);
98 #ifdef GENFB_DEBUG
99 	printf(prop_dictionary_externalize(dict));
100 #endif
101 	prop_dictionary_get_bool(dict, "is_console", &console);
102 
103 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
104 		GPRINTF("no width property\n");
105 		return;
106 	}
107 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
108 		GPRINTF("no height property\n");
109 		return;
110 	}
111 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
112 		GPRINTF("no depth property\n");
113 		return;
114 	}
115 
116 	/* XXX should be a 64bit value */
117 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
118 		GPRINTF("no address property\n");
119 		return;
120 	}
121 
122 	sc->sc_fboffset = fboffset;
123 
124 	sc->sc_fbaddr = NULL;
125 	if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
126 		sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
127 	}
128 
129 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
130 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
131 
132 	/*
133 	 * deal with a bug in the Raptor firmware which always sets
134 	 * stride = width even when depth != 8
135 	 */
136 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
137 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
138 
139 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
140 
141 	/* optional colour map callback */
142 	sc->sc_cmcb = NULL;
143 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
144 		if (cmap_cb != 0)
145 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
146 	}
147 
148 	/* optional pmf callback */
149 	sc->sc_pmfcb = NULL;
150 	if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
151 		if (pmf_cb != 0)
152 			sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
153 	}
154 
155 	/* optional mode callback */
156 	sc->sc_modecb = NULL;
157 	if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
158 		if (mode_cb != 0)
159 			sc->sc_modecb = (void *)(vaddr_t)mode_cb;
160 	}
161 
162 	/* optional backlight control callback */
163 	sc->sc_backlight = NULL;
164 	if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
165 		if (bl_cb != 0) {
166 			sc->sc_backlight = (void *)(vaddr_t)bl_cb;
167 			aprint_naive_dev(sc->sc_dev,
168 			    "enabling backlight control\n");
169 		}
170 	}
171 
172 	/* optional brightness control callback */
173 	sc->sc_brightness = NULL;
174 	if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
175 		if (br_cb != 0) {
176 			sc->sc_brightness = (void *)(vaddr_t)br_cb;
177 			aprint_naive_dev(sc->sc_dev,
178 			    "enabling brightness control\n");
179 			if (console &&
180 			    sc->sc_brightness->gpc_upd_parameter != NULL) {
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 	bool console;
201 #ifdef SPLASHSCREEN
202 	int i, j;
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 	aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
216 	    "stride %d\n",
217 	    sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
218 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
219 
220 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
221 		"default",
222 		0, 0,
223 		NULL,
224 		8, 16,
225 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
226 		NULL
227 	};
228 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
229 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
230 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
231 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
232 	if (sc->sc_modecb != NULL)
233 		sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
234 
235 	sc->sc_accessops.ioctl = genfb_ioctl;
236 	sc->sc_accessops.mmap = genfb_mmap;
237 
238 #ifdef GENFB_SHADOWFB
239 	sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
240 	if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
241 		memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
242 #endif
243 
244 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
245 	    &sc->sc_accessops);
246 	sc->vd.init_screen = genfb_init_screen;
247 
248 	/* Do not print anything between this point and the screen
249 	 * clear operation below.  Otherwise it will be lost. */
250 
251 	ri = &sc->sc_console_screen.scr_ri;
252 
253 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
254 	    &defattr);
255 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
256 
257 #ifdef SPLASHSCREEN
258 /*
259  * If system isn't going to go multiuser, or user has requested to see
260  * boot text, don't render splash screen immediately
261  */
262 	if (DISABLESPLASH)
263 #endif
264 		vcons_redraw_screen(&sc->sc_console_screen);
265 
266 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
267 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
268 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
269 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
270 
271 	if (crow >= ri->ri_rows) {
272 		crow = 0;
273 		sc->sc_want_clear = 1;
274 	}
275 
276 	if (console)
277 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
278 		    defattr);
279 
280 	/* Clear the whole screen to bring it to a known state. */
281 	if (sc->sc_want_clear)
282 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
283 
284 #ifdef SPLASHSCREEN
285 	j = 0;
286 	for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
287 		if (i >= SPLASH_CMAP_OFFSET &&
288 		    i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
289 			splash_get_cmap(i,
290 			    &sc->sc_cmap_red[i],
291 			    &sc->sc_cmap_green[i],
292 			    &sc->sc_cmap_blue[i]);
293 		} else {
294 			sc->sc_cmap_red[i] = rasops_cmap[j];
295 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
296 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
297 		}
298 		j += 3;
299 	}
300 	genfb_restore_palette(sc);
301 
302 	sc->sc_splash.si_depth = sc->sc_depth;
303 	sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_bits;
304 	sc->sc_splash.si_hwbits = sc->sc_fbaddr;
305 	sc->sc_splash.si_width = sc->sc_width;
306 	sc->sc_splash.si_height = sc->sc_height;
307 	sc->sc_splash.si_stride = sc->sc_stride;
308 	sc->sc_splash.si_fillrect = NULL;
309 	if (!DISABLESPLASH) {
310 		error = splash_render(&sc->sc_splash,
311 		    SPLASH_F_CENTER|SPLASH_F_FILL);
312 		if (error) {
313 			SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
314 			genfb_init_palette(sc);
315 			vcons_replay_msgbuf(&sc->sc_console_screen);
316 		}
317 	}
318 #else
319 	genfb_init_palette(sc);
320 	vcons_replay_msgbuf(&sc->sc_console_screen);
321 #endif
322 
323 	if (genfb_softc == NULL)
324 		genfb_softc = sc;
325 
326 	aa.console = console;
327 	aa.scrdata = &sc->sc_screenlist;
328 	aa.accessops = &sc->sc_accessops;
329 	aa.accesscookie = &sc->vd;
330 
331 #ifdef GENFB_DISABLE_TEXT
332 	if (!DISABLESPLASH && error == 0)
333 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
334 #endif
335 
336 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
337 
338 	return 0;
339 }
340 
341 static int
342 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
343 	struct lwp *l)
344 {
345 	struct vcons_data *vd = v;
346 	struct genfb_softc *sc = vd->cookie;
347 	struct wsdisplay_fbinfo *wdf;
348 	struct vcons_screen *ms = vd->active;
349 	struct wsdisplay_param *param;
350 	int new_mode, error, val;
351 
352 	switch (cmd) {
353 		case WSDISPLAYIO_GINFO:
354 			if (ms == NULL)
355 				return ENODEV;
356 			wdf = (void *)data;
357 			wdf->height = ms->scr_ri.ri_height;
358 			wdf->width = ms->scr_ri.ri_width;
359 			wdf->depth = ms->scr_ri.ri_depth;
360 			wdf->cmsize = 256;
361 			return 0;
362 
363 		case WSDISPLAYIO_GETCMAP:
364 			return genfb_getcmap(sc,
365 			    (struct wsdisplay_cmap *)data);
366 
367 		case WSDISPLAYIO_PUTCMAP:
368 			return genfb_putcmap(sc,
369 			    (struct wsdisplay_cmap *)data);
370 
371 		case WSDISPLAYIO_LINEBYTES:
372 			*(u_int *)data = sc->sc_stride;
373 			return 0;
374 
375 		case WSDISPLAYIO_SMODE:
376 			new_mode = *(int *)data;
377 
378 			/* notify the bus backend */
379 			error = 0;
380 			if (sc->sc_ops.genfb_ioctl)
381 				error = sc->sc_ops.genfb_ioctl(sc, vs,
382 					    cmd, data, flag, l);
383 			if (error && error != EPASSTHROUGH)
384 				return error;
385 
386 			if (new_mode != sc->sc_mode) {
387 				sc->sc_mode = new_mode;
388 				if (sc->sc_modecb != NULL)
389 					sc->sc_modecb->gmc_setmode(sc,
390 					    sc->sc_mode);
391 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
392 					genfb_restore_palette(sc);
393 					vcons_redraw_screen(ms);
394 				}
395 			}
396 			return 0;
397 
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 				genfb_init_palette(sc);
407 			}
408 			vcons_redraw_screen(ms);
409 			return 0;
410 #else
411 			return ENODEV;
412 #endif
413 		case WSDISPLAYIO_GETPARAM:
414 			param = (struct wsdisplay_param *)data;
415 			switch (param->param) {
416 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
417 				if (sc->sc_brightness == NULL)
418 					return EPASSTHROUGH;
419 				param->min = 0;
420 				param->max = 255;
421 				return sc->sc_brightness->gpc_get_parameter(
422 				    sc->sc_brightness->gpc_cookie,
423 				    &param->curval);
424 			case WSDISPLAYIO_PARAM_BACKLIGHT:
425 				if (sc->sc_backlight == NULL)
426 					return EPASSTHROUGH;
427 				param->min = 0;
428 				param->max = 1;
429 				return sc->sc_backlight->gpc_get_parameter(
430 				    sc->sc_backlight->gpc_cookie,
431 				    &param->curval);
432 			}
433 			return EPASSTHROUGH;
434 
435 		case WSDISPLAYIO_SETPARAM:
436 			param = (struct wsdisplay_param *)data;
437 			switch (param->param) {
438 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
439 				if (sc->sc_brightness == NULL)
440 					return EPASSTHROUGH;
441 				val = param->curval;
442 				if (val < 0) val = 0;
443 				if (val > 255) val = 255;
444 				return sc->sc_brightness->gpc_set_parameter(
445 				    sc->sc_brightness->gpc_cookie, val);
446 			case WSDISPLAYIO_PARAM_BACKLIGHT:
447 				if (sc->sc_backlight == NULL)
448 					return EPASSTHROUGH;
449 				val = param->curval;
450 				if (val < 0) val = 0;
451 				if (val > 1) val = 1;
452 				return sc->sc_backlight->gpc_set_parameter(
453 				    sc->sc_backlight->gpc_cookie, val);
454 			}
455 			return EPASSTHROUGH;
456 
457 		case WSDISPLAYIO_GET_EDID: {
458 			struct wsdisplayio_edid_info *d = data;
459 			return wsdisplayio_get_edid(sc->sc_dev, d);
460 		}
461 
462 		case WSDISPLAYIO_GET_FBINFO: {
463 			struct wsdisplayio_fbinfo *fbi = data;
464 			return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
465 		}
466 
467 		default:
468 			if (sc->sc_ops.genfb_ioctl)
469 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
470 				    data, flag, l);
471 	}
472 	return EPASSTHROUGH;
473 }
474 
475 static paddr_t
476 genfb_mmap(void *v, void *vs, off_t offset, int prot)
477 {
478 	struct vcons_data *vd = v;
479 	struct genfb_softc *sc = vd->cookie;
480 
481 	if (sc->sc_ops.genfb_mmap)
482 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
483 
484 	return -1;
485 }
486 
487 static void
488 genfb_init_screen(void *cookie, struct vcons_screen *scr,
489     int existing, long *defattr)
490 {
491 	struct genfb_softc *sc = cookie;
492 	struct rasops_info *ri = &scr->scr_ri;
493 
494 	ri->ri_depth = sc->sc_depth;
495 	ri->ri_width = sc->sc_width;
496 	ri->ri_height = sc->sc_height;
497 	ri->ri_stride = sc->sc_stride;
498 	ri->ri_flg = RI_CENTER;
499 	if (sc->sc_want_clear)
500 		ri->ri_flg |= RI_FULLCLEAR;
501 
502 #ifdef GENFB_SHADOWFB
503 	if (sc->sc_shadowfb != NULL) {
504 
505 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
506 		ri->ri_bits = (char *)sc->sc_shadowfb;
507 	} else
508 #endif
509 	{
510 		ri->ri_bits = (char *)sc->sc_fbaddr;
511 		scr->scr_flags |= VCONS_DONT_READ;
512 	}
513 
514 	if (existing && sc->sc_want_clear) {
515 		ri->ri_flg |= RI_CLEAR;
516 	}
517 
518 	if (ri->ri_depth == 32) {
519 		bool is_bgr = false;
520 
521 		ri->ri_flg |= RI_ENABLE_ALPHA;
522 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
523 		    "is_bgr", &is_bgr);
524 		if (is_bgr) {
525 			/* someone requested BGR */
526 			ri->ri_rnum = 8;
527 			ri->ri_gnum = 8;
528 			ri->ri_bnum = 8;
529 			ri->ri_rpos = 0;
530 			ri->ri_gpos = 8;
531 			ri->ri_bpos = 16;
532 		} else {
533 			/* assume RGB */
534 			ri->ri_rnum = 8;
535 			ri->ri_gnum = 8;
536 			ri->ri_bnum = 8;
537 			ri->ri_rpos = 16;
538 			ri->ri_gpos = 8;
539 			ri->ri_bpos = 0;
540 		}
541 	}
542 
543 	if (ri->ri_depth == 8 && sc->sc_cmcb != NULL)
544 		ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
545 
546 
547 	rasops_init(ri, 0, 0);
548 	ri->ri_caps = WSSCREEN_WSCOLORS;
549 
550 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
551 		    sc->sc_width / ri->ri_font->fontwidth);
552 
553 	/* TODO: actually center output */
554 	ri->ri_hw = scr;
555 
556 #ifdef GENFB_DISABLE_TEXT
557 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
558 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
559 #endif
560 }
561 
562 static int
563 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
564 {
565 	u_char *r, *g, *b;
566 	u_int index = cm->index;
567 	u_int count = cm->count;
568 	int i, error;
569 	u_char rbuf[256], gbuf[256], bbuf[256];
570 
571 #ifdef GENFB_DEBUG
572 	aprint_debug("putcmap: %d %d\n",index, count);
573 #endif
574 	if (cm->index >= 256 || cm->count > 256 ||
575 	    (cm->index + cm->count) > 256)
576 		return EINVAL;
577 	error = copyin(cm->red, &rbuf[index], count);
578 	if (error)
579 		return error;
580 	error = copyin(cm->green, &gbuf[index], count);
581 	if (error)
582 		return error;
583 	error = copyin(cm->blue, &bbuf[index], count);
584 	if (error)
585 		return error;
586 
587 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
588 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
589 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
590 
591 	r = &sc->sc_cmap_red[index];
592 	g = &sc->sc_cmap_green[index];
593 	b = &sc->sc_cmap_blue[index];
594 
595 	for (i = 0; i < count; i++) {
596 		genfb_putpalreg(sc, index, *r, *g, *b);
597 		index++;
598 		r++, g++, b++;
599 	}
600 	return 0;
601 }
602 
603 static int
604 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
605 {
606 	u_int index = cm->index;
607 	u_int count = cm->count;
608 	int error;
609 
610 	if (index >= 255 || count > 256 || index + count > 256)
611 		return EINVAL;
612 
613 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
614 	if (error)
615 		return error;
616 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
617 	if (error)
618 		return error;
619 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
620 	if (error)
621 		return error;
622 
623 	return 0;
624 }
625 
626 void
627 genfb_restore_palette(struct genfb_softc *sc)
628 {
629 	int i;
630 
631 	if (sc->sc_depth <= 8) {
632 		for (i = 0; i < (1 << sc->sc_depth); i++) {
633 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
634 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
635 		}
636 	}
637 }
638 
639 static void
640 genfb_init_palette(struct genfb_softc *sc)
641 {
642 	int i, j, tmp;
643 
644 	if (sc->sc_depth == 8) {
645 		/* generate an r3g3b2 colour map */
646 		for (i = 0; i < 256; i++) {
647 			tmp = i & 0xe0;
648 			/*
649 			 * replicate bits so 0xe0 maps to a red value of 0xff
650 			 * in order to make white look actually white
651 			 */
652 			tmp |= (tmp >> 3) | (tmp >> 6);
653 			sc->sc_cmap_red[i] = tmp;
654 
655 			tmp = (i & 0x1c) << 3;
656 			tmp |= (tmp >> 3) | (tmp >> 6);
657 			sc->sc_cmap_green[i] = tmp;
658 
659 			tmp = (i & 0x03) << 6;
660 			tmp |= tmp >> 2;
661 			tmp |= tmp >> 4;
662 			sc->sc_cmap_blue[i] = tmp;
663 
664 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
665 				       sc->sc_cmap_green[i],
666 				       sc->sc_cmap_blue[i]);
667 		}
668 	} else {
669 		/* steal rasops' ANSI cmap */
670 		j = 0;
671 		for (i = 0; i < 256; i++) {
672 			sc->sc_cmap_red[i] = rasops_cmap[j];
673 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
674 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
675 			j += 3;
676 		}
677 	}
678 }
679 
680 static int
681 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
682     uint8_t b)
683 {
684 
685 	if (sc->sc_cmcb) {
686 
687 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
688 		    idx, r, g, b);
689 	}
690 	return 0;
691 }
692 
693 void
694 genfb_cnattach(void)
695 {
696 	genfb_cnattach_called = 1;
697 }
698 
699 void
700 genfb_disable(void)
701 {
702 	genfb_enabled = 0;
703 }
704 
705 int
706 genfb_is_console(void)
707 {
708 	return genfb_cnattach_called;
709 }
710 
711 int
712 genfb_is_enabled(void)
713 {
714 	return genfb_enabled;
715 }
716 
717 int
718 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
719 {
720 	struct genfb_softc *sc = genfb_softc;
721 
722 	if (sc && sc->sc_ops.genfb_borrow)
723 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
724 	return 0;
725 }
726 
727 static void
728 genfb_brightness_up(device_t dev)
729 {
730 	struct genfb_softc *sc = device_private(dev);
731 
732 	KASSERT(sc->sc_brightness != NULL &&
733 		sc->sc_brightness->gpc_upd_parameter != NULL);
734 
735 	(void)sc->sc_brightness->gpc_upd_parameter(
736 	    sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
737 }
738 
739 static void
740 genfb_brightness_down(device_t dev)
741 {
742 	struct genfb_softc *sc = device_private(dev);
743 
744 	KASSERT(sc->sc_brightness != NULL &&
745 		sc->sc_brightness->gpc_upd_parameter != NULL);
746 
747 	(void)sc->sc_brightness->gpc_upd_parameter(
748 	    sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
749 }
750 
751 void
752 genfb_enable_polling(device_t dev)
753 {
754 	struct genfb_softc *sc = device_private(dev);
755 
756 	if (sc->sc_console_screen.scr_vd) {
757 		SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
758 		vcons_hard_switch(&sc->sc_console_screen);
759 		vcons_enable_polling(&sc->vd);
760 	}
761 }
762 
763 void
764 genfb_disable_polling(device_t dev)
765 {
766 	struct genfb_softc *sc = device_private(dev);
767 
768 	if (sc->sc_console_screen.scr_vd) {
769 		vcons_disable_polling(&sc->vd);
770 	}
771 }
772