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