fbcon: delay the start of the cursor timer until a con_switch()
[deliverable/linux.git] / drivers / video / console / fbcon.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
61#include <linux/module.h>
62#include <linux/types.h>
1da177e4
LT
63#include <linux/fs.h>
64#include <linux/kernel.h>
65#include <linux/delay.h> /* MSch: for IRQ probe */
1da177e4
LT
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 */
623e71b0 78#include <asm/fb.h>
1da177e4
LT
79#include <asm/irq.h>
80#include <asm/system.h>
1da177e4
LT
81#ifdef CONFIG_ATARI
82#include <asm/atariints.h>
83#endif
84#ifdef CONFIG_MAC
85#include <asm/macints.h>
86#endif
87#if defined(__mc68000__) || defined(CONFIG_APUS)
88#include <asm/machdep.h>
89#include <asm/setup.h>
90#endif
91
92#include "fbcon.h"
93
94#ifdef FBCONDEBUG
95# define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
96#else
97# define DPRINTK(fmt, args...)
98#endif
99
100enum {
101 FBCON_LOGO_CANSHOW = -1, /* the logo can be shown */
102 FBCON_LOGO_DRAW = -2, /* draw the logo to a console */
103 FBCON_LOGO_DONTSHOW = -3 /* do not show the logo */
104};
105
ab767201 106static struct display fb_display[MAX_NR_CONSOLES];
e4fc2761 107
1da177e4
LT
108static signed char con2fb_map[MAX_NR_CONSOLES];
109static signed char con2fb_map_boot[MAX_NR_CONSOLES];
70802c60 110#ifndef MODULE
1da177e4 111static int logo_height;
70802c60 112#endif
1da177e4
LT
113static int logo_lines;
114/* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO
115 enums. */
116static int logo_shown = FBCON_LOGO_CANSHOW;
117/* Software scrollback */
118static int fbcon_softback_size = 32768;
119static unsigned long softback_buf, softback_curr;
120static unsigned long softback_in;
121static unsigned long softback_top, softback_end;
122static int softback_lines;
123/* console mappings */
124static int first_fb_vc;
125static int last_fb_vc = MAX_NR_CONSOLES - 1;
126static int fbcon_is_default = 1;
e614b18d 127static int fbcon_has_exited;
623e71b0 128static int primary_device = -1;
4769a9a5
AD
129
130#ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
623e71b0 131static int map_override;
e614b18d 132
4769a9a5
AD
133static inline void fbcon_map_override(void)
134{
135 map_override = 1;
136}
137#else
138static inline void fbcon_map_override(void)
139{
140}
141#endif /* CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY */
142
1da177e4
LT
143/* font data */
144static char fontname[40];
145
146/* current fb_info */
147static int info_idx = -1;
148
e4fc2761
AD
149/* console rotation */
150static int rotate;
0a727dea 151static int fbcon_has_sysfs;
e4fc2761 152
1da177e4
LT
153static const struct consw fb_con;
154
155#define CM_SOFTBACK (8)
156
157#define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
158
1da177e4
LT
159static int fbcon_set_origin(struct vc_data *);
160
161#define CURSOR_DRAW_DELAY (1)
162
163/* # VBL ints between cursor state changes */
1da177e4
LT
164#define ATARI_CURSOR_BLINK_RATE (42)
165#define MAC_CURSOR_BLINK_RATE (32)
166#define DEFAULT_CURSOR_BLINK_RATE (20)
167
168static int vbl_cursor_cnt;
acba9cd0 169static int fbcon_cursor_noblink;
1da177e4
LT
170
171#define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1)
172
173/*
174 * Interface used by the world
175 */
176
177static const char *fbcon_startup(void);
178static void fbcon_init(struct vc_data *vc, int init);
179static void fbcon_deinit(struct vc_data *vc);
180static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
181 int width);
182static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
183static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
184 int count, int ypos, int xpos);
185static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
186static void fbcon_cursor(struct vc_data *vc, int mode);
187static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
188 int count);
189static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
190 int height, int width);
191static int fbcon_switch(struct vc_data *vc);
192static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
193static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
194static int fbcon_scrolldelta(struct vc_data *vc, int lines);
195
196/*
197 * Internal routines
198 */
1da177e4
LT
199static __inline__ void ywrap_up(struct vc_data *vc, int count);
200static __inline__ void ywrap_down(struct vc_data *vc, int count);
201static __inline__ void ypan_up(struct vc_data *vc, int count);
202static __inline__ void ypan_down(struct vc_data *vc, int count);
203static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
204 int dy, int dx, int height, int width, u_int y_break);
205static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
d1baa4ff 206 int unit);
1da177e4
LT
207static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
208 int line, int count, int dy);
a812c94b
AD
209static void fbcon_modechanged(struct fb_info *info);
210static void fbcon_set_all_vcs(struct fb_info *info);
5428b044
AD
211static void fbcon_start(void);
212static void fbcon_exit(void);
0c6c1ce0 213static struct device *fbcon_device;
9a179176 214
1da177e4
LT
215#ifdef CONFIG_MAC
216/*
217 * On the Macintoy, there may or may not be a working VBL int. We need to probe
218 */
219static int vbl_detected;
220
7d12e780 221static irqreturn_t fb_vbl_detect(int irq, void *dummy)
1da177e4
LT
222{
223 vbl_detected++;
224 return IRQ_HANDLED;
225}
226#endif
227
dbcbfe1e 228#ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
b73deed3 229static inline void fbcon_set_rotation(struct fb_info *info)
dbcbfe1e
AD
230{
231 struct fbcon_ops *ops = info->fbcon_par;
232
233 if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
b73deed3
AD
234 ops->p->con_rotate < 4)
235 ops->rotate = ops->p->con_rotate;
dbcbfe1e
AD
236 else
237 ops->rotate = 0;
238}
a812c94b
AD
239
240static void fbcon_rotate(struct fb_info *info, u32 rotate)
241{
242 struct fbcon_ops *ops= info->fbcon_par;
243 struct fb_info *fb_info;
244
245 if (!ops || ops->currcon == -1)
246 return;
247
248 fb_info = registered_fb[con2fb_map[ops->currcon]];
249
250 if (info == fb_info) {
251 struct display *p = &fb_display[ops->currcon];
252
253 if (rotate < 4)
254 p->con_rotate = rotate;
255 else
256 p->con_rotate = 0;
257
258 fbcon_modechanged(info);
259 }
260}
261
262static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
263{
264 struct fbcon_ops *ops = info->fbcon_par;
265 struct vc_data *vc;
266 struct display *p;
267 int i;
268
269 if (!ops || ops->currcon < 0 || rotate > 3)
270 return;
271
e614b18d 272 for (i = first_fb_vc; i <= last_fb_vc; i++) {
a812c94b
AD
273 vc = vc_cons[i].d;
274 if (!vc || vc->vc_mode != KD_TEXT ||
275 registered_fb[con2fb_map[i]] != info)
276 continue;
277
278 p = &fb_display[vc->vc_num];
279 p->con_rotate = rotate;
280 }
281
282 fbcon_set_all_vcs(info);
283}
dbcbfe1e 284#else
b73deed3 285static inline void fbcon_set_rotation(struct fb_info *info)
e4fc2761
AD
286{
287 struct fbcon_ops *ops = info->fbcon_par;
288
289 ops->rotate = FB_ROTATE_UR;
290}
a812c94b
AD
291
292static void fbcon_rotate(struct fb_info *info, u32 rotate)
293{
294 return;
295}
296
297static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
298{
299 return;
300}
dbcbfe1e 301#endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
e4fc2761 302
a812c94b
AD
303static int fbcon_get_rotate(struct fb_info *info)
304{
305 struct fbcon_ops *ops = info->fbcon_par;
306
307 return (ops) ? ops->rotate : 0;
308}
309
1da177e4
LT
310static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
311{
312 struct fbcon_ops *ops = info->fbcon_par;
313
314 return (info->state != FBINFO_STATE_RUNNING ||
315 vc->vc_mode != KD_TEXT || ops->graphics);
316}
317
318static inline int get_color(struct vc_data *vc, struct fb_info *info,
319 u16 c, int is_fg)
320{
b8c90945 321 int depth = fb_get_color_depth(&info->var, &info->fix);
1da177e4
LT
322 int color = 0;
323
324 if (console_blanked) {
325 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
326
327 c = vc->vc_video_erase_char & charmask;
328 }
329
330 if (depth != 1)
331 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
332 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
333
334 switch (depth) {
335 case 1:
336 {
b8c90945
AD
337 int col = ~(0xfff << (max(info->var.green.length,
338 max(info->var.red.length,
339 info->var.blue.length)))) & 0xff;
340
1da177e4 341 /* 0 or 1 */
b8c90945
AD
342 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
343 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
1da177e4
LT
344
345 if (console_blanked)
346 fg = bg;
347
348 color = (is_fg) ? fg : bg;
349 break;
350 }
351 case 2:
352 /*
353 * Scale down 16-colors to 4 colors. Default 4-color palette
2cc38ed1
AD
354 * is grayscale. However, simply dividing the values by 4
355 * will not work, as colors 1, 2 and 3 will be scaled-down
356 * to zero rendering them invisible. So empirically convert
357 * colors to a sane 4-level grayscale.
1da177e4 358 */
2cc38ed1
AD
359 switch (color) {
360 case 0:
361 color = 0; /* black */
362 break;
363 case 1 ... 6:
364 color = 2; /* white */
365 break;
366 case 7 ... 8:
367 color = 1; /* gray */
368 break;
369 default:
370 color = 3; /* intense white */
371 break;
372 }
373 break;
1da177e4
LT
374 case 3:
375 /*
376 * Last 8 entries of default 16-color palette is a more intense
377 * version of the first 8 (i.e., same chrominance, different
378 * luminance).
379 */
380 color &= 7;
381 break;
382 }
383
384
385 return color;
386}
387
4d9c5b6e
AD
388static void fbcon_update_softback(struct vc_data *vc)
389{
390 int l = fbcon_softback_size / vc->vc_size_row;
391
392 if (l > 5)
393 softback_end = softback_buf + l * vc->vc_size_row;
394 else
395 /* Smaller scrollback makes no sense, and 0 would screw
396 the operation totally */
397 softback_top = 0;
398}
399
c4028958 400static void fb_flashcursor(struct work_struct *work)
1da177e4 401{
c4028958 402 struct fb_info *info = container_of(work, struct fb_info, queue);
1da177e4
LT
403 struct fbcon_ops *ops = info->fbcon_par;
404 struct display *p;
405 struct vc_data *vc = NULL;
406 int c;
407 int mode;
408
5428b044
AD
409 acquire_console_sem();
410 if (ops && ops->currcon != -1)
1da177e4
LT
411 vc = vc_cons[ops->currcon].d;
412
413 if (!vc || !CON_IS_VISIBLE(vc) ||
dbd4f128 414 registered_fb[con2fb_map[vc->vc_num]] != info ||
212f2639 415 vc->vc_deccm != 1) {
5428b044 416 release_console_sem();
1da177e4 417 return;
5428b044
AD
418 }
419
1da177e4
LT
420 p = &fb_display[vc->vc_num];
421 c = scr_readw((u16 *) vc->vc_pos);
422 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
423 CM_ERASE : CM_DRAW;
b73deed3 424 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
1da177e4
LT
425 get_color(vc, info, c, 0));
426 release_console_sem();
427}
428
41359dca 429#if defined(CONFIG_ATARI) || defined(CONFIG_MAC)
1da177e4 430static int cursor_blink_rate;
7d12e780 431static irqreturn_t fb_vbl_handler(int irq, void *dev_id)
1da177e4
LT
432{
433 struct fb_info *info = dev_id;
434
435 if (vbl_cursor_cnt && --vbl_cursor_cnt == 0) {
436 schedule_work(&info->queue);
437 vbl_cursor_cnt = cursor_blink_rate;
438 }
439 return IRQ_HANDLED;
440}
441#endif
442
443static void cursor_timer_handler(unsigned long dev_addr)
444{
445 struct fb_info *info = (struct fb_info *) dev_addr;
446 struct fbcon_ops *ops = info->fbcon_par;
447
448 schedule_work(&info->queue);
449 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
450}
451
88fb2c6e
AD
452static void fbcon_add_cursor_timer(struct fb_info *info)
453{
454 struct fbcon_ops *ops = info->fbcon_par;
455
456 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
acba9cd0
AD
457 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
458 !fbcon_cursor_noblink) {
88fb2c6e 459 if (!info->queue.func)
c4028958 460 INIT_WORK(&info->queue, fb_flashcursor);
88fb2c6e
AD
461
462 init_timer(&ops->cursor_timer);
463 ops->cursor_timer.function = cursor_timer_handler;
464 ops->cursor_timer.expires = jiffies + HZ / 5;
465 ops->cursor_timer.data = (unsigned long ) info;
466 add_timer(&ops->cursor_timer);
467 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
468 }
469}
470
471static void fbcon_del_cursor_timer(struct fb_info *info)
472{
473 struct fbcon_ops *ops = info->fbcon_par;
474
475 if (info->queue.func == fb_flashcursor &&
476 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
477 del_timer_sync(&ops->cursor_timer);
478 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
479 }
480}
481
1da177e4
LT
482#ifndef MODULE
483static int __init fb_console_setup(char *this_opt)
484{
485 char *options;
486 int i, j;
487
488 if (!this_opt || !*this_opt)
9b41046c 489 return 1;
1da177e4
LT
490
491 while ((options = strsep(&this_opt, ",")) != NULL) {
492 if (!strncmp(options, "font:", 5))
493 strcpy(fontname, options + 5);
494
495 if (!strncmp(options, "scrollback:", 11)) {
496 options += 11;
497 if (*options) {
498 fbcon_softback_size = simple_strtoul(options, &options, 0);
499 if (*options == 'k' || *options == 'K') {
500 fbcon_softback_size *= 1024;
501 options++;
502 }
503 if (*options != ',')
9b41046c 504 return 1;
1da177e4
LT
505 options++;
506 } else
9b41046c 507 return 1;
1da177e4
LT
508 }
509
510 if (!strncmp(options, "map:", 4)) {
511 options += 4;
623e71b0 512 if (*options) {
1da177e4
LT
513 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
514 if (!options[j])
515 j = 0;
516 con2fb_map_boot[i] =
517 (options[j++]-'0') % FB_MAX;
518 }
623e71b0 519
4769a9a5 520 fbcon_map_override();
623e71b0
AD
521 }
522
9b41046c 523 return 1;
1da177e4
LT
524 }
525
526 if (!strncmp(options, "vc:", 3)) {
527 options += 3;
528 if (*options)
529 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
530 if (first_fb_vc < 0)
531 first_fb_vc = 0;
532 if (*options++ == '-')
533 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
534 fbcon_is_default = 0;
535 }
e4fc2761
AD
536
537 if (!strncmp(options, "rotate:", 7)) {
538 options += 7;
539 if (*options)
540 rotate = simple_strtoul(options, &options, 0);
541 if (rotate > 3)
542 rotate = 0;
543 }
1da177e4 544 }
9b41046c 545 return 1;
1da177e4
LT
546}
547
548__setup("fbcon=", fb_console_setup);
549#endif
550
551static int search_fb_in_map(int idx)
552{
553 int i, retval = 0;
554
e614b18d 555 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
556 if (con2fb_map[i] == idx)
557 retval = 1;
558 }
559 return retval;
560}
561
562static int search_for_mapped_con(void)
563{
564 int i, retval = 0;
565
e614b18d 566 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
567 if (con2fb_map[i] != -1)
568 retval = 1;
569 }
570 return retval;
571}
572
573static int fbcon_takeover(int show_logo)
574{
575 int err, i;
576
577 if (!num_registered_fb)
578 return -ENODEV;
579
580 if (!show_logo)
581 logo_shown = FBCON_LOGO_DONTSHOW;
582
583 for (i = first_fb_vc; i <= last_fb_vc; i++)
584 con2fb_map[i] = info_idx;
585
586 err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
587 fbcon_is_default);
e614b18d 588
1da177e4
LT
589 if (err) {
590 for (i = first_fb_vc; i <= last_fb_vc; i++) {
591 con2fb_map[i] = -1;
592 }
593 info_idx = -1;
594 }
595
596 return err;
597}
598
70802c60
AD
599#ifdef MODULE
600static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
601 int cols, int rows, int new_cols, int new_rows)
602{
603 logo_shown = FBCON_LOGO_DONTSHOW;
604}
605#else
1da177e4
LT
606static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
607 int cols, int rows, int new_cols, int new_rows)
608{
609 /* Need to make room for the logo */
9c44e5f6 610 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
611 int cnt, erase = vc->vc_video_erase_char, step;
612 unsigned short *save = NULL, *r, *q;
613
70802c60
AD
614 if (info->flags & FBINFO_MODULE) {
615 logo_shown = FBCON_LOGO_DONTSHOW;
616 return;
617 }
618
1da177e4
LT
619 /*
620 * remove underline attribute from erase character
621 * if black and white framebuffer.
622 */
b8c90945 623 if (fb_get_color_depth(&info->var, &info->fix) == 1)
1da177e4 624 erase &= ~0x400;
9c44e5f6 625 logo_height = fb_prepare_logo(info, ops->rotate);
1da177e4
LT
626 logo_lines = (logo_height + vc->vc_font.height - 1) /
627 vc->vc_font.height;
628 q = (unsigned short *) (vc->vc_origin +
629 vc->vc_size_row * rows);
630 step = logo_lines * cols;
631 for (r = q - logo_lines * cols; r < q; r++)
632 if (scr_readw(r) != vc->vc_video_erase_char)
633 break;
634 if (r != q && new_rows >= rows + logo_lines) {
635 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
636 if (save) {
637 int i = cols < new_cols ? cols : new_cols;
638 scr_memsetw(save, erase, logo_lines * new_cols * 2);
639 r = q - step;
640 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
641 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
642 r = q;
643 }
644 }
645 if (r == q) {
646 /* We can scroll screen down */
647 r = q - step - cols;
648 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
649 scr_memcpyw(r + step, r, vc->vc_size_row);
650 r -= cols;
651 }
652 if (!save) {
250038f5
GU
653 int lines;
654 if (vc->vc_y + logo_lines >= rows)
655 lines = rows - vc->vc_y - 1;
656 else
657 lines = logo_lines;
658 vc->vc_y += lines;
659 vc->vc_pos += lines * vc->vc_size_row;
1da177e4
LT
660 }
661 }
662 scr_memsetw((unsigned short *) vc->vc_origin,
663 erase,
664 vc->vc_size_row * logo_lines);
665
666 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
667 fbcon_clear_margins(vc, 0);
668 update_screen(vc);
669 }
670
671 if (save) {
672 q = (unsigned short *) (vc->vc_origin +
673 vc->vc_size_row *
674 rows);
675 scr_memcpyw(q, save, logo_lines * new_cols * 2);
676 vc->vc_y += logo_lines;
677 vc->vc_pos += logo_lines * vc->vc_size_row;
678 kfree(save);
679 }
680
681 if (logo_lines > vc->vc_bottom) {
682 logo_shown = FBCON_LOGO_CANSHOW;
683 printk(KERN_INFO
684 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
685 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
686 logo_shown = FBCON_LOGO_DRAW;
687 vc->vc_top = logo_lines;
688 }
689}
70802c60 690#endif /* MODULE */
1da177e4
LT
691
692#ifdef CONFIG_FB_TILEBLITTING
b73deed3 693static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
1da177e4
LT
694{
695 struct fbcon_ops *ops = info->fbcon_par;
696
b73deed3 697 ops->p = &fb_display[vc->vc_num];
ab767201 698
1da177e4 699 if ((info->flags & FBINFO_MISC_TILEBLITTING))
b73deed3 700 fbcon_set_tileops(vc, info);
e4fc2761 701 else {
b73deed3 702 fbcon_set_rotation(info);
1da177e4 703 fbcon_set_bitops(ops);
e4fc2761 704 }
1da177e4 705}
38b4982c
AD
706
707static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
708{
709 int err = 0;
710
711 if (info->flags & FBINFO_MISC_TILEBLITTING &&
712 info->tileops->fb_get_tilemax(info) < charcount)
713 err = 1;
714
715 return err;
716}
1da177e4 717#else
b73deed3 718static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
1da177e4
LT
719{
720 struct fbcon_ops *ops = info->fbcon_par;
721
722 info->flags &= ~FBINFO_MISC_TILEBLITTING;
b73deed3
AD
723 ops->p = &fb_display[vc->vc_num];
724 fbcon_set_rotation(info);
1da177e4
LT
725 fbcon_set_bitops(ops);
726}
38b4982c
AD
727
728static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
729{
730 return 0;
731}
732
1da177e4
LT
733#endif /* CONFIG_MISC_TILEBLITTING */
734
735
736static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
737 int unit, int oldidx)
738{
739 struct fbcon_ops *ops = NULL;
740 int err = 0;
741
742 if (!try_module_get(info->fbops->owner))
743 err = -ENODEV;
744
745 if (!err && info->fbops->fb_open &&
746 info->fbops->fb_open(info, 0))
747 err = -ENODEV;
748
749 if (!err) {
a39bc34e 750 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
1da177e4
LT
751 if (!ops)
752 err = -ENOMEM;
753 }
754
755 if (!err) {
1da177e4 756 info->fbcon_par = ops;
d1baa4ff
AD
757
758 if (vc)
759 set_blitting_type(vc, info);
1da177e4
LT
760 }
761
762 if (err) {
763 con2fb_map[unit] = oldidx;
764 module_put(info->fbops->owner);
765 }
766
767 return err;
768}
769
770static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
771 struct fb_info *newinfo, int unit,
772 int oldidx, int found)
773{
774 struct fbcon_ops *ops = oldinfo->fbcon_par;
775 int err = 0;
776
777 if (oldinfo->fbops->fb_release &&
778 oldinfo->fbops->fb_release(oldinfo, 0)) {
779 con2fb_map[unit] = oldidx;
780 if (!found && newinfo->fbops->fb_release)
781 newinfo->fbops->fb_release(newinfo, 0);
782 if (!found)
783 module_put(newinfo->fbops->owner);
784 err = -ENODEV;
785 }
786
787 if (!err) {
88fb2c6e 788 fbcon_del_cursor_timer(oldinfo);
1da177e4
LT
789 kfree(ops->cursor_state.mask);
790 kfree(ops->cursor_data);
e4fc2761 791 kfree(ops->fontbuffer);
1da177e4
LT
792 kfree(oldinfo->fbcon_par);
793 oldinfo->fbcon_par = NULL;
794 module_put(oldinfo->fbops->owner);
dd0314f7
AD
795 /*
796 If oldinfo and newinfo are driving the same hardware,
797 the fb_release() method of oldinfo may attempt to
798 restore the hardware state. This will leave the
799 newinfo in an undefined state. Thus, a call to
800 fb_set_par() may be needed for the newinfo.
801 */
802 if (newinfo->fbops->fb_set_par)
803 newinfo->fbops->fb_set_par(newinfo);
1da177e4
LT
804 }
805
806 return err;
807}
808
1da177e4
LT
809static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
810 int unit, int show_logo)
811{
812 struct fbcon_ops *ops = info->fbcon_par;
813
814 ops->currcon = fg_console;
815
816 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT))
817 info->fbops->fb_set_par(info);
818
819 ops->flags |= FBCON_FLAGS_INIT;
820 ops->graphics = 0;
d1baa4ff 821 fbcon_set_disp(info, &info->var, unit);
1da177e4
LT
822
823 if (show_logo) {
824 struct vc_data *fg_vc = vc_cons[fg_console].d;
825 struct fb_info *fg_info =
826 registered_fb[con2fb_map[fg_console]];
827
828 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
829 fg_vc->vc_rows, fg_vc->vc_cols,
830 fg_vc->vc_rows);
831 }
832
833 update_screen(vc_cons[fg_console].d);
834}
835
836/**
837 * set_con2fb_map - map console to frame buffer device
838 * @unit: virtual console number to map
839 * @newidx: frame buffer index to map virtual console to
840 * @user: user request
841 *
842 * Maps a virtual console @unit to a frame buffer device
843 * @newidx.
844 */
845static int set_con2fb_map(int unit, int newidx, int user)
846{
847 struct vc_data *vc = vc_cons[unit].d;
848 int oldidx = con2fb_map[unit];
849 struct fb_info *info = registered_fb[newidx];
850 struct fb_info *oldinfo = NULL;
851 int found, err = 0;
852
853 if (oldidx == newidx)
854 return 0;
855
e614b18d
AD
856 if (!info || fbcon_has_exited)
857 return -EINVAL;
1da177e4
LT
858
859 if (!err && !search_for_mapped_con()) {
860 info_idx = newidx;
861 return fbcon_takeover(0);
862 }
863
864 if (oldidx != -1)
865 oldinfo = registered_fb[oldidx];
866
867 found = search_fb_in_map(newidx);
868
869 acquire_console_sem();
870 con2fb_map[unit] = newidx;
871 if (!err && !found)
872 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
873
874
875 /*
876 * If old fb is not mapped to any of the consoles,
877 * fbcon should release it.
878 */
879 if (!err && oldinfo && !search_fb_in_map(oldidx))
880 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
881 found);
882
883 if (!err) {
884 int show_logo = (fg_console == 0 && !user &&
885 logo_shown != FBCON_LOGO_DONTSHOW);
886
887 if (!found)
88fb2c6e 888 fbcon_add_cursor_timer(info);
1da177e4
LT
889 con2fb_map_boot[unit] = newidx;
890 con2fb_init_display(vc, info, unit, show_logo);
891 }
892
e614b18d
AD
893 if (!search_fb_in_map(info_idx))
894 info_idx = newidx;
895
1da177e4
LT
896 release_console_sem();
897 return err;
898}
899
900/*
901 * Low Level Operations
902 */
903/* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
904static int var_to_display(struct display *disp,
905 struct fb_var_screeninfo *var,
906 struct fb_info *info)
907{
908 disp->xres_virtual = var->xres_virtual;
909 disp->yres_virtual = var->yres_virtual;
910 disp->bits_per_pixel = var->bits_per_pixel;
911 disp->grayscale = var->grayscale;
912 disp->nonstd = var->nonstd;
913 disp->accel_flags = var->accel_flags;
914 disp->height = var->height;
915 disp->width = var->width;
916 disp->red = var->red;
917 disp->green = var->green;
918 disp->blue = var->blue;
919 disp->transp = var->transp;
920 disp->rotate = var->rotate;
921 disp->mode = fb_match_mode(var, &info->modelist);
922 if (disp->mode == NULL)
923 /* This should not happen */
924 return -EINVAL;
925 return 0;
926}
927
928static void display_to_var(struct fb_var_screeninfo *var,
929 struct display *disp)
930{
931 fb_videomode_to_var(var, disp->mode);
932 var->xres_virtual = disp->xres_virtual;
933 var->yres_virtual = disp->yres_virtual;
934 var->bits_per_pixel = disp->bits_per_pixel;
935 var->grayscale = disp->grayscale;
936 var->nonstd = disp->nonstd;
937 var->accel_flags = disp->accel_flags;
938 var->height = disp->height;
939 var->width = disp->width;
940 var->red = disp->red;
941 var->green = disp->green;
942 var->blue = disp->blue;
943 var->transp = disp->transp;
944 var->rotate = disp->rotate;
945}
946
947static const char *fbcon_startup(void)
948{
949 const char *display_desc = "frame buffer device";
950 struct display *p = &fb_display[fg_console];
951 struct vc_data *vc = vc_cons[fg_console].d;
2f4516db 952 const struct font_desc *font = NULL;
1da177e4
LT
953 struct module *owner;
954 struct fb_info *info = NULL;
955 struct fbcon_ops *ops;
956 int rows, cols;
957 int irqres;
958
959 irqres = 1;
960 /*
961 * If num_registered_fb is zero, this is a call for the dummy part.
962 * The frame buffer devices weren't initialized yet.
963 */
964 if (!num_registered_fb || info_idx == -1)
965 return display_desc;
966 /*
967 * Instead of blindly using registered_fb[0], we use info_idx, set by
968 * fb_console_init();
969 */
970 info = registered_fb[info_idx];
971 if (!info)
972 return NULL;
973
974 owner = info->fbops->owner;
975 if (!try_module_get(owner))
976 return NULL;
977 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
978 module_put(owner);
979 return NULL;
980 }
981
a39bc34e 982 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
1da177e4
LT
983 if (!ops) {
984 module_put(owner);
985 return NULL;
986 }
987
1da177e4
LT
988 ops->currcon = -1;
989 ops->graphics = 1;
e4fc2761 990 ops->cur_rotate = -1;
1da177e4 991 info->fbcon_par = ops;
e4fc2761 992 p->con_rotate = rotate;
b73deed3 993 set_blitting_type(vc, info);
1da177e4
LT
994
995 if (info->fix.type != FB_TYPE_TEXT) {
996 if (fbcon_softback_size) {
997 if (!softback_buf) {
998 softback_buf =
999 (unsigned long)
1000 kmalloc(fbcon_softback_size,
1001 GFP_KERNEL);
1002 if (!softback_buf) {
1003 fbcon_softback_size = 0;
1004 softback_top = 0;
1005 }
1006 }
1007 } else {
1008 if (softback_buf) {
1009 kfree((void *) softback_buf);
1010 softback_buf = 0;
1011 softback_top = 0;
1012 }
1013 }
1014 if (softback_buf)
1015 softback_in = softback_top = softback_curr =
1016 softback_buf;
1017 softback_lines = 0;
1018 }
1019
1020 /* Setup default font */
1021 if (!p->fontdata) {
1022 if (!fontname[0] || !(font = find_font(fontname)))
1023 font = get_default_font(info->var.xres,
2d2699d9
AD
1024 info->var.yres,
1025 info->pixmap.blit_x,
1026 info->pixmap.blit_y);
1da177e4
LT
1027 vc->vc_font.width = font->width;
1028 vc->vc_font.height = font->height;
2f4516db 1029 vc->vc_font.data = (void *)(p->fontdata = font->data);
1da177e4
LT
1030 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1031 }
1032
e4fc2761
AD
1033 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1034 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1035 cols /= vc->vc_font.width;
1036 rows /= vc->vc_font.height;
1da177e4
LT
1037 vc_resize(vc, cols, rows);
1038
1039 DPRINTK("mode: %s\n", info->fix.id);
1040 DPRINTK("visual: %d\n", info->fix.visual);
1041 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1042 info->var.yres,
1043 info->var.bits_per_pixel);
1044
1045#ifdef CONFIG_ATARI
1046 if (MACH_IS_ATARI) {
1047 cursor_blink_rate = ATARI_CURSOR_BLINK_RATE;
1048 irqres =
1049 request_irq(IRQ_AUTO_4, fb_vbl_handler,
1050 IRQ_TYPE_PRIO, "framebuffer vbl",
1051 info);
1052 }
1053#endif /* CONFIG_ATARI */
1054
1055#ifdef CONFIG_MAC
1056 /*
1057 * On a Macintoy, the VBL interrupt may or may not be active.
1058 * As interrupt based cursor is more reliable and race free, we
1059 * probe for VBL interrupts.
1060 */
1061 if (MACH_IS_MAC) {
1062 int ct = 0;
1063 /*
1064 * Probe for VBL: set temp. handler ...
1065 */
1066 irqres = request_irq(IRQ_MAC_VBL, fb_vbl_detect, 0,
1067 "framebuffer vbl", info);
1068 vbl_detected = 0;
1069
1070 /*
1071 * ... and spin for 20 ms ...
1072 */
1073 while (!vbl_detected && ++ct < 1000)
1074 udelay(20);
1075
1076 if (ct == 1000)
1077 printk
1078 ("fbcon_startup: No VBL detected, using timer based cursor.\n");
1079
1080 free_irq(IRQ_MAC_VBL, fb_vbl_detect);
1081
1082 if (vbl_detected) {
1083 /*
1084 * interrupt based cursor ok
1085 */
1086 cursor_blink_rate = MAC_CURSOR_BLINK_RATE;
1087 irqres =
1088 request_irq(IRQ_MAC_VBL, fb_vbl_handler, 0,
1089 "framebuffer vbl", info);
1090 } else {
1091 /*
1092 * VBL not detected: fall through, use timer based cursor
1093 */
1094 irqres = 1;
1095 }
1096 }
1097#endif /* CONFIG_MAC */
1098
88fb2c6e 1099 fbcon_add_cursor_timer(info);
e614b18d 1100 fbcon_has_exited = 0;
1da177e4
LT
1101 return display_desc;
1102}
1103
1104static void fbcon_init(struct vc_data *vc, int init)
1105{
1106 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1107 struct fbcon_ops *ops;
1108 struct vc_data **default_mode = vc->vc_display_fg;
1109 struct vc_data *svc = *default_mode;
1110 struct display *t, *p = &fb_display[vc->vc_num];
1111 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
306958e8 1112 int cap;
1da177e4
LT
1113
1114 if (info_idx == -1 || info == NULL)
1115 return;
306958e8
AB
1116
1117 cap = info->flags;
1118
1da177e4
LT
1119 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1120 (info->fix.type == FB_TYPE_TEXT))
1121 logo = 0;
1122
1da177e4
LT
1123 if (var_to_display(p, &info->var, info))
1124 return;
1125
d1baa4ff
AD
1126 if (!info->fbcon_par)
1127 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1128
1da177e4
LT
1129 /* If we are not the first console on this
1130 fb, copy the font from that console */
e614b18d
AD
1131 t = &fb_display[fg_console];
1132 if (!p->fontdata) {
1133 if (t->fontdata) {
1134 struct vc_data *fvc = vc_cons[fg_console].d;
1135
1136 vc->vc_font.data = (void *)(p->fontdata =
1137 fvc->vc_font.data);
1138 vc->vc_font.width = fvc->vc_font.width;
1139 vc->vc_font.height = fvc->vc_font.height;
1140 p->userfont = t->userfont;
1141
1142 if (p->userfont)
1143 REFCOUNT(p->fontdata)++;
1144 } else {
1145 const struct font_desc *font = NULL;
1146
1147 if (!fontname[0] || !(font = find_font(fontname)))
1148 font = get_default_font(info->var.xres,
2d2699d9
AD
1149 info->var.yres,
1150 info->pixmap.blit_x,
1151 info->pixmap.blit_y);
e614b18d
AD
1152 vc->vc_font.width = font->width;
1153 vc->vc_font.height = font->height;
1154 vc->vc_font.data = (void *)(p->fontdata = font->data);
1155 vc->vc_font.charcount = 256; /* FIXME Need to
1156 support more fonts */
1157 }
1da177e4 1158 }
e614b18d 1159
1da177e4
LT
1160 if (p->userfont)
1161 charcnt = FNTCHARCNT(p->fontdata);
e614b18d 1162
b8c90945 1163 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1da177e4
LT
1164 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1165 if (charcnt == 256) {
1166 vc->vc_hi_font_mask = 0;
1167 } else {
1168 vc->vc_hi_font_mask = 0x100;
1169 if (vc->vc_can_do_color)
1170 vc->vc_complement_mask <<= 1;
1171 }
1172
1173 if (!*svc->vc_uni_pagedir_loc)
1174 con_set_default_unimap(svc);
1175 if (!*vc->vc_uni_pagedir_loc)
1176 con_copy_unimap(vc, svc);
1177
e4fc2761
AD
1178 ops = info->fbcon_par;
1179 p->con_rotate = rotate;
b73deed3 1180 set_blitting_type(vc, info);
e4fc2761 1181
1da177e4
LT
1182 cols = vc->vc_cols;
1183 rows = vc->vc_rows;
e4fc2761
AD
1184 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1185 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1186 new_cols /= vc->vc_font.width;
1187 new_rows /= vc->vc_font.height;
1da177e4
LT
1188 vc_resize(vc, new_cols, new_rows);
1189
1da177e4
LT
1190 /*
1191 * We must always set the mode. The mode of the previous console
1192 * driver could be in the same resolution but we are using different
1193 * hardware so we have to initialize the hardware.
1194 *
1195 * We need to do it in fbcon_init() to prevent screen corruption.
1196 */