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