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