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