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