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