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