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