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