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