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