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