1 /* 2 * linux/drivers/video/fbcon.c -- Low level frame buffer based console driver 3 * 4 * Copyright (C) 1995 Geert Uytterhoeven 5 * 6 * 7 * This file is based on the original Amiga console driver (amicon.c): 8 * 9 * Copyright (C) 1993 Hamish Macdonald 10 * Greg Harp 11 * Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk] 12 * 13 * with work by William Rucklidge (wjr@cs.cornell.edu) 14 * Geert Uytterhoeven 15 * Jes Sorensen (jds@kom.auc.dk) 16 * Martin Apel 17 * 18 * and on the original Atari console driver (atacon.c): 19 * 20 * Copyright (C) 1993 Bjoern Brauel 21 * Roman Hodek 22 * 23 * with work by Guenther Kelleter 24 * Martin Schaller 25 * Andreas Schwab 26 * 27 * Hardware cursor support added by Emmanuel Marty (core@ggi-project.org) 28 * Smart redraw scrolling, arbitrary font width support, 512char font support 29 * and software scrollback added by 30 * Jakub Jelinek (jj@ultra.linux.cz) 31 * 32 * Random hacking by Martin Mares <mj@ucw.cz> 33 * 34 * 2001 - Documented with DocBook 35 * - Brad Douglas <brad@neruo.com> 36 * 37 * The low level operations for the various display memory organizations are 38 * now in separate source files. 39 * 40 * Currently the following organizations are supported: 41 * 42 * o afb Amiga bitplanes 43 * o cfb{2,4,8,16,24,32} Packed pixels 44 * o ilbm Amiga interleaved bitplanes 45 * o iplan2p[248] Atari interleaved bitplanes 46 * o mfb Monochrome 47 * o vga VGA characters/attributes 48 * 49 * To do: 50 * 51 * - Implement 16 plane mode (iplan2p16) 52 * 53 * 54 * This file is subject to the terms and conditions of the GNU General Public 55 * License. See the file COPYING in the main directory of this archive for 56 * more details. 57 */ 58 59 #include <linux/export.h> 60 #include <linux/module.h> 61 #include <linux/types.h> 62 #include <linux/fs.h> 63 #include <linux/kernel.h> 64 #include <linux/delay.h> /* MSch: for IRQ probe */ 65 #include <linux/console.h> 66 #include <linux/string.h> 67 #include <linux/kd.h> 68 #include <linux/panic.h> 69 #include <linux/printk.h> 70 #include <linux/slab.h> 71 #include <linux/fb.h> 72 #include <linux/fbcon.h> 73 #include <linux/vt_kern.h> 74 #include <linux/selection.h> 75 #include <linux/font.h> 76 #include <linux/smp.h> 77 #include <linux/init.h> 78 #include <linux/interrupt.h> 79 #include <linux/crc32.h> /* For counting font checksums */ 80 #include <linux/uaccess.h> 81 #include <asm/irq.h> 82 83 #include "fbcon.h" 84 #include "fbcon_rotate.h" 85 #include "fb_internal.h" 86 87 /* 88 * FIXME: Locking 89 * 90 * - fbcon state itself is protected by the console_lock, and the code does a 91 * pretty good job at making sure that lock is held everywhere it's needed. 92 * 93 * - fbcon doesn't bother with fb_lock/unlock at all. This is buggy, since it 94 * means concurrent access to the same fbdev from both fbcon and userspace 95 * will blow up. To fix this all fbcon calls from fbmem.c need to be moved out 96 * of fb_lock/unlock protected sections, since otherwise we'll recurse and 97 * deadlock eventually. Aside: Due to these deadlock issues the fbdev code in 98 * fbmem.c cannot use locking asserts, and there's lots of callers which get 99 * the rules wrong, e.g. fbsysfs.c entirely missed fb_lock/unlock calls too. 100 */ 101 102 enum { 103 FBCON_LOGO_CANSHOW = -1, /* the logo can be shown */ 104 FBCON_LOGO_DRAW = -2, /* draw the logo to a console */ 105 FBCON_LOGO_DONTSHOW = -3 /* do not show the logo */ 106 }; 107 108 static struct fbcon_display fb_display[MAX_NR_CONSOLES]; 109 110 static struct fb_info *fbcon_registered_fb[FB_MAX]; 111 static int fbcon_num_registered_fb; 112 113 #define fbcon_for_each_registered_fb(i) \ 114 for (i = 0; WARN_CONSOLE_UNLOCKED(), i < FB_MAX; i++) \ 115 if (!fbcon_registered_fb[i]) {} else 116 117 static signed char con2fb_map[MAX_NR_CONSOLES]; 118 static signed char con2fb_map_boot[MAX_NR_CONSOLES]; 119 120 static struct fb_info *fbcon_info_from_console(int console) 121 { 122 signed char fb; 123 WARN_CONSOLE_UNLOCKED(); 124 125 fb = con2fb_map[console]; 126 if (fb < 0 || fb >= ARRAY_SIZE(fbcon_registered_fb)) 127 return NULL; 128 129 return fbcon_registered_fb[fb]; 130 } 131 132 static int logo_lines; 133 /* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO 134 enums. */ 135 static int logo_shown = FBCON_LOGO_CANSHOW; 136 /* console mappings */ 137 static unsigned int first_fb_vc; 138 static unsigned int last_fb_vc = MAX_NR_CONSOLES - 1; 139 static bool fbcon_is_default = true; 140 static int primary_device = -1; 141 static bool fbcon_has_console_bind; 142 143 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY 144 static int map_override; 145 146 static inline void fbcon_map_override(void) 147 { 148 map_override = 1; 149 } 150 #else 151 static inline void fbcon_map_override(void) 152 { 153 } 154 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY */ 155 156 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER 157 static bool deferred_takeover = true; 158 #else 159 #define deferred_takeover false 160 #endif 161 162 /* font data */ 163 static char fontname[40]; 164 165 /* current fb_info */ 166 static int info_idx = -1; 167 168 /* console rotation */ 169 static int initial_rotation = -1; 170 static int margin_color; 171 172 static const struct consw fb_con; 173 174 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row) 175 176 static bool fbcon_cursor_blink = true; 177 178 #define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1) 179 180 /* 181 * Interface used by the world 182 */ 183 184 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only); 185 static void fbcon_set_palette(struct vc_data *vc, const unsigned char *table); 186 187 /* 188 * Internal routines 189 */ 190 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var, 191 int unit); 192 static void fbcon_redraw_move(struct vc_data *vc, struct fbcon_display *p, 193 int line, int count, int dy); 194 static void fbcon_modechanged(struct fb_info *info); 195 static void fbcon_set_all_vcs(struct fb_info *info); 196 197 static struct device *fbcon_device; 198 199 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION 200 static inline void fbcon_set_rotation(struct fb_info *info) 201 { 202 struct fbcon_par *par = info->fbcon_par; 203 204 if (!(info->flags & FBINFO_MISC_TILEBLITTING) && 205 par->p->con_rotate < 4) 206 par->rotate = par->p->con_rotate; 207 else 208 par->rotate = 0; 209 } 210 211 static void fbcon_rotate(struct fb_info *info, u32 rotate) 212 { 213 struct fbcon_par *par = info->fbcon_par; 214 struct fb_info *fb_info; 215 216 if (!par || par->currcon == -1) 217 return; 218 219 fb_info = fbcon_info_from_console(par->currcon); 220 221 if (info == fb_info) { 222 struct fbcon_display *p = &fb_display[par->currcon]; 223 224 if (rotate < 4) 225 p->con_rotate = rotate; 226 else 227 p->con_rotate = 0; 228 229 fbcon_modechanged(info); 230 } 231 } 232 233 static void fbcon_rotate_all(struct fb_info *info, u32 rotate) 234 { 235 struct fbcon_par *par = info->fbcon_par; 236 struct vc_data *vc; 237 struct fbcon_display *p; 238 int i; 239 240 if (!par || par->currcon < 0 || rotate > 3) 241 return; 242 243 for (i = first_fb_vc; i <= last_fb_vc; i++) { 244 vc = vc_cons[i].d; 245 if (!vc || vc->vc_mode != KD_TEXT || 246 fbcon_info_from_console(i) != info) 247 continue; 248 249 p = &fb_display[vc->vc_num]; 250 p->con_rotate = rotate; 251 } 252 253 fbcon_set_all_vcs(info); 254 } 255 #else 256 static inline void fbcon_set_rotation(struct fb_info *info) 257 { 258 struct fbcon_par *par = info->fbcon_par; 259 260 par->rotate = FB_ROTATE_UR; 261 } 262 263 static void fbcon_rotate(struct fb_info *info, u32 rotate) 264 { 265 return; 266 } 267 268 static void fbcon_rotate_all(struct fb_info *info, u32 rotate) 269 { 270 return; 271 } 272 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */ 273 274 static void fbcon_set_bitops(struct fbcon_par *par) 275 { 276 switch (par->rotate) { 277 default: 278 fallthrough; 279 case FB_ROTATE_UR: 280 fbcon_set_bitops_ur(par); 281 break; 282 case FB_ROTATE_CW: 283 fbcon_set_bitops_cw(par); 284 break; 285 case FB_ROTATE_UD: 286 fbcon_set_bitops_ud(par); 287 break; 288 case FB_ROTATE_CCW: 289 fbcon_set_bitops_ccw(par); 290 break; 291 } 292 } 293 294 static int fbcon_get_rotate(struct fb_info *info) 295 { 296 struct fbcon_par *par = info->fbcon_par; 297 298 return (par) ? par->rotate : 0; 299 } 300 301 static bool fbcon_skip_panic(struct fb_info *info) 302 { 303 return (info->skip_panic && unlikely(panic_in_progress())); 304 } 305 306 static inline bool fbcon_is_active(struct vc_data *vc, struct fb_info *info) 307 { 308 struct fbcon_par *par = info->fbcon_par; 309 310 return info->state == FBINFO_STATE_RUNNING && 311 vc->vc_mode == KD_TEXT && !par->graphics && !fbcon_skip_panic(info); 312 } 313 314 static int get_color(struct vc_data *vc, struct fb_info *info, 315 u16 c, bool is_fg) 316 { 317 int depth = fb_get_color_depth(&info->var, &info->fix); 318 int color = 0; 319 320 if (console_blanked) { 321 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; 322 323 c = vc->vc_video_erase_char & charmask; 324 } 325 326 if (depth != 1) 327 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c) 328 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c); 329 330 switch (depth) { 331 case 1: 332 { 333 int col = mono_col(info); 334 /* 0 or 1 */ 335 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0; 336 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col; 337 338 if (console_blanked) 339 fg = bg; 340 341 color = (is_fg) ? fg : bg; 342 break; 343 } 344 case 2: 345 /* 346 * Scale down 16-colors to 4 colors. Default 4-color palette 347 * is grayscale. However, simply dividing the values by 4 348 * will not work, as colors 1, 2 and 3 will be scaled-down 349 * to zero rendering them invisible. So empirically convert 350 * colors to a sane 4-level grayscale. 351 */ 352 switch (color) { 353 case 0: 354 color = 0; /* black */ 355 break; 356 case 1 ... 6: 357 color = 2; /* white */ 358 break; 359 case 7 ... 8: 360 color = 1; /* gray */ 361 break; 362 default: 363 color = 3; /* intense white */ 364 break; 365 } 366 break; 367 case 3: 368 /* 369 * Last 8 entries of default 16-color palette is a more intense 370 * version of the first 8 (i.e., same chrominance, different 371 * luminance). 372 */ 373 color &= 7; 374 break; 375 } 376 377 378 return color; 379 } 380 381 static int get_fg_color(struct vc_data *vc, struct fb_info *info, u16 c) 382 { 383 return get_color(vc, info, c, true); 384 } 385 386 static int get_bg_color(struct vc_data *vc, struct fb_info *info, u16 c) 387 { 388 return get_color(vc, info, c, false); 389 } 390 391 static void fb_flashcursor(struct work_struct *work) 392 { 393 struct fbcon_par *par = container_of(work, struct fbcon_par, cursor_work.work); 394 struct fb_info *info; 395 struct vc_data *vc = NULL; 396 int c; 397 bool enable; 398 int ret; 399 400 /* FIXME: we should sort out the unbind locking instead */ 401 /* instead we just fail to flash the cursor if we can't get 402 * the lock instead of blocking fbcon deinit */ 403 ret = console_trylock(); 404 if (ret == 0) 405 return; 406 407 /* protected by console_lock */ 408 info = par->info; 409 410 if (par->currcon != -1) 411 vc = vc_cons[par->currcon].d; 412 413 if (!vc || !con_is_visible(vc) || 414 fbcon_info_from_console(vc->vc_num) != info || 415 vc->vc_deccm != 1) { 416 console_unlock(); 417 return; 418 } 419 420 c = scr_readw((u16 *) vc->vc_pos); 421 enable = par->cursor_flash && !par->cursor_state.enable; 422 par->bitops->cursor(vc, info, enable, 423 get_fg_color(vc, info, c), 424 get_bg_color(vc, info, c)); 425 console_unlock(); 426 427 queue_delayed_work(system_power_efficient_wq, &par->cursor_work, 428 par->cur_blink_jiffies); 429 } 430 431 static void fbcon_add_cursor_work(struct fb_info *info) 432 { 433 struct fbcon_par *par = info->fbcon_par; 434 435 if (fbcon_cursor_blink) 436 queue_delayed_work(system_power_efficient_wq, &par->cursor_work, 437 par->cur_blink_jiffies); 438 } 439 440 static void fbcon_del_cursor_work(struct fb_info *info) 441 { 442 struct fbcon_par *par = info->fbcon_par; 443 444 cancel_delayed_work_sync(&par->cursor_work); 445 } 446 447 #ifndef MODULE 448 static int __init fb_console_setup(char *this_opt) 449 { 450 char *options; 451 int i, j; 452 453 if (!this_opt || !*this_opt) 454 return 1; 455 456 while ((options = strsep(&this_opt, ",")) != NULL) { 457 if (!strncmp(options, "font:", 5)) { 458 strscpy(fontname, options + 5, sizeof(fontname)); 459 continue; 460 } 461 462 if (!strncmp(options, "scrollback:", 11)) { 463 pr_warn("Ignoring scrollback size option\n"); 464 continue; 465 } 466 467 if (!strncmp(options, "map:", 4)) { 468 options += 4; 469 if (*options) { 470 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) { 471 if (!options[j]) 472 j = 0; 473 con2fb_map_boot[i] = 474 (options[j++]-'0') % FB_MAX; 475 } 476 477 fbcon_map_override(); 478 } 479 continue; 480 } 481 482 if (!strncmp(options, "vc:", 3)) { 483 options += 3; 484 if (*options) 485 first_fb_vc = simple_strtoul(options, &options, 10) - 1; 486 if (first_fb_vc >= MAX_NR_CONSOLES) 487 first_fb_vc = 0; 488 if (*options++ == '-') 489 last_fb_vc = simple_strtoul(options, &options, 10) - 1; 490 if (last_fb_vc < first_fb_vc || last_fb_vc >= MAX_NR_CONSOLES) 491 last_fb_vc = MAX_NR_CONSOLES - 1; 492 fbcon_is_default = false; 493 continue; 494 } 495 496 if (!strncmp(options, "rotate:", 7)) { 497 options += 7; 498 if (*options) 499 initial_rotation = simple_strtoul(options, &options, 0); 500 if (initial_rotation > 3) 501 initial_rotation = 0; 502 continue; 503 } 504 505 if (!strncmp(options, "margin:", 7)) { 506 options += 7; 507 if (*options) 508 margin_color = simple_strtoul(options, &options, 0); 509 continue; 510 } 511 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER 512 if (!strcmp(options, "nodefer")) { 513 deferred_takeover = false; 514 continue; 515 } 516 #endif 517 518 #ifdef CONFIG_LOGO 519 if (!strncmp(options, "logo-pos:", 9)) { 520 options += 9; 521 if (!strcmp(options, "center")) 522 fb_center_logo = true; 523 continue; 524 } 525 526 if (!strncmp(options, "logo-count:", 11)) { 527 options += 11; 528 if (*options) 529 fb_logo_count = simple_strtol(options, &options, 0); 530 continue; 531 } 532 #endif 533 } 534 return 1; 535 } 536 537 __setup("fbcon=", fb_console_setup); 538 #endif 539 540 static int search_fb_in_map(int idx) 541 { 542 int i, retval = 0; 543 544 for (i = first_fb_vc; i <= last_fb_vc; i++) { 545 if (con2fb_map[i] == idx) { 546 retval = 1; 547 break; 548 } 549 } 550 return retval; 551 } 552 553 static int search_for_mapped_con(void) 554 { 555 int i, retval = 0; 556 557 for (i = first_fb_vc; i <= last_fb_vc; i++) { 558 if (con2fb_map[i] != -1) { 559 retval = 1; 560 break; 561 } 562 } 563 return retval; 564 } 565 566 static int do_fbcon_takeover(int show_logo) 567 { 568 int err, i; 569 570 if (!fbcon_num_registered_fb) 571 return -ENODEV; 572 573 if (!show_logo) 574 logo_shown = FBCON_LOGO_DONTSHOW; 575 576 for (i = first_fb_vc; i <= last_fb_vc; i++) 577 con2fb_map[i] = info_idx; 578 579 err = do_take_over_console(&fb_con, first_fb_vc, last_fb_vc, 580 fbcon_is_default); 581 582 if (err) { 583 for (i = first_fb_vc; i <= last_fb_vc; i++) 584 con2fb_map[i] = -1; 585 info_idx = -1; 586 } else { 587 fbcon_has_console_bind = true; 588 } 589 590 return err; 591 } 592 593 #ifdef MODULE 594 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info, 595 int cols, int rows, int new_cols, int new_rows) 596 { 597 logo_shown = FBCON_LOGO_DONTSHOW; 598 } 599 #else 600 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info, 601 int cols, int rows, int new_cols, int new_rows) 602 { 603 /* Need to make room for the logo */ 604 struct fbcon_par *par = info->fbcon_par; 605 int cnt, erase = vc->vc_video_erase_char, step; 606 unsigned short *save = NULL, *r, *q; 607 int logo_height; 608 609 if (info->fbops->owner) { 610 logo_shown = FBCON_LOGO_DONTSHOW; 611 return; 612 } 613 614 /* 615 * remove underline attribute from erase character 616 * if black and white framebuffer. 617 */ 618 if (fb_get_color_depth(&info->var, &info->fix) == 1) 619 erase &= ~0x400; 620 logo_height = fb_prepare_logo(info, par->rotate); 621 logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height); 622 q = (unsigned short *) (vc->vc_origin + 623 vc->vc_size_row * rows); 624 step = logo_lines * cols; 625 for (r = q - logo_lines * cols; r < q; r++) 626 if (scr_readw(r) != vc->vc_video_erase_char) 627 break; 628 if (r != q && new_rows >= rows + logo_lines) { 629 save = kmalloc(array3_size(logo_lines, new_cols, 2), 630 GFP_KERNEL); 631 if (save) { 632 int i = min(cols, new_cols); 633 scr_memsetw(save, erase, array3_size(logo_lines, new_cols, 2)); 634 r = q - step; 635 for (cnt = 0; cnt < logo_lines; cnt++, r += i) 636 scr_memcpyw(save + cnt * new_cols, r, 2 * i); 637 r = q; 638 } 639 } 640 if (r == q) { 641 /* We can scroll screen down */ 642 r = q - step - cols; 643 for (cnt = rows - logo_lines; cnt > 0; cnt--) { 644 scr_memcpyw(r + step, r, vc->vc_size_row); 645 r -= cols; 646 } 647 if (!save) { 648 int lines; 649 if (vc->state.y + logo_lines >= rows) 650 lines = rows - vc->state.y - 1; 651 else 652 lines = logo_lines; 653 vc->state.y += lines; 654 vc->vc_pos += lines * vc->vc_size_row; 655 } 656 } 657 scr_memsetw((unsigned short *) vc->vc_origin, 658 erase, 659 vc->vc_size_row * logo_lines); 660 661 if (con_is_visible(vc) && vc->vc_mode == KD_TEXT) { 662 fbcon_clear_margins(vc, 0); 663 update_screen(vc); 664 } 665 666 if (save) { 667 q = (unsigned short *) (vc->vc_origin + 668 vc->vc_size_row * 669 rows); 670 scr_memcpyw(q, save, array3_size(logo_lines, new_cols, 2)); 671 vc->state.y += logo_lines; 672 vc->vc_pos += logo_lines * vc->vc_size_row; 673 kfree(save); 674 } 675 676 if (logo_shown == FBCON_LOGO_DONTSHOW) 677 return; 678 679 if (logo_lines > vc->vc_bottom) { 680 logo_shown = FBCON_LOGO_CANSHOW; 681 pr_info("fbcon: disable boot-logo (boot-logo bigger than screen).\n"); 682 } else { 683 logo_shown = FBCON_LOGO_DRAW; 684 vc->vc_top = logo_lines; 685 } 686 } 687 #endif /* MODULE */ 688 689 #ifdef CONFIG_FB_TILEBLITTING 690 static void set_blitting_type(struct vc_data *vc, struct fb_info *info) 691 { 692 struct fbcon_par *par = info->fbcon_par; 693 694 par->p = &fb_display[vc->vc_num]; 695 696 if ((info->flags & FBINFO_MISC_TILEBLITTING)) 697 fbcon_set_tileops(vc, info); 698 else { 699 fbcon_set_rotation(info); 700 fbcon_set_bitops(par); 701 } 702 } 703 704 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount) 705 { 706 int err = 0; 707 708 if (info->flags & FBINFO_MISC_TILEBLITTING && 709 info->tileops->fb_get_tilemax(info) < charcount) 710 err = 1; 711 712 return err; 713 } 714 #else 715 static void set_blitting_type(struct vc_data *vc, struct fb_info *info) 716 { 717 struct fbcon_par *par = info->fbcon_par; 718 719 info->flags &= ~FBINFO_MISC_TILEBLITTING; 720 par->p = &fb_display[vc->vc_num]; 721 fbcon_set_rotation(info); 722 fbcon_set_bitops(par); 723 } 724 725 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount) 726 { 727 return 0; 728 } 729 730 #endif /* CONFIG_MISC_TILEBLITTING */ 731 732 static void fbcon_release(struct fb_info *info) 733 { 734 lock_fb_info(info); 735 if (info->fbops->fb_release) 736 info->fbops->fb_release(info, 0); 737 unlock_fb_info(info); 738 739 module_put(info->fbops->owner); 740 741 if (info->fbcon_par) { 742 struct fbcon_par *par = info->fbcon_par; 743 744 fbcon_del_cursor_work(info); 745 kfree(par->cursor_state.mask); 746 kfree(par->cursor_data); 747 kfree(par->cursor_src); 748 kfree(par->fontbuffer); 749 kfree(info->fbcon_par); 750 info->fbcon_par = NULL; 751 } 752 } 753 754 static int fbcon_open(struct fb_info *info) 755 { 756 struct fbcon_par *par; 757 758 if (!try_module_get(info->fbops->owner)) 759 return -ENODEV; 760 761 lock_fb_info(info); 762 if (info->fbops->fb_open && 763 info->fbops->fb_open(info, 0)) { 764 unlock_fb_info(info); 765 module_put(info->fbops->owner); 766 return -ENODEV; 767 } 768 unlock_fb_info(info); 769 770 par = kzalloc(sizeof(*par), GFP_KERNEL); 771 if (!par) { 772 fbcon_release(info); 773 return -ENOMEM; 774 } 775 776 INIT_DELAYED_WORK(&par->cursor_work, fb_flashcursor); 777 par->info = info; 778 info->fbcon_par = par; 779 par->cur_blink_jiffies = HZ / 5; 780 781 return 0; 782 } 783 784 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info, 785 int unit) 786 { 787 int err; 788 789 err = fbcon_open(info); 790 if (err) 791 return err; 792 793 if (vc) 794 set_blitting_type(vc, info); 795 796 return err; 797 } 798 799 static void con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo, 800 struct fb_info *newinfo) 801 { 802 int ret; 803 804 fbcon_release(oldinfo); 805 806 /* 807 If oldinfo and newinfo are driving the same hardware, 808 the fb_release() method of oldinfo may attempt to 809 restore the hardware state. This will leave the 810 newinfo in an undefined state. Thus, a call to 811 fb_set_par() may be needed for the newinfo. 812 */ 813 if (newinfo && newinfo->fbops->fb_set_par) { 814 ret = newinfo->fbops->fb_set_par(newinfo); 815 816 if (ret) 817 printk(KERN_ERR "con2fb_release_oldinfo: " 818 "detected unhandled fb_set_par error, " 819 "error code %d\n", ret); 820 } 821 } 822 823 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info, 824 int unit, int show_logo) 825 { 826 struct fbcon_par *par = info->fbcon_par; 827 int ret; 828 829 par->currcon = fg_console; 830 831 if (info->fbops->fb_set_par && !par->initialized) { 832 ret = info->fbops->fb_set_par(info); 833 834 if (ret) 835 printk(KERN_ERR "con2fb_init_display: detected " 836 "unhandled fb_set_par error, " 837 "error code %d\n", ret); 838 } 839 840 par->initialized = true; 841 par->graphics = 0; 842 fbcon_set_disp(info, &info->var, unit); 843 844 if (show_logo) { 845 struct vc_data *fg_vc = vc_cons[fg_console].d; 846 struct fb_info *fg_info = 847 fbcon_info_from_console(fg_console); 848 849 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols, 850 fg_vc->vc_rows, fg_vc->vc_cols, 851 fg_vc->vc_rows); 852 } 853 854 if (fg_console != unit) 855 update_screen(vc_cons[fg_console].d); 856 } 857 858 /** 859 * set_con2fb_map - map console to frame buffer device 860 * @unit: virtual console number to map 861 * @newidx: frame buffer index to map virtual console to 862 * @user: user request 863 * 864 * Maps a virtual console @unit to a frame buffer device 865 * @newidx. 866 * 867 * This should be called with the console lock held. 868 */ 869 static int set_con2fb_map(int unit, int newidx, int user) 870 { 871 struct vc_data *vc = vc_cons[unit].d; 872 int oldidx = con2fb_map[unit]; 873 struct fb_info *info = fbcon_registered_fb[newidx]; 874 struct fb_info *oldinfo = NULL; 875 int err = 0, show_logo; 876 877 WARN_CONSOLE_UNLOCKED(); 878 879 if (oldidx == newidx) 880 return 0; 881 882 if (!info) 883 return -EINVAL; 884 885 if (!search_for_mapped_con() || !con_is_bound(&fb_con)) { 886 info_idx = newidx; 887 return do_fbcon_takeover(0); 888 } 889 890 if (oldidx != -1) 891 oldinfo = fbcon_registered_fb[oldidx]; 892 893 if (!search_fb_in_map(newidx)) { 894 err = con2fb_acquire_newinfo(vc, info, unit); 895 if (err) 896 return err; 897 898 fbcon_add_cursor_work(info); 899 } else if (vc) { 900 set_blitting_type(vc, info); 901 } 902 903 con2fb_map[unit] = newidx; 904 905 /* 906 * If old fb is not mapped to any of the consoles, 907 * fbcon should release it. 908 */ 909 if (oldinfo && !search_fb_in_map(oldidx)) 910 con2fb_release_oldinfo(vc, oldinfo, info); 911 912 show_logo = (fg_console == 0 && !user && 913 logo_shown != FBCON_LOGO_DONTSHOW); 914 915 con2fb_map_boot[unit] = newidx; 916 con2fb_init_display(vc, info, unit, show_logo); 917 918 if (!search_fb_in_map(info_idx)) 919 info_idx = newidx; 920 921 return err; 922 } 923 924 /* 925 * Low Level Operations 926 */ 927 /* NOTE: fbcon cannot be __init: it may be called from do_take_over_console later */ 928 static int var_to_display(struct fbcon_display *disp, 929 struct fb_var_screeninfo *var, 930 struct fb_info *info) 931 { 932 disp->xres_virtual = var->xres_virtual; 933 disp->yres_virtual = var->yres_virtual; 934 disp->bits_per_pixel = var->bits_per_pixel; 935 disp->grayscale = var->grayscale; 936 disp->nonstd = var->nonstd; 937 disp->accel_flags = var->accel_flags; 938 disp->height = var->height; 939 disp->width = var->width; 940 disp->red = var->red; 941 disp->green = var->green; 942 disp->blue = var->blue; 943 disp->transp = var->transp; 944 disp->rotate = var->rotate; 945 disp->mode = fb_match_mode(var, &info->modelist); 946 if (disp->mode == NULL) 947 /* This should not happen */ 948 return -EINVAL; 949 return 0; 950 } 951 952 static void display_to_var(struct fb_var_screeninfo *var, 953 struct fbcon_display *disp) 954 { 955 fb_videomode_to_var(var, disp->mode); 956 var->xres_virtual = disp->xres_virtual; 957 var->yres_virtual = disp->yres_virtual; 958 var->bits_per_pixel = disp->bits_per_pixel; 959 var->grayscale = disp->grayscale; 960 var->nonstd = disp->nonstd; 961 var->accel_flags = disp->accel_flags; 962 var->height = disp->height; 963 var->width = disp->width; 964 var->red = disp->red; 965 var->green = disp->green; 966 var->blue = disp->blue; 967 var->transp = disp->transp; 968 var->rotate = disp->rotate; 969 } 970 971 static const char *fbcon_startup(void) 972 { 973 static const char display_desc[] = "frame buffer device"; 974 struct fbcon_display *p = &fb_display[fg_console]; 975 struct vc_data *vc = vc_cons[fg_console].d; 976 const struct font_desc *font = NULL; 977 struct fb_info *info = NULL; 978 struct fbcon_par *par; 979 int rows, cols; 980 981 /* 982 * If fbcon_num_registered_fb is zero, this is a call for the dummy part. 983 * The frame buffer devices weren't initialized yet. 984 */ 985 if (!fbcon_num_registered_fb || info_idx == -1) 986 return display_desc; 987 /* 988 * Instead of blindly using fbcon_registered_fb[0], we use info_idx, set by 989 * fbcon_fb_registered(); 990 */ 991 info = fbcon_registered_fb[info_idx]; 992 if (!info) 993 return NULL; 994 995 if (fbcon_open(info)) 996 return NULL; 997 998 par = info->fbcon_par; 999 par->currcon = -1; 1000 par->graphics = 1; 1001 par->cur_rotate = -1; 1002 1003 p->con_rotate = initial_rotation; 1004 if (p->con_rotate == -1) 1005 p->con_rotate = info->fbcon_rotate_hint; 1006 if (p->con_rotate == -1) 1007 p->con_rotate = FB_ROTATE_UR; 1008 1009 set_blitting_type(vc, info); 1010 1011 /* Setup default font */ 1012 if (!p->fontdata) { 1013 if (!fontname[0] || !(font = find_font(fontname))) 1014 font = get_default_font(info->var.xres, 1015 info->var.yres, 1016 info->pixmap.blit_x, 1017 info->pixmap.blit_y); 1018 vc->vc_font.width = font->width; 1019 vc->vc_font.height = font->height; 1020 vc->vc_font.data = (void *)(p->fontdata = font->data); 1021 vc->vc_font.charcount = font->charcount; 1022 } 1023 1024 cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres); 1025 rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 1026 cols /= vc->vc_font.width; 1027 rows /= vc->vc_font.height; 1028 vc_resize(vc, cols, rows); 1029 1030 pr_debug("mode: %s\n", info->fix.id); 1031 pr_debug("visual: %d\n", info->fix.visual); 1032 pr_debug("res: %dx%d-%d\n", info->var.xres, 1033 info->var.yres, 1034 info->var.bits_per_pixel); 1035 1036 fbcon_add_cursor_work(info); 1037 return display_desc; 1038 } 1039 1040 static void fbcon_init(struct vc_data *vc, bool init) 1041 { 1042 struct fb_info *info; 1043 struct fbcon_par *par; 1044 struct vc_data **default_mode = vc->vc_display_fg; 1045 struct vc_data *svc = *default_mode; 1046 struct fbcon_display *t, *p = &fb_display[vc->vc_num]; 1047 int logo = 1, new_rows, new_cols, rows, cols; 1048 int ret; 1049 1050 if (WARN_ON(info_idx == -1)) 1051 return; 1052 1053 if (con2fb_map[vc->vc_num] == -1) 1054 con2fb_map[vc->vc_num] = info_idx; 1055 1056 info = fbcon_info_from_console(vc->vc_num); 1057 1058 if (logo_shown < 0 && console_loglevel <= CONSOLE_LOGLEVEL_QUIET) 1059 logo_shown = FBCON_LOGO_DONTSHOW; 1060 1061 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW || 1062 (info->fix.type == FB_TYPE_TEXT)) 1063 logo = 0; 1064 1065 if (var_to_display(p, &info->var, info)) 1066 return; 1067 1068 if (!info->fbcon_par) 1069 con2fb_acquire_newinfo(vc, info, vc->vc_num); 1070 1071 /* If we are not the first console on this 1072 fb, copy the font from that console */ 1073 t = &fb_display[fg_console]; 1074 if (!p->fontdata) { 1075 if (t->fontdata) { 1076 struct vc_data *fvc = vc_cons[fg_console].d; 1077 1078 vc->vc_font.data = (void *)(p->fontdata = 1079 fvc->vc_font.data); 1080 vc->vc_font.width = fvc->vc_font.width; 1081 vc->vc_font.height = fvc->vc_font.height; 1082 vc->vc_font.charcount = fvc->vc_font.charcount; 1083 p->userfont = t->userfont; 1084 1085 if (p->userfont) 1086 REFCOUNT(p->fontdata)++; 1087 } else { 1088 const struct font_desc *font = NULL; 1089 1090 if (!fontname[0] || !(font = find_font(fontname))) 1091 font = get_default_font(info->var.xres, 1092 info->var.yres, 1093 info->pixmap.blit_x, 1094 info->pixmap.blit_y); 1095 vc->vc_font.width = font->width; 1096 vc->vc_font.height = font->height; 1097 vc->vc_font.data = (void *)(p->fontdata = font->data); 1098 vc->vc_font.charcount = font->charcount; 1099 } 1100 } 1101 1102 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1); 1103 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; 1104 if (vc->vc_font.charcount == 256) { 1105 vc->vc_hi_font_mask = 0; 1106 } else { 1107 vc->vc_hi_font_mask = 0x100; 1108 if (vc->vc_can_do_color) 1109 vc->vc_complement_mask <<= 1; 1110 } 1111 1112 if (!*svc->uni_pagedict_loc) 1113 con_set_default_unimap(svc); 1114 if (!*vc->uni_pagedict_loc) 1115 con_copy_unimap(vc, svc); 1116 1117 par = info->fbcon_par; 1118 par->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms); 1119 1120 p->con_rotate = initial_rotation; 1121 if (p->con_rotate == -1) 1122 p->con_rotate = info->fbcon_rotate_hint; 1123 if (p->con_rotate == -1) 1124 p->con_rotate = FB_ROTATE_UR; 1125 1126 set_blitting_type(vc, info); 1127 1128 cols = vc->vc_cols; 1129 rows = vc->vc_rows; 1130 new_cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres); 1131 new_rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 1132 new_cols /= vc->vc_font.width; 1133 new_rows /= vc->vc_font.height; 1134 1135 /* 1136 * We must always set the mode. The mode of the previous console 1137 * driver could be in the same resolution but we are using different 1138 * hardware so we have to initialize the hardware. 1139 * 1140 * We need to do it in fbcon_init() to prevent screen corruption. 1141 */ 1142 if (con_is_visible(vc) && vc->vc_mode == KD_TEXT) { 1143 if (info->fbops->fb_set_par && !par->initialized) { 1144 ret = info->fbops->fb_set_par(info); 1145 1146 if (ret) 1147 printk(KERN_ERR "fbcon_init: detected " 1148 "unhandled fb_set_par error, " 1149 "error code %d\n", ret); 1150 } 1151 1152 par->initialized = true; 1153 } 1154 1155 par->graphics = 0; 1156 1157 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_LEGACY_ACCELERATION 1158 if ((info->flags & FBINFO_HWACCEL_COPYAREA) && 1159 !(info->flags & FBINFO_HWACCEL_DISABLED)) 1160 p->scrollmode = SCROLL_MOVE; 1161 else /* default to something safe */ 1162 p->scrollmode = SCROLL_REDRAW; 1163 #endif 1164 1165 /* 1166 * ++guenther: console.c:vc_allocate() relies on initializing 1167 * vc_{cols,rows}, but we must not set those if we are only 1168 * resizing the console. 1169 */ 1170 if (init) { 1171 vc->vc_cols = new_cols; 1172 vc->vc_rows = new_rows; 1173 } else 1174 vc_resize(vc, new_cols, new_rows); 1175 1176 if (logo) 1177 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows); 1178 1179 if (par->bitops->rotate_font && par->bitops->rotate_font(info, vc)) { 1180 par->rotate = FB_ROTATE_UR; 1181 set_blitting_type(vc, info); 1182 } 1183 1184 par->p = &fb_display[fg_console]; 1185 } 1186 1187 static void fbcon_free_font(struct fbcon_display *p) 1188 { 1189 if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0)) 1190 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int)); 1191 p->fontdata = NULL; 1192 p->userfont = 0; 1193 } 1194 1195 static void set_vc_hi_font(struct vc_data *vc, bool set); 1196 1197 static void fbcon_release_all(void) 1198 { 1199 struct fb_info *info; 1200 int i, j, mapped; 1201 1202 fbcon_for_each_registered_fb(i) { 1203 mapped = 0; 1204 info = fbcon_registered_fb[i]; 1205 1206 for (j = first_fb_vc; j <= last_fb_vc; j++) { 1207 if (con2fb_map[j] == i) { 1208 mapped = 1; 1209 con2fb_map[j] = -1; 1210 } 1211 } 1212 1213 if (mapped) 1214 fbcon_release(info); 1215 } 1216 } 1217 1218 static void fbcon_deinit(struct vc_data *vc) 1219 { 1220 struct fbcon_display *p = &fb_display[vc->vc_num]; 1221 struct fb_info *info; 1222 struct fbcon_par *par; 1223 int idx; 1224 1225 fbcon_free_font(p); 1226 idx = con2fb_map[vc->vc_num]; 1227 1228 if (idx == -1) 1229 goto finished; 1230 1231 info = fbcon_registered_fb[idx]; 1232 1233 if (!info) 1234 goto finished; 1235 1236 par = info->fbcon_par; 1237 1238 if (!par) 1239 goto finished; 1240 1241 if (con_is_visible(vc)) 1242 fbcon_del_cursor_work(info); 1243 1244 par->initialized = false; 1245 finished: 1246 1247 fbcon_free_font(p); 1248 vc->vc_font.data = NULL; 1249 1250 if (vc->vc_hi_font_mask && vc->vc_screenbuf) 1251 set_vc_hi_font(vc, false); 1252 1253 if (!con_is_bound(&fb_con)) 1254 fbcon_release_all(); 1255 1256 if (vc->vc_num == logo_shown) 1257 logo_shown = FBCON_LOGO_CANSHOW; 1258 1259 return; 1260 } 1261 1262 /* ====================================================================== */ 1263 1264 /* fbcon_XXX routines - interface used by the world 1265 * 1266 * This system is now divided into two levels because of complications 1267 * caused by hardware scrolling. Top level functions: 1268 * 1269 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins() 1270 * 1271 * handles y values in range [0, scr_height-1] that correspond to real 1272 * screen positions. y_wrap shift means that first line of bitmap may be 1273 * anywhere on this display. These functions convert lineoffsets to 1274 * bitmap offsets and deal with the wrap-around case by splitting blits. 1275 * 1276 * fbcon_bmove_physical_8() -- These functions fast implementations 1277 * fbcon_clear_physical_8() -- of original fbcon_XXX fns. 1278 * fbcon_putc_physical_8() -- (font width != 8) may be added later 1279 * 1280 * WARNING: 1281 * 1282 * At the moment fbcon_putc() cannot blit across vertical wrap boundary 1283 * Implies should only really hardware scroll in rows. Only reason for 1284 * restriction is simplicity & efficiency at the moment. 1285 */ 1286 1287 static void __fbcon_clear(struct vc_data *vc, unsigned int sy, unsigned int sx, 1288 unsigned int height, unsigned int width) 1289 { 1290 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1291 struct fbcon_par *par = info->fbcon_par; 1292 int fg, bg; 1293 struct fbcon_display *p = &fb_display[vc->vc_num]; 1294 u_int y_break; 1295 1296 if (!fbcon_is_active(vc, info)) 1297 return; 1298 1299 if (!height || !width) 1300 return; 1301 1302 if (sy < vc->vc_top && vc->vc_top == logo_lines) { 1303 vc->vc_top = 0; 1304 /* 1305 * If the font dimensions are not an integral of the display 1306 * dimensions then the par->clear below won't end up clearing 1307 * the margins. Call clear_margins here in case the logo 1308 * bitmap stretched into the margin area. 1309 */ 1310 fbcon_clear_margins(vc, 0); 1311 } 1312 1313 fg = get_color(vc, info, vc->vc_video_erase_char, 1); 1314 bg = get_color(vc, info, vc->vc_video_erase_char, 0); 1315 /* Split blits that cross physical y_wrap boundary */ 1316 1317 y_break = p->vrows - p->yscroll; 1318 if (sy < y_break && sy + height - 1 >= y_break) { 1319 u_int b = y_break - sy; 1320 par->bitops->clear(vc, info, real_y(p, sy), sx, b, width, fg, bg); 1321 par->bitops->clear(vc, info, real_y(p, sy + b), sx, height - b, 1322 width, fg, bg); 1323 } else 1324 par->bitops->clear(vc, info, real_y(p, sy), sx, height, width, fg, bg); 1325 } 1326 1327 static void fbcon_clear(struct vc_data *vc, unsigned int sy, unsigned int sx, 1328 unsigned int width) 1329 { 1330 __fbcon_clear(vc, sy, sx, 1, width); 1331 } 1332 1333 static void fbcon_putcs(struct vc_data *vc, const u16 *s, unsigned int count, 1334 unsigned int ypos, unsigned int xpos) 1335 { 1336 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1337 struct fbcon_display *p = &fb_display[vc->vc_num]; 1338 struct fbcon_par *par = info->fbcon_par; 1339 1340 if (fbcon_is_active(vc, info)) 1341 par->bitops->putcs(vc, info, s, count, real_y(p, ypos), xpos, 1342 get_fg_color(vc, info, scr_readw(s)), 1343 get_bg_color(vc, info, scr_readw(s))); 1344 } 1345 1346 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only) 1347 { 1348 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1349 struct fbcon_par *par = info->fbcon_par; 1350 1351 if (fbcon_is_active(vc, info)) 1352 par->bitops->clear_margins(vc, info, margin_color, bottom_only); 1353 } 1354 1355 static void fbcon_cursor(struct vc_data *vc, bool enable) 1356 { 1357 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1358 struct fbcon_par *par = info->fbcon_par; 1359 int c = scr_readw((u16 *) vc->vc_pos); 1360 1361 par->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms); 1362 1363 if (!fbcon_is_active(vc, info) || vc->vc_deccm != 1) 1364 return; 1365 1366 if (vc->vc_cursor_type & CUR_SW) 1367 fbcon_del_cursor_work(info); 1368 else 1369 fbcon_add_cursor_work(info); 1370 1371 par->cursor_flash = enable; 1372 1373 if (!par->bitops->cursor) 1374 return; 1375 1376 par->bitops->cursor(vc, info, enable, 1377 get_fg_color(vc, info, c), 1378 get_bg_color(vc, info, c)); 1379 } 1380 1381 static int scrollback_phys_max = 0; 1382 static int scrollback_max = 0; 1383 static int scrollback_current = 0; 1384 1385 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var, 1386 int unit) 1387 { 1388 struct fbcon_display *p, *t; 1389 struct vc_data **default_mode, *vc; 1390 struct vc_data *svc; 1391 struct fbcon_par *par = info->fbcon_par; 1392 int rows, cols; 1393 unsigned long ret = 0; 1394 1395 p = &fb_display[unit]; 1396 1397 if (var_to_display(p, var, info)) 1398 return; 1399 1400 vc = vc_cons[unit].d; 1401 1402 if (!vc) 1403 return; 1404 1405 default_mode = vc->vc_display_fg; 1406 svc = *default_mode; 1407 t = &fb_display[svc->vc_num]; 1408 1409 if (!vc->vc_font.data) { 1410 vc->vc_font.data = (void *)(p->fontdata = t->fontdata); 1411 vc->vc_font.width = (*default_mode)->vc_font.width; 1412 vc->vc_font.height = (*default_mode)->vc_font.height; 1413 vc->vc_font.charcount = (*default_mode)->vc_font.charcount; 1414 p->userfont = t->userfont; 1415 if (p->userfont) 1416 REFCOUNT(p->fontdata)++; 1417 } 1418 1419 var->activate = FB_ACTIVATE_NOW; 1420 info->var.activate = var->activate; 1421 var->yoffset = info->var.yoffset; 1422 var->xoffset = info->var.xoffset; 1423 fb_set_var(info, var); 1424 par->var = info->var; 1425 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1); 1426 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; 1427 if (vc->vc_font.charcount == 256) { 1428 vc->vc_hi_font_mask = 0; 1429 } else { 1430 vc->vc_hi_font_mask = 0x100; 1431 if (vc->vc_can_do_color) 1432 vc->vc_complement_mask <<= 1; 1433 } 1434 1435 if (!*svc->uni_pagedict_loc) 1436 con_set_default_unimap(svc); 1437 if (!*vc->uni_pagedict_loc) 1438 con_copy_unimap(vc, svc); 1439 1440 cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres); 1441 rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 1442 cols /= vc->vc_font.width; 1443 rows /= vc->vc_font.height; 1444 ret = vc_resize(vc, cols, rows); 1445 1446 if (con_is_visible(vc) && !ret) 1447 update_screen(vc); 1448 } 1449 1450 static __inline__ void ywrap_up(struct vc_data *vc, int count) 1451 { 1452 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1453 struct fbcon_par *par = info->fbcon_par; 1454 struct fbcon_display *p = &fb_display[vc->vc_num]; 1455 1456 p->yscroll += count; 1457 if (p->yscroll >= p->vrows) /* Deal with wrap */ 1458 p->yscroll -= p->vrows; 1459 par->var.xoffset = 0; 1460 par->var.yoffset = p->yscroll * vc->vc_font.height; 1461 par->var.vmode |= FB_VMODE_YWRAP; 1462 par->bitops->update_start(info); 1463 scrollback_max += count; 1464 if (scrollback_max > scrollback_phys_max) 1465 scrollback_max = scrollback_phys_max; 1466 scrollback_current = 0; 1467 } 1468 1469 static __inline__ void ywrap_down(struct vc_data *vc, int count) 1470 { 1471 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1472 struct fbcon_par *par = info->fbcon_par; 1473 struct fbcon_display *p = &fb_display[vc->vc_num]; 1474 1475 p->yscroll -= count; 1476 if (p->yscroll < 0) /* Deal with wrap */ 1477 p->yscroll += p->vrows; 1478 par->var.xoffset = 0; 1479 par->var.yoffset = p->yscroll * vc->vc_font.height; 1480 par->var.vmode |= FB_VMODE_YWRAP; 1481 par->bitops->update_start(info); 1482 scrollback_max -= count; 1483 if (scrollback_max < 0) 1484 scrollback_max = 0; 1485 scrollback_current = 0; 1486 } 1487 1488 static __inline__ void ypan_up(struct vc_data *vc, int count) 1489 { 1490 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1491 struct fbcon_display *p = &fb_display[vc->vc_num]; 1492 struct fbcon_par *par = info->fbcon_par; 1493 1494 p->yscroll += count; 1495 if (p->yscroll > p->vrows - vc->vc_rows) { 1496 par->bitops->bmove(vc, info, p->vrows - vc->vc_rows, 1497 0, 0, 0, vc->vc_rows, vc->vc_cols); 1498 p->yscroll -= p->vrows - vc->vc_rows; 1499 } 1500 1501 par->var.xoffset = 0; 1502 par->var.yoffset = p->yscroll * vc->vc_font.height; 1503 par->var.vmode &= ~FB_VMODE_YWRAP; 1504 par->bitops->update_start(info); 1505 fbcon_clear_margins(vc, 1); 1506 scrollback_max += count; 1507 if (scrollback_max > scrollback_phys_max) 1508 scrollback_max = scrollback_phys_max; 1509 scrollback_current = 0; 1510 } 1511 1512 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count) 1513 { 1514 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1515 struct fbcon_par *par = info->fbcon_par; 1516 struct fbcon_display *p = &fb_display[vc->vc_num]; 1517 1518 p->yscroll += count; 1519 1520 if (p->yscroll > p->vrows - vc->vc_rows) { 1521 p->yscroll -= p->vrows - vc->vc_rows; 1522 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t); 1523 } 1524 1525 par->var.xoffset = 0; 1526 par->var.yoffset = p->yscroll * vc->vc_font.height; 1527 par->var.vmode &= ~FB_VMODE_YWRAP; 1528 par->bitops->update_start(info); 1529 fbcon_clear_margins(vc, 1); 1530 scrollback_max += count; 1531 if (scrollback_max > scrollback_phys_max) 1532 scrollback_max = scrollback_phys_max; 1533 scrollback_current = 0; 1534 } 1535 1536 static __inline__ void ypan_down(struct vc_data *vc, int count) 1537 { 1538 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1539 struct fbcon_display *p = &fb_display[vc->vc_num]; 1540 struct fbcon_par *par = info->fbcon_par; 1541 1542 p->yscroll -= count; 1543 if (p->yscroll < 0) { 1544 par->bitops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows, 1545 0, vc->vc_rows, vc->vc_cols); 1546 p->yscroll += p->vrows - vc->vc_rows; 1547 } 1548 1549 par->var.xoffset = 0; 1550 par->var.yoffset = p->yscroll * vc->vc_font.height; 1551 par->var.vmode &= ~FB_VMODE_YWRAP; 1552 par->bitops->update_start(info); 1553 fbcon_clear_margins(vc, 1); 1554 scrollback_max -= count; 1555 if (scrollback_max < 0) 1556 scrollback_max = 0; 1557 scrollback_current = 0; 1558 } 1559 1560 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count) 1561 { 1562 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1563 struct fbcon_par *par = info->fbcon_par; 1564 struct fbcon_display *p = &fb_display[vc->vc_num]; 1565 1566 p->yscroll -= count; 1567 1568 if (p->yscroll < 0) { 1569 p->yscroll += p->vrows - vc->vc_rows; 1570 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count); 1571 } 1572 1573 par->var.xoffset = 0; 1574 par->var.yoffset = p->yscroll * vc->vc_font.height; 1575 par->var.vmode &= ~FB_VMODE_YWRAP; 1576 par->bitops->update_start(info); 1577 fbcon_clear_margins(vc, 1); 1578 scrollback_max -= count; 1579 if (scrollback_max < 0) 1580 scrollback_max = 0; 1581 scrollback_current = 0; 1582 } 1583 1584 static void fbcon_redraw_move(struct vc_data *vc, struct fbcon_display *p, 1585 int line, int count, int dy) 1586 { 1587 unsigned short *s = (unsigned short *) 1588 (vc->vc_origin + vc->vc_size_row * line); 1589 1590 while (count--) { 1591 unsigned short *start = s; 1592 unsigned short *le = advance_row(s, 1); 1593 unsigned short c; 1594 int x = 0; 1595 unsigned short attr = 1; 1596 1597 do { 1598 c = scr_readw(s); 1599 if (attr != (c & 0xff00)) { 1600 attr = c & 0xff00; 1601 if (s > start) { 1602 fbcon_putcs(vc, start, s - start, 1603 dy, x); 1604 x += s - start; 1605 start = s; 1606 } 1607 } 1608 console_conditional_schedule(); 1609 s++; 1610 } while (s < le); 1611 if (s > start) 1612 fbcon_putcs(vc, start, s - start, dy, x); 1613 console_conditional_schedule(); 1614 dy++; 1615 } 1616 } 1617 1618 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info, 1619 struct fbcon_display *p, int line, int count, int ycount) 1620 { 1621 int offset = ycount * vc->vc_cols; 1622 unsigned short *d = (unsigned short *) 1623 (vc->vc_origin + vc->vc_size_row * line); 1624 unsigned short *s = d + offset; 1625 struct fbcon_par *par = info->fbcon_par; 1626 1627 while (count--) { 1628 unsigned short *start = s; 1629 unsigned short *le = advance_row(s, 1); 1630 unsigned short c; 1631 int x = 0; 1632 1633 do { 1634 c = scr_readw(s); 1635 1636 if (c == scr_readw(d)) { 1637 if (s > start) { 1638 par->bitops->bmove(vc, info, line + ycount, x, 1639 line, x, 1, s - start); 1640 x += s - start + 1; 1641 start = s + 1; 1642 } else { 1643 x++; 1644 start++; 1645 } 1646 } 1647 1648 scr_writew(c, d); 1649 console_conditional_schedule(); 1650 s++; 1651 d++; 1652 } while (s < le); 1653 if (s > start) 1654 par->bitops->bmove(vc, info, line + ycount, x, line, x, 1, 1655 s - start); 1656 console_conditional_schedule(); 1657 if (ycount > 0) 1658 line++; 1659 else { 1660 line--; 1661 /* NOTE: We subtract two lines from these pointers */ 1662 s -= vc->vc_size_row; 1663 d -= vc->vc_size_row; 1664 } 1665 } 1666 } 1667 1668 static void fbcon_redraw(struct vc_data *vc, int line, int count, int offset) 1669 { 1670 unsigned short *d = (unsigned short *) 1671 (vc->vc_origin + vc->vc_size_row * line); 1672 unsigned short *s = d + offset; 1673 1674 while (count--) { 1675 unsigned short *start = s; 1676 unsigned short *le = advance_row(s, 1); 1677 unsigned short c; 1678 int x = 0; 1679 unsigned short attr = 1; 1680 1681 do { 1682 c = scr_readw(s); 1683 if (attr != (c & 0xff00)) { 1684 attr = c & 0xff00; 1685 if (s > start) { 1686 fbcon_putcs(vc, start, s - start, 1687 line, x); 1688 x += s - start; 1689 start = s; 1690 } 1691 } 1692 if (c == scr_readw(d)) { 1693 if (s > start) { 1694 fbcon_putcs(vc, start, s - start, 1695 line, x); 1696 x += s - start + 1; 1697 start = s + 1; 1698 } else { 1699 x++; 1700 start++; 1701 } 1702 } 1703 scr_writew(c, d); 1704 console_conditional_schedule(); 1705 s++; 1706 d++; 1707 } while (s < le); 1708 if (s > start) 1709 fbcon_putcs(vc, start, s - start, line, x); 1710 console_conditional_schedule(); 1711 if (offset > 0) 1712 line++; 1713 else { 1714 line--; 1715 /* NOTE: We subtract two lines from these pointers */ 1716 s -= vc->vc_size_row; 1717 d -= vc->vc_size_row; 1718 } 1719 } 1720 } 1721 1722 static void fbcon_bmove_rec(struct vc_data *vc, struct fbcon_display *p, int sy, int sx, 1723 int dy, int dx, int height, int width, u_int y_break) 1724 { 1725 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1726 struct fbcon_par *par = info->fbcon_par; 1727 u_int b; 1728 1729 if (sy < y_break && sy + height > y_break) { 1730 b = y_break - sy; 1731 if (dy < sy) { /* Avoid trashing self */ 1732 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width, 1733 y_break); 1734 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx, 1735 height - b, width, y_break); 1736 } else { 1737 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx, 1738 height - b, width, y_break); 1739 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width, 1740 y_break); 1741 } 1742 return; 1743 } 1744 1745 if (dy < y_break && dy + height > y_break) { 1746 b = y_break - dy; 1747 if (dy < sy) { /* Avoid trashing self */ 1748 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width, 1749 y_break); 1750 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx, 1751 height - b, width, y_break); 1752 } else { 1753 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx, 1754 height - b, width, y_break); 1755 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width, 1756 y_break); 1757 } 1758 return; 1759 } 1760 par->bitops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx, 1761 height, width); 1762 } 1763 1764 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx, 1765 int height, int width) 1766 { 1767 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1768 struct fbcon_display *p = &fb_display[vc->vc_num]; 1769 1770 if (!fbcon_is_active(vc, info)) 1771 return; 1772 1773 if (!width || !height) 1774 return; 1775 1776 /* Split blits that cross physical y_wrap case. 1777 * Pathological case involves 4 blits, better to use recursive 1778 * code rather than unrolled case 1779 * 1780 * Recursive invocations don't need to erase the cursor over and 1781 * over again, so we use fbcon_bmove_rec() 1782 */ 1783 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width, 1784 p->vrows - p->yscroll); 1785 } 1786 1787 static bool fbcon_scroll(struct vc_data *vc, unsigned int t, unsigned int b, 1788 enum con_scroll dir, unsigned int count) 1789 { 1790 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 1791 struct fbcon_display *p = &fb_display[vc->vc_num]; 1792 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK; 1793 1794 if (!fbcon_is_active(vc, info)) 1795 return true; 1796 1797 fbcon_cursor(vc, false); 1798 1799 /* 1800 * ++Geert: Only use ywrap/ypan if the console is in text mode 1801 * ++Andrew: Only use ypan on hardware text mode when scrolling the 1802 * whole screen (prevents flicker). 1803 */ 1804 1805 switch (dir) { 1806 case SM_UP: 1807 if (count > vc->vc_rows) /* Maximum realistic size */ 1808 count = vc->vc_rows; 1809 switch (fb_scrollmode(p)) { 1810 case SCROLL_MOVE: 1811 fbcon_redraw_blit(vc, info, p, t, b - t - count, 1812 count); 1813 __fbcon_clear(vc, b - count, 0, count, vc->vc_cols); 1814 scr_memsetw((unsigned short *) (vc->vc_origin + 1815 vc->vc_size_row * 1816 (b - count)), 1817 vc->vc_video_erase_char, 1818 vc->vc_size_row * count); 1819 return true; 1820 1821 case SCROLL_WRAP_MOVE: 1822 if (b - t - count > 3 * vc->vc_rows >> 2) { 1823 if (t > 0) 1824 fbcon_bmove(vc, 0, 0, count, 0, t, 1825 vc->vc_cols); 1826 ywrap_up(vc, count); 1827 if (vc->vc_rows - b > 0) 1828 fbcon_bmove(vc, b - count, 0, b, 0, 1829 vc->vc_rows - b, 1830 vc->vc_cols); 1831 } else if (info->flags & FBINFO_READS_FAST) 1832 fbcon_bmove(vc, t + count, 0, t, 0, 1833 b - t - count, vc->vc_cols); 1834 else 1835 goto redraw_up; 1836 __fbcon_clear(vc, b - count, 0, count, vc->vc_cols); 1837 break; 1838 1839 case SCROLL_PAN_REDRAW: 1840 if ((p->yscroll + count <= 1841 2 * (p->vrows - vc->vc_rows)) 1842 && ((!scroll_partial && (b - t == vc->vc_rows)) 1843 || (scroll_partial 1844 && (b - t - count > 1845 3 * vc->vc_rows >> 2)))) { 1846 if (t > 0) 1847 fbcon_redraw_move(vc, p, 0, t, count); 1848 ypan_up_redraw(vc, t, count); 1849 if (vc->vc_rows - b > 0) 1850 fbcon_redraw_move(vc, p, b, 1851 vc->vc_rows - b, b); 1852 } else 1853 fbcon_redraw_move(vc, p, t + count, b - t - count, t); 1854 __fbcon_clear(vc, b - count, 0, count, vc->vc_cols); 1855 break; 1856 1857 case SCROLL_PAN_MOVE: 1858 if ((p->yscroll + count <= 1859 2 * (p->vrows - vc->vc_rows)) 1860 && ((!scroll_partial && (b - t == vc->vc_rows)) 1861 || (scroll_partial 1862 && (b - t - count > 1863 3 * vc->vc_rows >> 2)))) { 1864 if (t > 0) 1865 fbcon_bmove(vc, 0, 0, count, 0, t, 1866 vc->vc_cols); 1867 ypan_up(vc, count); 1868 if (vc->vc_rows - b > 0) 1869 fbcon_bmove(vc, b - count, 0, b, 0, 1870 vc->vc_rows - b, 1871 vc->vc_cols); 1872 } else if (info->flags & FBINFO_READS_FAST) 1873 fbcon_bmove(vc, t + count, 0, t, 0, 1874 b - t - count, vc->vc_cols); 1875 else 1876 goto redraw_up; 1877 __fbcon_clear(vc, b - count, 0, count, vc->vc_cols); 1878 break; 1879 1880 case SCROLL_REDRAW: 1881 redraw_up: 1882 fbcon_redraw(vc, t, b - t - count, 1883 count * vc->vc_cols); 1884 __fbcon_clear(vc, b - count, 0, count, vc->vc_cols); 1885 scr_memsetw((unsigned short *) (vc->vc_origin + 1886 vc->vc_size_row * 1887 (b - count)), 1888 vc->vc_video_erase_char, 1889 vc->vc_size_row * count); 1890 return true; 1891 } 1892 break; 1893 1894 case SM_DOWN: 1895 if (count > vc->vc_rows) /* Maximum realistic size */ 1896 count = vc->vc_rows; 1897 switch (fb_scrollmode(p)) { 1898 case SCROLL_MOVE: 1899 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count, 1900 -count); 1901 __fbcon_clear(vc, t, 0, count, vc->vc_cols); 1902 scr_memsetw((unsigned short *) (vc->vc_origin + 1903 vc->vc_size_row * 1904 t), 1905 vc->vc_video_erase_char, 1906 vc->vc_size_row * count); 1907 return true; 1908 1909 case SCROLL_WRAP_MOVE: 1910 if (b - t - count > 3 * vc->vc_rows >> 2) { 1911 if (vc->vc_rows - b > 0) 1912 fbcon_bmove(vc, b, 0, b - count, 0, 1913 vc->vc_rows - b, 1914 vc->vc_cols); 1915 ywrap_down(vc, count); 1916 if (t > 0) 1917 fbcon_bmove(vc, count, 0, 0, 0, t, 1918 vc->vc_cols); 1919 } else if (info->flags & FBINFO_READS_FAST) 1920 fbcon_bmove(vc, t, 0, t + count, 0, 1921 b - t - count, vc->vc_cols); 1922 else 1923 goto redraw_down; 1924 __fbcon_clear(vc, t, 0, count, vc->vc_cols); 1925 break; 1926 1927 case SCROLL_PAN_MOVE: 1928 if ((count - p->yscroll <= p->vrows - vc->vc_rows) 1929 && ((!scroll_partial && (b - t == vc->vc_rows)) 1930 || (scroll_partial 1931 && (b - t - count > 1932 3 * vc->vc_rows >> 2)))) { 1933 if (vc->vc_rows - b > 0) 1934 fbcon_bmove(vc, b, 0, b - count, 0, 1935 vc->vc_rows - b, 1936 vc->vc_cols); 1937 ypan_down(vc, count); 1938 if (t > 0) 1939 fbcon_bmove(vc, count, 0, 0, 0, t, 1940 vc->vc_cols); 1941 } else if (info->flags & FBINFO_READS_FAST) 1942 fbcon_bmove(vc, t, 0, t + count, 0, 1943 b - t - count, vc->vc_cols); 1944 else 1945 goto redraw_down; 1946 __fbcon_clear(vc, t, 0, count, vc->vc_cols); 1947 break; 1948 1949 case SCROLL_PAN_REDRAW: 1950 if ((count - p->yscroll <= p->vrows - vc->vc_rows) 1951 && ((!scroll_partial && (b - t == vc->vc_rows)) 1952 || (scroll_partial 1953 && (b - t - count > 1954 3 * vc->vc_rows >> 2)))) { 1955 if (vc->vc_rows - b > 0) 1956 fbcon_redraw_move(vc, p, b, vc->vc_rows - b, 1957 b - count); 1958 ypan_down_redraw(vc, t, count); 1959 if (t > 0) 1960 fbcon_redraw_move(vc, p, count, t, 0); 1961 } else 1962 fbcon_redraw_move(vc, p, t, b - t - count, t + count); 1963 __fbcon_clear(vc, t, 0, count, vc->vc_cols); 1964 break; 1965 1966 case SCROLL_REDRAW: 1967 redraw_down: 1968 fbcon_redraw(vc, b - 1, b - t - count, 1969 -count * vc->vc_cols); 1970 __fbcon_clear(vc, t, 0, count, vc->vc_cols); 1971 scr_memsetw((unsigned short *) (vc->vc_origin + 1972 vc->vc_size_row * 1973 t), 1974 vc->vc_video_erase_char, 1975 vc->vc_size_row * count); 1976 return true; 1977 } 1978 } 1979 return false; 1980 } 1981 1982 1983 static void updatescrollmode_accel(struct fbcon_display *p, 1984 struct fb_info *info, 1985 struct vc_data *vc) 1986 { 1987 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_LEGACY_ACCELERATION 1988 struct fbcon_par *par = info->fbcon_par; 1989 int cap = info->flags; 1990 u16 t = 0; 1991 int ypan = FBCON_SWAP(par->rotate, info->fix.ypanstep, info->fix.xpanstep); 1992 int ywrap = FBCON_SWAP(par->rotate, info->fix.ywrapstep, t); 1993 int yres = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 1994 int vyres = FBCON_SWAP(par->rotate, info->var.yres_virtual, info->var.xres_virtual); 1995 int good_pan = (cap & FBINFO_HWACCEL_YPAN) && 1996 divides(ypan, vc->vc_font.height) && vyres > yres; 1997 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) && 1998 divides(ywrap, vc->vc_font.height) && 1999 divides(vc->vc_font.height, vyres) && 2000 divides(vc->vc_font.height, yres); 2001 int reading_fast = cap & FBINFO_READS_FAST; 2002 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) && 2003 !(cap & FBINFO_HWACCEL_DISABLED); 2004 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) && 2005 !(cap & FBINFO_HWACCEL_DISABLED); 2006 2007 if (good_wrap || good_pan) { 2008 if (reading_fast || fast_copyarea) 2009 p->scrollmode = good_wrap ? 2010 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE; 2011 else 2012 p->scrollmode = good_wrap ? SCROLL_REDRAW : 2013 SCROLL_PAN_REDRAW; 2014 } else { 2015 if (reading_fast || (fast_copyarea && !fast_imageblit)) 2016 p->scrollmode = SCROLL_MOVE; 2017 else 2018 p->scrollmode = SCROLL_REDRAW; 2019 } 2020 #endif 2021 } 2022 2023 static void updatescrollmode(struct fbcon_display *p, 2024 struct fb_info *info, 2025 struct vc_data *vc) 2026 { 2027 struct fbcon_par *par = info->fbcon_par; 2028 int fh = vc->vc_font.height; 2029 int yres = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 2030 int vyres = FBCON_SWAP(par->rotate, info->var.yres_virtual, info->var.xres_virtual); 2031 2032 p->vrows = vyres/fh; 2033 if (yres > (fh * (vc->vc_rows + 1))) 2034 p->vrows -= (yres - (fh * vc->vc_rows)) / fh; 2035 if ((yres % fh) && (vyres % fh < yres % fh)) 2036 p->vrows--; 2037 2038 /* update scrollmode in case hardware acceleration is used */ 2039 updatescrollmode_accel(p, info, vc); 2040 } 2041 2042 #define PITCH(w) (((w) + 7) >> 3) 2043 #define CALC_FONTSZ(h, p, c) ((h) * (p) * (c)) /* size = height * pitch * charcount */ 2044 2045 static int fbcon_resize(struct vc_data *vc, unsigned int width, 2046 unsigned int height, bool from_user) 2047 { 2048 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 2049 struct fbcon_par *par = info->fbcon_par; 2050 struct fbcon_display *p = &fb_display[vc->vc_num]; 2051 struct fb_var_screeninfo var = info->var; 2052 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh; 2053 2054 if (p->userfont && FNTSIZE(vc->vc_font.data)) { 2055 int size; 2056 int pitch = PITCH(vc->vc_font.width); 2057 2058 /* 2059 * If user font, ensure that a possible change to user font 2060 * height or width will not allow a font data out-of-bounds access. 2061 * NOTE: must use original charcount in calculation as font 2062 * charcount can change and cannot be used to determine the 2063 * font data allocated size. 2064 */ 2065 if (pitch <= 0) 2066 return -EINVAL; 2067 size = CALC_FONTSZ(vc->vc_font.height, pitch, vc->vc_font.charcount); 2068 if (size > FNTSIZE(vc->vc_font.data)) 2069 return -EINVAL; 2070 } 2071 2072 virt_w = FBCON_SWAP(par->rotate, width, height); 2073 virt_h = FBCON_SWAP(par->rotate, height, width); 2074 virt_fw = FBCON_SWAP(par->rotate, vc->vc_font.width, vc->vc_font.height); 2075 virt_fh = FBCON_SWAP(par->rotate, vc->vc_font.height, vc->vc_font.width); 2076 var.xres = virt_w * virt_fw; 2077 var.yres = virt_h * virt_fh; 2078 x_diff = info->var.xres - var.xres; 2079 y_diff = info->var.yres - var.yres; 2080 if (x_diff < 0 || x_diff > virt_fw || 2081 y_diff < 0 || y_diff > virt_fh) { 2082 const struct fb_videomode *mode; 2083 2084 pr_debug("attempting resize %ix%i\n", var.xres, var.yres); 2085 mode = fb_find_best_mode(&var, &info->modelist); 2086 if (mode == NULL) 2087 return -EINVAL; 2088 display_to_var(&var, p); 2089 fb_videomode_to_var(&var, mode); 2090 2091 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh) 2092 return -EINVAL; 2093 2094 pr_debug("resize now %ix%i\n", var.xres, var.yres); 2095 if (con_is_visible(vc) && vc->vc_mode == KD_TEXT) { 2096 var.activate = FB_ACTIVATE_NOW | 2097 FB_ACTIVATE_FORCE; 2098 fb_set_var(info, &var); 2099 } 2100 var_to_display(p, &info->var, info); 2101 par->var = info->var; 2102 } 2103 updatescrollmode(p, info, vc); 2104 return 0; 2105 } 2106 2107 static bool fbcon_switch(struct vc_data *vc) 2108 { 2109 struct fb_info *info, *old_info = NULL; 2110 struct fbcon_par *par; 2111 struct fbcon_display *p = &fb_display[vc->vc_num]; 2112 struct fb_var_screeninfo var; 2113 int i, ret, prev_console; 2114 2115 info = fbcon_info_from_console(vc->vc_num); 2116 par = info->fbcon_par; 2117 2118 if (logo_shown >= 0) { 2119 struct vc_data *conp2 = vc_cons[logo_shown].d; 2120 2121 if (conp2->vc_top == logo_lines 2122 && conp2->vc_bottom == conp2->vc_rows) 2123 conp2->vc_top = 0; 2124 logo_shown = FBCON_LOGO_CANSHOW; 2125 } 2126 2127 prev_console = par->currcon; 2128 if (prev_console != -1) 2129 old_info = fbcon_info_from_console(prev_console); 2130 /* 2131 * FIXME: If we have multiple fbdev's loaded, we need to 2132 * update all info->currcon. Perhaps, we can place this 2133 * in a centralized structure, but this might break some 2134 * drivers. 2135 * 2136 * info->currcon = vc->vc_num; 2137 */ 2138 fbcon_for_each_registered_fb(i) { 2139 if (fbcon_registered_fb[i]->fbcon_par) { 2140 struct fbcon_par *par = fbcon_registered_fb[i]->fbcon_par; 2141 2142 par->currcon = vc->vc_num; 2143 } 2144 } 2145 memset(&var, 0, sizeof(struct fb_var_screeninfo)); 2146 display_to_var(&var, p); 2147 var.activate = FB_ACTIVATE_NOW; 2148 2149 /* 2150 * make sure we don't unnecessarily trip the memcmp() 2151 * in fb_set_var() 2152 */ 2153 info->var.activate = var.activate; 2154 var.vmode |= info->var.vmode & ~FB_VMODE_MASK; 2155 fb_set_var(info, &var); 2156 par->var = info->var; 2157 2158 if (old_info != NULL && (old_info != info || 2159 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) { 2160 if (info->fbops->fb_set_par) { 2161 ret = info->fbops->fb_set_par(info); 2162 2163 if (ret) 2164 printk(KERN_ERR "fbcon_switch: detected " 2165 "unhandled fb_set_par error, " 2166 "error code %d\n", ret); 2167 } 2168 2169 if (old_info != info) 2170 fbcon_del_cursor_work(old_info); 2171 } 2172 2173 if (!fbcon_is_active(vc, info) || par->blank_state != FB_BLANK_UNBLANK) 2174 fbcon_del_cursor_work(info); 2175 else 2176 fbcon_add_cursor_work(info); 2177 2178 set_blitting_type(vc, info); 2179 par->cursor_reset = 1; 2180 2181 if (par->bitops->rotate_font && par->bitops->rotate_font(info, vc)) { 2182 par->rotate = FB_ROTATE_UR; 2183 set_blitting_type(vc, info); 2184 } 2185 2186 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1); 2187 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; 2188 2189 if (vc->vc_font.charcount > 256) 2190 vc->vc_complement_mask <<= 1; 2191 2192 updatescrollmode(p, info, vc); 2193 2194 switch (fb_scrollmode(p)) { 2195 case SCROLL_WRAP_MOVE: 2196 scrollback_phys_max = p->vrows - vc->vc_rows; 2197 break; 2198 case SCROLL_PAN_MOVE: 2199 case SCROLL_PAN_REDRAW: 2200 scrollback_phys_max = p->vrows - 2 * vc->vc_rows; 2201 if (scrollback_phys_max < 0) 2202 scrollback_phys_max = 0; 2203 break; 2204 default: 2205 scrollback_phys_max = 0; 2206 break; 2207 } 2208 2209 scrollback_max = 0; 2210 scrollback_current = 0; 2211 2212 if (fbcon_is_active(vc, info)) { 2213 par->var.xoffset = par->var.yoffset = p->yscroll = 0; 2214 par->bitops->update_start(info); 2215 } 2216 2217 fbcon_set_palette(vc, color_table); 2218 fbcon_clear_margins(vc, 0); 2219 2220 if (logo_shown == FBCON_LOGO_DRAW) { 2221 2222 logo_shown = fg_console; 2223 fb_show_logo(info, par->rotate); 2224 update_region(vc, 2225 vc->vc_origin + vc->vc_size_row * vc->vc_top, 2226 vc->vc_size_row * (vc->vc_bottom - 2227 vc->vc_top) / 2); 2228 return false; 2229 } 2230 return true; 2231 } 2232 2233 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info, 2234 int blank) 2235 { 2236 if (blank) { 2237 unsigned short charmask = vc->vc_hi_font_mask ? 2238 0x1ff : 0xff; 2239 unsigned short oldc; 2240 2241 oldc = vc->vc_video_erase_char; 2242 vc->vc_video_erase_char &= charmask; 2243 __fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols); 2244 vc->vc_video_erase_char = oldc; 2245 } 2246 } 2247 2248 static bool fbcon_blank(struct vc_data *vc, enum vesa_blank_mode blank, 2249 bool mode_switch) 2250 { 2251 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 2252 struct fbcon_par *par = info->fbcon_par; 2253 2254 if (mode_switch) { 2255 struct fb_var_screeninfo var = info->var; 2256 2257 par->graphics = 1; 2258 2259 if (!blank) { 2260 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE | 2261 FB_ACTIVATE_KD_TEXT; 2262 fb_set_var(info, &var); 2263 par->graphics = 0; 2264 par->var = info->var; 2265 } 2266 } 2267 2268 if (fbcon_is_active(vc, info)) { 2269 if (par->blank_state != blank) { 2270 par->blank_state = blank; 2271 fbcon_cursor(vc, !blank); 2272 par->cursor_flash = (!blank); 2273 2274 if (fb_blank(info, blank)) 2275 fbcon_generic_blank(vc, info, blank); 2276 } 2277 2278 if (!blank) 2279 update_screen(vc); 2280 } 2281 2282 if (mode_switch || !fbcon_is_active(vc, info) || par->blank_state != FB_BLANK_UNBLANK) 2283 fbcon_del_cursor_work(info); 2284 else 2285 fbcon_add_cursor_work(info); 2286 2287 return false; 2288 } 2289 2290 static int fbcon_get_font(struct vc_data *vc, struct console_font *font, unsigned int vpitch) 2291 { 2292 u8 *fontdata = vc->vc_font.data; 2293 u8 *data = font->data; 2294 int i, j; 2295 2296 font->width = vc->vc_font.width; 2297 font->height = vc->vc_font.height; 2298 if (font->height > vpitch) 2299 return -ENOSPC; 2300 font->charcount = vc->vc_hi_font_mask ? 512 : 256; 2301 if (!font->data) 2302 return 0; 2303 2304 if (font->width <= 8) { 2305 j = vc->vc_font.height; 2306 if (font->charcount * j > FNTSIZE(fontdata)) 2307 return -EINVAL; 2308 2309 for (i = 0; i < font->charcount; i++) { 2310 memcpy(data, fontdata, j); 2311 memset(data + j, 0, vpitch - j); 2312 data += vpitch; 2313 fontdata += j; 2314 } 2315 } else if (font->width <= 16) { 2316 j = vc->vc_font.height * 2; 2317 if (font->charcount * j > FNTSIZE(fontdata)) 2318 return -EINVAL; 2319 2320 for (i = 0; i < font->charcount; i++) { 2321 memcpy(data, fontdata, j); 2322 memset(data + j, 0, 2*vpitch - j); 2323 data += 2*vpitch; 2324 fontdata += j; 2325 } 2326 } else if (font->width <= 24) { 2327 if (font->charcount * (vc->vc_font.height * sizeof(u32)) > FNTSIZE(fontdata)) 2328 return -EINVAL; 2329 2330 for (i = 0; i < font->charcount; i++) { 2331 for (j = 0; j < vc->vc_font.height; j++) { 2332 *data++ = fontdata[0]; 2333 *data++ = fontdata[1]; 2334 *data++ = fontdata[2]; 2335 fontdata += sizeof(u32); 2336 } 2337 memset(data, 0, 3 * (vpitch - j)); 2338 data += 3 * (vpitch - j); 2339 } 2340 } else { 2341 j = vc->vc_font.height * 4; 2342 if (font->charcount * j > FNTSIZE(fontdata)) 2343 return -EINVAL; 2344 2345 for (i = 0; i < font->charcount; i++) { 2346 memcpy(data, fontdata, j); 2347 memset(data + j, 0, 4 * vpitch - j); 2348 data += 4 * vpitch; 2349 fontdata += j; 2350 } 2351 } 2352 return 0; 2353 } 2354 2355 /* set/clear vc_hi_font_mask and update vc attrs accordingly */ 2356 static void set_vc_hi_font(struct vc_data *vc, bool set) 2357 { 2358 if (!set) { 2359 vc->vc_hi_font_mask = 0; 2360 if (vc->vc_can_do_color) { 2361 vc->vc_complement_mask >>= 1; 2362 vc->vc_s_complement_mask >>= 1; 2363 } 2364 2365 /* ++Edmund: reorder the attribute bits */ 2366 if (vc->vc_can_do_color) { 2367 unsigned short *cp = 2368 (unsigned short *) vc->vc_origin; 2369 int count = vc->vc_screenbuf_size / 2; 2370 unsigned short c; 2371 for (; count > 0; count--, cp++) { 2372 c = scr_readw(cp); 2373 scr_writew(((c & 0xfe00) >> 1) | 2374 (c & 0xff), cp); 2375 } 2376 c = vc->vc_video_erase_char; 2377 vc->vc_video_erase_char = 2378 ((c & 0xfe00) >> 1) | (c & 0xff); 2379 vc->vc_attr >>= 1; 2380 } 2381 } else { 2382 vc->vc_hi_font_mask = 0x100; 2383 if (vc->vc_can_do_color) { 2384 vc->vc_complement_mask <<= 1; 2385 vc->vc_s_complement_mask <<= 1; 2386 } 2387 2388 /* ++Edmund: reorder the attribute bits */ 2389 { 2390 unsigned short *cp = 2391 (unsigned short *) vc->vc_origin; 2392 int count = vc->vc_screenbuf_size / 2; 2393 unsigned short c; 2394 for (; count > 0; count--, cp++) { 2395 unsigned short newc; 2396 c = scr_readw(cp); 2397 if (vc->vc_can_do_color) 2398 newc = 2399 ((c & 0xff00) << 1) | (c & 2400 0xff); 2401 else 2402 newc = c & ~0x100; 2403 scr_writew(newc, cp); 2404 } 2405 c = vc->vc_video_erase_char; 2406 if (vc->vc_can_do_color) { 2407 vc->vc_video_erase_char = 2408 ((c & 0xff00) << 1) | (c & 0xff); 2409 vc->vc_attr <<= 1; 2410 } else 2411 vc->vc_video_erase_char = c & ~0x100; 2412 } 2413 } 2414 } 2415 2416 static int fbcon_do_set_font(struct vc_data *vc, int w, int h, int charcount, 2417 const u8 * data, int userfont) 2418 { 2419 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 2420 struct fbcon_par *par = info->fbcon_par; 2421 struct fbcon_display *p = &fb_display[vc->vc_num]; 2422 int resize, ret, old_userfont, old_width, old_height, old_charcount; 2423 u8 *old_data = vc->vc_font.data; 2424 2425 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height); 2426 vc->vc_font.data = (void *)(p->fontdata = data); 2427 old_userfont = p->userfont; 2428 if ((p->userfont = userfont)) 2429 REFCOUNT(data)++; 2430 2431 old_width = vc->vc_font.width; 2432 old_height = vc->vc_font.height; 2433 old_charcount = vc->vc_font.charcount; 2434 2435 vc->vc_font.width = w; 2436 vc->vc_font.height = h; 2437 vc->vc_font.charcount = charcount; 2438 if (vc->vc_hi_font_mask && charcount == 256) 2439 set_vc_hi_font(vc, false); 2440 else if (!vc->vc_hi_font_mask && charcount == 512) 2441 set_vc_hi_font(vc, true); 2442 2443 if (resize) { 2444 int cols, rows; 2445 2446 cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres); 2447 rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 2448 cols /= w; 2449 rows /= h; 2450 ret = vc_resize(vc, cols, rows); 2451 if (ret) 2452 goto err_out; 2453 } else if (con_is_visible(vc) 2454 && vc->vc_mode == KD_TEXT) { 2455 fbcon_clear_margins(vc, 0); 2456 update_screen(vc); 2457 } 2458 2459 if (old_userfont && (--REFCOUNT(old_data) == 0)) 2460 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int)); 2461 return 0; 2462 2463 err_out: 2464 p->fontdata = old_data; 2465 vc->vc_font.data = old_data; 2466 2467 if (userfont) { 2468 p->userfont = old_userfont; 2469 if (--REFCOUNT(data) == 0) 2470 kfree(data - FONT_EXTRA_WORDS * sizeof(int)); 2471 } 2472 2473 vc->vc_font.width = old_width; 2474 vc->vc_font.height = old_height; 2475 vc->vc_font.charcount = old_charcount; 2476 2477 return ret; 2478 } 2479 2480 /* 2481 * User asked to set font; we are guaranteed that charcount does not exceed 512 2482 * but lets not assume that, since charcount of 512 is small for unicode support. 2483 */ 2484 2485 static int fbcon_set_font(struct vc_data *vc, const struct console_font *font, 2486 unsigned int vpitch, unsigned int flags) 2487 { 2488 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 2489 unsigned charcount = font->charcount; 2490 int w = font->width; 2491 int h = font->height; 2492 int size, alloc_size; 2493 int i, csum; 2494 u8 *new_data, *data = font->data; 2495 int pitch = PITCH(font->width); 2496 2497 /* Is there a reason why fbconsole couldn't handle any charcount >256? 2498 * If not this check should be changed to charcount < 256 */ 2499 if (charcount != 256 && charcount != 512) 2500 return -EINVAL; 2501 2502 /* font bigger than screen resolution ? */ 2503 if (w > FBCON_SWAP(info->var.rotate, info->var.xres, info->var.yres) || 2504 h > FBCON_SWAP(info->var.rotate, info->var.yres, info->var.xres)) 2505 return -EINVAL; 2506 2507 if (font->width > FB_MAX_BLIT_WIDTH || font->height > FB_MAX_BLIT_HEIGHT) 2508 return -EINVAL; 2509 2510 /* Make sure drawing engine can handle the font */ 2511 if (!test_bit(font->width - 1, info->pixmap.blit_x) || 2512 !test_bit(font->height - 1, info->pixmap.blit_y)) 2513 return -EINVAL; 2514 2515 /* Make sure driver can handle the font length */ 2516 if (fbcon_invalid_charcount(info, charcount)) 2517 return -EINVAL; 2518 2519 /* Check for integer overflow in font size calculation */ 2520 if (check_mul_overflow(h, pitch, &size) || 2521 check_mul_overflow(size, charcount, &size)) 2522 return -EINVAL; 2523 2524 /* Check for overflow in allocation size calculation */ 2525 if (check_add_overflow(FONT_EXTRA_WORDS * sizeof(int), size, &alloc_size)) 2526 return -EINVAL; 2527 2528 new_data = kmalloc(alloc_size, GFP_USER); 2529 2530 if (!new_data) 2531 return -ENOMEM; 2532 2533 memset(new_data, 0, FONT_EXTRA_WORDS * sizeof(int)); 2534 2535 new_data += FONT_EXTRA_WORDS * sizeof(int); 2536 FNTSIZE(new_data) = size; 2537 REFCOUNT(new_data) = 0; /* usage counter */ 2538 for (i=0; i< charcount; i++) { 2539 memcpy(new_data + i*h*pitch, data + i*vpitch*pitch, h*pitch); 2540 } 2541 2542 /* Since linux has a nice crc32 function use it for counting font 2543 * checksums. */ 2544 csum = crc32(0, new_data, size); 2545 2546 FNTSUM(new_data) = csum; 2547 /* Check if the same font is on some other console already */ 2548 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2549 struct vc_data *tmp = vc_cons[i].d; 2550 2551 if (fb_display[i].userfont && 2552 fb_display[i].fontdata && 2553 FNTSUM(fb_display[i].fontdata) == csum && 2554 FNTSIZE(fb_display[i].fontdata) == size && 2555 tmp->vc_font.width == w && 2556 !memcmp(fb_display[i].fontdata, new_data, size)) { 2557 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int)); 2558 new_data = (u8 *)fb_display[i].fontdata; 2559 break; 2560 } 2561 } 2562 return fbcon_do_set_font(vc, font->width, font->height, charcount, new_data, 1); 2563 } 2564 2565 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, 2566 const char *name) 2567 { 2568 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 2569 const struct font_desc *f; 2570 2571 if (!name) 2572 f = get_default_font(info->var.xres, info->var.yres, 2573 info->pixmap.blit_x, info->pixmap.blit_y); 2574 else if (!(f = find_font(name))) 2575 return -ENOENT; 2576 2577 font->width = f->width; 2578 font->height = f->height; 2579 return fbcon_do_set_font(vc, f->width, f->height, f->charcount, f->data, 0); 2580 } 2581 2582 static u16 palette_red[16]; 2583 static u16 palette_green[16]; 2584 static u16 palette_blue[16]; 2585 2586 static struct fb_cmap palette_cmap = { 2587 0, 16, palette_red, palette_green, palette_blue, NULL 2588 }; 2589 2590 static void fbcon_set_palette(struct vc_data *vc, const unsigned char *table) 2591 { 2592 struct fb_info *info = fbcon_info_from_console(vc->vc_num); 2593 int i, j, k, depth; 2594 u8 val; 2595 2596 if (!fbcon_is_active(vc, info)) 2597 return; 2598 2599 if (!con_is_visible(vc)) 2600 return; 2601 2602 depth = fb_get_color_depth(&info->var, &info->fix); 2603 if (depth > 3) { 2604 for (i = j = 0; i < 16; i++) { 2605 k = table[i]; 2606 val = vc->vc_palette[j++]; 2607 palette_red[k] = (val << 8) | val; 2608 val = vc->vc_palette[j++]; 2609 palette_green[k] = (val << 8) | val; 2610 val = vc->vc_palette[j++]; 2611 palette_blue[k] = (val << 8) | val; 2612 } 2613 palette_cmap.len = 16; 2614 palette_cmap.start = 0; 2615 /* 2616 * If framebuffer is capable of less than 16 colors, 2617 * use default palette of fbcon. 2618 */ 2619 } else 2620 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap); 2621 2622 fb_set_cmap(&palette_cmap, info); 2623 } 2624 2625 /* As we might be inside of softback, we may work with non-contiguous buffer, 2626 that's why we have to use a separate routine. */ 2627 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt) 2628 { 2629 while (cnt--) { 2630 u16 a = scr_readw(p); 2631 if (!vc->vc_can_do_color) 2632 a ^= 0x0800; 2633 else if (vc->vc_hi_font_mask == 0x100) 2634 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | 2635 (((a) & 0x0e00) << 4); 2636 else 2637 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | 2638 (((a) & 0x0700) << 4); 2639 scr_writew(a, p++); 2640 } 2641 } 2642 2643 void fbcon_suspended(struct fb_info *info) 2644 { 2645 struct vc_data *vc = NULL; 2646 struct fbcon_par *par = info->fbcon_par; 2647 2648 if (!par || par->currcon < 0) 2649 return; 2650 vc = vc_cons[par->currcon].d; 2651 2652 /* Clear cursor, restore saved data */ 2653 fbcon_cursor(vc, false); 2654 } 2655 2656 void fbcon_resumed(struct fb_info *info) 2657 { 2658 struct vc_data *vc; 2659 struct fbcon_par *par = info->fbcon_par; 2660 2661 if (!par || par->currcon < 0) 2662 return; 2663 vc = vc_cons[par->currcon].d; 2664 2665 update_screen(vc); 2666 } 2667 2668 static void fbcon_modechanged(struct fb_info *info) 2669 { 2670 struct fbcon_par *par = info->fbcon_par; 2671 struct vc_data *vc; 2672 struct fbcon_display *p; 2673 int rows, cols; 2674 2675 if (!par || par->currcon < 0) 2676 return; 2677 vc = vc_cons[par->currcon].d; 2678 if (vc->vc_mode != KD_TEXT || 2679 fbcon_info_from_console(par->currcon) != info) 2680 return; 2681 2682 p = &fb_display[vc->vc_num]; 2683 set_blitting_type(vc, info); 2684 2685 if (con_is_visible(vc)) { 2686 var_to_display(p, &info->var, info); 2687 cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres); 2688 rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 2689 cols /= vc->vc_font.width; 2690 rows /= vc->vc_font.height; 2691 vc_resize(vc, cols, rows); 2692 updatescrollmode(p, info, vc); 2693 scrollback_max = 0; 2694 scrollback_current = 0; 2695 2696 if (fbcon_is_active(vc, info)) { 2697 par->var.xoffset = par->var.yoffset = p->yscroll = 0; 2698 par->bitops->update_start(info); 2699 } 2700 2701 fbcon_set_palette(vc, color_table); 2702 update_screen(vc); 2703 } 2704 } 2705 2706 static void fbcon_set_all_vcs(struct fb_info *info) 2707 { 2708 struct fbcon_par *par = info->fbcon_par; 2709 struct vc_data *vc; 2710 struct fbcon_display *p; 2711 int i, rows, cols, fg = -1; 2712 2713 if (!par || par->currcon < 0) 2714 return; 2715 2716 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2717 vc = vc_cons[i].d; 2718 if (!vc || vc->vc_mode != KD_TEXT || 2719 fbcon_info_from_console(i) != info) 2720 continue; 2721 2722 if (con_is_visible(vc)) { 2723 fg = i; 2724 continue; 2725 } 2726 2727 p = &fb_display[vc->vc_num]; 2728 set_blitting_type(vc, info); 2729 var_to_display(p, &info->var, info); 2730 cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres); 2731 rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres); 2732 cols /= vc->vc_font.width; 2733 rows /= vc->vc_font.height; 2734 vc_resize(vc, cols, rows); 2735 } 2736 2737 if (fg != -1) 2738 fbcon_modechanged(info); 2739 } 2740 2741 2742 void fbcon_update_vcs(struct fb_info *info, bool all) 2743 { 2744 if (all) 2745 fbcon_set_all_vcs(info); 2746 else 2747 fbcon_modechanged(info); 2748 } 2749 EXPORT_SYMBOL(fbcon_update_vcs); 2750 2751 /* let fbcon check if it supports a new screen resolution */ 2752 int fbcon_modechange_possible(struct fb_info *info, struct fb_var_screeninfo *var) 2753 { 2754 struct fbcon_par *par = info->fbcon_par; 2755 struct vc_data *vc; 2756 unsigned int i; 2757 2758 WARN_CONSOLE_UNLOCKED(); 2759 2760 if (!par) 2761 return 0; 2762 2763 /* prevent setting a screen size which is smaller than font size */ 2764 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2765 vc = vc_cons[i].d; 2766 if (!vc || vc->vc_mode != KD_TEXT || 2767 fbcon_info_from_console(i) != info) 2768 continue; 2769 2770 if (vc->vc_font.width > FBCON_SWAP(var->rotate, var->xres, var->yres) || 2771 vc->vc_font.height > FBCON_SWAP(var->rotate, var->yres, var->xres)) 2772 return -EINVAL; 2773 } 2774 2775 return 0; 2776 } 2777 EXPORT_SYMBOL_GPL(fbcon_modechange_possible); 2778 2779 int fbcon_mode_deleted(struct fb_info *info, 2780 struct fb_videomode *mode) 2781 { 2782 struct fb_info *fb_info; 2783 struct fbcon_display *p; 2784 int i, j, found = 0; 2785 2786 /* before deletion, ensure that mode is not in use */ 2787 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2788 j = con2fb_map[i]; 2789 if (j == -1) 2790 continue; 2791 fb_info = fbcon_registered_fb[j]; 2792 if (fb_info != info) 2793 continue; 2794 p = &fb_display[i]; 2795 if (!p || !p->mode) 2796 continue; 2797 if (fb_mode_is_equal(p->mode, mode)) { 2798 found = 1; 2799 break; 2800 } 2801 } 2802 return found; 2803 } 2804 2805 #ifdef CONFIG_VT_HW_CONSOLE_BINDING 2806 static void fbcon_unbind(void) 2807 { 2808 int ret; 2809 2810 ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc, 2811 fbcon_is_default); 2812 2813 if (!ret) 2814 fbcon_has_console_bind = false; 2815 } 2816 #else 2817 static inline void fbcon_unbind(void) {} 2818 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */ 2819 2820 void fbcon_fb_unbind(struct fb_info *info) 2821 { 2822 int i, new_idx = -1; 2823 int idx = info->node; 2824 2825 console_lock(); 2826 2827 if (!fbcon_has_console_bind) { 2828 console_unlock(); 2829 return; 2830 } 2831 2832 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2833 if (con2fb_map[i] != idx && 2834 con2fb_map[i] != -1) { 2835 new_idx = con2fb_map[i]; 2836 break; 2837 } 2838 } 2839 2840 if (new_idx != -1) { 2841 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2842 if (con2fb_map[i] == idx) 2843 set_con2fb_map(i, new_idx, 0); 2844 } 2845 } else { 2846 struct fb_info *info = fbcon_registered_fb[idx]; 2847 2848 /* This is sort of like set_con2fb_map, except it maps 2849 * the consoles to no device and then releases the 2850 * oldinfo to free memory and cancel the cursor blink 2851 * timer. I can imagine this just becoming part of 2852 * set_con2fb_map where new_idx is -1 2853 */ 2854 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2855 if (con2fb_map[i] == idx) { 2856 con2fb_map[i] = -1; 2857 if (!search_fb_in_map(idx)) { 2858 con2fb_release_oldinfo(vc_cons[i].d, 2859 info, NULL); 2860 } 2861 } 2862 } 2863 fbcon_unbind(); 2864 } 2865 2866 console_unlock(); 2867 } 2868 2869 void fbcon_fb_unregistered(struct fb_info *info) 2870 { 2871 int i, idx; 2872 2873 console_lock(); 2874 2875 fbcon_registered_fb[info->node] = NULL; 2876 fbcon_num_registered_fb--; 2877 2878 if (deferred_takeover) { 2879 console_unlock(); 2880 return; 2881 } 2882 2883 idx = info->node; 2884 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2885 if (con2fb_map[i] == idx) 2886 con2fb_map[i] = -1; 2887 } 2888 2889 if (idx == info_idx) { 2890 info_idx = -1; 2891 2892 fbcon_for_each_registered_fb(i) { 2893 info_idx = i; 2894 break; 2895 } 2896 } 2897 2898 if (info_idx != -1) { 2899 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2900 if (con2fb_map[i] == -1) 2901 con2fb_map[i] = info_idx; 2902 } 2903 } 2904 2905 if (primary_device == idx) 2906 primary_device = -1; 2907 2908 if (!fbcon_num_registered_fb) 2909 do_unregister_con_driver(&fb_con); 2910 console_unlock(); 2911 } 2912 2913 void fbcon_remap_all(struct fb_info *info) 2914 { 2915 int i, idx = info->node; 2916 2917 console_lock(); 2918 if (deferred_takeover) { 2919 for (i = first_fb_vc; i <= last_fb_vc; i++) 2920 con2fb_map_boot[i] = idx; 2921 fbcon_map_override(); 2922 console_unlock(); 2923 return; 2924 } 2925 2926 for (i = first_fb_vc; i <= last_fb_vc; i++) 2927 set_con2fb_map(i, idx, 0); 2928 2929 if (con_is_bound(&fb_con)) { 2930 printk(KERN_INFO "fbcon: Remapping primary device, " 2931 "fb%i, to tty %i-%i\n", idx, 2932 first_fb_vc + 1, last_fb_vc + 1); 2933 info_idx = idx; 2934 } 2935 console_unlock(); 2936 } 2937 2938 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY 2939 static void fbcon_select_primary(struct fb_info *info) 2940 { 2941 if (!map_override && primary_device == -1 && 2942 video_is_primary_device(info->device)) { 2943 int i; 2944 2945 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n", 2946 info->fix.id, info->node); 2947 primary_device = info->node; 2948 2949 for (i = first_fb_vc; i <= last_fb_vc; i++) 2950 con2fb_map_boot[i] = primary_device; 2951 2952 if (con_is_bound(&fb_con)) { 2953 printk(KERN_INFO "fbcon: Remapping primary device, " 2954 "fb%i, to tty %i-%i\n", info->node, 2955 first_fb_vc + 1, last_fb_vc + 1); 2956 info_idx = primary_device; 2957 } 2958 } 2959 2960 } 2961 #else 2962 static inline void fbcon_select_primary(struct fb_info *info) 2963 { 2964 return; 2965 } 2966 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */ 2967 2968 static bool lockless_register_fb; 2969 module_param_named_unsafe(lockless_register_fb, lockless_register_fb, bool, 0400); 2970 MODULE_PARM_DESC(lockless_register_fb, 2971 "Lockless framebuffer registration for debugging [default=off]"); 2972 2973 /* called with console_lock held */ 2974 static int do_fb_registered(struct fb_info *info) 2975 { 2976 int ret = 0, i, idx; 2977 2978 WARN_CONSOLE_UNLOCKED(); 2979 2980 fbcon_registered_fb[info->node] = info; 2981 fbcon_num_registered_fb++; 2982 2983 idx = info->node; 2984 fbcon_select_primary(info); 2985 2986 if (deferred_takeover) { 2987 pr_info("fbcon: Deferring console take-over\n"); 2988 return 0; 2989 } 2990 2991 if (info_idx == -1) { 2992 for (i = first_fb_vc; i <= last_fb_vc; i++) { 2993 if (con2fb_map_boot[i] == idx) { 2994 info_idx = idx; 2995 break; 2996 } 2997 } 2998 2999 if (info_idx != -1) 3000 ret = do_fbcon_takeover(1); 3001 } else { 3002 for (i = first_fb_vc; i <= last_fb_vc; i++) { 3003 if (con2fb_map_boot[i] == idx) 3004 set_con2fb_map(i, idx, 0); 3005 } 3006 } 3007 3008 return ret; 3009 } 3010 3011 int fbcon_fb_registered(struct fb_info *info) 3012 { 3013 int ret; 3014 3015 if (!lockless_register_fb) 3016 console_lock(); 3017 else 3018 atomic_inc(&ignore_console_lock_warning); 3019 3020 ret = do_fb_registered(info); 3021 3022 if (!lockless_register_fb) 3023 console_unlock(); 3024 else 3025 atomic_dec(&ignore_console_lock_warning); 3026 3027 return ret; 3028 } 3029 3030 void fbcon_fb_blanked(struct fb_info *info, int blank) 3031 { 3032 struct fbcon_par *par = info->fbcon_par; 3033 struct vc_data *vc; 3034 3035 if (!par || par->currcon < 0) 3036 return; 3037 3038 vc = vc_cons[par->currcon].d; 3039 if (vc->vc_mode != KD_TEXT || fbcon_info_from_console(par->currcon) != info) 3040 return; 3041 3042 if (con_is_visible(vc)) { 3043 if (blank) 3044 do_blank_screen(0); 3045 else 3046 do_unblank_screen(0); 3047 } 3048 par->blank_state = blank; 3049 } 3050 3051 void fbcon_new_modelist(struct fb_info *info) 3052 { 3053 int i; 3054 struct vc_data *vc; 3055 struct fb_var_screeninfo var; 3056 const struct fb_videomode *mode; 3057 3058 for (i = first_fb_vc; i <= last_fb_vc; i++) { 3059 if (fbcon_info_from_console(i) != info) 3060 continue; 3061 if (!fb_display[i].mode) 3062 continue; 3063 vc = vc_cons[i].d; 3064 display_to_var(&var, &fb_display[i]); 3065 mode = fb_find_nearest_mode(fb_display[i].mode, 3066 &info->modelist); 3067 fb_videomode_to_var(&var, mode); 3068 fbcon_set_disp(info, &var, vc->vc_num); 3069 } 3070 } 3071 3072 void fbcon_get_requirement(struct fb_info *info, 3073 struct fb_blit_caps *caps) 3074 { 3075 struct vc_data *vc; 3076 3077 if (caps->flags) { 3078 int i, charcnt; 3079 3080 for (i = first_fb_vc; i <= last_fb_vc; i++) { 3081 vc = vc_cons[i].d; 3082 if (vc && vc->vc_mode == KD_TEXT && 3083 info->node == con2fb_map[i]) { 3084 set_bit(vc->vc_font.width - 1, caps->x); 3085 set_bit(vc->vc_font.height - 1, caps->y); 3086 charcnt = vc->vc_font.charcount; 3087 if (caps->len < charcnt) 3088 caps->len = charcnt; 3089 } 3090 } 3091 } else { 3092 vc = vc_cons[fg_console].d; 3093 3094 if (vc && vc->vc_mode == KD_TEXT && 3095 info->node == con2fb_map[fg_console]) { 3096 bitmap_zero(caps->x, FB_MAX_BLIT_WIDTH); 3097 set_bit(vc->vc_font.width - 1, caps->x); 3098 bitmap_zero(caps->y, FB_MAX_BLIT_HEIGHT); 3099 set_bit(vc->vc_font.height - 1, caps->y); 3100 caps->len = vc->vc_font.charcount; 3101 } 3102 } 3103 } 3104 3105 int fbcon_set_con2fb_map_ioctl(void __user *argp) 3106 { 3107 struct fb_con2fbmap con2fb; 3108 int ret; 3109 3110 if (copy_from_user(&con2fb, argp, sizeof(con2fb))) 3111 return -EFAULT; 3112 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES) 3113 return -EINVAL; 3114 if (con2fb.framebuffer >= FB_MAX) 3115 return -EINVAL; 3116 if (!fbcon_registered_fb[con2fb.framebuffer]) 3117 request_module("fb%d", con2fb.framebuffer); 3118 if (!fbcon_registered_fb[con2fb.framebuffer]) { 3119 return -EINVAL; 3120 } 3121 3122 console_lock(); 3123 ret = set_con2fb_map(con2fb.console - 1, 3124 con2fb.framebuffer, 1); 3125 console_unlock(); 3126 3127 return ret; 3128 } 3129 3130 int fbcon_get_con2fb_map_ioctl(void __user *argp) 3131 { 3132 struct fb_con2fbmap con2fb; 3133 3134 if (copy_from_user(&con2fb, argp, sizeof(con2fb))) 3135 return -EFAULT; 3136 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES) 3137 return -EINVAL; 3138 3139 console_lock(); 3140 con2fb.framebuffer = con2fb_map[con2fb.console - 1]; 3141 console_unlock(); 3142 3143 return copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0; 3144 } 3145 3146 /* 3147 * The console `switch' structure for the frame buffer based console 3148 */ 3149 3150 static const struct consw fb_con = { 3151 .owner = THIS_MODULE, 3152 .con_startup = fbcon_startup, 3153 .con_init = fbcon_init, 3154 .con_deinit = fbcon_deinit, 3155 .con_clear = fbcon_clear, 3156 .con_putcs = fbcon_putcs, 3157 .con_cursor = fbcon_cursor, 3158 .con_scroll = fbcon_scroll, 3159 .con_switch = fbcon_switch, 3160 .con_blank = fbcon_blank, 3161 .con_font_set = fbcon_set_font, 3162 .con_font_get = fbcon_get_font, 3163 .con_font_default = fbcon_set_def_font, 3164 .con_set_palette = fbcon_set_palette, 3165 .con_invert_region = fbcon_invert_region, 3166 .con_resize = fbcon_resize, 3167 }; 3168 3169 static ssize_t rotate_store(struct device *device, 3170 struct device_attribute *attr, const char *buf, 3171 size_t count) 3172 { 3173 struct fb_info *info; 3174 int rotate, idx; 3175 char **last = NULL; 3176 3177 console_lock(); 3178 idx = con2fb_map[fg_console]; 3179 3180 if (idx == -1 || fbcon_registered_fb[idx] == NULL) 3181 goto err; 3182 3183 info = fbcon_registered_fb[idx]; 3184 rotate = simple_strtoul(buf, last, 0); 3185 fbcon_rotate(info, rotate); 3186 err: 3187 console_unlock(); 3188 return count; 3189 } 3190 3191 static ssize_t rotate_all_store(struct device *device, 3192 struct device_attribute *attr,const char *buf, 3193 size_t count) 3194 { 3195 struct fb_info *info; 3196 int rotate, idx; 3197 char **last = NULL; 3198 3199 console_lock(); 3200 idx = con2fb_map[fg_console]; 3201 3202 if (idx == -1 || fbcon_registered_fb[idx] == NULL) 3203 goto err; 3204 3205 info = fbcon_registered_fb[idx]; 3206 rotate = simple_strtoul(buf, last, 0); 3207 fbcon_rotate_all(info, rotate); 3208 err: 3209 console_unlock(); 3210 return count; 3211 } 3212 3213 static ssize_t rotate_show(struct device *device, 3214 struct device_attribute *attr,char *buf) 3215 { 3216 struct fb_info *info; 3217 int rotate = 0, idx; 3218 3219 console_lock(); 3220 idx = con2fb_map[fg_console]; 3221 3222 if (idx == -1 || fbcon_registered_fb[idx] == NULL) 3223 goto err; 3224 3225 info = fbcon_registered_fb[idx]; 3226 rotate = fbcon_get_rotate(info); 3227 err: 3228 console_unlock(); 3229 return sysfs_emit(buf, "%d\n", rotate); 3230 } 3231 3232 static ssize_t cursor_blink_show(struct device *device, 3233 struct device_attribute *attr, char *buf) 3234 { 3235 struct fb_info *info; 3236 struct fbcon_par *par; 3237 int idx, blink = -1; 3238 3239 console_lock(); 3240 idx = con2fb_map[fg_console]; 3241 3242 if (idx == -1 || fbcon_registered_fb[idx] == NULL) 3243 goto err; 3244 3245 info = fbcon_registered_fb[idx]; 3246 par = info->fbcon_par; 3247 3248 if (!par) 3249 goto err; 3250 3251 blink = delayed_work_pending(&par->cursor_work); 3252 err: 3253 console_unlock(); 3254 return sysfs_emit(buf, "%d\n", blink); 3255 } 3256 3257 static ssize_t cursor_blink_store(struct device *device, 3258 struct device_attribute *attr, 3259 const char *buf, size_t count) 3260 { 3261 struct fb_info *info; 3262 char **last = NULL; 3263 bool blink; 3264 int idx; 3265 3266 console_lock(); 3267 idx = con2fb_map[fg_console]; 3268 3269 if (idx == -1 || fbcon_registered_fb[idx] == NULL) 3270 goto err; 3271 3272 info = fbcon_registered_fb[idx]; 3273 3274 if (!info->fbcon_par) 3275 goto err; 3276 3277 blink = simple_strtoul(buf, last, 0); 3278 3279 if (blink) { 3280 fbcon_cursor_blink = true; 3281 fbcon_add_cursor_work(info); 3282 } else { 3283 fbcon_cursor_blink = false; 3284 fbcon_del_cursor_work(info); 3285 } 3286 3287 err: 3288 console_unlock(); 3289 return count; 3290 } 3291 3292 static DEVICE_ATTR_RW(cursor_blink); 3293 static DEVICE_ATTR_RW(rotate); 3294 static DEVICE_ATTR_WO(rotate_all); 3295 3296 static struct attribute *fbcon_device_attrs[] = { 3297 &dev_attr_cursor_blink.attr, 3298 &dev_attr_rotate.attr, 3299 &dev_attr_rotate_all.attr, 3300 NULL 3301 }; 3302 3303 ATTRIBUTE_GROUPS(fbcon_device); 3304 3305 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER 3306 static void fbcon_register_existing_fbs(struct work_struct *work) 3307 { 3308 int i; 3309 3310 console_lock(); 3311 3312 deferred_takeover = false; 3313 logo_shown = FBCON_LOGO_DONTSHOW; 3314 3315 fbcon_for_each_registered_fb(i) 3316 do_fb_registered(fbcon_registered_fb[i]); 3317 3318 console_unlock(); 3319 } 3320 3321 static struct notifier_block fbcon_output_nb; 3322 static DECLARE_WORK(fbcon_deferred_takeover_work, fbcon_register_existing_fbs); 3323 3324 static int fbcon_output_notifier(struct notifier_block *nb, 3325 unsigned long action, void *data) 3326 { 3327 WARN_CONSOLE_UNLOCKED(); 3328 3329 pr_info("fbcon: Taking over console\n"); 3330 3331 dummycon_unregister_output_notifier(&fbcon_output_nb); 3332 3333 /* We may get called in atomic context */ 3334 schedule_work(&fbcon_deferred_takeover_work); 3335 3336 return NOTIFY_OK; 3337 } 3338 #endif 3339 3340 static void fbcon_start(void) 3341 { 3342 WARN_CONSOLE_UNLOCKED(); 3343 3344 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER 3345 if (conswitchp != &dummy_con) 3346 deferred_takeover = false; 3347 3348 if (deferred_takeover) { 3349 fbcon_output_nb.notifier_call = fbcon_output_notifier; 3350 dummycon_register_output_notifier(&fbcon_output_nb); 3351 return; 3352 } 3353 #endif 3354 } 3355 3356 void __init fb_console_init(void) 3357 { 3358 int i; 3359 3360 console_lock(); 3361 fbcon_device = device_create_with_groups(fb_class, NULL, 3362 MKDEV(0, 0), NULL, 3363 fbcon_device_groups, "fbcon"); 3364 3365 if (IS_ERR(fbcon_device)) { 3366 printk(KERN_WARNING "Unable to create device " 3367 "for fbcon; errno = %ld\n", 3368 PTR_ERR(fbcon_device)); 3369 fbcon_device = NULL; 3370 } 3371 3372 for (i = 0; i < MAX_NR_CONSOLES; i++) 3373 con2fb_map[i] = -1; 3374 3375 fbcon_start(); 3376 console_unlock(); 3377 } 3378 3379 #ifdef MODULE 3380 3381 void __exit fb_console_exit(void) 3382 { 3383 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER 3384 console_lock(); 3385 if (deferred_takeover) 3386 dummycon_unregister_output_notifier(&fbcon_output_nb); 3387 console_unlock(); 3388 3389 cancel_work_sync(&fbcon_deferred_takeover_work); 3390 #endif 3391 3392 console_lock(); 3393 device_destroy(fb_class, MKDEV(0, 0)); 3394 3395 do_unregister_con_driver(&fb_con); 3396 console_unlock(); 3397 } 3398 #endif 3399