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