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