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