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