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