1 /* $NetBSD: genfb.c,v 1.47 2012/02/07 18:48:19 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.47 2012/02/07 18:48:19 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 #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) 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 case WSDISPLAYIO_SSPLASH: 398 #if defined(SPLASHSCREEN) 399 if(*(int *)data == 1) { 400 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen); 401 splash_render(&sc->sc_splash, 402 SPLASH_F_CENTER|SPLASH_F_FILL); 403 } else { 404 SCREEN_ENABLE_DRAWING(&sc->sc_console_screen); 405 genfb_init_palette(sc); 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 switch (param->param) { 415 case WSDISPLAYIO_PARAM_BRIGHTNESS: 416 if (sc->sc_brightness == NULL) 417 return EPASSTHROUGH; 418 param->min = 0; 419 param->max = 255; 420 return sc->sc_brightness->gpc_get_parameter( 421 sc->sc_brightness->gpc_cookie, 422 ¶m->curval); 423 case WSDISPLAYIO_PARAM_BACKLIGHT: 424 if (sc->sc_backlight == NULL) 425 return EPASSTHROUGH; 426 param->min = 0; 427 param->max = 1; 428 return sc->sc_backlight->gpc_get_parameter( 429 sc->sc_backlight->gpc_cookie, 430 ¶m->curval); 431 } 432 return EPASSTHROUGH; 433 434 case WSDISPLAYIO_SETPARAM: 435 param = (struct wsdisplay_param *)data; 436 switch (param->param) { 437 case WSDISPLAYIO_PARAM_BRIGHTNESS: 438 if (sc->sc_brightness == NULL) 439 return EPASSTHROUGH; 440 val = param->curval; 441 if (val < 0) val = 0; 442 if (val > 255) val = 255; 443 return sc->sc_brightness->gpc_set_parameter( 444 sc->sc_brightness->gpc_cookie, val); 445 case WSDISPLAYIO_PARAM_BACKLIGHT: 446 if (sc->sc_backlight == NULL) 447 return EPASSTHROUGH; 448 val = param->curval; 449 if (val < 0) val = 0; 450 if (val > 1) val = 1; 451 return sc->sc_backlight->gpc_set_parameter( 452 sc->sc_backlight->gpc_cookie, val); 453 } 454 return EPASSTHROUGH; 455 case WSDISPLAYIO_GET_EDID: { 456 struct wsdisplayio_edid_info *d = data; 457 return wsdisplayio_get_edid(sc->sc_dev, d); 458 } 459 default: 460 if (sc->sc_ops.genfb_ioctl) 461 return sc->sc_ops.genfb_ioctl(sc, vs, cmd, 462 data, flag, l); 463 } 464 return EPASSTHROUGH; 465 } 466 467 static paddr_t 468 genfb_mmap(void *v, void *vs, off_t offset, int prot) 469 { 470 struct vcons_data *vd = v; 471 struct genfb_softc *sc = vd->cookie; 472 473 if (sc->sc_ops.genfb_mmap) 474 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot); 475 476 return -1; 477 } 478 479 static void 480 genfb_init_screen(void *cookie, struct vcons_screen *scr, 481 int existing, long *defattr) 482 { 483 struct genfb_softc *sc = cookie; 484 struct rasops_info *ri = &scr->scr_ri; 485 486 ri->ri_depth = sc->sc_depth; 487 ri->ri_width = sc->sc_width; 488 ri->ri_height = sc->sc_height; 489 ri->ri_stride = sc->sc_stride; 490 ri->ri_flg = RI_CENTER; 491 if (sc->sc_want_clear) 492 ri->ri_flg |= RI_FULLCLEAR; 493 494 #ifdef GENFB_SHADOWFB 495 if (sc->sc_shadowfb != NULL) { 496 497 ri->ri_hwbits = (char *)sc->sc_fbaddr; 498 ri->ri_bits = (char *)sc->sc_shadowfb; 499 } else 500 #endif 501 { 502 ri->ri_bits = (char *)sc->sc_fbaddr; 503 scr->scr_flags |= VCONS_DONT_READ; 504 } 505 506 if (existing && sc->sc_want_clear) { 507 ri->ri_flg |= RI_CLEAR; 508 } 509 510 if (ri->ri_depth == 32) 511 ri->ri_flg |= RI_ENABLE_ALPHA; 512 513 if (ri->ri_depth == 8 && sc->sc_cmcb != NULL) 514 ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB; 515 516 517 rasops_init(ri, 0, 0); 518 ri->ri_caps = WSSCREEN_WSCOLORS; 519 520 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 521 sc->sc_width / ri->ri_font->fontwidth); 522 523 /* TODO: actually center output */ 524 ri->ri_hw = scr; 525 526 #ifdef GENFB_DISABLE_TEXT 527 if (scr == &sc->sc_console_screen && !DISABLESPLASH) 528 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen); 529 #endif 530 } 531 532 static int 533 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm) 534 { 535 u_char *r, *g, *b; 536 u_int index = cm->index; 537 u_int count = cm->count; 538 int i, error; 539 u_char rbuf[256], gbuf[256], bbuf[256]; 540 541 #ifdef GENFB_DEBUG 542 aprint_debug("putcmap: %d %d\n",index, count); 543 #endif 544 if (cm->index >= 256 || cm->count > 256 || 545 (cm->index + cm->count) > 256) 546 return EINVAL; 547 error = copyin(cm->red, &rbuf[index], count); 548 if (error) 549 return error; 550 error = copyin(cm->green, &gbuf[index], count); 551 if (error) 552 return error; 553 error = copyin(cm->blue, &bbuf[index], count); 554 if (error) 555 return error; 556 557 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 558 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 559 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 560 561 r = &sc->sc_cmap_red[index]; 562 g = &sc->sc_cmap_green[index]; 563 b = &sc->sc_cmap_blue[index]; 564 565 for (i = 0; i < count; i++) { 566 genfb_putpalreg(sc, index, *r, *g, *b); 567 index++; 568 r++, g++, b++; 569 } 570 return 0; 571 } 572 573 static int 574 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm) 575 { 576 u_int index = cm->index; 577 u_int count = cm->count; 578 int error; 579 580 if (index >= 255 || count > 256 || index + count > 256) 581 return EINVAL; 582 583 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 584 if (error) 585 return error; 586 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 587 if (error) 588 return error; 589 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 590 if (error) 591 return error; 592 593 return 0; 594 } 595 596 void 597 genfb_restore_palette(struct genfb_softc *sc) 598 { 599 int i; 600 601 if (sc->sc_depth <= 8) { 602 for (i = 0; i < (1 << sc->sc_depth); i++) { 603 genfb_putpalreg(sc, i, sc->sc_cmap_red[i], 604 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 605 } 606 } 607 } 608 609 static void 610 genfb_init_palette(struct genfb_softc *sc) 611 { 612 int i, j, tmp; 613 614 if (sc->sc_depth == 8) { 615 /* generate an r3g3b2 colour map */ 616 for (i = 0; i < 256; i++) { 617 tmp = i & 0xe0; 618 /* 619 * replicate bits so 0xe0 maps to a red value of 0xff 620 * in order to make white look actually white 621 */ 622 tmp |= (tmp >> 3) | (tmp >> 6); 623 sc->sc_cmap_red[i] = tmp; 624 625 tmp = (i & 0x1c) << 3; 626 tmp |= (tmp >> 3) | (tmp >> 6); 627 sc->sc_cmap_green[i] = tmp; 628 629 tmp = (i & 0x03) << 6; 630 tmp |= tmp >> 2; 631 tmp |= tmp >> 4; 632 sc->sc_cmap_blue[i] = tmp; 633 634 genfb_putpalreg(sc, i, sc->sc_cmap_red[i], 635 sc->sc_cmap_green[i], 636 sc->sc_cmap_blue[i]); 637 } 638 } else { 639 /* steal rasops' ANSI cmap */ 640 j = 0; 641 for (i = 0; i < 256; i++) { 642 sc->sc_cmap_red[i] = rasops_cmap[j]; 643 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 644 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 645 j += 3; 646 } 647 } 648 } 649 650 static int 651 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 652 uint8_t b) 653 { 654 655 if (sc->sc_cmcb) { 656 657 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie, 658 idx, r, g, b); 659 } 660 return 0; 661 } 662 663 void 664 genfb_cnattach(void) 665 { 666 genfb_cnattach_called = 1; 667 } 668 669 void 670 genfb_disable(void) 671 { 672 genfb_enabled = 0; 673 } 674 675 int 676 genfb_is_console(void) 677 { 678 return genfb_cnattach_called; 679 } 680 681 int 682 genfb_is_enabled(void) 683 { 684 return genfb_enabled; 685 } 686 687 int 688 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp) 689 { 690 struct genfb_softc *sc = genfb_softc; 691 692 if (sc && sc->sc_ops.genfb_borrow) 693 return sc->sc_ops.genfb_borrow(sc, addr, hdlp); 694 return 0; 695 } 696 697 static void 698 genfb_brightness_up(device_t dev) 699 { 700 struct genfb_softc *sc = device_private(dev); 701 702 KASSERT(sc->sc_brightness != NULL && 703 sc->sc_brightness->gpc_upd_parameter != NULL); 704 705 (void)sc->sc_brightness->gpc_upd_parameter( 706 sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP); 707 } 708 709 static void 710 genfb_brightness_down(device_t dev) 711 { 712 struct genfb_softc *sc = device_private(dev); 713 714 KASSERT(sc->sc_brightness != NULL && 715 sc->sc_brightness->gpc_upd_parameter != NULL); 716 717 (void)sc->sc_brightness->gpc_upd_parameter( 718 sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP); 719 } 720 721 void 722 genfb_enable_polling(device_t dev) 723 { 724 struct genfb_softc *sc = device_private(dev); 725 726 if (sc->sc_console_screen.scr_vd) { 727 SCREEN_ENABLE_DRAWING(&sc->sc_console_screen); 728 vcons_hard_switch(&sc->sc_console_screen); 729 vcons_enable_polling(&sc->vd); 730 } 731 } 732 733 void 734 genfb_disable_polling(device_t dev) 735 { 736 struct genfb_softc *sc = device_private(dev); 737 738 if (sc->sc_console_screen.scr_vd) { 739 vcons_disable_polling(&sc->vd); 740 } 741 } 742