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