fbcon: set_con2fb_map fixes
[firewire-audio.git] / drivers / video / console / fbcon.c
blob13b67ee8b3c0eb1320c90aa585ac71e3d7bdfee5
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>
81 #include <asm/uaccess.h>
82 #ifdef CONFIG_ATARI
83 #include <asm/atariints.h>
84 #endif
85 #ifdef CONFIG_MAC
86 #include <asm/macints.h>
87 #endif
88 #if defined(__mc68000__) || defined(CONFIG_APUS)
89 #include <asm/machdep.h>
90 #include <asm/setup.h>
91 #endif
93 #include "fbcon.h"
95 #ifdef FBCONDEBUG
96 # define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
97 #else
98 # define DPRINTK(fmt, args...)
99 #endif
101 enum {
102 FBCON_LOGO_CANSHOW = -1, /* the logo can be shown */
103 FBCON_LOGO_DRAW = -2, /* draw the logo to a console */
104 FBCON_LOGO_DONTSHOW = -3 /* do not show the logo */
107 static struct display fb_display[MAX_NR_CONSOLES];
109 static signed char con2fb_map[MAX_NR_CONSOLES];
110 static signed char con2fb_map_boot[MAX_NR_CONSOLES];
111 #ifndef MODULE
112 static int logo_height;
113 #endif
114 static int logo_lines;
115 /* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO
116 enums. */
117 static int logo_shown = FBCON_LOGO_CANSHOW;
118 /* Software scrollback */
119 static int fbcon_softback_size = 32768;
120 static unsigned long softback_buf, softback_curr;
121 static unsigned long softback_in;
122 static unsigned long softback_top, softback_end;
123 static int softback_lines;
124 /* console mappings */
125 static int first_fb_vc;
126 static int last_fb_vc = MAX_NR_CONSOLES - 1;
127 static int fbcon_is_default = 1;
128 static int fbcon_has_exited;
129 static int primary_device = -1;
130 static int map_override;
132 /* font data */
133 static char fontname[40];
135 /* current fb_info */
136 static int info_idx = -1;
138 /* console rotation */
139 static int rotate;
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 /* # VBL ints between cursor state changes */
153 #define ATARI_CURSOR_BLINK_RATE (42)
154 #define MAC_CURSOR_BLINK_RATE (32)
155 #define DEFAULT_CURSOR_BLINK_RATE (20)
157 static int vbl_cursor_cnt;
158 static int fbcon_cursor_noblink;
160 #define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1)
163 * Interface used by the world
166 static const char *fbcon_startup(void);
167 static void fbcon_init(struct vc_data *vc, int init);
168 static void fbcon_deinit(struct vc_data *vc);
169 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
170 int width);
171 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
172 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
173 int count, int ypos, int xpos);
174 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
175 static void fbcon_cursor(struct vc_data *vc, int mode);
176 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
177 int count);
178 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
179 int height, int width);
180 static int fbcon_switch(struct vc_data *vc);
181 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
182 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
183 static int fbcon_scrolldelta(struct vc_data *vc, int lines);
186 * Internal routines
188 static __inline__ void ywrap_up(struct vc_data *vc, int count);
189 static __inline__ void ywrap_down(struct vc_data *vc, int count);
190 static __inline__ void ypan_up(struct vc_data *vc, int count);
191 static __inline__ void ypan_down(struct vc_data *vc, int count);
192 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
193 int dy, int dx, int height, int width, u_int y_break);
194 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
195 int unit);
196 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
197 int line, int count, int dy);
198 static void fbcon_modechanged(struct fb_info *info);
199 static void fbcon_set_all_vcs(struct fb_info *info);
200 static void fbcon_start(void);
201 static void fbcon_exit(void);
202 static struct device *fbcon_device;
204 #ifdef CONFIG_MAC
206 * On the Macintoy, there may or may not be a working VBL int. We need to probe
208 static int vbl_detected;
210 static irqreturn_t fb_vbl_detect(int irq, void *dummy)
212 vbl_detected++;
213 return IRQ_HANDLED;
215 #endif
217 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
218 static inline void fbcon_set_rotation(struct fb_info *info)
220 struct fbcon_ops *ops = info->fbcon_par;
222 if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
223 ops->p->con_rotate < 4)
224 ops->rotate = ops->p->con_rotate;
225 else
226 ops->rotate = 0;
229 static void fbcon_rotate(struct fb_info *info, u32 rotate)
231 struct fbcon_ops *ops= info->fbcon_par;
232 struct fb_info *fb_info;
234 if (!ops || ops->currcon == -1)
235 return;
237 fb_info = registered_fb[con2fb_map[ops->currcon]];
239 if (info == fb_info) {
240 struct display *p = &fb_display[ops->currcon];
242 if (rotate < 4)
243 p->con_rotate = rotate;
244 else
245 p->con_rotate = 0;
247 fbcon_modechanged(info);
251 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
253 struct fbcon_ops *ops = info->fbcon_par;
254 struct vc_data *vc;
255 struct display *p;
256 int i;
258 if (!ops || ops->currcon < 0 || rotate > 3)
259 return;
261 for (i = first_fb_vc; i <= last_fb_vc; i++) {
262 vc = vc_cons[i].d;
263 if (!vc || vc->vc_mode != KD_TEXT ||
264 registered_fb[con2fb_map[i]] != info)
265 continue;
267 p = &fb_display[vc->vc_num];
268 p->con_rotate = rotate;
271 fbcon_set_all_vcs(info);
273 #else
274 static inline void fbcon_set_rotation(struct fb_info *info)
276 struct fbcon_ops *ops = info->fbcon_par;
278 ops->rotate = FB_ROTATE_UR;
281 static void fbcon_rotate(struct fb_info *info, u32 rotate)
283 return;
286 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
288 return;
290 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
292 static int fbcon_get_rotate(struct fb_info *info)
294 struct fbcon_ops *ops = info->fbcon_par;
296 return (ops) ? ops->rotate : 0;
299 static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
301 struct fbcon_ops *ops = info->fbcon_par;
303 return (info->state != FBINFO_STATE_RUNNING ||
304 vc->vc_mode != KD_TEXT || ops->graphics);
307 static inline int get_color(struct vc_data *vc, struct fb_info *info,
308 u16 c, int is_fg)
310 int depth = fb_get_color_depth(&info->var, &info->fix);
311 int color = 0;
313 if (console_blanked) {
314 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
316 c = vc->vc_video_erase_char & charmask;
319 if (depth != 1)
320 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
321 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
323 switch (depth) {
324 case 1:
326 int col = ~(0xfff << (max(info->var.green.length,
327 max(info->var.red.length,
328 info->var.blue.length)))) & 0xff;
330 /* 0 or 1 */
331 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
332 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
334 if (console_blanked)
335 fg = bg;
337 color = (is_fg) ? fg : bg;
338 break;
340 case 2:
342 * Scale down 16-colors to 4 colors. Default 4-color palette
343 * is grayscale. However, simply dividing the values by 4
344 * will not work, as colors 1, 2 and 3 will be scaled-down
345 * to zero rendering them invisible. So empirically convert
346 * colors to a sane 4-level grayscale.
348 switch (color) {
349 case 0:
350 color = 0; /* black */
351 break;
352 case 1 ... 6:
353 color = 2; /* white */
354 break;
355 case 7 ... 8:
356 color = 1; /* gray */
357 break;
358 default:
359 color = 3; /* intense white */
360 break;
362 break;
363 case 3:
365 * Last 8 entries of default 16-color palette is a more intense
366 * version of the first 8 (i.e., same chrominance, different
367 * luminance).
369 color &= 7;
370 break;
374 return color;
377 static void fbcon_update_softback(struct vc_data *vc)
379 int l = fbcon_softback_size / vc->vc_size_row;
381 if (l > 5)
382 softback_end = softback_buf + l * vc->vc_size_row;
383 else
384 /* Smaller scrollback makes no sense, and 0 would screw
385 the operation totally */
386 softback_top = 0;
389 static void fb_flashcursor(struct work_struct *work)
391 struct fb_info *info = container_of(work, struct fb_info, queue);
392 struct fbcon_ops *ops = info->fbcon_par;
393 struct display *p;
394 struct vc_data *vc = NULL;
395 int c;
396 int mode;
398 acquire_console_sem();
399 if (ops && ops->currcon != -1)
400 vc = vc_cons[ops->currcon].d;
402 if (!vc || !CON_IS_VISIBLE(vc) ||
403 registered_fb[con2fb_map[vc->vc_num]] != info ||
404 vc->vc_deccm != 1) {
405 release_console_sem();
406 return;
409 p = &fb_display[vc->vc_num];
410 c = scr_readw((u16 *) vc->vc_pos);
411 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
412 CM_ERASE : CM_DRAW;
413 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
414 get_color(vc, info, c, 0));
415 release_console_sem();
418 #if defined(CONFIG_ATARI) || defined(CONFIG_MAC)
419 static int cursor_blink_rate;
420 static irqreturn_t fb_vbl_handler(int irq, void *dev_id)
422 struct fb_info *info = dev_id;
424 if (vbl_cursor_cnt && --vbl_cursor_cnt == 0) {
425 schedule_work(&info->queue);
426 vbl_cursor_cnt = cursor_blink_rate;
428 return IRQ_HANDLED;
430 #endif
432 static void cursor_timer_handler(unsigned long dev_addr)
434 struct fb_info *info = (struct fb_info *) dev_addr;
435 struct fbcon_ops *ops = info->fbcon_par;
437 schedule_work(&info->queue);
438 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
441 static void fbcon_add_cursor_timer(struct fb_info *info)
443 struct fbcon_ops *ops = info->fbcon_par;
445 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
446 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
447 !fbcon_cursor_noblink) {
448 if (!info->queue.func)
449 INIT_WORK(&info->queue, fb_flashcursor);
451 init_timer(&ops->cursor_timer);
452 ops->cursor_timer.function = cursor_timer_handler;
453 ops->cursor_timer.expires = jiffies + HZ / 5;
454 ops->cursor_timer.data = (unsigned long ) info;
455 add_timer(&ops->cursor_timer);
456 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
460 static void fbcon_del_cursor_timer(struct fb_info *info)
462 struct fbcon_ops *ops = info->fbcon_par;
464 if (info->queue.func == fb_flashcursor &&
465 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
466 del_timer_sync(&ops->cursor_timer);
467 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
471 #ifndef MODULE
472 static int __init fb_console_setup(char *this_opt)
474 char *options;
475 int i, j;
477 if (!this_opt || !*this_opt)
478 return 1;
480 while ((options = strsep(&this_opt, ",")) != NULL) {
481 if (!strncmp(options, "font:", 5))
482 strcpy(fontname, options + 5);
484 if (!strncmp(options, "scrollback:", 11)) {
485 options += 11;
486 if (*options) {
487 fbcon_softback_size = simple_strtoul(options, &options, 0);
488 if (*options == 'k' || *options == 'K') {
489 fbcon_softback_size *= 1024;
490 options++;
492 if (*options != ',')
493 return 1;
494 options++;
495 } else
496 return 1;
499 if (!strncmp(options, "map:", 4)) {
500 options += 4;
501 if (*options) {
502 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
503 if (!options[j])
504 j = 0;
505 con2fb_map_boot[i] =
506 (options[j++]-'0') % FB_MAX;
509 map_override = 1;
512 return 1;
515 if (!strncmp(options, "vc:", 3)) {
516 options += 3;
517 if (*options)
518 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
519 if (first_fb_vc < 0)
520 first_fb_vc = 0;
521 if (*options++ == '-')
522 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
523 fbcon_is_default = 0;
526 if (!strncmp(options, "rotate:", 7)) {
527 options += 7;
528 if (*options)
529 rotate = simple_strtoul(options, &options, 0);
530 if (rotate > 3)
531 rotate = 0;
534 return 1;
537 __setup("fbcon=", fb_console_setup);
538 #endif
540 static int search_fb_in_map(int idx)
542 int i, retval = 0;
544 for (i = first_fb_vc; i <= last_fb_vc; i++) {
545 if (con2fb_map[i] == idx)
546 retval = 1;
548 return retval;
551 static int search_for_mapped_con(void)
553 int i, retval = 0;
555 for (i = first_fb_vc; i <= last_fb_vc; i++) {
556 if (con2fb_map[i] != -1)
557 retval = 1;
559 return retval;
562 static int fbcon_takeover(int show_logo)
564 int err, i;
566 if (!num_registered_fb)
567 return -ENODEV;
569 if (!show_logo)
570 logo_shown = FBCON_LOGO_DONTSHOW;
572 for (i = first_fb_vc; i <= last_fb_vc; i++)
573 con2fb_map[i] = info_idx;
575 err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
576 fbcon_is_default);
578 if (err) {
579 for (i = first_fb_vc; i <= last_fb_vc; i++) {
580 con2fb_map[i] = -1;
582 info_idx = -1;
585 return err;
588 #ifdef MODULE
589 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
590 int cols, int rows, int new_cols, int new_rows)
592 logo_shown = FBCON_LOGO_DONTSHOW;
594 #else
595 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
596 int cols, int rows, int new_cols, int new_rows)
598 /* Need to make room for the logo */
599 struct fbcon_ops *ops = info->fbcon_par;
600 int cnt, erase = vc->vc_video_erase_char, step;
601 unsigned short *save = NULL, *r, *q;
603 if (info->flags & FBINFO_MODULE) {
604 logo_shown = FBCON_LOGO_DONTSHOW;
605 return;
609 * remove underline attribute from erase character
610 * if black and white framebuffer.
612 if (fb_get_color_depth(&info->var, &info->fix) == 1)
613 erase &= ~0x400;
614 logo_height = fb_prepare_logo(info, ops->rotate);
615 logo_lines = (logo_height + vc->vc_font.height - 1) /
616 vc->vc_font.height;
617 q = (unsigned short *) (vc->vc_origin +
618 vc->vc_size_row * rows);
619 step = logo_lines * cols;
620 for (r = q - logo_lines * cols; r < q; r++)
621 if (scr_readw(r) != vc->vc_video_erase_char)
622 break;
623 if (r != q && new_rows >= rows + logo_lines) {
624 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
625 if (save) {
626 int i = cols < new_cols ? cols : new_cols;
627 scr_memsetw(save, erase, logo_lines * new_cols * 2);
628 r = q - step;
629 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
630 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
631 r = q;
634 if (r == q) {
635 /* We can scroll screen down */
636 r = q - step - cols;
637 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
638 scr_memcpyw(r + step, r, vc->vc_size_row);
639 r -= cols;
641 if (!save) {
642 int lines;
643 if (vc->vc_y + logo_lines >= rows)
644 lines = rows - vc->vc_y - 1;
645 else
646 lines = logo_lines;
647 vc->vc_y += lines;
648 vc->vc_pos += lines * vc->vc_size_row;
651 scr_memsetw((unsigned short *) vc->vc_origin,
652 erase,
653 vc->vc_size_row * logo_lines);
655 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
656 fbcon_clear_margins(vc, 0);
657 update_screen(vc);
660 if (save) {
661 q = (unsigned short *) (vc->vc_origin +
662 vc->vc_size_row *
663 rows);
664 scr_memcpyw(q, save, logo_lines * new_cols * 2);
665 vc->vc_y += logo_lines;
666 vc->vc_pos += logo_lines * vc->vc_size_row;
667 kfree(save);
670 if (logo_lines > vc->vc_bottom) {
671 logo_shown = FBCON_LOGO_CANSHOW;
672 printk(KERN_INFO
673 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
674 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
675 logo_shown = FBCON_LOGO_DRAW;
676 vc->vc_top = logo_lines;
679 #endif /* MODULE */
681 #ifdef CONFIG_FB_TILEBLITTING
682 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
684 struct fbcon_ops *ops = info->fbcon_par;
686 ops->p = &fb_display[vc->vc_num];
688 if ((info->flags & FBINFO_MISC_TILEBLITTING))
689 fbcon_set_tileops(vc, info);
690 else {
691 fbcon_set_rotation(info);
692 fbcon_set_bitops(ops);
696 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
698 int err = 0;
700 if (info->flags & FBINFO_MISC_TILEBLITTING &&
701 info->tileops->fb_get_tilemax(info) < charcount)
702 err = 1;
704 return err;
706 #else
707 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
709 struct fbcon_ops *ops = info->fbcon_par;
711 info->flags &= ~FBINFO_MISC_TILEBLITTING;
712 ops->p = &fb_display[vc->vc_num];
713 fbcon_set_rotation(info);
714 fbcon_set_bitops(ops);
717 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
719 return 0;
722 #endif /* CONFIG_MISC_TILEBLITTING */
725 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
726 int unit, int oldidx)
728 struct fbcon_ops *ops = NULL;
729 int err = 0;
731 if (!try_module_get(info->fbops->owner))
732 err = -ENODEV;
734 if (!err && info->fbops->fb_open &&
735 info->fbops->fb_open(info, 0))
736 err = -ENODEV;
738 if (!err) {
739 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
740 if (!ops)
741 err = -ENOMEM;
744 if (!err) {
745 info->fbcon_par = ops;
747 if (vc)
748 set_blitting_type(vc, info);
751 if (err) {
752 con2fb_map[unit] = oldidx;
753 module_put(info->fbops->owner);
756 return err;
759 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
760 struct fb_info *newinfo, int unit,
761 int oldidx, int found)
763 struct fbcon_ops *ops = oldinfo->fbcon_par;
764 int err = 0;
766 if (oldinfo->fbops->fb_release &&
767 oldinfo->fbops->fb_release(oldinfo, 0)) {
768 con2fb_map[unit] = oldidx;
769 if (!found && newinfo->fbops->fb_release)
770 newinfo->fbops->fb_release(newinfo, 0);
771 if (!found)
772 module_put(newinfo->fbops->owner);
773 err = -ENODEV;
776 if (!err) {
777 fbcon_del_cursor_timer(oldinfo);
778 kfree(ops->cursor_state.mask);
779 kfree(ops->cursor_data);
780 kfree(ops->fontbuffer);
781 kfree(oldinfo->fbcon_par);
782 oldinfo->fbcon_par = NULL;
783 module_put(oldinfo->fbops->owner);
785 If oldinfo and newinfo are driving the same hardware,
786 the fb_release() method of oldinfo may attempt to
787 restore the hardware state. This will leave the
788 newinfo in an undefined state. Thus, a call to
789 fb_set_par() may be needed for the newinfo.
791 if (newinfo->fbops->fb_set_par)
792 newinfo->fbops->fb_set_par(newinfo);
795 return err;
798 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
799 int unit, int show_logo)
801 struct fbcon_ops *ops = info->fbcon_par;
803 ops->currcon = fg_console;
805 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT))
806 info->fbops->fb_set_par(info);
808 ops->flags |= FBCON_FLAGS_INIT;
809 ops->graphics = 0;
810 fbcon_set_disp(info, &info->var, unit);
812 if (show_logo) {
813 struct vc_data *fg_vc = vc_cons[fg_console].d;
814 struct fb_info *fg_info =
815 registered_fb[con2fb_map[fg_console]];
817 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
818 fg_vc->vc_rows, fg_vc->vc_cols,
819 fg_vc->vc_rows);
822 update_screen(vc_cons[fg_console].d);
826 * set_con2fb_map - map console to frame buffer device
827 * @unit: virtual console number to map
828 * @newidx: frame buffer index to map virtual console to
829 * @user: user request
831 * Maps a virtual console @unit to a frame buffer device
832 * @newidx.
834 static int set_con2fb_map(int unit, int newidx, int user)
836 struct vc_data *vc = vc_cons[unit].d;
837 int oldidx = con2fb_map[unit];
838 struct fb_info *info = registered_fb[newidx];
839 struct fb_info *oldinfo = NULL;
840 int found, err = 0;
842 if (oldidx == newidx)
843 return 0;
845 if (!info || fbcon_has_exited)
846 return -EINVAL;
848 if (!err && !search_for_mapped_con()) {
849 info_idx = newidx;
850 return fbcon_takeover(0);
853 if (oldidx != -1)
854 oldinfo = registered_fb[oldidx];
856 found = search_fb_in_map(newidx);
858 acquire_console_sem();
859 con2fb_map[unit] = newidx;
860 if (!err && !found)
861 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
865 * If old fb is not mapped to any of the consoles,
866 * fbcon should release it.
868 if (!err && oldinfo && !search_fb_in_map(oldidx))
869 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
870 found);
872 if (!err) {
873 int show_logo = (fg_console == 0 && !user &&
874 logo_shown != FBCON_LOGO_DONTSHOW);
876 if (!found)
877 fbcon_add_cursor_timer(info);
878 con2fb_map_boot[unit] = newidx;
879 con2fb_init_display(vc, info, unit, show_logo);
882 if (!search_fb_in_map(info_idx))
883 info_idx = newidx;
885 release_console_sem();
886 return err;
890 * Low Level Operations
892 /* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
893 static int var_to_display(struct display *disp,
894 struct fb_var_screeninfo *var,
895 struct fb_info *info)
897 disp->xres_virtual = var->xres_virtual;
898 disp->yres_virtual = var->yres_virtual;
899 disp->bits_per_pixel = var->bits_per_pixel;
900 disp->grayscale = var->grayscale;
901 disp->nonstd = var->nonstd;
902 disp->accel_flags = var->accel_flags;
903 disp->height = var->height;
904 disp->width = var->width;
905 disp->red = var->red;
906 disp->green = var->green;
907 disp->blue = var->blue;
908 disp->transp = var->transp;
909 disp->rotate = var->rotate;
910 disp->mode = fb_match_mode(var, &info->modelist);
911 if (disp->mode == NULL)
912 /* This should not happen */
913 return -EINVAL;
914 return 0;
917 static void display_to_var(struct fb_var_screeninfo *var,
918 struct display *disp)
920 fb_videomode_to_var(var, disp->mode);
921 var->xres_virtual = disp->xres_virtual;
922 var->yres_virtual = disp->yres_virtual;
923 var->bits_per_pixel = disp->bits_per_pixel;
924 var->grayscale = disp->grayscale;
925 var->nonstd = disp->nonstd;
926 var->accel_flags = disp->accel_flags;
927 var->height = disp->height;
928 var->width = disp->width;
929 var->red = disp->red;
930 var->green = disp->green;
931 var->blue = disp->blue;
932 var->transp = disp->transp;
933 var->rotate = disp->rotate;
936 static const char *fbcon_startup(void)
938 const char *display_desc = "frame buffer device";
939 struct display *p = &fb_display[fg_console];
940 struct vc_data *vc = vc_cons[fg_console].d;
941 const struct font_desc *font = NULL;
942 struct module *owner;
943 struct fb_info *info = NULL;
944 struct fbcon_ops *ops;
945 int rows, cols;
946 int irqres;
948 irqres = 1;
950 * If num_registered_fb is zero, this is a call for the dummy part.
951 * The frame buffer devices weren't initialized yet.
953 if (!num_registered_fb || info_idx == -1)
954 return display_desc;
956 * Instead of blindly using registered_fb[0], we use info_idx, set by
957 * fb_console_init();
959 info = registered_fb[info_idx];
960 if (!info)
961 return NULL;
963 owner = info->fbops->owner;
964 if (!try_module_get(owner))
965 return NULL;
966 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
967 module_put(owner);
968 return NULL;
971 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
972 if (!ops) {
973 module_put(owner);
974 return NULL;
977 ops->currcon = -1;
978 ops->graphics = 1;
979 ops->cur_rotate = -1;
980 info->fbcon_par = ops;
981 p->con_rotate = rotate;
982 set_blitting_type(vc, info);
984 if (info->fix.type != FB_TYPE_TEXT) {
985 if (fbcon_softback_size) {
986 if (!softback_buf) {
987 softback_buf =
988 (unsigned long)
989 kmalloc(fbcon_softback_size,
990 GFP_KERNEL);
991 if (!softback_buf) {
992 fbcon_softback_size = 0;
993 softback_top = 0;
996 } else {
997 if (softback_buf) {
998 kfree((void *) softback_buf);
999 softback_buf = 0;
1000 softback_top = 0;
1003 if (softback_buf)
1004 softback_in = softback_top = softback_curr =
1005 softback_buf;
1006 softback_lines = 0;
1009 /* Setup default font */
1010 if (!p->fontdata) {
1011 if (!fontname[0] || !(font = find_font(fontname)))
1012 font = get_default_font(info->var.xres,
1013 info->var.yres,
1014 info->pixmap.blit_x,
1015 info->pixmap.blit_y);
1016 vc->vc_font.width = font->width;
1017 vc->vc_font.height = font->height;
1018 vc->vc_font.data = (void *)(p->fontdata = font->data);
1019 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1022 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1023 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1024 cols /= vc->vc_font.width;
1025 rows /= vc->vc_font.height;
1026 vc_resize(vc, cols, rows);
1028 DPRINTK("mode: %s\n", info->fix.id);
1029 DPRINTK("visual: %d\n", info->fix.visual);
1030 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1031 info->var.yres,
1032 info->var.bits_per_pixel);
1034 #ifdef CONFIG_ATARI
1035 if (MACH_IS_ATARI) {
1036 cursor_blink_rate = ATARI_CURSOR_BLINK_RATE;
1037 irqres =
1038 request_irq(IRQ_AUTO_4, fb_vbl_handler,
1039 IRQ_TYPE_PRIO, "framebuffer vbl",
1040 info);
1042 #endif /* CONFIG_ATARI */
1044 #ifdef CONFIG_MAC
1046 * On a Macintoy, the VBL interrupt may or may not be active.
1047 * As interrupt based cursor is more reliable and race free, we
1048 * probe for VBL interrupts.
1050 if (MACH_IS_MAC) {
1051 int ct = 0;
1053 * Probe for VBL: set temp. handler ...
1055 irqres = request_irq(IRQ_MAC_VBL, fb_vbl_detect, 0,
1056 "framebuffer vbl", info);
1057 vbl_detected = 0;
1060 * ... and spin for 20 ms ...
1062 while (!vbl_detected && ++ct < 1000)
1063 udelay(20);
1065 if (ct == 1000)
1066 printk
1067 ("fbcon_startup: No VBL detected, using timer based cursor.\n");
1069 free_irq(IRQ_MAC_VBL, fb_vbl_detect);
1071 if (vbl_detected) {
1073 * interrupt based cursor ok
1075 cursor_blink_rate = MAC_CURSOR_BLINK_RATE;
1076 irqres =
1077 request_irq(IRQ_MAC_VBL, fb_vbl_handler, 0,
1078 "framebuffer vbl", info);
1079 } else {
1081 * VBL not detected: fall through, use timer based cursor
1083 irqres = 1;
1086 #endif /* CONFIG_MAC */
1088 fbcon_add_cursor_timer(info);
1089 fbcon_has_exited = 0;
1090 return display_desc;
1093 static void fbcon_init(struct vc_data *vc, int init)
1095 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1096 struct fbcon_ops *ops;
1097 struct vc_data **default_mode = vc->vc_display_fg;
1098 struct vc_data *svc = *default_mode;
1099 struct display *t, *p = &fb_display[vc->vc_num];
1100 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
1101 int cap;
1103 if (info_idx == -1 || info == NULL)
1104 return;
1106 cap = info->flags;
1108 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1109 (info->fix.type == FB_TYPE_TEXT))
1110 logo = 0;
1112 if (var_to_display(p, &info->var, info))
1113 return;
1115 if (!info->fbcon_par)
1116 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1118 /* If we are not the first console on this
1119 fb, copy the font from that console */
1120 t = &fb_display[fg_console];
1121 if (!p->fontdata) {
1122 if (t->fontdata) {
1123 struct vc_data *fvc = vc_cons[fg_console].d;
1125 vc->vc_font.data = (void *)(p->fontdata =
1126 fvc->vc_font.data);
1127 vc->vc_font.width = fvc->vc_font.width;
1128 vc->vc_font.height = fvc->vc_font.height;
1129 p->userfont = t->userfont;
1131 if (p->userfont)
1132 REFCOUNT(p->fontdata)++;
1133 } else {
1134 const struct font_desc *font = NULL;
1136 if (!fontname[0] || !(font = find_font(fontname)))
1137 font = get_default_font(info->var.xres,
1138 info->var.yres,
1139 info->pixmap.blit_x,
1140 info->pixmap.blit_y);
1141 vc->vc_font.width = font->width;
1142 vc->vc_font.height = font->height;
1143 vc->vc_font.data = (void *)(p->fontdata = font->data);
1144 vc->vc_font.charcount = 256; /* FIXME Need to
1145 support more fonts */
1149 if (p->userfont)
1150 charcnt = FNTCHARCNT(p->fontdata);
1152 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1153 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1154 if (charcnt == 256) {
1155 vc->vc_hi_font_mask = 0;
1156 } else {
1157 vc->vc_hi_font_mask = 0x100;
1158 if (vc->vc_can_do_color)
1159 vc->vc_complement_mask <<= 1;
1162 if (!*svc->vc_uni_pagedir_loc)
1163 con_set_default_unimap(svc);
1164 if (!*vc->vc_uni_pagedir_loc)
1165 con_copy_unimap(vc, svc);
1167 ops = info->fbcon_par;
1168 p->con_rotate = rotate;
1169 set_blitting_type(vc, info);
1171 cols = vc->vc_cols;
1172 rows = vc->vc_rows;
1173 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1174 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1175 new_cols /= vc->vc_font.width;
1176 new_rows /= vc->vc_font.height;
1177 vc_resize(vc, new_cols, new_rows);
1180 * We must always set the mode. The mode of the previous console
1181 * driver could be in the same resolution but we are using different
1182 * hardware so we have to initialize the hardware.
1184 * We need to do it in fbcon_init() to prevent screen corruption.
1186 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
1187 if (info->fbops->fb_set_par &&
1188 !(ops->flags & FBCON_FLAGS_INIT))
1189 info->fbops->fb_set_par(info);
1190 ops->flags |= FBCON_FLAGS_INIT;
1193 ops->graphics = 0;
1195 if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1196 !(cap & FBINFO_HWACCEL_DISABLED))
1197 p->scrollmode = SCROLL_MOVE;
1198 else /* default to something safe */
1199 p->scrollmode = SCROLL_REDRAW;
1202 * ++guenther: console.c:vc_allocate() relies on initializing
1203 * vc_{cols,rows}, but we must not set those if we are only
1204 * resizing the console.
1206 if (!init) {
1207 vc->vc_cols = new_cols;
1208 vc->vc_rows = new_rows;
1211 if (logo)
1212 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1214 if (vc == svc && softback_buf)
1215 fbcon_update_softback(vc);
1217 if (ops->rotate_font && ops->rotate_font(info, vc)) {
1218 ops->rotate = FB_ROTATE_UR;
1219 set_blitting_type(vc, info);
1222 ops->p = &fb_display[fg_console];
1225 static void fbcon_free_font(struct display *p)
1227 if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1228 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1229 p->fontdata = NULL;
1230 p->userfont = 0;
1233 static void fbcon_deinit(struct vc_data *vc)
1235 struct display *p = &fb_display[vc->vc_num];
1236 struct fb_info *info;
1237 struct fbcon_ops *ops;
1238 int idx;
1240 fbcon_free_font(p);
1241 idx = con2fb_map[vc->vc_num];
1243 if (idx == -1)
1244 goto finished;
1246 info = registered_fb[idx];
1248 if (!info)
1249 goto finished;
1251 ops = info->fbcon_par;
1253 if (!ops)
1254 goto finished;
1256 if (CON_IS_VISIBLE(vc))
1257 fbcon_del_cursor_timer(info);
1259 ops->flags &= ~FBCON_FLAGS_INIT;
1260 finished:
1262 if (!con_is_bound(&fb_con))
1263 fbcon_exit();
1265 return;
1268 /* ====================================================================== */
1270 /* fbcon_XXX routines - interface used by the world
1272 * This system is now divided into two levels because of complications
1273 * caused by hardware scrolling. Top level functions:
1275 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1277 * handles y values in range [0, scr_height-1] that correspond to real
1278 * screen positions. y_wrap shift means that first line of bitmap may be
1279 * anywhere on this display. These functions convert lineoffsets to
1280 * bitmap offsets and deal with the wrap-around case by splitting blits.
1282 * fbcon_bmove_physical_8() -- These functions fast implementations
1283 * fbcon_clear_physical_8() -- of original fbcon_XXX fns.
1284 * fbcon_putc_physical_8() -- (font width != 8) may be added later
1286 * WARNING:
1288 * At the moment fbcon_putc() cannot blit across vertical wrap boundary
1289 * Implies should only really hardware scroll in rows. Only reason for
1290 * restriction is simplicity & efficiency at the moment.
1293 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1294 int width)
1296 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1297 struct fbcon_ops *ops = info->fbcon_par;
1299 struct display *p = &fb_display[vc->vc_num];
1300 u_int y_break;
1302 if (fbcon_is_inactive(vc, info))
1303 return;
1305 if (!height || !width)
1306 return;
1308 /* Split blits that cross physical y_wrap boundary */
1310 y_break = p->vrows - p->yscroll;
1311 if (sy < y_break && sy + height - 1 >= y_break) {
1312 u_int b = y_break - sy;
1313 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1314 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1315 width);
1316 } else
1317 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1320 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1321 int count, int ypos, int xpos)
1323 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1324 struct display *p = &fb_display[vc->vc_num];
1325 struct fbcon_ops *ops = info->fbcon_par;
1327 if (!fbcon_is_inactive(vc, info))
1328 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1329 get_color(vc, info, scr_readw(s), 1),
1330 get_color(vc, info, scr_readw(s), 0));
1333 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1335 unsigned short chr;
1337 scr_writew(c, &chr);
1338 fbcon_putcs(vc, &chr, 1, ypos, xpos);
1341 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1343 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1344 struct fbcon_ops *ops = info->fbcon_par;
1346 if (!fbcon_is_inactive(vc, info))
1347 ops->clear_margins(vc, info, bottom_only);
1350 static void fbcon_cursor(struct vc_data *vc, int mode)
1352 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1353 struct fbcon_ops *ops = info->fbcon_par;
1354 int y;
1355 int c = scr_readw((u16 *) vc->vc_pos);
1357 if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1358 return;
1360 if (vc->vc_cursor_type & 0x10)
1361 fbcon_del_cursor_timer(info);
1362 else
1363 fbcon_add_cursor_timer(info);
1365 ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1366 if (mode & CM_SOFTBACK) {
1367 mode &= ~CM_SOFTBACK;
1368 y = softback_lines;
1369 } else {
1370 if (softback_lines)
1371 fbcon_set_origin(vc);
1372 y = 0;
1375 ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1376 get_color(vc, info, c, 0));
1377 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1380 static int scrollback_phys_max = 0;
1381 static int scrollback_max = 0;
1382 static int scrollback_current = 0;
1384 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1385 int unit)
1387 struct display *p, *t;
1388 struct vc_data **default_mode, *vc;
1389 struct vc_data *svc;
1390 struct fbcon_ops *ops = info->fbcon_par;
1391 int rows, cols, charcnt = 256;
1393 p = &fb_display[unit];
1395 if (var_to_display(p, var, info))
1396 return;
1398 vc = vc_cons[unit].d;
1400 if (!vc)
1401 return;
1403 default_mode = vc->vc_display_fg;
1404 svc = *default_mode;
1405 t = &fb_display[svc->vc_num];
1407 if (!vc->vc_font.data) {
1408 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1409 vc->vc_font.width = (*default_mode)->vc_font.width;
1410 vc->vc_font.height = (*default_mode)->vc_font.height;
1411 p->userfont = t->userfont;
1412 if (p->userfont)
1413 REFCOUNT(p->fontdata)++;
1415 if (p->userfont)
1416 charcnt = FNTCHARCNT(p->fontdata);
1418 var->activate = FB_ACTIVATE_NOW;
1419 info->var.activate = var->activate;
1420 var->yoffset = info->var.yoffset;
1421 var->xoffset = info->var.xoffset;
1422 fb_set_var(info, var);
1423 ops->var = info->var;
1424 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1425 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1426 if (charcnt == 256) {
1427 vc->vc_hi_font_mask = 0;
1428 } else {
1429 vc->vc_hi_font_mask = 0x100;
1430 if (vc->vc_can_do_color)
1431 vc->vc_complement_mask <<= 1;
1434 if (!*svc->vc_uni_pagedir_loc)
1435 con_set_default_unimap(svc);
1436 if (!*vc->vc_uni_pagedir_loc)
1437 con_copy_unimap(vc, svc);
1439 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1440 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1441 cols /= vc->vc_font.width;
1442 rows /= vc->vc_font.height;
1443 vc_resize(vc, cols, rows);
1445 if (CON_IS_VISIBLE(vc)) {
1446 update_screen(vc);
1447 if (softback_buf)
1448 fbcon_update_softback(vc);
1452 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1454 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1455 struct fbcon_ops *ops = info->fbcon_par;
1456 struct display *p = &fb_display[vc->vc_num];
1458 p->yscroll += count;
1459 if (p->yscroll >= p->vrows) /* Deal with wrap */
1460 p->yscroll -= p->vrows;
1461 ops->var.xoffset = 0;
1462 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1463 ops->var.vmode |= FB_VMODE_YWRAP;
1464 ops->update_start(info);
1465 scrollback_max += count;
1466 if (scrollback_max > scrollback_phys_max)
1467 scrollback_max = scrollback_phys_max;
1468 scrollback_current = 0;
1471 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1473 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1474 struct fbcon_ops *ops = info->fbcon_par;
1475 struct display *p = &fb_display[vc->vc_num];
1477 p->yscroll -= count;
1478 if (p->yscroll < 0) /* Deal with wrap */
1479 p->yscroll += p->vrows;
1480 ops->var.xoffset = 0;
1481 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1482 ops->var.vmode |= FB_VMODE_YWRAP;
1483 ops->update_start(info);
1484 scrollback_max -= count;
1485 if (scrollback_max < 0)
1486 scrollback_max = 0;
1487 scrollback_current = 0;
1490 static __inline__ void ypan_up(struct vc_data *vc, int count)
1492 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1493 struct display *p = &fb_display[vc->vc_num];
1494 struct fbcon_ops *ops = info->fbcon_par;
1496 p->yscroll += count;
1497 if (p->yscroll > p->vrows - vc->vc_rows) {
1498 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1499 0, 0, 0, vc->vc_rows, vc->vc_cols);
1500 p->yscroll -= p->vrows - vc->vc_rows;
1503 ops->var.xoffset = 0;
1504 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1505 ops->var.vmode &= ~FB_VMODE_YWRAP;
1506 ops->update_start(info);
1507 fbcon_clear_margins(vc, 1);
1508 scrollback_max += count;
1509 if (scrollback_max > scrollback_phys_max)
1510 scrollback_max = scrollback_phys_max;
1511 scrollback_current = 0;
1514 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1516 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1517 struct fbcon_ops *ops = info->fbcon_par;
1518 struct display *p = &fb_display[vc->vc_num];
1520 p->yscroll += count;
1522 if (p->yscroll > p->vrows - vc->vc_rows) {
1523 p->yscroll -= p->vrows - vc->vc_rows;
1524 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1527 ops->var.xoffset = 0;
1528 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1529 ops->var.vmode &= ~FB_VMODE_YWRAP;
1530 ops->update_start(info);
1531 fbcon_clear_margins(vc, 1);
1532 scrollback_max += count;
1533 if (scrollback_max > scrollback_phys_max)
1534 scrollback_max = scrollback_phys_max;
1535 scrollback_current = 0;
1538 static __inline__ void ypan_down(struct vc_data *vc, int count)
1540 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1541 struct display *p = &fb_display[vc->vc_num];
1542 struct fbcon_ops *ops = info->fbcon_par;
1544 p->yscroll -= count;
1545 if (p->yscroll < 0) {
1546 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1547 0, vc->vc_rows, vc->vc_cols);
1548 p->yscroll += p->vrows - vc->vc_rows;
1551 ops->var.xoffset = 0;
1552 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1553 ops->var.vmode &= ~FB_VMODE_YWRAP;
1554 ops->update_start(info);
1555 fbcon_clear_margins(vc, 1);
1556 scrollback_max -= count;
1557 if (scrollback_max < 0)
1558 scrollback_max = 0;
1559 scrollback_current = 0;
1562 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1564 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1565 struct fbcon_ops *ops = info->fbcon_par;
1566 struct display *p = &fb_display[vc->vc_num];
1568 p->yscroll -= count;
1570 if (p->yscroll < 0) {
1571 p->yscroll += p->vrows - vc->vc_rows;
1572 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1575 ops->var.xoffset = 0;
1576 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1577 ops->var.vmode &= ~FB_VMODE_YWRAP;
1578 ops->update_start(info);
1579 fbcon_clear_margins(vc, 1);
1580 scrollback_max -= count;
1581 if (scrollback_max < 0)
1582 scrollback_max = 0;
1583 scrollback_current = 0;
1586 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1587 long delta)
1589 int count = vc->vc_rows;
1590 unsigned short *d, *s;
1591 unsigned long n;
1592 int line = 0;
1594 d = (u16 *) softback_curr;
1595 if (d == (u16 *) softback_in)
1596 d = (u16 *) vc->vc_origin;
1597 n = softback_curr + delta * vc->vc_size_row;
1598 softback_lines -= delta;
1599 if (delta < 0) {
1600 if (softback_curr < softback_top && n < softback_buf) {
1601 n += softback_end - softback_buf;
1602 if (n < softback_top) {
1603 softback_lines -=
1604 (softback_top - n) / vc->vc_size_row;
1605 n = softback_top;
1607 } else if (softback_curr >= softback_top
1608 && n < softback_top) {
1609 softback_lines -=
1610 (softback_top - n) / vc->vc_size_row;
1611 n = softback_top;
1613 } else {
1614 if (softback_curr > softback_in && n >= softback_end) {
1615 n += softback_buf - softback_end;
1616 if (n > softback_in) {
1617 n = softback_in;
1618 softback_lines = 0;
1620 } else if (softback_curr <= softback_in && n > softback_in) {
1621 n = softback_in;
1622 softback_lines = 0;
1625 if (n == softback_curr)
1626 return;
1627 softback_curr = n;
1628 s = (u16 *) softback_curr;
1629 if (s == (u16 *) softback_in)
1630 s = (u16 *) vc->vc_origin;
1631 while (count--) {
1632 unsigned short *start;
1633 unsigned short *le;
1634 unsigned short c;
1635 int x = 0;
1636 unsigned short attr = 1;
1638 start = s;
1639 le = advance_row(s, 1);
1640 do {
1641 c = scr_readw(s);
1642 if (attr != (c & 0xff00)) {
1643 attr = c & 0xff00;
1644 if (s > start) {
1645 fbcon_putcs(vc, start, s - start,
1646 line, x);
1647 x += s - start;
1648 start = s;
1651 if (c == scr_readw(d)) {
1652 if (s > start) {
1653 fbcon_putcs(vc, start, s - start,
1654 line, x);
1655 x += s - start + 1;
1656 start = s + 1;
1657 } else {
1658 x++;
1659 start++;
1662 s++;
1663 d++;
1664 } while (s < le);
1665 if (s > start)
1666 fbcon_putcs(vc, start, s - start, line, x);
1667 line++;
1668 if (d == (u16 *) softback_end)
1669 d = (u16 *) softback_buf;
1670 if (d == (u16 *) softback_in)
1671 d = (u16 *) vc->vc_origin;
1672 if (s == (u16 *) softback_end)
1673 s = (u16 *) softback_buf;
1674 if (s == (u16 *) softback_in)
1675 s = (u16 *) vc->vc_origin;
1679 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1680 int line, int count, int dy)
1682 unsigned short *s = (unsigned short *)
1683 (vc->vc_origin + vc->vc_size_row * line);
1685 while (count--) {
1686 unsigned short *start = s;
1687 unsigned short *le = advance_row(s, 1);
1688 unsigned short c;
1689 int x = 0;
1690 unsigned short attr = 1;
1692 do {
1693 c = scr_readw(s);
1694 if (attr != (c & 0xff00)) {
1695 attr = c & 0xff00;
1696 if (s > start) {
1697 fbcon_putcs(vc, start, s - start,
1698 dy, x);
1699 x += s - start;
1700 start = s;
1703 console_conditional_schedule();
1704 s++;
1705 } while (s < le);
1706 if (s > start)
1707 fbcon_putcs(vc, start, s - start, dy, x);
1708 console_conditional_schedule();
1709 dy++;
1713 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1714 struct display *p, int line, int count, int ycount)
1716 int offset = ycount * vc->vc_cols;
1717 unsigned short *d = (unsigned short *)
1718 (vc->vc_origin + vc->vc_size_row * line);
1719 unsigned short *s = d + offset;
1720 struct fbcon_ops *ops = info->fbcon_par;
1722 while (count--) {
1723 unsigned short *start = s;
1724 unsigned short *le = advance_row(s, 1);
1725 unsigned short c;
1726 int x = 0;
1728 do {
1729 c = scr_readw(s);
1731 if (c == scr_readw(d)) {
1732 if (s > start) {
1733 ops->bmove(vc, info, line + ycount, x,
1734 line, x, 1, s-start);
1735 x += s - start + 1;
1736 start = s + 1;
1737 } else {
1738 x++;
1739 start++;
1743 scr_writew(c, d);
1744 console_conditional_schedule();
1745 s++;
1746 d++;
1747 } while (s < le);
1748 if (s > start)
1749 ops->bmove(vc, info, line + ycount, x, line, x, 1,
1750 s-start);
1751 console_conditional_schedule();
1752 if (ycount > 0)
1753 line++;
1754 else {
1755 line--;
1756 /* NOTE: We subtract two lines from these pointers */
1757 s -= vc->vc_size_row;
1758 d -= vc->vc_size_row;
1763 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1764 int line, int count, int offset)
1766 unsigned short *d = (unsigned short *)
1767 (vc->vc_origin + vc->vc_size_row * line);
1768 unsigned short *s = d + offset;
1770 while (count--) {
1771 unsigned short *start = s;
1772 unsigned short *le = advance_row(s, 1);
1773 unsigned short c;
1774 int x = 0;
1775 unsigned short attr = 1;
1777 do {
1778 c = scr_readw(s);
1779 if (attr != (c & 0xff00)) {
1780 attr = c & 0xff00;
1781 if (s > start) {
1782 fbcon_putcs(vc, start, s - start,
1783 line, x);
1784 x += s - start;
1785 start = s;
1788 if (c == scr_readw(d)) {
1789 if (s > start) {
1790 fbcon_putcs(vc, start, s - start,
1791 line, x);
1792 x += s - start + 1;
1793 start = s + 1;
1794 } else {
1795 x++;
1796 start++;
1799 scr_writew(c, d);
1800 console_conditional_schedule();
1801 s++;
1802 d++;
1803 } while (s < le);
1804 if (s > start)
1805 fbcon_putcs(vc, start, s - start, line, x);
1806 console_conditional_schedule();
1807 if (offset > 0)
1808 line++;
1809 else {
1810 line--;
1811 /* NOTE: We subtract two lines from these pointers */
1812 s -= vc->vc_size_row;
1813 d -= vc->vc_size_row;
1818 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1819 int count)
1821 unsigned short *p;
1823 if (vc->vc_num != fg_console)
1824 return;
1825 p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1827 while (count) {
1828 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1829 count--;
1830 p = advance_row(p, 1);
1831 softback_in += vc->vc_size_row;
1832 if (softback_in == softback_end)
1833 softback_in = softback_buf;
1834 if (softback_in == softback_top) {
1835 softback_top += vc->vc_size_row;
1836 if (softback_top == softback_end)
1837 softback_top = softback_buf;
1840 softback_curr = softback_in;
1843 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1844 int count)
1846 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1847 struct display *p = &fb_display[vc->vc_num];
1848 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1850 if (fbcon_is_inactive(vc, info))
1851 return -EINVAL;
1853 fbcon_cursor(vc, CM_ERASE);
1856 * ++Geert: Only use ywrap/ypan if the console is in text mode
1857 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1858 * whole screen (prevents flicker).
1861 switch (dir) {
1862 case SM_UP:
1863 if (count > vc->vc_rows) /* Maximum realistic size */
1864 count = vc->vc_rows;
1865 if (softback_top)
1866 fbcon_softback_note(vc, t, count);
1867 if (logo_shown >= 0)
1868 goto redraw_up;
1869 switch (p->scrollmode) {
1870 case SCROLL_MOVE:
1871 fbcon_redraw_blit(vc, info, p, t, b - t - count,
1872 count);
1873 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1874 scr_memsetw((unsigned short *) (vc->vc_origin +
1875 vc->vc_size_row *
1876 (b - count)),
1877 vc->vc_video_erase_char,
1878 vc->vc_size_row * count);
1879 return 1;
1880 break;
1882 case SCROLL_WRAP_MOVE:
1883 if (b - t - count > 3 * vc->vc_rows >> 2) {
1884 if (t > 0)
1885 fbcon_bmove(vc, 0, 0, count, 0, t,
1886 vc->vc_cols);
1887 ywrap_up(vc, count);
1888 if (vc->vc_rows - b > 0)
1889 fbcon_bmove(vc, b - count, 0, b, 0,
1890 vc->vc_rows - b,
1891 vc->vc_cols);
1892 } else if (info->flags & FBINFO_READS_FAST)
1893 fbcon_bmove(vc, t + count, 0, t, 0,
1894 b - t - count, vc->vc_cols);
1895 else
1896 goto redraw_up;
1897 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1898 break;
1900 case SCROLL_PAN_REDRAW:
1901 if ((p->yscroll + count <=
1902 2 * (p->vrows - vc->vc_rows))
1903 && ((!scroll_partial && (b - t == vc->vc_rows))
1904 || (scroll_partial
1905 && (b - t - count >
1906 3 * vc->vc_rows >> 2)))) {
1907 if (t > 0)
1908 fbcon_redraw_move(vc, p, 0, t, count);
1909 ypan_up_redraw(vc, t, count);
1910 if (vc->vc_rows - b > 0)
1911 fbcon_redraw_move(vc, p, b,
1912 vc->vc_rows - b, b);
1913 } else
1914 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1915 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1916 break;
1918 case SCROLL_PAN_MOVE:
1919 if ((p->yscroll + count <=
1920 2 * (p->vrows - vc->vc_rows))
1921 && ((!scroll_partial && (b - t == vc->vc_rows))
1922 || (scroll_partial
1923 && (b - t - count >
1924 3 * vc->vc_rows >> 2)))) {
1925 if (t > 0)
1926 fbcon_bmove(vc, 0, 0, count, 0, t,
1927 vc->vc_cols);
1928 ypan_up(vc, count);
1929 if (vc->vc_rows - b > 0)
1930 fbcon_bmove(vc, b - count, 0, b, 0,
1931 vc->vc_rows - b,
1932 vc->vc_cols);
1933 } else if (info->flags & FBINFO_READS_FAST)
1934 fbcon_bmove(vc, t + count, 0, t, 0,
1935 b - t - count, vc->vc_cols);
1936 else
1937 goto redraw_up;
1938 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1939 break;
1941 case SCROLL_REDRAW:
1942 redraw_up:
1943 fbcon_redraw(vc, p, t, b - t - count,
1944 count * vc->vc_cols);
1945 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1946 scr_memsetw((unsigned short *) (vc->vc_origin +
1947 vc->vc_size_row *
1948 (b - count)),
1949 vc->vc_video_erase_char,
1950 vc->vc_size_row * count);
1951 return 1;
1953 break;
1955 case SM_DOWN:
1956 if (count > vc->vc_rows) /* Maximum realistic size */
1957 count = vc->vc_rows;
1958 if (logo_shown >= 0)
1959 goto redraw_down;
1960 switch (p->scrollmode) {
1961 case SCROLL_MOVE:
1962 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1963 -count);
1964 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1965 scr_memsetw((unsigned short *) (vc->vc_origin +
1966 vc->vc_size_row *
1968 vc->vc_video_erase_char,
1969 vc->vc_size_row * count);
1970 return 1;
1971 break;
1973 case SCROLL_WRAP_MOVE:
1974 if (b - t - count > 3 * vc->vc_rows >> 2) {
1975 if (vc->vc_rows - b > 0)
1976 fbcon_bmove(vc, b, 0, b - count, 0,
1977 vc->vc_rows - b,
1978 vc->vc_cols);
1979 ywrap_down(vc, count);
1980 if (t > 0)
1981 fbcon_bmove(vc, count, 0, 0, 0, t,
1982 vc->vc_cols);
1983 } else if (info->flags & FBINFO_READS_FAST)
1984 fbcon_bmove(vc, t, 0, t + count, 0,
1985 b - t - count, vc->vc_cols);
1986 else
1987 goto redraw_down;
1988 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1989 break;
1991 case SCROLL_PAN_MOVE:
1992 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1993 && ((!scroll_partial && (b - t == vc->vc_rows))
1994 || (scroll_partial
1995 && (b - t - count >
1996 3 * vc->vc_rows >> 2)))) {
1997 if (vc->vc_rows - b > 0)
1998 fbcon_bmove(vc, b, 0, b - count, 0,
1999 vc->vc_rows - b,
2000 vc->vc_cols);
2001 ypan_down(vc, count);
2002 if (t > 0)
2003 fbcon_bmove(vc, count, 0, 0, 0, t,
2004 vc->vc_cols);
2005 } else if (info->flags & FBINFO_READS_FAST)
2006 fbcon_bmove(vc, t, 0, t + count, 0,
2007 b - t - count, vc->vc_cols);
2008 else
2009 goto redraw_down;
2010 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2011 break;
2013 case SCROLL_PAN_REDRAW:
2014 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
2015 && ((!scroll_partial && (b - t == vc->vc_rows))
2016 || (scroll_partial
2017 && (b - t - count >
2018 3 * vc->vc_rows >> 2)))) {
2019 if (vc->vc_rows - b > 0)
2020 fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
2021 b - count);
2022 ypan_down_redraw(vc, t, count);
2023 if (t > 0)
2024 fbcon_redraw_move(vc, p, count, t, 0);
2025 } else
2026 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
2027 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2028 break;
2030 case SCROLL_REDRAW:
2031 redraw_down:
2032 fbcon_redraw(vc, p, b - 1, b - t - count,
2033 -count * vc->vc_cols);
2034 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2035 scr_memsetw((unsigned short *) (vc->vc_origin +
2036 vc->vc_size_row *
2038 vc->vc_video_erase_char,
2039 vc->vc_size_row * count);
2040 return 1;
2043 return 0;
2047 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
2048 int height, int width)
2050 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2051 struct display *p = &fb_display[vc->vc_num];
2053 if (fbcon_is_inactive(vc, info))
2054 return;
2056 if (!width || !height)
2057 return;
2059 /* Split blits that cross physical y_wrap case.
2060 * Pathological case involves 4 blits, better to use recursive
2061 * code rather than unrolled case
2063 * Recursive invocations don't need to erase the cursor over and
2064 * over again, so we use fbcon_bmove_rec()
2066 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2067 p->vrows - p->yscroll);
2070 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
2071 int dy, int dx, int height, int width, u_int y_break)
2073 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2074 struct fbcon_ops *ops = info->fbcon_par;
2075 u_int b;
2077 if (sy < y_break && sy + height > y_break) {
2078 b = y_break - sy;
2079 if (dy < sy) { /* Avoid trashing self */
2080 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2081 y_break);
2082 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2083 height - b, width, y_break);
2084 } else {
2085 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2086 height - b, width, y_break);
2087 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2088 y_break);
2090 return;
2093 if (dy < y_break && dy + height > y_break) {
2094 b = y_break - dy;
2095 if (dy < sy) { /* Avoid trashing self */
2096 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2097 y_break);
2098 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2099 height - b, width, y_break);
2100 } else {
2101 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2102 height - b, width, y_break);
2103 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2104 y_break);
2106 return;
2108 ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2109 height, width);
2112 static __inline__ void updatescrollmode(struct display *p,
2113 struct fb_info *info,
2114 struct vc_data *vc)
2116 struct fbcon_ops *ops = info->fbcon_par;
2117 int fh = vc->vc_font.height;
2118 int cap = info->flags;
2119 u16 t = 0;
2120 int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2121 info->fix.xpanstep);
2122 int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2123 int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2124 int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2125 info->var.xres_virtual);
2126 int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2127 divides(ypan, vc->vc_font.height) && vyres > yres;
2128 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2129 divides(ywrap, vc->vc_font.height) &&
2130 divides(vc->vc_font.height, vyres) &&
2131 divides(vc->vc_font.height, yres);
2132 int reading_fast = cap & FBINFO_READS_FAST;
2133 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2134 !(cap & FBINFO_HWACCEL_DISABLED);
2135 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2136 !(cap & FBINFO_HWACCEL_DISABLED);
2138 p->vrows = vyres/fh;
2139 if (yres > (fh * (vc->vc_rows + 1)))
2140 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2141 if ((yres % fh) && (vyres % fh < yres % fh))
2142 p->vrows--;
2144 if (good_wrap || good_pan) {
2145 if (reading_fast || fast_copyarea)
2146 p->scrollmode = good_wrap ?
2147 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2148 else
2149 p->scrollmode = good_wrap ? SCROLL_REDRAW :
2150 SCROLL_PAN_REDRAW;
2151 } else {
2152 if (reading_fast || (fast_copyarea && !fast_imageblit))
2153 p->scrollmode = SCROLL_MOVE;
2154 else
2155 p->scrollmode = SCROLL_REDRAW;
2159 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2160 unsigned int height)
2162 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2163 struct fbcon_ops *ops = info->fbcon_par;
2164 struct display *p = &fb_display[vc->vc_num];
2165 struct fb_var_screeninfo var = info->var;
2166 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2168 virt_w = FBCON_SWAP(ops->rotate, width, height);
2169 virt_h = FBCON_SWAP(ops->rotate, height, width);
2170 virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2171 vc->vc_font.height);
2172 virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2173 vc->vc_font.width);
2174 var.xres = virt_w * virt_fw;
2175 var.yres = virt_h * virt_fh;
2176 x_diff = info->var.xres - var.xres;
2177 y_diff = info->var.yres - var.yres;
2178 if (x_diff < 0 || x_diff > virt_fw ||
2179 y_diff < 0 || y_diff > virt_fh) {
2180 const struct fb_videomode *mode;
2182 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2183 mode = fb_find_best_mode(&var, &info->modelist);
2184 if (mode == NULL)
2185 return -EINVAL;
2186 display_to_var(&var, p);
2187 fb_videomode_to_var(&var, mode);
2189 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2190 return -EINVAL;
2192 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2193 if (CON_IS_VISIBLE(vc)) {
2194 var.activate = FB_ACTIVATE_NOW |
2195 FB_ACTIVATE_FORCE;
2196 fb_set_var(info, &var);
2198 var_to_display(p, &info->var, info);
2199 ops->var = info->var;
2201 updatescrollmode(p, info, vc);
2202 return 0;
2205 static int fbcon_switch(struct vc_data *vc)
2207 struct fb_info *info, *old_info = NULL;
2208 struct fbcon_ops *ops;
2209 struct display *p = &fb_display[vc->vc_num];
2210 struct fb_var_screeninfo var;
2211 int i, prev_console, charcnt = 256;
2213 info = registered_fb[con2fb_map[vc->vc_num]];
2214 ops = info->fbcon_par;
2216 if (softback_top) {
2217 if (softback_lines)
2218 fbcon_set_origin(vc);
2219 softback_top = softback_curr = softback_in = softback_buf;
2220 softback_lines = 0;
2221 fbcon_update_softback(vc);
2224 if (logo_shown >= 0) {
2225 struct vc_data *conp2 = vc_cons[logo_shown].d;
2227 if (conp2->vc_top == logo_lines
2228 && conp2->vc_bottom == conp2->vc_rows)
2229 conp2->vc_top = 0;
2230 logo_shown = FBCON_LOGO_CANSHOW;
2233 prev_console = ops->currcon;
2234 if (prev_console != -1)
2235 old_info = registered_fb[con2fb_map[prev_console]];
2237 * FIXME: If we have multiple fbdev's loaded, we need to
2238 * update all info->currcon. Perhaps, we can place this
2239 * in a centralized structure, but this might break some
2240 * drivers.
2242 * info->currcon = vc->vc_num;
2244 for (i = 0; i < FB_MAX; i++) {
2245 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
2246 struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2248 o->currcon = vc->vc_num;
2251 memset(&var, 0, sizeof(struct fb_var_screeninfo));
2252 display_to_var(&var, p);
2253 var.activate = FB_ACTIVATE_NOW;
2256 * make sure we don't unnecessarily trip the memcmp()
2257 * in fb_set_var()
2259 info->var.activate = var.activate;
2260 var.yoffset = info->var.yoffset;
2261 var.xoffset = info->var.xoffset;
2262 var.vmode = info->var.vmode;
2263 fb_set_var(info, &var);
2264 ops->var = info->var;
2266 if (old_info != NULL && (old_info != info ||
2267 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2268 if (info->fbops->fb_set_par)
2269 info->fbops->fb_set_par(info);
2271 if (old_info != info)
2272 fbcon_del_cursor_timer(old_info);
2275 if (fbcon_is_inactive(vc, info) ||
2276 ops->blank_state != FB_BLANK_UNBLANK)
2277 fbcon_del_cursor_timer(info);
2278 else
2279 fbcon_add_cursor_timer(info);
2281 set_blitting_type(vc, info);
2282 ops->cursor_reset = 1;
2284 if (ops->rotate_font && ops->rotate_font(info, vc)) {
2285 ops->rotate = FB_ROTATE_UR;
2286 set_blitting_type(vc, info);
2289 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2290 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2292 if (p->userfont)
2293 charcnt = FNTCHARCNT(vc->vc_font.data);
2295 if (charcnt > 256)
2296 vc->vc_complement_mask <<= 1;
2298 updatescrollmode(p, info, vc);
2300 switch (p->scrollmode) {
2301 case SCROLL_WRAP_MOVE:
2302 scrollback_phys_max = p->vrows - vc->vc_rows;
2303 break;
2304 case SCROLL_PAN_MOVE:
2305 case SCROLL_PAN_REDRAW:
2306 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2307 if (scrollback_phys_max < 0)
2308 scrollback_phys_max = 0;
2309 break;
2310 default:
2311 scrollback_phys_max = 0;
2312 break;
2315 scrollback_max = 0;
2316 scrollback_current = 0;
2318 if (!fbcon_is_inactive(vc, info)) {
2319 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2320 ops->update_start(info);
2323 fbcon_set_palette(vc, color_table);
2324 fbcon_clear_margins(vc, 0);
2326 if (logo_shown == FBCON_LOGO_DRAW) {
2328 logo_shown = fg_console;
2329 /* This is protected above by initmem_freed */
2330 fb_show_logo(info, ops->rotate);
2331 update_region(vc,
2332 vc->vc_origin + vc->vc_size_row * vc->vc_top,
2333 vc->vc_size_row * (vc->vc_bottom -
2334 vc->vc_top) / 2);
2335 return 0;
2337 return 1;
2340 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2341 int blank)
2343 struct fb_event event;
2345 if (blank) {
2346 unsigned short charmask = vc->vc_hi_font_mask ?
2347 0x1ff : 0xff;
2348 unsigned short oldc;
2350 oldc = vc->vc_video_erase_char;
2351 vc->vc_video_erase_char &= charmask;
2352 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2353 vc->vc_video_erase_char = oldc;
2357 event.info = info;
2358 event.data = &blank;
2359 fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
2362 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2364 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2365 struct fbcon_ops *ops = info->fbcon_par;
2367 if (mode_switch) {
2368 struct fb_var_screeninfo var = info->var;
2370 ops->graphics = 1;
2372 if (!blank) {
2373 if (info->fbops->fb_save_state)
2374 info->fbops->fb_save_state(info);
2375 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2376 fb_set_var(info, &var);
2377 ops->graphics = 0;
2378 ops->var = info->var;
2379 } else if (info->fbops->fb_restore_state)
2380 info->fbops->fb_restore_state(info);
2383 if (!fbcon_is_inactive(vc, info)) {
2384 if (ops->blank_state != blank) {
2385 ops->blank_state = blank;
2386 fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2387 ops->cursor_flash = (!blank);
2389 if (fb_blank(info, blank))
2390 fbcon_generic_blank(vc, info, blank);
2393 if (!blank)
2394 update_screen(vc);
2397 if (fbcon_is_inactive(vc, info) ||
2398 ops->blank_state != FB_BLANK_UNBLANK)
2399 fbcon_del_cursor_timer(info);
2400 else
2401 fbcon_add_cursor_timer(info);
2403 return 0;
2406 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2408 u8 *fontdata = vc->vc_font.data;
2409 u8 *data = font->data;
2410 int i, j;
2412 font->width = vc->vc_font.width;
2413 font->height = vc->vc_font.height;
2414 font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2415 if (!font->data)
2416 return 0;
2418 if (font->width <= 8) {
2419 j = vc->vc_font.height;
2420 for (i = 0; i < font->charcount; i++) {
2421 memcpy(data, fontdata, j);
2422 memset(data + j, 0, 32 - j);
2423 data += 32;
2424 fontdata += j;
2426 } else if (font->width <= 16) {
2427 j = vc->vc_font.height * 2;
2428 for (i = 0; i < font->charcount; i++) {
2429 memcpy(data, fontdata, j);
2430 memset(data + j, 0, 64 - j);
2431 data += 64;
2432 fontdata += j;
2434 } else if (font->width <= 24) {
2435 for (i = 0; i < font->charcount; i++) {
2436 for (j = 0; j < vc->vc_font.height; j++) {
2437 *data++ = fontdata[0];
2438 *data++ = fontdata[1];
2439 *data++ = fontdata[2];
2440 fontdata += sizeof(u32);
2442 memset(data, 0, 3 * (32 - j));
2443 data += 3 * (32 - j);
2445 } else {
2446 j = vc->vc_font.height * 4;
2447 for (i = 0; i < font->charcount; i++) {
2448 memcpy(data, fontdata, j);
2449 memset(data + j, 0, 128 - j);
2450 data += 128;
2451 fontdata += j;
2454 return 0;
2457 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2458 const u8 * data, int userfont)
2460 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2461 struct fbcon_ops *ops = info->fbcon_par;
2462 struct display *p = &fb_display[vc->vc_num];
2463 int resize;
2464 int cnt;
2465 char *old_data = NULL;
2467 if (CON_IS_VISIBLE(vc) && softback_lines)
2468 fbcon_set_origin(vc);
2470 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2471 if (p->userfont)
2472 old_data = vc->vc_font.data;
2473 if (userfont)
2474 cnt = FNTCHARCNT(data);
2475 else
2476 cnt = 256;
2477 vc->vc_font.data = (void *)(p->fontdata = data);
2478 if ((p->userfont = userfont))
2479 REFCOUNT(data)++;
2480 vc->vc_font.width = w;
2481 vc->vc_font.height = h;
2482 if (vc->vc_hi_font_mask && cnt == 256) {
2483 vc->vc_hi_font_mask = 0;
2484 if (vc->vc_can_do_color) {
2485 vc->vc_complement_mask >>= 1;
2486 vc->vc_s_complement_mask >>= 1;
2489 /* ++Edmund: reorder the attribute bits */
2490 if (vc->vc_can_do_color) {
2491 unsigned short *cp =
2492 (unsigned short *) vc->vc_origin;
2493 int count = vc->vc_screenbuf_size / 2;
2494 unsigned short c;
2495 for (; count > 0; count--, cp++) {
2496 c = scr_readw(cp);
2497 scr_writew(((c & 0xfe00) >> 1) |
2498 (c & 0xff), cp);
2500 c = vc->vc_video_erase_char;
2501 vc->vc_video_erase_char =
2502 ((c & 0xfe00) >> 1) | (c & 0xff);
2503 vc->vc_attr >>= 1;
2505 } else if (!vc->vc_hi_font_mask && cnt == 512) {
2506 vc->vc_hi_font_mask = 0x100;
2507 if (vc->vc_can_do_color) {
2508 vc->vc_complement_mask <<= 1;
2509 vc->vc_s_complement_mask <<= 1;
2512 /* ++Edmund: reorder the attribute bits */
2514 unsigned short *cp =
2515 (unsigned short *) vc->vc_origin;
2516 int count = vc->vc_screenbuf_size / 2;
2517 unsigned short c;
2518 for (; count > 0; count--, cp++) {
2519 unsigned short newc;
2520 c = scr_readw(cp);
2521 if (vc->vc_can_do_color)
2522 newc =
2523 ((c & 0xff00) << 1) | (c &
2524 0xff);
2525 else
2526 newc = c & ~0x100;
2527 scr_writew(newc, cp);
2529 c = vc->vc_video_erase_char;
2530 if (vc->vc_can_do_color) {
2531 vc->vc_video_erase_char =
2532 ((c & 0xff00) << 1) | (c & 0xff);
2533 vc->vc_attr <<= 1;
2534 } else
2535 vc->vc_video_erase_char = c & ~0x100;
2540 if (resize) {
2541 int cols, rows;
2543 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2544 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2545 cols /= w;
2546 rows /= h;
2547 vc_resize(vc, cols, rows);
2548 if (CON_IS_VISIBLE(vc) && softback_buf)
2549 fbcon_update_softback(vc);
2550 } else if (CON_IS_VISIBLE(vc)
2551 && vc->vc_mode == KD_TEXT) {
2552 fbcon_clear_margins(vc, 0);
2553 update_screen(vc);
2556 if (old_data && (--REFCOUNT(old_data) == 0))
2557 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2558 return 0;
2561 static int fbcon_copy_font(struct vc_data *vc, int con)
2563 struct display *od = &fb_display[con];
2564 struct console_font *f = &vc->vc_font;
2566 if (od->fontdata == f->data)
2567 return 0; /* already the same font... */
2568 return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2572 * User asked to set font; we are guaranteed that
2573 * a) width and height are in range 1..32
2574 * b) charcount does not exceed 512
2575 * but lets not assume that, since someone might someday want to use larger
2576 * fonts. And charcount of 512 is small for unicode support.
2578 * However, user space gives the font in 32 rows , regardless of
2579 * actual font height. So a new API is needed if support for larger fonts
2580 * is ever implemented.
2583 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2585 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2586 unsigned charcount = font->charcount;
2587 int w = font->width;
2588 int h = font->height;
2589 int size;
2590 int i, csum;
2591 u8 *new_data, *data = font->data;
2592 int pitch = (font->width+7) >> 3;
2594 /* Is there a reason why fbconsole couldn't handle any charcount >256?
2595 * If not this check should be changed to charcount < 256 */
2596 if (charcount != 256 && charcount != 512)
2597 return -EINVAL;
2599 /* Make sure drawing engine can handle the font */
2600 if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2601 !(info->pixmap.blit_y & (1 << (font->height - 1))))
2602 return -EINVAL;
2604 /* Make sure driver can handle the font length */
2605 if (fbcon_invalid_charcount(info, charcount))
2606 return -EINVAL;
2608 size = h * pitch * charcount;
2610 new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2612 if (!new_data)
2613 return -ENOMEM;
2615 new_data += FONT_EXTRA_WORDS * sizeof(int);
2616 FNTSIZE(new_data) = size;
2617 FNTCHARCNT(new_data) = charcount;
2618 REFCOUNT(new_data) = 0; /* usage counter */
2619 for (i=0; i< charcount; i++) {
2620 memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
2623 /* Since linux has a nice crc32 function use it for counting font
2624 * checksums. */
2625 csum = crc32(0, new_data, size);
2627 FNTSUM(new_data) = csum;
2628 /* Check if the same font is on some other console already */
2629 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2630 struct vc_data *tmp = vc_cons[i].d;
2632 if (fb_display[i].userfont &&
2633 fb_display[i].fontdata &&
2634 FNTSUM(fb_display[i].fontdata) == csum &&
2635 FNTSIZE(fb_display[i].fontdata) == size &&
2636 tmp->vc_font.width == w &&
2637 !memcmp(fb_display[i].fontdata, new_data, size)) {
2638 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2639 new_data = (u8 *)fb_display[i].fontdata;
2640 break;
2643 return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2646 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2648 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2649 const struct font_desc *f;
2651 if (!name)
2652 f = get_default_font(info->var.xres, info->var.yres,
2653 info->pixmap.blit_x, info->pixmap.blit_y);
2654 else if (!(f = find_font(name)))
2655 return -ENOENT;
2657 font->width = f->width;
2658 font->height = f->height;
2659 return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2662 static u16 palette_red[16];
2663 static u16 palette_green[16];
2664 static u16 palette_blue[16];
2666 static struct fb_cmap palette_cmap = {
2667 0, 16, palette_red, palette_green, palette_blue, NULL
2670 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2672 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2673 int i, j, k, depth;
2674 u8 val;
2676 if (fbcon_is_inactive(vc, info))
2677 return -EINVAL;
2679 if (!CON_IS_VISIBLE(vc))
2680 return 0;
2682 depth = fb_get_color_depth(&info->var, &info->fix);
2683 if (depth > 3) {
2684 for (i = j = 0; i < 16; i++) {
2685 k = table[i];
2686 val = vc->vc_palette[j++];
2687 palette_red[k] = (val << 8) | val;
2688 val = vc->vc_palette[j++];
2689 palette_green[k] = (val << 8) | val;
2690 val = vc->vc_palette[j++];
2691 palette_blue[k] = (val << 8) | val;
2693 palette_cmap.len = 16;
2694 palette_cmap.start = 0;
2696 * If framebuffer is capable of less than 16 colors,
2697 * use default palette of fbcon.
2699 } else
2700 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2702 return fb_set_cmap(&palette_cmap, info);
2705 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2707 unsigned long p;
2708 int line;
2710 if (vc->vc_num != fg_console || !softback_lines)
2711 return (u16 *) (vc->vc_origin + offset);
2712 line = offset / vc->vc_size_row;
2713 if (line >= softback_lines)
2714 return (u16 *) (vc->vc_origin + offset -
2715 softback_lines * vc->vc_size_row);
2716 p = softback_curr + offset;
2717 if (p >= softback_end)
2718 p += softback_buf - softback_end;
2719 return (u16 *) p;
2722 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2723 int *px, int *py)
2725 unsigned long ret;
2726 int x, y;
2728 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2729 unsigned long offset = (pos - vc->vc_origin) / 2;
2731 x = offset % vc->vc_cols;
2732 y = offset / vc->vc_cols;
2733 if (vc->vc_num == fg_console)
2734 y += softback_lines;
2735 ret = pos + (vc->vc_cols - x) * 2;
2736 } else if (vc->vc_num == fg_console && softback_lines) {
2737 unsigned long offset = pos - softback_curr;
2739 if (pos < softback_curr)
2740 offset += softback_end - softback_buf;
2741 offset /= 2;
2742 x = offset % vc->vc_cols;
2743 y = offset / vc->vc_cols;
2744 ret = pos + (vc->vc_cols - x) * 2;
2745 if (ret == softback_end)
2746 ret = softback_buf;
2747 if (ret == softback_in)
2748 ret = vc->vc_origin;
2749 } else {
2750 /* Should not happen */
2751 x = y = 0;
2752 ret = vc->vc_origin;
2754 if (px)
2755 *px = x;
2756 if (py)
2757 *py = y;
2758 return ret;
2761 /* As we might be inside of softback, we may work with non-contiguous buffer,
2762 that's why we have to use a separate routine. */
2763 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2765 while (cnt--) {
2766 u16 a = scr_readw(p);
2767 if (!vc->vc_can_do_color)
2768 a ^= 0x0800;
2769 else if (vc->vc_hi_font_mask == 0x100)
2770 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2771 (((a) & 0x0e00) << 4);
2772 else
2773 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2774 (((a) & 0x0700) << 4);
2775 scr_writew(a, p++);
2776 if (p == (u16 *) softback_end)
2777 p = (u16 *) softback_buf;
2778 if (p == (u16 *) softback_in)
2779 p = (u16 *) vc->vc_origin;
2783 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2785 struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2786 struct fbcon_ops *ops = info->fbcon_par;
2787 struct display *p = &fb_display[fg_console];
2788 int offset, limit, scrollback_old;
2790 if (softback_top) {
2791 if (vc->vc_num != fg_console)
2792 return 0;
2793 if (vc->vc_mode != KD_TEXT || !lines)
2794 return 0;
2795 if (logo_shown >= 0) {
2796 struct vc_data *conp2 = vc_cons[logo_shown].d;
2798 if (conp2->vc_top == logo_lines
2799 && conp2->vc_bottom == conp2->vc_rows)
2800 conp2->vc_top = 0;
2801 if (logo_shown == vc->vc_num) {
2802 unsigned long p, q;
2803 int i;
2805 p = softback_in;
2806 q = vc->vc_origin +
2807 logo_lines * vc->vc_size_row;
2808 for (i = 0; i < logo_lines; i++) {
2809 if (p == softback_top)
2810 break;
2811 if (p == softback_buf)
2812 p = softback_end;
2813 p -= vc->vc_size_row;
2814 q -= vc->vc_size_row;
2815 scr_memcpyw((u16 *) q, (u16 *) p,
2816 vc->vc_size_row);
2818 softback_in = softback_curr = p;
2819 update_region(vc, vc->vc_origin,
2820 logo_lines * vc->vc_cols);
2822 logo_shown = FBCON_LOGO_CANSHOW;
2824 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2825 fbcon_redraw_softback(vc, p, lines);
2826 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2827 return 0;
2830 if (!scrollback_phys_max)
2831 return -ENOSYS;
2833 scrollback_old = scrollback_current;
2834 scrollback_current -= lines;
2835 if (scrollback_current < 0)
2836 scrollback_current = 0;
2837 else if (scrollback_current > scrollback_max)
2838 scrollback_current = scrollback_max;
2839 if (scrollback_current == scrollback_old)
2840 return 0;
2842 if (fbcon_is_inactive(vc, info))
2843 return 0;
2845 fbcon_cursor(vc, CM_ERASE);
2847 offset = p->yscroll - scrollback_current;
2848 limit = p->vrows;
2849 switch (p->scrollmode) {
2850 case SCROLL_WRAP_MOVE:
2851 info->var.vmode |= FB_VMODE_YWRAP;
2852 break;
2853 case SCROLL_PAN_MOVE:
2854 case SCROLL_PAN_REDRAW:
2855 limit -= vc->vc_rows;
2856 info->var.vmode &= ~FB_VMODE_YWRAP;
2857 break;
2859 if (offset < 0)
2860 offset += limit;
2861 else if (offset >= limit)
2862 offset -= limit;
2864 ops->var.xoffset = 0;
2865 ops->var.yoffset = offset * vc->vc_font.height;
2866 ops->update_start(info);
2868 if (!scrollback_current)
2869 fbcon_cursor(vc, CM_DRAW);
2870 return 0;
2873 static int fbcon_set_origin(struct vc_data *vc)
2875 if (softback_lines)
2876 fbcon_scrolldelta(vc, softback_lines);
2877 return 0;
2880 static void fbcon_suspended(struct fb_info *info)
2882 struct vc_data *vc = NULL;
2883 struct fbcon_ops *ops = info->fbcon_par;
2885 if (!ops || ops->currcon < 0)
2886 return;
2887 vc = vc_cons[ops->currcon].d;
2889 /* Clear cursor, restore saved data */
2890 fbcon_cursor(vc, CM_ERASE);
2893 static void fbcon_resumed(struct fb_info *info)
2895 struct vc_data *vc;
2896 struct fbcon_ops *ops = info->fbcon_par;
2898 if (!ops || ops->currcon < 0)
2899 return;
2900 vc = vc_cons[ops->currcon].d;
2902 update_screen(vc);
2905 static void fbcon_modechanged(struct fb_info *info)
2907 struct fbcon_ops *ops = info->fbcon_par;
2908 struct vc_data *vc;
2909 struct display *p;
2910 int rows, cols;
2912 if (!ops || ops->currcon < 0)
2913 return;
2914 vc = vc_cons[ops->currcon].d;
2915 if (vc->vc_mode != KD_TEXT ||
2916 registered_fb[con2fb_map[ops->currcon]] != info)
2917 return;
2919 p = &fb_display[vc->vc_num];
2920 set_blitting_type(vc, info);
2922 if (CON_IS_VISIBLE(vc)) {
2923 var_to_display(p, &info->var, info);
2924 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2925 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2926 cols /= vc->vc_font.width;
2927 rows /= vc->vc_font.height;
2928 vc_resize(vc, cols, rows);
2929 updatescrollmode(p, info, vc);
2930 scrollback_max = 0;
2931 scrollback_current = 0;
2933 if (!fbcon_is_inactive(vc, info)) {
2934 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2935 ops->update_start(info);
2938 fbcon_set_palette(vc, color_table);
2939 update_screen(vc);
2940 if (softback_buf)
2941 fbcon_update_softback(vc);
2945 static void fbcon_set_all_vcs(struct fb_info *info)
2947 struct fbcon_ops *ops = info->fbcon_par;
2948 struct vc_data *vc;
2949 struct display *p;
2950 int i, rows, cols, fg = -1;
2952 if (!ops || ops->currcon < 0)
2953 return;
2955 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2956 vc = vc_cons[i].d;
2957 if (!vc || vc->vc_mode != KD_TEXT ||
2958 registered_fb[con2fb_map[i]] != info)
2959 continue;
2961 if (CON_IS_VISIBLE(vc)) {
2962 fg = i;
2963 continue;
2966 p = &fb_display[vc->vc_num];
2967 set_blitting_type(vc, info);
2968 var_to_display(p, &info->var, info);
2969 cols = FBCON_SWAP(p->rotate, info->var.xres, info->var.yres);
2970 rows = FBCON_SWAP(p->rotate, info->var.yres, info->var.xres);
2971 cols /= vc->vc_font.width;
2972 rows /= vc->vc_font.height;
2973 vc_resize(vc, cols, rows);
2976 if (fg != -1)
2977 fbcon_modechanged(info);
2980 static int fbcon_mode_deleted(struct fb_info *info,
2981 struct fb_videomode *mode)
2983 struct fb_info *fb_info;
2984 struct display *p;
2985 int i, j, found = 0;
2987 /* before deletion, ensure that mode is not in use */
2988 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2989 j = con2fb_map[i];
2990 if (j == -1)
2991 continue;
2992 fb_info = registered_fb[j];
2993 if (fb_info != info)
2994 continue;
2995 p = &fb_display[i];
2996 if (!p || !p->mode)
2997 continue;
2998 if (fb_mode_is_equal(p->mode, mode)) {
2999 found = 1;
3000 break;
3003 return found;
3006 static int fbcon_fb_unregistered(struct fb_info *info)
3008 int i, idx = info->node;
3010 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3011 if (con2fb_map[i] == idx)
3012 con2fb_map[i] = -1;
3015 if (idx == info_idx) {
3016 info_idx = -1;
3018 for (i = 0; i < FB_MAX; i++) {
3019 if (registered_fb[i] != NULL) {
3020 info_idx = i;
3021 break;
3026 if (info_idx != -1) {
3027 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3028 if (con2fb_map[i] == -1)
3029 con2fb_map[i] = info_idx;
3033 if (!num_registered_fb)
3034 unregister_con_driver(&fb_con);
3037 if (primary_device == idx)
3038 primary_device = -1;
3040 return 0;
3043 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3044 static int fbcon_select_primary(struct fb_info *info)
3046 int ret = 0;
3048 if (!map_override && primary_device == -1 &&
3049 fb_is_primary_device(info)) {
3050 int i, err;
3052 printk(KERN_INFO "fbcon: %s is primary device\n",
3053 info->fix.id);
3054 primary_device = info->node;
3056 if (!con_is_bound(&fb_con))
3057 goto done;
3059 printk(KERN_INFO "fbcon: Unbinding old driver\n");
3060 unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
3061 fbcon_is_default);
3062 info_idx = primary_device;
3064 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3065 con2fb_map_boot[i] = primary_device;
3066 con2fb_map[i] = primary_device;
3069 printk(KERN_INFO "fbcon: Selecting new driver\n");
3070 err = bind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
3071 fbcon_is_default);
3073 if (err) {
3074 for (i = first_fb_vc; i <= last_fb_vc; i++)
3075 con2fb_map[i] = -1;
3077 info_idx = -1;
3080 ret = 1;
3083 done:
3084 return ret;
3086 #else
3087 static inline int fbcon_select_primary(struct fb_info *info)
3089 return 0;
3091 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3093 static int fbcon_fb_registered(struct fb_info *info)
3095 int ret = 0, i, idx = info->node;
3097 if (fbcon_select_primary(info))
3098 goto done;
3101 if (info_idx == -1) {
3102 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3103 if (con2fb_map_boot[i] == idx) {
3104 info_idx = idx;
3105 break;
3109 if (info_idx != -1)
3110 ret = fbcon_takeover(1);
3111 } else {
3112 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3113 if (con2fb_map_boot[i] == idx)
3114 set_con2fb_map(i, idx, 0);
3118 done:
3119 return ret;
3122 static void fbcon_fb_blanked(struct fb_info *info, int blank)
3124 struct fbcon_ops *ops = info->fbcon_par;
3125 struct vc_data *vc;
3127 if (!ops || ops->currcon < 0)
3128 return;
3130 vc = vc_cons[ops->currcon].d;
3131 if (vc->vc_mode != KD_TEXT ||
3132 registered_fb[con2fb_map[ops->currcon]] != info)
3133 return;
3135 if (CON_IS_VISIBLE(vc)) {
3136 if (blank)
3137 do_blank_screen(0);
3138 else
3139 do_unblank_screen(0);
3141 ops->blank_state = blank;
3144 static void fbcon_new_modelist(struct fb_info *info)
3146 int i;
3147 struct vc_data *vc;
3148 struct fb_var_screeninfo var;
3149 const struct fb_videomode *mode;
3151 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3152 if (registered_fb[con2fb_map[i]] != info)
3153 continue;
3154 if (!fb_display[i].mode)
3155 continue;
3156 vc = vc_cons[i].d;
3157 display_to_var(&var, &fb_display[i]);
3158 mode = fb_find_nearest_mode(fb_display[i].mode,
3159 &info->modelist);
3160 fb_videomode_to_var(&var, mode);
3161 fbcon_set_disp(info, &var, vc->vc_num);
3165 static void fbcon_get_requirement(struct fb_info *info,
3166 struct fb_blit_caps *caps)
3168 struct vc_data *vc;
3169 struct display *p;
3171 if (caps->flags) {
3172 int i, charcnt;
3174 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3175 vc = vc_cons[i].d;
3176 if (vc && vc->vc_mode == KD_TEXT &&
3177 info->node == con2fb_map[i]) {
3178 p = &fb_display[i];
3179 caps->x |= 1 << (vc->vc_font.width - 1);
3180 caps->y |= 1 << (vc->vc_font.height - 1);
3181 charcnt = (p->userfont) ?
3182 FNTCHARCNT(p->fontdata) : 256;
3183 if (caps->len < charcnt)
3184 caps->len = charcnt;
3187 } else {
3188 vc = vc_cons[fg_console].d;
3190 if (vc && vc->vc_mode == KD_TEXT &&
3191 info->node == con2fb_map[fg_console]) {
3192 p = &fb_display[fg_console];
3193 caps->x = 1 << (vc->vc_font.width - 1);
3194 caps->y = 1 << (vc->vc_font.height - 1);
3195 caps->len = (p->userfont) ?
3196 FNTCHARCNT(p->fontdata) : 256;
3201 static int fbcon_event_notify(struct notifier_block *self,
3202 unsigned long action, void *data)
3204 struct fb_event *event = data;
3205 struct fb_info *info = event->info;
3206 struct fb_videomode *mode;
3207 struct fb_con2fbmap *con2fb;
3208 struct fb_blit_caps *caps;
3209 int ret = 0;
3212 * ignore all events except driver registration and deregistration
3213 * if fbcon is not active
3215 if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3216 action == FB_EVENT_FB_UNREGISTERED))
3217 goto done;
3219 switch(action) {
3220 case FB_EVENT_SUSPEND:
3221 fbcon_suspended(info);
3222 break;
3223 case FB_EVENT_RESUME:
3224 fbcon_resumed(info);
3225 break;
3226 case FB_EVENT_MODE_CHANGE:
3227 fbcon_modechanged(info);
3228 break;
3229 case FB_EVENT_MODE_CHANGE_ALL:
3230 fbcon_set_all_vcs(info);
3231 break;
3232 case FB_EVENT_MODE_DELETE:
3233 mode = event->data;
3234 ret = fbcon_mode_deleted(info, mode);
3235 break;
3236 case FB_EVENT_FB_REGISTERED:
3237 ret = fbcon_fb_registered(info);
3238 break;
3239 case FB_EVENT_FB_UNREGISTERED:
3240 ret = fbcon_fb_unregistered(info);
3241 break;
3242 case FB_EVENT_SET_CONSOLE_MAP:
3243 con2fb = event->data;
3244 ret = set_con2fb_map(con2fb->console - 1,
3245 con2fb->framebuffer, 1);
3246 break;
3247 case FB_EVENT_GET_CONSOLE_MAP:
3248 con2fb = event->data;
3249 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3250 break;
3251 case FB_EVENT_BLANK:
3252 fbcon_fb_blanked(info, *(int *)event->data);
3253 break;
3254 case FB_EVENT_NEW_MODELIST:
3255 fbcon_new_modelist(info);
3256 break;
3257 case FB_EVENT_GET_REQ:
3258 caps = event->data;
3259 fbcon_get_requirement(info, caps);
3260 break;
3263 done:
3264 return ret;
3268 * The console `switch' structure for the frame buffer based console
3271 static const struct consw fb_con = {
3272 .owner = THIS_MODULE,
3273 .con_startup = fbcon_startup,
3274 .con_init = fbcon_init,
3275 .con_deinit = fbcon_deinit,
3276 .con_clear = fbcon_clear,
3277 .con_putc = fbcon_putc,
3278 .con_putcs = fbcon_putcs,
3279 .con_cursor = fbcon_cursor,
3280 .con_scroll = fbcon_scroll,
3281 .con_bmove = fbcon_bmove,
3282 .con_switch = fbcon_switch,
3283 .con_blank = fbcon_blank,
3284 .con_font_set = fbcon_set_font,
3285 .con_font_get = fbcon_get_font,
3286 .con_font_default = fbcon_set_def_font,
3287 .con_font_copy = fbcon_copy_font,
3288 .con_set_palette = fbcon_set_palette,
3289 .con_scrolldelta = fbcon_scrolldelta,
3290 .con_set_origin = fbcon_set_origin,
3291 .con_invert_region = fbcon_invert_region,
3292 .con_screen_pos = fbcon_screen_pos,
3293 .con_getxy = fbcon_getxy,
3294 .con_resize = fbcon_resize,
3297 static struct notifier_block fbcon_event_notifier = {
3298 .notifier_call = fbcon_event_notify,
3301 static ssize_t store_rotate(struct device *device,
3302 struct device_attribute *attr, const char *buf,
3303 size_t count)
3305 struct fb_info *info;
3306 int rotate, idx;
3307 char **last = NULL;
3309 if (fbcon_has_exited)
3310 return count;
3312 acquire_console_sem();
3313 idx = con2fb_map[fg_console];
3315 if (idx == -1 || registered_fb[idx] == NULL)
3316 goto err;
3318 info = registered_fb[idx];
3319 rotate = simple_strtoul(buf, last, 0);
3320 fbcon_rotate(info, rotate);
3321 err:
3322 release_console_sem();
3323 return count;
3326 static ssize_t store_rotate_all(struct device *device,
3327 struct device_attribute *attr,const char *buf,
3328 size_t count)
3330 struct fb_info *info;
3331 int rotate, idx;
3332 char **last = NULL;
3334 if (fbcon_has_exited)
3335 return count;
3337 acquire_console_sem();
3338 idx = con2fb_map[fg_console];
3340 if (idx == -1 || registered_fb[idx] == NULL)
3341 goto err;
3343 info = registered_fb[idx];
3344 rotate = simple_strtoul(buf, last, 0);
3345 fbcon_rotate_all(info, rotate);
3346 err:
3347 release_console_sem();
3348 return count;
3351 static ssize_t show_rotate(struct device *device,
3352 struct device_attribute *attr,char *buf)
3354 struct fb_info *info;
3355 int rotate = 0, idx;
3357 if (fbcon_has_exited)
3358 return 0;
3360 acquire_console_sem();
3361 idx = con2fb_map[fg_console];
3363 if (idx == -1 || registered_fb[idx] == NULL)
3364 goto err;
3366 info = registered_fb[idx];
3367 rotate = fbcon_get_rotate(info);
3368 err:
3369 release_console_sem();
3370 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3373 static ssize_t show_cursor_blink(struct device *device,
3374 struct device_attribute *attr, char *buf)
3376 struct fb_info *info;
3377 struct fbcon_ops *ops;
3378 int idx, blink = -1;
3380 if (fbcon_has_exited)
3381 return 0;
3383 acquire_console_sem();
3384 idx = con2fb_map[fg_console];
3386 if (idx == -1 || registered_fb[idx] == NULL)
3387 goto err;
3389 info = registered_fb[idx];
3390 ops = info->fbcon_par;
3392 if (!ops)
3393 goto err;
3395 blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3396 err:
3397 release_console_sem();
3398 return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3401 static ssize_t store_cursor_blink(struct device *device,
3402 struct device_attribute *attr,
3403 const char *buf, size_t count)
3405 struct fb_info *info;
3406 int blink, idx;
3407 char **last = NULL;
3409 if (fbcon_has_exited)
3410 return count;
3412 acquire_console_sem();
3413 idx = con2fb_map[fg_console];
3415 if (idx == -1 || registered_fb[idx] == NULL)
3416 goto err;
3418 info = registered_fb[idx];
3420 if (!info->fbcon_par)
3421 goto err;
3423 blink = simple_strtoul(buf, last, 0);
3425 if (blink) {
3426 fbcon_cursor_noblink = 0;
3427 fbcon_add_cursor_timer(info);
3428 } else {
3429 fbcon_cursor_noblink = 1;
3430 fbcon_del_cursor_timer(info);
3433 err:
3434 release_console_sem();
3435 return count;
3438 static struct device_attribute device_attrs[] = {
3439 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3440 __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3441 __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3442 store_cursor_blink),
3445 static int fbcon_init_device(void)
3447 int i, error = 0;
3449 fbcon_has_sysfs = 1;
3451 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3452 error = device_create_file(fbcon_device, &device_attrs[i]);
3454 if (error)
3455 break;
3458 if (error) {
3459 while (--i >= 0)
3460 device_remove_file(fbcon_device, &device_attrs[i]);
3462 fbcon_has_sysfs = 0;
3465 return 0;
3468 static void fbcon_start(void)
3470 if (num_registered_fb) {
3471 int i;
3473 acquire_console_sem();
3475 for (i = 0; i < FB_MAX; i++) {
3476 if (registered_fb[i] != NULL) {
3477 info_idx = i;
3478 break;
3482 release_console_sem();
3483 fbcon_takeover(0);
3487 static void fbcon_exit(void)
3489 struct fb_info *info;
3490 int i, j, mapped;
3492 if (fbcon_has_exited)
3493 return;
3495 #ifdef CONFIG_ATARI
3496 free_irq(IRQ_AUTO_4, fb_vbl_handler);
3497 #endif
3498 #ifdef CONFIG_MAC
3499 if (MACH_IS_MAC && vbl_detected)
3500 free_irq(IRQ_MAC_VBL, fb_vbl_handler);
3501 #endif
3503 kfree((void *)softback_buf);
3504 softback_buf = 0UL;
3506 for (i = 0; i < FB_MAX; i++) {
3507 mapped = 0;
3508 info = registered_fb[i];
3510 if (info == NULL)
3511 continue;
3513 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3514 if (con2fb_map[j] == i)
3515 mapped = 1;
3518 if (mapped) {
3519 if (info->fbops->fb_release)
3520 info->fbops->fb_release(info, 0);
3521 module_put(info->fbops->owner);
3523 if (info->fbcon_par) {
3524 struct fbcon_ops *ops = info->fbcon_par;
3526 fbcon_del_cursor_timer(info);
3527 kfree(ops->cursor_src);
3528 kfree(info->fbcon_par);
3529 info->fbcon_par = NULL;
3532 if (info->queue.func == fb_flashcursor)
3533 info->queue.func = NULL;
3537 fbcon_has_exited = 1;
3540 static int __init fb_console_init(void)
3542 int i;
3544 acquire_console_sem();
3545 fb_register_client(&fbcon_event_notifier);
3546 fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), "fbcon");
3548 if (IS_ERR(fbcon_device)) {
3549 printk(KERN_WARNING "Unable to create device "
3550 "for fbcon; errno = %ld\n",
3551 PTR_ERR(fbcon_device));
3552 fbcon_device = NULL;
3553 } else
3554 fbcon_init_device();
3556 for (i = 0; i < MAX_NR_CONSOLES; i++)
3557 con2fb_map[i] = -1;
3559 release_console_sem();
3560 fbcon_start();
3561 return 0;
3564 module_init(fb_console_init);
3566 #ifdef MODULE
3568 static void __exit fbcon_deinit_device(void)
3570 int i;
3572 if (fbcon_has_sysfs) {
3573 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3574 device_remove_file(fbcon_device, &device_attrs[i]);
3576 fbcon_has_sysfs = 0;
3580 static void __exit fb_console_exit(void)
3582 acquire_console_sem();
3583 fb_unregister_client(&fbcon_event_notifier);
3584 fbcon_deinit_device();
3585 device_destroy(fb_class, MKDEV(0, 0));
3586 fbcon_exit();
3587 release_console_sem();
3588 unregister_con_driver(&fb_con);
3591 module_exit(fb_console_exit);
3593 #endif
3595 MODULE_LICENSE("GPL");