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