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