]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/video/console/fbcon.c
Merge tag 'md/4.3-rc6-fixes' of git://neil.brown.name/md
[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         ops->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms);
1097         p->con_rotate = initial_rotation;
1098         set_blitting_type(vc, info);
1099
1100         cols = vc->vc_cols;
1101         rows = vc->vc_rows;
1102         new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1103         new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1104         new_cols /= vc->vc_font.width;
1105         new_rows /= vc->vc_font.height;
1106
1107         /*
1108          * We must always set the mode. The mode of the previous console
1109          * driver could be in the same resolution but we are using different
1110          * hardware so we have to initialize the hardware.
1111          *
1112          * We need to do it in fbcon_init() to prevent screen corruption.
1113          */
1114         if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
1115                 if (info->fbops->fb_set_par &&
1116                     !(ops->flags & FBCON_FLAGS_INIT)) {
1117                         ret = info->fbops->fb_set_par(info);
1118
1119                         if (ret)
1120                                 printk(KERN_ERR "fbcon_init: detected "
1121                                         "unhandled fb_set_par error, "
1122                                         "error code %d\n", ret);
1123                 }
1124
1125                 ops->flags |= FBCON_FLAGS_INIT;
1126         }
1127
1128         ops->graphics = 0;
1129
1130         if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1131             !(cap & FBINFO_HWACCEL_DISABLED))
1132                 p->scrollmode = SCROLL_MOVE;
1133         else /* default to something safe */
1134                 p->scrollmode = SCROLL_REDRAW;
1135
1136         /*
1137          *  ++guenther: console.c:vc_allocate() relies on initializing
1138          *  vc_{cols,rows}, but we must not set those if we are only
1139          *  resizing the console.
1140          */
1141         if (init) {
1142                 vc->vc_cols = new_cols;
1143                 vc->vc_rows = new_rows;
1144         } else
1145                 vc_resize(vc, new_cols, new_rows);
1146
1147         if (logo)
1148                 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1149
1150         if (vc == svc && softback_buf)
1151                 fbcon_update_softback(vc);
1152
1153         if (ops->rotate_font && ops->rotate_font(info, vc)) {
1154                 ops->rotate = FB_ROTATE_UR;
1155                 set_blitting_type(vc, info);
1156         }
1157
1158         ops->p = &fb_display[fg_console];
1159 }
1160
1161 static void fbcon_free_font(struct display *p, bool freefont)
1162 {
1163         if (freefont && p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1164                 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1165         p->fontdata = NULL;
1166         p->userfont = 0;
1167 }
1168
1169 static void fbcon_deinit(struct vc_data *vc)
1170 {
1171         struct display *p = &fb_display[vc->vc_num];
1172         struct fb_info *info;
1173         struct fbcon_ops *ops;
1174         int idx;
1175         bool free_font = true;
1176
1177         idx = con2fb_map[vc->vc_num];
1178
1179         if (idx == -1)
1180                 goto finished;
1181
1182         info = registered_fb[idx];
1183
1184         if (!info)
1185                 goto finished;
1186
1187         if (info->flags & FBINFO_MISC_FIRMWARE)
1188                 free_font = false;
1189         ops = info->fbcon_par;
1190
1191         if (!ops)
1192                 goto finished;
1193
1194         if (CON_IS_VISIBLE(vc))
1195                 fbcon_del_cursor_timer(info);
1196
1197         ops->flags &= ~FBCON_FLAGS_INIT;
1198 finished:
1199
1200         fbcon_free_font(p, free_font);
1201         if (free_font)
1202                 vc->vc_font.data = NULL;
1203
1204         if (!con_is_bound(&fb_con))
1205                 fbcon_exit();
1206
1207         return;
1208 }
1209
1210 /* ====================================================================== */
1211
1212 /*  fbcon_XXX routines - interface used by the world
1213  *
1214  *  This system is now divided into two levels because of complications
1215  *  caused by hardware scrolling. Top level functions:
1216  *
1217  *      fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1218  *
1219  *  handles y values in range [0, scr_height-1] that correspond to real
1220  *  screen positions. y_wrap shift means that first line of bitmap may be
1221  *  anywhere on this display. These functions convert lineoffsets to
1222  *  bitmap offsets and deal with the wrap-around case by splitting blits.
1223  *
1224  *      fbcon_bmove_physical_8()    -- These functions fast implementations
1225  *      fbcon_clear_physical_8()    -- of original fbcon_XXX fns.
1226  *      fbcon_putc_physical_8()     -- (font width != 8) may be added later
1227  *
1228  *  WARNING:
1229  *
1230  *  At the moment fbcon_putc() cannot blit across vertical wrap boundary
1231  *  Implies should only really hardware scroll in rows. Only reason for
1232  *  restriction is simplicity & efficiency at the moment.
1233  */
1234
1235 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1236                         int width)
1237 {
1238         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1239         struct fbcon_ops *ops = info->fbcon_par;
1240
1241         struct display *p = &fb_display[vc->vc_num];
1242         u_int y_break;
1243
1244         if (fbcon_is_inactive(vc, info))
1245                 return;
1246
1247         if (!height || !width)
1248                 return;
1249
1250         if (sy < vc->vc_top && vc->vc_top == logo_lines) {
1251                 vc->vc_top = 0;
1252                 /*
1253                  * If the font dimensions are not an integral of the display
1254                  * dimensions then the ops->clear below won't end up clearing
1255                  * the margins.  Call clear_margins here in case the logo
1256                  * bitmap stretched into the margin area.
1257                  */
1258                 fbcon_clear_margins(vc, 0);
1259         }
1260
1261         /* Split blits that cross physical y_wrap boundary */
1262
1263         y_break = p->vrows - p->yscroll;
1264         if (sy < y_break && sy + height - 1 >= y_break) {
1265                 u_int b = y_break - sy;
1266                 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1267                 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1268                                  width);
1269         } else
1270                 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1271 }
1272
1273 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1274                         int count, int ypos, int xpos)
1275 {
1276         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1277         struct display *p = &fb_display[vc->vc_num];
1278         struct fbcon_ops *ops = info->fbcon_par;
1279
1280         if (!fbcon_is_inactive(vc, info))
1281                 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1282                            get_color(vc, info, scr_readw(s), 1),
1283                            get_color(vc, info, scr_readw(s), 0));
1284 }
1285
1286 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1287 {
1288         unsigned short chr;
1289
1290         scr_writew(c, &chr);
1291         fbcon_putcs(vc, &chr, 1, ypos, xpos);
1292 }
1293
1294 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1295 {
1296         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1297         struct fbcon_ops *ops = info->fbcon_par;
1298
1299         if (!fbcon_is_inactive(vc, info))
1300                 ops->clear_margins(vc, info, bottom_only);
1301 }
1302
1303 static void fbcon_cursor(struct vc_data *vc, int mode)
1304 {
1305         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1306         struct fbcon_ops *ops = info->fbcon_par;
1307         int y;
1308         int c = scr_readw((u16 *) vc->vc_pos);
1309
1310         ops->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms);
1311
1312         if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1313                 return;
1314
1315         if (vc->vc_cursor_type & 0x10)
1316                 fbcon_del_cursor_timer(info);
1317         else
1318                 fbcon_add_cursor_timer(info);
1319
1320         ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1321         if (mode & CM_SOFTBACK) {
1322                 mode &= ~CM_SOFTBACK;
1323                 y = softback_lines;
1324         } else {
1325                 if (softback_lines)
1326                         fbcon_set_origin(vc);
1327                 y = 0;
1328         }
1329
1330         ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1331                     get_color(vc, info, c, 0));
1332 }
1333
1334 static int scrollback_phys_max = 0;
1335 static int scrollback_max = 0;
1336 static int scrollback_current = 0;
1337
1338 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1339                            int unit)
1340 {
1341         struct display *p, *t;
1342         struct vc_data **default_mode, *vc;
1343         struct vc_data *svc;
1344         struct fbcon_ops *ops = info->fbcon_par;
1345         int rows, cols, charcnt = 256;
1346
1347         p = &fb_display[unit];
1348
1349         if (var_to_display(p, var, info))
1350                 return;
1351
1352         vc = vc_cons[unit].d;
1353
1354         if (!vc)
1355                 return;
1356
1357         default_mode = vc->vc_display_fg;
1358         svc = *default_mode;
1359         t = &fb_display[svc->vc_num];
1360
1361         if (!vc->vc_font.data) {
1362                 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1363                 vc->vc_font.width = (*default_mode)->vc_font.width;
1364                 vc->vc_font.height = (*default_mode)->vc_font.height;
1365                 p->userfont = t->userfont;
1366                 if (p->userfont)
1367                         REFCOUNT(p->fontdata)++;
1368         }
1369         if (p->userfont)
1370                 charcnt = FNTCHARCNT(p->fontdata);
1371
1372         var->activate = FB_ACTIVATE_NOW;
1373         info->var.activate = var->activate;
1374         var->yoffset = info->var.yoffset;
1375         var->xoffset = info->var.xoffset;
1376         fb_set_var(info, var);
1377         ops->var = info->var;
1378         vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1379         vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1380         if (charcnt == 256) {
1381                 vc->vc_hi_font_mask = 0;
1382         } else {
1383                 vc->vc_hi_font_mask = 0x100;
1384                 if (vc->vc_can_do_color)
1385                         vc->vc_complement_mask <<= 1;
1386         }
1387
1388         if (!*svc->vc_uni_pagedir_loc)
1389                 con_set_default_unimap(svc);
1390         if (!*vc->vc_uni_pagedir_loc)
1391                 con_copy_unimap(vc, svc);
1392
1393         cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1394         rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1395         cols /= vc->vc_font.width;
1396         rows /= vc->vc_font.height;
1397         vc_resize(vc, cols, rows);
1398
1399         if (CON_IS_VISIBLE(vc)) {
1400                 update_screen(vc);
1401                 if (softback_buf)
1402                         fbcon_update_softback(vc);
1403         }
1404 }
1405
1406 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1407 {
1408         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1409         struct fbcon_ops *ops = info->fbcon_par;
1410         struct display *p = &fb_display[vc->vc_num];
1411         
1412         p->yscroll += count;
1413         if (p->yscroll >= p->vrows)     /* Deal with wrap */
1414                 p->yscroll -= p->vrows;
1415         ops->var.xoffset = 0;
1416         ops->var.yoffset = p->yscroll * vc->vc_font.height;
1417         ops->var.vmode |= FB_VMODE_YWRAP;
1418         ops->update_start(info);
1419         scrollback_max += count;
1420         if (scrollback_max > scrollback_phys_max)
1421                 scrollback_max = scrollback_phys_max;
1422         scrollback_current = 0;
1423 }
1424
1425 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1426 {
1427         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1428         struct fbcon_ops *ops = info->fbcon_par;
1429         struct display *p = &fb_display[vc->vc_num];
1430         
1431         p->yscroll -= count;
1432         if (p->yscroll < 0)     /* Deal with wrap */
1433                 p->yscroll += p->vrows;
1434         ops->var.xoffset = 0;
1435         ops->var.yoffset = p->yscroll * vc->vc_font.height;
1436         ops->var.vmode |= FB_VMODE_YWRAP;
1437         ops->update_start(info);
1438         scrollback_max -= count;
1439         if (scrollback_max < 0)
1440                 scrollback_max = 0;
1441         scrollback_current = 0;
1442 }
1443
1444 static __inline__ void ypan_up(struct vc_data *vc, int count)
1445 {
1446         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1447         struct display *p = &fb_display[vc->vc_num];
1448         struct fbcon_ops *ops = info->fbcon_par;
1449
1450         p->yscroll += count;
1451         if (p->yscroll > p->vrows - vc->vc_rows) {
1452                 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1453                             0, 0, 0, vc->vc_rows, vc->vc_cols);
1454                 p->yscroll -= p->vrows - vc->vc_rows;
1455         }
1456
1457         ops->var.xoffset = 0;
1458         ops->var.yoffset = p->yscroll * vc->vc_font.height;
1459         ops->var.vmode &= ~FB_VMODE_YWRAP;
1460         ops->update_start(info);
1461         fbcon_clear_margins(vc, 1);
1462         scrollback_max += count;
1463         if (scrollback_max > scrollback_phys_max)
1464                 scrollback_max = scrollback_phys_max;
1465         scrollback_current = 0;
1466 }
1467
1468 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1469 {
1470         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1471         struct fbcon_ops *ops = info->fbcon_par;
1472         struct display *p = &fb_display[vc->vc_num];
1473
1474         p->yscroll += count;
1475
1476         if (p->yscroll > p->vrows - vc->vc_rows) {
1477                 p->yscroll -= p->vrows - vc->vc_rows;
1478                 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1479         }
1480
1481         ops->var.xoffset = 0;
1482         ops->var.yoffset = p->yscroll * vc->vc_font.height;
1483         ops->var.vmode &= ~FB_VMODE_YWRAP;
1484         ops->update_start(info);
1485         fbcon_clear_margins(vc, 1);
1486         scrollback_max += count;
1487         if (scrollback_max > scrollback_phys_max)
1488                 scrollback_max = scrollback_phys_max;
1489         scrollback_current = 0;
1490 }
1491
1492 static __inline__ void ypan_down(struct vc_data *vc, int count)
1493 {
1494         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1495         struct display *p = &fb_display[vc->vc_num];
1496         struct fbcon_ops *ops = info->fbcon_par;
1497         
1498         p->yscroll -= count;
1499         if (p->yscroll < 0) {
1500                 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1501                             0, vc->vc_rows, vc->vc_cols);
1502                 p->yscroll += p->vrows - vc->vc_rows;
1503         }
1504
1505         ops->var.xoffset = 0;
1506         ops->var.yoffset = p->yscroll * vc->vc_font.height;
1507         ops->var.vmode &= ~FB_VMODE_YWRAP;
1508         ops->update_start(info);
1509         fbcon_clear_margins(vc, 1);
1510         scrollback_max -= count;
1511         if (scrollback_max < 0)
1512                 scrollback_max = 0;
1513         scrollback_current = 0;
1514 }
1515
1516 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1517 {
1518         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1519         struct fbcon_ops *ops = info->fbcon_par;
1520         struct display *p = &fb_display[vc->vc_num];
1521
1522         p->yscroll -= count;
1523
1524         if (p->yscroll < 0) {
1525                 p->yscroll += p->vrows - vc->vc_rows;
1526                 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1527         }
1528
1529         ops->var.xoffset = 0;
1530         ops->var.yoffset = p->yscroll * vc->vc_font.height;
1531         ops->var.vmode &= ~FB_VMODE_YWRAP;
1532         ops->update_start(info);
1533         fbcon_clear_margins(vc, 1);
1534         scrollback_max -= count;
1535         if (scrollback_max < 0)
1536                 scrollback_max = 0;
1537         scrollback_current = 0;
1538 }
1539
1540 static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1541                                   long delta)
1542 {
1543         int count = vc->vc_rows;
1544         unsigned short *d, *s;
1545         unsigned long n;
1546         int line = 0;
1547
1548         d = (u16 *) softback_curr;
1549         if (d == (u16 *) softback_in)
1550                 d = (u16 *) vc->vc_origin;
1551         n = softback_curr + delta * vc->vc_size_row;
1552         softback_lines -= delta;
1553         if (delta < 0) {
1554                 if (softback_curr < softback_top && n < softback_buf) {
1555                         n += softback_end - softback_buf;
1556                         if (n < softback_top) {
1557                                 softback_lines -=
1558                                     (softback_top - n) / vc->vc_size_row;
1559                                 n = softback_top;
1560                         }
1561                 } else if (softback_curr >= softback_top
1562                            && n < softback_top) {
1563                         softback_lines -=
1564                             (softback_top - n) / vc->vc_size_row;
1565                         n = softback_top;
1566                 }
1567         } else {
1568                 if (softback_curr > softback_in && n >= softback_end) {
1569                         n += softback_buf - softback_end;
1570                         if (n > softback_in) {
1571                                 n = softback_in;
1572                                 softback_lines = 0;
1573                         }
1574                 } else if (softback_curr <= softback_in && n > softback_in) {
1575                         n = softback_in;
1576                         softback_lines = 0;
1577                 }
1578         }
1579         if (n == softback_curr)
1580                 return;
1581         softback_curr = n;
1582         s = (u16 *) softback_curr;
1583         if (s == (u16 *) softback_in)
1584                 s = (u16 *) vc->vc_origin;
1585         while (count--) {
1586                 unsigned short *start;
1587                 unsigned short *le;
1588                 unsigned short c;
1589                 int x = 0;
1590                 unsigned short attr = 1;
1591
1592                 start = s;
1593                 le = advance_row(s, 1);
1594                 do {
1595                         c = scr_readw(s);
1596                         if (attr != (c & 0xff00)) {
1597                                 attr = c & 0xff00;
1598                                 if (s > start) {
1599                                         fbcon_putcs(vc, start, s - start,
1600                                                     line, x);
1601                                         x += s - start;
1602                                         start = s;
1603                                 }
1604                         }
1605                         if (c == scr_readw(d)) {
1606                                 if (s > start) {
1607                                         fbcon_putcs(vc, start, s - start,
1608                                                     line, x);
1609                                         x += s - start + 1;
1610                                         start = s + 1;
1611                                 } else {
1612                                         x++;
1613                                         start++;
1614                                 }
1615                         }
1616                         s++;
1617                         d++;
1618                 } while (s < le);
1619                 if (s > start)
1620                         fbcon_putcs(vc, start, s - start, line, x);
1621                 line++;
1622                 if (d == (u16 *) softback_end)
1623                         d = (u16 *) softback_buf;
1624                 if (d == (u16 *) softback_in)
1625                         d = (u16 *) vc->vc_origin;
1626                 if (s == (u16 *) softback_end)
1627                         s = (u16 *) softback_buf;
1628                 if (s == (u16 *) softback_in)
1629                         s = (u16 *) vc->vc_origin;
1630         }
1631 }
1632
1633 static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1634                               int line, int count, int dy)
1635 {
1636         unsigned short *s = (unsigned short *)
1637                 (vc->vc_origin + vc->vc_size_row * line);
1638
1639         while (count--) {
1640                 unsigned short *start = s;
1641                 unsigned short *le = advance_row(s, 1);
1642                 unsigned short c;
1643                 int x = 0;
1644                 unsigned short attr = 1;
1645
1646                 do {
1647                         c = scr_readw(s);
1648                         if (attr != (c & 0xff00)) {
1649                                 attr = c & 0xff00;
1650                                 if (s > start) {
1651                                         fbcon_putcs(vc, start, s - start,
1652                                                     dy, x);
1653                                         x += s - start;
1654                                         start = s;
1655                                 }
1656                         }
1657                         console_conditional_schedule();
1658                         s++;
1659                 } while (s < le);
1660                 if (s > start)
1661                         fbcon_putcs(vc, start, s - start, dy, x);
1662                 console_conditional_schedule();
1663                 dy++;
1664         }
1665 }
1666
1667 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1668                         struct display *p, int line, int count, int ycount)
1669 {
1670         int offset = ycount * vc->vc_cols;
1671         unsigned short *d = (unsigned short *)
1672             (vc->vc_origin + vc->vc_size_row * line);
1673         unsigned short *s = d + offset;
1674         struct fbcon_ops *ops = info->fbcon_par;
1675
1676         while (count--) {
1677                 unsigned short *start = s;
1678                 unsigned short *le = advance_row(s, 1);
1679                 unsigned short c;
1680                 int x = 0;
1681
1682                 do {
1683                         c = scr_readw(s);
1684
1685                         if (c == scr_readw(d)) {
1686                                 if (s > start) {
1687                                         ops->bmove(vc, info, line + ycount, x,
1688                                                    line, x, 1, s-start);
1689                                         x += s - start + 1;
1690                                         start = s + 1;
1691                                 } else {
1692                                         x++;
1693                                         start++;
1694                                 }
1695                         }
1696
1697                         scr_writew(c, d);
1698                         console_conditional_schedule();
1699                         s++;
1700                         d++;
1701                 } while (s < le);
1702                 if (s > start)
1703                         ops->bmove(vc, info, line + ycount, x, line, x, 1,
1704                                    s-start);
1705                 console_conditional_schedule();
1706                 if (ycount > 0)
1707                         line++;
1708                 else {
1709                         line--;
1710                         /* NOTE: We subtract two lines from these pointers */
1711                         s -= vc->vc_size_row;
1712                         d -= vc->vc_size_row;
1713                 }
1714         }
1715 }
1716
1717 static void fbcon_redraw(struct vc_data *vc, struct display *p,
1718                          int line, int count, int offset)
1719 {
1720         unsigned short *d = (unsigned short *)
1721             (vc->vc_origin + vc->vc_size_row * line);
1722         unsigned short *s = d + offset;
1723
1724         while (count--) {
1725                 unsigned short *start = s;
1726                 unsigned short *le = advance_row(s, 1);
1727                 unsigned short c;
1728                 int x = 0;
1729                 unsigned short attr = 1;
1730
1731                 do {
1732                         c = scr_readw(s);
1733                         if (attr != (c & 0xff00)) {
1734                                 attr = c & 0xff00;
1735                                 if (s > start) {
1736                                         fbcon_putcs(vc, start, s - start,
1737                                                     line, x);
1738                                         x += s - start;
1739                                         start = s;
1740                                 }
1741                         }
1742                         if (c == scr_readw(d)) {
1743                                 if (s > start) {
1744                                         fbcon_putcs(vc, start, s - start,
1745                                                      line, x);
1746                                         x += s - start + 1;
1747                                         start = s + 1;
1748                                 } else {
1749                                         x++;
1750                                         start++;
1751                                 }
1752                         }
1753                         scr_writew(c, d);
1754                         console_conditional_schedule();
1755                         s++;
1756                         d++;
1757                 } while (s < le);
1758                 if (s > start)
1759                         fbcon_putcs(vc, start, s - start, line, x);
1760                 console_conditional_schedule();
1761                 if (offset > 0)
1762                         line++;
1763                 else {
1764                         line--;
1765                         /* NOTE: We subtract two lines from these pointers */
1766                         s -= vc->vc_size_row;
1767                         d -= vc->vc_size_row;
1768                 }
1769         }
1770 }
1771
1772 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1773                                        int count)
1774 {
1775         unsigned short *p;
1776
1777         if (vc->vc_num != fg_console)
1778                 return;
1779         p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1780
1781         while (count) {
1782                 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1783                 count--;
1784                 p = advance_row(p, 1);
1785                 softback_in += vc->vc_size_row;
1786                 if (softback_in == softback_end)
1787                         softback_in = softback_buf;
1788                 if (softback_in == softback_top) {
1789                         softback_top += vc->vc_size_row;
1790                         if (softback_top == softback_end)
1791                                 softback_top = softback_buf;
1792                 }
1793         }
1794         softback_curr = softback_in;
1795 }
1796
1797 static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1798                         int count)
1799 {
1800         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1801         struct display *p = &fb_display[vc->vc_num];
1802         int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1803
1804         if (fbcon_is_inactive(vc, info))
1805                 return -EINVAL;
1806
1807         fbcon_cursor(vc, CM_ERASE);
1808
1809         /*
1810          * ++Geert: Only use ywrap/ypan if the console is in text mode
1811          * ++Andrew: Only use ypan on hardware text mode when scrolling the
1812          *           whole screen (prevents flicker).
1813          */
1814
1815         switch (dir) {
1816         case SM_UP:
1817                 if (count > vc->vc_rows)        /* Maximum realistic size */
1818                         count = vc->vc_rows;
1819                 if (softback_top)
1820                         fbcon_softback_note(vc, t, count);
1821                 if (logo_shown >= 0)
1822                         goto redraw_up;
1823                 switch (p->scrollmode) {
1824                 case SCROLL_MOVE:
1825                         fbcon_redraw_blit(vc, info, p, t, b - t - count,
1826                                      count);
1827                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1828                         scr_memsetw((unsigned short *) (vc->vc_origin +
1829                                                         vc->vc_size_row *
1830                                                         (b - count)),
1831                                     vc->vc_video_erase_char,
1832                                     vc->vc_size_row * count);
1833                         return 1;
1834                         break;
1835
1836                 case SCROLL_WRAP_MOVE:
1837                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1838                                 if (t > 0)
1839                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1840                                                     vc->vc_cols);
1841                                 ywrap_up(vc, count);
1842                                 if (vc->vc_rows - b > 0)
1843                                         fbcon_bmove(vc, b - count, 0, b, 0,
1844                                                     vc->vc_rows - b,
1845                                                     vc->vc_cols);
1846                         } else if (info->flags & FBINFO_READS_FAST)
1847                                 fbcon_bmove(vc, t + count, 0, t, 0,
1848                                             b - t - count, vc->vc_cols);
1849                         else
1850                                 goto redraw_up;
1851                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1852                         break;
1853
1854                 case SCROLL_PAN_REDRAW:
1855                         if ((p->yscroll + count <=
1856                              2 * (p->vrows - vc->vc_rows))
1857                             && ((!scroll_partial && (b - t == vc->vc_rows))
1858                                 || (scroll_partial
1859                                     && (b - t - count >
1860                                         3 * vc->vc_rows >> 2)))) {
1861                                 if (t > 0)
1862                                         fbcon_redraw_move(vc, p, 0, t, count);
1863                                 ypan_up_redraw(vc, t, count);
1864                                 if (vc->vc_rows - b > 0)
1865                                         fbcon_redraw_move(vc, p, b,
1866                                                           vc->vc_rows - b, b);
1867                         } else
1868                                 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1869                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1870                         break;
1871
1872                 case SCROLL_PAN_MOVE:
1873                         if ((p->yscroll + count <=
1874                              2 * (p->vrows - vc->vc_rows))
1875                             && ((!scroll_partial && (b - t == vc->vc_rows))
1876                                 || (scroll_partial
1877                                     && (b - t - count >
1878                                         3 * vc->vc_rows >> 2)))) {
1879                                 if (t > 0)
1880                                         fbcon_bmove(vc, 0, 0, count, 0, t,
1881                                                     vc->vc_cols);
1882                                 ypan_up(vc, count);
1883                                 if (vc->vc_rows - b > 0)
1884                                         fbcon_bmove(vc, b - count, 0, b, 0,
1885                                                     vc->vc_rows - b,
1886                                                     vc->vc_cols);
1887                         } else if (info->flags & FBINFO_READS_FAST)
1888                                 fbcon_bmove(vc, t + count, 0, t, 0,
1889                                             b - t - count, vc->vc_cols);
1890                         else
1891                                 goto redraw_up;
1892                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1893                         break;
1894
1895                 case SCROLL_REDRAW:
1896                       redraw_up:
1897                         fbcon_redraw(vc, p, t, b - t - count,
1898                                      count * vc->vc_cols);
1899                         fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1900                         scr_memsetw((unsigned short *) (vc->vc_origin +
1901                                                         vc->vc_size_row *
1902                                                         (b - count)),
1903                                     vc->vc_video_erase_char,
1904                                     vc->vc_size_row * count);
1905                         return 1;
1906                 }
1907                 break;
1908
1909         case SM_DOWN:
1910                 if (count > vc->vc_rows)        /* Maximum realistic size */
1911                         count = vc->vc_rows;
1912                 if (logo_shown >= 0)
1913                         goto redraw_down;
1914                 switch (p->scrollmode) {
1915                 case SCROLL_MOVE:
1916                         fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1917                                      -count);
1918                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1919                         scr_memsetw((unsigned short *) (vc->vc_origin +
1920                                                         vc->vc_size_row *
1921                                                         t),
1922                                     vc->vc_video_erase_char,
1923                                     vc->vc_size_row * count);
1924                         return 1;
1925                         break;
1926
1927                 case SCROLL_WRAP_MOVE:
1928                         if (b - t - count > 3 * vc->vc_rows >> 2) {
1929                                 if (vc->vc_rows - b > 0)
1930                                         fbcon_bmove(vc, b, 0, b - count, 0,
1931                                                     vc->vc_rows - b,
1932                                                     vc->vc_cols);
1933                                 ywrap_down(vc, count);
1934                                 if (t > 0)
1935                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1936                                                     vc->vc_cols);
1937                         } else if (info->flags & FBINFO_READS_FAST)
1938                                 fbcon_bmove(vc, t, 0, t + count, 0,
1939                                             b - t - count, vc->vc_cols);
1940                         else
1941                                 goto redraw_down;
1942                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1943                         break;
1944
1945                 case SCROLL_PAN_MOVE:
1946                         if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1947                             && ((!scroll_partial && (b - t == vc->vc_rows))
1948                                 || (scroll_partial
1949                                     && (b - t - count >
1950                                         3 * vc->vc_rows >> 2)))) {
1951                                 if (vc->vc_rows - b > 0)
1952                                         fbcon_bmove(vc, b, 0, b - count, 0,
1953                                                     vc->vc_rows - b,
1954                                                     vc->vc_cols);
1955                                 ypan_down(vc, count);
1956                                 if (t > 0)
1957                                         fbcon_bmove(vc, count, 0, 0, 0, t,
1958                                                     vc->vc_cols);
1959                         } else if (info->flags & FBINFO_READS_FAST)
1960                                 fbcon_bmove(vc, t, 0, t + count, 0,
1961                                             b - t - count, vc->vc_cols);
1962                         else
1963                                 goto redraw_down;
1964                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1965                         break;
1966
1967                 case SCROLL_PAN_REDRAW:
1968                         if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1969                             && ((!scroll_partial && (b - t == vc->vc_rows))
1970                                 || (scroll_partial
1971                                     && (b - t - count >
1972                                         3 * vc->vc_rows >> 2)))) {
1973                                 if (vc->vc_rows - b > 0)
1974                                         fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
1975                                                           b - count);
1976                                 ypan_down_redraw(vc, t, count);
1977                                 if (t > 0)
1978                                         fbcon_redraw_move(vc, p, count, t, 0);
1979                         } else
1980                                 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
1981                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1982                         break;
1983
1984                 case SCROLL_REDRAW:
1985                       redraw_down:
1986                         fbcon_redraw(vc, p, b - 1, b - t - count,
1987                                      -count * vc->vc_cols);
1988                         fbcon_clear(vc, t, 0, count, vc->vc_cols);
1989                         scr_memsetw((unsigned short *) (vc->vc_origin +
1990                                                         vc->vc_size_row *
1991                                                         t),
1992                                     vc->vc_video_erase_char,
1993                                     vc->vc_size_row * count);
1994                         return 1;
1995                 }
1996         }
1997         return 0;
1998 }
1999
2000
2001 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
2002                         int height, int width)
2003 {
2004         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2005         struct display *p = &fb_display[vc->vc_num];
2006         
2007         if (fbcon_is_inactive(vc, info))
2008                 return;
2009
2010         if (!width || !height)
2011                 return;
2012
2013         /*  Split blits that cross physical y_wrap case.
2014          *  Pathological case involves 4 blits, better to use recursive
2015          *  code rather than unrolled case
2016          *
2017          *  Recursive invocations don't need to erase the cursor over and
2018          *  over again, so we use fbcon_bmove_rec()
2019          */
2020         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2021                         p->vrows - p->yscroll);
2022 }
2023
2024 static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx, 
2025                             int dy, int dx, int height, int width, u_int y_break)
2026 {
2027         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2028         struct fbcon_ops *ops = info->fbcon_par;
2029         u_int b;
2030
2031         if (sy < y_break && sy + height > y_break) {
2032                 b = y_break - sy;
2033                 if (dy < sy) {  /* Avoid trashing self */
2034                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2035                                         y_break);
2036                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2037                                         height - b, width, y_break);
2038                 } else {
2039                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2040                                         height - b, width, y_break);
2041                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2042                                         y_break);
2043                 }
2044                 return;
2045         }
2046
2047         if (dy < y_break && dy + height > y_break) {
2048                 b = y_break - dy;
2049                 if (dy < sy) {  /* Avoid trashing self */
2050                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2051                                         y_break);
2052                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2053                                         height - b, width, y_break);
2054                 } else {
2055                         fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2056                                         height - b, width, y_break);
2057                         fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2058                                         y_break);
2059                 }
2060                 return;
2061         }
2062         ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2063                    height, width);
2064 }
2065
2066 static void updatescrollmode(struct display *p,
2067                                         struct fb_info *info,
2068                                         struct vc_data *vc)
2069 {
2070         struct fbcon_ops *ops = info->fbcon_par;
2071         int fh = vc->vc_font.height;
2072         int cap = info->flags;
2073         u16 t = 0;
2074         int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2075                                   info->fix.xpanstep);
2076         int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2077         int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2078         int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2079                                    info->var.xres_virtual);
2080         int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2081                 divides(ypan, vc->vc_font.height) && vyres > yres;
2082         int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2083                 divides(ywrap, vc->vc_font.height) &&
2084                 divides(vc->vc_font.height, vyres) &&
2085                 divides(vc->vc_font.height, yres);
2086         int reading_fast = cap & FBINFO_READS_FAST;
2087         int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2088                 !(cap & FBINFO_HWACCEL_DISABLED);
2089         int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2090                 !(cap & FBINFO_HWACCEL_DISABLED);
2091
2092         p->vrows = vyres/fh;
2093         if (yres > (fh * (vc->vc_rows + 1)))
2094                 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2095         if ((yres % fh) && (vyres % fh < yres % fh))
2096                 p->vrows--;
2097
2098         if (good_wrap || good_pan) {
2099                 if (reading_fast || fast_copyarea)
2100                         p->scrollmode = good_wrap ?
2101                                 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2102                 else
2103                         p->scrollmode = good_wrap ? SCROLL_REDRAW :
2104                                 SCROLL_PAN_REDRAW;
2105         } else {
2106                 if (reading_fast || (fast_copyarea && !fast_imageblit))
2107                         p->scrollmode = SCROLL_MOVE;
2108                 else
2109                         p->scrollmode = SCROLL_REDRAW;
2110         }
2111 }
2112
2113 static int fbcon_resize(struct vc_data *vc, unsigned int width, 
2114                         unsigned int height, unsigned int user)
2115 {
2116         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2117         struct fbcon_ops *ops = info->fbcon_par;
2118         struct display *p = &fb_display[vc->vc_num];
2119         struct fb_var_screeninfo var = info->var;
2120         int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2121
2122         virt_w = FBCON_SWAP(ops->rotate, width, height);
2123         virt_h = FBCON_SWAP(ops->rotate, height, width);
2124         virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2125                                  vc->vc_font.height);
2126         virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2127                                  vc->vc_font.width);
2128         var.xres = virt_w * virt_fw;
2129         var.yres = virt_h * virt_fh;
2130         x_diff = info->var.xres - var.xres;
2131         y_diff = info->var.yres - var.yres;
2132         if (x_diff < 0 || x_diff > virt_fw ||
2133             y_diff < 0 || y_diff > virt_fh) {
2134                 const struct fb_videomode *mode;
2135
2136                 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2137                 mode = fb_find_best_mode(&var, &info->modelist);
2138                 if (mode == NULL)
2139                         return -EINVAL;
2140                 display_to_var(&var, p);
2141                 fb_videomode_to_var(&var, mode);
2142
2143                 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2144                         return -EINVAL;
2145
2146                 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2147                 if (CON_IS_VISIBLE(vc)) {
2148                         var.activate = FB_ACTIVATE_NOW |
2149                                 FB_ACTIVATE_FORCE;
2150                         fb_set_var(info, &var);
2151                 }
2152                 var_to_display(p, &info->var, info);
2153                 ops->var = info->var;
2154         }
2155         updatescrollmode(p, info, vc);
2156         return 0;
2157 }
2158
2159 static int fbcon_switch(struct vc_data *vc)
2160 {
2161         struct fb_info *info, *old_info = NULL;
2162         struct fbcon_ops *ops;
2163         struct display *p = &fb_display[vc->vc_num];
2164         struct fb_var_screeninfo var;
2165         int i, ret, prev_console, charcnt = 256;
2166
2167         info = registered_fb[con2fb_map[vc->vc_num]];
2168         ops = info->fbcon_par;
2169
2170         if (softback_top) {
2171                 if (softback_lines)
2172                         fbcon_set_origin(vc);
2173                 softback_top = softback_curr = softback_in = softback_buf;
2174                 softback_lines = 0;
2175                 fbcon_update_softback(vc);
2176         }
2177
2178         if (logo_shown >= 0) {
2179                 struct vc_data *conp2 = vc_cons[logo_shown].d;
2180
2181                 if (conp2->vc_top == logo_lines
2182                     && conp2->vc_bottom == conp2->vc_rows)
2183                         conp2->vc_top = 0;
2184                 logo_shown = FBCON_LOGO_CANSHOW;
2185         }
2186
2187         prev_console = ops->currcon;
2188         if (prev_console != -1)
2189                 old_info = registered_fb[con2fb_map[prev_console]];
2190         /*
2191          * FIXME: If we have multiple fbdev's loaded, we need to
2192          * update all info->currcon.  Perhaps, we can place this
2193          * in a centralized structure, but this might break some
2194          * drivers.
2195          *
2196          * info->currcon = vc->vc_num;
2197          */
2198         for (i = 0; i < FB_MAX; i++) {
2199                 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
2200                         struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2201
2202                         o->currcon = vc->vc_num;
2203                 }
2204         }
2205         memset(&var, 0, sizeof(struct fb_var_screeninfo));
2206         display_to_var(&var, p);
2207         var.activate = FB_ACTIVATE_NOW;
2208
2209         /*
2210          * make sure we don't unnecessarily trip the memcmp()
2211          * in fb_set_var()
2212          */
2213         info->var.activate = var.activate;
2214         var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
2215         fb_set_var(info, &var);
2216         ops->var = info->var;
2217
2218         if (old_info != NULL && (old_info != info ||
2219                                  info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2220                 if (info->fbops->fb_set_par) {
2221                         ret = info->fbops->fb_set_par(info);
2222
2223                         if (ret)
2224                                 printk(KERN_ERR "fbcon_switch: detected "
2225                                         "unhandled fb_set_par error, "
2226                                         "error code %d\n", ret);
2227                 }
2228
2229                 if (old_info != info)
2230                         fbcon_del_cursor_timer(old_info);
2231         }
2232
2233         if (fbcon_is_inactive(vc, info) ||
2234             ops->blank_state != FB_BLANK_UNBLANK)
2235                 fbcon_del_cursor_timer(info);
2236         else
2237                 fbcon_add_cursor_timer(info);
2238
2239         set_blitting_type(vc, info);
2240         ops->cursor_reset = 1;
2241
2242         if (ops->rotate_font && ops->rotate_font(info, vc)) {
2243                 ops->rotate = FB_ROTATE_UR;
2244                 set_blitting_type(vc, info);
2245         }
2246
2247         vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2248         vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2249
2250         if (p->userfont)
2251                 charcnt = FNTCHARCNT(vc->vc_font.data);
2252
2253         if (charcnt > 256)
2254                 vc->vc_complement_mask <<= 1;
2255
2256         updatescrollmode(p, info, vc);
2257
2258         switch (p->scrollmode) {
2259         case SCROLL_WRAP_MOVE:
2260                 scrollback_phys_max = p->vrows - vc->vc_rows;
2261                 break;
2262         case SCROLL_PAN_MOVE:
2263         case SCROLL_PAN_REDRAW:
2264                 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2265                 if (scrollback_phys_max < 0)
2266                         scrollback_phys_max = 0;
2267                 break;
2268         default:
2269                 scrollback_phys_max = 0;
2270                 break;
2271         }
2272
2273         scrollback_max = 0;
2274         scrollback_current = 0;
2275
2276         if (!fbcon_is_inactive(vc, info)) {
2277             ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2278             ops->update_start(info);
2279         }
2280
2281         fbcon_set_palette(vc, color_table);     
2282         fbcon_clear_margins(vc, 0);
2283
2284         if (logo_shown == FBCON_LOGO_DRAW) {
2285
2286                 logo_shown = fg_console;
2287                 /* This is protected above by initmem_freed */
2288                 fb_show_logo(info, ops->rotate);
2289                 update_region(vc,
2290                               vc->vc_origin + vc->vc_size_row * vc->vc_top,
2291                               vc->vc_size_row * (vc->vc_bottom -
2292                                                  vc->vc_top) / 2);
2293                 return 0;
2294         }
2295         return 1;
2296 }
2297
2298 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2299                                 int blank)
2300 {
2301         struct fb_event event;
2302
2303         if (blank) {
2304                 unsigned short charmask = vc->vc_hi_font_mask ?
2305                         0x1ff : 0xff;
2306                 unsigned short oldc;
2307
2308                 oldc = vc->vc_video_erase_char;
2309                 vc->vc_video_erase_char &= charmask;
2310                 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2311                 vc->vc_video_erase_char = oldc;
2312         }
2313
2314
2315         if (!lock_fb_info(info))
2316                 return;
2317         event.info = info;
2318         event.data = &blank;
2319         fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
2320         unlock_fb_info(info);
2321 }
2322
2323 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2324 {
2325         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2326         struct fbcon_ops *ops = info->fbcon_par;
2327
2328         if (mode_switch) {
2329                 struct fb_var_screeninfo var = info->var;
2330
2331                 ops->graphics = 1;
2332
2333                 if (!blank) {
2334                         var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2335                         fb_set_var(info, &var);
2336                         ops->graphics = 0;
2337                         ops->var = info->var;
2338                 }
2339         }
2340
2341         if (!fbcon_is_inactive(vc, info)) {
2342                 if (ops->blank_state != blank) {
2343                         ops->blank_state = blank;
2344                         fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2345                         ops->cursor_flash = (!blank);
2346
2347                         if (!(info->flags & FBINFO_MISC_USEREVENT))
2348                                 if (fb_blank(info, blank))
2349                                         fbcon_generic_blank(vc, info, blank);
2350                 }
2351
2352                 if (!blank)
2353                         update_screen(vc);
2354         }
2355
2356         if (mode_switch || fbcon_is_inactive(vc, info) ||
2357             ops->blank_state != FB_BLANK_UNBLANK)
2358                 fbcon_del_cursor_timer(info);
2359         else
2360                 fbcon_add_cursor_timer(info);
2361
2362         return 0;
2363 }
2364
2365 static int fbcon_debug_enter(struct vc_data *vc)
2366 {
2367         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2368         struct fbcon_ops *ops = info->fbcon_par;
2369
2370         ops->save_graphics = ops->graphics;
2371         ops->graphics = 0;
2372         if (info->fbops->fb_debug_enter)
2373                 info->fbops->fb_debug_enter(info);
2374         fbcon_set_palette(vc, color_table);
2375         return 0;
2376 }
2377
2378 static int fbcon_debug_leave(struct vc_data *vc)
2379 {
2380         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2381         struct fbcon_ops *ops = info->fbcon_par;
2382
2383         ops->graphics = ops->save_graphics;
2384         if (info->fbops->fb_debug_leave)
2385                 info->fbops->fb_debug_leave(info);
2386         return 0;
2387 }
2388
2389 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2390 {
2391         u8 *fontdata = vc->vc_font.data;
2392         u8 *data = font->data;
2393         int i, j;
2394
2395         font->width = vc->vc_font.width;
2396         font->height = vc->vc_font.height;
2397         font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2398         if (!font->data)
2399                 return 0;
2400
2401         if (font->width <= 8) {
2402                 j = vc->vc_font.height;
2403                 for (i = 0; i < font->charcount; i++) {
2404                         memcpy(data, fontdata, j);
2405                         memset(data + j, 0, 32 - j);
2406                         data += 32;
2407                         fontdata += j;
2408                 }
2409         } else if (font->width <= 16) {
2410                 j = vc->vc_font.height * 2;
2411                 for (i = 0; i < font->charcount; i++) {
2412                         memcpy(data, fontdata, j);
2413                         memset(data + j, 0, 64 - j);
2414                         data += 64;
2415                         fontdata += j;
2416                 }
2417         } else if (font->width <= 24) {
2418                 for (i = 0; i < font->charcount; i++) {
2419                         for (j = 0; j < vc->vc_font.height; j++) {
2420                                 *data++ = fontdata[0];
2421                                 *data++ = fontdata[1];
2422                                 *data++ = fontdata[2];
2423                                 fontdata += sizeof(u32);
2424                         }
2425                         memset(data, 0, 3 * (32 - j));
2426                         data += 3 * (32 - j);
2427                 }
2428         } else {
2429                 j = vc->vc_font.height * 4;
2430                 for (i = 0; i < font->charcount; i++) {
2431                         memcpy(data, fontdata, j);
2432                         memset(data + j, 0, 128 - j);
2433                         data += 128;
2434                         fontdata += j;
2435                 }
2436         }
2437         return 0;
2438 }
2439
2440 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2441                              const u8 * data, int userfont)
2442 {
2443         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2444         struct fbcon_ops *ops = info->fbcon_par;
2445         struct display *p = &fb_display[vc->vc_num];
2446         int resize;
2447         int cnt;
2448         char *old_data = NULL;
2449
2450         if (CON_IS_VISIBLE(vc) && softback_lines)
2451                 fbcon_set_origin(vc);
2452
2453         resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2454         if (p->userfont)
2455                 old_data = vc->vc_font.data;
2456         if (userfont)
2457                 cnt = FNTCHARCNT(data);
2458         else
2459                 cnt = 256;
2460         vc->vc_font.data = (void *)(p->fontdata = data);
2461         if ((p->userfont = userfont))
2462                 REFCOUNT(data)++;
2463         vc->vc_font.width = w;
2464         vc->vc_font.height = h;
2465         if (vc->vc_hi_font_mask && cnt == 256) {
2466                 vc->vc_hi_font_mask = 0;
2467                 if (vc->vc_can_do_color) {
2468                         vc->vc_complement_mask >>= 1;
2469                         vc->vc_s_complement_mask >>= 1;
2470                 }
2471                         
2472                 /* ++Edmund: reorder the attribute bits */
2473                 if (vc->vc_can_do_color) {
2474                         unsigned short *cp =
2475                             (unsigned short *) vc->vc_origin;
2476                         int count = vc->vc_screenbuf_size / 2;
2477                         unsigned short c;
2478                         for (; count > 0; count--, cp++) {
2479                                 c = scr_readw(cp);
2480                                 scr_writew(((c & 0xfe00) >> 1) |
2481                                            (c & 0xff), cp);
2482                         }
2483                         c = vc->vc_video_erase_char;
2484                         vc->vc_video_erase_char =
2485                             ((c & 0xfe00) >> 1) | (c & 0xff);
2486                         vc->vc_attr >>= 1;
2487                 }
2488         } else if (!vc->vc_hi_font_mask && cnt == 512) {
2489                 vc->vc_hi_font_mask = 0x100;
2490                 if (vc->vc_can_do_color) {
2491                         vc->vc_complement_mask <<= 1;
2492                         vc->vc_s_complement_mask <<= 1;
2493                 }
2494                         
2495                 /* ++Edmund: reorder the attribute bits */
2496                 {
2497                         unsigned short *cp =
2498                             (unsigned short *) vc->vc_origin;
2499                         int count = vc->vc_screenbuf_size / 2;
2500                         unsigned short c;
2501                         for (; count > 0; count--, cp++) {
2502                                 unsigned short newc;
2503                                 c = scr_readw(cp);
2504                                 if (vc->vc_can_do_color)
2505                                         newc =
2506                                             ((c & 0xff00) << 1) | (c &
2507                                                                    0xff);
2508                                 else
2509                                         newc = c & ~0x100;
2510                                 scr_writew(newc, cp);
2511                         }
2512                         c = vc->vc_video_erase_char;
2513                         if (vc->vc_can_do_color) {
2514                                 vc->vc_video_erase_char =
2515                                     ((c & 0xff00) << 1) | (c & 0xff);
2516                                 vc->vc_attr <<= 1;
2517                         } else
2518                                 vc->vc_video_erase_char = c & ~0x100;
2519                 }
2520
2521         }
2522
2523         if (resize) {
2524                 int cols, rows;
2525
2526                 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2527                 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2528                 cols /= w;
2529                 rows /= h;
2530                 vc_resize(vc, cols, rows);
2531                 if (CON_IS_VISIBLE(vc) && softback_buf)
2532                         fbcon_update_softback(vc);
2533         } else if (CON_IS_VISIBLE(vc)
2534                    && vc->vc_mode == KD_TEXT) {
2535                 fbcon_clear_margins(vc, 0);
2536                 update_screen(vc);
2537         }
2538
2539         if (old_data && (--REFCOUNT(old_data) == 0))
2540                 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2541         return 0;
2542 }
2543
2544 static int fbcon_copy_font(struct vc_data *vc, int con)
2545 {
2546         struct display *od = &fb_display[con];
2547         struct console_font *f = &vc->vc_font;
2548
2549         if (od->fontdata == f->data)
2550                 return 0;       /* already the same font... */
2551         return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2552 }
2553
2554 /*
2555  *  User asked to set font; we are guaranteed that
2556  *      a) width and height are in range 1..32
2557  *      b) charcount does not exceed 512
2558  *  but lets not assume that, since someone might someday want to use larger
2559  *  fonts. And charcount of 512 is small for unicode support.
2560  *
2561  *  However, user space gives the font in 32 rows , regardless of
2562  *  actual font height. So a new API is needed if support for larger fonts
2563  *  is ever implemented.
2564  */
2565
2566 static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2567 {
2568         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2569         unsigned charcount = font->charcount;
2570         int w = font->width;
2571         int h = font->height;
2572         int size;
2573         int i, csum;
2574         u8 *new_data, *data = font->data;
2575         int pitch = (font->width+7) >> 3;
2576
2577         /* Is there a reason why fbconsole couldn't handle any charcount >256?
2578          * If not this check should be changed to charcount < 256 */
2579         if (charcount != 256 && charcount != 512)
2580                 return -EINVAL;
2581
2582         /* Make sure drawing engine can handle the font */
2583         if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2584             !(info->pixmap.blit_y & (1 << (font->height - 1))))
2585                 return -EINVAL;
2586
2587         /* Make sure driver can handle the font length */
2588         if (fbcon_invalid_charcount(info, charcount))
2589                 return -EINVAL;
2590
2591         size = h * pitch * charcount;
2592
2593         new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2594
2595         if (!new_data)
2596                 return -ENOMEM;
2597
2598         new_data += FONT_EXTRA_WORDS * sizeof(int);
2599         FNTSIZE(new_data) = size;
2600         FNTCHARCNT(new_data) = charcount;
2601         REFCOUNT(new_data) = 0; /* usage counter */
2602         for (i=0; i< charcount; i++) {
2603                 memcpy(new_data + i*h*pitch, data +  i*32*pitch, h*pitch);
2604         }
2605
2606         /* Since linux has a nice crc32 function use it for counting font
2607          * checksums. */
2608         csum = crc32(0, new_data, size);
2609
2610         FNTSUM(new_data) = csum;
2611         /* Check if the same font is on some other console already */
2612         for (i = first_fb_vc; i <= last_fb_vc; i++) {
2613                 struct vc_data *tmp = vc_cons[i].d;
2614                 
2615                 if (fb_display[i].userfont &&
2616                     fb_display[i].fontdata &&
2617                     FNTSUM(fb_display[i].fontdata) == csum &&
2618                     FNTSIZE(fb_display[i].fontdata) == size &&
2619                     tmp->vc_font.width == w &&
2620                     !memcmp(fb_display[i].fontdata, new_data, size)) {
2621                         kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2622                         new_data = (u8 *)fb_display[i].fontdata;
2623                         break;
2624                 }
2625         }
2626         return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2627 }
2628
2629 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2630 {
2631         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2632         const struct font_desc *f;
2633
2634         if (!name)
2635                 f = get_default_font(info->var.xres, info->var.yres,
2636                                      info->pixmap.blit_x, info->pixmap.blit_y);
2637         else if (!(f = find_font(name)))
2638                 return -ENOENT;
2639
2640         font->width = f->width;
2641         font->height = f->height;
2642         return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2643 }
2644
2645 static u16 palette_red[16];
2646 static u16 palette_green[16];
2647 static u16 palette_blue[16];
2648
2649 static struct fb_cmap palette_cmap = {
2650         0, 16, palette_red, palette_green, palette_blue, NULL
2651 };
2652
2653 static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2654 {
2655         struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2656         int i, j, k, depth;
2657         u8 val;
2658
2659         if (fbcon_is_inactive(vc, info))
2660                 return -EINVAL;
2661
2662         if (!CON_IS_VISIBLE(vc))
2663                 return 0;
2664
2665         depth = fb_get_color_depth(&info->var, &info->fix);
2666         if (depth > 3) {
2667                 for (i = j = 0; i < 16; i++) {
2668                         k = table[i];
2669                         val = vc->vc_palette[j++];
2670                         palette_red[k] = (val << 8) | val;
2671                         val = vc->vc_palette[j++];
2672                         palette_green[k] = (val << 8) | val;
2673                         val = vc->vc_palette[j++];
2674                         palette_blue[k] = (val << 8) | val;
2675                 }
2676                 palette_cmap.len = 16;
2677                 palette_cmap.start = 0;
2678         /*
2679          * If framebuffer is capable of less than 16 colors,
2680          * use default palette of fbcon.
2681          */
2682         } else
2683                 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2684
2685         return fb_set_cmap(&palette_cmap, info);
2686 }
2687
2688 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2689 {
2690         unsigned long p;
2691         int line;
2692         
2693         if (vc->vc_num != fg_console || !softback_lines)
2694                 return (u16 *) (vc->vc_origin + offset);
2695         line = offset / vc->vc_size_row;
2696         if (line >= softback_lines)
2697                 return (u16 *) (vc->vc_origin + offset -
2698                                 softback_lines * vc->vc_size_row);
2699         p = softback_curr + offset;
2700         if (p >= softback_end)
2701                 p += softback_buf - softback_end;
2702         return (u16 *) p;
2703 }
2704
2705 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2706                                  int *px, int *py)
2707 {
2708         unsigned long ret;
2709         int x, y;
2710
2711         if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2712                 unsigned long offset = (pos - vc->vc_origin) / 2;
2713
2714                 x = offset % vc->vc_cols;
2715                 y = offset / vc->vc_cols;
2716                 if (vc->vc_num == fg_console)
2717                         y += softback_lines;
2718                 ret = pos + (vc->vc_cols - x) * 2;
2719         } else if (vc->vc_num == fg_console && softback_lines) {
2720                 unsigned long offset = pos - softback_curr;
2721
2722                 if (pos < softback_curr)
2723                         offset += softback_end - softback_buf;
2724                 offset /= 2;
2725                 x = offset % vc->vc_cols;
2726                 y = offset / vc->vc_cols;
2727                 ret = pos + (vc->vc_cols - x) * 2;
2728                 if (ret == softback_end)
2729                         ret = softback_buf;
2730                 if (ret == softback_in)
2731                         ret = vc->vc_origin;
2732         } else {
2733                 /* Should not happen */
2734                 x = y = 0;
2735                 ret = vc->vc_origin;
2736         }
2737         if (px)
2738                 *px = x;
2739         if (py)
2740                 *py = y;
2741         return ret;
2742 }
2743
2744 /* As we might be inside of softback, we may work with non-contiguous buffer,
2745    that's why we have to use a separate routine. */
2746 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2747 {
2748         while (cnt--) {
2749                 u16 a = scr_readw(p);
2750                 if (!vc->vc_can_do_color)
2751                         a ^= 0x0800;
2752                 else if (vc->vc_hi_font_mask == 0x100)
2753                         a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2754                             (((a) & 0x0e00) << 4);
2755                 else
2756                         a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2757                             (((a) & 0x0700) << 4);
2758                 scr_writew(a, p++);
2759                 if (p == (u16 *) softback_end)
2760                         p = (u16 *) softback_buf;
2761                 if (p == (u16 *) softback_in)
2762                         p = (u16 *) vc->vc_origin;
2763         }
2764 }
2765
2766 static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2767 {
2768         struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2769         struct fbcon_ops *ops = info->fbcon_par;
2770         struct display *disp = &fb_display[fg_console];
2771         int offset, limit, scrollback_old;
2772
2773         if (softback_top) {
2774                 if (vc->vc_num != fg_console)
2775                         return 0;
2776                 if (vc->vc_mode != KD_TEXT || !lines)
2777                         return 0;
2778                 if (logo_shown >= 0) {
2779                         struct vc_data *conp2 = vc_cons[logo_shown].d;
2780
2781                         if (conp2->vc_top == logo_lines
2782                             && conp2->vc_bottom == conp2->vc_rows)
2783                                 conp2->vc_top = 0;
2784                         if (logo_shown == vc->vc_num) {
2785                                 unsigned long p, q;
2786                                 int i;
2787
2788                                 p = softback_in;
2789                                 q = vc->vc_origin +
2790                                     logo_lines * vc->vc_size_row;
2791                                 for (i = 0; i < logo_lines; i++) {
2792                                         if (p == softback_top)
2793                                                 break;
2794                                         if (p == softback_buf)
2795                                                 p = softback_end;
2796                                         p -= vc->vc_size_row;
2797                                         q -= vc->vc_size_row;
2798                                         scr_memcpyw((u16 *) q, (u16 *) p,
2799                                                     vc->vc_size_row);
2800                                 }
2801                                 softback_in = softback_curr = p;
2802                                 update_region(vc, vc->vc_origin,
2803                                               logo_lines * vc->vc_cols);
2804                         }
2805                         logo_shown = FBCON_LOGO_CANSHOW;
2806                 }
2807                 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2808                 fbcon_redraw_softback(vc, disp, lines);
2809                 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2810                 return 0;
2811         }
2812
2813         if (!scrollback_phys_max)
2814                 return -ENOSYS;
2815
2816         scrollback_old = scrollback_current;
2817         scrollback_current -= lines;
2818         if (scrollback_current < 0)
2819                 scrollback_current = 0;
2820         else if (scrollback_current > scrollback_max)
2821                 scrollback_current = scrollback_max;
2822         if (scrollback_current == scrollback_old)
2823                 return 0;
2824
2825         if (fbcon_is_inactive(vc, info))
2826                 return 0;
2827
2828         fbcon_cursor(vc, CM_ERASE);
2829
2830         offset = disp->yscroll - scrollback_current;
2831         limit = disp->vrows;
2832         switch (disp->scrollmode) {
2833         case SCROLL_WRAP_MOVE:
2834                 info->var.vmode |= FB_VMODE_YWRAP;
2835                 break;
2836         case SCROLL_PAN_MOVE:
2837         case SCROLL_PAN_REDRAW:
2838                 limit -= vc->vc_rows;
2839                 info->var.vmode &= ~FB_VMODE_YWRAP;
2840                 break;
2841         }
2842         if (offset < 0)
2843                 offset += limit;
2844         else if (offset >= limit)
2845                 offset -= limit;
2846
2847         ops->var.xoffset = 0;
2848         ops->var.yoffset = offset * vc->vc_font.height;
2849         ops->update_start(info);
2850
2851         if (!scrollback_current)
2852                 fbcon_cursor(vc, CM_DRAW);
2853         return 0;
2854 }
2855
2856 static int fbcon_set_origin(struct vc_data *vc)
2857 {
2858         if (softback_lines)
2859                 fbcon_scrolldelta(vc, softback_lines);
2860         return 0;
2861 }
2862
2863 static void fbcon_suspended(struct fb_info *info)
2864 {
2865         struct vc_data *vc = NULL;
2866         struct fbcon_ops *ops = info->fbcon_par;
2867
2868         if (!ops || ops->currcon < 0)
2869                 return;
2870         vc = vc_cons[ops->currcon].d;
2871
2872         /* Clear cursor, restore saved data */
2873         fbcon_cursor(vc, CM_ERASE);
2874 }
2875
2876 static void fbcon_resumed(struct fb_info *info)
2877 {
2878         struct vc_data *vc;
2879         struct fbcon_ops *ops = info->fbcon_par;
2880
2881         if (!ops || ops->currcon < 0)
2882                 return;
2883         vc = vc_cons[ops->currcon].d;
2884
2885         update_screen(vc);
2886 }
2887
2888 static void fbcon_modechanged(struct fb_info *info)
2889 {
2890         struct fbcon_ops *ops = info->fbcon_par;
2891         struct vc_data *vc;
2892         struct display *p;
2893         int rows, cols;
2894
2895         if (!ops || ops->currcon < 0)
2896                 return;
2897         vc = vc_cons[ops->currcon].d;
2898         if (vc->vc_mode != KD_TEXT ||
2899             registered_fb[con2fb_map[ops->currcon]] != info)
2900                 return;
2901
2902         p = &fb_display[vc->vc_num];
2903         set_blitting_type(vc, info);
2904
2905         if (CON_IS_VISIBLE(vc)) {
2906                 var_to_display(p, &info->var, info);
2907                 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2908                 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2909                 cols /= vc->vc_font.width;
2910                 rows /= vc->vc_font.height;
2911                 vc_resize(vc, cols, rows);
2912                 updatescrollmode(p, info, vc);
2913                 scrollback_max = 0;
2914                 scrollback_current = 0;
2915
2916                 if (!fbcon_is_inactive(vc, info)) {
2917                     ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2918                     ops->update_start(info);
2919                 }
2920
2921                 fbcon_set_palette(vc, color_table);
2922                 update_screen(vc);
2923                 if (softback_buf)
2924                         fbcon_update_softback(vc);
2925         }
2926 }
2927
2928 static void fbcon_set_all_vcs(struct fb_info *info)
2929 {
2930         struct fbcon_ops *ops = info->fbcon_par;
2931         struct vc_data *vc;
2932         struct display *p;
2933         int i, rows, cols, fg = -1;
2934
2935         if (!ops || ops->currcon < 0)
2936                 return;
2937
2938         for (i = first_fb_vc; i <= last_fb_vc; i++) {
2939                 vc = vc_cons[i].d;
2940                 if (!vc || vc->vc_mode != KD_TEXT ||
2941                     registered_fb[con2fb_map[i]] != info)
2942                         continue;
2943
2944                 if (CON_IS_VISIBLE(vc)) {
2945                         fg = i;
2946                         continue;
2947                 }
2948
2949                 p = &fb_display[vc->vc_num];
2950                 set_blitting_type(vc, info);
2951                 var_to_display(p, &info->var, info);
2952                 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2953                 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2954                 cols /= vc->vc_font.width;
2955                 rows /= vc->vc_font.height;
2956                 vc_resize(vc, cols, rows);
2957         }
2958
2959         if (fg != -1)
2960                 fbcon_modechanged(info);
2961 }
2962
2963 static int fbcon_mode_deleted(struct fb_info *info,
2964                               struct fb_videomode *mode)
2965 {
2966         struct fb_info *fb_info;
2967         struct display *p;
2968         int i, j, found = 0;
2969
2970         /* before deletion, ensure that mode is not in use */
2971         for (i = first_fb_vc; i <= last_fb_vc; i++) {
2972                 j = con2fb_map[i];
2973                 if (j == -1)
2974                         continue;
2975                 fb_info = registered_fb[j];
2976                 if (fb_info != info)
2977                         continue;
2978                 p = &fb_display[i];
2979                 if (!p || !p->mode)
2980                         continue;
2981                 if (fb_mode_is_equal(p->mode, mode)) {
2982                         found = 1;
2983                         break;
2984                 }
2985         }
2986         return found;
2987 }
2988
2989 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
2990 static int fbcon_unbind(void)
2991 {
2992         int ret;
2993
2994         ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
2995                                 fbcon_is_default);
2996
2997         if (!ret)
2998                 fbcon_has_console_bind = 0;
2999
3000         return ret;
3001 }
3002 #else
3003 static inline int fbcon_unbind(void)
3004 {
3005         return -EINVAL;
3006 }
3007 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3008
3009 /* called with console_lock held */
3010 static int fbcon_fb_unbind(int idx)
3011 {
3012         int i, new_idx = -1, ret = 0;
3013
3014         if (!fbcon_has_console_bind)
3015                 return 0;
3016
3017         for (i = first_fb_vc; i <= last_fb_vc; i++) {
3018                 if (con2fb_map[i] != idx &&
3019                     con2fb_map[i] != -1) {
3020                         new_idx = i;
3021                         break;
3022                 }
3023         }
3024
3025         if (new_idx != -1) {
3026                 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3027                         if (con2fb_map[i] == idx)
3028                                 set_con2fb_map(i, new_idx, 0);
3029                 }
3030         } else {
3031                 struct fb_info *info = registered_fb[idx];
3032
3033                 /* This is sort of like set_con2fb_map, except it maps
3034                  * the consoles to no device and then releases the
3035                  * oldinfo to free memory and cancel the cursor blink
3036                  * timer. I can imagine this just becoming part of
3037                  * set_con2fb_map where new_idx is -1
3038                  */
3039                 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3040                         if (con2fb_map[i] == idx) {
3041                                 con2fb_map[i] = -1;
3042                                 if (!search_fb_in_map(idx)) {
3043                                         ret = con2fb_release_oldinfo(vc_cons[i].d,
3044                                                                      info, NULL, i,
3045                                                                      idx, 0);
3046                                         if (ret) {
3047                                                 con2fb_map[i] = idx;
3048                                                 return ret;
3049                                         }
3050                                 }
3051                         }
3052                 }
3053                 ret = fbcon_unbind();
3054         }
3055
3056         return ret;
3057 }
3058
3059 /* called with console_lock held */
3060 static int fbcon_fb_unregistered(struct fb_info *info)
3061 {
3062         int i, idx;
3063
3064         idx = info->node;
3065         for (i = first_fb_vc; i <= last_fb_vc; i++) {
3066                 if (con2fb_map[i] == idx)
3067                         con2fb_map[i] = -1;
3068         }
3069
3070         if (idx == info_idx) {
3071                 info_idx = -1;
3072
3073                 for (i = 0; i < FB_MAX; i++) {
3074                         if (registered_fb[i] != NULL) {
3075                                 info_idx = i;
3076                                 break;
3077                         }
3078                 }
3079         }
3080
3081         if (info_idx != -1) {
3082                 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3083                         if (con2fb_map[i] == -1)
3084                                 con2fb_map[i] = info_idx;
3085                 }
3086         }
3087
3088         if (primary_device == idx)
3089                 primary_device = -1;
3090
3091         if (!num_registered_fb)
3092                 do_unregister_con_driver(&fb_con);
3093
3094         return 0;
3095 }
3096
3097 /* called with console_lock held */
3098 static void fbcon_remap_all(int idx)
3099 {
3100         int i;
3101         for (i = first_fb_vc; i <= last_fb_vc; i++)
3102                 set_con2fb_map(i, idx, 0);
3103
3104         if (con_is_bound(&fb_con)) {
3105                 printk(KERN_INFO "fbcon: Remapping primary device, "
3106                        "fb%i, to tty %i-%i\n", idx,
3107                        first_fb_vc + 1, last_fb_vc + 1);
3108                 info_idx = idx;
3109         }
3110 }
3111
3112 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3113 static void fbcon_select_primary(struct fb_info *info)
3114 {
3115         if (!map_override && primary_device == -1 &&
3116             fb_is_primary_device(info)) {
3117                 int i;
3118
3119                 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3120                        info->fix.id, info->node);
3121                 primary_device = info->node;
3122
3123                 for (i = first_fb_vc; i <= last_fb_vc; i++)
3124                         con2fb_map_boot[i] = primary_device;
3125
3126                 if (con_is_bound(&fb_con)) {
3127                         printk(KERN_INFO "fbcon: Remapping primary device, "
3128                                "fb%i, to tty %i-%i\n", info->node,
3129                                first_fb_vc + 1, last_fb_vc + 1);
3130                         info_idx = primary_device;
3131                 }
3132         }
3133
3134 }
3135 #else
3136 static inline void fbcon_select_primary(struct fb_info *info)
3137 {
3138         return;
3139 }
3140 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3141
3142 /* called with console_lock held */
3143 static int fbcon_fb_registered(struct fb_info *info)
3144 {
3145         int ret = 0, i, idx;
3146
3147         idx = info->node;
3148         fbcon_select_primary(info);
3149
3150         if (info_idx == -1) {
3151                 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3152                         if (con2fb_map_boot[i] == idx) {
3153                                 info_idx = idx;
3154                                 break;
3155                         }
3156                 }
3157
3158                 if (info_idx != -1)
3159                         ret = do_fbcon_takeover(1);
3160         } else {
3161                 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3162                         if (con2fb_map_boot[i] == idx)
3163                                 set_con2fb_map(i, idx, 0);
3164                 }
3165         }
3166
3167         return ret;
3168 }
3169
3170 static void fbcon_fb_blanked(struct fb_info *info, int blank)
3171 {
3172         struct fbcon_ops *ops = info->fbcon_par;
3173         struct vc_data *vc;
3174
3175         if (!ops || ops->currcon < 0)
3176                 return;
3177
3178         vc = vc_cons[ops->currcon].d;
3179         if (vc->vc_mode != KD_TEXT ||
3180                         registered_fb[con2fb_map[ops->currcon]] != info)
3181                 return;
3182
3183         if (CON_IS_VISIBLE(vc)) {
3184                 if (blank)
3185                         do_blank_screen(0);
3186                 else
3187                         do_unblank_screen(0);
3188         }
3189         ops->blank_state = blank;
3190 }
3191
3192 static void fbcon_new_modelist(struct fb_info *info)
3193 {
3194         int i;
3195         struct vc_data *vc;
3196         struct fb_var_screeninfo var;
3197         const struct fb_videomode *mode;
3198
3199         for (i = first_fb_vc; i <= last_fb_vc; i++) {
3200                 if (registered_fb[con2fb_map[i]] != info)
3201                         continue;
3202                 if (!fb_display[i].mode)
3203                         continue;
3204                 vc = vc_cons[i].d;
3205                 display_to_var(&var, &fb_display[i]);
3206                 mode = fb_find_nearest_mode(fb_display[i].mode,
3207                                             &info->modelist);
3208                 fb_videomode_to_var(&var, mode);
3209                 fbcon_set_disp(info, &var, vc->vc_num);
3210         }
3211 }
3212
3213 static void fbcon_get_requirement(struct fb_info *info,
3214                                   struct fb_blit_caps *caps)
3215 {
3216         struct vc_data *vc;
3217         struct display *p;
3218
3219         if (caps->flags) {
3220                 int i, charcnt;
3221
3222                 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3223                         vc = vc_cons[i].d;
3224                         if (vc && vc->vc_mode == KD_TEXT &&
3225                             info->node == con2fb_map[i]) {
3226                                 p = &fb_display[i];
3227                                 caps->x |= 1 << (vc->vc_font.width - 1);
3228                                 caps->y |= 1 << (vc->vc_font.height - 1);
3229                                 charcnt = (p->userfont) ?
3230                                         FNTCHARCNT(p->fontdata) : 256;
3231                                 if (caps->len < charcnt)
3232                                         caps->len = charcnt;
3233                         }
3234                 }
3235         } else {
3236                 vc = vc_cons[fg_console].d;
3237
3238                 if (vc && vc->vc_mode == KD_TEXT &&
3239                     info->node == con2fb_map[fg_console]) {
3240                         p = &fb_display[fg_console];
3241                         caps->x = 1 << (vc->vc_font.width - 1);
3242                         caps->y = 1 << (vc->vc_font.height - 1);
3243                         caps->len = (p->userfont) ?
3244                                 FNTCHARCNT(p->fontdata) : 256;
3245                 }
3246         }
3247 }
3248
3249 static int fbcon_event_notify(struct notifier_block *self,
3250                               unsigned long action, void *data)
3251 {
3252         struct fb_event *event = data;
3253         struct fb_info *info = event->info;
3254         struct fb_videomode *mode;
3255         struct fb_con2fbmap *con2fb;
3256         struct fb_blit_caps *caps;
3257         int idx, ret = 0;
3258
3259         /*
3260          * ignore all events except driver registration and deregistration
3261          * if fbcon is not active
3262          */
3263         if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3264                                   action == FB_EVENT_FB_UNREGISTERED))
3265                 goto done;
3266
3267         switch(action) {
3268         case FB_EVENT_SUSPEND:
3269                 fbcon_suspended(info);
3270                 break;
3271         case FB_EVENT_RESUME:
3272                 fbcon_resumed(info);
3273                 break;
3274         case FB_EVENT_MODE_CHANGE:
3275                 fbcon_modechanged(info);
3276                 break;
3277         case FB_EVENT_MODE_CHANGE_ALL:
3278                 fbcon_set_all_vcs(info);
3279                 break;
3280         case FB_EVENT_MODE_DELETE:
3281                 mode = event->data;
3282                 ret = fbcon_mode_deleted(info, mode);
3283                 break;
3284         case FB_EVENT_FB_UNBIND:
3285                 idx = info->node;
3286                 ret = fbcon_fb_unbind(idx);
3287                 break;
3288         case FB_EVENT_FB_REGISTERED:
3289                 ret = fbcon_fb_registered(info);
3290                 break;
3291         case FB_EVENT_FB_UNREGISTERED:
3292                 ret = fbcon_fb_unregistered(info);
3293                 break;
3294         case FB_EVENT_SET_CONSOLE_MAP:
3295                 /* called with console lock held */
3296                 con2fb = event->data;
3297                 ret = set_con2fb_map(con2fb->console - 1,
3298                                      con2fb->framebuffer, 1);
3299                 break;
3300         case FB_EVENT_GET_CONSOLE_MAP:
3301                 con2fb = event->data;
3302                 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3303                 break;
3304         case FB_EVENT_BLANK:
3305                 fbcon_fb_blanked(info, *(int *)event->data);
3306                 break;
3307         case FB_EVENT_NEW_MODELIST:
3308                 fbcon_new_modelist(info);
3309                 break;
3310         case FB_EVENT_GET_REQ:
3311                 caps = event->data;
3312                 fbcon_get_requirement(info, caps);
3313                 break;
3314         case FB_EVENT_REMAP_ALL_CONSOLE:
3315                 idx = info->node;
3316                 fbcon_remap_all(idx);
3317                 break;
3318         }
3319 done:
3320         return ret;
3321 }
3322
3323 /*
3324  *  The console `switch' structure for the frame buffer based console
3325  */
3326
3327 static const struct consw fb_con = {
3328         .owner                  = THIS_MODULE,
3329         .con_startup            = fbcon_startup,
3330         .con_init               = fbcon_init,
3331         .con_deinit             = fbcon_deinit,
3332         .con_clear              = fbcon_clear,
3333         .con_putc               = fbcon_putc,
3334         .con_putcs              = fbcon_putcs,
3335         .con_cursor             = fbcon_cursor,
3336         .con_scroll             = fbcon_scroll,
3337         .con_bmove              = fbcon_bmove,
3338         .con_switch             = fbcon_switch,
3339         .con_blank              = fbcon_blank,
3340         .con_font_set           = fbcon_set_font,
3341         .con_font_get           = fbcon_get_font,
3342         .con_font_default       = fbcon_set_def_font,
3343         .con_font_copy          = fbcon_copy_font,
3344         .con_set_palette        = fbcon_set_palette,
3345         .con_scrolldelta        = fbcon_scrolldelta,
3346         .con_set_origin         = fbcon_set_origin,
3347         .con_invert_region      = fbcon_invert_region,
3348         .con_screen_pos         = fbcon_screen_pos,
3349         .con_getxy              = fbcon_getxy,
3350         .con_resize             = fbcon_resize,
3351         .con_debug_enter        = fbcon_debug_enter,
3352         .con_debug_leave        = fbcon_debug_leave,
3353 };
3354
3355 static struct notifier_block fbcon_event_notifier = {
3356         .notifier_call  = fbcon_event_notify,
3357 };
3358
3359 static ssize_t store_rotate(struct device *device,
3360                             struct device_attribute *attr, const char *buf,
3361                             size_t count)
3362 {
3363         struct fb_info *info;
3364         int rotate, idx;
3365         char **last = NULL;
3366
3367         if (fbcon_has_exited)
3368                 return count;
3369
3370         console_lock();
3371         idx = con2fb_map[fg_console];
3372
3373         if (idx == -1 || registered_fb[idx] == NULL)
3374                 goto err;
3375
3376         info = registered_fb[idx];
3377         rotate = simple_strtoul(buf, last, 0);
3378         fbcon_rotate(info, rotate);
3379 err:
3380         console_unlock();
3381         return count;
3382 }
3383
3384 static ssize_t store_rotate_all(struct device *device,
3385                                 struct device_attribute *attr,const char *buf,
3386                                 size_t count)
3387 {
3388         struct fb_info *info;
3389         int rotate, idx;
3390         char **last = NULL;
3391
3392         if (fbcon_has_exited)
3393                 return count;
3394
3395         console_lock();
3396         idx = con2fb_map[fg_console];
3397
3398         if (idx == -1 || registered_fb[idx] == NULL)
3399                 goto err;
3400
3401         info = registered_fb[idx];
3402         rotate = simple_strtoul(buf, last, 0);
3403         fbcon_rotate_all(info, rotate);
3404 err:
3405         console_unlock();
3406         return count;
3407 }
3408
3409 static ssize_t show_rotate(struct device *device,
3410                            struct device_attribute *attr,char *buf)
3411 {
3412         struct fb_info *info;
3413         int rotate = 0, idx;
3414
3415         if (fbcon_has_exited)
3416                 return 0;
3417
3418         console_lock();
3419         idx = con2fb_map[fg_console];
3420
3421         if (idx == -1 || registered_fb[idx] == NULL)
3422                 goto err;
3423
3424         info = registered_fb[idx];
3425         rotate = fbcon_get_rotate(info);
3426 err:
3427         console_unlock();
3428         return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3429 }
3430
3431 static ssize_t show_cursor_blink(struct device *device,
3432                                  struct device_attribute *attr, char *buf)
3433 {
3434         struct fb_info *info;
3435         struct fbcon_ops *ops;
3436         int idx, blink = -1;
3437
3438         if (fbcon_has_exited)
3439                 return 0;
3440
3441         console_lock();
3442         idx = con2fb_map[fg_console];
3443
3444         if (idx == -1 || registered_fb[idx] == NULL)
3445                 goto err;
3446
3447         info = registered_fb[idx];
3448         ops = info->fbcon_par;
3449
3450         if (!ops)
3451                 goto err;
3452
3453         blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3454 err:
3455         console_unlock();
3456         return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3457 }
3458
3459 static ssize_t store_cursor_blink(struct device *device,
3460                                   struct device_attribute *attr,
3461                                   const char *buf, size_t count)
3462 {
3463         struct fb_info *info;
3464         int blink, idx;
3465         char **last = NULL;
3466
3467         if (fbcon_has_exited)
3468                 return count;
3469
3470         console_lock();
3471         idx = con2fb_map[fg_console];
3472
3473         if (idx == -1 || registered_fb[idx] == NULL)
3474                 goto err;
3475
3476         info = registered_fb[idx];
3477
3478         if (!info->fbcon_par)
3479                 goto err;
3480
3481         blink = simple_strtoul(buf, last, 0);
3482
3483         if (blink) {
3484                 fbcon_cursor_noblink = 0;
3485                 fbcon_add_cursor_timer(info);
3486         } else {
3487                 fbcon_cursor_noblink = 1;
3488                 fbcon_del_cursor_timer(info);
3489         }
3490
3491 err:
3492         console_unlock();
3493         return count;
3494 }
3495
3496 static struct device_attribute device_attrs[] = {
3497         __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3498         __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3499         __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3500                store_cursor_blink),
3501 };
3502
3503 static int fbcon_init_device(void)
3504 {
3505         int i, error = 0;
3506
3507         fbcon_has_sysfs = 1;
3508
3509         for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3510                 error = device_create_file(fbcon_device, &device_attrs[i]);
3511
3512                 if (error)
3513                         break;
3514         }
3515
3516         if (error) {
3517                 while (--i >= 0)
3518                         device_remove_file(fbcon_device, &device_attrs[i]);
3519
3520                 fbcon_has_sysfs = 0;
3521         }
3522
3523         return 0;
3524 }
3525
3526 static void fbcon_start(void)
3527 {
3528         if (num_registered_fb) {
3529                 int i;
3530
3531                 console_lock();
3532
3533                 for (i = 0; i < FB_MAX; i++) {
3534                         if (registered_fb[i] != NULL) {
3535                                 info_idx = i;
3536                                 break;
3537                         }
3538                 }
3539
3540                 do_fbcon_takeover(0);
3541                 console_unlock();
3542
3543         }
3544 }
3545
3546 static void fbcon_exit(void)
3547 {
3548         struct fb_info *info;
3549         int i, j, mapped;
3550
3551         if (fbcon_has_exited)
3552                 return;
3553
3554         kfree((void *)softback_buf);
3555         softback_buf = 0UL;
3556
3557         for (i = 0; i < FB_MAX; i++) {
3558                 int pending = 0;
3559
3560                 mapped = 0;
3561                 info = registered_fb[i];
3562
3563                 if (info == NULL)
3564                         continue;
3565
3566                 if (info->queue.func)
3567                         pending = cancel_work_sync(&info->queue);
3568                 DPRINTK("fbcon: %s pending work\n", (pending ? "canceled" :
3569                         "no"));
3570
3571                 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3572                         if (con2fb_map[j] == i) {
3573                                 mapped = 1;
3574                                 break;
3575                         }
3576                 }
3577
3578                 if (mapped) {
3579                         if (info->fbops->fb_release)
3580                                 info->fbops->fb_release(info, 0);
3581                         module_put(info->fbops->owner);
3582
3583                         if (info->fbcon_par) {
3584                                 struct fbcon_ops *ops = info->fbcon_par;
3585
3586                                 fbcon_del_cursor_timer(info);
3587                                 kfree(ops->cursor_src);
3588                                 kfree(ops->cursor_state.mask);
3589                                 kfree(info->fbcon_par);
3590                                 info->fbcon_par = NULL;
3591                         }
3592
3593                         if (info->queue.func == fb_flashcursor)
3594                                 info->queue.func = NULL;
3595                 }
3596         }
3597
3598         fbcon_has_exited = 1;
3599 }
3600
3601 static int __init fb_console_init(void)
3602 {
3603         int i;
3604
3605         console_lock();
3606         fb_register_client(&fbcon_event_notifier);
3607         fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3608                                      "fbcon");
3609
3610         if (IS_ERR(fbcon_device)) {
3611                 printk(KERN_WARNING "Unable to create device "
3612                        "for fbcon; errno = %ld\n",
3613                        PTR_ERR(fbcon_device));
3614                 fbcon_device = NULL;
3615         } else
3616                 fbcon_init_device();
3617
3618         for (i = 0; i < MAX_NR_CONSOLES; i++)
3619                 con2fb_map[i] = -1;
3620
3621         console_unlock();
3622         fbcon_start();
3623         return 0;
3624 }
3625
3626 fs_initcall(fb_console_init);
3627
3628 #ifdef MODULE
3629
3630 static void __exit fbcon_deinit_device(void)
3631 {
3632         int i;
3633
3634         if (fbcon_has_sysfs) {
3635                 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3636                         device_remove_file(fbcon_device, &device_attrs[i]);
3637
3638                 fbcon_has_sysfs = 0;
3639         }
3640 }
3641
3642 static void __exit fb_console_exit(void)
3643 {
3644         console_lock();
3645         fb_unregister_client(&fbcon_event_notifier);
3646         fbcon_deinit_device();
3647         device_destroy(fb_class, MKDEV(0, 0));
3648         fbcon_exit();
3649         do_unregister_con_driver(&fb_con);
3650         console_unlock();
3651 }       
3652
3653 module_exit(fb_console_exit);
3654
3655 #endif
3656
3657 MODULE_LICENSE("GPL");