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