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