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