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