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