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