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