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