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