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