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