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