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