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