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