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