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