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