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