Linux 3.11-rc3
[cris-mirror.git] / drivers / video / console / fbcon.c
blobcd8a8027f8aeef2c9de03e9bd1ba9b7c4f040b35
1 /*
2 * linux/drivers/video/fbcon.c -- Low level frame buffer based console driver
4 * Copyright (C) 1995 Geert Uytterhoeven
7 * This file is based on the original Amiga console driver (amicon.c):
9 * Copyright (C) 1993 Hamish Macdonald
10 * Greg Harp
11 * Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
13 * with work by William Rucklidge (wjr@cs.cornell.edu)
14 * Geert Uytterhoeven
15 * Jes Sorensen (jds@kom.auc.dk)
16 * Martin Apel
18 * and on the original Atari console driver (atacon.c):
20 * Copyright (C) 1993 Bjoern Brauel
21 * Roman Hodek
23 * with work by Guenther Kelleter
24 * Martin Schaller
25 * Andreas Schwab
27 * Hardware cursor support added by Emmanuel Marty (core@ggi-project.org)
28 * Smart redraw scrolling, arbitrary font width support, 512char font support
29 * and software scrollback added by
30 * Jakub Jelinek (jj@ultra.linux.cz)
32 * Random hacking by Martin Mares <mj@ucw.cz>
34 * 2001 - Documented with DocBook
35 * - Brad Douglas <brad@neruo.com>
37 * The low level operations for the various display memory organizations are
38 * now in separate source files.
40 * Currently the following organizations are supported:
42 * o afb Amiga bitplanes
43 * o cfb{2,4,8,16,24,32} Packed pixels
44 * o ilbm Amiga interleaved bitplanes
45 * o iplan2p[248] Atari interleaved bitplanes
46 * o mfb Monochrome
47 * o vga VGA characters/attributes
49 * To do:
51 * - Implement 16 plane mode (iplan2p16)
54 * This file is subject to the terms and conditions of the GNU General Public
55 * License. See the file COPYING in the main directory of this archive for
56 * more details.
59 #undef FBCONDEBUG
61 #include <linux/module.h>
62 #include <linux/types.h>
63 #include <linux/fs.h>
64 #include <linux/kernel.h>
65 #include <linux/delay.h> /* MSch: for IRQ probe */
66 #include <linux/console.h>
67 #include <linux/string.h>
68 #include <linux/kd.h>
69 #include <linux/slab.h>
70 #include <linux/fb.h>
71 #include <linux/vt_kern.h>
72 #include <linux/selection.h>
73 #include <linux/font.h>
74 #include <linux/smp.h>
75 #include <linux/init.h>
76 #include <linux/interrupt.h>
77 #include <linux/crc32.h> /* For counting font checksums */
78 #include <asm/fb.h>
79 #include <asm/irq.h>
81 #include "fbcon.h"
83 #ifdef FBCONDEBUG
84 # define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__ , ## args)
85 #else
86 # define DPRINTK(fmt, args...)
87 #endif
89 enum {
90 FBCON_LOGO_CANSHOW = -1, /* the logo can be shown */
91 FBCON_LOGO_DRAW = -2, /* draw the logo to a console */
92 FBCON_LOGO_DONTSHOW = -3 /* do not show the logo */
95 static struct display fb_display[MAX_NR_CONSOLES];
97 static signed char con2fb_map[MAX_NR_CONSOLES];
98 static signed char con2fb_map_boot[MAX_NR_CONSOLES];
100 static int logo_lines;
101 /* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO
102 enums. */
103 static int logo_shown = FBCON_LOGO_CANSHOW;
104 /* Software scrollback */
105 static int fbcon_softback_size = 32768;
106 static unsigned long softback_buf, softback_curr;
107 static unsigned long softback_in;
108 static unsigned long softback_top, softback_end;
109 static int softback_lines;
110 /* console mappings */
111 static int first_fb_vc;
112 static int last_fb_vc = MAX_NR_CONSOLES - 1;
113 static int fbcon_is_default = 1;
114 static int fbcon_has_exited;
115 static int primary_device = -1;
116 static int fbcon_has_console_bind;
118 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
119 static int map_override;
121 static inline void fbcon_map_override(void)
123 map_override = 1;
125 #else
126 static inline void fbcon_map_override(void)
129 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY */
131 /* font data */
132 static char fontname[40];
134 /* current fb_info */
135 static int info_idx = -1;
137 /* console rotation */
138 static int initial_rotation;
139 static int fbcon_has_sysfs;
141 static const struct consw fb_con;
143 #define CM_SOFTBACK (8)
145 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
147 static int fbcon_set_origin(struct vc_data *);
149 #define CURSOR_DRAW_DELAY (1)
151 static int vbl_cursor_cnt;
152 static int fbcon_cursor_noblink;
154 #define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1)
157 * Interface used by the world
160 static const char *fbcon_startup(void);
161 static void fbcon_init(struct vc_data *vc, int init);
162 static void fbcon_deinit(struct vc_data *vc);
163 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
164 int width);
165 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
166 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
167 int count, int ypos, int xpos);
168 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
169 static void fbcon_cursor(struct vc_data *vc, int mode);
170 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
171 int count);
172 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
173 int height, int width);
174 static int fbcon_switch(struct vc_data *vc);
175 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
176 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
177 static int fbcon_scrolldelta(struct vc_data *vc, int lines);
180 * Internal routines
182 static __inline__ void ywrap_up(struct vc_data *vc, int count);
183 static __inline__ void ywrap_down(struct vc_data *vc, int count);
184 static __inline__ void ypan_up(struct vc_data *vc, int count);
185 static __inline__ void ypan_down(struct vc_data *vc, int count);
186 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
187 int dy, int dx, int height, int width, u_int y_break);
188 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
189 int unit);
190 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
191 int line, int count, int dy);
192 static void fbcon_modechanged(struct fb_info *info);
193 static void fbcon_set_all_vcs(struct fb_info *info);
194 static void fbcon_start(void);
195 static void fbcon_exit(void);
196 static struct device *fbcon_device;
198 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
199 static inline void fbcon_set_rotation(struct fb_info *info)
201 struct fbcon_ops *ops = info->fbcon_par;
203 if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
204 ops->p->con_rotate < 4)
205 ops->rotate = ops->p->con_rotate;
206 else
207 ops->rotate = 0;
210 static void fbcon_rotate(struct fb_info *info, u32 rotate)
212 struct fbcon_ops *ops= info->fbcon_par;
213 struct fb_info *fb_info;
215 if (!ops || ops->currcon == -1)
216 return;
218 fb_info = registered_fb[con2fb_map[ops->currcon]];
220 if (info == fb_info) {
221 struct display *p = &fb_display[ops->currcon];
223 if (rotate < 4)
224 p->con_rotate = rotate;
225 else
226 p->con_rotate = 0;
228 fbcon_modechanged(info);
232 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
234 struct fbcon_ops *ops = info->fbcon_par;
235 struct vc_data *vc;
236 struct display *p;
237 int i;
239 if (!ops || ops->currcon < 0 || rotate > 3)
240 return;
242 for (i = first_fb_vc; i <= last_fb_vc; i++) {
243 vc = vc_cons[i].d;
244 if (!vc || vc->vc_mode != KD_TEXT ||
245 registered_fb[con2fb_map[i]] != info)
246 continue;
248 p = &fb_display[vc->vc_num];
249 p->con_rotate = rotate;
252 fbcon_set_all_vcs(info);
254 #else
255 static inline void fbcon_set_rotation(struct fb_info *info)
257 struct fbcon_ops *ops = info->fbcon_par;
259 ops->rotate = FB_ROTATE_UR;
262 static void fbcon_rotate(struct fb_info *info, u32 rotate)
264 return;
267 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
269 return;
271 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
273 static int fbcon_get_rotate(struct fb_info *info)
275 struct fbcon_ops *ops = info->fbcon_par;
277 return (ops) ? ops->rotate : 0;
280 static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
282 struct fbcon_ops *ops = info->fbcon_par;
284 return (info->state != FBINFO_STATE_RUNNING ||
285 vc->vc_mode != KD_TEXT || ops->graphics) &&
286 !vt_force_oops_output(vc);
289 static int get_color(struct vc_data *vc, struct fb_info *info,
290 u16 c, int is_fg)
292 int depth = fb_get_color_depth(&info->var, &info->fix);
293 int color = 0;
295 if (console_blanked) {
296 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
298 c = vc->vc_video_erase_char & charmask;
301 if (depth != 1)
302 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
303 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
305 switch (depth) {
306 case 1:
308 int col = mono_col(info);
309 /* 0 or 1 */
310 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
311 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
313 if (console_blanked)
314 fg = bg;
316 color = (is_fg) ? fg : bg;
317 break;
319 case 2:
321 * Scale down 16-colors to 4 colors. Default 4-color palette
322 * is grayscale. However, simply dividing the values by 4
323 * will not work, as colors 1, 2 and 3 will be scaled-down
324 * to zero rendering them invisible. So empirically convert
325 * colors to a sane 4-level grayscale.
327 switch (color) {
328 case 0:
329 color = 0; /* black */
330 break;
331 case 1 ... 6:
332 color = 2; /* white */
333 break;
334 case 7 ... 8:
335 color = 1; /* gray */
336 break;
337 default:
338 color = 3; /* intense white */
339 break;
341 break;
342 case 3:
344 * Last 8 entries of default 16-color palette is a more intense
345 * version of the first 8 (i.e., same chrominance, different
346 * luminance).
348 color &= 7;
349 break;
353 return color;
356 static void fbcon_update_softback(struct vc_data *vc)
358 int l = fbcon_softback_size / vc->vc_size_row;
360 if (l > 5)
361 softback_end = softback_buf + l * vc->vc_size_row;
362 else
363 /* Smaller scrollback makes no sense, and 0 would screw
364 the operation totally */
365 softback_top = 0;
368 static void fb_flashcursor(struct work_struct *work)
370 struct fb_info *info = container_of(work, struct fb_info, queue);
371 struct fbcon_ops *ops = info->fbcon_par;
372 struct vc_data *vc = NULL;
373 int c;
374 int mode;
375 int ret;
377 /* FIXME: we should sort out the unbind locking instead */
378 /* instead we just fail to flash the cursor if we can't get
379 * the lock instead of blocking fbcon deinit */
380 ret = console_trylock();
381 if (ret == 0)
382 return;
384 if (ops && ops->currcon != -1)
385 vc = vc_cons[ops->currcon].d;
387 if (!vc || !CON_IS_VISIBLE(vc) ||
388 registered_fb[con2fb_map[vc->vc_num]] != info ||
389 vc->vc_deccm != 1) {
390 console_unlock();
391 return;
394 c = scr_readw((u16 *) vc->vc_pos);
395 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
396 CM_ERASE : CM_DRAW;
397 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
398 get_color(vc, info, c, 0));
399 console_unlock();
402 static void cursor_timer_handler(unsigned long dev_addr)
404 struct fb_info *info = (struct fb_info *) dev_addr;
405 struct fbcon_ops *ops = info->fbcon_par;
407 queue_work(system_power_efficient_wq, &info->queue);
408 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
411 static void fbcon_add_cursor_timer(struct fb_info *info)
413 struct fbcon_ops *ops = info->fbcon_par;
415 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
416 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
417 !fbcon_cursor_noblink) {
418 if (!info->queue.func)
419 INIT_WORK(&info->queue, fb_flashcursor);
421 init_timer(&ops->cursor_timer);
422 ops->cursor_timer.function = cursor_timer_handler;
423 ops->cursor_timer.expires = jiffies + HZ / 5;
424 ops->cursor_timer.data = (unsigned long ) info;
425 add_timer(&ops->cursor_timer);
426 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
430 static void fbcon_del_cursor_timer(struct fb_info *info)
432 struct fbcon_ops *ops = info->fbcon_par;
434 if (info->queue.func == fb_flashcursor &&
435 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
436 del_timer_sync(&ops->cursor_timer);
437 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
441 #ifndef MODULE
442 static int __init fb_console_setup(char *this_opt)
444 char *options;
445 int i, j;
447 if (!this_opt || !*this_opt)
448 return 1;
450 while ((options = strsep(&this_opt, ",")) != NULL) {
451 if (!strncmp(options, "font:", 5))
452 strlcpy(fontname, options + 5, sizeof(fontname));
454 if (!strncmp(options, "scrollback:", 11)) {
455 options += 11;
456 if (*options) {
457 fbcon_softback_size = simple_strtoul(options, &options, 0);
458 if (*options == 'k' || *options == 'K') {
459 fbcon_softback_size *= 1024;
460 options++;
462 if (*options != ',')
463 return 1;
464 options++;
465 } else
466 return 1;
469 if (!strncmp(options, "map:", 4)) {
470 options += 4;
471 if (*options) {
472 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
473 if (!options[j])
474 j = 0;
475 con2fb_map_boot[i] =
476 (options[j++]-'0') % FB_MAX;
479 fbcon_map_override();
482 return 1;
485 if (!strncmp(options, "vc:", 3)) {
486 options += 3;
487 if (*options)
488 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
489 if (first_fb_vc < 0)
490 first_fb_vc = 0;
491 if (*options++ == '-')
492 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
493 fbcon_is_default = 0;
496 if (!strncmp(options, "rotate:", 7)) {
497 options += 7;
498 if (*options)
499 initial_rotation = simple_strtoul(options, &options, 0);
500 if (initial_rotation > 3)
501 initial_rotation = 0;
504 return 1;
507 __setup("fbcon=", fb_console_setup);
508 #endif
510 static int search_fb_in_map(int idx)
512 int i, retval = 0;
514 for (i = first_fb_vc; i <= last_fb_vc; i++) {
515 if (con2fb_map[i] == idx)
516 retval = 1;
518 return retval;
521 static int search_for_mapped_con(void)
523 int i, retval = 0;
525 for (i = first_fb_vc; i <= last_fb_vc; i++) {
526 if (con2fb_map[i] != -1)
527 retval = 1;
529 return retval;
532 static int do_fbcon_takeover(int show_logo)
534 int err, i;
536 if (!num_registered_fb)
537 return -ENODEV;
539 if (!show_logo)
540 logo_shown = FBCON_LOGO_DONTSHOW;
542 for (i = first_fb_vc; i <= last_fb_vc; i++)
543 con2fb_map[i] = info_idx;
545 err = do_take_over_console(&fb_con, first_fb_vc, last_fb_vc,
546 fbcon_is_default);
548 if (err) {
549 for (i = first_fb_vc; i <= last_fb_vc; i++)
550 con2fb_map[i] = -1;
551 info_idx = -1;
552 } else {
553 fbcon_has_console_bind = 1;
556 return err;
559 #ifdef MODULE
560 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
561 int cols, int rows, int new_cols, int new_rows)
563 logo_shown = FBCON_LOGO_DONTSHOW;
565 #else
566 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
567 int cols, int rows, int new_cols, int new_rows)
569 /* Need to make room for the logo */
570 struct fbcon_ops *ops = info->fbcon_par;
571 int cnt, erase = vc->vc_video_erase_char, step;
572 unsigned short *save = NULL, *r, *q;
573 int logo_height;
575 if (info->flags & FBINFO_MODULE) {
576 logo_shown = FBCON_LOGO_DONTSHOW;
577 return;
581 * remove underline attribute from erase character
582 * if black and white framebuffer.
584 if (fb_get_color_depth(&info->var, &info->fix) == 1)
585 erase &= ~0x400;
586 logo_height = fb_prepare_logo(info, ops->rotate);
587 logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
588 q = (unsigned short *) (vc->vc_origin +
589 vc->vc_size_row * rows);
590 step = logo_lines * cols;
591 for (r = q - logo_lines * cols; r < q; r++)
592 if (scr_readw(r) != vc->vc_video_erase_char)
593 break;
594 if (r != q && new_rows >= rows + logo_lines) {
595 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
596 if (save) {
597 int i = cols < new_cols ? cols : new_cols;
598 scr_memsetw(save, erase, logo_lines * new_cols * 2);
599 r = q - step;
600 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
601 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
602 r = q;
605 if (r == q) {
606 /* We can scroll screen down */
607 r = q - step - cols;
608 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
609 scr_memcpyw(r + step, r, vc->vc_size_row);
610 r -= cols;
612 if (!save) {
613 int lines;
614 if (vc->vc_y + logo_lines >= rows)
615 lines = rows - vc->vc_y - 1;
616 else
617 lines = logo_lines;
618 vc->vc_y += lines;
619 vc->vc_pos += lines * vc->vc_size_row;
622 scr_memsetw((unsigned short *) vc->vc_origin,
623 erase,
624 vc->vc_size_row * logo_lines);
626 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
627 fbcon_clear_margins(vc, 0);
628 update_screen(vc);
631 if (save) {
632 q = (unsigned short *) (vc->vc_origin +
633 vc->vc_size_row *
634 rows);
635 scr_memcpyw(q, save, logo_lines * new_cols * 2);
636 vc->vc_y += logo_lines;
637 vc->vc_pos += logo_lines * vc->vc_size_row;
638 kfree(save);
641 if (logo_lines > vc->vc_bottom) {
642 logo_shown = FBCON_LOGO_CANSHOW;
643 printk(KERN_INFO
644 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
645 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
646 logo_shown = FBCON_LOGO_DRAW;
647 vc->vc_top = logo_lines;
650 #endif /* MODULE */
652 #ifdef CONFIG_FB_TILEBLITTING
653 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
655 struct fbcon_ops *ops = info->fbcon_par;
657 ops->p = &fb_display[vc->vc_num];
659 if ((info->flags & FBINFO_MISC_TILEBLITTING))
660 fbcon_set_tileops(vc, info);
661 else {
662 fbcon_set_rotation(info);
663 fbcon_set_bitops(ops);
667 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
669 int err = 0;
671 if (info->flags & FBINFO_MISC_TILEBLITTING &&
672 info->tileops->fb_get_tilemax(info) < charcount)
673 err = 1;
675 return err;
677 #else
678 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
680 struct fbcon_ops *ops = info->fbcon_par;
682 info->flags &= ~FBINFO_MISC_TILEBLITTING;
683 ops->p = &fb_display[vc->vc_num];
684 fbcon_set_rotation(info);
685 fbcon_set_bitops(ops);
688 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
690 return 0;
693 #endif /* CONFIG_MISC_TILEBLITTING */
696 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
697 int unit, int oldidx)
699 struct fbcon_ops *ops = NULL;
700 int err = 0;
702 if (!try_module_get(info->fbops->owner))
703 err = -ENODEV;
705 if (!err && info->fbops->fb_open &&
706 info->fbops->fb_open(info, 0))
707 err = -ENODEV;
709 if (!err) {
710 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
711 if (!ops)
712 err = -ENOMEM;
715 if (!err) {
716 info->fbcon_par = ops;
718 if (vc)
719 set_blitting_type(vc, info);
722 if (err) {
723 con2fb_map[unit] = oldidx;
724 module_put(info->fbops->owner);
727 return err;
730 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
731 struct fb_info *newinfo, int unit,
732 int oldidx, int found)
734 struct fbcon_ops *ops = oldinfo->fbcon_par;
735 int err = 0, ret;
737 if (oldinfo->fbops->fb_release &&
738 oldinfo->fbops->fb_release(oldinfo, 0)) {
739 con2fb_map[unit] = oldidx;
740 if (!found && newinfo->fbops->fb_release)
741 newinfo->fbops->fb_release(newinfo, 0);
742 if (!found)
743 module_put(newinfo->fbops->owner);
744 err = -ENODEV;
747 if (!err) {
748 fbcon_del_cursor_timer(oldinfo);
749 kfree(ops->cursor_state.mask);
750 kfree(ops->cursor_data);
751 kfree(ops->fontbuffer);
752 kfree(oldinfo->fbcon_par);
753 oldinfo->fbcon_par = NULL;
754 module_put(oldinfo->fbops->owner);
756 If oldinfo and newinfo are driving the same hardware,
757 the fb_release() method of oldinfo may attempt to
758 restore the hardware state. This will leave the
759 newinfo in an undefined state. Thus, a call to
760 fb_set_par() may be needed for the newinfo.
762 if (newinfo->fbops->fb_set_par) {
763 ret = newinfo->fbops->fb_set_par(newinfo);
765 if (ret)
766 printk(KERN_ERR "con2fb_release_oldinfo: "
767 "detected unhandled fb_set_par error, "
768 "error code %d\n", ret);
772 return err;
775 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
776 int unit, int show_logo)
778 struct fbcon_ops *ops = info->fbcon_par;
779 int ret;
781 ops->currcon = fg_console;
783 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT)) {
784 ret = info->fbops->fb_set_par(info);
786 if (ret)
787 printk(KERN_ERR "con2fb_init_display: detected "
788 "unhandled fb_set_par error, "
789 "error code %d\n", ret);
792 ops->flags |= FBCON_FLAGS_INIT;
793 ops->graphics = 0;
794 fbcon_set_disp(info, &info->var, unit);
796 if (show_logo) {
797 struct vc_data *fg_vc = vc_cons[fg_console].d;
798 struct fb_info *fg_info =
799 registered_fb[con2fb_map[fg_console]];
801 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
802 fg_vc->vc_rows, fg_vc->vc_cols,
803 fg_vc->vc_rows);
806 update_screen(vc_cons[fg_console].d);
810 * set_con2fb_map - map console to frame buffer device
811 * @unit: virtual console number to map
812 * @newidx: frame buffer index to map virtual console to
813 * @user: user request
815 * Maps a virtual console @unit to a frame buffer device
816 * @newidx.
818 * This should be called with the console lock held.
820 static int set_con2fb_map(int unit, int newidx, int user)
822 struct vc_data *vc = vc_cons[unit].d;
823 int oldidx = con2fb_map[unit];
824 struct fb_info *info = registered_fb[newidx];
825 struct fb_info *oldinfo = NULL;
826 int found, err = 0;
828 if (oldidx == newidx)
829 return 0;
831 if (!info)
832 return -EINVAL;
834 if (!search_for_mapped_con() || !con_is_bound(&fb_con)) {
835 info_idx = newidx;
836 return do_fbcon_takeover(0);
839 if (oldidx != -1)
840 oldinfo = registered_fb[oldidx];
842 found = search_fb_in_map(newidx);
844 con2fb_map[unit] = newidx;
845 if (!err && !found)
846 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
850 * If old fb is not mapped to any of the consoles,
851 * fbcon should release it.
853 if (!err && oldinfo && !search_fb_in_map(oldidx))
854 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
855 found);
857 if (!err) {
858 int show_logo = (fg_console == 0 && !user &&
859 logo_shown != FBCON_LOGO_DONTSHOW);
861 if (!found)
862 fbcon_add_cursor_timer(info);
863 con2fb_map_boot[unit] = newidx;
864 con2fb_init_display(vc, info, unit, show_logo);
867 if (!search_fb_in_map(info_idx))
868 info_idx = newidx;
870 return err;
874 * Low Level Operations
876 /* NOTE: fbcon cannot be __init: it may be called from do_take_over_console later */
877 static int var_to_display(struct display *disp,
878 struct fb_var_screeninfo *var,
879 struct fb_info *info)
881 disp->xres_virtual = var->xres_virtual;
882 disp->yres_virtual = var->yres_virtual;
883 disp->bits_per_pixel = var->bits_per_pixel;
884 disp->grayscale = var->grayscale;
885 disp->nonstd = var->nonstd;
886 disp->accel_flags = var->accel_flags;
887 disp->height = var->height;
888 disp->width = var->width;
889 disp->red = var->red;
890 disp->green = var->green;
891 disp->blue = var->blue;
892 disp->transp = var->transp;
893 disp->rotate = var->rotate;
894 disp->mode = fb_match_mode(var, &info->modelist);
895 if (disp->mode == NULL)
896 /* This should not happen */
897 return -EINVAL;
898 return 0;
901 static void display_to_var(struct fb_var_screeninfo *var,
902 struct display *disp)
904 fb_videomode_to_var(var, disp->mode);
905 var->xres_virtual = disp->xres_virtual;
906 var->yres_virtual = disp->yres_virtual;
907 var->bits_per_pixel = disp->bits_per_pixel;
908 var->grayscale = disp->grayscale;
909 var->nonstd = disp->nonstd;
910 var->accel_flags = disp->accel_flags;
911 var->height = disp->height;
912 var->width = disp->width;
913 var->red = disp->red;
914 var->green = disp->green;
915 var->blue = disp->blue;
916 var->transp = disp->transp;
917 var->rotate = disp->rotate;
920 static const char *fbcon_startup(void)
922 const char *display_desc = "frame buffer device";
923 struct display *p = &fb_display[fg_console];
924 struct vc_data *vc = vc_cons[fg_console].d;
925 const struct font_desc *font = NULL;
926 struct module *owner;
927 struct fb_info *info = NULL;
928 struct fbcon_ops *ops;
929 int rows, cols;
932 * If num_registered_fb is zero, this is a call for the dummy part.
933 * The frame buffer devices weren't initialized yet.
935 if (!num_registered_fb || info_idx == -1)
936 return display_desc;
938 * Instead of blindly using registered_fb[0], we use info_idx, set by
939 * fb_console_init();
941 info = registered_fb[info_idx];
942 if (!info)
943 return NULL;
945 owner = info->fbops->owner;
946 if (!try_module_get(owner))
947 return NULL;
948 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
949 module_put(owner);
950 return NULL;
953 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
954 if (!ops) {
955 module_put(owner);
956 return NULL;
959 ops->currcon = -1;
960 ops->graphics = 1;
961 ops->cur_rotate = -1;
962 info->fbcon_par = ops;
963 p->con_rotate = initial_rotation;
964 set_blitting_type(vc, info);
966 if (info->fix.type != FB_TYPE_TEXT) {
967 if (fbcon_softback_size) {
968 if (!softback_buf) {
969 softback_buf =
970 (unsigned long)
971 kmalloc(fbcon_softback_size,
972 GFP_KERNEL);
973 if (!softback_buf) {
974 fbcon_softback_size = 0;
975 softback_top = 0;
978 } else {
979 if (softback_buf) {
980 kfree((void *) softback_buf);
981 softback_buf = 0;
982 softback_top = 0;
985 if (softback_buf)
986 softback_in = softback_top = softback_curr =
987 softback_buf;
988 softback_lines = 0;
991 /* Setup default font */
992 if (!p->fontdata && !vc->vc_font.data) {
993 if (!fontname[0] || !(font = find_font(fontname)))
994 font = get_default_font(info->var.xres,
995 info->var.yres,
996 info->pixmap.blit_x,
997 info->pixmap.blit_y);
998 vc->vc_font.width = font->width;
999 vc->vc_font.height = font->height;
1000 vc->vc_font.data = (void *)(p->fontdata = font->data);
1001 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1002 } else {
1003 p->fontdata = vc->vc_font.data;
1006 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1007 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1008 cols /= vc->vc_font.width;
1009 rows /= vc->vc_font.height;
1010 vc_resize(vc, cols, rows);
1012 DPRINTK("mode: %s\n", info->fix.id);
1013 DPRINTK("visual: %d\n", info->fix.visual);
1014 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1015 info->var.yres,
1016 info->var.bits_per_pixel);
1018 fbcon_add_cursor_timer(info);
1019 fbcon_has_exited = 0;
1020 return display_desc;
1023 static void fbcon_init(struct vc_data *vc, int init)
1025 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1026 struct fbcon_ops *ops;
1027 struct vc_data **default_mode = vc->vc_display_fg;
1028 struct vc_data *svc = *default_mode;
1029 struct display *t, *p = &fb_display[vc->vc_num];
1030 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
1031 int cap, ret;
1033 if (info_idx == -1 || info == NULL)
1034 return;
1036 cap = info->flags;
1038 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1039 (info->fix.type == FB_TYPE_TEXT))
1040 logo = 0;
1042 if (var_to_display(p, &info->var, info))
1043 return;
1045 if (!info->fbcon_par)
1046 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1048 /* If we are not the first console on this
1049 fb, copy the font from that console */
1050 t = &fb_display[fg_console];
1051 if (!p->fontdata) {
1052 if (t->fontdata) {
1053 struct vc_data *fvc = vc_cons[fg_console].d;
1055 vc->vc_font.data = (void *)(p->fontdata =
1056 fvc->vc_font.data);
1057 vc->vc_font.width = fvc->vc_font.width;
1058 vc->vc_font.height = fvc->vc_font.height;
1059 p->userfont = t->userfont;
1061 if (p->userfont)
1062 REFCOUNT(p->fontdata)++;
1063 } else {
1064 const struct font_desc *font = NULL;
1066 if (!fontname[0] || !(font = find_font(fontname)))
1067 font = get_default_font(info->var.xres,
1068 info->var.yres,
1069 info->pixmap.blit_x,
1070 info->pixmap.blit_y);
1071 vc->vc_font.width = font->width;
1072 vc->vc_font.height = font->height;
1073 vc->vc_font.data = (void *)(p->fontdata = font->data);
1074 vc->vc_font.charcount = 256; /* FIXME Need to
1075 support more fonts */
1079 if (p->userfont)
1080 charcnt = FNTCHARCNT(p->fontdata);
1082 vc->vc_panic_force_write = !!(info->flags & FBINFO_CAN_FORCE_OUTPUT);
1083 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1084 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1085 if (charcnt == 256) {
1086 vc->vc_hi_font_mask = 0;
1087 } else {
1088 vc->vc_hi_font_mask = 0x100;
1089 if (vc->vc_can_do_color)
1090 vc->vc_complement_mask <<= 1;
1093 if (!*svc->vc_uni_pagedir_loc)
1094 con_set_default_unimap(svc);
1095 if (!*vc->vc_uni_pagedir_loc)
1096 con_copy_unimap(vc, svc);
1098 ops = info->fbcon_par;
1099 p->con_rotate = initial_rotation;
1100 set_blitting_type(vc, info);
1102 cols = vc->vc_cols;
1103 rows = vc->vc_rows;
1104 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1105 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1106 new_cols /= vc->vc_font.width;
1107 new_rows /= vc->vc_font.height;
1110 * We must always set the mode. The mode of the previous console
1111 * driver could be in the same resolution but we are using different
1112 * hardware so we have to initialize the hardware.
1114 * We need to do it in fbcon_init() to prevent screen corruption.
1116 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
1117 if (info->fbops->fb_set_par &&
1118 !(ops->flags & FBCON_FLAGS_INIT)) {
1119 ret = info->fbops->fb_set_par(info);
1121 if (ret)
1122 printk(KERN_ERR "fbcon_init: detected "
1123 "unhandled fb_set_par error, "
1124 "error code %d\n", ret);
1127 ops->flags |= FBCON_FLAGS_INIT;
1130 ops->graphics = 0;
1132 if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1133 !(cap & FBINFO_HWACCEL_DISABLED))
1134 p->scrollmode = SCROLL_MOVE;
1135 else /* default to something safe */
1136 p->scrollmode = SCROLL_REDRAW;
1139 * ++guenther: console.c:vc_allocate() relies on initializing
1140 * vc_{cols,rows}, but we must not set those if we are only
1141 * resizing the console.
1143 if (init) {
1144 vc->vc_cols = new_cols;
1145 vc->vc_rows = new_rows;
1146 } else
1147 vc_resize(vc, new_cols, new_rows);
1149 if (logo)
1150 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1152 if (vc == svc && softback_buf)
1153 fbcon_update_softback(vc);
1155 if (ops->rotate_font && ops->rotate_font(info, vc)) {
1156 ops->rotate = FB_ROTATE_UR;
1157 set_blitting_type(vc, info);
1160 ops->p = &fb_display[fg_console];
1163 static void fbcon_free_font(struct display *p, bool freefont)
1165 if (freefont && p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1166 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1167 p->fontdata = NULL;
1168 p->userfont = 0;
1171 static void fbcon_deinit(struct vc_data *vc)
1173 struct display *p = &fb_display[vc->vc_num];
1174 struct fb_info *info;
1175 struct fbcon_ops *ops;
1176 int idx;
1177 bool free_font = true;
1179 idx = con2fb_map[vc->vc_num];
1181 if (idx == -1)
1182 goto finished;
1184 info = registered_fb[idx];
1186 if (!info)
1187 goto finished;
1189 if (info->flags & FBINFO_MISC_FIRMWARE)
1190 free_font = false;
1191 ops = info->fbcon_par;
1193 if (!ops)
1194 goto finished;
1196 if (CON_IS_VISIBLE(vc))
1197 fbcon_del_cursor_timer(info);
1199 ops->flags &= ~FBCON_FLAGS_INIT;
1200 finished:
1202 fbcon_free_font(p, free_font);
1203 if (free_font)
1204 vc->vc_font.data = NULL;
1206 if (!con_is_bound(&fb_con))
1207 fbcon_exit();
1209 return;
1212 /* ====================================================================== */
1214 /* fbcon_XXX routines - interface used by the world
1216 * This system is now divided into two levels because of complications
1217 * caused by hardware scrolling. Top level functions:
1219 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1221 * handles y values in range [0, scr_height-1] that correspond to real
1222 * screen positions. y_wrap shift means that first line of bitmap may be
1223 * anywhere on this display. These functions convert lineoffsets to
1224 * bitmap offsets and deal with the wrap-around case by splitting blits.
1226 * fbcon_bmove_physical_8() -- These functions fast implementations
1227 * fbcon_clear_physical_8() -- of original fbcon_XXX fns.
1228 * fbcon_putc_physical_8() -- (font width != 8) may be added later
1230 * WARNING:
1232 * At the moment fbcon_putc() cannot blit across vertical wrap boundary
1233 * Implies should only really hardware scroll in rows. Only reason for
1234 * restriction is simplicity & efficiency at the moment.
1237 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1238 int width)
1240 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1241 struct fbcon_ops *ops = info->fbcon_par;
1243 struct display *p = &fb_display[vc->vc_num];
1244 u_int y_break;
1246 if (fbcon_is_inactive(vc, info))
1247 return;
1249 if (!height || !width)
1250 return;
1252 if (sy < vc->vc_top && vc->vc_top == logo_lines) {
1253 vc->vc_top = 0;
1255 * If the font dimensions are not an integral of the display
1256 * dimensions then the ops->clear below won't end up clearing
1257 * the margins. Call clear_margins here in case the logo
1258 * bitmap stretched into the margin area.
1260 fbcon_clear_margins(vc, 0);
1263 /* Split blits that cross physical y_wrap boundary */
1265 y_break = p->vrows - p->yscroll;
1266 if (sy < y_break && sy + height - 1 >= y_break) {
1267 u_int b = y_break - sy;
1268 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1269 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1270 width);
1271 } else
1272 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1275 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1276 int count, int ypos, int xpos)
1278 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1279 struct display *p = &fb_display[vc->vc_num];
1280 struct fbcon_ops *ops = info->fbcon_par;
1282 if (!fbcon_is_inactive(vc, info))
1283 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1284 get_color(vc, info, scr_readw(s), 1),
1285 get_color(vc, info, scr_readw(s), 0));
1288 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1290 unsigned short chr;
1292 scr_writew(c, &chr);
1293 fbcon_putcs(vc, &chr, 1, ypos, xpos);
1296 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1298 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1299 struct fbcon_ops *ops = info->fbcon_par;
1301 if (!fbcon_is_inactive(vc, info))
1302 ops->clear_margins(vc, info, bottom_only);
1305 static void fbcon_cursor(struct vc_data *vc, int mode)
1307 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1308 struct fbcon_ops *ops = info->fbcon_par;
1309 int y;
1310 int c = scr_readw((u16 *) vc->vc_pos);
1312 if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1313 return;
1315 if (vc->vc_cursor_type & 0x10)
1316 fbcon_del_cursor_timer(info);
1317 else
1318 fbcon_add_cursor_timer(info);
1320 ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1321 if (mode & CM_SOFTBACK) {
1322 mode &= ~CM_SOFTBACK;
1323 y = softback_lines;
1324 } else {
1325 if (softback_lines)
1326 fbcon_set_origin(vc);
1327 y = 0;
1330 ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1331 get_color(vc, info, c, 0));
1332 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1335 static int scrollback_phys_max = 0;
1336 static int scrollback_max = 0;
1337 static int scrollback_current = 0;
1339 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1340 int unit)
1342 struct display *p, *t;
1343 struct vc_data **default_mode, *vc;
1344 struct vc_data *svc;
1345 struct fbcon_ops *ops = info->fbcon_par;
1346 int rows, cols, charcnt = 256;
1348 p = &fb_display[unit];
1350 if (var_to_display(p, var, info))
1351 return;
1353 vc = vc_cons[unit].d;
1355 if (!vc)
1356 return;
1358 default_mode = vc->vc_display_fg;
1359 svc = *default_mode;
1360 t = &fb_display[svc->vc_num];
1362 if (!vc->vc_font.data) {
1363 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1364 vc->vc_font.width = (*default_mode)->vc_font.width;
1365 vc->vc_font.height = (*default_mode)->vc_font.height;
1366 p->userfont = t->userfont;
1367 if (p->userfont)
1368 REFCOUNT(p->fontdata)++;
1370 if (p->userfont)
1371 charcnt = FNTCHARCNT(p->fontdata);
1373 var->activate = FB_ACTIVATE_NOW;
1374 info->var.activate = var->activate;
1375 var->yoffset = info->var.yoffset;
1376 var->xoffset = info->var.xoffset;
1377 fb_set_var(info, var);
1378 ops->var = info->var;
1379 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1380 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1381 if (charcnt == 256) {
1382 vc->vc_hi_font_mask = 0;
1383 } else {
1384 vc->vc_hi_font_mask = 0x100;
1385 if (vc->vc_can_do_color)
1386 vc->vc_complement_mask <<= 1;
1389 if (!*svc->vc_uni_pagedir_loc)
1390 con_set_default_unimap(svc);
1391 if (!*vc->vc_uni_pagedir_loc)
1392 con_copy_unimap(vc, svc);
1394 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1395 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1396 cols /= vc->vc_font.width;
1397 rows /= vc->vc_font.height;
1398 vc_resize(vc, cols, rows);
1400 if (CON_IS_VISIBLE(vc)) {
1401 update_screen(vc);
1402 if (softback_buf)
1403 fbcon_update_softback(vc);
1407 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1409 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1410 struct fbcon_ops *ops = info->fbcon_par;
1411 struct display *p = &fb_display[vc->vc_num];
1413 p->yscroll += count;
1414 if (p->yscroll >= p->vrows) /* Deal with wrap */
1415 p->yscroll -= p->vrows;
1416 ops->var.xoffset = 0;
1417 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1418 ops->var.vmode |= FB_VMODE_YWRAP;
1419 ops->update_start(info);
1420 scrollback_max += count;
1421 if (scrollback_max > scrollback_phys_max)
1422 scrollback_max = scrollback_phys_max;
1423 scrollback_current = 0;
1426 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1428 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1429 struct fbcon_ops *ops = info->fbcon_par;
1430 struct display *p = &fb_display[vc->vc_num];
1432 p->yscroll -= count;
1433 if (p->yscroll < 0) /* Deal with wrap */
1434 p->yscroll += p->vrows;
1435 ops->var.xoffset = 0;
1436 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1437 ops->var.vmode |= FB_VMODE_YWRAP;
1438 ops->update_start(info);
1439 scrollback_max -= count;
1440 if (scrollback_max < 0)
1441 scrollback_max = 0;
1442 scrollback_current = 0;
1445 static __inline__ void ypan_up(struct vc_data *vc, int count)
1447 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1448 struct display *p = &fb_display[vc->vc_num];
1449 struct fbcon_ops *ops = info->fbcon_par;
1451 p->yscroll += count;
1452 if (p->yscroll > p->vrows - vc->vc_rows) {
1453 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1454 0, 0, 0, vc->vc_rows, vc->vc_cols);
1455 p->yscroll -= p->vrows - vc->vc_rows;
1458 ops->var.xoffset = 0;
1459 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1460 ops->var.vmode &= ~FB_VMODE_YWRAP;
1461 ops->update_start(info);
1462 fbcon_clear_margins(vc, 1);
1463 scrollback_max += count;
1464 if (scrollback_max > scrollback_phys_max)
1465 scrollback_max = scrollback_phys_max;
1466 scrollback_current = 0;
1469 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1471 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1472 struct fbcon_ops *ops = info->fbcon_par;
1473 struct display *p = &fb_display[vc->vc_num];
1475 p->yscroll += count;
1477 if (p->yscroll > p->vrows - vc->vc_rows) {
1478 p->yscroll -= p->vrows - vc->vc_rows;
1479 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1482 ops->var.xoffset = 0;
1483 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1484 ops->var.vmode &= ~FB_VMODE_YWRAP;
1485 ops->update_start(info);
1486 fbcon_clear_margins(vc, 1);
1487 scrollback_max += count;
1488 if (scrollback_max > scrollback_phys_max)
1489 scrollback_max = scrollback_phys_max;
1490 scrollback_current = 0;
1493 static __inline__ void ypan_down(struct vc_data *vc, int count)
1495 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1496 struct display *p = &fb_display[vc->vc_num];
1497 struct fbcon_ops *ops = info->fbcon_par;
1499 p->yscroll -= count;
1500 if (p->yscroll < 0) {
1501 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1502 0, vc->vc_rows, vc->vc_cols);
1503 p->yscroll += p->vrows - vc->vc_rows;
1506 ops->var.xoffset = 0;
1507 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1508 ops->var.vmode &= ~FB_VMODE_YWRAP;
1509 ops->update_start(info);
1510 fbcon_clear_margins(vc, 1);
1511 scrollback_max -= count;
1512 if (scrollback_max < 0)
1513 scrollback_max = 0;
1514 scrollback_current = 0;
1517 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1519 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1520 struct fbcon_ops *ops = info->fbcon_par;
1521 struct display *p = &fb_display[vc->vc_num];
1523 p->yscroll -= count;
1525 if (p->yscroll < 0) {
1526 p->yscroll += p->vrows - vc->vc_rows;
1527 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1530 ops->var.xoffset = 0;
1531 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1532 ops->var.vmode &= ~FB_VMODE_YWRAP;
1533 ops->update_start(info);
1534 fbcon_clear_margins(vc, 1);
1535 scrollback_max -= count;
1536 if (scrollback_max < 0)
1537 scrollback_max = 0;
1538 scrollback_current = 0;
1541 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1542 long delta)
1544 int count = vc->vc_rows;
1545 unsigned short *d, *s;
1546 unsigned long n;
1547 int line = 0;
1549 d = (u16 *) softback_curr;
1550 if (d == (u16 *) softback_in)
1551 d = (u16 *) vc->vc_origin;
1552 n = softback_curr + delta * vc->vc_size_row;
1553 softback_lines -= delta;
1554 if (delta < 0) {
1555 if (softback_curr < softback_top && n < softback_buf) {
1556 n += softback_end - softback_buf;
1557 if (n < softback_top) {
1558 softback_lines -=
1559 (softback_top - n) / vc->vc_size_row;
1560 n = softback_top;
1562 } else if (softback_curr >= softback_top
1563 && n < softback_top) {
1564 softback_lines -=
1565 (softback_top - n) / vc->vc_size_row;
1566 n = softback_top;
1568 } else {
1569 if (softback_curr > softback_in && n >= softback_end) {
1570 n += softback_buf - softback_end;
1571 if (n > softback_in) {
1572 n = softback_in;
1573 softback_lines = 0;
1575 } else if (softback_curr <= softback_in && n > softback_in) {
1576 n = softback_in;
1577 softback_lines = 0;
1580 if (n == softback_curr)
1581 return;
1582 softback_curr = n;
1583 s = (u16 *) softback_curr;
1584 if (s == (u16 *) softback_in)
1585 s = (u16 *) vc->vc_origin;
1586 while (count--) {
1587 unsigned short *start;
1588 unsigned short *le;
1589 unsigned short c;
1590 int x = 0;
1591 unsigned short attr = 1;
1593 start = s;
1594 le = advance_row(s, 1);
1595 do {
1596 c = scr_readw(s);
1597 if (attr != (c & 0xff00)) {
1598 attr = c & 0xff00;
1599 if (s > start) {
1600 fbcon_putcs(vc, start, s - start,
1601 line, x);
1602 x += s - start;
1603 start = s;
1606 if (c == scr_readw(d)) {
1607 if (s > start) {
1608 fbcon_putcs(vc, start, s - start,
1609 line, x);
1610 x += s - start + 1;
1611 start = s + 1;
1612 } else {
1613 x++;
1614 start++;
1617 s++;
1618 d++;
1619 } while (s < le);
1620 if (s > start)
1621 fbcon_putcs(vc, start, s - start, line, x);
1622 line++;
1623 if (d == (u16 *) softback_end)
1624 d = (u16 *) softback_buf;
1625 if (d == (u16 *) softback_in)
1626 d = (u16 *) vc->vc_origin;
1627 if (s == (u16 *) softback_end)
1628 s = (u16 *) softback_buf;
1629 if (s == (u16 *) softback_in)
1630 s = (u16 *) vc->vc_origin;
1634 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1635 int line, int count, int dy)
1637 unsigned short *s = (unsigned short *)
1638 (vc->vc_origin + vc->vc_size_row * line);
1640 while (count--) {
1641 unsigned short *start = s;
1642 unsigned short *le = advance_row(s, 1);
1643 unsigned short c;
1644 int x = 0;
1645 unsigned short attr = 1;
1647 do {
1648 c = scr_readw(s);
1649 if (attr != (c & 0xff00)) {
1650 attr = c & 0xff00;
1651 if (s > start) {
1652 fbcon_putcs(vc, start, s - start,
1653 dy, x);
1654 x += s - start;
1655 start = s;
1658 console_conditional_schedule();
1659 s++;
1660 } while (s < le);
1661 if (s > start)
1662 fbcon_putcs(vc, start, s - start, dy, x);
1663 console_conditional_schedule();
1664 dy++;
1668 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1669 struct display *p, int line, int count, int ycount)
1671 int offset = ycount * vc->vc_cols;
1672 unsigned short *d = (unsigned short *)
1673 (vc->vc_origin + vc->vc_size_row * line);
1674 unsigned short *s = d + offset;
1675 struct fbcon_ops *ops = info->fbcon_par;
1677 while (count--) {
1678 unsigned short *start = s;
1679 unsigned short *le = advance_row(s, 1);
1680 unsigned short c;
1681 int x = 0;
1683 do {
1684 c = scr_readw(s);
1686 if (c == scr_readw(d)) {
1687 if (s > start) {
1688 ops->bmove(vc, info, line + ycount, x,
1689 line, x, 1, s-start);
1690 x += s - start + 1;
1691 start = s + 1;
1692 } else {
1693 x++;
1694 start++;
1698 scr_writew(c, d);
1699 console_conditional_schedule();
1700 s++;
1701 d++;
1702 } while (s < le);
1703 if (s > start)
1704 ops->bmove(vc, info, line + ycount, x, line, x, 1,
1705 s-start);
1706 console_conditional_schedule();
1707 if (ycount > 0)
1708 line++;
1709 else {
1710 line--;
1711 /* NOTE: We subtract two lines from these pointers */
1712 s -= vc->vc_size_row;
1713 d -= vc->vc_size_row;
1718 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1719 int line, int count, int offset)
1721 unsigned short *d = (unsigned short *)
1722 (vc->vc_origin + vc->vc_size_row * line);
1723 unsigned short *s = d + offset;
1725 while (count--) {
1726 unsigned short *start = s;
1727 unsigned short *le = advance_row(s, 1);
1728 unsigned short c;
1729 int x = 0;
1730 unsigned short attr = 1;
1732 do {
1733 c = scr_readw(s);
1734 if (attr != (c & 0xff00)) {
1735 attr = c & 0xff00;
1736 if (s > start) {
1737 fbcon_putcs(vc, start, s - start,
1738 line, x);
1739 x += s - start;
1740 start = s;
1743 if (c == scr_readw(d)) {
1744 if (s > start) {
1745 fbcon_putcs(vc, start, s - start,
1746 line, x);
1747 x += s - start + 1;
1748 start = s + 1;
1749 } else {
1750 x++;
1751 start++;
1754 scr_writew(c, d);
1755 console_conditional_schedule();
1756 s++;
1757 d++;
1758 } while (s < le);
1759 if (s > start)
1760 fbcon_putcs(vc, start, s - start, line, x);
1761 console_conditional_schedule();
1762 if (offset > 0)
1763 line++;
1764 else {
1765 line--;
1766 /* NOTE: We subtract two lines from these pointers */
1767 s -= vc->vc_size_row;
1768 d -= vc->vc_size_row;
1773 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1774 int count)
1776 unsigned short *p;
1778 if (vc->vc_num != fg_console)
1779 return;
1780 p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1782 while (count) {
1783 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1784 count--;
1785 p = advance_row(p, 1);
1786 softback_in += vc->vc_size_row;
1787 if (softback_in == softback_end)
1788 softback_in = softback_buf;
1789 if (softback_in == softback_top) {
1790 softback_top += vc->vc_size_row;
1791 if (softback_top == softback_end)
1792 softback_top = softback_buf;
1795 softback_curr = softback_in;
1798 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1799 int count)
1801 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1802 struct display *p = &fb_display[vc->vc_num];
1803 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1805 if (fbcon_is_inactive(vc, info))
1806 return -EINVAL;
1808 fbcon_cursor(vc, CM_ERASE);
1811 * ++Geert: Only use ywrap/ypan if the console is in text mode
1812 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1813 * whole screen (prevents flicker).
1816 switch (dir) {
1817 case SM_UP:
1818 if (count > vc->vc_rows) /* Maximum realistic size */
1819 count = vc->vc_rows;
1820 if (softback_top)
1821 fbcon_softback_note(vc, t, count);
1822 if (logo_shown >= 0)
1823 goto redraw_up;
1824 switch (p->scrollmode) {
1825 case SCROLL_MOVE:
1826 fbcon_redraw_blit(vc, info, p, t, b - t - count,
1827 count);
1828 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1829 scr_memsetw((unsigned short *) (vc->vc_origin +
1830 vc->vc_size_row *
1831 (b - count)),
1832 vc->vc_video_erase_char,
1833 vc->vc_size_row * count);
1834 return 1;
1835 break;
1837 case SCROLL_WRAP_MOVE:
1838 if (b - t - count > 3 * vc->vc_rows >> 2) {
1839 if (t > 0)
1840 fbcon_bmove(vc, 0, 0, count, 0, t,
1841 vc->vc_cols);
1842 ywrap_up(vc, count);
1843 if (vc->vc_rows - b > 0)
1844 fbcon_bmove(vc, b - count, 0, b, 0,
1845 vc->vc_rows - b,
1846 vc->vc_cols);
1847 } else if (info->flags & FBINFO_READS_FAST)
1848 fbcon_bmove(vc, t + count, 0, t, 0,
1849 b - t - count, vc->vc_cols);
1850 else
1851 goto redraw_up;
1852 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1853 break;
1855 case SCROLL_PAN_REDRAW:
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_redraw_move(vc, p, 0, t, count);
1864 ypan_up_redraw(vc, t, count);
1865 if (vc->vc_rows - b > 0)
1866 fbcon_redraw_move(vc, p, b,
1867 vc->vc_rows - b, b);
1868 } else
1869 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1870 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1871 break;
1873 case SCROLL_PAN_MOVE:
1874 if ((p->yscroll + count <=
1875 2 * (p->vrows - vc->vc_rows))
1876 && ((!scroll_partial && (b - t == vc->vc_rows))
1877 || (scroll_partial
1878 && (b - t - count >
1879 3 * vc->vc_rows >> 2)))) {
1880 if (t > 0)
1881 fbcon_bmove(vc, 0, 0, count, 0, t,
1882 vc->vc_cols);
1883 ypan_up(vc, count);
1884 if (vc->vc_rows - b > 0)
1885 fbcon_bmove(vc, b - count, 0, b, 0,
1886 vc->vc_rows - b,
1887 vc->vc_cols);
1888 } else if (info->flags & FBINFO_READS_FAST)
1889 fbcon_bmove(vc, t + count, 0, t, 0,
1890 b - t - count, vc->vc_cols);
1891 else
1892 goto redraw_up;
1893 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1894 break;
1896 case SCROLL_REDRAW:
1897 redraw_up:
1898 fbcon_redraw(vc, p, t, b - t - count,
1899 count * vc->vc_cols);
1900 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1901 scr_memsetw((unsigned short *) (vc->vc_origin +
1902 vc->vc_size_row *
1903 (b - count)),
1904 vc->vc_video_erase_char,
1905 vc->vc_size_row * count);
1906 return 1;
1908 break;
1910 case SM_DOWN:
1911 if (count > vc->vc_rows) /* Maximum realistic size */
1912 count = vc->vc_rows;
1913 if (logo_shown >= 0)
1914 goto redraw_down;
1915 switch (p->scrollmode) {
1916 case SCROLL_MOVE:
1917 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1918 -count);
1919 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1920 scr_memsetw((unsigned short *) (vc->vc_origin +
1921 vc->vc_size_row *
1923 vc->vc_video_erase_char,
1924 vc->vc_size_row * count);
1925 return 1;
1926 break;
1928 case SCROLL_WRAP_MOVE:
1929 if (b - t - count > 3 * vc->vc_rows >> 2) {
1930 if (vc->vc_rows - b > 0)
1931 fbcon_bmove(vc, b, 0, b - count, 0,
1932 vc->vc_rows - b,
1933 vc->vc_cols);
1934 ywrap_down(vc, count);
1935 if (t > 0)
1936 fbcon_bmove(vc, count, 0, 0, 0, t,
1937 vc->vc_cols);
1938 } else if (info->flags & FBINFO_READS_FAST)
1939 fbcon_bmove(vc, t, 0, t + count, 0,
1940 b - t - count, vc->vc_cols);
1941 else
1942 goto redraw_down;
1943 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1944 break;
1946 case SCROLL_PAN_MOVE:
1947 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1948 && ((!scroll_partial && (b - t == vc->vc_rows))
1949 || (scroll_partial
1950 && (b - t - count >
1951 3 * vc->vc_rows >> 2)))) {
1952 if (vc->vc_rows - b > 0)
1953 fbcon_bmove(vc, b, 0, b - count, 0,
1954 vc->vc_rows - b,
1955 vc->vc_cols);
1956 ypan_down(vc, count);
1957 if (t > 0)
1958 fbcon_bmove(vc, count, 0, 0, 0, t,
1959 vc->vc_cols);
1960 } else if (info->flags & FBINFO_READS_FAST)
1961 fbcon_bmove(vc, t, 0, t + count, 0,
1962 b - t - count, vc->vc_cols);
1963 else
1964 goto redraw_down;
1965 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1966 break;
1968 case SCROLL_PAN_REDRAW:
1969 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1970 && ((!scroll_partial && (b - t == vc->vc_rows))
1971 || (scroll_partial
1972 && (b - t - count >
1973 3 * vc->vc_rows >> 2)))) {
1974 if (vc->vc_rows - b > 0)
1975 fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
1976 b - count);
1977 ypan_down_redraw(vc, t, count);
1978 if (t > 0)
1979 fbcon_redraw_move(vc, p, count, t, 0);
1980 } else
1981 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
1982 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1983 break;
1985 case SCROLL_REDRAW:
1986 redraw_down:
1987 fbcon_redraw(vc, p, b - 1, b - t - count,
1988 -count * vc->vc_cols);
1989 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1990 scr_memsetw((unsigned short *) (vc->vc_origin +
1991 vc->vc_size_row *
1993 vc->vc_video_erase_char,
1994 vc->vc_size_row * count);
1995 return 1;
1998 return 0;
2002 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
2003 int height, int width)
2005 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2006 struct display *p = &fb_display[vc->vc_num];
2008 if (fbcon_is_inactive(vc, info))
2009 return;
2011 if (!width || !height)
2012 return;
2014 /* Split blits that cross physical y_wrap case.
2015 * Pathological case involves 4 blits, better to use recursive
2016 * code rather than unrolled case
2018 * Recursive invocations don't need to erase the cursor over and
2019 * over again, so we use fbcon_bmove_rec()
2021 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2022 p->vrows - p->yscroll);
2025 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
2026 int dy, int dx, int height, int width, u_int y_break)
2028 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2029 struct fbcon_ops *ops = info->fbcon_par;
2030 u_int b;
2032 if (sy < y_break && sy + height > y_break) {
2033 b = y_break - sy;
2034 if (dy < sy) { /* Avoid trashing self */
2035 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2036 y_break);
2037 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2038 height - b, width, y_break);
2039 } else {
2040 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2041 height - b, width, y_break);
2042 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2043 y_break);
2045 return;
2048 if (dy < y_break && dy + height > y_break) {
2049 b = y_break - dy;
2050 if (dy < sy) { /* Avoid trashing self */
2051 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2052 y_break);
2053 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2054 height - b, width, y_break);
2055 } else {
2056 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2057 height - b, width, y_break);
2058 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2059 y_break);
2061 return;
2063 ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2064 height, width);
2067 static void updatescrollmode(struct display *p,
2068 struct fb_info *info,
2069 struct vc_data *vc)
2071 struct fbcon_ops *ops = info->fbcon_par;
2072 int fh = vc->vc_font.height;
2073 int cap = info->flags;
2074 u16 t = 0;
2075 int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2076 info->fix.xpanstep);
2077 int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2078 int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2079 int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2080 info->var.xres_virtual);
2081 int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2082 divides(ypan, vc->vc_font.height) && vyres > yres;
2083 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2084 divides(ywrap, vc->vc_font.height) &&
2085 divides(vc->vc_font.height, vyres) &&
2086 divides(vc->vc_font.height, yres);
2087 int reading_fast = cap & FBINFO_READS_FAST;
2088 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2089 !(cap & FBINFO_HWACCEL_DISABLED);
2090 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2091 !(cap & FBINFO_HWACCEL_DISABLED);
2093 p->vrows = vyres/fh;
2094 if (yres > (fh * (vc->vc_rows + 1)))
2095 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2096 if ((yres % fh) && (vyres % fh < yres % fh))
2097 p->vrows--;
2099 if (good_wrap || good_pan) {
2100 if (reading_fast || fast_copyarea)
2101 p->scrollmode = good_wrap ?
2102 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2103 else
2104 p->scrollmode = good_wrap ? SCROLL_REDRAW :
2105 SCROLL_PAN_REDRAW;
2106 } else {
2107 if (reading_fast || (fast_copyarea && !fast_imageblit))
2108 p->scrollmode = SCROLL_MOVE;
2109 else
2110 p->scrollmode = SCROLL_REDRAW;
2114 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2115 unsigned int height, unsigned int user)
2117 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2118 struct fbcon_ops *ops = info->fbcon_par;
2119 struct display *p = &fb_display[vc->vc_num];
2120 struct fb_var_screeninfo var = info->var;
2121 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2123 virt_w = FBCON_SWAP(ops->rotate, width, height);
2124 virt_h = FBCON_SWAP(ops->rotate, height, width);
2125 virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2126 vc->vc_font.height);
2127 virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2128 vc->vc_font.width);
2129 var.xres = virt_w * virt_fw;
2130 var.yres = virt_h * virt_fh;
2131 x_diff = info->var.xres - var.xres;
2132 y_diff = info->var.yres - var.yres;
2133 if (x_diff < 0 || x_diff > virt_fw ||
2134 y_diff < 0 || y_diff > virt_fh) {
2135 const struct fb_videomode *mode;
2137 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2138 mode = fb_find_best_mode(&var, &info->modelist);
2139 if (mode == NULL)
2140 return -EINVAL;
2141 display_to_var(&var, p);
2142 fb_videomode_to_var(&var, mode);
2144 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2145 return -EINVAL;
2147 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2148 if (CON_IS_VISIBLE(vc)) {
2149 var.activate = FB_ACTIVATE_NOW |
2150 FB_ACTIVATE_FORCE;
2151 fb_set_var(info, &var);
2153 var_to_display(p, &info->var, info);
2154 ops->var = info->var;
2156 updatescrollmode(p, info, vc);
2157 return 0;
2160 static int fbcon_switch(struct vc_data *vc)
2162 struct fb_info *info, *old_info = NULL;
2163 struct fbcon_ops *ops;
2164 struct display *p = &fb_display[vc->vc_num];
2165 struct fb_var_screeninfo var;
2166 int i, ret, prev_console, charcnt = 256;
2168 info = registered_fb[con2fb_map[vc->vc_num]];
2169 ops = info->fbcon_par;
2171 if (softback_top) {
2172 if (softback_lines)
2173 fbcon_set_origin(vc);
2174 softback_top = softback_curr = softback_in = softback_buf;
2175 softback_lines = 0;
2176 fbcon_update_softback(vc);
2179 if (logo_shown >= 0) {
2180 struct vc_data *conp2 = vc_cons[logo_shown].d;
2182 if (conp2->vc_top == logo_lines
2183 && conp2->vc_bottom == conp2->vc_rows)
2184 conp2->vc_top = 0;
2185 logo_shown = FBCON_LOGO_CANSHOW;
2188 prev_console = ops->currcon;
2189 if (prev_console != -1)
2190 old_info = registered_fb[con2fb_map[prev_console]];
2192 * FIXME: If we have multiple fbdev's loaded, we need to
2193 * update all info->currcon. Perhaps, we can place this
2194 * in a centralized structure, but this might break some
2195 * drivers.
2197 * info->currcon = vc->vc_num;
2199 for (i = 0; i < FB_MAX; i++) {
2200 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
2201 struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2203 o->currcon = vc->vc_num;
2206 memset(&var, 0, sizeof(struct fb_var_screeninfo));
2207 display_to_var(&var, p);
2208 var.activate = FB_ACTIVATE_NOW;
2211 * make sure we don't unnecessarily trip the memcmp()
2212 * in fb_set_var()
2214 info->var.activate = var.activate;
2215 var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
2216 fb_set_var(info, &var);
2217 ops->var = info->var;
2219 if (old_info != NULL && (old_info != info ||
2220 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2221 if (info->fbops->fb_set_par) {
2222 ret = info->fbops->fb_set_par(info);
2224 if (ret)
2225 printk(KERN_ERR "fbcon_switch: detected "
2226 "unhandled fb_set_par error, "
2227 "error code %d\n", ret);
2230 if (old_info != info)
2231 fbcon_del_cursor_timer(old_info);
2234 if (fbcon_is_inactive(vc, info) ||
2235 ops->blank_state != FB_BLANK_UNBLANK)
2236 fbcon_del_cursor_timer(info);
2237 else
2238 fbcon_add_cursor_timer(info);
2240 set_blitting_type(vc, info);
2241 ops->cursor_reset = 1;
2243 if (ops->rotate_font && ops->rotate_font(info, vc)) {
2244 ops->rotate = FB_ROTATE_UR;
2245 set_blitting_type(vc, info);
2248 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2249 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2251 if (p->userfont)
2252 charcnt = FNTCHARCNT(vc->vc_font.data);
2254 if (charcnt > 256)
2255 vc->vc_complement_mask <<= 1;
2257 updatescrollmode(p, info, vc);
2259 switch (p->scrollmode) {
2260 case SCROLL_WRAP_MOVE:
2261 scrollback_phys_max = p->vrows - vc->vc_rows;
2262 break;
2263 case SCROLL_PAN_MOVE:
2264 case SCROLL_PAN_REDRAW:
2265 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2266 if (scrollback_phys_max < 0)
2267 scrollback_phys_max = 0;
2268 break;
2269 default:
2270 scrollback_phys_max = 0;
2271 break;
2274 scrollback_max = 0;
2275 scrollback_current = 0;
2277 if (!fbcon_is_inactive(vc, info)) {
2278 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2279 ops->update_start(info);
2282 fbcon_set_palette(vc, color_table);
2283 fbcon_clear_margins(vc, 0);
2285 if (logo_shown == FBCON_LOGO_DRAW) {
2287 logo_shown = fg_console;
2288 /* This is protected above by initmem_freed */
2289 fb_show_logo(info, ops->rotate);
2290 update_region(vc,
2291 vc->vc_origin + vc->vc_size_row * vc->vc_top,
2292 vc->vc_size_row * (vc->vc_bottom -
2293 vc->vc_top) / 2);
2294 return 0;
2296 return 1;
2299 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2300 int blank)
2302 struct fb_event event;
2304 if (blank) {
2305 unsigned short charmask = vc->vc_hi_font_mask ?
2306 0x1ff : 0xff;
2307 unsigned short oldc;
2309 oldc = vc->vc_video_erase_char;
2310 vc->vc_video_erase_char &= charmask;
2311 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2312 vc->vc_video_erase_char = oldc;
2316 if (!lock_fb_info(info))
2317 return;
2318 event.info = info;
2319 event.data = &blank;
2320 fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
2321 unlock_fb_info(info);
2324 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2326 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2327 struct fbcon_ops *ops = info->fbcon_par;
2329 if (mode_switch) {
2330 struct fb_var_screeninfo var = info->var;
2332 ops->graphics = 1;
2334 if (!blank) {
2335 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2336 fb_set_var(info, &var);
2337 ops->graphics = 0;
2338 ops->var = info->var;
2342 if (!fbcon_is_inactive(vc, info)) {
2343 if (ops->blank_state != blank) {
2344 ops->blank_state = blank;
2345 fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2346 ops->cursor_flash = (!blank);
2348 if (!(info->flags & FBINFO_MISC_USEREVENT))
2349 if (fb_blank(info, blank))
2350 fbcon_generic_blank(vc, info, blank);
2353 if (!blank)
2354 update_screen(vc);
2357 if (mode_switch || fbcon_is_inactive(vc, info) ||
2358 ops->blank_state != FB_BLANK_UNBLANK)
2359 fbcon_del_cursor_timer(info);
2360 else
2361 fbcon_add_cursor_timer(info);
2363 return 0;
2366 static int fbcon_debug_enter(struct vc_data *vc)
2368 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2369 struct fbcon_ops *ops = info->fbcon_par;
2371 ops->save_graphics = ops->graphics;
2372 ops->graphics = 0;
2373 if (info->fbops->fb_debug_enter)
2374 info->fbops->fb_debug_enter(info);
2375 fbcon_set_palette(vc, color_table);
2376 return 0;
2379 static int fbcon_debug_leave(struct vc_data *vc)
2381 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2382 struct fbcon_ops *ops = info->fbcon_par;
2384 ops->graphics = ops->save_graphics;
2385 if (info->fbops->fb_debug_leave)
2386 info->fbops->fb_debug_leave(info);
2387 return 0;
2390 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2392 u8 *fontdata = vc->vc_font.data;
2393 u8 *data = font->data;
2394 int i, j;
2396 font->width = vc->vc_font.width;
2397 font->height = vc->vc_font.height;
2398 font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2399 if (!font->data)
2400 return 0;
2402 if (font->width <= 8) {
2403 j = vc->vc_font.height;
2404 for (i = 0; i < font->charcount; i++) {
2405 memcpy(data, fontdata, j);
2406 memset(data + j, 0, 32 - j);
2407 data += 32;
2408 fontdata += j;
2410 } else if (font->width <= 16) {
2411 j = vc->vc_font.height * 2;
2412 for (i = 0; i < font->charcount; i++) {
2413 memcpy(data, fontdata, j);
2414 memset(data + j, 0, 64 - j);
2415 data += 64;
2416 fontdata += j;
2418 } else if (font->width <= 24) {
2419 for (i = 0; i < font->charcount; i++) {
2420 for (j = 0; j < vc->vc_font.height; j++) {
2421 *data++ = fontdata[0];
2422 *data++ = fontdata[1];
2423 *data++ = fontdata[2];
2424 fontdata += sizeof(u32);
2426 memset(data, 0, 3 * (32 - j));
2427 data += 3 * (32 - j);
2429 } else {
2430 j = vc->vc_font.height * 4;
2431 for (i = 0; i < font->charcount; i++) {
2432 memcpy(data, fontdata, j);
2433 memset(data + j, 0, 128 - j);
2434 data += 128;
2435 fontdata += j;
2438 return 0;
2441 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2442 const u8 * data, int userfont)
2444 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2445 struct fbcon_ops *ops = info->fbcon_par;
2446 struct display *p = &fb_display[vc->vc_num];
2447 int resize;
2448 int cnt;
2449 char *old_data = NULL;
2451 if (CON_IS_VISIBLE(vc) && softback_lines)
2452 fbcon_set_origin(vc);
2454 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2455 if (p->userfont)
2456 old_data = vc->vc_font.data;
2457 if (userfont)
2458 cnt = FNTCHARCNT(data);
2459 else
2460 cnt = 256;
2461 vc->vc_font.data = (void *)(p->fontdata = data);
2462 if ((p->userfont = userfont))
2463 REFCOUNT(data)++;
2464 vc->vc_font.width = w;
2465 vc->vc_font.height = h;
2466 if (vc->vc_hi_font_mask && cnt == 256) {
2467 vc->vc_hi_font_mask = 0;
2468 if (vc->vc_can_do_color) {
2469 vc->vc_complement_mask >>= 1;
2470 vc->vc_s_complement_mask >>= 1;
2473 /* ++Edmund: reorder the attribute bits */
2474 if (vc->vc_can_do_color) {
2475 unsigned short *cp =
2476 (unsigned short *) vc->vc_origin;
2477 int count = vc->vc_screenbuf_size / 2;
2478 unsigned short c;
2479 for (; count > 0; count--, cp++) {
2480 c = scr_readw(cp);
2481 scr_writew(((c & 0xfe00) >> 1) |
2482 (c & 0xff), cp);
2484 c = vc->vc_video_erase_char;
2485 vc->vc_video_erase_char =
2486 ((c & 0xfe00) >> 1) | (c & 0xff);
2487 vc->vc_attr >>= 1;
2489 } else if (!vc->vc_hi_font_mask && cnt == 512) {
2490 vc->vc_hi_font_mask = 0x100;
2491 if (vc->vc_can_do_color) {
2492 vc->vc_complement_mask <<= 1;
2493 vc->vc_s_complement_mask <<= 1;
2496 /* ++Edmund: reorder the attribute bits */
2498 unsigned short *cp =
2499 (unsigned short *) vc->vc_origin;
2500 int count = vc->vc_screenbuf_size / 2;
2501 unsigned short c;
2502 for (; count > 0; count--, cp++) {
2503 unsigned short newc;
2504 c = scr_readw(cp);
2505 if (vc->vc_can_do_color)
2506 newc =
2507 ((c & 0xff00) << 1) | (c &
2508 0xff);
2509 else
2510 newc = c & ~0x100;
2511 scr_writew(newc, cp);
2513 c = vc->vc_video_erase_char;
2514 if (vc->vc_can_do_color) {
2515 vc->vc_video_erase_char =
2516 ((c & 0xff00) << 1) | (c & 0xff);
2517 vc->vc_attr <<= 1;
2518 } else
2519 vc->vc_video_erase_char = c & ~0x100;
2524 if (resize) {
2525 int cols, rows;
2527 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2528 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2529 cols /= w;
2530 rows /= h;
2531 vc_resize(vc, cols, rows);
2532 if (CON_IS_VISIBLE(vc) && softback_buf)
2533 fbcon_update_softback(vc);
2534 } else if (CON_IS_VISIBLE(vc)
2535 && vc->vc_mode == KD_TEXT) {
2536 fbcon_clear_margins(vc, 0);
2537 update_screen(vc);
2540 if (old_data && (--REFCOUNT(old_data) == 0))
2541 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2542 return 0;
2545 static int fbcon_copy_font(struct vc_data *vc, int con)
2547 struct display *od = &fb_display[con];
2548 struct console_font *f = &vc->vc_font;
2550 if (od->fontdata == f->data)
2551 return 0; /* already the same font... */
2552 return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2556 * User asked to set font; we are guaranteed that
2557 * a) width and height are in range 1..32
2558 * b) charcount does not exceed 512
2559 * but lets not assume that, since someone might someday want to use larger
2560 * fonts. And charcount of 512 is small for unicode support.
2562 * However, user space gives the font in 32 rows , regardless of
2563 * actual font height. So a new API is needed if support for larger fonts
2564 * is ever implemented.
2567 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2569 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2570 unsigned charcount = font->charcount;
2571 int w = font->width;
2572 int h = font->height;
2573 int size;
2574 int i, csum;
2575 u8 *new_data, *data = font->data;
2576 int pitch = (font->width+7) >> 3;
2578 /* Is there a reason why fbconsole couldn't handle any charcount >256?
2579 * If not this check should be changed to charcount < 256 */
2580 if (charcount != 256 && charcount != 512)
2581 return -EINVAL;
2583 /* Make sure drawing engine can handle the font */
2584 if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2585 !(info->pixmap.blit_y & (1 << (font->height - 1))))
2586 return -EINVAL;
2588 /* Make sure driver can handle the font length */
2589 if (fbcon_invalid_charcount(info, charcount))
2590 return -EINVAL;
2592 size = h * pitch * charcount;
2594 new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2596 if (!new_data)
2597 return -ENOMEM;
2599 new_data += FONT_EXTRA_WORDS * sizeof(int);
2600 FNTSIZE(new_data) = size;
2601 FNTCHARCNT(new_data) = charcount;
2602 REFCOUNT(new_data) = 0; /* usage counter */
2603 for (i=0; i< charcount; i++) {
2604 memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
2607 /* Since linux has a nice crc32 function use it for counting font
2608 * checksums. */
2609 csum = crc32(0, new_data, size);
2611 FNTSUM(new_data) = csum;
2612 /* Check if the same font is on some other console already */
2613 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2614 struct vc_data *tmp = vc_cons[i].d;
2616 if (fb_display[i].userfont &&
2617 fb_display[i].fontdata &&
2618 FNTSUM(fb_display[i].fontdata) == csum &&
2619 FNTSIZE(fb_display[i].fontdata) == size &&
2620 tmp->vc_font.width == w &&
2621 !memcmp(fb_display[i].fontdata, new_data, size)) {
2622 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2623 new_data = (u8 *)fb_display[i].fontdata;
2624 break;
2627 return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2630 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2632 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2633 const struct font_desc *f;
2635 if (!name)
2636 f = get_default_font(info->var.xres, info->var.yres,
2637 info->pixmap.blit_x, info->pixmap.blit_y);
2638 else if (!(f = find_font(name)))
2639 return -ENOENT;
2641 font->width = f->width;
2642 font->height = f->height;
2643 return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2646 static u16 palette_red[16];
2647 static u16 palette_green[16];
2648 static u16 palette_blue[16];
2650 static struct fb_cmap palette_cmap = {
2651 0, 16, palette_red, palette_green, palette_blue, NULL
2654 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2656 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2657 int i, j, k, depth;
2658 u8 val;
2660 if (fbcon_is_inactive(vc, info))
2661 return -EINVAL;
2663 if (!CON_IS_VISIBLE(vc))
2664 return 0;
2666 depth = fb_get_color_depth(&info->var, &info->fix);
2667 if (depth > 3) {
2668 for (i = j = 0; i < 16; i++) {
2669 k = table[i];
2670 val = vc->vc_palette[j++];
2671 palette_red[k] = (val << 8) | val;
2672 val = vc->vc_palette[j++];
2673 palette_green[k] = (val << 8) | val;
2674 val = vc->vc_palette[j++];
2675 palette_blue[k] = (val << 8) | val;
2677 palette_cmap.len = 16;
2678 palette_cmap.start = 0;
2680 * If framebuffer is capable of less than 16 colors,
2681 * use default palette of fbcon.
2683 } else
2684 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2686 return fb_set_cmap(&palette_cmap, info);
2689 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2691 unsigned long p;
2692 int line;
2694 if (vc->vc_num != fg_console || !softback_lines)
2695 return (u16 *) (vc->vc_origin + offset);
2696 line = offset / vc->vc_size_row;
2697 if (line >= softback_lines)
2698 return (u16 *) (vc->vc_origin + offset -
2699 softback_lines * vc->vc_size_row);
2700 p = softback_curr + offset;
2701 if (p >= softback_end)
2702 p += softback_buf - softback_end;
2703 return (u16 *) p;
2706 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2707 int *px, int *py)
2709 unsigned long ret;
2710 int x, y;
2712 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2713 unsigned long offset = (pos - vc->vc_origin) / 2;
2715 x = offset % vc->vc_cols;
2716 y = offset / vc->vc_cols;
2717 if (vc->vc_num == fg_console)
2718 y += softback_lines;
2719 ret = pos + (vc->vc_cols - x) * 2;
2720 } else if (vc->vc_num == fg_console && softback_lines) {
2721 unsigned long offset = pos - softback_curr;
2723 if (pos < softback_curr)
2724 offset += softback_end - softback_buf;
2725 offset /= 2;
2726 x = offset % vc->vc_cols;
2727 y = offset / vc->vc_cols;
2728 ret = pos + (vc->vc_cols - x) * 2;
2729 if (ret == softback_end)
2730 ret = softback_buf;
2731 if (ret == softback_in)
2732 ret = vc->vc_origin;
2733 } else {
2734 /* Should not happen */
2735 x = y = 0;
2736 ret = vc->vc_origin;
2738 if (px)
2739 *px = x;
2740 if (py)
2741 *py = y;
2742 return ret;
2745 /* As we might be inside of softback, we may work with non-contiguous buffer,
2746 that's why we have to use a separate routine. */
2747 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2749 while (cnt--) {
2750 u16 a = scr_readw(p);
2751 if (!vc->vc_can_do_color)
2752 a ^= 0x0800;
2753 else if (vc->vc_hi_font_mask == 0x100)
2754 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2755 (((a) & 0x0e00) << 4);
2756 else
2757 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2758 (((a) & 0x0700) << 4);
2759 scr_writew(a, p++);
2760 if (p == (u16 *) softback_end)
2761 p = (u16 *) softback_buf;
2762 if (p == (u16 *) softback_in)
2763 p = (u16 *) vc->vc_origin;
2767 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2769 struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2770 struct fbcon_ops *ops = info->fbcon_par;
2771 struct display *disp = &fb_display[fg_console];
2772 int offset, limit, scrollback_old;
2774 if (softback_top) {
2775 if (vc->vc_num != fg_console)
2776 return 0;
2777 if (vc->vc_mode != KD_TEXT || !lines)
2778 return 0;
2779 if (logo_shown >= 0) {
2780 struct vc_data *conp2 = vc_cons[logo_shown].d;
2782 if (conp2->vc_top == logo_lines
2783 && conp2->vc_bottom == conp2->vc_rows)
2784 conp2->vc_top = 0;
2785 if (logo_shown == vc->vc_num) {
2786 unsigned long p, q;
2787 int i;
2789 p = softback_in;
2790 q = vc->vc_origin +
2791 logo_lines * vc->vc_size_row;
2792 for (i = 0; i < logo_lines; i++) {
2793 if (p == softback_top)
2794 break;
2795 if (p == softback_buf)
2796 p = softback_end;
2797 p -= vc->vc_size_row;
2798 q -= vc->vc_size_row;
2799 scr_memcpyw((u16 *) q, (u16 *) p,
2800 vc->vc_size_row);
2802 softback_in = softback_curr = p;
2803 update_region(vc, vc->vc_origin,
2804 logo_lines * vc->vc_cols);
2806 logo_shown = FBCON_LOGO_CANSHOW;
2808 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2809 fbcon_redraw_softback(vc, disp, lines);
2810 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2811 return 0;
2814 if (!scrollback_phys_max)
2815 return -ENOSYS;
2817 scrollback_old = scrollback_current;
2818 scrollback_current -= lines;
2819 if (scrollback_current < 0)
2820 scrollback_current = 0;
2821 else if (scrollback_current > scrollback_max)
2822 scrollback_current = scrollback_max;
2823 if (scrollback_current == scrollback_old)
2824 return 0;
2826 if (fbcon_is_inactive(vc, info))
2827 return 0;
2829 fbcon_cursor(vc, CM_ERASE);
2831 offset = disp->yscroll - scrollback_current;
2832 limit = disp->vrows;
2833 switch (disp->scrollmode) {
2834 case SCROLL_WRAP_MOVE:
2835 info->var.vmode |= FB_VMODE_YWRAP;
2836 break;
2837 case SCROLL_PAN_MOVE:
2838 case SCROLL_PAN_REDRAW:
2839 limit -= vc->vc_rows;
2840 info->var.vmode &= ~FB_VMODE_YWRAP;
2841 break;
2843 if (offset < 0)
2844 offset += limit;
2845 else if (offset >= limit)
2846 offset -= limit;
2848 ops->var.xoffset = 0;
2849 ops->var.yoffset = offset * vc->vc_font.height;
2850 ops->update_start(info);
2852 if (!scrollback_current)
2853 fbcon_cursor(vc, CM_DRAW);
2854 return 0;
2857 static int fbcon_set_origin(struct vc_data *vc)
2859 if (softback_lines)
2860 fbcon_scrolldelta(vc, softback_lines);
2861 return 0;
2864 static void fbcon_suspended(struct fb_info *info)
2866 struct vc_data *vc = NULL;
2867 struct fbcon_ops *ops = info->fbcon_par;
2869 if (!ops || ops->currcon < 0)
2870 return;
2871 vc = vc_cons[ops->currcon].d;
2873 /* Clear cursor, restore saved data */
2874 fbcon_cursor(vc, CM_ERASE);
2877 static void fbcon_resumed(struct fb_info *info)
2879 struct vc_data *vc;
2880 struct fbcon_ops *ops = info->fbcon_par;
2882 if (!ops || ops->currcon < 0)
2883 return;
2884 vc = vc_cons[ops->currcon].d;
2886 update_screen(vc);
2889 static void fbcon_modechanged(struct fb_info *info)
2891 struct fbcon_ops *ops = info->fbcon_par;
2892 struct vc_data *vc;
2893 struct display *p;
2894 int rows, cols;
2896 if (!ops || ops->currcon < 0)
2897 return;
2898 vc = vc_cons[ops->currcon].d;
2899 if (vc->vc_mode != KD_TEXT ||
2900 registered_fb[con2fb_map[ops->currcon]] != info)
2901 return;
2903 p = &fb_display[vc->vc_num];
2904 set_blitting_type(vc, info);
2906 if (CON_IS_VISIBLE(vc)) {
2907 var_to_display(p, &info->var, info);
2908 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2909 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2910 cols /= vc->vc_font.width;
2911 rows /= vc->vc_font.height;
2912 vc_resize(vc, cols, rows);
2913 updatescrollmode(p, info, vc);
2914 scrollback_max = 0;
2915 scrollback_current = 0;
2917 if (!fbcon_is_inactive(vc, info)) {
2918 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2919 ops->update_start(info);
2922 fbcon_set_palette(vc, color_table);
2923 update_screen(vc);
2924 if (softback_buf)
2925 fbcon_update_softback(vc);
2929 static void fbcon_set_all_vcs(struct fb_info *info)
2931 struct fbcon_ops *ops = info->fbcon_par;
2932 struct vc_data *vc;
2933 struct display *p;
2934 int i, rows, cols, fg = -1;
2936 if (!ops || ops->currcon < 0)
2937 return;
2939 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2940 vc = vc_cons[i].d;
2941 if (!vc || vc->vc_mode != KD_TEXT ||
2942 registered_fb[con2fb_map[i]] != info)
2943 continue;
2945 if (CON_IS_VISIBLE(vc)) {
2946 fg = i;
2947 continue;
2950 p = &fb_display[vc->vc_num];
2951 set_blitting_type(vc, info);
2952 var_to_display(p, &info->var, info);
2953 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2954 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2955 cols /= vc->vc_font.width;
2956 rows /= vc->vc_font.height;
2957 vc_resize(vc, cols, rows);
2960 if (fg != -1)
2961 fbcon_modechanged(info);
2964 static int fbcon_mode_deleted(struct fb_info *info,
2965 struct fb_videomode *mode)
2967 struct fb_info *fb_info;
2968 struct display *p;
2969 int i, j, found = 0;
2971 /* before deletion, ensure that mode is not in use */
2972 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2973 j = con2fb_map[i];
2974 if (j == -1)
2975 continue;
2976 fb_info = registered_fb[j];
2977 if (fb_info != info)
2978 continue;
2979 p = &fb_display[i];
2980 if (!p || !p->mode)
2981 continue;
2982 if (fb_mode_is_equal(p->mode, mode)) {
2983 found = 1;
2984 break;
2987 return found;
2990 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
2991 static int fbcon_unbind(void)
2993 int ret;
2995 ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
2996 fbcon_is_default);
2998 if (!ret)
2999 fbcon_has_console_bind = 0;
3001 return ret;
3003 #else
3004 static inline int fbcon_unbind(void)
3006 return -EINVAL;
3008 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3010 /* called with console_lock held */
3011 static int fbcon_fb_unbind(int idx)
3013 int i, new_idx = -1, ret = 0;
3015 if (!fbcon_has_console_bind)
3016 return 0;
3018 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3019 if (con2fb_map[i] != idx &&
3020 con2fb_map[i] != -1) {
3021 new_idx = i;
3022 break;
3026 if (new_idx != -1) {
3027 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3028 if (con2fb_map[i] == idx)
3029 set_con2fb_map(i, new_idx, 0);
3031 } else
3032 ret = fbcon_unbind();
3034 return ret;
3037 /* called with console_lock held */
3038 static int fbcon_fb_unregistered(struct fb_info *info)
3040 int i, idx;
3042 idx = info->node;
3043 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3044 if (con2fb_map[i] == idx)
3045 con2fb_map[i] = -1;
3048 if (idx == info_idx) {
3049 info_idx = -1;
3051 for (i = 0; i < FB_MAX; i++) {
3052 if (registered_fb[i] != NULL) {
3053 info_idx = i;
3054 break;
3059 if (info_idx != -1) {
3060 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3061 if (con2fb_map[i] == -1)
3062 con2fb_map[i] = info_idx;
3066 if (primary_device == idx)
3067 primary_device = -1;
3069 if (!num_registered_fb)
3070 do_unregister_con_driver(&fb_con);
3072 return 0;
3075 /* called with console_lock held */
3076 static void fbcon_remap_all(int idx)
3078 int i;
3079 for (i = first_fb_vc; i <= last_fb_vc; i++)
3080 set_con2fb_map(i, idx, 0);
3082 if (con_is_bound(&fb_con)) {
3083 printk(KERN_INFO "fbcon: Remapping primary device, "
3084 "fb%i, to tty %i-%i\n", idx,
3085 first_fb_vc + 1, last_fb_vc + 1);
3086 info_idx = idx;
3090 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3091 static void fbcon_select_primary(struct fb_info *info)
3093 if (!map_override && primary_device == -1 &&
3094 fb_is_primary_device(info)) {
3095 int i;
3097 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3098 info->fix.id, info->node);
3099 primary_device = info->node;
3101 for (i = first_fb_vc; i <= last_fb_vc; i++)
3102 con2fb_map_boot[i] = primary_device;
3104 if (con_is_bound(&fb_con)) {
3105 printk(KERN_INFO "fbcon: Remapping primary device, "
3106 "fb%i, to tty %i-%i\n", info->node,
3107 first_fb_vc + 1, last_fb_vc + 1);
3108 info_idx = primary_device;
3113 #else
3114 static inline void fbcon_select_primary(struct fb_info *info)
3116 return;
3118 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3120 /* called with console_lock held */
3121 static int fbcon_fb_registered(struct fb_info *info)
3123 int ret = 0, i, idx;
3125 idx = info->node;
3126 fbcon_select_primary(info);
3128 if (info_idx == -1) {
3129 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3130 if (con2fb_map_boot[i] == idx) {
3131 info_idx = idx;
3132 break;
3136 if (info_idx != -1)
3137 ret = do_fbcon_takeover(1);
3138 } else {
3139 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3140 if (con2fb_map_boot[i] == idx)
3141 set_con2fb_map(i, idx, 0);
3145 return ret;
3148 static void fbcon_fb_blanked(struct fb_info *info, int blank)
3150 struct fbcon_ops *ops = info->fbcon_par;
3151 struct vc_data *vc;
3153 if (!ops || ops->currcon < 0)
3154 return;
3156 vc = vc_cons[ops->currcon].d;
3157 if (vc->vc_mode != KD_TEXT ||
3158 registered_fb[con2fb_map[ops->currcon]] != info)
3159 return;
3161 if (CON_IS_VISIBLE(vc)) {
3162 if (blank)
3163 do_blank_screen(0);
3164 else
3165 do_unblank_screen(0);
3167 ops->blank_state = blank;
3170 static void fbcon_new_modelist(struct fb_info *info)
3172 int i;
3173 struct vc_data *vc;
3174 struct fb_var_screeninfo var;
3175 const struct fb_videomode *mode;
3177 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3178 if (registered_fb[con2fb_map[i]] != info)
3179 continue;
3180 if (!fb_display[i].mode)
3181 continue;
3182 vc = vc_cons[i].d;
3183 display_to_var(&var, &fb_display[i]);
3184 mode = fb_find_nearest_mode(fb_display[i].mode,
3185 &info->modelist);
3186 fb_videomode_to_var(&var, mode);
3187 fbcon_set_disp(info, &var, vc->vc_num);
3191 static void fbcon_get_requirement(struct fb_info *info,
3192 struct fb_blit_caps *caps)
3194 struct vc_data *vc;
3195 struct display *p;
3197 if (caps->flags) {
3198 int i, charcnt;
3200 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3201 vc = vc_cons[i].d;
3202 if (vc && vc->vc_mode == KD_TEXT &&
3203 info->node == con2fb_map[i]) {
3204 p = &fb_display[i];
3205 caps->x |= 1 << (vc->vc_font.width - 1);
3206 caps->y |= 1 << (vc->vc_font.height - 1);
3207 charcnt = (p->userfont) ?
3208 FNTCHARCNT(p->fontdata) : 256;
3209 if (caps->len < charcnt)
3210 caps->len = charcnt;
3213 } else {
3214 vc = vc_cons[fg_console].d;
3216 if (vc && vc->vc_mode == KD_TEXT &&
3217 info->node == con2fb_map[fg_console]) {
3218 p = &fb_display[fg_console];
3219 caps->x = 1 << (vc->vc_font.width - 1);
3220 caps->y = 1 << (vc->vc_font.height - 1);
3221 caps->len = (p->userfont) ?
3222 FNTCHARCNT(p->fontdata) : 256;
3227 static int fbcon_event_notify(struct notifier_block *self,
3228 unsigned long action, void *data)
3230 struct fb_event *event = data;
3231 struct fb_info *info = event->info;
3232 struct fb_videomode *mode;
3233 struct fb_con2fbmap *con2fb;
3234 struct fb_blit_caps *caps;
3235 int idx, ret = 0;
3238 * ignore all events except driver registration and deregistration
3239 * if fbcon is not active
3241 if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3242 action == FB_EVENT_FB_UNREGISTERED))
3243 goto done;
3245 switch(action) {
3246 case FB_EVENT_SUSPEND:
3247 fbcon_suspended(info);
3248 break;
3249 case FB_EVENT_RESUME:
3250 fbcon_resumed(info);
3251 break;
3252 case FB_EVENT_MODE_CHANGE:
3253 fbcon_modechanged(info);
3254 break;
3255 case FB_EVENT_MODE_CHANGE_ALL:
3256 fbcon_set_all_vcs(info);
3257 break;
3258 case FB_EVENT_MODE_DELETE:
3259 mode = event->data;
3260 ret = fbcon_mode_deleted(info, mode);
3261 break;
3262 case FB_EVENT_FB_UNBIND:
3263 idx = info->node;
3264 ret = fbcon_fb_unbind(idx);
3265 break;
3266 case FB_EVENT_FB_REGISTERED:
3267 ret = fbcon_fb_registered(info);
3268 break;
3269 case FB_EVENT_FB_UNREGISTERED:
3270 ret = fbcon_fb_unregistered(info);
3271 break;
3272 case FB_EVENT_SET_CONSOLE_MAP:
3273 /* called with console lock held */
3274 con2fb = event->data;
3275 ret = set_con2fb_map(con2fb->console - 1,
3276 con2fb->framebuffer, 1);
3277 break;
3278 case FB_EVENT_GET_CONSOLE_MAP:
3279 con2fb = event->data;
3280 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3281 break;
3282 case FB_EVENT_BLANK:
3283 fbcon_fb_blanked(info, *(int *)event->data);
3284 break;
3285 case FB_EVENT_NEW_MODELIST:
3286 fbcon_new_modelist(info);
3287 break;
3288 case FB_EVENT_GET_REQ:
3289 caps = event->data;
3290 fbcon_get_requirement(info, caps);
3291 break;
3292 case FB_EVENT_REMAP_ALL_CONSOLE:
3293 idx = info->node;
3294 fbcon_remap_all(idx);
3295 break;
3297 done:
3298 return ret;
3302 * The console `switch' structure for the frame buffer based console
3305 static const struct consw fb_con = {
3306 .owner = THIS_MODULE,
3307 .con_startup = fbcon_startup,
3308 .con_init = fbcon_init,
3309 .con_deinit = fbcon_deinit,
3310 .con_clear = fbcon_clear,
3311 .con_putc = fbcon_putc,
3312 .con_putcs = fbcon_putcs,
3313 .con_cursor = fbcon_cursor,
3314 .con_scroll = fbcon_scroll,
3315 .con_bmove = fbcon_bmove,
3316 .con_switch = fbcon_switch,
3317 .con_blank = fbcon_blank,
3318 .con_font_set = fbcon_set_font,
3319 .con_font_get = fbcon_get_font,
3320 .con_font_default = fbcon_set_def_font,
3321 .con_font_copy = fbcon_copy_font,
3322 .con_set_palette = fbcon_set_palette,
3323 .con_scrolldelta = fbcon_scrolldelta,
3324 .con_set_origin = fbcon_set_origin,
3325 .con_invert_region = fbcon_invert_region,
3326 .con_screen_pos = fbcon_screen_pos,
3327 .con_getxy = fbcon_getxy,
3328 .con_resize = fbcon_resize,
3329 .con_debug_enter = fbcon_debug_enter,
3330 .con_debug_leave = fbcon_debug_leave,
3333 static struct notifier_block fbcon_event_notifier = {
3334 .notifier_call = fbcon_event_notify,
3337 static ssize_t store_rotate(struct device *device,
3338 struct device_attribute *attr, const char *buf,
3339 size_t count)
3341 struct fb_info *info;
3342 int rotate, idx;
3343 char **last = NULL;
3345 if (fbcon_has_exited)
3346 return count;
3348 console_lock();
3349 idx = con2fb_map[fg_console];
3351 if (idx == -1 || registered_fb[idx] == NULL)
3352 goto err;
3354 info = registered_fb[idx];
3355 rotate = simple_strtoul(buf, last, 0);
3356 fbcon_rotate(info, rotate);
3357 err:
3358 console_unlock();
3359 return count;
3362 static ssize_t store_rotate_all(struct device *device,
3363 struct device_attribute *attr,const char *buf,
3364 size_t count)
3366 struct fb_info *info;
3367 int rotate, idx;
3368 char **last = NULL;
3370 if (fbcon_has_exited)
3371 return count;
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 = simple_strtoul(buf, last, 0);
3381 fbcon_rotate_all(info, rotate);
3382 err:
3383 console_unlock();
3384 return count;
3387 static ssize_t show_rotate(struct device *device,
3388 struct device_attribute *attr,char *buf)
3390 struct fb_info *info;
3391 int rotate = 0, idx;
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 rotate = fbcon_get_rotate(info);
3404 err:
3405 console_unlock();
3406 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3409 static ssize_t show_cursor_blink(struct device *device,
3410 struct device_attribute *attr, char *buf)
3412 struct fb_info *info;
3413 struct fbcon_ops *ops;
3414 int idx, blink = -1;
3416 if (fbcon_has_exited)
3417 return 0;
3419 console_lock();
3420 idx = con2fb_map[fg_console];
3422 if (idx == -1 || registered_fb[idx] == NULL)
3423 goto err;
3425 info = registered_fb[idx];
3426 ops = info->fbcon_par;
3428 if (!ops)
3429 goto err;
3431 blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3432 err:
3433 console_unlock();
3434 return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3437 static ssize_t store_cursor_blink(struct device *device,
3438 struct device_attribute *attr,
3439 const char *buf, size_t count)
3441 struct fb_info *info;
3442 int blink, idx;
3443 char **last = NULL;
3445 if (fbcon_has_exited)
3446 return count;
3448 console_lock();
3449 idx = con2fb_map[fg_console];
3451 if (idx == -1 || registered_fb[idx] == NULL)
3452 goto err;
3454 info = registered_fb[idx];
3456 if (!info->fbcon_par)
3457 goto err;
3459 blink = simple_strtoul(buf, last, 0);
3461 if (blink) {
3462 fbcon_cursor_noblink = 0;
3463 fbcon_add_cursor_timer(info);
3464 } else {
3465 fbcon_cursor_noblink = 1;
3466 fbcon_del_cursor_timer(info);
3469 err:
3470 console_unlock();
3471 return count;
3474 static struct device_attribute device_attrs[] = {
3475 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3476 __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3477 __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3478 store_cursor_blink),
3481 static int fbcon_init_device(void)
3483 int i, error = 0;
3485 fbcon_has_sysfs = 1;
3487 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3488 error = device_create_file(fbcon_device, &device_attrs[i]);
3490 if (error)
3491 break;
3494 if (error) {
3495 while (--i >= 0)
3496 device_remove_file(fbcon_device, &device_attrs[i]);
3498 fbcon_has_sysfs = 0;
3501 return 0;
3504 static void fbcon_start(void)
3506 if (num_registered_fb) {
3507 int i;
3509 console_lock();
3511 for (i = 0; i < FB_MAX; i++) {
3512 if (registered_fb[i] != NULL) {
3513 info_idx = i;
3514 break;
3518 do_fbcon_takeover(0);
3519 console_unlock();
3524 static void fbcon_exit(void)
3526 struct fb_info *info;
3527 int i, j, mapped;
3529 if (fbcon_has_exited)
3530 return;
3532 kfree((void *)softback_buf);
3533 softback_buf = 0UL;
3535 for (i = 0; i < FB_MAX; i++) {
3536 int pending = 0;
3538 mapped = 0;
3539 info = registered_fb[i];
3541 if (info == NULL)
3542 continue;
3544 if (info->queue.func)
3545 pending = cancel_work_sync(&info->queue);
3546 DPRINTK("fbcon: %s pending work\n", (pending ? "canceled" :
3547 "no"));
3549 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3550 if (con2fb_map[j] == i)
3551 mapped = 1;
3554 if (mapped) {
3555 if (info->fbops->fb_release)
3556 info->fbops->fb_release(info, 0);
3557 module_put(info->fbops->owner);
3559 if (info->fbcon_par) {
3560 struct fbcon_ops *ops = info->fbcon_par;
3562 fbcon_del_cursor_timer(info);
3563 kfree(ops->cursor_src);
3564 kfree(info->fbcon_par);
3565 info->fbcon_par = NULL;
3568 if (info->queue.func == fb_flashcursor)
3569 info->queue.func = NULL;
3573 fbcon_has_exited = 1;
3576 static int __init fb_console_init(void)
3578 int i;
3580 console_lock();
3581 fb_register_client(&fbcon_event_notifier);
3582 fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3583 "fbcon");
3585 if (IS_ERR(fbcon_device)) {
3586 printk(KERN_WARNING "Unable to create device "
3587 "for fbcon; errno = %ld\n",
3588 PTR_ERR(fbcon_device));
3589 fbcon_device = NULL;
3590 } else
3591 fbcon_init_device();
3593 for (i = 0; i < MAX_NR_CONSOLES; i++)
3594 con2fb_map[i] = -1;
3596 console_unlock();
3597 fbcon_start();
3598 return 0;
3601 module_init(fb_console_init);
3603 #ifdef MODULE
3605 static void __exit fbcon_deinit_device(void)
3607 int i;
3609 if (fbcon_has_sysfs) {
3610 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3611 device_remove_file(fbcon_device, &device_attrs[i]);
3613 fbcon_has_sysfs = 0;
3617 static void __exit fb_console_exit(void)
3619 console_lock();
3620 fb_unregister_client(&fbcon_event_notifier);
3621 fbcon_deinit_device();
3622 device_destroy(fb_class, MKDEV(0, 0));
3623 fbcon_exit();
3624 do_unregister_con_driver(&fb_con);
3625 console_unlock();
3628 module_exit(fb_console_exit);
3630 #endif
3632 MODULE_LICENSE("GPL");