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