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