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