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/module.h> 15 16 #include <linux/compat.h> 17 #include <linux/types.h> 18 #include <linux/errno.h> 19 #include <linux/kernel.h> 20 #include <linux/major.h> 21 #include <linux/slab.h> 22 #include <linux/sysfb.h> 23 #include <linux/mm.h> 24 #include <linux/mman.h> 25 #include <linux/vt.h> 26 #include <linux/init.h> 27 #include <linux/linux_logo.h> 28 #include <linux/proc_fs.h> 29 #include <linux/platform_device.h> 30 #include <linux/seq_file.h> 31 #include <linux/console.h> 32 #include <linux/kmod.h> 33 #include <linux/err.h> 34 #include <linux/device.h> 35 #include <linux/efi.h> 36 #include <linux/fb.h> 37 #include <linux/fbcon.h> 38 #include <linux/mem_encrypt.h> 39 #include <linux/pci.h> 40 41 #include <asm/fb.h> 42 43 44 /* 45 * Frame buffer device initialization and setup routines 46 */ 47 48 #define FBPIXMAPSIZE (1024 * 8) 49 50 static DEFINE_MUTEX(registration_lock); 51 52 struct fb_info *registered_fb[FB_MAX] __read_mostly; 53 EXPORT_SYMBOL(registered_fb); 54 55 int num_registered_fb __read_mostly; 56 EXPORT_SYMBOL(num_registered_fb); 57 58 bool fb_center_logo __read_mostly; 59 60 int fb_logo_count __read_mostly = -1; 61 62 static struct fb_info *get_fb_info(unsigned int idx) 63 { 64 struct fb_info *fb_info; 65 66 if (idx >= FB_MAX) 67 return ERR_PTR(-ENODEV); 68 69 mutex_lock(®istration_lock); 70 fb_info = registered_fb[idx]; 71 if (fb_info) 72 refcount_inc(&fb_info->count); 73 mutex_unlock(®istration_lock); 74 75 return fb_info; 76 } 77 78 static void put_fb_info(struct fb_info *fb_info) 79 { 80 if (!refcount_dec_and_test(&fb_info->count)) 81 return; 82 if (fb_info->fbops->fb_destroy) 83 fb_info->fbops->fb_destroy(fb_info); 84 } 85 86 /* 87 * Helpers 88 */ 89 90 int fb_get_color_depth(struct fb_var_screeninfo *var, 91 struct fb_fix_screeninfo *fix) 92 { 93 int depth = 0; 94 95 if (fix->visual == FB_VISUAL_MONO01 || 96 fix->visual == FB_VISUAL_MONO10) 97 depth = 1; 98 else { 99 if (var->green.length == var->blue.length && 100 var->green.length == var->red.length && 101 var->green.offset == var->blue.offset && 102 var->green.offset == var->red.offset) 103 depth = var->green.length; 104 else 105 depth = var->green.length + var->red.length + 106 var->blue.length; 107 } 108 109 return depth; 110 } 111 EXPORT_SYMBOL(fb_get_color_depth); 112 113 /* 114 * Data padding functions. 115 */ 116 void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height) 117 { 118 __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height); 119 } 120 EXPORT_SYMBOL(fb_pad_aligned_buffer); 121 122 void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height, 123 u32 shift_high, u32 shift_low, u32 mod) 124 { 125 u8 mask = (u8) (0xfff << shift_high), tmp; 126 int i, j; 127 128 for (i = height; i--; ) { 129 for (j = 0; j < idx; j++) { 130 tmp = dst[j]; 131 tmp &= mask; 132 tmp |= *src >> shift_low; 133 dst[j] = tmp; 134 tmp = *src << shift_high; 135 dst[j+1] = tmp; 136 src++; 137 } 138 tmp = dst[idx]; 139 tmp &= mask; 140 tmp |= *src >> shift_low; 141 dst[idx] = tmp; 142 if (shift_high < mod) { 143 tmp = *src << shift_high; 144 dst[idx+1] = tmp; 145 } 146 src++; 147 dst += d_pitch; 148 } 149 } 150 EXPORT_SYMBOL(fb_pad_unaligned_buffer); 151 152 /* 153 * we need to lock this section since fb_cursor 154 * may use fb_imageblit() 155 */ 156 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size) 157 { 158 u32 align = buf->buf_align - 1, offset; 159 char *addr = buf->addr; 160 161 /* If IO mapped, we need to sync before access, no sharing of 162 * the pixmap is done 163 */ 164 if (buf->flags & FB_PIXMAP_IO) { 165 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC)) 166 info->fbops->fb_sync(info); 167 return addr; 168 } 169 170 /* See if we fit in the remaining pixmap space */ 171 offset = buf->offset + align; 172 offset &= ~align; 173 if (offset + size > buf->size) { 174 /* We do not fit. In order to be able to re-use the buffer, 175 * we must ensure no asynchronous DMA'ing or whatever operation 176 * is in progress, we sync for that. 177 */ 178 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC)) 179 info->fbops->fb_sync(info); 180 offset = 0; 181 } 182 buf->offset = offset + size; 183 addr += offset; 184 185 return addr; 186 } 187 EXPORT_SYMBOL(fb_get_buffer_offset); 188 189 #ifdef CONFIG_LOGO 190 191 static inline unsigned safe_shift(unsigned d, int n) 192 { 193 return n < 0 ? d >> -n : d << n; 194 } 195 196 static void fb_set_logocmap(struct fb_info *info, 197 const struct linux_logo *logo) 198 { 199 struct fb_cmap palette_cmap; 200 u16 palette_green[16]; 201 u16 palette_blue[16]; 202 u16 palette_red[16]; 203 int i, j, n; 204 const unsigned char *clut = logo->clut; 205 206 palette_cmap.start = 0; 207 palette_cmap.len = 16; 208 palette_cmap.red = palette_red; 209 palette_cmap.green = palette_green; 210 palette_cmap.blue = palette_blue; 211 palette_cmap.transp = NULL; 212 213 for (i = 0; i < logo->clutsize; i += n) { 214 n = logo->clutsize - i; 215 /* palette_cmap provides space for only 16 colors at once */ 216 if (n > 16) 217 n = 16; 218 palette_cmap.start = 32 + i; 219 palette_cmap.len = n; 220 for (j = 0; j < n; ++j) { 221 palette_cmap.red[j] = clut[0] << 8 | clut[0]; 222 palette_cmap.green[j] = clut[1] << 8 | clut[1]; 223 palette_cmap.blue[j] = clut[2] << 8 | clut[2]; 224 clut += 3; 225 } 226 fb_set_cmap(&palette_cmap, info); 227 } 228 } 229 230 static void fb_set_logo_truepalette(struct fb_info *info, 231 const struct linux_logo *logo, 232 u32 *palette) 233 { 234 static const unsigned char mask[] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff }; 235 unsigned char redmask, greenmask, bluemask; 236 int redshift, greenshift, blueshift; 237 int i; 238 const unsigned char *clut = logo->clut; 239 240 /* 241 * We have to create a temporary palette since console palette is only 242 * 16 colors long. 243 */ 244 /* Bug: Doesn't obey msb_right ... (who needs that?) */ 245 redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8]; 246 greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8]; 247 bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8]; 248 redshift = info->var.red.offset - (8 - info->var.red.length); 249 greenshift = info->var.green.offset - (8 - info->var.green.length); 250 blueshift = info->var.blue.offset - (8 - info->var.blue.length); 251 252 for ( i = 0; i < logo->clutsize; i++) { 253 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) | 254 safe_shift((clut[1] & greenmask), greenshift) | 255 safe_shift((clut[2] & bluemask), blueshift)); 256 clut += 3; 257 } 258 } 259 260 static void fb_set_logo_directpalette(struct fb_info *info, 261 const struct linux_logo *logo, 262 u32 *palette) 263 { 264 int redshift, greenshift, blueshift; 265 int i; 266 267 redshift = info->var.red.offset; 268 greenshift = info->var.green.offset; 269 blueshift = info->var.blue.offset; 270 271 for (i = 32; i < 32 + logo->clutsize; i++) 272 palette[i] = i << redshift | i << greenshift | i << blueshift; 273 } 274 275 static void fb_set_logo(struct fb_info *info, 276 const struct linux_logo *logo, u8 *dst, 277 int depth) 278 { 279 int i, j, k; 280 const u8 *src = logo->data; 281 u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0; 282 u8 fg = 1, d; 283 284 switch (fb_get_color_depth(&info->var, &info->fix)) { 285 case 1: 286 fg = 1; 287 break; 288 case 2: 289 fg = 3; 290 break; 291 default: 292 fg = 7; 293 break; 294 } 295 296 if (info->fix.visual == FB_VISUAL_MONO01 || 297 info->fix.visual == FB_VISUAL_MONO10) 298 fg = ~((u8) (0xfff << info->var.green.length)); 299 300 switch (depth) { 301 case 4: 302 for (i = 0; i < logo->height; i++) 303 for (j = 0; j < logo->width; src++) { 304 *dst++ = *src >> 4; 305 j++; 306 if (j < logo->width) { 307 *dst++ = *src & 0x0f; 308 j++; 309 } 310 } 311 break; 312 case 1: 313 for (i = 0; i < logo->height; i++) { 314 for (j = 0; j < logo->width; src++) { 315 d = *src ^ xor; 316 for (k = 7; k >= 0 && j < logo->width; k--) { 317 *dst++ = ((d >> k) & 1) ? fg : 0; 318 j++; 319 } 320 } 321 } 322 break; 323 } 324 } 325 326 /* 327 * Three (3) kinds of logo maps exist. linux_logo_clut224 (>16 colors), 328 * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors). Depending on 329 * the visual format and color depth of the framebuffer, the DAC, the 330 * pseudo_palette, and the logo data will be adjusted accordingly. 331 * 332 * Case 1 - linux_logo_clut224: 333 * Color exceeds the number of console colors (16), thus we set the hardware DAC 334 * using fb_set_cmap() appropriately. The "needs_cmapreset" flag will be set. 335 * 336 * For visuals that require color info from the pseudo_palette, we also construct 337 * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags 338 * will be set. 339 * 340 * Case 2 - linux_logo_vga16: 341 * The number of colors just matches the console colors, thus there is no need 342 * to set the DAC or the pseudo_palette. However, the bitmap is packed, ie, 343 * each byte contains color information for two pixels (upper and lower nibble). 344 * To be consistent with fb_imageblit() usage, we therefore separate the two 345 * nibbles into separate bytes. The "depth" flag will be set to 4. 346 * 347 * Case 3 - linux_logo_mono: 348 * This is similar with Case 2. Each byte contains information for 8 pixels. 349 * We isolate each bit and expand each into a byte. The "depth" flag will 350 * be set to 1. 351 */ 352 static struct logo_data { 353 int depth; 354 int needs_directpalette; 355 int needs_truepalette; 356 int needs_cmapreset; 357 const struct linux_logo *logo; 358 } fb_logo __read_mostly; 359 360 static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height) 361 { 362 u32 size = width * height, i; 363 364 out += size - 1; 365 366 for (i = size; i--; ) 367 *out-- = *in++; 368 } 369 370 static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height) 371 { 372 int i, j, h = height - 1; 373 374 for (i = 0; i < height; i++) 375 for (j = 0; j < width; j++) 376 out[height * j + h - i] = *in++; 377 } 378 379 static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height) 380 { 381 int i, j, w = width - 1; 382 383 for (i = 0; i < height; i++) 384 for (j = 0; j < width; j++) 385 out[height * (w - j) + i] = *in++; 386 } 387 388 static void fb_rotate_logo(struct fb_info *info, u8 *dst, 389 struct fb_image *image, int rotate) 390 { 391 u32 tmp; 392 393 if (rotate == FB_ROTATE_UD) { 394 fb_rotate_logo_ud(image->data, dst, image->width, 395 image->height); 396 image->dx = info->var.xres - image->width - image->dx; 397 image->dy = info->var.yres - image->height - image->dy; 398 } else if (rotate == FB_ROTATE_CW) { 399 fb_rotate_logo_cw(image->data, dst, image->width, 400 image->height); 401 swap(image->width, image->height); 402 tmp = image->dy; 403 image->dy = image->dx; 404 image->dx = info->var.xres - image->width - tmp; 405 } else if (rotate == FB_ROTATE_CCW) { 406 fb_rotate_logo_ccw(image->data, dst, image->width, 407 image->height); 408 swap(image->width, image->height); 409 tmp = image->dx; 410 image->dx = image->dy; 411 image->dy = info->var.yres - image->height - tmp; 412 } 413 414 image->data = dst; 415 } 416 417 static void fb_do_show_logo(struct fb_info *info, struct fb_image *image, 418 int rotate, unsigned int num) 419 { 420 unsigned int x; 421 422 if (image->width > info->var.xres || image->height > info->var.yres) 423 return; 424 425 if (rotate == FB_ROTATE_UR) { 426 for (x = 0; 427 x < num && image->dx + image->width <= info->var.xres; 428 x++) { 429 info->fbops->fb_imageblit(info, image); 430 image->dx += image->width + 8; 431 } 432 } else if (rotate == FB_ROTATE_UD) { 433 u32 dx = image->dx; 434 435 for (x = 0; x < num && image->dx <= dx; x++) { 436 info->fbops->fb_imageblit(info, image); 437 image->dx -= image->width + 8; 438 } 439 } else if (rotate == FB_ROTATE_CW) { 440 for (x = 0; 441 x < num && image->dy + image->height <= info->var.yres; 442 x++) { 443 info->fbops->fb_imageblit(info, image); 444 image->dy += image->height + 8; 445 } 446 } else if (rotate == FB_ROTATE_CCW) { 447 u32 dy = image->dy; 448 449 for (x = 0; x < num && image->dy <= dy; x++) { 450 info->fbops->fb_imageblit(info, image); 451 image->dy -= image->height + 8; 452 } 453 } 454 } 455 456 static int fb_show_logo_line(struct fb_info *info, int rotate, 457 const struct linux_logo *logo, int y, 458 unsigned int n) 459 { 460 u32 *palette = NULL, *saved_pseudo_palette = NULL; 461 unsigned char *logo_new = NULL, *logo_rotate = NULL; 462 struct fb_image image; 463 464 /* Return if the frame buffer is not mapped or suspended */ 465 if (logo == NULL || info->state != FBINFO_STATE_RUNNING || 466 info->fbops->owner) 467 return 0; 468 469 image.depth = 8; 470 image.data = logo->data; 471 472 if (fb_logo.needs_cmapreset) 473 fb_set_logocmap(info, logo); 474 475 if (fb_logo.needs_truepalette || 476 fb_logo.needs_directpalette) { 477 palette = kmalloc(256 * 4, GFP_KERNEL); 478 if (palette == NULL) 479 return 0; 480 481 if (fb_logo.needs_truepalette) 482 fb_set_logo_truepalette(info, logo, palette); 483 else 484 fb_set_logo_directpalette(info, logo, palette); 485 486 saved_pseudo_palette = info->pseudo_palette; 487 info->pseudo_palette = palette; 488 } 489 490 if (fb_logo.depth <= 4) { 491 logo_new = kmalloc_array(logo->width, logo->height, 492 GFP_KERNEL); 493 if (logo_new == NULL) { 494 kfree(palette); 495 if (saved_pseudo_palette) 496 info->pseudo_palette = saved_pseudo_palette; 497 return 0; 498 } 499 image.data = logo_new; 500 fb_set_logo(info, logo, logo_new, fb_logo.depth); 501 } 502 503 if (fb_center_logo) { 504 int xres = info->var.xres; 505 int yres = info->var.yres; 506 507 if (rotate == FB_ROTATE_CW || rotate == FB_ROTATE_CCW) { 508 xres = info->var.yres; 509 yres = info->var.xres; 510 } 511 512 while (n && (n * (logo->width + 8) - 8 > xres)) 513 --n; 514 image.dx = (xres - n * (logo->width + 8) - 8) / 2; 515 image.dy = y ?: (yres - logo->height) / 2; 516 } else { 517 image.dx = 0; 518 image.dy = y; 519 } 520 521 image.width = logo->width; 522 image.height = logo->height; 523 524 if (rotate) { 525 logo_rotate = kmalloc_array(logo->width, logo->height, 526 GFP_KERNEL); 527 if (logo_rotate) 528 fb_rotate_logo(info, logo_rotate, &image, rotate); 529 } 530 531 fb_do_show_logo(info, &image, rotate, n); 532 533 kfree(palette); 534 if (saved_pseudo_palette != NULL) 535 info->pseudo_palette = saved_pseudo_palette; 536 kfree(logo_new); 537 kfree(logo_rotate); 538 return image.dy + logo->height; 539 } 540 541 542 #ifdef CONFIG_FB_LOGO_EXTRA 543 544 #define FB_LOGO_EX_NUM_MAX 10 545 static struct logo_data_extra { 546 const struct linux_logo *logo; 547 unsigned int n; 548 } fb_logo_ex[FB_LOGO_EX_NUM_MAX]; 549 static unsigned int fb_logo_ex_num; 550 551 void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n) 552 { 553 if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX) 554 return; 555 556 fb_logo_ex[fb_logo_ex_num].logo = logo; 557 fb_logo_ex[fb_logo_ex_num].n = n; 558 fb_logo_ex_num++; 559 } 560 561 static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height, 562 unsigned int yres) 563 { 564 unsigned int i; 565 566 /* FIXME: logo_ex supports only truecolor fb. */ 567 if (info->fix.visual != FB_VISUAL_TRUECOLOR) 568 fb_logo_ex_num = 0; 569 570 for (i = 0; i < fb_logo_ex_num; i++) { 571 if (fb_logo_ex[i].logo->type != fb_logo.logo->type) { 572 fb_logo_ex[i].logo = NULL; 573 continue; 574 } 575 height += fb_logo_ex[i].logo->height; 576 if (height > yres) { 577 height -= fb_logo_ex[i].logo->height; 578 fb_logo_ex_num = i; 579 break; 580 } 581 } 582 return height; 583 } 584 585 static int fb_show_extra_logos(struct fb_info *info, int y, int rotate) 586 { 587 unsigned int i; 588 589 for (i = 0; i < fb_logo_ex_num; i++) 590 y = fb_show_logo_line(info, rotate, 591 fb_logo_ex[i].logo, y, fb_logo_ex[i].n); 592 593 return y; 594 } 595 596 #else /* !CONFIG_FB_LOGO_EXTRA */ 597 598 static inline int fb_prepare_extra_logos(struct fb_info *info, 599 unsigned int height, 600 unsigned int yres) 601 { 602 return height; 603 } 604 605 static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate) 606 { 607 return y; 608 } 609 610 #endif /* CONFIG_FB_LOGO_EXTRA */ 611 612 613 int fb_prepare_logo(struct fb_info *info, int rotate) 614 { 615 int depth = fb_get_color_depth(&info->var, &info->fix); 616 unsigned int yres; 617 int height; 618 619 memset(&fb_logo, 0, sizeof(struct logo_data)); 620 621 if (info->flags & FBINFO_MISC_TILEBLITTING || 622 info->fbops->owner || !fb_logo_count) 623 return 0; 624 625 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) { 626 depth = info->var.blue.length; 627 if (info->var.red.length < depth) 628 depth = info->var.red.length; 629 if (info->var.green.length < depth) 630 depth = info->var.green.length; 631 } 632 633 if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) { 634 /* assume console colormap */ 635 depth = 4; 636 } 637 638 /* Return if no suitable logo was found */ 639 fb_logo.logo = fb_find_logo(depth); 640 641 if (!fb_logo.logo) { 642 return 0; 643 } 644 645 if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD) 646 yres = info->var.yres; 647 else 648 yres = info->var.xres; 649 650 if (fb_logo.logo->height > yres) { 651 fb_logo.logo = NULL; 652 return 0; 653 } 654 655 /* What depth we asked for might be different from what we get */ 656 if (fb_logo.logo->type == LINUX_LOGO_CLUT224) 657 fb_logo.depth = 8; 658 else if (fb_logo.logo->type == LINUX_LOGO_VGA16) 659 fb_logo.depth = 4; 660 else 661 fb_logo.depth = 1; 662 663 664 if (fb_logo.depth > 4 && depth > 4) { 665 switch (info->fix.visual) { 666 case FB_VISUAL_TRUECOLOR: 667 fb_logo.needs_truepalette = 1; 668 break; 669 case FB_VISUAL_DIRECTCOLOR: 670 fb_logo.needs_directpalette = 1; 671 fb_logo.needs_cmapreset = 1; 672 break; 673 case FB_VISUAL_PSEUDOCOLOR: 674 fb_logo.needs_cmapreset = 1; 675 break; 676 } 677 } 678 679 height = fb_logo.logo->height; 680 if (fb_center_logo) 681 height += (yres - fb_logo.logo->height) / 2; 682 683 return fb_prepare_extra_logos(info, height, yres); 684 } 685 686 int fb_show_logo(struct fb_info *info, int rotate) 687 { 688 unsigned int count; 689 int y; 690 691 if (!fb_logo_count) 692 return 0; 693 694 count = fb_logo_count < 0 ? num_online_cpus() : fb_logo_count; 695 y = fb_show_logo_line(info, rotate, fb_logo.logo, 0, count); 696 y = fb_show_extra_logos(info, y, rotate); 697 698 return y; 699 } 700 #else 701 int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; } 702 int fb_show_logo(struct fb_info *info, int rotate) { return 0; } 703 #endif /* CONFIG_LOGO */ 704 EXPORT_SYMBOL(fb_prepare_logo); 705 EXPORT_SYMBOL(fb_show_logo); 706 707 static void *fb_seq_start(struct seq_file *m, loff_t *pos) 708 { 709 mutex_lock(®istration_lock); 710 return (*pos < FB_MAX) ? pos : NULL; 711 } 712 713 static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos) 714 { 715 (*pos)++; 716 return (*pos < FB_MAX) ? pos : NULL; 717 } 718 719 static void fb_seq_stop(struct seq_file *m, void *v) 720 { 721 mutex_unlock(®istration_lock); 722 } 723 724 static int fb_seq_show(struct seq_file *m, void *v) 725 { 726 int i = *(loff_t *)v; 727 struct fb_info *fi = registered_fb[i]; 728 729 if (fi) 730 seq_printf(m, "%d %s\n", fi->node, fi->fix.id); 731 return 0; 732 } 733 734 static const struct seq_operations __maybe_unused proc_fb_seq_ops = { 735 .start = fb_seq_start, 736 .next = fb_seq_next, 737 .stop = fb_seq_stop, 738 .show = fb_seq_show, 739 }; 740 741 /* 742 * We hold a reference to the fb_info in file->private_data, 743 * but if the current registered fb has changed, we don't 744 * actually want to use it. 745 * 746 * So look up the fb_info using the inode minor number, 747 * and just verify it against the reference we have. 748 */ 749 static struct fb_info *file_fb_info(struct file *file) 750 { 751 struct inode *inode = file_inode(file); 752 int fbidx = iminor(inode); 753 struct fb_info *info = registered_fb[fbidx]; 754 755 if (info != file->private_data) 756 info = NULL; 757 return info; 758 } 759 760 static ssize_t 761 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 762 { 763 unsigned long p = *ppos; 764 struct fb_info *info = file_fb_info(file); 765 u8 *buffer, *dst; 766 u8 __iomem *src; 767 int c, cnt = 0, err = 0; 768 unsigned long total_size; 769 770 if (!info || ! info->screen_base) 771 return -ENODEV; 772 773 if (info->state != FBINFO_STATE_RUNNING) 774 return -EPERM; 775 776 if (info->fbops->fb_read) 777 return info->fbops->fb_read(info, buf, count, ppos); 778 779 total_size = info->screen_size; 780 781 if (total_size == 0) 782 total_size = info->fix.smem_len; 783 784 if (p >= total_size) 785 return 0; 786 787 if (count >= total_size) 788 count = total_size; 789 790 if (count + p > total_size) 791 count = total_size - p; 792 793 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, 794 GFP_KERNEL); 795 if (!buffer) 796 return -ENOMEM; 797 798 src = (u8 __iomem *) (info->screen_base + p); 799 800 if (info->fbops->fb_sync) 801 info->fbops->fb_sync(info); 802 803 while (count) { 804 c = (count > PAGE_SIZE) ? PAGE_SIZE : count; 805 dst = buffer; 806 fb_memcpy_fromfb(dst, src, c); 807 dst += c; 808 src += c; 809 810 if (copy_to_user(buf, buffer, c)) { 811 err = -EFAULT; 812 break; 813 } 814 *ppos += c; 815 buf += c; 816 cnt += c; 817 count -= c; 818 } 819 820 kfree(buffer); 821 822 return (err) ? err : cnt; 823 } 824 825 static ssize_t 826 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) 827 { 828 unsigned long p = *ppos; 829 struct fb_info *info = file_fb_info(file); 830 u8 *buffer, *src; 831 u8 __iomem *dst; 832 int c, cnt = 0, err = 0; 833 unsigned long total_size; 834 835 if (!info || !info->screen_base) 836 return -ENODEV; 837 838 if (info->state != FBINFO_STATE_RUNNING) 839 return -EPERM; 840 841 if (info->fbops->fb_write) 842 return info->fbops->fb_write(info, buf, count, ppos); 843 844 total_size = info->screen_size; 845 846 if (total_size == 0) 847 total_size = info->fix.smem_len; 848 849 if (p > total_size) 850 return -EFBIG; 851 852 if (count > total_size) { 853 err = -EFBIG; 854 count = total_size; 855 } 856 857 if (count + p > total_size) { 858 if (!err) 859 err = -ENOSPC; 860 861 count = total_size - p; 862 } 863 864 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, 865 GFP_KERNEL); 866 if (!buffer) 867 return -ENOMEM; 868 869 dst = (u8 __iomem *) (info->screen_base + p); 870 871 if (info->fbops->fb_sync) 872 info->fbops->fb_sync(info); 873 874 while (count) { 875 c = (count > PAGE_SIZE) ? PAGE_SIZE : count; 876 src = buffer; 877 878 if (copy_from_user(src, buf, c)) { 879 err = -EFAULT; 880 break; 881 } 882 883 fb_memcpy_tofb(dst, src, c); 884 dst += c; 885 src += c; 886 *ppos += c; 887 buf += c; 888 cnt += c; 889 count -= c; 890 } 891 892 kfree(buffer); 893 894 return (cnt) ? cnt : err; 895 } 896 897 int 898 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var) 899 { 900 struct fb_fix_screeninfo *fix = &info->fix; 901 unsigned int yres = info->var.yres; 902 int err = 0; 903 904 if (var->yoffset > 0) { 905 if (var->vmode & FB_VMODE_YWRAP) { 906 if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep)) 907 err = -EINVAL; 908 else 909 yres = 0; 910 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep)) 911 err = -EINVAL; 912 } 913 914 if (var->xoffset > 0 && (!fix->xpanstep || 915 (var->xoffset % fix->xpanstep))) 916 err = -EINVAL; 917 918 if (err || !info->fbops->fb_pan_display || 919 var->yoffset > info->var.yres_virtual - yres || 920 var->xoffset > info->var.xres_virtual - info->var.xres) 921 return -EINVAL; 922 923 if ((err = info->fbops->fb_pan_display(var, info))) 924 return err; 925 info->var.xoffset = var->xoffset; 926 info->var.yoffset = var->yoffset; 927 if (var->vmode & FB_VMODE_YWRAP) 928 info->var.vmode |= FB_VMODE_YWRAP; 929 else 930 info->var.vmode &= ~FB_VMODE_YWRAP; 931 return 0; 932 } 933 EXPORT_SYMBOL(fb_pan_display); 934 935 static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var, 936 u32 activate) 937 { 938 struct fb_blit_caps caps, fbcaps; 939 int err = 0; 940 941 memset(&caps, 0, sizeof(caps)); 942 memset(&fbcaps, 0, sizeof(fbcaps)); 943 caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0; 944 fbcon_get_requirement(info, &caps); 945 info->fbops->fb_get_caps(info, &fbcaps, var); 946 947 if (((fbcaps.x ^ caps.x) & caps.x) || 948 ((fbcaps.y ^ caps.y) & caps.y) || 949 (fbcaps.len < caps.len)) 950 err = -EINVAL; 951 952 return err; 953 } 954 955 int 956 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var) 957 { 958 int ret = 0; 959 u32 activate; 960 struct fb_var_screeninfo old_var; 961 struct fb_videomode mode; 962 struct fb_event event; 963 u32 unused; 964 965 if (var->activate & FB_ACTIVATE_INV_MODE) { 966 struct fb_videomode mode1, mode2; 967 968 fb_var_to_videomode(&mode1, var); 969 fb_var_to_videomode(&mode2, &info->var); 970 /* make sure we don't delete the videomode of current var */ 971 ret = fb_mode_is_equal(&mode1, &mode2); 972 if (!ret) { 973 ret = fbcon_mode_deleted(info, &mode1); 974 if (!ret) 975 fb_delete_videomode(&mode1, &info->modelist); 976 } 977 978 return ret ? -EINVAL : 0; 979 } 980 981 if (!(var->activate & FB_ACTIVATE_FORCE) && 982 !memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) 983 return 0; 984 985 activate = var->activate; 986 987 /* When using FOURCC mode, make sure the red, green, blue and 988 * transp fields are set to 0. 989 */ 990 if ((info->fix.capabilities & FB_CAP_FOURCC) && 991 var->grayscale > 1) { 992 if (var->red.offset || var->green.offset || 993 var->blue.offset || var->transp.offset || 994 var->red.length || var->green.length || 995 var->blue.length || var->transp.length || 996 var->red.msb_right || var->green.msb_right || 997 var->blue.msb_right || var->transp.msb_right) 998 return -EINVAL; 999 } 1000 1001 if (!info->fbops->fb_check_var) { 1002 *var = info->var; 1003 return 0; 1004 } 1005 1006 /* bitfill_aligned() assumes that it's at least 8x8 */ 1007 if (var->xres < 8 || var->yres < 8) 1008 return -EINVAL; 1009 1010 /* Too huge resolution causes multiplication overflow. */ 1011 if (check_mul_overflow(var->xres, var->yres, &unused) || 1012 check_mul_overflow(var->xres_virtual, var->yres_virtual, &unused)) 1013 return -EINVAL; 1014 1015 ret = info->fbops->fb_check_var(var, info); 1016 1017 if (ret) 1018 return ret; 1019 1020 if ((var->activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW) 1021 return 0; 1022 1023 if (info->fbops->fb_get_caps) { 1024 ret = fb_check_caps(info, var, activate); 1025 1026 if (ret) 1027 return ret; 1028 } 1029 1030 old_var = info->var; 1031 info->var = *var; 1032 1033 if (info->fbops->fb_set_par) { 1034 ret = info->fbops->fb_set_par(info); 1035 1036 if (ret) { 1037 info->var = old_var; 1038 printk(KERN_WARNING "detected " 1039 "fb_set_par error, " 1040 "error code: %d\n", ret); 1041 return ret; 1042 } 1043 } 1044 1045 fb_pan_display(info, &info->var); 1046 fb_set_cmap(&info->cmap, info); 1047 fb_var_to_videomode(&mode, &info->var); 1048 1049 if (info->modelist.prev && info->modelist.next && 1050 !list_empty(&info->modelist)) 1051 ret = fb_add_videomode(&mode, &info->modelist); 1052 1053 if (ret) 1054 return ret; 1055 1056 event.info = info; 1057 event.data = &mode; 1058 fb_notifier_call_chain(FB_EVENT_MODE_CHANGE, &event); 1059 1060 return 0; 1061 } 1062 EXPORT_SYMBOL(fb_set_var); 1063 1064 int 1065 fb_blank(struct fb_info *info, int blank) 1066 { 1067 struct fb_event event; 1068 int ret = -EINVAL; 1069 1070 if (blank > FB_BLANK_POWERDOWN) 1071 blank = FB_BLANK_POWERDOWN; 1072 1073 event.info = info; 1074 event.data = ␣ 1075 1076 if (info->fbops->fb_blank) 1077 ret = info->fbops->fb_blank(blank, info); 1078 1079 if (!ret) 1080 fb_notifier_call_chain(FB_EVENT_BLANK, &event); 1081 1082 return ret; 1083 } 1084 EXPORT_SYMBOL(fb_blank); 1085 1086 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, 1087 unsigned long arg) 1088 { 1089 const struct fb_ops *fb; 1090 struct fb_var_screeninfo var; 1091 struct fb_fix_screeninfo fix; 1092 struct fb_cmap cmap_from; 1093 struct fb_cmap_user cmap; 1094 void __user *argp = (void __user *)arg; 1095 long ret = 0; 1096 1097 switch (cmd) { 1098 case FBIOGET_VSCREENINFO: 1099 lock_fb_info(info); 1100 var = info->var; 1101 unlock_fb_info(info); 1102 1103 ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0; 1104 break; 1105 case FBIOPUT_VSCREENINFO: 1106 if (copy_from_user(&var, argp, sizeof(var))) 1107 return -EFAULT; 1108 console_lock(); 1109 lock_fb_info(info); 1110 ret = fb_set_var(info, &var); 1111 if (!ret) 1112 fbcon_update_vcs(info, var.activate & FB_ACTIVATE_ALL); 1113 unlock_fb_info(info); 1114 console_unlock(); 1115 if (!ret && copy_to_user(argp, &var, sizeof(var))) 1116 ret = -EFAULT; 1117 break; 1118 case FBIOGET_FSCREENINFO: 1119 lock_fb_info(info); 1120 memcpy(&fix, &info->fix, sizeof(fix)); 1121 if (info->flags & FBINFO_HIDE_SMEM_START) 1122 fix.smem_start = 0; 1123 unlock_fb_info(info); 1124 1125 ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0; 1126 break; 1127 case FBIOPUTCMAP: 1128 if (copy_from_user(&cmap, argp, sizeof(cmap))) 1129 return -EFAULT; 1130 ret = fb_set_user_cmap(&cmap, info); 1131 break; 1132 case FBIOGETCMAP: 1133 if (copy_from_user(&cmap, argp, sizeof(cmap))) 1134 return -EFAULT; 1135 lock_fb_info(info); 1136 cmap_from = info->cmap; 1137 unlock_fb_info(info); 1138 ret = fb_cmap_to_user(&cmap_from, &cmap); 1139 break; 1140 case FBIOPAN_DISPLAY: 1141 if (copy_from_user(&var, argp, sizeof(var))) 1142 return -EFAULT; 1143 console_lock(); 1144 lock_fb_info(info); 1145 ret = fb_pan_display(info, &var); 1146 unlock_fb_info(info); 1147 console_unlock(); 1148 if (ret == 0 && copy_to_user(argp, &var, sizeof(var))) 1149 return -EFAULT; 1150 break; 1151 case FBIO_CURSOR: 1152 ret = -EINVAL; 1153 break; 1154 case FBIOGET_CON2FBMAP: 1155 ret = fbcon_get_con2fb_map_ioctl(argp); 1156 break; 1157 case FBIOPUT_CON2FBMAP: 1158 ret = fbcon_set_con2fb_map_ioctl(argp); 1159 break; 1160 case FBIOBLANK: 1161 if (arg > FB_BLANK_POWERDOWN) 1162 return -EINVAL; 1163 console_lock(); 1164 lock_fb_info(info); 1165 ret = fb_blank(info, arg); 1166 /* might again call into fb_blank */ 1167 fbcon_fb_blanked(info, arg); 1168 unlock_fb_info(info); 1169 console_unlock(); 1170 break; 1171 default: 1172 lock_fb_info(info); 1173 fb = info->fbops; 1174 if (fb->fb_ioctl) 1175 ret = fb->fb_ioctl(info, cmd, arg); 1176 else 1177 ret = -ENOTTY; 1178 unlock_fb_info(info); 1179 } 1180 return ret; 1181 } 1182 1183 static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1184 { 1185 struct fb_info *info = file_fb_info(file); 1186 1187 if (!info) 1188 return -ENODEV; 1189 return do_fb_ioctl(info, cmd, arg); 1190 } 1191 1192 #ifdef CONFIG_COMPAT 1193 struct fb_fix_screeninfo32 { 1194 char id[16]; 1195 compat_caddr_t smem_start; 1196 u32 smem_len; 1197 u32 type; 1198 u32 type_aux; 1199 u32 visual; 1200 u16 xpanstep; 1201 u16 ypanstep; 1202 u16 ywrapstep; 1203 u32 line_length; 1204 compat_caddr_t mmio_start; 1205 u32 mmio_len; 1206 u32 accel; 1207 u16 reserved[3]; 1208 }; 1209 1210 struct fb_cmap32 { 1211 u32 start; 1212 u32 len; 1213 compat_caddr_t red; 1214 compat_caddr_t green; 1215 compat_caddr_t blue; 1216 compat_caddr_t transp; 1217 }; 1218 1219 static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, 1220 unsigned long arg) 1221 { 1222 struct fb_cmap32 cmap32; 1223 struct fb_cmap cmap_from; 1224 struct fb_cmap_user cmap; 1225 1226 if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) 1227 return -EFAULT; 1228 1229 cmap = (struct fb_cmap_user) { 1230 .start = cmap32.start, 1231 .len = cmap32.len, 1232 .red = compat_ptr(cmap32.red), 1233 .green = compat_ptr(cmap32.green), 1234 .blue = compat_ptr(cmap32.blue), 1235 .transp = compat_ptr(cmap32.transp), 1236 }; 1237 1238 if (cmd == FBIOPUTCMAP) 1239 return fb_set_user_cmap(&cmap, info); 1240 1241 lock_fb_info(info); 1242 cmap_from = info->cmap; 1243 unlock_fb_info(info); 1244 1245 return fb_cmap_to_user(&cmap_from, &cmap); 1246 } 1247 1248 static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, 1249 struct fb_fix_screeninfo32 __user *fix32) 1250 { 1251 __u32 data; 1252 int err; 1253 1254 err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); 1255 1256 data = (__u32) (unsigned long) fix->smem_start; 1257 err |= put_user(data, &fix32->smem_start); 1258 1259 err |= put_user(fix->smem_len, &fix32->smem_len); 1260 err |= put_user(fix->type, &fix32->type); 1261 err |= put_user(fix->type_aux, &fix32->type_aux); 1262 err |= put_user(fix->visual, &fix32->visual); 1263 err |= put_user(fix->xpanstep, &fix32->xpanstep); 1264 err |= put_user(fix->ypanstep, &fix32->ypanstep); 1265 err |= put_user(fix->ywrapstep, &fix32->ywrapstep); 1266 err |= put_user(fix->line_length, &fix32->line_length); 1267 1268 data = (__u32) (unsigned long) fix->mmio_start; 1269 err |= put_user(data, &fix32->mmio_start); 1270 1271 err |= put_user(fix->mmio_len, &fix32->mmio_len); 1272 err |= put_user(fix->accel, &fix32->accel); 1273 err |= copy_to_user(fix32->reserved, fix->reserved, 1274 sizeof(fix->reserved)); 1275 1276 if (err) 1277 return -EFAULT; 1278 return 0; 1279 } 1280 1281 static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, 1282 unsigned long arg) 1283 { 1284 struct fb_fix_screeninfo fix; 1285 1286 lock_fb_info(info); 1287 fix = info->fix; 1288 if (info->flags & FBINFO_HIDE_SMEM_START) 1289 fix.smem_start = 0; 1290 unlock_fb_info(info); 1291 return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); 1292 } 1293 1294 static long fb_compat_ioctl(struct file *file, unsigned int cmd, 1295 unsigned long arg) 1296 { 1297 struct fb_info *info = file_fb_info(file); 1298 const struct fb_ops *fb; 1299 long ret = -ENOIOCTLCMD; 1300 1301 if (!info) 1302 return -ENODEV; 1303 fb = info->fbops; 1304 switch(cmd) { 1305 case FBIOGET_VSCREENINFO: 1306 case FBIOPUT_VSCREENINFO: 1307 case FBIOPAN_DISPLAY: 1308 case FBIOGET_CON2FBMAP: 1309 case FBIOPUT_CON2FBMAP: 1310 arg = (unsigned long) compat_ptr(arg); 1311 fallthrough; 1312 case FBIOBLANK: 1313 ret = do_fb_ioctl(info, cmd, arg); 1314 break; 1315 1316 case FBIOGET_FSCREENINFO: 1317 ret = fb_get_fscreeninfo(info, cmd, arg); 1318 break; 1319 1320 case FBIOGETCMAP: 1321 case FBIOPUTCMAP: 1322 ret = fb_getput_cmap(info, cmd, arg); 1323 break; 1324 1325 default: 1326 if (fb->fb_compat_ioctl) 1327 ret = fb->fb_compat_ioctl(info, cmd, arg); 1328 break; 1329 } 1330 return ret; 1331 } 1332 #endif 1333 1334 static int 1335 fb_mmap(struct file *file, struct vm_area_struct * vma) 1336 { 1337 struct fb_info *info = file_fb_info(file); 1338 unsigned long mmio_pgoff; 1339 unsigned long start; 1340 u32 len; 1341 1342 if (!info) 1343 return -ENODEV; 1344 mutex_lock(&info->mm_lock); 1345 1346 if (info->fbops->fb_mmap) { 1347 int res; 1348 1349 /* 1350 * The framebuffer needs to be accessed decrypted, be sure 1351 * SME protection is removed ahead of the call 1352 */ 1353 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); 1354 res = info->fbops->fb_mmap(info, vma); 1355 mutex_unlock(&info->mm_lock); 1356 return res; 1357 #if IS_ENABLED(CONFIG_FB_DEFERRED_IO) 1358 } else if (info->fbdefio) { 1359 /* 1360 * FB deferred I/O wants you to handle mmap in your drivers. At a 1361 * minimum, point struct fb_ops.fb_mmap to fb_deferred_io_mmap(). 1362 */ 1363 dev_warn_once(info->dev, "fbdev mmap not set up for deferred I/O.\n"); 1364 mutex_unlock(&info->mm_lock); 1365 return -ENODEV; 1366 #endif 1367 } 1368 1369 /* 1370 * Ugh. This can be either the frame buffer mapping, or 1371 * if pgoff points past it, the mmio mapping. 1372 */ 1373 start = info->fix.smem_start; 1374 len = info->fix.smem_len; 1375 mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT; 1376 if (vma->vm_pgoff >= mmio_pgoff) { 1377 if (info->var.accel_flags) { 1378 mutex_unlock(&info->mm_lock); 1379 return -EINVAL; 1380 } 1381 1382 vma->vm_pgoff -= mmio_pgoff; 1383 start = info->fix.mmio_start; 1384 len = info->fix.mmio_len; 1385 } 1386 mutex_unlock(&info->mm_lock); 1387 1388 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 1389 fb_pgprotect(file, vma, start); 1390 1391 return vm_iomap_memory(vma, start, len); 1392 } 1393 1394 static int 1395 fb_open(struct inode *inode, struct file *file) 1396 __acquires(&info->lock) 1397 __releases(&info->lock) 1398 { 1399 int fbidx = iminor(inode); 1400 struct fb_info *info; 1401 int res = 0; 1402 1403 info = get_fb_info(fbidx); 1404 if (!info) { 1405 request_module("fb%d", fbidx); 1406 info = get_fb_info(fbidx); 1407 if (!info) 1408 return -ENODEV; 1409 } 1410 if (IS_ERR(info)) 1411 return PTR_ERR(info); 1412 1413 lock_fb_info(info); 1414 if (!try_module_get(info->fbops->owner)) { 1415 res = -ENODEV; 1416 goto out; 1417 } 1418 file->private_data = info; 1419 if (info->fbops->fb_open) { 1420 res = info->fbops->fb_open(info,1); 1421 if (res) 1422 module_put(info->fbops->owner); 1423 } 1424 #ifdef CONFIG_FB_DEFERRED_IO 1425 if (info->fbdefio) 1426 fb_deferred_io_open(info, inode, file); 1427 #endif 1428 out: 1429 unlock_fb_info(info); 1430 if (res) 1431 put_fb_info(info); 1432 return res; 1433 } 1434 1435 static int 1436 fb_release(struct inode *inode, struct file *file) 1437 __acquires(&info->lock) 1438 __releases(&info->lock) 1439 { 1440 struct fb_info * const info = file->private_data; 1441 1442 lock_fb_info(info); 1443 if (info->fbops->fb_release) 1444 info->fbops->fb_release(info,1); 1445 module_put(info->fbops->owner); 1446 unlock_fb_info(info); 1447 put_fb_info(info); 1448 return 0; 1449 } 1450 1451 #if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU) 1452 unsigned long get_fb_unmapped_area(struct file *filp, 1453 unsigned long addr, unsigned long len, 1454 unsigned long pgoff, unsigned long flags) 1455 { 1456 struct fb_info * const info = filp->private_data; 1457 unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len); 1458 1459 if (pgoff > fb_size || len > fb_size - pgoff) 1460 return -EINVAL; 1461 1462 return (unsigned long)info->screen_base + pgoff; 1463 } 1464 #endif 1465 1466 static const struct file_operations fb_fops = { 1467 .owner = THIS_MODULE, 1468 .read = fb_read, 1469 .write = fb_write, 1470 .unlocked_ioctl = fb_ioctl, 1471 #ifdef CONFIG_COMPAT 1472 .compat_ioctl = fb_compat_ioctl, 1473 #endif 1474 .mmap = fb_mmap, 1475 .open = fb_open, 1476 .release = fb_release, 1477 #if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \ 1478 (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \ 1479 !defined(CONFIG_MMU)) 1480 .get_unmapped_area = get_fb_unmapped_area, 1481 #endif 1482 #ifdef CONFIG_FB_DEFERRED_IO 1483 .fsync = fb_deferred_io_fsync, 1484 #endif 1485 .llseek = default_llseek, 1486 }; 1487 1488 struct class *fb_class; 1489 EXPORT_SYMBOL(fb_class); 1490 1491 static int fb_check_foreignness(struct fb_info *fi) 1492 { 1493 const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN; 1494 1495 fi->flags &= ~FBINFO_FOREIGN_ENDIAN; 1496 1497 #ifdef __BIG_ENDIAN 1498 fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH; 1499 #else 1500 fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0; 1501 #endif /* __BIG_ENDIAN */ 1502 1503 if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) { 1504 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to " 1505 "support this framebuffer\n", fi->fix.id); 1506 return -ENOSYS; 1507 } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) { 1508 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to " 1509 "support this framebuffer\n", fi->fix.id); 1510 return -ENOSYS; 1511 } 1512 1513 return 0; 1514 } 1515 1516 static bool apertures_overlap(struct aperture *gen, struct aperture *hw) 1517 { 1518 /* is the generic aperture base the same as the HW one */ 1519 if (gen->base == hw->base) 1520 return true; 1521 /* is the generic aperture base inside the hw base->hw base+size */ 1522 if (gen->base > hw->base && gen->base < hw->base + hw->size) 1523 return true; 1524 return false; 1525 } 1526 1527 static bool fb_do_apertures_overlap(struct apertures_struct *gena, 1528 struct apertures_struct *hwa) 1529 { 1530 int i, j; 1531 if (!hwa || !gena) 1532 return false; 1533 1534 for (i = 0; i < hwa->count; ++i) { 1535 struct aperture *h = &hwa->ranges[i]; 1536 for (j = 0; j < gena->count; ++j) { 1537 struct aperture *g = &gena->ranges[j]; 1538 printk(KERN_DEBUG "checking generic (%llx %llx) vs hw (%llx %llx)\n", 1539 (unsigned long long)g->base, 1540 (unsigned long long)g->size, 1541 (unsigned long long)h->base, 1542 (unsigned long long)h->size); 1543 if (apertures_overlap(g, h)) 1544 return true; 1545 } 1546 } 1547 1548 return false; 1549 } 1550 1551 static void do_unregister_framebuffer(struct fb_info *fb_info); 1552 1553 #define VGA_FB_PHYS 0xA0000 1554 static void do_remove_conflicting_framebuffers(struct apertures_struct *a, 1555 const char *name, bool primary) 1556 { 1557 int i; 1558 1559 /* check all firmware fbs and kick off if the base addr overlaps */ 1560 for_each_registered_fb(i) { 1561 struct apertures_struct *gen_aper; 1562 struct device *device; 1563 1564 if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE)) 1565 continue; 1566 1567 gen_aper = registered_fb[i]->apertures; 1568 device = registered_fb[i]->device; 1569 if (fb_do_apertures_overlap(gen_aper, a) || 1570 (primary && gen_aper && gen_aper->count && 1571 gen_aper->ranges[0].base == VGA_FB_PHYS)) { 1572 1573 printk(KERN_INFO "fb%d: switching to %s from %s\n", 1574 i, name, registered_fb[i]->fix.id); 1575 1576 /* 1577 * If we kick-out a firmware driver, we also want to remove 1578 * the underlying platform device, such as simple-framebuffer, 1579 * VESA, EFI, etc. A native driver will then be able to 1580 * allocate the memory range. 1581 * 1582 * If it's not a platform device, at least print a warning. A 1583 * fix would add code to remove the device from the system. For 1584 * framebuffers without any Linux device, print a warning as 1585 * well. 1586 */ 1587 if (!device) { 1588 pr_warn("fb%d: no device set\n", i); 1589 do_unregister_framebuffer(registered_fb[i]); 1590 } else if (dev_is_platform(device)) { 1591 registered_fb[i]->forced_out = true; 1592 platform_device_unregister(to_platform_device(device)); 1593 } else { 1594 pr_warn("fb%d: cannot remove device\n", i); 1595 do_unregister_framebuffer(registered_fb[i]); 1596 } 1597 } 1598 } 1599 } 1600 1601 static int do_register_framebuffer(struct fb_info *fb_info) 1602 { 1603 int i; 1604 struct fb_videomode mode; 1605 1606 if (fb_check_foreignness(fb_info)) 1607 return -ENOSYS; 1608 1609 do_remove_conflicting_framebuffers(fb_info->apertures, 1610 fb_info->fix.id, 1611 fb_is_primary_device(fb_info)); 1612 1613 if (num_registered_fb == FB_MAX) 1614 return -ENXIO; 1615 1616 num_registered_fb++; 1617 for (i = 0 ; i < FB_MAX; i++) 1618 if (!registered_fb[i]) 1619 break; 1620 fb_info->node = i; 1621 refcount_set(&fb_info->count, 1); 1622 mutex_init(&fb_info->lock); 1623 mutex_init(&fb_info->mm_lock); 1624 1625 fb_info->dev = device_create(fb_class, fb_info->device, 1626 MKDEV(FB_MAJOR, i), NULL, "fb%d", i); 1627 if (IS_ERR(fb_info->dev)) { 1628 /* Not fatal */ 1629 printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev)); 1630 fb_info->dev = NULL; 1631 } else 1632 fb_init_device(fb_info); 1633 1634 if (fb_info->pixmap.addr == NULL) { 1635 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL); 1636 if (fb_info->pixmap.addr) { 1637 fb_info->pixmap.size = FBPIXMAPSIZE; 1638 fb_info->pixmap.buf_align = 1; 1639 fb_info->pixmap.scan_align = 1; 1640 fb_info->pixmap.access_align = 32; 1641 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT; 1642 } 1643 } 1644 fb_info->pixmap.offset = 0; 1645 1646 if (!fb_info->pixmap.blit_x) 1647 fb_info->pixmap.blit_x = ~(u32)0; 1648 1649 if (!fb_info->pixmap.blit_y) 1650 fb_info->pixmap.blit_y = ~(u32)0; 1651 1652 if (!fb_info->modelist.prev || !fb_info->modelist.next) 1653 INIT_LIST_HEAD(&fb_info->modelist); 1654 1655 if (fb_info->skip_vt_switch) 1656 pm_vt_switch_required(fb_info->dev, false); 1657 else 1658 pm_vt_switch_required(fb_info->dev, true); 1659 1660 fb_var_to_videomode(&mode, &fb_info->var); 1661 fb_add_videomode(&mode, &fb_info->modelist); 1662 registered_fb[i] = fb_info; 1663 1664 #ifdef CONFIG_GUMSTIX_AM200EPD 1665 { 1666 struct fb_event event; 1667 event.info = fb_info; 1668 fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event); 1669 } 1670 #endif 1671 1672 return fbcon_fb_registered(fb_info); 1673 } 1674 1675 static void unbind_console(struct fb_info *fb_info) 1676 { 1677 int i = fb_info->node; 1678 1679 if (WARN_ON(i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)) 1680 return; 1681 1682 fbcon_fb_unbind(fb_info); 1683 } 1684 1685 static void unlink_framebuffer(struct fb_info *fb_info) 1686 { 1687 int i; 1688 1689 i = fb_info->node; 1690 if (WARN_ON(i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)) 1691 return; 1692 1693 if (!fb_info->dev) 1694 return; 1695 1696 device_destroy(fb_class, MKDEV(FB_MAJOR, i)); 1697 1698 pm_vt_switch_unregister(fb_info->dev); 1699 1700 unbind_console(fb_info); 1701 1702 fb_info->dev = NULL; 1703 } 1704 1705 static void do_unregister_framebuffer(struct fb_info *fb_info) 1706 { 1707 unlink_framebuffer(fb_info); 1708 if (fb_info->pixmap.addr && 1709 (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT)) { 1710 kfree(fb_info->pixmap.addr); 1711 fb_info->pixmap.addr = NULL; 1712 } 1713 1714 fb_destroy_modelist(&fb_info->modelist); 1715 registered_fb[fb_info->node] = NULL; 1716 num_registered_fb--; 1717 fb_cleanup_device(fb_info); 1718 #ifdef CONFIG_GUMSTIX_AM200EPD 1719 { 1720 struct fb_event event; 1721 event.info = fb_info; 1722 fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event); 1723 } 1724 #endif 1725 fbcon_fb_unregistered(fb_info); 1726 1727 /* this may free fb info */ 1728 put_fb_info(fb_info); 1729 } 1730 1731 /** 1732 * remove_conflicting_framebuffers - remove firmware-configured framebuffers 1733 * @a: memory range, users of which are to be removed 1734 * @name: requesting driver name 1735 * @primary: also kick vga16fb if present 1736 * 1737 * This function removes framebuffer devices (initialized by firmware/bootloader) 1738 * which use memory range described by @a. If @a is NULL all such devices are 1739 * removed. 1740 */ 1741 int remove_conflicting_framebuffers(struct apertures_struct *a, 1742 const char *name, bool primary) 1743 { 1744 bool do_free = false; 1745 1746 if (!a) { 1747 a = alloc_apertures(1); 1748 if (!a) 1749 return -ENOMEM; 1750 1751 a->ranges[0].base = 0; 1752 a->ranges[0].size = ~0; 1753 do_free = true; 1754 } 1755 1756 /* 1757 * If a driver asked to unregister a platform device registered by 1758 * sysfb, then can be assumed that this is a driver for a display 1759 * that is set up by the system firmware and has a generic driver. 1760 * 1761 * Drivers for devices that don't have a generic driver will never 1762 * ask for this, so let's assume that a real driver for the display 1763 * was already probed and prevent sysfb to register devices later. 1764 */ 1765 sysfb_disable(); 1766 1767 mutex_lock(®istration_lock); 1768 do_remove_conflicting_framebuffers(a, name, primary); 1769 mutex_unlock(®istration_lock); 1770 1771 if (do_free) 1772 kfree(a); 1773 1774 return 0; 1775 } 1776 EXPORT_SYMBOL(remove_conflicting_framebuffers); 1777 1778 /** 1779 * remove_conflicting_pci_framebuffers - remove firmware-configured framebuffers for PCI devices 1780 * @pdev: PCI device 1781 * @name: requesting driver name 1782 * 1783 * This function removes framebuffer devices (eg. initialized by firmware) 1784 * using memory range configured for any of @pdev's memory bars. 1785 * 1786 * The function assumes that PCI device with shadowed ROM drives a primary 1787 * display and so kicks out vga16fb. 1788 */ 1789 int remove_conflicting_pci_framebuffers(struct pci_dev *pdev, const char *name) 1790 { 1791 struct apertures_struct *ap; 1792 bool primary = false; 1793 int err, idx, bar; 1794 1795 for (idx = 0, bar = 0; bar < PCI_STD_NUM_BARS; bar++) { 1796 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) 1797 continue; 1798 idx++; 1799 } 1800 1801 ap = alloc_apertures(idx); 1802 if (!ap) 1803 return -ENOMEM; 1804 1805 for (idx = 0, bar = 0; bar < PCI_STD_NUM_BARS; bar++) { 1806 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) 1807 continue; 1808 ap->ranges[idx].base = pci_resource_start(pdev, bar); 1809 ap->ranges[idx].size = pci_resource_len(pdev, bar); 1810 pci_dbg(pdev, "%s: bar %d: 0x%lx -> 0x%lx\n", __func__, bar, 1811 (unsigned long)pci_resource_start(pdev, bar), 1812 (unsigned long)pci_resource_end(pdev, bar)); 1813 idx++; 1814 } 1815 1816 #ifdef CONFIG_X86 1817 primary = pdev->resource[PCI_ROM_RESOURCE].flags & 1818 IORESOURCE_ROM_SHADOW; 1819 #endif 1820 err = remove_conflicting_framebuffers(ap, name, primary); 1821 kfree(ap); 1822 return err; 1823 } 1824 EXPORT_SYMBOL(remove_conflicting_pci_framebuffers); 1825 1826 /** 1827 * register_framebuffer - registers a frame buffer device 1828 * @fb_info: frame buffer info structure 1829 * 1830 * Registers a frame buffer device @fb_info. 1831 * 1832 * Returns negative errno on error, or zero for success. 1833 * 1834 */ 1835 int 1836 register_framebuffer(struct fb_info *fb_info) 1837 { 1838 int ret; 1839 1840 mutex_lock(®istration_lock); 1841 ret = do_register_framebuffer(fb_info); 1842 mutex_unlock(®istration_lock); 1843 1844 return ret; 1845 } 1846 EXPORT_SYMBOL(register_framebuffer); 1847 1848 /** 1849 * unregister_framebuffer - releases a frame buffer device 1850 * @fb_info: frame buffer info structure 1851 * 1852 * Unregisters a frame buffer device @fb_info. 1853 * 1854 * Returns negative errno on error, or zero for success. 1855 * 1856 * This function will also notify the framebuffer console 1857 * to release the driver. 1858 * 1859 * This is meant to be called within a driver's module_exit() 1860 * function. If this is called outside module_exit(), ensure 1861 * that the driver implements fb_open() and fb_release() to 1862 * check that no processes are using the device. 1863 */ 1864 void 1865 unregister_framebuffer(struct fb_info *fb_info) 1866 { 1867 bool forced_out = fb_info->forced_out; 1868 1869 if (!forced_out) 1870 mutex_lock(®istration_lock); 1871 do_unregister_framebuffer(fb_info); 1872 if (!forced_out) 1873 mutex_unlock(®istration_lock); 1874 } 1875 EXPORT_SYMBOL(unregister_framebuffer); 1876 1877 /** 1878 * fb_set_suspend - low level driver signals suspend 1879 * @info: framebuffer affected 1880 * @state: 0 = resuming, !=0 = suspending 1881 * 1882 * This is meant to be used by low level drivers to 1883 * signal suspend/resume to the core & clients. 1884 * It must be called with the console semaphore held 1885 */ 1886 void fb_set_suspend(struct fb_info *info, int state) 1887 { 1888 WARN_CONSOLE_UNLOCKED(); 1889 1890 if (state) { 1891 fbcon_suspended(info); 1892 info->state = FBINFO_STATE_SUSPENDED; 1893 } else { 1894 info->state = FBINFO_STATE_RUNNING; 1895 fbcon_resumed(info); 1896 } 1897 } 1898 EXPORT_SYMBOL(fb_set_suspend); 1899 1900 /** 1901 * fbmem_init - init frame buffer subsystem 1902 * 1903 * Initialize the frame buffer subsystem. 1904 * 1905 * NOTE: This function is _only_ to be called by drivers/char/mem.c. 1906 * 1907 */ 1908 1909 static int __init 1910 fbmem_init(void) 1911 { 1912 int ret; 1913 1914 if (!proc_create_seq("fb", 0, NULL, &proc_fb_seq_ops)) 1915 return -ENOMEM; 1916 1917 ret = register_chrdev(FB_MAJOR, "fb", &fb_fops); 1918 if (ret) { 1919 printk("unable to get major %d for fb devs\n", FB_MAJOR); 1920 goto err_chrdev; 1921 } 1922 1923 fb_class = class_create(THIS_MODULE, "graphics"); 1924 if (IS_ERR(fb_class)) { 1925 ret = PTR_ERR(fb_class); 1926 pr_warn("Unable to create fb class; errno = %d\n", ret); 1927 fb_class = NULL; 1928 goto err_class; 1929 } 1930 1931 fb_console_init(); 1932 1933 return 0; 1934 1935 err_class: 1936 unregister_chrdev(FB_MAJOR, "fb"); 1937 err_chrdev: 1938 remove_proc_entry("fb", NULL); 1939 return ret; 1940 } 1941 1942 #ifdef MODULE 1943 module_init(fbmem_init); 1944 static void __exit 1945 fbmem_exit(void) 1946 { 1947 fb_console_exit(); 1948 1949 remove_proc_entry("fb", NULL); 1950 class_destroy(fb_class); 1951 unregister_chrdev(FB_MAJOR, "fb"); 1952 } 1953 1954 module_exit(fbmem_exit); 1955 MODULE_LICENSE("GPL"); 1956 MODULE_DESCRIPTION("Framebuffer base"); 1957 #else 1958 subsys_initcall(fbmem_init); 1959 #endif 1960 1961 int fb_new_modelist(struct fb_info *info) 1962 { 1963 struct fb_var_screeninfo var = info->var; 1964 struct list_head *pos, *n; 1965 struct fb_modelist *modelist; 1966 struct fb_videomode *m, mode; 1967 int err; 1968 1969 list_for_each_safe(pos, n, &info->modelist) { 1970 modelist = list_entry(pos, struct fb_modelist, list); 1971 m = &modelist->mode; 1972 fb_videomode_to_var(&var, m); 1973 var.activate = FB_ACTIVATE_TEST; 1974 err = fb_set_var(info, &var); 1975 fb_var_to_videomode(&mode, &var); 1976 if (err || !fb_mode_is_equal(m, &mode)) { 1977 list_del(pos); 1978 kfree(pos); 1979 } 1980 } 1981 1982 if (list_empty(&info->modelist)) 1983 return 1; 1984 1985 fbcon_new_modelist(info); 1986 1987 return 0; 1988 } 1989 1990 MODULE_LICENSE("GPL"); 1991