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