patch-2_6_7-vs1_9_1_12
[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
605         if (vc->vc_num != display_fg || (info->flags & FBINFO_FLAG_MODULE) ||
606             (info->fix.type == FB_TYPE_TEXT))
607                 logo = 0;
608
609         info->var.xoffset = info->var.yoffset = p->yscroll = 0; /* reset wrap/pan */
610
611         /* If we are not the first console on this
612            fb, copy the font from that console */
613         t = &fb_display[display_fg];
614         vc->vc_font.width = (*default_mode)->vc_font.width;
615         vc->vc_font.height = (*default_mode)->vc_font.height;
616         vc->vc_font.data = p->fontdata = t->fontdata;
617         p->userfont = t->userfont;
618         if (p->userfont) {
619                 REFCOUNT(p->fontdata)++;
620                 charcnt = FNTCHARCNT(p->fontdata);
621         }
622         con_copy_unimap(vc->vc_num, display_fg);
623
624         vc->vc_can_do_color = info->var.bits_per_pixel != 1;
625         vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
626         if (charcnt == 256) {
627                 vc->vc_hi_font_mask = 0;
628         } else {
629                 vc->vc_hi_font_mask = 0x100;
630                 if (vc->vc_can_do_color)
631                         vc->vc_complement_mask <<= 1;
632         }
633
634         cols = info->var.xres / vc->vc_font.width;
635         rows = info->var.yres / vc->vc_font.height;
636         vc_resize(vc->vc_num, cols, rows);
637
638         if (info->var.accel_flags)
639                 p->scrollmode = SCROLL_YNOMOVE;
640         else
641                 p->scrollmode = SCROLL_YREDRAW;
642
643         /*
644          *  ++guenther: console.c:vc_allocate() relies on initializing
645          *  vc_{cols,rows}, but we must not set those if we are only
646          *  resizing the console.
647          */
648         if (init) {
649                 vc->vc_cols = cols;
650                 vc->vc_rows = rows;
651         }
652
653         if (logo) {
654                 /* Need to make room for the logo */
655                 int cnt;
656                 int step;
657
658                 logo_height = fb_prepare_logo(info);
659                 logo_lines = (logo_height + vc->vc_font.height - 1) /
660                              vc->vc_font.height;
661                 q = (unsigned short *) (vc->vc_origin +
662                                         vc->vc_size_row * rows);
663                 step = logo_lines * cols;
664                 for (r = q - logo_lines * cols; r < q; r++)
665                         if (scr_readw(r) != vc->vc_video_erase_char)
666                                 break;
667                 if (r != q && rows >= rows + logo_lines) {
668                         save = kmalloc(logo_lines * cols * 2, GFP_KERNEL);
669                         if (save) {
670                                 scr_memsetw(save, vc->vc_video_erase_char,
671                                             logo_lines * cols * 2);
672                                 r = q - step;
673                                 for (cnt = 0; cnt < logo_lines; cnt++, r += cols)
674                                         scr_memcpyw(save + cnt * cols, r, 2 * cols);
675                                 r = q;
676                         }
677                 }
678                 if (r == q) {
679                         /* We can scroll screen down */
680                         r = q - step - cols;
681                         for (cnt = rows - logo_lines; cnt > 0; cnt--) {
682                                 scr_memcpyw(r + step, r, vc->vc_size_row);
683                                 r -= cols;
684                         }
685                         if (!save) {
686                                 vc->vc_y += logo_lines;
687                                 vc->vc_pos += logo_lines * vc->vc_size_row;
688                         }
689                 }
690                 scr_memsetw((unsigned short *) vc->vc_origin,
691                             vc->vc_video_erase_char,
692                             vc->vc_size_row * logo_lines);
693
694                 if (CON_IS_VISIBLE(vc) && vt_cons[vc->vc_num]->vc_mode == KD_TEXT) {
695                         accel_clear_margins(vc, info, 0);
696                         update_screen(vc->vc_num);
697                 }
698                 if (save) {
699                         q = (unsigned short *) (vc->vc_origin +
700                                                 vc->vc_size_row *
701                                                 rows);
702                         scr_memcpyw(q, save, logo_lines * cols * 2);
703                         vc->vc_y += logo_lines;
704                         vc->vc_pos += logo_lines * vc->vc_size_row;
705                         kfree(save);
706                 }
707                 if (logo_lines > vc->vc_bottom) {
708                         logo_shown = -1;
709                         printk(KERN_INFO
710                                "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
711                 } else {
712                         logo_shown = -2;
713                         vc->vc_top = logo_lines;
714                 }
715         }
716
717         if (vc->vc_num == display_fg && softback_buf) {
718                 int l = fbcon_softback_size / vc->vc_size_row;
719                 if (l > 5)
720                         softback_end = softback_buf + l * vc->vc_size_row;
721                 else {
722                         /* Smaller scrollback makes no sense, and 0 would screw
723                            the operation totally */
724                         softback_top = 0;
725                 }
726         }
727 }
728
729 static void fbcon_deinit(struct vc_data *vc)
730 {
731         struct display *p = &fb_display[vc->vc_num];
732
733         fbcon_free_font(p);
734 }
735
736 /* ====================================================================== */
737
738 /*  fbcon_XXX routines - interface used by the world
739  *
740  *  This system is now divided into two levels because of complications
741  *  caused by hardware scrolling. Top level functions:
742  *
743  *      fbcon_bmove(), fbcon_clear(), fbcon_putc()
744  *
745  *  handles y values in range [0, scr_height-1] that correspond to real
746  *  screen positions. y_wrap shift means that first line of bitmap may be
747  *  anywhere on this display. These functions convert lineoffsets to
748  *  bitmap offsets and deal with the wrap-around case by splitting blits.
749  *
750  *      fbcon_bmove_physical_8()    -- These functions fast implementations
751  *      fbcon_clear_physical_8()    -- of original fbcon_XXX fns.
752  *      fbcon_putc_physical_8()     -- (font width != 8) may be added later
753  *
754  *  WARNING:
755  *
756  *  At the moment fbcon_putc() cannot blit across vertical wrap boundary
757  *  Implies should only really hardware scroll in rows. Only reason for
758  *  restriction is simplicity & efficiency at the moment.
759  */
760
761 static __inline__ int real_y(struct display *p, int ypos)
762 {
763         int rows = p->vrows;
764
765         ypos += p->yscroll;
766         return ypos < rows ? ypos : ypos - rows;
767 }
768
769
770 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
771                         int width)
772 {
773         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
774         
775         struct display *p = &fb_display[vc->vc_num];
776         u_int y_break;
777
778         if (!info->fbops->fb_blank && console_blanked)
779                 return;
780         if (info->state != FBINFO_STATE_RUNNING)
781                 return;
782
783         if (!height || !width)
784                 return;
785
786         /* Split blits that cross physical y_wrap boundary */
787
788         y_break = p->vrows - p->yscroll;
789         if (sy < y_break && sy + height - 1 >= y_break) {
790                 u_int b = y_break - sy;
791                 accel_clear(vc, info, real_y(p, sy), sx, b, width);
792                 accel_clear(vc, info, real_y(p, sy + b), sx, height - b,
793                                  width);
794         } else
795                 accel_clear(vc, info, real_y(p, sy), sx, height, width);
796 }
797
798 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
799 {
800         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
801         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
802         unsigned int scan_align = info->pixmap.scan_align - 1;
803         unsigned int buf_align = info->pixmap.buf_align - 1;
804         unsigned int width = (vc->vc_font.width + 7) >> 3;
805         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
806         int fgshift = (vc->vc_hi_font_mask) ? 9 : 8;
807         struct display *p = &fb_display[vc->vc_num];
808         unsigned int size, pitch;
809         struct fb_image image;
810         u8 *src, *dst;
811
812         if (!info->fbops->fb_blank && console_blanked)
813                 return;
814         if (info->state != FBINFO_STATE_RUNNING)
815                 return;
816
817         if (vt_cons[vc->vc_num]->vc_mode != KD_TEXT)
818                 return;
819
820         image.dx = xpos * vc->vc_font.width;
821         image.dy = real_y(p, ypos) * vc->vc_font.height;
822         image.width = vc->vc_font.width;
823         image.height = vc->vc_font.height;
824         image.fg_color = attr_fgcol(fgshift, c);
825         image.bg_color = attr_bgcol(bgshift, c);
826         image.depth = 1;
827
828         src = vc->vc_font.data + (c & charmask) * vc->vc_font.height * width;
829
830         pitch = width + scan_align;
831         pitch &= ~scan_align;
832         size = pitch * vc->vc_font.height;
833         size += buf_align;
834         size &= ~buf_align;
835
836         dst = fb_get_buffer_offset(info, &info->pixmap, size);
837         image.data = dst;
838
839         fb_move_buf_aligned(info, &info->pixmap, dst, pitch, src, width, image.height);
840
841         info->fbops->fb_imageblit(info, &image);
842 }
843
844 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
845                         int count, int ypos, int xpos)
846 {
847         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
848         struct display *p = &fb_display[vc->vc_num];
849
850         if (!info->fbops->fb_blank && console_blanked)
851                 return;
852         if (info->state != FBINFO_STATE_RUNNING)
853                 return;
854
855         if (vt_cons[vc->vc_num]->vc_mode != KD_TEXT)
856                 return;
857
858         accel_putcs(vc, info, s, count, real_y(p, ypos), xpos);
859 }
860
861 static void fbcon_cursor(struct vc_data *vc, int mode)
862 {
863         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
864         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
865         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
866         int fgshift = (vc->vc_hi_font_mask) ? 9 : 8;
867         struct display *p = &fb_display[vc->vc_num];
868         int w = (vc->vc_font.width + 7) >> 3, c;
869         int y = real_y(p, vc->vc_y);
870         struct fb_cursor cursor;
871         
872         if (mode & CM_SOFTBACK) {
873                 mode &= ~CM_SOFTBACK;
874                 if (softback_lines) {
875                         if (y + softback_lines >= vc->vc_rows)
876                                 mode = CM_ERASE;
877                         else
878                                 y += softback_lines;
879                 }
880         } else if (softback_lines)
881                 fbcon_set_origin(vc);
882
883         c = scr_readw((u16 *) vc->vc_pos);
884
885         cursor.image.data = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
886         cursor.set = FB_CUR_SETCUR;
887         cursor.image.depth = 1;
888         
889         switch (mode) {
890         case CM_ERASE:
891                 if (info->cursor.rop == ROP_XOR) {
892                         info->cursor.enable = 0;
893                         info->cursor.rop = ROP_COPY;
894                         info->fbops->fb_cursor(info, &cursor);
895                 }       
896                 break;
897         case CM_MOVE:
898         case CM_DRAW:
899                 info->cursor.enable = 1;
900                 
901                 if (info->cursor.image.fg_color != attr_fgcol(fgshift, c) ||
902                     info->cursor.image.bg_color != attr_bgcol(bgshift, c)) {
903                         cursor.image.fg_color = attr_fgcol(fgshift, c);
904                         cursor.image.bg_color = attr_bgcol(bgshift, c);
905                         cursor.set |= FB_CUR_SETCMAP;
906                 }
907                 
908                 if ((info->cursor.image.dx != (vc->vc_font.width * vc->vc_x)) ||
909                     (info->cursor.image.dy != (vc->vc_font.height * y))) {
910                         cursor.image.dx = vc->vc_font.width * vc->vc_x;
911                         cursor.image.dy = vc->vc_font.height * y;
912                         cursor.set |= FB_CUR_SETPOS;
913                 }
914
915                 if (info->cursor.image.height != vc->vc_font.height ||
916                     info->cursor.image.width != vc->vc_font.width) {
917                         cursor.image.height = vc->vc_font.height;
918                         cursor.image.width = vc->vc_font.width;
919                         cursor.set |= FB_CUR_SETSIZE;
920                 }
921
922                 if (info->cursor.hot.x || info->cursor.hot.y) {
923                         cursor.hot.x = cursor.hot.y = 0;
924                         cursor.set |= FB_CUR_SETHOT;
925                 }
926
927                 if ((cursor.set & FB_CUR_SETSIZE) || ((vc->vc_cursor_type & 0x0f) != p->cursor_shape)) {
928                         char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
929                         int cur_height, size, i = 0;
930
931                         if (!mask)      return; 
932                 
933                         if (info->cursor.mask)
934                                 kfree(info->cursor.mask);
935                         info->cursor.mask = mask;
936         
937                         p->cursor_shape = vc->vc_cursor_type & 0x0f;
938                         cursor.set |= FB_CUR_SETSHAPE;
939
940                         switch (vc->vc_cursor_type & 0x0f) {
941                         case CUR_NONE:
942                                 cur_height = 0;
943                                 break;
944                         case CUR_UNDERLINE:
945                                 cur_height = (vc->vc_font.height < 10) ? 1 : 2;
946                                 break;
947                         case CUR_LOWER_THIRD:
948                                 cur_height = vc->vc_font.height/3;
949                                 break;
950                         case CUR_LOWER_HALF:
951                                 cur_height = vc->vc_font.height >> 1;
952                                 break;
953                         case CUR_TWO_THIRDS:
954                                 cur_height = (vc->vc_font.height << 1)/3;
955                                 break;
956                         case CUR_BLOCK:
957                         default:
958                                 cur_height = vc->vc_font.height;
959                                 break;
960                         }
961                         size = (vc->vc_font.height - cur_height) * w;
962                         while (size--)
963                                 mask[i++] = 0;
964                         size = cur_height * w;
965                         while (size--)
966                                 mask[i++] = 0xff;
967                 }
968                 info->cursor.rop = ROP_XOR;
969                 info->fbops->fb_cursor(info, &cursor);
970                 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
971                 break;
972         }
973 }
974
975 static int scrollback_phys_max = 0;
976 static int scrollback_max = 0;
977 static int scrollback_current = 0;
978
979 int update_var(int con, struct fb_info *info)
980 {
981         if (con == info->currcon) 
982                 return fb_pan_display(info, &info->var);
983         return 0;
984 }
985
986 static __inline__ void ywrap_up(struct vc_data *vc, int count)
987 {
988         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
989         struct display *p = &fb_display[vc->vc_num];
990         
991         p->yscroll += count;
992         if (p->yscroll >= p->vrows)     /* Deal with wrap */
993                 p->yscroll -= p->vrows;
994         info->var.xoffset = 0;
995         info->var.yoffset = p->yscroll * vc->vc_font.height;
996         info->var.vmode |= FB_VMODE_YWRAP;
997         update_var(vc->vc_num, info);
998         scrollback_max += count;
999         if (scrollback_max > scrollback_phys_max)
1000                 scrollback_max = scrollback_phys_max;
1001         scrollback_current = 0;
1002 }
1003
1004 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1005 {
1006         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1007         struct display *p = &fb_display[vc->vc_num];
1008         
1009         p->yscroll -= count;
1010         if (p->yscroll < 0)     /* Deal with wrap */
1011                 p->yscroll += p->vrows;
1012         info->var.xoffset = 0;
1013         info->var.yoffset = p->yscroll * vc->vc_font.height;
1014         info->var.vmode |= FB_VMODE_YWRAP;
1015         update_var(vc->vc_num, info);
1016         scrollback_max -= count;
1017         if (scrollback_max < 0)
1018                 scrollback_max = 0;
1019         scrollback_current = 0;
1020 }
1021
1022 static __inline__ void ypan_up(struct vc_data *vc, int count)
1023 {
1024         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1025         struct display *p = &fb_display[vc->vc_num];
1026         
1027         p->yscroll += count;
1028         if (p->yscroll > p->vrows - vc->vc_rows) {
1029                 accel_bmove(vc, info, p->vrows - vc->vc_rows, 
1030                                 0, 0, 0, vc->vc_rows, vc->vc_cols);
1031                 p->yscroll -= p->vrows - vc->vc_rows;
1032         }
1033         info->var.xoffset = 0;
1034         info->var.yoffset = p->yscroll * vc->vc_font.height;
1035         info->var.vmode &= ~FB_VMODE_YWRAP;
1036         update_var(vc->vc_num, info);
1037         accel_clear_margins(vc, info, 1);
1038         scrollback_max += count;
1039         if (scrollback_max > scrollback_phys_max)
1040                 scrollback_max = scrollback_phys_max;
1041         scrollback_current = 0;
1042 }
1043
1044 static __inline__ void ypan_down(struct vc_data *vc, int count)
1045 {
1046         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1047         struct display *p = &fb_display[vc->vc_num];
1048         
1049         p->yscroll -= count;
1050         if (p->yscroll < 0) {
1051                 accel_bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1052                                 0, vc->vc_rows, vc->vc_cols);
1053                 p->yscroll += p->vrows - vc->vc_rows;
1054         }
1055         info->var.xoffset = 0;
1056         info->var.yoffset = p->yscroll * vc->vc_font.height;
1057         info->var.vmode &= ~FB_VMODE_YWRAP;
1058         update_var(vc->vc_num, info);
1059         accel_clear_margins(vc, info, 1);
1060         scrollback_max -= count;
1061         if (scrollback_max < 0)
1062                 scrollback_max = 0;
1063         scrollback_current = 0;
1064 }
1065
1066 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1067                                   long delta)
1068 {
1069         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1070         int count = vc->vc_rows;
1071         unsigned short *d, *s;
1072         unsigned long n;
1073         int line = 0;
1074
1075         d = (u16 *) softback_curr;
1076         if (d == (u16 *) softback_in)
1077                 d = (u16 *) vc->vc_origin;
1078         n = softback_curr + delta * vc->vc_size_row;
1079         softback_lines -= delta;
1080         if (delta < 0) {
1081                 if (softback_curr < softback_top && n < softback_buf) {
1082                         n += softback_end - softback_buf;
1083                         if (n < softback_top) {
1084                                 softback_lines -=
1085                                     (softback_top - n) / vc->vc_size_row;
1086                                 n = softback_top;
1087                         }
1088                 } else if (softback_curr >= softback_top
1089                            && n < softback_top) {
1090                         softback_lines -=
1091                             (softback_top - n) / vc->vc_size_row;
1092                         n = softback_top;
1093                 }
1094         } else {
1095                 if (softback_curr > softback_in && n >= softback_end) {
1096                         n += softback_buf - softback_end;
1097                         if (n > softback_in) {
1098                                 n = softback_in;
1099                                 softback_lines = 0;
1100                         }
1101                 } else if (softback_curr <= softback_in && n > softback_in) {
1102                         n = softback_in;
1103                         softback_lines = 0;
1104                 }
1105         }
1106         if (n == softback_curr)
1107                 return;
1108         softback_curr = n;
1109         s = (u16 *) softback_curr;
1110         if (s == (u16 *) softback_in)
1111                 s = (u16 *) vc->vc_origin;
1112         while (count--) {
1113                 unsigned short *start;
1114                 unsigned short *le;
1115                 unsigned short c;
1116                 int x = 0;
1117                 unsigned short attr = 1;
1118
1119                 start = s;
1120                 le = advance_row(s, 1);
1121                 do {
1122                         c = scr_readw(s);
1123                         if (attr != (c & 0xff00)) {
1124                                 attr = c & 0xff00;
1125                                 if (s > start) {
1126                                         accel_putcs(vc, info, start, s - start,
1127                                                     real_y(p, line), x);
1128                                         x += s - start;
1129                                         start = s;
1130                                 }
1131                         }
1132                         if (c == scr_readw(d)) {
1133                                 if (s > start) {
1134                                         accel_putcs(vc, info, start, s - start,
1135                                                     real_y(p, line), x);
1136                                         x += s - start + 1;
1137                                         start = s + 1;
1138                                 } else {
1139                                         x++;
1140                                         start++;
1141                                 }
1142                         }
1143                         s++;
1144                         d++;
1145                 } while (s < le);
1146                 if (s > start)
1147                         accel_putcs(vc, info, start, s - start,
1148                                     real_y(p, line), x);
1149                 line++;
1150                 if (d == (u16 *) softback_end)
1151                         d = (u16 *) softback_buf;
1152                 if (d == (u16 *) softback_in)
1153                         d = (u16 *) vc->vc_origin;
1154                 if (s == (u16 *) softback_end)
1155                         s = (u16 *) softback_buf;
1156                 if (s == (u16 *) softback_in)
1157                         s = (u16 *) vc->vc_origin;
1158         }
1159 }
1160
1161 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1162                          int line, int count, int offset)
1163 {
1164         unsigned short *d = (unsigned short *)
1165             (vc->vc_origin + vc->vc_size_row * line);
1166         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1167         unsigned short *s = d + offset;
1168
1169         while (count--) {
1170                 unsigned short *start = s;
1171                 unsigned short *le = advance_row(s, 1);
1172                 unsigned short c;
1173                 int x = 0;
1174                 unsigned short attr = 1;
1175
1176                 do {
1177                         c = scr_readw(s);
1178                         if (attr != (c & 0xff00)) {
1179                                 attr = c & 0xff00;
1180                                 if (s > start) {
1181                                         accel_putcs(vc, info, start, s - start,
1182                                                     real_y(p, line), x);
1183                                         x += s - start;
1184                                         start = s;
1185                                 }
1186                         }
1187                         if (c == scr_readw(d)) {
1188                                 if (s > start) {
1189                                         accel_putcs(vc, info, start, s - start,
1190                                                     real_y(p, line), x);
1191                                         x += s - start + 1;
1192                                         start = s + 1;
1193                                 } else {
1194                                         x++;
1195                                         start++;
1196                                 }
1197                         }
1198                         scr_writew(c, d);
1199                         console_conditional_schedule();
1200                         s++;
1201                         d++;
1202                 } while (s < le);
1203                 if (s > start)
1204                         accel_putcs(vc, info, start, s - start,
1205                                     real_y(p, line), x);
1206                 console_conditional_schedule();
1207                 if (offset > 0)
1208                         line++;
1209                 else {
1210                         line--;
1211                         /* NOTE: We subtract two lines from these pointers */
1212                         s -= vc->vc_size_row;
1213                         d -= vc->vc_size_row;
1214                 }
1215         }
1216 }
1217
1218 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1219                                        int count)
1220 {
1221         unsigned short *p;
1222
1223         if (vc->vc_num != fg_console)
1224                 return;
1225         p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1226
1227         while (count) {
1228                 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1229                 count--;
1230                 p = advance_row(p, 1);
1231                 softback_in += vc->vc_size_row;
1232                 if (softback_in == softback_end)
1233                         softback_in = softback_buf;
1234                 if (softback_in == softback_top) {
1235                         softback_top += vc->vc_size_row;
1236                         if (softback_top == softback_end)
1237                                 softback_top = softback_buf;
1238                 }
1239         }
1240         softback_curr = softback_in;
1241 }
1242
1243 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1244                         int count)
1245 {
1246         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1247         struct display *p = &fb_display[vc->vc_num];
1248         int scroll_partial = !(p->scrollmode & __SCROLL_YNOPARTIAL);
1249
1250         if (!info->fbops->fb_blank && console_blanked)
1251                 return 0;
1252
1253         if (!count || vt_cons[vc->vc_num]->vc_mode != KD_TEXT)
1254                 return 0;
1255
1256         fbcon_cursor(vc, CM_ERASE);
1257
1258         /*
1259          * ++Geert: Only use ywrap/ypan if the console is in text mode
1260          * ++Andrew: Only use ypan on hardware text mode when scrolling the
1261          *           whole screen (prevents flicker).
1262          */
1263
1264         switch (dir) {
1265         case SM_UP:
1266                 if (count > vc->vc_rows)        /* Maximum realistic size */
1267                         count = vc->vc_rows;
1268                 if (softback_top)
1269                         fbcon_softback_note(vc, t, count);
1270                 if (logo_shown >= 0)
1271                         goto redraw_up;
1272                 switch (p->scrollmode & __SCROLL_YMASK) {
1273                 case __SCROLL_YMOVE:
1274                         accel_bmove(vc, info, t + count, 0, t, 0,
1275                                          b - t - count, vc->vc_cols);
1276                         accel_clear(vc, info, b - count, 0, count,
1277                                          vc->vc_cols);
1278                         break;
1279
1280                 case __SCROLL_YWRAP:
1281                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1282                                 if (t > 0)
1283                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1284                                                     vc->vc_cols);
1285                                 ywrap_up(vc, count);
1286                                 if (vc->vc_rows - b > 0)
1287                                         fbcon_bmove(vc, b - count, 0, b, 0,
1288                                                     vc->vc_rows - b,
1289                                                     vc->vc_cols);
1290                         } else if (p->scrollmode & __SCROLL_YPANREDRAW)
1291                                 goto redraw_up;
1292                         else
1293                                 fbcon_bmove(vc, t + count, 0, t, 0,
1294                                             b - t - count, vc->vc_cols);
1295                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1296                         break;
1297
1298                 case __SCROLL_YPAN:
1299                         if ((p->yscroll + count <=
1300                              2 * (p->vrows - vc->vc_rows))
1301                             && ((!scroll_partial && (b - t == vc->vc_rows))
1302                                 || (scroll_partial
1303                                     && (b - t - count >
1304                                         3 * vc->vc_rows >> 2)))) {
1305                                 if (t > 0)
1306                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1307                                                     vc->vc_cols);
1308                                 ypan_up(vc, count);
1309                                 if (vc->vc_rows - b > 0)
1310                                         fbcon_bmove(vc, b - count, 0, b, 0,
1311                                                     vc->vc_rows - b,
1312                                                     vc->vc_cols);
1313                         } else if (p->scrollmode & __SCROLL_YPANREDRAW)
1314                                 goto redraw_up;
1315                         else
1316                                 fbcon_bmove(vc, t + count, 0, t, 0,
1317                                             b - t - count, vc->vc_cols);
1318                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1319                         break;
1320
1321                 case __SCROLL_YREDRAW:
1322                       redraw_up:
1323                         fbcon_redraw(vc, p, t, b - t - count,
1324                                      count * vc->vc_cols);
1325                         accel_clear(vc, info, real_y(p, b - count), 0,
1326                                          count, vc->vc_cols);
1327                         scr_memsetw((unsigned short *) (vc->vc_origin +
1328                                                         vc->vc_size_row *
1329                                                         (b - count)),
1330                                     vc->vc_video_erase_char,
1331                                     vc->vc_size_row * count);
1332                         return 1;
1333                 }
1334                 break;
1335
1336         case SM_DOWN:
1337                 if (count > vc->vc_rows)        /* Maximum realistic size */
1338                         count = vc->vc_rows;
1339                 switch (p->scrollmode & __SCROLL_YMASK) {
1340                 case __SCROLL_YMOVE:
1341                         accel_bmove(vc, info, t, 0, t + count, 0,
1342                                          b - t - count, vc->vc_cols);
1343                         accel_clear(vc, info, t, 0, count, vc->vc_cols);
1344                         break;
1345
1346                 case __SCROLL_YWRAP:
1347                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1348                                 if (vc->vc_rows - b > 0)
1349                                         fbcon_bmove(vc, b, 0, b - count, 0,
1350                                                     vc->vc_rows - b,
1351                                                     vc->vc_cols);
1352                                 ywrap_down(vc, count);
1353                                 if (t > 0)
1354                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1355                                                     vc->vc_cols);
1356                         } else if (p->scrollmode & __SCROLL_YPANREDRAW)
1357                                 goto redraw_down;
1358                         else
1359                                 fbcon_bmove(vc, t, 0, t + count, 0,
1360                                             b - t - count, vc->vc_cols);
1361                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1362                         break;
1363
1364                 case __SCROLL_YPAN:
1365                         if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1366                             && ((!scroll_partial && (b - t == vc->vc_rows))
1367                                 || (scroll_partial
1368                                     && (b - t - count >
1369                                         3 * vc->vc_rows >> 2)))) {
1370                                 if (vc->vc_rows - b > 0)
1371                                         fbcon_bmove(vc, b, 0, b - count, 0,
1372                                                     vc->vc_rows - b,
1373                                                     vc->vc_cols);
1374                                 ypan_down(vc, count);
1375                                 if (t > 0)
1376                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1377                                                     vc->vc_cols);
1378                         } else if (p->scrollmode & __SCROLL_YPANREDRAW)
1379                                 goto redraw_down;
1380                         else
1381                                 fbcon_bmove(vc, t, 0, t + count, 0,
1382                                             b - t - count, vc->vc_cols);
1383                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1384                         break;
1385
1386                 case __SCROLL_YREDRAW:
1387                       redraw_down:
1388                         fbcon_redraw(vc, p, b - 1, b - t - count,
1389                                      -count * vc->vc_cols);
1390                         accel_clear(vc, info, real_y(p, t), 0, count,
1391                                          vc->vc_cols);
1392                         scr_memsetw((unsigned short *) (vc->vc_origin +
1393                                                         vc->vc_size_row *
1394                                                         t),
1395                                     vc->vc_video_erase_char,
1396                                     vc->vc_size_row * count);
1397                         return 1;
1398                 }
1399         }
1400         return 0;
1401 }
1402
1403
1404 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
1405                         int height, int width)
1406 {
1407         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1408         struct display *p = &fb_display[vc->vc_num];
1409         
1410         if (!info->fbops->fb_blank && console_blanked)
1411                 return;
1412
1413         if (!width || !height)
1414                 return;
1415
1416         /*  Split blits that cross physical y_wrap case.
1417          *  Pathological case involves 4 blits, better to use recursive
1418          *  code rather than unrolled case
1419          *
1420          *  Recursive invocations don't need to erase the cursor over and
1421          *  over again, so we use fbcon_bmove_rec()
1422          */
1423         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
1424                         p->vrows - p->yscroll);
1425 }
1426
1427 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx, 
1428                             int dy, int dx, int height, int width, u_int y_break)
1429 {
1430         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1431         u_int b;
1432
1433         if (sy < y_break && sy + height > y_break) {
1434                 b = y_break - sy;
1435                 if (dy < sy) {  /* Avoid trashing self */
1436                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1437                                         y_break);
1438                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1439                                         height - b, width, y_break);
1440                 } else {
1441                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1442                                         height - b, width, y_break);
1443                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1444                                         y_break);
1445                 }
1446                 return;
1447         }
1448
1449         if (dy < y_break && dy + height > y_break) {
1450                 b = y_break - dy;
1451                 if (dy < sy) {  /* Avoid trashing self */
1452                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1453                                         y_break);
1454                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1455                                         height - b, width, y_break);
1456                 } else {
1457                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1458                                         height - b, width, y_break);
1459                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1460                                         y_break);
1461                 }
1462                 return;
1463         }
1464         accel_bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
1465                         height, width);
1466 }
1467
1468 static __inline__ void updatescrollmode(struct display *p, struct fb_info *info, struct vc_data *vc)
1469 {
1470         int m;
1471
1472         if (p->scrollmode & __SCROLL_YFIXED)
1473                 return;
1474         if (divides(info->fix.ywrapstep, vc->vc_font.height) &&
1475             divides(vc->vc_font.height, info->var.yres_virtual))
1476                 m = __SCROLL_YWRAP;
1477         else if (divides(info->fix.ypanstep, vc->vc_font.height) &&
1478                  info->var.yres_virtual >= info->var.yres + vc->vc_font.height)
1479                 m = __SCROLL_YPAN;
1480         else if (p->scrollmode & __SCROLL_YNOMOVE)
1481                 m = __SCROLL_YREDRAW;
1482         else
1483                 m = __SCROLL_YMOVE;
1484         p->scrollmode = (p->scrollmode & ~__SCROLL_YMASK) | m;
1485 }
1486
1487 static int fbcon_resize(struct vc_data *vc, unsigned int width, 
1488                         unsigned int height)
1489 {
1490         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1491         struct display *p = &fb_display[vc->vc_num];
1492         struct fb_var_screeninfo var = info->var;
1493         int err; int x_diff, y_diff;
1494         int fw = vc->vc_font.width;
1495         int fh = vc->vc_font.height;
1496
1497         var.xres = width * fw;
1498         var.yres = height * fh;
1499         x_diff = info->var.xres - var.xres;
1500         y_diff = info->var.yres - var.yres;
1501         if (x_diff < 0 || x_diff > fw || (y_diff < 0 || y_diff > fh)) {
1502                 char mode[40];
1503
1504                 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
1505                 if (!info->fbops->fb_set_par)
1506                         return -EINVAL;
1507
1508                 sprintf(mode, "%dx%d", var.xres, var.yres);
1509                 err = fb_find_mode(&var, info, mode, NULL, 0, NULL,
1510                                         info->var.bits_per_pixel);
1511                 if (!err || width > var.xres/fw || height > var.yres/fh)
1512                         return -EINVAL;
1513                 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
1514                 if (CON_IS_VISIBLE(vc)) {
1515                         var.activate = FB_ACTIVATE_NOW;
1516                         fb_set_var(info, &var);
1517                 }
1518         }
1519         p->vrows = var.yres_virtual/fh;
1520         if (var.yres > (fh * (height + 1)))
1521                 p->vrows -= (var.yres - (fh * height)) / fh;
1522         if ((var.yres % fh) && (var.yres_virtual % fh < var.yres % fh))
1523                 p->vrows--;
1524         updatescrollmode(p, info, vc);
1525         return 0;
1526 }
1527
1528 static int fbcon_switch(struct vc_data *vc)
1529 {
1530         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1531         struct display *p = &fb_display[vc->vc_num];
1532
1533         if (softback_top) {
1534                 int l = fbcon_softback_size / vc->vc_size_row;
1535                 if (softback_lines)
1536                         fbcon_set_origin(vc);
1537                 softback_top = softback_curr = softback_in = softback_buf;
1538                 softback_lines = 0;
1539
1540                 if (l > 5)
1541                         softback_end = softback_buf + l * vc->vc_size_row;
1542                 else {
1543                         /* Smaller scrollback makes no sense, and 0 would screw
1544                            the operation totally */
1545                         softback_top = 0;
1546                 }
1547         }
1548         if (logo_shown >= 0) {
1549                 struct vc_data *conp2 = vc_cons[logo_shown].d;
1550
1551                 if (conp2->vc_top == logo_lines
1552                     && conp2->vc_bottom == conp2->vc_rows)
1553                         conp2->vc_top = 0;
1554                 logo_shown = -1;
1555         }
1556         if (info)
1557                 info->var.yoffset = p->yscroll = 0;
1558         fbcon_resize(vc, vc->vc_cols, vc->vc_rows);
1559         switch (p->scrollmode & __SCROLL_YMASK) {
1560         case __SCROLL_YWRAP:
1561                 scrollback_phys_max = p->vrows - vc->vc_rows;
1562                 break;
1563         case __SCROLL_YPAN:
1564                 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
1565                 if (scrollback_phys_max < 0)
1566                         scrollback_phys_max = 0;
1567                 break;
1568         default:
1569                 scrollback_phys_max = 0;
1570                 break;
1571         }
1572         scrollback_max = 0;
1573         scrollback_current = 0;
1574
1575         info->currcon = vc->vc_num;
1576         
1577         update_var(vc->vc_num, info);
1578         fbcon_set_palette(vc, color_table);     
1579
1580         if (vt_cons[vc->vc_num]->vc_mode == KD_TEXT)
1581                 accel_clear_margins(vc, info, 0);
1582         if (logo_shown == -2) {
1583                 logo_shown = fg_console;
1584                 /* This is protected above by initmem_freed */
1585                 fb_show_logo(info);
1586                 update_region(fg_console,
1587                               vc->vc_origin + vc->vc_size_row * vc->vc_top,
1588                               vc->vc_size_row * (vc->vc_bottom -
1589                                                  vc->vc_top) / 2);
1590                 return 0;
1591         }
1592         return 1;
1593 }
1594
1595 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
1596 {
1597         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
1598         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1599         struct display *p = &fb_display[vc->vc_num];
1600
1601         if (mode_switch) {
1602                 struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1603                 struct fb_var_screeninfo var = info->var;
1604
1605                 if (blank) {
1606                         fbcon_cursor(vc, CM_ERASE);
1607                         return 0;
1608                 }
1609                 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
1610                 fb_set_var(info, &var);
1611         }
1612
1613         fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
1614
1615         if (!info->fbops->fb_blank) {
1616                 if (blank) {
1617                         unsigned short oldc;
1618                         u_int height;
1619                         u_int y_break;
1620
1621                         oldc = vc->vc_video_erase_char;
1622                         vc->vc_video_erase_char &= charmask;
1623                         height = vc->vc_rows;
1624                         y_break = p->vrows - p->yscroll;
1625                         if (height > y_break) {
1626                                 accel_clear(vc, info, real_y(p, 0),
1627                                             0, y_break, vc->vc_cols);
1628                                 accel_clear(vc, info, real_y(p, y_break),
1629                                             0, height - y_break, 
1630                                             vc->vc_cols);
1631                         } else
1632                                 accel_clear(vc, info, real_y(p, 0),
1633                                             0, height, vc->vc_cols);
1634                         vc->vc_video_erase_char = oldc;
1635                 } else
1636                         update_screen(vc->vc_num);
1637                 return 0;
1638         } else
1639                 return fb_blank(info, blank);
1640 }
1641
1642 static void fbcon_free_font(struct display *p)
1643 {
1644         if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1645                 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1646         p->fontdata = NULL;
1647         p->userfont = 0;
1648 }
1649
1650 static inline int fbcon_get_font(struct vc_data *vc, struct console_font_op *op)
1651 {
1652         u8 *fontdata = vc->vc_font.data;
1653         u8 *data = op->data;
1654         int i, j;
1655
1656         op->width = vc->vc_font.width;
1657         op->height = vc->vc_font.height;
1658         op->charcount = vc->vc_hi_font_mask ? 512 : 256;
1659         if (!op->data)
1660                 return 0;
1661
1662         if (op->width <= 8) {
1663                 j = vc->vc_font.height;
1664                 for (i = 0; i < op->charcount; i++) {
1665                         memcpy(data, fontdata, j);
1666                         memset(data + j, 0, 32 - j);
1667                         data += 32;
1668                         fontdata += j;
1669                 }
1670         } else if (op->width <= 16) {
1671                 j = vc->vc_font.height * 2;
1672                 for (i = 0; i < op->charcount; i++) {
1673                         memcpy(data, fontdata, j);
1674                         memset(data + j, 0, 64 - j);
1675                         data += 64;
1676                         fontdata += j;
1677                 }
1678         } else if (op->width <= 24) {
1679                 for (i = 0; i < op->charcount; i++) {
1680                         for (j = 0; j < vc->vc_font.height; j++) {
1681                                 *data++ = fontdata[0];
1682                                 *data++ = fontdata[1];
1683                                 *data++ = fontdata[2];
1684                                 fontdata += sizeof(u32);
1685                         }
1686                         memset(data, 0, 3 * (32 - j));
1687                         data += 3 * (32 - j);
1688                 }
1689         } else {
1690                 j = vc->vc_font.height * 4;
1691                 for (i = 0; i < op->charcount; i++) {
1692                         memcpy(data, fontdata, j);
1693                         memset(data + j, 0, 128 - j);
1694                         data += 128;
1695                         fontdata += j;
1696                 }
1697         }
1698         return 0;
1699 }
1700
1701 static int fbcon_do_set_font(struct vc_data *vc, struct console_font_op *op,
1702                              u8 * data, int userfont)
1703 {
1704         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1705         struct display *p = &fb_display[vc->vc_num];
1706         int resize;
1707         int w = op->width;
1708         int h = op->height;
1709         int cnt;
1710         char *old_data = NULL;
1711
1712         if (!w > 32) {
1713                 if (userfont && op->op != KD_FONT_OP_COPY)
1714                         kfree(data - FONT_EXTRA_WORDS * sizeof(int));
1715                 return -ENXIO;
1716         }
1717
1718         if (CON_IS_VISIBLE(vc) && softback_lines)
1719                 fbcon_set_origin(vc);
1720
1721         resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
1722         if (p->userfont)
1723                 old_data = vc->vc_font.data;
1724         if (userfont)
1725                 cnt = FNTCHARCNT(data);
1726         else
1727                 cnt = 256;
1728         vc->vc_font.data = p->fontdata = data;
1729         if ((p->userfont = userfont))
1730                 REFCOUNT(data)++;
1731         vc->vc_font.width = w;
1732         vc->vc_font.height = h;
1733         if (vc->vc_hi_font_mask && cnt == 256) {
1734                 vc->vc_hi_font_mask = 0;
1735                 if (vc->vc_can_do_color) {
1736                         vc->vc_complement_mask >>= 1;
1737                         vc->vc_s_complement_mask >>= 1;
1738                 }
1739                         
1740                 /* ++Edmund: reorder the attribute bits */
1741                 if (vc->vc_can_do_color) {
1742                         unsigned short *cp =
1743                             (unsigned short *) vc->vc_origin;
1744                         int count = vc->vc_screenbuf_size / 2;
1745                         unsigned short c;
1746                         for (; count > 0; count--, cp++) {
1747                                 c = scr_readw(cp);
1748                                 scr_writew(((c & 0xfe00) >> 1) |
1749                                            (c & 0xff), cp);
1750                         }
1751                         c = vc->vc_video_erase_char;
1752                         vc->vc_video_erase_char =
1753                             ((c & 0xfe00) >> 1) | (c & 0xff);
1754                         vc->vc_attr >>= 1;
1755                 }
1756         } else if (!vc->vc_hi_font_mask && cnt == 512) {
1757                 vc->vc_hi_font_mask = 0x100;
1758                 if (vc->vc_can_do_color) {
1759                         vc->vc_complement_mask <<= 1;
1760                         vc->vc_s_complement_mask <<= 1;
1761                 }
1762                         
1763                 /* ++Edmund: reorder the attribute bits */
1764                 {
1765                         unsigned short *cp =
1766                             (unsigned short *) vc->vc_origin;
1767                         int count = vc->vc_screenbuf_size / 2;
1768                         unsigned short c;
1769                         for (; count > 0; count--, cp++) {
1770                                 unsigned short newc;
1771                                 c = scr_readw(cp);
1772                                 if (vc->vc_can_do_color)
1773                                         newc =
1774                                             ((c & 0xff00) << 1) | (c &
1775                                                                    0xff);
1776                                 else
1777                                         newc = c & ~0x100;
1778                                 scr_writew(newc, cp);
1779                         }
1780                         c = vc->vc_video_erase_char;
1781                         if (vc->vc_can_do_color) {
1782                                 vc->vc_video_erase_char =
1783                                     ((c & 0xff00) << 1) | (c & 0xff);
1784                                 vc->vc_attr <<= 1;
1785                         } else
1786                                 vc->vc_video_erase_char = c & ~0x100;
1787                 }
1788
1789         }
1790
1791         if (resize) {
1792                 /* reset wrap/pan */
1793                 info->var.xoffset = info->var.yoffset = p->yscroll = 0;
1794                 vc_resize(vc->vc_num, info->var.xres / w, info->var.yres / h);
1795                 if (CON_IS_VISIBLE(vc) && softback_buf) {
1796                         int l = fbcon_softback_size / vc->vc_size_row;
1797                         if (l > 5)
1798                                 softback_end =
1799                                     softback_buf + l * vc->vc_size_row;
1800                         else {
1801                                 /* Smaller scrollback makes no sense, and 0 would screw
1802                                    the operation totally */
1803                                 softback_top = 0;
1804                         }
1805                 }
1806         } else if (CON_IS_VISIBLE(vc)
1807                    && vt_cons[vc->vc_num]->vc_mode == KD_TEXT) {
1808                 accel_clear_margins(vc, info, 0);
1809                 update_screen(vc->vc_num);
1810         }
1811
1812         if (old_data && (--REFCOUNT(old_data) == 0))
1813                 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
1814         return 0;
1815 }
1816
1817 static inline int fbcon_copy_font(struct vc_data *vc, struct console_font_op *op)
1818 {
1819         struct display *od;
1820         int h = op->height;
1821
1822         if (h < 0 || !vc_cons_allocated(h))
1823                 return -ENOTTY;
1824         if (h == vc->vc_num)
1825                 return 0;       /* nothing to do */
1826         od = &fb_display[h];
1827         if (od->fontdata == vc->vc_font.data)
1828                 return 0;       /* already the same font... */
1829         op->width = vc->vc_font.width;
1830         op->height = vc->vc_font.height;
1831         return fbcon_do_set_font(vc, op, od->fontdata, od->userfont);
1832 }
1833
1834 static inline int fbcon_set_font(struct vc_data *vc, struct console_font_op *op)
1835 {
1836         int w = op->width;
1837         int h = op->height;
1838         int size = h;
1839         int i, k;
1840         u8 *new_data, *data = op->data, *p;
1841
1842         if ((w <= 0) || (w > 32)
1843             || (op->charcount != 256 && op->charcount != 512))
1844                 return -EINVAL;
1845
1846         if (w > 8) {
1847                 if (w <= 16)
1848                         size *= 2;
1849                 else
1850                         size *= 4;
1851         }
1852         size *= op->charcount;
1853
1854         if (!
1855             (new_data =
1856              kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER)))
1857                 return -ENOMEM;
1858         new_data += FONT_EXTRA_WORDS * sizeof(int);
1859         FNTSIZE(new_data) = size;
1860         FNTCHARCNT(new_data) = op->charcount;
1861         REFCOUNT(new_data) = 0; /* usage counter */
1862         p = new_data;
1863         if (w <= 8) {
1864                 for (i = 0; i < op->charcount; i++) {
1865                         memcpy(p, data, h);
1866                         data += 32;
1867                         p += h;
1868                 }
1869         } else if (w <= 16) {
1870                 h *= 2;
1871                 for (i = 0; i < op->charcount; i++) {
1872                         memcpy(p, data, h);
1873                         data += 64;
1874                         p += h;
1875                 }
1876         } else if (w <= 24) {
1877                 for (i = 0; i < op->charcount; i++) {
1878                         int j;
1879                         for (j = 0; j < h; j++) {
1880                                 memcpy(p, data, 3);
1881                                 p[3] = 0;
1882                                 data += 3;
1883                                 p += sizeof(u32);
1884                         }
1885                         data += 3 * (32 - h);
1886                 }
1887         } else {
1888                 h *= 4;
1889                 for (i = 0; i < op->charcount; i++) {
1890                         memcpy(p, data, h);
1891                         data += 128;
1892                         p += h;
1893                 }
1894         }
1895         /* we can do it in u32 chunks because of charcount is 256 or 512, so
1896            font length must be multiple of 256, at least. And 256 is multiple
1897            of 4 */
1898         k = 0;
1899         while (p > new_data) {
1900                 p = (u8 *)((u32 *)p - 1);
1901                 k += *(u32 *) p;
1902         }
1903         FNTSUM(new_data) = k;
1904         /* Check if the same font is on some other console already */
1905         for (i = 0; i < MAX_NR_CONSOLES; i++) {
1906                 struct vc_data *tmp = vc_cons[i].d;
1907                 
1908                 if (fb_display[i].userfont &&
1909                     fb_display[i].fontdata &&
1910                     FNTSUM(fb_display[i].fontdata) == k &&
1911                     FNTSIZE(fb_display[i].fontdata) == size &&
1912                     tmp->vc_font.width == w &&
1913                     !memcmp(fb_display[i].fontdata, new_data, size)) {
1914                         kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
1915                         new_data = fb_display[i].fontdata;
1916                         break;
1917                 }
1918         }
1919         return fbcon_do_set_font(vc, op, new_data, 1);
1920 }
1921
1922 static inline int fbcon_set_def_font(struct vc_data *vc, struct console_font_op *op)
1923 {
1924         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1925         char name[MAX_FONT_NAME];
1926         struct font_desc *f;
1927
1928         if (!op->data)
1929                 f = get_default_font(info->var.xres, info->var.yres);
1930         else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
1931                 return -EFAULT;
1932         else {
1933                 name[MAX_FONT_NAME - 1] = 0;
1934                 if (!(f = find_font(name)))
1935                         return -ENOENT;
1936         }
1937         op->width = f->width;
1938         op->height = f->height;
1939         return fbcon_do_set_font(vc, op, f->data, 0);
1940 }
1941
1942 static int fbcon_font_op(struct vc_data *vc, struct console_font_op *op)
1943 {
1944         switch (op->op) {
1945         case KD_FONT_OP_SET:
1946                 return fbcon_set_font(vc, op);
1947         case KD_FONT_OP_GET:
1948                 return fbcon_get_font(vc, op);
1949         case KD_FONT_OP_SET_DEFAULT:
1950                 return fbcon_set_def_font(vc, op);
1951         case KD_FONT_OP_COPY:
1952                 return fbcon_copy_font(vc, op);
1953         default:
1954                 return -ENOSYS;
1955         }
1956 }
1957
1958 static u16 palette_red[16];
1959 static u16 palette_green[16];
1960 static u16 palette_blue[16];
1961
1962 static struct fb_cmap palette_cmap = {
1963         0, 16, palette_red, palette_green, palette_blue, NULL
1964 };
1965
1966 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
1967 {
1968         struct fb_info *info = registered_fb[(int) con2fb_map[vc->vc_num]];
1969         int i, j, k;
1970         u8 val;
1971
1972         if (!vc->vc_can_do_color
1973             || (!info->fbops->fb_blank && console_blanked))
1974                 return -EINVAL;
1975         for (i = j = 0; i < 16; i++) {
1976                 k = table[i];
1977                 val = vc->vc_palette[j++];
1978                 palette_red[k] = (val << 8) | val;
1979                 val = vc->vc_palette[j++];
1980                 palette_green[k] = (val << 8) | val;
1981                 val = vc->vc_palette[j++];
1982                 palette_blue[k] = (val << 8) | val;
1983         }
1984         if (info->var.bits_per_pixel <= 4)
1985                 palette_cmap.len = 1 << info->var.bits_per_pixel;
1986         else
1987                 palette_cmap.len = 16;
1988         palette_cmap.start = 0;
1989         return fb_set_cmap(&palette_cmap, 1, info);
1990 }
1991
1992 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
1993 {
1994         unsigned long p;
1995         int line;
1996         
1997         if (vc->vc_num != fg_console || !softback_lines)
1998                 return (u16 *) (vc->vc_origin + offset);
1999         line = offset / vc->vc_size_row;
2000         if (line >= softback_lines)
2001                 return (u16 *) (vc->vc_origin + offset -
2002                                 softback_lines * vc->vc_size_row);
2003         p = softback_curr + offset;
2004         if (p >= softback_end)
2005                 p += softback_buf - softback_end;
2006         return (u16 *) p;
2007 }
2008
2009 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2010                                  int *px, int *py)
2011 {
2012         unsigned long ret;
2013         int x, y;
2014
2015         if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2016                 unsigned long offset = (pos - vc->vc_origin) / 2;
2017
2018                 x = offset % vc->vc_cols;
2019                 y = offset / vc->vc_cols;
2020                 if (vc->vc_num == fg_console)
2021                         y += softback_lines;
2022                 ret = pos + (vc->vc_cols - x) * 2;
2023         } else if (vc->vc_num == fg_console && softback_lines) {
2024                 unsigned long offset = pos - softback_curr;
2025
2026                 if (pos < softback_curr)
2027                         offset += softback_end - softback_buf;
2028                 offset /= 2;
2029                 x = offset % vc->vc_cols;
2030                 y = offset / vc->vc_cols;
2031                 ret = pos + (vc->vc_cols - x) * 2;
2032                 if (ret == softback_end)
2033                         ret = softback_buf;
2034                 if (ret == softback_in)
2035                         ret = vc->vc_origin;
2036         } else {
2037                 /* Should not happen */
2038                 x = y = 0;
2039                 ret = vc->vc_origin;
2040         }
2041         if (px)
2042                 *px = x;
2043         if (py)
2044                 *py = y;
2045         return ret;
2046 }
2047
2048 /* As we might be inside of softback, we may work with non-contiguous buffer,
2049    that's why we have to use a separate routine. */
2050 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2051 {
2052         while (cnt--) {
2053                 u16 a = scr_readw(p);
2054                 if (!vc->vc_can_do_color)
2055                         a ^= 0x0800;
2056                 else if (vc->vc_hi_font_mask == 0x100)
2057                         a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2058                             (((a) & 0x0e00) << 4);
2059                 else
2060                         a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2061                             (((a) & 0x0700) << 4);
2062                 scr_writew(a, p++);
2063                 if (p == (u16 *) softback_end)
2064                         p = (u16 *) softback_buf;
2065                 if (p == (u16 *) softback_in)
2066                         p = (u16 *) vc->vc_origin;
2067         }
2068 }
2069
2070 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2071 {
2072         struct fb_info *info = registered_fb[(int) con2fb_map[fg_console]];
2073         struct display *p = &fb_display[fg_console];
2074         int offset, limit, scrollback_old;
2075
2076         if (softback_top) {
2077                 if (vc->vc_num != fg_console)
2078                         return 0;
2079                 if (vt_cons[vc->vc_num]->vc_mode != KD_TEXT || !lines)
2080                         return 0;
2081                 if (logo_shown >= 0) {
2082                         struct vc_data *conp2 = vc_cons[logo_shown].d;
2083
2084                         if (conp2->vc_top == logo_lines
2085                             && conp2->vc_bottom == conp2->vc_rows)
2086                                 conp2->vc_top = 0;
2087                         if (logo_shown == vc->vc_num) {
2088                                 unsigned long p, q;
2089                                 int i;
2090
2091                                 p = softback_in;
2092                                 q = vc->vc_origin +
2093                                     logo_lines * vc->vc_size_row;
2094                                 for (i = 0; i < logo_lines; i++) {
2095                                         if (p == softback_top)
2096                                                 break;
2097                                         if (p == softback_buf)
2098                                                 p = softback_end;
2099                                         p -= vc->vc_size_row;
2100                                         q -= vc->vc_size_row;
2101                                         scr_memcpyw((u16 *) q, (u16 *) p,
2102                                                     vc->vc_size_row);
2103                                 }
2104                                 softback_in = p;
2105                                 update_region(vc->vc_num, vc->vc_origin,
2106                                               logo_lines * vc->vc_cols);
2107                         }
2108                         logo_shown = -1;
2109                 }
2110                 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2111                 fbcon_redraw_softback(vc, p, lines);
2112                 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2113                 return 0;
2114         }
2115
2116         if (!scrollback_phys_max)
2117                 return -ENOSYS;
2118
2119         scrollback_old = scrollback_current;
2120         scrollback_current -= lines;
2121         if (scrollback_current < 0)
2122                 scrollback_current = 0;
2123         else if (scrollback_current > scrollback_max)
2124                 scrollback_current = scrollback_max;
2125         if (scrollback_current == scrollback_old)
2126                 return 0;
2127
2128         if (!info->fbops->fb_blank &&
2129             (console_blanked || vt_cons[vc->vc_num]->vc_mode != KD_TEXT
2130              || !lines))
2131                 return 0;
2132         fbcon_cursor(vc, CM_ERASE);
2133
2134         offset = p->yscroll - scrollback_current;
2135         limit = p->vrows;
2136         switch (p->scrollmode && __SCROLL_YMASK) {
2137         case __SCROLL_YWRAP:
2138                 info->var.vmode |= FB_VMODE_YWRAP;
2139                 break;
2140         case __SCROLL_YPAN:
2141                 limit -= vc->vc_rows;
2142                 info->var.vmode &= ~FB_VMODE_YWRAP;
2143                 break;
2144         }
2145         if (offset < 0)
2146                 offset += limit;
2147         else if (offset >= limit)
2148                 offset -= limit;
2149         info->var.xoffset = 0;
2150         info->var.yoffset = offset * vc->vc_font.height;
2151         update_var(vc->vc_num, info);
2152         if (!scrollback_current)
2153                 fbcon_cursor(vc, CM_DRAW);
2154         return 0;
2155 }
2156
2157 static int fbcon_set_origin(struct vc_data *vc)
2158 {
2159         if (softback_lines && !console_blanked)
2160                 fbcon_scrolldelta(vc, softback_lines);
2161         return 0;
2162 }
2163
2164 static void fbcon_suspended(struct fb_info *info)
2165 {
2166         /* Clear cursor, restore saved data */
2167         info->cursor.enable = 0;
2168         info->fbops->fb_cursor(info, &info->cursor);
2169 }
2170
2171 static void fbcon_resumed(struct fb_info *info)
2172 {
2173         struct vc_data *vc;
2174
2175         if (info->currcon < 0)
2176                 return;
2177         vc = vc_cons[info->currcon].d;
2178
2179         update_screen(vc->vc_num);
2180 }
2181 static int fbcon_event_notify(struct notifier_block *self, 
2182                               unsigned long action, void *data)
2183 {
2184         struct fb_info *info = (struct fb_info *) data;
2185
2186         switch(action) {
2187         case FB_EVENT_SUSPEND:
2188                 fbcon_suspended(info);
2189                 break;
2190         case FB_EVENT_RESUME:
2191                 fbcon_resumed(info);
2192                 break;
2193         }
2194         return 0;
2195 }
2196
2197 /*
2198  *  The console `switch' structure for the frame buffer based console
2199  */
2200
2201 const struct consw fb_con = {
2202         .owner                  = THIS_MODULE,
2203         .con_startup            = fbcon_startup,
2204         .con_init               = fbcon_init,
2205         .con_deinit             = fbcon_deinit,
2206         .con_clear              = fbcon_clear,
2207         .con_putc               = fbcon_putc,
2208         .con_putcs              = fbcon_putcs,
2209         .con_cursor             = fbcon_cursor,
2210         .con_scroll             = fbcon_scroll,
2211         .con_bmove              = fbcon_bmove,
2212         .con_switch             = fbcon_switch,
2213         .con_blank              = fbcon_blank,
2214         .con_font_op            = fbcon_font_op,
2215         .con_set_palette        = fbcon_set_palette,
2216         .con_scrolldelta        = fbcon_scrolldelta,
2217         .con_set_origin         = fbcon_set_origin,
2218         .con_invert_region      = fbcon_invert_region,
2219         .con_screen_pos         = fbcon_screen_pos,
2220         .con_getxy              = fbcon_getxy,
2221         .con_resize             = fbcon_resize,
2222 };
2223
2224 static struct notifier_block fbcon_event_notifer = {
2225         .notifier_call  = fbcon_event_notify,
2226 };
2227
2228 static int fbcon_event_notifier_registered;
2229
2230 int __init fb_console_init(void)
2231 {
2232         int err;
2233
2234         if (!num_registered_fb)
2235                 return -ENODEV;
2236
2237         err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
2238                                 fbcon_is_default);
2239         if (err)
2240                 return err;
2241
2242         acquire_console_sem();
2243         if (!fbcon_event_notifier_registered) {
2244                 fb_register_client(&fbcon_event_notifer);
2245                 fbcon_event_notifier_registered = 1;
2246         } 
2247         release_console_sem();
2248         return 0;
2249 }
2250
2251 #ifdef MODULE
2252
2253 void __exit fb_console_exit(void)
2254 {
2255         acquire_console_sem();
2256         if (fbcon_event_notifier_registered) {
2257                 fb_unregister_client(&fbcon_event_notifer);
2258                 fbcon_event_notifier_registered = 0;
2259         }
2260         release_console_sem();
2261         give_up_console(&fb_con);
2262 }       
2263
2264 module_init(fb_console_init);
2265 module_exit(fb_console_exit);
2266
2267 #endif
2268
2269 /*
2270  *  Visible symbols for modules
2271  */
2272
2273 EXPORT_SYMBOL(fb_display);
2274 EXPORT_SYMBOL(fb_con);
2275
2276 MODULE_LICENSE("GPL");