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