[PATCH] fbcon: Consolidate redundant code
[safe/jmp/linux-2.6] / drivers / video / console / fbcon.c
1 /*
2  *  linux/drivers/video/fbcon.c -- Low level frame buffer based console driver
3  *
4  *      Copyright (C) 1995 Geert Uytterhoeven
5  *
6  *
7  *  This file is based on the original Amiga console driver (amicon.c):
8  *
9  *      Copyright (C) 1993 Hamish Macdonald
10  *                         Greg Harp
11  *      Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
12  *
13  *            with work by William Rucklidge (wjr@cs.cornell.edu)
14  *                         Geert Uytterhoeven
15  *                         Jes Sorensen (jds@kom.auc.dk)
16  *                         Martin Apel
17  *
18  *  and on the original Atari console driver (atacon.c):
19  *
20  *      Copyright (C) 1993 Bjoern Brauel
21  *                         Roman Hodek
22  *
23  *            with work by Guenther Kelleter
24  *                         Martin Schaller
25  *                         Andreas Schwab
26  *
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)
31  *
32  *  Random hacking by Martin Mares <mj@ucw.cz>
33  *
34  *      2001 - Documented with DocBook
35  *      - Brad Douglas <brad@neruo.com>
36  *
37  *  The low level operations for the various display memory organizations are
38  *  now in separate source files.
39  *
40  *  Currently the following organizations are supported:
41  *
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
48  *
49  *  To do:
50  *
51  *    - Implement 16 plane mode (iplan2p16)
52  *
53  *
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.
57  */
58
59 #undef FBCONDEBUG
60
61 #include <linux/config.h>
62 #include <linux/module.h>
63 #include <linux/types.h>
64 #include <linux/sched.h>
65 #include <linux/fs.h>
66 #include <linux/kernel.h>
67 #include <linux/delay.h>        /* MSch: for IRQ probe */
68 #include <linux/tty.h>
69 #include <linux/console.h>
70 #include <linux/string.h>
71 #include <linux/kd.h>
72 #include <linux/slab.h>
73 #include <linux/fb.h>
74 #include <linux/vt_kern.h>
75 #include <linux/selection.h>
76 #include <linux/font.h>
77 #include <linux/smp.h>
78 #include <linux/init.h>
79 #include <linux/interrupt.h>
80 #include <linux/crc32.h> /* For counting font checksums */
81 #include <asm/irq.h>
82 #include <asm/system.h>
83 #include <asm/uaccess.h>
84 #ifdef CONFIG_ATARI
85 #include <asm/atariints.h>
86 #endif
87 #ifdef CONFIG_MAC
88 #include <asm/macints.h>
89 #endif
90 #if defined(__mc68000__) || defined(CONFIG_APUS)
91 #include <asm/machdep.h>
92 #include <asm/setup.h>
93 #endif
94
95 #include "fbcon.h"
96
97 #ifdef FBCONDEBUG
98 #  define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
99 #else
100 #  define DPRINTK(fmt, args...)
101 #endif
102
103 enum {
104         FBCON_LOGO_CANSHOW      = -1,   /* the logo can be shown */
105         FBCON_LOGO_DRAW         = -2,   /* draw the logo to a console */
106         FBCON_LOGO_DONTSHOW     = -3    /* do not show the logo */
107 };
108
109 struct display fb_display[MAX_NR_CONSOLES];
110 static signed char con2fb_map[MAX_NR_CONSOLES];
111 static signed char con2fb_map_boot[MAX_NR_CONSOLES];
112 static int logo_height;
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 /* font data */
128 static char fontname[40];
129
130 /* current fb_info */
131 static int info_idx = -1;
132
133 static const struct consw fb_con;
134
135 #define CM_SOFTBACK     (8)
136
137 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
138
139 static void fbcon_free_font(struct display *);
140 static int fbcon_set_origin(struct vc_data *);
141
142 #define CURSOR_DRAW_DELAY               (1)
143
144 /* # VBL ints between cursor state changes */
145 #define ATARI_CURSOR_BLINK_RATE         (42)
146 #define MAC_CURSOR_BLINK_RATE           (32)
147 #define DEFAULT_CURSOR_BLINK_RATE       (20)
148
149 static int vbl_cursor_cnt;
150
151 #define divides(a, b)   ((!(a) || (b)%(a)) ? 0 : 1)
152
153 /*
154  *  Interface used by the world
155  */
156
157 static const char *fbcon_startup(void);
158 static void fbcon_init(struct vc_data *vc, int init);
159 static void fbcon_deinit(struct vc_data *vc);
160 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
161                         int width);
162 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
163 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
164                         int count, int ypos, int xpos);
165 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
166 static void fbcon_cursor(struct vc_data *vc, int mode);
167 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
168                         int count);
169 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
170                         int height, int width);
171 static int fbcon_switch(struct vc_data *vc);
172 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
173 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
174 static int fbcon_scrolldelta(struct vc_data *vc, int lines);
175
176 /*
177  *  Internal routines
178  */
179 static __inline__ int real_y(struct display *p, int ypos);
180 static __inline__ void ywrap_up(struct vc_data *vc, int count);
181 static __inline__ void ywrap_down(struct vc_data *vc, int count);
182 static __inline__ void ypan_up(struct vc_data *vc, int count);
183 static __inline__ void ypan_down(struct vc_data *vc, int count);
184 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
185                             int dy, int dx, int height, int width, u_int y_break);
186 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
187                            struct vc_data *vc);
188 static void fbcon_preset_disp(struct fb_info *info, struct fb_var_screeninfo *var,
189                               int unit);
190 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
191                               int line, int count, int dy);
192
193 #ifdef CONFIG_MAC
194 /*
195  * On the Macintoy, there may or may not be a working VBL int. We need to probe
196  */
197 static int vbl_detected;
198
199 static irqreturn_t fb_vbl_detect(int irq, void *dummy, struct pt_regs *fp)
200 {
201         vbl_detected++;
202         return IRQ_HANDLED;
203 }
204 #endif
205
206 static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
207 {
208         struct fbcon_ops *ops = info->fbcon_par;
209
210         return (info->state != FBINFO_STATE_RUNNING ||
211                 vc->vc_mode != KD_TEXT || ops->graphics);
212 }
213
214 static inline int get_color(struct vc_data *vc, struct fb_info *info,
215               u16 c, int is_fg)
216 {
217         int depth = fb_get_color_depth(&info->var, &info->fix);
218         int color = 0;
219
220         if (console_blanked) {
221                 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
222
223                 c = vc->vc_video_erase_char & charmask;
224         }
225
226         if (depth != 1)
227                 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
228                         : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
229
230         switch (depth) {
231         case 1:
232         {
233                 int col = ~(0xfff << (max(info->var.green.length,
234                                           max(info->var.red.length,
235                                               info->var.blue.length)))) & 0xff;
236
237                 /* 0 or 1 */
238                 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
239                 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
240
241                 if (console_blanked)
242                         fg = bg;
243
244                 color = (is_fg) ? fg : bg;
245                 break;
246         }
247         case 2:
248                 /*
249                  * Scale down 16-colors to 4 colors. Default 4-color palette
250                  * is grayscale. However, simply dividing the values by 4
251                  * will not work, as colors 1, 2 and 3 will be scaled-down
252                  * to zero rendering them invisible.  So empirically convert
253                  * colors to a sane 4-level grayscale.
254                  */
255                 switch (color) {
256                 case 0:
257                         color = 0; /* black */
258                         break;
259                 case 1 ... 6:
260                         color = 2; /* white */
261                         break;
262                 case 7 ... 8:
263                         color = 1; /* gray */
264                         break;
265                 default:
266                         color = 3; /* intense white */
267                         break;
268                 }
269                 break;
270         case 3:
271                 /*
272                  * Last 8 entries of default 16-color palette is a more intense
273                  * version of the first 8 (i.e., same chrominance, different
274                  * luminance).
275                  */
276                 color &= 7;
277                 break;
278         }
279
280
281         return color;
282 }
283
284 static void fbcon_update_softback(struct vc_data *vc)
285 {
286         int l = fbcon_softback_size / vc->vc_size_row;
287
288         if (l > 5)
289                 softback_end = softback_buf + l * vc->vc_size_row;
290         else
291                 /* Smaller scrollback makes no sense, and 0 would screw
292                    the operation totally */
293                 softback_top = 0;
294 }
295
296 static void fb_flashcursor(void *private)
297 {
298         struct fb_info *info = private;
299         struct fbcon_ops *ops = info->fbcon_par;
300         struct display *p;
301         struct vc_data *vc = NULL;
302         int c;
303         int mode;
304
305         if (ops->currcon != -1)
306                 vc = vc_cons[ops->currcon].d;
307
308         if (!vc || !CON_IS_VISIBLE(vc) ||
309             fbcon_is_inactive(vc, info) ||
310             registered_fb[con2fb_map[vc->vc_num]] != info ||
311             vc_cons[ops->currcon].d->vc_deccm != 1)
312                 return;
313         acquire_console_sem();
314         p = &fb_display[vc->vc_num];
315         c = scr_readw((u16 *) vc->vc_pos);
316         mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
317                 CM_ERASE : CM_DRAW;
318         ops->cursor(vc, info, p, mode, softback_lines, get_color(vc, info, c, 1),
319                     get_color(vc, info, c, 0));
320         release_console_sem();
321 }
322
323 #if defined(CONFIG_ATARI) || defined(CONFIG_MAC)
324 static int cursor_blink_rate;
325 static irqreturn_t fb_vbl_handler(int irq, void *dev_id, struct pt_regs *fp)
326 {
327         struct fb_info *info = dev_id;
328
329         if (vbl_cursor_cnt && --vbl_cursor_cnt == 0) {
330                 schedule_work(&info->queue);    
331                 vbl_cursor_cnt = cursor_blink_rate; 
332         }
333         return IRQ_HANDLED;
334 }
335 #endif
336         
337 static void cursor_timer_handler(unsigned long dev_addr)
338 {
339         struct fb_info *info = (struct fb_info *) dev_addr;
340         struct fbcon_ops *ops = info->fbcon_par;
341
342         schedule_work(&info->queue);
343         mod_timer(&ops->cursor_timer, jiffies + HZ/5);
344 }
345
346 static void fbcon_add_cursor_timer(struct fb_info *info)
347 {
348         struct fbcon_ops *ops = info->fbcon_par;
349
350         if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
351             !(ops->flags & FBCON_FLAGS_CURSOR_TIMER)) {
352                 if (!info->queue.func)
353                         INIT_WORK(&info->queue, fb_flashcursor, info);
354
355                 init_timer(&ops->cursor_timer);
356                 ops->cursor_timer.function = cursor_timer_handler;
357                 ops->cursor_timer.expires = jiffies + HZ / 5;
358                 ops->cursor_timer.data = (unsigned long ) info;
359                 add_timer(&ops->cursor_timer);
360                 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
361         }
362 }
363
364 static void fbcon_del_cursor_timer(struct fb_info *info)
365 {
366         struct fbcon_ops *ops = info->fbcon_par;
367
368         if (info->queue.func == fb_flashcursor &&
369             ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
370                 del_timer_sync(&ops->cursor_timer);
371                 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
372         }
373 }
374
375 #ifndef MODULE
376 static int __init fb_console_setup(char *this_opt)
377 {
378         char *options;
379         int i, j;
380
381         if (!this_opt || !*this_opt)
382                 return 0;
383
384         while ((options = strsep(&this_opt, ",")) != NULL) {
385                 if (!strncmp(options, "font:", 5))
386                         strcpy(fontname, options + 5);
387                 
388                 if (!strncmp(options, "scrollback:", 11)) {
389                         options += 11;
390                         if (*options) {
391                                 fbcon_softback_size = simple_strtoul(options, &options, 0);
392                                 if (*options == 'k' || *options == 'K') {
393                                         fbcon_softback_size *= 1024;
394                                         options++;
395                                 }
396                                 if (*options != ',')
397                                         return 0;
398                                 options++;
399                         } else
400                                 return 0;
401                 }
402                 
403                 if (!strncmp(options, "map:", 4)) {
404                         options += 4;
405                         if (*options)
406                                 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
407                                         if (!options[j])
408                                                 j = 0;
409                                         con2fb_map_boot[i] =
410                                                 (options[j++]-'0') % FB_MAX;
411                                 }
412                         return 0;
413                 }
414
415                 if (!strncmp(options, "vc:", 3)) {
416                         options += 3;
417                         if (*options)
418                                 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
419                         if (first_fb_vc < 0)
420                                 first_fb_vc = 0;
421                         if (*options++ == '-')
422                                 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
423                         fbcon_is_default = 0; 
424                 }       
425         }
426         return 0;
427 }
428
429 __setup("fbcon=", fb_console_setup);
430 #endif
431
432 static int search_fb_in_map(int idx)
433 {
434         int i, retval = 0;
435
436         for (i = 0; i < MAX_NR_CONSOLES; i++) {
437                 if (con2fb_map[i] == idx)
438                         retval = 1;
439         }
440         return retval;
441 }
442
443 static int search_for_mapped_con(void)
444 {
445         int i, retval = 0;
446
447         for (i = 0; i < MAX_NR_CONSOLES; i++) {
448                 if (con2fb_map[i] != -1)
449                         retval = 1;
450         }
451         return retval;
452 }
453
454 static int fbcon_takeover(int show_logo)
455 {
456         int err, i;
457
458         if (!num_registered_fb)
459                 return -ENODEV;
460
461         if (!show_logo)
462                 logo_shown = FBCON_LOGO_DONTSHOW;
463
464         for (i = first_fb_vc; i <= last_fb_vc; i++)
465                 con2fb_map[i] = info_idx;
466
467         err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
468                                 fbcon_is_default);
469         if (err) {
470                 for (i = first_fb_vc; i <= last_fb_vc; i++) {
471                         con2fb_map[i] = -1;
472                 }
473                 info_idx = -1;
474         }
475
476         return err;
477 }
478
479 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
480                                int cols, int rows, int new_cols, int new_rows)
481 {
482         /* Need to make room for the logo */
483         int cnt, erase = vc->vc_video_erase_char, step;
484         unsigned short *save = NULL, *r, *q;
485
486         /*
487          * remove underline attribute from erase character
488          * if black and white framebuffer.
489          */
490         if (fb_get_color_depth(&info->var, &info->fix) == 1)
491                 erase &= ~0x400;
492         logo_height = fb_prepare_logo(info);
493         logo_lines = (logo_height + vc->vc_font.height - 1) /
494                 vc->vc_font.height;
495         q = (unsigned short *) (vc->vc_origin +
496                                 vc->vc_size_row * rows);
497         step = logo_lines * cols;
498         for (r = q - logo_lines * cols; r < q; r++)
499                 if (scr_readw(r) != vc->vc_video_erase_char)
500                         break;
501         if (r != q && new_rows >= rows + logo_lines) {
502                 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
503                 if (save) {
504                         int i = cols < new_cols ? cols : new_cols;
505                         scr_memsetw(save, erase, logo_lines * new_cols * 2);
506                         r = q - step;
507                         for (cnt = 0; cnt < logo_lines; cnt++, r += i)
508                                 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
509                         r = q;
510                 }
511         }
512         if (r == q) {
513                 /* We can scroll screen down */
514                 r = q - step - cols;
515                 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
516                         scr_memcpyw(r + step, r, vc->vc_size_row);
517                         r -= cols;
518                 }
519                 if (!save) {
520                         vc->vc_y += logo_lines;
521                         vc->vc_pos += logo_lines * vc->vc_size_row;
522                 }
523         }
524         scr_memsetw((unsigned short *) vc->vc_origin,
525                     erase,
526                     vc->vc_size_row * logo_lines);
527
528         if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
529                 fbcon_clear_margins(vc, 0);
530                 update_screen(vc);
531         }
532
533         if (save) {
534                 q = (unsigned short *) (vc->vc_origin +
535                                         vc->vc_size_row *
536                                         rows);
537                 scr_memcpyw(q, save, logo_lines * new_cols * 2);
538                 vc->vc_y += logo_lines;
539                 vc->vc_pos += logo_lines * vc->vc_size_row;
540                 kfree(save);
541         }
542
543         if (logo_lines > vc->vc_bottom) {
544                 logo_shown = FBCON_LOGO_CANSHOW;
545                 printk(KERN_INFO
546                        "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
547         } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
548                 logo_shown = FBCON_LOGO_DRAW;
549                 vc->vc_top = logo_lines;
550         }
551 }
552
553 #ifdef CONFIG_FB_TILEBLITTING
554 static void set_blitting_type(struct vc_data *vc, struct fb_info *info,
555                               struct display *p)
556 {
557         struct fbcon_ops *ops = info->fbcon_par;
558
559         if ((info->flags & FBINFO_MISC_TILEBLITTING))
560                 fbcon_set_tileops(vc, info, p, ops);
561         else
562                 fbcon_set_bitops(ops);
563 }
564 #else
565 static void set_blitting_type(struct vc_data *vc, struct fb_info *info,
566                               struct display *p)
567 {
568         struct fbcon_ops *ops = info->fbcon_par;
569
570         info->flags &= ~FBINFO_MISC_TILEBLITTING;
571         fbcon_set_bitops(ops);
572 }
573 #endif /* CONFIG_MISC_TILEBLITTING */
574
575
576 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
577                                   int unit, int oldidx)
578 {
579         struct fbcon_ops *ops = NULL;
580         int err = 0;
581
582         if (!try_module_get(info->fbops->owner))
583                 err = -ENODEV;
584
585         if (!err && info->fbops->fb_open &&
586             info->fbops->fb_open(info, 0))
587                 err = -ENODEV;
588
589         if (!err) {
590                 ops = kmalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
591                 if (!ops)
592                         err = -ENOMEM;
593         }
594
595         if (!err) {
596                 memset(ops, 0, sizeof(struct fbcon_ops));
597                 info->fbcon_par = ops;
598                 set_blitting_type(vc, info, NULL);
599         }
600
601         if (err) {
602                 con2fb_map[unit] = oldidx;
603                 module_put(info->fbops->owner);
604         }
605
606         return err;
607 }
608
609 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
610                                   struct fb_info *newinfo, int unit,
611                                   int oldidx, int found)
612 {
613         struct fbcon_ops *ops = oldinfo->fbcon_par;
614         int err = 0;
615
616         if (oldinfo->fbops->fb_release &&
617             oldinfo->fbops->fb_release(oldinfo, 0)) {
618                 con2fb_map[unit] = oldidx;
619                 if (!found && newinfo->fbops->fb_release)
620                         newinfo->fbops->fb_release(newinfo, 0);
621                 if (!found)
622                         module_put(newinfo->fbops->owner);
623                 err = -ENODEV;
624         }
625
626         if (!err) {
627                 fbcon_del_cursor_timer(oldinfo);
628                 kfree(ops->cursor_state.mask);
629                 kfree(ops->cursor_data);
630                 kfree(oldinfo->fbcon_par);
631                 oldinfo->fbcon_par = NULL;
632                 module_put(oldinfo->fbops->owner);
633         }
634
635         return err;
636 }
637
638 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
639                                 int unit, int show_logo)
640 {
641         struct fbcon_ops *ops = info->fbcon_par;
642
643         ops->currcon = fg_console;
644
645         if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT))
646                 info->fbops->fb_set_par(info);
647
648         ops->flags |= FBCON_FLAGS_INIT;
649         ops->graphics = 0;
650
651         if (vc)
652                 fbcon_set_disp(info, &info->var, vc);
653         else
654                 fbcon_preset_disp(info, &info->var, unit);
655
656         if (show_logo) {
657                 struct vc_data *fg_vc = vc_cons[fg_console].d;
658                 struct fb_info *fg_info =
659                         registered_fb[con2fb_map[fg_console]];
660
661                 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
662                                    fg_vc->vc_rows, fg_vc->vc_cols,
663                                    fg_vc->vc_rows);
664         }
665
666         update_screen(vc_cons[fg_console].d);
667 }
668
669 /**
670  *      set_con2fb_map - map console to frame buffer device
671  *      @unit: virtual console number to map
672  *      @newidx: frame buffer index to map virtual console to
673  *      @user: user request
674  *
675  *      Maps a virtual console @unit to a frame buffer device
676  *      @newidx.
677  */
678 static int set_con2fb_map(int unit, int newidx, int user)
679 {
680         struct vc_data *vc = vc_cons[unit].d;
681         int oldidx = con2fb_map[unit];
682         struct fb_info *info = registered_fb[newidx];
683         struct fb_info *oldinfo = NULL;
684         int found, err = 0;
685
686         if (oldidx == newidx)
687                 return 0;
688
689         if (!info)
690                 err =  -EINVAL;
691
692         if (!err && !search_for_mapped_con()) {
693                 info_idx = newidx;
694                 return fbcon_takeover(0);
695         }
696
697         if (oldidx != -1)
698                 oldinfo = registered_fb[oldidx];
699
700         found = search_fb_in_map(newidx);
701
702         acquire_console_sem();
703         con2fb_map[unit] = newidx;
704         if (!err && !found)
705                 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
706
707
708         /*
709          * If old fb is not mapped to any of the consoles,
710          * fbcon should release it.
711          */
712         if (!err && oldinfo && !search_fb_in_map(oldidx))
713                 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
714                                              found);
715
716         if (!err) {
717                 int show_logo = (fg_console == 0 && !user &&
718                                  logo_shown != FBCON_LOGO_DONTSHOW);
719
720                 if (!found)
721                         fbcon_add_cursor_timer(info);
722                 con2fb_map_boot[unit] = newidx;
723                 con2fb_init_display(vc, info, unit, show_logo);
724         }
725
726         release_console_sem();
727         return err;
728 }
729
730 /*
731  *  Low Level Operations
732  */
733 /* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
734 static int var_to_display(struct display *disp,
735                           struct fb_var_screeninfo *var,
736                           struct fb_info *info)
737 {
738         disp->xres_virtual = var->xres_virtual;
739         disp->yres_virtual = var->yres_virtual;
740         disp->bits_per_pixel = var->bits_per_pixel;
741         disp->grayscale = var->grayscale;
742         disp->nonstd = var->nonstd;
743         disp->accel_flags = var->accel_flags;
744         disp->height = var->height;
745         disp->width = var->width;
746         disp->red = var->red;
747         disp->green = var->green;
748         disp->blue = var->blue;
749         disp->transp = var->transp;
750         disp->rotate = var->rotate;
751         disp->mode = fb_match_mode(var, &info->modelist);
752         if (disp->mode == NULL)
753                 /* This should not happen */
754                 return -EINVAL;
755         return 0;
756 }
757
758 static void display_to_var(struct fb_var_screeninfo *var,
759                            struct display *disp)
760 {
761         fb_videomode_to_var(var, disp->mode);
762         var->xres_virtual = disp->xres_virtual;
763         var->yres_virtual = disp->yres_virtual;
764         var->bits_per_pixel = disp->bits_per_pixel;
765         var->grayscale = disp->grayscale;
766         var->nonstd = disp->nonstd;
767         var->accel_flags = disp->accel_flags;
768         var->height = disp->height;
769         var->width = disp->width;
770         var->red = disp->red;
771         var->green = disp->green;
772         var->blue = disp->blue;
773         var->transp = disp->transp;
774         var->rotate = disp->rotate;
775 }
776
777 static const char *fbcon_startup(void)
778 {
779         const char *display_desc = "frame buffer device";
780         struct display *p = &fb_display[fg_console];
781         struct vc_data *vc = vc_cons[fg_console].d;
782         const struct font_desc *font = NULL;
783         struct module *owner;
784         struct fb_info *info = NULL;
785         struct fbcon_ops *ops;
786         int rows, cols;
787         int irqres;
788
789         irqres = 1;
790         /*
791          *  If num_registered_fb is zero, this is a call for the dummy part.
792          *  The frame buffer devices weren't initialized yet.
793          */
794         if (!num_registered_fb || info_idx == -1)
795                 return display_desc;
796         /*
797          * Instead of blindly using registered_fb[0], we use info_idx, set by
798          * fb_console_init();
799          */
800         info = registered_fb[info_idx];
801         if (!info)
802                 return NULL;
803         
804         owner = info->fbops->owner;
805         if (!try_module_get(owner))
806                 return NULL;
807         if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
808                 module_put(owner);
809                 return NULL;
810         }
811
812         ops = kmalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
813         if (!ops) {
814                 module_put(owner);
815                 return NULL;
816         }
817
818         memset(ops, 0, sizeof(struct fbcon_ops));
819         ops->currcon = -1;
820         ops->graphics = 1;
821         info->fbcon_par = ops;
822         set_blitting_type(vc, info, NULL);
823
824         if (info->fix.type != FB_TYPE_TEXT) {
825                 if (fbcon_softback_size) {
826                         if (!softback_buf) {
827                                 softback_buf =
828                                     (unsigned long)
829                                     kmalloc(fbcon_softback_size,
830                                             GFP_KERNEL);
831                                 if (!softback_buf) {
832                                         fbcon_softback_size = 0;
833                                         softback_top = 0;
834                                 }
835                         }
836                 } else {
837                         if (softback_buf) {
838                                 kfree((void *) softback_buf);
839                                 softback_buf = 0;
840                                 softback_top = 0;
841                         }
842                 }
843                 if (softback_buf)
844                         softback_in = softback_top = softback_curr =
845                             softback_buf;
846                 softback_lines = 0;
847         }
848
849         /* Setup default font */
850         if (!p->fontdata) {
851                 if (!fontname[0] || !(font = find_font(fontname)))
852                         font = get_default_font(info->var.xres,
853                                                 info->var.yres);
854                 vc->vc_font.width = font->width;
855                 vc->vc_font.height = font->height;
856                 vc->vc_font.data = (void *)(p->fontdata = font->data);
857                 vc->vc_font.charcount = 256; /* FIXME  Need to support more fonts */
858         }
859
860         cols = info->var.xres / vc->vc_font.width;
861         rows = info->var.yres / vc->vc_font.height;
862         vc_resize(vc, cols, rows);
863
864         DPRINTK("mode:   %s\n", info->fix.id);
865         DPRINTK("visual: %d\n", info->fix.visual);
866         DPRINTK("res:    %dx%d-%d\n", info->var.xres,
867                 info->var.yres,
868                 info->var.bits_per_pixel);
869
870 #ifdef CONFIG_ATARI
871         if (MACH_IS_ATARI) {
872                 cursor_blink_rate = ATARI_CURSOR_BLINK_RATE;
873                 irqres =
874                     request_irq(IRQ_AUTO_4, fb_vbl_handler,
875                                 IRQ_TYPE_PRIO, "framebuffer vbl",
876                                 info);
877         }
878 #endif                          /* CONFIG_ATARI */
879
880 #ifdef CONFIG_MAC
881         /*
882          * On a Macintoy, the VBL interrupt may or may not be active. 
883          * As interrupt based cursor is more reliable and race free, we 
884          * probe for VBL interrupts.
885          */
886         if (MACH_IS_MAC) {
887                 int ct = 0;
888                 /*
889                  * Probe for VBL: set temp. handler ...
890                  */
891                 irqres = request_irq(IRQ_MAC_VBL, fb_vbl_detect, 0,
892                                      "framebuffer vbl", info);
893                 vbl_detected = 0;
894
895                 /*
896                  * ... and spin for 20 ms ...
897                  */
898                 while (!vbl_detected && ++ct < 1000)
899                         udelay(20);
900
901                 if (ct == 1000)
902                         printk
903                             ("fbcon_startup: No VBL detected, using timer based cursor.\n");
904
905                 free_irq(IRQ_MAC_VBL, fb_vbl_detect);
906
907                 if (vbl_detected) {
908                         /*
909                          * interrupt based cursor ok
910                          */
911                         cursor_blink_rate = MAC_CURSOR_BLINK_RATE;
912                         irqres =
913                             request_irq(IRQ_MAC_VBL, fb_vbl_handler, 0,
914                                         "framebuffer vbl", info);
915                 } else {
916                         /*
917                          * VBL not detected: fall through, use timer based cursor
918                          */
919                         irqres = 1;
920                 }
921         }
922 #endif                          /* CONFIG_MAC */
923
924         fbcon_add_cursor_timer(info);
925         return display_desc;
926 }
927
928 static void fbcon_init(struct vc_data *vc, int init)
929 {
930         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
931         struct fbcon_ops *ops;
932         struct vc_data **default_mode = vc->vc_display_fg;
933         struct vc_data *svc = *default_mode;
934         struct display *t, *p = &fb_display[vc->vc_num];
935         int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
936         int cap;
937
938         if (info_idx == -1 || info == NULL)
939             return;
940
941         cap = info->flags;
942
943         if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
944             (info->fix.type == FB_TYPE_TEXT))
945                 logo = 0;
946
947         info->var.xoffset = info->var.yoffset = p->yscroll = 0; /* reset wrap/pan */
948
949         if (var_to_display(p, &info->var, info))
950                 return;
951
952         /* If we are not the first console on this
953            fb, copy the font from that console */
954         t = &fb_display[svc->vc_num];
955         if (!vc->vc_font.data) {
956                 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
957                 vc->vc_font.width = (*default_mode)->vc_font.width;
958                 vc->vc_font.height = (*default_mode)->vc_font.height;
959                 p->userfont = t->userfont;
960                 if (p->userfont)
961                         REFCOUNT(p->fontdata)++;
962         }
963         if (p->userfont)
964                 charcnt = FNTCHARCNT(p->fontdata);
965         vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
966         vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
967         if (charcnt == 256) {
968                 vc->vc_hi_font_mask = 0;
969         } else {
970                 vc->vc_hi_font_mask = 0x100;
971                 if (vc->vc_can_do_color)
972                         vc->vc_complement_mask <<= 1;
973         }
974
975         if (!*svc->vc_uni_pagedir_loc)
976                 con_set_default_unimap(svc);
977         if (!*vc->vc_uni_pagedir_loc)
978                 con_copy_unimap(vc, svc);
979
980         cols = vc->vc_cols;
981         rows = vc->vc_rows;
982         new_cols = info->var.xres / vc->vc_font.width;
983         new_rows = info->var.yres / vc->vc_font.height;
984         vc_resize(vc, new_cols, new_rows);
985
986         ops = info->fbcon_par;
987         /*
988          * We must always set the mode. The mode of the previous console
989          * driver could be in the same resolution but we are using different
990          * hardware so we have to initialize the hardware.
991          *
992          * We need to do it in fbcon_init() to prevent screen corruption.
993          */
994         if (CON_IS_VISIBLE(vc)) {
995                 if (info->fbops->fb_set_par &&
996                     !(ops->flags & FBCON_FLAGS_INIT))
997                         info->fbops->fb_set_par(info);
998                 ops->flags |= FBCON_FLAGS_INIT;
999         }
1000
1001         ops->graphics = 0;
1002
1003         if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1004             !(cap & FBINFO_HWACCEL_DISABLED))
1005                 p->scrollmode = SCROLL_MOVE;
1006         else /* default to something safe */
1007                 p->scrollmode = SCROLL_REDRAW;
1008
1009         /*
1010          *  ++guenther: console.c:vc_allocate() relies on initializing
1011          *  vc_{cols,rows}, but we must not set those if we are only
1012          *  resizing the console.
1013          */
1014         if (!init) {
1015                 vc->vc_cols = new_cols;
1016                 vc->vc_rows = new_rows;
1017         }
1018
1019         if (logo)
1020                 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1021
1022         if (vc == svc && softback_buf)
1023                 fbcon_update_softback(vc);
1024 }
1025
1026 static void fbcon_deinit(struct vc_data *vc)
1027 {
1028         struct display *p = &fb_display[vc->vc_num];
1029
1030         if (info_idx != -1)
1031             return;
1032         fbcon_free_font(p);
1033 }
1034
1035 /* ====================================================================== */
1036
1037 /*  fbcon_XXX routines - interface used by the world
1038  *
1039  *  This system is now divided into two levels because of complications
1040  *  caused by hardware scrolling. Top level functions:
1041  *
1042  *      fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1043  *
1044  *  handles y values in range [0, scr_height-1] that correspond to real
1045  *  screen positions. y_wrap shift means that first line of bitmap may be
1046  *  anywhere on this display. These functions convert lineoffsets to
1047  *  bitmap offsets and deal with the wrap-around case by splitting blits.
1048  *
1049  *      fbcon_bmove_physical_8()    -- These functions fast implementations
1050  *      fbcon_clear_physical_8()    -- of original fbcon_XXX fns.
1051  *      fbcon_putc_physical_8()     -- (font width != 8) may be added later
1052  *
1053  *  WARNING:
1054  *
1055  *  At the moment fbcon_putc() cannot blit across vertical wrap boundary
1056  *  Implies should only really hardware scroll in rows. Only reason for
1057  *  restriction is simplicity & efficiency at the moment.
1058  */
1059
1060 static __inline__ int real_y(struct display *p, int ypos)
1061 {
1062         int rows = p->vrows;
1063
1064         ypos += p->yscroll;
1065         return ypos < rows ? ypos : ypos - rows;
1066 }
1067
1068
1069 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1070                         int width)
1071 {
1072         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1073         struct fbcon_ops *ops = info->fbcon_par;
1074
1075         struct display *p = &fb_display[vc->vc_num];
1076         u_int y_break;
1077
1078         if (fbcon_is_inactive(vc, info))
1079                 return;
1080
1081         if (!height || !width)
1082                 return;
1083
1084         /* Split blits that cross physical y_wrap boundary */
1085
1086         y_break = p->vrows - p->yscroll;
1087         if (sy < y_break && sy + height - 1 >= y_break) {
1088                 u_int b = y_break - sy;
1089                 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1090                 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1091                                  width);
1092         } else
1093                 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1094 }
1095
1096 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1097                         int count, int ypos, int xpos)
1098 {
1099         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1100         struct display *p = &fb_display[vc->vc_num];
1101         struct fbcon_ops *ops = info->fbcon_par;
1102
1103         if (!fbcon_is_inactive(vc, info))
1104                 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1105                            get_color(vc, info, scr_readw(s), 1),
1106                            get_color(vc, info, scr_readw(s), 0));
1107 }
1108
1109 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1110 {
1111         unsigned short chr;
1112
1113         scr_writew(c, &chr);
1114         fbcon_putcs(vc, &chr, 1, ypos, xpos);
1115 }
1116
1117 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1118 {
1119         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1120         struct fbcon_ops *ops = info->fbcon_par;
1121
1122         if (!fbcon_is_inactive(vc, info))
1123                 ops->clear_margins(vc, info, bottom_only);
1124 }
1125
1126 static void fbcon_cursor(struct vc_data *vc, int mode)
1127 {
1128         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1129         struct fbcon_ops *ops = info->fbcon_par;
1130         struct display *p = &fb_display[vc->vc_num];
1131         int y;
1132         int c = scr_readw((u16 *) vc->vc_pos);
1133
1134         if (fbcon_is_inactive(vc, info))
1135                 return;
1136
1137         ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1138         if (mode & CM_SOFTBACK) {
1139                 mode &= ~CM_SOFTBACK;
1140                 y = softback_lines;
1141         } else {
1142                 if (softback_lines)
1143                         fbcon_set_origin(vc);
1144                 y = 0;
1145         }
1146
1147         ops->cursor(vc, info, p, mode, y, get_color(vc, info, c, 1),
1148                     get_color(vc, info, c, 0));
1149         vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1150 }
1151
1152 static int scrollback_phys_max = 0;
1153 static int scrollback_max = 0;
1154 static int scrollback_current = 0;
1155
1156 static int update_var(int con, struct fb_info *info)
1157 {
1158         if (con == ((struct fbcon_ops *)info->fbcon_par)->currcon)
1159                 return fb_pan_display(info, &info->var);
1160         return 0;
1161 }
1162
1163 /*
1164  * If no vc is existent yet, just set struct display
1165  */
1166 static void fbcon_preset_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1167                               int unit)
1168 {
1169         struct display *p = &fb_display[unit];
1170         struct display *t = &fb_display[fg_console];
1171
1172         var->xoffset = var->yoffset = p->yscroll = 0;
1173         if (var_to_display(p, var, info))
1174                 return;
1175
1176         p->fontdata = t->fontdata;
1177         p->userfont = t->userfont;
1178         if (p->userfont)
1179                 REFCOUNT(p->fontdata)++;
1180 }
1181
1182 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1183                            struct vc_data *vc)
1184 {
1185         struct display *p = &fb_display[vc->vc_num], *t;
1186         struct vc_data **default_mode = vc->vc_display_fg;
1187         struct vc_data *svc = *default_mode;
1188         int rows, cols, charcnt = 256;
1189
1190         var->xoffset = var->yoffset = p->yscroll = 0;
1191         if (var_to_display(p, var, info))
1192                 return;
1193         t = &fb_display[svc->vc_num];
1194         if (!vc->vc_font.data) {
1195                 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1196                 vc->vc_font.width = (*default_mode)->vc_font.width;
1197                 vc->vc_font.height = (*default_mode)->vc_font.height;
1198                 p->userfont = t->userfont;
1199                 if (p->userfont)
1200                         REFCOUNT(p->fontdata)++;
1201         }
1202         if (p->userfont)
1203                 charcnt = FNTCHARCNT(p->fontdata);
1204
1205         var->activate = FB_ACTIVATE_NOW;
1206         info->var.activate = var->activate;
1207         info->var.yoffset = info->var.xoffset = 0;
1208         fb_set_var(info, var);
1209
1210         vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1211         vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1212         if (charcnt == 256) {
1213                 vc->vc_hi_font_mask = 0;
1214         } else {
1215                 vc->vc_hi_font_mask = 0x100;
1216                 if (vc->vc_can_do_color)
1217                         vc->vc_complement_mask <<= 1;
1218         }
1219
1220         if (!*svc->vc_uni_pagedir_loc)
1221                 con_set_default_unimap(svc);
1222         if (!*vc->vc_uni_pagedir_loc)
1223                 con_copy_unimap(vc, svc);
1224
1225         cols = var->xres / vc->vc_font.width;
1226         rows = var->yres / vc->vc_font.height;
1227         vc_resize(vc, cols, rows);
1228         if (CON_IS_VISIBLE(vc)) {
1229                 update_screen(vc);
1230                 if (softback_buf)
1231                         fbcon_update_softback(vc);
1232         }
1233 }
1234
1235 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1236 {
1237         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1238         struct display *p = &fb_display[vc->vc_num];
1239         
1240         p->yscroll += count;
1241         if (p->yscroll >= p->vrows)     /* Deal with wrap */
1242                 p->yscroll -= p->vrows;
1243         info->var.xoffset = 0;
1244         info->var.yoffset = p->yscroll * vc->vc_font.height;
1245         info->var.vmode |= FB_VMODE_YWRAP;
1246         update_var(vc->vc_num, info);
1247         scrollback_max += count;
1248         if (scrollback_max > scrollback_phys_max)
1249                 scrollback_max = scrollback_phys_max;
1250         scrollback_current = 0;
1251 }
1252
1253 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1254 {
1255         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1256         struct display *p = &fb_display[vc->vc_num];
1257         
1258         p->yscroll -= count;
1259         if (p->yscroll < 0)     /* Deal with wrap */
1260                 p->yscroll += p->vrows;
1261         info->var.xoffset = 0;
1262         info->var.yoffset = p->yscroll * vc->vc_font.height;
1263         info->var.vmode |= FB_VMODE_YWRAP;
1264         update_var(vc->vc_num, info);
1265         scrollback_max -= count;
1266         if (scrollback_max < 0)
1267                 scrollback_max = 0;
1268         scrollback_current = 0;
1269 }
1270
1271 static __inline__ void ypan_up(struct vc_data *vc, int count)
1272 {
1273         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1274         struct display *p = &fb_display[vc->vc_num];
1275         struct fbcon_ops *ops = info->fbcon_par;
1276
1277         p->yscroll += count;
1278         if (p->yscroll > p->vrows - vc->vc_rows) {
1279                 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1280                             0, 0, 0, vc->vc_rows, vc->vc_cols);
1281                 p->yscroll -= p->vrows - vc->vc_rows;
1282         }
1283         info->var.xoffset = 0;
1284         info->var.yoffset = p->yscroll * vc->vc_font.height;
1285         info->var.vmode &= ~FB_VMODE_YWRAP;
1286         update_var(vc->vc_num, info);
1287         fbcon_clear_margins(vc, 1);
1288         scrollback_max += count;
1289         if (scrollback_max > scrollback_phys_max)
1290                 scrollback_max = scrollback_phys_max;
1291         scrollback_current = 0;
1292 }
1293
1294 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1295 {
1296         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1297         struct display *p = &fb_display[vc->vc_num];
1298         int redraw = 0;
1299
1300         p->yscroll += count;
1301         if (p->yscroll > p->vrows - vc->vc_rows) {
1302                 p->yscroll -= p->vrows - vc->vc_rows;
1303                 redraw = 1;
1304         }
1305
1306         info->var.xoffset = 0;
1307         info->var.yoffset = p->yscroll * vc->vc_font.height;
1308         info->var.vmode &= ~FB_VMODE_YWRAP;
1309         if (redraw)
1310                 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1311         update_var(vc->vc_num, info);
1312         fbcon_clear_margins(vc, 1);
1313         scrollback_max += count;
1314         if (scrollback_max > scrollback_phys_max)
1315                 scrollback_max = scrollback_phys_max;
1316         scrollback_current = 0;
1317 }
1318
1319 static __inline__ void ypan_down(struct vc_data *vc, int count)
1320 {
1321         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1322         struct display *p = &fb_display[vc->vc_num];
1323         struct fbcon_ops *ops = info->fbcon_par;
1324         
1325         p->yscroll -= count;
1326         if (p->yscroll < 0) {
1327                 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1328                             0, vc->vc_rows, vc->vc_cols);
1329                 p->yscroll += p->vrows - vc->vc_rows;
1330         }
1331         info->var.xoffset = 0;
1332         info->var.yoffset = p->yscroll * vc->vc_font.height;
1333         info->var.vmode &= ~FB_VMODE_YWRAP;
1334         update_var(vc->vc_num, info);
1335         fbcon_clear_margins(vc, 1);
1336         scrollback_max -= count;
1337         if (scrollback_max < 0)
1338                 scrollback_max = 0;
1339         scrollback_current = 0;
1340 }
1341
1342 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1343 {
1344         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1345         struct display *p = &fb_display[vc->vc_num];
1346         int redraw = 0;
1347
1348         p->yscroll -= count;
1349         if (p->yscroll < 0) {
1350                 p->yscroll += p->vrows - vc->vc_rows;
1351                 redraw = 1;
1352         }
1353         info->var.xoffset = 0;
1354         info->var.yoffset = p->yscroll * vc->vc_font.height;
1355         info->var.vmode &= ~FB_VMODE_YWRAP;
1356         if (redraw)
1357                 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1358         update_var(vc->vc_num, info);
1359         fbcon_clear_margins(vc, 1);
1360         scrollback_max -= count;
1361         if (scrollback_max < 0)
1362                 scrollback_max = 0;
1363         scrollback_current = 0;
1364 }
1365
1366 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1367                                   long delta)
1368 {
1369         int count = vc->vc_rows;
1370         unsigned short *d, *s;
1371         unsigned long n;
1372         int line = 0;
1373
1374         d = (u16 *) softback_curr;
1375         if (d == (u16 *) softback_in)
1376                 d = (u16 *) vc->vc_origin;
1377         n = softback_curr + delta * vc->vc_size_row;
1378         softback_lines -= delta;
1379         if (delta < 0) {
1380                 if (softback_curr < softback_top && n < softback_buf) {
1381                         n += softback_end - softback_buf;
1382                         if (n < softback_top) {
1383                                 softback_lines -=
1384                                     (softback_top - n) / vc->vc_size_row;
1385                                 n = softback_top;
1386                         }
1387                 } else if (softback_curr >= softback_top
1388                            && n < softback_top) {
1389                         softback_lines -=
1390                             (softback_top - n) / vc->vc_size_row;
1391                         n = softback_top;
1392                 }
1393         } else {
1394                 if (softback_curr > softback_in && n >= softback_end) {
1395                         n += softback_buf - softback_end;
1396                         if (n > softback_in) {
1397                                 n = softback_in;
1398                                 softback_lines = 0;
1399                         }
1400                 } else if (softback_curr <= softback_in && n > softback_in) {
1401                         n = softback_in;
1402                         softback_lines = 0;
1403                 }
1404         }
1405         if (n == softback_curr)
1406                 return;
1407         softback_curr = n;
1408         s = (u16 *) softback_curr;
1409         if (s == (u16 *) softback_in)
1410                 s = (u16 *) vc->vc_origin;
1411         while (count--) {
1412                 unsigned short *start;
1413                 unsigned short *le;
1414                 unsigned short c;
1415                 int x = 0;
1416                 unsigned short attr = 1;
1417
1418                 start = s;
1419                 le = advance_row(s, 1);
1420                 do {
1421                         c = scr_readw(s);
1422                         if (attr != (c & 0xff00)) {
1423                                 attr = c & 0xff00;
1424                                 if (s > start) {
1425                                         fbcon_putcs(vc, start, s - start,
1426                                                     line, x);
1427                                         x += s - start;
1428                                         start = s;
1429                                 }
1430                         }
1431                         if (c == scr_readw(d)) {
1432                                 if (s > start) {
1433                                         fbcon_putcs(vc, start, s - start,
1434                                                     line, x);
1435                                         x += s - start + 1;
1436                                         start = s + 1;
1437                                 } else {
1438                                         x++;
1439                                         start++;
1440                                 }
1441                         }
1442                         s++;
1443                         d++;
1444                 } while (s < le);
1445                 if (s > start)
1446                         fbcon_putcs(vc, start, s - start, line, x);
1447                 line++;
1448                 if (d == (u16 *) softback_end)
1449                         d = (u16 *) softback_buf;
1450                 if (d == (u16 *) softback_in)
1451                         d = (u16 *) vc->vc_origin;
1452                 if (s == (u16 *) softback_end)
1453                         s = (u16 *) softback_buf;
1454                 if (s == (u16 *) softback_in)
1455                         s = (u16 *) vc->vc_origin;
1456         }
1457 }
1458
1459 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1460                               int line, int count, int dy)
1461 {
1462         unsigned short *s = (unsigned short *)
1463                 (vc->vc_origin + vc->vc_size_row * line);
1464
1465         while (count--) {
1466                 unsigned short *start = s;
1467                 unsigned short *le = advance_row(s, 1);
1468                 unsigned short c;
1469                 int x = 0;
1470                 unsigned short attr = 1;
1471
1472                 do {
1473                         c = scr_readw(s);
1474                         if (attr != (c & 0xff00)) {
1475                                 attr = c & 0xff00;
1476                                 if (s > start) {
1477                                         fbcon_putcs(vc, start, s - start,
1478                                                     dy, x);
1479                                         x += s - start;
1480                                         start = s;
1481                                 }
1482                         }
1483                         console_conditional_schedule();
1484                         s++;
1485                 } while (s < le);
1486                 if (s > start)
1487                         fbcon_putcs(vc, start, s - start, dy, x);
1488                 console_conditional_schedule();
1489                 dy++;
1490         }
1491 }
1492
1493 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1494                          int line, int count, int offset)
1495 {
1496         unsigned short *d = (unsigned short *)
1497             (vc->vc_origin + vc->vc_size_row * line);
1498         unsigned short *s = d + offset;
1499
1500         while (count--) {
1501                 unsigned short *start = s;
1502                 unsigned short *le = advance_row(s, 1);
1503                 unsigned short c;
1504                 int x = 0;
1505                 unsigned short attr = 1;
1506
1507                 do {
1508                         c = scr_readw(s);
1509                         if (attr != (c & 0xff00)) {
1510                                 attr = c & 0xff00;
1511                                 if (s > start) {
1512                                         fbcon_putcs(vc, start, s - start,
1513                                                     line, x);
1514                                         x += s - start;
1515                                         start = s;
1516                                 }
1517                         }
1518                         if (c == scr_readw(d)) {
1519                                 if (s > start) {
1520                                         fbcon_putcs(vc, start, s - start,
1521                                                      line, x);
1522                                         x += s - start + 1;
1523                                         start = s + 1;
1524                                 } else {
1525                                         x++;
1526                                         start++;
1527                                 }
1528                         }
1529                         scr_writew(c, d);
1530                         console_conditional_schedule();
1531                         s++;
1532                         d++;
1533                 } while (s < le);
1534                 if (s > start)
1535                         fbcon_putcs(vc, start, s - start, line, x);
1536                 console_conditional_schedule();
1537                 if (offset > 0)
1538                         line++;
1539                 else {
1540                         line--;
1541                         /* NOTE: We subtract two lines from these pointers */
1542                         s -= vc->vc_size_row;
1543                         d -= vc->vc_size_row;
1544                 }
1545         }
1546 }
1547
1548 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1549                                        int count)
1550 {
1551         unsigned short *p;
1552
1553         if (vc->vc_num != fg_console)
1554                 return;
1555         p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1556
1557         while (count) {
1558                 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1559                 count--;
1560                 p = advance_row(p, 1);
1561                 softback_in += vc->vc_size_row;
1562                 if (softback_in == softback_end)
1563                         softback_in = softback_buf;
1564                 if (softback_in == softback_top) {
1565                         softback_top += vc->vc_size_row;
1566                         if (softback_top == softback_end)
1567                                 softback_top = softback_buf;
1568                 }
1569         }
1570         softback_curr = softback_in;
1571 }
1572
1573 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1574                         int count)
1575 {
1576         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1577         struct display *p = &fb_display[vc->vc_num];
1578         struct fbcon_ops *ops = info->fbcon_par;
1579         int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1580
1581         if (fbcon_is_inactive(vc, info))
1582                 return -EINVAL;
1583
1584         fbcon_cursor(vc, CM_ERASE);
1585
1586         /*
1587          * ++Geert: Only use ywrap/ypan if the console is in text mode
1588          * ++Andrew: Only use ypan on hardware text mode when scrolling the
1589          *           whole screen (prevents flicker).
1590          */
1591
1592         switch (dir) {
1593         case SM_UP:
1594                 if (count > vc->vc_rows)        /* Maximum realistic size */
1595                         count = vc->vc_rows;
1596                 if (softback_top)
1597                         fbcon_softback_note(vc, t, count);
1598                 if (logo_shown >= 0)
1599                         goto redraw_up;
1600                 switch (p->scrollmode) {
1601                 case SCROLL_MOVE:
1602                         ops->bmove(vc, info, t + count, 0, t, 0,
1603                                     b - t - count, vc->vc_cols);
1604                         ops->clear(vc, info, b - count, 0, count,
1605                                   vc->vc_cols);
1606                         break;
1607
1608                 case SCROLL_WRAP_MOVE:
1609                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1610                                 if (t > 0)
1611                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1612                                                     vc->vc_cols);
1613                                 ywrap_up(vc, count);
1614                                 if (vc->vc_rows - b > 0)
1615                                         fbcon_bmove(vc, b - count, 0, b, 0,
1616                                                     vc->vc_rows - b,
1617                                                     vc->vc_cols);
1618                         } else if (info->flags & FBINFO_READS_FAST)
1619                                 fbcon_bmove(vc, t + count, 0, t, 0,
1620                                             b - t - count, vc->vc_cols);
1621                         else
1622                                 goto redraw_up;
1623                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1624                         break;
1625
1626                 case SCROLL_PAN_REDRAW:
1627                         if ((p->yscroll + count <=
1628                              2 * (p->vrows - vc->vc_rows))
1629                             && ((!scroll_partial && (b - t == vc->vc_rows))
1630                                 || (scroll_partial
1631                                     && (b - t - count >
1632                                         3 * vc->vc_rows >> 2)))) {
1633                                 if (t > 0)
1634                                         fbcon_redraw_move(vc, p, 0, t, count);
1635                                 ypan_up_redraw(vc, t, count);
1636                                 if (vc->vc_rows - b > 0)
1637                                         fbcon_redraw_move(vc, p, b - count,
1638                                                           vc->vc_rows - b, b);
1639                         } else
1640                                 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1641                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1642                         break;
1643
1644                 case SCROLL_PAN_MOVE:
1645                         if ((p->yscroll + count <=
1646                              2 * (p->vrows - vc->vc_rows))
1647                             && ((!scroll_partial && (b - t == vc->vc_rows))
1648                                 || (scroll_partial
1649                                     && (b - t - count >
1650                                         3 * vc->vc_rows >> 2)))) {
1651                                 if (t > 0)
1652                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1653                                                     vc->vc_cols);
1654                                 ypan_up(vc, count);
1655                                 if (vc->vc_rows - b > 0)
1656                                         fbcon_bmove(vc, b - count, 0, b, 0,
1657                                                     vc->vc_rows - b,
1658                                                     vc->vc_cols);
1659                         } else if (info->flags & FBINFO_READS_FAST)
1660                                 fbcon_bmove(vc, t + count, 0, t, 0,
1661                                             b - t - count, vc->vc_cols);
1662                         else
1663                                 goto redraw_up;
1664                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1665                         break;
1666
1667                 case SCROLL_REDRAW:
1668                       redraw_up:
1669                         fbcon_redraw(vc, p, t, b - t - count,
1670                                      count * vc->vc_cols);
1671                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1672                         scr_memsetw((unsigned short *) (vc->vc_origin +
1673                                                         vc->vc_size_row *
1674                                                         (b - count)),
1675                                     vc->vc_video_erase_char,
1676                                     vc->vc_size_row * count);
1677                         return 1;
1678                 }
1679                 break;
1680
1681         case SM_DOWN:
1682                 if (count > vc->vc_rows)        /* Maximum realistic size */
1683                         count = vc->vc_rows;
1684                 if (logo_shown >= 0)
1685                         goto redraw_down;
1686                 switch (p->scrollmode) {
1687                 case SCROLL_MOVE:
1688                         ops->bmove(vc, info, t, 0, t + count, 0,
1689                                     b - t - count, vc->vc_cols);
1690                         ops->clear(vc, info, t, 0, count, vc->vc_cols);
1691                         break;
1692
1693                 case SCROLL_WRAP_MOVE:
1694                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1695                                 if (vc->vc_rows - b > 0)
1696                                         fbcon_bmove(vc, b, 0, b - count, 0,
1697                                                     vc->vc_rows - b,
1698                                                     vc->vc_cols);
1699                                 ywrap_down(vc, count);
1700                                 if (t > 0)
1701                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1702                                                     vc->vc_cols);
1703                         } else if (info->flags & FBINFO_READS_FAST)
1704                                 fbcon_bmove(vc, t, 0, t + count, 0,
1705                                             b - t - count, vc->vc_cols);
1706                         else
1707                                 goto redraw_down;
1708                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1709                         break;
1710
1711                 case SCROLL_PAN_MOVE:
1712                         if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1713                             && ((!scroll_partial && (b - t == vc->vc_rows))
1714                                 || (scroll_partial
1715                                     && (b - t - count >
1716                                         3 * vc->vc_rows >> 2)))) {
1717                                 if (vc->vc_rows - b > 0)
1718                                         fbcon_bmove(vc, b, 0, b - count, 0,
1719                                                     vc->vc_rows - b,
1720                                                     vc->vc_cols);
1721                                 ypan_down(vc, count);
1722                                 if (t > 0)
1723                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1724                                                     vc->vc_cols);
1725                         } else if (info->flags & FBINFO_READS_FAST)
1726                                 fbcon_bmove(vc, t, 0, t + count, 0,
1727                                             b - t - count, vc->vc_cols);
1728                         else
1729                                 goto redraw_down;
1730                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1731                         break;
1732
1733                 case SCROLL_PAN_REDRAW:
1734                         if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1735                             && ((!scroll_partial && (b - t == vc->vc_rows))
1736                                 || (scroll_partial
1737                                     && (b - t - count >
1738                                         3 * vc->vc_rows >> 2)))) {
1739                                 if (vc->vc_rows - b > 0)
1740                                         fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
1741                                                           b - count);
1742                                 ypan_down_redraw(vc, t, count);
1743                                 if (t > 0)
1744                                         fbcon_redraw_move(vc, p, count, t, 0);
1745                         } else
1746                                 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
1747                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1748                         break;
1749
1750                 case SCROLL_REDRAW:
1751                       redraw_down:
1752                         fbcon_redraw(vc, p, b - 1, b - t - count,
1753                                      -count * vc->vc_cols);
1754                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1755                         scr_memsetw((unsigned short *) (vc->vc_origin +
1756                                                         vc->vc_size_row *
1757                                                         t),
1758                                     vc->vc_video_erase_char,
1759                                     vc->vc_size_row * count);
1760                         return 1;
1761                 }
1762         }
1763         return 0;
1764 }
1765
1766
1767 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
1768                         int height, int width)
1769 {
1770         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1771         struct display *p = &fb_display[vc->vc_num];
1772         
1773         if (fbcon_is_inactive(vc, info))
1774                 return;
1775
1776         if (!width || !height)
1777                 return;
1778
1779         /*  Split blits that cross physical y_wrap case.
1780          *  Pathological case involves 4 blits, better to use recursive
1781          *  code rather than unrolled case
1782          *
1783          *  Recursive invocations don't need to erase the cursor over and
1784          *  over again, so we use fbcon_bmove_rec()
1785          */
1786         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
1787                         p->vrows - p->yscroll);
1788 }
1789
1790 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx, 
1791                             int dy, int dx, int height, int width, u_int y_break)
1792 {
1793         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1794         struct fbcon_ops *ops = info->fbcon_par;
1795         u_int b;
1796
1797         if (sy < y_break && sy + height > y_break) {
1798                 b = y_break - sy;
1799                 if (dy < sy) {  /* Avoid trashing self */
1800                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1801                                         y_break);
1802                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1803                                         height - b, width, y_break);
1804                 } else {
1805                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1806                                         height - b, width, y_break);
1807                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1808                                         y_break);
1809                 }
1810                 return;
1811         }
1812
1813         if (dy < y_break && dy + height > y_break) {
1814                 b = y_break - dy;
1815                 if (dy < sy) {  /* Avoid trashing self */
1816                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1817                                         y_break);
1818                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1819                                         height - b, width, y_break);
1820                 } else {
1821                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1822                                         height - b, width, y_break);
1823                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1824                                         y_break);
1825                 }
1826                 return;
1827         }
1828         ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
1829                    height, width);
1830 }
1831
1832 static __inline__ void updatescrollmode(struct display *p, struct fb_info *info,
1833                                         struct vc_data *vc)
1834 {
1835         int fh = vc->vc_font.height;
1836         int cap = info->flags;
1837         int good_pan = (cap & FBINFO_HWACCEL_YPAN)
1838                  && divides(info->fix.ypanstep, vc->vc_font.height)
1839                  && info->var.yres_virtual > info->var.yres;
1840         int good_wrap = (cap & FBINFO_HWACCEL_YWRAP)
1841                  && divides(info->fix.ywrapstep, vc->vc_font.height)
1842                  && divides(vc->vc_font.height, info->var.yres_virtual);
1843         int reading_fast = cap & FBINFO_READS_FAST;
1844         int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) && !(cap & FBINFO_HWACCEL_DISABLED);
1845         int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) && !(cap & FBINFO_HWACCEL_DISABLED);
1846
1847         p->vrows = info->var.yres_virtual/fh;
1848         if (info->var.yres > (fh * (vc->vc_rows + 1)))
1849                 p->vrows -= (info->var.yres - (fh * vc->vc_rows)) / fh;
1850         if ((info->var.yres % fh) && (info->var.yres_virtual % fh <
1851                                       info->var.yres % fh))
1852                 p->vrows--;
1853
1854         if (good_wrap || good_pan) {
1855                 if (reading_fast || fast_copyarea)
1856                         p->scrollmode = good_wrap ? SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
1857                 else
1858                         p->scrollmode = good_wrap ? SCROLL_REDRAW :
1859                                 SCROLL_PAN_REDRAW;
1860         } else {
1861                 if (reading_fast || (fast_copyarea && !fast_imageblit))
1862                         p->scrollmode = SCROLL_MOVE;
1863                 else
1864                         p->scrollmode = SCROLL_REDRAW;
1865         }
1866 }
1867
1868 static int fbcon_resize(struct vc_data *vc, unsigned int width, 
1869                         unsigned int height)
1870 {
1871         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1872         struct display *p = &fb_display[vc->vc_num];
1873         struct fb_var_screeninfo var = info->var;
1874         int x_diff, y_diff;
1875         int fw = vc->vc_font.width;
1876         int fh = vc->vc_font.height;
1877
1878         var.xres = width * fw;
1879         var.yres = height * fh;
1880         x_diff = info->var.xres - var.xres;
1881         y_diff = info->var.yres - var.yres;
1882         if (x_diff < 0 || x_diff > fw || (y_diff < 0 || y_diff > fh)) {
1883                 struct fb_videomode *mode;
1884
1885                 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
1886                 mode = fb_find_best_mode(&var, &info->modelist);
1887                 if (mode == NULL)
1888                         return -EINVAL;
1889                 fb_videomode_to_var(&var, mode);
1890                 if (width > var.xres/fw || height > var.yres/fh)
1891                         return -EINVAL;
1892                 /*
1893                  * The following can probably have any value... Do we need to
1894                  * set all of them?
1895                  */
1896                 var.bits_per_pixel = p->bits_per_pixel;
1897                 var.xres_virtual = p->xres_virtual;
1898                 var.yres_virtual = p->yres_virtual;
1899                 var.accel_flags = p->accel_flags;
1900                 var.width = p->width;
1901                 var.height = p->height;
1902                 var.red = p->red;
1903                 var.green = p->green;
1904                 var.blue = p->blue;
1905                 var.transp = p->transp;
1906                 var.nonstd = p->nonstd;
1907
1908                 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
1909                 if (CON_IS_VISIBLE(vc)) {
1910                         var.activate = FB_ACTIVATE_NOW |
1911                                 FB_ACTIVATE_FORCE;
1912                         fb_set_var(info, &var);
1913                 }
1914                 var_to_display(p, &info->var, info);
1915         }
1916         updatescrollmode(p, info, vc);
1917         return 0;
1918 }
1919
1920 static int fbcon_switch(struct vc_data *vc)
1921 {
1922         struct fb_info *info, *old_info = NULL;
1923         struct display *p = &fb_display[vc->vc_num];
1924         struct fb_var_screeninfo var;
1925         int i, prev_console;
1926
1927         info = registered_fb[con2fb_map[vc->vc_num]];
1928
1929         if (softback_top) {
1930                 if (softback_lines)
1931                         fbcon_set_origin(vc);
1932                 softback_top = softback_curr = softback_in = softback_buf;
1933                 softback_lines = 0;
1934                 fbcon_update_softback(vc);
1935         }
1936
1937         if (logo_shown >= 0) {
1938                 struct vc_data *conp2 = vc_cons[logo_shown].d;
1939
1940                 if (conp2->vc_top == logo_lines
1941                     && conp2->vc_bottom == conp2->vc_rows)
1942                         conp2->vc_top = 0;
1943                 logo_shown = FBCON_LOGO_CANSHOW;
1944         }
1945
1946         prev_console = ((struct fbcon_ops *)info->fbcon_par)->currcon;
1947         if (prev_console != -1)
1948                 old_info = registered_fb[con2fb_map[prev_console]];
1949         /*
1950          * FIXME: If we have multiple fbdev's loaded, we need to
1951          * update all info->currcon.  Perhaps, we can place this
1952          * in a centralized structure, but this might break some
1953          * drivers.
1954          *
1955          * info->currcon = vc->vc_num;
1956          */
1957         for (i = 0; i < FB_MAX; i++) {
1958                 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
1959                         struct fbcon_ops *ops = registered_fb[i]->fbcon_par;
1960
1961                         ops->currcon = vc->vc_num;
1962                 }
1963         }
1964         memset(&var, 0, sizeof(struct fb_var_screeninfo));
1965         display_to_var(&var, p);
1966         var.activate = FB_ACTIVATE_NOW;
1967
1968         /*
1969          * make sure we don't unnecessarily trip the memcmp()
1970          * in fb_set_var()
1971          */
1972         info->var.activate = var.activate;
1973         info->var.yoffset = info->var.xoffset = p->yscroll = 0;
1974         fb_set_var(info, &var);
1975
1976         if (old_info != NULL && old_info != info) {
1977                 if (info->fbops->fb_set_par)
1978                         info->fbops->fb_set_par(info);
1979                 fbcon_del_cursor_timer(old_info);
1980                 fbcon_add_cursor_timer(info);
1981         }
1982
1983         set_blitting_type(vc, info, p);
1984         ((struct fbcon_ops *)info->fbcon_par)->cursor_reset = 1;
1985
1986         vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1987         vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1988         updatescrollmode(p, info, vc);
1989
1990         switch (p->scrollmode) {
1991         case SCROLL_WRAP_MOVE:
1992                 scrollback_phys_max = p->vrows - vc->vc_rows;
1993                 break;
1994         case SCROLL_PAN_MOVE:
1995         case SCROLL_PAN_REDRAW:
1996                 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
1997                 if (scrollback_phys_max < 0)
1998                         scrollback_phys_max = 0;
1999                 break;
2000         default:
2001                 scrollback_phys_max = 0;
2002                 break;
2003         }
2004         scrollback_max = 0;
2005         scrollback_current = 0;
2006
2007         update_var(vc->vc_num, info);
2008         fbcon_set_palette(vc, color_table);     
2009         fbcon_clear_margins(vc, 0);
2010
2011         if (logo_shown == FBCON_LOGO_DRAW) {
2012
2013                 logo_shown = fg_console;
2014                 /* This is protected above by initmem_freed */
2015                 fb_show_logo(info);
2016                 update_region(vc,
2017                               vc->vc_origin + vc->vc_size_row * vc->vc_top,
2018                               vc->vc_size_row * (vc->vc_bottom -
2019                                                  vc->vc_top) / 2);
2020                 return 0;
2021         }
2022         return 1;
2023 }
2024
2025 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2026                                 int blank)
2027 {
2028         if (blank) {
2029                 unsigned short charmask = vc->vc_hi_font_mask ?
2030                         0x1ff : 0xff;
2031                 unsigned short oldc;
2032
2033                 oldc = vc->vc_video_erase_char;
2034                 vc->vc_video_erase_char &= charmask;
2035                 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2036                 vc->vc_video_erase_char = oldc;
2037         }
2038 }
2039
2040 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2041 {
2042         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2043         struct fbcon_ops *ops = info->fbcon_par;
2044
2045         if (mode_switch) {
2046                 struct fb_var_screeninfo var = info->var;
2047
2048                 ops->graphics = 1;
2049
2050                 if (!blank) {
2051                         var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2052                         fb_set_var(info, &var);
2053                         ops->graphics = 0;
2054                 }
2055         }
2056
2057         if (!fbcon_is_inactive(vc, info)) {
2058                 if (ops->blank_state != blank) {
2059                         ops->blank_state = blank;
2060                         fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2061                         ops->cursor_flash = (!blank);
2062
2063                         if (fb_blank(info, blank))
2064                                 fbcon_generic_blank(vc, info, blank);
2065                 }
2066
2067                 if (!blank)
2068                         update_screen(vc);
2069         }
2070
2071         if (!blank)
2072                 fbcon_add_cursor_timer(info);
2073         else
2074                 fbcon_del_cursor_timer(info);
2075
2076         return 0;
2077 }
2078
2079 static void fbcon_free_font(struct display *p)
2080 {
2081         if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
2082                 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
2083         p->fontdata = NULL;
2084         p->userfont = 0;
2085 }
2086
2087 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2088 {
2089         u8 *fontdata = vc->vc_font.data;
2090         u8 *data = font->data;
2091         int i, j;
2092
2093         font->width = vc->vc_font.width;
2094         font->height = vc->vc_font.height;
2095         font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2096         if (!font->data)
2097                 return 0;
2098
2099         if (font->width <= 8) {
2100                 j = vc->vc_font.height;
2101                 for (i = 0; i < font->charcount; i++) {
2102                         memcpy(data, fontdata, j);
2103                         memset(data + j, 0, 32 - j);
2104                         data += 32;
2105                         fontdata += j;
2106                 }
2107         } else if (font->width <= 16) {
2108                 j = vc->vc_font.height * 2;
2109                 for (i = 0; i < font->charcount; i++) {
2110                         memcpy(data, fontdata, j);
2111                         memset(data + j, 0, 64 - j);
2112                         data += 64;
2113                         fontdata += j;
2114                 }
2115         } else if (font->width <= 24) {
2116                 for (i = 0; i < font->charcount; i++) {
2117                         for (j = 0; j < vc->vc_font.height; j++) {
2118                                 *data++ = fontdata[0];
2119                                 *data++ = fontdata[1];
2120                                 *data++ = fontdata[2];
2121                                 fontdata += sizeof(u32);
2122                         }
2123                         memset(data, 0, 3 * (32 - j));
2124                         data += 3 * (32 - j);
2125                 }
2126         } else {
2127                 j = vc->vc_font.height * 4;
2128                 for (i = 0; i < font->charcount; i++) {
2129                         memcpy(data, fontdata, j);
2130                         memset(data + j, 0, 128 - j);
2131                         data += 128;
2132                         fontdata += j;
2133                 }
2134         }
2135         return 0;
2136 }
2137
2138 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2139                              const u8 * data, int userfont)
2140 {
2141         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2142         struct display *p = &fb_display[vc->vc_num];
2143         int resize;
2144         int cnt;
2145         char *old_data = NULL;
2146
2147         if (CON_IS_VISIBLE(vc) && softback_lines)
2148                 fbcon_set_origin(vc);
2149
2150         resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2151         if (p->userfont)
2152                 old_data = vc->vc_font.data;
2153         if (userfont)
2154                 cnt = FNTCHARCNT(data);
2155         else
2156                 cnt = 256;
2157         vc->vc_font.data = (void *)(p->fontdata = data);
2158         if ((p->userfont = userfont))
2159                 REFCOUNT(data)++;
2160         vc->vc_font.width = w;
2161         vc->vc_font.height = h;
2162         if (vc->vc_hi_font_mask && cnt == 256) {
2163                 vc->vc_hi_font_mask = 0;
2164                 if (vc->vc_can_do_color) {
2165                         vc->vc_complement_mask >>= 1;
2166                         vc->vc_s_complement_mask >>= 1;
2167                 }
2168                         
2169                 /* ++Edmund: reorder the attribute bits */
2170                 if (vc->vc_can_do_color) {
2171                         unsigned short *cp =
2172                             (unsigned short *) vc->vc_origin;
2173                         int count = vc->vc_screenbuf_size / 2;
2174                         unsigned short c;
2175                         for (; count > 0; count--, cp++) {
2176                                 c = scr_readw(cp);
2177                                 scr_writew(((c & 0xfe00) >> 1) |
2178                                            (c & 0xff), cp);
2179                         }
2180                         c = vc->vc_video_erase_char;
2181                         vc->vc_video_erase_char =
2182                             ((c & 0xfe00) >> 1) | (c & 0xff);
2183                         vc->vc_attr >>= 1;
2184                 }
2185         } else if (!vc->vc_hi_font_mask && cnt == 512) {
2186                 vc->vc_hi_font_mask = 0x100;
2187                 if (vc->vc_can_do_color) {
2188                         vc->vc_complement_mask <<= 1;
2189                         vc->vc_s_complement_mask <<= 1;
2190                 }
2191                         
2192                 /* ++Edmund: reorder the attribute bits */
2193                 {
2194                         unsigned short *cp =
2195                             (unsigned short *) vc->vc_origin;
2196                         int count = vc->vc_screenbuf_size / 2;
2197                         unsigned short c;
2198                         for (; count > 0; count--, cp++) {
2199                                 unsigned short newc;
2200                                 c = scr_readw(cp);
2201                                 if (vc->vc_can_do_color)
2202                                         newc =
2203                                             ((c & 0xff00) << 1) | (c &
2204                                                                    0xff);
2205                                 else
2206                                         newc = c & ~0x100;
2207                                 scr_writew(newc, cp);
2208                         }
2209                         c = vc->vc_video_erase_char;
2210                         if (vc->vc_can_do_color) {
2211                                 vc->vc_video_erase_char =
2212                                     ((c & 0xff00) << 1) | (c & 0xff);
2213                                 vc->vc_attr <<= 1;
2214                         } else
2215                                 vc->vc_video_erase_char = c & ~0x100;
2216                 }
2217
2218         }
2219
2220         if (resize) {
2221                 /* reset wrap/pan */
2222                 info->var.xoffset = info->var.yoffset = p->yscroll = 0;
2223                 vc_resize(vc, info->var.xres / w, info->var.yres / h);
2224                 if (CON_IS_VISIBLE(vc) && softback_buf)
2225                         fbcon_update_softback(vc);
2226         } else if (CON_IS_VISIBLE(vc)
2227                    && vc->vc_mode == KD_TEXT) {
2228                 fbcon_clear_margins(vc, 0);
2229                 update_screen(vc);
2230         }
2231
2232         if (old_data && (--REFCOUNT(old_data) == 0))
2233                 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2234         return 0;
2235 }
2236
2237 static int fbcon_copy_font(struct vc_data *vc, int con)
2238 {
2239         struct display *od = &fb_display[con];
2240         struct console_font *f = &vc->vc_font;
2241
2242         if (od->fontdata == f->data)
2243                 return 0;       /* already the same font... */
2244         return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2245 }
2246
2247 /*
2248  *  User asked to set font; we are guaranteed that
2249  *      a) width and height are in range 1..32
2250  *      b) charcount does not exceed 512
2251  *  but lets not assume that, since someone might someday want to use larger
2252  *  fonts. And charcount of 512 is small for unicode support.
2253  *
2254  *  However, user space gives the font in 32 rows , regardless of
2255  *  actual font height. So a new API is needed if support for larger fonts
2256  *  is ever implemented.
2257  */
2258
2259 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2260 {
2261         unsigned charcount = font->charcount;
2262         int w = font->width;
2263         int h = font->height;
2264         int size;
2265         int i, csum;
2266         u8 *new_data, *data = font->data;
2267         int pitch = (font->width+7) >> 3;
2268
2269         /* Is there a reason why fbconsole couldn't handle any charcount >256?
2270          * If not this check should be changed to charcount < 256 */
2271         if (charcount != 256 && charcount != 512)
2272                 return -EINVAL;
2273
2274         size = h * pitch * charcount;
2275
2276         new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2277
2278         if (!new_data)
2279                 return -ENOMEM;
2280
2281         new_data += FONT_EXTRA_WORDS * sizeof(int);
2282         FNTSIZE(new_data) = size;
2283         FNTCHARCNT(new_data) = charcount;
2284         REFCOUNT(new_data) = 0; /* usage counter */
2285         for (i=0; i< charcount; i++) {
2286                 memcpy(new_data + i*h*pitch, data +  i*32*pitch, h*pitch);
2287         }
2288
2289         /* Since linux has a nice crc32 function use it for counting font
2290          * checksums. */
2291         csum = crc32(0, new_data, size);
2292
2293         FNTSUM(new_data) = csum;
2294         /* Check if the same font is on some other console already */
2295         for (i = 0; i < MAX_NR_CONSOLES; i++) {
2296                 struct vc_data *tmp = vc_cons[i].d;
2297                 
2298                 if (fb_display[i].userfont &&
2299                     fb_display[i].fontdata &&
2300                     FNTSUM(fb_display[i].fontdata) == csum &&
2301                     FNTSIZE(fb_display[i].fontdata) == size &&
2302                     tmp->vc_font.width == w &&
2303                     !memcmp(fb_display[i].fontdata, new_data, size)) {
2304                         kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2305                         new_data = (u8 *)fb_display[i].fontdata;
2306                         break;
2307                 }
2308         }
2309         return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2310 }
2311
2312 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2313 {
2314         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2315         const struct font_desc *f;
2316
2317         if (!name)
2318                 f = get_default_font(info->var.xres, info->var.yres);
2319         else if (!(f = find_font(name)))
2320                 return -ENOENT;
2321
2322         font->width = f->width;
2323         font->height = f->height;
2324         return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2325 }
2326
2327 static u16 palette_red[16];
2328 static u16 palette_green[16];
2329 static u16 palette_blue[16];
2330
2331 static struct fb_cmap palette_cmap = {
2332         0, 16, palette_red, palette_green, palette_blue, NULL
2333 };
2334
2335 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2336 {
2337         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2338         int i, j, k, depth;
2339         u8 val;
2340
2341         if (fbcon_is_inactive(vc, info))
2342                 return -EINVAL;
2343
2344         if (!CON_IS_VISIBLE(vc))
2345                 return 0;
2346
2347         depth = fb_get_color_depth(&info->var, &info->fix);
2348         if (depth > 3) {
2349                 for (i = j = 0; i < 16; i++) {
2350                         k = table[i];
2351                         val = vc->vc_palette[j++];
2352                         palette_red[k] = (val << 8) | val;
2353                         val = vc->vc_palette[j++];
2354                         palette_green[k] = (val << 8) | val;
2355                         val = vc->vc_palette[j++];
2356                         palette_blue[k] = (val << 8) | val;
2357                 }
2358                 palette_cmap.len = 16;
2359                 palette_cmap.start = 0;
2360         /*
2361          * If framebuffer is capable of less than 16 colors,
2362          * use default palette of fbcon.
2363          */
2364         } else
2365                 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2366
2367         return fb_set_cmap(&palette_cmap, info);
2368 }
2369
2370 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2371 {
2372         unsigned long p;
2373         int line;
2374         
2375         if (vc->vc_num != fg_console || !softback_lines)
2376                 return (u16 *) (vc->vc_origin + offset);
2377         line = offset / vc->vc_size_row;
2378         if (line >= softback_lines)
2379                 return (u16 *) (vc->vc_origin + offset -
2380                                 softback_lines * vc->vc_size_row);
2381         p = softback_curr + offset;
2382         if (p >= softback_end)
2383                 p += softback_buf - softback_end;
2384         return (u16 *) p;
2385 }
2386
2387 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2388                                  int *px, int *py)
2389 {
2390         unsigned long ret;
2391         int x, y;
2392
2393         if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2394                 unsigned long offset = (pos - vc->vc_origin) / 2;
2395
2396                 x = offset % vc->vc_cols;
2397                 y = offset / vc->vc_cols;
2398                 if (vc->vc_num == fg_console)
2399                         y += softback_lines;
2400                 ret = pos + (vc->vc_cols - x) * 2;
2401         } else if (vc->vc_num == fg_console && softback_lines) {
2402                 unsigned long offset = pos - softback_curr;
2403
2404                 if (pos < softback_curr)
2405                         offset += softback_end - softback_buf;
2406                 offset /= 2;
2407                 x = offset % vc->vc_cols;
2408                 y = offset / vc->vc_cols;
2409                 ret = pos + (vc->vc_cols - x) * 2;
2410                 if (ret == softback_end)
2411                         ret = softback_buf;
2412                 if (ret == softback_in)
2413                         ret = vc->vc_origin;
2414         } else {
2415                 /* Should not happen */
2416                 x = y = 0;
2417                 ret = vc->vc_origin;
2418         }
2419         if (px)
2420                 *px = x;
2421         if (py)
2422                 *py = y;
2423         return ret;
2424 }
2425
2426 /* As we might be inside of softback, we may work with non-contiguous buffer,
2427    that's why we have to use a separate routine. */
2428 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2429 {
2430         while (cnt--) {
2431                 u16 a = scr_readw(p);
2432                 if (!vc->vc_can_do_color)
2433                         a ^= 0x0800;
2434                 else if (vc->vc_hi_font_mask == 0x100)
2435                         a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2436                             (((a) & 0x0e00) << 4);
2437                 else
2438                         a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2439                             (((a) & 0x0700) << 4);
2440                 scr_writew(a, p++);
2441                 if (p == (u16 *) softback_end)
2442                         p = (u16 *) softback_buf;
2443                 if (p == (u16 *) softback_in)
2444                         p = (u16 *) vc->vc_origin;
2445         }
2446 }
2447
2448 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2449 {
2450         struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2451         struct display *p = &fb_display[fg_console];
2452         int offset, limit, scrollback_old;
2453
2454         if (softback_top) {
2455                 if (vc->vc_num != fg_console)
2456                         return 0;
2457                 if (vc->vc_mode != KD_TEXT || !lines)
2458                         return 0;
2459                 if (logo_shown >= 0) {
2460                         struct vc_data *conp2 = vc_cons[logo_shown].d;
2461
2462                         if (conp2->vc_top == logo_lines
2463                             && conp2->vc_bottom == conp2->vc_rows)
2464                                 conp2->vc_top = 0;
2465                         if (logo_shown == vc->vc_num) {
2466                                 unsigned long p, q;
2467                                 int i;
2468
2469                                 p = softback_in;
2470                                 q = vc->vc_origin +
2471                                     logo_lines * vc->vc_size_row;
2472                                 for (i = 0; i < logo_lines; i++) {
2473                                         if (p == softback_top)
2474                                                 break;
2475                                         if (p == softback_buf)
2476                                                 p = softback_end;
2477                                         p -= vc->vc_size_row;
2478                                         q -= vc->vc_size_row;
2479                                         scr_memcpyw((u16 *) q, (u16 *) p,
2480                                                     vc->vc_size_row);
2481                                 }
2482                                 softback_in = p;
2483                                 update_region(vc, vc->vc_origin,
2484                                               logo_lines * vc->vc_cols);
2485                         }
2486                         logo_shown = FBCON_LOGO_CANSHOW;
2487                 }
2488                 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2489                 fbcon_redraw_softback(vc, p, lines);
2490                 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2491                 return 0;
2492         }
2493
2494         if (!scrollback_phys_max)
2495                 return -ENOSYS;
2496
2497         scrollback_old = scrollback_current;
2498         scrollback_current -= lines;
2499         if (scrollback_current < 0)
2500                 scrollback_current = 0;
2501         else if (scrollback_current > scrollback_max)
2502                 scrollback_current = scrollback_max;
2503         if (scrollback_current == scrollback_old)
2504                 return 0;
2505
2506         if (fbcon_is_inactive(vc, info))
2507                 return 0;
2508
2509         fbcon_cursor(vc, CM_ERASE);
2510
2511         offset = p->yscroll - scrollback_current;
2512         limit = p->vrows;
2513         switch (p->scrollmode) {
2514         case SCROLL_WRAP_MOVE:
2515                 info->var.vmode |= FB_VMODE_YWRAP;
2516                 break;
2517         case SCROLL_PAN_MOVE:
2518         case SCROLL_PAN_REDRAW:
2519                 limit -= vc->vc_rows;
2520                 info->var.vmode &= ~FB_VMODE_YWRAP;
2521                 break;
2522         }
2523         if (offset < 0)
2524                 offset += limit;
2525         else if (offset >= limit)
2526                 offset -= limit;
2527         info->var.xoffset = 0;
2528         info->var.yoffset = offset * vc->vc_font.height;
2529         update_var(vc->vc_num, info);
2530         if (!scrollback_current)
2531                 fbcon_cursor(vc, CM_DRAW);
2532         return 0;
2533 }
2534
2535 static int fbcon_set_origin(struct vc_data *vc)
2536 {
2537         if (softback_lines)
2538                 fbcon_scrolldelta(vc, softback_lines);
2539         return 0;
2540 }
2541
2542 static void fbcon_suspended(struct fb_info *info)
2543 {
2544         struct vc_data *vc = NULL;
2545         struct fbcon_ops *ops = info->fbcon_par;
2546
2547         if (!ops || ops->currcon < 0)
2548                 return;
2549         vc = vc_cons[ops->currcon].d;
2550
2551         /* Clear cursor, restore saved data */
2552         fbcon_cursor(vc, CM_ERASE);
2553 }
2554
2555 static void fbcon_resumed(struct fb_info *info)
2556 {
2557         struct vc_data *vc;
2558         struct fbcon_ops *ops = info->fbcon_par;
2559
2560         if (!ops || ops->currcon < 0)
2561                 return;
2562         vc = vc_cons[ops->currcon].d;
2563
2564         update_screen(vc);
2565 }
2566
2567 static void fbcon_modechanged(struct fb_info *info)
2568 {
2569         struct fbcon_ops *ops = info->fbcon_par;
2570         struct vc_data *vc;
2571         struct display *p;
2572         int rows, cols;
2573
2574         if (!ops || ops->currcon < 0)
2575                 return;
2576         vc = vc_cons[ops->currcon].d;
2577         if (vc->vc_mode != KD_TEXT || registered_fb[con2fb_map[ops->currcon]] != info)
2578                 return;
2579
2580         p = &fb_display[vc->vc_num];
2581
2582         info->var.xoffset = info->var.yoffset = p->yscroll = 0;
2583
2584         if (CON_IS_VISIBLE(vc)) {
2585                 var_to_display(p, &info->var, info);
2586                 cols = info->var.xres / vc->vc_font.width;
2587                 rows = info->var.yres / vc->vc_font.height;
2588                 vc_resize(vc, cols, rows);
2589                 updatescrollmode(p, info, vc);
2590                 scrollback_max = 0;
2591                 scrollback_current = 0;
2592                 update_var(vc->vc_num, info);
2593                 fbcon_set_palette(vc, color_table);
2594                 update_screen(vc);
2595                 if (softback_buf)
2596                         fbcon_update_softback(vc);
2597         }
2598 }
2599
2600 static void fbcon_set_all_vcs(struct fb_info *info)
2601 {
2602         struct fbcon_ops *ops = info->fbcon_par;
2603         struct vc_data *vc;
2604         struct display *p;
2605         int i, rows, cols;
2606
2607         if (!ops || ops->currcon < 0)
2608                 return;
2609
2610         for (i = 0; i < MAX_NR_CONSOLES; i++) {
2611                 vc = vc_cons[i].d;
2612                 if (!vc || vc->vc_mode != KD_TEXT ||
2613                     registered_fb[con2fb_map[i]] != info)
2614                         continue;
2615
2616                 p = &fb_display[vc->vc_num];
2617
2618                 info->var.xoffset = info->var.yoffset = p->yscroll = 0;
2619                 var_to_display(p, &info->var, info);
2620                 cols = info->var.xres / vc->vc_font.width;
2621                 rows = info->var.yres / vc->vc_font.height;
2622                 vc_resize(vc, cols, rows);
2623
2624                 if (CON_IS_VISIBLE(vc)) {
2625                         updatescrollmode(p, info, vc);
2626                         scrollback_max = 0;
2627                         scrollback_current = 0;
2628                         update_var(vc->vc_num, info);
2629                         fbcon_set_palette(vc, color_table);
2630                         update_screen(vc);
2631                         if (softback_buf)
2632                                 fbcon_update_softback(vc);
2633                 }
2634         }
2635 }
2636
2637 static int fbcon_mode_deleted(struct fb_info *info,
2638                               struct fb_videomode *mode)
2639 {
2640         struct fb_info *fb_info;
2641         struct display *p;
2642         int i, j, found = 0;
2643
2644         /* before deletion, ensure that mode is not in use */
2645         for (i = first_fb_vc; i <= last_fb_vc; i++) {
2646                 j = con2fb_map[i];
2647                 if (j == -1)
2648                         continue;
2649                 fb_info = registered_fb[j];
2650                 if (fb_info != info)
2651                         continue;
2652                 p = &fb_display[i];
2653                 if (!p || !p->mode)
2654                         continue;
2655                 if (fb_mode_is_equal(p->mode, mode)) {
2656                         found = 1;
2657                         break;
2658                 }
2659         }
2660         return found;
2661 }
2662
2663 static int fbcon_fb_registered(int idx)
2664 {
2665         int ret = 0, i;
2666
2667         if (info_idx == -1) {
2668                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
2669                         if (con2fb_map_boot[i] == idx) {
2670                                 info_idx = idx;
2671                                 break;
2672                         }
2673                 }
2674                 if (info_idx != -1)
2675                         ret = fbcon_takeover(1);
2676         } else {
2677                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
2678                         if (con2fb_map_boot[i] == idx)
2679                                 set_con2fb_map(i, idx, 0);
2680                 }
2681         }
2682
2683         return ret;
2684 }
2685
2686 static void fbcon_fb_blanked(struct fb_info *info, int blank)
2687 {
2688         struct fbcon_ops *ops = info->fbcon_par;
2689         struct vc_data *vc;
2690
2691         if (!ops || ops->currcon < 0)
2692                 return;
2693
2694         vc = vc_cons[ops->currcon].d;
2695         if (vc->vc_mode != KD_TEXT ||
2696                         registered_fb[con2fb_map[ops->currcon]] != info)
2697                 return;
2698
2699         if (CON_IS_VISIBLE(vc)) {
2700                 if (blank)
2701                         do_blank_screen(0);
2702                 else
2703                         do_unblank_screen(0);
2704         }
2705         ops->blank_state = blank;
2706 }
2707
2708 static void fbcon_new_modelist(struct fb_info *info)
2709 {
2710         int i;
2711         struct vc_data *vc;
2712         struct fb_var_screeninfo var;
2713         struct fb_videomode *mode;
2714
2715         for (i = 0; i < MAX_NR_CONSOLES; i++) {
2716                 if (registered_fb[con2fb_map[i]] != info)
2717                         continue;
2718                 if (!fb_display[i].mode)
2719                         continue;
2720                 vc = vc_cons[i].d;
2721                 display_to_var(&var, &fb_display[i]);
2722                 mode = fb_find_nearest_mode(&var, &info->modelist);
2723                 fb_videomode_to_var(&var, mode);
2724
2725                 if (vc)
2726                         fbcon_set_disp(info, &var, vc);
2727                 else
2728                         fbcon_preset_disp(info, &var, i);
2729
2730         }
2731 }
2732
2733 static int fbcon_event_notify(struct notifier_block *self, 
2734                               unsigned long action, void *data)
2735 {
2736         struct fb_event *event = data;
2737         struct fb_info *info = event->info;
2738         struct fb_videomode *mode;
2739         struct fb_con2fbmap *con2fb;
2740         int ret = 0;
2741
2742         switch(action) {
2743         case FB_EVENT_SUSPEND:
2744                 fbcon_suspended(info);
2745                 break;
2746         case FB_EVENT_RESUME:
2747                 fbcon_resumed(info);
2748                 break;
2749         case FB_EVENT_MODE_CHANGE:
2750                 fbcon_modechanged(info);
2751                 break;
2752         case FB_EVENT_MODE_CHANGE_ALL:
2753                 fbcon_set_all_vcs(info);
2754                 break;
2755         case FB_EVENT_MODE_DELETE:
2756                 mode = event->data;
2757                 ret = fbcon_mode_deleted(info, mode);
2758                 break;
2759         case FB_EVENT_FB_REGISTERED:
2760                 ret = fbcon_fb_registered(info->node);
2761                 break;
2762         case FB_EVENT_SET_CONSOLE_MAP:
2763                 con2fb = event->data;
2764                 ret = set_con2fb_map(con2fb->console - 1,
2765                                      con2fb->framebuffer, 1);
2766                 break;
2767         case FB_EVENT_GET_CONSOLE_MAP:
2768                 con2fb = event->data;
2769                 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
2770                 break;
2771         case FB_EVENT_BLANK:
2772                 fbcon_fb_blanked(info, *(int *)event->data);
2773                 break;
2774         case FB_EVENT_NEW_MODELIST:
2775                 fbcon_new_modelist(info);
2776                 break;
2777         }
2778
2779         return ret;
2780 }
2781
2782 /*
2783  *  The console `switch' structure for the frame buffer based console
2784  */
2785
2786 static const struct consw fb_con = {
2787         .owner                  = THIS_MODULE,
2788         .con_startup            = fbcon_startup,
2789         .con_init               = fbcon_init,
2790         .con_deinit             = fbcon_deinit,
2791         .con_clear              = fbcon_clear,
2792         .con_putc               = fbcon_putc,
2793         .con_putcs              = fbcon_putcs,
2794         .con_cursor             = fbcon_cursor,
2795         .con_scroll             = fbcon_scroll,
2796         .con_bmove              = fbcon_bmove,
2797         .con_switch             = fbcon_switch,
2798         .con_blank              = fbcon_blank,
2799         .con_font_set           = fbcon_set_font,
2800         .con_font_get           = fbcon_get_font,
2801         .con_font_default       = fbcon_set_def_font,
2802         .con_font_copy          = fbcon_copy_font,
2803         .con_set_palette        = fbcon_set_palette,
2804         .con_scrolldelta        = fbcon_scrolldelta,
2805         .con_set_origin         = fbcon_set_origin,
2806         .con_invert_region      = fbcon_invert_region,
2807         .con_screen_pos         = fbcon_screen_pos,
2808         .con_getxy              = fbcon_getxy,
2809         .con_resize             = fbcon_resize,
2810 };
2811
2812 static struct notifier_block fbcon_event_notifier = {
2813         .notifier_call  = fbcon_event_notify,
2814 };
2815
2816 static int __init fb_console_init(void)
2817 {
2818         int i;
2819
2820         acquire_console_sem();
2821         fb_register_client(&fbcon_event_notifier);
2822         release_console_sem();
2823
2824         for (i = 0; i < MAX_NR_CONSOLES; i++)
2825                 con2fb_map[i] = -1;
2826
2827         if (num_registered_fb) {
2828                 for (i = 0; i < FB_MAX; i++) {
2829                         if (registered_fb[i] != NULL) {
2830                                 info_idx = i;
2831                                 break;
2832                         }
2833                 }
2834                 fbcon_takeover(0);
2835         }
2836
2837         return 0;
2838 }
2839
2840 module_init(fb_console_init);
2841
2842 #ifdef MODULE
2843
2844 static void __exit fb_console_exit(void)
2845 {
2846         acquire_console_sem();
2847         fb_unregister_client(&fbcon_event_notifier);
2848         release_console_sem();
2849         give_up_console(&fb_con);
2850 }       
2851
2852 module_exit(fb_console_exit);
2853
2854 #endif
2855
2856 MODULE_LICENSE("GPL");