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