fb: rework locking to fix lock ordering on takeover
[GitHub/mt8127/android_kernel_alcatel_ttab.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;
d8636a27
DA
375 int ret;
376
377 /* FIXME: we should sort out the unbind locking instead */
378 /* instead we just fail to flash the cursor if we can't get
379 * the lock instead of blocking fbcon deinit */
380 ret = console_trylock();
381 if (ret == 0)
382 return;
1da177e4 383
5428b044 384 if (ops && ops->currcon != -1)
1da177e4
LT
385 vc = vc_cons[ops->currcon].d;
386
387 if (!vc || !CON_IS_VISIBLE(vc) ||
dbd4f128 388 registered_fb[con2fb_map[vc->vc_num]] != info ||
212f2639 389 vc->vc_deccm != 1) {
ac751efa 390 console_unlock();
1da177e4 391 return;
5428b044
AD
392 }
393
1da177e4
LT
394 c = scr_readw((u16 *) vc->vc_pos);
395 mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
396 CM_ERASE : CM_DRAW;
b73deed3 397 ops->cursor(vc, info, mode, softback_lines, get_color(vc, info, c, 1),
1da177e4 398 get_color(vc, info, c, 0));
ac751efa 399 console_unlock();
1da177e4
LT
400}
401
1da177e4
LT
402static void cursor_timer_handler(unsigned long dev_addr)
403{
404 struct fb_info *info = (struct fb_info *) dev_addr;
405 struct fbcon_ops *ops = info->fbcon_par;
406
407 schedule_work(&info->queue);
408 mod_timer(&ops->cursor_timer, jiffies + HZ/5);
409}
410
88fb2c6e
AD
411static void fbcon_add_cursor_timer(struct fb_info *info)
412{
413 struct fbcon_ops *ops = info->fbcon_par;
414
415 if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
acba9cd0
AD
416 !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
417 !fbcon_cursor_noblink) {
88fb2c6e 418 if (!info->queue.func)
c4028958 419 INIT_WORK(&info->queue, fb_flashcursor);
88fb2c6e
AD
420
421 init_timer(&ops->cursor_timer);
422 ops->cursor_timer.function = cursor_timer_handler;
423 ops->cursor_timer.expires = jiffies + HZ / 5;
424 ops->cursor_timer.data = (unsigned long ) info;
425 add_timer(&ops->cursor_timer);
426 ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
427 }
428}
429
430static void fbcon_del_cursor_timer(struct fb_info *info)
431{
432 struct fbcon_ops *ops = info->fbcon_par;
433
434 if (info->queue.func == fb_flashcursor &&
435 ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
436 del_timer_sync(&ops->cursor_timer);
437 ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
438 }
439}
440
1da177e4
LT
441#ifndef MODULE
442static int __init fb_console_setup(char *this_opt)
443{
444 char *options;
445 int i, j;
446
447 if (!this_opt || !*this_opt)
9b41046c 448 return 1;
1da177e4
LT
449
450 while ((options = strsep(&this_opt, ",")) != NULL) {
451 if (!strncmp(options, "font:", 5))
e432964a 452 strlcpy(fontname, options + 5, sizeof(fontname));
1da177e4
LT
453
454 if (!strncmp(options, "scrollback:", 11)) {
455 options += 11;
456 if (*options) {
457 fbcon_softback_size = simple_strtoul(options, &options, 0);
458 if (*options == 'k' || *options == 'K') {
459 fbcon_softback_size *= 1024;
460 options++;
461 }
462 if (*options != ',')
9b41046c 463 return 1;
1da177e4
LT
464 options++;
465 } else
9b41046c 466 return 1;
1da177e4
LT
467 }
468
469 if (!strncmp(options, "map:", 4)) {
470 options += 4;
623e71b0 471 if (*options) {
1da177e4
LT
472 for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
473 if (!options[j])
474 j = 0;
475 con2fb_map_boot[i] =
476 (options[j++]-'0') % FB_MAX;
477 }
623e71b0 478
4769a9a5 479 fbcon_map_override();
623e71b0
AD
480 }
481
9b41046c 482 return 1;
1da177e4
LT
483 }
484
485 if (!strncmp(options, "vc:", 3)) {
486 options += 3;
487 if (*options)
488 first_fb_vc = simple_strtoul(options, &options, 10) - 1;
489 if (first_fb_vc < 0)
490 first_fb_vc = 0;
491 if (*options++ == '-')
492 last_fb_vc = simple_strtoul(options, &options, 10) - 1;
493 fbcon_is_default = 0;
494 }
e4fc2761
AD
495
496 if (!strncmp(options, "rotate:", 7)) {
497 options += 7;
498 if (*options)
2428e59b
MS
499 initial_rotation = simple_strtoul(options, &options, 0);
500 if (initial_rotation > 3)
501 initial_rotation = 0;
e4fc2761 502 }
1da177e4 503 }
9b41046c 504 return 1;
1da177e4
LT
505}
506
507__setup("fbcon=", fb_console_setup);
508#endif
509
510static int search_fb_in_map(int idx)
511{
512 int i, retval = 0;
513
e614b18d 514 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
515 if (con2fb_map[i] == idx)
516 retval = 1;
517 }
518 return retval;
519}
520
521static int search_for_mapped_con(void)
522{
523 int i, retval = 0;
524
e614b18d 525 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
526 if (con2fb_map[i] != -1)
527 retval = 1;
528 }
529 return retval;
530}
531
50e244cc
AC
532static int do_fbcon_takeover(int show_logo)
533{
534 int err, i;
535
536 if (!num_registered_fb)
537 return -ENODEV;
538
539 if (!show_logo)
540 logo_shown = FBCON_LOGO_DONTSHOW;
541
542 for (i = first_fb_vc; i <= last_fb_vc; i++)
543 con2fb_map[i] = info_idx;
544
545 err = do_take_over_console(&fb_con, first_fb_vc, last_fb_vc,
546 fbcon_is_default);
547
548 if (err) {
549 for (i = first_fb_vc; i <= last_fb_vc; i++)
550 con2fb_map[i] = -1;
551 info_idx = -1;
552 } else {
553 fbcon_has_console_bind = 1;
554 }
555
556 return err;
557}
558
1da177e4
LT
559static int fbcon_takeover(int show_logo)
560{
561 int err, i;
562
563 if (!num_registered_fb)
564 return -ENODEV;
565
566 if (!show_logo)
567 logo_shown = FBCON_LOGO_DONTSHOW;
568
569 for (i = first_fb_vc; i <= last_fb_vc; i++)
570 con2fb_map[i] = info_idx;
571
572 err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
573 fbcon_is_default);
e614b18d 574
1da177e4
LT
575 if (err) {
576 for (i = first_fb_vc; i <= last_fb_vc; i++) {
577 con2fb_map[i] = -1;
578 }
579 info_idx = -1;
2ddce3fd
IA
580 } else {
581 fbcon_has_console_bind = 1;
1da177e4
LT
582 }
583
584 return err;
585}
586
70802c60
AD
587#ifdef MODULE
588static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
589 int cols, int rows, int new_cols, int new_rows)
590{
591 logo_shown = FBCON_LOGO_DONTSHOW;
592}
593#else
1da177e4
LT
594static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
595 int cols, int rows, int new_cols, int new_rows)
596{
597 /* Need to make room for the logo */
9c44e5f6 598 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
599 int cnt, erase = vc->vc_video_erase_char, step;
600 unsigned short *save = NULL, *r, *q;
49a1d28f 601 int logo_height;
1da177e4 602
70802c60
AD
603 if (info->flags & FBINFO_MODULE) {
604 logo_shown = FBCON_LOGO_DONTSHOW;
605 return;
606 }
607
1da177e4
LT
608 /*
609 * remove underline attribute from erase character
610 * if black and white framebuffer.
611 */
b8c90945 612 if (fb_get_color_depth(&info->var, &info->fix) == 1)
1da177e4 613 erase &= ~0x400;
9c44e5f6 614 logo_height = fb_prepare_logo(info, ops->rotate);
416e74ea 615 logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
1da177e4
LT
616 q = (unsigned short *) (vc->vc_origin +
617 vc->vc_size_row * rows);
618 step = logo_lines * cols;
619 for (r = q - logo_lines * cols; r < q; r++)
620 if (scr_readw(r) != vc->vc_video_erase_char)
621 break;
622 if (r != q && new_rows >= rows + logo_lines) {
623 save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
624 if (save) {
625 int i = cols < new_cols ? cols : new_cols;
626 scr_memsetw(save, erase, logo_lines * new_cols * 2);
627 r = q - step;
628 for (cnt = 0; cnt < logo_lines; cnt++, r += i)
629 scr_memcpyw(save + cnt * new_cols, r, 2 * i);
630 r = q;
631 }
632 }
633 if (r == q) {
634 /* We can scroll screen down */
635 r = q - step - cols;
636 for (cnt = rows - logo_lines; cnt > 0; cnt--) {
637 scr_memcpyw(r + step, r, vc->vc_size_row);
638 r -= cols;
639 }
640 if (!save) {
250038f5
GU
641 int lines;
642 if (vc->vc_y + logo_lines >= rows)
643 lines = rows - vc->vc_y - 1;
644 else
645 lines = logo_lines;
646 vc->vc_y += lines;
647 vc->vc_pos += lines * vc->vc_size_row;
1da177e4
LT
648 }
649 }
650 scr_memsetw((unsigned short *) vc->vc_origin,
651 erase,
652 vc->vc_size_row * logo_lines);
653
654 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
655 fbcon_clear_margins(vc, 0);
656 update_screen(vc);
657 }
658
659 if (save) {
660 q = (unsigned short *) (vc->vc_origin +
661 vc->vc_size_row *
662 rows);
663 scr_memcpyw(q, save, logo_lines * new_cols * 2);
664 vc->vc_y += logo_lines;
665 vc->vc_pos += logo_lines * vc->vc_size_row;
666 kfree(save);
667 }
668
669 if (logo_lines > vc->vc_bottom) {
670 logo_shown = FBCON_LOGO_CANSHOW;
671 printk(KERN_INFO
672 "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
673 } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
674 logo_shown = FBCON_LOGO_DRAW;
675 vc->vc_top = logo_lines;
676 }
677}
70802c60 678#endif /* MODULE */
1da177e4
LT
679
680#ifdef CONFIG_FB_TILEBLITTING
b73deed3 681static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
1da177e4
LT
682{
683 struct fbcon_ops *ops = info->fbcon_par;
684
b73deed3 685 ops->p = &fb_display[vc->vc_num];
ab767201 686
1da177e4 687 if ((info->flags & FBINFO_MISC_TILEBLITTING))
b73deed3 688 fbcon_set_tileops(vc, info);
e4fc2761 689 else {
b73deed3 690 fbcon_set_rotation(info);
1da177e4 691 fbcon_set_bitops(ops);
e4fc2761 692 }
1da177e4 693}
38b4982c
AD
694
695static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
696{
697 int err = 0;
698
699 if (info->flags & FBINFO_MISC_TILEBLITTING &&
700 info->tileops->fb_get_tilemax(info) < charcount)
701 err = 1;
702
703 return err;
704}
1da177e4 705#else
b73deed3 706static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
1da177e4
LT
707{
708 struct fbcon_ops *ops = info->fbcon_par;
709
710 info->flags &= ~FBINFO_MISC_TILEBLITTING;
b73deed3
AD
711 ops->p = &fb_display[vc->vc_num];
712 fbcon_set_rotation(info);
1da177e4
LT
713 fbcon_set_bitops(ops);
714}
38b4982c
AD
715
716static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
717{
718 return 0;
719}
720
1da177e4
LT
721#endif /* CONFIG_MISC_TILEBLITTING */
722
723
724static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
725 int unit, int oldidx)
726{
727 struct fbcon_ops *ops = NULL;
728 int err = 0;
729
730 if (!try_module_get(info->fbops->owner))
731 err = -ENODEV;
732
733 if (!err && info->fbops->fb_open &&
734 info->fbops->fb_open(info, 0))
735 err = -ENODEV;
736
737 if (!err) {
a39bc34e 738 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
1da177e4
LT
739 if (!ops)
740 err = -ENOMEM;
741 }
742
743 if (!err) {
1da177e4 744 info->fbcon_par = ops;
d1baa4ff
AD
745
746 if (vc)
747 set_blitting_type(vc, info);
1da177e4
LT
748 }
749
750 if (err) {
751 con2fb_map[unit] = oldidx;
752 module_put(info->fbops->owner);
753 }
754
755 return err;
756}
757
758static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
759 struct fb_info *newinfo, int unit,
760 int oldidx, int found)
761{
762 struct fbcon_ops *ops = oldinfo->fbcon_par;
0fcf6ada 763 int err = 0, ret;
1da177e4
LT
764
765 if (oldinfo->fbops->fb_release &&
766 oldinfo->fbops->fb_release(oldinfo, 0)) {
767 con2fb_map[unit] = oldidx;
768 if (!found && newinfo->fbops->fb_release)
769 newinfo->fbops->fb_release(newinfo, 0);
770 if (!found)
771 module_put(newinfo->fbops->owner);
772 err = -ENODEV;
773 }
774
775 if (!err) {
88fb2c6e 776 fbcon_del_cursor_timer(oldinfo);
1da177e4
LT
777 kfree(ops->cursor_state.mask);
778 kfree(ops->cursor_data);
e4fc2761 779 kfree(ops->fontbuffer);
1da177e4
LT
780 kfree(oldinfo->fbcon_par);
781 oldinfo->fbcon_par = NULL;
782 module_put(oldinfo->fbops->owner);
dd0314f7
AD
783 /*
784 If oldinfo and newinfo are driving the same hardware,
785 the fb_release() method of oldinfo may attempt to
786 restore the hardware state. This will leave the
787 newinfo in an undefined state. Thus, a call to
788 fb_set_par() may be needed for the newinfo.
789 */
0fcf6ada
FTS
790 if (newinfo->fbops->fb_set_par) {
791 ret = newinfo->fbops->fb_set_par(newinfo);
792
793 if (ret)
794 printk(KERN_ERR "con2fb_release_oldinfo: "
795 "detected unhandled fb_set_par error, "
796 "error code %d\n", ret);
797 }
1da177e4
LT
798 }
799
800 return err;
801}
802
1da177e4
LT
803static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
804 int unit, int show_logo)
805{
806 struct fbcon_ops *ops = info->fbcon_par;
0fcf6ada 807 int ret;
1da177e4
LT
808
809 ops->currcon = fg_console;
810
0fcf6ada
FTS
811 if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT)) {
812 ret = info->fbops->fb_set_par(info);
813
814 if (ret)
815 printk(KERN_ERR "con2fb_init_display: detected "
816 "unhandled fb_set_par error, "
817 "error code %d\n", ret);
818 }
1da177e4
LT
819
820 ops->flags |= FBCON_FLAGS_INIT;
821 ops->graphics = 0;
d1baa4ff 822 fbcon_set_disp(info, &info->var, unit);
1da177e4
LT
823
824 if (show_logo) {
825 struct vc_data *fg_vc = vc_cons[fg_console].d;
826 struct fb_info *fg_info =
827 registered_fb[con2fb_map[fg_console]];
828
829 fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
830 fg_vc->vc_rows, fg_vc->vc_cols,
831 fg_vc->vc_rows);
832 }
833
834 update_screen(vc_cons[fg_console].d);
835}
836
837/**
838 * set_con2fb_map - map console to frame buffer device
839 * @unit: virtual console number to map
840 * @newidx: frame buffer index to map virtual console to
841 * @user: user request
842 *
843 * Maps a virtual console @unit to a frame buffer device
844 * @newidx.
845 */
846static int set_con2fb_map(int unit, int newidx, int user)
847{
848 struct vc_data *vc = vc_cons[unit].d;
849 int oldidx = con2fb_map[unit];
850 struct fb_info *info = registered_fb[newidx];
851 struct fb_info *oldinfo = NULL;
852 int found, err = 0;
853
854 if (oldidx == newidx)
855 return 0;
856
32b98bf8 857 if (!info)
e614b18d 858 return -EINVAL;
1da177e4 859
32b98bf8 860 if (!search_for_mapped_con() || !con_is_bound(&fb_con)) {
1da177e4
LT
861 info_idx = newidx;
862 return fbcon_takeover(0);
863 }
864
865 if (oldidx != -1)
866 oldinfo = registered_fb[oldidx];
867
868 found = search_fb_in_map(newidx);
869
ac751efa 870 console_lock();
1da177e4
LT
871 con2fb_map[unit] = newidx;
872 if (!err && !found)
873 err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
874
875
876 /*
877 * If old fb is not mapped to any of the consoles,
878 * fbcon should release it.
879 */
880 if (!err && oldinfo && !search_fb_in_map(oldidx))
881 err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
882 found);
883
884 if (!err) {
885 int show_logo = (fg_console == 0 && !user &&
886 logo_shown != FBCON_LOGO_DONTSHOW);
887
888 if (!found)
88fb2c6e 889 fbcon_add_cursor_timer(info);
1da177e4
LT
890 con2fb_map_boot[unit] = newidx;
891 con2fb_init_display(vc, info, unit, show_logo);
892 }
893
e614b18d
AD
894 if (!search_fb_in_map(info_idx))
895 info_idx = newidx;
896
ac751efa 897 console_unlock();
1da177e4
LT
898 return err;
899}
900
901/*
902 * Low Level Operations
903 */
904/* NOTE: fbcon cannot be __init: it may be called from take_over_console later */
905static int var_to_display(struct display *disp,
906 struct fb_var_screeninfo *var,
907 struct fb_info *info)
908{
909 disp->xres_virtual = var->xres_virtual;
910 disp->yres_virtual = var->yres_virtual;
911 disp->bits_per_pixel = var->bits_per_pixel;
912 disp->grayscale = var->grayscale;
913 disp->nonstd = var->nonstd;
914 disp->accel_flags = var->accel_flags;
915 disp->height = var->height;
916 disp->width = var->width;
917 disp->red = var->red;
918 disp->green = var->green;
919 disp->blue = var->blue;
920 disp->transp = var->transp;
921 disp->rotate = var->rotate;
922 disp->mode = fb_match_mode(var, &info->modelist);
923 if (disp->mode == NULL)
924 /* This should not happen */
925 return -EINVAL;
926 return 0;
927}
928
929static void display_to_var(struct fb_var_screeninfo *var,
930 struct display *disp)
931{
932 fb_videomode_to_var(var, disp->mode);
933 var->xres_virtual = disp->xres_virtual;
934 var->yres_virtual = disp->yres_virtual;
935 var->bits_per_pixel = disp->bits_per_pixel;
936 var->grayscale = disp->grayscale;
937 var->nonstd = disp->nonstd;
938 var->accel_flags = disp->accel_flags;
939 var->height = disp->height;
940 var->width = disp->width;
941 var->red = disp->red;
942 var->green = disp->green;
943 var->blue = disp->blue;
944 var->transp = disp->transp;
945 var->rotate = disp->rotate;
946}
947
948static const char *fbcon_startup(void)
949{
950 const char *display_desc = "frame buffer device";
951 struct display *p = &fb_display[fg_console];
952 struct vc_data *vc = vc_cons[fg_console].d;
2f4516db 953 const struct font_desc *font = NULL;
1da177e4
LT
954 struct module *owner;
955 struct fb_info *info = NULL;
956 struct fbcon_ops *ops;
957 int rows, cols;
1da177e4 958
1da177e4
LT
959 /*
960 * If num_registered_fb is zero, this is a call for the dummy part.
961 * The frame buffer devices weren't initialized yet.
962 */
963 if (!num_registered_fb || info_idx == -1)
964 return display_desc;
965 /*
966 * Instead of blindly using registered_fb[0], we use info_idx, set by
967 * fb_console_init();
968 */
969 info = registered_fb[info_idx];
970 if (!info)
971 return NULL;
972
973 owner = info->fbops->owner;
974 if (!try_module_get(owner))
975 return NULL;
976 if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
977 module_put(owner);
978 return NULL;
979 }
980
a39bc34e 981 ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
1da177e4
LT
982 if (!ops) {
983 module_put(owner);
984 return NULL;
985 }
986
1da177e4
LT
987 ops->currcon = -1;
988 ops->graphics = 1;
e4fc2761 989 ops->cur_rotate = -1;
1da177e4 990 info->fbcon_par = ops;
2428e59b 991 p->con_rotate = initial_rotation;
b73deed3 992 set_blitting_type(vc, info);
1da177e4
LT
993
994 if (info->fix.type != FB_TYPE_TEXT) {
995 if (fbcon_softback_size) {
996 if (!softback_buf) {
997 softback_buf =
998 (unsigned long)
999 kmalloc(fbcon_softback_size,
1000 GFP_KERNEL);
1001 if (!softback_buf) {
1002 fbcon_softback_size = 0;
1003 softback_top = 0;
1004 }
1005 }
1006 } else {
1007 if (softback_buf) {
1008 kfree((void *) softback_buf);
1009 softback_buf = 0;
1010 softback_top = 0;
1011 }
1012 }
1013 if (softback_buf)
1014 softback_in = softback_top = softback_curr =
1015 softback_buf;
1016 softback_lines = 0;
1017 }
1018
1019 /* Setup default font */
1020 if (!p->fontdata) {
1021 if (!fontname[0] || !(font = find_font(fontname)))
1022 font = get_default_font(info->var.xres,
2d2699d9
AD
1023 info->var.yres,
1024 info->pixmap.blit_x,
1025 info->pixmap.blit_y);
1da177e4
LT
1026 vc->vc_font.width = font->width;
1027 vc->vc_font.height = font->height;
2f4516db 1028 vc->vc_font.data = (void *)(p->fontdata = font->data);
1da177e4
LT
1029 vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1030 }
1031
e4fc2761
AD
1032 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1033 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1034 cols /= vc->vc_font.width;
1035 rows /= vc->vc_font.height;
1da177e4
LT
1036 vc_resize(vc, cols, rows);
1037
1038 DPRINTK("mode: %s\n", info->fix.id);
1039 DPRINTK("visual: %d\n", info->fix.visual);
1040 DPRINTK("res: %dx%d-%d\n", info->var.xres,
1041 info->var.yres,
1042 info->var.bits_per_pixel);
1043
88fb2c6e 1044 fbcon_add_cursor_timer(info);
e614b18d 1045 fbcon_has_exited = 0;
1da177e4
LT
1046 return display_desc;
1047}
1048
1049static void fbcon_init(struct vc_data *vc, int init)
1050{
1051 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1052 struct fbcon_ops *ops;
1053 struct vc_data **default_mode = vc->vc_display_fg;
1054 struct vc_data *svc = *default_mode;
1055 struct display *t, *p = &fb_display[vc->vc_num];
1056 int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
0fcf6ada 1057 int cap, ret;
1da177e4
LT
1058
1059 if (info_idx == -1 || info == NULL)
1060 return;
306958e8
AB
1061
1062 cap = info->flags;
1063
1da177e4
LT
1064 if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1065 (info->fix.type == FB_TYPE_TEXT))
1066 logo = 0;
1067
1da177e4
LT
1068 if (var_to_display(p, &info->var, info))
1069 return;
1070
d1baa4ff
AD
1071 if (!info->fbcon_par)
1072 con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1073
1da177e4
LT
1074 /* If we are not the first console on this
1075 fb, copy the font from that console */
e614b18d
AD
1076 t = &fb_display[fg_console];
1077 if (!p->fontdata) {
1078 if (t->fontdata) {
1079 struct vc_data *fvc = vc_cons[fg_console].d;
1080
1081 vc->vc_font.data = (void *)(p->fontdata =
1082 fvc->vc_font.data);
1083 vc->vc_font.width = fvc->vc_font.width;
1084 vc->vc_font.height = fvc->vc_font.height;
1085 p->userfont = t->userfont;
1086
1087 if (p->userfont)
1088 REFCOUNT(p->fontdata)++;
1089 } else {
1090 const struct font_desc *font = NULL;
1091
1092 if (!fontname[0] || !(font = find_font(fontname)))
1093 font = get_default_font(info->var.xres,
2d2699d9
AD
1094 info->var.yres,
1095 info->pixmap.blit_x,
1096 info->pixmap.blit_y);
e614b18d
AD
1097 vc->vc_font.width = font->width;
1098 vc->vc_font.height = font->height;
1099 vc->vc_font.data = (void *)(p->fontdata = font->data);
1100 vc->vc_font.charcount = 256; /* FIXME Need to
1101 support more fonts */
1102 }
1da177e4 1103 }
e614b18d 1104
1da177e4
LT
1105 if (p->userfont)
1106 charcnt = FNTCHARCNT(p->fontdata);
e614b18d 1107
8fd4bd22 1108 vc->vc_panic_force_write = !!(info->flags & FBINFO_CAN_FORCE_OUTPUT);
b8c90945 1109 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1da177e4
LT
1110 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1111 if (charcnt == 256) {
1112 vc->vc_hi_font_mask = 0;
1113 } else {
1114 vc->vc_hi_font_mask = 0x100;
1115 if (vc->vc_can_do_color)
1116 vc->vc_complement_mask <<= 1;
1117 }
1118
1119 if (!*svc->vc_uni_pagedir_loc)
1120 con_set_default_unimap(svc);
1121 if (!*vc->vc_uni_pagedir_loc)
1122 con_copy_unimap(vc, svc);
1123
e4fc2761 1124 ops = info->fbcon_par;
2428e59b 1125 p->con_rotate = initial_rotation;
b73deed3 1126 set_blitting_type(vc, info);
e4fc2761 1127
1da177e4
LT
1128 cols = vc->vc_cols;
1129 rows = vc->vc_rows;
e4fc2761
AD
1130 new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1131 new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1132 new_cols /= vc->vc_font.width;
1133 new_rows /= vc->vc_font.height;
1da177e4 1134
1da177e4
LT
1135 /*
1136 * We must always set the mode. The mode of the previous console
1137 * driver could be in the same resolution but we are using different
1138 * hardware so we have to initialize the hardware.
1139 *
1140 * We need to do it in fbcon_init() to prevent screen corruption.
1141 */
d354d9af 1142 if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
1da177e4 1143 if (info->fbops->fb_set_par &&
0fcf6ada
FTS
1144 !(ops->flags & FBCON_FLAGS_INIT)) {
1145 ret = info->fbops->fb_set_par(info);
1146
1147 if (ret)
1148 printk(KERN_ERR "fbcon_init: detected "
1149 "unhandled fb_set_par error, "
1150 "error code %d\n", ret);
1151 }
1152
1da177e4
LT
1153 ops->flags |= FBCON_FLAGS_INIT;
1154 }
1155
1156 ops->graphics = 0;
1157
1158 if ((cap & FBINFO_HWACCEL_COPYAREA) &&
1159 !(cap & FBINFO_HWACCEL_DISABLED))
1160 p->scrollmode = SCROLL_MOVE;
1161 else /* default to something safe */
1162 p->scrollmode = SCROLL_REDRAW;
1163
1164 /*
1165 * ++guenther: console.c:vc_allocate() relies on initializing
1166 * vc_{cols,rows}, but we must not set those if we are only
1167 * resizing the console.
1168 */
0035fe00 1169 if (init) {
1da177e4
LT
1170 vc->vc_cols = new_cols;
1171 vc->vc_rows = new_rows;
0035fe00
JW
1172 } else
1173 vc_resize(vc, new_cols, new_rows);
1da177e4
LT
1174
1175 if (logo)
1176 fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1177
4d9c5b6e
AD
1178 if (vc == svc && softback_buf)
1179 fbcon_update_softback(vc);
e4fc2761 1180
b73deed3 1181 if (ops->rotate_font && ops->rotate_font(info, vc)) {
e4fc2761 1182 ops->rotate = FB_ROTATE_UR;
b73deed3 1183 set_blitting_type(vc, info);
e4fc2761
AD
1184 }
1185
1a37d5f5 1186 ops->p = &fb_display[fg_console];
1da177e4
LT
1187}
1188
e614b18d
AD
1189static void fbcon_free_font(struct display *p)
1190{
1191 if (p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1192 kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1193 p->fontdata = NULL;
1194 p->userfont = 0;
1195}
1196
1da177e4
LT
1197static void fbcon_deinit(struct vc_data *vc)
1198{
1199 struct display *p = &fb_display[vc->vc_num];
e614b18d
AD
1200 struct fb_info *info;
1201 struct fbcon_ops *ops;
1202 int idx;
1da177e4 1203
1da177e4 1204 fbcon_free_font(p);
e614b18d
AD
1205 idx = con2fb_map[vc->vc_num];
1206
1207 if (idx == -1)
1208 goto finished;
1209
1210 info = registered_fb[idx];
1211
1212 if (!info)
1213 goto finished;
1214
1215 ops = info->fbcon_par;
1216
1217 if (!ops)
1218 goto finished;
1219
1220 if (CON_IS_VISIBLE(vc))
1221 fbcon_del_cursor_timer(info);
1222
1223 ops->flags &= ~FBCON_FLAGS_INIT;
1224finished:
1225
1226 if (!con_is_bound(&fb_con))
1227 fbcon_exit();
1228
1229 return;
1da177e4
LT
1230}
1231
1232/* ====================================================================== */
1233
1234/* fbcon_XXX routines - interface used by the world
1235 *
1236 * This system is now divided into two levels because of complications
1237 * caused by hardware scrolling. Top level functions:
1238 *
1239 * fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1240 *
1241 * handles y values in range [0, scr_height-1] that correspond to real
1242 * screen positions. y_wrap shift means that first line of bitmap may be
1243 * anywhere on this display. These functions convert lineoffsets to
1244 * bitmap offsets and deal with the wrap-around case by splitting blits.
1245 *
1246 * fbcon_bmove_physical_8() -- These functions fast implementations
1247 * fbcon_clear_physical_8() -- of original fbcon_XXX fns.
1248 * fbcon_putc_physical_8() -- (font width != 8) may be added later
1249 *
1250 * WARNING:
1251 *
1252 * At the moment fbcon_putc() cannot blit across vertical wrap boundary
1253 * Implies should only really hardware scroll in rows. Only reason for
1254 * restriction is simplicity & efficiency at the moment.
1255 */
1256
1da177e4
LT
1257static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1258 int width)
1259{
1260 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1261 struct fbcon_ops *ops = info->fbcon_par;
1262
1263 struct display *p = &fb_display[vc->vc_num];
1264 u_int y_break;
1265
1266 if (fbcon_is_inactive(vc, info))
1267 return;
1268
1269 if (!height || !width)
1270 return;
1271
c213ddf3
KH
1272 if (sy < vc->vc_top && vc->vc_top == logo_lines)
1273 vc->vc_top = 0;
1274
1da177e4
LT
1275 /* Split blits that cross physical y_wrap boundary */
1276
1277 y_break = p->vrows - p->yscroll;
1278 if (sy < y_break && sy + height - 1 >= y_break) {
1279 u_int b = y_break - sy;
1280 ops->clear(vc, info, real_y(p, sy), sx, b, width);
1281 ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1282 width);
1283 } else
1284 ops->clear(vc, info, real_y(p, sy), sx, height, width);
1285}
1286
1287static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1288 int count, int ypos, int xpos)
1289{
1290 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1291 struct display *p = &fb_display[vc->vc_num];
1292 struct fbcon_ops *ops = info->fbcon_par;
1293
1294 if (!fbcon_is_inactive(vc, info))
1295 ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1296 get_color(vc, info, scr_readw(s), 1),
1297 get_color(vc, info, scr_readw(s), 0));
1298}
1299
1300static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1301{
1302 unsigned short chr;
1303
1304 scr_writew(c, &chr);
1305 fbcon_putcs(vc, &chr, 1, ypos, xpos);
1306}
1307
1308static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1309{
1310 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1311 struct fbcon_ops *ops = info->fbcon_par;
1312
1313 if (!fbcon_is_inactive(vc, info))
1314 ops->clear_margins(vc, info, bottom_only);
1315}
1316
1317static void fbcon_cursor(struct vc_data *vc, int mode)
1318{
1319 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1320 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
1321 int y;
1322 int c = scr_readw((u16 *) vc->vc_pos);
1323
d1e23066 1324 if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1da177e4
LT
1325 return;
1326
acba9cd0
AD
1327 if (vc->vc_cursor_type & 0x10)
1328 fbcon_del_cursor_timer(info);
1329 else
1330 fbcon_add_cursor_timer(info);
1331
1da177e4
LT
1332 ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1333 if (mode & CM_SOFTBACK) {
1334 mode &= ~CM_SOFTBACK;
1335 y = softback_lines;
1336 } else {
1337 if (softback_lines)
1338 fbcon_set_origin(vc);
1339 y = 0;
1340 }
1341
b73deed3 1342 ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1da177e4
LT
1343 get_color(vc, info, c, 0));
1344 vbl_cursor_cnt = CURSOR_DRAW_DELAY;
1345}
1346
1347static int scrollback_phys_max = 0;
1348static int scrollback_max = 0;
1349static int scrollback_current = 0;
1350
1da177e4 1351static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
d1baa4ff 1352 int unit)
1da177e4 1353{
d1baa4ff
AD
1354 struct display *p, *t;
1355 struct vc_data **default_mode, *vc;
1356 struct vc_data *svc;
e4fc2761 1357 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
1358 int rows, cols, charcnt = 256;
1359
d1baa4ff
AD
1360 p = &fb_display[unit];
1361
1da177e4
LT
1362 if (var_to_display(p, var, info))
1363 return;
d1baa4ff
AD
1364
1365 vc = vc_cons[unit].d;
1366
1367 if (!vc)
1368 return;
1369
1370 default_mode = vc->vc_display_fg;
1371 svc = *default_mode;
1da177e4 1372 t = &fb_display[svc->vc_num];
d1baa4ff 1373
1da177e4 1374 if (!vc->vc_font.data) {
2f4516db 1375 vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1da177e4
LT
1376 vc->vc_font.width = (*default_mode)->vc_font.width;
1377 vc->vc_font.height = (*default_mode)->vc_font.height;
1378 p->userfont = t->userfont;
1379 if (p->userfont)
1380 REFCOUNT(p->fontdata)++;
1381 }
1382 if (p->userfont)
1383 charcnt = FNTCHARCNT(p->fontdata);
1384
b8c90945
AD
1385 var->activate = FB_ACTIVATE_NOW;
1386 info->var.activate = var->activate;
e4fc2761
AD
1387 var->yoffset = info->var.yoffset;
1388 var->xoffset = info->var.xoffset;
b8c90945 1389 fb_set_var(info, var);
e4fc2761 1390 ops->var = info->var;
b8c90945 1391 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1da177e4
LT
1392 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1393 if (charcnt == 256) {
1394 vc->vc_hi_font_mask = 0;
1395 } else {
1396 vc->vc_hi_font_mask = 0x100;
1397 if (vc->vc_can_do_color)
1398 vc->vc_complement_mask <<= 1;
1399 }
1400
1401 if (!*svc->vc_uni_pagedir_loc)
1402 con_set_default_unimap(svc);
1403 if (!*vc->vc_uni_pagedir_loc)
1404 con_copy_unimap(vc, svc);
1405
e4fc2761
AD
1406 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1407 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1408 cols /= vc->vc_font.width;
1409 rows /= vc->vc_font.height;
1da177e4 1410 vc_resize(vc, cols, rows);
e4fc2761 1411
1da177e4
LT
1412 if (CON_IS_VISIBLE(vc)) {
1413 update_screen(vc);
4d9c5b6e
AD
1414 if (softback_buf)
1415 fbcon_update_softback(vc);
1da177e4
LT
1416 }
1417}
1418
1419static __inline__ void ywrap_up(struct vc_data *vc, int count)
1420{
1421 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
e4fc2761 1422 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
1423 struct display *p = &fb_display[vc->vc_num];
1424
1425 p->yscroll += count;
1426 if (p->yscroll >= p->vrows) /* Deal with wrap */
1427 p->yscroll -= p->vrows;
e4fc2761
AD
1428 ops->var.xoffset = 0;
1429 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1430 ops->var.vmode |= FB_VMODE_YWRAP;
1431 ops->update_start(info);
1da177e4
LT
1432 scrollback_max += count;
1433 if (scrollback_max > scrollback_phys_max)
1434 scrollback_max = scrollback_phys_max;
1435 scrollback_current = 0;
1436}
1437
1438static __inline__ void ywrap_down(struct vc_data *vc, int count)
1439{
1440 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
e4fc2761 1441 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
1442 struct display *p = &fb_display[vc->vc_num];
1443
1444 p->yscroll -= count;
1445 if (p->yscroll < 0) /* Deal with wrap */
1446 p->yscroll += p->vrows;
e4fc2761
AD
1447 ops->var.xoffset = 0;
1448 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1449 ops->var.vmode |= FB_VMODE_YWRAP;
1450 ops->update_start(info);
1da177e4
LT
1451 scrollback_max -= count;
1452 if (scrollback_max < 0)
1453 scrollback_max = 0;
1454 scrollback_current = 0;
1455}
1456
1457static __inline__ void ypan_up(struct vc_data *vc, int count)
1458{
1459 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1460 struct display *p = &fb_display[vc->vc_num];
1461 struct fbcon_ops *ops = info->fbcon_par;
1462
1463 p->yscroll += count;
1464 if (p->yscroll > p->vrows - vc->vc_rows) {
1465 ops->bmove(vc, info, p->vrows - vc->vc_rows,
1466 0, 0, 0, vc->vc_rows, vc->vc_cols);
1467 p->yscroll -= p->vrows - vc->vc_rows;
1468 }
e4fc2761
AD
1469
1470 ops->var.xoffset = 0;
1471 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1472 ops->var.vmode &= ~FB_VMODE_YWRAP;
1473 ops->update_start(info);
1da177e4
LT
1474 fbcon_clear_margins(vc, 1);
1475 scrollback_max += count;
1476 if (scrollback_max > scrollback_phys_max)
1477 scrollback_max = scrollback_phys_max;
1478 scrollback_current = 0;
1479}
1480
1481static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1482{
1483 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
e4fc2761 1484 struct fbcon_ops *ops = info->fbcon_par;
1da177e4 1485 struct display *p = &fb_display[vc->vc_num];
1da177e4
LT
1486
1487 p->yscroll += count;
a39bc34e 1488
1da177e4
LT
1489 if (p->yscroll > p->vrows - vc->vc_rows) {
1490 p->yscroll -= p->vrows - vc->vc_rows;
1da177e4 1491 fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
a39bc34e 1492 }
e4fc2761
AD
1493
1494 ops->var.xoffset = 0;
1495 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1496 ops->var.vmode &= ~FB_VMODE_YWRAP;
1497 ops->update_start(info);
1da177e4
LT
1498 fbcon_clear_margins(vc, 1);
1499 scrollback_max += count;
1500 if (scrollback_max > scrollback_phys_max)
1501 scrollback_max = scrollback_phys_max;
1502 scrollback_current = 0;
1503}
1504
1505static __inline__ void ypan_down(struct vc_data *vc, int count)
1506{
1507 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1508 struct display *p = &fb_display[vc->vc_num];
1509 struct fbcon_ops *ops = info->fbcon_par;
1510
1511 p->yscroll -= count;
1512 if (p->yscroll < 0) {
1513 ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1514 0, vc->vc_rows, vc->vc_cols);
1515 p->yscroll += p->vrows - vc->vc_rows;
1516 }
e4fc2761
AD
1517
1518 ops->var.xoffset = 0;
1519 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1520 ops->var.vmode &= ~FB_VMODE_YWRAP;
1521 ops->update_start(info);
1da177e4
LT
1522 fbcon_clear_margins(vc, 1);
1523 scrollback_max -= count;
1524 if (scrollback_max < 0)
1525 scrollback_max = 0;
1526 scrollback_current = 0;
1527}
1528
1529static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1530{
1531 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
e4fc2761 1532 struct fbcon_ops *ops = info->fbcon_par;
1da177e4 1533 struct display *p = &fb_display[vc->vc_num];
1da177e4
LT
1534
1535 p->yscroll -= count;
a39bc34e 1536
1da177e4
LT
1537 if (p->yscroll < 0) {
1538 p->yscroll += p->vrows - vc->vc_rows;
1da177e4 1539 fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
a39bc34e 1540 }
e4fc2761
AD
1541
1542 ops->var.xoffset = 0;
1543 ops->var.yoffset = p->yscroll * vc->vc_font.height;
1544 ops->var.vmode &= ~FB_VMODE_YWRAP;
1545 ops->update_start(info);
1da177e4
LT
1546 fbcon_clear_margins(vc, 1);
1547 scrollback_max -= count;
1548 if (scrollback_max < 0)
1549 scrollback_max = 0;
1550 scrollback_current = 0;
1551}
1552
1553static void fbcon_redraw_softback(struct vc_data *vc, struct display *p,
1554 long delta)
1555{
1556 int count = vc->vc_rows;
1557 unsigned short *d, *s;
1558 unsigned long n;
1559 int line = 0;
1560
1561 d = (u16 *) softback_curr;
1562 if (d == (u16 *) softback_in)
1563 d = (u16 *) vc->vc_origin;
1564 n = softback_curr + delta * vc->vc_size_row;
1565 softback_lines -= delta;
1566 if (delta < 0) {
1567 if (softback_curr < softback_top && n < softback_buf) {
1568 n += softback_end - softback_buf;
1569 if (n < softback_top) {
1570 softback_lines -=
1571 (softback_top - n) / vc->vc_size_row;
1572 n = softback_top;
1573 }
1574 } else if (softback_curr >= softback_top
1575 && n < softback_top) {
1576 softback_lines -=
1577 (softback_top - n) / vc->vc_size_row;
1578 n = softback_top;
1579 }
1580 } else {
1581 if (softback_curr > softback_in && n >= softback_end) {
1582 n += softback_buf - softback_end;
1583 if (n > softback_in) {
1584 n = softback_in;
1585 softback_lines = 0;
1586 }
1587 } else if (softback_curr <= softback_in && n > softback_in) {
1588 n = softback_in;
1589 softback_lines = 0;
1590 }
1591 }
1592 if (n == softback_curr)
1593 return;
1594 softback_curr = n;
1595 s = (u16 *) softback_curr;
1596 if (s == (u16 *) softback_in)
1597 s = (u16 *) vc->vc_origin;
1598 while (count--) {
1599 unsigned short *start;
1600 unsigned short *le;
1601 unsigned short c;
1602 int x = 0;
1603 unsigned short attr = 1;
1604
1605 start = s;
1606 le = advance_row(s, 1);
1607 do {
1608 c = scr_readw(s);
1609 if (attr != (c & 0xff00)) {
1610 attr = c & 0xff00;
1611 if (s > start) {
1612 fbcon_putcs(vc, start, s - start,
1613 line, x);
1614 x += s - start;
1615 start = s;
1616 }
1617 }
1618 if (c == scr_readw(d)) {
1619 if (s > start) {
1620 fbcon_putcs(vc, start, s - start,
1621 line, x);
1622 x += s - start + 1;
1623 start = s + 1;
1624 } else {
1625 x++;
1626 start++;
1627 }
1628 }
1629 s++;
1630 d++;
1631 } while (s < le);
1632 if (s > start)
1633 fbcon_putcs(vc, start, s - start, line, x);
1634 line++;
1635 if (d == (u16 *) softback_end)
1636 d = (u16 *) softback_buf;
1637 if (d == (u16 *) softback_in)
1638 d = (u16 *) vc->vc_origin;
1639 if (s == (u16 *) softback_end)
1640 s = (u16 *) softback_buf;
1641 if (s == (u16 *) softback_in)
1642 s = (u16 *) vc->vc_origin;
1643 }
1644}
1645
1646static void fbcon_redraw_move(struct vc_data *vc, struct display *p,
1647 int line, int count, int dy)
1648{
1649 unsigned short *s = (unsigned short *)
1650 (vc->vc_origin + vc->vc_size_row * line);
1651
1652 while (count--) {
1653 unsigned short *start = s;
1654 unsigned short *le = advance_row(s, 1);
1655 unsigned short c;
1656 int x = 0;
1657 unsigned short attr = 1;
1658
1659 do {
1660 c = scr_readw(s);
1661 if (attr != (c & 0xff00)) {
1662 attr = c & 0xff00;
1663 if (s > start) {
1664 fbcon_putcs(vc, start, s - start,
1665 dy, x);
1666 x += s - start;
1667 start = s;
1668 }
1669 }
1670 console_conditional_schedule();
1671 s++;
1672 } while (s < le);
1673 if (s > start)
1674 fbcon_putcs(vc, start, s - start, dy, x);
1675 console_conditional_schedule();
1676 dy++;
1677 }
1678}
1679
bad07ff7
KH
1680static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1681 struct display *p, int line, int count, int ycount)
1682{
1683 int offset = ycount * vc->vc_cols;
1684 unsigned short *d = (unsigned short *)
1685 (vc->vc_origin + vc->vc_size_row * line);
1686 unsigned short *s = d + offset;
1687 struct fbcon_ops *ops = info->fbcon_par;
1688
1689 while (count--) {
1690 unsigned short *start = s;
1691 unsigned short *le = advance_row(s, 1);
1692 unsigned short c;
1693 int x = 0;
1694
1695 do {
1696 c = scr_readw(s);
1697
1698 if (c == scr_readw(d)) {
1699 if (s > start) {
1700 ops->bmove(vc, info, line + ycount, x,
1701 line, x, 1, s-start);
1702 x += s - start + 1;
1703 start = s + 1;
1704 } else {
1705 x++;
1706 start++;
1707 }
1708 }
1709
1710 scr_writew(c, d);
1711 console_conditional_schedule();
1712 s++;
1713 d++;
1714 } while (s < le);
1715 if (s > start)
1716 ops->bmove(vc, info, line + ycount, x, line, x, 1,
1717 s-start);
1718 console_conditional_schedule();
1719 if (ycount > 0)
1720 line++;
1721 else {
1722 line--;
1723 /* NOTE: We subtract two lines from these pointers */
1724 s -= vc->vc_size_row;
1725 d -= vc->vc_size_row;
1726 }
1727 }
1728}
1729
1da177e4
LT
1730static void fbcon_redraw(struct vc_data *vc, struct display *p,
1731 int line, int count, int offset)
1732{
1733 unsigned short *d = (unsigned short *)
1734 (vc->vc_origin + vc->vc_size_row * line);
1735 unsigned short *s = d + offset;
1736
1737 while (count--) {
1738 unsigned short *start = s;
1739 unsigned short *le = advance_row(s, 1);
1740 unsigned short c;
1741 int x = 0;
1742 unsigned short attr = 1;
1743
1744 do {
1745 c = scr_readw(s);
1746 if (attr != (c & 0xff00)) {
1747 attr = c & 0xff00;
1748 if (s > start) {
1749 fbcon_putcs(vc, start, s - start,
1750 line, x);
1751 x += s - start;
1752 start = s;
1753 }
1754 }
1755 if (c == scr_readw(d)) {
1756 if (s > start) {
1757 fbcon_putcs(vc, start, s - start,
1758 line, x);
1759 x += s - start + 1;
1760 start = s + 1;
1761 } else {
1762 x++;
1763 start++;
1764 }
1765 }
1766 scr_writew(c, d);
1767 console_conditional_schedule();
1768 s++;
1769 d++;
1770 } while (s < le);
1771 if (s > start)
1772 fbcon_putcs(vc, start, s - start, line, x);
1773 console_conditional_schedule();
1774 if (offset > 0)
1775 line++;
1776 else {
1777 line--;
1778 /* NOTE: We subtract two lines from these pointers */
1779 s -= vc->vc_size_row;
1780 d -= vc->vc_size_row;
1781 }
1782 }
1783}
1784
1785static inline void fbcon_softback_note(struct vc_data *vc, int t,
1786 int count)
1787{
1788 unsigned short *p;
1789
1790 if (vc->vc_num != fg_console)
1791 return;
1792 p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1793
1794 while (count) {
1795 scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1796 count--;
1797 p = advance_row(p, 1);
1798 softback_in += vc->vc_size_row;
1799 if (softback_in == softback_end)
1800 softback_in = softback_buf;
1801 if (softback_in == softback_top) {
1802 softback_top += vc->vc_size_row;
1803 if (softback_top == softback_end)
1804 softback_top = softback_buf;
1805 }
1806 }
1807 softback_curr = softback_in;
1808}
1809
1810static int fbcon_scroll(struct vc_data *vc, int t, int b, int dir,
1811 int count)
1812{
1813 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1814 struct display *p = &fb_display[vc->vc_num];
1da177e4
LT
1815 int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1816
1817 if (fbcon_is_inactive(vc, info))
1818 return -EINVAL;
1819
1820 fbcon_cursor(vc, CM_ERASE);
1821
1822 /*
1823 * ++Geert: Only use ywrap/ypan if the console is in text mode
1824 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1825 * whole screen (prevents flicker).
1826 */
1827
1828 switch (dir) {
1829 case SM_UP:
1830 if (count > vc->vc_rows) /* Maximum realistic size */
1831 count = vc->vc_rows;
1832 if (softback_top)
1833 fbcon_softback_note(vc, t, count);
1834 if (logo_shown >= 0)
1835 goto redraw_up;
1836 switch (p->scrollmode) {
1837 case SCROLL_MOVE:
bad07ff7
KH
1838 fbcon_redraw_blit(vc, info, p, t, b - t - count,
1839 count);
1840 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1841 scr_memsetw((unsigned short *) (vc->vc_origin +
1842 vc->vc_size_row *
1843 (b - count)),
93f78da4 1844 vc->vc_video_erase_char,
bad07ff7 1845 vc->vc_size_row * count);
93f78da4 1846 return 1;
1da177e4
LT
1847 break;
1848
1849 case SCROLL_WRAP_MOVE:
1850 if (b - t - count > 3 * vc->vc_rows >> 2) {
1851 if (t > 0)
1852 fbcon_bmove(vc, 0, 0, count, 0, t,
1853 vc->vc_cols);
1854 ywrap_up(vc, count);
1855 if (vc->vc_rows - b > 0)
1856 fbcon_bmove(vc, b - count, 0, b, 0,
1857 vc->vc_rows - b,
1858 vc->vc_cols);
1859 } else if (info->flags & FBINFO_READS_FAST)
1860 fbcon_bmove(vc, t + count, 0, t, 0,
1861 b - t - count, vc->vc_cols);
1862 else
1863 goto redraw_up;
1864 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1865 break;
1866
1867 case SCROLL_PAN_REDRAW:
1868 if ((p->yscroll + count <=
1869 2 * (p->vrows - vc->vc_rows))
1870 && ((!scroll_partial && (b - t == vc->vc_rows))
1871 || (scroll_partial
1872 && (b - t - count >
1873 3 * vc->vc_rows >> 2)))) {
1874 if (t > 0)
1875 fbcon_redraw_move(vc, p, 0, t, count);
1876 ypan_up_redraw(vc, t, count);
1877 if (vc->vc_rows - b > 0)
26e780e8 1878 fbcon_redraw_move(vc, p, b,
1da177e4
LT
1879 vc->vc_rows - b, b);
1880 } else
1881 fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1882 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1883 break;
1884
1885 case SCROLL_PAN_MOVE:
1886 if ((p->yscroll + count <=
1887 2 * (p->vrows - vc->vc_rows))
1888 && ((!scroll_partial && (b - t == vc->vc_rows))
1889 || (scroll_partial
1890 && (b - t - count >
1891 3 * vc->vc_rows >> 2)))) {
1892 if (t > 0)
1893 fbcon_bmove(vc, 0, 0, count, 0, t,
1894 vc->vc_cols);
1895 ypan_up(vc, count);
1896 if (vc->vc_rows - b > 0)
1897 fbcon_bmove(vc, b - count, 0, b, 0,
1898 vc->vc_rows - b,
1899 vc->vc_cols);
1900 } else if (info->flags & FBINFO_READS_FAST)
1901 fbcon_bmove(vc, t + count, 0, t, 0,
1902 b - t - count, vc->vc_cols);
1903 else
1904 goto redraw_up;
1905 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1906 break;
1907
1908 case SCROLL_REDRAW:
1909 redraw_up:
1910 fbcon_redraw(vc, p, t, b - t - count,
1911 count * vc->vc_cols);
1912 fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1913 scr_memsetw((unsigned short *) (vc->vc_origin +
1914 vc->vc_size_row *
1915 (b - count)),
93f78da4 1916 vc->vc_video_erase_char,
1da177e4 1917 vc->vc_size_row * count);
93f78da4 1918 return 1;
1da177e4
LT
1919 }
1920 break;
1921
1922 case SM_DOWN:
1923 if (count > vc->vc_rows) /* Maximum realistic size */
1924 count = vc->vc_rows;
e703ecc3
JB
1925 if (logo_shown >= 0)
1926 goto redraw_down;
1da177e4
LT
1927 switch (p->scrollmode) {
1928 case SCROLL_MOVE:
bad07ff7
KH
1929 fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1930 -count);
1931 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1932 scr_memsetw((unsigned short *) (vc->vc_origin +
1933 vc->vc_size_row *
1934 t),
93f78da4 1935 vc->vc_video_erase_char,
bad07ff7 1936 vc->vc_size_row * count);
93f78da4 1937 return 1;
1da177e4
LT
1938 break;
1939
1940 case SCROLL_WRAP_MOVE:
1941 if (b - t - count > 3 * vc->vc_rows >> 2) {
1942 if (vc->vc_rows - b > 0)
1943 fbcon_bmove(vc, b, 0, b - count, 0,
1944 vc->vc_rows - b,
1945 vc->vc_cols);
1946 ywrap_down(vc, count);
1947 if (t > 0)
1948 fbcon_bmove(vc, count, 0, 0, 0, t,
1949 vc->vc_cols);
1950 } else if (info->flags & FBINFO_READS_FAST)
1951 fbcon_bmove(vc, t, 0, t + count, 0,
1952 b - t - count, vc->vc_cols);
1953 else
1954 goto redraw_down;
1955 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1956 break;
1957
1958 case SCROLL_PAN_MOVE:
1959 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1960 && ((!scroll_partial && (b - t == vc->vc_rows))
1961 || (scroll_partial
1962 && (b - t - count >
1963 3 * vc->vc_rows >> 2)))) {
1964 if (vc->vc_rows - b > 0)
1965 fbcon_bmove(vc, b, 0, b - count, 0,
1966 vc->vc_rows - b,
1967 vc->vc_cols);
1968 ypan_down(vc, count);
1969 if (t > 0)
1970 fbcon_bmove(vc, count, 0, 0, 0, t,
1971 vc->vc_cols);
1972 } else if (info->flags & FBINFO_READS_FAST)
1973 fbcon_bmove(vc, t, 0, t + count, 0,
1974 b - t - count, vc->vc_cols);
1975 else
1976 goto redraw_down;
1977 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1978 break;
1979
1980 case SCROLL_PAN_REDRAW:
1981 if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1982 && ((!scroll_partial && (b - t == vc->vc_rows))
1983 || (scroll_partial
1984 && (b - t - count >
1985 3 * vc->vc_rows >> 2)))) {
1986 if (vc->vc_rows - b > 0)
1987 fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
1988 b - count);
1989 ypan_down_redraw(vc, t, count);
1990 if (t > 0)
1991 fbcon_redraw_move(vc, p, count, t, 0);
1992 } else
1993 fbcon_redraw_move(vc, p, t, b - t - count, t + count);
1994 fbcon_clear(vc, t, 0, count, vc->vc_cols);
1995 break;
1996
1997 case SCROLL_REDRAW:
1998 redraw_down:
1999 fbcon_redraw(vc, p, b - 1, b - t - count,
2000 -count * vc->vc_cols);
2001 fbcon_clear(vc, t, 0, count, vc->vc_cols);
2002 scr_memsetw((unsigned short *) (vc->vc_origin +
2003 vc->vc_size_row *
2004 t),
93f78da4 2005 vc->vc_video_erase_char,
1da177e4 2006 vc->vc_size_row * count);
93f78da4 2007 return 1;
1da177e4
LT
2008 }
2009 }
93f78da4 2010 return 0;
1da177e4
LT
2011}
2012
2013
2014static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
2015 int height, int width)
2016{
2017 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2018 struct display *p = &fb_display[vc->vc_num];
2019
2020 if (fbcon_is_inactive(vc, info))
2021 return;
2022
2023 if (!width || !height)
2024 return;
2025
2026 /* Split blits that cross physical y_wrap case.
2027 * Pathological case involves 4 blits, better to use recursive
2028 * code rather than unrolled case
2029 *
2030 * Recursive invocations don't need to erase the cursor over and
2031 * over again, so we use fbcon_bmove_rec()
2032 */
2033 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2034 p->vrows - p->yscroll);
2035}
2036
2037static void fbcon_bmove_rec(struct vc_data *vc, struct display *p, int sy, int sx,
2038 int dy, int dx, int height, int width, u_int y_break)
2039{
2040 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2041 struct fbcon_ops *ops = info->fbcon_par;
2042 u_int b;
2043
2044 if (sy < y_break && sy + height > y_break) {
2045 b = y_break - sy;
2046 if (dy < sy) { /* Avoid trashing self */
2047 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2048 y_break);
2049 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2050 height - b, width, y_break);
2051 } else {
2052 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2053 height - b, width, y_break);
2054 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2055 y_break);
2056 }
2057 return;
2058 }
2059
2060 if (dy < y_break && dy + height > y_break) {
2061 b = y_break - dy;
2062 if (dy < sy) { /* Avoid trashing self */
2063 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2064 y_break);
2065 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2066 height - b, width, y_break);
2067 } else {
2068 fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2069 height - b, width, y_break);
2070 fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2071 y_break);
2072 }
2073 return;
2074 }
2075 ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2076 height, width);
2077}
2078
def1be2d 2079static void updatescrollmode(struct display *p,
e4fc2761 2080 struct fb_info *info,
1da177e4
LT
2081 struct vc_data *vc)
2082{
e4fc2761 2083 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
2084 int fh = vc->vc_font.height;
2085 int cap = info->flags;
e4fc2761
AD
2086 u16 t = 0;
2087 int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2088 info->fix.xpanstep);
2089 int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2090 int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2091 int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2092 info->var.xres_virtual);
2093 int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2094 divides(ypan, vc->vc_font.height) && vyres > yres;
2095 int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2096 divides(ywrap, vc->vc_font.height) &&
244ab72d
KP
2097 divides(vc->vc_font.height, vyres) &&
2098 divides(vc->vc_font.height, yres);
1da177e4 2099 int reading_fast = cap & FBINFO_READS_FAST;
e4fc2761
AD
2100 int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2101 !(cap & FBINFO_HWACCEL_DISABLED);
2102 int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2103 !(cap & FBINFO_HWACCEL_DISABLED);
2104
2105 p->vrows = vyres/fh;
2106 if (yres > (fh * (vc->vc_rows + 1)))
2107 p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2108 if ((yres % fh) && (vyres % fh < yres % fh))
1da177e4
LT
2109 p->vrows--;
2110
2111 if (good_wrap || good_pan) {
2112 if (reading_fast || fast_copyarea)
e4fc2761
AD
2113 p->scrollmode = good_wrap ?
2114 SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
1da177e4
LT
2115 else
2116 p->scrollmode = good_wrap ? SCROLL_REDRAW :
2117 SCROLL_PAN_REDRAW;
2118 } else {
2119 if (reading_fast || (fast_copyarea && !fast_imageblit))
2120 p->scrollmode = SCROLL_MOVE;
2121 else
2122 p->scrollmode = SCROLL_REDRAW;
2123 }
2124}
2125
2126static int fbcon_resize(struct vc_data *vc, unsigned int width,
e400b6ec 2127 unsigned int height, unsigned int user)
1da177e4
LT
2128{
2129 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
e4fc2761 2130 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
2131 struct display *p = &fb_display[vc->vc_num];
2132 struct fb_var_screeninfo var = info->var;
e4fc2761
AD
2133 int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2134
2135 virt_w = FBCON_SWAP(ops->rotate, width, height);
2136 virt_h = FBCON_SWAP(ops->rotate, height, width);
2137 virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2138 vc->vc_font.height);
2139 virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2140 vc->vc_font.width);
2141 var.xres = virt_w * virt_fw;
2142 var.yres = virt_h * virt_fh;
1da177e4
LT
2143 x_diff = info->var.xres - var.xres;
2144 y_diff = info->var.yres - var.yres;
e4fc2761
AD
2145 if (x_diff < 0 || x_diff > virt_fw ||
2146 y_diff < 0 || y_diff > virt_fh) {
9791d763 2147 const struct fb_videomode *mode;
1da177e4
LT
2148
2149 DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2150 mode = fb_find_best_mode(&var, &info->modelist);
2151 if (mode == NULL)
2152 return -EINVAL;
3084a895 2153 display_to_var(&var, p);
1da177e4 2154 fb_videomode_to_var(&var, mode);
3084a895 2155
e4fc2761 2156 if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
1da177e4 2157 return -EINVAL;
1da177e4
LT
2158
2159 DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2160 if (CON_IS_VISIBLE(vc)) {
2161 var.activate = FB_ACTIVATE_NOW |
2162 FB_ACTIVATE_FORCE;
2163 fb_set_var(info, &var);
2164 }
2165 var_to_display(p, &info->var, info);
e4fc2761 2166 ops->var = info->var;
1da177e4
LT
2167 }
2168 updatescrollmode(p, info, vc);
2169 return 0;
2170}
2171
2172static int fbcon_switch(struct vc_data *vc)
2173{
88fb2c6e 2174 struct fb_info *info, *old_info = NULL;
e4fc2761 2175 struct fbcon_ops *ops;
1da177e4
LT
2176 struct display *p = &fb_display[vc->vc_num];
2177 struct fb_var_screeninfo var;
0fcf6ada 2178 int i, ret, prev_console, charcnt = 256;
1da177e4
LT
2179
2180 info = registered_fb[con2fb_map[vc->vc_num]];
e4fc2761 2181 ops = info->fbcon_par;
1da177e4
LT
2182
2183 if (softback_top) {
1da177e4
LT
2184 if (softback_lines)
2185 fbcon_set_origin(vc);
2186 softback_top = softback_curr = softback_in = softback_buf;
2187 softback_lines = 0;
4d9c5b6e 2188 fbcon_update_softback(vc);
1da177e4
LT
2189 }
2190
2191 if (logo_shown >= 0) {
2192 struct vc_data *conp2 = vc_cons[logo_shown].d;
2193
2194 if (conp2->vc_top == logo_lines
2195 && conp2->vc_bottom == conp2->vc_rows)
2196 conp2->vc_top = 0;
2197 logo_shown = FBCON_LOGO_CANSHOW;
2198 }
2199
e4fc2761 2200 prev_console = ops->currcon;
88fb2c6e
AD
2201 if (prev_console != -1)
2202 old_info = registered_fb[con2fb_map[prev_console]];
1da177e4
LT
2203 /*
2204 * FIXME: If we have multiple fbdev's loaded, we need to
2205 * update all info->currcon. Perhaps, we can place this
2206 * in a centralized structure, but this might break some
2207 * drivers.
2208 *
2209 * info->currcon = vc->vc_num;
2210 */
2211 for (i = 0; i < FB_MAX; i++) {
2212 if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
e4fc2761 2213 struct fbcon_ops *o = registered_fb[i]->fbcon_par;
1da177e4 2214
e4fc2761 2215 o->currcon = vc->vc_num;
1da177e4
LT
2216 }
2217 }
2218 memset(&var, 0, sizeof(struct fb_var_screeninfo));
2219 display_to_var(&var, p);
2220 var.activate = FB_ACTIVATE_NOW;
2221
2222 /*
2223 * make sure we don't unnecessarily trip the memcmp()
2224 * in fb_set_var()
2225 */
2226 info->var.activate = var.activate;
10732c35 2227 var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
1da177e4 2228 fb_set_var(info, &var);
e4fc2761 2229 ops->var = info->var;
1da177e4 2230
39942fd8
KP
2231 if (old_info != NULL && (old_info != info ||
2232 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
0fcf6ada
FTS
2233 if (info->fbops->fb_set_par) {
2234 ret = info->fbops->fb_set_par(info);
2235
2236 if (ret)
2237 printk(KERN_ERR "fbcon_switch: detected "
2238 "unhandled fb_set_par error, "
2239 "error code %d\n", ret);
2240 }
a39bc34e 2241
5428b044 2242 if (old_info != info)
a39bc34e 2243 fbcon_del_cursor_timer(old_info);
88fb2c6e 2244 }
1da177e4 2245
212f2639
AD
2246 if (fbcon_is_inactive(vc, info) ||
2247 ops->blank_state != FB_BLANK_UNBLANK)
2248 fbcon_del_cursor_timer(info);
2249 else
2250 fbcon_add_cursor_timer(info);
2251
b73deed3 2252 set_blitting_type(vc, info);
e4fc2761
AD
2253 ops->cursor_reset = 1;
2254
b73deed3 2255 if (ops->rotate_font && ops->rotate_font(info, vc)) {
e4fc2761 2256 ops->rotate = FB_ROTATE_UR;
b73deed3 2257 set_blitting_type(vc, info);
e4fc2761 2258 }
1da177e4 2259
b8c90945 2260 vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1da177e4 2261 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
56f0d64d
AD
2262
2263 if (p->userfont)
2264 charcnt = FNTCHARCNT(vc->vc_font.data);
2265
2266 if (charcnt > 256)
2267 vc->vc_complement_mask <<= 1;
2268
1da177e4
LT
2269 updatescrollmode(p, info, vc);
2270
2271 switch (p->scrollmode) {
2272 case SCROLL_WRAP_MOVE:
2273 scrollback_phys_max = p->vrows - vc->vc_rows;
2274 break;
2275 case SCROLL_PAN_MOVE:
2276 case SCROLL_PAN_REDRAW:
2277 scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2278 if (scrollback_phys_max < 0)
2279 scrollback_phys_max = 0;
2280 break;
2281 default:
2282 scrollback_phys_max = 0;
2283 break;
2284 }
e4fc2761 2285
1da177e4
LT
2286 scrollback_max = 0;
2287 scrollback_current = 0;
4e1567d3
AD
2288
2289 if (!fbcon_is_inactive(vc, info)) {
2290 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2291 ops->update_start(info);
2292 }
2293
1da177e4
LT
2294 fbcon_set_palette(vc, color_table);
2295 fbcon_clear_margins(vc, 0);
2296
2297 if (logo_shown == FBCON_LOGO_DRAW) {
2298
2299 logo_shown = fg_console;
2300 /* This is protected above by initmem_freed */
9c44e5f6 2301 fb_show_logo(info, ops->rotate);
1da177e4
LT
2302 update_region(vc,
2303 vc->vc_origin + vc->vc_size_row * vc->vc_top,
2304 vc->vc_size_row * (vc->vc_bottom -
2305 vc->vc_top) / 2);
2306 return 0;
2307 }
2308 return 1;
2309}
2310
2311static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2312 int blank)
2313{
994efacd
RP
2314 struct fb_event event;
2315
1da177e4
LT
2316 if (blank) {
2317 unsigned short charmask = vc->vc_hi_font_mask ?
2318 0x1ff : 0xff;
2319 unsigned short oldc;
2320
2321 oldc = vc->vc_video_erase_char;
2322 vc->vc_video_erase_char &= charmask;
2323 fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2324 vc->vc_video_erase_char = oldc;
2325 }
994efacd
RP
2326
2327
513adb58
AR
2328 if (!lock_fb_info(info))
2329 return;
994efacd
RP
2330 event.info = info;
2331 event.data = &blank;
2332 fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
513adb58 2333 unlock_fb_info(info);
1da177e4
LT
2334}
2335
2336static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2337{
2338 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2339 struct fbcon_ops *ops = info->fbcon_par;
2340
2341 if (mode_switch) {
2342 struct fb_var_screeninfo var = info->var;
2343
2344 ops->graphics = 1;
2345
2346 if (!blank) {
2347 var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2348 fb_set_var(info, &var);
2349 ops->graphics = 0;
e4fc2761 2350 ops->var = info->var;
f5c15d0b 2351 }
1da177e4
LT
2352 }
2353
2354 if (!fbcon_is_inactive(vc, info)) {
2355 if (ops->blank_state != blank) {
2356 ops->blank_state = blank;
2357 fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2358 ops->cursor_flash = (!blank);
2359
bca404af
DES
2360 if (!(info->flags & FBINFO_MISC_USEREVENT))
2361 if (fb_blank(info, blank))
2362 fbcon_generic_blank(vc, info, blank);
1da177e4
LT
2363 }
2364
88fb2c6e
AD
2365 if (!blank)
2366 update_screen(vc);
2367 }
2368
4d8a2d98 2369 if (mode_switch || fbcon_is_inactive(vc, info) ||
212f2639 2370 ops->blank_state != FB_BLANK_UNBLANK)
88fb2c6e 2371 fbcon_del_cursor_timer(info);
212f2639
AD
2372 else
2373 fbcon_add_cursor_timer(info);
1da177e4 2374
88fb2c6e 2375 return 0;
1da177e4
LT
2376}
2377
d219adc1
JB
2378static int fbcon_debug_enter(struct vc_data *vc)
2379{
2380 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2381 struct fbcon_ops *ops = info->fbcon_par;
2382
2383 ops->save_graphics = ops->graphics;
2384 ops->graphics = 0;
2385 if (info->fbops->fb_debug_enter)
2386 info->fbops->fb_debug_enter(info);
2387 fbcon_set_palette(vc, color_table);
2388 return 0;
2389}
2390
2391static int fbcon_debug_leave(struct vc_data *vc)
2392{
2393 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2394 struct fbcon_ops *ops = info->fbcon_par;
2395
2396 ops->graphics = ops->save_graphics;
2397 if (info->fbops->fb_debug_leave)
2398 info->fbops->fb_debug_leave(info);
2399 return 0;
2400}
2401
1da177e4
LT
2402static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2403{
2404 u8 *fontdata = vc->vc_font.data;
2405 u8 *data = font->data;
2406 int i, j;
2407
2408 font->width = vc->vc_font.width;
2409 font->height = vc->vc_font.height;
2410 font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2411 if (!font->data)
2412 return 0;
2413
2414 if (font->width <= 8) {
2415 j = vc->vc_font.height;
2416 for (i = 0; i < font->charcount; i++) {
2417 memcpy(data, fontdata, j);
2418 memset(data + j, 0, 32 - j);
2419 data += 32;
2420 fontdata += j;
2421 }
2422 } else if (font->width <= 16) {
2423 j = vc->vc_font.height * 2;
2424 for (i = 0; i < font->charcount; i++) {
2425 memcpy(data, fontdata, j);
2426 memset(data + j, 0, 64 - j);
2427 data += 64;
2428 fontdata += j;
2429 }
2430 } else if (font->width <= 24) {
2431 for (i = 0; i < font->charcount; i++) {
2432 for (j = 0; j < vc->vc_font.height; j++) {
2433 *data++ = fontdata[0];
2434 *data++ = fontdata[1];
2435 *data++ = fontdata[2];
2436 fontdata += sizeof(u32);
2437 }
2438 memset(data, 0, 3 * (32 - j));
2439 data += 3 * (32 - j);
2440 }
2441 } else {
2442 j = vc->vc_font.height * 4;
2443 for (i = 0; i < font->charcount; i++) {
2444 memcpy(data, fontdata, j);
2445 memset(data + j, 0, 128 - j);
2446 data += 128;
2447 fontdata += j;
2448 }
2449 }
2450 return 0;
2451}
2452
2453static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2f4516db 2454 const u8 * data, int userfont)
1da177e4
LT
2455{
2456 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
e4fc2761 2457 struct fbcon_ops *ops = info->fbcon_par;
1da177e4
LT
2458 struct display *p = &fb_display[vc->vc_num];
2459 int resize;
2460 int cnt;
2461 char *old_data = NULL;
2462
2463 if (CON_IS_VISIBLE(vc) && softback_lines)
2464 fbcon_set_origin(vc);
2465
2466 resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2467 if (p->userfont)
2468 old_data = vc->vc_font.data;
2469 if (userfont)
2470 cnt = FNTCHARCNT(data);
2471 else
2472 cnt = 256;
2f4516db 2473 vc->vc_font.data = (void *)(p->fontdata = data);
1da177e4
LT
2474 if ((p->userfont = userfont))
2475 REFCOUNT(data)++;
2476 vc->vc_font.width = w;
2477 vc->vc_font.height = h;
2478 if (vc->vc_hi_font_mask && cnt == 256) {
2479 vc->vc_hi_font_mask = 0;
2480 if (vc->vc_can_do_color) {
2481 vc->vc_complement_mask >>= 1;
2482 vc->vc_s_complement_mask >>= 1;
2483 }
2484
2485 /* ++Edmund: reorder the attribute bits */
2486 if (vc->vc_can_do_color) {
2487 unsigned short *cp =
2488 (unsigned short *) vc->vc_origin;
2489 int count = vc->vc_screenbuf_size / 2;
2490 unsigned short c;
2491 for (; count > 0; count--, cp++) {
2492 c = scr_readw(cp);
2493 scr_writew(((c & 0xfe00) >> 1) |
2494 (c & 0xff), cp);
2495 }
2496 c = vc->vc_video_erase_char;
2497 vc->vc_video_erase_char =
2498 ((c & 0xfe00) >> 1) | (c & 0xff);
2499 vc->vc_attr >>= 1;
2500 }
2501 } else if (!vc->vc_hi_font_mask && cnt == 512) {
2502 vc->vc_hi_font_mask = 0x100;
2503 if (vc->vc_can_do_color) {
2504 vc->vc_complement_mask <<= 1;
2505 vc->vc_s_complement_mask <<= 1;
2506 }
2507
2508 /* ++Edmund: reorder the attribute bits */
2509 {
2510 unsigned short *cp =
2511 (unsigned short *) vc->vc_origin;
2512 int count = vc->vc_screenbuf_size / 2;
2513 unsigned short c;
2514 for (; count > 0; count--, cp++) {
2515 unsigned short newc;
2516 c = scr_readw(cp);
2517 if (vc->vc_can_do_color)
2518 newc =
2519 ((c & 0xff00) << 1) | (c &
2520 0xff);
2521 else
2522 newc = c & ~0x100;
2523 scr_writew(newc, cp);
2524 }
2525 c = vc->vc_video_erase_char;
2526 if (vc->vc_can_do_color) {
2527 vc->vc_video_erase_char =
2528 ((c & 0xff00) << 1) | (c & 0xff);
2529 vc->vc_attr <<= 1;
93f78da4 2530 } else
1da177e4
LT
2531 vc->vc_video_erase_char = c & ~0x100;
2532 }
2533
2534 }
2535
2536 if (resize) {
e4fc2761
AD
2537 int cols, rows;
2538
2539 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2540 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2541 cols /= w;
2542 rows /= h;
2543 vc_resize(vc, cols, rows);
4d9c5b6e
AD
2544 if (CON_IS_VISIBLE(vc) && softback_buf)
2545 fbcon_update_softback(vc);
1da177e4
LT
2546 } else if (CON_IS_VISIBLE(vc)
2547 && vc->vc_mode == KD_TEXT) {
2548 fbcon_clear_margins(vc, 0);
2549 update_screen(vc);
2550 }
2551
2552 if (old_data && (--REFCOUNT(old_data) == 0))
2553 kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2554 return 0;
2555}
2556
2557static int fbcon_copy_font(struct vc_data *vc, int con)
2558{
2559 struct display *od = &fb_display[con];
2560 struct console_font *f = &vc->vc_font;
2561
2562 if (od->fontdata == f->data)
2563 return 0; /* already the same font... */
2564 return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2565}
2566
2567/*
2568 * User asked to set font; we are guaranteed that
2569 * a) width and height are in range 1..32
2570 * b) charcount does not exceed 512
2571 * but lets not assume that, since someone might someday want to use larger
2572 * fonts. And charcount of 512 is small for unicode support.
2573 *
2574 * However, user space gives the font in 32 rows , regardless of
2575 * actual font height. So a new API is needed if support for larger fonts
2576 * is ever implemented.
2577 */
2578
2579static int fbcon_set_font(struct vc_data *vc, struct console_font *font, unsigned flags)
2580{
2d2699d9 2581 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1da177e4
LT
2582 unsigned charcount = font->charcount;
2583 int w = font->width;
2584 int h = font->height;
2585 int size;
2586 int i, csum;
2587 u8 *new_data, *data = font->data;
2588 int pitch = (font->width+7) >> 3;
2589
2590 /* Is there a reason why fbconsole couldn't handle any charcount >256?
2591 * If not this check should be changed to charcount < 256 */
2592 if (charcount != 256 && charcount != 512)
2593 return -EINVAL;
2594
2d2699d9
AD
2595 /* Make sure drawing engine can handle the font */
2596 if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2597 !(info->pixmap.blit_y & (1 << (font->height - 1))))
2598 return -EINVAL;
38b4982c
AD
2599
2600 /* Make sure driver can handle the font length */
2601 if (fbcon_invalid_charcount(info, charcount))
2602 return -EINVAL;
2d2699d9 2603
1da177e4
LT
2604 size = h * pitch * charcount;
2605
2606 new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2607
2608 if (!new_data)
2609 return -ENOMEM;
2610
2611 new_data += FONT_EXTRA_WORDS * sizeof(int);
2612 FNTSIZE(new_data) = size;
2613 FNTCHARCNT(new_data) = charcount;
2614 REFCOUNT(new_data) = 0; /* usage counter */
2615 for (i=0; i< charcount; i++) {
2616 memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
2617 }
2618
2619 /* Since linux has a nice crc32 function use it for counting font
2620 * checksums. */
2621 csum = crc32(0, new_data, size);
2622
2623 FNTSUM(new_data) = csum;
2624 /* Check if the same font is on some other console already */
e614b18d 2625 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
2626 struct vc_data *tmp = vc_cons[i].d;
2627
2628 if (fb_display[i].userfont &&
2629 fb_display[i].fontdata &&
2630 FNTSUM(fb_display[i].fontdata) == csum &&
2631 FNTSIZE(fb_display[i].fontdata) == size &&
2632 tmp->vc_font.width == w &&
2633 !memcmp(fb_display[i].fontdata, new_data, size)) {
2634 kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2f4516db 2635 new_data = (u8 *)fb_display[i].fontdata;
1da177e4
LT
2636 break;
2637 }
2638 }
2639 return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2640}
2641
2642static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2643{
2644 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2f4516db 2645 const struct font_desc *f;
1da177e4
LT
2646
2647 if (!name)
2d2699d9
AD
2648 f = get_default_font(info->var.xres, info->var.yres,
2649 info->pixmap.blit_x, info->pixmap.blit_y);
1da177e4
LT
2650 else if (!(f = find_font(name)))
2651 return -ENOENT;
2652
2653 font->width = f->width;
2654 font->height = f->height;
2655 return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2656}
2657
2658static u16 palette_red[16];
2659static u16 palette_green[16];
2660static u16 palette_blue[16];
2661
2662static struct fb_cmap palette_cmap = {
2663 0, 16, palette_red, palette_green, palette_blue, NULL
2664};
2665
2666static int fbcon_set_palette(struct vc_data *vc, unsigned char *table)
2667{
2668 struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2669 int i, j, k, depth;
2670 u8 val;
2671
2672 if (fbcon_is_inactive(vc, info))
2673 return -EINVAL;
2674
2675 if (!CON_IS_VISIBLE(vc))
2676 return 0;
2677
b8c90945 2678 depth = fb_get_color_depth(&info->var, &info->fix);
1da177e4
LT
2679 if (depth > 3) {
2680 for (i = j = 0; i < 16; i++) {
2681 k = table[i];
2682 val = vc->vc_palette[j++];
2683 palette_red[k] = (val << 8) | val;
2684 val = vc->vc_palette[j++];
2685 palette_green[k] = (val << 8) | val;
2686 val = vc->vc_palette[j++];
2687 palette_blue[k] = (val << 8) | val;
2688 }
2689 palette_cmap.len = 16;
2690 palette_cmap.start = 0;
2691 /*
2692 * If framebuffer is capable of less than 16 colors,
2693 * use default palette of fbcon.
2694 */
2695 } else
2696 fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2697
2698 return fb_set_cmap(&palette_cmap, info);
2699}
2700
2701static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2702{
2703 unsigned long p;
2704 int line;
2705
2706 if (vc->vc_num != fg_console || !softback_lines)
2707 return (u16 *) (vc->vc_origin + offset);
2708 line = offset / vc->vc_size_row;
2709 if (line >= softback_lines)
2710 return (u16 *) (vc->vc_origin + offset -
2711 softback_lines * vc->vc_size_row);
2712 p = softback_curr + offset;
2713 if (p >= softback_end)
2714 p += softback_buf - softback_end;
2715 return (u16 *) p;
2716}
2717
2718static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2719 int *px, int *py)
2720{
2721 unsigned long ret;
2722 int x, y;
2723
2724 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2725 unsigned long offset = (pos - vc->vc_origin) / 2;
2726
2727 x = offset % vc->vc_cols;
2728 y = offset / vc->vc_cols;
2729 if (vc->vc_num == fg_console)
2730 y += softback_lines;
2731 ret = pos + (vc->vc_cols - x) * 2;
2732 } else if (vc->vc_num == fg_console && softback_lines) {
2733 unsigned long offset = pos - softback_curr;
2734
2735 if (pos < softback_curr)
2736 offset += softback_end - softback_buf;
2737 offset /= 2;
2738 x = offset % vc->vc_cols;
2739 y = offset / vc->vc_cols;
2740 ret = pos + (vc->vc_cols - x) * 2;
2741 if (ret == softback_end)
2742 ret = softback_buf;
2743 if (ret == softback_in)
2744 ret = vc->vc_origin;
2745 } else {
2746 /* Should not happen */
2747 x = y = 0;
2748 ret = vc->vc_origin;
2749 }
2750 if (px)
2751 *px = x;
2752 if (py)
2753 *py = y;
2754 return ret;
2755}
2756
2757/* As we might be inside of softback, we may work with non-contiguous buffer,
2758 that's why we have to use a separate routine. */
2759static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2760{
2761 while (cnt--) {
2762 u16 a = scr_readw(p);
2763 if (!vc->vc_can_do_color)
2764 a ^= 0x0800;
2765 else if (vc->vc_hi_font_mask == 0x100)
2766 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2767 (((a) & 0x0e00) << 4);
2768 else
2769 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2770 (((a) & 0x0700) << 4);
2771 scr_writew(a, p++);
2772 if (p == (u16 *) softback_end)
2773 p = (u16 *) softback_buf;
2774 if (p == (u16 *) softback_in)
2775 p = (u16 *) vc->vc_origin;
2776 }
2777}
2778
2779static int fbcon_scrolldelta(struct vc_data *vc, int lines)
2780{
2781 struct fb_info *info = registered_fb[con2fb_map[fg_console]];
e4fc2761 2782 struct fbcon_ops *ops = info->fbcon_par;
2c6cc35c 2783 struct display *disp = &fb_display[fg_console];
1da177e4
LT
2784 int offset, limit, scrollback_old;
2785
2786 if (softback_top) {
2787 if (vc->vc_num != fg_console)
2788 return 0;
2789 if (vc->vc_mode != KD_TEXT || !lines)
2790 return 0;
2791 if (logo_shown >= 0) {
2792 struct vc_data *conp2 = vc_cons[logo_shown].d;
2793
2794 if (conp2->vc_top == logo_lines
2795 && conp2->vc_bottom == conp2->vc_rows)
2796 conp2->vc_top = 0;
2797 if (logo_shown == vc->vc_num) {
2798 unsigned long p, q;
2799 int i;
2800
2801 p = softback_in;
2802 q = vc->vc_origin +
2803 logo_lines * vc->vc_size_row;
2804 for (i = 0; i < logo_lines; i++) {
2805 if (p == softback_top)
2806 break;
2807 if (p == softback_buf)
2808 p = softback_end;
2809 p -= vc->vc_size_row;
2810 q -= vc->vc_size_row;
2811 scr_memcpyw((u16 *) q, (u16 *) p,
2812 vc->vc_size_row);
2813 }
308af929 2814 softback_in = softback_curr = p;
1da177e4
LT
2815 update_region(vc, vc->vc_origin,
2816 logo_lines * vc->vc_cols);
2817 }
2818 logo_shown = FBCON_LOGO_CANSHOW;
2819 }
2820 fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2c6cc35c 2821 fbcon_redraw_softback(vc, disp, lines);
1da177e4
LT
2822 fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2823 return 0;
2824 }
2825
2826 if (!scrollback_phys_max)
2827 return -ENOSYS;
2828
2829 scrollback_old = scrollback_current;
2830 scrollback_current -= lines;
2831 if (scrollback_current < 0)
2832 scrollback_current = 0;
2833 else if (scrollback_current > scrollback_max)
2834 scrollback_current = scrollback_max;
2835 if (scrollback_current == scrollback_old)
2836 return 0;
2837
2838 if (fbcon_is_inactive(vc, info))
2839 return 0;
2840
2841 fbcon_cursor(vc, CM_ERASE);
2842
2c6cc35c
MS
2843 offset = disp->yscroll - scrollback_current;
2844 limit = disp->vrows;
2845 switch (disp->scrollmode) {
1da177e4
LT
2846 case SCROLL_WRAP_MOVE:
2847 info->var.vmode |= FB_VMODE_YWRAP;
2848 break;
2849 case SCROLL_PAN_MOVE:
2850 case SCROLL_PAN_REDRAW:
2851 limit -= vc->vc_rows;
2852 info->var.vmode &= ~FB_VMODE_YWRAP;
2853 break;
2854 }
2855 if (offset < 0)
2856 offset += limit;
2857 else if (offset >= limit)
2858 offset -= limit;
e4fc2761
AD
2859
2860 ops->var.xoffset = 0;
2861 ops->var.yoffset = offset * vc->vc_font.height;
2862 ops->update_start(info);
2863
1da177e4
LT
2864 if (!scrollback_current)
2865 fbcon_cursor(vc, CM_DRAW);
2866 return 0;
2867}
2868
2869static int fbcon_set_origin(struct vc_data *vc)
2870{
2871 if (softback_lines)
2872 fbcon_scrolldelta(vc, softback_lines);
2873 return 0;
2874}
2875
2876static void fbcon_suspended(struct fb_info *info)
2877{
2878 struct vc_data *vc = NULL;
2879 struct fbcon_ops *ops = info->fbcon_par;
2880
2881 if (!ops || ops->currcon < 0)
2882 return;
2883 vc = vc_cons[ops->currcon].d;
2884
2885 /* Clear cursor, restore saved data */
2886 fbcon_cursor(vc, CM_ERASE);
2887}
2888
2889static void fbcon_resumed(struct fb_info *info)
2890{
2891 struct vc_data *vc;
2892 struct fbcon_ops *ops = info->fbcon_par;
2893
2894 if (!ops || ops->currcon < 0)
2895 return;
2896 vc = vc_cons[ops->currcon].d;
2897
2898 update_screen(vc);
2899}
2900
2901static void fbcon_modechanged(struct fb_info *info)
2902{
2903 struct fbcon_ops *ops = info->fbcon_par;
2904 struct vc_data *vc;
2905 struct display *p;
2906 int rows, cols;
2907
2908 if (!ops || ops->currcon < 0)
2909 return;
2910 vc = vc_cons[ops->currcon].d;
e4fc2761
AD
2911 if (vc->vc_mode != KD_TEXT ||
2912 registered_fb[con2fb_map[ops->currcon]] != info)
1da177e4
LT
2913 return;
2914
2915 p = &fb_display[vc->vc_num];
b73deed3 2916 set_blitting_type(vc, info);
1da177e4
LT
2917
2918 if (CON_IS_VISIBLE(vc)) {
2919 var_to_display(p, &info->var, info);
e4fc2761
AD
2920 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2921 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2922 cols /= vc->vc_font.width;
2923 rows /= vc->vc_font.height;
1da177e4
LT
2924 vc_resize(vc, cols, rows);
2925 updatescrollmode(p, info, vc);
2926 scrollback_max = 0;
2927 scrollback_current = 0;
4e1567d3
AD
2928
2929 if (!fbcon_is_inactive(vc, info)) {
2930 ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2931 ops->update_start(info);
2932 }
2933
1da177e4
LT
2934 fbcon_set_palette(vc, color_table);
2935 update_screen(vc);
4d9c5b6e
AD
2936 if (softback_buf)
2937 fbcon_update_softback(vc);
1da177e4
LT
2938 }
2939}
2940
7726e9e1
AD
2941static void fbcon_set_all_vcs(struct fb_info *info)
2942{
2943 struct fbcon_ops *ops = info->fbcon_par;
2944 struct vc_data *vc;
2945 struct display *p;
95d67bb1 2946 int i, rows, cols, fg = -1;
7726e9e1
AD
2947
2948 if (!ops || ops->currcon < 0)
2949 return;
2950
e614b18d 2951 for (i = first_fb_vc; i <= last_fb_vc; i++) {
7726e9e1
AD
2952 vc = vc_cons[i].d;
2953 if (!vc || vc->vc_mode != KD_TEXT ||
2954 registered_fb[con2fb_map[i]] != info)
2955 continue;
2956
95d67bb1
AD
2957 if (CON_IS_VISIBLE(vc)) {
2958 fg = i;
2959 continue;
2960 }
2961
7726e9e1 2962 p = &fb_display[vc->vc_num];
b73deed3 2963 set_blitting_type(vc, info);
7726e9e1 2964 var_to_display(p, &info->var, info);
232fb69a
ON
2965 cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2966 rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
e4fc2761
AD
2967 cols /= vc->vc_font.width;
2968 rows /= vc->vc_font.height;
7726e9e1 2969 vc_resize(vc, cols, rows);
7726e9e1 2970 }
04a2fe57 2971
95d67bb1
AD
2972 if (fg != -1)
2973 fbcon_modechanged(info);
7726e9e1
AD
2974}
2975
1da177e4
LT
2976static int fbcon_mode_deleted(struct fb_info *info,
2977 struct fb_videomode *mode)
2978{
2979 struct fb_info *fb_info;
2980 struct display *p;
2981 int i, j, found = 0;
2982
2983 /* before deletion, ensure that mode is not in use */
2984 for (i = first_fb_vc; i <= last_fb_vc; i++) {
2985 j = con2fb_map[i];
2986 if (j == -1)
2987 continue;
2988 fb_info = registered_fb[j];
2989 if (fb_info != info)
2990 continue;
2991 p = &fb_display[i];
2992 if (!p || !p->mode)
2993 continue;
2994 if (fb_mode_is_equal(p->mode, mode)) {
2995 found = 1;
2996 break;
2997 }
2998 }
2999 return found;
3000}
3001
cfafca80
JB
3002#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3003static int fbcon_unbind(void)
3004{
3005 int ret;
3006
3007 ret = unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
3008 fbcon_is_default);
2ddce3fd
IA
3009
3010 if (!ret)
3011 fbcon_has_console_bind = 0;
3012
cfafca80
JB
3013 return ret;
3014}
3015#else
3016static inline int fbcon_unbind(void)
3017{
3018 return -EINVAL;
3019}
3020#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3021
3022static int fbcon_fb_unbind(int idx)
3023{
3024 int i, new_idx = -1, ret = 0;
3025
2ddce3fd
IA
3026 if (!fbcon_has_console_bind)
3027 return 0;
3028
cfafca80
JB
3029 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3030 if (con2fb_map[i] != idx &&
3031 con2fb_map[i] != -1) {
3032 new_idx = i;
3033 break;
3034 }
3035 }
3036
3037 if (new_idx != -1) {
3038 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3039 if (con2fb_map[i] == idx)
3040 set_con2fb_map(i, new_idx, 0);
3041 }
3042 } else
3043 ret = fbcon_unbind();
3044
3045 return ret;
3046}
3047
623e71b0 3048static int fbcon_fb_unregistered(struct fb_info *info)
e614b18d 3049{
66c1ca01 3050 int i, idx;
e614b18d 3051
66c1ca01 3052 idx = info->node;
e614b18d
AD
3053 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3054 if (con2fb_map[i] == idx)
3055 con2fb_map[i] = -1;
3056 }
3057
3058 if (idx == info_idx) {
3059 info_idx = -1;
3060
3061 for (i = 0; i < FB_MAX; i++) {
3062 if (registered_fb[i] != NULL) {
3063 info_idx = i;
3064 break;
3065 }
3066 }
3067 }
3068
3069 if (info_idx != -1) {
3070 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3071 if (con2fb_map[i] == -1)
3072 con2fb_map[i] = info_idx;
3073 }
3074 }
3075
623e71b0
AD
3076 if (primary_device == idx)
3077 primary_device = -1;
3078
66c1ca01
AR
3079 if (!num_registered_fb)
3080 unregister_con_driver(&fb_con);
3081
623e71b0
AD
3082 return 0;
3083}
3084
6a9ee8af
DA
3085static void fbcon_remap_all(int idx)
3086{
3087 int i;
3088 for (i = first_fb_vc; i <= last_fb_vc; i++)
3089 set_con2fb_map(i, idx, 0);
3090
3091 if (con_is_bound(&fb_con)) {
3092 printk(KERN_INFO "fbcon: Remapping primary device, "
3093 "fb%i, to tty %i-%i\n", idx,
3094 first_fb_vc + 1, last_fb_vc + 1);
3095 info_idx = idx;
3096 }
3097}
3098
623e71b0 3099#ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
afd1db16 3100static void fbcon_select_primary(struct fb_info *info)
623e71b0 3101{
623e71b0
AD
3102 if (!map_override && primary_device == -1 &&
3103 fb_is_primary_device(info)) {
afd1db16 3104 int i;
623e71b0 3105
afd1db16
AD
3106 printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3107 info->fix.id, info->node);
623e71b0
AD
3108 primary_device = info->node;
3109
afd1db16 3110 for (i = first_fb_vc; i <= last_fb_vc; i++)
623e71b0 3111 con2fb_map_boot[i] = primary_device;
623e71b0 3112
afd1db16
AD
3113 if (con_is_bound(&fb_con)) {
3114 printk(KERN_INFO "fbcon: Remapping primary device, "
3115 "fb%i, to tty %i-%i\n", info->node,
3116 first_fb_vc + 1, last_fb_vc + 1);
3117 info_idx = primary_device;
623e71b0 3118 }
623e71b0
AD
3119 }
3120
623e71b0
AD
3121}
3122#else
afd1db16 3123static inline void fbcon_select_primary(struct fb_info *info)
623e71b0 3124{
afd1db16 3125 return;
e614b18d 3126}
623e71b0 3127#endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
e614b18d 3128
623e71b0 3129static int fbcon_fb_registered(struct fb_info *info)
1da177e4 3130{
66c1ca01 3131 int ret = 0, i, idx;
623e71b0 3132
66c1ca01 3133 idx = info->node;
afd1db16 3134 fbcon_select_primary(info);
1da177e4
LT
3135
3136 if (info_idx == -1) {
e614b18d 3137 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
3138 if (con2fb_map_boot[i] == idx) {
3139 info_idx = idx;
3140 break;
3141 }
3142 }
e614b18d 3143
1da177e4 3144 if (info_idx != -1)
50e244cc 3145 ret = do_fbcon_takeover(1);
1da177e4 3146 } else {
e614b18d 3147 for (i = first_fb_vc; i <= last_fb_vc; i++) {
d1baa4ff 3148 if (con2fb_map_boot[i] == idx)
1da177e4
LT
3149 set_con2fb_map(i, idx, 0);
3150 }
3151 }
3152
3153 return ret;
3154}
3155
3156static void fbcon_fb_blanked(struct fb_info *info, int blank)
3157{
3158 struct fbcon_ops *ops = info->fbcon_par;
3159 struct vc_data *vc;
3160
3161 if (!ops || ops->currcon < 0)
3162 return;
3163
3164 vc = vc_cons[ops->currcon].d;
3165 if (vc->vc_mode != KD_TEXT ||
3166 registered_fb[con2fb_map[ops->currcon]] != info)
3167 return;
3168
3169 if (CON_IS_VISIBLE(vc)) {
3170 if (blank)
3171 do_blank_screen(0);
3172 else
3173 do_unblank_screen(0);
3174 }
3175 ops->blank_state = blank;
3176}
3177
3178static void fbcon_new_modelist(struct fb_info *info)
3179{
3180 int i;
3181 struct vc_data *vc;
3182 struct fb_var_screeninfo var;
9791d763 3183 const struct fb_videomode *mode;
1da177e4 3184
e614b18d 3185 for (i = first_fb_vc; i <= last_fb_vc; i++) {
1da177e4
LT
3186 if (registered_fb[con2fb_map[i]] != info)
3187 continue;
3188 if (!fb_display[i].mode)
3189 continue;
3190 vc = vc_cons[i].d;
3191 display_to_var(&var, &fb_display[i]);
8fb6567e
MJ
3192 mode = fb_find_nearest_mode(fb_display[i].mode,
3193 &info->modelist);
1da177e4 3194 fb_videomode_to_var(&var, mode);
d1baa4ff 3195 fbcon_set_disp(info, &var, vc->vc_num);
1da177e4
LT
3196 }
3197}
3198
38a3dc51
AD
3199static void fbcon_get_requirement(struct fb_info *info,
3200 struct fb_blit_caps *caps)
3201{
3202 struct vc_data *vc;
3203 struct display *p;
38a3dc51
AD
3204
3205 if (caps->flags) {
167f07f1 3206 int i, charcnt;
38a3dc51
AD
3207
3208 for (i = first_fb_vc; i <= last_fb_vc; i++) {
3209 vc = vc_cons[i].d;
167f07f1
AD
3210 if (vc && vc->vc_mode == KD_TEXT &&
3211 info->node == con2fb_map[i]) {
38a3dc51
AD
3212 p = &fb_display[i];
3213 caps->x |= 1 << (vc->vc_font.width - 1);
3214 caps->y |= 1 << (vc->vc_font.height - 1);
3215 charcnt = (p->userfont) ?
3216 FNTCHARCNT(p->fontdata) : 256;
3217 if (caps->len < charcnt)
3218 caps->len = charcnt;
3219 }
3220 }
3221 } else {
3222 vc = vc_cons[fg_console].d;
3223
167f07f1
AD
3224 if (vc && vc->vc_mode == KD_TEXT &&
3225 info->node == con2fb_map[fg_console]) {
38a3dc51 3226 p = &fb_display[fg_console];
167f07f1
AD
3227 caps->x = 1 << (vc->vc_font.width - 1);
3228 caps->y = 1 << (vc->vc_font.height - 1);
3229 caps->len = (p->userfont) ?
38a3dc51 3230 FNTCHARCNT(p->fontdata) : 256;
38a3dc51
AD
3231 }
3232 }
3233}
3234
66c1ca01 3235static int fbcon_event_notify(struct notifier_block *self,
1da177e4
LT
3236 unsigned long action, void *data)
3237{
3238 struct fb_event *event = data;
3239 struct fb_info *info = event->info;
3240 struct fb_videomode *mode;
3241 struct fb_con2fbmap *con2fb;
38a3dc51 3242 struct fb_blit_caps *caps;
66c1ca01 3243 int idx, ret = 0;
1da177e4 3244
e614b18d
AD
3245 /*
3246 * ignore all events except driver registration and deregistration
3247 * if fbcon is not active
3248 */
3249 if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
3250 action == FB_EVENT_FB_UNREGISTERED))
3251 goto done;
3252
1da177e4
LT
3253 switch(action) {
3254 case FB_EVENT_SUSPEND:
3255 fbcon_suspended(info);
3256 break;
3257 case FB_EVENT_RESUME:
3258 fbcon_resumed(info);
3259 break;
3260 case FB_EVENT_MODE_CHANGE:
3261 fbcon_modechanged(info);
3262 break;
7726e9e1
AD
3263 case FB_EVENT_MODE_CHANGE_ALL:
3264 fbcon_set_all_vcs(info);
3265 break;
1da177e4
LT
3266 case FB_EVENT_MODE_DELETE:
3267 mode = event->data;
3268 ret = fbcon_mode_deleted(info, mode);
3269 break;
cfafca80 3270 case FB_EVENT_FB_UNBIND:
66c1ca01 3271 idx = info->node;
66c1ca01 3272 ret = fbcon_fb_unbind(idx);
cfafca80 3273 break;
1da177e4 3274 case FB_EVENT_FB_REGISTERED:
623e71b0 3275 ret = fbcon_fb_registered(info);
1da177e4 3276 break;
e614b18d 3277 case FB_EVENT_FB_UNREGISTERED:
623e71b0 3278 ret = fbcon_fb_unregistered(info);
e614b18d 3279 break;
1da177e4
LT
3280 case FB_EVENT_SET_CONSOLE_MAP:
3281 con2fb = event->data;
3282 ret = set_con2fb_map(con2fb->console - 1,
3283 con2fb->framebuffer, 1);
3284 break;
3285 case FB_EVENT_GET_CONSOLE_MAP:
3286 con2fb = event->data;
3287 con2fb->framebuffer = con2fb_map[con2fb->console - 1];
3288 break;
3289 case FB_EVENT_BLANK:
3290 fbcon_fb_blanked(info, *(int *)event->data);
3291 break;
3292 case FB_EVENT_NEW_MODELIST:
3293 fbcon_new_modelist(info);
3294 break;
38a3dc51
AD
3295 case FB_EVENT_GET_REQ:
3296 caps = event->data;
3297 fbcon_get_requirement(info, caps);
3298 break;
6a9ee8af
DA
3299 case FB_EVENT_REMAP_ALL_CONSOLE:
3300 idx = info->node;
3301 fbcon_remap_all(idx);
3302 break;
1da177e4 3303 }
e614b18d 3304done:
1da177e4
LT
3305 return ret;
3306}
3307
3308/*
3309 * The console `switch' structure for the frame buffer based console
3310 */
3311
3312static const struct consw fb_con = {
3313 .owner = THIS_MODULE,
3314 .con_startup = fbcon_startup,
3315 .con_init = fbcon_init,
3316 .con_deinit = fbcon_deinit,
3317 .con_clear = fbcon_clear,
3318 .con_putc = fbcon_putc,
3319 .con_putcs = fbcon_putcs,
3320 .con_cursor = fbcon_cursor,
3321 .con_scroll = fbcon_scroll,
3322 .con_bmove = fbcon_bmove,
3323 .con_switch = fbcon_switch,
3324 .con_blank = fbcon_blank,
3325 .con_font_set = fbcon_set_font,
3326 .con_font_get = fbcon_get_font,
3327 .con_font_default = fbcon_set_def_font,
3328 .con_font_copy = fbcon_copy_font,
3329 .con_set_palette = fbcon_set_palette,
3330 .con_scrolldelta = fbcon_scrolldelta,
3331 .con_set_origin = fbcon_set_origin,
3332 .con_invert_region = fbcon_invert_region,
3333 .con_screen_pos = fbcon_screen_pos,
3334 .con_getxy = fbcon_getxy,
3335 .con_resize = fbcon_resize,
d219adc1
JB
3336 .con_debug_enter = fbcon_debug_enter,
3337 .con_debug_leave = fbcon_debug_leave,
1da177e4
LT
3338};
3339
3340static struct notifier_block fbcon_event_notifier = {
3341 .notifier_call = fbcon_event_notify,
3342};
3343
0c6c1ce0
AD
3344static ssize_t store_rotate(struct device *device,
3345 struct device_attribute *attr, const char *buf,
3346 size_t count)
9a179176
AD
3347{
3348 struct fb_info *info;
3349 int rotate, idx;
3350 char **last = NULL;
3351
e614b18d
AD
3352 if (fbcon_has_exited)
3353 return count;
3354
ac751efa 3355 console_lock();
9a179176
AD
3356 idx = con2fb_map[fg_console];
3357
3358 if (idx == -1 || registered_fb[idx] == NULL)
3359 goto err;
3360
3361 info = registered_fb[idx];
3362 rotate = simple_strtoul(buf, last, 0);
3363 fbcon_rotate(info, rotate);
3364err:
ac751efa 3365 console_unlock();
9a179176
AD
3366 return count;
3367}
3368
0c6c1ce0
AD
3369static ssize_t store_rotate_all(struct device *device,
3370 struct device_attribute *attr,const char *buf,
3371 size_t count)
9a179176
AD
3372{
3373 struct fb_info *info;
3374 int rotate, idx;
3375 char **last = NULL;
3376
e614b18d
AD
3377 if (fbcon_has_exited)
3378 return count;
3379
ac751efa 3380 console_lock();
9a179176
AD
3381 idx = con2fb_map[fg_console];
3382
3383 if (idx == -1 || registered_fb[idx] == NULL)
3384 goto err;
3385
3386 info = registered_fb[idx];
3387 rotate = simple_strtoul(buf, last, 0);
3388 fbcon_rotate_all(info, rotate);
3389err:
ac751efa 3390 console_unlock();
9a179176
AD
3391 return count;
3392}
3393
0c6c1ce0
AD
3394static ssize_t show_rotate(struct device *device,
3395 struct device_attribute *attr,char *buf)
9a179176
AD
3396{
3397 struct fb_info *info;
3398 int rotate = 0, idx;
3399
e614b18d
AD
3400 if (fbcon_has_exited)
3401 return 0;
3402
ac751efa 3403 console_lock();
9a179176
AD
3404 idx = con2fb_map[fg_console];
3405
3406 if (idx == -1 || registered_fb[idx] == NULL)
3407 goto err;
3408
3409 info = registered_fb[idx];
3410 rotate = fbcon_get_rotate(info);
3411err:
ac751efa 3412 console_unlock();
9a179176
AD
3413 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3414}
3415
0c6c1ce0
AD
3416static ssize_t show_cursor_blink(struct device *device,
3417 struct device_attribute *attr, char *buf)
acba9cd0
AD
3418{
3419 struct fb_info *info;
3420 struct fbcon_ops *ops;
3421 int idx, blink = -1;
3422
3423 if (fbcon_has_exited)
3424 return 0;
3425
ac751efa 3426 console_lock();
acba9cd0
AD
3427 idx = con2fb_map[fg_console];
3428
3429 if (idx == -1 || registered_fb[idx] == NULL)
3430 goto err;
3431
3432 info = registered_fb[idx];
3433 ops = info->fbcon_par;
3434
3435 if (!ops)
3436 goto err;
3437
3438 blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3439err:
ac751efa 3440 console_unlock();
acba9cd0
AD
3441 return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3442}
3443
0c6c1ce0
AD
3444static ssize_t store_cursor_blink(struct device *device,
3445 struct device_attribute *attr,
acba9cd0
AD
3446 const char *buf, size_t count)
3447{
3448 struct fb_info *info;
3449 int blink, idx;
3450 char **last = NULL;
3451
3452 if (fbcon_has_exited)
3453 return count;
3454
ac751efa 3455 console_lock();
acba9cd0
AD
3456 idx = con2fb_map[fg_console];
3457
3458 if (idx == -1 || registered_fb[idx] == NULL)
3459 goto err;
3460
3461 info = registered_fb[idx];
3462
3463 if (!info->fbcon_par)
3464 goto err;
3465
3466 blink = simple_strtoul(buf, last, 0);
3467
3468 if (blink) {
3469 fbcon_cursor_noblink = 0;
3470 fbcon_add_cursor_timer(info);
3471 } else {
3472 fbcon_cursor_noblink = 1;
3473 fbcon_del_cursor_timer(info);
3474 }
3475
3476err:
ac751efa 3477 console_unlock();
acba9cd0
AD
3478 return count;
3479}
3480
0c6c1ce0 3481static struct device_attribute device_attrs[] = {
9a179176
AD
3482 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3483 __ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
acba9cd0
AD
3484 __ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3485 store_cursor_blink),
9a179176
AD
3486};
3487
0c6c1ce0 3488static int fbcon_init_device(void)
9a179176 3489{
0a727dea
AD
3490 int i, error = 0;
3491
3492 fbcon_has_sysfs = 1;
3493
0c6c1ce0
AD
3494 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3495 error = device_create_file(fbcon_device, &device_attrs[i]);
0a727dea
AD
3496
3497 if (error)
3498 break;
3499 }
3500
3501 if (error) {
3502 while (--i >= 0)
0c6c1ce0 3503 device_remove_file(fbcon_device, &device_attrs[i]);
0a727dea
AD
3504
3505 fbcon_has_sysfs = 0;
3506 }
9a179176 3507
9a179176
AD
3508 return 0;
3509}
3510
5428b044 3511static void fbcon_start(void)
1da177e4 3512{
5428b044
AD
3513 if (num_registered_fb) {
3514 int i;
9a179176 3515
ac751efa 3516 console_lock();
1da177e4 3517
1da177e4
LT
3518 for (i = 0; i < FB_MAX; i++) {
3519 if (registered_fb[i] != NULL) {
3520 info_idx = i;
3521 break;
3522 }
3523 }
5428b044 3524
ac751efa 3525 console_unlock();
1da177e4
LT
3526 fbcon_takeover(0);
3527 }
9a179176
AD
3528}
3529
5428b044 3530static void fbcon_exit(void)
9a179176 3531{
e55186fe
AD
3532 struct fb_info *info;
3533 int i, j, mapped;
3534
e614b18d
AD
3535 if (fbcon_has_exited)
3536 return;
3537
e55186fe 3538 kfree((void *)softback_buf);
5428b044 3539 softback_buf = 0UL;
e55186fe
AD
3540
3541 for (i = 0; i < FB_MAX; i++) {
142092e5 3542 int pending = 0;
beaa4867 3543
e55186fe
AD
3544 mapped = 0;
3545 info = registered_fb[i];
3546
3547 if (info == NULL)
3548 continue;
3549
142092e5
JP
3550 if (info->queue.func)
3551 pending = cancel_work_sync(&info->queue);
beaa4867
GL
3552 DPRINTK("fbcon: %s pending work\n", (pending ? "canceled" :
3553 "no"));
3554
e614b18d
AD
3555 for (j = first_fb_vc; j <= last_fb_vc; j++) {
3556 if (con2fb_map[j] == i)
e55186fe 3557 mapped = 1;
e55186fe
AD
3558 }
3559
3560 if (mapped) {
3561 if (info->fbops->fb_release)
3562 info->fbops->fb_release(info, 0);
3563 module_put(info->fbops->owner);
5428b044
AD
3564
3565 if (info->fbcon_par) {
e299dd4d
DJ
3566 struct fbcon_ops *ops = info->fbcon_par;
3567
5428b044 3568 fbcon_del_cursor_timer(info);
e299dd4d 3569 kfree(ops->cursor_src);
5428b044
AD
3570 kfree(info->fbcon_par);
3571 info->fbcon_par = NULL;
3572 }
3573
3574 if (info->queue.func == fb_flashcursor)
3575 info->queue.func = NULL;
e55186fe
AD
3576 }
3577 }
3578
e614b18d 3579 fbcon_has_exited = 1;
5428b044
AD
3580}
3581
3582static int __init fb_console_init(void)
3583{
3584 int i;
3585
ac751efa 3586 console_lock();
5428b044 3587 fb_register_client(&fbcon_event_notifier);
77997aaa
GKH
3588 fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3589 "fbcon");
5428b044 3590
0c6c1ce0
AD
3591 if (IS_ERR(fbcon_device)) {
3592 printk(KERN_WARNING "Unable to create device "
5428b044 3593 "for fbcon; errno = %ld\n",
0c6c1ce0
AD
3594 PTR_ERR(fbcon_device));
3595 fbcon_device = NULL;
5428b044 3596 } else
0c6c1ce0 3597 fbcon_init_device();
5428b044
AD
3598
3599 for (i = 0; i < MAX_NR_CONSOLES; i++)
3600 con2fb_map[i] = -1;
3601
ac751efa 3602 console_unlock();
5428b044
AD
3603 fbcon_start();
3604 return 0;
3605}
3606
3607module_init(fb_console_init);
3608
3609#ifdef MODULE
3610
0c6c1ce0 3611static void __exit fbcon_deinit_device(void)
5428b044
AD
3612{
3613 int i;
3614
0a727dea 3615 if (fbcon_has_sysfs) {
0c6c1ce0
AD
3616 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3617 device_remove_file(fbcon_device, &device_attrs[i]);
0a727dea
AD
3618
3619 fbcon_has_sysfs = 0;
3620 }
9a179176
AD
3621}
3622
1da177e4
LT
3623static void __exit fb_console_exit(void)
3624{
ac751efa 3625 console_lock();
1da177e4 3626 fb_unregister_client(&fbcon_event_notifier);
0c6c1ce0
AD
3627 fbcon_deinit_device();
3628 device_destroy(fb_class, MKDEV(0, 0));
e614b18d 3629 fbcon_exit();
ac751efa 3630 console_unlock();
e614b18d 3631 unregister_con_driver(&fb_con);
1da177e4
LT
3632}
3633
3634module_exit(fb_console_exit);
3635
3636#endif
3637
3638MODULE_LICENSE("GPL");