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