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