bridge: Pull ip header into skb->data before looking into ip header.
[linux/fpc-iii.git] / drivers / video / console / fbcon.c
blobbf9a9b773de03b744d3cbe30e23731f890514902
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>
80 #include <asm/system.h>
82 #include "fbcon.h"
84 #ifdef FBCONDEBUG
85 # define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__ , ## args)
86 #else
87 # define DPRINTK(fmt, args...)
88 #endif
90 enum {
91 FBCON_LOGO_CANSHOW = -1, /* the logo can be shown */
92 FBCON_LOGO_DRAW = -2, /* draw the logo to a console */
93 FBCON_LOGO_DONTSHOW = -3 /* do not show the logo */
96 static struct display fb_display[MAX_NR_CONSOLES];
98 static signed char con2fb_map[MAX_NR_CONSOLES];
99 static signed char con2fb_map_boot[MAX_NR_CONSOLES];
101 static int logo_lines;
102 /* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO
103 enums. */
104 static int logo_shown = FBCON_LOGO_CANSHOW;
105 /* Software scrollback */
106 static int fbcon_softback_size = 32768;
107 static unsigned long softback_buf, softback_curr;
108 static unsigned long softback_in;
109 static unsigned long softback_top, softback_end;
110 static int softback_lines;
111 /* console mappings */
112 static int first_fb_vc;
113 static int last_fb_vc = MAX_NR_CONSOLES - 1;
114 static int fbcon_is_default = 1;
115 static int fbcon_has_exited;
116 static int primary_device = -1;
117 static int fbcon_has_console_bind;
119 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
120 static int map_override;
122 static inline void fbcon_map_override(void)
124 map_override = 1;
126 #else
127 static inline void fbcon_map_override(void)
130 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY */
132 /* font data */
133 static char fontname[40];
135 /* current fb_info */
136 static int info_idx = -1;
138 /* console rotation */
139 static int initial_rotation;
140 static int fbcon_has_sysfs;
142 static const struct consw fb_con;
144 #define CM_SOFTBACK (8)
146 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
148 static int fbcon_set_origin(struct vc_data *);
150 #define CURSOR_DRAW_DELAY (1)
152 static int vbl_cursor_cnt;
153 static int fbcon_cursor_noblink;
155 #define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1)
158 * Interface used by the world
161 static const char *fbcon_startup(void);
162 static void fbcon_init(struct vc_data *vc, int init);
163 static void fbcon_deinit(struct vc_data *vc);
164 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
165 int width);
166 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
167 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
168 int count, int ypos, int xpos);
169 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
170 static void fbcon_cursor(struct vc_data *vc, int mode);
171 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
172 int count);
173 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
174 int height, int width);
175 static int fbcon_switch(struct vc_data *vc);
176 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
177 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
178 static int fbcon_scrolldelta(struct vc_data *vc, int lines);
181 * Internal routines
183 static __inline__ void ywrap_up(struct vc_data *vc, int count);
184 static __inline__ void ywrap_down(struct vc_data *vc, int count);
185 static __inline__ void ypan_up(struct vc_data *vc, int count);
186 static __inline__ void ypan_down(struct vc_data *vc, int count);
187 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
188 int dy, int dx, int height, int width, u_int y_break);
189 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
190 int unit);
191 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
192 int line, int count, int dy);
193 static void fbcon_modechanged(struct fb_info *info);
194 static void fbcon_set_all_vcs(struct fb_info *info);
195 static void fbcon_start(void);
196 static void fbcon_exit(void);
197 static struct device *fbcon_device;
199 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
200 static inline void fbcon_set_rotation(struct fb_info *info)
202 struct fbcon_ops *ops = info->fbcon_par;
204 if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
205 ops->p->con_rotate < 4)
206 ops->rotate = ops->p->con_rotate;
207 else
208 ops->rotate = 0;
211 static void fbcon_rotate(struct fb_info *info, u32 rotate)
213 struct fbcon_ops *ops= info->fbcon_par;
214 struct fb_info *fb_info;
216 if (!ops || ops->currcon == -1)
217 return;
219 fb_info = registered_fb[con2fb_map[ops->currcon]];
221 if (info == fb_info) {
222 struct display *p = &fb_display[ops->currcon];
224 if (rotate < 4)
225 p->con_rotate = rotate;
226 else
227 p->con_rotate = 0;
229 fbcon_modechanged(info);
233 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
235 struct fbcon_ops *ops = info->fbcon_par;
236 struct vc_data *vc;
237 struct display *p;
238 int i;
240 if (!ops || ops->currcon < 0 || rotate > 3)
241 return;
243 for (i = first_fb_vc; i <= last_fb_vc; i++) {
244 vc = vc_cons[i].d;
245 if (!vc || vc->vc_mode != KD_TEXT ||
246 registered_fb[con2fb_map[i]] != info)
247 continue;
249 p = &fb_display[vc->vc_num];
250 p->con_rotate = rotate;
253 fbcon_set_all_vcs(info);
255 #else
256 static inline void fbcon_set_rotation(struct fb_info *info)
258 struct fbcon_ops *ops = info->fbcon_par;
260 ops->rotate = FB_ROTATE_UR;
263 static void fbcon_rotate(struct fb_info *info, u32 rotate)
265 return;
268 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
270 return;
272 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
274 static int fbcon_get_rotate(struct fb_info *info)
276 struct fbcon_ops *ops = info->fbcon_par;
278 return (ops) ? ops->rotate : 0;
281 static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
283 struct fbcon_ops *ops = info->fbcon_par;
285 return (info->state != FBINFO_STATE_RUNNING ||
286 vc->vc_mode != KD_TEXT || ops->graphics) &&
287 !vt_force_oops_output(vc);
290 static int get_color(struct vc_data *vc, struct fb_info *info,
291 u16 c, int is_fg)
293 int depth = fb_get_color_depth(&info->var, &info->fix);
294 int color = 0;
296 if (console_blanked) {
297 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
299 c = vc->vc_video_erase_char & charmask;
302 if (depth != 1)
303 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
304 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
306 switch (depth) {
307 case 1:
309 int col = mono_col(info);
310 /* 0 or 1 */
311 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
312 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
314 if (console_blanked)
315 fg = bg;
317 color = (is_fg) ? fg : bg;
318 break;
320 case 2:
322 * Scale down 16-colors to 4 colors. Default 4-color palette
323 * is grayscale. However, simply dividing the values by 4
324 * will not work, as colors 1, 2 and 3 will be scaled-down
325 * to zero rendering them invisible. So empirically convert
326 * colors to a sane 4-level grayscale.
328 switch (color) {
329 case 0:
330 color = 0; /* black */
331 break;
332 case 1 ... 6:
333 color = 2; /* white */
334 break;
335 case 7 ... 8:
336 color = 1; /* gray */
337 break;
338 default:
339 color = 3; /* intense white */
340 break;
342 break;
343 case 3:
345 * Last 8 entries of default 16-color palette is a more intense
346 * version of the first 8 (i.e., same chrominance, different
347 * luminance).
349 color &= 7;
350 break;
354 return color;
357 static void fbcon_update_softback(struct vc_data *vc)
359 int l = fbcon_softback_size / vc->vc_size_row;
361 if (l > 5)
362 softback_end = softback_buf + l * vc->vc_size_row;
363 else
364 /* Smaller scrollback makes no sense, and 0 would screw
365 the operation totally */
366 softback_top = 0;
369 static void fb_flashcursor(struct work_struct *work)
371 struct fb_info *info = container_of(work, struct fb_info, queue);
372 struct fbcon_ops *ops = info->fbcon_par;
373 struct vc_data *vc = NULL;
374 int c;
375 int mode;
376 int ret;
378 /* FIXME: we should sort out the unbind locking instead */
379 /* instead we just fail to flash the cursor if we can't get
380 * the lock instead of blocking fbcon deinit */
381 ret = console_trylock();
382 if (ret == 0)
383 return;
385 if (ops && ops->currcon != -1)
386 vc = vc_cons[ops->currcon].d;
388 if (!vc || !CON_IS_VISIBLE(vc) ||
389 registered_fb[con2fb_map[vc->vc_num]] != info ||
390 vc->vc_deccm != 1) {
391 console_unlock();
392 return;
395 c = scr_readw((u16 *) vc->vc_pos);
396 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
397 CM_ERASE : CM_DRAW;
398 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
399 get_color(vc, info, c, 0));
400 console_unlock();
403 static void cursor_timer_handler(unsigned long dev_addr)
405 struct fb_info *info = (struct fb_info *) dev_addr;
406 struct fbcon_ops *ops = info->fbcon_par;
408 schedule_work(&info->queue);
409 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
412 static void fbcon_add_cursor_timer(struct fb_info *info)
414 struct fbcon_ops *ops = info->fbcon_par;
416 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
417 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
418 !fbcon_cursor_noblink) {
419 if (!info->queue.func)
420 INIT_WORK(&info->queue, fb_flashcursor);
422 init_timer(&ops->cursor_timer);
423 ops->cursor_timer.function = cursor_timer_handler;
424 ops->cursor_timer.expires = jiffies + HZ / 5;
425 ops->cursor_timer.data = (unsigned long ) info;
426 add_timer(&ops->cursor_timer);
427 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
431 static void fbcon_del_cursor_timer(struct fb_info *info)
433 struct fbcon_ops *ops = info->fbcon_par;
435 if (info->queue.func == fb_flashcursor &&
436 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
437 del_timer_sync(&ops->cursor_timer);
438 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
442 #ifndef MODULE
443 static int __init fb_console_setup(char *this_opt)
445 char *options;
446 int i, j;
448 if (!this_opt || !*this_opt)
449 return 1;
451 while ((options = strsep(&this_opt, ",")) != NULL) {
452 if (!strncmp(options, "font:", 5))
453 strcpy(fontname, options + 5);
455 if (!strncmp(options, "scrollback:", 11)) {
456 options += 11;
457 if (*options) {
458 fbcon_softback_size = simple_strtoul(options, &options, 0);
459 if (*options == 'k' || *options == 'K') {
460 fbcon_softback_size *= 1024;
461 options++;
463 if (*options != ',')
464 return 1;
465 options++;
466 } else
467 return 1;
470 if (!strncmp(options, "map:", 4)) {
471 options += 4;
472 if (*options) {
473 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
474 if (!options[j])
475 j = 0;
476 con2fb_map_boot[i] =
477 (options[j++]-'0') % FB_MAX;
480 fbcon_map_override();
483 return 1;
486 if (!strncmp(options, "vc:", 3)) {
487 options += 3;
488 if (*options)
489 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
490 if (first_fb_vc < 0)
491 first_fb_vc = 0;
492 if (*options++ == '-')
493 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
494 fbcon_is_default = 0;
497 if (!strncmp(options, "rotate:", 7)) {
498 options += 7;
499 if (*options)
500 initial_rotation = simple_strtoul(options, &options, 0);
501 if (initial_rotation > 3)
502 initial_rotation = 0;
505 return 1;
508 __setup("fbcon=", fb_console_setup);
509 #endif
511 static int search_fb_in_map(int idx)
513 int i, retval = 0;
515 for (i = first_fb_vc; i <= last_fb_vc; i++) {
516 if (con2fb_map[i] == idx)
517 retval = 1;
519 return retval;
522 static int search_for_mapped_con(void)
524 int i, retval = 0;
526 for (i = first_fb_vc; i <= last_fb_vc; i++) {
527 if (con2fb_map[i] != -1)
528 retval = 1;
530 return retval;
533 static int fbcon_takeover(int show_logo)
535 int err, i;
537 if (!num_registered_fb)
538 return -ENODEV;
540 if (!show_logo)
541 logo_shown = FBCON_LOGO_DONTSHOW;
543 for (i = first_fb_vc; i <= last_fb_vc; i++)
544 con2fb_map[i] = info_idx;
546 err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
547 fbcon_is_default);
549 if (err) {
550 for (i = first_fb_vc; i <= last_fb_vc; i++) {
551 con2fb_map[i] = -1;
553 info_idx = -1;
554 } else {
555 fbcon_has_console_bind = 1;
558 return err;
561 #ifdef MODULE
562 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
563 int cols, int rows, int new_cols, int new_rows)
565 logo_shown = FBCON_LOGO_DONTSHOW;
567 #else
568 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
569 int cols, int rows, int new_cols, int new_rows)
571 /* Need to make room for the logo */
572 struct fbcon_ops *ops = info->fbcon_par;
573 int cnt, erase = vc->vc_video_erase_char, step;
574 unsigned short *save = NULL, *r, *q;
575 int logo_height;
577 if (info->flags & FBINFO_MODULE) {
578 logo_shown = FBCON_LOGO_DONTSHOW;
579 return;
583 * remove underline attribute from erase character
584 * if black and white framebuffer.
586 if (fb_get_color_depth(&info->var, &info->fix) == 1)
587 erase &= ~0x400;
588 logo_height = fb_prepare_logo(info, ops->rotate);
589 logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
590 q = (unsigned short *) (vc->vc_origin +
591 vc->vc_size_row * rows);
592 step = logo_lines * cols;
593 for (r = q - logo_lines * cols; r < q; r++)
594 if (scr_readw(r) != vc->vc_video_erase_char)
595 break;
596 if (r != q && new_rows >= rows + logo_lines) {
597 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
598 if (save) {
599 int i = cols < new_cols ? cols : new_cols;
600 scr_memsetw(save, erase, logo_lines * new_cols * 2);
601 r = q - step;
602 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
603 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
604 r = q;
607 if (r == q) {
608 /* We can scroll screen down */
609 r = q - step - cols;
610 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
611 scr_memcpyw(r + step, r, vc->vc_size_row);
612 r -= cols;
614 if (!save) {
615 int lines;
616 if (vc->vc_y + logo_lines >= rows)
617 lines = rows - vc->vc_y - 1;
618 else
619 lines = logo_lines;
620 vc->vc_y += lines;
621 vc->vc_pos += lines * vc->vc_size_row;
624 scr_memsetw((unsigned short *) vc->vc_origin,
625 erase,
626 vc->vc_size_row * logo_lines);
628 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
629 fbcon_clear_margins(vc, 0);
630 update_screen(vc);
633 if (save) {
634 q = (unsigned short *) (vc->vc_origin +
635 vc->vc_size_row *
636 rows);
637 scr_memcpyw(q, save, logo_lines * new_cols * 2);
638 vc->vc_y += logo_lines;
639 vc->vc_pos += logo_lines * vc->vc_size_row;
640 kfree(save);
643 if (logo_lines > vc->vc_bottom) {
644 logo_shown = FBCON_LOGO_CANSHOW;
645 printk(KERN_INFO
646 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
647 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
648 logo_shown = FBCON_LOGO_DRAW;
649 vc->vc_top = logo_lines;
652 #endif /* MODULE */
654 #ifdef CONFIG_FB_TILEBLITTING
655 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
657 struct fbcon_ops *ops = info->fbcon_par;
659 ops->p = &fb_display[vc->vc_num];
661 if ((info->flags & FBINFO_MISC_TILEBLITTING))
662 fbcon_set_tileops(vc, info);
663 else {
664 fbcon_set_rotation(info);
665 fbcon_set_bitops(ops);
669 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
671 int err = 0;
673 if (info->flags & FBINFO_MISC_TILEBLITTING &&
674 info->tileops->fb_get_tilemax(info) < charcount)
675 err = 1;
677 return err;
679 #else
680 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
682 struct fbcon_ops *ops = info->fbcon_par;
684 info->flags &= ~FBINFO_MISC_TILEBLITTING;
685 ops->p = &fb_display[vc->vc_num];
686 fbcon_set_rotation(info);
687 fbcon_set_bitops(ops);
690 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
692 return 0;
695 #endif /* CONFIG_MISC_TILEBLITTING */
698 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
699 int unit, int oldidx)
701 struct fbcon_ops *ops = NULL;
702 int err = 0;
704 if (!try_module_get(info->fbops->owner))
705 err = -ENODEV;
707 if (!err && info->fbops->fb_open &&
708 info->fbops->fb_open(info, 0))
709 err = -ENODEV;
711 if (!err) {
712 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
713 if (!ops)
714 err = -ENOMEM;
717 if (!err) {
718 info->fbcon_par = ops;
720 if (vc)
721 set_blitting_type(vc, info);
724 if (err) {
725 con2fb_map[unit] = oldidx;
726 module_put(info->fbops->owner);
729 return err;
732 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
733 struct fb_info *newinfo, int unit,
734 int oldidx, int found)
736 struct fbcon_ops *ops = oldinfo->fbcon_par;
737 int err = 0, ret;
739 if (oldinfo->fbops->fb_release &&
740 oldinfo->fbops->fb_release(oldinfo, 0)) {
741 con2fb_map[unit] = oldidx;
742 if (!found && newinfo->fbops->fb_release)
743 newinfo->fbops->fb_release(newinfo, 0);
744 if (!found)
745 module_put(newinfo->fbops->owner);
746 err = -ENODEV;
749 if (!err) {
750 fbcon_del_cursor_timer(oldinfo);
751 kfree(ops->cursor_state.mask);
752 kfree(ops->cursor_data);
753 kfree(ops->fontbuffer);
754 kfree(oldinfo->fbcon_par);
755 oldinfo->fbcon_par = NULL;
756 module_put(oldinfo->fbops->owner);
758 If oldinfo and newinfo are driving the same hardware,
759 the fb_release() method of oldinfo may attempt to
760 restore the hardware state. This will leave the
761 newinfo in an undefined state. Thus, a call to
762 fb_set_par() may be needed for the newinfo.
764 if (newinfo->fbops->fb_set_par) {
765 ret = newinfo->fbops->fb_set_par(newinfo);
767 if (ret)
768 printk(KERN_ERR "con2fb_release_oldinfo: "
769 "detected unhandled fb_set_par error, "
770 "error code %d\n", ret);
774 return err;
777 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
778 int unit, int show_logo)
780 struct fbcon_ops *ops = info->fbcon_par;
781 int ret;
783 ops->currcon = fg_console;
785 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT)) {
786 ret = info->fbops->fb_set_par(info);
788 if (ret)
789 printk(KERN_ERR "con2fb_init_display: detected "
790 "unhandled fb_set_par error, "
791 "error code %d\n", ret);
794 ops->flags |= FBCON_FLAGS_INIT;
795 ops->graphics = 0;
796 fbcon_set_disp(info, &info->var, unit);
798 if (show_logo) {
799 struct vc_data *fg_vc = vc_cons[fg_console].d;
800 struct fb_info *fg_info =
801 registered_fb[con2fb_map[fg_console]];
803 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
804 fg_vc->vc_rows, fg_vc->vc_cols,
805 fg_vc->vc_rows);
808 update_screen(vc_cons[fg_console].d);
812 * set_con2fb_map - map console to frame buffer device
813 * @unit: virtual console number to map
814 * @newidx: frame buffer index to map virtual console to
815 * @user: user request
817 * Maps a virtual console @unit to a frame buffer device
818 * @newidx.
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 fbcon_takeover(0);
839 if (oldidx != -1)
840 oldinfo = registered_fb[oldidx];
842 found = search_fb_in_map(newidx);
844 console_lock();
845 con2fb_map[unit] = newidx;
846 if (!err && !found)
847 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
851 * If old fb is not mapped to any of the consoles,
852 * fbcon should release it.
854 if (!err && oldinfo && !search_fb_in_map(oldidx))
855 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
856 found);
858 if (!err) {
859 int show_logo = (fg_console == 0 && !user &&
860 logo_shown != FBCON_LOGO_DONTSHOW);
862 if (!found)
863 fbcon_add_cursor_timer(info);
864 con2fb_map_boot[unit] = newidx;
865 con2fb_init_display(vc, info, unit, show_logo);
868 if (!search_fb_in_map(info_idx))
869 info_idx = newidx;
871 console_unlock();
872 return err;
876 * Low Level Operations
878 /* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
879 static int var_to_display(struct display *disp,
880 struct fb_var_screeninfo *var,
881 struct fb_info *info)
883 disp->xres_virtual = var->xres_virtual;
884 disp->yres_virtual = var->yres_virtual;
885 disp->bits_per_pixel = var->bits_per_pixel;
886 disp->grayscale = var->grayscale;
887 disp->nonstd = var->nonstd;
888 disp->accel_flags = var->accel_flags;
889 disp->height = var->height;
890 disp->width = var->width;
891 disp->red = var->red;
892 disp->green = var->green;
893 disp->blue = var->blue;
894 disp->transp = var->transp;
895 disp->rotate = var->rotate;
896 disp->mode = fb_match_mode(var, &info->modelist);
897 if (disp->mode == NULL)
898 /* This should not happen */
899 return -EINVAL;
900 return 0;
903 static void display_to_var(struct fb_var_screeninfo *var,
904 struct display *disp)
906 fb_videomode_to_var(var, disp->mode);
907 var->xres_virtual = disp->xres_virtual;
908 var->yres_virtual = disp->yres_virtual;
909 var->bits_per_pixel = disp->bits_per_pixel;
910 var->grayscale = disp->grayscale;
911 var->nonstd = disp->nonstd;
912 var->accel_flags = disp->accel_flags;
913 var->height = disp->height;
914 var->width = disp->width;
915 var->red = disp->red;
916 var->green = disp->green;
917 var->blue = disp->blue;
918 var->transp = disp->transp;
919 var->rotate = disp->rotate;
922 static const char *fbcon_startup(void)
924 const char *display_desc = "frame buffer device";
925 struct display *p = &fb_display[fg_console];
926 struct vc_data *vc = vc_cons[fg_console].d;
927 const struct font_desc *font = NULL;
928 struct module *owner;
929 struct fb_info *info = NULL;
930 struct fbcon_ops *ops;
931 int rows, cols;
934 * If num_registered_fb is zero, this is a call for the dummy part.
935 * The frame buffer devices weren't initialized yet.
937 if (!num_registered_fb || info_idx == -1)
938 return display_desc;
940 * Instead of blindly using registered_fb[0], we use info_idx, set by
941 * fb_console_init();
943 info = registered_fb[info_idx];
944 if (!info)
945 return NULL;
947 owner = info->fbops->owner;
948 if (!try_module_get(owner))
949 return NULL;
950 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
951 module_put(owner);
952 return NULL;
955 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
956 if (!ops) {
957 module_put(owner);
958 return NULL;
961 ops->currcon = -1;
962 ops->graphics = 1;
963 ops->cur_rotate = -1;
964 info->fbcon_par = ops;
965 p->con_rotate = initial_rotation;
966 set_blitting_type(vc, info);
968 if (info->fix.type != FB_TYPE_TEXT) {
969 if (fbcon_softback_size) {
970 if (!softback_buf) {
971 softback_buf =
972 (unsigned long)
973 kmalloc(fbcon_softback_size,
974 GFP_KERNEL);
975 if (!softback_buf) {
976 fbcon_softback_size = 0;
977 softback_top = 0;
980 } else {
981 if (softback_buf) {
982 kfree((void *) softback_buf);
983 softback_buf = 0;
984 softback_top = 0;
987 if (softback_buf)
988 softback_in = softback_top = softback_curr =
989 softback_buf;
990 softback_lines = 0;
993 /* Setup default font */
994 if (!p->fontdata) {
995 if (!fontname[0] || !(font = find_font(fontname)))
996 font = get_default_font(info->var.xres,
997 info->var.yres,
998 info->pixmap.blit_x,
999 info->pixmap.blit_y);
1000 vc->vc_font.width = font->width;
1001 vc->vc_font.height = font->height;
1002 vc->vc_font.data = (void *)(p->fontdata = font->data);
1003 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
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)
1165 if (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;
1178 fbcon_free_font(p);
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 ops = info->fbcon_par;
1191 if (!ops)
1192 goto finished;
1194 if (CON_IS_VISIBLE(vc))
1195 fbcon_del_cursor_timer(info);
1197 ops->flags &= ~FBCON_FLAGS_INIT;
1198 finished:
1200 if (!con_is_bound(&fb_con))
1201 fbcon_exit();
1203 return;
1206 /* ====================================================================== */
1208 /* fbcon_XXX routines - interface used by the world
1210 * This system is now divided into two levels because of complications
1211 * caused by hardware scrolling. Top level functions:
1213 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1215 * handles y values in range [0, scr_height-1] that correspond to real
1216 * screen positions. y_wrap shift means that first line of bitmap may be
1217 * anywhere on this display. These functions convert lineoffsets to
1218 * bitmap offsets and deal with the wrap-around case by splitting blits.
1220 * fbcon_bmove_physical_8() -- These functions fast implementations
1221 * fbcon_clear_physical_8() -- of original fbcon_XXX fns.
1222 * fbcon_putc_physical_8() -- (font width != 8) may be added later
1224 * WARNING:
1226 * At the moment fbcon_putc() cannot blit across vertical wrap boundary
1227 * Implies should only really hardware scroll in rows. Only reason for
1228 * restriction is simplicity & efficiency at the moment.
1231 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1232 int width)
1234 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1235 struct fbcon_ops *ops = info->fbcon_par;
1237 struct display *p = &fb_display[vc->vc_num];
1238 u_int y_break;
1240 if (fbcon_is_inactive(vc, info))
1241 return;
1243 if (!height || !width)
1244 return;
1246 if (sy < vc->vc_top && vc->vc_top == logo_lines)
1247 vc->vc_top = 0;
1249 /* Split blits that cross physical y_wrap boundary */
1251 y_break = p->vrows - p->yscroll;
1252 if (sy < y_break && sy + height - 1 >= y_break) {
1253 u_int b = y_break - sy;
1254 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1255 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1256 width);
1257 } else
1258 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1261 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1262 int count, int ypos, int xpos)
1264 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1265 struct display *p = &fb_display[vc->vc_num];
1266 struct fbcon_ops *ops = info->fbcon_par;
1268 if (!fbcon_is_inactive(vc, info))
1269 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1270 get_color(vc, info, scr_readw(s), 1),
1271 get_color(vc, info, scr_readw(s), 0));
1274 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1276 unsigned short chr;
1278 scr_writew(c, &chr);
1279 fbcon_putcs(vc, &chr, 1, ypos, xpos);
1282 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1284 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1285 struct fbcon_ops *ops = info->fbcon_par;
1287 if (!fbcon_is_inactive(vc, info))
1288 ops->clear_margins(vc, info, bottom_only);
1291 static void fbcon_cursor(struct vc_data *vc, int mode)
1293 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1294 struct fbcon_ops *ops = info->fbcon_par;
1295 int y;
1296 int c = scr_readw((u16 *) vc->vc_pos);
1298 if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1299 return;
1301 if (vc->vc_cursor_type & 0x10)
1302 fbcon_del_cursor_timer(info);
1303 else
1304 fbcon_add_cursor_timer(info);
1306 ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1307 if (mode & CM_SOFTBACK) {
1308 mode &= ~CM_SOFTBACK;
1309 y = softback_lines;
1310 } else {
1311 if (softback_lines)
1312 fbcon_set_origin(vc);
1313 y = 0;
1316 ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1317 get_color(vc, info, c, 0));
1318 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1321 static int scrollback_phys_max = 0;
1322 static int scrollback_max = 0;
1323 static int scrollback_current = 0;
1325 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1326 int unit)
1328 struct display *p, *t;
1329 struct vc_data **default_mode, *vc;
1330 struct vc_data *svc;
1331 struct fbcon_ops *ops = info->fbcon_par;
1332 int rows, cols, charcnt = 256;
1334 p = &fb_display[unit];
1336 if (var_to_display(p, var, info))
1337 return;
1339 vc = vc_cons[unit].d;
1341 if (!vc)
1342 return;
1344 default_mode = vc->vc_display_fg;
1345 svc = *default_mode;
1346 t = &fb_display[svc->vc_num];
1348 if (!vc->vc_font.data) {
1349 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1350 vc->vc_font.width = (*default_mode)->vc_font.width;
1351 vc->vc_font.height = (*default_mode)->vc_font.height;
1352 p->userfont = t->userfont;
1353 if (p->userfont)
1354 REFCOUNT(p->fontdata)++;
1356 if (p->userfont)
1357 charcnt = FNTCHARCNT(p->fontdata);
1359 var->activate = FB_ACTIVATE_NOW;
1360 info->var.activate = var->activate;
1361 var->yoffset = info->var.yoffset;
1362 var->xoffset = info->var.xoffset;
1363 fb_set_var(info, var);
1364 ops->var = info->var;
1365 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1366 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1367 if (charcnt == 256) {
1368 vc->vc_hi_font_mask = 0;
1369 } else {
1370 vc->vc_hi_font_mask = 0x100;
1371 if (vc->vc_can_do_color)
1372 vc->vc_complement_mask <<= 1;
1375 if (!*svc->vc_uni_pagedir_loc)
1376 con_set_default_unimap(svc);
1377 if (!*vc->vc_uni_pagedir_loc)
1378 con_copy_unimap(vc, svc);
1380 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1381 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1382 cols /= vc->vc_font.width;
1383 rows /= vc->vc_font.height;
1384 vc_resize(vc, cols, rows);
1386 if (CON_IS_VISIBLE(vc)) {
1387 update_screen(vc);
1388 if (softback_buf)
1389 fbcon_update_softback(vc);
1393 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1395 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1396 struct fbcon_ops *ops = info->fbcon_par;
1397 struct display *p = &fb_display[vc->vc_num];
1399 p->yscroll += count;
1400 if (p->yscroll >= p->vrows) /* Deal with wrap */
1401 p->yscroll -= p->vrows;
1402 ops->var.xoffset = 0;
1403 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1404 ops->var.vmode |= FB_VMODE_YWRAP;
1405 ops->update_start(info);
1406 scrollback_max += count;
1407 if (scrollback_max > scrollback_phys_max)
1408 scrollback_max = scrollback_phys_max;
1409 scrollback_current = 0;
1412 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1414 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1415 struct fbcon_ops *ops = info->fbcon_par;
1416 struct display *p = &fb_display[vc->vc_num];
1418 p->yscroll -= count;
1419 if (p->yscroll < 0) /* Deal with wrap */
1420 p->yscroll += p->vrows;
1421 ops->var.xoffset = 0;
1422 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1423 ops->var.vmode |= FB_VMODE_YWRAP;
1424 ops->update_start(info);
1425 scrollback_max -= count;
1426 if (scrollback_max < 0)
1427 scrollback_max = 0;
1428 scrollback_current = 0;
1431 static __inline__ void ypan_up(struct vc_data *vc, int count)
1433 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1434 struct display *p = &fb_display[vc->vc_num];
1435 struct fbcon_ops *ops = info->fbcon_par;
1437 p->yscroll += count;
1438 if (p->yscroll > p->vrows - vc->vc_rows) {
1439 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1440 0, 0, 0, vc->vc_rows, vc->vc_cols);
1441 p->yscroll -= p->vrows - vc->vc_rows;
1444 ops->var.xoffset = 0;
1445 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1446 ops->var.vmode &= ~FB_VMODE_YWRAP;
1447 ops->update_start(info);
1448 fbcon_clear_margins(vc, 1);
1449 scrollback_max += count;
1450 if (scrollback_max > scrollback_phys_max)
1451 scrollback_max = scrollback_phys_max;
1452 scrollback_current = 0;
1455 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1457 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1458 struct fbcon_ops *ops = info->fbcon_par;
1459 struct display *p = &fb_display[vc->vc_num];
1461 p->yscroll += count;
1463 if (p->yscroll > p->vrows - vc->vc_rows) {
1464 p->yscroll -= p->vrows - vc->vc_rows;
1465 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1468 ops->var.xoffset = 0;
1469 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1470 ops->var.vmode &= ~FB_VMODE_YWRAP;
1471 ops->update_start(info);
1472 fbcon_clear_margins(vc, 1);
1473 scrollback_max += count;
1474 if (scrollback_max > scrollback_phys_max)
1475 scrollback_max = scrollback_phys_max;
1476 scrollback_current = 0;
1479 static __inline__ void ypan_down(struct vc_data *vc, int count)
1481 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1482 struct display *p = &fb_display[vc->vc_num];
1483 struct fbcon_ops *ops = info->fbcon_par;
1485 p->yscroll -= count;
1486 if (p->yscroll < 0) {
1487 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1488 0, vc->vc_rows, vc->vc_cols);
1489 p->yscroll += p->vrows - vc->vc_rows;
1492 ops->var.xoffset = 0;
1493 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1494 ops->var.vmode &= ~FB_VMODE_YWRAP;
1495 ops->update_start(info);
1496 fbcon_clear_margins(vc, 1);
1497 scrollback_max -= count;
1498 if (scrollback_max < 0)
1499 scrollback_max = 0;
1500 scrollback_current = 0;
1503 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1505 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1506 struct fbcon_ops *ops = info->fbcon_par;
1507 struct display *p = &fb_display[vc->vc_num];
1509 p->yscroll -= count;
1511 if (p->yscroll < 0) {
1512 p->yscroll += p->vrows - vc->vc_rows;
1513 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1516 ops->var.xoffset = 0;
1517 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1518 ops->var.vmode &= ~FB_VMODE_YWRAP;
1519 ops->update_start(info);
1520 fbcon_clear_margins(vc, 1);
1521 scrollback_max -= count;
1522 if (scrollback_max < 0)
1523 scrollback_max = 0;
1524 scrollback_current = 0;
1527 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1528 long delta)
1530 int count = vc->vc_rows;
1531 unsigned short *d, *s;
1532 unsigned long n;
1533 int line = 0;
1535 d = (u16 *) softback_curr;
1536 if (d == (u16 *) softback_in)
1537 d = (u16 *) vc->vc_origin;
1538 n = softback_curr + delta * vc->vc_size_row;
1539 softback_lines -= delta;
1540 if (delta < 0) {
1541 if (softback_curr < softback_top && n < softback_buf) {
1542 n += softback_end - softback_buf;
1543 if (n < softback_top) {
1544 softback_lines -=
1545 (softback_top - n) / vc->vc_size_row;
1546 n = softback_top;
1548 } else if (softback_curr >= softback_top
1549 && n < softback_top) {
1550 softback_lines -=
1551 (softback_top - n) / vc->vc_size_row;
1552 n = softback_top;
1554 } else {
1555 if (softback_curr > softback_in && n >= softback_end) {
1556 n += softback_buf - softback_end;
1557 if (n > softback_in) {
1558 n = softback_in;
1559 softback_lines = 0;
1561 } else if (softback_curr <= softback_in && n > softback_in) {
1562 n = softback_in;
1563 softback_lines = 0;
1566 if (n == softback_curr)
1567 return;
1568 softback_curr = n;
1569 s = (u16 *) softback_curr;
1570 if (s == (u16 *) softback_in)
1571 s = (u16 *) vc->vc_origin;
1572 while (count--) {
1573 unsigned short *start;
1574 unsigned short *le;
1575 unsigned short c;
1576 int x = 0;
1577 unsigned short attr = 1;
1579 start = s;
1580 le = advance_row(s, 1);
1581 do {
1582 c = scr_readw(s);
1583 if (attr != (c & 0xff00)) {
1584 attr = c & 0xff00;
1585 if (s > start) {
1586 fbcon_putcs(vc, start, s - start,
1587 line, x);
1588 x += s - start;
1589 start = s;
1592 if (c == scr_readw(d)) {
1593 if (s > start) {
1594 fbcon_putcs(vc, start, s - start,
1595 line, x);
1596 x += s - start + 1;
1597 start = s + 1;
1598 } else {
1599 x++;
1600 start++;
1603 s++;
1604 d++;
1605 } while (s < le);
1606 if (s > start)
1607 fbcon_putcs(vc, start, s - start, line, x);
1608 line++;
1609 if (d == (u16 *) softback_end)
1610 d = (u16 *) softback_buf;
1611 if (d == (u16 *) softback_in)
1612 d = (u16 *) vc->vc_origin;
1613 if (s == (u16 *) softback_end)
1614 s = (u16 *) softback_buf;
1615 if (s == (u16 *) softback_in)
1616 s = (u16 *) vc->vc_origin;
1620 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1621 int line, int count, int dy)
1623 unsigned short *s = (unsigned short *)
1624 (vc->vc_origin + vc->vc_size_row * line);
1626 while (count--) {
1627 unsigned short *start = s;
1628 unsigned short *le = advance_row(s, 1);
1629 unsigned short c;
1630 int x = 0;
1631 unsigned short attr = 1;
1633 do {
1634 c = scr_readw(s);
1635 if (attr != (c & 0xff00)) {
1636 attr = c & 0xff00;
1637 if (s > start) {
1638 fbcon_putcs(vc, start, s - start,
1639 dy, x);
1640 x += s - start;
1641 start = s;
1644 console_conditional_schedule();
1645 s++;
1646 } while (s < le);
1647 if (s > start)
1648 fbcon_putcs(vc, start, s - start, dy, x);
1649 console_conditional_schedule();
1650 dy++;
1654 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1655 struct display *p, int line, int count, int ycount)
1657 int offset = ycount * vc->vc_cols;
1658 unsigned short *d = (unsigned short *)
1659 (vc->vc_origin + vc->vc_size_row * line);
1660 unsigned short *s = d + offset;
1661 struct fbcon_ops *ops = info->fbcon_par;
1663 while (count--) {
1664 unsigned short *start = s;
1665 unsigned short *le = advance_row(s, 1);
1666 unsigned short c;
1667 int x = 0;
1669 do {
1670 c = scr_readw(s);
1672 if (c == scr_readw(d)) {
1673 if (s > start) {
1674 ops->bmove(vc, info, line + ycount, x,
1675 line, x, 1, s-start);
1676 x += s - start + 1;
1677 start = s + 1;
1678 } else {
1679 x++;
1680 start++;
1684 scr_writew(c, d);
1685 console_conditional_schedule();
1686 s++;
1687 d++;
1688 } while (s < le);
1689 if (s > start)
1690 ops->bmove(vc, info, line + ycount, x, line, x, 1,
1691 s-start);
1692 console_conditional_schedule();
1693 if (ycount > 0)
1694 line++;
1695 else {
1696 line--;
1697 /* NOTE: We subtract two lines from these pointers */
1698 s -= vc->vc_size_row;
1699 d -= vc->vc_size_row;
1704 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1705 int line, int count, int offset)
1707 unsigned short *d = (unsigned short *)
1708 (vc->vc_origin + vc->vc_size_row * line);
1709 unsigned short *s = d + offset;
1711 while (count--) {
1712 unsigned short *start = s;
1713 unsigned short *le = advance_row(s, 1);
1714 unsigned short c;
1715 int x = 0;
1716 unsigned short attr = 1;
1718 do {
1719 c = scr_readw(s);
1720 if (attr != (c & 0xff00)) {
1721 attr = c & 0xff00;
1722 if (s > start) {
1723 fbcon_putcs(vc, start, s - start,
1724 line, x);
1725 x += s - start;
1726 start = s;
1729 if (c == scr_readw(d)) {
1730 if (s > start) {
1731 fbcon_putcs(vc, start, s - start,
1732 line, x);
1733 x += s - start + 1;
1734 start = s + 1;
1735 } else {
1736 x++;
1737 start++;
1740 scr_writew(c, d);
1741 console_conditional_schedule();
1742 s++;
1743 d++;
1744 } while (s < le);
1745 if (s > start)
1746 fbcon_putcs(vc, start, s - start, line, x);
1747 console_conditional_schedule();
1748 if (offset > 0)
1749 line++;
1750 else {
1751 line--;
1752 /* NOTE: We subtract two lines from these pointers */
1753 s -= vc->vc_size_row;
1754 d -= vc->vc_size_row;
1759 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1760 int count)
1762 unsigned short *p;
1764 if (vc->vc_num != fg_console)
1765 return;
1766 p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1768 while (count) {
1769 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1770 count--;
1771 p = advance_row(p, 1);
1772 softback_in += vc->vc_size_row;
1773 if (softback_in == softback_end)
1774 softback_in = softback_buf;
1775 if (softback_in == softback_top) {
1776 softback_top += vc->vc_size_row;
1777 if (softback_top == softback_end)
1778 softback_top = softback_buf;
1781 softback_curr = softback_in;
1784 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1785 int count)
1787 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1788 struct display *p = &fb_display[vc->vc_num];
1789 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1791 if (fbcon_is_inactive(vc, info))
1792 return -EINVAL;
1794 fbcon_cursor(vc, CM_ERASE);
1797 * ++Geert: Only use ywrap/ypan if the console is in text mode
1798 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1799 * whole screen (prevents flicker).
1802 switch (dir) {
1803 case SM_UP:
1804 if (count > vc->vc_rows) /* Maximum realistic size */
1805 count = vc->vc_rows;
1806 if (softback_top)
1807 fbcon_softback_note(vc, t, count);
1808 if (logo_shown >= 0)
1809 goto redraw_up;
1810 switch (p->scrollmode) {
1811 case SCROLL_MOVE:
1812 fbcon_redraw_blit(vc, info, p, t, b - t - count,
1813 count);
1814 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1815 scr_memsetw((unsigned short *) (vc->vc_origin +
1816 vc->vc_size_row *
1817 (b - count)),
1818 vc->vc_video_erase_char,
1819 vc->vc_size_row * count);
1820 return 1;
1821 break;
1823 case SCROLL_WRAP_MOVE:
1824 if (b - t - count > 3 * vc->vc_rows >> 2) {
1825 if (t > 0)
1826 fbcon_bmove(vc, 0, 0, count, 0, t,
1827 vc->vc_cols);
1828 ywrap_up(vc, count);
1829 if (vc->vc_rows - b > 0)
1830 fbcon_bmove(vc, b - count, 0, b, 0,
1831 vc->vc_rows - b,
1832 vc->vc_cols);
1833 } else if (info->flags & FBINFO_READS_FAST)
1834 fbcon_bmove(vc, t + count, 0, t, 0,
1835 b - t - count, vc->vc_cols);
1836 else
1837 goto redraw_up;
1838 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1839 break;
1841 case SCROLL_PAN_REDRAW:
1842 if ((p->yscroll + count <=
1843 2 * (p->vrows - vc->vc_rows))
1844 && ((!scroll_partial && (b - t == vc->vc_rows))
1845 || (scroll_partial
1846 && (b - t - count >
1847 3 * vc->vc_rows >> 2)))) {
1848 if (t > 0)
1849 fbcon_redraw_move(vc, p, 0, t, count);
1850 ypan_up_redraw(vc, t, count);
1851 if (vc->vc_rows - b > 0)
1852 fbcon_redraw_move(vc, p, b,
1853 vc->vc_rows - b, b);
1854 } else
1855 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1856 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1857 break;
1859 case SCROLL_PAN_MOVE:
1860 if ((p->yscroll + count <=
1861 2 * (p->vrows - vc->vc_rows))
1862 && ((!scroll_partial && (b - t == vc->vc_rows))
1863 || (scroll_partial
1864 && (b - t - count >
1865 3 * vc->vc_rows >> 2)))) {
1866 if (t > 0)
1867 fbcon_bmove(vc, 0, 0, count, 0, t,
1868 vc->vc_cols);
1869 ypan_up(vc, count);
1870 if (vc->vc_rows - b > 0)
1871 fbcon_bmove(vc, b - count, 0, b, 0,
1872 vc->vc_rows - b,
1873 vc->vc_cols);
1874 } else if (info->flags & FBINFO_READS_FAST)
1875 fbcon_bmove(vc, t + count, 0, t, 0,
1876 b - t - count, vc->vc_cols);
1877 else
1878 goto redraw_up;
1879 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1880 break;
1882 case SCROLL_REDRAW:
1883 redraw_up:
1884 fbcon_redraw(vc, p, t, b - t - count,
1885 count * vc->vc_cols);
1886 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1887 scr_memsetw((unsigned short *) (vc->vc_origin +
1888 vc->vc_size_row *
1889 (b - count)),
1890 vc->vc_video_erase_char,
1891 vc->vc_size_row * count);
1892 return 1;
1894 break;
1896 case SM_DOWN:
1897 if (count > vc->vc_rows) /* Maximum realistic size */
1898 count = vc->vc_rows;
1899 if (logo_shown >= 0)
1900 goto redraw_down;
1901 switch (p->scrollmode) {
1902 case SCROLL_MOVE:
1903 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1904 -count);
1905 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1906 scr_memsetw((unsigned short *) (vc->vc_origin +
1907 vc->vc_size_row *
1909 vc->vc_video_erase_char,
1910 vc->vc_size_row * count);
1911 return 1;
1912 break;
1914 case SCROLL_WRAP_MOVE:
1915 if (b - t - count > 3 * vc->vc_rows >> 2) {
1916 if (vc->vc_rows - b > 0)
1917 fbcon_bmove(vc, b, 0, b - count, 0,
1918 vc->vc_rows - b,
1919 vc->vc_cols);
1920 ywrap_down(vc, count);
1921 if (t > 0)
1922 fbcon_bmove(vc, count, 0, 0, 0, t,
1923 vc->vc_cols);
1924 } else if (info->flags & FBINFO_READS_FAST)
1925 fbcon_bmove(vc, t, 0, t + count, 0,
1926 b - t - count, vc->vc_cols);
1927 else
1928 goto redraw_down;
1929 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1930 break;
1932 case SCROLL_PAN_MOVE:
1933 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1934 && ((!scroll_partial && (b - t == vc->vc_rows))
1935 || (scroll_partial
1936 && (b - t - count >
1937 3 * vc->vc_rows >> 2)))) {
1938 if (vc->vc_rows - b > 0)
1939 fbcon_bmove(vc, b, 0, b - count, 0,
1940 vc->vc_rows - b,
1941 vc->vc_cols);
1942 ypan_down(vc, count);
1943 if (t > 0)
1944 fbcon_bmove(vc, count, 0, 0, 0, t,
1945 vc->vc_cols);
1946 } else if (info->flags & FBINFO_READS_FAST)
1947 fbcon_bmove(vc, t, 0, t + count, 0,
1948 b - t - count, vc->vc_cols);
1949 else
1950 goto redraw_down;
1951 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1952 break;
1954 case SCROLL_PAN_REDRAW:
1955 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1956 && ((!scroll_partial && (b - t == vc->vc_rows))
1957 || (scroll_partial
1958 && (b - t - count >
1959 3 * vc->vc_rows >> 2)))) {
1960 if (vc->vc_rows - b > 0)
1961 fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
1962 b - count);
1963 ypan_down_redraw(vc, t, count);
1964 if (t > 0)
1965 fbcon_redraw_move(vc, p, count, t, 0);
1966 } else
1967 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
1968 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1969 break;
1971 case SCROLL_REDRAW:
1972 redraw_down:
1973 fbcon_redraw(vc, p, b - 1, b - t - count,
1974 -count * vc->vc_cols);
1975 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1976 scr_memsetw((unsigned short *) (vc->vc_origin +
1977 vc->vc_size_row *
1979 vc->vc_video_erase_char,
1980 vc->vc_size_row * count);
1981 return 1;
1984 return 0;
1988 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
1989 int height, int width)
1991 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1992 struct display *p = &fb_display[vc->vc_num];
1994 if (fbcon_is_inactive(vc, info))
1995 return;
1997 if (!width || !height)
1998 return;
2000 /* Split blits that cross physical y_wrap case.
2001 * Pathological case involves 4 blits, better to use recursive
2002 * code rather than unrolled case
2004 * Recursive invocations don't need to erase the cursor over and
2005 * over again, so we use fbcon_bmove_rec()
2007 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2008 p->vrows - p->yscroll);
2011 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
2012 int dy, int dx, int height, int width, u_int y_break)
2014 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2015 struct fbcon_ops *ops = info->fbcon_par;
2016 u_int b;
2018 if (sy < y_break && sy + height > y_break) {
2019 b = y_break - sy;
2020 if (dy < sy) { /* Avoid trashing self */
2021 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2022 y_break);
2023 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2024 height - b, width, y_break);
2025 } else {
2026 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2027 height - b, width, y_break);
2028 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2029 y_break);
2031 return;
2034 if (dy < y_break && dy + height > y_break) {
2035 b = y_break - dy;
2036 if (dy < sy) { /* Avoid trashing self */
2037 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2038 y_break);
2039 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2040 height - b, width, y_break);
2041 } else {
2042 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2043 height - b, width, y_break);
2044 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2045 y_break);
2047 return;
2049 ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2050 height, width);
2053 static void updatescrollmode(struct display *p,
2054 struct fb_info *info,
2055 struct vc_data *vc)
2057 struct fbcon_ops *ops = info->fbcon_par;
2058 int fh = vc->vc_font.height;
2059 int cap = info->flags;
2060 u16 t = 0;
2061 int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2062 info->fix.xpanstep);
2063 int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2064 int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2065 int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2066 info->var.xres_virtual);
2067 int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2068 divides(ypan, vc->vc_font.height) && vyres > yres;
2069 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2070 divides(ywrap, vc->vc_font.height) &&
2071 divides(vc->vc_font.height, vyres) &&
2072 divides(vc->vc_font.height, yres);
2073 int reading_fast = cap & FBINFO_READS_FAST;
2074 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2075 !(cap & FBINFO_HWACCEL_DISABLED);
2076 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2077 !(cap & FBINFO_HWACCEL_DISABLED);
2079 p->vrows = vyres/fh;
2080 if (yres > (fh * (vc->vc_rows + 1)))
2081 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2082 if ((yres % fh) && (vyres % fh < yres % fh))
2083 p->vrows--;
2085 if (good_wrap || good_pan) {
2086 if (reading_fast || fast_copyarea)
2087 p->scrollmode = good_wrap ?
2088 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2089 else
2090 p->scrollmode = good_wrap ? SCROLL_REDRAW :
2091 SCROLL_PAN_REDRAW;
2092 } else {
2093 if (reading_fast || (fast_copyarea && !fast_imageblit))
2094 p->scrollmode = SCROLL_MOVE;
2095 else
2096 p->scrollmode = SCROLL_REDRAW;
2100 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2101 unsigned int height, unsigned int user)
2103 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2104 struct fbcon_ops *ops = info->fbcon_par;
2105 struct display *p = &fb_display[vc->vc_num];
2106 struct fb_var_screeninfo var = info->var;
2107 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2109 virt_w = FBCON_SWAP(ops->rotate, width, height);
2110 virt_h = FBCON_SWAP(ops->rotate, height, width);
2111 virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2112 vc->vc_font.height);
2113 virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2114 vc->vc_font.width);
2115 var.xres = virt_w * virt_fw;
2116 var.yres = virt_h * virt_fh;
2117 x_diff = info->var.xres - var.xres;
2118 y_diff = info->var.yres - var.yres;
2119 if (x_diff < 0 || x_diff > virt_fw ||
2120 y_diff < 0 || y_diff > virt_fh) {
2121 const struct fb_videomode *mode;
2123 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2124 mode = fb_find_best_mode(&var, &info->modelist);
2125 if (mode == NULL)
2126 return -EINVAL;
2127 display_to_var(&var, p);
2128 fb_videomode_to_var(&var, mode);
2130 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2131 return -EINVAL;
2133 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2134 if (CON_IS_VISIBLE(vc)) {
2135 var.activate = FB_ACTIVATE_NOW |
2136 FB_ACTIVATE_FORCE;
2137 fb_set_var(info, &var);
2139 var_to_display(p, &info->var, info);
2140 ops->var = info->var;
2142 updatescrollmode(p, info, vc);
2143 return 0;
2146 static int fbcon_switch(struct vc_data *vc)
2148 struct fb_info *info, *old_info = NULL;
2149 struct fbcon_ops *ops;
2150 struct display *p = &fb_display[vc->vc_num];
2151 struct fb_var_screeninfo var;
2152 int i, ret, prev_console, charcnt = 256;
2154 info = registered_fb[con2fb_map[vc->vc_num]];
2155 ops = info->fbcon_par;
2157 if (softback_top) {
2158 if (softback_lines)
2159 fbcon_set_origin(vc);
2160 softback_top = softback_curr = softback_in = softback_buf;
2161 softback_lines = 0;
2162 fbcon_update_softback(vc);
2165 if (logo_shown >= 0) {
2166 struct vc_data *conp2 = vc_cons[logo_shown].d;
2168 if (conp2->vc_top == logo_lines
2169 && conp2->vc_bottom == conp2->vc_rows)
2170 conp2->vc_top = 0;
2171 logo_shown = FBCON_LOGO_CANSHOW;
2174 prev_console = ops->currcon;
2175 if (prev_console != -1)
2176 old_info = registered_fb[con2fb_map[prev_console]];
2178 * FIXME: If we have multiple fbdev's loaded, we need to
2179 * update all info->currcon. Perhaps, we can place this
2180 * in a centralized structure, but this might break some
2181 * drivers.
2183 * info->currcon = vc->vc_num;
2185 for (i = 0; i < FB_MAX; i++) {
2186 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
2187 struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2189 o->currcon = vc->vc_num;
2192 memset(&var, 0, sizeof(struct fb_var_screeninfo));
2193 display_to_var(&var, p);
2194 var.activate = FB_ACTIVATE_NOW;
2197 * make sure we don't unnecessarily trip the memcmp()
2198 * in fb_set_var()
2200 info->var.activate = var.activate;
2201 var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
2202 fb_set_var(info, &var);
2203 ops->var = info->var;
2205 if (old_info != NULL && (old_info != info ||
2206 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2207 if (info->fbops->fb_set_par) {
2208 ret = info->fbops->fb_set_par(info);
2210 if (ret)
2211 printk(KERN_ERR "fbcon_switch: detected "
2212 "unhandled fb_set_par error, "
2213 "error code %d\n", ret);
2216 if (old_info != info)
2217 fbcon_del_cursor_timer(old_info);
2220 if (fbcon_is_inactive(vc, info) ||
2221 ops->blank_state != FB_BLANK_UNBLANK)
2222 fbcon_del_cursor_timer(info);
2223 else
2224 fbcon_add_cursor_timer(info);
2226 set_blitting_type(vc, info);
2227 ops->cursor_reset = 1;
2229 if (ops->rotate_font && ops->rotate_font(info, vc)) {
2230 ops->rotate = FB_ROTATE_UR;
2231 set_blitting_type(vc, info);
2234 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2235 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2237 if (p->userfont)
2238 charcnt = FNTCHARCNT(vc->vc_font.data);
2240 if (charcnt > 256)
2241 vc->vc_complement_mask <<= 1;
2243 updatescrollmode(p, info, vc);
2245 switch (p->scrollmode) {
2246 case SCROLL_WRAP_MOVE:
2247 scrollback_phys_max = p->vrows - vc->vc_rows;
2248 break;
2249 case SCROLL_PAN_MOVE:
2250 case SCROLL_PAN_REDRAW:
2251 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2252 if (scrollback_phys_max < 0)
2253 scrollback_phys_max = 0;
2254 break;
2255 default:
2256 scrollback_phys_max = 0;
2257 break;
2260 scrollback_max = 0;
2261 scrollback_current = 0;
2263 if (!fbcon_is_inactive(vc, info)) {
2264 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2265 ops->update_start(info);
2268 fbcon_set_palette(vc, color_table);
2269 fbcon_clear_margins(vc, 0);
2271 if (logo_shown == FBCON_LOGO_DRAW) {
2273 logo_shown = fg_console;
2274 /* This is protected above by initmem_freed */
2275 fb_show_logo(info, ops->rotate);
2276 update_region(vc,
2277 vc->vc_origin + vc->vc_size_row * vc->vc_top,
2278 vc->vc_size_row * (vc->vc_bottom -
2279 vc->vc_top) / 2);
2280 return 0;
2282 return 1;
2285 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2286 int blank)
2288 struct fb_event event;
2290 if (blank) {
2291 unsigned short charmask = vc->vc_hi_font_mask ?
2292 0x1ff : 0xff;
2293 unsigned short oldc;
2295 oldc = vc->vc_video_erase_char;
2296 vc->vc_video_erase_char &= charmask;
2297 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2298 vc->vc_video_erase_char = oldc;
2302 if (!lock_fb_info(info))
2303 return;
2304 event.info = info;
2305 event.data = &blank;
2306 fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
2307 unlock_fb_info(info);
2310 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2312 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2313 struct fbcon_ops *ops = info->fbcon_par;
2315 if (mode_switch) {
2316 struct fb_var_screeninfo var = info->var;
2318 ops->graphics = 1;
2320 if (!blank) {
2321 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2322 fb_set_var(info, &var);
2323 ops->graphics = 0;
2324 ops->var = info->var;
2328 if (!fbcon_is_inactive(vc, info)) {
2329 if (ops->blank_state != blank) {
2330 ops->blank_state = blank;
2331 fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2332 ops->cursor_flash = (!blank);
2334 if (!(info->flags & FBINFO_MISC_USEREVENT))
2335 if (fb_blank(info, blank))
2336 fbcon_generic_blank(vc, info, blank);
2339 if (!blank)
2340 update_screen(vc);
2343 if (mode_switch || fbcon_is_inactive(vc, info) ||
2344 ops->blank_state != FB_BLANK_UNBLANK)
2345 fbcon_del_cursor_timer(info);
2346 else
2347 fbcon_add_cursor_timer(info);
2349 return 0;
2352 static int fbcon_debug_enter(struct vc_data *vc)
2354 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2355 struct fbcon_ops *ops = info->fbcon_par;
2357 ops->save_graphics = ops->graphics;
2358 ops->graphics = 0;
2359 if (info->fbops->fb_debug_enter)
2360 info->fbops->fb_debug_enter(info);
2361 fbcon_set_palette(vc, color_table);
2362 return 0;
2365 static int fbcon_debug_leave(struct vc_data *vc)
2367 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2368 struct fbcon_ops *ops = info->fbcon_par;
2370 ops->graphics = ops->save_graphics;
2371 if (info->fbops->fb_debug_leave)
2372 info->fbops->fb_debug_leave(info);
2373 return 0;
2376 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2378 u8 *fontdata = vc->vc_font.data;
2379 u8 *data = font->data;
2380 int i, j;
2382 font->width = vc->vc_font.width;
2383 font->height = vc->vc_font.height;
2384 font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2385 if (!font->data)
2386 return 0;
2388 if (font->width <= 8) {
2389 j = vc->vc_font.height;
2390 for (i = 0; i < font->charcount; i++) {
2391 memcpy(data, fontdata, j);
2392 memset(data + j, 0, 32 - j);
2393 data += 32;
2394 fontdata += j;
2396 } else if (font->width <= 16) {
2397 j = vc->vc_font.height * 2;
2398 for (i = 0; i < font->charcount; i++) {
2399 memcpy(data, fontdata, j);
2400 memset(data + j, 0, 64 - j);
2401 data += 64;
2402 fontdata += j;
2404 } else if (font->width <= 24) {
2405 for (i = 0; i < font->charcount; i++) {
2406 for (j = 0; j < vc->vc_font.height; j++) {
2407 *data++ = fontdata[0];
2408 *data++ = fontdata[1];
2409 *data++ = fontdata[2];
2410 fontdata += sizeof(u32);
2412 memset(data, 0, 3 * (32 - j));
2413 data += 3 * (32 - j);
2415 } else {
2416 j = vc->vc_font.height * 4;
2417 for (i = 0; i < font->charcount; i++) {
2418 memcpy(data, fontdata, j);
2419 memset(data + j, 0, 128 - j);
2420 data += 128;
2421 fontdata += j;
2424 return 0;
2427 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2428 const u8 * data, int userfont)
2430 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2431 struct fbcon_ops *ops = info->fbcon_par;
2432 struct display *p = &fb_display[vc->vc_num];
2433 int resize;
2434 int cnt;
2435 char *old_data = NULL;
2437 if (CON_IS_VISIBLE(vc) && softback_lines)
2438 fbcon_set_origin(vc);
2440 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2441 if (p->userfont)
2442 old_data = vc->vc_font.data;
2443 if (userfont)
2444 cnt = FNTCHARCNT(data);
2445 else
2446 cnt = 256;
2447 vc->vc_font.data = (void *)(p->fontdata = data);
2448 if ((p->userfont = userfont))
2449 REFCOUNT(data)++;
2450 vc->vc_font.width = w;
2451 vc->vc_font.height = h;
2452 if (vc->vc_hi_font_mask && cnt == 256) {
2453 vc->vc_hi_font_mask = 0;
2454 if (vc->vc_can_do_color) {
2455 vc->vc_complement_mask >>= 1;
2456 vc->vc_s_complement_mask >>= 1;
2459 /* ++Edmund: reorder the attribute bits */
2460 if (vc->vc_can_do_color) {
2461 unsigned short *cp =
2462 (unsigned short *) vc->vc_origin;
2463 int count = vc->vc_screenbuf_size / 2;
2464 unsigned short c;
2465 for (; count > 0; count--, cp++) {
2466 c = scr_readw(cp);
2467 scr_writew(((c & 0xfe00) >> 1) |
2468 (c & 0xff), cp);
2470 c = vc->vc_video_erase_char;
2471 vc->vc_video_erase_char =
2472 ((c & 0xfe00) >> 1) | (c & 0xff);
2473 vc->vc_attr >>= 1;
2475 } else if (!vc->vc_hi_font_mask && cnt == 512) {
2476 vc->vc_hi_font_mask = 0x100;
2477 if (vc->vc_can_do_color) {
2478 vc->vc_complement_mask <<= 1;
2479 vc->vc_s_complement_mask <<= 1;
2482 /* ++Edmund: reorder the attribute bits */
2484 unsigned short *cp =
2485 (unsigned short *) vc->vc_origin;
2486 int count = vc->vc_screenbuf_size / 2;
2487 unsigned short c;
2488 for (; count > 0; count--, cp++) {
2489 unsigned short newc;
2490 c = scr_readw(cp);
2491 if (vc->vc_can_do_color)
2492 newc =
2493 ((c & 0xff00) << 1) | (c &
2494 0xff);
2495 else
2496 newc = c & ~0x100;
2497 scr_writew(newc, cp);
2499 c = vc->vc_video_erase_char;
2500 if (vc->vc_can_do_color) {
2501 vc->vc_video_erase_char =
2502 ((c & 0xff00) << 1) | (c & 0xff);
2503 vc->vc_attr <<= 1;
2504 } else
2505 vc->vc_video_erase_char = c & ~0x100;
2510 if (resize) {
2511 int cols, rows;
2513 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2514 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2515 cols /= w;
2516 rows /= h;
2517 vc_resize(vc, cols, rows);
2518 if (CON_IS_VISIBLE(vc) && softback_buf)
2519 fbcon_update_softback(vc);
2520 } else if (CON_IS_VISIBLE(vc)
2521 && vc->vc_mode == KD_TEXT) {
2522 fbcon_clear_margins(vc, 0);
2523 update_screen(vc);
2526 if (old_data && (--REFCOUNT(old_data) == 0))
2527 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2528 return 0;
2531 static int fbcon_copy_font(struct vc_data *vc, int con)
2533 struct display *od = &fb_display[con];
2534 struct console_font *f = &vc->vc_font;
2536 if (od->fontdata == f->data)
2537 return 0; /* already the same font... */
2538 return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2542 * User asked to set font; we are guaranteed that
2543 * a) width and height are in range 1..32
2544 * b) charcount does not exceed 512
2545 * but lets not assume that, since someone might someday want to use larger
2546 * fonts. And charcount of 512 is small for unicode support.
2548 * However, user space gives the font in 32 rows , regardless of
2549 * actual font height. So a new API is needed if support for larger fonts
2550 * is ever implemented.
2553 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2555 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2556 unsigned charcount = font->charcount;
2557 int w = font->width;
2558 int h = font->height;
2559 int size;
2560 int i, csum;
2561 u8 *new_data, *data = font->data;
2562 int pitch = (font->width+7) >> 3;
2564 /* Is there a reason why fbconsole couldn't handle any charcount >256?
2565 * If not this check should be changed to charcount < 256 */
2566 if (charcount != 256 && charcount != 512)
2567 return -EINVAL;
2569 /* Make sure drawing engine can handle the font */
2570 if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2571 !(info->pixmap.blit_y & (1 << (font->height - 1))))
2572 return -EINVAL;
2574 /* Make sure driver can handle the font length */
2575 if (fbcon_invalid_charcount(info, charcount))
2576 return -EINVAL;
2578 size = h * pitch * charcount;
2580 new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2582 if (!new_data)
2583 return -ENOMEM;
2585 new_data += FONT_EXTRA_WORDS * sizeof(int);
2586 FNTSIZE(new_data) = size;
2587 FNTCHARCNT(new_data) = charcount;
2588 REFCOUNT(new_data) = 0; /* usage counter */
2589 for (i=0; i< charcount; i++) {
2590 memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
2593 /* Since linux has a nice crc32 function use it for counting font
2594 * checksums. */
2595 csum = crc32(0, new_data, size);
2597 FNTSUM(new_data) = csum;
2598 /* Check if the same font is on some other console already */
2599 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2600 struct vc_data *tmp = vc_cons[i].d;
2602 if (fb_display[i].userfont &&
2603 fb_display[i].fontdata &&
2604 FNTSUM(fb_display[i].fontdata) == csum &&
2605 FNTSIZE(fb_display[i].fontdata) == size &&
2606 tmp->vc_font.width == w &&
2607 !memcmp(fb_display[i].fontdata, new_data, size)) {
2608 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2609 new_data = (u8 *)fb_display[i].fontdata;
2610 break;
2613 return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2616 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2618 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2619 const struct font_desc *f;
2621 if (!name)
2622 f = get_default_font(info->var.xres, info->var.yres,
2623 info->pixmap.blit_x, info->pixmap.blit_y);
2624 else if (!(f = find_font(name)))
2625 return -ENOENT;
2627 font->width = f->width;
2628 font->height = f->height;
2629 return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2632 static u16 palette_red[16];
2633 static u16 palette_green[16];
2634 static u16 palette_blue[16];
2636 static struct fb_cmap palette_cmap = {
2637 0, 16, palette_red, palette_green, palette_blue, NULL
2640 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2642 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2643 int i, j, k, depth;
2644 u8 val;
2646 if (fbcon_is_inactive(vc, info))
2647 return -EINVAL;
2649 if (!CON_IS_VISIBLE(vc))
2650 return 0;
2652 depth = fb_get_color_depth(&info->var, &info->fix);
2653 if (depth > 3) {
2654 for (i = j = 0; i < 16; i++) {
2655 k = table[i];
2656 val = vc->vc_palette[j++];
2657 palette_red[k] = (val << 8) | val;
2658 val = vc->vc_palette[j++];
2659 palette_green[k] = (val << 8) | val;
2660 val = vc->vc_palette[j++];
2661 palette_blue[k] = (val << 8) | val;
2663 palette_cmap.len = 16;
2664 palette_cmap.start = 0;
2666 * If framebuffer is capable of less than 16 colors,
2667 * use default palette of fbcon.
2669 } else
2670 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2672 return fb_set_cmap(&palette_cmap, info);
2675 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2677 unsigned long p;
2678 int line;
2680 if (vc->vc_num != fg_console || !softback_lines)
2681 return (u16 *) (vc->vc_origin + offset);
2682 line = offset / vc->vc_size_row;
2683 if (line >= softback_lines)
2684 return (u16 *) (vc->vc_origin + offset -
2685 softback_lines * vc->vc_size_row);
2686 p = softback_curr + offset;
2687 if (p >= softback_end)
2688 p += softback_buf - softback_end;
2689 return (u16 *) p;
2692 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2693 int *px, int *py)
2695 unsigned long ret;
2696 int x, y;
2698 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2699 unsigned long offset = (pos - vc->vc_origin) / 2;
2701 x = offset % vc->vc_cols;
2702 y = offset / vc->vc_cols;
2703 if (vc->vc_num == fg_console)
2704 y += softback_lines;
2705 ret = pos + (vc->vc_cols - x) * 2;
2706 } else if (vc->vc_num == fg_console && softback_lines) {
2707 unsigned long offset = pos - softback_curr;
2709 if (pos < softback_curr)
2710 offset += softback_end - softback_buf;
2711 offset /= 2;
2712 x = offset % vc->vc_cols;
2713 y = offset / vc->vc_cols;
2714 ret = pos + (vc->vc_cols - x) * 2;
2715 if (ret == softback_end)
2716 ret = softback_buf;
2717 if (ret == softback_in)
2718 ret = vc->vc_origin;
2719 } else {
2720 /* Should not happen */
2721 x = y = 0;
2722 ret = vc->vc_origin;
2724 if (px)
2725 *px = x;
2726 if (py)
2727 *py = y;
2728 return ret;
2731 /* As we might be inside of softback, we may work with non-contiguous buffer,
2732 that's why we have to use a separate routine. */
2733 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2735 while (cnt--) {
2736 u16 a = scr_readw(p);
2737 if (!vc->vc_can_do_color)
2738 a ^= 0x0800;
2739 else if (vc->vc_hi_font_mask == 0x100)
2740 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2741 (((a) & 0x0e00) << 4);
2742 else
2743 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2744 (((a) & 0x0700) << 4);
2745 scr_writew(a, p++);
2746 if (p == (u16 *) softback_end)
2747 p = (u16 *) softback_buf;
2748 if (p == (u16 *) softback_in)
2749 p = (u16 *) vc->vc_origin;
2753 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2755 struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2756 struct fbcon_ops *ops = info->fbcon_par;
2757 struct display *disp = &fb_display[fg_console];
2758 int offset, limit, scrollback_old;
2760 if (softback_top) {
2761 if (vc->vc_num != fg_console)
2762 return 0;
2763 if (vc->vc_mode != KD_TEXT || !lines)
2764 return 0;
2765 if (logo_shown >= 0) {
2766 struct vc_data *conp2 = vc_cons[logo_shown].d;
2768 if (conp2->vc_top == logo_lines
2769 && conp2->vc_bottom == conp2->vc_rows)
2770 conp2->vc_top = 0;
2771 if (logo_shown == vc->vc_num) {
2772 unsigned long p, q;
2773 int i;
2775 p = softback_in;
2776 q = vc->vc_origin +
2777 logo_lines * vc->vc_size_row;
2778 for (i = 0; i < logo_lines; i++) {
2779 if (p == softback_top)
2780 break;
2781 if (p == softback_buf)
2782 p = softback_end;
2783 p -= vc->vc_size_row;
2784 q -= vc->vc_size_row;
2785 scr_memcpyw((u16 *) q, (u16 *) p,
2786 vc->vc_size_row);
2788 softback_in = softback_curr = p;
2789 update_region(vc, vc->vc_origin,
2790 logo_lines * vc->vc_cols);
2792 logo_shown = FBCON_LOGO_CANSHOW;
2794 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2795 fbcon_redraw_softback(vc, disp, lines);
2796 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2797 return 0;
2800 if (!scrollback_phys_max)
2801 return -ENOSYS;
2803 scrollback_old = scrollback_current;
2804 scrollback_current -= lines;
2805 if (scrollback_current < 0)
2806 scrollback_current = 0;
2807 else if (scrollback_current > scrollback_max)
2808 scrollback_current = scrollback_max;
2809 if (scrollback_current == scrollback_old)
2810 return 0;
2812 if (fbcon_is_inactive(vc, info))
2813 return 0;
2815 fbcon_cursor(vc, CM_ERASE);
2817 offset = disp->yscroll - scrollback_current;
2818 limit = disp->vrows;
2819 switch (disp->scrollmode) {
2820 case SCROLL_WRAP_MOVE:
2821 info->var.vmode |= FB_VMODE_YWRAP;
2822 break;
2823 case SCROLL_PAN_MOVE:
2824 case SCROLL_PAN_REDRAW:
2825 limit -= vc->vc_rows;
2826 info->var.vmode &= ~FB_VMODE_YWRAP;
2827 break;
2829 if (offset < 0)
2830 offset += limit;
2831 else if (offset >= limit)
2832 offset -= limit;
2834 ops->var.xoffset = 0;
2835 ops->var.yoffset = offset * vc->vc_font.height;
2836 ops->update_start(info);
2838 if (!scrollback_current)
2839 fbcon_cursor(vc, CM_DRAW);
2840 return 0;
2843 static int fbcon_set_origin(struct vc_data *vc)
2845 if (softback_lines)
2846 fbcon_scrolldelta(vc, softback_lines);
2847 return 0;
2850 static void fbcon_suspended(struct fb_info *info)
2852 struct vc_data *vc = NULL;
2853 struct fbcon_ops *ops = info->fbcon_par;
2855 if (!ops || ops->currcon < 0)
2856 return;
2857 vc = vc_cons[ops->currcon].d;
2859 /* Clear cursor, restore saved data */
2860 fbcon_cursor(vc, CM_ERASE);
2863 static void fbcon_resumed(struct fb_info *info)
2865 struct vc_data *vc;
2866 struct fbcon_ops *ops = info->fbcon_par;
2868 if (!ops || ops->currcon < 0)
2869 return;
2870 vc = vc_cons[ops->currcon].d;
2872 update_screen(vc);
2875 static void fbcon_modechanged(struct fb_info *info)
2877 struct fbcon_ops *ops = info->fbcon_par;
2878 struct vc_data *vc;
2879 struct display *p;
2880 int rows, cols;
2882 if (!ops || ops->currcon < 0)
2883 return;
2884 vc = vc_cons[ops->currcon].d;
2885 if (vc->vc_mode != KD_TEXT ||
2886 registered_fb[con2fb_map[ops->currcon]] != info)
2887 return;
2889 p = &fb_display[vc->vc_num];
2890 set_blitting_type(vc, info);
2892 if (CON_IS_VISIBLE(vc)) {
2893 var_to_display(p, &info->var, info);
2894 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2895 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2896 cols /= vc->vc_font.width;
2897 rows /= vc->vc_font.height;
2898 vc_resize(vc, cols, rows);
2899 updatescrollmode(p, info, vc);
2900 scrollback_max = 0;
2901 scrollback_current = 0;
2903 if (!fbcon_is_inactive(vc, info)) {
2904 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2905 ops->update_start(info);
2908 fbcon_set_palette(vc, color_table);
2909 update_screen(vc);
2910 if (softback_buf)
2911 fbcon_update_softback(vc);
2915 static void fbcon_set_all_vcs(struct fb_info *info)
2917 struct fbcon_ops *ops = info->fbcon_par;
2918 struct vc_data *vc;
2919 struct display *p;
2920 int i, rows, cols, fg = -1;
2922 if (!ops || ops->currcon < 0)
2923 return;
2925 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2926 vc = vc_cons[i].d;
2927 if (!vc || vc->vc_mode != KD_TEXT ||
2928 registered_fb[con2fb_map[i]] != info)
2929 continue;
2931 if (CON_IS_VISIBLE(vc)) {
2932 fg = i;
2933 continue;
2936 p = &fb_display[vc->vc_num];
2937 set_blitting_type(vc, info);
2938 var_to_display(p, &info->var, info);
2939 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2940 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2941 cols /= vc->vc_font.width;
2942 rows /= vc->vc_font.height;
2943 vc_resize(vc, cols, rows);
2946 if (fg != -1)
2947 fbcon_modechanged(info);
2950 static int fbcon_mode_deleted(struct fb_info *info,
2951 struct fb_videomode *mode)
2953 struct fb_info *fb_info;
2954 struct display *p;
2955 int i, j, found = 0;
2957 /* before deletion, ensure that mode is not in use */
2958 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2959 j = con2fb_map[i];
2960 if (j == -1)
2961 continue;
2962 fb_info = registered_fb[j];
2963 if (fb_info != info)
2964 continue;
2965 p = &fb_display[i];
2966 if (!p || !p->mode)
2967 continue;
2968 if (fb_mode_is_equal(p->mode, mode)) {
2969 found = 1;
2970 break;
2973 return found;
2976 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
2977 static int fbcon_unbind(void)
2979 int ret;
2981 ret = unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
2982 fbcon_is_default);
2984 if (!ret)
2985 fbcon_has_console_bind = 0;
2987 return ret;
2989 #else
2990 static inline int fbcon_unbind(void)
2992 return -EINVAL;
2994 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
2996 static int fbcon_fb_unbind(int idx)
2998 int i, new_idx = -1, ret = 0;
3000 if (!fbcon_has_console_bind)
3001 return 0;
3003 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3004 if (con2fb_map[i] != idx &&
3005 con2fb_map[i] != -1) {
3006 new_idx = i;
3007 break;
3011 if (new_idx != -1) {
3012 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3013 if (con2fb_map[i] == idx)
3014 set_con2fb_map(i, new_idx, 0);
3016 } else
3017 ret = fbcon_unbind();
3019 return ret;
3022 static int fbcon_fb_unregistered(struct fb_info *info)
3024 int i, idx;
3026 idx = info->node;
3027 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3028 if (con2fb_map[i] == idx)
3029 con2fb_map[i] = -1;
3032 if (idx == info_idx) {
3033 info_idx = -1;
3035 for (i = 0; i < FB_MAX; i++) {
3036 if (registered_fb[i] != NULL) {
3037 info_idx = i;
3038 break;
3043 if (info_idx != -1) {
3044 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3045 if (con2fb_map[i] == -1)
3046 con2fb_map[i] = info_idx;
3050 if (primary_device == idx)
3051 primary_device = -1;
3053 if (!num_registered_fb)
3054 unregister_con_driver(&fb_con);
3056 return 0;
3059 static void fbcon_remap_all(int idx)
3061 int i;
3062 for (i = first_fb_vc; i <= last_fb_vc; i++)
3063 set_con2fb_map(i, idx, 0);
3065 if (con_is_bound(&fb_con)) {
3066 printk(KERN_INFO "fbcon: Remapping primary device, "
3067 "fb%i, to tty %i-%i\n", idx,
3068 first_fb_vc + 1, last_fb_vc + 1);
3069 info_idx = idx;
3073 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3074 static void fbcon_select_primary(struct fb_info *info)
3076 if (!map_override && primary_device == -1 &&
3077 fb_is_primary_device(info)) {
3078 int i;
3080 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3081 info->fix.id, info->node);
3082 primary_device = info->node;
3084 for (i = first_fb_vc; i <= last_fb_vc; i++)
3085 con2fb_map_boot[i] = primary_device;
3087 if (con_is_bound(&fb_con)) {
3088 printk(KERN_INFO "fbcon: Remapping primary device, "
3089 "fb%i, to tty %i-%i\n", info->node,
3090 first_fb_vc + 1, last_fb_vc + 1);
3091 info_idx = primary_device;
3096 #else
3097 static inline void fbcon_select_primary(struct fb_info *info)
3099 return;
3101 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3103 static int fbcon_fb_registered(struct fb_info *info)
3105 int ret = 0, i, idx;
3107 idx = info->node;
3108 fbcon_select_primary(info);
3110 if (info_idx == -1) {
3111 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3112 if (con2fb_map_boot[i] == idx) {
3113 info_idx = idx;
3114 break;
3118 if (info_idx != -1)
3119 ret = fbcon_takeover(1);
3120 } else {
3121 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3122 if (con2fb_map_boot[i] == idx)
3123 set_con2fb_map(i, idx, 0);
3127 return ret;
3130 static void fbcon_fb_blanked(struct fb_info *info, int blank)
3132 struct fbcon_ops *ops = info->fbcon_par;
3133 struct vc_data *vc;
3135 if (!ops || ops->currcon < 0)
3136 return;
3138 vc = vc_cons[ops->currcon].d;
3139 if (vc->vc_mode != KD_TEXT ||
3140 registered_fb[con2fb_map[ops->currcon]] != info)
3141 return;
3143 if (CON_IS_VISIBLE(vc)) {
3144 if (blank)
3145 do_blank_screen(0);
3146 else
3147 do_unblank_screen(0);
3149 ops->blank_state = blank;
3152 static void fbcon_new_modelist(struct fb_info *info)
3154 int i;
3155 struct vc_data *vc;
3156 struct fb_var_screeninfo var;
3157 const struct fb_videomode *mode;
3159 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3160 if (registered_fb[con2fb_map[i]] != info)
3161 continue;
3162 if (!fb_display[i].mode)
3163 continue;
3164 vc = vc_cons[i].d;
3165 display_to_var(&var, &fb_display[i]);
3166 mode = fb_find_nearest_mode(fb_display[i].mode,
3167 &info->modelist);
3168 fb_videomode_to_var(&var, mode);
3169 fbcon_set_disp(info, &var, vc->vc_num);
3173 static void fbcon_get_requirement(struct fb_info *info,
3174 struct fb_blit_caps *caps)
3176 struct vc_data *vc;
3177 struct display *p;
3179 if (caps->flags) {
3180 int i, charcnt;
3182 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3183 vc = vc_cons[i].d;
3184 if (vc && vc->vc_mode == KD_TEXT &&
3185 info->node == con2fb_map[i]) {
3186 p = &fb_display[i];
3187 caps->x |= 1 << (vc->vc_font.width - 1);
3188 caps->y |= 1 << (vc->vc_font.height - 1);
3189 charcnt = (p->userfont) ?
3190 FNTCHARCNT(p->fontdata) : 256;
3191 if (caps->len < charcnt)
3192 caps->len = charcnt;
3195 } else {
3196 vc = vc_cons[fg_console].d;
3198 if (vc && vc->vc_mode == KD_TEXT &&
3199 info->node == con2fb_map[fg_console]) {
3200 p = &fb_display[fg_console];
3201 caps->x = 1 << (vc->vc_font.width - 1);
3202 caps->y = 1 << (vc->vc_font.height - 1);
3203 caps->len = (p->userfont) ?
3204 FNTCHARCNT(p->fontdata) : 256;
3209 static int fbcon_event_notify(struct notifier_block *self,
3210 unsigned long action, void *data)
3212 struct fb_event *event = data;
3213 struct fb_info *info = event->info;
3214 struct fb_videomode *mode;
3215 struct fb_con2fbmap *con2fb;
3216 struct fb_blit_caps *caps;
3217 int idx, ret = 0;
3220 * ignore all events except driver registration and deregistration
3221 * if fbcon is not active
3223 if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3224 action == FB_EVENT_FB_UNREGISTERED))
3225 goto done;
3227 switch(action) {
3228 case FB_EVENT_SUSPEND:
3229 fbcon_suspended(info);
3230 break;
3231 case FB_EVENT_RESUME:
3232 fbcon_resumed(info);
3233 break;
3234 case FB_EVENT_MODE_CHANGE:
3235 fbcon_modechanged(info);
3236 break;
3237 case FB_EVENT_MODE_CHANGE_ALL:
3238 fbcon_set_all_vcs(info);
3239 break;
3240 case FB_EVENT_MODE_DELETE:
3241 mode = event->data;
3242 ret = fbcon_mode_deleted(info, mode);
3243 break;
3244 case FB_EVENT_FB_UNBIND:
3245 idx = info->node;
3246 ret = fbcon_fb_unbind(idx);
3247 break;
3248 case FB_EVENT_FB_REGISTERED:
3249 ret = fbcon_fb_registered(info);
3250 break;
3251 case FB_EVENT_FB_UNREGISTERED:
3252 ret = fbcon_fb_unregistered(info);
3253 break;
3254 case FB_EVENT_SET_CONSOLE_MAP:
3255 con2fb = event->data;
3256 ret = set_con2fb_map(con2fb->console - 1,
3257 con2fb->framebuffer, 1);
3258 break;
3259 case FB_EVENT_GET_CONSOLE_MAP:
3260 con2fb = event->data;
3261 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3262 break;
3263 case FB_EVENT_BLANK:
3264 fbcon_fb_blanked(info, *(int *)event->data);
3265 break;
3266 case FB_EVENT_NEW_MODELIST:
3267 fbcon_new_modelist(info);
3268 break;
3269 case FB_EVENT_GET_REQ:
3270 caps = event->data;
3271 fbcon_get_requirement(info, caps);
3272 break;
3273 case FB_EVENT_REMAP_ALL_CONSOLE:
3274 idx = info->node;
3275 fbcon_remap_all(idx);
3276 break;
3278 done:
3279 return ret;
3283 * The console `switch' structure for the frame buffer based console
3286 static const struct consw fb_con = {
3287 .owner = THIS_MODULE,
3288 .con_startup = fbcon_startup,
3289 .con_init = fbcon_init,
3290 .con_deinit = fbcon_deinit,
3291 .con_clear = fbcon_clear,
3292 .con_putc = fbcon_putc,
3293 .con_putcs = fbcon_putcs,
3294 .con_cursor = fbcon_cursor,
3295 .con_scroll = fbcon_scroll,
3296 .con_bmove = fbcon_bmove,
3297 .con_switch = fbcon_switch,
3298 .con_blank = fbcon_blank,
3299 .con_font_set = fbcon_set_font,
3300 .con_font_get = fbcon_get_font,
3301 .con_font_default = fbcon_set_def_font,
3302 .con_font_copy = fbcon_copy_font,
3303 .con_set_palette = fbcon_set_palette,
3304 .con_scrolldelta = fbcon_scrolldelta,
3305 .con_set_origin = fbcon_set_origin,
3306 .con_invert_region = fbcon_invert_region,
3307 .con_screen_pos = fbcon_screen_pos,
3308 .con_getxy = fbcon_getxy,
3309 .con_resize = fbcon_resize,
3310 .con_debug_enter = fbcon_debug_enter,
3311 .con_debug_leave = fbcon_debug_leave,
3314 static struct notifier_block fbcon_event_notifier = {
3315 .notifier_call = fbcon_event_notify,
3318 static ssize_t store_rotate(struct device *device,
3319 struct device_attribute *attr, const char *buf,
3320 size_t count)
3322 struct fb_info *info;
3323 int rotate, idx;
3324 char **last = NULL;
3326 if (fbcon_has_exited)
3327 return count;
3329 console_lock();
3330 idx = con2fb_map[fg_console];
3332 if (idx == -1 || registered_fb[idx] == NULL)
3333 goto err;
3335 info = registered_fb[idx];
3336 rotate = simple_strtoul(buf, last, 0);
3337 fbcon_rotate(info, rotate);
3338 err:
3339 console_unlock();
3340 return count;
3343 static ssize_t store_rotate_all(struct device *device,
3344 struct device_attribute *attr,const char *buf,
3345 size_t count)
3347 struct fb_info *info;
3348 int rotate, idx;
3349 char **last = NULL;
3351 if (fbcon_has_exited)
3352 return count;
3354 console_lock();
3355 idx = con2fb_map[fg_console];
3357 if (idx == -1 || registered_fb[idx] == NULL)
3358 goto err;
3360 info = registered_fb[idx];
3361 rotate = simple_strtoul(buf, last, 0);
3362 fbcon_rotate_all(info, rotate);
3363 err:
3364 console_unlock();
3365 return count;
3368 static ssize_t show_rotate(struct device *device,
3369 struct device_attribute *attr,char *buf)
3371 struct fb_info *info;
3372 int rotate = 0, idx;
3374 if (fbcon_has_exited)
3375 return 0;
3377 console_lock();
3378 idx = con2fb_map[fg_console];
3380 if (idx == -1 || registered_fb[idx] == NULL)
3381 goto err;
3383 info = registered_fb[idx];
3384 rotate = fbcon_get_rotate(info);
3385 err:
3386 console_unlock();
3387 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3390 static ssize_t show_cursor_blink(struct device *device,
3391 struct device_attribute *attr, char *buf)
3393 struct fb_info *info;
3394 struct fbcon_ops *ops;
3395 int idx, blink = -1;
3397 if (fbcon_has_exited)
3398 return 0;
3400 console_lock();
3401 idx = con2fb_map[fg_console];
3403 if (idx == -1 || registered_fb[idx] == NULL)
3404 goto err;
3406 info = registered_fb[idx];
3407 ops = info->fbcon_par;
3409 if (!ops)
3410 goto err;
3412 blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3413 err:
3414 console_unlock();
3415 return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3418 static ssize_t store_cursor_blink(struct device *device,
3419 struct device_attribute *attr,
3420 const char *buf, size_t count)
3422 struct fb_info *info;
3423 int blink, idx;
3424 char **last = NULL;
3426 if (fbcon_has_exited)
3427 return count;
3429 console_lock();
3430 idx = con2fb_map[fg_console];
3432 if (idx == -1 || registered_fb[idx] == NULL)
3433 goto err;
3435 info = registered_fb[idx];
3437 if (!info->fbcon_par)
3438 goto err;
3440 blink = simple_strtoul(buf, last, 0);
3442 if (blink) {
3443 fbcon_cursor_noblink = 0;
3444 fbcon_add_cursor_timer(info);
3445 } else {
3446 fbcon_cursor_noblink = 1;
3447 fbcon_del_cursor_timer(info);
3450 err:
3451 console_unlock();
3452 return count;
3455 static struct device_attribute device_attrs[] = {
3456 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3457 __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3458 __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3459 store_cursor_blink),
3462 static int fbcon_init_device(void)
3464 int i, error = 0;
3466 fbcon_has_sysfs = 1;
3468 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3469 error = device_create_file(fbcon_device, &device_attrs[i]);
3471 if (error)
3472 break;
3475 if (error) {
3476 while (--i >= 0)
3477 device_remove_file(fbcon_device, &device_attrs[i]);
3479 fbcon_has_sysfs = 0;
3482 return 0;
3485 static void fbcon_start(void)
3487 if (num_registered_fb) {
3488 int i;
3490 console_lock();
3492 for (i = 0; i < FB_MAX; i++) {
3493 if (registered_fb[i] != NULL) {
3494 info_idx = i;
3495 break;
3499 console_unlock();
3500 fbcon_takeover(0);
3504 static void fbcon_exit(void)
3506 struct fb_info *info;
3507 int i, j, mapped;
3509 if (fbcon_has_exited)
3510 return;
3512 kfree((void *)softback_buf);
3513 softback_buf = 0UL;
3515 for (i = 0; i < FB_MAX; i++) {
3516 int pending = 0;
3518 mapped = 0;
3519 info = registered_fb[i];
3521 if (info == NULL)
3522 continue;
3524 if (info->queue.func)
3525 pending = cancel_work_sync(&info->queue);
3526 DPRINTK("fbcon: %s pending work\n", (pending ? "canceled" :
3527 "no"));
3529 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3530 if (con2fb_map[j] == i)
3531 mapped = 1;
3534 if (mapped) {
3535 if (info->fbops->fb_release)
3536 info->fbops->fb_release(info, 0);
3537 module_put(info->fbops->owner);
3539 if (info->fbcon_par) {
3540 struct fbcon_ops *ops = info->fbcon_par;
3542 fbcon_del_cursor_timer(info);
3543 kfree(ops->cursor_src);
3544 kfree(info->fbcon_par);
3545 info->fbcon_par = NULL;
3548 if (info->queue.func == fb_flashcursor)
3549 info->queue.func = NULL;
3553 fbcon_has_exited = 1;
3556 static int __init fb_console_init(void)
3558 int i;
3560 console_lock();
3561 fb_register_client(&fbcon_event_notifier);
3562 fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3563 "fbcon");
3565 if (IS_ERR(fbcon_device)) {
3566 printk(KERN_WARNING "Unable to create device "
3567 "for fbcon; errno = %ld\n",
3568 PTR_ERR(fbcon_device));
3569 fbcon_device = NULL;
3570 } else
3571 fbcon_init_device();
3573 for (i = 0; i < MAX_NR_CONSOLES; i++)
3574 con2fb_map[i] = -1;
3576 console_unlock();
3577 fbcon_start();
3578 return 0;
3581 module_init(fb_console_init);
3583 #ifdef MODULE
3585 static void __exit fbcon_deinit_device(void)
3587 int i;
3589 if (fbcon_has_sysfs) {
3590 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3591 device_remove_file(fbcon_device, &device_attrs[i]);
3593 fbcon_has_sysfs = 0;
3597 static void __exit fb_console_exit(void)
3599 console_lock();
3600 fb_unregister_client(&fbcon_event_notifier);
3601 fbcon_deinit_device();
3602 device_destroy(fb_class, MKDEV(0, 0));
3603 fbcon_exit();
3604 console_unlock();
3605 unregister_con_driver(&fb_con);
3608 module_exit(fb_console_exit);
3610 #endif
3612 MODULE_LICENSE("GPL");