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