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