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