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