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