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