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