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