net/mlx5: E-Switch, Introduce Vport administration functions
[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
acba9cd0 149static int fbcon_cursor_noblink;
1da177e4
LT
150
151#define divides(a, b) ((!(a) || (b)%(a)) ? 0 : 1)
152
153/*
154 * Interface used by the world
155 */
156
157static const char *fbcon_startup(void);
158static void fbcon_init(struct vc_data *vc, int init);
159static void fbcon_deinit(struct vc_data *vc);
160static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
161 int width);
162static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
163static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
164 int count, int ypos, int xpos);
165static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
166static void fbcon_cursor(struct vc_data *vc, int mode);
167static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
168 int count);
169static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
170 int height, int width);
171static int fbcon_switch(struct vc_data *vc);
172static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
173static int fbcon_set_palette(struct vc_data *vc, unsigned char *table);
174static int fbcon_scrolldelta(struct vc_data *vc, int lines);
175
176/*
177 * Internal routines
178 */
1da177e4
LT
179static __inline__ void ywrap_up(struct vc_data *vc, int count);
180static __inline__ void ywrap_down(struct vc_data *vc, int count);
181static __inline__ void ypan_up(struct vc_data *vc, int count);
182static __inline__ void ypan_down(struct vc_data *vc, int count);
183static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
184 int dy, int dx, int height, int width, u_int y_break);
185static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
d1baa4ff 186 int unit);
1da177e4
LT
187static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
188 int line, int count, int dy);
a812c94b
AD
189static void fbcon_modechanged(struct fb_info *info);
190static void fbcon_set_all_vcs(struct fb_info *info);
5428b044
AD
191static void fbcon_start(void);
192static void fbcon_exit(void);
0c6c1ce0 193static struct device *fbcon_device;
9a179176 194
dbcbfe1e 195#ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
b73deed3 196static inline void fbcon_set_rotation(struct fb_info *info)
dbcbfe1e
AD
197{
198 struct fbcon_ops *ops = info->fbcon_par;
199
200 if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
b73deed3
AD
201 ops->p->con_rotate < 4)
202 ops->rotate = ops->p->con_rotate;
dbcbfe1e
AD
203 else
204 ops->rotate = 0;
205}
a812c94b
AD
206
207static void fbcon_rotate(struct fb_info *info, u32 rotate)
208{
209 struct fbcon_ops *ops= info->fbcon_par;
210 struct fb_info *fb_info;
211
212 if (!ops || ops->currcon == -1)
213 return;
214
215 fb_info = registered_fb[con2fb_map[ops->currcon]];
216
217 if (info == fb_info) {
218 struct display *p = &fb_display[ops->currcon];
219
220 if (rotate < 4)
221 p->con_rotate = rotate;
222 else
223 p->con_rotate = 0;
224
225 fbcon_modechanged(info);
226 }
227}
228
229static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
230{
231 struct fbcon_ops *ops = info->fbcon_par;
232 struct vc_data *vc;
233 struct display *p;
234 int i;
235
236 if (!ops || ops->currcon < 0 || rotate > 3)
237 return;
238
e614b18d 239 for (i = first_fb_vc; i <= last_fb_vc; i++) {
a812c94b
AD
240 vc = vc_cons[i].d;
241 if (!vc || vc->vc_mode != KD_TEXT ||
242 registered_fb[con2fb_map[i]] != info)
243 continue;
244
245 p = &fb_display[vc->vc_num];
246 p->con_rotate = rotate;
247 }
248
249 fbcon_set_all_vcs(info);
250}
dbcbfe1e 251#else
b73deed3 252static inline void fbcon_set_rotation(struct fb_info *info)
e4fc2761
AD
253{
254 struct fbcon_ops *ops = info->fbcon_par;
255
256 ops->rotate = FB_ROTATE_UR;
257}
a812c94b
AD
258
259static void fbcon_rotate(struct fb_info *info, u32 rotate)
260{
261 return;
262}
263
264static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
265{
266 return;
267}
dbcbfe1e 268#endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
e4fc2761 269
a812c94b
AD
270static int fbcon_get_rotate(struct fb_info *info)
271{
272 struct fbcon_ops *ops = info->fbcon_par;
273
274 return (ops) ? ops->rotate : 0;
275}
276
1da177e4
LT
277static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
278{
279 struct fbcon_ops *ops = info->fbcon_par;
280
281 return (info->state != FBINFO_STATE_RUNNING ||
8fd4bd22
JB
282 vc->vc_mode != KD_TEXT || ops->graphics) &&
283 !vt_force_oops_output(vc);
1da177e4
LT
284}
285
da909ce4 286static int get_color(struct vc_data *vc, struct fb_info *info,
1da177e4
LT
287 u16 c, int is_fg)
288{
b8c90945 289 int depth = fb_get_color_depth(&info->var, &info->fix);
1da177e4
LT
290 int color = 0;
291
292 if (console_blanked) {
293 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
294
295 c = vc->vc_video_erase_char & charmask;
296 }
297
298 if (depth != 1)
299 color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
300 : attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
301
302 switch (depth) {
303 case 1:
304 {
91c43132 305 int col = mono_col(info);
1da177e4 306 /* 0 or 1 */
b8c90945
AD
307 int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
308 int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
1da177e4
LT
309
310 if (console_blanked)
311 fg = bg;
312
313 color = (is_fg) ? fg : bg;
314 break;
315 }
316 case 2:
317 /*
318 * Scale down 16-colors to 4 colors. Default 4-color palette
2cc38ed1
AD
319 * is grayscale. However, simply dividing the values by 4
320 * will not work, as colors 1, 2 and 3 will be scaled-down
321 * to zero rendering them invisible. So empirically convert
322 * colors to a sane 4-level grayscale.
1da177e4 323 */
2cc38ed1
AD
324 switch (color) {
325 case 0:
326 color = 0; /* black */
327 break;
328 case 1 ... 6:
329 color = 2; /* white */
330 break;
331 case 7 ... 8:
332 color = 1; /* gray */
333 break;
334 default:
335 color = 3; /* intense white */
336 break;
337 }
338 break;
1da177e4
LT
339 case 3:
340 /*
341 * Last 8 entries of default 16-color palette is a more intense
342 * version of the first 8 (i.e., same chrominance, different
343 * luminance).
344 */
345 color &= 7;
346 break;
347 }
348
349
350 return color;
351}
352
4d9c5b6e
AD
353static void fbcon_update_softback(struct vc_data *vc)
354{
355 int l = fbcon_softback_size / vc->vc_size_row;
356
357 if (l > 5)
358 softback_end = softback_buf + l * vc->vc_size_row;
359 else
360 /* Smaller scrollback makes no sense, and 0 would screw
361 the operation totally */
362 softback_top = 0;
363}
364
c4028958 365static void fb_flashcursor(struct work_struct *work)
1da177e4 366{
c4028958 367 struct fb_info *info = container_of(work, struct fb_info, queue);
1da177e4 368 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
369 struct vc_data *vc = NULL;
370 int c;
371 int mode;
d8636a27
DA
372 int ret;
373
374 /* FIXME: we should sort out the unbind locking instead */
375 /* instead we just fail to flash the cursor if we can't get
376 * the lock instead of blocking fbcon deinit */
377 ret = console_trylock();
378 if (ret == 0)
379 return;
1da177e4 380
5428b044 381 if (ops && ops->currcon != -1)
1da177e4
LT
382 vc = vc_cons[ops->currcon].d;
383
384 if (!vc || !CON_IS_VISIBLE(vc) ||
dbd4f128 385 registered_fb[con2fb_map[vc->vc_num]] != info ||
212f2639 386 vc->vc_deccm != 1) {
ac751efa 387 console_unlock();
1da177e4 388 return;
5428b044
AD
389 }
390
1da177e4
LT
391 c = scr_readw((u16 *) vc->vc_pos);
392 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
393 CM_ERASE : CM_DRAW;
b73deed3 394 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
1da177e4 395 get_color(vc, info, c, 0));
ac751efa 396 console_unlock();
1da177e4
LT
397}
398
1da177e4
LT
399static void cursor_timer_handler(unsigned long dev_addr)
400{
401 struct fb_info *info = (struct fb_info *) dev_addr;
402 struct fbcon_ops *ops = info->fbcon_par;
403
a85f1a41 404 queue_work(system_power_efficient_wq, &info->queue);
27a4c827 405 mod_timer(&ops->cursor_timer, jiffies + ops->cur_blink_jiffies);
1da177e4
LT
406}
407
88fb2c6e
AD
408static void fbcon_add_cursor_timer(struct fb_info *info)
409{
410 struct fbcon_ops *ops = info->fbcon_par;
411
412 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
acba9cd0
AD
413 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
414 !fbcon_cursor_noblink) {
88fb2c6e 415 if (!info->queue.func)
c4028958 416 INIT_WORK(&info->queue, fb_flashcursor);
88fb2c6e
AD
417
418 init_timer(&ops->cursor_timer);
419 ops->cursor_timer.function = cursor_timer_handler;
27a4c827 420 ops->cursor_timer.expires = jiffies + ops->cur_blink_jiffies;
88fb2c6e
AD
421 ops->cursor_timer.data = (unsigned long ) info;
422 add_timer(&ops->cursor_timer);
423 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
424 }
425}
426
427static void fbcon_del_cursor_timer(struct fb_info *info)
428{
429 struct fbcon_ops *ops = info->fbcon_par;
430
431 if (info->queue.func == fb_flashcursor &&
432 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
433 del_timer_sync(&ops->cursor_timer);
434 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
435 }
436}
437
1da177e4
LT
438#ifndef MODULE
439static int __init fb_console_setup(char *this_opt)
440{
441 char *options;
442 int i, j;
443
444 if (!this_opt || !*this_opt)
9b41046c 445 return 1;
1da177e4
LT
446
447 while ((options = strsep(&this_opt, ",")) != NULL) {
37773c4e 448 if (!strncmp(options, "font:", 5)) {
e432964a 449 strlcpy(fontname, options + 5, sizeof(fontname));
37773c4e
MH
450 continue;
451 }
1da177e4
LT
452
453 if (!strncmp(options, "scrollback:", 11)) {
454 options += 11;
455 if (*options) {
456 fbcon_softback_size = simple_strtoul(options, &options, 0);
457 if (*options == 'k' || *options == 'K') {
458 fbcon_softback_size *= 1024;
1da177e4 459 }
37773c4e
MH
460 }
461 continue;
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 475 }
37773c4e 476 continue;
1da177e4
LT
477 }
478
479 if (!strncmp(options, "vc:", 3)) {
480 options += 3;
481 if (*options)
482 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
483 if (first_fb_vc < 0)
484 first_fb_vc = 0;
485 if (*options++ == '-')
486 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
487 fbcon_is_default = 0;
37773c4e
MH
488 continue;
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;
37773c4e 497 continue;
e4fc2761 498 }
1da177e4 499 }
9b41046c 500 return 1;
1da177e4
LT
501}
502
503__setup("fbcon=", fb_console_setup);
504#endif
505
506static int search_fb_in_map(int idx)
507{
508 int i, retval = 0;
509
e614b18d 510 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
511 if (con2fb_map[i] == idx)
512 retval = 1;
513 }
514 return retval;
515}
516
517static int search_for_mapped_con(void)
518{
519 int i, retval = 0;
520
e614b18d 521 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
522 if (con2fb_map[i] != -1)
523 retval = 1;
524 }
525 return retval;
526}
527
50e244cc
AC
528static int do_fbcon_takeover(int show_logo)
529{
530 int err, i;
531
532 if (!num_registered_fb)
533 return -ENODEV;
534
535 if (!show_logo)
536 logo_shown = FBCON_LOGO_DONTSHOW;
537
538 for (i = first_fb_vc; i <= last_fb_vc; i++)
539 con2fb_map[i] = info_idx;
540
541 err = do_take_over_console(&fb_con, first_fb_vc, last_fb_vc,
542 fbcon_is_default);
543
544 if (err) {
545 for (i = first_fb_vc; i <= last_fb_vc; i++)
546 con2fb_map[i] = -1;
547 info_idx = -1;
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);
7a966fbd 747 kfree(ops->cursor_src);
e4fc2761 748 kfree(ops->fontbuffer);
1da177e4
LT
749 kfree(oldinfo->fbcon_par);
750 oldinfo->fbcon_par = NULL;
751 module_put(oldinfo->fbops->owner);
dd0314f7
AD
752 /*
753 If oldinfo and newinfo are driving the same hardware,
754 the fb_release() method of oldinfo may attempt to
755 restore the hardware state. This will leave the
756 newinfo in an undefined state. Thus, a call to
757 fb_set_par() may be needed for the newinfo.
758 */
5f4dc28b 759 if (newinfo && newinfo->fbops->fb_set_par) {
0fcf6ada
FTS
760 ret = newinfo->fbops->fb_set_par(newinfo);
761
762 if (ret)
763 printk(KERN_ERR "con2fb_release_oldinfo: "
764 "detected unhandled fb_set_par error, "
765 "error code %d\n", ret);
766 }
1da177e4
LT
767 }
768
769 return err;
770}
771
1da177e4
LT
772static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
773 int unit, int show_logo)
774{
775 struct fbcon_ops *ops = info->fbcon_par;
0fcf6ada 776 int ret;
1da177e4
LT
777
778 ops->currcon = fg_console;
779
0fcf6ada
FTS
780 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT)) {
781 ret = info->fbops->fb_set_par(info);
782
783 if (ret)
784 printk(KERN_ERR "con2fb_init_display: detected "
785 "unhandled fb_set_par error, "
786 "error code %d\n", ret);
787 }
1da177e4
LT
788
789 ops->flags |= FBCON_FLAGS_INIT;
790 ops->graphics = 0;
d1baa4ff 791 fbcon_set_disp(info, &info->var, unit);
1da177e4
LT
792
793 if (show_logo) {
794 struct vc_data *fg_vc = vc_cons[fg_console].d;
795 struct fb_info *fg_info =
796 registered_fb[con2fb_map[fg_console]];
797
798 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
799 fg_vc->vc_rows, fg_vc->vc_cols,
800 fg_vc->vc_rows);
801 }
802
803 update_screen(vc_cons[fg_console].d);
804}
805
806/**
807 * set_con2fb_map - map console to frame buffer device
808 * @unit: virtual console number to map
809 * @newidx: frame buffer index to map virtual console to
810 * @user: user request
811 *
812 * Maps a virtual console @unit to a frame buffer device
813 * @newidx.
054430e7
DA
814 *
815 * This should be called with the console lock held.
1da177e4
LT
816 */
817static int set_con2fb_map(int unit, int newidx, int user)
818{
819 struct vc_data *vc = vc_cons[unit].d;
820 int oldidx = con2fb_map[unit];
821 struct fb_info *info = registered_fb[newidx];
822 struct fb_info *oldinfo = NULL;
823 int found, err = 0;
824
825 if (oldidx == newidx)
826 return 0;
827
32b98bf8 828 if (!info)
e614b18d 829 return -EINVAL;
1da177e4 830
32b98bf8 831 if (!search_for_mapped_con() || !con_is_bound(&fb_con)) {
1da177e4 832 info_idx = newidx;
054430e7 833 return do_fbcon_takeover(0);
1da177e4
LT
834 }
835
836 if (oldidx != -1)
837 oldinfo = registered_fb[oldidx];
838
839 found = search_fb_in_map(newidx);
840
1da177e4
LT
841 con2fb_map[unit] = newidx;
842 if (!err && !found)
843 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
844
845
846 /*
847 * If old fb is not mapped to any of the consoles,
848 * fbcon should release it.
849 */
850 if (!err && oldinfo && !search_fb_in_map(oldidx))
851 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
852 found);
853
854 if (!err) {
855 int show_logo = (fg_console == 0 && !user &&
856 logo_shown != FBCON_LOGO_DONTSHOW);
857
858 if (!found)
88fb2c6e 859 fbcon_add_cursor_timer(info);
1da177e4
LT
860 con2fb_map_boot[unit] = newidx;
861 con2fb_init_display(vc, info, unit, show_logo);
862 }
863
e614b18d
AD
864 if (!search_fb_in_map(info_idx))
865 info_idx = newidx;
866
1da177e4
LT
867 return err;
868}
869
870/*
871 * Low Level Operations
872 */
155957f5 873/* NOTE: fbcon cannot be __init: it may be called from do_take_over_console later */
1da177e4
LT
874static int var_to_display(struct display *disp,
875 struct fb_var_screeninfo *var,
876 struct fb_info *info)
877{
878 disp->xres_virtual = var->xres_virtual;
879 disp->yres_virtual = var->yres_virtual;
880 disp->bits_per_pixel = var->bits_per_pixel;
881 disp->grayscale = var->grayscale;
882 disp->nonstd = var->nonstd;
883 disp->accel_flags = var->accel_flags;
884 disp->height = var->height;
885 disp->width = var->width;
886 disp->red = var->red;
887 disp->green = var->green;
888 disp->blue = var->blue;
889 disp->transp = var->transp;
890 disp->rotate = var->rotate;
891 disp->mode = fb_match_mode(var, &info->modelist);
892 if (disp->mode == NULL)
893 /* This should not happen */
894 return -EINVAL;
895 return 0;
896}
897
898static void display_to_var(struct fb_var_screeninfo *var,
899 struct display *disp)
900{
901 fb_videomode_to_var(var, disp->mode);
902 var->xres_virtual = disp->xres_virtual;
903 var->yres_virtual = disp->yres_virtual;
904 var->bits_per_pixel = disp->bits_per_pixel;
905 var->grayscale = disp->grayscale;
906 var->nonstd = disp->nonstd;
907 var->accel_flags = disp->accel_flags;
908 var->height = disp->height;
909 var->width = disp->width;
910 var->red = disp->red;
911 var->green = disp->green;
912 var->blue = disp->blue;
913 var->transp = disp->transp;
914 var->rotate = disp->rotate;
915}
916
917static const char *fbcon_startup(void)
918{
919 const char *display_desc = "frame buffer device";
920 struct display *p = &fb_display[fg_console];
921 struct vc_data *vc = vc_cons[fg_console].d;
2f4516db 922 const struct font_desc *font = NULL;
1da177e4
LT
923 struct module *owner;
924 struct fb_info *info = NULL;
925 struct fbcon_ops *ops;
926 int rows, cols;
1da177e4 927
1da177e4
LT
928 /*
929 * If num_registered_fb is zero, this is a call for the dummy part.
930 * The frame buffer devices weren't initialized yet.
931 */
932 if (!num_registered_fb || info_idx == -1)
933 return display_desc;
934 /*
935 * Instead of blindly using registered_fb[0], we use info_idx, set by
936 * fb_console_init();
937 */
938 info = registered_fb[info_idx];
939 if (!info)
940 return NULL;
941
942 owner = info->fbops->owner;
943 if (!try_module_get(owner))
944 return NULL;
945 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
946 module_put(owner);
947 return NULL;
948 }
949
a39bc34e 950 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
1da177e4
LT
951 if (!ops) {
952 module_put(owner);
953 return NULL;
954 }
955
1da177e4
LT
956 ops->currcon = -1;
957 ops->graphics = 1;
e4fc2761 958 ops->cur_rotate = -1;
1da177e4 959 info->fbcon_par = ops;
2428e59b 960 p->con_rotate = initial_rotation;
b73deed3 961 set_blitting_type(vc, info);
1da177e4
LT
962
963 if (info->fix.type != FB_TYPE_TEXT) {
964 if (fbcon_softback_size) {
965 if (!softback_buf) {
966 softback_buf =
967 (unsigned long)
968 kmalloc(fbcon_softback_size,
969 GFP_KERNEL);
970 if (!softback_buf) {
971 fbcon_softback_size = 0;
972 softback_top = 0;
973 }
974 }
975 } else {
976 if (softback_buf) {
977 kfree((void *) softback_buf);
978 softback_buf = 0;
979 softback_top = 0;
980 }
981 }
982 if (softback_buf)
983 softback_in = softback_top = softback_curr =
984 softback_buf;
985 softback_lines = 0;
986 }
987
988 /* Setup default font */
ae128786 989 if (!p->fontdata && !vc->vc_font.data) {
1da177e4
LT
990 if (!fontname[0] || !(font = find_font(fontname)))
991 font = get_default_font(info->var.xres,
2d2699d9
AD
992 info->var.yres,
993 info->pixmap.blit_x,
994 info->pixmap.blit_y);
1da177e4
LT
995 vc->vc_font.width = font->width;
996 vc->vc_font.height = font->height;
2f4516db 997 vc->vc_font.data = (void *)(p->fontdata = font->data);
1da177e4 998 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
ae128786
DA
999 } else {
1000 p->fontdata = vc->vc_font.data;
1da177e4
LT
1001 }
1002
e4fc2761
AD
1003 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1004 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1005 cols /= vc->vc_font.width;
1006 rows /= vc->vc_font.height;
1da177e4
LT
1007 vc_resize(vc, cols, rows);
1008
1009 DPRINTK("mode: %s\n", info->fix.id);
1010 DPRINTK("visual: %d\n", info->fix.visual);
1011 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1012 info->var.yres,
1013 info->var.bits_per_pixel);
1014
88fb2c6e 1015 fbcon_add_cursor_timer(info);
e614b18d 1016 fbcon_has_exited = 0;
1da177e4
LT
1017 return display_desc;
1018}
1019
1020static void fbcon_init(struct vc_data *vc, int init)
1021{
1022 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1023 struct fbcon_ops *ops;
1024 struct vc_data **default_mode = vc->vc_display_fg;
1025 struct vc_data *svc = *default_mode;
1026 struct display *t, *p = &fb_display[vc->vc_num];
1027 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
0fcf6ada 1028 int cap, ret;
1da177e4
LT
1029
1030 if (info_idx == -1 || info == NULL)
1031 return;
306958e8
AB
1032
1033 cap = info->flags;
1034
1da177e4
LT
1035 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1036 (info->fix.type == FB_TYPE_TEXT))
1037 logo = 0;
1038
1da177e4
LT
1039 if (var_to_display(p, &info->var, info))
1040 return;
1041
d1baa4ff
AD
1042 if (!info->fbcon_par)
1043 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1044
1da177e4
LT
1045 /* If we are not the first console on this
1046 fb, copy the font from that console */
e614b18d
AD
1047 t = &fb_display[fg_console];
1048 if (!p->fontdata) {
1049 if (t->fontdata) {
1050 struct vc_data *fvc = vc_cons[fg_console].d;
1051
1052 vc->vc_font.data = (void *)(p->fontdata =
1053 fvc->vc_font.data);
1054 vc->vc_font.width = fvc->vc_font.width;
1055 vc->vc_font.height = fvc->vc_font.height;
1056 p->userfont = t->userfont;
1057
1058 if (p->userfont)
1059 REFCOUNT(p->fontdata)++;
1060 } else {
1061 const struct font_desc *font = NULL;
1062
1063 if (!fontname[0] || !(font = find_font(fontname)))
1064 font = get_default_font(info->var.xres,
2d2699d9
AD
1065 info->var.yres,
1066 info->pixmap.blit_x,
1067 info->pixmap.blit_y);
e614b18d
AD
1068 vc->vc_font.width = font->width;
1069 vc->vc_font.height = font->height;
1070 vc->vc_font.data = (void *)(p->fontdata = font->data);
1071 vc->vc_font.charcount = 256; /* FIXME Need to
1072 support more fonts */
1073 }
1da177e4 1074 }
e614b18d 1075
1da177e4
LT
1076 if (p->userfont)
1077 charcnt = FNTCHARCNT(p->fontdata);
e614b18d 1078
8fd4bd22 1079 vc->vc_panic_force_write = !!(info->flags & FBINFO_CAN_FORCE_OUTPUT);
b8c90945 1080 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1da177e4
LT
1081 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1082 if (charcnt == 256) {
1083 vc->vc_hi_font_mask = 0;
1084 } else {
1085 vc->vc_hi_font_mask = 0x100;
1086 if (vc->vc_can_do_color)
1087 vc->vc_complement_mask <<= 1;
1088 }
1089
1090 if (!*svc->vc_uni_pagedir_loc)
1091 con_set_default_unimap(svc);
1092 if (!*vc->vc_uni_pagedir_loc)
1093 con_copy_unimap(vc, svc);
1094
e4fc2761 1095 ops = info->fbcon_par;
f235f664 1096 ops->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms);
2428e59b 1097 p->con_rotate = initial_rotation;
b73deed3 1098 set_blitting_type(vc, info);
e4fc2761 1099
1da177e4
LT
1100 cols = vc->vc_cols;
1101 rows = vc->vc_rows;
e4fc2761
AD
1102 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1103 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1104 new_cols /= vc->vc_font.width;
1105 new_rows /= vc->vc_font.height;
1da177e4 1106
1da177e4
LT
1107 /*
1108 * We must always set the mode. The mode of the previous console
1109 * driver could be in the same resolution but we are using different
1110 * hardware so we have to initialize the hardware.
1111 *
1112 * We need to do it in fbcon_init() to prevent screen corruption.
1113 */