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