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