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