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