Merge to Fedora kernel-2.6.7-1.441
[linux-2.6.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
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 struct display fb_display[MAX_NR_CONSOLES];
104 char con2fb_map[MAX_NR_CONSOLES];
105 static int logo_height;
106 static int logo_lines;
107 static int logo_shown = -1;
108 /* Software scrollback */
109 int fbcon_softback_size = 32768;
110 static unsigned long softback_buf, softback_curr;
111 static unsigned long softback_in;
112 static unsigned long softback_top, softback_end;
113 static int softback_lines;
114 /* console mappings */
115 static int first_fb_vc;
116 static int last_fb_vc = MAX_NR_CONSOLES - 1;
117 static int fbcon_is_default = 1; 
118 /* font data */
119 static char fontname[40];
120
121 #define REFCOUNT(fd)    (((int *)(fd))[-1])
122 #define FNTSIZE(fd)     (((int *)(fd))[-2])
123 #define FNTCHARCNT(fd)  (((int *)(fd))[-3])
124 #define FNTSUM(fd)      (((int *)(fd))[-4])
125 #define FONT_EXTRA_WORDS 4
126
127 #define CM_SOFTBACK     (8)
128
129 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
130
131 static void fbcon_free_font(struct display *);
132 static int fbcon_set_origin(struct vc_data *);
133
134 #define CURSOR_DRAW_DELAY               (1)
135
136 /* # VBL ints between cursor state changes */
137 #define ARM_CURSOR_BLINK_RATE           (10)
138 #define ATARI_CURSOR_BLINK_RATE         (42)
139 #define MAC_CURSOR_BLINK_RATE           (32)
140 #define DEFAULT_CURSOR_BLINK_RATE       (20)
141
142 static int vbl_cursor_cnt;
143
144 #define divides(a, b)   ((!(a) || (b)%(a)) ? 0 : 1)
145
146 /*
147  *  Interface used by the world
148  */
149
150 static const char *fbcon_startup(void);
151 static void fbcon_init(struct vc_data *vc, int init);
152 static void fbcon_deinit(struct vc_data *vc);
153 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
154                         int width);
155 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
156 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
157                         int count, int ypos, int xpos);
158 static void fbcon_cursor(struct vc_data *vc, int mode);
159 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
160                         int count);
161 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
162                         int height, int width);
163 static int fbcon_switch(struct vc_data *vc);
164 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
165 static int fbcon_font_op(struct vc_data *vc, struct console_font_op *op);
166 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
167 static int fbcon_scrolldelta(struct vc_data *vc, int lines);
168
169
170 /*
171  *  Internal routines
172  */
173 static __inline__ int real_y(struct display *p, int ypos);
174 static __inline__ void ywrap_up(struct vc_data *vc, int count);
175 static __inline__ void ywrap_down(struct vc_data *vc, int count);
176 static __inline__ void ypan_up(struct vc_data *vc, int count);
177 static __inline__ void ypan_down(struct vc_data *vc, int count);
178 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
179                             int dy, int dx, int height, int width, u_int y_break);
180
181 #ifdef CONFIG_MAC
182 /*
183  * On the Macintoy, there may or may not be a working VBL int. We need to probe
184  */
185 static int vbl_detected;
186
187 static irqreturn_t fb_vbl_detect(int irq, void *dummy, struct pt_regs *fp)
188 {
189         vbl_detected++;
190         return IRQ_HANDLED;
191 }
192 #endif
193
194 static void fb_flashcursor(void *private)
195 {
196         struct fb_info *info = (struct fb_info *) private;
197
198         if (!info || info->state != FBINFO_STATE_RUNNING ||
199             info->cursor.rop == ROP_COPY)
200                 return;
201         acquire_console_sem();
202         info->cursor.enable ^= 1;
203         info->fbops->fb_cursor(info, &info->cursor);
204         release_console_sem();
205 }
206
207 #if (defined(__arm__) && defined(IRQ_VSYNCPULSE)) || defined(CONFIG_ATARI) || defined(CONFIG_MAC)
208 static int cursor_blink_rate;
209 static irqreturn_t fb_vbl_handler(int irq, void *dev_id, struct pt_regs *fp)
210 {
211         struct fb_info *info = dev_id;
212
213         if (vbl_cursor_cnt && --vbl_cursor_cnt == 0) {
214                 schedule_work(&info->queue);    
215                 vbl_cursor_cnt = cursor_blink_rate; 
216         }
217         return IRQ_HANDLED;
218 }
219 #endif
220         
221 static void cursor_timer_handler(unsigned long dev_addr);
222
223 static struct timer_list cursor_timer =
224                 TIMER_INITIALIZER(cursor_timer_handler, 0, 0);
225
226 static void cursor_timer_handler(unsigned long dev_addr)
227 {
228         struct fb_info *info = (struct fb_info *) dev_addr;
229         
230         schedule_work(&info->queue);    
231         mod_timer(&cursor_timer, jiffies + HZ/5);
232 }
233
234 int __init fb_console_setup(char *this_opt)
235 {
236         char *options;
237         int i, j;
238
239         if (!this_opt || !*this_opt)
240                 return 0;
241
242         while ((options = strsep(&this_opt, ",")) != NULL) {
243                 if (!strncmp(options, "font:", 5))
244                         strcpy(fontname, options + 5);
245                 
246                 if (!strncmp(options, "scrollback:", 11)) {
247                         options += 11;
248                         if (*options) {
249                                 fbcon_softback_size = simple_strtoul(options, &options, 0);
250                                 if (*options == 'k' || *options == 'K') {
251                                         fbcon_softback_size *= 1024;
252                                         options++;
253                                 }
254                                 if (*options != ',')
255                                         return 0;
256                                 options++;
257                         } else
258                                 return 0;
259                 }
260                 
261                 if (!strncmp(options, "map:", 4)) {
262                         options += 4;
263                         if (*options)
264                                 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
265                                         if (!options[j])
266                                                 j = 0;
267                                         con2fb_map[i] = (options[j++]-'0') % FB_MAX;
268                                 }
269                         return 0;
270                 }
271
272                 if (!strncmp(options, "vc:", 3)) {
273                         options += 3;
274                         if (*options)
275                                 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
276                         if (first_fb_vc < 0)
277                                 first_fb_vc = 0;
278                         if (*options++ == '-')
279                                 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
280                         fbcon_is_default = 0; 
281                 }       
282         }
283         return 0;
284 }
285
286 __setup("fbcon=", fb_console_setup);
287
288 /**
289  *      set_con2fb_map - map console to frame buffer device
290  *      @unit: virtual console number to map
291  *      @newidx: frame buffer index to map virtual console to
292  *
293  *      Maps a virtual console @unit to a frame buffer device
294  *      @newidx.
295  */
296 int set_con2fb_map(int unit, int newidx)
297 {
298         struct vc_data *vc = vc_cons[unit].d;
299
300         if (!vc)
301                 return -ENODEV;
302         con2fb_map[unit] = newidx;
303         fbcon_is_default = (vc->vc_sw == &fb_con) ? 1 : 0;
304         return take_over_console(&fb_con, unit, unit, fbcon_is_default);
305 }
306
307 /*
308  * Accelerated handlers.
309  */
310 void accel_bmove(struct vc_data *vc, struct fb_info *info, int sy, 
311                 int sx, int dy, int dx, int height, int width)
312 {
313         struct fb_copyarea area;
314
315         area.sx = sx * vc->vc_font.width;
316         area.sy = sy * vc->vc_font.height;
317         area.dx = dx * vc->vc_font.width;
318         area.dy = dy * vc->vc_font.height;
319         area.height = height * vc->vc_font.height;
320         area.width = width * vc->vc_font.width;
321
322         info->fbops->fb_copyarea(info, &area);
323 }
324
325 void accel_clear(struct vc_data *vc, struct fb_info *info, int sy,
326                         int sx, int height, int width)
327 {
328         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
329         struct fb_fillrect region;
330
331         region.color = attr_bgcol_ec(bgshift, vc);
332         region.dx = sx * vc->vc_font.width;
333         region.dy = sy * vc->vc_font.height;
334         region.width = width * vc->vc_font.width;
335         region.height = height * vc->vc_font.height;
336         region.rop = ROP_COPY;
337
338         info->fbops->fb_fillrect(info, &region);
339 }       
340
341 void accel_putcs(struct vc_data *vc, struct fb_info *info,
342                         const unsigned short *s, int count, int yy, int xx)
343 {
344         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
345         unsigned int width = (vc->vc_font.width + 7) >> 3;
346         unsigned int cellsize = vc->vc_font.height * width;
347         unsigned int maxcnt = info->pixmap.size/cellsize;
348         unsigned int scan_align = info->pixmap.scan_align - 1;
349         unsigned int buf_align = info->pixmap.buf_align - 1;
350         unsigned int shift_low = 0, mod = vc->vc_font.width % 8;
351         unsigned int shift_high = 8, pitch, cnt, size, k;
352         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
353         int fgshift = (vc->vc_hi_font_mask) ? 9 : 8;
354         unsigned int idx = vc->vc_font.width >> 3;
355         struct fb_image image;
356         u16 c = scr_readw(s);
357         u8 *src, *dst, *dst0;
358
359         image.fg_color = attr_fgcol(fgshift, c);
360         image.bg_color = attr_bgcol(bgshift, c);
361         image.dx = xx * vc->vc_font.width;
362         image.dy = yy * vc->vc_font.height;
363         image.height = vc->vc_font.height;
364         image.depth = 1;
365
366         while (count) {
367                 if (count > maxcnt)
368                         cnt = k = maxcnt;
369                 else
370                         cnt = k = count;
371
372                 image.width = vc->vc_font.width * cnt;
373                 pitch = ((image.width + 7) >> 3) + scan_align;
374                 pitch &= ~scan_align;
375                 size = pitch * image.height + buf_align;
376                 size &= ~buf_align;
377                 dst0 = fb_get_buffer_offset(info, &info->pixmap, size);
378                 image.data = dst0;
379                 while (k--) {
380                         src = vc->vc_font.data + (scr_readw(s++) & charmask)*cellsize;
381                         dst = dst0;
382
383                         if (mod) {
384                                 fb_move_buf_unaligned(info, &info->pixmap, dst, pitch,
385                                                    src, idx, image.height, shift_high,
386                                                    shift_low, mod);
387                                 shift_low += mod;
388                                 dst0 += (shift_low >= 8) ? width : width - 1;
389                                 shift_low &= 7;
390                                 shift_high = 8 - shift_low;
391                         } else {
392                                 fb_move_buf_aligned(info, &info->pixmap, dst, pitch,
393                                                  src, idx, image.height);
394                                 dst0 += width;
395                         }
396                 }
397                 info->fbops->fb_imageblit(info, &image);
398                 image.dx += cnt * vc->vc_font.width;
399                 count -= cnt;
400         }
401 }
402
403 void accel_clear_margins(struct vc_data *vc, struct fb_info *info,
404                                 int bottom_only)
405 {
406         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
407         unsigned int cw = vc->vc_font.width;
408         unsigned int ch = vc->vc_font.height;
409         unsigned int rw = info->var.xres - (vc->vc_cols*cw);
410         unsigned int bh = info->var.yres - (vc->vc_rows*ch);
411         unsigned int rs = info->var.xres - rw;
412         unsigned int bs = info->var.yres - bh;
413         struct fb_fillrect region;
414
415         region.color = attr_bgcol_ec(bgshift, vc);
416         region.rop = ROP_COPY;
417
418         if (rw && !bottom_only) {
419                 region.dx = info->var.xoffset + rs;
420                 region.dy = 0;
421                 region.width = rw;
422                 region.height = info->var.yres_virtual;
423                 info->fbops->fb_fillrect(info, &region);
424         }
425
426         if (bh) {
427                 region.dx = info->var.xoffset;
428                 region.dy = info->var.yoffset + bs;
429                 region.width = rs;
430                 region.height = bh;
431                 info->fbops->fb_fillrect(info, &region);
432         }       
433 }       
434
435 /*
436  *  Low Level Operations
437  */
438 /* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
439 static const char *fbcon_startup(void)
440 {
441         const char *display_desc = "frame buffer device";
442         struct display *p = &fb_display[fg_console];
443         struct vc_data *vc = vc_cons[fg_console].d;
444         struct font_desc *font = NULL;
445         struct module *owner;
446         struct fb_info *info;
447         static int done = 0;
448         int cols, rows;
449         int irqres;
450
451         irqres = 1;
452         /*
453          *  If num_registered_fb is zero, this is a call for the dummy part.
454          *  The frame buffer devices weren't initialized yet.
455          */
456         if (!num_registered_fb || done)
457                 return display_desc;
458         done = 1;
459
460         info = registered_fb[0];        
461         if (!info)      return NULL;
462         info->currcon = -1;
463         
464         owner = info->fbops->owner;
465         if (!try_module_get(owner))
466                 return NULL;
467         if (info->fbops->fb_open && info->fbops->fb_open(info, 0))
468                 module_put(owner);
469         
470         if (info->fix.type != FB_TYPE_TEXT) {
471                 if (fbcon_softback_size) {
472                         if (!softback_buf) {
473                                 softback_buf =
474                                     (unsigned long)
475                                     kmalloc(fbcon_softback_size,
476                                             GFP_KERNEL);
477                                 if (!softback_buf) {
478                                         fbcon_softback_size = 0;
479                                         softback_top = 0;
480                                 }
481                         }
482                 } else {
483                         if (softback_buf) {
484                                 kfree((void *) softback_buf);
485                                 softback_buf = 0;
486                                 softback_top = 0;
487                         }
488                 }
489                 if (softback_buf)
490                         softback_in = softback_top = softback_curr =
491                             softback_buf;
492                 softback_lines = 0;
493         }
494
495         /* Setup default font */
496         if (!p->fontdata) {
497                 if (!fontname[0] || !(font = find_font(fontname)))
498                         font = get_default_font(info->var.xres,
499                                                    info->var.yres);
500                 vc->vc_font.width = font->width;
501                 vc->vc_font.height = font->height;
502                 vc->vc_font.data = p->fontdata = font->data;
503                 vc->vc_font.charcount = 256; /* FIXME  Need to support more fonts */
504         }
505
506         /*
507          * We must always set the mode. The mode of the previous console
508          * driver could be in the same resolution but we are using different
509          * hardware so we have to initialize the hardware.
510          */
511         if (info->fbops->fb_set_par)
512                 info->fbops->fb_set_par(info);
513         cols = info->var.xres/vc->vc_font.width;
514         rows = info->var.yres/vc->vc_font.height;
515         vc_resize(vc->vc_num, cols, rows);
516
517         DPRINTK("mode:   %s\n", info->fix.id);
518         DPRINTK("visual: %d\n", info->fix.visual);
519         DPRINTK("res:    %dx%d-%d\n", info->var.xres,
520                 info->var.yres,
521                 info->var.bits_per_pixel);
522         con_set_default_unimap(vc->vc_num);
523
524 #ifdef CONFIG_ATARI
525         if (MACH_IS_ATARI) {
526                 cursor_blink_rate = ATARI_CURSOR_BLINK_RATE;
527                 irqres =
528                     request_irq(IRQ_AUTO_4, fb_vbl_handler,
529                                 IRQ_TYPE_PRIO, "framebuffer vbl",
530                                 info);
531         }
532 #endif                          /* CONFIG_ATARI */
533
534 #ifdef CONFIG_MAC
535         /*
536          * On a Macintoy, the VBL interrupt may or may not be active. 
537          * As interrupt based cursor is more reliable and race free, we 
538          * probe for VBL interrupts.
539          */
540         if (MACH_IS_MAC) {
541                 int ct = 0;
542                 /*
543                  * Probe for VBL: set temp. handler ...
544                  */
545                 irqres = request_irq(IRQ_MAC_VBL, fb_vbl_detect, 0,
546                                      "framebuffer vbl", info);
547                 vbl_detected = 0;
548
549                 /*
550                  * ... and spin for 20 ms ...
551                  */
552                 while (!vbl_detected && ++ct < 1000)
553                         udelay(20);
554
555                 if (ct == 1000)
556                         printk
557                             ("fbcon_startup: No VBL detected, using timer based cursor.\n");
558
559                 free_irq(IRQ_MAC_VBL, fb_vbl_detect);
560
561                 if (vbl_detected) {
562                         /*
563                          * interrupt based cursor ok
564                          */
565                         cursor_blink_rate = MAC_CURSOR_BLINK_RATE;
566                         irqres =
567                             request_irq(IRQ_MAC_VBL, fb_vbl_handler, 0,
568                                         "framebuffer vbl", info);
569                 } else {
570                         /*
571                          * VBL not detected: fall through, use timer based cursor
572                          */
573                         irqres = 1;
574                 }
575         }
576 #endif                          /* CONFIG_MAC */
577
578 #if defined(__arm__) && defined(IRQ_VSYNCPULSE)
579         cursor_blink_rate = ARM_CURSOR_BLINK_RATE;
580         irqres = request_irq(IRQ_VSYNCPULSE, fb_vbl_handler, SA_SHIRQ,
581                              "framebuffer vbl", info);
582 #endif
583         /* Initialize the work queue. If the driver provides its
584          * own work queue this means it will use something besides 
585          * default timer to flash the cursor. */
586         if (!info->queue.func) {
587                 INIT_WORK(&info->queue, fb_flashcursor, info);
588                 
589                 cursor_timer.expires = jiffies + HZ / 5;
590                 cursor_timer.data = (unsigned long ) info;
591                 add_timer(&cursor_timer);
592         }
593         return display_desc;
594 }
595
596 static void fbcon_init(struct vc_data *vc, int init)
597 {
598         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
599         struct vc_data **default_mode = vc->vc_display_fg;
600         struct display *t, *p = &fb_display[vc->vc_num];
601         int display_fg = (*default_mode)->vc_num;
602         int logo = 1, rows, cols, charcnt = 256;
603         unsigned short *save = NULL, *r, *q;
604         int cap = info->flags;
605
606         if (vc->vc_num != display_fg || (info->flags & FBINFO_MODULE) ||
607             (info->fix.type == FB_TYPE_TEXT))
608                 logo = 0;
609
610         info->var.xoffset = info->var.yoffset = p->yscroll = 0; /* reset wrap/pan */
611
612         /* If we are not the first console on this
613            fb, copy the font from that console */
614         t = &fb_display[display_fg];
615         vc->vc_font.width = (*default_mode)->vc_font.width;
616         vc->vc_font.height = (*default_mode)->vc_font.height;
617         vc->vc_font.data = p->fontdata = t->fontdata;
618         p->userfont = t->userfont;
619         if (p->userfont) {
620                 REFCOUNT(p->fontdata)++;
621                 charcnt = FNTCHARCNT(p->fontdata);
622         }
623         con_copy_unimap(vc->vc_num, display_fg);
624
625         vc->vc_can_do_color = info->var.bits_per_pixel != 1;
626         vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
627         if (charcnt == 256) {
628                 vc->vc_hi_font_mask = 0;
629         } else {
630                 vc->vc_hi_font_mask = 0x100;
631                 if (vc->vc_can_do_color)
632                         vc->vc_complement_mask <<= 1;
633         }
634
635         cols = info->var.xres / vc->vc_font.width;
636         rows = info->var.yres / vc->vc_font.height;
637         vc_resize(vc->vc_num, cols, rows);
638
639         if ((cap & FBINFO_HWACCEL_COPYAREA) && !(cap & FBINFO_HWACCEL_DISABLED))
640                 p->scrollmode = SCROLL_ACCEL;
641         else /* default to something safe */
642                 p->scrollmode = SCROLL_REDRAW;
643
644         /*
645          *  ++guenther: console.c:vc_allocate() relies on initializing
646          *  vc_{cols,rows}, but we must not set those if we are only
647          *  resizing the console.
648          */
649         if (init) {
650                 vc->vc_cols = cols;
651                 vc->vc_rows = rows;
652         }
653
654         if (logo) {
655                 /* Need to make room for the logo */
656                 int cnt;
657                 int step;
658
659                 logo_height = fb_prepare_logo(info);
660                 logo_lines = (logo_height + vc->vc_font.height - 1) /
661                              vc->vc_font.height;
662                 q = (unsigned short *) (vc->vc_origin +
663                                         vc->vc_size_row * rows);
664                 step = logo_lines * cols;
665                 for (r = q - logo_lines * cols; r < q; r++)
666                         if (scr_readw(r) != vc->vc_video_erase_char)
667                                 break;
668                 if (r != q && rows >= rows + logo_lines) {
669                         save = kmalloc(logo_lines * cols * 2, GFP_KERNEL);
670                         if (save) {
671                                 scr_memsetw(save, vc->vc_video_erase_char,
672                                             logo_lines * cols * 2);
673                                 r = q - step;
674                                 for (cnt = 0; cnt < logo_lines; cnt++, r += cols)
675                                         scr_memcpyw(save + cnt * cols, r, 2 * cols);
676                                 r = q;
677                         }
678                 }
679                 if (r == q) {
680                         /* We can scroll screen down */
681                         r = q - step - cols;
682                         for (cnt = rows - logo_lines; cnt > 0; cnt--) {
683                                 scr_memcpyw(r + step, r, vc->vc_size_row);
684                                 r -= cols;
685                         }
686                         if (!save) {
687                                 vc->vc_y += logo_lines;
688                                 vc->vc_pos += logo_lines * vc->vc_size_row;
689                         }
690                 }
691                 scr_memsetw((unsigned short *) vc->vc_origin,
692                             vc->vc_video_erase_char,
693                             vc->vc_size_row * logo_lines);
694
695                 if (CON_IS_VISIBLE(vc) && vt_cons[vc->vc_num]->vc_mode == KD_TEXT) {
696                         accel_clear_margins(vc, info, 0);
697                         update_screen(vc->vc_num);
698                 }
699                 if (save) {
700                         q = (unsigned short *) (vc->vc_origin +
701                                                 vc->vc_size_row *
702                                                 rows);
703                         scr_memcpyw(q, save, logo_lines * cols * 2);
704                         vc->vc_y += logo_lines;
705                         vc->vc_pos += logo_lines * vc->vc_size_row;
706                         kfree(save);
707                 }
708                 if (logo_lines > vc->vc_bottom) {
709                         logo_shown = -1;
710                         printk(KERN_INFO
711                                "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
712                 } else {
713                         logo_shown = -2;
714                         vc->vc_top = logo_lines;
715                 }
716         }
717
718         if (vc->vc_num == display_fg && softback_buf) {
719                 int l = fbcon_softback_size / vc->vc_size_row;
720                 if (l > 5)
721                         softback_end = softback_buf + l * vc->vc_size_row;
722                 else {
723                         /* Smaller scrollback makes no sense, and 0 would screw
724                            the operation totally */
725                         softback_top = 0;
726                 }
727         }
728 }
729
730 static void fbcon_deinit(struct vc_data *vc)
731 {
732         struct display *p = &fb_display[vc->vc_num];
733
734         fbcon_free_font(p);
735 }
736
737 /* ====================================================================== */
738
739 /*  fbcon_XXX routines - interface used by the world
740  *
741  *  This system is now divided into two levels because of complications
742  *  caused by hardware scrolling. Top level functions:
743  *
744  *      fbcon_bmove(), fbcon_clear(), fbcon_putc()
745  *
746  *  handles y values in range [0, scr_height-1] that correspond to real
747  *  screen positions. y_wrap shift means that first line of bitmap may be
748  *  anywhere on this display. These functions convert lineoffsets to
749  *  bitmap offsets and deal with the wrap-around case by splitting blits.
750  *
751  *      fbcon_bmove_physical_8()    -- These functions fast implementations
752  *      fbcon_clear_physical_8()    -- of original fbcon_XXX fns.
753  *      fbcon_putc_physical_8()     -- (font width != 8) may be added later
754  *
755  *  WARNING:
756  *
757  *  At the moment fbcon_putc() cannot blit across vertical wrap boundary
758  *  Implies should only really hardware scroll in rows. Only reason for
759  *  restriction is simplicity & efficiency at the moment.
760  */
761
762 static __inline__ int real_y(struct display *p, int ypos)
763 {
764         int rows = p->vrows;
765
766         ypos += p->yscroll;
767         return ypos < rows ? ypos : ypos - rows;
768 }
769
770
771 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
772                         int width)
773 {
774         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
775         
776         struct display *p = &fb_display[vc->vc_num];
777         u_int y_break;
778
779         if (!info->fbops->fb_blank && console_blanked)
780                 return;
781         if (info->state != FBINFO_STATE_RUNNING)
782                 return;
783
784         if (!height || !width)
785                 return;
786
787         /* Split blits that cross physical y_wrap boundary */
788
789         y_break = p->vrows - p->yscroll;
790         if (sy < y_break && sy + height - 1 >= y_break) {
791                 u_int b = y_break - sy;
792                 accel_clear(vc, info, real_y(p, sy), sx, b, width);
793                 accel_clear(vc, info, real_y(p, sy + b), sx, height - b,
794                                  width);
795         } else
796                 accel_clear(vc, info, real_y(p, sy), sx, height, width);
797 }
798
799 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
800 {
801         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
802         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
803         unsigned int scan_align = info->pixmap.scan_align - 1;
804         unsigned int buf_align = info->pixmap.buf_align - 1;
805         unsigned int width = (vc->vc_font.width + 7) >> 3;
806         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
807         int fgshift = (vc->vc_hi_font_mask) ? 9 : 8;
808         struct display *p = &fb_display[vc->vc_num];
809         unsigned int size, pitch;
810         struct fb_image image;
811         u8 *src, *dst;
812
813         if (!info->fbops->fb_blank && console_blanked)
814                 return;
815         if (info->state != FBINFO_STATE_RUNNING)
816                 return;
817
818         if (vt_cons[vc->vc_num]->vc_mode != KD_TEXT)
819                 return;
820
821         image.dx = xpos * vc->vc_font.width;
822         image.dy = real_y(p, ypos) * vc->vc_font.height;
823         image.width = vc->vc_font.width;
824         image.height = vc->vc_font.height;
825         image.fg_color = attr_fgcol(fgshift, c);
826         image.bg_color = attr_bgcol(bgshift, c);
827         image.depth = 1;
828
829         src = vc->vc_font.data + (c & charmask) * vc->vc_font.height * width;
830
831         pitch = width + scan_align;
832         pitch &= ~scan_align;
833         size = pitch * vc->vc_font.height;
834         size += buf_align;
835         size &= ~buf_align;
836
837         dst = fb_get_buffer_offset(info, &info->pixmap, size);
838         image.data = dst;
839
840         fb_move_buf_aligned(info, &info->pixmap, dst, pitch, src, width, image.height);
841
842         info->fbops->fb_imageblit(info, &image);
843 }
844
845 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
846                         int count, int ypos, int xpos)
847 {
848         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
849         struct display *p = &fb_display[vc->vc_num];
850
851         if (!info->fbops->fb_blank && console_blanked)
852                 return;
853         if (info->state != FBINFO_STATE_RUNNING)
854                 return;
855
856         if (vt_cons[vc->vc_num]->vc_mode != KD_TEXT)
857                 return;
858
859         accel_putcs(vc, info, s, count, real_y(p, ypos), xpos);
860 }
861
862 static void fbcon_cursor(struct vc_data *vc, int mode)
863 {
864         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
865         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
866         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
867         int fgshift = (vc->vc_hi_font_mask) ? 9 : 8;
868         struct display *p = &fb_display[vc->vc_num];
869         int w = (vc->vc_font.width + 7) >> 3, c;
870         int y = real_y(p, vc->vc_y);
871         struct fb_cursor cursor;
872         
873         if (mode & CM_SOFTBACK) {
874                 mode &= ~CM_SOFTBACK;
875                 if (softback_lines) {
876                         if (y + softback_lines >= vc->vc_rows)
877                                 mode = CM_ERASE;
878                         else
879                                 y += softback_lines;
880                 }
881         } else if (softback_lines)
882                 fbcon_set_origin(vc);
883
884         c = scr_readw((u16 *) vc->vc_pos);
885
886         cursor.image.data = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
887         cursor.set = FB_CUR_SETCUR;
888         cursor.image.depth = 1;
889         
890         switch (mode) {
891         case CM_ERASE:
892                 if (info->cursor.rop == ROP_XOR) {
893                         info->cursor.enable = 0;
894                         info->cursor.rop = ROP_COPY;
895                         info->fbops->fb_cursor(info, &cursor);
896                 }       
897                 break;
898         case CM_MOVE:
899         case CM_DRAW:
900                 info->cursor.enable = 1;
901                 
902                 if (info->cursor.image.fg_color != attr_fgcol(fgshift, c) ||
903                     info->cursor.image.bg_color != attr_bgcol(bgshift, c)) {
904                         cursor.image.fg_color = attr_fgcol(fgshift, c);
905                         cursor.image.bg_color = attr_bgcol(bgshift, c);
906                         cursor.set |= FB_CUR_SETCMAP;
907                 }
908                 
909                 if ((info->cursor.image.dx != (vc->vc_font.width * vc->vc_x)) ||
910                     (info->cursor.image.dy != (vc->vc_font.height * y))) {
911                         cursor.image.dx = vc->vc_font.width * vc->vc_x;
912                         cursor.image.dy = vc->vc_font.height * y;
913                         cursor.set |= FB_CUR_SETPOS;
914                 }
915
916                 if (info->cursor.image.height != vc->vc_font.height ||
917                     info->cursor.image.width != vc->vc_font.width) {
918                         cursor.image.height = vc->vc_font.height;
919                         cursor.image.width = vc->vc_font.width;
920                         cursor.set |= FB_CUR_SETSIZE;
921                 }
922
923                 if (info->cursor.hot.x || info->cursor.hot.y) {
924                         cursor.hot.x = cursor.hot.y = 0;
925                         cursor.set |= FB_CUR_SETHOT;
926                 }
927
928                 if ((cursor.set & FB_CUR_SETSIZE) || ((vc->vc_cursor_type & 0x0f) != p->cursor_shape)) {
929                         char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
930                         int cur_height, size, i = 0;
931
932                         if (!mask)      return; 
933                 
934                         if (info->cursor.mask)
935                                 kfree(info->cursor.mask);
936                         info->cursor.mask = mask;
937         
938                         p->cursor_shape = vc->vc_cursor_type & 0x0f;
939                         cursor.set |= FB_CUR_SETSHAPE;
940
941                         switch (vc->vc_cursor_type & 0x0f) {
942                         case CUR_NONE:
943                                 cur_height = 0;
944                                 break;
945                         case CUR_UNDERLINE:
946                                 cur_height = (vc->vc_font.height < 10) ? 1 : 2;
947                                 break;
948                         case CUR_LOWER_THIRD:
949                                 cur_height = vc->vc_font.height/3;
950                                 break;
951                         case CUR_LOWER_HALF:
952                                 cur_height = vc->vc_font.height >> 1;
953                                 break;
954                         case CUR_TWO_THIRDS:
955                                 cur_height = (vc->vc_font.height << 1)/3;
956                                 break;
957                         case CUR_BLOCK:
958                         default:
959                                 cur_height = vc->vc_font.height;
960                                 break;
961                         }
962                         size = (vc->vc_font.height - cur_height) * w;
963                         while (size--)
964                                 mask[i++] = 0;
965                         size = cur_height * w;
966                         while (size--)
967                                 mask[i++] = 0xff;
968                 }
969                 info->cursor.rop = ROP_XOR;
970                 info->fbops->fb_cursor(info, &cursor);
971                 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
972                 break;
973         }
974 }
975
976 static int scrollback_phys_max = 0;
977 static int scrollback_max = 0;
978 static int scrollback_current = 0;
979
980 int update_var(int con, struct fb_info *info)
981 {
982         if (con == info->currcon) 
983                 return fb_pan_display(info, &info->var);
984         return 0;
985 }
986
987 static __inline__ void ywrap_up(struct vc_data *vc, int count)
988 {
989         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
990         struct display *p = &fb_display[vc->vc_num];
991         
992         p->yscroll += count;
993         if (p->yscroll >= p->vrows)     /* Deal with wrap */
994                 p->yscroll -= p->vrows;
995         info->var.xoffset = 0;
996         info->var.yoffset = p->yscroll * vc->vc_font.height;
997         info->var.vmode |= FB_VMODE_YWRAP;
998         update_var(vc->vc_num, info);
999         scrollback_max += count;
1000         if (scrollback_max > scrollback_phys_max)
1001                 scrollback_max = scrollback_phys_max;
1002         scrollback_current = 0;
1003 }
1004
1005 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1006 {
1007         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1008         struct display *p = &fb_display[vc->vc_num];
1009         
1010         p->yscroll -= count;
1011         if (p->yscroll < 0)     /* Deal with wrap */
1012                 p->yscroll += p->vrows;
1013         info->var.xoffset = 0;
1014         info->var.yoffset = p->yscroll * vc->vc_font.height;
1015         info->var.vmode |= FB_VMODE_YWRAP;
1016         update_var(vc->vc_num, info);
1017         scrollback_max -= count;
1018         if (scrollback_max < 0)
1019                 scrollback_max = 0;
1020         scrollback_current = 0;
1021 }
1022
1023 static __inline__ void ypan_up(struct vc_data *vc, int count)
1024 {
1025         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1026         struct display *p = &fb_display[vc->vc_num];
1027         
1028         p->yscroll += count;
1029         if (p->yscroll > p->vrows - vc->vc_rows) {
1030                 accel_bmove(vc, info, p->vrows - vc->vc_rows, 
1031                                 0, 0, 0, vc->vc_rows, vc->vc_cols);
1032                 p->yscroll -= p->vrows - vc->vc_rows;
1033         }
1034         info->var.xoffset = 0;
1035         info->var.yoffset = p->yscroll * vc->vc_font.height;
1036         info->var.vmode &= ~FB_VMODE_YWRAP;
1037         update_var(vc->vc_num, info);
1038         accel_clear_margins(vc, info, 1);
1039         scrollback_max += count;
1040         if (scrollback_max > scrollback_phys_max)
1041                 scrollback_max = scrollback_phys_max;
1042         scrollback_current = 0;
1043 }
1044
1045 static __inline__ void ypan_down(struct vc_data *vc, int count)
1046 {
1047         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1048         struct display *p = &fb_display[vc->vc_num];
1049         
1050         p->yscroll -= count;
1051         if (p->yscroll < 0) {
1052                 accel_bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1053                                 0, vc->vc_rows, vc->vc_cols);
1054                 p->yscroll += p->vrows - vc->vc_rows;
1055         }
1056         info->var.xoffset = 0;
1057         info->var.yoffset = p->yscroll * vc->vc_font.height;
1058         info->var.vmode &= ~FB_VMODE_YWRAP;
1059         update_var(vc->vc_num, info);
1060         accel_clear_margins(vc, info, 1);
1061         scrollback_max -= count;
1062         if (scrollback_max < 0)
1063                 scrollback_max = 0;
1064         scrollback_current = 0;
1065 }
1066
1067 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1068                                   long delta)
1069 {
1070         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1071         int count = vc->vc_rows;
1072         unsigned short *d, *s;
1073         unsigned long n;
1074         int line = 0;
1075
1076         d = (u16 *) softback_curr;
1077         if (d == (u16 *) softback_in)
1078                 d = (u16 *) vc->vc_origin;
1079         n = softback_curr + delta * vc->vc_size_row;
1080         softback_lines -= delta;
1081         if (delta < 0) {
1082                 if (softback_curr < softback_top && n < softback_buf) {
1083                         n += softback_end - softback_buf;
1084                         if (n < softback_top) {
1085                                 softback_lines -=
1086                                     (softback_top - n) / vc->vc_size_row;
1087                                 n = softback_top;
1088                         }
1089                 } else if (softback_curr >= softback_top
1090                            && n < softback_top) {
1091                         softback_lines -=
1092                             (softback_top - n) / vc->vc_size_row;
1093                         n = softback_top;
1094                 }
1095         } else {
1096                 if (softback_curr > softback_in && n >= softback_end) {
1097                         n += softback_buf - softback_end;
1098                         if (n > softback_in) {
1099                                 n = softback_in;
1100                                 softback_lines = 0;
1101                         }
1102                 } else if (softback_curr <= softback_in && n > softback_in) {
1103                         n = softback_in;
1104                         softback_lines = 0;
1105                 }
1106         }
1107         if (n == softback_curr)
1108                 return;
1109         softback_curr = n;
1110         s = (u16 *) softback_curr;
1111         if (s == (u16 *) softback_in)
1112                 s = (u16 *) vc->vc_origin;
1113         while (count--) {
1114                 unsigned short *start;
1115                 unsigned short *le;
1116                 unsigned short c;
1117                 int x = 0;
1118                 unsigned short attr = 1;
1119
1120                 start = s;
1121                 le = advance_row(s, 1);
1122                 do {
1123                         c = scr_readw(s);
1124                         if (attr != (c & 0xff00)) {
1125                                 attr = c & 0xff00;
1126                                 if (s > start) {
1127                                         accel_putcs(vc, info, start, s - start,
1128                                                     real_y(p, line), x);
1129                                         x += s - start;
1130                                         start = s;
1131                                 }
1132                         }
1133                         if (c == scr_readw(d)) {
1134                                 if (s > start) {
1135                                         accel_putcs(vc, info, start, s - start,
1136                                                     real_y(p, line), x);
1137                                         x += s - start + 1;
1138                                         start = s + 1;
1139                                 } else {
1140                                         x++;
1141                                         start++;
1142                                 }
1143                         }
1144                         s++;
1145                         d++;
1146                 } while (s < le);
1147                 if (s > start)
1148                         accel_putcs(vc, info, start, s - start,
1149                                     real_y(p, line), x);
1150                 line++;
1151                 if (d == (u16 *) softback_end)
1152                         d = (u16 *) softback_buf;
1153                 if (d == (u16 *) softback_in)
1154                         d = (u16 *) vc->vc_origin;
1155                 if (s == (u16 *) softback_end)
1156                         s = (u16 *) softback_buf;
1157                 if (s == (u16 *) softback_in)
1158                         s = (u16 *) vc->vc_origin;
1159         }
1160 }
1161
1162 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1163                          int line, int count, int offset)
1164 {
1165         unsigned short *d = (unsigned short *)
1166             (vc->vc_origin + vc->vc_size_row * line);
1167         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1168         unsigned short *s = d + offset;
1169
1170         while (count--) {
1171                 unsigned short *start = s;
1172                 unsigned short *le = advance_row(s, 1);
1173                 unsigned short c;
1174                 int x = 0;
1175                 unsigned short attr = 1;
1176
1177                 do {
1178                         c = scr_readw(s);
1179                         if (attr != (c & 0xff00)) {
1180                                 attr = c & 0xff00;
1181                                 if (s > start) {
1182                                         accel_putcs(vc, info, start, s - start,
1183                                                     real_y(p, line), x);
1184                                         x += s - start;
1185                                         start = s;
1186                                 }
1187                         }
1188                         if (c == scr_readw(d)) {
1189                                 if (s > start) {
1190                                         accel_putcs(vc, info, start, s - start,
1191                                                     real_y(p, line), x);
1192                                         x += s - start + 1;
1193                                         start = s + 1;
1194                                 } else {
1195                                         x++;
1196                                         start++;
1197                                 }
1198                         }
1199                         scr_writew(c, d);
1200                         console_conditional_schedule();
1201                         s++;
1202                         d++;
1203                 } while (s < le);
1204                 if (s > start)
1205                         accel_putcs(vc, info, start, s - start,
1206                                     real_y(p, line), x);
1207                 console_conditional_schedule();
1208                 if (offset > 0)
1209                         line++;
1210                 else {
1211                         line--;
1212                         /* NOTE: We subtract two lines from these pointers */
1213                         s -= vc->vc_size_row;
1214                         d -= vc->vc_size_row;
1215                 }
1216         }
1217 }
1218
1219 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1220                                        int count)
1221 {
1222         unsigned short *p;
1223
1224         if (vc->vc_num != fg_console)
1225                 return;
1226         p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1227
1228         while (count) {
1229                 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1230                 count--;
1231                 p = advance_row(p, 1);
1232                 softback_in += vc->vc_size_row;
1233                 if (softback_in == softback_end)
1234                         softback_in = softback_buf;
1235                 if (softback_in == softback_top) {
1236                         softback_top += vc->vc_size_row;
1237                         if (softback_top == softback_end)
1238                                 softback_top = softback_buf;
1239                 }
1240         }
1241         softback_curr = softback_in;
1242 }
1243
1244 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1245                         int count)
1246 {
1247         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1248         struct display *p = &fb_display[vc->vc_num];
1249         int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1250
1251         if (!info->fbops->fb_blank && console_blanked)
1252                 return 0;
1253
1254         if (!count || vt_cons[vc->vc_num]->vc_mode != KD_TEXT)
1255                 return 0;
1256
1257         fbcon_cursor(vc, CM_ERASE);
1258
1259         /*
1260          * ++Geert: Only use ywrap/ypan if the console is in text mode
1261          * ++Andrew: Only use ypan on hardware text mode when scrolling the
1262          *           whole screen (prevents flicker).
1263          */
1264
1265         switch (dir) {
1266         case SM_UP:
1267                 if (count > vc->vc_rows)        /* Maximum realistic size */
1268                         count = vc->vc_rows;
1269                 if (softback_top)
1270                         fbcon_softback_note(vc, t, count);
1271                 if (logo_shown >= 0)
1272                         goto redraw_up;
1273                 switch (p->scrollmode) {
1274                 case SCROLL_ACCEL:
1275                         accel_bmove(vc, info, t + count, 0, t, 0,
1276                                          b - t - count, vc->vc_cols);
1277                         accel_clear(vc, info, b - count, 0, count,
1278                                          vc->vc_cols);
1279                         break;
1280
1281                 case SCROLL_WRAP:
1282                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1283                                 if (t > 0)
1284                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1285                                                     vc->vc_cols);
1286                                 ywrap_up(vc, count);
1287                                 if (vc->vc_rows - b > 0)
1288                                         fbcon_bmove(vc, b - count, 0, b, 0,
1289                                                     vc->vc_rows - b,
1290                                                     vc->vc_cols);
1291                         } else if (info->flags & FBINFO_READS_FAST)
1292                                 fbcon_bmove(vc, t + count, 0, t, 0,
1293                                             b - t - count, vc->vc_cols);
1294                         else
1295                                 goto redraw_up;
1296                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1297                         break;
1298
1299                 case SCROLL_PAN:
1300                         if ((p->yscroll + count <=
1301                              2 * (p->vrows - vc->vc_rows))
1302                             && ((!scroll_partial && (b - t == vc->vc_rows))
1303                                 || (scroll_partial
1304                                     && (b - t - count >
1305                                         3 * vc->vc_rows >> 2)))) {
1306                                 if (t > 0)
1307                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1308                                                     vc->vc_cols);
1309                                 ypan_up(vc, count);
1310                                 if (vc->vc_rows - b > 0)
1311                                         fbcon_bmove(vc, b - count, 0, b, 0,
1312                                                     vc->vc_rows - b,
1313                                                     vc->vc_cols);
1314                         } else if (info->flags & FBINFO_READS_FAST)
1315                                 fbcon_bmove(vc, t + count, 0, t, 0,
1316                                             b - t - count, vc->vc_cols);
1317                         else
1318                                 goto redraw_up;
1319                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1320                         break;
1321
1322                 case SCROLL_REDRAW:
1323                       redraw_up:
1324                         fbcon_redraw(vc, p, t, b - t - count,
1325                                      count * vc->vc_cols);
1326                         accel_clear(vc, info, real_y(p, b - count), 0,
1327                                          count, vc->vc_cols);
1328                         scr_memsetw((unsigned short *) (vc->vc_origin +
1329                                                         vc->vc_size_row *
1330                                                         (b - count)),
1331                                     vc->vc_video_erase_char,
1332                                     vc->vc_size_row * count);
1333                         return 1;
1334                 }
1335                 break;
1336
1337         case SM_DOWN:
1338                 if (count > vc->vc_rows)        /* Maximum realistic size */
1339                         count = vc->vc_rows;
1340                 switch (p->scrollmode) {
1341                 case SCROLL_ACCEL:
1342                         accel_bmove(vc, info, t, 0, t + count, 0,
1343                                          b - t - count, vc->vc_cols);
1344                         accel_clear(vc, info, t, 0, count, vc->vc_cols);
1345                         break;
1346
1347                 case SCROLL_WRAP:
1348                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1349                                 if (vc->vc_rows - b > 0)
1350                                         fbcon_bmove(vc, b, 0, b - count, 0,
1351                                                     vc->vc_rows - b,
1352                                                     vc->vc_cols);
1353                                 ywrap_down(vc, count);
1354                                 if (t > 0)
1355                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1356                                                     vc->vc_cols);
1357                         } else if (info->flags & FBINFO_READS_FAST)
1358                                 fbcon_bmove(vc, t, 0, t + count, 0,
1359                                             b - t - count, vc->vc_cols);
1360                         else
1361                                 goto redraw_down;
1362                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1363                         break;
1364
1365                 case SCROLL_PAN:
1366                         if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1367                             && ((!scroll_partial && (b - t == vc->vc_rows))
1368                                 || (scroll_partial
1369                                     && (b - t - count >
1370                                         3 * vc->vc_rows >> 2)))) {
1371                                 if (vc->vc_rows - b > 0)
1372                                         fbcon_bmove(vc, b, 0, b - count, 0,
1373                                                     vc->vc_rows - b,
1374                                                     vc->vc_cols);
1375                                 ypan_down(vc, count);
1376                                 if (t > 0)
1377                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1378                                                     vc->vc_cols);
1379                         } else if (info->flags & FBINFO_READS_FAST)
1380                                 fbcon_bmove(vc, t, 0, t + count, 0,
1381                                             b - t - count, vc->vc_cols);
1382                         else
1383                                 goto redraw_down;
1384                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1385                         break;
1386
1387                 case SCROLL_REDRAW:
1388                       redraw_down:
1389                         fbcon_redraw(vc, p, b - 1, b - t - count,
1390                                      -count * vc->vc_cols);
1391                         accel_clear(vc, info, real_y(p, t), 0, count,
1392                                          vc->vc_cols);
1393                         scr_memsetw((unsigned short *) (vc->vc_origin +
1394                                                         vc->vc_size_row *
1395                                                         t),
1396                                     vc->vc_video_erase_char,
1397                                     vc->vc_size_row * count);
1398                         return 1;
1399                 }
1400         }
1401         return 0;
1402 }
1403
1404
1405 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
1406                         int height, int width)
1407 {
1408         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1409         struct display *p = &fb_display[vc->vc_num];
1410         
1411         if (!info->fbops->fb_blank && console_blanked)
1412                 return;
1413
1414         if (!width || !height)
1415                 return;
1416
1417         /*  Split blits that cross physical y_wrap case.
1418          *  Pathological case involves 4 blits, better to use recursive
1419          *  code rather than unrolled case
1420          *
1421          *  Recursive invocations don't need to erase the cursor over and
1422          *  over again, so we use fbcon_bmove_rec()
1423          */
1424         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
1425                         p->vrows - p->yscroll);
1426 }
1427
1428 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx, 
1429                             int dy, int dx, int height, int width, u_int y_break)
1430 {
1431         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1432         u_int b;
1433
1434         if (sy < y_break && sy + height > y_break) {
1435                 b = y_break - sy;
1436                 if (dy < sy) {  /* Avoid trashing self */
1437                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1438                                         y_break);
1439                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1440                                         height - b, width, y_break);
1441                 } else {
1442                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1443                                         height - b, width, y_break);
1444                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1445                                         y_break);
1446                 }
1447                 return;
1448         }
1449
1450         if (dy < y_break && dy + height > y_break) {
1451                 b = y_break - dy;
1452                 if (dy < sy) {  /* Avoid trashing self */
1453                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1454                                         y_break);
1455                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1456                                         height - b, width, y_break);
1457                 } else {
1458                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1459                                         height - b, width, y_break);
1460                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1461                                         y_break);
1462                 }
1463                 return;
1464         }
1465         accel_bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
1466                         height, width);
1467 }
1468
1469 static __inline__ void updatescrollmode(struct display *p, struct fb_info *info, struct vc_data *vc)
1470 {
1471         int cap = info->flags;
1472         int good_pan = (cap & FBINFO_HWACCEL_YPAN)
1473                  && divides(info->fix.ypanstep, vc->vc_font.height)
1474                  && info->var.yres_virtual >= 2*info->var.yres;
1475         int good_wrap = (cap & FBINFO_HWACCEL_YWRAP)
1476                  && divides(info->fix.ywrapstep, vc->vc_font.height)
1477                  && divides(vc->vc_font.height, info->var.yres_virtual);
1478         int reading_fast = cap & FBINFO_READS_FAST;
1479         int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) && !(cap & FBINFO_HWACCEL_DISABLED);
1480
1481         if (good_wrap || good_pan) {
1482                 if (reading_fast || fast_copyarea)
1483                         p->scrollmode = good_wrap ? SCROLL_WRAP : SCROLL_PAN;
1484                 else
1485                         p->scrollmode = SCROLL_REDRAW;
1486         } else {
1487                 if (reading_fast || fast_copyarea)
1488                         p->scrollmode = SCROLL_ACCEL;
1489                 else
1490                         p->scrollmode = SCROLL_REDRAW;
1491         }
1492 }
1493
1494 static int fbcon_resize(struct vc_data *vc, unsigned int width, 
1495                         unsigned int height)
1496 {
1497         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1498         struct display *p = &fb_display[vc->vc_num];
1499         struct fb_var_screeninfo var = info->var;
1500         int err; int x_diff, y_diff;
1501         int fw = vc->vc_font.width;
1502         int fh = vc->vc_font.height;
1503
1504         var.xres = width * fw;
1505         var.yres = height * fh;
1506         x_diff = info->var.xres - var.xres;
1507         y_diff = info->var.yres - var.yres;
1508         if (x_diff < 0 || x_diff > fw || (y_diff < 0 || y_diff > fh)) {
1509                 char mode[40];
1510
1511                 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
1512                 if (!info->fbops->fb_set_par)
1513                         return -EINVAL;
1514
1515                 snprintf(mode, 40, "%ix%i", var.xres, var.yres);
1516                 err = fb_find_mode(&var, info, mode, info->monspecs.modedb,
1517                                    info->monspecs.modedb_len, NULL,
1518                                    info->var.bits_per_pixel);
1519                 if (!err || width > var.xres/fw || height > var.yres/fh)
1520                         return -EINVAL;
1521                 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
1522                 if (CON_IS_VISIBLE(vc)) {
1523                         var.activate = FB_ACTIVATE_NOW;
1524                         fb_set_var(info, &var);
1525                 }
1526         }
1527         p->vrows = var.yres_virtual/fh;
1528         if (var.yres > (fh * (height + 1)))
1529                 p->vrows -= (var.yres - (fh * height)) / fh;
1530         if ((var.yres % fh) && (var.yres_virtual % fh < var.yres % fh))
1531                 p->vrows--;
1532         updatescrollmode(p, info, vc);
1533         return 0;
1534 }
1535
1536 static int fbcon_switch(struct vc_data *vc)
1537 {
1538         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1539         struct display *p = &fb_display[vc->vc_num];
1540
1541         if (softback_top) {
1542                 int l = fbcon_softback_size / vc->vc_size_row;
1543                 if (softback_lines)
1544                         fbcon_set_origin(vc);
1545                 softback_top = softback_curr = softback_in = softback_buf;
1546                 softback_lines = 0;
1547
1548                 if (l > 5)
1549                         softback_end = softback_buf + l * vc->vc_size_row;
1550                 else {
1551                         /* Smaller scrollback makes no sense, and 0 would screw
1552                            the operation totally */
1553                         softback_top = 0;
1554                 }
1555         }
1556         if (logo_shown >= 0) {
1557                 struct vc_data *conp2 = vc_cons[logo_shown].d;
1558
1559                 if (conp2->vc_top == logo_lines
1560                     && conp2->vc_bottom == conp2->vc_rows)
1561                         conp2->vc_top = 0;
1562                 logo_shown = -1;
1563         }
1564         if (info)
1565                 info->var.yoffset = p->yscroll = 0;
1566         fbcon_resize(vc, vc->vc_cols, vc->vc_rows);
1567         switch (p->scrollmode) {
1568         case SCROLL_WRAP:
1569                 scrollback_phys_max = p->vrows - vc->vc_rows;
1570                 break;
1571         case SCROLL_PAN:
1572                 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
1573                 if (scrollback_phys_max < 0)
1574                         scrollback_phys_max = 0;
1575                 break;
1576         default:
1577                 scrollback_phys_max = 0;
1578                 break;
1579         }
1580         scrollback_max = 0;
1581         scrollback_current = 0;
1582
1583         info->currcon = vc->vc_num;
1584         
1585         update_var(vc->vc_num, info);
1586         fbcon_set_palette(vc, color_table);     
1587
1588         if (vt_cons[vc->vc_num]->vc_mode == KD_TEXT)
1589                 accel_clear_margins(vc, info, 0);
1590         if (logo_shown == -2) {
1591                 logo_shown = fg_console;
1592                 /* This is protected above by initmem_freed */
1593                 fb_show_logo(info);
1594                 update_region(fg_console,
1595                               vc->vc_origin + vc->vc_size_row * vc->vc_top,
1596                               vc->vc_size_row * (vc->vc_bottom -
1597                                                  vc->vc_top) / 2);
1598                 return 0;
1599         }
1600         return 1;
1601 }
1602
1603 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
1604 {
1605         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
1606         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1607         struct display *p = &fb_display[vc->vc_num];
1608
1609         if (mode_switch) {
1610                 struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1611                 struct fb_var_screeninfo var = info->var;
1612
1613                 if (blank) {
1614                         fbcon_cursor(vc, CM_ERASE);
1615                         return 0;
1616                 }
1617                 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
1618                 fb_set_var(info, &var);
1619         }
1620
1621         fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
1622
1623         if (!info->fbops->fb_blank) {
1624                 if (blank) {
1625                         unsigned short oldc;
1626                         u_int height;
1627                         u_int y_break;
1628
1629                         oldc = vc->vc_video_erase_char;
1630                         vc->vc_video_erase_char &= charmask;
1631                         height = vc->vc_rows;
1632                         y_break = p->vrows - p->yscroll;
1633                         if (height > y_break) {
1634                                 accel_clear(vc, info, real_y(p, 0),
1635                                             0, y_break, vc->vc_cols);
1636                                 accel_clear(vc, info, real_y(p, y_break),
1637                                             0, height - y_break, 
1638                                             vc->vc_cols);
1639                         } else
1640                                 accel_clear(vc, info, real_y(p, 0),
1641                                             0, height, vc->vc_cols);
1642                         vc->vc_video_erase_char = oldc;
1643                 } else
1644                         update_screen(vc->vc_num);
1645                 return 0;
1646         } else
1647                 return fb_blank(info, blank);
1648 }
1649
1650 static void fbcon_free_font(struct display *p)
1651 {
1652         if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1653                 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1654         p->fontdata = NULL;
1655         p->userfont = 0;
1656 }
1657
1658 static inline int fbcon_get_font(struct vc_data *vc, struct console_font_op *op)
1659 {
1660         u8 *fontdata = vc->vc_font.data;
1661         u8 *data = op->data;
1662         int i, j;
1663
1664         op->width = vc->vc_font.width;
1665         op->height = vc->vc_font.height;
1666         op->charcount = vc->vc_hi_font_mask ? 512 : 256;
1667         if (!op->data)
1668                 return 0;
1669
1670         if (op->width <= 8) {
1671                 j = vc->vc_font.height;
1672                 for (i = 0; i < op->charcount; i++) {
1673                         memcpy(data, fontdata, j);
1674                         memset(data + j, 0, 32 - j);
1675                         data += 32;
1676                         fontdata += j;
1677                 }
1678         } else if (op->width <= 16) {
1679                 j = vc->vc_font.height * 2;
1680                 for (i = 0; i < op->charcount; i++) {
1681                         memcpy(data, fontdata, j);
1682                         memset(data + j, 0, 64 - j);
1683                         data += 64;
1684                         fontdata += j;
1685                 }
1686         } else if (op->width <= 24) {
1687                 for (i = 0; i < op->charcount; i++) {
1688                         for (j = 0; j < vc->vc_font.height; j++) {
1689                                 *data++ = fontdata[0];
1690                                 *data++ = fontdata[1];
1691                                 *data++ = fontdata[2];
1692                                 fontdata += sizeof(u32);
1693                         }
1694                         memset(data, 0, 3 * (32 - j));
1695                         data += 3 * (32 - j);
1696                 }
1697         } else {
1698                 j = vc->vc_font.height * 4;
1699                 for (i = 0; i < op->charcount; i++) {
1700                         memcpy(data, fontdata, j);
1701                         memset(data + j, 0, 128 - j);
1702                         data += 128;
1703                         fontdata += j;
1704                 }
1705         }
1706         return 0;
1707 }
1708
1709 static int fbcon_do_set_font(struct vc_data *vc, struct console_font_op *op,
1710                              u8 * data, int userfont)
1711 {
1712         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1713         struct display *p = &fb_display[vc->vc_num];
1714         int resize;
1715         int w = op->width;
1716         int h = op->height;
1717         int cnt;
1718         char *old_data = NULL;
1719
1720         if (!w > 32) {
1721                 if (userfont && op->op != KD_FONT_OP_COPY)
1722                         kfree(data - FONT_EXTRA_WORDS * sizeof(int));
1723                 return -ENXIO;
1724         }
1725
1726         if (CON_IS_VISIBLE(vc) && softback_lines)
1727                 fbcon_set_origin(vc);
1728
1729         resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
1730         if (p->userfont)
1731                 old_data = vc->vc_font.data;
1732         if (userfont)
1733                 cnt = FNTCHARCNT(data);
1734         else
1735                 cnt = 256;
1736         vc->vc_font.data = p->fontdata = data;
1737         if ((p->userfont = userfont))
1738                 REFCOUNT(data)++;
1739         vc->vc_font.width = w;
1740         vc->vc_font.height = h;
1741         if (vc->vc_hi_font_mask && cnt == 256) {
1742                 vc->vc_hi_font_mask = 0;
1743                 if (vc->vc_can_do_color) {
1744                         vc->vc_complement_mask >>= 1;
1745                         vc->vc_s_complement_mask >>= 1;
1746                 }
1747                         
1748                 /* ++Edmund: reorder the attribute bits */
1749                 if (vc->vc_can_do_color) {
1750                         unsigned short *cp =
1751                             (unsigned short *) vc->vc_origin;
1752                         int count = vc->vc_screenbuf_size / 2;
1753                         unsigned short c;
1754                         for (; count > 0; count--, cp++) {
1755                                 c = scr_readw(cp);
1756                                 scr_writew(((c & 0xfe00) >> 1) |
1757                                            (c & 0xff), cp);
1758                         }
1759                         c = vc->vc_video_erase_char;
1760                         vc->vc_video_erase_char =
1761                             ((c & 0xfe00) >> 1) | (c & 0xff);
1762                         vc->vc_attr >>= 1;
1763                 }
1764         } else if (!vc->vc_hi_font_mask && cnt == 512) {
1765                 vc->vc_hi_font_mask = 0x100;
1766                 if (vc->vc_can_do_color) {
1767                         vc->vc_complement_mask <<= 1;
1768                         vc->vc_s_complement_mask <<= 1;
1769                 }
1770                         
1771                 /* ++Edmund: reorder the attribute bits */
1772                 {
1773                         unsigned short *cp =
1774                             (unsigned short *) vc->vc_origin;
1775                         int count = vc->vc_screenbuf_size / 2;
1776                         unsigned short c;
1777                         for (; count > 0; count--, cp++) {
1778                                 unsigned short newc;
1779                                 c = scr_readw(cp);
1780                                 if (vc->vc_can_do_color)
1781                                         newc =
1782                                             ((c & 0xff00) << 1) | (c &
1783                                                                    0xff);
1784                                 else
1785                                         newc = c & ~0x100;
1786                                 scr_writew(newc, cp);
1787                         }
1788                         c = vc->vc_video_erase_char;
1789                         if (vc->vc_can_do_color) {
1790                                 vc->vc_video_erase_char =
1791                                     ((c & 0xff00) << 1) | (c & 0xff);
1792                                 vc->vc_attr <<= 1;
1793                         } else
1794                                 vc->vc_video_erase_char = c & ~0x100;
1795                 }
1796
1797         }
1798
1799         if (resize) {
1800                 /* reset wrap/pan */
1801                 info->var.xoffset = info->var.yoffset = p->yscroll = 0;
1802                 vc_resize(vc->vc_num, info->var.xres / w, info->var.yres / h);
1803                 if (CON_IS_VISIBLE(vc) && softback_buf) {
1804                         int l = fbcon_softback_size / vc->vc_size_row;
1805                         if (l > 5)
1806                                 softback_end =
1807                                     softback_buf + l * vc->vc_size_row;
1808                         else {
1809                                 /* Smaller scrollback makes no sense, and 0 would screw
1810                                    the operation totally */
1811                                 softback_top = 0;
1812                         }
1813                 }
1814         } else if (CON_IS_VISIBLE(vc)
1815                    && vt_cons[vc->vc_num]->vc_mode == KD_TEXT) {
1816                 accel_clear_margins(vc, info, 0);
1817                 update_screen(vc->vc_num);
1818         }
1819
1820         if (old_data && (--REFCOUNT(old_data) == 0))
1821                 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
1822         return 0;
1823 }
1824
1825 static inline int fbcon_copy_font(struct vc_data *vc, struct console_font_op *op)
1826 {
1827         struct display *od;
1828         int h = op->height;
1829
1830         if (h < 0 || !vc_cons_allocated(h))
1831                 return -ENOTTY;
1832         if (h == vc->vc_num)
1833                 return 0;       /* nothing to do */
1834         od = &fb_display[h];
1835         if (od->fontdata == vc->vc_font.data)
1836                 return 0;       /* already the same font... */
1837         op->width = vc->vc_font.width;
1838         op->height = vc->vc_font.height;
1839         return fbcon_do_set_font(vc, op, od->fontdata, od->userfont);
1840 }
1841
1842 static inline int fbcon_set_font(struct vc_data *vc, struct console_font_op *op)
1843 {
1844         int w = op->width;
1845         int h = op->height;
1846         int size = h;
1847         int i, k;
1848         u8 *new_data, *data = op->data, *p;
1849
1850         if ((w <= 0) || (w > 32)
1851             || (op->charcount != 256 && op->charcount != 512))
1852                 return -EINVAL;
1853
1854         if (w > 8) {
1855                 if (w <= 16)
1856                         size *= 2;
1857                 else
1858                         size *= 4;
1859         }
1860         size *= op->charcount;
1861
1862         if (!
1863             (new_data =
1864              kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER)))
1865                 return -ENOMEM;
1866         new_data += FONT_EXTRA_WORDS * sizeof(int);
1867         FNTSIZE(new_data) = size;
1868         FNTCHARCNT(new_data) = op->charcount;
1869         REFCOUNT(new_data) = 0; /* usage counter */
1870         p = new_data;
1871         if (w <= 8) {
1872                 for (i = 0; i < op->charcount; i++) {
1873                         memcpy(p, data, h);
1874                         data += 32;
1875                         p += h;
1876                 }
1877         } else if (w <= 16) {
1878                 h *= 2;
1879                 for (i = 0; i < op->charcount; i++) {
1880                         memcpy(p, data, h);
1881                         data += 64;
1882                         p += h;
1883                 }
1884         } else if (w <= 24) {
1885                 for (i = 0; i < op->charcount; i++) {
1886                         int j;
1887                         for (j = 0; j < h; j++) {
1888                                 memcpy(p, data, 3);
1889                                 p[3] = 0;
1890                                 data += 3;
1891                                 p += sizeof(u32);
1892                         }
1893                         data += 3 * (32 - h);
1894                 }
1895         } else {
1896                 h *= 4;
1897                 for (i = 0; i < op->charcount; i++) {
1898                         memcpy(p, data, h);
1899                         data += 128;
1900                         p += h;
1901                 }
1902         }
1903         /* we can do it in u32 chunks because of charcount is 256 or 512, so
1904            font length must be multiple of 256, at least. And 256 is multiple
1905            of 4 */
1906         k = 0;
1907         while (p > new_data) {
1908                 p = (u8 *)((u32 *)p - 1);
1909                 k += *(u32 *) p;
1910         }
1911         FNTSUM(new_data) = k;
1912         /* Check if the same font is on some other console already */
1913         for (i = 0; i < MAX_NR_CONSOLES; i++) {
1914                 struct vc_data *tmp = vc_cons[i].d;
1915                 
1916                 if (fb_display[i].userfont &&
1917                     fb_display[i].fontdata &&
1918                     FNTSUM(fb_display[i].fontdata) == k &&
1919                     FNTSIZE(fb_display[i].fontdata) == size &&
1920                     tmp->vc_font.width == w &&
1921                     !memcmp(fb_display[i].fontdata, new_data, size)) {
1922                         kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
1923                         new_data = fb_display[i].fontdata;
1924                         break;
1925                 }
1926         }
1927         return fbcon_do_set_font(vc, op, new_data, 1);
1928 }
1929
1930 static inline int fbcon_set_def_font(struct vc_data *vc, struct console_font_op *op)
1931 {
1932         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1933         char name[MAX_FONT_NAME];
1934         struct font_desc *f;
1935
1936         if (!op->data)
1937                 f = get_default_font(info->var.xres, info->var.yres);
1938         else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
1939                 return -EFAULT;
1940         else {
1941                 name[MAX_FONT_NAME - 1] = 0;
1942                 if (!(f = find_font(name)))
1943                         return -ENOENT;
1944         }
1945         op->width = f->width;
1946         op->height = f->height;
1947         return fbcon_do_set_font(vc, op, f->data, 0);
1948 }
1949
1950 static int fbcon_font_op(struct vc_data *vc, struct console_font_op *op)
1951 {
1952         switch (op->op) {
1953         case KD_FONT_OP_SET:
1954                 return fbcon_set_font(vc, op);
1955         case KD_FONT_OP_GET:
1956                 return fbcon_get_font(vc, op);
1957         case KD_FONT_OP_SET_DEFAULT:
1958                 return fbcon_set_def_font(vc, op);
1959         case KD_FONT_OP_COPY:
1960                 return fbcon_copy_font(vc, op);
1961         default:
1962                 return -ENOSYS;
1963         }
1964 }
1965
1966 static u16 palette_red[16];
1967 static u16 palette_green[16];
1968 static u16 palette_blue[16];
1969
1970 static struct fb_cmap palette_cmap = {
1971         0, 16, palette_red, palette_green, palette_blue, NULL
1972 };
1973
1974 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
1975 {
1976         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1977         int i, j, k;
1978         u8 val;
1979
1980         if (!vc->vc_can_do_color
1981             || (!info->fbops->fb_blank && console_blanked))
1982                 return -EINVAL;
1983         for (i = j = 0; i < 16; i++) {
1984                 k = table[i];
1985                 val = vc->vc_palette[j++];
1986                 palette_red[k] = (val << 8) | val;
1987                 val = vc->vc_palette[j++];
1988                 palette_green[k] = (val << 8) | val;
1989                 val = vc->vc_palette[j++];
1990                 palette_blue[k] = (val << 8) | val;
1991         }
1992         if (info->var.bits_per_pixel <= 4)
1993                 palette_cmap.len = 1 << info->var.bits_per_pixel;
1994         else
1995                 palette_cmap.len = 16;
1996         palette_cmap.start = 0;
1997         return fb_set_cmap(&palette_cmap, 1, info);
1998 }
1999
2000 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2001 {
2002         unsigned long p;
2003         int line;
2004         
2005         if (vc->vc_num != fg_console || !softback_lines)
2006                 return (u16 *) (vc->vc_origin + offset);
2007         line = offset / vc->vc_size_row;
2008         if (line >= softback_lines)
2009                 return (u16 *) (vc->vc_origin + offset -
2010                                 softback_lines * vc->vc_size_row);
2011         p = softback_curr + offset;
2012         if (p >= softback_end)
2013                 p += softback_buf - softback_end;
2014         return (u16 *) p;
2015 }
2016
2017 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2018                                  int *px, int *py)
2019 {
2020         unsigned long ret;
2021         int x, y;
2022
2023         if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2024                 unsigned long offset = (pos - vc->vc_origin) / 2;
2025
2026                 x = offset % vc->vc_cols;
2027                 y = offset / vc->vc_cols;
2028                 if (vc->vc_num == fg_console)
2029                         y += softback_lines;
2030                 ret = pos + (vc->vc_cols - x) * 2;
2031         } else if (vc->vc_num == fg_console && softback_lines) {
2032                 unsigned long offset = pos - softback_curr;
2033
2034                 if (pos < softback_curr)
2035                         offset += softback_end - softback_buf;
2036                 offset /= 2;
2037                 x = offset % vc->vc_cols;
2038                 y = offset / vc->vc_cols;
2039                 ret = pos + (vc->vc_cols - x) * 2;
2040                 if (ret == softback_end)
2041                         ret = softback_buf;
2042                 if (ret == softback_in)
2043                         ret = vc->vc_origin;
2044         } else {
2045                 /* Should not happen */
2046                 x = y = 0;
2047                 ret = vc->vc_origin;
2048         }
2049         if (px)
2050                 *px = x;
2051         if (py)
2052                 *py = y;
2053         return ret;
2054 }
2055
2056 /* As we might be inside of softback, we may work with non-contiguous buffer,
2057    that's why we have to use a separate routine. */
2058 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2059 {
2060         while (cnt--) {
2061                 u16 a = scr_readw(p);
2062                 if (!vc->vc_can_do_color)
2063                         a ^= 0x0800;
2064                 else if (vc->vc_hi_font_mask == 0x100)
2065                         a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2066                             (((a) & 0x0e00) << 4);
2067                 else
2068                         a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2069                             (((a) & 0x0700) << 4);
2070                 scr_writew(a, p++);
2071                 if (p == (u16 *) softback_end)
2072                         p = (u16 *) softback_buf;
2073                 if (p == (u16 *) softback_in)
2074                         p = (u16 *) vc->vc_origin;
2075         }
2076 }
2077
2078 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2079 {
2080         struct fb_info *info = registered_fb[(int) con2fb_map[fg_console]];
2081         struct display *p = &fb_display[fg_console];
2082         int offset, limit, scrollback_old;
2083
2084         if (softback_top) {
2085                 if (vc->vc_num != fg_console)
2086                         return 0;
2087                 if (vt_cons[vc->vc_num]->vc_mode != KD_TEXT || !lines)
2088                         return 0;
2089                 if (logo_shown >= 0) {
2090                         struct vc_data *conp2 = vc_cons[logo_shown].d;
2091
2092                         if (conp2->vc_top == logo_lines
2093                             && conp2->vc_bottom == conp2->vc_rows)
2094                                 conp2->vc_top = 0;
2095                         if (logo_shown == vc->vc_num) {
2096                                 unsigned long p, q;
2097                                 int i;
2098
2099                                 p = softback_in;
2100                                 q = vc->vc_origin +
2101                                     logo_lines * vc->vc_size_row;
2102                                 for (i = 0; i < logo_lines; i++) {
2103                                         if (p == softback_top)
2104                                                 break;
2105                                         if (p == softback_buf)
2106                                                 p = softback_end;
2107                                         p -= vc->vc_size_row;
2108                                         q -= vc->vc_size_row;
2109                                         scr_memcpyw((u16 *) q, (u16 *) p,
2110                                                     vc->vc_size_row);
2111                                 }
2112                                 softback_in = p;
2113                                 update_region(vc->vc_num, vc->vc_origin,
2114                                               logo_lines * vc->vc_cols);
2115                         }
2116                         logo_shown = -1;
2117                 }
2118                 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2119                 fbcon_redraw_softback(vc, p, lines);
2120                 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2121                 return 0;
2122         }
2123
2124         if (!scrollback_phys_max)
2125                 return -ENOSYS;
2126
2127         scrollback_old = scrollback_current;
2128         scrollback_current -= lines;
2129         if (scrollback_current < 0)
2130                 scrollback_current = 0;
2131         else if (scrollback_current > scrollback_max)
2132                 scrollback_current = scrollback_max;
2133         if (scrollback_current == scrollback_old)
2134                 return 0;
2135
2136         if (!info->fbops->fb_blank &&
2137             (console_blanked || vt_cons[vc->vc_num]->vc_mode != KD_TEXT
2138              || !lines))
2139                 return 0;
2140         fbcon_cursor(vc, CM_ERASE);
2141
2142         offset = p->yscroll - scrollback_current;
2143         limit = p->vrows;
2144         switch (p->scrollmode) {
2145         case SCROLL_WRAP:
2146                 info->var.vmode |= FB_VMODE_YWRAP;
2147                 break;
2148         case SCROLL_PAN:
2149                 limit -= vc->vc_rows;
2150                 info->var.vmode &= ~FB_VMODE_YWRAP;
2151                 break;
2152         }
2153         if (offset < 0)
2154                 offset += limit;
2155         else if (offset >= limit)
2156                 offset -= limit;
2157         info->var.xoffset = 0;
2158         info->var.yoffset = offset * vc->vc_font.height;
2159         update_var(vc->vc_num, info);
2160         if (!scrollback_current)
2161                 fbcon_cursor(vc, CM_DRAW);
2162         return 0;
2163 }
2164
2165 static int fbcon_set_origin(struct vc_data *vc)
2166 {
2167         if (softback_lines && !console_blanked)
2168                 fbcon_scrolldelta(vc, softback_lines);
2169         return 0;
2170 }
2171
2172 static void fbcon_suspended(struct fb_info *info)
2173 {
2174         /* Clear cursor, restore saved data */
2175         info->cursor.enable = 0;
2176         info->fbops->fb_cursor(info, &info->cursor);
2177 }
2178
2179 static void fbcon_resumed(struct fb_info *info)
2180 {
2181         struct vc_data *vc;
2182
2183         if (info->currcon < 0)
2184                 return;
2185         vc = vc_cons[info->currcon].d;
2186
2187         update_screen(vc->vc_num);
2188 }
2189 static int fbcon_event_notify(struct notifier_block *self, 
2190                               unsigned long action, void *data)
2191 {
2192         struct fb_info *info = (struct fb_info *) data;
2193
2194         switch(action) {
2195         case FB_EVENT_SUSPEND:
2196                 fbcon_suspended(info);
2197                 break;
2198         case FB_EVENT_RESUME:
2199                 fbcon_resumed(info);
2200                 break;
2201         }
2202         return 0;
2203 }
2204
2205 /*
2206  *  The console `switch' structure for the frame buffer based console
2207  */
2208
2209 const struct consw fb_con = {
2210         .owner                  = THIS_MODULE,
2211         .con_startup            = fbcon_startup,
2212         .con_init               = fbcon_init,
2213         .con_deinit             = fbcon_deinit,
2214         .con_clear              = fbcon_clear,
2215         .con_putc               = fbcon_putc,
2216         .con_putcs              = fbcon_putcs,
2217         .con_cursor             = fbcon_cursor,
2218         .con_scroll             = fbcon_scroll,
2219         .con_bmove              = fbcon_bmove,
2220         .con_switch             = fbcon_switch,
2221         .con_blank              = fbcon_blank,
2222         .con_font_op            = fbcon_font_op,
2223         .con_set_palette        = fbcon_set_palette,
2224         .con_scrolldelta        = fbcon_scrolldelta,
2225         .con_set_origin         = fbcon_set_origin,
2226         .con_invert_region      = fbcon_invert_region,
2227         .con_screen_pos         = fbcon_screen_pos,
2228         .con_getxy              = fbcon_getxy,
2229         .con_resize             = fbcon_resize,
2230 };
2231
2232 static struct notifier_block fbcon_event_notifer = {
2233         .notifier_call  = fbcon_event_notify,
2234 };
2235
2236 static int fbcon_event_notifier_registered;
2237
2238 int __init fb_console_init(void)
2239 {
2240         int err;
2241
2242         if (!num_registered_fb)
2243                 return -ENODEV;
2244
2245         err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
2246                                 fbcon_is_default);
2247         if (err)
2248                 return err;
2249
2250         acquire_console_sem();
2251         if (!fbcon_event_notifier_registered) {
2252                 fb_register_client(&fbcon_event_notifer);
2253                 fbcon_event_notifier_registered = 1;
2254         } 
2255         release_console_sem();
2256         return 0;
2257 }
2258
2259 #ifdef MODULE
2260
2261 void __exit fb_console_exit(void)
2262 {
2263         acquire_console_sem();
2264         if (fbcon_event_notifier_registered) {
2265                 fb_unregister_client(&fbcon_event_notifer);
2266                 fbcon_event_notifier_registered = 0;
2267         }
2268         release_console_sem();
2269         give_up_console(&fb_con);
2270 }       
2271
2272 module_init(fb_console_init);
2273 module_exit(fb_console_exit);
2274
2275 #endif
2276
2277 /*
2278  *  Visible symbols for modules
2279  */
2280
2281 EXPORT_SYMBOL(fb_display);
2282 EXPORT_SYMBOL(fb_con);
2283
2284 MODULE_LICENSE("GPL");