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