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