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