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