1 /* 2 * linux/drivers/video/fbmem.c 3 * 4 * Copyright (C) 1994 Martin Schaller 5 * 6 * 2001 - Documented with DocBook 7 * - Brad Douglas <brad@neruo.com> 8 * 9 * This file is subject to the terms and conditions of the GNU General Public 10 * License. See the file COPYING in the main directory of this archive 11 * for more details. 12 */ 13 14 #include <linux/console.h> 15 #include <linux/export.h> 16 #include <linux/fb.h> 17 #include <linux/fbcon.h> 18 #include <linux/lcd.h> 19 #include <linux/leds.h> 20 21 #include <video/nomodeset.h> 22 23 #include "fb_internal.h" 24 25 /* 26 * Frame buffer device initialization and setup routines 27 */ 28 29 #define FBPIXMAPSIZE (1024 * 8) 30 31 struct class *fb_class; 32 33 DEFINE_MUTEX(registration_lock); 34 struct fb_info *registered_fb[FB_MAX] __read_mostly; 35 int num_registered_fb __read_mostly; 36 #define for_each_registered_fb(i) \ 37 for (i = 0; i < FB_MAX; i++) \ 38 if (!registered_fb[i]) {} else 39 40 struct fb_info *get_fb_info(unsigned int idx) 41 { 42 struct fb_info *fb_info; 43 44 if (idx >= FB_MAX) 45 return ERR_PTR(-ENODEV); 46 47 mutex_lock(®istration_lock); 48 fb_info = registered_fb[idx]; 49 if (fb_info) 50 refcount_inc(&fb_info->count); 51 mutex_unlock(®istration_lock); 52 53 return fb_info; 54 } 55 56 void put_fb_info(struct fb_info *fb_info) 57 { 58 if (!refcount_dec_and_test(&fb_info->count)) 59 return; 60 if (fb_info->fbops->fb_destroy) 61 fb_info->fbops->fb_destroy(fb_info); 62 } 63 64 /* 65 * Helpers 66 */ 67 68 int fb_get_color_depth(struct fb_var_screeninfo *var, 69 struct fb_fix_screeninfo *fix) 70 { 71 int depth = 0; 72 73 if (fix->visual == FB_VISUAL_MONO01 || 74 fix->visual == FB_VISUAL_MONO10) 75 depth = 1; 76 else { 77 if (var->green.length == var->blue.length && 78 var->green.length == var->red.length && 79 var->green.offset == var->blue.offset && 80 var->green.offset == var->red.offset) 81 depth = var->green.length; 82 else 83 depth = var->green.length + var->red.length + 84 var->blue.length; 85 } 86 87 return depth; 88 } 89 EXPORT_SYMBOL(fb_get_color_depth); 90 91 /* 92 * Data padding functions. 93 */ 94 void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height) 95 { 96 __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height); 97 } 98 EXPORT_SYMBOL(fb_pad_aligned_buffer); 99 100 void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height, 101 u32 shift_high, u32 shift_low, u32 mod) 102 { 103 u8 mask = (u8) (0xfff << shift_high), tmp; 104 int i, j; 105 106 for (i = height; i--; ) { 107 for (j = 0; j < idx; j++) { 108 tmp = dst[j]; 109 tmp &= mask; 110 tmp |= *src >> shift_low; 111 dst[j] = tmp; 112 tmp = *src << shift_high; 113 dst[j+1] = tmp; 114 src++; 115 } 116 tmp = dst[idx]; 117 tmp &= mask; 118 tmp |= *src >> shift_low; 119 dst[idx] = tmp; 120 if (shift_high < mod) { 121 tmp = *src << shift_high; 122 dst[idx+1] = tmp; 123 } 124 src++; 125 dst += d_pitch; 126 } 127 } 128 EXPORT_SYMBOL(fb_pad_unaligned_buffer); 129 130 /* 131 * we need to lock this section since fb_cursor 132 * may use fb_imageblit() 133 */ 134 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size) 135 { 136 u32 align = buf->buf_align - 1, offset; 137 char *addr = buf->addr; 138 139 /* If IO mapped, we need to sync before access, no sharing of 140 * the pixmap is done 141 */ 142 if (buf->flags & FB_PIXMAP_IO) { 143 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC)) 144 info->fbops->fb_sync(info); 145 return addr; 146 } 147 148 /* See if we fit in the remaining pixmap space */ 149 offset = buf->offset + align; 150 offset &= ~align; 151 if (offset + size > buf->size) { 152 /* We do not fit. In order to be able to re-use the buffer, 153 * we must ensure no asynchronous DMA'ing or whatever operation 154 * is in progress, we sync for that. 155 */ 156 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC)) 157 info->fbops->fb_sync(info); 158 offset = 0; 159 } 160 buf->offset = offset + size; 161 addr += offset; 162 163 return addr; 164 } 165 EXPORT_SYMBOL(fb_get_buffer_offset); 166 167 int 168 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var) 169 { 170 struct fb_fix_screeninfo *fix = &info->fix; 171 unsigned int yres = info->var.yres; 172 int err = 0; 173 174 if (var->yoffset > 0) { 175 if (var->vmode & FB_VMODE_YWRAP) { 176 if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep)) 177 err = -EINVAL; 178 else 179 yres = 0; 180 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep)) 181 err = -EINVAL; 182 } 183 184 if (var->xoffset > 0 && (!fix->xpanstep || 185 (var->xoffset % fix->xpanstep))) 186 err = -EINVAL; 187 188 if (err || !info->fbops->fb_pan_display || 189 var->yoffset > info->var.yres_virtual - yres || 190 var->xoffset > info->var.xres_virtual - info->var.xres) 191 return -EINVAL; 192 193 if ((err = info->fbops->fb_pan_display(var, info))) 194 return err; 195 info->var.xoffset = var->xoffset; 196 info->var.yoffset = var->yoffset; 197 if (var->vmode & FB_VMODE_YWRAP) 198 info->var.vmode |= FB_VMODE_YWRAP; 199 else 200 info->var.vmode &= ~FB_VMODE_YWRAP; 201 return 0; 202 } 203 EXPORT_SYMBOL(fb_pan_display); 204 205 static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var, 206 u32 activate) 207 { 208 struct fb_blit_caps caps, fbcaps; 209 int err = 0; 210 211 memset(&caps, 0, sizeof(caps)); 212 memset(&fbcaps, 0, sizeof(fbcaps)); 213 caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0; 214 fbcon_get_requirement(info, &caps); 215 info->fbops->fb_get_caps(info, &fbcaps, var); 216 217 if (!bitmap_subset(caps.x, fbcaps.x, FB_MAX_BLIT_WIDTH) || 218 !bitmap_subset(caps.y, fbcaps.y, FB_MAX_BLIT_HEIGHT) || 219 (fbcaps.len < caps.len)) 220 err = -EINVAL; 221 222 return err; 223 } 224 225 static void fb_lcd_notify_mode_change(struct fb_info *info, 226 struct fb_videomode *mode) 227 { 228 lcd_notify_mode_change_all(info->device, mode->xres, mode->yres); 229 } 230 231 int 232 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var) 233 { 234 int ret = 0; 235 u32 activate; 236 struct fb_var_screeninfo old_var; 237 struct fb_videomode mode; 238 u32 unused; 239 240 if (var->activate & FB_ACTIVATE_INV_MODE) { 241 struct fb_videomode mode1, mode2; 242 243 fb_var_to_videomode(&mode1, var); 244 fb_var_to_videomode(&mode2, &info->var); 245 /* make sure we don't delete the videomode of current var */ 246 ret = fb_mode_is_equal(&mode1, &mode2); 247 if (!ret) { 248 ret = fbcon_mode_deleted(info, &mode1); 249 if (!ret) 250 fb_delete_videomode(&mode1, &info->modelist); 251 } 252 253 return ret ? -EINVAL : 0; 254 } 255 256 if (!(var->activate & FB_ACTIVATE_FORCE) && 257 !memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) 258 return 0; 259 260 activate = var->activate; 261 262 /* When using FOURCC mode, make sure the red, green, blue and 263 * transp fields are set to 0. 264 */ 265 if ((info->fix.capabilities & FB_CAP_FOURCC) && 266 var->grayscale > 1) { 267 if (var->red.offset || var->green.offset || 268 var->blue.offset || var->transp.offset || 269 var->red.length || var->green.length || 270 var->blue.length || var->transp.length || 271 var->red.msb_right || var->green.msb_right || 272 var->blue.msb_right || var->transp.msb_right) 273 return -EINVAL; 274 } 275 276 if (!info->fbops->fb_check_var) { 277 *var = info->var; 278 return 0; 279 } 280 281 /* bitfill_aligned() assumes that it's at least 8x8 */ 282 if (var->xres < 8 || var->yres < 8) 283 return -EINVAL; 284 285 /* Too huge resolution causes multiplication overflow. */ 286 if (check_mul_overflow(var->xres, var->yres, &unused) || 287 check_mul_overflow(var->xres_virtual, var->yres_virtual, &unused)) 288 return -EINVAL; 289 290 ret = info->fbops->fb_check_var(var, info); 291 292 if (ret) 293 return ret; 294 295 /* verify that virtual resolution >= physical resolution */ 296 if (var->xres_virtual < var->xres || 297 var->yres_virtual < var->yres) { 298 pr_warn("WARNING: fbcon: Driver '%s' missed to adjust virtual screen size (%ux%u vs. %ux%u)\n", 299 info->fix.id, 300 var->xres_virtual, var->yres_virtual, 301 var->xres, var->yres); 302 return -EINVAL; 303 } 304 305 if ((var->activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW) 306 return 0; 307 308 if (info->fbops->fb_get_caps) { 309 ret = fb_check_caps(info, var, activate); 310 311 if (ret) 312 return ret; 313 } 314 315 old_var = info->var; 316 info->var = *var; 317 318 if (info->fbops->fb_set_par) { 319 ret = info->fbops->fb_set_par(info); 320 321 if (ret) { 322 info->var = old_var; 323 printk(KERN_WARNING "detected " 324 "fb_set_par error, " 325 "error code: %d\n", ret); 326 return ret; 327 } 328 } 329 330 fb_pan_display(info, &info->var); 331 fb_set_cmap(&info->cmap, info); 332 fb_var_to_videomode(&mode, &info->var); 333 334 if (info->modelist.prev && info->modelist.next && 335 !list_empty(&info->modelist)) 336 ret = fb_add_videomode(&mode, &info->modelist); 337 338 if (ret) { 339 info->var = old_var; 340 return ret; 341 } 342 343 fb_lcd_notify_mode_change(info, &mode); 344 345 return 0; 346 } 347 EXPORT_SYMBOL(fb_set_var); 348 349 static void fb_lcd_notify_blank(struct fb_info *info) 350 { 351 int power; 352 353 switch (info->blank) { 354 case FB_BLANK_UNBLANK: 355 power = LCD_POWER_ON; 356 break; 357 /* deprecated; TODO: should become 'off' */ 358 case FB_BLANK_NORMAL: 359 power = LCD_POWER_REDUCED; 360 break; 361 case FB_BLANK_VSYNC_SUSPEND: 362 power = LCD_POWER_REDUCED_VSYNC_SUSPEND; 363 break; 364 /* 'off' */ 365 case FB_BLANK_HSYNC_SUSPEND: 366 case FB_BLANK_POWERDOWN: 367 default: 368 power = LCD_POWER_OFF; 369 break; 370 } 371 372 lcd_notify_blank_all(info->device, power); 373 } 374 375 static void fb_ledtrig_backlight_notify_blank(struct fb_info *info) 376 { 377 if (info->blank == FB_BLANK_UNBLANK) 378 ledtrig_backlight_blank(false); 379 else 380 ledtrig_backlight_blank(true); 381 } 382 383 int fb_blank(struct fb_info *info, int blank) 384 { 385 int old_blank = info->blank; 386 int ret; 387 388 if (!info->fbops->fb_blank) 389 return -EINVAL; 390 391 if (blank > FB_BLANK_POWERDOWN) 392 blank = FB_BLANK_POWERDOWN; 393 394 info->blank = blank; 395 396 ret = info->fbops->fb_blank(blank, info); 397 if (ret) 398 goto err; 399 400 fb_bl_notify_blank(info, old_blank); 401 fb_lcd_notify_blank(info); 402 fb_ledtrig_backlight_notify_blank(info); 403 404 return 0; 405 406 err: 407 info->blank = old_blank; 408 return ret; 409 } 410 EXPORT_SYMBOL(fb_blank); 411 412 static int fb_check_foreignness(struct fb_info *fi) 413 { 414 const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN; 415 416 fi->flags &= ~FBINFO_FOREIGN_ENDIAN; 417 418 #ifdef __BIG_ENDIAN 419 fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH; 420 #else 421 fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0; 422 #endif /* __BIG_ENDIAN */ 423 424 if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) { 425 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to " 426 "support this framebuffer\n", fi->fix.id); 427 return -ENOSYS; 428 } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) { 429 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to " 430 "support this framebuffer\n", fi->fix.id); 431 return -ENOSYS; 432 } 433 434 return 0; 435 } 436 437 static int do_register_framebuffer(struct fb_info *fb_info) 438 { 439 int i, err = 0; 440 struct fb_videomode mode; 441 442 if (fb_check_foreignness(fb_info)) 443 return -ENOSYS; 444 445 if (num_registered_fb == FB_MAX) 446 return -ENXIO; 447 448 for (i = 0 ; i < FB_MAX; i++) 449 if (!registered_fb[i]) 450 break; 451 452 if (!fb_info->modelist.prev || !fb_info->modelist.next) 453 INIT_LIST_HEAD(&fb_info->modelist); 454 455 fb_var_to_videomode(&mode, &fb_info->var); 456 err = fb_add_videomode(&mode, &fb_info->modelist); 457 if (err < 0) 458 return err; 459 460 fb_info->node = i; 461 refcount_set(&fb_info->count, 1); 462 mutex_init(&fb_info->lock); 463 mutex_init(&fb_info->mm_lock); 464 465 /* 466 * With an fb_blank callback present, we assume that the 467 * display is blank, so that fb_blank() enables it on the 468 * first modeset. 469 */ 470 if (fb_info->fbops->fb_blank) 471 fb_info->blank = FB_BLANK_POWERDOWN; 472 473 fb_device_create(fb_info); 474 475 if (fb_info->pixmap.addr == NULL) { 476 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL); 477 if (fb_info->pixmap.addr) { 478 fb_info->pixmap.size = FBPIXMAPSIZE; 479 fb_info->pixmap.buf_align = 1; 480 fb_info->pixmap.scan_align = 1; 481 fb_info->pixmap.access_align = 32; 482 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT; 483 } 484 } 485 fb_info->pixmap.offset = 0; 486 487 if (bitmap_empty(fb_info->pixmap.blit_x, FB_MAX_BLIT_WIDTH)) 488 bitmap_fill(fb_info->pixmap.blit_x, FB_MAX_BLIT_WIDTH); 489 490 if (bitmap_empty(fb_info->pixmap.blit_y, FB_MAX_BLIT_HEIGHT)) 491 bitmap_fill(fb_info->pixmap.blit_y, FB_MAX_BLIT_HEIGHT); 492 493 if (fb_info->skip_vt_switch) 494 pm_vt_switch_required(fb_info->device, false); 495 else 496 pm_vt_switch_required(fb_info->device, true); 497 498 num_registered_fb++; 499 registered_fb[i] = fb_info; 500 501 #ifdef CONFIG_GUMSTIX_AM200EPD 502 { 503 struct fb_event event; 504 event.info = fb_info; 505 fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event); 506 } 507 #endif 508 509 return fbcon_fb_registered(fb_info); 510 } 511 512 static void unbind_console(struct fb_info *fb_info) 513 { 514 int i = fb_info->node; 515 516 if (WARN_ON(i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)) 517 return; 518 519 fbcon_fb_unbind(fb_info); 520 } 521 522 static void unlink_framebuffer(struct fb_info *fb_info) 523 { 524 int i; 525 526 i = fb_info->node; 527 if (WARN_ON(i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)) 528 return; 529 530 fb_device_destroy(fb_info); 531 pm_vt_switch_unregister(fb_info->device); 532 unbind_console(fb_info); 533 } 534 535 static void do_unregister_framebuffer(struct fb_info *fb_info) 536 { 537 unlink_framebuffer(fb_info); 538 if (fb_info->pixmap.addr && 539 (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT)) { 540 kfree(fb_info->pixmap.addr); 541 fb_info->pixmap.addr = NULL; 542 } 543 544 fb_destroy_modelist(&fb_info->modelist); 545 registered_fb[fb_info->node] = NULL; 546 num_registered_fb--; 547 #ifdef CONFIG_GUMSTIX_AM200EPD 548 { 549 struct fb_event event; 550 event.info = fb_info; 551 fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event); 552 } 553 #endif 554 fbcon_fb_unregistered(fb_info); 555 556 /* this may free fb info */ 557 put_fb_info(fb_info); 558 } 559 560 /** 561 * register_framebuffer - registers a frame buffer device 562 * @fb_info: frame buffer info structure 563 * 564 * Registers a frame buffer device @fb_info. 565 * 566 * Returns negative errno on error, or zero for success. 567 * 568 */ 569 int 570 register_framebuffer(struct fb_info *fb_info) 571 { 572 int ret; 573 574 mutex_lock(®istration_lock); 575 ret = do_register_framebuffer(fb_info); 576 mutex_unlock(®istration_lock); 577 578 return ret; 579 } 580 EXPORT_SYMBOL(register_framebuffer); 581 582 /** 583 * unregister_framebuffer - releases a frame buffer device 584 * @fb_info: frame buffer info structure 585 * 586 * Unregisters a frame buffer device @fb_info. 587 * 588 * Returns negative errno on error, or zero for success. 589 * 590 * This function will also notify the framebuffer console 591 * to release the driver. 592 * 593 * This is meant to be called within a driver's module_exit() 594 * function. If this is called outside module_exit(), ensure 595 * that the driver implements fb_open() and fb_release() to 596 * check that no processes are using the device. 597 */ 598 void 599 unregister_framebuffer(struct fb_info *fb_info) 600 { 601 mutex_lock(®istration_lock); 602 do_unregister_framebuffer(fb_info); 603 mutex_unlock(®istration_lock); 604 } 605 EXPORT_SYMBOL(unregister_framebuffer); 606 607 static void devm_unregister_framebuffer(void *data) 608 { 609 struct fb_info *info = data; 610 611 unregister_framebuffer(info); 612 } 613 614 /** 615 * devm_register_framebuffer - resource-managed frame buffer device registration 616 * @dev: device the framebuffer belongs to 617 * @fb_info: frame buffer info structure 618 * 619 * Registers a frame buffer device @fb_info to device @dev. 620 * 621 * Returns negative errno on error, or zero for success. 622 * 623 */ 624 int 625 devm_register_framebuffer(struct device *dev, struct fb_info *fb_info) 626 { 627 int ret; 628 629 ret = register_framebuffer(fb_info); 630 if (ret) 631 return ret; 632 633 return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info); 634 } 635 EXPORT_SYMBOL(devm_register_framebuffer); 636 637 /** 638 * fb_set_suspend - low level driver signals suspend 639 * @info: framebuffer affected 640 * @state: 0 = resuming, !=0 = suspending 641 * 642 * This is meant to be used by low level drivers to 643 * signal suspend/resume to the core & clients. 644 * It must be called with the console semaphore held 645 */ 646 void fb_set_suspend(struct fb_info *info, int state) 647 { 648 WARN_CONSOLE_UNLOCKED(); 649 650 if (state) { 651 fbcon_suspended(info); 652 info->state = FBINFO_STATE_SUSPENDED; 653 } else { 654 info->state = FBINFO_STATE_RUNNING; 655 fbcon_resumed(info); 656 } 657 } 658 EXPORT_SYMBOL(fb_set_suspend); 659 660 static int __init fbmem_init(void) 661 { 662 int ret; 663 664 fb_class = class_create("graphics"); 665 if (IS_ERR(fb_class)) { 666 ret = PTR_ERR(fb_class); 667 pr_err("Unable to create fb class; errno = %d\n", ret); 668 goto err_fb_class; 669 } 670 671 ret = fb_init_procfs(); 672 if (ret) 673 goto err_class_destroy; 674 675 ret = fb_register_chrdev(); 676 if (ret) 677 goto err_fb_cleanup_procfs; 678 679 fb_console_init(); 680 681 return 0; 682 683 err_fb_cleanup_procfs: 684 fb_cleanup_procfs(); 685 err_class_destroy: 686 class_destroy(fb_class); 687 err_fb_class: 688 fb_class = NULL; 689 return ret; 690 } 691 692 #ifdef MODULE 693 static void __exit fbmem_exit(void) 694 { 695 fb_console_exit(); 696 fb_unregister_chrdev(); 697 fb_cleanup_procfs(); 698 class_destroy(fb_class); 699 } 700 701 module_init(fbmem_init); 702 module_exit(fbmem_exit); 703 MODULE_LICENSE("GPL"); 704 MODULE_DESCRIPTION("Framebuffer base"); 705 #else 706 subsys_initcall(fbmem_init); 707 #endif 708 709 int fb_new_modelist(struct fb_info *info) 710 { 711 struct fb_var_screeninfo var = info->var; 712 struct list_head *pos, *n; 713 struct fb_modelist *modelist; 714 struct fb_videomode *m, mode; 715 int err; 716 717 list_for_each_safe(pos, n, &info->modelist) { 718 modelist = list_entry(pos, struct fb_modelist, list); 719 m = &modelist->mode; 720 fb_videomode_to_var(&var, m); 721 var.activate = FB_ACTIVATE_TEST; 722 err = fb_set_var(info, &var); 723 fb_var_to_videomode(&mode, &var); 724 if (err || !fb_mode_is_equal(m, &mode)) { 725 list_del(pos); 726 kfree(pos); 727 } 728 } 729 730 if (list_empty(&info->modelist)) 731 return 1; 732 733 fbcon_new_modelist(info); 734 735 return 0; 736 } 737 738 bool fb_modesetting_disabled(const char *drvname) 739 { 740 bool fwonly = video_firmware_drivers_only(); 741 742 if (fwonly) 743 pr_warn("Driver %s not loading because of nomodeset parameter\n", 744 drvname); 745 746 return fwonly; 747 } 748 EXPORT_SYMBOL(fb_modesetting_disabled); 749 750 MODULE_LICENSE("GPL"); 751