Linux 3.4.102
[linux/fpc-iii.git] / drivers / video / console / fbcon.c
bloba01317c9c07b152c3458d6e629d4d6e7c9daf49b
1 /*
2 * linux/drivers/video/fbcon.c -- Low level frame buffer based console driver
4 * Copyright (C) 1995 Geert Uytterhoeven
7 * This file is based on the original Amiga console driver (amicon.c):
9 * Copyright (C) 1993 Hamish Macdonald
10 * Greg Harp
11 * Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
13 * with work by William Rucklidge (wjr@cs.cornell.edu)
14 * Geert Uytterhoeven
15 * Jes Sorensen (jds@kom.auc.dk)
16 * Martin Apel
18 * and on the original Atari console driver (atacon.c):
20 * Copyright (C) 1993 Bjoern Brauel
21 * Roman Hodek
23 * with work by Guenther Kelleter
24 * Martin Schaller
25 * Andreas Schwab
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)
32 * Random hacking by Martin Mares <mj@ucw.cz>
34 * 2001 - Documented with DocBook
35 * - Brad Douglas <brad@neruo.com>
37 * The low level operations for the various display memory organizations are
38 * now in separate source files.
40 * Currently the following organizations are supported:
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
49 * To do:
51 * - Implement 16 plane mode (iplan2p16)
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.
59 #undef FBCONDEBUG
61 #include <linux/module.h>
62 #include <linux/types.h>
63 #include <linux/fs.h>
64 #include <linux/kernel.h>
65 #include <linux/delay.h> /* MSch: for IRQ probe */
66 #include <linux/console.h>
67 #include <linux/string.h>
68 #include <linux/kd.h>
69 #include <linux/slab.h>
70 #include <linux/fb.h>
71 #include <linux/vt_kern.h>
72 #include <linux/selection.h>
73 #include <linux/font.h>
74 #include <linux/smp.h>
75 #include <linux/init.h>
76 #include <linux/interrupt.h>
77 #include <linux/crc32.h> /* For counting font checksums */
78 #include <asm/fb.h>
79 #include <asm/irq.h>
81 #include "fbcon.h"
83 #ifdef FBCONDEBUG
84 # define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__ , ## args)
85 #else
86 # define DPRINTK(fmt, args...)
87 #endif
89 enum {
90 FBCON_LOGO_CANSHOW = -1, /* the logo can be shown */
91 FBCON_LOGO_DRAW = -2, /* draw the logo to a console */
92 FBCON_LOGO_DONTSHOW = -3 /* do not show the logo */
95 static struct display fb_display[MAX_NR_CONSOLES];
97 static signed char con2fb_map[MAX_NR_CONSOLES];
98 static signed char con2fb_map_boot[MAX_NR_CONSOLES];
100 static int logo_lines;
101 /* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO
102 enums. */
103 static int logo_shown = FBCON_LOGO_CANSHOW;
104 /* Software scrollback */
105 static int fbcon_softback_size = 32768;
106 static unsigned long softback_buf, softback_curr;
107 static unsigned long softback_in;
108 static unsigned long softback_top, softback_end;
109 static int softback_lines;
110 /* console mappings */
111 static int first_fb_vc;
112 static int last_fb_vc = MAX_NR_CONSOLES - 1;
113 static int fbcon_is_default = 1;
114 static int fbcon_has_exited;
115 static int primary_device = -1;
116 static int fbcon_has_console_bind;
118 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
119 static int map_override;
121 static inline void fbcon_map_override(void)
123 map_override = 1;
125 #else
126 static inline void fbcon_map_override(void)
129 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY */
131 /* font data */
132 static char fontname[40];
134 /* current fb_info */
135 static int info_idx = -1;
137 /* console rotation */
138 static int initial_rotation;
139 static int fbcon_has_sysfs;
141 static const struct consw fb_con;
143 #define CM_SOFTBACK (8)
145 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
147 static int fbcon_set_origin(struct vc_data *);
149 #define CURSOR_DRAW_DELAY (1)
151 static int vbl_cursor_cnt;
152 static int fbcon_cursor_noblink;
154 #define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1)
157 * Interface used by the world
160 static const char *fbcon_startup(void);
161 static void fbcon_init(struct vc_data *vc, int init);
162 static void fbcon_deinit(struct vc_data *vc);
163 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
164 int width);
165 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
166 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
167 int count, int ypos, int xpos);
168 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
169 static void fbcon_cursor(struct vc_data *vc, int mode);
170 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
171 int count);
172 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
173 int height, int width);
174 static int fbcon_switch(struct vc_data *vc);
175 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
176 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
177 static int fbcon_scrolldelta(struct vc_data *vc, int lines);
180 * Internal routines
182 static __inline__ void ywrap_up(struct vc_data *vc, int count);
183 static __inline__ void ywrap_down(struct vc_data *vc, int count);
184 static __inline__ void ypan_up(struct vc_data *vc, int count);
185 static __inline__ void ypan_down(struct vc_data *vc, int count);
186 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
187 int dy, int dx, int height, int width, u_int y_break);
188 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
189 int unit);
190 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
191 int line, int count, int dy);
192 static void fbcon_modechanged(struct fb_info *info);
193 static void fbcon_set_all_vcs(struct fb_info *info);
194 static void fbcon_start(void);
195 static void fbcon_exit(void);
196 static struct device *fbcon_device;
198 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
199 static inline void fbcon_set_rotation(struct fb_info *info)
201 struct fbcon_ops *ops = info->fbcon_par;
203 if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
204 ops->p->con_rotate < 4)
205 ops->rotate = ops->p->con_rotate;
206 else
207 ops->rotate = 0;
210 static void fbcon_rotate(struct fb_info *info, u32 rotate)
212 struct fbcon_ops *ops= info->fbcon_par;
213 struct fb_info *fb_info;
215 if (!ops || ops->currcon == -1)
216 return;
218 fb_info = registered_fb[con2fb_map[ops->currcon]];
220 if (info == fb_info) {
221 struct display *p = &fb_display[ops->currcon];
223 if (rotate < 4)
224 p->con_rotate = rotate;
225 else
226 p->con_rotate = 0;
228 fbcon_modechanged(info);
232 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
234 struct fbcon_ops *ops = info->fbcon_par;
235 struct vc_data *vc;
236 struct display *p;
237 int i;
239 if (!ops || ops->currcon < 0 || rotate > 3)
240 return;
242 for (i = first_fb_vc; i <= last_fb_vc; i++) {
243 vc = vc_cons[i].d;
244 if (!vc || vc->vc_mode != KD_TEXT ||
245 registered_fb[con2fb_map[i]] != info)
246 continue;
248 p = &fb_display[vc->vc_num];
249 p->con_rotate = rotate;
252 fbcon_set_all_vcs(info);
254 #else
255 static inline void fbcon_set_rotation(struct fb_info *info)
257 struct fbcon_ops *ops = info->fbcon_par;
259 ops->rotate = FB_ROTATE_UR;
262 static void fbcon_rotate(struct fb_info *info, u32 rotate)
264 return;
267 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
269 return;
271 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
273 static int fbcon_get_rotate(struct fb_info *info)
275 struct fbcon_ops *ops = info->fbcon_par;
277 return (ops) ? ops->rotate : 0;
280 static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
282 struct fbcon_ops *ops = info->fbcon_par;
284 return (info->state != FBINFO_STATE_RUNNING ||
285 vc->vc_mode != KD_TEXT || ops->graphics) &&
286 !vt_force_oops_output(vc);
289 static int get_color(struct vc_data *vc, struct fb_info *info,
290 u16 c, int is_fg)
292 int depth = fb_get_color_depth(&info->var, &info->fix);
293 int color = 0;
295 if (console_blanked) {
296 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
298 c = vc->vc_video_erase_char & charmask;
301 if (depth != 1)
302 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
303 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
305 switch (depth) {
306 case 1:
308 int col = mono_col(info);
309 /* 0 or 1 */
310 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
311 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
313 if (console_blanked)
314 fg = bg;
316 color = (is_fg) ? fg : bg;
317 break;
319 case 2:
321 * Scale down 16-colors to 4 colors. Default 4-color palette
322 * is grayscale. However, simply dividing the values by 4
323 * will not work, as colors 1, 2 and 3 will be scaled-down
324 * to zero rendering them invisible. So empirically convert
325 * colors to a sane 4-level grayscale.
327 switch (color) {
328 case 0:
329 color = 0; /* black */
330 break;
331 case 1 ... 6:
332 color = 2; /* white */
333 break;
334 case 7 ... 8:
335 color = 1; /* gray */
336 break;
337 default:
338 color = 3; /* intense white */
339 break;
341 break;
342 case 3:
344 * Last 8 entries of default 16-color palette is a more intense
345 * version of the first 8 (i.e., same chrominance, different
346 * luminance).
348 color &= 7;
349 break;
353 return color;
356 static void fbcon_update_softback(struct vc_data *vc)
358 int l = fbcon_softback_size / vc->vc_size_row;
360 if (l > 5)
361 softback_end = softback_buf + l * vc->vc_size_row;
362 else
363 /* Smaller scrollback makes no sense, and 0 would screw
364 the operation totally */
365 softback_top = 0;
368 static void fb_flashcursor(struct work_struct *work)
370 struct fb_info *info = container_of(work, struct fb_info, queue);
371 struct fbcon_ops *ops = info->fbcon_par;
372 struct vc_data *vc = NULL;
373 int c;
374 int mode;
375 int ret;
377 /* FIXME: we should sort out the unbind locking instead */
378 /* instead we just fail to flash the cursor if we can't get
379 * the lock instead of blocking fbcon deinit */
380 ret = console_trylock();
381 if (ret == 0)
382 return;
384 if (ops && ops->currcon != -1)
385 vc = vc_cons[ops->currcon].d;
387 if (!vc || !CON_IS_VISIBLE(vc) ||
388 registered_fb[con2fb_map[vc->vc_num]] != info ||
389 vc->vc_deccm != 1) {
390 console_unlock();
391 return;
394 c = scr_readw((u16 *) vc->vc_pos);
395 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
396 CM_ERASE : CM_DRAW;
397 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
398 get_color(vc, info, c, 0));
399 console_unlock();
402 static void cursor_timer_handler(unsigned long dev_addr)
404 struct fb_info *info = (struct fb_info *) dev_addr;
405 struct fbcon_ops *ops = info->fbcon_par;
407 schedule_work(&info->queue);
408 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
411 static void fbcon_add_cursor_timer(struct fb_info *info)
413 struct fbcon_ops *ops = info->fbcon_par;
415 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
416 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
417 !fbcon_cursor_noblink) {
418 if (!info->queue.func)
419 INIT_WORK(&info->queue, fb_flashcursor);
421 init_timer(&ops->cursor_timer);
422 ops->cursor_timer.function = cursor_timer_handler;
423 ops->cursor_timer.expires = jiffies + HZ / 5;
424 ops->cursor_timer.data = (unsigned long ) info;
425 add_timer(&ops->cursor_timer);
426 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
430 static void fbcon_del_cursor_timer(struct fb_info *info)
432 struct fbcon_ops *ops = info->fbcon_par;
434 if (info->queue.func == fb_flashcursor &&
435 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
436 del_timer_sync(&ops->cursor_timer);
437 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
441 #ifndef MODULE
442 static int __init fb_console_setup(char *this_opt)
444 char *options;
445 int i, j;
447 if (!this_opt || !*this_opt)
448 return 1;
450 while ((options = strsep(&this_opt, ",")) != NULL) {
451 if (!strncmp(options, "font:", 5))
452 strcpy(fontname, options + 5);
454 if (!strncmp(options, "scrollback:", 11)) {
455 options += 11;
456 if (*options) {
457 fbcon_softback_size = simple_strtoul(options, &options, 0);
458 if (*options == 'k' || *options == 'K') {
459 fbcon_softback_size *= 1024;
460 options++;
462 if (*options != ',')
463 return 1;
464 options++;
465 } else
466 return 1;
469 if (!strncmp(options, "map:", 4)) {
470 options += 4;
471 if (*options) {
472 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
473 if (!options[j])
474 j = 0;
475 con2fb_map_boot[i] =
476 (options[j++]-'0') % FB_MAX;
479 fbcon_map_override();
482 return 1;
485 if (!strncmp(options, "vc:", 3)) {
486 options += 3;
487 if (*options)
488 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
489 if (first_fb_vc < 0)
490 first_fb_vc = 0;
491 if (*options++ == '-')
492 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
493 fbcon_is_default = 0;
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;
504 return 1;
507 __setup("fbcon=", fb_console_setup);
508 #endif
510 static int search_fb_in_map(int idx)
512 int i, retval = 0;
514 for (i = first_fb_vc; i <= last_fb_vc; i++) {
515 if (con2fb_map[i] == idx)
516 retval = 1;
518 return retval;
521 static int search_for_mapped_con(void)
523 int i, retval = 0;
525 for (i = first_fb_vc; i <= last_fb_vc; i++) {
526 if (con2fb_map[i] != -1)
527 retval = 1;
529 return retval;
532 static int do_fbcon_takeover(int show_logo)
534 int err, i;
536 if (!num_registered_fb)
537 return -ENODEV;
539 if (!show_logo)
540 logo_shown = FBCON_LOGO_DONTSHOW;
542 for (i = first_fb_vc; i <= last_fb_vc; i++)
543 con2fb_map[i] = info_idx;
545 err = do_take_over_console(&fb_con, first_fb_vc, last_fb_vc,
546 fbcon_is_default);
548 if (err) {
549 for (i = first_fb_vc; i <= last_fb_vc; i++)
550 con2fb_map[i] = -1;
551 info_idx = -1;
552 } else {
553 fbcon_has_console_bind = 1;
556 return err;
559 static int fbcon_takeover(int show_logo)
561 int err, i;
563 if (!num_registered_fb)
564 return -ENODEV;
566 if (!show_logo)
567 logo_shown = FBCON_LOGO_DONTSHOW;
569 for (i = first_fb_vc; i <= last_fb_vc; i++)
570 con2fb_map[i] = info_idx;
572 err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
573 fbcon_is_default);
575 if (err) {
576 for (i = first_fb_vc; i <= last_fb_vc; i++) {
577 con2fb_map[i] = -1;
579 info_idx = -1;
580 } else {
581 fbcon_has_console_bind = 1;
584 return err;
587 #ifdef MODULE
588 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
589 int cols, int rows, int new_cols, int new_rows)
591 logo_shown = FBCON_LOGO_DONTSHOW;
593 #else
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)
597 /* Need to make room for the logo */
598 struct fbcon_ops *ops = info->fbcon_par;
599 int cnt, erase = vc->vc_video_erase_char, step;
600 unsigned short *save = NULL, *r, *q;
601 int logo_height;
603 if (info->flags & FBINFO_MODULE) {
604 logo_shown = FBCON_LOGO_DONTSHOW;
605 return;
609 * remove underline attribute from erase character
610 * if black and white framebuffer.
612 if (fb_get_color_depth(&info->var, &info->fix) == 1)
613 erase &= ~0x400;
614 logo_height = fb_prepare_logo(info, ops->rotate);
615 logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
616 q = (unsigned short *) (vc->vc_origin +
617 vc->vc_size_row * rows);
618 step = logo_lines * cols;
619 for (r = q - logo_lines * cols; r < q; r++)
620 if (scr_readw(r) != vc->vc_video_erase_char)
621 break;
622 if (r != q && new_rows >= rows + logo_lines) {
623 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
624 if (save) {
625 int i = cols < new_cols ? cols : new_cols;
626 scr_memsetw(save, erase, logo_lines * new_cols * 2);
627 r = q - step;
628 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
629 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
630 r = q;
633 if (r == q) {
634 /* We can scroll screen down */
635 r = q - step - cols;
636 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
637 scr_memcpyw(r + step, r, vc->vc_size_row);
638 r -= cols;
640 if (!save) {
641 int lines;
642 if (vc->vc_y + logo_lines >= rows)
643 lines = rows - vc->vc_y - 1;
644 else
645 lines = logo_lines;
646 vc->vc_y += lines;
647 vc->vc_pos += lines * vc->vc_size_row;
650 scr_memsetw((unsigned short *) vc->vc_origin,
651 erase,
652 vc->vc_size_row * logo_lines);
654 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
655 fbcon_clear_margins(vc, 0);
656 update_screen(vc);
659 if (save) {
660 q = (unsigned short *) (vc->vc_origin +
661 vc->vc_size_row *
662 rows);
663 scr_memcpyw(q, save, logo_lines * new_cols * 2);
664 vc->vc_y += logo_lines;
665 vc->vc_pos += logo_lines * vc->vc_size_row;
666 kfree(save);
669 if (logo_lines > vc->vc_bottom) {
670 logo_shown = FBCON_LOGO_CANSHOW;
671 printk(KERN_INFO
672 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
673 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
674 logo_shown = FBCON_LOGO_DRAW;
675 vc->vc_top = logo_lines;
678 #endif /* MODULE */
680 #ifdef CONFIG_FB_TILEBLITTING
681 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
683 struct fbcon_ops *ops = info->fbcon_par;
685 ops->p = &fb_display[vc->vc_num];
687 if ((info->flags & FBINFO_MISC_TILEBLITTING))
688 fbcon_set_tileops(vc, info);
689 else {
690 fbcon_set_rotation(info);
691 fbcon_set_bitops(ops);
695 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
697 int err = 0;
699 if (info->flags & FBINFO_MISC_TILEBLITTING &&
700 info->tileops->fb_get_tilemax(info) < charcount)
701 err = 1;
703 return err;
705 #else
706 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
708 struct fbcon_ops *ops = info->fbcon_par;
710 info->flags &= ~FBINFO_MISC_TILEBLITTING;
711 ops->p = &fb_display[vc->vc_num];
712 fbcon_set_rotation(info);
713 fbcon_set_bitops(ops);
716 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
718 return 0;
721 #endif /* CONFIG_MISC_TILEBLITTING */
724 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
725 int unit, int oldidx)
727 struct fbcon_ops *ops = NULL;
728 int err = 0;
730 if (!try_module_get(info->fbops->owner))
731 err = -ENODEV;
733 if (!err && info->fbops->fb_open &&
734 info->fbops->fb_open(info, 0))
735 err = -ENODEV;
737 if (!err) {
738 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
739 if (!ops)
740 err = -ENOMEM;
743 if (!err) {
744 info->fbcon_par = ops;
746 if (vc)
747 set_blitting_type(vc, info);
750 if (err) {
751 con2fb_map[unit] = oldidx;
752 module_put(info->fbops->owner);
755 return err;
758 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
759 struct fb_info *newinfo, int unit,
760 int oldidx, int found)
762 struct fbcon_ops *ops = oldinfo->fbcon_par;
763 int err = 0, ret;
765 if (oldinfo->fbops->fb_release &&
766 oldinfo->fbops->fb_release(oldinfo, 0)) {
767 con2fb_map[unit] = oldidx;
768 if (!found && newinfo->fbops->fb_release)
769 newinfo->fbops->fb_release(newinfo, 0);
770 if (!found)
771 module_put(newinfo->fbops->owner);
772 err = -ENODEV;
775 if (!err) {
776 fbcon_del_cursor_timer(oldinfo);
777 kfree(ops->cursor_state.mask);
778 kfree(ops->cursor_data);
779 kfree(ops->fontbuffer);
780 kfree(oldinfo->fbcon_par);
781 oldinfo->fbcon_par = NULL;
782 module_put(oldinfo->fbops->owner);
784 If oldinfo and newinfo are driving the same hardware,
785 the fb_release() method of oldinfo may attempt to
786 restore the hardware state. This will leave the
787 newinfo in an undefined state. Thus, a call to
788 fb_set_par() may be needed for the newinfo.
790 if (newinfo->fbops->fb_set_par) {
791 ret = newinfo->fbops->fb_set_par(newinfo);
793 if (ret)
794 printk(KERN_ERR "con2fb_release_oldinfo: "
795 "detected unhandled fb_set_par error, "
796 "error code %d\n", ret);
800 return err;
803 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
804 int unit, int show_logo)
806 struct fbcon_ops *ops = info->fbcon_par;
807 int ret;
809 ops->currcon = fg_console;
811 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT)) {
812 ret = info->fbops->fb_set_par(info);
814 if (ret)
815 printk(KERN_ERR "con2fb_init_display: detected "
816 "unhandled fb_set_par error, "
817 "error code %d\n", ret);
820 ops->flags |= FBCON_FLAGS_INIT;
821 ops->graphics = 0;
822 fbcon_set_disp(info, &info->var, unit);
824 if (show_logo) {
825 struct vc_data *fg_vc = vc_cons[fg_console].d;
826 struct fb_info *fg_info =
827 registered_fb[con2fb_map[fg_console]];
829 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
830 fg_vc->vc_rows, fg_vc->vc_cols,
831 fg_vc->vc_rows);
834 update_screen(vc_cons[fg_console].d);
838 * set_con2fb_map - map console to frame buffer device
839 * @unit: virtual console number to map
840 * @newidx: frame buffer index to map virtual console to
841 * @user: user request
843 * Maps a virtual console @unit to a frame buffer device
844 * @newidx.
846 * This should be called with the console lock held.
848 static int set_con2fb_map(int unit, int newidx, int user)
850 struct vc_data *vc = vc_cons[unit].d;
851 int oldidx = con2fb_map[unit];
852 struct fb_info *info = registered_fb[newidx];
853 struct fb_info *oldinfo = NULL;
854 int found, err = 0;
856 if (oldidx == newidx)
857 return 0;
859 if (!info)
860 return -EINVAL;
862 if (!search_for_mapped_con() || !con_is_bound(&fb_con)) {
863 info_idx = newidx;
864 return do_fbcon_takeover(0);
867 if (oldidx != -1)
868 oldinfo = registered_fb[oldidx];
870 found = search_fb_in_map(newidx);
872 con2fb_map[unit] = newidx;
873 if (!err && !found)
874 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
878 * If old fb is not mapped to any of the consoles,
879 * fbcon should release it.
881 if (!err && oldinfo && !search_fb_in_map(oldidx))
882 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
883 found);
885 if (!err) {
886 int show_logo = (fg_console == 0 && !user &&
887 logo_shown != FBCON_LOGO_DONTSHOW);
889 if (!found)
890 fbcon_add_cursor_timer(info);
891 con2fb_map_boot[unit] = newidx;
892 con2fb_init_display(vc, info, unit, show_logo);
895 if (!search_fb_in_map(info_idx))
896 info_idx = newidx;
898 return err;
902 * Low Level Operations
904 /* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
905 static int var_to_display(struct display *disp,
906 struct fb_var_screeninfo *var,
907 struct fb_info *info)
909 disp->xres_virtual = var->xres_virtual;
910 disp->yres_virtual = var->yres_virtual;
911 disp->bits_per_pixel = var->bits_per_pixel;
912 disp->grayscale = var->grayscale;
913 disp->nonstd = var->nonstd;
914 disp->accel_flags = var->accel_flags;
915 disp->height = var->height;
916 disp->width = var->width;
917 disp->red = var->red;
918 disp->green = var->green;
919 disp->blue = var->blue;
920 disp->transp = var->transp;
921 disp->rotate = var->rotate;
922 disp->mode = fb_match_mode(var, &info->modelist);
923 if (disp->mode == NULL)
924 /* This should not happen */
925 return -EINVAL;
926 return 0;
929 static void display_to_var(struct fb_var_screeninfo *var,
930 struct display *disp)
932 fb_videomode_to_var(var, disp->mode);
933 var->xres_virtual = disp->xres_virtual;
934 var->yres_virtual = disp->yres_virtual;
935 var->bits_per_pixel = disp->bits_per_pixel;
936 var->grayscale = disp->grayscale;
937 var->nonstd = disp->nonstd;
938 var->accel_flags = disp->accel_flags;
939 var->height = disp->height;
940 var->width = disp->width;
941 var->red = disp->red;
942 var->green = disp->green;
943 var->blue = disp->blue;
944 var->transp = disp->transp;
945 var->rotate = disp->rotate;
948 static const char *fbcon_startup(void)
950 const char *display_desc = "frame buffer device";
951 struct display *p = &fb_display[fg_console];
952 struct vc_data *vc = vc_cons[fg_console].d;
953 const struct font_desc *font = NULL;
954 struct module *owner;
955 struct fb_info *info = NULL;
956 struct fbcon_ops *ops;
957 int rows, cols;
960 * If num_registered_fb is zero, this is a call for the dummy part.
961 * The frame buffer devices weren't initialized yet.
963 if (!num_registered_fb || info_idx == -1)
964 return display_desc;
966 * Instead of blindly using registered_fb[0], we use info_idx, set by
967 * fb_console_init();
969 info = registered_fb[info_idx];
970 if (!info)
971 return NULL;
973 owner = info->fbops->owner;
974 if (!try_module_get(owner))
975 return NULL;
976 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
977 module_put(owner);
978 return NULL;
981 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
982 if (!ops) {
983 module_put(owner);
984 return NULL;
987 ops->currcon = -1;
988 ops->graphics = 1;
989 ops->cur_rotate = -1;
990 info->fbcon_par = ops;
991 p->con_rotate = initial_rotation;
992 set_blitting_type(vc, info);
994 if (info->fix.type != FB_TYPE_TEXT) {
995 if (fbcon_softback_size) {
996 if (!softback_buf) {
997 softback_buf =
998 (unsigned long)
999 kmalloc(fbcon_softback_size,
1000 GFP_KERNEL);
1001 if (!softback_buf) {
1002 fbcon_softback_size = 0;
1003 softback_top = 0;
1006 } else {
1007 if (softback_buf) {
1008 kfree((void *) softback_buf);
1009 softback_buf = 0;
1010 softback_top = 0;
1013 if (softback_buf)
1014 softback_in = softback_top = softback_curr =
1015 softback_buf;
1016 softback_lines = 0;
1019 /* Setup default font */
1020 if (!p->fontdata && !vc->vc_font.data) {
1021 if (!fontname[0] || !(font = find_font(fontname)))
1022 font = get_default_font(info->var.xres,
1023 info->var.yres,
1024 info->pixmap.blit_x,
1025 info->pixmap.blit_y);
1026 vc->vc_font.width = font->width;
1027 vc->vc_font.height = font->height;
1028 vc->vc_font.data = (void *)(p->fontdata = font->data);
1029 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1030 } else {
1031 p->fontdata = vc->vc_font.data;
1034 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1035 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1036 cols /= vc->vc_font.width;
1037 rows /= vc->vc_font.height;
1038 vc_resize(vc, cols, rows);
1040 DPRINTK("mode: %s\n", info->fix.id);
1041 DPRINTK("visual: %d\n", info->fix.visual);
1042 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1043 info->var.yres,
1044 info->var.bits_per_pixel);
1046 fbcon_add_cursor_timer(info);
1047 fbcon_has_exited = 0;
1048 return display_desc;
1051 static void fbcon_init(struct vc_data *vc, int init)
1053 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1054 struct fbcon_ops *ops;
1055 struct vc_data **default_mode = vc->vc_display_fg;
1056 struct vc_data *svc = *default_mode;
1057 struct display *t, *p = &fb_display[vc->vc_num];
1058 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
1059 int cap, ret;
1061 if (info_idx == -1 || info == NULL)
1062 return;
1064 cap = info->flags;
1066 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1067 (info->fix.type == FB_TYPE_TEXT))
1068 logo = 0;
1070 if (var_to_display(p, &info->var, info))
1071 return;
1073 if (!info->fbcon_par)
1074 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1076 /* If we are not the first console on this
1077 fb, copy the font from that console */
1078 t = &fb_display[fg_console];
1079 if (!p->fontdata) {
1080 if (t->fontdata) {
1081 struct vc_data *fvc = vc_cons[fg_console].d;
1083 vc->vc_font.data = (void *)(p->fontdata =
1084 fvc->vc_font.data);
1085 vc->vc_font.width = fvc->vc_font.width;
1086 vc->vc_font.height = fvc->vc_font.height;
1087 p->userfont = t->userfont;
1089 if (p->userfont)
1090 REFCOUNT(p->fontdata)++;
1091 } else {
1092 const struct font_desc *font = NULL;
1094 if (!fontname[0] || !(font = find_font(fontname)))
1095 font = get_default_font(info->var.xres,
1096 info->var.yres,
1097 info->pixmap.blit_x,
1098 info->pixmap.blit_y);
1099 vc->vc_font.width = font->width;
1100 vc->vc_font.height = font->height;
1101 vc->vc_font.data = (void *)(p->fontdata = font->data);
1102 vc->vc_font.charcount = 256; /* FIXME Need to
1103 support more fonts */
1107 if (p->userfont)
1108 charcnt = FNTCHARCNT(p->fontdata);
1110 vc->vc_panic_force_write = !!(info->flags & FBINFO_CAN_FORCE_OUTPUT);
1111 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1112 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1113 if (charcnt == 256) {
1114 vc->vc_hi_font_mask = 0;
1115 } else {
1116 vc->vc_hi_font_mask = 0x100;
1117 if (vc->vc_can_do_color)
1118 vc->vc_complement_mask <<= 1;
1121 if (!*svc->vc_uni_pagedir_loc)
1122 con_set_default_unimap(svc);
1123 if (!*vc->vc_uni_pagedir_loc)
1124 con_copy_unimap(vc, svc);
1126 ops = info->fbcon_par;
1127 p->con_rotate = initial_rotation;
1128 set_blitting_type(vc, info);
1130 cols = vc->vc_cols;
1131 rows = vc->vc_rows;
1132 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1133 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1134 new_cols /= vc->vc_font.width;
1135 new_rows /= vc->vc_font.height;
1138 * We must always set the mode. The mode of the previous console
1139 * driver could be in the same resolution but we are using different
1140 * hardware so we have to initialize the hardware.
1142 * We need to do it in fbcon_init() to prevent screen corruption.
1144 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
1145 if (info->fbops->fb_set_par &&
1146 !(ops->flags & FBCON_FLAGS_INIT)) {
1147 ret = info->fbops->fb_set_par(info);
1149 if (ret)
1150 printk(KERN_ERR "fbcon_init: detected "
1151 "unhandled fb_set_par error, "
1152 "error code %d\n", ret);
1155 ops->flags |= FBCON_FLAGS_INIT;
1158 ops->graphics = 0;
1160 if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1161 !(cap & FBINFO_HWACCEL_DISABLED))
1162 p->scrollmode = SCROLL_MOVE;
1163 else /* default to something safe */
1164 p->scrollmode = SCROLL_REDRAW;
1167 * ++guenther: console.c:vc_allocate() relies on initializing
1168 * vc_{cols,rows}, but we must not set those if we are only
1169 * resizing the console.
1171 if (init) {
1172 vc->vc_cols = new_cols;
1173 vc->vc_rows = new_rows;
1174 } else
1175 vc_resize(vc, new_cols, new_rows);
1177 if (logo)
1178 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1180 if (vc == svc && softback_buf)
1181 fbcon_update_softback(vc);
1183 if (ops->rotate_font && ops->rotate_font(info, vc)) {
1184 ops->rotate = FB_ROTATE_UR;
1185 set_blitting_type(vc, info);
1188 ops->p = &fb_display[fg_console];
1191 static void fbcon_free_font(struct display *p, bool freefont)
1193 if (freefont && p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1194 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1195 p->fontdata = NULL;
1196 p->userfont = 0;
1199 static void fbcon_deinit(struct vc_data *vc)
1201 struct display *p = &fb_display[vc->vc_num];
1202 struct fb_info *info;
1203 struct fbcon_ops *ops;
1204 int idx;
1205 bool free_font = true;
1207 idx = con2fb_map[vc->vc_num];
1209 if (idx == -1)
1210 goto finished;
1212 info = registered_fb[idx];
1214 if (!info)
1215 goto finished;
1217 if (info->flags & FBINFO_MISC_FIRMWARE)
1218 free_font = false;
1219 ops = info->fbcon_par;
1221 if (!ops)
1222 goto finished;
1224 if (CON_IS_VISIBLE(vc))
1225 fbcon_del_cursor_timer(info);
1227 ops->flags &= ~FBCON_FLAGS_INIT;
1228 finished:
1230 fbcon_free_font(p, free_font);
1231 if (free_font)
1232 vc->vc_font.data = NULL;
1234 if (!con_is_bound(&fb_con))
1235 fbcon_exit();
1237 return;
1240 /* ====================================================================== */
1242 /* fbcon_XXX routines - interface used by the world
1244 * This system is now divided into two levels because of complications
1245 * caused by hardware scrolling. Top level functions:
1247 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1249 * handles y values in range [0, scr_height-1] that correspond to real
1250 * screen positions. y_wrap shift means that first line of bitmap may be
1251 * anywhere on this display. These functions convert lineoffsets to
1252 * bitmap offsets and deal with the wrap-around case by splitting blits.
1254 * fbcon_bmove_physical_8() -- These functions fast implementations
1255 * fbcon_clear_physical_8() -- of original fbcon_XXX fns.
1256 * fbcon_putc_physical_8() -- (font width != 8) may be added later
1258 * WARNING:
1260 * At the moment fbcon_putc() cannot blit across vertical wrap boundary
1261 * Implies should only really hardware scroll in rows. Only reason for
1262 * restriction is simplicity & efficiency at the moment.
1265 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1266 int width)
1268 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1269 struct fbcon_ops *ops = info->fbcon_par;
1271 struct display *p = &fb_display[vc->vc_num];
1272 u_int y_break;
1274 if (fbcon_is_inactive(vc, info))
1275 return;
1277 if (!height || !width)
1278 return;
1280 if (sy < vc->vc_top && vc->vc_top == logo_lines)
1281 vc->vc_top = 0;
1283 /* Split blits that cross physical y_wrap boundary */
1285 y_break = p->vrows - p->yscroll;
1286 if (sy < y_break && sy + height - 1 >= y_break) {
1287 u_int b = y_break - sy;
1288 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1289 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1290 width);
1291 } else
1292 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1295 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1296 int count, int ypos, int xpos)
1298 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1299 struct display *p = &fb_display[vc->vc_num];
1300 struct fbcon_ops *ops = info->fbcon_par;
1302 if (!fbcon_is_inactive(vc, info))
1303 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1304 get_color(vc, info, scr_readw(s), 1),
1305 get_color(vc, info, scr_readw(s), 0));
1308 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1310 unsigned short chr;
1312 scr_writew(c, &chr);
1313 fbcon_putcs(vc, &chr, 1, ypos, xpos);
1316 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1318 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1319 struct fbcon_ops *ops = info->fbcon_par;
1321 if (!fbcon_is_inactive(vc, info))
1322 ops->clear_margins(vc, info, bottom_only);
1325 static void fbcon_cursor(struct vc_data *vc, int mode)
1327 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1328 struct fbcon_ops *ops = info->fbcon_par;
1329 int y;
1330 int c = scr_readw((u16 *) vc->vc_pos);
1332 if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1333 return;
1335 if (vc->vc_cursor_type & 0x10)
1336 fbcon_del_cursor_timer(info);
1337 else
1338 fbcon_add_cursor_timer(info);
1340 ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1341 if (mode & CM_SOFTBACK) {
1342 mode &= ~CM_SOFTBACK;
1343 y = softback_lines;
1344 } else {
1345 if (softback_lines)
1346 fbcon_set_origin(vc);
1347 y = 0;
1350 ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1351 get_color(vc, info, c, 0));
1352 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1355 static int scrollback_phys_max = 0;
1356 static int scrollback_max = 0;
1357 static int scrollback_current = 0;
1359 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1360 int unit)
1362 struct display *p, *t;
1363 struct vc_data **default_mode, *vc;
1364 struct vc_data *svc;
1365 struct fbcon_ops *ops = info->fbcon_par;
1366 int rows, cols, charcnt = 256;
1368 p = &fb_display[unit];
1370 if (var_to_display(p, var, info))
1371 return;
1373 vc = vc_cons[unit].d;
1375 if (!vc)
1376 return;
1378 default_mode = vc->vc_display_fg;
1379 svc = *default_mode;
1380 t = &fb_display[svc->vc_num];
1382 if (!vc->vc_font.data) {
1383 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1384 vc->vc_font.width = (*default_mode)->vc_font.width;
1385 vc->vc_font.height = (*default_mode)->vc_font.height;
1386 p->userfont = t->userfont;
1387 if (p->userfont)
1388 REFCOUNT(p->fontdata)++;
1390 if (p->userfont)
1391 charcnt = FNTCHARCNT(p->fontdata);
1393 var->activate = FB_ACTIVATE_NOW;
1394 info->var.activate = var->activate;
1395 var->yoffset = info->var.yoffset;
1396 var->xoffset = info->var.xoffset;
1397 fb_set_var(info, var);
1398 ops->var = info->var;
1399 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1400 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1401 if (charcnt == 256) {
1402 vc->vc_hi_font_mask = 0;
1403 } else {
1404 vc->vc_hi_font_mask = 0x100;
1405 if (vc->vc_can_do_color)
1406 vc->vc_complement_mask <<= 1;
1409 if (!*svc->vc_uni_pagedir_loc)
1410 con_set_default_unimap(svc);
1411 if (!*vc->vc_uni_pagedir_loc)
1412 con_copy_unimap(vc, svc);
1414 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1415 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1416 cols /= vc->vc_font.width;
1417 rows /= vc->vc_font.height;
1418 vc_resize(vc, cols, rows);
1420 if (CON_IS_VISIBLE(vc)) {
1421 update_screen(vc);
1422 if (softback_buf)
1423 fbcon_update_softback(vc);
1427 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1429 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1430 struct fbcon_ops *ops = info->fbcon_par;
1431 struct display *p = &fb_display[vc->vc_num];
1433 p->yscroll += count;
1434 if (p->yscroll >= p->vrows) /* Deal with wrap */
1435 p->yscroll -= p->vrows;
1436 ops->var.xoffset = 0;
1437 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1438 ops->var.vmode |= FB_VMODE_YWRAP;
1439 ops->update_start(info);
1440 scrollback_max += count;
1441 if (scrollback_max > scrollback_phys_max)
1442 scrollback_max = scrollback_phys_max;
1443 scrollback_current = 0;
1446 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1448 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1449 struct fbcon_ops *ops = info->fbcon_par;
1450 struct display *p = &fb_display[vc->vc_num];
1452 p->yscroll -= count;
1453 if (p->yscroll < 0) /* Deal with wrap */
1454 p->yscroll += p->vrows;
1455 ops->var.xoffset = 0;
1456 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1457 ops->var.vmode |= FB_VMODE_YWRAP;
1458 ops->update_start(info);
1459 scrollback_max -= count;
1460 if (scrollback_max < 0)
1461 scrollback_max = 0;
1462 scrollback_current = 0;
1465 static __inline__ void ypan_up(struct vc_data *vc, int count)
1467 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1468 struct display *p = &fb_display[vc->vc_num];
1469 struct fbcon_ops *ops = info->fbcon_par;
1471 p->yscroll += count;
1472 if (p->yscroll > p->vrows - vc->vc_rows) {
1473 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1474 0, 0, 0, vc->vc_rows, vc->vc_cols);
1475 p->yscroll -= p->vrows - vc->vc_rows;
1478 ops->var.xoffset = 0;
1479 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1480 ops->var.vmode &= ~FB_VMODE_YWRAP;
1481 ops->update_start(info);
1482 fbcon_clear_margins(vc, 1);
1483 scrollback_max += count;
1484 if (scrollback_max > scrollback_phys_max)
1485 scrollback_max = scrollback_phys_max;
1486 scrollback_current = 0;
1489 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1491 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1492 struct fbcon_ops *ops = info->fbcon_par;
1493 struct display *p = &fb_display[vc->vc_num];
1495 p->yscroll += count;
1497 if (p->yscroll > p->vrows - vc->vc_rows) {
1498 p->yscroll -= p->vrows - vc->vc_rows;
1499 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1502 ops->var.xoffset = 0;
1503 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1504 ops->var.vmode &= ~FB_VMODE_YWRAP;
1505 ops->update_start(info);
1506 fbcon_clear_margins(vc, 1);
1507 scrollback_max += count;
1508 if (scrollback_max > scrollback_phys_max)
1509 scrollback_max = scrollback_phys_max;
1510 scrollback_current = 0;
1513 static __inline__ void ypan_down(struct vc_data *vc, int count)
1515 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1516 struct display *p = &fb_display[vc->vc_num];
1517 struct fbcon_ops *ops = info->fbcon_par;
1519 p->yscroll -= count;
1520 if (p->yscroll < 0) {
1521 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1522 0, vc->vc_rows, vc->vc_cols);
1523 p->yscroll += p->vrows - vc->vc_rows;
1526 ops->var.xoffset = 0;
1527 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1528 ops->var.vmode &= ~FB_VMODE_YWRAP;
1529 ops->update_start(info);
1530 fbcon_clear_margins(vc, 1);
1531 scrollback_max -= count;
1532 if (scrollback_max < 0)
1533 scrollback_max = 0;
1534 scrollback_current = 0;
1537 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1539 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1540 struct fbcon_ops *ops = info->fbcon_par;
1541 struct display *p = &fb_display[vc->vc_num];
1543 p->yscroll -= count;
1545 if (p->yscroll < 0) {
1546 p->yscroll += p->vrows - vc->vc_rows;
1547 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1550 ops->var.xoffset = 0;
1551 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1552 ops->var.vmode &= ~FB_VMODE_YWRAP;
1553 ops->update_start(info);
1554 fbcon_clear_margins(vc, 1);
1555 scrollback_max -= count;
1556 if (scrollback_max < 0)
1557 scrollback_max = 0;
1558 scrollback_current = 0;
1561 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1562 long delta)
1564 int count = vc->vc_rows;
1565 unsigned short *d, *s;
1566 unsigned long n;
1567 int line = 0;
1569 d = (u16 *) softback_curr;
1570 if (d == (u16 *) softback_in)
1571 d = (u16 *) vc->vc_origin;
1572 n = softback_curr + delta * vc->vc_size_row;
1573 softback_lines -= delta;
1574 if (delta < 0) {
1575 if (softback_curr < softback_top && n < softback_buf) {
1576 n += softback_end - softback_buf;
1577 if (n < softback_top) {
1578 softback_lines -=
1579 (softback_top - n) / vc->vc_size_row;
1580 n = softback_top;
1582 } else if (softback_curr >= softback_top
1583 && n < softback_top) {
1584 softback_lines -=
1585 (softback_top - n) / vc->vc_size_row;
1586 n = softback_top;
1588 } else {
1589 if (softback_curr > softback_in && n >= softback_end) {
1590 n += softback_buf - softback_end;
1591 if (n > softback_in) {
1592 n = softback_in;
1593 softback_lines = 0;
1595 } else if (softback_curr <= softback_in && n > softback_in) {
1596 n = softback_in;
1597 softback_lines = 0;
1600 if (n == softback_curr)
1601 return;
1602 softback_curr = n;
1603 s = (u16 *) softback_curr;
1604 if (s == (u16 *) softback_in)
1605 s = (u16 *) vc->vc_origin;
1606 while (count--) {
1607 unsigned short *start;
1608 unsigned short *le;
1609 unsigned short c;
1610 int x = 0;
1611 unsigned short attr = 1;
1613 start = s;
1614 le = advance_row(s, 1);
1615 do {
1616 c = scr_readw(s);
1617 if (attr != (c & 0xff00)) {
1618 attr = c & 0xff00;
1619 if (s > start) {
1620 fbcon_putcs(vc, start, s - start,
1621 line, x);
1622 x += s - start;
1623 start = s;
1626 if (c == scr_readw(d)) {
1627 if (s > start) {
1628 fbcon_putcs(vc, start, s - start,
1629 line, x);
1630 x += s - start + 1;
1631 start = s + 1;
1632 } else {
1633 x++;
1634 start++;
1637 s++;
1638 d++;
1639 } while (s < le);
1640 if (s > start)
1641 fbcon_putcs(vc, start, s - start, line, x);
1642 line++;
1643 if (d == (u16 *) softback_end)
1644 d = (u16 *) softback_buf;
1645 if (d == (u16 *) softback_in)
1646 d = (u16 *) vc->vc_origin;
1647 if (s == (u16 *) softback_end)
1648 s = (u16 *) softback_buf;
1649 if (s == (u16 *) softback_in)
1650 s = (u16 *) vc->vc_origin;
1654 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1655 int line, int count, int dy)
1657 unsigned short *s = (unsigned short *)
1658 (vc->vc_origin + vc->vc_size_row * line);
1660 while (count--) {
1661 unsigned short *start = s;
1662 unsigned short *le = advance_row(s, 1);
1663 unsigned short c;
1664 int x = 0;
1665 unsigned short attr = 1;
1667 do {
1668 c = scr_readw(s);
1669 if (attr != (c & 0xff00)) {
1670 attr = c & 0xff00;
1671 if (s > start) {
1672 fbcon_putcs(vc, start, s - start,
1673 dy, x);
1674 x += s - start;
1675 start = s;
1678 console_conditional_schedule();
1679 s++;
1680 } while (s < le);
1681 if (s > start)
1682 fbcon_putcs(vc, start, s - start, dy, x);
1683 console_conditional_schedule();
1684 dy++;
1688 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1689 struct display *p, int line, int count, int ycount)
1691 int offset = ycount * vc->vc_cols;
1692 unsigned short *d = (unsigned short *)
1693 (vc->vc_origin + vc->vc_size_row * line);
1694 unsigned short *s = d + offset;
1695 struct fbcon_ops *ops = info->fbcon_par;
1697 while (count--) {
1698 unsigned short *start = s;
1699 unsigned short *le = advance_row(s, 1);
1700 unsigned short c;
1701 int x = 0;
1703 do {
1704 c = scr_readw(s);
1706 if (c == scr_readw(d)) {
1707 if (s > start) {
1708 ops->bmove(vc, info, line + ycount, x,
1709 line, x, 1, s-start);
1710 x += s - start + 1;
1711 start = s + 1;
1712 } else {
1713 x++;
1714 start++;
1718 scr_writew(c, d);
1719 console_conditional_schedule();
1720 s++;
1721 d++;
1722 } while (s < le);
1723 if (s > start)
1724 ops->bmove(vc, info, line + ycount, x, line, x, 1,
1725 s-start);
1726 console_conditional_schedule();
1727 if (ycount > 0)
1728 line++;
1729 else {
1730 line--;
1731 /* NOTE: We subtract two lines from these pointers */
1732 s -= vc->vc_size_row;
1733 d -= vc->vc_size_row;
1738 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1739 int line, int count, int offset)
1741 unsigned short *d = (unsigned short *)
1742 (vc->vc_origin + vc->vc_size_row * line);
1743 unsigned short *s = d + offset;
1745 while (count--) {
1746 unsigned short *start = s;
1747 unsigned short *le = advance_row(s, 1);
1748 unsigned short c;
1749 int x = 0;
1750 unsigned short attr = 1;
1752 do {
1753 c = scr_readw(s);
1754 if (attr != (c & 0xff00)) {
1755 attr = c & 0xff00;
1756 if (s > start) {
1757 fbcon_putcs(vc, start, s - start,
1758 line, x);
1759 x += s - start;
1760 start = s;
1763 if (c == scr_readw(d)) {
1764 if (s > start) {
1765 fbcon_putcs(vc, start, s - start,
1766 line, x);
1767 x += s - start + 1;
1768 start = s + 1;
1769 } else {
1770 x++;
1771 start++;
1774 scr_writew(c, d);
1775 console_conditional_schedule();
1776 s++;
1777 d++;
1778 } while (s < le);
1779 if (s > start)
1780 fbcon_putcs(vc, start, s - start, line, x);
1781 console_conditional_schedule();
1782 if (offset > 0)
1783 line++;
1784 else {
1785 line--;
1786 /* NOTE: We subtract two lines from these pointers */
1787 s -= vc->vc_size_row;
1788 d -= vc->vc_size_row;
1793 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1794 int count)
1796 unsigned short *p;
1798 if (vc->vc_num != fg_console)
1799 return;
1800 p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1802 while (count) {
1803 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1804 count--;
1805 p = advance_row(p, 1);
1806 softback_in += vc->vc_size_row;
1807 if (softback_in == softback_end)
1808 softback_in = softback_buf;
1809 if (softback_in == softback_top) {
1810 softback_top += vc->vc_size_row;
1811 if (softback_top == softback_end)
1812 softback_top = softback_buf;
1815 softback_curr = softback_in;
1818 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1819 int count)
1821 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1822 struct display *p = &fb_display[vc->vc_num];
1823 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1825 if (fbcon_is_inactive(vc, info))
1826 return -EINVAL;
1828 fbcon_cursor(vc, CM_ERASE);
1831 * ++Geert: Only use ywrap/ypan if the console is in text mode
1832 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1833 * whole screen (prevents flicker).
1836 switch (dir) {
1837 case SM_UP:
1838 if (count > vc->vc_rows) /* Maximum realistic size */
1839 count = vc->vc_rows;
1840 if (softback_top)
1841 fbcon_softback_note(vc, t, count);
1842 if (logo_shown >= 0)
1843 goto redraw_up;
1844 switch (p->scrollmode) {
1845 case SCROLL_MOVE:
1846 fbcon_redraw_blit(vc, info, p, t, b - t - count,
1847 count);
1848 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1849 scr_memsetw((unsigned short *) (vc->vc_origin +
1850 vc->vc_size_row *
1851 (b - count)),
1852 vc->vc_video_erase_char,
1853 vc->vc_size_row * count);
1854 return 1;
1855 break;
1857 case SCROLL_WRAP_MOVE:
1858 if (b - t - count > 3 * vc->vc_rows >> 2) {
1859 if (t > 0)
1860 fbcon_bmove(vc, 0, 0, count, 0, t,
1861 vc->vc_cols);
1862 ywrap_up(vc, count);
1863 if (vc->vc_rows - b > 0)
1864 fbcon_bmove(vc, b - count, 0, b, 0,
1865 vc->vc_rows - b,
1866 vc->vc_cols);
1867 } else if (info->flags & FBINFO_READS_FAST)
1868 fbcon_bmove(vc, t + count, 0, t, 0,
1869 b - t - count, vc->vc_cols);
1870 else
1871 goto redraw_up;
1872 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1873 break;
1875 case SCROLL_PAN_REDRAW:
1876 if ((p->yscroll + count <=
1877 2 * (p->vrows - vc->vc_rows))
1878 && ((!scroll_partial && (b - t == vc->vc_rows))
1879 || (scroll_partial
1880 && (b - t - count >
1881 3 * vc->vc_rows >> 2)))) {
1882 if (t > 0)
1883 fbcon_redraw_move(vc, p, 0, t, count);
1884 ypan_up_redraw(vc, t, count);
1885 if (vc->vc_rows - b > 0)
1886 fbcon_redraw_move(vc, p, b,
1887 vc->vc_rows - b, b);
1888 } else
1889 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1890 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1891 break;
1893 case SCROLL_PAN_MOVE:
1894 if ((p->yscroll + count <=
1895 2 * (p->vrows - vc->vc_rows))
1896 && ((!scroll_partial && (b - t == vc->vc_rows))
1897 || (scroll_partial
1898 && (b - t - count >
1899 3 * vc->vc_rows >> 2)))) {
1900 if (t > 0)
1901 fbcon_bmove(vc, 0, 0, count, 0, t,
1902 vc->vc_cols);
1903 ypan_up(vc, count);
1904 if (vc->vc_rows - b > 0)
1905 fbcon_bmove(vc, b - count, 0, b, 0,
1906 vc->vc_rows - b,
1907 vc->vc_cols);
1908 } else if (info->flags & FBINFO_READS_FAST)
1909 fbcon_bmove(vc, t + count, 0, t, 0,
1910 b - t - count, vc->vc_cols);
1911 else
1912 goto redraw_up;
1913 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1914 break;
1916 case SCROLL_REDRAW:
1917 redraw_up:
1918 fbcon_redraw(vc, p, t, b - t - count,
1919 count * vc->vc_cols);
1920 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1921 scr_memsetw((unsigned short *) (vc->vc_origin +
1922 vc->vc_size_row *
1923 (b - count)),
1924 vc->vc_video_erase_char,
1925 vc->vc_size_row * count);
1926 return 1;
1928 break;
1930 case SM_DOWN:
1931 if (count > vc->vc_rows) /* Maximum realistic size */
1932 count = vc->vc_rows;
1933 if (logo_shown >= 0)
1934 goto redraw_down;
1935 switch (p->scrollmode) {
1936 case SCROLL_MOVE:
1937 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1938 -count);
1939 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1940 scr_memsetw((unsigned short *) (vc->vc_origin +
1941 vc->vc_size_row *
1943 vc->vc_video_erase_char,
1944 vc->vc_size_row * count);
1945 return 1;
1946 break;
1948 case SCROLL_WRAP_MOVE:
1949 if (b - t - count > 3 * vc->vc_rows >> 2) {
1950 if (vc->vc_rows - b > 0)
1951 fbcon_bmove(vc, b, 0, b - count, 0,
1952 vc->vc_rows - b,
1953 vc->vc_cols);
1954 ywrap_down(vc, count);
1955 if (t > 0)
1956 fbcon_bmove(vc, count, 0, 0, 0, t,
1957 vc->vc_cols);
1958 } else if (info->flags & FBINFO_READS_FAST)
1959 fbcon_bmove(vc, t, 0, t + count, 0,
1960 b - t - count, vc->vc_cols);
1961 else
1962 goto redraw_down;
1963 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1964 break;
1966 case SCROLL_PAN_MOVE:
1967 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1968 && ((!scroll_partial && (b - t == vc->vc_rows))
1969 || (scroll_partial
1970 && (b - t - count >
1971 3 * vc->vc_rows >> 2)))) {
1972 if (vc->vc_rows - b > 0)
1973 fbcon_bmove(vc, b, 0, b - count, 0,
1974 vc->vc_rows - b,
1975 vc->vc_cols);
1976 ypan_down(vc, count);
1977 if (t > 0)
1978 fbcon_bmove(vc, count, 0, 0, 0, t,
1979 vc->vc_cols);
1980 } else if (info->flags & FBINFO_READS_FAST)
1981 fbcon_bmove(vc, t, 0, t + count, 0,
1982 b - t - count, vc->vc_cols);
1983 else
1984 goto redraw_down;
1985 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1986 break;
1988 case SCROLL_PAN_REDRAW:
1989 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1990 && ((!scroll_partial && (b - t == vc->vc_rows))
1991 || (scroll_partial
1992 && (b - t - count >
1993 3 * vc->vc_rows >> 2)))) {
1994 if (vc->vc_rows - b > 0)
1995 fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
1996 b - count);
1997 ypan_down_redraw(vc, t, count);
1998 if (t > 0)
1999 fbcon_redraw_move(vc, p, count, t, 0);
2000 } else
2001 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
2002 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2003 break;
2005 case SCROLL_REDRAW:
2006 redraw_down:
2007 fbcon_redraw(vc, p, b - 1, b - t - count,
2008 -count * vc->vc_cols);
2009 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2010 scr_memsetw((unsigned short *) (vc->vc_origin +
2011 vc->vc_size_row *
2013 vc->vc_video_erase_char,
2014 vc->vc_size_row * count);
2015 return 1;
2018 return 0;
2022 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
2023 int height, int width)
2025 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2026 struct display *p = &fb_display[vc->vc_num];
2028 if (fbcon_is_inactive(vc, info))
2029 return;
2031 if (!width || !height)
2032 return;
2034 /* Split blits that cross physical y_wrap case.
2035 * Pathological case involves 4 blits, better to use recursive
2036 * code rather than unrolled case
2038 * Recursive invocations don't need to erase the cursor over and
2039 * over again, so we use fbcon_bmove_rec()
2041 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2042 p->vrows - p->yscroll);
2045 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
2046 int dy, int dx, int height, int width, u_int y_break)
2048 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2049 struct fbcon_ops *ops = info->fbcon_par;
2050 u_int b;
2052 if (sy < y_break && sy + height > y_break) {
2053 b = y_break - sy;
2054 if (dy < sy) { /* Avoid trashing self */
2055 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2056 y_break);
2057 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2058 height - b, width, y_break);
2059 } else {
2060 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2061 height - b, width, y_break);
2062 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2063 y_break);
2065 return;
2068 if (dy < y_break && dy + height > y_break) {
2069 b = y_break - dy;
2070 if (dy < sy) { /* Avoid trashing self */
2071 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2072 y_break);
2073 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2074 height - b, width, y_break);
2075 } else {
2076 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2077 height - b, width, y_break);
2078 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2079 y_break);
2081 return;
2083 ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2084 height, width);
2087 static void updatescrollmode(struct display *p,
2088 struct fb_info *info,
2089 struct vc_data *vc)
2091 struct fbcon_ops *ops = info->fbcon_par;
2092 int fh = vc->vc_font.height;
2093 int cap = info->flags;
2094 u16 t = 0;
2095 int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2096 info->fix.xpanstep);
2097 int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2098 int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2099 int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2100 info->var.xres_virtual);
2101 int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2102 divides(ypan, vc->vc_font.height) && vyres > yres;
2103 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2104 divides(ywrap, vc->vc_font.height) &&
2105 divides(vc->vc_font.height, vyres) &&
2106 divides(vc->vc_font.height, yres);
2107 int reading_fast = cap & FBINFO_READS_FAST;
2108 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2109 !(cap & FBINFO_HWACCEL_DISABLED);
2110 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2111 !(cap & FBINFO_HWACCEL_DISABLED);
2113 p->vrows = vyres/fh;
2114 if (yres > (fh * (vc->vc_rows + 1)))
2115 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2116 if ((yres % fh) && (vyres % fh < yres % fh))
2117 p->vrows--;
2119 if (good_wrap || good_pan) {
2120 if (reading_fast || fast_copyarea)
2121 p->scrollmode = good_wrap ?
2122 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2123 else
2124 p->scrollmode = good_wrap ? SCROLL_REDRAW :
2125 SCROLL_PAN_REDRAW;
2126 } else {
2127 if (reading_fast || (fast_copyarea && !fast_imageblit))
2128 p->scrollmode = SCROLL_MOVE;
2129 else
2130 p->scrollmode = SCROLL_REDRAW;
2134 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2135 unsigned int height, unsigned int user)
2137 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2138 struct fbcon_ops *ops = info->fbcon_par;
2139 struct display *p = &fb_display[vc->vc_num];
2140 struct fb_var_screeninfo var = info->var;
2141 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2143 virt_w = FBCON_SWAP(ops->rotate, width, height);
2144 virt_h = FBCON_SWAP(ops->rotate, height, width);
2145 virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2146 vc->vc_font.height);
2147 virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2148 vc->vc_font.width);
2149 var.xres = virt_w * virt_fw;
2150 var.yres = virt_h * virt_fh;
2151 x_diff = info->var.xres - var.xres;
2152 y_diff = info->var.yres - var.yres;
2153 if (x_diff < 0 || x_diff > virt_fw ||
2154 y_diff < 0 || y_diff > virt_fh) {
2155 const struct fb_videomode *mode;
2157 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2158 mode = fb_find_best_mode(&var, &info->modelist);
2159 if (mode == NULL)
2160 return -EINVAL;
2161 display_to_var(&var, p);
2162 fb_videomode_to_var(&var, mode);
2164 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2165 return -EINVAL;
2167 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2168 if (CON_IS_VISIBLE(vc)) {
2169 var.activate = FB_ACTIVATE_NOW |
2170 FB_ACTIVATE_FORCE;
2171 fb_set_var(info, &var);
2173 var_to_display(p, &info->var, info);
2174 ops->var = info->var;
2176 updatescrollmode(p, info, vc);
2177 return 0;
2180 static int fbcon_switch(struct vc_data *vc)
2182 struct fb_info *info, *old_info = NULL;
2183 struct fbcon_ops *ops;
2184 struct display *p = &fb_display[vc->vc_num];
2185 struct fb_var_screeninfo var;
2186 int i, ret, prev_console, charcnt = 256;
2188 info = registered_fb[con2fb_map[vc->vc_num]];
2189 ops = info->fbcon_par;
2191 if (softback_top) {
2192 if (softback_lines)
2193 fbcon_set_origin(vc);
2194 softback_top = softback_curr = softback_in = softback_buf;
2195 softback_lines = 0;
2196 fbcon_update_softback(vc);
2199 if (logo_shown >= 0) {
2200 struct vc_data *conp2 = vc_cons[logo_shown].d;
2202 if (conp2->vc_top == logo_lines
2203 && conp2->vc_bottom == conp2->vc_rows)
2204 conp2->vc_top = 0;
2205 logo_shown = FBCON_LOGO_CANSHOW;
2208 prev_console = ops->currcon;
2209 if (prev_console != -1)
2210 old_info = registered_fb[con2fb_map[prev_console]];
2212 * FIXME: If we have multiple fbdev's loaded, we need to
2213 * update all info->currcon. Perhaps, we can place this
2214 * in a centralized structure, but this might break some
2215 * drivers.
2217 * info->currcon = vc->vc_num;
2219 for (i = 0; i < FB_MAX; i++) {
2220 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
2221 struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2223 o->currcon = vc->vc_num;
2226 memset(&var, 0, sizeof(struct fb_var_screeninfo));
2227 display_to_var(&var, p);
2228 var.activate = FB_ACTIVATE_NOW;
2231 * make sure we don't unnecessarily trip the memcmp()
2232 * in fb_set_var()
2234 info->var.activate = var.activate;
2235 var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
2236 fb_set_var(info, &var);
2237 ops->var = info->var;
2239 if (old_info != NULL && (old_info != info ||
2240 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2241 if (info->fbops->fb_set_par) {
2242 ret = info->fbops->fb_set_par(info);
2244 if (ret)
2245 printk(KERN_ERR "fbcon_switch: detected "
2246 "unhandled fb_set_par error, "
2247 "error code %d\n", ret);
2250 if (old_info != info)
2251 fbcon_del_cursor_timer(old_info);
2254 if (fbcon_is_inactive(vc, info) ||
2255 ops->blank_state != FB_BLANK_UNBLANK)
2256 fbcon_del_cursor_timer(info);
2257 else
2258 fbcon_add_cursor_timer(info);
2260 set_blitting_type(vc, info);
2261 ops->cursor_reset = 1;
2263 if (ops->rotate_font && ops->rotate_font(info, vc)) {
2264 ops->rotate = FB_ROTATE_UR;
2265 set_blitting_type(vc, info);
2268 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2269 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2271 if (p->userfont)
2272 charcnt = FNTCHARCNT(vc->vc_font.data);
2274 if (charcnt > 256)
2275 vc->vc_complement_mask <<= 1;
2277 updatescrollmode(p, info, vc);
2279 switch (p->scrollmode) {
2280 case SCROLL_WRAP_MOVE:
2281 scrollback_phys_max = p->vrows - vc->vc_rows;
2282 break;
2283 case SCROLL_PAN_MOVE:
2284 case SCROLL_PAN_REDRAW:
2285 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2286 if (scrollback_phys_max < 0)
2287 scrollback_phys_max = 0;
2288 break;
2289 default:
2290 scrollback_phys_max = 0;
2291 break;
2294 scrollback_max = 0;
2295 scrollback_current = 0;
2297 if (!fbcon_is_inactive(vc, info)) {
2298 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2299 ops->update_start(info);
2302 fbcon_set_palette(vc, color_table);
2303 fbcon_clear_margins(vc, 0);
2305 if (logo_shown == FBCON_LOGO_DRAW) {
2307 logo_shown = fg_console;
2308 /* This is protected above by initmem_freed */
2309 fb_show_logo(info, ops->rotate);
2310 update_region(vc,
2311 vc->vc_origin + vc->vc_size_row * vc->vc_top,
2312 vc->vc_size_row * (vc->vc_bottom -
2313 vc->vc_top) / 2);
2314 return 0;
2316 return 1;
2319 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2320 int blank)
2322 struct fb_event event;
2324 if (blank) {
2325 unsigned short charmask = vc->vc_hi_font_mask ?
2326 0x1ff : 0xff;
2327 unsigned short oldc;
2329 oldc = vc->vc_video_erase_char;
2330 vc->vc_video_erase_char &= charmask;
2331 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2332 vc->vc_video_erase_char = oldc;
2336 if (!lock_fb_info(info))
2337 return;
2338 event.info = info;
2339 event.data = &blank;
2340 fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
2341 unlock_fb_info(info);
2344 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2346 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2347 struct fbcon_ops *ops = info->fbcon_par;
2349 if (mode_switch) {
2350 struct fb_var_screeninfo var = info->var;
2352 ops->graphics = 1;
2354 if (!blank) {
2355 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2356 fb_set_var(info, &var);
2357 ops->graphics = 0;
2358 ops->var = info->var;
2362 if (!fbcon_is_inactive(vc, info)) {
2363 if (ops->blank_state != blank) {
2364 ops->blank_state = blank;
2365 fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2366 ops->cursor_flash = (!blank);
2368 if (!(info->flags & FBINFO_MISC_USEREVENT))
2369 if (fb_blank(info, blank))
2370 fbcon_generic_blank(vc, info, blank);
2373 if (!blank)
2374 update_screen(vc);
2377 if (mode_switch || fbcon_is_inactive(vc, info) ||
2378 ops->blank_state != FB_BLANK_UNBLANK)
2379 fbcon_del_cursor_timer(info);
2380 else
2381 fbcon_add_cursor_timer(info);
2383 return 0;
2386 static int fbcon_debug_enter(struct vc_data *vc)
2388 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2389 struct fbcon_ops *ops = info->fbcon_par;
2391 ops->save_graphics = ops->graphics;
2392 ops->graphics = 0;
2393 if (info->fbops->fb_debug_enter)
2394 info->fbops->fb_debug_enter(info);
2395 fbcon_set_palette(vc, color_table);
2396 return 0;
2399 static int fbcon_debug_leave(struct vc_data *vc)
2401 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2402 struct fbcon_ops *ops = info->fbcon_par;
2404 ops->graphics = ops->save_graphics;
2405 if (info->fbops->fb_debug_leave)
2406 info->fbops->fb_debug_leave(info);
2407 return 0;
2410 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2412 u8 *fontdata = vc->vc_font.data;
2413 u8 *data = font->data;
2414 int i, j;
2416 font->width = vc->vc_font.width;
2417 font->height = vc->vc_font.height;
2418 font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2419 if (!font->data)
2420 return 0;
2422 if (font->width <= 8) {
2423 j = vc->vc_font.height;
2424 for (i = 0; i < font->charcount; i++) {
2425 memcpy(data, fontdata, j);
2426 memset(data + j, 0, 32 - j);
2427 data += 32;
2428 fontdata += j;
2430 } else if (font->width <= 16) {
2431 j = vc->vc_font.height * 2;
2432 for (i = 0; i < font->charcount; i++) {
2433 memcpy(data, fontdata, j);
2434 memset(data + j, 0, 64 - j);
2435 data += 64;
2436 fontdata += j;
2438 } else if (font->width <= 24) {
2439 for (i = 0; i < font->charcount; i++) {
2440 for (j = 0; j < vc->vc_font.height; j++) {
2441 *data++ = fontdata[0];
2442 *data++ = fontdata[1];
2443 *data++ = fontdata[2];
2444 fontdata += sizeof(u32);
2446 memset(data, 0, 3 * (32 - j));
2447 data += 3 * (32 - j);
2449 } else {
2450 j = vc->vc_font.height * 4;
2451 for (i = 0; i < font->charcount; i++) {
2452 memcpy(data, fontdata, j);
2453 memset(data + j, 0, 128 - j);
2454 data += 128;
2455 fontdata += j;
2458 return 0;
2461 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2462 const u8 * data, int userfont)
2464 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2465 struct fbcon_ops *ops = info->fbcon_par;
2466 struct display *p = &fb_display[vc->vc_num];
2467 int resize;
2468 int cnt;
2469 char *old_data = NULL;
2471 if (CON_IS_VISIBLE(vc) && softback_lines)
2472 fbcon_set_origin(vc);
2474 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2475 if (p->userfont)
2476 old_data = vc->vc_font.data;
2477 if (userfont)
2478 cnt = FNTCHARCNT(data);
2479 else
2480 cnt = 256;
2481 vc->vc_font.data = (void *)(p->fontdata = data);
2482 if ((p->userfont = userfont))
2483 REFCOUNT(data)++;
2484 vc->vc_font.width = w;
2485 vc->vc_font.height = h;
2486 if (vc->vc_hi_font_mask && cnt == 256) {
2487 vc->vc_hi_font_mask = 0;
2488 if (vc->vc_can_do_color) {
2489 vc->vc_complement_mask >>= 1;
2490 vc->vc_s_complement_mask >>= 1;
2493 /* ++Edmund: reorder the attribute bits */
2494 if (vc->vc_can_do_color) {
2495 unsigned short *cp =
2496 (unsigned short *) vc->vc_origin;
2497 int count = vc->vc_screenbuf_size / 2;
2498 unsigned short c;
2499 for (; count > 0; count--, cp++) {
2500 c = scr_readw(cp);
2501 scr_writew(((c & 0xfe00) >> 1) |
2502 (c & 0xff), cp);
2504 c = vc->vc_video_erase_char;
2505 vc->vc_video_erase_char =
2506 ((c & 0xfe00) >> 1) | (c & 0xff);
2507 vc->vc_attr >>= 1;
2509 } else if (!vc->vc_hi_font_mask && cnt == 512) {
2510 vc->vc_hi_font_mask = 0x100;
2511 if (vc->vc_can_do_color) {
2512 vc->vc_complement_mask <<= 1;
2513 vc->vc_s_complement_mask <<= 1;
2516 /* ++Edmund: reorder the attribute bits */
2518 unsigned short *cp =
2519 (unsigned short *) vc->vc_origin;
2520 int count = vc->vc_screenbuf_size / 2;
2521 unsigned short c;
2522 for (; count > 0; count--, cp++) {
2523 unsigned short newc;
2524 c = scr_readw(cp);
2525 if (vc->vc_can_do_color)
2526 newc =
2527 ((c & 0xff00) << 1) | (c &
2528 0xff);
2529 else
2530 newc = c & ~0x100;
2531 scr_writew(newc, cp);
2533 c = vc->vc_video_erase_char;
2534 if (vc->vc_can_do_color) {
2535 vc->vc_video_erase_char =
2536 ((c & 0xff00) << 1) | (c & 0xff);
2537 vc->vc_attr <<= 1;
2538 } else
2539 vc->vc_video_erase_char = c & ~0x100;
2544 if (resize) {
2545 int cols, rows;
2547 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2548 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2549 cols /= w;
2550 rows /= h;
2551 vc_resize(vc, cols, rows);
2552 if (CON_IS_VISIBLE(vc) && softback_buf)
2553 fbcon_update_softback(vc);
2554 } else if (CON_IS_VISIBLE(vc)
2555 && vc->vc_mode == KD_TEXT) {
2556 fbcon_clear_margins(vc, 0);
2557 update_screen(vc);
2560 if (old_data && (--REFCOUNT(old_data) == 0))
2561 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2562 return 0;
2565 static int fbcon_copy_font(struct vc_data *vc, int con)
2567 struct display *od = &fb_display[con];
2568 struct console_font *f = &vc->vc_font;
2570 if (od->fontdata == f->data)
2571 return 0; /* already the same font... */
2572 return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2576 * User asked to set font; we are guaranteed that
2577 * a) width and height are in range 1..32
2578 * b) charcount does not exceed 512
2579 * but lets not assume that, since someone might someday want to use larger
2580 * fonts. And charcount of 512 is small for unicode support.
2582 * However, user space gives the font in 32 rows , regardless of
2583 * actual font height. So a new API is needed if support for larger fonts
2584 * is ever implemented.
2587 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2589 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2590 unsigned charcount = font->charcount;
2591 int w = font->width;
2592 int h = font->height;
2593 int size;
2594 int i, csum;
2595 u8 *new_data, *data = font->data;
2596 int pitch = (font->width+7) >> 3;
2598 /* Is there a reason why fbconsole couldn't handle any charcount >256?
2599 * If not this check should be changed to charcount < 256 */
2600 if (charcount != 256 && charcount != 512)
2601 return -EINVAL;
2603 /* Make sure drawing engine can handle the font */
2604 if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2605 !(info->pixmap.blit_y & (1 << (font->height - 1))))
2606 return -EINVAL;
2608 /* Make sure driver can handle the font length */
2609 if (fbcon_invalid_charcount(info, charcount))
2610 return -EINVAL;
2612 size = h * pitch * charcount;
2614 new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2616 if (!new_data)
2617 return -ENOMEM;
2619 new_data += FONT_EXTRA_WORDS * sizeof(int);
2620 FNTSIZE(new_data) = size;
2621 FNTCHARCNT(new_data) = charcount;
2622 REFCOUNT(new_data) = 0; /* usage counter */
2623 for (i=0; i< charcount; i++) {
2624 memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
2627 /* Since linux has a nice crc32 function use it for counting font
2628 * checksums. */
2629 csum = crc32(0, new_data, size);
2631 FNTSUM(new_data) = csum;
2632 /* Check if the same font is on some other console already */
2633 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2634 struct vc_data *tmp = vc_cons[i].d;
2636 if (fb_display[i].userfont &&
2637 fb_display[i].fontdata &&
2638 FNTSUM(fb_display[i].fontdata) == csum &&
2639 FNTSIZE(fb_display[i].fontdata) == size &&
2640 tmp->vc_font.width == w &&
2641 !memcmp(fb_display[i].fontdata, new_data, size)) {
2642 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2643 new_data = (u8 *)fb_display[i].fontdata;
2644 break;
2647 return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2650 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2652 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2653 const struct font_desc *f;
2655 if (!name)
2656 f = get_default_font(info->var.xres, info->var.yres,
2657 info->pixmap.blit_x, info->pixmap.blit_y);
2658 else if (!(f = find_font(name)))
2659 return -ENOENT;
2661 font->width = f->width;
2662 font->height = f->height;
2663 return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2666 static u16 palette_red[16];
2667 static u16 palette_green[16];
2668 static u16 palette_blue[16];
2670 static struct fb_cmap palette_cmap = {
2671 0, 16, palette_red, palette_green, palette_blue, NULL
2674 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2676 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2677 int i, j, k, depth;
2678 u8 val;
2680 if (fbcon_is_inactive(vc, info))
2681 return -EINVAL;
2683 if (!CON_IS_VISIBLE(vc))
2684 return 0;
2686 depth = fb_get_color_depth(&info->var, &info->fix);
2687 if (depth > 3) {
2688 for (i = j = 0; i < 16; i++) {
2689 k = table[i];
2690 val = vc->vc_palette[j++];
2691 palette_red[k] = (val << 8) | val;
2692 val = vc->vc_palette[j++];
2693 palette_green[k] = (val << 8) | val;
2694 val = vc->vc_palette[j++];
2695 palette_blue[k] = (val << 8) | val;
2697 palette_cmap.len = 16;
2698 palette_cmap.start = 0;
2700 * If framebuffer is capable of less than 16 colors,
2701 * use default palette of fbcon.
2703 } else
2704 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2706 return fb_set_cmap(&palette_cmap, info);
2709 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2711 unsigned long p;
2712 int line;
2714 if (vc->vc_num != fg_console || !softback_lines)
2715 return (u16 *) (vc->vc_origin + offset);
2716 line = offset / vc->vc_size_row;
2717 if (line >= softback_lines)
2718 return (u16 *) (vc->vc_origin + offset -
2719 softback_lines * vc->vc_size_row);
2720 p = softback_curr + offset;
2721 if (p >= softback_end)
2722 p += softback_buf - softback_end;
2723 return (u16 *) p;
2726 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2727 int *px, int *py)
2729 unsigned long ret;
2730 int x, y;
2732 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2733 unsigned long offset = (pos - vc->vc_origin) / 2;
2735 x = offset % vc->vc_cols;
2736 y = offset / vc->vc_cols;
2737 if (vc->vc_num == fg_console)
2738 y += softback_lines;
2739 ret = pos + (vc->vc_cols - x) * 2;
2740 } else if (vc->vc_num == fg_console && softback_lines) {
2741 unsigned long offset = pos - softback_curr;
2743 if (pos < softback_curr)
2744 offset += softback_end - softback_buf;
2745 offset /= 2;
2746 x = offset % vc->vc_cols;
2747 y = offset / vc->vc_cols;
2748 ret = pos + (vc->vc_cols - x) * 2;
2749 if (ret == softback_end)
2750 ret = softback_buf;
2751 if (ret == softback_in)
2752 ret = vc->vc_origin;
2753 } else {
2754 /* Should not happen */
2755 x = y = 0;
2756 ret = vc->vc_origin;
2758 if (px)
2759 *px = x;
2760 if (py)
2761 *py = y;
2762 return ret;
2765 /* As we might be inside of softback, we may work with non-contiguous buffer,
2766 that's why we have to use a separate routine. */
2767 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2769 while (cnt--) {
2770 u16 a = scr_readw(p);
2771 if (!vc->vc_can_do_color)
2772 a ^= 0x0800;
2773 else if (vc->vc_hi_font_mask == 0x100)
2774 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2775 (((a) & 0x0e00) << 4);
2776 else
2777 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2778 (((a) & 0x0700) << 4);
2779 scr_writew(a, p++);
2780 if (p == (u16 *) softback_end)
2781 p = (u16 *) softback_buf;
2782 if (p == (u16 *) softback_in)
2783 p = (u16 *) vc->vc_origin;
2787 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2789 struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2790 struct fbcon_ops *ops = info->fbcon_par;
2791 struct display *disp = &fb_display[fg_console];
2792 int offset, limit, scrollback_old;
2794 if (softback_top) {
2795 if (vc->vc_num != fg_console)
2796 return 0;
2797 if (vc->vc_mode != KD_TEXT || !lines)
2798 return 0;
2799 if (logo_shown >= 0) {
2800 struct vc_data *conp2 = vc_cons[logo_shown].d;
2802 if (conp2->vc_top == logo_lines
2803 && conp2->vc_bottom == conp2->vc_rows)
2804 conp2->vc_top = 0;
2805 if (logo_shown == vc->vc_num) {
2806 unsigned long p, q;
2807 int i;
2809 p = softback_in;
2810 q = vc->vc_origin +
2811 logo_lines * vc->vc_size_row;
2812 for (i = 0; i < logo_lines; i++) {
2813 if (p == softback_top)
2814 break;
2815 if (p == softback_buf)
2816 p = softback_end;
2817 p -= vc->vc_size_row;
2818 q -= vc->vc_size_row;
2819 scr_memcpyw((u16 *) q, (u16 *) p,
2820 vc->vc_size_row);
2822 softback_in = softback_curr = p;
2823 update_region(vc, vc->vc_origin,
2824 logo_lines * vc->vc_cols);
2826 logo_shown = FBCON_LOGO_CANSHOW;
2828 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2829 fbcon_redraw_softback(vc, disp, lines);
2830 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2831 return 0;
2834 if (!scrollback_phys_max)
2835 return -ENOSYS;
2837 scrollback_old = scrollback_current;
2838 scrollback_current -= lines;
2839 if (scrollback_current < 0)
2840 scrollback_current = 0;
2841 else if (scrollback_current > scrollback_max)
2842 scrollback_current = scrollback_max;
2843 if (scrollback_current == scrollback_old)
2844 return 0;
2846 if (fbcon_is_inactive(vc, info))
2847 return 0;
2849 fbcon_cursor(vc, CM_ERASE);
2851 offset = disp->yscroll - scrollback_current;
2852 limit = disp->vrows;
2853 switch (disp->scrollmode) {
2854 case SCROLL_WRAP_MOVE:
2855 info->var.vmode |= FB_VMODE_YWRAP;
2856 break;
2857 case SCROLL_PAN_MOVE:
2858 case SCROLL_PAN_REDRAW:
2859 limit -= vc->vc_rows;
2860 info->var.vmode &= ~FB_VMODE_YWRAP;
2861 break;
2863 if (offset < 0)
2864 offset += limit;
2865 else if (offset >= limit)
2866 offset -= limit;
2868 ops->var.xoffset = 0;
2869 ops->var.yoffset = offset * vc->vc_font.height;
2870 ops->update_start(info);
2872 if (!scrollback_current)
2873 fbcon_cursor(vc, CM_DRAW);
2874 return 0;
2877 static int fbcon_set_origin(struct vc_data *vc)
2879 if (softback_lines)
2880 fbcon_scrolldelta(vc, softback_lines);
2881 return 0;
2884 static void fbcon_suspended(struct fb_info *info)
2886 struct vc_data *vc = NULL;
2887 struct fbcon_ops *ops = info->fbcon_par;
2889 if (!ops || ops->currcon < 0)
2890 return;
2891 vc = vc_cons[ops->currcon].d;
2893 /* Clear cursor, restore saved data */
2894 fbcon_cursor(vc, CM_ERASE);
2897 static void fbcon_resumed(struct fb_info *info)
2899 struct vc_data *vc;
2900 struct fbcon_ops *ops = info->fbcon_par;
2902 if (!ops || ops->currcon < 0)
2903 return;
2904 vc = vc_cons[ops->currcon].d;
2906 update_screen(vc);
2909 static void fbcon_modechanged(struct fb_info *info)
2911 struct fbcon_ops *ops = info->fbcon_par;
2912 struct vc_data *vc;
2913 struct display *p;
2914 int rows, cols;
2916 if (!ops || ops->currcon < 0)
2917 return;
2918 vc = vc_cons[ops->currcon].d;
2919 if (vc->vc_mode != KD_TEXT ||
2920 registered_fb[con2fb_map[ops->currcon]] != info)
2921 return;
2923 p = &fb_display[vc->vc_num];
2924 set_blitting_type(vc, info);
2926 if (CON_IS_VISIBLE(vc)) {
2927 var_to_display(p, &info->var, info);
2928 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2929 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2930 cols /= vc->vc_font.width;
2931 rows /= vc->vc_font.height;
2932 vc_resize(vc, cols, rows);
2933 updatescrollmode(p, info, vc);
2934 scrollback_max = 0;
2935 scrollback_current = 0;
2937 if (!fbcon_is_inactive(vc, info)) {
2938 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2939 ops->update_start(info);
2942 fbcon_set_palette(vc, color_table);
2943 update_screen(vc);
2944 if (softback_buf)
2945 fbcon_update_softback(vc);
2949 static void fbcon_set_all_vcs(struct fb_info *info)
2951 struct fbcon_ops *ops = info->fbcon_par;
2952 struct vc_data *vc;
2953 struct display *p;
2954 int i, rows, cols, fg = -1;
2956 if (!ops || ops->currcon < 0)
2957 return;
2959 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2960 vc = vc_cons[i].d;
2961 if (!vc || vc->vc_mode != KD_TEXT ||
2962 registered_fb[con2fb_map[i]] != info)
2963 continue;
2965 if (CON_IS_VISIBLE(vc)) {
2966 fg = i;
2967 continue;
2970 p = &fb_display[vc->vc_num];
2971 set_blitting_type(vc, info);
2972 var_to_display(p, &info->var, info);
2973 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2974 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2975 cols /= vc->vc_font.width;
2976 rows /= vc->vc_font.height;
2977 vc_resize(vc, cols, rows);
2980 if (fg != -1)
2981 fbcon_modechanged(info);
2984 static int fbcon_mode_deleted(struct fb_info *info,
2985 struct fb_videomode *mode)
2987 struct fb_info *fb_info;
2988 struct display *p;
2989 int i, j, found = 0;
2991 /* before deletion, ensure that mode is not in use */
2992 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2993 j = con2fb_map[i];
2994 if (j == -1)
2995 continue;
2996 fb_info = registered_fb[j];
2997 if (fb_info != info)
2998 continue;
2999 p = &fb_display[i];
3000 if (!p || !p->mode)
3001 continue;
3002 if (fb_mode_is_equal(p->mode, mode)) {
3003 found = 1;
3004 break;
3007 return found;
3010 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
3011 static int fbcon_unbind(void)
3013 int ret;
3015 ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
3016 fbcon_is_default);
3018 if (!ret)
3019 fbcon_has_console_bind = 0;
3021 return ret;
3023 #else
3024 static inline int fbcon_unbind(void)
3026 return -EINVAL;
3028 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3030 /* called with console_lock held */
3031 static int fbcon_fb_unbind(int idx)
3033 int i, new_idx = -1, ret = 0;
3035 if (!fbcon_has_console_bind)
3036 return 0;
3038 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3039 if (con2fb_map[i] != idx &&
3040 con2fb_map[i] != -1) {
3041 new_idx = i;
3042 break;
3046 if (new_idx != -1) {
3047 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3048 if (con2fb_map[i] == idx)
3049 set_con2fb_map(i, new_idx, 0);
3051 } else
3052 ret = fbcon_unbind();
3054 return ret;
3057 /* called with console_lock held */
3058 static int fbcon_fb_unregistered(struct fb_info *info)
3060 int i, idx;
3062 idx = info->node;
3063 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3064 if (con2fb_map[i] == idx)
3065 con2fb_map[i] = -1;
3068 if (idx == info_idx) {
3069 info_idx = -1;
3071 for (i = 0; i < FB_MAX; i++) {
3072 if (registered_fb[i] != NULL) {
3073 info_idx = i;
3074 break;
3079 if (info_idx != -1) {
3080 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3081 if (con2fb_map[i] == -1)
3082 con2fb_map[i] = info_idx;
3086 if (primary_device == idx)
3087 primary_device = -1;
3089 if (!num_registered_fb)
3090 do_unregister_con_driver(&fb_con);
3092 return 0;
3095 /* called with console_lock held */
3096 static void fbcon_remap_all(int idx)
3098 int i;
3099 for (i = first_fb_vc; i <= last_fb_vc; i++)
3100 set_con2fb_map(i, idx, 0);
3102 if (con_is_bound(&fb_con)) {
3103 printk(KERN_INFO "fbcon: Remapping primary device, "
3104 "fb%i, to tty %i-%i\n", idx,
3105 first_fb_vc + 1, last_fb_vc + 1);
3106 info_idx = idx;
3110 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3111 static void fbcon_select_primary(struct fb_info *info)
3113 if (!map_override && primary_device == -1 &&
3114 fb_is_primary_device(info)) {
3115 int i;
3117 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3118 info->fix.id, info->node);
3119 primary_device = info->node;
3121 for (i = first_fb_vc; i <= last_fb_vc; i++)
3122 con2fb_map_boot[i] = primary_device;
3124 if (con_is_bound(&fb_con)) {
3125 printk(KERN_INFO "fbcon: Remapping primary device, "
3126 "fb%i, to tty %i-%i\n", info->node,
3127 first_fb_vc + 1, last_fb_vc + 1);
3128 info_idx = primary_device;
3133 #else
3134 static inline void fbcon_select_primary(struct fb_info *info)
3136 return;
3138 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3140 /* called with console_lock held */
3141 static int fbcon_fb_registered(struct fb_info *info)
3143 int ret = 0, i, idx;
3145 idx = info->node;
3146 fbcon_select_primary(info);
3148 if (info_idx == -1) {
3149 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3150 if (con2fb_map_boot[i] == idx) {
3151 info_idx = idx;
3152 break;
3156 if (info_idx != -1)
3157 ret = do_fbcon_takeover(1);
3158 } else {
3159 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3160 if (con2fb_map_boot[i] == idx)
3161 set_con2fb_map(i, idx, 0);
3165 return ret;
3168 static void fbcon_fb_blanked(struct fb_info *info, int blank)
3170 struct fbcon_ops *ops = info->fbcon_par;
3171 struct vc_data *vc;
3173 if (!ops || ops->currcon < 0)
3174 return;
3176 vc = vc_cons[ops->currcon].d;
3177 if (vc->vc_mode != KD_TEXT ||
3178 registered_fb[con2fb_map[ops->currcon]] != info)
3179 return;
3181 if (CON_IS_VISIBLE(vc)) {
3182 if (blank)
3183 do_blank_screen(0);
3184 else
3185 do_unblank_screen(0);
3187 ops->blank_state = blank;
3190 static void fbcon_new_modelist(struct fb_info *info)
3192 int i;
3193 struct vc_data *vc;
3194 struct fb_var_screeninfo var;
3195 const struct fb_videomode *mode;
3197 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3198 if (registered_fb[con2fb_map[i]] != info)
3199 continue;
3200 if (!fb_display[i].mode)
3201 continue;
3202 vc = vc_cons[i].d;
3203 display_to_var(&var, &fb_display[i]);
3204 mode = fb_find_nearest_mode(fb_display[i].mode,
3205 &info->modelist);
3206 fb_videomode_to_var(&var, mode);
3207 fbcon_set_disp(info, &var, vc->vc_num);
3211 static void fbcon_get_requirement(struct fb_info *info,
3212 struct fb_blit_caps *caps)
3214 struct vc_data *vc;
3215 struct display *p;
3217 if (caps->flags) {
3218 int i, charcnt;
3220 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3221 vc = vc_cons[i].d;
3222 if (vc && vc->vc_mode == KD_TEXT &&
3223 info->node == con2fb_map[i]) {
3224 p = &fb_display[i];
3225 caps->x |= 1 << (vc->vc_font.width - 1);
3226 caps->y |= 1 << (vc->vc_font.height - 1);
3227 charcnt = (p->userfont) ?
3228 FNTCHARCNT(p->fontdata) : 256;
3229 if (caps->len < charcnt)
3230 caps->len = charcnt;
3233 } else {
3234 vc = vc_cons[fg_console].d;
3236 if (vc && vc->vc_mode == KD_TEXT &&
3237 info->node == con2fb_map[fg_console]) {
3238 p = &fb_display[fg_console];
3239 caps->x = 1 << (vc->vc_font.width - 1);
3240 caps->y = 1 << (vc->vc_font.height - 1);
3241 caps->len = (p->userfont) ?
3242 FNTCHARCNT(p->fontdata) : 256;
3247 static int fbcon_event_notify(struct notifier_block *self,
3248 unsigned long action, void *data)
3250 struct fb_event *event = data;
3251 struct fb_info *info = event->info;
3252 struct fb_videomode *mode;
3253 struct fb_con2fbmap *con2fb;
3254 struct fb_blit_caps *caps;
3255 int idx, ret = 0;
3258 * ignore all events except driver registration and deregistration
3259 * if fbcon is not active
3261 if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3262 action == FB_EVENT_FB_UNREGISTERED))
3263 goto done;
3265 switch(action) {
3266 case FB_EVENT_SUSPEND:
3267 fbcon_suspended(info);
3268 break;
3269 case FB_EVENT_RESUME:
3270 fbcon_resumed(info);
3271 break;
3272 case FB_EVENT_MODE_CHANGE:
3273 fbcon_modechanged(info);
3274 break;
3275 case FB_EVENT_MODE_CHANGE_ALL:
3276 fbcon_set_all_vcs(info);
3277 break;
3278 case FB_EVENT_MODE_DELETE:
3279 mode = event->data;
3280 ret = fbcon_mode_deleted(info, mode);
3281 break;
3282 case FB_EVENT_FB_UNBIND:
3283 idx = info->node;
3284 ret = fbcon_fb_unbind(idx);
3285 break;
3286 case FB_EVENT_FB_REGISTERED:
3287 ret = fbcon_fb_registered(info);
3288 break;
3289 case FB_EVENT_FB_UNREGISTERED:
3290 ret = fbcon_fb_unregistered(info);
3291 break;
3292 case FB_EVENT_SET_CONSOLE_MAP:
3293 /* called with console lock held */
3294 con2fb = event->data;
3295 ret = set_con2fb_map(con2fb->console - 1,
3296 con2fb->framebuffer, 1);
3297 break;
3298 case FB_EVENT_GET_CONSOLE_MAP:
3299 con2fb = event->data;
3300 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3301 break;
3302 case FB_EVENT_BLANK:
3303 fbcon_fb_blanked(info, *(int *)event->data);
3304 break;
3305 case FB_EVENT_NEW_MODELIST:
3306 fbcon_new_modelist(info);
3307 break;
3308 case FB_EVENT_GET_REQ:
3309 caps = event->data;
3310 fbcon_get_requirement(info, caps);
3311 break;
3312 case FB_EVENT_REMAP_ALL_CONSOLE:
3313 idx = info->node;
3314 fbcon_remap_all(idx);
3315 break;
3317 done:
3318 return ret;
3322 * The console `switch' structure for the frame buffer based console
3325 static const struct consw fb_con = {
3326 .owner = THIS_MODULE,
3327 .con_startup = fbcon_startup,
3328 .con_init = fbcon_init,
3329 .con_deinit = fbcon_deinit,
3330 .con_clear = fbcon_clear,
3331 .con_putc = fbcon_putc,
3332 .con_putcs = fbcon_putcs,
3333 .con_cursor = fbcon_cursor,
3334 .con_scroll = fbcon_scroll,
3335 .con_bmove = fbcon_bmove,
3336 .con_switch = fbcon_switch,
3337 .con_blank = fbcon_blank,
3338 .con_font_set = fbcon_set_font,
3339 .con_font_get = fbcon_get_font,
3340 .con_font_default = fbcon_set_def_font,
3341 .con_font_copy = fbcon_copy_font,
3342 .con_set_palette = fbcon_set_palette,
3343 .con_scrolldelta = fbcon_scrolldelta,
3344 .con_set_origin = fbcon_set_origin,
3345 .con_invert_region = fbcon_invert_region,
3346 .con_screen_pos = fbcon_screen_pos,
3347 .con_getxy = fbcon_getxy,
3348 .con_resize = fbcon_resize,
3349 .con_debug_enter = fbcon_debug_enter,
3350 .con_debug_leave = fbcon_debug_leave,
3353 static struct notifier_block fbcon_event_notifier = {
3354 .notifier_call = fbcon_event_notify,
3357 static ssize_t store_rotate(struct device *device,
3358 struct device_attribute *attr, const char *buf,
3359 size_t count)
3361 struct fb_info *info;
3362 int rotate, idx;
3363 char **last = NULL;
3365 if (fbcon_has_exited)
3366 return count;
3368 console_lock();
3369 idx = con2fb_map[fg_console];
3371 if (idx == -1 || registered_fb[idx] == NULL)
3372 goto err;
3374 info = registered_fb[idx];
3375 rotate = simple_strtoul(buf, last, 0);
3376 fbcon_rotate(info, rotate);
3377 err:
3378 console_unlock();
3379 return count;
3382 static ssize_t store_rotate_all(struct device *device,
3383 struct device_attribute *attr,const char *buf,
3384 size_t count)
3386 struct fb_info *info;
3387 int rotate, idx;
3388 char **last = NULL;
3390 if (fbcon_has_exited)
3391 return count;
3393 console_lock();
3394 idx = con2fb_map[fg_console];
3396 if (idx == -1 || registered_fb[idx] == NULL)
3397 goto err;
3399 info = registered_fb[idx];
3400 rotate = simple_strtoul(buf, last, 0);
3401 fbcon_rotate_all(info, rotate);
3402 err:
3403 console_unlock();
3404 return count;
3407 static ssize_t show_rotate(struct device *device,
3408 struct device_attribute *attr,char *buf)
3410 struct fb_info *info;
3411 int rotate = 0, idx;
3413 if (fbcon_has_exited)
3414 return 0;
3416 console_lock();
3417 idx = con2fb_map[fg_console];
3419 if (idx == -1 || registered_fb[idx] == NULL)
3420 goto err;
3422 info = registered_fb[idx];
3423 rotate = fbcon_get_rotate(info);
3424 err:
3425 console_unlock();
3426 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3429 static ssize_t show_cursor_blink(struct device *device,
3430 struct device_attribute *attr, char *buf)
3432 struct fb_info *info;
3433 struct fbcon_ops *ops;
3434 int idx, blink = -1;
3436 if (fbcon_has_exited)
3437 return 0;
3439 console_lock();
3440 idx = con2fb_map[fg_console];
3442 if (idx == -1 || registered_fb[idx] == NULL)
3443 goto err;
3445 info = registered_fb[idx];
3446 ops = info->fbcon_par;
3448 if (!ops)
3449 goto err;
3451 blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3452 err:
3453 console_unlock();
3454 return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3457 static ssize_t store_cursor_blink(struct device *device,
3458 struct device_attribute *attr,
3459 const char *buf, size_t count)
3461 struct fb_info *info;
3462 int blink, idx;
3463 char **last = NULL;
3465 if (fbcon_has_exited)
3466 return count;
3468 console_lock();
3469 idx = con2fb_map[fg_console];
3471 if (idx == -1 || registered_fb[idx] == NULL)
3472 goto err;
3474 info = registered_fb[idx];
3476 if (!info->fbcon_par)
3477 goto err;
3479 blink = simple_strtoul(buf, last, 0);
3481 if (blink) {
3482 fbcon_cursor_noblink = 0;
3483 fbcon_add_cursor_timer(info);
3484 } else {
3485 fbcon_cursor_noblink = 1;
3486 fbcon_del_cursor_timer(info);
3489 err:
3490 console_unlock();
3491 return count;
3494 static struct device_attribute device_attrs[] = {
3495 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3496 __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3497 __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3498 store_cursor_blink),
3501 static int fbcon_init_device(void)
3503 int i, error = 0;
3505 fbcon_has_sysfs = 1;
3507 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3508 error = device_create_file(fbcon_device, &device_attrs[i]);
3510 if (error)
3511 break;
3514 if (error) {
3515 while (--i >= 0)
3516 device_remove_file(fbcon_device, &device_attrs[i]);
3518 fbcon_has_sysfs = 0;
3521 return 0;
3524 static void fbcon_start(void)
3526 if (num_registered_fb) {
3527 int i;
3529 console_lock();
3531 for (i = 0; i < FB_MAX; i++) {
3532 if (registered_fb[i] != NULL) {
3533 info_idx = i;
3534 break;
3538 console_unlock();
3539 fbcon_takeover(0);
3543 static void fbcon_exit(void)
3545 struct fb_info *info;
3546 int i, j, mapped;
3548 if (fbcon_has_exited)
3549 return;
3551 kfree((void *)softback_buf);
3552 softback_buf = 0UL;
3554 for (i = 0; i < FB_MAX; i++) {
3555 int pending = 0;
3557 mapped = 0;
3558 info = registered_fb[i];
3560 if (info == NULL)
3561 continue;
3563 if (info->queue.func)
3564 pending = cancel_work_sync(&info->queue);
3565 DPRINTK("fbcon: %s pending work\n", (pending ? "canceled" :
3566 "no"));
3568 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3569 if (con2fb_map[j] == i)
3570 mapped = 1;
3573 if (mapped) {
3574 if (info->fbops->fb_release)
3575 info->fbops->fb_release(info, 0);
3576 module_put(info->fbops->owner);
3578 if (info->fbcon_par) {
3579 struct fbcon_ops *ops = info->fbcon_par;
3581 fbcon_del_cursor_timer(info);
3582 kfree(ops->cursor_src);
3583 kfree(info->fbcon_par);
3584 info->fbcon_par = NULL;
3587 if (info->queue.func == fb_flashcursor)
3588 info->queue.func = NULL;
3592 fbcon_has_exited = 1;
3595 static int __init fb_console_init(void)
3597 int i;
3599 console_lock();
3600 fb_register_client(&fbcon_event_notifier);
3601 fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3602 "fbcon");
3604 if (IS_ERR(fbcon_device)) {
3605 printk(KERN_WARNING "Unable to create device "
3606 "for fbcon; errno = %ld\n",
3607 PTR_ERR(fbcon_device));
3608 fbcon_device = NULL;
3609 } else
3610 fbcon_init_device();
3612 for (i = 0; i < MAX_NR_CONSOLES; i++)
3613 con2fb_map[i] = -1;
3615 console_unlock();
3616 fbcon_start();
3617 return 0;
3620 module_init(fb_console_init);
3622 #ifdef MODULE
3624 static void __exit fbcon_deinit_device(void)
3626 int i;
3628 if (fbcon_has_sysfs) {
3629 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3630 device_remove_file(fbcon_device, &device_attrs[i]);
3632 fbcon_has_sysfs = 0;
3636 static void __exit fb_console_exit(void)
3638 console_lock();
3639 fb_unregister_client(&fbcon_event_notifier);
3640 fbcon_deinit_device();
3641 device_destroy(fb_class, MKDEV(0, 0));
3642 fbcon_exit();
3643 console_unlock();
3644 unregister_con_driver(&fb_con);
3647 module_exit(fb_console_exit);
3649 #endif
3651 MODULE_LICENSE("GPL");