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