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