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