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