fbcon: fix race condition between console lock and cursor timer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / console / fbcon.c
blobf8a79fca4a2293c5b1b2d8923d60f26822ea748f
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 ret = console_trylock();
378 if (ret == 0)
379 return;
381 if (ops && ops->currcon != -1)
382 vc = vc_cons[ops->currcon].d;
384 if (!vc || !CON_IS_VISIBLE(vc) ||
385 registered_fb[con2fb_map[vc->vc_num]] != info ||
386 vc->vc_deccm != 1) {
387 console_unlock();
388 return;
391 c = scr_readw((u16 *) vc->vc_pos);
392 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
393 CM_ERASE : CM_DRAW;
394 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
395 get_color(vc, info, c, 0));
396 console_unlock();
399 static void cursor_timer_handler(unsigned long dev_addr)
401 struct fb_info *info = (struct fb_info *) dev_addr;
402 struct fbcon_ops *ops = info->fbcon_par;
404 schedule_work(&info->queue);
405 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
408 static void fbcon_add_cursor_timer(struct fb_info *info)
410 struct fbcon_ops *ops = info->fbcon_par;
412 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
413 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
414 !fbcon_cursor_noblink) {
415 if (!info->queue.func)
416 INIT_WORK(&info->queue, fb_flashcursor);
418 init_timer(&ops->cursor_timer);
419 ops->cursor_timer.function = cursor_timer_handler;
420 ops->cursor_timer.expires = jiffies + HZ / 5;
421 ops->cursor_timer.data = (unsigned long ) info;
422 add_timer(&ops->cursor_timer);
423 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
427 static void fbcon_del_cursor_timer(struct fb_info *info)
429 struct fbcon_ops *ops = info->fbcon_par;
431 if (info->queue.func == fb_flashcursor &&
432 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
433 del_timer_sync(&ops->cursor_timer);
434 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
438 #ifndef MODULE
439 static int __init fb_console_setup(char *this_opt)
441 char *options;
442 int i, j;
444 if (!this_opt || !*this_opt)
445 return 1;
447 while ((options = strsep(&this_opt, ",")) != NULL) {
448 if (!strncmp(options, "font:", 5))
449 strcpy(fontname, options + 5);
451 if (!strncmp(options, "scrollback:", 11)) {
452 options += 11;
453 if (*options) {
454 fbcon_softback_size = simple_strtoul(options, &options, 0);
455 if (*options == 'k' || *options == 'K') {
456 fbcon_softback_size *= 1024;
457 options++;
459 if (*options != ',')
460 return 1;
461 options++;
462 } else
463 return 1;
466 if (!strncmp(options, "map:", 4)) {
467 options += 4;
468 if (*options) {
469 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
470 if (!options[j])
471 j = 0;
472 con2fb_map_boot[i] =
473 (options[j++]-'0') % FB_MAX;
476 fbcon_map_override();
479 return 1;
482 if (!strncmp(options, "vc:", 3)) {
483 options += 3;
484 if (*options)
485 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
486 if (first_fb_vc < 0)
487 first_fb_vc = 0;
488 if (*options++ == '-')
489 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
490 fbcon_is_default = 0;
493 if (!strncmp(options, "rotate:", 7)) {
494 options += 7;
495 if (*options)
496 initial_rotation = simple_strtoul(options, &options, 0);
497 if (initial_rotation > 3)
498 initial_rotation = 0;
501 return 1;
504 __setup("fbcon=", fb_console_setup);
505 #endif
507 static int search_fb_in_map(int idx)
509 int i, retval = 0;
511 for (i = first_fb_vc; i <= last_fb_vc; i++) {
512 if (con2fb_map[i] == idx)
513 retval = 1;
515 return retval;
518 static int search_for_mapped_con(void)
520 int i, retval = 0;
522 for (i = first_fb_vc; i <= last_fb_vc; i++) {
523 if (con2fb_map[i] != -1)
524 retval = 1;
526 return retval;
529 static int fbcon_takeover(int show_logo)
531 int err, i;
533 if (!num_registered_fb)
534 return -ENODEV;
536 if (!show_logo)
537 logo_shown = FBCON_LOGO_DONTSHOW;
539 for (i = first_fb_vc; i <= last_fb_vc; i++)
540 con2fb_map[i] = info_idx;
542 err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
543 fbcon_is_default);
545 if (err) {
546 for (i = first_fb_vc; i <= last_fb_vc; i++) {
547 con2fb_map[i] = -1;
549 info_idx = -1;
550 } else {
551 fbcon_has_console_bind = 1;
554 return err;
557 #ifdef MODULE
558 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
559 int cols, int rows, int new_cols, int new_rows)
561 logo_shown = FBCON_LOGO_DONTSHOW;
563 #else
564 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
565 int cols, int rows, int new_cols, int new_rows)
567 /* Need to make room for the logo */
568 struct fbcon_ops *ops = info->fbcon_par;
569 int cnt, erase = vc->vc_video_erase_char, step;
570 unsigned short *save = NULL, *r, *q;
571 int logo_height;
573 if (info->flags & FBINFO_MODULE) {
574 logo_shown = FBCON_LOGO_DONTSHOW;
575 return;
579 * remove underline attribute from erase character
580 * if black and white framebuffer.
582 if (fb_get_color_depth(&info->var, &info->fix) == 1)
583 erase &= ~0x400;
584 logo_height = fb_prepare_logo(info, ops->rotate);
585 logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
586 q = (unsigned short *) (vc->vc_origin +
587 vc->vc_size_row * rows);
588 step = logo_lines * cols;
589 for (r = q - logo_lines * cols; r < q; r++)
590 if (scr_readw(r) != vc->vc_video_erase_char)
591 break;
592 if (r != q && new_rows >= rows + logo_lines) {
593 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
594 if (save) {
595 int i = cols < new_cols ? cols : new_cols;
596 scr_memsetw(save, erase, logo_lines * new_cols * 2);
597 r = q - step;
598 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
599 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
600 r = q;
603 if (r == q) {
604 /* We can scroll screen down */
605 r = q - step - cols;
606 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
607 scr_memcpyw(r + step, r, vc->vc_size_row);
608 r -= cols;
610 if (!save) {
611 int lines;
612 if (vc->vc_y + logo_lines >= rows)
613 lines = rows - vc->vc_y - 1;
614 else
615 lines = logo_lines;
616 vc->vc_y += lines;
617 vc->vc_pos += lines * vc->vc_size_row;
620 scr_memsetw((unsigned short *) vc->vc_origin,
621 erase,
622 vc->vc_size_row * logo_lines);
624 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
625 fbcon_clear_margins(vc, 0);
626 update_screen(vc);
629 if (save) {
630 q = (unsigned short *) (vc->vc_origin +
631 vc->vc_size_row *
632 rows);
633 scr_memcpyw(q, save, logo_lines * new_cols * 2);
634 vc->vc_y += logo_lines;
635 vc->vc_pos += logo_lines * vc->vc_size_row;
636 kfree(save);
639 if (logo_lines > vc->vc_bottom) {
640 logo_shown = FBCON_LOGO_CANSHOW;
641 printk(KERN_INFO
642 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
643 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
644 logo_shown = FBCON_LOGO_DRAW;
645 vc->vc_top = logo_lines;
648 #endif /* MODULE */
650 #ifdef CONFIG_FB_TILEBLITTING
651 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
653 struct fbcon_ops *ops = info->fbcon_par;
655 ops->p = &fb_display[vc->vc_num];
657 if ((info->flags & FBINFO_MISC_TILEBLITTING))
658 fbcon_set_tileops(vc, info);
659 else {
660 fbcon_set_rotation(info);
661 fbcon_set_bitops(ops);
665 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
667 int err = 0;
669 if (info->flags & FBINFO_MISC_TILEBLITTING &&
670 info->tileops->fb_get_tilemax(info) < charcount)
671 err = 1;
673 return err;
675 #else
676 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
678 struct fbcon_ops *ops = info->fbcon_par;
680 info->flags &= ~FBINFO_MISC_TILEBLITTING;
681 ops->p = &fb_display[vc->vc_num];
682 fbcon_set_rotation(info);
683 fbcon_set_bitops(ops);
686 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
688 return 0;
691 #endif /* CONFIG_MISC_TILEBLITTING */
694 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
695 int unit, int oldidx)
697 struct fbcon_ops *ops = NULL;
698 int err = 0;
700 if (!try_module_get(info->fbops->owner))
701 err = -ENODEV;
703 if (!err && info->fbops->fb_open &&
704 info->fbops->fb_open(info, 0))
705 err = -ENODEV;
707 if (!err) {
708 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
709 if (!ops)
710 err = -ENOMEM;
713 if (!err) {
714 info->fbcon_par = ops;
716 if (vc)
717 set_blitting_type(vc, info);
720 if (err) {
721 con2fb_map[unit] = oldidx;
722 module_put(info->fbops->owner);
725 return err;
728 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
729 struct fb_info *newinfo, int unit,
730 int oldidx, int found)
732 struct fbcon_ops *ops = oldinfo->fbcon_par;
733 int err = 0, ret;
735 if (oldinfo->fbops->fb_release &&
736 oldinfo->fbops->fb_release(oldinfo, 0)) {
737 con2fb_map[unit] = oldidx;
738 if (!found && newinfo->fbops->fb_release)
739 newinfo->fbops->fb_release(newinfo, 0);
740 if (!found)
741 module_put(newinfo->fbops->owner);
742 err = -ENODEV;
745 if (!err) {
746 fbcon_del_cursor_timer(oldinfo);
747 kfree(ops->cursor_state.mask);
748 kfree(ops->cursor_data);
749 kfree(ops->fontbuffer);
750 kfree(oldinfo->fbcon_par);
751 oldinfo->fbcon_par = NULL;
752 module_put(oldinfo->fbops->owner);
754 If oldinfo and newinfo are driving the same hardware,
755 the fb_release() method of oldinfo may attempt to
756 restore the hardware state. This will leave the
757 newinfo in an undefined state. Thus, a call to
758 fb_set_par() may be needed for the newinfo.
760 if (newinfo->fbops->fb_set_par) {
761 ret = newinfo->fbops->fb_set_par(newinfo);
763 if (ret)
764 printk(KERN_ERR "con2fb_release_oldinfo: "
765 "detected unhandled fb_set_par error, "
766 "error code %d\n", ret);
770 return err;
773 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
774 int unit, int show_logo)
776 struct fbcon_ops *ops = info->fbcon_par;
777 int ret;
779 ops->currcon = fg_console;
781 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT)) {
782 ret = info->fbops->fb_set_par(info);
784 if (ret)
785 printk(KERN_ERR "con2fb_init_display: detected "
786 "unhandled fb_set_par error, "
787 "error code %d\n", ret);
790 ops->flags |= FBCON_FLAGS_INIT;
791 ops->graphics = 0;
792 fbcon_set_disp(info, &info->var, unit);
794 if (show_logo) {
795 struct vc_data *fg_vc = vc_cons[fg_console].d;
796 struct fb_info *fg_info =
797 registered_fb[con2fb_map[fg_console]];
799 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
800 fg_vc->vc_rows, fg_vc->vc_cols,
801 fg_vc->vc_rows);
804 update_screen(vc_cons[fg_console].d);
808 * set_con2fb_map - map console to frame buffer device
809 * @unit: virtual console number to map
810 * @newidx: frame buffer index to map virtual console to
811 * @user: user request
813 * Maps a virtual console @unit to a frame buffer device
814 * @newidx.
816 static int set_con2fb_map(int unit, int newidx, int user)
818 struct vc_data *vc = vc_cons[unit].d;
819 int oldidx = con2fb_map[unit];
820 struct fb_info *info = registered_fb[newidx];
821 struct fb_info *oldinfo = NULL;
822 int found, err = 0;
824 if (oldidx == newidx)
825 return 0;
827 if (!info)
828 return -EINVAL;
830 if (!search_for_mapped_con() || !con_is_bound(&fb_con)) {
831 info_idx = newidx;
832 return fbcon_takeover(0);
835 if (oldidx != -1)
836 oldinfo = registered_fb[oldidx];
838 found = search_fb_in_map(newidx);
840 console_lock();
841 con2fb_map[unit] = newidx;
842 if (!err && !found)
843 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
847 * If old fb is not mapped to any of the consoles,
848 * fbcon should release it.
850 if (!err && oldinfo && !search_fb_in_map(oldidx))
851 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
852 found);
854 if (!err) {
855 int show_logo = (fg_console == 0 && !user &&
856 logo_shown != FBCON_LOGO_DONTSHOW);
858 if (!found)
859 fbcon_add_cursor_timer(info);
860 con2fb_map_boot[unit] = newidx;
861 con2fb_init_display(vc, info, unit, show_logo);
864 if (!search_fb_in_map(info_idx))
865 info_idx = newidx;
867 console_unlock();
868 return err;
872 * Low Level Operations
874 /* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
875 static int var_to_display(struct display *disp,
876 struct fb_var_screeninfo *var,
877 struct fb_info *info)
879 disp->xres_virtual = var->xres_virtual;
880 disp->yres_virtual = var->yres_virtual;
881 disp->bits_per_pixel = var->bits_per_pixel;
882 disp->grayscale = var->grayscale;
883 disp->nonstd = var->nonstd;
884 disp->accel_flags = var->accel_flags;
885 disp->height = var->height;
886 disp->width = var->width;
887 disp->red = var->red;
888 disp->green = var->green;
889 disp->blue = var->blue;
890 disp->transp = var->transp;
891 disp->rotate = var->rotate;
892 disp->mode = fb_match_mode(var, &info->modelist);
893 if (disp->mode == NULL)
894 /* This should not happen */
895 return -EINVAL;
896 return 0;
899 static void display_to_var(struct fb_var_screeninfo *var,
900 struct display *disp)
902 fb_videomode_to_var(var, disp->mode);
903 var->xres_virtual = disp->xres_virtual;
904 var->yres_virtual = disp->yres_virtual;
905 var->bits_per_pixel = disp->bits_per_pixel;
906 var->grayscale = disp->grayscale;
907 var->nonstd = disp->nonstd;
908 var->accel_flags = disp->accel_flags;
909 var->height = disp->height;
910 var->width = disp->width;
911 var->red = disp->red;
912 var->green = disp->green;
913 var->blue = disp->blue;
914 var->transp = disp->transp;
915 var->rotate = disp->rotate;
918 static const char *fbcon_startup(void)
920 const char *display_desc = "frame buffer device";
921 struct display *p = &fb_display[fg_console];
922 struct vc_data *vc = vc_cons[fg_console].d;
923 const struct font_desc *font = NULL;
924 struct module *owner;
925 struct fb_info *info = NULL;
926 struct fbcon_ops *ops;
927 int rows, cols;
930 * If num_registered_fb is zero, this is a call for the dummy part.
931 * The frame buffer devices weren't initialized yet.
933 if (!num_registered_fb || info_idx == -1)
934 return display_desc;
936 * Instead of blindly using registered_fb[0], we use info_idx, set by
937 * fb_console_init();
939 info = registered_fb[info_idx];
940 if (!info)
941 return NULL;
943 owner = info->fbops->owner;
944 if (!try_module_get(owner))
945 return NULL;
946 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
947 module_put(owner);
948 return NULL;
951 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
952 if (!ops) {
953 module_put(owner);
954 return NULL;
957 ops->currcon = -1;
958 ops->graphics = 1;
959 ops->cur_rotate = -1;
960 info->fbcon_par = ops;
961 p->con_rotate = initial_rotation;
962 set_blitting_type(vc, info);
964 if (info->fix.type != FB_TYPE_TEXT) {
965 if (fbcon_softback_size) {
966 if (!softback_buf) {
967 softback_buf =
968 (unsigned long)
969 kmalloc(fbcon_softback_size,
970 GFP_KERNEL);
971 if (!softback_buf) {
972 fbcon_softback_size = 0;
973 softback_top = 0;
976 } else {
977 if (softback_buf) {
978 kfree((void *) softback_buf);
979 softback_buf = 0;
980 softback_top = 0;
983 if (softback_buf)
984 softback_in = softback_top = softback_curr =
985 softback_buf;
986 softback_lines = 0;
989 /* Setup default font */
990 if (!p->fontdata) {
991 if (!fontname[0] || !(font = find_font(fontname)))
992 font = get_default_font(info->var.xres,
993 info->var.yres,
994 info->pixmap.blit_x,
995 info->pixmap.blit_y);
996 vc->vc_font.width = font->width;
997 vc->vc_font.height = font->height;
998 vc->vc_font.data = (void *)(p->fontdata = font->data);
999 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1002 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1003 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1004 cols /= vc->vc_font.width;
1005 rows /= vc->vc_font.height;
1006 vc_resize(vc, cols, rows);
1008 DPRINTK("mode: %s\n", info->fix.id);
1009 DPRINTK("visual: %d\n", info->fix.visual);
1010 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1011 info->var.yres,
1012 info->var.bits_per_pixel);
1014 fbcon_add_cursor_timer(info);
1015 fbcon_has_exited = 0;
1016 return display_desc;
1019 static void fbcon_init(struct vc_data *vc, int init)
1021 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1022 struct fbcon_ops *ops;
1023 struct vc_data **default_mode = vc->vc_display_fg;
1024 struct vc_data *svc = *default_mode;
1025 struct display *t, *p = &fb_display[vc->vc_num];
1026 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
1027 int cap, ret;
1029 if (info_idx == -1 || info == NULL)
1030 return;
1032 cap = info->flags;
1034 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1035 (info->fix.type == FB_TYPE_TEXT))
1036 logo = 0;
1038 if (var_to_display(p, &info->var, info))
1039 return;
1041 if (!info->fbcon_par)
1042 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1044 /* If we are not the first console on this
1045 fb, copy the font from that console */
1046 t = &fb_display[fg_console];
1047 if (!p->fontdata) {
1048 if (t->fontdata) {
1049 struct vc_data *fvc = vc_cons[fg_console].d;
1051 vc->vc_font.data = (void *)(p->fontdata =
1052 fvc->vc_font.data);
1053 vc->vc_font.width = fvc->vc_font.width;
1054 vc->vc_font.height = fvc->vc_font.height;
1055 p->userfont = t->userfont;
1057 if (p->userfont)
1058 REFCOUNT(p->fontdata)++;
1059 } else {
1060 const struct font_desc *font = NULL;
1062 if (!fontname[0] || !(font = find_font(fontname)))
1063 font = get_default_font(info->var.xres,
1064 info->var.yres,
1065 info->pixmap.blit_x,
1066 info->pixmap.blit_y);
1067 vc->vc_font.width = font->width;
1068 vc->vc_font.height = font->height;
1069 vc->vc_font.data = (void *)(p->fontdata = font->data);
1070 vc->vc_font.charcount = 256; /* FIXME Need to
1071 support more fonts */
1075 if (p->userfont)
1076 charcnt = FNTCHARCNT(p->fontdata);
1078 vc->vc_panic_force_write = !!(info->flags & FBINFO_CAN_FORCE_OUTPUT);
1079 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1080 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1081 if (charcnt == 256) {
1082 vc->vc_hi_font_mask = 0;
1083 } else {
1084 vc->vc_hi_font_mask = 0x100;
1085 if (vc->vc_can_do_color)
1086 vc->vc_complement_mask <<= 1;
1089 if (!*svc->vc_uni_pagedir_loc)
1090 con_set_default_unimap(svc);
1091 if (!*vc->vc_uni_pagedir_loc)
1092 con_copy_unimap(vc, svc);
1094 ops = info->fbcon_par;
1095 p->con_rotate = initial_rotation;
1096 set_blitting_type(vc, info);
1098 cols = vc->vc_cols;
1099 rows = vc->vc_rows;
1100 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1101 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1102 new_cols /= vc->vc_font.width;
1103 new_rows /= vc->vc_font.height;
1106 * We must always set the mode. The mode of the previous console
1107 * driver could be in the same resolution but we are using different
1108 * hardware so we have to initialize the hardware.
1110 * We need to do it in fbcon_init() to prevent screen corruption.
1112 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
1113 if (info->fbops->fb_set_par &&
1114 !(ops->flags & FBCON_FLAGS_INIT)) {
1115 ret = info->fbops->fb_set_par(info);
1117 if (ret)
1118 printk(KERN_ERR "fbcon_init: detected "
1119 "unhandled fb_set_par error, "
1120 "error code %d\n", ret);
1123 ops->flags |= FBCON_FLAGS_INIT;
1126 ops->graphics = 0;
1128 if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1129 !(cap & FBINFO_HWACCEL_DISABLED))
1130 p->scrollmode = SCROLL_MOVE;
1131 else /* default to something safe */
1132 p->scrollmode = SCROLL_REDRAW;
1135 * ++guenther: console.c:vc_allocate() relies on initializing
1136 * vc_{cols,rows}, but we must not set those if we are only
1137 * resizing the console.
1139 if (init) {
1140 vc->vc_cols = new_cols;
1141 vc->vc_rows = new_rows;
1142 } else
1143 vc_resize(vc, new_cols, new_rows);
1145 if (logo)
1146 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1148 if (vc == svc && softback_buf)
1149 fbcon_update_softback(vc);
1151 if (ops->rotate_font && ops->rotate_font(info, vc)) {
1152 ops->rotate = FB_ROTATE_UR;
1153 set_blitting_type(vc, info);
1156 ops->p = &fb_display[fg_console];
1159 static void fbcon_free_font(struct display *p)
1161 if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1162 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1163 p->fontdata = NULL;
1164 p->userfont = 0;
1167 static void fbcon_deinit(struct vc_data *vc)
1169 struct display *p = &fb_display[vc->vc_num];
1170 struct fb_info *info;
1171 struct fbcon_ops *ops;
1172 int idx;
1174 fbcon_free_font(p);
1175 idx = con2fb_map[vc->vc_num];
1177 if (idx == -1)
1178 goto finished;
1180 info = registered_fb[idx];
1182 if (!info)
1183 goto finished;
1185 ops = info->fbcon_par;
1187 if (!ops)
1188 goto finished;
1190 if (CON_IS_VISIBLE(vc))
1191 fbcon_del_cursor_timer(info);
1193 ops->flags &= ~FBCON_FLAGS_INIT;
1194 finished:
1196 if (!con_is_bound(&fb_con))
1197 fbcon_exit();
1199 return;
1202 /* ====================================================================== */
1204 /* fbcon_XXX routines - interface used by the world
1206 * This system is now divided into two levels because of complications
1207 * caused by hardware scrolling. Top level functions:
1209 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1211 * handles y values in range [0, scr_height-1] that correspond to real
1212 * screen positions. y_wrap shift means that first line of bitmap may be
1213 * anywhere on this display. These functions convert lineoffsets to
1214 * bitmap offsets and deal with the wrap-around case by splitting blits.
1216 * fbcon_bmove_physical_8() -- These functions fast implementations
1217 * fbcon_clear_physical_8() -- of original fbcon_XXX fns.
1218 * fbcon_putc_physical_8() -- (font width != 8) may be added later
1220 * WARNING:
1222 * At the moment fbcon_putc() cannot blit across vertical wrap boundary
1223 * Implies should only really hardware scroll in rows. Only reason for
1224 * restriction is simplicity & efficiency at the moment.
1227 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1228 int width)
1230 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1231 struct fbcon_ops *ops = info->fbcon_par;
1233 struct display *p = &fb_display[vc->vc_num];
1234 u_int y_break;
1236 if (fbcon_is_inactive(vc, info))
1237 return;
1239 if (!height || !width)
1240 return;
1242 if (sy < vc->vc_top && vc->vc_top == logo_lines)
1243 vc->vc_top = 0;
1245 /* Split blits that cross physical y_wrap boundary */
1247 y_break = p->vrows - p->yscroll;
1248 if (sy < y_break && sy + height - 1 >= y_break) {
1249 u_int b = y_break - sy;
1250 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1251 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1252 width);
1253 } else
1254 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1257 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1258 int count, int ypos, int xpos)
1260 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1261 struct display *p = &fb_display[vc->vc_num];
1262 struct fbcon_ops *ops = info->fbcon_par;
1264 if (!fbcon_is_inactive(vc, info))
1265 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1266 get_color(vc, info, scr_readw(s), 1),
1267 get_color(vc, info, scr_readw(s), 0));
1270 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1272 unsigned short chr;
1274 scr_writew(c, &chr);
1275 fbcon_putcs(vc, &chr, 1, ypos, xpos);
1278 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1280 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1281 struct fbcon_ops *ops = info->fbcon_par;
1283 if (!fbcon_is_inactive(vc, info))
1284 ops->clear_margins(vc, info, bottom_only);
1287 static void fbcon_cursor(struct vc_data *vc, int mode)
1289 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1290 struct fbcon_ops *ops = info->fbcon_par;
1291 int y;
1292 int c = scr_readw((u16 *) vc->vc_pos);
1294 if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1295 return;
1297 if (vc->vc_cursor_type & 0x10)
1298 fbcon_del_cursor_timer(info);
1299 else
1300 fbcon_add_cursor_timer(info);
1302 ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1303 if (mode & CM_SOFTBACK) {
1304 mode &= ~CM_SOFTBACK;
1305 y = softback_lines;
1306 } else {
1307 if (softback_lines)
1308 fbcon_set_origin(vc);
1309 y = 0;
1312 ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1313 get_color(vc, info, c, 0));
1314 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1317 static int scrollback_phys_max = 0;
1318 static int scrollback_max = 0;
1319 static int scrollback_current = 0;
1321 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1322 int unit)
1324 struct display *p, *t;
1325 struct vc_data **default_mode, *vc;
1326 struct vc_data *svc;
1327 struct fbcon_ops *ops = info->fbcon_par;
1328 int rows, cols, charcnt = 256;
1330 p = &fb_display[unit];
1332 if (var_to_display(p, var, info))
1333 return;
1335 vc = vc_cons[unit].d;
1337 if (!vc)
1338 return;
1340 default_mode = vc->vc_display_fg;
1341 svc = *default_mode;
1342 t = &fb_display[svc->vc_num];
1344 if (!vc->vc_font.data) {
1345 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1346 vc->vc_font.width = (*default_mode)->vc_font.width;
1347 vc->vc_font.height = (*default_mode)->vc_font.height;
1348 p->userfont = t->userfont;
1349 if (p->userfont)
1350 REFCOUNT(p->fontdata)++;
1352 if (p->userfont)
1353 charcnt = FNTCHARCNT(p->fontdata);
1355 var->activate = FB_ACTIVATE_NOW;
1356 info->var.activate = var->activate;
1357 var->yoffset = info->var.yoffset;
1358 var->xoffset = info->var.xoffset;
1359 fb_set_var(info, var);
1360 ops->var = info->var;
1361 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1362 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1363 if (charcnt == 256) {
1364 vc->vc_hi_font_mask = 0;
1365 } else {
1366 vc->vc_hi_font_mask = 0x100;
1367 if (vc->vc_can_do_color)
1368 vc->vc_complement_mask <<= 1;
1371 if (!*svc->vc_uni_pagedir_loc)
1372 con_set_default_unimap(svc);
1373 if (!*vc->vc_uni_pagedir_loc)
1374 con_copy_unimap(vc, svc);
1376 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1377 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1378 cols /= vc->vc_font.width;
1379 rows /= vc->vc_font.height;
1380 vc_resize(vc, cols, rows);
1382 if (CON_IS_VISIBLE(vc)) {
1383 update_screen(vc);
1384 if (softback_buf)
1385 fbcon_update_softback(vc);
1389 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1391 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1392 struct fbcon_ops *ops = info->fbcon_par;
1393 struct display *p = &fb_display[vc->vc_num];
1395 p->yscroll += count;
1396 if (p->yscroll >= p->vrows) /* Deal with wrap */
1397 p->yscroll -= p->vrows;
1398 ops->var.xoffset = 0;
1399 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1400 ops->var.vmode |= FB_VMODE_YWRAP;
1401 ops->update_start(info);
1402 scrollback_max += count;
1403 if (scrollback_max > scrollback_phys_max)
1404 scrollback_max = scrollback_phys_max;
1405 scrollback_current = 0;
1408 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1410 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1411 struct fbcon_ops *ops = info->fbcon_par;
1412 struct display *p = &fb_display[vc->vc_num];
1414 p->yscroll -= count;
1415 if (p->yscroll < 0) /* Deal with wrap */
1416 p->yscroll += p->vrows;
1417 ops->var.xoffset = 0;
1418 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1419 ops->var.vmode |= FB_VMODE_YWRAP;
1420 ops->update_start(info);
1421 scrollback_max -= count;
1422 if (scrollback_max < 0)
1423 scrollback_max = 0;
1424 scrollback_current = 0;
1427 static __inline__ void ypan_up(struct vc_data *vc, int count)
1429 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1430 struct display *p = &fb_display[vc->vc_num];
1431 struct fbcon_ops *ops = info->fbcon_par;
1433 p->yscroll += count;
1434 if (p->yscroll > p->vrows - vc->vc_rows) {
1435 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1436 0, 0, 0, vc->vc_rows, vc->vc_cols);
1437 p->yscroll -= p->vrows - vc->vc_rows;
1440 ops->var.xoffset = 0;
1441 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1442 ops->var.vmode &= ~FB_VMODE_YWRAP;
1443 ops->update_start(info);
1444 fbcon_clear_margins(vc, 1);
1445 scrollback_max += count;
1446 if (scrollback_max > scrollback_phys_max)
1447 scrollback_max = scrollback_phys_max;
1448 scrollback_current = 0;
1451 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1453 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1454 struct fbcon_ops *ops = info->fbcon_par;
1455 struct display *p = &fb_display[vc->vc_num];
1457 p->yscroll += count;
1459 if (p->yscroll > p->vrows - vc->vc_rows) {
1460 p->yscroll -= p->vrows - vc->vc_rows;
1461 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1464 ops->var.xoffset = 0;
1465 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1466 ops->var.vmode &= ~FB_VMODE_YWRAP;
1467 ops->update_start(info);
1468 fbcon_clear_margins(vc, 1);
1469 scrollback_max += count;
1470 if (scrollback_max > scrollback_phys_max)
1471 scrollback_max = scrollback_phys_max;
1472 scrollback_current = 0;
1475 static __inline__ void ypan_down(struct vc_data *vc, int count)
1477 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1478 struct display *p = &fb_display[vc->vc_num];
1479 struct fbcon_ops *ops = info->fbcon_par;
1481 p->yscroll -= count;
1482 if (p->yscroll < 0) {
1483 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1484 0, vc->vc_rows, vc->vc_cols);
1485 p->yscroll += p->vrows - vc->vc_rows;
1488 ops->var.xoffset = 0;
1489 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1490 ops->var.vmode &= ~FB_VMODE_YWRAP;
1491 ops->update_start(info);
1492 fbcon_clear_margins(vc, 1);
1493 scrollback_max -= count;
1494 if (scrollback_max < 0)
1495 scrollback_max = 0;
1496 scrollback_current = 0;
1499 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1501 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1502 struct fbcon_ops *ops = info->fbcon_par;
1503 struct display *p = &fb_display[vc->vc_num];
1505 p->yscroll -= count;
1507 if (p->yscroll < 0) {
1508 p->yscroll += p->vrows - vc->vc_rows;
1509 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1512 ops->var.xoffset = 0;
1513 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1514 ops->var.vmode &= ~FB_VMODE_YWRAP;
1515 ops->update_start(info);
1516 fbcon_clear_margins(vc, 1);
1517 scrollback_max -= count;
1518 if (scrollback_max < 0)
1519 scrollback_max = 0;
1520 scrollback_current = 0;
1523 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1524 long delta)
1526 int count = vc->vc_rows;
1527 unsigned short *d, *s;
1528 unsigned long n;
1529 int line = 0;
1531 d = (u16 *) softback_curr;
1532 if (d == (u16 *) softback_in)
1533 d = (u16 *) vc->vc_origin;
1534 n = softback_curr + delta * vc->vc_size_row;
1535 softback_lines -= delta;
1536 if (delta < 0) {
1537 if (softback_curr < softback_top && n < softback_buf) {
1538 n += softback_end - softback_buf;
1539 if (n < softback_top) {
1540 softback_lines -=
1541 (softback_top - n) / vc->vc_size_row;
1542 n = softback_top;
1544 } else if (softback_curr >= softback_top
1545 && n < softback_top) {
1546 softback_lines -=
1547 (softback_top - n) / vc->vc_size_row;
1548 n = softback_top;
1550 } else {
1551 if (softback_curr > softback_in && n >= softback_end) {
1552 n += softback_buf - softback_end;
1553 if (n > softback_in) {
1554 n = softback_in;
1555 softback_lines = 0;
1557 } else if (softback_curr <= softback_in && n > softback_in) {
1558 n = softback_in;
1559 softback_lines = 0;
1562 if (n == softback_curr)
1563 return;
1564 softback_curr = n;
1565 s = (u16 *) softback_curr;
1566 if (s == (u16 *) softback_in)
1567 s = (u16 *) vc->vc_origin;
1568 while (count--) {
1569 unsigned short *start;
1570 unsigned short *le;
1571 unsigned short c;
1572 int x = 0;
1573 unsigned short attr = 1;
1575 start = s;
1576 le = advance_row(s, 1);
1577 do {
1578 c = scr_readw(s);
1579 if (attr != (c & 0xff00)) {
1580 attr = c & 0xff00;
1581 if (s > start) {
1582 fbcon_putcs(vc, start, s - start,
1583 line, x);
1584 x += s - start;
1585 start = s;
1588 if (c == scr_readw(d)) {
1589 if (s > start) {
1590 fbcon_putcs(vc, start, s - start,
1591 line, x);
1592 x += s - start + 1;
1593 start = s + 1;
1594 } else {
1595 x++;
1596 start++;
1599 s++;
1600 d++;
1601 } while (s < le);
1602 if (s > start)
1603 fbcon_putcs(vc, start, s - start, line, x);
1604 line++;
1605 if (d == (u16 *) softback_end)
1606 d = (u16 *) softback_buf;
1607 if (d == (u16 *) softback_in)
1608 d = (u16 *) vc->vc_origin;
1609 if (s == (u16 *) softback_end)
1610 s = (u16 *) softback_buf;
1611 if (s == (u16 *) softback_in)
1612 s = (u16 *) vc->vc_origin;
1616 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1617 int line, int count, int dy)
1619 unsigned short *s = (unsigned short *)
1620 (vc->vc_origin + vc->vc_size_row * line);
1622 while (count--) {
1623 unsigned short *start = s;
1624 unsigned short *le = advance_row(s, 1);
1625 unsigned short c;
1626 int x = 0;
1627 unsigned short attr = 1;
1629 do {
1630 c = scr_readw(s);
1631 if (attr != (c & 0xff00)) {
1632 attr = c & 0xff00;
1633 if (s > start) {
1634 fbcon_putcs(vc, start, s - start,
1635 dy, x);
1636 x += s - start;
1637 start = s;
1640 console_conditional_schedule();
1641 s++;
1642 } while (s < le);
1643 if (s > start)
1644 fbcon_putcs(vc, start, s - start, dy, x);
1645 console_conditional_schedule();
1646 dy++;
1650 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1651 struct display *p, int line, int count, int ycount)
1653 int offset = ycount * vc->vc_cols;
1654 unsigned short *d = (unsigned short *)
1655 (vc->vc_origin + vc->vc_size_row * line);
1656 unsigned short *s = d + offset;
1657 struct fbcon_ops *ops = info->fbcon_par;
1659 while (count--) {
1660 unsigned short *start = s;
1661 unsigned short *le = advance_row(s, 1);
1662 unsigned short c;
1663 int x = 0;
1665 do {
1666 c = scr_readw(s);
1668 if (c == scr_readw(d)) {
1669 if (s > start) {
1670 ops->bmove(vc, info, line + ycount, x,
1671 line, x, 1, s-start);
1672 x += s - start + 1;
1673 start = s + 1;
1674 } else {
1675 x++;
1676 start++;
1680 scr_writew(c, d);
1681 console_conditional_schedule();
1682 s++;
1683 d++;
1684 } while (s < le);
1685 if (s > start)
1686 ops->bmove(vc, info, line + ycount, x, line, x, 1,
1687 s-start);
1688 console_conditional_schedule();
1689 if (ycount > 0)
1690 line++;
1691 else {
1692 line--;
1693 /* NOTE: We subtract two lines from these pointers */
1694 s -= vc->vc_size_row;
1695 d -= vc->vc_size_row;
1700 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1701 int line, int count, int offset)
1703 unsigned short *d = (unsigned short *)
1704 (vc->vc_origin + vc->vc_size_row * line);
1705 unsigned short *s = d + offset;
1707 while (count--) {
1708 unsigned short *start = s;
1709 unsigned short *le = advance_row(s, 1);
1710 unsigned short c;
1711 int x = 0;
1712 unsigned short attr = 1;
1714 do {
1715 c = scr_readw(s);
1716 if (attr != (c & 0xff00)) {
1717 attr = c & 0xff00;
1718 if (s > start) {
1719 fbcon_putcs(vc, start, s - start,
1720 line, x);
1721 x += s - start;
1722 start = s;
1725 if (c == scr_readw(d)) {
1726 if (s > start) {
1727 fbcon_putcs(vc, start, s - start,
1728 line, x);
1729 x += s - start + 1;
1730 start = s + 1;
1731 } else {
1732 x++;
1733 start++;
1736 scr_writew(c, d);
1737 console_conditional_schedule();
1738 s++;
1739 d++;
1740 } while (s < le);
1741 if (s > start)
1742 fbcon_putcs(vc, start, s - start, line, x);
1743 console_conditional_schedule();
1744 if (offset > 0)
1745 line++;
1746 else {
1747 line--;
1748 /* NOTE: We subtract two lines from these pointers */
1749 s -= vc->vc_size_row;
1750 d -= vc->vc_size_row;
1755 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1756 int count)
1758 unsigned short *p;
1760 if (vc->vc_num != fg_console)
1761 return;
1762 p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1764 while (count) {
1765 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1766 count--;
1767 p = advance_row(p, 1);
1768 softback_in += vc->vc_size_row;
1769 if (softback_in == softback_end)
1770 softback_in = softback_buf;
1771 if (softback_in == softback_top) {
1772 softback_top += vc->vc_size_row;
1773 if (softback_top == softback_end)
1774 softback_top = softback_buf;
1777 softback_curr = softback_in;
1780 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1781 int count)
1783 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1784 struct display *p = &fb_display[vc->vc_num];
1785 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1787 if (fbcon_is_inactive(vc, info))
1788 return -EINVAL;
1790 fbcon_cursor(vc, CM_ERASE);
1793 * ++Geert: Only use ywrap/ypan if the console is in text mode
1794 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1795 * whole screen (prevents flicker).
1798 switch (dir) {
1799 case SM_UP:
1800 if (count > vc->vc_rows) /* Maximum realistic size */
1801 count = vc->vc_rows;
1802 if (softback_top)
1803 fbcon_softback_note(vc, t, count);
1804 if (logo_shown >= 0)
1805 goto redraw_up;
1806 switch (p->scrollmode) {
1807 case SCROLL_MOVE:
1808 fbcon_redraw_blit(vc, info, p, t, b - t - count,
1809 count);
1810 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1811 scr_memsetw((unsigned short *) (vc->vc_origin +
1812 vc->vc_size_row *
1813 (b - count)),
1814 vc->vc_video_erase_char,
1815 vc->vc_size_row * count);
1816 return 1;
1817 break;
1819 case SCROLL_WRAP_MOVE:
1820 if (b - t - count > 3 * vc->vc_rows >> 2) {
1821 if (t > 0)
1822 fbcon_bmove(vc, 0, 0, count, 0, t,
1823 vc->vc_cols);
1824 ywrap_up(vc, count);
1825 if (vc->vc_rows - b > 0)
1826 fbcon_bmove(vc, b - count, 0, b, 0,
1827 vc->vc_rows - b,
1828 vc->vc_cols);
1829 } else if (info->flags & FBINFO_READS_FAST)
1830 fbcon_bmove(vc, t + count, 0, t, 0,
1831 b - t - count, vc->vc_cols);
1832 else
1833 goto redraw_up;
1834 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1835 break;
1837 case SCROLL_PAN_REDRAW:
1838 if ((p->yscroll + count <=
1839 2 * (p->vrows - vc->vc_rows))
1840 && ((!scroll_partial && (b - t == vc->vc_rows))
1841 || (scroll_partial
1842 && (b - t - count >
1843 3 * vc->vc_rows >> 2)))) {
1844 if (t > 0)
1845 fbcon_redraw_move(vc, p, 0, t, count);
1846 ypan_up_redraw(vc, t, count);
1847 if (vc->vc_rows - b > 0)
1848 fbcon_redraw_move(vc, p, b,
1849 vc->vc_rows - b, b);
1850 } else
1851 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1852 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1853 break;
1855 case SCROLL_PAN_MOVE:
1856 if ((p->yscroll + count <=
1857 2 * (p->vrows - vc->vc_rows))
1858 && ((!scroll_partial && (b - t == vc->vc_rows))
1859 || (scroll_partial
1860 && (b - t - count >
1861 3 * vc->vc_rows >> 2)))) {
1862 if (t > 0)
1863 fbcon_bmove(vc, 0, 0, count, 0, t,
1864 vc->vc_cols);
1865 ypan_up(vc, count);
1866 if (vc->vc_rows - b > 0)
1867 fbcon_bmove(vc, b - count, 0, b, 0,
1868 vc->vc_rows - b,
1869 vc->vc_cols);
1870 } else if (info->flags & FBINFO_READS_FAST)
1871 fbcon_bmove(vc, t + count, 0, t, 0,
1872 b - t - count, vc->vc_cols);
1873 else
1874 goto redraw_up;
1875 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1876 break;
1878 case SCROLL_REDRAW:
1879 redraw_up:
1880 fbcon_redraw(vc, p, t, b - t - count,
1881 count * vc->vc_cols);
1882 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1883 scr_memsetw((unsigned short *) (vc->vc_origin +
1884 vc->vc_size_row *
1885 (b - count)),
1886 vc->vc_video_erase_char,
1887 vc->vc_size_row * count);
1888 return 1;
1890 break;
1892 case SM_DOWN:
1893 if (count > vc->vc_rows) /* Maximum realistic size */
1894 count = vc->vc_rows;
1895 if (logo_shown >= 0)
1896 goto redraw_down;
1897 switch (p->scrollmode) {
1898 case SCROLL_MOVE:
1899 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1900 -count);
1901 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1902 scr_memsetw((unsigned short *) (vc->vc_origin +
1903 vc->vc_size_row *
1905 vc->vc_video_erase_char,
1906 vc->vc_size_row * count);
1907 return 1;
1908 break;
1910 case SCROLL_WRAP_MOVE:
1911 if (b - t - count > 3 * vc->vc_rows >> 2) {
1912 if (vc->vc_rows - b > 0)
1913 fbcon_bmove(vc, b, 0, b - count, 0,
1914 vc->vc_rows - b,
1915 vc->vc_cols);
1916 ywrap_down(vc, count);
1917 if (t > 0)
1918 fbcon_bmove(vc, count, 0, 0, 0, t,
1919 vc->vc_cols);
1920 } else if (info->flags & FBINFO_READS_FAST)
1921 fbcon_bmove(vc, t, 0, t + count, 0,
1922 b - t - count, vc->vc_cols);
1923 else
1924 goto redraw_down;
1925 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1926 break;
1928 case SCROLL_PAN_MOVE:
1929 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1930 && ((!scroll_partial && (b - t == vc->vc_rows))
1931 || (scroll_partial
1932 && (b - t - count >
1933 3 * vc->vc_rows >> 2)))) {
1934 if (vc->vc_rows - b > 0)
1935 fbcon_bmove(vc, b, 0, b - count, 0,
1936 vc->vc_rows - b,
1937 vc->vc_cols);
1938 ypan_down(vc, count);
1939 if (t > 0)
1940 fbcon_bmove(vc, count, 0, 0, 0, t,
1941 vc->vc_cols);
1942 } else if (info->flags & FBINFO_READS_FAST)
1943 fbcon_bmove(vc, t, 0, t + count, 0,
1944 b - t - count, vc->vc_cols);
1945 else
1946 goto redraw_down;
1947 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1948 break;
1950 case SCROLL_PAN_REDRAW:
1951 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1952 && ((!scroll_partial && (b - t == vc->vc_rows))
1953 || (scroll_partial
1954 && (b - t - count >
1955 3 * vc->vc_rows >> 2)))) {
1956 if (vc->vc_rows - b > 0)
1957 fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
1958 b - count);
1959 ypan_down_redraw(vc, t, count);
1960 if (t > 0)
1961 fbcon_redraw_move(vc, p, count, t, 0);
1962 } else
1963 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
1964 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1965 break;
1967 case SCROLL_REDRAW:
1968 redraw_down:
1969 fbcon_redraw(vc, p, b - 1, b - t - count,
1970 -count * vc->vc_cols);
1971 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1972 scr_memsetw((unsigned short *) (vc->vc_origin +
1973 vc->vc_size_row *
1975 vc->vc_video_erase_char,
1976 vc->vc_size_row * count);
1977 return 1;
1980 return 0;
1984 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
1985 int height, int width)
1987 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1988 struct display *p = &fb_display[vc->vc_num];
1990 if (fbcon_is_inactive(vc, info))
1991 return;
1993 if (!width || !height)
1994 return;
1996 /* Split blits that cross physical y_wrap case.
1997 * Pathological case involves 4 blits, better to use recursive
1998 * code rather than unrolled case
2000 * Recursive invocations don't need to erase the cursor over and
2001 * over again, so we use fbcon_bmove_rec()
2003 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2004 p->vrows - p->yscroll);
2007 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
2008 int dy, int dx, int height, int width, u_int y_break)
2010 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2011 struct fbcon_ops *ops = info->fbcon_par;
2012 u_int b;
2014 if (sy < y_break && sy + height > y_break) {
2015 b = y_break - sy;
2016 if (dy < sy) { /* Avoid trashing self */
2017 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2018 y_break);
2019 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2020 height - b, width, y_break);
2021 } else {
2022 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2023 height - b, width, y_break);
2024 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2025 y_break);
2027 return;
2030 if (dy < y_break && dy + height > y_break) {
2031 b = y_break - dy;
2032 if (dy < sy) { /* Avoid trashing self */
2033 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2034 y_break);
2035 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2036 height - b, width, y_break);
2037 } else {
2038 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2039 height - b, width, y_break);
2040 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2041 y_break);
2043 return;
2045 ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2046 height, width);
2049 static void updatescrollmode(struct display *p,
2050 struct fb_info *info,
2051 struct vc_data *vc)
2053 struct fbcon_ops *ops = info->fbcon_par;
2054 int fh = vc->vc_font.height;
2055 int cap = info->flags;
2056 u16 t = 0;
2057 int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2058 info->fix.xpanstep);
2059 int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2060 int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2061 int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2062 info->var.xres_virtual);
2063 int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2064 divides(ypan, vc->vc_font.height) && vyres > yres;
2065 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2066 divides(ywrap, vc->vc_font.height) &&
2067 divides(vc->vc_font.height, vyres) &&
2068 divides(vc->vc_font.height, yres);
2069 int reading_fast = cap & FBINFO_READS_FAST;
2070 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2071 !(cap & FBINFO_HWACCEL_DISABLED);
2072 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2073 !(cap & FBINFO_HWACCEL_DISABLED);
2075 p->vrows = vyres/fh;
2076 if (yres > (fh * (vc->vc_rows + 1)))
2077 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2078 if ((yres % fh) && (vyres % fh < yres % fh))
2079 p->vrows--;
2081 if (good_wrap || good_pan) {
2082 if (reading_fast || fast_copyarea)
2083 p->scrollmode = good_wrap ?
2084 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2085 else
2086 p->scrollmode = good_wrap ? SCROLL_REDRAW :
2087 SCROLL_PAN_REDRAW;
2088 } else {
2089 if (reading_fast || (fast_copyarea && !fast_imageblit))
2090 p->scrollmode = SCROLL_MOVE;
2091 else
2092 p->scrollmode = SCROLL_REDRAW;
2096 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2097 unsigned int height, unsigned int user)
2099 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2100 struct fbcon_ops *ops = info->fbcon_par;
2101 struct display *p = &fb_display[vc->vc_num];
2102 struct fb_var_screeninfo var = info->var;
2103 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2105 virt_w = FBCON_SWAP(ops->rotate, width, height);
2106 virt_h = FBCON_SWAP(ops->rotate, height, width);
2107 virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2108 vc->vc_font.height);
2109 virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2110 vc->vc_font.width);
2111 var.xres = virt_w * virt_fw;
2112 var.yres = virt_h * virt_fh;
2113 x_diff = info->var.xres - var.xres;
2114 y_diff = info->var.yres - var.yres;
2115 if (x_diff < 0 || x_diff > virt_fw ||
2116 y_diff < 0 || y_diff > virt_fh) {
2117 const struct fb_videomode *mode;
2119 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2120 mode = fb_find_best_mode(&var, &info->modelist);
2121 if (mode == NULL)
2122 return -EINVAL;
2123 display_to_var(&var, p);
2124 fb_videomode_to_var(&var, mode);
2126 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2127 return -EINVAL;
2129 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2130 if (CON_IS_VISIBLE(vc)) {
2131 var.activate = FB_ACTIVATE_NOW |
2132 FB_ACTIVATE_FORCE;
2133 fb_set_var(info, &var);
2135 var_to_display(p, &info->var, info);
2136 ops->var = info->var;
2138 updatescrollmode(p, info, vc);
2139 return 0;
2142 static int fbcon_switch(struct vc_data *vc)
2144 struct fb_info *info, *old_info = NULL;
2145 struct fbcon_ops *ops;
2146 struct display *p = &fb_display[vc->vc_num];
2147 struct fb_var_screeninfo var;
2148 int i, ret, prev_console, charcnt = 256;
2150 info = registered_fb[con2fb_map[vc->vc_num]];
2151 ops = info->fbcon_par;
2153 if (softback_top) {
2154 if (softback_lines)
2155 fbcon_set_origin(vc);
2156 softback_top = softback_curr = softback_in = softback_buf;
2157 softback_lines = 0;
2158 fbcon_update_softback(vc);
2161 if (logo_shown >= 0) {
2162 struct vc_data *conp2 = vc_cons[logo_shown].d;
2164 if (conp2->vc_top == logo_lines
2165 && conp2->vc_bottom == conp2->vc_rows)
2166 conp2->vc_top = 0;
2167 logo_shown = FBCON_LOGO_CANSHOW;
2170 prev_console = ops->currcon;
2171 if (prev_console != -1)
2172 old_info = registered_fb[con2fb_map[prev_console]];
2174 * FIXME: If we have multiple fbdev's loaded, we need to
2175 * update all info->currcon. Perhaps, we can place this
2176 * in a centralized structure, but this might break some
2177 * drivers.
2179 * info->currcon = vc->vc_num;
2181 for (i = 0; i < FB_MAX; i++) {
2182 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
2183 struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2185 o->currcon = vc->vc_num;
2188 memset(&var, 0, sizeof(struct fb_var_screeninfo));
2189 display_to_var(&var, p);
2190 var.activate = FB_ACTIVATE_NOW;
2193 * make sure we don't unnecessarily trip the memcmp()
2194 * in fb_set_var()
2196 info->var.activate = var.activate;
2197 var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
2198 fb_set_var(info, &var);
2199 ops->var = info->var;
2201 if (old_info != NULL && (old_info != info ||
2202 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2203 if (info->fbops->fb_set_par) {
2204 ret = info->fbops->fb_set_par(info);
2206 if (ret)
2207 printk(KERN_ERR "fbcon_switch: detected "
2208 "unhandled fb_set_par error, "
2209 "error code %d\n", ret);
2212 if (old_info != info)
2213 fbcon_del_cursor_timer(old_info);
2216 if (fbcon_is_inactive(vc, info) ||
2217 ops->blank_state != FB_BLANK_UNBLANK)
2218 fbcon_del_cursor_timer(info);
2219 else
2220 fbcon_add_cursor_timer(info);
2222 set_blitting_type(vc, info);
2223 ops->cursor_reset = 1;
2225 if (ops->rotate_font && ops->rotate_font(info, vc)) {
2226 ops->rotate = FB_ROTATE_UR;
2227 set_blitting_type(vc, info);
2230 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2231 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2233 if (p->userfont)
2234 charcnt = FNTCHARCNT(vc->vc_font.data);
2236 if (charcnt > 256)
2237 vc->vc_complement_mask <<= 1;
2239 updatescrollmode(p, info, vc);
2241 switch (p->scrollmode) {
2242 case SCROLL_WRAP_MOVE:
2243 scrollback_phys_max = p->vrows - vc->vc_rows;
2244 break;
2245 case SCROLL_PAN_MOVE:
2246 case SCROLL_PAN_REDRAW:
2247 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2248 if (scrollback_phys_max < 0)
2249 scrollback_phys_max = 0;
2250 break;
2251 default:
2252 scrollback_phys_max = 0;
2253 break;
2256 scrollback_max = 0;
2257 scrollback_current = 0;
2259 if (!fbcon_is_inactive(vc, info)) {
2260 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2261 ops->update_start(info);
2264 fbcon_set_palette(vc, color_table);
2265 fbcon_clear_margins(vc, 0);
2267 if (logo_shown == FBCON_LOGO_DRAW) {
2269 logo_shown = fg_console;
2270 /* This is protected above by initmem_freed */
2271 fb_show_logo(info, ops->rotate);
2272 update_region(vc,
2273 vc->vc_origin + vc->vc_size_row * vc->vc_top,
2274 vc->vc_size_row * (vc->vc_bottom -
2275 vc->vc_top) / 2);
2276 return 0;
2278 return 1;
2281 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2282 int blank)
2284 struct fb_event event;
2286 if (blank) {
2287 unsigned short charmask = vc->vc_hi_font_mask ?
2288 0x1ff : 0xff;
2289 unsigned short oldc;
2291 oldc = vc->vc_video_erase_char;
2292 vc->vc_video_erase_char &= charmask;
2293 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2294 vc->vc_video_erase_char = oldc;
2298 if (!lock_fb_info(info))
2299 return;
2300 event.info = info;
2301 event.data = &blank;
2302 fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
2303 unlock_fb_info(info);
2306 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2308 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2309 struct fbcon_ops *ops = info->fbcon_par;
2311 if (mode_switch) {
2312 struct fb_var_screeninfo var = info->var;
2314 ops->graphics = 1;
2316 if (!blank) {
2317 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2318 fb_set_var(info, &var);
2319 ops->graphics = 0;
2320 ops->var = info->var;
2324 if (!fbcon_is_inactive(vc, info)) {
2325 if (ops->blank_state != blank) {
2326 ops->blank_state = blank;
2327 fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2328 ops->cursor_flash = (!blank);
2330 if (!(info->flags & FBINFO_MISC_USEREVENT))
2331 if (fb_blank(info, blank))
2332 fbcon_generic_blank(vc, info, blank);
2335 if (!blank)
2336 update_screen(vc);
2339 if (mode_switch || fbcon_is_inactive(vc, info) ||
2340 ops->blank_state != FB_BLANK_UNBLANK)
2341 fbcon_del_cursor_timer(info);
2342 else
2343 fbcon_add_cursor_timer(info);
2345 return 0;
2348 static int fbcon_debug_enter(struct vc_data *vc)
2350 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2351 struct fbcon_ops *ops = info->fbcon_par;
2353 ops->save_graphics = ops->graphics;
2354 ops->graphics = 0;
2355 if (info->fbops->fb_debug_enter)
2356 info->fbops->fb_debug_enter(info);
2357 fbcon_set_palette(vc, color_table);
2358 return 0;
2361 static int fbcon_debug_leave(struct vc_data *vc)
2363 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2364 struct fbcon_ops *ops = info->fbcon_par;
2366 ops->graphics = ops->save_graphics;
2367 if (info->fbops->fb_debug_leave)
2368 info->fbops->fb_debug_leave(info);
2369 return 0;
2372 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2374 u8 *fontdata = vc->vc_font.data;
2375 u8 *data = font->data;
2376 int i, j;
2378 font->width = vc->vc_font.width;
2379 font->height = vc->vc_font.height;
2380 font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2381 if (!font->data)
2382 return 0;
2384 if (font->width <= 8) {
2385 j = vc->vc_font.height;
2386 for (i = 0; i < font->charcount; i++) {
2387 memcpy(data, fontdata, j);
2388 memset(data + j, 0, 32 - j);
2389 data += 32;
2390 fontdata += j;
2392 } else if (font->width <= 16) {
2393 j = vc->vc_font.height * 2;
2394 for (i = 0; i < font->charcount; i++) {
2395 memcpy(data, fontdata, j);
2396 memset(data + j, 0, 64 - j);
2397 data += 64;
2398 fontdata += j;
2400 } else if (font->width <= 24) {
2401 for (i = 0; i < font->charcount; i++) {
2402 for (j = 0; j < vc->vc_font.height; j++) {
2403 *data++ = fontdata[0];
2404 *data++ = fontdata[1];
2405 *data++ = fontdata[2];
2406 fontdata += sizeof(u32);
2408 memset(data, 0, 3 * (32 - j));
2409 data += 3 * (32 - j);
2411 } else {
2412 j = vc->vc_font.height * 4;
2413 for (i = 0; i < font->charcount; i++) {
2414 memcpy(data, fontdata, j);
2415 memset(data + j, 0, 128 - j);
2416 data += 128;
2417 fontdata += j;
2420 return 0;
2423 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2424 const u8 * data, int userfont)
2426 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2427 struct fbcon_ops *ops = info->fbcon_par;
2428 struct display *p = &fb_display[vc->vc_num];
2429 int resize;
2430 int cnt;
2431 char *old_data = NULL;
2433 if (CON_IS_VISIBLE(vc) && softback_lines)
2434 fbcon_set_origin(vc);
2436 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2437 if (p->userfont)
2438 old_data = vc->vc_font.data;
2439 if (userfont)
2440 cnt = FNTCHARCNT(data);
2441 else
2442 cnt = 256;
2443 vc->vc_font.data = (void *)(p->fontdata = data);
2444 if ((p->userfont = userfont))
2445 REFCOUNT(data)++;
2446 vc->vc_font.width = w;
2447 vc->vc_font.height = h;
2448 if (vc->vc_hi_font_mask && cnt == 256) {
2449 vc->vc_hi_font_mask = 0;
2450 if (vc->vc_can_do_color) {
2451 vc->vc_complement_mask >>= 1;
2452 vc->vc_s_complement_mask >>= 1;
2455 /* ++Edmund: reorder the attribute bits */
2456 if (vc->vc_can_do_color) {
2457 unsigned short *cp =
2458 (unsigned short *) vc->vc_origin;
2459 int count = vc->vc_screenbuf_size / 2;
2460 unsigned short c;
2461 for (; count > 0; count--, cp++) {
2462 c = scr_readw(cp);
2463 scr_writew(((c & 0xfe00) >> 1) |
2464 (c & 0xff), cp);
2466 c = vc->vc_video_erase_char;
2467 vc->vc_video_erase_char =
2468 ((c & 0xfe00) >> 1) | (c & 0xff);
2469 vc->vc_attr >>= 1;
2471 } else if (!vc->vc_hi_font_mask && cnt == 512) {
2472 vc->vc_hi_font_mask = 0x100;
2473 if (vc->vc_can_do_color) {
2474 vc->vc_complement_mask <<= 1;
2475 vc->vc_s_complement_mask <<= 1;
2478 /* ++Edmund: reorder the attribute bits */
2480 unsigned short *cp =
2481 (unsigned short *) vc->vc_origin;
2482 int count = vc->vc_screenbuf_size / 2;
2483 unsigned short c;
2484 for (; count > 0; count--, cp++) {
2485 unsigned short newc;
2486 c = scr_readw(cp);
2487 if (vc->vc_can_do_color)
2488 newc =
2489 ((c & 0xff00) << 1) | (c &
2490 0xff);
2491 else
2492 newc = c & ~0x100;
2493 scr_writew(newc, cp);
2495 c = vc->vc_video_erase_char;
2496 if (vc->vc_can_do_color) {
2497 vc->vc_video_erase_char =
2498 ((c & 0xff00) << 1) | (c & 0xff);
2499 vc->vc_attr <<= 1;
2500 } else
2501 vc->vc_video_erase_char = c & ~0x100;
2506 if (resize) {
2507 int cols, rows;
2509 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2510 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2511 cols /= w;
2512 rows /= h;
2513 vc_resize(vc, cols, rows);
2514 if (CON_IS_VISIBLE(vc) && softback_buf)
2515 fbcon_update_softback(vc);
2516 } else if (CON_IS_VISIBLE(vc)
2517 && vc->vc_mode == KD_TEXT) {
2518 fbcon_clear_margins(vc, 0);
2519 update_screen(vc);
2522 if (old_data && (--REFCOUNT(old_data) == 0))
2523 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2524 return 0;
2527 static int fbcon_copy_font(struct vc_data *vc, int con)
2529 struct display *od = &fb_display[con];
2530 struct console_font *f = &vc->vc_font;
2532 if (od->fontdata == f->data)
2533 return 0; /* already the same font... */
2534 return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2538 * User asked to set font; we are guaranteed that
2539 * a) width and height are in range 1..32
2540 * b) charcount does not exceed 512
2541 * but lets not assume that, since someone might someday want to use larger
2542 * fonts. And charcount of 512 is small for unicode support.
2544 * However, user space gives the font in 32 rows , regardless of
2545 * actual font height. So a new API is needed if support for larger fonts
2546 * is ever implemented.
2549 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2551 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2552 unsigned charcount = font->charcount;
2553 int w = font->width;
2554 int h = font->height;
2555 int size;
2556 int i, csum;
2557 u8 *new_data, *data = font->data;
2558 int pitch = (font->width+7) >> 3;
2560 /* Is there a reason why fbconsole couldn't handle any charcount >256?
2561 * If not this check should be changed to charcount < 256 */
2562 if (charcount != 256 && charcount != 512)
2563 return -EINVAL;
2565 /* Make sure drawing engine can handle the font */
2566 if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2567 !(info->pixmap.blit_y & (1 << (font->height - 1))))
2568 return -EINVAL;
2570 /* Make sure driver can handle the font length */
2571 if (fbcon_invalid_charcount(info, charcount))
2572 return -EINVAL;
2574 size = h * pitch * charcount;
2576 new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2578 if (!new_data)
2579 return -ENOMEM;
2581 new_data += FONT_EXTRA_WORDS * sizeof(int);
2582 FNTSIZE(new_data) = size;
2583 FNTCHARCNT(new_data) = charcount;
2584 REFCOUNT(new_data) = 0; /* usage counter */
2585 for (i=0; i< charcount; i++) {
2586 memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
2589 /* Since linux has a nice crc32 function use it for counting font
2590 * checksums. */
2591 csum = crc32(0, new_data, size);
2593 FNTSUM(new_data) = csum;
2594 /* Check if the same font is on some other console already */
2595 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2596 struct vc_data *tmp = vc_cons[i].d;
2598 if (fb_display[i].userfont &&
2599 fb_display[i].fontdata &&
2600 FNTSUM(fb_display[i].fontdata) == csum &&
2601 FNTSIZE(fb_display[i].fontdata) == size &&
2602 tmp->vc_font.width == w &&
2603 !memcmp(fb_display[i].fontdata, new_data, size)) {
2604 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2605 new_data = (u8 *)fb_display[i].fontdata;
2606 break;
2609 return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2612 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2614 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2615 const struct font_desc *f;
2617 if (!name)
2618 f = get_default_font(info->var.xres, info->var.yres,
2619 info->pixmap.blit_x, info->pixmap.blit_y);
2620 else if (!(f = find_font(name)))
2621 return -ENOENT;
2623 font->width = f->width;
2624 font->height = f->height;
2625 return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2628 static u16 palette_red[16];
2629 static u16 palette_green[16];
2630 static u16 palette_blue[16];
2632 static struct fb_cmap palette_cmap = {
2633 0, 16, palette_red, palette_green, palette_blue, NULL
2636 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2638 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2639 int i, j, k, depth;
2640 u8 val;
2642 if (fbcon_is_inactive(vc, info))
2643 return -EINVAL;
2645 if (!CON_IS_VISIBLE(vc))
2646 return 0;
2648 depth = fb_get_color_depth(&info->var, &info->fix);
2649 if (depth > 3) {
2650 for (i = j = 0; i < 16; i++) {
2651 k = table[i];
2652 val = vc->vc_palette[j++];
2653 palette_red[k] = (val << 8) | val;
2654 val = vc->vc_palette[j++];
2655 palette_green[k] = (val << 8) | val;
2656 val = vc->vc_palette[j++];
2657 palette_blue[k] = (val << 8) | val;
2659 palette_cmap.len = 16;
2660 palette_cmap.start = 0;
2662 * If framebuffer is capable of less than 16 colors,
2663 * use default palette of fbcon.
2665 } else
2666 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2668 return fb_set_cmap(&palette_cmap, info);
2671 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2673 unsigned long p;
2674 int line;
2676 if (vc->vc_num != fg_console || !softback_lines)
2677 return (u16 *) (vc->vc_origin + offset);
2678 line = offset / vc->vc_size_row;
2679 if (line >= softback_lines)
2680 return (u16 *) (vc->vc_origin + offset -
2681 softback_lines * vc->vc_size_row);
2682 p = softback_curr + offset;
2683 if (p >= softback_end)
2684 p += softback_buf - softback_end;
2685 return (u16 *) p;
2688 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2689 int *px, int *py)
2691 unsigned long ret;
2692 int x, y;
2694 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2695 unsigned long offset = (pos - vc->vc_origin) / 2;
2697 x = offset % vc->vc_cols;
2698 y = offset / vc->vc_cols;
2699 if (vc->vc_num == fg_console)
2700 y += softback_lines;
2701 ret = pos + (vc->vc_cols - x) * 2;
2702 } else if (vc->vc_num == fg_console && softback_lines) {
2703 unsigned long offset = pos - softback_curr;
2705 if (pos < softback_curr)
2706 offset += softback_end - softback_buf;
2707 offset /= 2;
2708 x = offset % vc->vc_cols;
2709 y = offset / vc->vc_cols;
2710 ret = pos + (vc->vc_cols - x) * 2;
2711 if (ret == softback_end)
2712 ret = softback_buf;
2713 if (ret == softback_in)
2714 ret = vc->vc_origin;
2715 } else {
2716 /* Should not happen */
2717 x = y = 0;
2718 ret = vc->vc_origin;
2720 if (px)
2721 *px = x;
2722 if (py)
2723 *py = y;
2724 return ret;
2727 /* As we might be inside of softback, we may work with non-contiguous buffer,
2728 that's why we have to use a separate routine. */
2729 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2731 while (cnt--) {
2732 u16 a = scr_readw(p);
2733 if (!vc->vc_can_do_color)
2734 a ^= 0x0800;
2735 else if (vc->vc_hi_font_mask == 0x100)
2736 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2737 (((a) & 0x0e00) << 4);
2738 else
2739 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2740 (((a) & 0x0700) << 4);
2741 scr_writew(a, p++);
2742 if (p == (u16 *) softback_end)
2743 p = (u16 *) softback_buf;
2744 if (p == (u16 *) softback_in)
2745 p = (u16 *) vc->vc_origin;
2749 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2751 struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2752 struct fbcon_ops *ops = info->fbcon_par;
2753 struct display *disp = &fb_display[fg_console];
2754 int offset, limit, scrollback_old;
2756 if (softback_top) {
2757 if (vc->vc_num != fg_console)
2758 return 0;
2759 if (vc->vc_mode != KD_TEXT || !lines)
2760 return 0;
2761 if (logo_shown >= 0) {
2762 struct vc_data *conp2 = vc_cons[logo_shown].d;
2764 if (conp2->vc_top == logo_lines
2765 && conp2->vc_bottom == conp2->vc_rows)
2766 conp2->vc_top = 0;
2767 if (logo_shown == vc->vc_num) {
2768 unsigned long p, q;
2769 int i;
2771 p = softback_in;
2772 q = vc->vc_origin +
2773 logo_lines * vc->vc_size_row;
2774 for (i = 0; i < logo_lines; i++) {
2775 if (p == softback_top)
2776 break;
2777 if (p == softback_buf)
2778 p = softback_end;
2779 p -= vc->vc_size_row;
2780 q -= vc->vc_size_row;
2781 scr_memcpyw((u16 *) q, (u16 *) p,
2782 vc->vc_size_row);
2784 softback_in = softback_curr = p;
2785 update_region(vc, vc->vc_origin,
2786 logo_lines * vc->vc_cols);
2788 logo_shown = FBCON_LOGO_CANSHOW;
2790 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2791 fbcon_redraw_softback(vc, disp, lines);
2792 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2793 return 0;
2796 if (!scrollback_phys_max)
2797 return -ENOSYS;
2799 scrollback_old = scrollback_current;
2800 scrollback_current -= lines;
2801 if (scrollback_current < 0)
2802 scrollback_current = 0;
2803 else if (scrollback_current > scrollback_max)
2804 scrollback_current = scrollback_max;
2805 if (scrollback_current == scrollback_old)
2806 return 0;
2808 if (fbcon_is_inactive(vc, info))
2809 return 0;
2811 fbcon_cursor(vc, CM_ERASE);
2813 offset = disp->yscroll - scrollback_current;
2814 limit = disp->vrows;
2815 switch (disp->scrollmode) {
2816 case SCROLL_WRAP_MOVE:
2817 info->var.vmode |= FB_VMODE_YWRAP;
2818 break;
2819 case SCROLL_PAN_MOVE:
2820 case SCROLL_PAN_REDRAW:
2821 limit -= vc->vc_rows;
2822 info->var.vmode &= ~FB_VMODE_YWRAP;
2823 break;
2825 if (offset < 0)
2826 offset += limit;
2827 else if (offset >= limit)
2828 offset -= limit;
2830 ops->var.xoffset = 0;
2831 ops->var.yoffset = offset * vc->vc_font.height;
2832 ops->update_start(info);
2834 if (!scrollback_current)
2835 fbcon_cursor(vc, CM_DRAW);
2836 return 0;
2839 static int fbcon_set_origin(struct vc_data *vc)
2841 if (softback_lines)
2842 fbcon_scrolldelta(vc, softback_lines);
2843 return 0;
2846 static void fbcon_suspended(struct fb_info *info)
2848 struct vc_data *vc = NULL;
2849 struct fbcon_ops *ops = info->fbcon_par;
2851 if (!ops || ops->currcon < 0)
2852 return;
2853 vc = vc_cons[ops->currcon].d;
2855 /* Clear cursor, restore saved data */
2856 fbcon_cursor(vc, CM_ERASE);
2859 static void fbcon_resumed(struct fb_info *info)
2861 struct vc_data *vc;
2862 struct fbcon_ops *ops = info->fbcon_par;
2864 if (!ops || ops->currcon < 0)
2865 return;
2866 vc = vc_cons[ops->currcon].d;
2868 update_screen(vc);
2871 static void fbcon_modechanged(struct fb_info *info)
2873 struct fbcon_ops *ops = info->fbcon_par;
2874 struct vc_data *vc;
2875 struct display *p;
2876 int rows, cols;
2878 if (!ops || ops->currcon < 0)
2879 return;
2880 vc = vc_cons[ops->currcon].d;
2881 if (vc->vc_mode != KD_TEXT ||
2882 registered_fb[con2fb_map[ops->currcon]] != info)
2883 return;
2885 p = &fb_display[vc->vc_num];
2886 set_blitting_type(vc, info);
2888 if (CON_IS_VISIBLE(vc)) {
2889 var_to_display(p, &info->var, info);
2890 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2891 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2892 cols /= vc->vc_font.width;
2893 rows /= vc->vc_font.height;
2894 vc_resize(vc, cols, rows);
2895 updatescrollmode(p, info, vc);
2896 scrollback_max = 0;
2897 scrollback_current = 0;
2899 if (!fbcon_is_inactive(vc, info)) {
2900 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2901 ops->update_start(info);
2904 fbcon_set_palette(vc, color_table);
2905 update_screen(vc);
2906 if (softback_buf)
2907 fbcon_update_softback(vc);
2911 static void fbcon_set_all_vcs(struct fb_info *info)
2913 struct fbcon_ops *ops = info->fbcon_par;
2914 struct vc_data *vc;
2915 struct display *p;
2916 int i, rows, cols, fg = -1;
2918 if (!ops || ops->currcon < 0)
2919 return;
2921 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2922 vc = vc_cons[i].d;
2923 if (!vc || vc->vc_mode != KD_TEXT ||
2924 registered_fb[con2fb_map[i]] != info)
2925 continue;
2927 if (CON_IS_VISIBLE(vc)) {
2928 fg = i;
2929 continue;
2932 p = &fb_display[vc->vc_num];
2933 set_blitting_type(vc, info);
2934 var_to_display(p, &info->var, info);
2935 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2936 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2937 cols /= vc->vc_font.width;
2938 rows /= vc->vc_font.height;
2939 vc_resize(vc, cols, rows);
2942 if (fg != -1)
2943 fbcon_modechanged(info);
2946 static int fbcon_mode_deleted(struct fb_info *info,
2947 struct fb_videomode *mode)
2949 struct fb_info *fb_info;
2950 struct display *p;
2951 int i, j, found = 0;
2953 /* before deletion, ensure that mode is not in use */
2954 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2955 j = con2fb_map[i];
2956 if (j == -1)
2957 continue;
2958 fb_info = registered_fb[j];
2959 if (fb_info != info)
2960 continue;
2961 p = &fb_display[i];
2962 if (!p || !p->mode)
2963 continue;
2964 if (fb_mode_is_equal(p->mode, mode)) {
2965 found = 1;
2966 break;
2969 return found;
2972 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
2973 static int fbcon_unbind(void)
2975 int ret;
2977 ret = unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
2978 fbcon_is_default);
2980 if (!ret)
2981 fbcon_has_console_bind = 0;
2983 return ret;
2985 #else
2986 static inline int fbcon_unbind(void)
2988 return -EINVAL;
2990 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
2992 static int fbcon_fb_unbind(int idx)
2994 int i, new_idx = -1, ret = 0;
2996 if (!fbcon_has_console_bind)
2997 return 0;
2999 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3000 if (con2fb_map[i] != idx &&
3001 con2fb_map[i] != -1) {
3002 new_idx = i;
3003 break;
3007 if (new_idx != -1) {
3008 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3009 if (con2fb_map[i] == idx)
3010 set_con2fb_map(i, new_idx, 0);
3012 } else
3013 ret = fbcon_unbind();
3015 return ret;
3018 static int fbcon_fb_unregistered(struct fb_info *info)
3020 int i, idx;
3022 idx = info->node;
3023 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3024 if (con2fb_map[i] == idx)
3025 con2fb_map[i] = -1;
3028 if (idx == info_idx) {
3029 info_idx = -1;
3031 for (i = 0; i < FB_MAX; i++) {
3032 if (registered_fb[i] != NULL) {
3033 info_idx = i;
3034 break;
3039 if (info_idx != -1) {
3040 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3041 if (con2fb_map[i] == -1)
3042 con2fb_map[i] = info_idx;
3046 if (primary_device == idx)
3047 primary_device = -1;
3049 if (!num_registered_fb)
3050 unregister_con_driver(&fb_con);
3052 return 0;
3055 static void fbcon_remap_all(int idx)
3057 int i;
3058 for (i = first_fb_vc; i <= last_fb_vc; i++)
3059 set_con2fb_map(i, idx, 0);
3061 if (con_is_bound(&fb_con)) {
3062 printk(KERN_INFO "fbcon: Remapping primary device, "
3063 "fb%i, to tty %i-%i\n", idx,
3064 first_fb_vc + 1, last_fb_vc + 1);
3065 info_idx = idx;
3069 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3070 static void fbcon_select_primary(struct fb_info *info)
3072 if (!map_override && primary_device == -1 &&
3073 fb_is_primary_device(info)) {
3074 int i;
3076 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3077 info->fix.id, info->node);
3078 primary_device = info->node;
3080 for (i = first_fb_vc; i <= last_fb_vc; i++)
3081 con2fb_map_boot[i] = primary_device;
3083 if (con_is_bound(&fb_con)) {
3084 printk(KERN_INFO "fbcon: Remapping primary device, "
3085 "fb%i, to tty %i-%i\n", info->node,
3086 first_fb_vc + 1, last_fb_vc + 1);
3087 info_idx = primary_device;
3092 #else
3093 static inline void fbcon_select_primary(struct fb_info *info)
3095 return;
3097 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3099 static int fbcon_fb_registered(struct fb_info *info)
3101 int ret = 0, i, idx;
3103 idx = info->node;
3104 fbcon_select_primary(info);
3106 if (info_idx == -1) {
3107 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3108 if (con2fb_map_boot[i] == idx) {
3109 info_idx = idx;
3110 break;
3114 if (info_idx != -1)
3115 ret = fbcon_takeover(1);
3116 } else {
3117 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3118 if (con2fb_map_boot[i] == idx)
3119 set_con2fb_map(i, idx, 0);
3123 return ret;
3126 static void fbcon_fb_blanked(struct fb_info *info, int blank)
3128 struct fbcon_ops *ops = info->fbcon_par;
3129 struct vc_data *vc;
3131 if (!ops || ops->currcon < 0)
3132 return;
3134 vc = vc_cons[ops->currcon].d;
3135 if (vc->vc_mode != KD_TEXT ||
3136 registered_fb[con2fb_map[ops->currcon]] != info)
3137 return;
3139 if (CON_IS_VISIBLE(vc)) {
3140 if (blank)
3141 do_blank_screen(0);
3142 else
3143 do_unblank_screen(0);
3145 ops->blank_state = blank;
3148 static void fbcon_new_modelist(struct fb_info *info)
3150 int i;
3151 struct vc_data *vc;
3152 struct fb_var_screeninfo var;
3153 const struct fb_videomode *mode;
3155 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3156 if (registered_fb[con2fb_map[i]] != info)
3157 continue;
3158 if (!fb_display[i].mode)
3159 continue;
3160 vc = vc_cons[i].d;
3161 display_to_var(&var, &fb_display[i]);
3162 mode = fb_find_nearest_mode(fb_display[i].mode,
3163 &info->modelist);
3164 fb_videomode_to_var(&var, mode);
3165 fbcon_set_disp(info, &var, vc->vc_num);
3169 static void fbcon_get_requirement(struct fb_info *info,
3170 struct fb_blit_caps *caps)
3172 struct vc_data *vc;
3173 struct display *p;
3175 if (caps->flags) {
3176 int i, charcnt;
3178 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3179 vc = vc_cons[i].d;
3180 if (vc && vc->vc_mode == KD_TEXT &&
3181 info->node == con2fb_map[i]) {
3182 p = &fb_display[i];
3183 caps->x |= 1 << (vc->vc_font.width - 1);
3184 caps->y |= 1 << (vc->vc_font.height - 1);
3185 charcnt = (p->userfont) ?
3186 FNTCHARCNT(p->fontdata) : 256;
3187 if (caps->len < charcnt)
3188 caps->len = charcnt;
3191 } else {
3192 vc = vc_cons[fg_console].d;
3194 if (vc && vc->vc_mode == KD_TEXT &&
3195 info->node == con2fb_map[fg_console]) {
3196 p = &fb_display[fg_console];
3197 caps->x = 1 << (vc->vc_font.width - 1);
3198 caps->y = 1 << (vc->vc_font.height - 1);
3199 caps->len = (p->userfont) ?
3200 FNTCHARCNT(p->fontdata) : 256;
3205 static int fbcon_event_notify(struct notifier_block *self,
3206 unsigned long action, void *data)
3208 struct fb_event *event = data;
3209 struct fb_info *info = event->info;
3210 struct fb_videomode *mode;
3211 struct fb_con2fbmap *con2fb;
3212 struct fb_blit_caps *caps;
3213 int idx, ret = 0;
3216 * ignore all events except driver registration and deregistration
3217 * if fbcon is not active
3219 if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3220 action == FB_EVENT_FB_UNREGISTERED))
3221 goto done;
3223 switch(action) {
3224 case FB_EVENT_SUSPEND:
3225 fbcon_suspended(info);
3226 break;
3227 case FB_EVENT_RESUME:
3228 fbcon_resumed(info);
3229 break;
3230 case FB_EVENT_MODE_CHANGE:
3231 fbcon_modechanged(info);
3232 break;
3233 case FB_EVENT_MODE_CHANGE_ALL:
3234 fbcon_set_all_vcs(info);
3235 break;
3236 case FB_EVENT_MODE_DELETE:
3237 mode = event->data;
3238 ret = fbcon_mode_deleted(info, mode);
3239 break;
3240 case FB_EVENT_FB_UNBIND:
3241 idx = info->node;
3242 ret = fbcon_fb_unbind(idx);
3243 break;
3244 case FB_EVENT_FB_REGISTERED:
3245 ret = fbcon_fb_registered(info);
3246 break;
3247 case FB_EVENT_FB_UNREGISTERED:
3248 ret = fbcon_fb_unregistered(info);
3249 break;
3250 case FB_EVENT_SET_CONSOLE_MAP:
3251 con2fb = event->data;
3252 ret = set_con2fb_map(con2fb->console - 1,
3253 con2fb->framebuffer, 1);
3254 break;
3255 case FB_EVENT_GET_CONSOLE_MAP:
3256 con2fb = event->data;
3257 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3258 break;
3259 case FB_EVENT_BLANK:
3260 fbcon_fb_blanked(info, *(int *)event->data);
3261 break;
3262 case FB_EVENT_NEW_MODELIST:
3263 fbcon_new_modelist(info);
3264 break;
3265 case FB_EVENT_GET_REQ:
3266 caps = event->data;
3267 fbcon_get_requirement(info, caps);
3268 break;
3269 case FB_EVENT_REMAP_ALL_CONSOLE:
3270 idx = info->node;
3271 fbcon_remap_all(idx);
3272 break;
3274 done:
3275 return ret;
3279 * The console `switch' structure for the frame buffer based console
3282 static const struct consw fb_con = {
3283 .owner = THIS_MODULE,
3284 .con_startup = fbcon_startup,
3285 .con_init = fbcon_init,
3286 .con_deinit = fbcon_deinit,
3287 .con_clear = fbcon_clear,
3288 .con_putc = fbcon_putc,
3289 .con_putcs = fbcon_putcs,
3290 .con_cursor = fbcon_cursor,
3291 .con_scroll = fbcon_scroll,
3292 .con_bmove = fbcon_bmove,
3293 .con_switch = fbcon_switch,
3294 .con_blank = fbcon_blank,
3295 .con_font_set = fbcon_set_font,
3296 .con_font_get = fbcon_get_font,
3297 .con_font_default = fbcon_set_def_font,
3298 .con_font_copy = fbcon_copy_font,
3299 .con_set_palette = fbcon_set_palette,
3300 .con_scrolldelta = fbcon_scrolldelta,
3301 .con_set_origin = fbcon_set_origin,
3302 .con_invert_region = fbcon_invert_region,
3303 .con_screen_pos = fbcon_screen_pos,
3304 .con_getxy = fbcon_getxy,
3305 .con_resize = fbcon_resize,
3306 .con_debug_enter = fbcon_debug_enter,
3307 .con_debug_leave = fbcon_debug_leave,
3310 static struct notifier_block fbcon_event_notifier = {
3311 .notifier_call = fbcon_event_notify,
3314 static ssize_t store_rotate(struct device *device,
3315 struct device_attribute *attr, const char *buf,
3316 size_t count)
3318 struct fb_info *info;
3319 int rotate, idx;
3320 char **last = NULL;
3322 if (fbcon_has_exited)
3323 return count;
3325 console_lock();
3326 idx = con2fb_map[fg_console];
3328 if (idx == -1 || registered_fb[idx] == NULL)
3329 goto err;
3331 info = registered_fb[idx];
3332 rotate = simple_strtoul(buf, last, 0);
3333 fbcon_rotate(info, rotate);
3334 err:
3335 console_unlock();
3336 return count;
3339 static ssize_t store_rotate_all(struct device *device,
3340 struct device_attribute *attr,const char *buf,
3341 size_t count)
3343 struct fb_info *info;
3344 int rotate, idx;
3345 char **last = NULL;
3347 if (fbcon_has_exited)
3348 return count;
3350 console_lock();
3351 idx = con2fb_map[fg_console];
3353 if (idx == -1 || registered_fb[idx] == NULL)
3354 goto err;
3356 info = registered_fb[idx];
3357 rotate = simple_strtoul(buf, last, 0);
3358 fbcon_rotate_all(info, rotate);
3359 err:
3360 console_unlock();
3361 return count;
3364 static ssize_t show_rotate(struct device *device,
3365 struct device_attribute *attr,char *buf)
3367 struct fb_info *info;
3368 int rotate = 0, idx;
3370 if (fbcon_has_exited)
3371 return 0;
3373 console_lock();
3374 idx = con2fb_map[fg_console];
3376 if (idx == -1 || registered_fb[idx] == NULL)
3377 goto err;
3379 info = registered_fb[idx];
3380 rotate = fbcon_get_rotate(info);
3381 err:
3382 console_unlock();
3383 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3386 static ssize_t show_cursor_blink(struct device *device,
3387 struct device_attribute *attr, char *buf)
3389 struct fb_info *info;
3390 struct fbcon_ops *ops;
3391 int idx, blink = -1;
3393 if (fbcon_has_exited)
3394 return 0;
3396 console_lock();
3397 idx = con2fb_map[fg_console];
3399 if (idx == -1 || registered_fb[idx] == NULL)
3400 goto err;
3402 info = registered_fb[idx];
3403 ops = info->fbcon_par;
3405 if (!ops)
3406 goto err;
3408 blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3409 err:
3410 console_unlock();
3411 return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3414 static ssize_t store_cursor_blink(struct device *device,
3415 struct device_attribute *attr,
3416 const char *buf, size_t count)
3418 struct fb_info *info;
3419 int blink, idx;
3420 char **last = NULL;
3422 if (fbcon_has_exited)
3423 return count;
3425 console_lock();
3426 idx = con2fb_map[fg_console];
3428 if (idx == -1 || registered_fb[idx] == NULL)
3429 goto err;
3431 info = registered_fb[idx];
3433 if (!info->fbcon_par)
3434 goto err;
3436 blink = simple_strtoul(buf, last, 0);
3438 if (blink) {
3439 fbcon_cursor_noblink = 0;
3440 fbcon_add_cursor_timer(info);
3441 } else {
3442 fbcon_cursor_noblink = 1;
3443 fbcon_del_cursor_timer(info);
3446 err:
3447 console_unlock();
3448 return count;
3451 static struct device_attribute device_attrs[] = {
3452 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3453 __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3454 __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3455 store_cursor_blink),
3458 static int fbcon_init_device(void)
3460 int i, error = 0;
3462 fbcon_has_sysfs = 1;
3464 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3465 error = device_create_file(fbcon_device, &device_attrs[i]);
3467 if (error)
3468 break;
3471 if (error) {
3472 while (--i >= 0)
3473 device_remove_file(fbcon_device, &device_attrs[i]);
3475 fbcon_has_sysfs = 0;
3478 return 0;
3481 static void fbcon_start(void)
3483 if (num_registered_fb) {
3484 int i;
3486 console_lock();
3488 for (i = 0; i < FB_MAX; i++) {
3489 if (registered_fb[i] != NULL) {
3490 info_idx = i;
3491 break;
3495 console_unlock();
3496 fbcon_takeover(0);
3500 static void fbcon_exit(void)
3502 struct fb_info *info;
3503 int i, j, mapped;
3505 if (fbcon_has_exited)
3506 return;
3508 kfree((void *)softback_buf);
3509 softback_buf = 0UL;
3511 for (i = 0; i < FB_MAX; i++) {
3512 int pending = 0;
3514 mapped = 0;
3515 info = registered_fb[i];
3517 if (info == NULL)
3518 continue;
3520 if (info->queue.func)
3521 pending = cancel_work_sync(&info->queue);
3522 DPRINTK("fbcon: %s pending work\n", (pending ? "canceled" :
3523 "no"));
3525 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3526 if (con2fb_map[j] == i)
3527 mapped = 1;
3530 if (mapped) {
3531 if (info->fbops->fb_release)
3532 info->fbops->fb_release(info, 0);
3533 module_put(info->fbops->owner);
3535 if (info->fbcon_par) {
3536 struct fbcon_ops *ops = info->fbcon_par;
3538 fbcon_del_cursor_timer(info);
3539 kfree(ops->cursor_src);
3540 kfree(info->fbcon_par);
3541 info->fbcon_par = NULL;
3544 if (info->queue.func == fb_flashcursor)
3545 info->queue.func = NULL;
3549 fbcon_has_exited = 1;
3552 static int __init fb_console_init(void)
3554 int i;
3556 console_lock();
3557 fb_register_client(&fbcon_event_notifier);
3558 fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3559 "fbcon");
3561 if (IS_ERR(fbcon_device)) {
3562 printk(KERN_WARNING "Unable to create device "
3563 "for fbcon; errno = %ld\n",
3564 PTR_ERR(fbcon_device));
3565 fbcon_device = NULL;
3566 } else
3567 fbcon_init_device();
3569 for (i = 0; i < MAX_NR_CONSOLES; i++)
3570 con2fb_map[i] = -1;
3572 console_unlock();
3573 fbcon_start();
3574 return 0;
3577 module_init(fb_console_init);
3579 #ifdef MODULE
3581 static void __exit fbcon_deinit_device(void)
3583 int i;
3585 if (fbcon_has_sysfs) {
3586 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3587 device_remove_file(fbcon_device, &device_attrs[i]);
3589 fbcon_has_sysfs = 0;
3593 static void __exit fb_console_exit(void)
3595 console_lock();
3596 fb_unregister_client(&fbcon_event_notifier);
3597 fbcon_deinit_device();
3598 device_destroy(fb_class, MKDEV(0, 0));
3599 fbcon_exit();
3600 console_unlock();
3601 unregister_con_driver(&fb_con);
3604 module_exit(fb_console_exit);
3606 #endif
3608 MODULE_LICENSE("GPL");