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