xref: /linux/drivers/video/fbdev/core/fbcon.c (revision 995832b2cebe6969d1b42635db698803ee31294d)
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 	p->mode = NULL;
1277 	idx = con2fb_map[vc->vc_num];
1278 
1279 	if (idx == -1)
1280 		goto finished;
1281 
1282 	info = fbcon_registered_fb[idx];
1283 
1284 	if (!info)
1285 		goto finished;
1286 
1287 	par = info->fbcon_par;
1288 
1289 	if (!par)
1290 		goto finished;
1291 
1292 	if (con_is_visible(vc))
1293 		fbcon_del_cursor_work(info);
1294 
1295 	par->initialized = false;
1296 finished:
1297 
1298 	fbcon_free_font(p);
1299 	vc->vc_font.data = NULL;
1300 
1301 	if (vc->vc_hi_font_mask && vc->vc_screenbuf)
1302 		set_vc_hi_font(vc, false);
1303 
1304 	if (!con_is_bound(&fb_con))
1305 		fbcon_release_all();
1306 
1307 	if (vc->vc_num == logo_shown)
1308 		logo_shown = FBCON_LOGO_CANSHOW;
1309 
1310 	return;
1311 }
1312 
1313 /* ====================================================================== */
1314 
1315 /*  fbcon_XXX routines - interface used by the world
1316  *
1317  *  This system is now divided into two levels because of complications
1318  *  caused by hardware scrolling. Top level functions:
1319  *
1320  *	fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1321  *
1322  *  handles y values in range [0, scr_height-1] that correspond to real
1323  *  screen positions. y_wrap shift means that first line of bitmap may be
1324  *  anywhere on this display. These functions convert lineoffsets to
1325  *  bitmap offsets and deal with the wrap-around case by splitting blits.
1326  *
1327  *	fbcon_bmove_physical_8()    -- These functions fast implementations
1328  *	fbcon_clear_physical_8()    -- of original fbcon_XXX fns.
1329  *	fbcon_putc_physical_8()	    -- (font width != 8) may be added later
1330  *
1331  *  WARNING:
1332  *
1333  *  At the moment fbcon_putc() cannot blit across vertical wrap boundary
1334  *  Implies should only really hardware scroll in rows. Only reason for
1335  *  restriction is simplicity & efficiency at the moment.
1336  */
1337 
1338 static void __fbcon_clear(struct vc_data *vc, unsigned int sy, unsigned int sx,
1339 			  unsigned int height, unsigned int width)
1340 {
1341 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1342 	struct fbcon_par *par = info->fbcon_par;
1343 	int fg, bg;
1344 	struct fbcon_display *p = &fb_display[vc->vc_num];
1345 	u_int y_break;
1346 
1347 	if (!fbcon_is_active(vc, info))
1348 		return;
1349 
1350 	if (!height || !width)
1351 		return;
1352 
1353 	if (sy < vc->vc_top && vc->vc_top == logo_lines) {
1354 		vc->vc_top = 0;
1355 		/*
1356 		 * If the font dimensions are not an integral of the display
1357 		 * dimensions then the par->clear below won't end up clearing
1358 		 * the margins.  Call clear_margins here in case the logo
1359 		 * bitmap stretched into the margin area.
1360 		 */
1361 		fbcon_clear_margins(vc, 0);
1362 	}
1363 
1364 	fg = get_color(vc, info, vc->vc_video_erase_char, 1);
1365 	bg = get_color(vc, info, vc->vc_video_erase_char, 0);
1366 	/* Split blits that cross physical y_wrap boundary */
1367 
1368 	y_break = p->vrows - p->yscroll;
1369 	if (sy < y_break && sy + height - 1 >= y_break) {
1370 		u_int b = y_break - sy;
1371 		par->bitops->clear(vc, info, real_y(p, sy), sx, b, width, fg, bg);
1372 		par->bitops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1373 				     width, fg, bg);
1374 	} else
1375 		par->bitops->clear(vc, info, real_y(p, sy), sx, height, width, fg, bg);
1376 }
1377 
1378 static void fbcon_clear(struct vc_data *vc, unsigned int sy, unsigned int sx,
1379 			unsigned int width)
1380 {
1381 	__fbcon_clear(vc, sy, sx, 1, width);
1382 }
1383 
1384 static void fbcon_putcs(struct vc_data *vc, const u16 *s, unsigned int count,
1385 			unsigned int ypos, unsigned int xpos)
1386 {
1387 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1388 	struct fbcon_display *p = &fb_display[vc->vc_num];
1389 	struct fbcon_par *par = info->fbcon_par;
1390 
1391 	if (fbcon_is_active(vc, info))
1392 		par->bitops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1393 				   get_fg_color(vc, info, scr_readw(s)),
1394 				   get_bg_color(vc, info, scr_readw(s)));
1395 }
1396 
1397 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1398 {
1399 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1400 	struct fbcon_par *par = info->fbcon_par;
1401 
1402 	if (fbcon_is_active(vc, info))
1403 		par->bitops->clear_margins(vc, info, margin_color, bottom_only);
1404 }
1405 
1406 static void fbcon_cursor(struct vc_data *vc, bool enable)
1407 {
1408 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1409 	struct fbcon_par *par = info->fbcon_par;
1410  	int c = scr_readw((u16 *) vc->vc_pos);
1411 
1412 	par->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms);
1413 
1414 	if (!fbcon_is_active(vc, info) || vc->vc_deccm != 1)
1415 		return;
1416 
1417 	if (vc->vc_cursor_type & CUR_SW)
1418 		fbcon_del_cursor_work(info);
1419 	else
1420 		fbcon_add_cursor_work(info);
1421 
1422 	par->cursor_flash = enable;
1423 
1424 	if (!par->bitops->cursor)
1425 		return;
1426 
1427 	par->bitops->cursor(vc, info, enable,
1428 			    get_fg_color(vc, info, c),
1429 			    get_bg_color(vc, info, c));
1430 }
1431 
1432 static int scrollback_phys_max = 0;
1433 static int scrollback_max = 0;
1434 static int scrollback_current = 0;
1435 
1436 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1437 			   int unit)
1438 {
1439 	struct fbcon_display *p, *t;
1440 	struct vc_data **default_mode, *vc;
1441 	struct vc_data *svc;
1442 	struct fbcon_par *par = info->fbcon_par;
1443 	int rows, cols, ret;
1444 
1445 	p = &fb_display[unit];
1446 
1447 	vc = vc_cons[unit].d;
1448 
1449 	if (!vc)
1450 		return;
1451 
1452 	if (var_to_display(p, var, info))
1453 		return;
1454 
1455 	default_mode = vc->vc_display_fg;
1456 	svc = *default_mode;
1457 	t = &fb_display[svc->vc_num];
1458 
1459 	if (!p->fontdata) {
1460 		vc->vc_font.data = font_data_buf(t->fontdata);
1461 		vc->vc_font.width = (*default_mode)->vc_font.width;
1462 		vc->vc_font.height = (*default_mode)->vc_font.height;
1463 		vc->vc_font.charcount = (*default_mode)->vc_font.charcount;
1464 		p->fontdata = t->fontdata;
1465 		font_data_get(p->fontdata);
1466 	}
1467 
1468 	var->activate = FB_ACTIVATE_NOW;
1469 	info->var.activate = var->activate;
1470 	var->yoffset = info->var.yoffset;
1471 	var->xoffset = info->var.xoffset;
1472 	fb_set_var(info, var);
1473 	par->var = info->var;
1474 	vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1475 	vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1476 	if (vc->vc_font.charcount == 256) {
1477 		vc->vc_hi_font_mask = 0;
1478 	} else {
1479 		vc->vc_hi_font_mask = 0x100;
1480 		if (vc->vc_can_do_color)
1481 			vc->vc_complement_mask <<= 1;
1482 	}
1483 
1484 	if (!*svc->uni_pagedict_loc)
1485 		con_set_default_unimap(svc);
1486 	if (!*vc->uni_pagedict_loc)
1487 		con_copy_unimap(vc, svc);
1488 
1489 	cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres);
1490 	rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres);
1491 	cols /= vc->vc_font.width;
1492 	rows /= vc->vc_font.height;
1493 	ret = vc_resize(vc, cols, rows);
1494 
1495 	if (con_is_visible(vc) && !ret)
1496 		update_screen(vc);
1497 }
1498 
1499 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1500 {
1501 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1502 	struct fbcon_par *par = info->fbcon_par;
1503 	struct fbcon_display *p = &fb_display[vc->vc_num];
1504 
1505 	p->yscroll += count;
1506 	if (p->yscroll >= p->vrows)	/* Deal with wrap */
1507 		p->yscroll -= p->vrows;
1508 	par->var.xoffset = 0;
1509 	par->var.yoffset = p->yscroll * vc->vc_font.height;
1510 	par->var.vmode |= FB_VMODE_YWRAP;
1511 	par->bitops->update_start(info);
1512 	scrollback_max += count;
1513 	if (scrollback_max > scrollback_phys_max)
1514 		scrollback_max = scrollback_phys_max;
1515 	scrollback_current = 0;
1516 }
1517 
1518 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1519 {
1520 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1521 	struct fbcon_par *par = info->fbcon_par;
1522 	struct fbcon_display *p = &fb_display[vc->vc_num];
1523 
1524 	p->yscroll -= count;
1525 	if (p->yscroll < 0)	/* Deal with wrap */
1526 		p->yscroll += p->vrows;
1527 	par->var.xoffset = 0;
1528 	par->var.yoffset = p->yscroll * vc->vc_font.height;
1529 	par->var.vmode |= FB_VMODE_YWRAP;
1530 	par->bitops->update_start(info);
1531 	scrollback_max -= count;
1532 	if (scrollback_max < 0)
1533 		scrollback_max = 0;
1534 	scrollback_current = 0;
1535 }
1536 
1537 static __inline__ void ypan_up(struct vc_data *vc, int count)
1538 {
1539 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1540 	struct fbcon_display *p = &fb_display[vc->vc_num];
1541 	struct fbcon_par *par = info->fbcon_par;
1542 
1543 	p->yscroll += count;
1544 	if (p->yscroll > p->vrows - vc->vc_rows) {
1545 		par->bitops->bmove(vc, info, p->vrows - vc->vc_rows,
1546 				   0, 0, 0, vc->vc_rows, vc->vc_cols);
1547 		p->yscroll -= p->vrows - vc->vc_rows;
1548 	}
1549 
1550 	par->var.xoffset = 0;
1551 	par->var.yoffset = p->yscroll * vc->vc_font.height;
1552 	par->var.vmode &= ~FB_VMODE_YWRAP;
1553 	par->bitops->update_start(info);
1554 	fbcon_clear_margins(vc, 1);
1555 	scrollback_max += count;
1556 	if (scrollback_max > scrollback_phys_max)
1557 		scrollback_max = scrollback_phys_max;
1558 	scrollback_current = 0;
1559 }
1560 
1561 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1562 {
1563 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1564 	struct fbcon_par *par = info->fbcon_par;
1565 	struct fbcon_display *p = &fb_display[vc->vc_num];
1566 
1567 	p->yscroll += count;
1568 
1569 	if (p->yscroll > p->vrows - vc->vc_rows) {
1570 		p->yscroll -= p->vrows - vc->vc_rows;
1571 		fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1572 	}
1573 
1574 	par->var.xoffset = 0;
1575 	par->var.yoffset = p->yscroll * vc->vc_font.height;
1576 	par->var.vmode &= ~FB_VMODE_YWRAP;
1577 	par->bitops->update_start(info);
1578 	fbcon_clear_margins(vc, 1);
1579 	scrollback_max += count;
1580 	if (scrollback_max > scrollback_phys_max)
1581 		scrollback_max = scrollback_phys_max;
1582 	scrollback_current = 0;
1583 }
1584 
1585 static __inline__ void ypan_down(struct vc_data *vc, int count)
1586 {
1587 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1588 	struct fbcon_display *p = &fb_display[vc->vc_num];
1589 	struct fbcon_par *par = info->fbcon_par;
1590 
1591 	p->yscroll -= count;
1592 	if (p->yscroll < 0) {
1593 		par->bitops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1594 				   0, vc->vc_rows, vc->vc_cols);
1595 		p->yscroll += p->vrows - vc->vc_rows;
1596 	}
1597 
1598 	par->var.xoffset = 0;
1599 	par->var.yoffset = p->yscroll * vc->vc_font.height;
1600 	par->var.vmode &= ~FB_VMODE_YWRAP;
1601 	par->bitops->update_start(info);
1602 	fbcon_clear_margins(vc, 1);
1603 	scrollback_max -= count;
1604 	if (scrollback_max < 0)
1605 		scrollback_max = 0;
1606 	scrollback_current = 0;
1607 }
1608 
1609 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1610 {
1611 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1612 	struct fbcon_par *par = info->fbcon_par;
1613 	struct fbcon_display *p = &fb_display[vc->vc_num];
1614 
1615 	p->yscroll -= count;
1616 
1617 	if (p->yscroll < 0) {
1618 		p->yscroll += p->vrows - vc->vc_rows;
1619 		fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1620 	}
1621 
1622 	par->var.xoffset = 0;
1623 	par->var.yoffset = p->yscroll * vc->vc_font.height;
1624 	par->var.vmode &= ~FB_VMODE_YWRAP;
1625 	par->bitops->update_start(info);
1626 	fbcon_clear_margins(vc, 1);
1627 	scrollback_max -= count;
1628 	if (scrollback_max < 0)
1629 		scrollback_max = 0;
1630 	scrollback_current = 0;
1631 }
1632 
1633 static void fbcon_redraw_move(struct vc_data *vc, struct fbcon_display *p,
1634 			      int line, int count, int dy)
1635 {
1636 	unsigned short *s = (unsigned short *)
1637 		(vc->vc_origin + vc->vc_size_row * line);
1638 
1639 	while (count--) {
1640 		unsigned short *start = s;
1641 		unsigned short *le = advance_row(s, 1);
1642 		unsigned short c;
1643 		int x = 0;
1644 		unsigned short attr = 1;
1645 
1646 		do {
1647 			c = scr_readw(s);
1648 			if (attr != (c & 0xff00)) {
1649 				attr = c & 0xff00;
1650 				if (s > start) {
1651 					fbcon_putcs(vc, start, s - start,
1652 						    dy, x);
1653 					x += s - start;
1654 					start = s;
1655 				}
1656 			}
1657 			s++;
1658 		} while (s < le);
1659 		if (s > start)
1660 			fbcon_putcs(vc, start, s - start, dy, x);
1661 		dy++;
1662 	}
1663 }
1664 
1665 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1666 			struct fbcon_display *p, int line, int count, int ycount)
1667 {
1668 	int offset = ycount * vc->vc_cols;
1669 	unsigned short *d = (unsigned short *)
1670 	    (vc->vc_origin + vc->vc_size_row * line);
1671 	unsigned short *s = d + offset;
1672 	struct fbcon_par *par = info->fbcon_par;
1673 
1674 	while (count--) {
1675 		unsigned short *start = s;
1676 		unsigned short *le = advance_row(s, 1);
1677 		unsigned short c;
1678 		int x = 0;
1679 
1680 		do {
1681 			c = scr_readw(s);
1682 
1683 			if (c == scr_readw(d)) {
1684 				if (s > start) {
1685 					par->bitops->bmove(vc, info, line + ycount, x,
1686 							   line, x, 1, s - start);
1687 					x += s - start + 1;
1688 					start = s + 1;
1689 				} else {
1690 					x++;
1691 					start++;
1692 				}
1693 			}
1694 
1695 			scr_writew(c, d);
1696 			s++;
1697 			d++;
1698 		} while (s < le);
1699 		if (s > start)
1700 			par->bitops->bmove(vc, info, line + ycount, x, line, x, 1,
1701 					     s - start);
1702 		if (ycount > 0)
1703 			line++;
1704 		else {
1705 			line--;
1706 			/* NOTE: We subtract two lines from these pointers */
1707 			s -= vc->vc_size_row;
1708 			d -= vc->vc_size_row;
1709 		}
1710 	}
1711 }
1712 
1713 static void fbcon_redraw(struct vc_data *vc, int line, int count, int offset)
1714 {
1715 	unsigned short *d = (unsigned short *)
1716 	    (vc->vc_origin + vc->vc_size_row * line);
1717 	unsigned short *s = d + offset;
1718 
1719 	while (count--) {
1720 		unsigned short *start = s;
1721 		unsigned short *le = advance_row(s, 1);
1722 		unsigned short c;
1723 		int x = 0;
1724 		unsigned short attr = 1;
1725 
1726 		do {
1727 			c = scr_readw(s);
1728 			if (attr != (c & 0xff00)) {
1729 				attr = c & 0xff00;
1730 				if (s > start) {
1731 					fbcon_putcs(vc, start, s - start,
1732 						    line, x);
1733 					x += s - start;
1734 					start = s;
1735 				}
1736 			}
1737 			if (c == scr_readw(d)) {
1738 				if (s > start) {
1739 					fbcon_putcs(vc, start, s - start,
1740 						     line, x);
1741 					x += s - start + 1;
1742 					start = s + 1;
1743 				} else {
1744 					x++;
1745 					start++;
1746 				}
1747 			}
1748 			scr_writew(c, d);
1749 			s++;
1750 			d++;
1751 		} while (s < le);
1752 		if (s > start)
1753 			fbcon_putcs(vc, start, s - start, line, x);
1754 		if (offset > 0)
1755 			line++;
1756 		else {
1757 			line--;
1758 			/* NOTE: We subtract two lines from these pointers */
1759 			s -= vc->vc_size_row;
1760 			d -= vc->vc_size_row;
1761 		}
1762 	}
1763 }
1764 
1765 static void fbcon_bmove_rec(struct vc_data *vc, struct fbcon_display *p, int sy, int sx,
1766 			    int dy, int dx, int height, int width, u_int y_break)
1767 {
1768 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1769 	struct fbcon_par *par = info->fbcon_par;
1770 	u_int b;
1771 
1772 	if (sy < y_break && sy + height > y_break) {
1773 		b = y_break - sy;
1774 		if (dy < sy) {	/* Avoid trashing self */
1775 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1776 					y_break);
1777 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1778 					height - b, width, y_break);
1779 		} else {
1780 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1781 					height - b, width, y_break);
1782 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1783 					y_break);
1784 		}
1785 		return;
1786 	}
1787 
1788 	if (dy < y_break && dy + height > y_break) {
1789 		b = y_break - dy;
1790 		if (dy < sy) {	/* Avoid trashing self */
1791 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1792 					y_break);
1793 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1794 					height - b, width, y_break);
1795 		} else {
1796 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
1797 					height - b, width, y_break);
1798 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
1799 					y_break);
1800 		}
1801 		return;
1802 	}
1803 	par->bitops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
1804 			     height, width);
1805 }
1806 
1807 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
1808 			int height, int width)
1809 {
1810 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1811 	struct fbcon_display *p = &fb_display[vc->vc_num];
1812 
1813 	if (!fbcon_is_active(vc, info))
1814 		return;
1815 
1816 	if (!width || !height)
1817 		return;
1818 
1819 	/*  Split blits that cross physical y_wrap case.
1820 	 *  Pathological case involves 4 blits, better to use recursive
1821 	 *  code rather than unrolled case
1822 	 *
1823 	 *  Recursive invocations don't need to erase the cursor over and
1824 	 *  over again, so we use fbcon_bmove_rec()
1825 	 */
1826 	fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
1827 			p->vrows - p->yscroll);
1828 }
1829 
1830 static bool fbcon_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
1831 		enum con_scroll dir, unsigned int count)
1832 {
1833 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
1834 	struct fbcon_display *p = &fb_display[vc->vc_num];
1835 	int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1836 
1837 	if (!fbcon_is_active(vc, info))
1838 		return true;
1839 
1840 	fbcon_cursor(vc, false);
1841 
1842 	/*
1843 	 * ++Geert: Only use ywrap/ypan if the console is in text mode
1844 	 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1845 	 *           whole screen (prevents flicker).
1846 	 */
1847 
1848 	switch (dir) {
1849 	case SM_UP:
1850 		if (count > vc->vc_rows)	/* Maximum realistic size */
1851 			count = vc->vc_rows;
1852 		switch (fb_scrollmode(p)) {
1853 		case SCROLL_MOVE:
1854 			fbcon_redraw_blit(vc, info, p, t, b - t - count,
1855 				     count);
1856 			__fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1857 			scr_memsetw((unsigned short *) (vc->vc_origin +
1858 							vc->vc_size_row *
1859 							(b - count)),
1860 				    vc->vc_video_erase_char,
1861 				    vc->vc_size_row * count);
1862 			return true;
1863 
1864 		case SCROLL_WRAP_MOVE:
1865 			if (b - t - count > 3 * vc->vc_rows >> 2) {
1866 				if (t > 0)
1867 					fbcon_bmove(vc, 0, 0, count, 0, t,
1868 						    vc->vc_cols);
1869 				ywrap_up(vc, count);
1870 				if (vc->vc_rows - b > 0)
1871 					fbcon_bmove(vc, b - count, 0, b, 0,
1872 						    vc->vc_rows - b,
1873 						    vc->vc_cols);
1874 			} else if (info->flags & FBINFO_READS_FAST)
1875 				fbcon_bmove(vc, t + count, 0, t, 0,
1876 					    b - t - count, vc->vc_cols);
1877 			else
1878 				goto redraw_up;
1879 			__fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1880 			break;
1881 
1882 		case SCROLL_PAN_REDRAW:
1883 			if ((p->yscroll + count <=
1884 			     2 * (p->vrows - vc->vc_rows))
1885 			    && ((!scroll_partial && (b - t == vc->vc_rows))
1886 				|| (scroll_partial
1887 				    && (b - t - count >
1888 					3 * vc->vc_rows >> 2)))) {
1889 				if (t > 0)
1890 					fbcon_redraw_move(vc, p, 0, t, count);
1891 				ypan_up_redraw(vc, t, count);
1892 				if (vc->vc_rows - b > 0)
1893 					fbcon_redraw_move(vc, p, b,
1894 							  vc->vc_rows - b, b);
1895 			} else
1896 				fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1897 			__fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1898 			break;
1899 
1900 		case SCROLL_PAN_MOVE:
1901 			if ((p->yscroll + count <=
1902 			     2 * (p->vrows - vc->vc_rows))
1903 			    && ((!scroll_partial && (b - t == vc->vc_rows))
1904 				|| (scroll_partial
1905 				    && (b - t - count >
1906 					3 * vc->vc_rows >> 2)))) {
1907 				if (t > 0)
1908 					fbcon_bmove(vc, 0, 0, count, 0, t,
1909 						    vc->vc_cols);
1910 				ypan_up(vc, count);
1911 				if (vc->vc_rows - b > 0)
1912 					fbcon_bmove(vc, b - count, 0, b, 0,
1913 						    vc->vc_rows - b,
1914 						    vc->vc_cols);
1915 			} else if (info->flags & FBINFO_READS_FAST)
1916 				fbcon_bmove(vc, t + count, 0, t, 0,
1917 					    b - t - count, vc->vc_cols);
1918 			else
1919 				goto redraw_up;
1920 			__fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1921 			break;
1922 
1923 		case SCROLL_REDRAW:
1924 		      redraw_up:
1925 			fbcon_redraw(vc, t, b - t - count,
1926 				     count * vc->vc_cols);
1927 			__fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1928 			scr_memsetw((unsigned short *) (vc->vc_origin +
1929 							vc->vc_size_row *
1930 							(b - count)),
1931 				    vc->vc_video_erase_char,
1932 				    vc->vc_size_row * count);
1933 			return true;
1934 		}
1935 		break;
1936 
1937 	case SM_DOWN:
1938 		if (count > vc->vc_rows)	/* Maximum realistic size */
1939 			count = vc->vc_rows;
1940 		switch (fb_scrollmode(p)) {
1941 		case SCROLL_MOVE:
1942 			fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1943 				     -count);
1944 			__fbcon_clear(vc, t, 0, count, vc->vc_cols);
1945 			scr_memsetw((unsigned short *) (vc->vc_origin +
1946 							vc->vc_size_row *
1947 							t),
1948 				    vc->vc_video_erase_char,
1949 				    vc->vc_size_row * count);
1950 			return true;
1951 
1952 		case SCROLL_WRAP_MOVE:
1953 			if (b - t - count > 3 * vc->vc_rows >> 2) {
1954 				if (vc->vc_rows - b > 0)
1955 					fbcon_bmove(vc, b, 0, b - count, 0,
1956 						    vc->vc_rows - b,
1957 						    vc->vc_cols);
1958 				ywrap_down(vc, count);
1959 				if (t > 0)
1960 					fbcon_bmove(vc, count, 0, 0, 0, t,
1961 						    vc->vc_cols);
1962 			} else if (info->flags & FBINFO_READS_FAST)
1963 				fbcon_bmove(vc, t, 0, t + count, 0,
1964 					    b - t - count, vc->vc_cols);
1965 			else
1966 				goto redraw_down;
1967 			__fbcon_clear(vc, t, 0, count, vc->vc_cols);
1968 			break;
1969 
1970 		case SCROLL_PAN_MOVE:
1971 			if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1972 			    && ((!scroll_partial && (b - t == vc->vc_rows))
1973 				|| (scroll_partial
1974 				    && (b - t - count >
1975 					3 * vc->vc_rows >> 2)))) {
1976 				if (vc->vc_rows - b > 0)
1977 					fbcon_bmove(vc, b, 0, b - count, 0,
1978 						    vc->vc_rows - b,
1979 						    vc->vc_cols);
1980 				ypan_down(vc, count);
1981 				if (t > 0)
1982 					fbcon_bmove(vc, count, 0, 0, 0, t,
1983 						    vc->vc_cols);
1984 			} else if (info->flags & FBINFO_READS_FAST)
1985 				fbcon_bmove(vc, t, 0, t + count, 0,
1986 					    b - t - count, vc->vc_cols);
1987 			else
1988 				goto redraw_down;
1989 			__fbcon_clear(vc, t, 0, count, vc->vc_cols);
1990 			break;
1991 
1992 		case SCROLL_PAN_REDRAW:
1993 			if ((count - p->yscroll <= p->vrows - vc->vc_rows)
1994 			    && ((!scroll_partial && (b - t == vc->vc_rows))
1995 				|| (scroll_partial
1996 				    && (b - t - count >
1997 					3 * vc->vc_rows >> 2)))) {
1998 				if (vc->vc_rows - b > 0)
1999 					fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
2000 							  b - count);
2001 				ypan_down_redraw(vc, t, count);
2002 				if (t > 0)
2003 					fbcon_redraw_move(vc, p, count, t, 0);
2004 			} else
2005 				fbcon_redraw_move(vc, p, t, b - t - count, t + count);
2006 			__fbcon_clear(vc, t, 0, count, vc->vc_cols);
2007 			break;
2008 
2009 		case SCROLL_REDRAW:
2010 		      redraw_down:
2011 			fbcon_redraw(vc, b - 1, b - t - count,
2012 				     -count * vc->vc_cols);
2013 			__fbcon_clear(vc, t, 0, count, vc->vc_cols);
2014 			scr_memsetw((unsigned short *) (vc->vc_origin +
2015 							vc->vc_size_row *
2016 							t),
2017 				    vc->vc_video_erase_char,
2018 				    vc->vc_size_row * count);
2019 			return true;
2020 		}
2021 	}
2022 	return false;
2023 }
2024 
2025 
2026 static void updatescrollmode_accel(struct fbcon_display *p,
2027 					struct fb_info *info,
2028 					struct vc_data *vc)
2029 {
2030 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_LEGACY_ACCELERATION
2031 	struct fbcon_par *par = info->fbcon_par;
2032 	int cap = info->flags;
2033 	u16 t = 0;
2034 	int ypan = FBCON_SWAP(par->rotate, info->fix.ypanstep, info->fix.xpanstep);
2035 	int ywrap = FBCON_SWAP(par->rotate, info->fix.ywrapstep, t);
2036 	int yres = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres);
2037 	int vyres = FBCON_SWAP(par->rotate, info->var.yres_virtual, info->var.xres_virtual);
2038 	int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2039 		divides(ypan, vc->vc_font.height) && vyres > yres;
2040 	int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2041 		divides(ywrap, vc->vc_font.height) &&
2042 		divides(vc->vc_font.height, vyres) &&
2043 		divides(vc->vc_font.height, yres);
2044 	int reading_fast = cap & FBINFO_READS_FAST;
2045 	int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2046 		!(cap & FBINFO_HWACCEL_DISABLED);
2047 	int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2048 		!(cap & FBINFO_HWACCEL_DISABLED);
2049 
2050 	if (good_wrap || good_pan) {
2051 		if (reading_fast || fast_copyarea)
2052 			p->scrollmode = good_wrap ?
2053 				SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2054 		else
2055 			p->scrollmode = good_wrap ? SCROLL_REDRAW :
2056 				SCROLL_PAN_REDRAW;
2057 	} else {
2058 		if (reading_fast || (fast_copyarea && !fast_imageblit))
2059 			p->scrollmode = SCROLL_MOVE;
2060 		else
2061 			p->scrollmode = SCROLL_REDRAW;
2062 	}
2063 #endif
2064 }
2065 
2066 static void updatescrollmode(struct fbcon_display *p,
2067 					struct fb_info *info,
2068 					struct vc_data *vc)
2069 {
2070 	struct fbcon_par *par = info->fbcon_par;
2071 	int fh = vc->vc_font.height;
2072 	int yres = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres);
2073 	int vyres = FBCON_SWAP(par->rotate, info->var.yres_virtual, info->var.xres_virtual);
2074 
2075 	p->vrows = vyres/fh;
2076 	if (yres > (fh * (vc->vc_rows + 1)))
2077 		p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2078 	if ((yres % fh) && (vyres % fh < yres % fh))
2079 		p->vrows--;
2080 
2081 	/* update scrollmode in case hardware acceleration is used */
2082 	updatescrollmode_accel(p, info, vc);
2083 }
2084 
2085 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2086 			unsigned int height, bool from_user)
2087 {
2088 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
2089 	struct fbcon_par *par = info->fbcon_par;
2090 	struct fbcon_display *p = &fb_display[vc->vc_num];
2091 	struct fb_var_screeninfo var = info->var;
2092 	int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2093 
2094 	if (font_data_size(p->fontdata)) {
2095 		unsigned int size = vc_font_size(&vc->vc_font);
2096 
2097 		/*
2098 		 * If user font, ensure that a possible change to user font
2099 		 * height or width will not allow a font data out-of-bounds access.
2100 		 * NOTE: must use original charcount in calculation as font
2101 		 * charcount can change and cannot be used to determine the
2102 		 * font data allocated size.
2103 		 */
2104 		if (!size || size > font_data_size(p->fontdata))
2105 			return -EINVAL;
2106 	}
2107 
2108 	virt_w = FBCON_SWAP(par->rotate, width, height);
2109 	virt_h = FBCON_SWAP(par->rotate, height, width);
2110 	virt_fw = FBCON_SWAP(par->rotate, vc->vc_font.width, vc->vc_font.height);
2111 	virt_fh = FBCON_SWAP(par->rotate, vc->vc_font.height, vc->vc_font.width);
2112 	var.xres = virt_w * virt_fw;
2113 	var.yres = virt_h * virt_fh;
2114 	x_diff = info->var.xres - var.xres;
2115 	y_diff = info->var.yres - var.yres;
2116 	if (x_diff < 0 || x_diff > virt_fw ||
2117 	    y_diff < 0 || y_diff > virt_fh) {
2118 		const struct fb_videomode *mode;
2119 
2120 		pr_debug("attempting resize %ix%i\n", var.xres, var.yres);
2121 		mode = fb_find_best_mode(&var, &info->modelist);
2122 		if (mode == NULL)
2123 			return -EINVAL;
2124 		display_to_var(&var, p);
2125 		fb_videomode_to_var(&var, mode);
2126 
2127 		if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2128 			return -EINVAL;
2129 
2130 		pr_debug("resize now %ix%i\n", var.xres, var.yres);
2131 		if (con_is_visible(vc) && vc->vc_mode == KD_TEXT) {
2132 			var.activate = FB_ACTIVATE_NOW |
2133 				FB_ACTIVATE_FORCE;
2134 			fb_set_var(info, &var);
2135 		}
2136 		var_to_display(p, &info->var, info);
2137 		par->var = info->var;
2138 	}
2139 	updatescrollmode(p, info, vc);
2140 	return 0;
2141 }
2142 
2143 static bool fbcon_switch(struct vc_data *vc)
2144 {
2145 	struct fb_info *info, *old_info = NULL;
2146 	struct fbcon_par *par;
2147 	struct fbcon_display *p = &fb_display[vc->vc_num];
2148 	struct fb_var_screeninfo var;
2149 	int i, ret, prev_console;
2150 
2151 	info = fbcon_info_from_console(vc->vc_num);
2152 	par = info->fbcon_par;
2153 
2154 	if (logo_shown >= 0) {
2155 		struct vc_data *conp2 = vc_cons[logo_shown].d;
2156 
2157 		if (conp2->vc_top == logo_lines
2158 		    && conp2->vc_bottom == conp2->vc_rows)
2159 			conp2->vc_top = 0;
2160 		logo_shown = FBCON_LOGO_CANSHOW;
2161 	}
2162 
2163 	prev_console = par->currcon;
2164 	if (prev_console != -1)
2165 		old_info = fbcon_info_from_console(prev_console);
2166 	/*
2167 	 * FIXME: If we have multiple fbdev's loaded, we need to
2168 	 * update all info->currcon.  Perhaps, we can place this
2169 	 * in a centralized structure, but this might break some
2170 	 * drivers.
2171 	 *
2172 	 * info->currcon = vc->vc_num;
2173 	 */
2174 	fbcon_for_each_registered_fb(i) {
2175 		if (fbcon_registered_fb[i]->fbcon_par) {
2176 			struct fbcon_par *par = fbcon_registered_fb[i]->fbcon_par;
2177 
2178 			par->currcon = vc->vc_num;
2179 		}
2180 	}
2181 	memset(&var, 0, sizeof(struct fb_var_screeninfo));
2182 	display_to_var(&var, p);
2183 	var.activate = FB_ACTIVATE_NOW;
2184 
2185 	/*
2186 	 * make sure we don't unnecessarily trip the memcmp()
2187 	 * in fb_set_var()
2188 	 */
2189 	info->var.activate = var.activate;
2190 	var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
2191 	fb_set_var(info, &var);
2192 	par->var = info->var;
2193 
2194 	if (old_info != NULL && (old_info != info ||
2195 				 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2196 		if (info->fbops->fb_set_par) {
2197 			ret = info->fbops->fb_set_par(info);
2198 
2199 			if (ret)
2200 				printk(KERN_ERR "fbcon_switch: detected "
2201 					"unhandled fb_set_par error, "
2202 					"error code %d\n", ret);
2203 		}
2204 
2205 		if (old_info != info)
2206 			fbcon_del_cursor_work(old_info);
2207 	}
2208 
2209 	if (!fbcon_is_active(vc, info) || par->blank_state != FB_BLANK_UNBLANK)
2210 		fbcon_del_cursor_work(info);
2211 	else
2212 		fbcon_add_cursor_work(info);
2213 
2214 	set_blitting_type(vc, info);
2215 	par->cursor_reset = 1;
2216 
2217 	if (par->bitops->rotate_font && par->bitops->rotate_font(info, vc)) {
2218 		par->rotate = FB_ROTATE_UR;
2219 		set_blitting_type(vc, info);
2220 	}
2221 
2222 	vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2223 	vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2224 
2225 	if (vc->vc_font.charcount > 256)
2226 		vc->vc_complement_mask <<= 1;
2227 
2228 	updatescrollmode(p, info, vc);
2229 
2230 	switch (fb_scrollmode(p)) {
2231 	case SCROLL_WRAP_MOVE:
2232 		scrollback_phys_max = p->vrows - vc->vc_rows;
2233 		break;
2234 	case SCROLL_PAN_MOVE:
2235 	case SCROLL_PAN_REDRAW:
2236 		scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2237 		if (scrollback_phys_max < 0)
2238 			scrollback_phys_max = 0;
2239 		break;
2240 	default:
2241 		scrollback_phys_max = 0;
2242 		break;
2243 	}
2244 
2245 	scrollback_max = 0;
2246 	scrollback_current = 0;
2247 
2248 	if (fbcon_is_active(vc, info)) {
2249 		par->var.xoffset = par->var.yoffset = p->yscroll = 0;
2250 		par->bitops->update_start(info);
2251 	}
2252 
2253 	fbcon_set_palette(vc, color_table);
2254 	fbcon_clear_margins(vc, 0);
2255 
2256 	if (logo_shown == FBCON_LOGO_DRAW) {
2257 
2258 		logo_shown = fg_console;
2259 		fb_show_logo(info, par->rotate);
2260 		update_region(vc,
2261 			      vc->vc_origin + vc->vc_size_row * vc->vc_top,
2262 			      vc->vc_size_row * (vc->vc_bottom -
2263 						 vc->vc_top) / 2);
2264 		return false;
2265 	}
2266 	return true;
2267 }
2268 
2269 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2270 				int blank)
2271 {
2272 	if (blank) {
2273 		unsigned short charmask = vc->vc_hi_font_mask ?
2274 			0x1ff : 0xff;
2275 		unsigned short oldc;
2276 
2277 		oldc = vc->vc_video_erase_char;
2278 		vc->vc_video_erase_char &= charmask;
2279 		__fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2280 		vc->vc_video_erase_char = oldc;
2281 	}
2282 }
2283 
2284 static bool fbcon_blank(struct vc_data *vc, enum vesa_blank_mode blank,
2285 			bool mode_switch)
2286 {
2287 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
2288 	struct fbcon_par *par = info->fbcon_par;
2289 
2290 	if (mode_switch) {
2291 		struct fb_var_screeninfo var = info->var;
2292 
2293 		par->graphics = 1;
2294 
2295 		if (!blank) {
2296 			var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE |
2297 				FB_ACTIVATE_KD_TEXT;
2298 			fb_set_var(info, &var);
2299 			par->graphics = 0;
2300 			par->var = info->var;
2301 		}
2302 	}
2303 
2304 	if (fbcon_is_active(vc, info)) {
2305 		if (par->blank_state != blank) {
2306 			par->blank_state = blank;
2307 			fbcon_cursor(vc, !blank);
2308 			par->cursor_flash = (!blank);
2309 
2310 			if (fb_blank(info, blank))
2311 				fbcon_generic_blank(vc, info, blank);
2312 		}
2313 
2314 		if (!blank)
2315 			update_screen(vc);
2316 	}
2317 
2318 	if (mode_switch || !fbcon_is_active(vc, info) || par->blank_state != FB_BLANK_UNBLANK)
2319 		fbcon_del_cursor_work(info);
2320 	else
2321 		fbcon_add_cursor_work(info);
2322 
2323 	return false;
2324 }
2325 
2326 static int fbcon_get_font(struct vc_data *vc, struct console_font *font, unsigned int vpitch)
2327 {
2328 	const struct fbcon_display *p = &fb_display[vc->vc_num];
2329 
2330 	font->width = vc->vc_font.width;
2331 	font->height = vc->vc_font.height;
2332 	if (font->height > vpitch)
2333 		return -ENOSPC;
2334 	font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2335 
2336 	return font_data_export(p->fontdata, font, vpitch);
2337 }
2338 
2339 /* set/clear vc_hi_font_mask and update vc attrs accordingly */
2340 static void set_vc_hi_font(struct vc_data *vc, bool set)
2341 {
2342 	if (!set) {
2343 		vc->vc_hi_font_mask = 0;
2344 		if (vc->vc_can_do_color) {
2345 			vc->vc_complement_mask >>= 1;
2346 			vc->vc_s_complement_mask >>= 1;
2347 		}
2348 
2349 		/* ++Edmund: reorder the attribute bits */
2350 		if (vc->vc_can_do_color) {
2351 			unsigned short *cp =
2352 			    (unsigned short *) vc->vc_origin;
2353 			int count = vc->vc_screenbuf_size / 2;
2354 			unsigned short c;
2355 			for (; count > 0; count--, cp++) {
2356 				c = scr_readw(cp);
2357 				scr_writew(((c & 0xfe00) >> 1) |
2358 					   (c & 0xff), cp);
2359 			}
2360 			c = vc->vc_video_erase_char;
2361 			vc->vc_video_erase_char =
2362 			    ((c & 0xfe00) >> 1) | (c & 0xff);
2363 			vc->vc_attr >>= 1;
2364 		}
2365 	} else {
2366 		vc->vc_hi_font_mask = 0x100;
2367 		if (vc->vc_can_do_color) {
2368 			vc->vc_complement_mask <<= 1;
2369 			vc->vc_s_complement_mask <<= 1;
2370 		}
2371 
2372 		/* ++Edmund: reorder the attribute bits */
2373 		{
2374 			unsigned short *cp =
2375 			    (unsigned short *) vc->vc_origin;
2376 			int count = vc->vc_screenbuf_size / 2;
2377 			unsigned short c;
2378 			for (; count > 0; count--, cp++) {
2379 				unsigned short newc;
2380 				c = scr_readw(cp);
2381 				if (vc->vc_can_do_color)
2382 					newc =
2383 					    ((c & 0xff00) << 1) | (c &
2384 								   0xff);
2385 				else
2386 					newc = c & ~0x100;
2387 				scr_writew(newc, cp);
2388 			}
2389 			c = vc->vc_video_erase_char;
2390 			if (vc->vc_can_do_color) {
2391 				vc->vc_video_erase_char =
2392 				    ((c & 0xff00) << 1) | (c & 0xff);
2393 				vc->vc_attr <<= 1;
2394 			} else
2395 				vc->vc_video_erase_char = c & ~0x100;
2396 		}
2397 	}
2398 }
2399 
2400 static int fbcon_do_set_font(struct vc_data *vc, int w, int h, int charcount,
2401 			     font_data_t *data)
2402 {
2403 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
2404 	struct fbcon_par *par = info->fbcon_par;
2405 	struct fbcon_display *p = &fb_display[vc->vc_num];
2406 	int resize, ret, old_width, old_height, old_charcount;
2407 	font_data_t *old_fontdata = p->fontdata;
2408 	const u8 *old_data = vc->vc_font.data;
2409 	unsigned short old_hi_font_mask = vc->vc_hi_font_mask;
2410 
2411 	font_data_get(data);
2412 
2413 	resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2414 	p->fontdata = data;
2415 	vc->vc_font.data = font_data_buf(p->fontdata);
2416 	old_width = vc->vc_font.width;
2417 	old_height = vc->vc_font.height;
2418 	old_charcount = vc->vc_font.charcount;
2419 
2420 	vc->vc_font.width = w;
2421 	vc->vc_font.height = h;
2422 	vc->vc_font.charcount = charcount;
2423 	if (vc->vc_hi_font_mask && charcount == 256)
2424 		set_vc_hi_font(vc, false);
2425 	else if (!vc->vc_hi_font_mask && charcount == 512)
2426 		set_vc_hi_font(vc, true);
2427 
2428 	if (resize) {
2429 		int cols, rows;
2430 
2431 		cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres);
2432 		rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres);
2433 		cols /= w;
2434 		rows /= h;
2435 		ret = vc_resize(vc, cols, rows);
2436 		if (ret)
2437 			goto err_out;
2438 	} else if (con_is_visible(vc)
2439 		   && vc->vc_mode == KD_TEXT) {
2440 		fbcon_clear_margins(vc, 0);
2441 		update_screen(vc);
2442 	}
2443 
2444 	if (old_fontdata)
2445 		font_data_put(old_fontdata);
2446 
2447 	return 0;
2448 
2449 err_out:
2450 	p->fontdata = old_fontdata;
2451 	vc->vc_font.data = old_data;
2452 	vc->vc_font.width = old_width;
2453 	vc->vc_font.height = old_height;
2454 	vc->vc_font.charcount = old_charcount;
2455 
2456 	/* Restore the hi_font state and screen buffer */
2457 	if (old_hi_font_mask && !vc->vc_hi_font_mask)
2458 		set_vc_hi_font(vc, true);
2459 	else if (!old_hi_font_mask && vc->vc_hi_font_mask)
2460 		set_vc_hi_font(vc, false);
2461 
2462 	font_data_put(data);
2463 
2464 	return ret;
2465 }
2466 
2467 /*
2468  *  User asked to set font; we are guaranteed that charcount does not exceed 512
2469  *  but lets not assume that, since charcount of 512 is small for unicode support.
2470  */
2471 
2472 static int fbcon_set_font(struct vc_data *vc, const struct console_font *font,
2473 			  unsigned int vpitch, unsigned int flags)
2474 {
2475 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
2476 	unsigned charcount = font->charcount;
2477 	int w = font->width;
2478 	int h = font->height;
2479 	int i, ret;
2480 	font_data_t *new_data;
2481 
2482 	/* Is there a reason why fbconsole couldn't handle any charcount >256?
2483 	 * If not this check should be changed to charcount < 256 */
2484 	if (charcount != 256 && charcount != 512)
2485 		return -EINVAL;
2486 
2487 	/* font bigger than screen resolution ? */
2488 	if (w > FBCON_SWAP(info->var.rotate, info->var.xres, info->var.yres) ||
2489 	    h > FBCON_SWAP(info->var.rotate, info->var.yres, info->var.xres))
2490 		return -EINVAL;
2491 
2492 	if (font->width > FB_MAX_BLIT_WIDTH || font->height > FB_MAX_BLIT_HEIGHT)
2493 		return -EINVAL;
2494 
2495 	/* Make sure drawing engine can handle the font */
2496 	if (!test_bit(font->width - 1, info->pixmap.blit_x) ||
2497 	    !test_bit(font->height - 1, info->pixmap.blit_y))
2498 		return -EINVAL;
2499 
2500 	/* Make sure driver can handle the font length */
2501 	if (fbcon_invalid_charcount(info, charcount))
2502 		return -EINVAL;
2503 
2504 	new_data = font_data_import(font, vpitch, crc32);
2505 	if (IS_ERR(new_data))
2506 		return PTR_ERR(new_data);
2507 
2508 	/* Check if the same font is on some other console already */
2509 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2510 		if (fb_display[i].fontdata &&
2511 		    font_data_is_equal(fb_display[i].fontdata, new_data)) {
2512 			font_data_get(fb_display[i].fontdata);
2513 			font_data_put(new_data);
2514 			new_data = fb_display[i].fontdata;
2515 			break;
2516 		}
2517 	}
2518 	ret = fbcon_do_set_font(vc, font->width, font->height, charcount, new_data);
2519 	font_data_put(new_data);
2520 
2521 	return ret;
2522 }
2523 
2524 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font,
2525 			      const char *name)
2526 {
2527 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
2528 	const struct font_desc *f;
2529 
2530 	if (!name)
2531 		f = get_default_font(info->var.xres, info->var.yres,
2532 				     info->pixmap.blit_x, info->pixmap.blit_y);
2533 	else if (!(f = find_font(name)))
2534 		return -ENOENT;
2535 
2536 	font->width = f->width;
2537 	font->height = f->height;
2538 	return fbcon_do_set_font(vc, f->width, f->height, f->charcount, f->data);
2539 }
2540 
2541 static u16 palette_red[16];
2542 static u16 palette_green[16];
2543 static u16 palette_blue[16];
2544 
2545 static struct fb_cmap palette_cmap = {
2546 	0, 16, palette_red, palette_green, palette_blue, NULL
2547 };
2548 
2549 static void fbcon_set_palette(struct vc_data *vc, const unsigned char *table)
2550 {
2551 	struct fb_info *info = fbcon_info_from_console(vc->vc_num);
2552 	int i, j, k, depth;
2553 	u8 val;
2554 
2555 	if (!fbcon_is_active(vc, info))
2556 		return;
2557 
2558 	if (!con_is_visible(vc))
2559 		return;
2560 
2561 	depth = fb_get_color_depth(&info->var, &info->fix);
2562 	if (depth > 3) {
2563 		for (i = j = 0; i < 16; i++) {
2564 			k = table[i];
2565 			val = vc->vc_palette[j++];
2566 			palette_red[k] = (val << 8) | val;
2567 			val = vc->vc_palette[j++];
2568 			palette_green[k] = (val << 8) | val;
2569 			val = vc->vc_palette[j++];
2570 			palette_blue[k] = (val << 8) | val;
2571 		}
2572 		palette_cmap.len = 16;
2573 		palette_cmap.start = 0;
2574 	/*
2575 	 * If framebuffer is capable of less than 16 colors,
2576 	 * use default palette of fbcon.
2577 	 */
2578 	} else
2579 		fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2580 
2581 	fb_set_cmap(&palette_cmap, info);
2582 }
2583 
2584 /* As we might be inside of softback, we may work with non-contiguous buffer,
2585    that's why we have to use a separate routine. */
2586 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2587 {
2588 	while (cnt--) {
2589 		u16 a = scr_readw(p);
2590 		if (!vc->vc_can_do_color)
2591 			a ^= 0x0800;
2592 		else if (vc->vc_hi_font_mask == 0x100)
2593 			a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2594 			    (((a) & 0x0e00) << 4);
2595 		else
2596 			a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2597 			    (((a) & 0x0700) << 4);
2598 		scr_writew(a, p++);
2599 	}
2600 }
2601 
2602 void fbcon_suspended(struct fb_info *info)
2603 {
2604 	struct vc_data *vc = NULL;
2605 	struct fbcon_par *par = info->fbcon_par;
2606 
2607 	if (!par || par->currcon < 0)
2608 		return;
2609 	vc = vc_cons[par->currcon].d;
2610 
2611 	/* Clear cursor, restore saved data when in text mode */
2612 	if ((vc->vc_mode == KD_TEXT) && con_is_visible(vc))
2613 		fbcon_cursor(vc, false);
2614 }
2615 
2616 void fbcon_resumed(struct fb_info *info)
2617 {
2618 	struct vc_data *vc;
2619 	struct fbcon_par *par = info->fbcon_par;
2620 
2621 	if (!par || par->currcon < 0)
2622 		return;
2623 	vc = vc_cons[par->currcon].d;
2624 
2625 	/* Update screen when in text mode only */
2626 	if ((vc->vc_mode == KD_TEXT) && con_is_visible(vc))
2627 		update_screen(vc);
2628 }
2629 
2630 static void fbcon_modechanged(struct fb_info *info)
2631 {
2632 	struct fbcon_par *par = info->fbcon_par;
2633 	struct vc_data *vc;
2634 	struct fbcon_display *p;
2635 	int rows, cols;
2636 
2637 	if (!par || par->currcon < 0)
2638 		return;
2639 	vc = vc_cons[par->currcon].d;
2640 	if (vc->vc_mode != KD_TEXT ||
2641 	    fbcon_info_from_console(par->currcon) != info)
2642 		return;
2643 
2644 	p = &fb_display[vc->vc_num];
2645 	set_blitting_type(vc, info);
2646 
2647 	if (con_is_visible(vc)) {
2648 		var_to_display(p, &info->var, info);
2649 		cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres);
2650 		rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres);
2651 		cols /= vc->vc_font.width;
2652 		rows /= vc->vc_font.height;
2653 		vc_resize(vc, cols, rows);
2654 		updatescrollmode(p, info, vc);
2655 		scrollback_max = 0;
2656 		scrollback_current = 0;
2657 
2658 		if (fbcon_is_active(vc, info)) {
2659 			par->var.xoffset = par->var.yoffset = p->yscroll = 0;
2660 			par->bitops->update_start(info);
2661 		}
2662 
2663 		fbcon_set_palette(vc, color_table);
2664 		update_screen(vc);
2665 	}
2666 }
2667 
2668 static void fbcon_set_all_vcs(struct fb_info *info)
2669 {
2670 	struct fbcon_par *par = info->fbcon_par;
2671 	struct vc_data *vc;
2672 	struct fbcon_display *p;
2673 	int i, rows, cols, fg = -1;
2674 
2675 	if (!par || par->currcon < 0)
2676 		return;
2677 
2678 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2679 		vc = vc_cons[i].d;
2680 		if (!vc || vc->vc_mode != KD_TEXT ||
2681 		    fbcon_info_from_console(i) != info)
2682 			continue;
2683 
2684 		if (con_is_visible(vc)) {
2685 			fg = i;
2686 			continue;
2687 		}
2688 
2689 		p = &fb_display[vc->vc_num];
2690 		set_blitting_type(vc, info);
2691 		var_to_display(p, &info->var, info);
2692 		cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres);
2693 		rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres);
2694 		cols /= vc->vc_font.width;
2695 		rows /= vc->vc_font.height;
2696 		vc_resize(vc, cols, rows);
2697 	}
2698 
2699 	if (fg != -1)
2700 		fbcon_modechanged(info);
2701 }
2702 
2703 
2704 void fbcon_update_vcs(struct fb_info *info, bool all)
2705 {
2706 	if (all)
2707 		fbcon_set_all_vcs(info);
2708 	else
2709 		fbcon_modechanged(info);
2710 }
2711 
2712 /* let fbcon check if it supports a new screen resolution */
2713 int fbcon_modechange_possible(struct fb_info *info, struct fb_var_screeninfo *var)
2714 {
2715 	struct fbcon_par *par = info->fbcon_par;
2716 	struct vc_data *vc;
2717 	unsigned int i;
2718 
2719 	WARN_CONSOLE_UNLOCKED();
2720 
2721 	if (!par)
2722 		return 0;
2723 
2724 	/* prevent setting a screen size which is smaller than font size */
2725 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2726 		vc = vc_cons[i].d;
2727 		if (!vc || vc->vc_mode != KD_TEXT ||
2728 			   fbcon_info_from_console(i) != info)
2729 			continue;
2730 
2731 		if (vc->vc_font.width  > FBCON_SWAP(var->rotate, var->xres, var->yres) ||
2732 		    vc->vc_font.height > FBCON_SWAP(var->rotate, var->yres, var->xres))
2733 			return -EINVAL;
2734 	}
2735 
2736 	return 0;
2737 }
2738 
2739 int fbcon_mode_deleted(struct fb_info *info,
2740 		       struct fb_videomode *mode)
2741 {
2742 	struct fb_info *fb_info;
2743 	struct fbcon_display *p;
2744 	int i, j, found = 0;
2745 
2746 	/* before deletion, ensure that mode is not in use */
2747 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2748 		j = con2fb_map[i];
2749 		if (j == -1)
2750 			continue;
2751 		fb_info = fbcon_registered_fb[j];
2752 		if (fb_info != info)
2753 			continue;
2754 		p = &fb_display[i];
2755 		if (!p || !p->mode)
2756 			continue;
2757 		if (fb_mode_is_equal(p->mode, mode)) {
2758 			found = 1;
2759 			break;
2760 		}
2761 	}
2762 	return found;
2763 }
2764 
2765 static void fbcon_delete_mode(struct fb_videomode *m)
2766 {
2767 	struct fbcon_display *p;
2768 
2769 	for (int i = first_fb_vc; i <= last_fb_vc; i++) {
2770 		p = &fb_display[i];
2771 		if (p->mode == m)
2772 			p->mode = NULL;
2773 	}
2774 }
2775 
2776 void fbcon_delete_modelist(struct list_head *head)
2777 {
2778 	struct fb_modelist *modelist;
2779 
2780 	list_for_each_entry(modelist, head, list)
2781 		fbcon_delete_mode(&modelist->mode);
2782 }
2783 
2784 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
2785 static void fbcon_unbind(void)
2786 {
2787 	int ret;
2788 
2789 	ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
2790 				fbcon_is_default);
2791 
2792 	if (!ret)
2793 		fbcon_has_console_bind = false;
2794 }
2795 #else
2796 static inline void fbcon_unbind(void) {}
2797 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
2798 
2799 void fbcon_fb_unbind(struct fb_info *info)
2800 {
2801 	int i, new_idx = -1;
2802 	int idx = info->node;
2803 
2804 	console_lock();
2805 
2806 	if (!fbcon_has_console_bind) {
2807 		console_unlock();
2808 		return;
2809 	}
2810 
2811 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2812 		if (con2fb_map[i] != idx &&
2813 		    con2fb_map[i] != -1) {
2814 			new_idx = con2fb_map[i];
2815 			break;
2816 		}
2817 	}
2818 
2819 	if (new_idx != -1) {
2820 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2821 			if (con2fb_map[i] == idx)
2822 				set_con2fb_map(i, new_idx, 0);
2823 		}
2824 	} else {
2825 		struct fb_info *info = fbcon_registered_fb[idx];
2826 
2827 		/* This is sort of like set_con2fb_map, except it maps
2828 		 * the consoles to no device and then releases the
2829 		 * oldinfo to free memory and cancel the cursor blink
2830 		 * timer. I can imagine this just becoming part of
2831 		 * set_con2fb_map where new_idx is -1
2832 		 */
2833 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2834 			if (con2fb_map[i] == idx) {
2835 				con2fb_map[i] = -1;
2836 				if (!search_fb_in_map(idx)) {
2837 					con2fb_release_oldinfo(vc_cons[i].d,
2838 							       info, NULL);
2839 				}
2840 			}
2841 		}
2842 		fbcon_unbind();
2843 	}
2844 
2845 	console_unlock();
2846 }
2847 
2848 void fbcon_fb_unregistered(struct fb_info *info)
2849 {
2850 	int i, idx;
2851 
2852 	console_lock();
2853 
2854 	if (info->device && dev_is_pci(info->device))
2855 		vga_switcheroo_client_fb_set(to_pci_dev(info->device), NULL);
2856 
2857 	fbcon_registered_fb[info->node] = NULL;
2858 	fbcon_num_registered_fb--;
2859 
2860 	if (deferred_takeover) {
2861 		console_unlock();
2862 		return;
2863 	}
2864 
2865 	idx = info->node;
2866 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2867 		if (con2fb_map[i] == idx)
2868 			con2fb_map[i] = -1;
2869 	}
2870 
2871 	if (idx == info_idx) {
2872 		info_idx = -1;
2873 
2874 		fbcon_for_each_registered_fb(i) {
2875 			info_idx = i;
2876 			break;
2877 		}
2878 	}
2879 
2880 	if (info_idx != -1) {
2881 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2882 			if (con2fb_map[i] == -1)
2883 				con2fb_map[i] = info_idx;
2884 		}
2885 	}
2886 
2887 	if (primary_device == idx)
2888 		primary_device = -1;
2889 
2890 	if (!fbcon_num_registered_fb)
2891 		do_unregister_con_driver(&fb_con);
2892 	console_unlock();
2893 }
2894 
2895 void fbcon_remap_all(struct fb_info *info)
2896 {
2897 	int i, idx = info->node;
2898 
2899 	console_lock();
2900 	if (deferred_takeover) {
2901 		for (i = first_fb_vc; i <= last_fb_vc; i++)
2902 			con2fb_map_boot[i] = idx;
2903 		fbcon_map_override();
2904 		console_unlock();
2905 		return;
2906 	}
2907 
2908 	for (i = first_fb_vc; i <= last_fb_vc; i++)
2909 		set_con2fb_map(i, idx, 0);
2910 
2911 	if (con_is_bound(&fb_con)) {
2912 		printk(KERN_INFO "fbcon: Remapping primary device, "
2913 		       "fb%i, to tty %i-%i\n", idx,
2914 		       first_fb_vc + 1, last_fb_vc + 1);
2915 		info_idx = idx;
2916 	}
2917 	console_unlock();
2918 }
2919 
2920 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
2921 static void fbcon_select_primary(struct fb_info *info)
2922 {
2923 	if (!map_override && primary_device == -1 &&
2924 	    video_is_primary_device(info->device)) {
2925 		int i;
2926 
2927 		printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
2928 		       info->fix.id, info->node);
2929 		primary_device = info->node;
2930 
2931 		for (i = first_fb_vc; i <= last_fb_vc; i++)
2932 			con2fb_map_boot[i] = primary_device;
2933 
2934 		if (con_is_bound(&fb_con)) {
2935 			printk(KERN_INFO "fbcon: Remapping primary device, "
2936 			       "fb%i, to tty %i-%i\n", info->node,
2937 			       first_fb_vc + 1, last_fb_vc + 1);
2938 			info_idx = primary_device;
2939 		}
2940 	}
2941 
2942 }
2943 #else
2944 static inline void fbcon_select_primary(struct fb_info *info)
2945 {
2946 	return;
2947 }
2948 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
2949 
2950 static bool lockless_register_fb;
2951 module_param_named_unsafe(lockless_register_fb, lockless_register_fb, bool, 0400);
2952 MODULE_PARM_DESC(lockless_register_fb,
2953 	"Lockless framebuffer registration for debugging [default=off]");
2954 
2955 /* called with console_lock held */
2956 static int do_fb_registered(struct fb_info *info)
2957 {
2958 	int ret = 0, i, idx;
2959 
2960 	WARN_CONSOLE_UNLOCKED();
2961 
2962 	fbcon_registered_fb[info->node] = info;
2963 	fbcon_num_registered_fb++;
2964 
2965 	idx = info->node;
2966 	fbcon_select_primary(info);
2967 
2968 	if (deferred_takeover) {
2969 		pr_info("fbcon: Deferring console take-over\n");
2970 		return 0;
2971 	}
2972 
2973 	if (info_idx == -1) {
2974 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2975 			if (con2fb_map_boot[i] == idx) {
2976 				info_idx = idx;
2977 				break;
2978 			}
2979 		}
2980 
2981 		if (info_idx != -1)
2982 			ret = do_fbcon_takeover(1);
2983 	} else {
2984 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2985 			if (con2fb_map_boot[i] == idx)
2986 				set_con2fb_map(i, idx, 0);
2987 		}
2988 	}
2989 
2990 	/* Set the fb info for vga_switcheroo clients. Does nothing otherwise. */
2991 	if (info->device && dev_is_pci(info->device))
2992 		vga_switcheroo_client_fb_set(to_pci_dev(info->device), info);
2993 
2994 	return ret;
2995 }
2996 
2997 int fbcon_fb_registered(struct fb_info *info)
2998 {
2999 	int ret;
3000 
3001 	if (!lockless_register_fb)
3002 		console_lock();
3003 	else
3004 		atomic_inc(&ignore_console_lock_warning);
3005 
3006 	ret = do_fb_registered(info);
3007 
3008 	if (!lockless_register_fb)
3009 		console_unlock();
3010 	else
3011 		atomic_dec(&ignore_console_lock_warning);
3012 
3013 	return ret;
3014 }
3015 
3016 void fbcon_fb_blanked(struct fb_info *info, int blank)
3017 {
3018 	struct fbcon_par *par = info->fbcon_par;
3019 	struct vc_data *vc;
3020 
3021 	if (!par || par->currcon < 0)
3022 		return;
3023 
3024 	vc = vc_cons[par->currcon].d;
3025 	if (vc->vc_mode != KD_TEXT || fbcon_info_from_console(par->currcon) != info)
3026 		return;
3027 
3028 	if (con_is_visible(vc)) {
3029 		if (blank)
3030 			do_blank_screen(0);
3031 		else
3032 			do_unblank_screen(0);
3033 	}
3034 	par->blank_state = blank;
3035 }
3036 
3037 void fbcon_new_modelist(struct fb_info *info)
3038 {
3039 	int i;
3040 	struct vc_data *vc;
3041 	struct fb_var_screeninfo var;
3042 	const struct fb_videomode *mode;
3043 
3044 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
3045 		if (fbcon_info_from_console(i) != info)
3046 			continue;
3047 		if (!fb_display[i].mode)
3048 			continue;
3049 		vc = vc_cons[i].d;
3050 		display_to_var(&var, &fb_display[i]);
3051 		mode = fb_find_nearest_mode(fb_display[i].mode,
3052 					    &info->modelist);
3053 		fb_videomode_to_var(&var, mode);
3054 		fbcon_set_disp(info, &var, vc->vc_num);
3055 	}
3056 }
3057 
3058 void fbcon_get_requirement(struct fb_info *info,
3059 			   struct fb_blit_caps *caps)
3060 {
3061 	struct vc_data *vc;
3062 
3063 	if (caps->flags) {
3064 		int i, charcnt;
3065 
3066 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3067 			vc = vc_cons[i].d;
3068 			if (vc && vc->vc_mode == KD_TEXT &&
3069 			    info->node == con2fb_map[i]) {
3070 				set_bit(vc->vc_font.width - 1, caps->x);
3071 				set_bit(vc->vc_font.height - 1, caps->y);
3072 				charcnt = vc->vc_font.charcount;
3073 				if (caps->len < charcnt)
3074 					caps->len = charcnt;
3075 			}
3076 		}
3077 	} else {
3078 		vc = vc_cons[fg_console].d;
3079 
3080 		if (vc && vc->vc_mode == KD_TEXT &&
3081 		    info->node == con2fb_map[fg_console]) {
3082 			bitmap_zero(caps->x, FB_MAX_BLIT_WIDTH);
3083 			set_bit(vc->vc_font.width - 1, caps->x);
3084 			bitmap_zero(caps->y, FB_MAX_BLIT_HEIGHT);
3085 			set_bit(vc->vc_font.height - 1, caps->y);
3086 			caps->len = vc->vc_font.charcount;
3087 		}
3088 	}
3089 }
3090 
3091 int fbcon_set_con2fb_map_ioctl(void __user *argp)
3092 {
3093 	struct fb_con2fbmap con2fb;
3094 	int ret;
3095 
3096 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
3097 		return -EFAULT;
3098 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
3099 		return -EINVAL;
3100 	if (con2fb.framebuffer >= FB_MAX)
3101 		return -EINVAL;
3102 	if (!fbcon_registered_fb[con2fb.framebuffer])
3103 		request_module("fb%d", con2fb.framebuffer);
3104 	if (!fbcon_registered_fb[con2fb.framebuffer]) {
3105 		return -EINVAL;
3106 	}
3107 
3108 	console_lock();
3109 	ret = set_con2fb_map(con2fb.console - 1,
3110 			     con2fb.framebuffer, 1);
3111 	console_unlock();
3112 
3113 	return ret;
3114 }
3115 
3116 int fbcon_get_con2fb_map_ioctl(void __user *argp)
3117 {
3118 	struct fb_con2fbmap con2fb;
3119 
3120 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
3121 		return -EFAULT;
3122 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
3123 		return -EINVAL;
3124 
3125 	console_lock();
3126 	con2fb.framebuffer = con2fb_map[con2fb.console - 1];
3127 	console_unlock();
3128 
3129 	return copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
3130 }
3131 
3132 /*
3133  *  The console `switch' structure for the frame buffer based console
3134  */
3135 
3136 static const struct consw fb_con = {
3137 	.owner			= THIS_MODULE,
3138 	.con_startup 		= fbcon_startup,
3139 	.con_init 		= fbcon_init,
3140 	.con_deinit 		= fbcon_deinit,
3141 	.con_clear 		= fbcon_clear,
3142 	.con_putcs 		= fbcon_putcs,
3143 	.con_cursor 		= fbcon_cursor,
3144 	.con_scroll 		= fbcon_scroll,
3145 	.con_switch 		= fbcon_switch,
3146 	.con_blank 		= fbcon_blank,
3147 	.con_font_set 		= fbcon_set_font,
3148 	.con_font_get 		= fbcon_get_font,
3149 	.con_font_default	= fbcon_set_def_font,
3150 	.con_set_palette 	= fbcon_set_palette,
3151 	.con_invert_region 	= fbcon_invert_region,
3152 	.con_resize             = fbcon_resize,
3153 };
3154 
3155 static ssize_t rotate_store(struct device *device,
3156 			    struct device_attribute *attr, const char *buf,
3157 			    size_t count)
3158 {
3159 	struct fb_info *info;
3160 	int rotate, idx;
3161 	char **last = NULL;
3162 
3163 	console_lock();
3164 	idx = con2fb_map[fg_console];
3165 
3166 	if (idx == -1 || fbcon_registered_fb[idx] == NULL)
3167 		goto err;
3168 
3169 	info = fbcon_registered_fb[idx];
3170 	rotate = simple_strtoul(buf, last, 0);
3171 	fbcon_rotate(info, rotate);
3172 err:
3173 	console_unlock();
3174 	return count;
3175 }
3176 
3177 static ssize_t rotate_all_store(struct device *device,
3178 				struct device_attribute *attr,const char *buf,
3179 				size_t count)
3180 {
3181 	struct fb_info *info;
3182 	int rotate, idx;
3183 	char **last = NULL;
3184 
3185 	console_lock();
3186 	idx = con2fb_map[fg_console];
3187 
3188 	if (idx == -1 || fbcon_registered_fb[idx] == NULL)
3189 		goto err;
3190 
3191 	info = fbcon_registered_fb[idx];
3192 	rotate = simple_strtoul(buf, last, 0);
3193 	fbcon_rotate_all(info, rotate);
3194 err:
3195 	console_unlock();
3196 	return count;
3197 }
3198 
3199 static ssize_t rotate_show(struct device *device,
3200 			   struct device_attribute *attr,char *buf)
3201 {
3202 	struct fb_info *info;
3203 	int rotate = 0, idx;
3204 
3205 	console_lock();
3206 	idx = con2fb_map[fg_console];
3207 
3208 	if (idx == -1 || fbcon_registered_fb[idx] == NULL)
3209 		goto err;
3210 
3211 	info = fbcon_registered_fb[idx];
3212 	rotate = fbcon_get_rotate(info);
3213 err:
3214 	console_unlock();
3215 	return sysfs_emit(buf, "%d\n", rotate);
3216 }
3217 
3218 static ssize_t cursor_blink_show(struct device *device,
3219 				 struct device_attribute *attr, char *buf)
3220 {
3221 	struct fb_info *info;
3222 	struct fbcon_par *par;
3223 	int idx, blink = -1;
3224 
3225 	console_lock();
3226 	idx = con2fb_map[fg_console];
3227 
3228 	if (idx == -1 || fbcon_registered_fb[idx] == NULL)
3229 		goto err;
3230 
3231 	info = fbcon_registered_fb[idx];
3232 	par = info->fbcon_par;
3233 
3234 	if (!par)
3235 		goto err;
3236 
3237 	blink = delayed_work_pending(&par->cursor_work);
3238 err:
3239 	console_unlock();
3240 	return sysfs_emit(buf, "%d\n", blink);
3241 }
3242 
3243 static ssize_t cursor_blink_store(struct device *device,
3244 				  struct device_attribute *attr,
3245 				  const char *buf, size_t count)
3246 {
3247 	struct fb_info *info;
3248 	char **last = NULL;
3249 	bool blink;
3250 	int idx;
3251 
3252 	console_lock();
3253 	idx = con2fb_map[fg_console];
3254 
3255 	if (idx == -1 || fbcon_registered_fb[idx] == NULL)
3256 		goto err;
3257 
3258 	info = fbcon_registered_fb[idx];
3259 
3260 	if (!info->fbcon_par)
3261 		goto err;
3262 
3263 	blink = simple_strtoul(buf, last, 0);
3264 
3265 	if (blink) {
3266 		fbcon_cursor_blink = true;
3267 		fbcon_add_cursor_work(info);
3268 	} else {
3269 		fbcon_cursor_blink = false;
3270 		fbcon_del_cursor_work(info);
3271 	}
3272 
3273 err:
3274 	console_unlock();
3275 	return count;
3276 }
3277 
3278 static DEVICE_ATTR_RW(cursor_blink);
3279 static DEVICE_ATTR_RW(rotate);
3280 static DEVICE_ATTR_WO(rotate_all);
3281 
3282 static struct attribute *fbcon_device_attrs[] = {
3283 	&dev_attr_cursor_blink.attr,
3284 	&dev_attr_rotate.attr,
3285 	&dev_attr_rotate_all.attr,
3286 	NULL
3287 };
3288 
3289 ATTRIBUTE_GROUPS(fbcon_device);
3290 
3291 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3292 static void fbcon_register_existing_fbs(struct work_struct *work)
3293 {
3294 	int i;
3295 
3296 	console_lock();
3297 
3298 	deferred_takeover = false;
3299 	logo_shown = FBCON_LOGO_DONTSHOW;
3300 
3301 	fbcon_for_each_registered_fb(i)
3302 		do_fb_registered(fbcon_registered_fb[i]);
3303 
3304 	console_unlock();
3305 }
3306 
3307 static struct notifier_block fbcon_output_nb;
3308 static DECLARE_WORK(fbcon_deferred_takeover_work, fbcon_register_existing_fbs);
3309 
3310 static int fbcon_output_notifier(struct notifier_block *nb,
3311 				 unsigned long action, void *data)
3312 {
3313 	WARN_CONSOLE_UNLOCKED();
3314 
3315 	pr_info("fbcon: Taking over console\n");
3316 
3317 	dummycon_unregister_output_notifier(&fbcon_output_nb);
3318 
3319 	/* We may get called in atomic context */
3320 	schedule_work(&fbcon_deferred_takeover_work);
3321 
3322 	return NOTIFY_OK;
3323 }
3324 #endif
3325 
3326 static void fbcon_start(void)
3327 {
3328 	WARN_CONSOLE_UNLOCKED();
3329 
3330 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3331 	if (conswitchp != &dummy_con)
3332 		deferred_takeover = false;
3333 
3334 	if (deferred_takeover) {
3335 		fbcon_output_nb.notifier_call = fbcon_output_notifier;
3336 		dummycon_register_output_notifier(&fbcon_output_nb);
3337 		return;
3338 	}
3339 #endif
3340 }
3341 
3342 void __init fb_console_init(void)
3343 {
3344 	int i;
3345 
3346 	console_lock();
3347 	fbcon_device = device_create_with_groups(fb_class, NULL,
3348 						 MKDEV(0, 0), NULL,
3349 						 fbcon_device_groups, "fbcon");
3350 
3351 	if (IS_ERR(fbcon_device)) {
3352 		printk(KERN_WARNING "Unable to create device "
3353 		       "for fbcon; errno = %ld\n",
3354 		       PTR_ERR(fbcon_device));
3355 		fbcon_device = NULL;
3356 	}
3357 
3358 	for (i = 0; i < MAX_NR_CONSOLES; i++)
3359 		con2fb_map[i] = -1;
3360 
3361 	fbcon_start();
3362 	console_unlock();
3363 }
3364 
3365 #ifdef MODULE
3366 
3367 void __exit fb_console_exit(void)
3368 {
3369 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3370 	console_lock();
3371 	if (deferred_takeover)
3372 		dummycon_unregister_output_notifier(&fbcon_output_nb);
3373 	console_unlock();
3374 
3375 	cancel_work_sync(&fbcon_deferred_takeover_work);
3376 #endif
3377 
3378 	console_lock();
3379 	device_destroy(fb_class, MKDEV(0, 0));
3380 
3381 	do_unregister_con_driver(&fb_con);
3382 	console_unlock();
3383 }
3384 #endif
3385