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