vt: fix background color on line feed
[linux-2.6/libata-dev.git] / drivers / video / console / fbcon.c
blobad31983b43eb6a169a100b2846e96f1554e32b1b
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 #ifdef CONFIG_ATARI
82 #include <asm/atariints.h>
83 #endif
84 #ifdef CONFIG_MAC
85 #include <asm/macints.h>
86 #endif
87 #if defined(__mc68000__)
88 #include <asm/machdep.h>
89 #include <asm/setup.h>
90 #endif
92 #include "fbcon.h"
94 #ifdef FBCONDEBUG
95 # define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __func__ , ## args)
96 #else
97 # define DPRINTK(fmt, args...)
98 #endif
100 enum {
101 FBCON_LOGO_CANSHOW = -1, /* the logo can be shown */
102 FBCON_LOGO_DRAW = -2, /* draw the logo to a console */
103 FBCON_LOGO_DONTSHOW = -3 /* do not show the logo */
106 static struct display fb_display[MAX_NR_CONSOLES];
108 static signed char con2fb_map[MAX_NR_CONSOLES];
109 static signed char con2fb_map_boot[MAX_NR_CONSOLES];
110 #ifndef MODULE
111 static int logo_height;
112 #endif
113 static int logo_lines;
114 /* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO
115 enums. */
116 static int logo_shown = FBCON_LOGO_CANSHOW;
117 /* Software scrollback */
118 static int fbcon_softback_size = 32768;
119 static unsigned long softback_buf, softback_curr;
120 static unsigned long softback_in;
121 static unsigned long softback_top, softback_end;
122 static int softback_lines;
123 /* console mappings */
124 static int first_fb_vc;
125 static int last_fb_vc = MAX_NR_CONSOLES - 1;
126 static int fbcon_is_default = 1;
127 static int fbcon_has_exited;
128 static int primary_device = -1;
130 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
131 static int map_override;
133 static inline void fbcon_map_override(void)
135 map_override = 1;
137 #else
138 static inline void fbcon_map_override(void)
141 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY */
143 /* font data */
144 static char fontname[40];
146 /* current fb_info */
147 static int info_idx = -1;
149 /* console rotation */
150 static int initial_rotation;
151 static int fbcon_has_sysfs;
153 static const struct consw fb_con;
155 #define CM_SOFTBACK (8)
157 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
159 static int fbcon_set_origin(struct vc_data *);
161 #define CURSOR_DRAW_DELAY (1)
163 /* # VBL ints between cursor state changes */
164 #define ATARI_CURSOR_BLINK_RATE (42)
165 #define MAC_CURSOR_BLINK_RATE (32)
166 #define DEFAULT_CURSOR_BLINK_RATE (20)
168 static int vbl_cursor_cnt;
169 static int fbcon_cursor_noblink;
171 #define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1)
174 * Interface used by the world
177 static const char *fbcon_startup(void);
178 static void fbcon_init(struct vc_data *vc, int init);
179 static void fbcon_deinit(struct vc_data *vc);
180 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
181 int width);
182 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
183 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
184 int count, int ypos, int xpos);
185 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
186 static void fbcon_cursor(struct vc_data *vc, int mode);
187 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
188 int count);
189 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
190 int height, int width);
191 static int fbcon_switch(struct vc_data *vc);
192 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
193 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
194 static int fbcon_scrolldelta(struct vc_data *vc, int lines);
197 * Internal routines
199 static __inline__ void ywrap_up(struct vc_data *vc, int count);
200 static __inline__ void ywrap_down(struct vc_data *vc, int count);
201 static __inline__ void ypan_up(struct vc_data *vc, int count);
202 static __inline__ void ypan_down(struct vc_data *vc, int count);
203 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
204 int dy, int dx, int height, int width, u_int y_break);
205 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
206 int unit);
207 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
208 int line, int count, int dy);
209 static void fbcon_modechanged(struct fb_info *info);
210 static void fbcon_set_all_vcs(struct fb_info *info);
211 static void fbcon_start(void);
212 static void fbcon_exit(void);
213 static struct device *fbcon_device;
215 #ifdef CONFIG_MAC
217 * On the Macintoy, there may or may not be a working VBL int. We need to probe
219 static int vbl_detected;
221 static irqreturn_t fb_vbl_detect(int irq, void *dummy)
223 vbl_detected++;
224 return IRQ_HANDLED;
226 #endif
228 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
229 static inline void fbcon_set_rotation(struct fb_info *info)
231 struct fbcon_ops *ops = info->fbcon_par;
233 if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
234 ops->p->con_rotate < 4)
235 ops->rotate = ops->p->con_rotate;
236 else
237 ops->rotate = 0;
240 static void fbcon_rotate(struct fb_info *info, u32 rotate)
242 struct fbcon_ops *ops= info->fbcon_par;
243 struct fb_info *fb_info;
245 if (!ops || ops->currcon == -1)
246 return;
248 fb_info = registered_fb[con2fb_map[ops->currcon]];
250 if (info == fb_info) {
251 struct display *p = &fb_display[ops->currcon];
253 if (rotate < 4)
254 p->con_rotate = rotate;
255 else
256 p->con_rotate = 0;
258 fbcon_modechanged(info);
262 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
264 struct fbcon_ops *ops = info->fbcon_par;
265 struct vc_data *vc;
266 struct display *p;
267 int i;
269 if (!ops || ops->currcon < 0 || rotate > 3)
270 return;
272 for (i = first_fb_vc; i <= last_fb_vc; i++) {
273 vc = vc_cons[i].d;
274 if (!vc || vc->vc_mode != KD_TEXT ||
275 registered_fb[con2fb_map[i]] != info)
276 continue;
278 p = &fb_display[vc->vc_num];
279 p->con_rotate = rotate;
282 fbcon_set_all_vcs(info);
284 #else
285 static inline void fbcon_set_rotation(struct fb_info *info)
287 struct fbcon_ops *ops = info->fbcon_par;
289 ops->rotate = FB_ROTATE_UR;
292 static void fbcon_rotate(struct fb_info *info, u32 rotate)
294 return;
297 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
299 return;
301 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
303 static int fbcon_get_rotate(struct fb_info *info)
305 struct fbcon_ops *ops = info->fbcon_par;
307 return (ops) ? ops->rotate : 0;
310 static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
312 struct fbcon_ops *ops = info->fbcon_par;
314 return (info->state != FBINFO_STATE_RUNNING ||
315 vc->vc_mode != KD_TEXT || ops->graphics);
318 static inline int get_color(struct vc_data *vc, struct fb_info *info,
319 u16 c, int is_fg)
321 int depth = fb_get_color_depth(&info->var, &info->fix);
322 int color = 0;
324 if (console_blanked) {
325 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
327 c = vc->vc_video_erase_char & charmask;
330 if (depth != 1)
331 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
332 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
334 switch (depth) {
335 case 1:
337 int col = mono_col(info);
338 /* 0 or 1 */
339 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
340 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
342 if (console_blanked)
343 fg = bg;
345 color = (is_fg) ? fg : bg;
346 break;
348 case 2:
350 * Scale down 16-colors to 4 colors. Default 4-color palette
351 * is grayscale. However, simply dividing the values by 4
352 * will not work, as colors 1, 2 and 3 will be scaled-down
353 * to zero rendering them invisible. So empirically convert
354 * colors to a sane 4-level grayscale.
356 switch (color) {
357 case 0:
358 color = 0; /* black */
359 break;
360 case 1 ... 6:
361 color = 2; /* white */
362 break;
363 case 7 ... 8:
364 color = 1; /* gray */
365 break;
366 default:
367 color = 3; /* intense white */
368 break;
370 break;
371 case 3:
373 * Last 8 entries of default 16-color palette is a more intense
374 * version of the first 8 (i.e., same chrominance, different
375 * luminance).
377 color &= 7;
378 break;
382 return color;
385 static void fbcon_update_softback(struct vc_data *vc)
387 int l = fbcon_softback_size / vc->vc_size_row;
389 if (l > 5)
390 softback_end = softback_buf + l * vc->vc_size_row;
391 else
392 /* Smaller scrollback makes no sense, and 0 would screw
393 the operation totally */
394 softback_top = 0;
397 static void fb_flashcursor(struct work_struct *work)
399 struct fb_info *info = container_of(work, struct fb_info, queue);
400 struct fbcon_ops *ops = info->fbcon_par;
401 struct display *p;
402 struct vc_data *vc = NULL;
403 int c;
404 int mode;
406 acquire_console_sem();
407 if (ops && ops->currcon != -1)
408 vc = vc_cons[ops->currcon].d;
410 if (!vc || !CON_IS_VISIBLE(vc) ||
411 registered_fb[con2fb_map[vc->vc_num]] != info ||
412 vc->vc_deccm != 1) {
413 release_console_sem();
414 return;
417 p = &fb_display[vc->vc_num];
418 c = scr_readw((u16 *) vc->vc_pos);
419 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
420 CM_ERASE : CM_DRAW;
421 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
422 get_color(vc, info, c, 0));
423 release_console_sem();
426 #if defined(CONFIG_ATARI) || defined(CONFIG_MAC)
427 static int cursor_blink_rate;
428 static irqreturn_t fb_vbl_handler(int irq, void *dev_id)
430 struct fb_info *info = dev_id;
432 if (vbl_cursor_cnt && --vbl_cursor_cnt == 0) {
433 schedule_work(&info->queue);
434 vbl_cursor_cnt = cursor_blink_rate;
436 return IRQ_HANDLED;
438 #endif
440 static void cursor_timer_handler(unsigned long dev_addr)
442 struct fb_info *info = (struct fb_info *) dev_addr;
443 struct fbcon_ops *ops = info->fbcon_par;
445 schedule_work(&info->queue);
446 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
449 static void fbcon_add_cursor_timer(struct fb_info *info)
451 struct fbcon_ops *ops = info->fbcon_par;
453 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
454 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
455 !fbcon_cursor_noblink) {
456 if (!info->queue.func)
457 INIT_WORK(&info->queue, fb_flashcursor);
459 init_timer(&ops->cursor_timer);
460 ops->cursor_timer.function = cursor_timer_handler;
461 ops->cursor_timer.expires = jiffies + HZ / 5;
462 ops->cursor_timer.data = (unsigned long ) info;
463 add_timer(&ops->cursor_timer);
464 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
468 static void fbcon_del_cursor_timer(struct fb_info *info)
470 struct fbcon_ops *ops = info->fbcon_par;
472 if (info->queue.func == fb_flashcursor &&
473 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
474 del_timer_sync(&ops->cursor_timer);
475 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
479 #ifndef MODULE
480 static int __init fb_console_setup(char *this_opt)
482 char *options;
483 int i, j;
485 if (!this_opt || !*this_opt)
486 return 1;
488 while ((options = strsep(&this_opt, ",")) != NULL) {
489 if (!strncmp(options, "font:", 5))
490 strcpy(fontname, options + 5);
492 if (!strncmp(options, "scrollback:", 11)) {
493 options += 11;
494 if (*options) {
495 fbcon_softback_size = simple_strtoul(options, &options, 0);
496 if (*options == 'k' || *options == 'K') {
497 fbcon_softback_size *= 1024;
498 options++;
500 if (*options != ',')
501 return 1;
502 options++;
503 } else
504 return 1;
507 if (!strncmp(options, "map:", 4)) {
508 options += 4;
509 if (*options) {
510 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
511 if (!options[j])
512 j = 0;
513 con2fb_map_boot[i] =
514 (options[j++]-'0') % FB_MAX;
517 fbcon_map_override();
520 return 1;
523 if (!strncmp(options, "vc:", 3)) {
524 options += 3;
525 if (*options)
526 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
527 if (first_fb_vc < 0)
528 first_fb_vc = 0;
529 if (*options++ == '-')
530 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
531 fbcon_is_default = 0;
534 if (!strncmp(options, "rotate:", 7)) {
535 options += 7;
536 if (*options)
537 initial_rotation = simple_strtoul(options, &options, 0);
538 if (initial_rotation > 3)
539 initial_rotation = 0;
542 return 1;
545 __setup("fbcon=", fb_console_setup);
546 #endif
548 static int search_fb_in_map(int idx)
550 int i, retval = 0;
552 for (i = first_fb_vc; i <= last_fb_vc; i++) {
553 if (con2fb_map[i] == idx)
554 retval = 1;
556 return retval;
559 static int search_for_mapped_con(void)
561 int i, retval = 0;
563 for (i = first_fb_vc; i <= last_fb_vc; i++) {
564 if (con2fb_map[i] != -1)
565 retval = 1;
567 return retval;
570 static int fbcon_takeover(int show_logo)
572 int err, i;
574 if (!num_registered_fb)
575 return -ENODEV;
577 if (!show_logo)
578 logo_shown = FBCON_LOGO_DONTSHOW;
580 for (i = first_fb_vc; i <= last_fb_vc; i++)
581 con2fb_map[i] = info_idx;
583 err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
584 fbcon_is_default);
586 if (err) {
587 for (i = first_fb_vc; i <= last_fb_vc; i++) {
588 con2fb_map[i] = -1;
590 info_idx = -1;
593 return err;
596 #ifdef MODULE
597 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
598 int cols, int rows, int new_cols, int new_rows)
600 logo_shown = FBCON_LOGO_DONTSHOW;
602 #else
603 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
604 int cols, int rows, int new_cols, int new_rows)
606 /* Need to make room for the logo */
607 struct fbcon_ops *ops = info->fbcon_par;
608 int cnt, erase = vc->vc_video_erase_char, step;
609 unsigned short *save = NULL, *r, *q;
611 if (info->flags & FBINFO_MODULE) {
612 logo_shown = FBCON_LOGO_DONTSHOW;
613 return;
617 * remove underline attribute from erase character
618 * if black and white framebuffer.
620 if (fb_get_color_depth(&info->var, &info->fix) == 1)
621 erase &= ~0x400;
622 logo_height = fb_prepare_logo(info, ops->rotate);
623 logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
624 q = (unsigned short *) (vc->vc_origin +
625 vc->vc_size_row * rows);
626 step = logo_lines * cols;
627 for (r = q - logo_lines * cols; r < q; r++)
628 if (scr_readw(r) != vc->vc_video_erase_char)
629 break;
630 if (r != q && new_rows >= rows + logo_lines) {
631 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
632 if (save) {
633 int i = cols < new_cols ? cols : new_cols;
634 scr_memsetw(save, erase, logo_lines * new_cols * 2);
635 r = q - step;
636 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
637 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
638 r = q;
641 if (r == q) {
642 /* We can scroll screen down */
643 r = q - step - cols;
644 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
645 scr_memcpyw(r + step, r, vc->vc_size_row);
646 r -= cols;
648 if (!save) {
649 int lines;
650 if (vc->vc_y + logo_lines >= rows)
651 lines = rows - vc->vc_y - 1;
652 else
653 lines = logo_lines;
654 vc->vc_y += lines;
655 vc->vc_pos += lines * vc->vc_size_row;
658 scr_memsetw((unsigned short *) vc->vc_origin,
659 erase,
660 vc->vc_size_row * logo_lines);
662 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
663 fbcon_clear_margins(vc, 0);
664 update_screen(vc);
667 if (save) {
668 q = (unsigned short *) (vc->vc_origin +
669 vc->vc_size_row *
670 rows);
671 scr_memcpyw(q, save, logo_lines * new_cols * 2);
672 vc->vc_y += logo_lines;
673 vc->vc_pos += logo_lines * vc->vc_size_row;
674 kfree(save);
677 if (logo_lines > vc->vc_bottom) {
678 logo_shown = FBCON_LOGO_CANSHOW;
679 printk(KERN_INFO
680 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
681 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
682 logo_shown = FBCON_LOGO_DRAW;
683 vc->vc_top = logo_lines;
686 #endif /* MODULE */
688 #ifdef CONFIG_FB_TILEBLITTING
689 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
691 struct fbcon_ops *ops = info->fbcon_par;
693 ops->p = &fb_display[vc->vc_num];
695 if ((info->flags & FBINFO_MISC_TILEBLITTING))
696 fbcon_set_tileops(vc, info);
697 else {
698 fbcon_set_rotation(info);
699 fbcon_set_bitops(ops);
703 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
705 int err = 0;
707 if (info->flags & FBINFO_MISC_TILEBLITTING &&
708 info->tileops->fb_get_tilemax(info) < charcount)
709 err = 1;
711 return err;
713 #else
714 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
716 struct fbcon_ops *ops = info->fbcon_par;
718 info->flags &= ~FBINFO_MISC_TILEBLITTING;
719 ops->p = &fb_display[vc->vc_num];
720 fbcon_set_rotation(info);
721 fbcon_set_bitops(ops);
724 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
726 return 0;
729 #endif /* CONFIG_MISC_TILEBLITTING */
732 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
733 int unit, int oldidx)
735 struct fbcon_ops *ops = NULL;
736 int err = 0;
738 if (!try_module_get(info->fbops->owner))
739 err = -ENODEV;
741 if (!err && info->fbops->fb_open &&
742 info->fbops->fb_open(info, 0))
743 err = -ENODEV;
745 if (!err) {
746 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
747 if (!ops)
748 err = -ENOMEM;
751 if (!err) {
752 info->fbcon_par = ops;
754 if (vc)
755 set_blitting_type(vc, info);
758 if (err) {
759 con2fb_map[unit] = oldidx;
760 module_put(info->fbops->owner);
763 return err;
766 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
767 struct fb_info *newinfo, int unit,
768 int oldidx, int found)
770 struct fbcon_ops *ops = oldinfo->fbcon_par;
771 int err = 0;
773 if (oldinfo->fbops->fb_release &&
774 oldinfo->fbops->fb_release(oldinfo, 0)) {
775 con2fb_map[unit] = oldidx;
776 if (!found && newinfo->fbops->fb_release)
777 newinfo->fbops->fb_release(newinfo, 0);
778 if (!found)
779 module_put(newinfo->fbops->owner);
780 err = -ENODEV;
783 if (!err) {
784 fbcon_del_cursor_timer(oldinfo);
785 kfree(ops->cursor_state.mask);
786 kfree(ops->cursor_data);
787 kfree(ops->fontbuffer);
788 kfree(oldinfo->fbcon_par);
789 oldinfo->fbcon_par = NULL;
790 module_put(oldinfo->fbops->owner);
792 If oldinfo and newinfo are driving the same hardware,
793 the fb_release() method of oldinfo may attempt to
794 restore the hardware state. This will leave the
795 newinfo in an undefined state. Thus, a call to
796 fb_set_par() may be needed for the newinfo.
798 if (newinfo->fbops->fb_set_par)
799 newinfo->fbops->fb_set_par(newinfo);
802 return err;
805 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
806 int unit, int show_logo)
808 struct fbcon_ops *ops = info->fbcon_par;
810 ops->currcon = fg_console;
812 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT))
813 info->fbops->fb_set_par(info);
815 ops->flags |= FBCON_FLAGS_INIT;
816 ops->graphics = 0;
817 fbcon_set_disp(info, &info->var, unit);
819 if (show_logo) {
820 struct vc_data *fg_vc = vc_cons[fg_console].d;
821 struct fb_info *fg_info =
822 registered_fb[con2fb_map[fg_console]];
824 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
825 fg_vc->vc_rows, fg_vc->vc_cols,
826 fg_vc->vc_rows);
829 update_screen(vc_cons[fg_console].d);
833 * set_con2fb_map - map console to frame buffer device
834 * @unit: virtual console number to map
835 * @newidx: frame buffer index to map virtual console to
836 * @user: user request
838 * Maps a virtual console @unit to a frame buffer device
839 * @newidx.
841 static int set_con2fb_map(int unit, int newidx, int user)
843 struct vc_data *vc = vc_cons[unit].d;
844 int oldidx = con2fb_map[unit];
845 struct fb_info *info = registered_fb[newidx];
846 struct fb_info *oldinfo = NULL;
847 int found, err = 0;
849 if (oldidx == newidx)
850 return 0;
852 if (!info || fbcon_has_exited)
853 return -EINVAL;
855 if (!err && !search_for_mapped_con()) {
856 info_idx = newidx;
857 return fbcon_takeover(0);
860 if (oldidx != -1)
861 oldinfo = registered_fb[oldidx];
863 found = search_fb_in_map(newidx);
865 acquire_console_sem();
866 con2fb_map[unit] = newidx;
867 if (!err && !found)
868 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
872 * If old fb is not mapped to any of the consoles,
873 * fbcon should release it.
875 if (!err && oldinfo && !search_fb_in_map(oldidx))
876 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
877 found);
879 if (!err) {
880 int show_logo = (fg_console == 0 && !user &&
881 logo_shown != FBCON_LOGO_DONTSHOW);
883 if (!found)
884 fbcon_add_cursor_timer(info);
885 con2fb_map_boot[unit] = newidx;
886 con2fb_init_display(vc, info, unit, show_logo);
889 if (!search_fb_in_map(info_idx))
890 info_idx = newidx;
892 release_console_sem();
893 return err;
897 * Low Level Operations
899 /* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
900 static int var_to_display(struct display *disp,
901 struct fb_var_screeninfo *var,
902 struct fb_info *info)
904 disp->xres_virtual = var->xres_virtual;
905 disp->yres_virtual = var->yres_virtual;
906 disp->bits_per_pixel = var->bits_per_pixel;
907 disp->grayscale = var->grayscale;
908 disp->nonstd = var->nonstd;
909 disp->accel_flags = var->accel_flags;
910 disp->height = var->height;
911 disp->width = var->width;
912 disp->red = var->red;
913 disp->green = var->green;
914 disp->blue = var->blue;
915 disp->transp = var->transp;
916 disp->rotate = var->rotate;
917 disp->mode = fb_match_mode(var, &info->modelist);
918 if (disp->mode == NULL)
919 /* This should not happen */
920 return -EINVAL;
921 return 0;
924 static void display_to_var(struct fb_var_screeninfo *var,
925 struct display *disp)
927 fb_videomode_to_var(var, disp->mode);
928 var->xres_virtual = disp->xres_virtual;
929 var->yres_virtual = disp->yres_virtual;
930 var->bits_per_pixel = disp->bits_per_pixel;
931 var->grayscale = disp->grayscale;
932 var->nonstd = disp->nonstd;
933 var->accel_flags = disp->accel_flags;
934 var->height = disp->height;
935 var->width = disp->width;
936 var->red = disp->red;
937 var->green = disp->green;
938 var->blue = disp->blue;
939 var->transp = disp->transp;
940 var->rotate = disp->rotate;
943 static const char *fbcon_startup(void)
945 const char *display_desc = "frame buffer device";
946 struct display *p = &fb_display[fg_console];
947 struct vc_data *vc = vc_cons[fg_console].d;
948 const struct font_desc *font = NULL;
949 struct module *owner;
950 struct fb_info *info = NULL;
951 struct fbcon_ops *ops;
952 int rows, cols;
953 int irqres;
955 irqres = 1;
957 * If num_registered_fb is zero, this is a call for the dummy part.
958 * The frame buffer devices weren't initialized yet.
960 if (!num_registered_fb || info_idx == -1)
961 return display_desc;
963 * Instead of blindly using registered_fb[0], we use info_idx, set by
964 * fb_console_init();
966 info = registered_fb[info_idx];
967 if (!info)
968 return NULL;
970 owner = info->fbops->owner;
971 if (!try_module_get(owner))
972 return NULL;
973 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
974 module_put(owner);
975 return NULL;
978 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
979 if (!ops) {
980 module_put(owner);
981 return NULL;
984 ops->currcon = -1;
985 ops->graphics = 1;
986 ops->cur_rotate = -1;
987 info->fbcon_par = ops;
988 p->con_rotate = initial_rotation;
989 set_blitting_type(vc, info);
991 if (info->fix.type != FB_TYPE_TEXT) {
992 if (fbcon_softback_size) {
993 if (!softback_buf) {
994 softback_buf =
995 (unsigned long)
996 kmalloc(fbcon_softback_size,
997 GFP_KERNEL);
998 if (!softback_buf) {
999 fbcon_softback_size = 0;
1000 softback_top = 0;
1003 } else {
1004 if (softback_buf) {
1005 kfree((void *) softback_buf);
1006 softback_buf = 0;
1007 softback_top = 0;
1010 if (softback_buf)
1011 softback_in = softback_top = softback_curr =
1012 softback_buf;
1013 softback_lines = 0;
1016 /* Setup default font */
1017 if (!p->fontdata) {
1018 if (!fontname[0] || !(font = find_font(fontname)))
1019 font = get_default_font(info->var.xres,
1020 info->var.yres,
1021 info->pixmap.blit_x,
1022 info->pixmap.blit_y);
1023 vc->vc_font.width = font->width;
1024 vc->vc_font.height = font->height;
1025 vc->vc_font.data = (void *)(p->fontdata = font->data);
1026 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1029 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1030 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1031 cols /= vc->vc_font.width;
1032 rows /= vc->vc_font.height;
1033 vc_resize(vc, cols, rows);
1035 DPRINTK("mode: %s\n", info->fix.id);
1036 DPRINTK("visual: %d\n", info->fix.visual);
1037 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1038 info->var.yres,
1039 info->var.bits_per_pixel);
1041 #ifdef CONFIG_ATARI
1042 if (MACH_IS_ATARI) {
1043 cursor_blink_rate = ATARI_CURSOR_BLINK_RATE;
1044 irqres =
1045 request_irq(IRQ_AUTO_4, fb_vbl_handler,
1046 IRQ_TYPE_PRIO, "framebuffer vbl",
1047 info);
1049 #endif /* CONFIG_ATARI */
1051 #ifdef CONFIG_MAC
1053 * On a Macintoy, the VBL interrupt may or may not be active.
1054 * As interrupt based cursor is more reliable and race free, we
1055 * probe for VBL interrupts.
1057 if (MACH_IS_MAC) {
1058 int ct = 0;
1060 * Probe for VBL: set temp. handler ...
1062 irqres = request_irq(IRQ_MAC_VBL, fb_vbl_detect, 0,
1063 "framebuffer vbl", info);
1064 vbl_detected = 0;
1067 * ... and spin for 20 ms ...
1069 while (!vbl_detected && ++ct < 1000)
1070 udelay(20);
1072 if (ct == 1000)
1073 printk
1074 ("fbcon_startup: No VBL detected, using timer based cursor.\n");
1076 free_irq(IRQ_MAC_VBL, fb_vbl_detect);
1078 if (vbl_detected) {
1080 * interrupt based cursor ok
1082 cursor_blink_rate = MAC_CURSOR_BLINK_RATE;
1083 irqres =
1084 request_irq(IRQ_MAC_VBL, fb_vbl_handler, 0,
1085 "framebuffer vbl", info);
1086 } else {
1088 * VBL not detected: fall through, use timer based cursor
1090 irqres = 1;
1093 #endif /* CONFIG_MAC */
1095 fbcon_add_cursor_timer(info);
1096 fbcon_has_exited = 0;
1097 return display_desc;
1100 static void fbcon_init(struct vc_data *vc, int init)
1102 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1103 struct fbcon_ops *ops;
1104 struct vc_data **default_mode = vc->vc_display_fg;
1105 struct vc_data *svc = *default_mode;
1106 struct display *t, *p = &fb_display[vc->vc_num];
1107 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
1108 int cap;
1110 if (info_idx == -1 || info == NULL)
1111 return;
1113 cap = info->flags;
1115 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1116 (info->fix.type == FB_TYPE_TEXT))
1117 logo = 0;
1119 if (var_to_display(p, &info->var, info))
1120 return;
1122 if (!info->fbcon_par)
1123 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1125 /* If we are not the first console on this
1126 fb, copy the font from that console */
1127 t = &fb_display[fg_console];
1128 if (!p->fontdata) {
1129 if (t->fontdata) {
1130 struct vc_data *fvc = vc_cons[fg_console].d;
1132 vc->vc_font.data = (void *)(p->fontdata =
1133 fvc->vc_font.data);
1134 vc->vc_font.width = fvc->vc_font.width;
1135 vc->vc_font.height = fvc->vc_font.height;
1136 p->userfont = t->userfont;
1138 if (p->userfont)
1139 REFCOUNT(p->fontdata)++;
1140 } else {
1141 const struct font_desc *font = NULL;
1143 if (!fontname[0] || !(font = find_font(fontname)))
1144 font = get_default_font(info->var.xres,
1145 info->var.yres,
1146 info->pixmap.blit_x,
1147 info->pixmap.blit_y);
1148 vc->vc_font.width = font->width;
1149 vc->vc_font.height = font->height;
1150 vc->vc_font.data = (void *)(p->fontdata = font->data);
1151 vc->vc_font.charcount = 256; /* FIXME Need to
1152 support more fonts */
1156 if (p->userfont)
1157 charcnt = FNTCHARCNT(p->fontdata);
1159 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1160 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1161 if (charcnt == 256) {
1162 vc->vc_hi_font_mask = 0;
1163 } else {
1164 vc->vc_hi_font_mask = 0x100;
1165 if (vc->vc_can_do_color)
1166 vc->vc_complement_mask <<= 1;
1169 if (!*svc->vc_uni_pagedir_loc)
1170 con_set_default_unimap(svc);
1171 if (!*vc->vc_uni_pagedir_loc)
1172 con_copy_unimap(vc, svc);
1174 ops = info->fbcon_par;
1175 p->con_rotate = initial_rotation;
1176 set_blitting_type(vc, info);
1178 cols = vc->vc_cols;
1179 rows = vc->vc_rows;
1180 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1181 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1182 new_cols /= vc->vc_font.width;
1183 new_rows /= vc->vc_font.height;
1184 vc_resize(vc, new_cols, new_rows);
1187 * We must always set the mode. The mode of the previous console
1188 * driver could be in the same resolution but we are using different
1189 * hardware so we have to initialize the hardware.
1191 * We need to do it in fbcon_init() to prevent screen corruption.
1193 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
1194 if (info->fbops->fb_set_par &&
1195 !(ops->flags & FBCON_FLAGS_INIT))
1196 info->fbops->fb_set_par(info);
1197 ops->flags |= FBCON_FLAGS_INIT;
1200 ops->graphics = 0;
1202 if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1203 !(cap & FBINFO_HWACCEL_DISABLED))
1204 p->scrollmode = SCROLL_MOVE;
1205 else /* default to something safe */
1206 p->scrollmode = SCROLL_REDRAW;
1209 * ++guenther: console.c:vc_allocate() relies on initializing
1210 * vc_{cols,rows}, but we must not set those if we are only
1211 * resizing the console.
1213 if (!init) {
1214 vc->vc_cols = new_cols;
1215 vc->vc_rows = new_rows;
1218 if (logo)
1219 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1221 if (vc == svc && softback_buf)
1222 fbcon_update_softback(vc);
1224 if (ops->rotate_font && ops->rotate_font(info, vc)) {
1225 ops->rotate = FB_ROTATE_UR;
1226 set_blitting_type(vc, info);
1229 ops->p = &fb_display[fg_console];
1232 static void fbcon_free_font(struct display *p)
1234 if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1235 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1236 p->fontdata = NULL;
1237 p->userfont = 0;
1240 static void fbcon_deinit(struct vc_data *vc)
1242 struct display *p = &fb_display[vc->vc_num];
1243 struct fb_info *info;
1244 struct fbcon_ops *ops;
1245 int idx;
1247 fbcon_free_font(p);
1248 idx = con2fb_map[vc->vc_num];
1250 if (idx == -1)
1251 goto finished;
1253 info = registered_fb[idx];
1255 if (!info)
1256 goto finished;
1258 ops = info->fbcon_par;
1260 if (!ops)
1261 goto finished;
1263 if (CON_IS_VISIBLE(vc))
1264 fbcon_del_cursor_timer(info);
1266 ops->flags &= ~FBCON_FLAGS_INIT;
1267 finished:
1269 if (!con_is_bound(&fb_con))
1270 fbcon_exit();
1272 return;
1275 /* ====================================================================== */
1277 /* fbcon_XXX routines - interface used by the world
1279 * This system is now divided into two levels because of complications
1280 * caused by hardware scrolling. Top level functions:
1282 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1284 * handles y values in range [0, scr_height-1] that correspond to real
1285 * screen positions. y_wrap shift means that first line of bitmap may be
1286 * anywhere on this display. These functions convert lineoffsets to
1287 * bitmap offsets and deal with the wrap-around case by splitting blits.
1289 * fbcon_bmove_physical_8() -- These functions fast implementations
1290 * fbcon_clear_physical_8() -- of original fbcon_XXX fns.
1291 * fbcon_putc_physical_8() -- (font width != 8) may be added later
1293 * WARNING:
1295 * At the moment fbcon_putc() cannot blit across vertical wrap boundary
1296 * Implies should only really hardware scroll in rows. Only reason for
1297 * restriction is simplicity & efficiency at the moment.
1300 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1301 int width)
1303 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1304 struct fbcon_ops *ops = info->fbcon_par;
1306 struct display *p = &fb_display[vc->vc_num];
1307 u_int y_break;
1309 if (fbcon_is_inactive(vc, info))
1310 return;
1312 if (!height || !width)
1313 return;
1315 /* Split blits that cross physical y_wrap boundary */
1317 y_break = p->vrows - p->yscroll;
1318 if (sy < y_break && sy + height - 1 >= y_break) {
1319 u_int b = y_break - sy;
1320 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1321 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1322 width);
1323 } else
1324 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1327 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1328 int count, int ypos, int xpos)
1330 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1331 struct display *p = &fb_display[vc->vc_num];
1332 struct fbcon_ops *ops = info->fbcon_par;
1334 if (!fbcon_is_inactive(vc, info))
1335 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1336 get_color(vc, info, scr_readw(s), 1),
1337 get_color(vc, info, scr_readw(s), 0));
1340 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1342 unsigned short chr;
1344 scr_writew(c, &chr);
1345 fbcon_putcs(vc, &chr, 1, ypos, xpos);
1348 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1350 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1351 struct fbcon_ops *ops = info->fbcon_par;
1353 if (!fbcon_is_inactive(vc, info))
1354 ops->clear_margins(vc, info, bottom_only);
1357 static void fbcon_cursor(struct vc_data *vc, int mode)
1359 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1360 struct fbcon_ops *ops = info->fbcon_par;
1361 int y;
1362 int c = scr_readw((u16 *) vc->vc_pos);
1364 if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1365 return;
1367 if (vc->vc_cursor_type & 0x10)
1368 fbcon_del_cursor_timer(info);
1369 else
1370 fbcon_add_cursor_timer(info);
1372 ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1373 if (mode & CM_SOFTBACK) {
1374 mode &= ~CM_SOFTBACK;
1375 y = softback_lines;
1376 } else {
1377 if (softback_lines)
1378 fbcon_set_origin(vc);
1379 y = 0;
1382 ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1383 get_color(vc, info, c, 0));
1384 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1387 static int scrollback_phys_max = 0;
1388 static int scrollback_max = 0;
1389 static int scrollback_current = 0;
1391 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1392 int unit)
1394 struct display *p, *t;
1395 struct vc_data **default_mode, *vc;
1396 struct vc_data *svc;
1397 struct fbcon_ops *ops = info->fbcon_par;
1398 int rows, cols, charcnt = 256;
1400 p = &fb_display[unit];
1402 if (var_to_display(p, var, info))
1403 return;
1405 vc = vc_cons[unit].d;
1407 if (!vc)
1408 return;
1410 default_mode = vc->vc_display_fg;
1411 svc = *default_mode;
1412 t = &fb_display[svc->vc_num];
1414 if (!vc->vc_font.data) {
1415 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1416 vc->vc_font.width = (*default_mode)->vc_font.width;
1417 vc->vc_font.height = (*default_mode)->vc_font.height;
1418 p->userfont = t->userfont;
1419 if (p->userfont)
1420 REFCOUNT(p->fontdata)++;
1422 if (p->userfont)
1423 charcnt = FNTCHARCNT(p->fontdata);
1425 var->activate = FB_ACTIVATE_NOW;
1426 info->var.activate = var->activate;
1427 var->yoffset = info->var.yoffset;
1428 var->xoffset = info->var.xoffset;
1429 fb_set_var(info, var);
1430 ops->var = info->var;
1431 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1432 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1433 if (charcnt == 256) {
1434 vc->vc_hi_font_mask = 0;
1435 } else {
1436 vc->vc_hi_font_mask = 0x100;
1437 if (vc->vc_can_do_color)
1438 vc->vc_complement_mask <<= 1;
1441 if (!*svc->vc_uni_pagedir_loc)
1442 con_set_default_unimap(svc);
1443 if (!*vc->vc_uni_pagedir_loc)
1444 con_copy_unimap(vc, svc);
1446 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1447 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1448 cols /= vc->vc_font.width;
1449 rows /= vc->vc_font.height;
1450 vc_resize(vc, cols, rows);
1452 if (CON_IS_VISIBLE(vc)) {
1453 update_screen(vc);
1454 if (softback_buf)
1455 fbcon_update_softback(vc);
1459 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1461 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1462 struct fbcon_ops *ops = info->fbcon_par;
1463 struct display *p = &fb_display[vc->vc_num];
1465 p->yscroll += count;
1466 if (p->yscroll >= p->vrows) /* Deal with wrap */
1467 p->yscroll -= p->vrows;
1468 ops->var.xoffset = 0;
1469 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1470 ops->var.vmode |= FB_VMODE_YWRAP;
1471 ops->update_start(info);
1472 scrollback_max += count;
1473 if (scrollback_max > scrollback_phys_max)
1474 scrollback_max = scrollback_phys_max;
1475 scrollback_current = 0;
1478 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1480 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1481 struct fbcon_ops *ops = info->fbcon_par;
1482 struct display *p = &fb_display[vc->vc_num];
1484 p->yscroll -= count;
1485 if (p->yscroll < 0) /* Deal with wrap */
1486 p->yscroll += p->vrows;
1487 ops->var.xoffset = 0;
1488 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1489 ops->var.vmode |= FB_VMODE_YWRAP;
1490 ops->update_start(info);
1491 scrollback_max -= count;
1492 if (scrollback_max < 0)
1493 scrollback_max = 0;
1494 scrollback_current = 0;
1497 static __inline__ void ypan_up(struct vc_data *vc, int count)
1499 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1500 struct display *p = &fb_display[vc->vc_num];
1501 struct fbcon_ops *ops = info->fbcon_par;
1503 p->yscroll += count;
1504 if (p->yscroll > p->vrows - vc->vc_rows) {
1505 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1506 0, 0, 0, vc->vc_rows, vc->vc_cols);
1507 p->yscroll -= p->vrows - vc->vc_rows;
1510 ops->var.xoffset = 0;
1511 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1512 ops->var.vmode &= ~FB_VMODE_YWRAP;
1513 ops->update_start(info);
1514 fbcon_clear_margins(vc, 1);
1515 scrollback_max += count;
1516 if (scrollback_max > scrollback_phys_max)
1517 scrollback_max = scrollback_phys_max;
1518 scrollback_current = 0;
1521 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1523 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1524 struct fbcon_ops *ops = info->fbcon_par;
1525 struct display *p = &fb_display[vc->vc_num];
1527 p->yscroll += count;
1529 if (p->yscroll > p->vrows - vc->vc_rows) {
1530 p->yscroll -= p->vrows - vc->vc_rows;
1531 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1534 ops->var.xoffset = 0;
1535 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1536 ops->var.vmode &= ~FB_VMODE_YWRAP;
1537 ops->update_start(info);
1538 fbcon_clear_margins(vc, 1);
1539 scrollback_max += count;
1540 if (scrollback_max > scrollback_phys_max)
1541 scrollback_max = scrollback_phys_max;
1542 scrollback_current = 0;
1545 static __inline__ void ypan_down(struct vc_data *vc, int count)
1547 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1548 struct display *p = &fb_display[vc->vc_num];
1549 struct fbcon_ops *ops = info->fbcon_par;
1551 p->yscroll -= count;
1552 if (p->yscroll < 0) {
1553 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1554 0, vc->vc_rows, vc->vc_cols);
1555 p->yscroll += p->vrows - vc->vc_rows;
1558 ops->var.xoffset = 0;
1559 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1560 ops->var.vmode &= ~FB_VMODE_YWRAP;
1561 ops->update_start(info);
1562 fbcon_clear_margins(vc, 1);
1563 scrollback_max -= count;
1564 if (scrollback_max < 0)
1565 scrollback_max = 0;
1566 scrollback_current = 0;
1569 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1571 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1572 struct fbcon_ops *ops = info->fbcon_par;
1573 struct display *p = &fb_display[vc->vc_num];
1575 p->yscroll -= count;
1577 if (p->yscroll < 0) {
1578 p->yscroll += p->vrows - vc->vc_rows;
1579 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1582 ops->var.xoffset = 0;
1583 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1584 ops->var.vmode &= ~FB_VMODE_YWRAP;
1585 ops->update_start(info);
1586 fbcon_clear_margins(vc, 1);
1587 scrollback_max -= count;
1588 if (scrollback_max < 0)
1589 scrollback_max = 0;
1590 scrollback_current = 0;
1593 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1594 long delta)
1596 int count = vc->vc_rows;
1597 unsigned short *d, *s;
1598 unsigned long n;
1599 int line = 0;
1601 d = (u16 *) softback_curr;
1602 if (d == (u16 *) softback_in)
1603 d = (u16 *) vc->vc_origin;
1604 n = softback_curr + delta * vc->vc_size_row;
1605 softback_lines -= delta;
1606 if (delta < 0) {
1607 if (softback_curr < softback_top && n < softback_buf) {
1608 n += softback_end - softback_buf;
1609 if (n < softback_top) {
1610 softback_lines -=
1611 (softback_top - n) / vc->vc_size_row;
1612 n = softback_top;
1614 } else if (softback_curr >= softback_top
1615 && n < softback_top) {
1616 softback_lines -=
1617 (softback_top - n) / vc->vc_size_row;
1618 n = softback_top;
1620 } else {
1621 if (softback_curr > softback_in && n >= softback_end) {
1622 n += softback_buf - softback_end;
1623 if (n > softback_in) {
1624 n = softback_in;
1625 softback_lines = 0;
1627 } else if (softback_curr <= softback_in && n > softback_in) {
1628 n = softback_in;
1629 softback_lines = 0;
1632 if (n == softback_curr)
1633 return;
1634 softback_curr = n;
1635 s = (u16 *) softback_curr;
1636 if (s == (u16 *) softback_in)
1637 s = (u16 *) vc->vc_origin;
1638 while (count--) {
1639 unsigned short *start;
1640 unsigned short *le;
1641 unsigned short c;
1642 int x = 0;
1643 unsigned short attr = 1;
1645 start = s;
1646 le = advance_row(s, 1);
1647 do {
1648 c = scr_readw(s);
1649 if (attr != (c & 0xff00)) {
1650 attr = c & 0xff00;
1651 if (s > start) {
1652 fbcon_putcs(vc, start, s - start,
1653 line, x);
1654 x += s - start;
1655 start = s;
1658 if (c == scr_readw(d)) {
1659 if (s > start) {
1660 fbcon_putcs(vc, start, s - start,
1661 line, x);
1662 x += s - start + 1;
1663 start = s + 1;
1664 } else {
1665 x++;
1666 start++;
1669 s++;
1670 d++;
1671 } while (s < le);
1672 if (s > start)
1673 fbcon_putcs(vc, start, s - start, line, x);
1674 line++;
1675 if (d == (u16 *) softback_end)
1676 d = (u16 *) softback_buf;
1677 if (d == (u16 *) softback_in)
1678 d = (u16 *) vc->vc_origin;
1679 if (s == (u16 *) softback_end)
1680 s = (u16 *) softback_buf;
1681 if (s == (u16 *) softback_in)
1682 s = (u16 *) vc->vc_origin;
1686 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1687 int line, int count, int dy)
1689 unsigned short *s = (unsigned short *)
1690 (vc->vc_origin + vc->vc_size_row * line);
1692 while (count--) {
1693 unsigned short *start = s;
1694 unsigned short *le = advance_row(s, 1);
1695 unsigned short c;
1696 int x = 0;
1697 unsigned short attr = 1;
1699 do {
1700 c = scr_readw(s);
1701 if (attr != (c & 0xff00)) {
1702 attr = c & 0xff00;
1703 if (s > start) {
1704 fbcon_putcs(vc, start, s - start,
1705 dy, x);
1706 x += s - start;
1707 start = s;
1710 console_conditional_schedule();
1711 s++;
1712 } while (s < le);
1713 if (s > start)
1714 fbcon_putcs(vc, start, s - start, dy, x);
1715 console_conditional_schedule();
1716 dy++;
1720 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1721 struct display *p, int line, int count, int ycount)
1723 int offset = ycount * vc->vc_cols;
1724 unsigned short *d = (unsigned short *)
1725 (vc->vc_origin + vc->vc_size_row * line);
1726 unsigned short *s = d + offset;
1727 struct fbcon_ops *ops = info->fbcon_par;
1729 while (count--) {
1730 unsigned short *start = s;
1731 unsigned short *le = advance_row(s, 1);
1732 unsigned short c;
1733 int x = 0;
1735 do {
1736 c = scr_readw(s);
1738 if (c == scr_readw(d)) {
1739 if (s > start) {
1740 ops->bmove(vc, info, line + ycount, x,
1741 line, x, 1, s-start);
1742 x += s - start + 1;
1743 start = s + 1;
1744 } else {
1745 x++;
1746 start++;
1750 scr_writew(c, d);
1751 console_conditional_schedule();
1752 s++;
1753 d++;
1754 } while (s < le);
1755 if (s > start)
1756 ops->bmove(vc, info, line + ycount, x, line, x, 1,
1757 s-start);
1758 console_conditional_schedule();
1759 if (ycount > 0)
1760 line++;
1761 else {
1762 line--;
1763 /* NOTE: We subtract two lines from these pointers */
1764 s -= vc->vc_size_row;
1765 d -= vc->vc_size_row;
1770 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1771 int line, int count, int offset)
1773 unsigned short *d = (unsigned short *)
1774 (vc->vc_origin + vc->vc_size_row * line);
1775 unsigned short *s = d + offset;
1777 while (count--) {
1778 unsigned short *start = s;
1779 unsigned short *le = advance_row(s, 1);
1780 unsigned short c;
1781 int x = 0;
1782 unsigned short attr = 1;
1784 do {
1785 c = scr_readw(s);
1786 if (attr != (c & 0xff00)) {
1787 attr = c & 0xff00;
1788 if (s > start) {
1789 fbcon_putcs(vc, start, s - start,
1790 line, x);
1791 x += s - start;
1792 start = s;
1795 if (c == scr_readw(d)) {
1796 if (s > start) {
1797 fbcon_putcs(vc, start, s - start,
1798 line, x);
1799 x += s - start + 1;
1800 start = s + 1;
1801 } else {
1802 x++;
1803 start++;
1806 scr_writew(c, d);
1807 console_conditional_schedule();
1808 s++;
1809 d++;
1810 } while (s < le);
1811 if (s > start)
1812 fbcon_putcs(vc, start, s - start, line, x);
1813 console_conditional_schedule();
1814 if (offset > 0)
1815 line++;
1816 else {
1817 line--;
1818 /* NOTE: We subtract two lines from these pointers */
1819 s -= vc->vc_size_row;
1820 d -= vc->vc_size_row;
1825 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1826 int count)
1828 unsigned short *p;
1830 if (vc->vc_num != fg_console)
1831 return;
1832 p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1834 while (count) {
1835 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1836 count--;
1837 p = advance_row(p, 1);
1838 softback_in += vc->vc_size_row;
1839 if (softback_in == softback_end)
1840 softback_in = softback_buf;
1841 if (softback_in == softback_top) {
1842 softback_top += vc->vc_size_row;
1843 if (softback_top == softback_end)
1844 softback_top = softback_buf;
1847 softback_curr = softback_in;
1850 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1851 int count)
1853 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1854 struct display *p = &fb_display[vc->vc_num];
1855 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1857 if (fbcon_is_inactive(vc, info))
1858 return -EINVAL;
1860 fbcon_cursor(vc, CM_ERASE);
1863 * ++Geert: Only use ywrap/ypan if the console is in text mode
1864 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1865 * whole screen (prevents flicker).
1868 switch (dir) {
1869 case SM_UP:
1870 if (count > vc->vc_rows) /* Maximum realistic size */
1871 count = vc->vc_rows;
1872 if (softback_top)
1873 fbcon_softback_note(vc, t, count);
1874 if (logo_shown >= 0)
1875 goto redraw_up;
1876 switch (p->scrollmode) {
1877 case SCROLL_MOVE:
1878 fbcon_redraw_blit(vc, info, p, t, b - t - count,
1879 count);
1880 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1881 scr_memsetw((unsigned short *) (vc->vc_origin +
1882 vc->vc_size_row *
1883 (b - count)),
1884 vc->vc_scrl_erase_char,
1885 vc->vc_size_row * count);
1886 return 1;
1887 break;
1889 case SCROLL_WRAP_MOVE:
1890 if (b - t - count > 3 * vc->vc_rows >> 2) {
1891 if (t > 0)
1892 fbcon_bmove(vc, 0, 0, count, 0, t,
1893 vc->vc_cols);
1894 ywrap_up(vc, count);
1895 if (vc->vc_rows - b > 0)
1896 fbcon_bmove(vc, b - count, 0, b, 0,
1897 vc->vc_rows - b,
1898 vc->vc_cols);
1899 } else if (info->flags & FBINFO_READS_FAST)
1900 fbcon_bmove(vc, t + count, 0, t, 0,
1901 b - t - count, vc->vc_cols);
1902 else
1903 goto redraw_up;
1904 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1905 break;
1907 case SCROLL_PAN_REDRAW:
1908 if ((p->yscroll + count <=
1909 2 * (p->vrows - vc->vc_rows))
1910 && ((!scroll_partial && (b - t == vc->vc_rows))
1911 || (scroll_partial
1912 && (b - t - count >
1913 3 * vc->vc_rows >> 2)))) {
1914 if (t > 0)
1915 fbcon_redraw_move(vc, p, 0, t, count);
1916 ypan_up_redraw(vc, t, count);
1917 if (vc->vc_rows - b > 0)
1918 fbcon_redraw_move(vc, p, b,
1919 vc->vc_rows - b, b);
1920 } else
1921 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1922 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1923 break;
1925 case SCROLL_PAN_MOVE:
1926 if ((p->yscroll + count <=
1927 2 * (p->vrows - vc->vc_rows))
1928 && ((!scroll_partial && (b - t == vc->vc_rows))
1929 || (scroll_partial
1930 && (b - t - count >
1931 3 * vc->vc_rows >> 2)))) {
1932 if (t > 0)
1933 fbcon_bmove(vc, 0, 0, count, 0, t,
1934 vc->vc_cols);
1935 ypan_up(vc, count);
1936 if (vc->vc_rows - b > 0)
1937 fbcon_bmove(vc, b - count, 0, b, 0,
1938 vc->vc_rows - b,
1939 vc->vc_cols);
1940 } else if (info->flags & FBINFO_READS_FAST)
1941 fbcon_bmove(vc, t + count, 0, t, 0,
1942 b - t - count, vc->vc_cols);
1943 else
1944 goto redraw_up;
1945 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1946 break;
1948 case SCROLL_REDRAW:
1949 redraw_up:
1950 fbcon_redraw(vc, p, t, b - t - count,
1951 count * vc->vc_cols);
1952 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1953 scr_memsetw((unsigned short *) (vc->vc_origin +
1954 vc->vc_size_row *
1955 (b - count)),
1956 vc->vc_scrl_erase_char,
1957 vc->vc_size_row * count);
1958 return 1;
1960 break;
1962 case SM_DOWN:
1963 if (count > vc->vc_rows) /* Maximum realistic size */
1964 count = vc->vc_rows;
1965 if (logo_shown >= 0)
1966 goto redraw_down;
1967 switch (p->scrollmode) {
1968 case SCROLL_MOVE:
1969 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1970 -count);
1971 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1972 scr_memsetw((unsigned short *) (vc->vc_origin +
1973 vc->vc_size_row *
1975 vc->vc_scrl_erase_char,
1976 vc->vc_size_row * count);
1977 return 1;
1978 break;
1980 case SCROLL_WRAP_MOVE:
1981 if (b - t - count > 3 * vc->vc_rows >> 2) {
1982 if (vc->vc_rows - b > 0)
1983 fbcon_bmove(vc, b, 0, b - count, 0,
1984 vc->vc_rows - b,
1985 vc->vc_cols);
1986 ywrap_down(vc, count);
1987 if (t > 0)
1988 fbcon_bmove(vc, count, 0, 0, 0, t,
1989 vc->vc_cols);
1990 } else if (info->flags & FBINFO_READS_FAST)
1991 fbcon_bmove(vc, t, 0, t + count, 0,
1992 b - t - count, vc->vc_cols);
1993 else
1994 goto redraw_down;
1995 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1996 break;
1998 case SCROLL_PAN_MOVE:
1999 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
2000 && ((!scroll_partial && (b - t == vc->vc_rows))
2001 || (scroll_partial
2002 && (b - t - count >
2003 3 * vc->vc_rows >> 2)))) {
2004 if (vc->vc_rows - b > 0)
2005 fbcon_bmove(vc, b, 0, b - count, 0,
2006 vc->vc_rows - b,
2007 vc->vc_cols);
2008 ypan_down(vc, count);
2009 if (t > 0)
2010 fbcon_bmove(vc, count, 0, 0, 0, t,
2011 vc->vc_cols);
2012 } else if (info->flags & FBINFO_READS_FAST)
2013 fbcon_bmove(vc, t, 0, t + count, 0,
2014 b - t - count, vc->vc_cols);
2015 else
2016 goto redraw_down;
2017 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2018 break;
2020 case SCROLL_PAN_REDRAW:
2021 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
2022 && ((!scroll_partial && (b - t == vc->vc_rows))
2023 || (scroll_partial
2024 && (b - t - count >
2025 3 * vc->vc_rows >> 2)))) {
2026 if (vc->vc_rows - b > 0)
2027 fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
2028 b - count);
2029 ypan_down_redraw(vc, t, count);
2030 if (t > 0)
2031 fbcon_redraw_move(vc, p, count, t, 0);
2032 } else
2033 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
2034 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2035 break;
2037 case SCROLL_REDRAW:
2038 redraw_down:
2039 fbcon_redraw(vc, p, b - 1, b - t - count,
2040 -count * vc->vc_cols);
2041 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2042 scr_memsetw((unsigned short *) (vc->vc_origin +
2043 vc->vc_size_row *
2045 vc->vc_scrl_erase_char,
2046 vc->vc_size_row * count);
2047 return 1;
2050 return 0;
2054 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
2055 int height, int width)
2057 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2058 struct display *p = &fb_display[vc->vc_num];
2060 if (fbcon_is_inactive(vc, info))
2061 return;
2063 if (!width || !height)
2064 return;
2066 /* Split blits that cross physical y_wrap case.
2067 * Pathological case involves 4 blits, better to use recursive
2068 * code rather than unrolled case
2070 * Recursive invocations don't need to erase the cursor over and
2071 * over again, so we use fbcon_bmove_rec()
2073 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2074 p->vrows - p->yscroll);
2077 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
2078 int dy, int dx, int height, int width, u_int y_break)
2080 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2081 struct fbcon_ops *ops = info->fbcon_par;
2082 u_int b;
2084 if (sy < y_break && sy + height > y_break) {
2085 b = y_break - sy;
2086 if (dy < sy) { /* Avoid trashing self */
2087 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2088 y_break);
2089 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2090 height - b, width, y_break);
2091 } else {
2092 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2093 height - b, width, y_break);
2094 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2095 y_break);
2097 return;
2100 if (dy < y_break && dy + height > y_break) {
2101 b = y_break - dy;
2102 if (dy < sy) { /* Avoid trashing self */
2103 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2104 y_break);
2105 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2106 height - b, width, y_break);
2107 } else {
2108 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2109 height - b, width, y_break);
2110 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2111 y_break);
2113 return;
2115 ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2116 height, width);
2119 static __inline__ void updatescrollmode(struct display *p,
2120 struct fb_info *info,
2121 struct vc_data *vc)
2123 struct fbcon_ops *ops = info->fbcon_par;
2124 int fh = vc->vc_font.height;
2125 int cap = info->flags;
2126 u16 t = 0;
2127 int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2128 info->fix.xpanstep);
2129 int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2130 int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2131 int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2132 info->var.xres_virtual);
2133 int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2134 divides(ypan, vc->vc_font.height) && vyres > yres;
2135 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2136 divides(ywrap, vc->vc_font.height) &&
2137 divides(vc->vc_font.height, vyres) &&
2138 divides(vc->vc_font.height, yres);
2139 int reading_fast = cap & FBINFO_READS_FAST;
2140 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2141 !(cap & FBINFO_HWACCEL_DISABLED);
2142 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2143 !(cap & FBINFO_HWACCEL_DISABLED);
2145 p->vrows = vyres/fh;
2146 if (yres > (fh * (vc->vc_rows + 1)))
2147 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2148 if ((yres % fh) && (vyres % fh < yres % fh))
2149 p->vrows--;
2151 if (good_wrap || good_pan) {
2152 if (reading_fast || fast_copyarea)
2153 p->scrollmode = good_wrap ?
2154 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2155 else
2156 p->scrollmode = good_wrap ? SCROLL_REDRAW :
2157 SCROLL_PAN_REDRAW;
2158 } else {
2159 if (reading_fast || (fast_copyarea && !fast_imageblit))
2160 p->scrollmode = SCROLL_MOVE;
2161 else
2162 p->scrollmode = SCROLL_REDRAW;
2166 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2167 unsigned int height, unsigned int user)
2169 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2170 struct fbcon_ops *ops = info->fbcon_par;
2171 struct display *p = &fb_display[vc->vc_num];
2172 struct fb_var_screeninfo var = info->var;
2173 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2175 virt_w = FBCON_SWAP(ops->rotate, width, height);
2176 virt_h = FBCON_SWAP(ops->rotate, height, width);
2177 virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2178 vc->vc_font.height);
2179 virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2180 vc->vc_font.width);
2181 var.xres = virt_w * virt_fw;
2182 var.yres = virt_h * virt_fh;
2183 x_diff = info->var.xres - var.xres;
2184 y_diff = info->var.yres - var.yres;
2185 if (x_diff < 0 || x_diff > virt_fw ||
2186 y_diff < 0 || y_diff > virt_fh) {
2187 const struct fb_videomode *mode;
2189 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2190 mode = fb_find_best_mode(&var, &info->modelist);
2191 if (mode == NULL)
2192 return -EINVAL;
2193 display_to_var(&var, p);
2194 fb_videomode_to_var(&var, mode);
2196 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2197 return -EINVAL;
2199 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2200 if (CON_IS_VISIBLE(vc)) {
2201 var.activate = FB_ACTIVATE_NOW |
2202 FB_ACTIVATE_FORCE;
2203 fb_set_var(info, &var);
2205 var_to_display(p, &info->var, info);
2206 ops->var = info->var;
2208 updatescrollmode(p, info, vc);
2209 return 0;
2212 static int fbcon_switch(struct vc_data *vc)
2214 struct fb_info *info, *old_info = NULL;
2215 struct fbcon_ops *ops;
2216 struct display *p = &fb_display[vc->vc_num];
2217 struct fb_var_screeninfo var;
2218 int i, prev_console, charcnt = 256;
2220 info = registered_fb[con2fb_map[vc->vc_num]];
2221 ops = info->fbcon_par;
2223 if (softback_top) {
2224 if (softback_lines)
2225 fbcon_set_origin(vc);
2226 softback_top = softback_curr = softback_in = softback_buf;
2227 softback_lines = 0;
2228 fbcon_update_softback(vc);
2231 if (logo_shown >= 0) {
2232 struct vc_data *conp2 = vc_cons[logo_shown].d;
2234 if (conp2->vc_top == logo_lines
2235 && conp2->vc_bottom == conp2->vc_rows)
2236 conp2->vc_top = 0;
2237 logo_shown = FBCON_LOGO_CANSHOW;
2240 prev_console = ops->currcon;
2241 if (prev_console != -1)
2242 old_info = registered_fb[con2fb_map[prev_console]];
2244 * FIXME: If we have multiple fbdev's loaded, we need to
2245 * update all info->currcon. Perhaps, we can place this
2246 * in a centralized structure, but this might break some
2247 * drivers.
2249 * info->currcon = vc->vc_num;
2251 for (i = 0; i < FB_MAX; i++) {
2252 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
2253 struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2255 o->currcon = vc->vc_num;
2258 memset(&var, 0, sizeof(struct fb_var_screeninfo));
2259 display_to_var(&var, p);
2260 var.activate = FB_ACTIVATE_NOW;
2263 * make sure we don't unnecessarily trip the memcmp()
2264 * in fb_set_var()
2266 info->var.activate = var.activate;
2267 var.yoffset = info->var.yoffset;
2268 var.xoffset = info->var.xoffset;
2269 var.vmode = info->var.vmode;
2270 fb_set_var(info, &var);
2271 ops->var = info->var;
2273 if (old_info != NULL && (old_info != info ||
2274 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2275 if (info->fbops->fb_set_par)
2276 info->fbops->fb_set_par(info);
2278 if (old_info != info)
2279 fbcon_del_cursor_timer(old_info);
2282 if (fbcon_is_inactive(vc, info) ||
2283 ops->blank_state != FB_BLANK_UNBLANK)
2284 fbcon_del_cursor_timer(info);
2285 else
2286 fbcon_add_cursor_timer(info);
2288 set_blitting_type(vc, info);
2289 ops->cursor_reset = 1;
2291 if (ops->rotate_font && ops->rotate_font(info, vc)) {
2292 ops->rotate = FB_ROTATE_UR;
2293 set_blitting_type(vc, info);
2296 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2297 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2299 if (p->userfont)
2300 charcnt = FNTCHARCNT(vc->vc_font.data);
2302 if (charcnt > 256)
2303 vc->vc_complement_mask <<= 1;
2305 updatescrollmode(p, info, vc);
2307 switch (p->scrollmode) {
2308 case SCROLL_WRAP_MOVE:
2309 scrollback_phys_max = p->vrows - vc->vc_rows;
2310 break;
2311 case SCROLL_PAN_MOVE:
2312 case SCROLL_PAN_REDRAW:
2313 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2314 if (scrollback_phys_max < 0)
2315 scrollback_phys_max = 0;
2316 break;
2317 default:
2318 scrollback_phys_max = 0;
2319 break;
2322 scrollback_max = 0;
2323 scrollback_current = 0;
2325 if (!fbcon_is_inactive(vc, info)) {
2326 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2327 ops->update_start(info);
2330 fbcon_set_palette(vc, color_table);
2331 fbcon_clear_margins(vc, 0);
2333 if (logo_shown == FBCON_LOGO_DRAW) {
2335 logo_shown = fg_console;
2336 /* This is protected above by initmem_freed */
2337 fb_show_logo(info, ops->rotate);
2338 update_region(vc,
2339 vc->vc_origin + vc->vc_size_row * vc->vc_top,
2340 vc->vc_size_row * (vc->vc_bottom -
2341 vc->vc_top) / 2);
2342 return 0;
2344 return 1;
2347 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2348 int blank)
2350 struct fb_event event;
2352 if (blank) {
2353 unsigned short charmask = vc->vc_hi_font_mask ?
2354 0x1ff : 0xff;
2355 unsigned short oldc;
2357 oldc = vc->vc_video_erase_char;
2358 vc->vc_video_erase_char &= charmask;
2359 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2360 vc->vc_video_erase_char = oldc;
2364 event.info = info;
2365 event.data = &blank;
2366 fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
2369 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2371 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2372 struct fbcon_ops *ops = info->fbcon_par;
2374 if (mode_switch) {
2375 struct fb_var_screeninfo var = info->var;
2377 ops->graphics = 1;
2379 if (!blank) {
2380 if (info->fbops->fb_save_state)
2381 info->fbops->fb_save_state(info);
2382 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2383 fb_set_var(info, &var);
2384 ops->graphics = 0;
2385 ops->var = info->var;
2386 } else if (info->fbops->fb_restore_state)
2387 info->fbops->fb_restore_state(info);
2390 if (!fbcon_is_inactive(vc, info)) {
2391 if (ops->blank_state != blank) {
2392 ops->blank_state = blank;
2393 fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2394 ops->cursor_flash = (!blank);
2396 if (fb_blank(info, blank))
2397 fbcon_generic_blank(vc, info, blank);
2400 if (!blank)
2401 update_screen(vc);
2404 if (mode_switch || fbcon_is_inactive(vc, info) ||
2405 ops->blank_state != FB_BLANK_UNBLANK)
2406 fbcon_del_cursor_timer(info);
2407 else
2408 fbcon_add_cursor_timer(info);
2410 return 0;
2413 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2415 u8 *fontdata = vc->vc_font.data;
2416 u8 *data = font->data;
2417 int i, j;
2419 font->width = vc->vc_font.width;
2420 font->height = vc->vc_font.height;
2421 font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2422 if (!font->data)
2423 return 0;
2425 if (font->width <= 8) {
2426 j = vc->vc_font.height;
2427 for (i = 0; i < font->charcount; i++) {
2428 memcpy(data, fontdata, j);
2429 memset(data + j, 0, 32 - j);
2430 data += 32;
2431 fontdata += j;
2433 } else if (font->width <= 16) {
2434 j = vc->vc_font.height * 2;
2435 for (i = 0; i < font->charcount; i++) {
2436 memcpy(data, fontdata, j);
2437 memset(data + j, 0, 64 - j);
2438 data += 64;
2439 fontdata += j;
2441 } else if (font->width <= 24) {
2442 for (i = 0; i < font->charcount; i++) {
2443 for (j = 0; j < vc->vc_font.height; j++) {
2444 *data++ = fontdata[0];
2445 *data++ = fontdata[1];
2446 *data++ = fontdata[2];
2447 fontdata += sizeof(u32);
2449 memset(data, 0, 3 * (32 - j));
2450 data += 3 * (32 - j);
2452 } else {
2453 j = vc->vc_font.height * 4;
2454 for (i = 0; i < font->charcount; i++) {
2455 memcpy(data, fontdata, j);
2456 memset(data + j, 0, 128 - j);
2457 data += 128;
2458 fontdata += j;
2461 return 0;
2464 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2465 const u8 * data, int userfont)
2467 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2468 struct fbcon_ops *ops = info->fbcon_par;
2469 struct display *p = &fb_display[vc->vc_num];
2470 int resize;
2471 int cnt;
2472 char *old_data = NULL;
2474 if (CON_IS_VISIBLE(vc) && softback_lines)
2475 fbcon_set_origin(vc);
2477 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2478 if (p->userfont)
2479 old_data = vc->vc_font.data;
2480 if (userfont)
2481 cnt = FNTCHARCNT(data);
2482 else
2483 cnt = 256;
2484 vc->vc_font.data = (void *)(p->fontdata = data);
2485 if ((p->userfont = userfont))
2486 REFCOUNT(data)++;
2487 vc->vc_font.width = w;
2488 vc->vc_font.height = h;
2489 if (vc->vc_hi_font_mask && cnt == 256) {
2490 vc->vc_hi_font_mask = 0;
2491 if (vc->vc_can_do_color) {
2492 vc->vc_complement_mask >>= 1;
2493 vc->vc_s_complement_mask >>= 1;
2496 /* ++Edmund: reorder the attribute bits */
2497 if (vc->vc_can_do_color) {
2498 unsigned short *cp =
2499 (unsigned short *) vc->vc_origin;
2500 int count = vc->vc_screenbuf_size / 2;
2501 unsigned short c;
2502 for (; count > 0; count--, cp++) {
2503 c = scr_readw(cp);
2504 scr_writew(((c & 0xfe00) >> 1) |
2505 (c & 0xff), cp);
2507 c = vc->vc_video_erase_char;
2508 vc->vc_video_erase_char =
2509 ((c & 0xfe00) >> 1) | (c & 0xff);
2510 vc->vc_attr >>= 1;
2512 } else if (!vc->vc_hi_font_mask && cnt == 512) {
2513 vc->vc_hi_font_mask = 0x100;
2514 if (vc->vc_can_do_color) {
2515 vc->vc_complement_mask <<= 1;
2516 vc->vc_s_complement_mask <<= 1;
2519 /* ++Edmund: reorder the attribute bits */
2521 unsigned short *cp =
2522 (unsigned short *) vc->vc_origin;
2523 int count = vc->vc_screenbuf_size / 2;
2524 unsigned short c;
2525 for (; count > 0; count--, cp++) {
2526 unsigned short newc;
2527 c = scr_readw(cp);
2528 if (vc->vc_can_do_color)
2529 newc =
2530 ((c & 0xff00) << 1) | (c &
2531 0xff);
2532 else
2533 newc = c & ~0x100;
2534 scr_writew(newc, cp);
2536 c = vc->vc_video_erase_char;
2537 if (vc->vc_can_do_color) {
2538 vc->vc_video_erase_char =
2539 ((c & 0xff00) << 1) | (c & 0xff);
2540 vc->vc_attr <<= 1;
2541 } else
2542 vc->vc_video_erase_char = c & ~0x100;
2547 if (resize) {
2548 int cols, rows;
2550 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2551 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2552 cols /= w;
2553 rows /= h;
2554 vc_resize(vc, cols, rows);
2555 if (CON_IS_VISIBLE(vc) && softback_buf)
2556 fbcon_update_softback(vc);
2557 } else if (CON_IS_VISIBLE(vc)
2558 && vc->vc_mode == KD_TEXT) {
2559 fbcon_clear_margins(vc, 0);
2560 update_screen(vc);
2563 if (old_data && (--REFCOUNT(old_data) == 0))
2564 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2565 return 0;
2568 static int fbcon_copy_font(struct vc_data *vc, int con)
2570 struct display *od = &fb_display[con];
2571 struct console_font *f = &vc->vc_font;
2573 if (od->fontdata == f->data)
2574 return 0; /* already the same font... */
2575 return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2579 * User asked to set font; we are guaranteed that
2580 * a) width and height are in range 1..32
2581 * b) charcount does not exceed 512
2582 * but lets not assume that, since someone might someday want to use larger
2583 * fonts. And charcount of 512 is small for unicode support.
2585 * However, user space gives the font in 32 rows , regardless of
2586 * actual font height. So a new API is needed if support for larger fonts
2587 * is ever implemented.
2590 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2592 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2593 unsigned charcount = font->charcount;
2594 int w = font->width;
2595 int h = font->height;
2596 int size;
2597 int i, csum;
2598 u8 *new_data, *data = font->data;
2599 int pitch = (font->width+7) >> 3;
2601 /* Is there a reason why fbconsole couldn't handle any charcount >256?
2602 * If not this check should be changed to charcount < 256 */
2603 if (charcount != 256 && charcount != 512)
2604 return -EINVAL;
2606 /* Make sure drawing engine can handle the font */
2607 if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2608 !(info->pixmap.blit_y & (1 << (font->height - 1))))
2609 return -EINVAL;
2611 /* Make sure driver can handle the font length */
2612 if (fbcon_invalid_charcount(info, charcount))
2613 return -EINVAL;
2615 size = h * pitch * charcount;
2617 new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2619 if (!new_data)
2620 return -ENOMEM;
2622 new_data += FONT_EXTRA_WORDS * sizeof(int);
2623 FNTSIZE(new_data) = size;
2624 FNTCHARCNT(new_data) = charcount;
2625 REFCOUNT(new_data) = 0; /* usage counter */
2626 for (i=0; i< charcount; i++) {
2627 memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
2630 /* Since linux has a nice crc32 function use it for counting font
2631 * checksums. */
2632 csum = crc32(0, new_data, size);
2634 FNTSUM(new_data) = csum;
2635 /* Check if the same font is on some other console already */
2636 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2637 struct vc_data *tmp = vc_cons[i].d;
2639 if (fb_display[i].userfont &&
2640 fb_display[i].fontdata &&
2641 FNTSUM(fb_display[i].fontdata) == csum &&
2642 FNTSIZE(fb_display[i].fontdata) == size &&
2643 tmp->vc_font.width == w &&
2644 !memcmp(fb_display[i].fontdata, new_data, size)) {
2645 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2646 new_data = (u8 *)fb_display[i].fontdata;
2647 break;
2650 return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2653 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2655 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2656 const struct font_desc *f;
2658 if (!name)
2659 f = get_default_font(info->var.xres, info->var.yres,
2660 info->pixmap.blit_x, info->pixmap.blit_y);
2661 else if (!(f = find_font(name)))
2662 return -ENOENT;
2664 font->width = f->width;
2665 font->height = f->height;
2666 return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2669 static u16 palette_red[16];
2670 static u16 palette_green[16];
2671 static u16 palette_blue[16];
2673 static struct fb_cmap palette_cmap = {
2674 0, 16, palette_red, palette_green, palette_blue, NULL
2677 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2679 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2680 int i, j, k, depth;
2681 u8 val;
2683 if (fbcon_is_inactive(vc, info))
2684 return -EINVAL;
2686 if (!CON_IS_VISIBLE(vc))
2687 return 0;
2689 depth = fb_get_color_depth(&info->var, &info->fix);
2690 if (depth > 3) {
2691 for (i = j = 0; i < 16; i++) {
2692 k = table[i];
2693 val = vc->vc_palette[j++];
2694 palette_red[k] = (val << 8) | val;
2695 val = vc->vc_palette[j++];
2696 palette_green[k] = (val << 8) | val;
2697 val = vc->vc_palette[j++];
2698 palette_blue[k] = (val << 8) | val;
2700 palette_cmap.len = 16;
2701 palette_cmap.start = 0;
2703 * If framebuffer is capable of less than 16 colors,
2704 * use default palette of fbcon.
2706 } else
2707 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2709 return fb_set_cmap(&palette_cmap, info);
2712 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2714 unsigned long p;
2715 int line;
2717 if (vc->vc_num != fg_console || !softback_lines)
2718 return (u16 *) (vc->vc_origin + offset);
2719 line = offset / vc->vc_size_row;
2720 if (line >= softback_lines)
2721 return (u16 *) (vc->vc_origin + offset -
2722 softback_lines * vc->vc_size_row);
2723 p = softback_curr + offset;
2724 if (p >= softback_end)
2725 p += softback_buf - softback_end;
2726 return (u16 *) p;
2729 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2730 int *px, int *py)
2732 unsigned long ret;
2733 int x, y;
2735 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2736 unsigned long offset = (pos - vc->vc_origin) / 2;
2738 x = offset % vc->vc_cols;
2739 y = offset / vc->vc_cols;
2740 if (vc->vc_num == fg_console)
2741 y += softback_lines;
2742 ret = pos + (vc->vc_cols - x) * 2;
2743 } else if (vc->vc_num == fg_console && softback_lines) {
2744 unsigned long offset = pos - softback_curr;
2746 if (pos < softback_curr)
2747 offset += softback_end - softback_buf;
2748 offset /= 2;
2749 x = offset % vc->vc_cols;
2750 y = offset / vc->vc_cols;
2751 ret = pos + (vc->vc_cols - x) * 2;
2752 if (ret == softback_end)
2753 ret = softback_buf;
2754 if (ret == softback_in)
2755 ret = vc->vc_origin;
2756 } else {
2757 /* Should not happen */
2758 x = y = 0;
2759 ret = vc->vc_origin;
2761 if (px)
2762 *px = x;
2763 if (py)
2764 *py = y;
2765 return ret;
2768 /* As we might be inside of softback, we may work with non-contiguous buffer,
2769 that's why we have to use a separate routine. */
2770 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2772 while (cnt--) {
2773 u16 a = scr_readw(p);
2774 if (!vc->vc_can_do_color)
2775 a ^= 0x0800;
2776 else if (vc->vc_hi_font_mask == 0x100)
2777 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2778 (((a) & 0x0e00) << 4);
2779 else
2780 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2781 (((a) & 0x0700) << 4);
2782 scr_writew(a, p++);
2783 if (p == (u16 *) softback_end)
2784 p = (u16 *) softback_buf;
2785 if (p == (u16 *) softback_in)
2786 p = (u16 *) vc->vc_origin;
2790 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2792 struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2793 struct fbcon_ops *ops = info->fbcon_par;
2794 struct display *disp = &fb_display[fg_console];
2795 int offset, limit, scrollback_old;
2797 if (softback_top) {
2798 if (vc->vc_num != fg_console)
2799 return 0;
2800 if (vc->vc_mode != KD_TEXT || !lines)
2801 return 0;
2802 if (logo_shown >= 0) {
2803 struct vc_data *conp2 = vc_cons[logo_shown].d;
2805 if (conp2->vc_top == logo_lines
2806 && conp2->vc_bottom == conp2->vc_rows)
2807 conp2->vc_top = 0;
2808 if (logo_shown == vc->vc_num) {
2809 unsigned long p, q;
2810 int i;
2812 p = softback_in;
2813 q = vc->vc_origin +
2814 logo_lines * vc->vc_size_row;
2815 for (i = 0; i < logo_lines; i++) {
2816 if (p == softback_top)
2817 break;
2818 if (p == softback_buf)
2819 p = softback_end;
2820 p -= vc->vc_size_row;
2821 q -= vc->vc_size_row;
2822 scr_memcpyw((u16 *) q, (u16 *) p,
2823 vc->vc_size_row);
2825 softback_in = softback_curr = p;
2826 update_region(vc, vc->vc_origin,
2827 logo_lines * vc->vc_cols);
2829 logo_shown = FBCON_LOGO_CANSHOW;
2831 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2832 fbcon_redraw_softback(vc, disp, lines);
2833 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2834 return 0;
2837 if (!scrollback_phys_max)
2838 return -ENOSYS;
2840 scrollback_old = scrollback_current;
2841 scrollback_current -= lines;
2842 if (scrollback_current < 0)
2843 scrollback_current = 0;
2844 else if (scrollback_current > scrollback_max)
2845 scrollback_current = scrollback_max;
2846 if (scrollback_current == scrollback_old)
2847 return 0;
2849 if (fbcon_is_inactive(vc, info))
2850 return 0;
2852 fbcon_cursor(vc, CM_ERASE);
2854 offset = disp->yscroll - scrollback_current;
2855 limit = disp->vrows;
2856 switch (disp->scrollmode) {
2857 case SCROLL_WRAP_MOVE:
2858 info->var.vmode |= FB_VMODE_YWRAP;
2859 break;
2860 case SCROLL_PAN_MOVE:
2861 case SCROLL_PAN_REDRAW:
2862 limit -= vc->vc_rows;
2863 info->var.vmode &= ~FB_VMODE_YWRAP;
2864 break;
2866 if (offset < 0)
2867 offset += limit;
2868 else if (offset >= limit)
2869 offset -= limit;
2871 ops->var.xoffset = 0;
2872 ops->var.yoffset = offset * vc->vc_font.height;
2873 ops->update_start(info);
2875 if (!scrollback_current)
2876 fbcon_cursor(vc, CM_DRAW);
2877 return 0;
2880 static int fbcon_set_origin(struct vc_data *vc)
2882 if (softback_lines)
2883 fbcon_scrolldelta(vc, softback_lines);
2884 return 0;
2887 static void fbcon_suspended(struct fb_info *info)
2889 struct vc_data *vc = NULL;
2890 struct fbcon_ops *ops = info->fbcon_par;
2892 if (!ops || ops->currcon < 0)
2893 return;
2894 vc = vc_cons[ops->currcon].d;
2896 /* Clear cursor, restore saved data */
2897 fbcon_cursor(vc, CM_ERASE);
2900 static void fbcon_resumed(struct fb_info *info)
2902 struct vc_data *vc;
2903 struct fbcon_ops *ops = info->fbcon_par;
2905 if (!ops || ops->currcon < 0)
2906 return;
2907 vc = vc_cons[ops->currcon].d;
2909 update_screen(vc);
2912 static void fbcon_modechanged(struct fb_info *info)
2914 struct fbcon_ops *ops = info->fbcon_par;
2915 struct vc_data *vc;
2916 struct display *p;
2917 int rows, cols;
2919 if (!ops || ops->currcon < 0)
2920 return;
2921 vc = vc_cons[ops->currcon].d;
2922 if (vc->vc_mode != KD_TEXT ||
2923 registered_fb[con2fb_map[ops->currcon]] != info)
2924 return;
2926 p = &fb_display[vc->vc_num];
2927 set_blitting_type(vc, info);
2929 if (CON_IS_VISIBLE(vc)) {
2930 var_to_display(p, &info->var, info);
2931 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2932 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2933 cols /= vc->vc_font.width;
2934 rows /= vc->vc_font.height;
2935 vc_resize(vc, cols, rows);
2936 updatescrollmode(p, info, vc);
2937 scrollback_max = 0;
2938 scrollback_current = 0;
2940 if (!fbcon_is_inactive(vc, info)) {
2941 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2942 ops->update_start(info);
2945 fbcon_set_palette(vc, color_table);
2946 update_screen(vc);
2947 if (softback_buf)
2948 fbcon_update_softback(vc);
2952 static void fbcon_set_all_vcs(struct fb_info *info)
2954 struct fbcon_ops *ops = info->fbcon_par;
2955 struct vc_data *vc;
2956 struct display *p;
2957 int i, rows, cols, fg = -1;
2959 if (!ops || ops->currcon < 0)
2960 return;
2962 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2963 vc = vc_cons[i].d;
2964 if (!vc || vc->vc_mode != KD_TEXT ||
2965 registered_fb[con2fb_map[i]] != info)
2966 continue;
2968 if (CON_IS_VISIBLE(vc)) {
2969 fg = i;
2970 continue;
2973 p = &fb_display[vc->vc_num];
2974 set_blitting_type(vc, info);
2975 var_to_display(p, &info->var, info);
2976 cols = FBCON_SWAP(p->rotate, info->var.xres, info->var.yres);
2977 rows = FBCON_SWAP(p->rotate, info->var.yres, info->var.xres);
2978 cols /= vc->vc_font.width;
2979 rows /= vc->vc_font.height;
2980 vc_resize(vc, cols, rows);
2983 if (fg != -1)
2984 fbcon_modechanged(info);
2987 static int fbcon_mode_deleted(struct fb_info *info,
2988 struct fb_videomode *mode)
2990 struct fb_info *fb_info;
2991 struct display *p;
2992 int i, j, found = 0;
2994 /* before deletion, ensure that mode is not in use */
2995 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2996 j = con2fb_map[i];
2997 if (j == -1)
2998 continue;
2999 fb_info = registered_fb[j];
3000 if (fb_info != info)
3001 continue;
3002 p = &fb_display[i];
3003 if (!p || !p->mode)
3004 continue;
3005 if (fb_mode_is_equal(p->mode, mode)) {
3006 found = 1;
3007 break;
3010 return found;
3013 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
3014 static int fbcon_unbind(void)
3016 int ret;
3018 ret = unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
3019 fbcon_is_default);
3020 return ret;
3022 #else
3023 static inline int fbcon_unbind(void)
3025 return -EINVAL;
3027 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3029 static int fbcon_fb_unbind(int idx)
3031 int i, new_idx = -1, ret = 0;
3033 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3034 if (con2fb_map[i] != idx &&
3035 con2fb_map[i] != -1) {
3036 new_idx = i;
3037 break;
3041 if (new_idx != -1) {
3042 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3043 if (con2fb_map[i] == idx)
3044 set_con2fb_map(i, new_idx, 0);
3046 } else
3047 ret = fbcon_unbind();
3049 return ret;
3052 static int fbcon_fb_unregistered(struct fb_info *info)
3054 int i, idx = info->node;
3056 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3057 if (con2fb_map[i] == idx)
3058 con2fb_map[i] = -1;
3061 if (idx == info_idx) {
3062 info_idx = -1;
3064 for (i = 0; i < FB_MAX; i++) {
3065 if (registered_fb[i] != NULL) {
3066 info_idx = i;
3067 break;
3072 if (info_idx != -1) {
3073 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3074 if (con2fb_map[i] == -1)
3075 con2fb_map[i] = info_idx;
3079 if (!num_registered_fb)
3080 unregister_con_driver(&fb_con);
3083 if (primary_device == idx)
3084 primary_device = -1;
3086 return 0;
3089 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3090 static void fbcon_select_primary(struct fb_info *info)
3092 if (!map_override && primary_device == -1 &&
3093 fb_is_primary_device(info)) {
3094 int i;
3096 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3097 info->fix.id, info->node);
3098 primary_device = info->node;
3100 for (i = first_fb_vc; i <= last_fb_vc; i++)
3101 con2fb_map_boot[i] = primary_device;
3103 if (con_is_bound(&fb_con)) {
3104 printk(KERN_INFO "fbcon: Remapping primary device, "
3105 "fb%i, to tty %i-%i\n", info->node,
3106 first_fb_vc + 1, last_fb_vc + 1);
3107 info_idx = primary_device;
3112 #else
3113 static inline void fbcon_select_primary(struct fb_info *info)
3115 return;
3117 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3119 static int fbcon_fb_registered(struct fb_info *info)
3121 int ret = 0, i, idx = info->node;
3123 fbcon_select_primary(info);
3125 if (info_idx == -1) {
3126 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3127 if (con2fb_map_boot[i] == idx) {
3128 info_idx = idx;
3129 break;
3133 if (info_idx != -1)
3134 ret = fbcon_takeover(1);
3135 } else {
3136 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3137 if (con2fb_map_boot[i] == idx)
3138 set_con2fb_map(i, idx, 0);
3142 return ret;
3145 static void fbcon_fb_blanked(struct fb_info *info, int blank)
3147 struct fbcon_ops *ops = info->fbcon_par;
3148 struct vc_data *vc;
3150 if (!ops || ops->currcon < 0)
3151 return;
3153 vc = vc_cons[ops->currcon].d;
3154 if (vc->vc_mode != KD_TEXT ||
3155 registered_fb[con2fb_map[ops->currcon]] != info)
3156 return;
3158 if (CON_IS_VISIBLE(vc)) {
3159 if (blank)
3160 do_blank_screen(0);
3161 else
3162 do_unblank_screen(0);
3164 ops->blank_state = blank;
3167 static void fbcon_new_modelist(struct fb_info *info)
3169 int i;
3170 struct vc_data *vc;
3171 struct fb_var_screeninfo var;
3172 const struct fb_videomode *mode;
3174 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3175 if (registered_fb[con2fb_map[i]] != info)
3176 continue;
3177 if (!fb_display[i].mode)
3178 continue;
3179 vc = vc_cons[i].d;
3180 display_to_var(&var, &fb_display[i]);
3181 mode = fb_find_nearest_mode(fb_display[i].mode,
3182 &info->modelist);
3183 fb_videomode_to_var(&var, mode);
3184 fbcon_set_disp(info, &var, vc->vc_num);
3188 static void fbcon_get_requirement(struct fb_info *info,
3189 struct fb_blit_caps *caps)
3191 struct vc_data *vc;
3192 struct display *p;
3194 if (caps->flags) {
3195 int i, charcnt;
3197 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3198 vc = vc_cons[i].d;
3199 if (vc && vc->vc_mode == KD_TEXT &&
3200 info->node == con2fb_map[i]) {
3201 p = &fb_display[i];
3202 caps->x |= 1 << (vc->vc_font.width - 1);
3203 caps->y |= 1 << (vc->vc_font.height - 1);
3204 charcnt = (p->userfont) ?
3205 FNTCHARCNT(p->fontdata) : 256;
3206 if (caps->len < charcnt)
3207 caps->len = charcnt;
3210 } else {
3211 vc = vc_cons[fg_console].d;
3213 if (vc && vc->vc_mode == KD_TEXT &&
3214 info->node == con2fb_map[fg_console]) {
3215 p = &fb_display[fg_console];
3216 caps->x = 1 << (vc->vc_font.width - 1);
3217 caps->y = 1 << (vc->vc_font.height - 1);
3218 caps->len = (p->userfont) ?
3219 FNTCHARCNT(p->fontdata) : 256;
3224 static int fbcon_event_notify(struct notifier_block *self,
3225 unsigned long action, void *data)
3227 struct fb_event *event = data;
3228 struct fb_info *info = event->info;
3229 struct fb_videomode *mode;
3230 struct fb_con2fbmap *con2fb;
3231 struct fb_blit_caps *caps;
3232 int ret = 0;
3235 * ignore all events except driver registration and deregistration
3236 * if fbcon is not active
3238 if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3239 action == FB_EVENT_FB_UNREGISTERED))
3240 goto done;
3242 switch(action) {
3243 case FB_EVENT_SUSPEND:
3244 fbcon_suspended(info);
3245 break;
3246 case FB_EVENT_RESUME:
3247 fbcon_resumed(info);
3248 break;
3249 case FB_EVENT_MODE_CHANGE:
3250 fbcon_modechanged(info);
3251 break;
3252 case FB_EVENT_MODE_CHANGE_ALL:
3253 fbcon_set_all_vcs(info);
3254 break;
3255 case FB_EVENT_MODE_DELETE:
3256 mode = event->data;
3257 ret = fbcon_mode_deleted(info, mode);
3258 break;
3259 case FB_EVENT_FB_UNBIND:
3260 ret = fbcon_fb_unbind(info->node);
3261 break;
3262 case FB_EVENT_FB_REGISTERED:
3263 ret = fbcon_fb_registered(info);
3264 break;
3265 case FB_EVENT_FB_UNREGISTERED:
3266 ret = fbcon_fb_unregistered(info);
3267 break;
3268 case FB_EVENT_SET_CONSOLE_MAP:
3269 con2fb = event->data;
3270 ret = set_con2fb_map(con2fb->console - 1,
3271 con2fb->framebuffer, 1);
3272 break;
3273 case FB_EVENT_GET_CONSOLE_MAP:
3274 con2fb = event->data;
3275 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3276 break;
3277 case FB_EVENT_BLANK:
3278 fbcon_fb_blanked(info, *(int *)event->data);
3279 break;
3280 case FB_EVENT_NEW_MODELIST:
3281 fbcon_new_modelist(info);
3282 break;
3283 case FB_EVENT_GET_REQ:
3284 caps = event->data;
3285 fbcon_get_requirement(info, caps);
3286 break;
3289 done:
3290 return ret;
3294 * The console `switch' structure for the frame buffer based console
3297 static const struct consw fb_con = {
3298 .owner = THIS_MODULE,
3299 .con_startup = fbcon_startup,
3300 .con_init = fbcon_init,
3301 .con_deinit = fbcon_deinit,
3302 .con_clear = fbcon_clear,
3303 .con_putc = fbcon_putc,
3304 .con_putcs = fbcon_putcs,
3305 .con_cursor = fbcon_cursor,
3306 .con_scroll = fbcon_scroll,
3307 .con_bmove = fbcon_bmove,
3308 .con_switch = fbcon_switch,
3309 .con_blank = fbcon_blank,
3310 .con_font_set = fbcon_set_font,
3311 .con_font_get = fbcon_get_font,
3312 .con_font_default = fbcon_set_def_font,
3313 .con_font_copy = fbcon_copy_font,
3314 .con_set_palette = fbcon_set_palette,
3315 .con_scrolldelta = fbcon_scrolldelta,
3316 .con_set_origin = fbcon_set_origin,
3317 .con_invert_region = fbcon_invert_region,
3318 .con_screen_pos = fbcon_screen_pos,
3319 .con_getxy = fbcon_getxy,
3320 .con_resize = fbcon_resize,
3323 static struct notifier_block fbcon_event_notifier = {
3324 .notifier_call = fbcon_event_notify,
3327 static ssize_t store_rotate(struct device *device,
3328 struct device_attribute *attr, const char *buf,
3329 size_t count)
3331 struct fb_info *info;
3332 int rotate, idx;
3333 char **last = NULL;
3335 if (fbcon_has_exited)
3336 return count;
3338 acquire_console_sem();
3339 idx = con2fb_map[fg_console];
3341 if (idx == -1 || registered_fb[idx] == NULL)
3342 goto err;
3344 info = registered_fb[idx];
3345 rotate = simple_strtoul(buf, last, 0);
3346 fbcon_rotate(info, rotate);
3347 err:
3348 release_console_sem();
3349 return count;
3352 static ssize_t store_rotate_all(struct device *device,
3353 struct device_attribute *attr,const char *buf,
3354 size_t count)
3356 struct fb_info *info;
3357 int rotate, idx;
3358 char **last = NULL;
3360 if (fbcon_has_exited)
3361 return count;
3363 acquire_console_sem();
3364 idx = con2fb_map[fg_console];
3366 if (idx == -1 || registered_fb[idx] == NULL)
3367 goto err;
3369 info = registered_fb[idx];
3370 rotate = simple_strtoul(buf, last, 0);
3371 fbcon_rotate_all(info, rotate);
3372 err:
3373 release_console_sem();
3374 return count;
3377 static ssize_t show_rotate(struct device *device,
3378 struct device_attribute *attr,char *buf)
3380 struct fb_info *info;
3381 int rotate = 0, idx;
3383 if (fbcon_has_exited)
3384 return 0;
3386 acquire_console_sem();
3387 idx = con2fb_map[fg_console];
3389 if (idx == -1 || registered_fb[idx] == NULL)
3390 goto err;
3392 info = registered_fb[idx];
3393 rotate = fbcon_get_rotate(info);
3394 err:
3395 release_console_sem();
3396 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3399 static ssize_t show_cursor_blink(struct device *device,
3400 struct device_attribute *attr, char *buf)
3402 struct fb_info *info;
3403 struct fbcon_ops *ops;
3404 int idx, blink = -1;
3406 if (fbcon_has_exited)
3407 return 0;
3409 acquire_console_sem();
3410 idx = con2fb_map[fg_console];
3412 if (idx == -1 || registered_fb[idx] == NULL)
3413 goto err;
3415 info = registered_fb[idx];
3416 ops = info->fbcon_par;
3418 if (!ops)
3419 goto err;
3421 blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3422 err:
3423 release_console_sem();
3424 return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3427 static ssize_t store_cursor_blink(struct device *device,
3428 struct device_attribute *attr,
3429 const char *buf, size_t count)
3431 struct fb_info *info;
3432 int blink, idx;
3433 char **last = NULL;
3435 if (fbcon_has_exited)
3436 return count;
3438 acquire_console_sem();
3439 idx = con2fb_map[fg_console];
3441 if (idx == -1 || registered_fb[idx] == NULL)
3442 goto err;
3444 info = registered_fb[idx];
3446 if (!info->fbcon_par)
3447 goto err;
3449 blink = simple_strtoul(buf, last, 0);
3451 if (blink) {
3452 fbcon_cursor_noblink = 0;
3453 fbcon_add_cursor_timer(info);
3454 } else {
3455 fbcon_cursor_noblink = 1;
3456 fbcon_del_cursor_timer(info);
3459 err:
3460 release_console_sem();
3461 return count;
3464 static struct device_attribute device_attrs[] = {
3465 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3466 __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3467 __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3468 store_cursor_blink),
3471 static int fbcon_init_device(void)
3473 int i, error = 0;
3475 fbcon_has_sysfs = 1;
3477 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3478 error = device_create_file(fbcon_device, &device_attrs[i]);
3480 if (error)
3481 break;
3484 if (error) {
3485 while (--i >= 0)
3486 device_remove_file(fbcon_device, &device_attrs[i]);
3488 fbcon_has_sysfs = 0;
3491 return 0;
3494 static void fbcon_start(void)
3496 if (num_registered_fb) {
3497 int i;
3499 acquire_console_sem();
3501 for (i = 0; i < FB_MAX; i++) {
3502 if (registered_fb[i] != NULL) {
3503 info_idx = i;
3504 break;
3508 release_console_sem();
3509 fbcon_takeover(0);
3513 static void fbcon_exit(void)
3515 struct fb_info *info;
3516 int i, j, mapped;
3518 if (fbcon_has_exited)
3519 return;
3521 #ifdef CONFIG_ATARI
3522 free_irq(IRQ_AUTO_4, fb_vbl_handler);
3523 #endif
3524 #ifdef CONFIG_MAC
3525 if (MACH_IS_MAC && vbl_detected)
3526 free_irq(IRQ_MAC_VBL, fb_vbl_handler);
3527 #endif
3529 kfree((void *)softback_buf);
3530 softback_buf = 0UL;
3532 for (i = 0; i < FB_MAX; i++) {
3533 mapped = 0;
3534 info = registered_fb[i];
3536 if (info == NULL)
3537 continue;
3539 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3540 if (con2fb_map[j] == i)
3541 mapped = 1;
3544 if (mapped) {
3545 if (info->fbops->fb_release)
3546 info->fbops->fb_release(info, 0);
3547 module_put(info->fbops->owner);
3549 if (info->fbcon_par) {
3550 struct fbcon_ops *ops = info->fbcon_par;
3552 fbcon_del_cursor_timer(info);
3553 kfree(ops->cursor_src);
3554 kfree(info->fbcon_par);
3555 info->fbcon_par = NULL;
3558 if (info->queue.func == fb_flashcursor)
3559 info->queue.func = NULL;
3563 fbcon_has_exited = 1;
3566 static int __init fb_console_init(void)
3568 int i;
3570 acquire_console_sem();
3571 fb_register_client(&fbcon_event_notifier);
3572 fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), "fbcon");
3574 if (IS_ERR(fbcon_device)) {
3575 printk(KERN_WARNING "Unable to create device "
3576 "for fbcon; errno = %ld\n",
3577 PTR_ERR(fbcon_device));
3578 fbcon_device = NULL;
3579 } else
3580 fbcon_init_device();
3582 for (i = 0; i < MAX_NR_CONSOLES; i++)
3583 con2fb_map[i] = -1;
3585 release_console_sem();
3586 fbcon_start();
3587 return 0;
3590 module_init(fb_console_init);
3592 #ifdef MODULE
3594 static void __exit fbcon_deinit_device(void)
3596 int i;
3598 if (fbcon_has_sysfs) {
3599 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3600 device_remove_file(fbcon_device, &device_attrs[i]);
3602 fbcon_has_sysfs = 0;
3606 static void __exit fb_console_exit(void)
3608 acquire_console_sem();
3609 fb_unregister_client(&fbcon_event_notifier);
3610 fbcon_deinit_device();
3611 device_destroy(fb_class, MKDEV(0, 0));
3612 fbcon_exit();
3613 release_console_sem();
3614 unregister_con_driver(&fb_con);
3617 module_exit(fb_console_exit);
3619 #endif
3621 MODULE_LICENSE("GPL");