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