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