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