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