xref: /linux/drivers/video/fbdev/core/fbcon.c (revision 15a1fbdcfb519c2bd291ed01c6c94e0b89537a77)
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, 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->vc_y + logo_lines >= rows)
659 				lines = rows - vc->vc_y - 1;
660 			else
661 				lines = logo_lines;
662 			vc->vc_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, logo_lines * new_cols * 2);
680 		vc->vc_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 	return;
1286 }
1287 
1288 /* ====================================================================== */
1289 
1290 /*  fbcon_XXX routines - interface used by the world
1291  *
1292  *  This system is now divided into two levels because of complications
1293  *  caused by hardware scrolling. Top level functions:
1294  *
1295  *	fbcon_bmove(), fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1296  *
1297  *  handles y values in range [0, scr_height-1] that correspond to real
1298  *  screen positions. y_wrap shift means that first line of bitmap may be
1299  *  anywhere on this display. These functions convert lineoffsets to
1300  *  bitmap offsets and deal with the wrap-around case by splitting blits.
1301  *
1302  *	fbcon_bmove_physical_8()    -- These functions fast implementations
1303  *	fbcon_clear_physical_8()    -- of original fbcon_XXX fns.
1304  *	fbcon_putc_physical_8()	    -- (font width != 8) may be added later
1305  *
1306  *  WARNING:
1307  *
1308  *  At the moment fbcon_putc() cannot blit across vertical wrap boundary
1309  *  Implies should only really hardware scroll in rows. Only reason for
1310  *  restriction is simplicity & efficiency at the moment.
1311  */
1312 
1313 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1314 			int width)
1315 {
1316 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1317 	struct fbcon_ops *ops = info->fbcon_par;
1318 
1319 	struct fbcon_display *p = &fb_display[vc->vc_num];
1320 	u_int y_break;
1321 
1322 	if (fbcon_is_inactive(vc, info))
1323 		return;
1324 
1325 	if (!height || !width)
1326 		return;
1327 
1328 	if (sy < vc->vc_top && vc->vc_top == logo_lines) {
1329 		vc->vc_top = 0;
1330 		/*
1331 		 * If the font dimensions are not an integral of the display
1332 		 * dimensions then the ops->clear below won't end up clearing
1333 		 * the margins.  Call clear_margins here in case the logo
1334 		 * bitmap stretched into the margin area.
1335 		 */
1336 		fbcon_clear_margins(vc, 0);
1337 	}
1338 
1339 	/* Split blits that cross physical y_wrap boundary */
1340 
1341 	y_break = p->vrows - p->yscroll;
1342 	if (sy < y_break && sy + height - 1 >= y_break) {
1343 		u_int b = y_break - sy;
1344 		ops->clear(vc, info, real_y(p, sy), sx, b, width);
1345 		ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1346 				 width);
1347 	} else
1348 		ops->clear(vc, info, real_y(p, sy), sx, height, width);
1349 }
1350 
1351 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1352 			int count, int ypos, int xpos)
1353 {
1354 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1355 	struct fbcon_display *p = &fb_display[vc->vc_num];
1356 	struct fbcon_ops *ops = info->fbcon_par;
1357 
1358 	if (!fbcon_is_inactive(vc, info))
1359 		ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1360 			   get_color(vc, info, scr_readw(s), 1),
1361 			   get_color(vc, info, scr_readw(s), 0));
1362 }
1363 
1364 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1365 {
1366 	unsigned short chr;
1367 
1368 	scr_writew(c, &chr);
1369 	fbcon_putcs(vc, &chr, 1, ypos, xpos);
1370 }
1371 
1372 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1373 {
1374 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1375 	struct fbcon_ops *ops = info->fbcon_par;
1376 
1377 	if (!fbcon_is_inactive(vc, info))
1378 		ops->clear_margins(vc, info, margin_color, bottom_only);
1379 }
1380 
1381 static void fbcon_cursor(struct vc_data *vc, int mode)
1382 {
1383 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1384 	struct fbcon_ops *ops = info->fbcon_par;
1385 	int y;
1386  	int c = scr_readw((u16 *) vc->vc_pos);
1387 
1388 	ops->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms);
1389 
1390 	if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1391 		return;
1392 
1393 	if (vc->vc_cursor_type & 0x10)
1394 		fbcon_del_cursor_timer(info);
1395 	else
1396 		fbcon_add_cursor_timer(info);
1397 
1398 	ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1399 	if (mode & CM_SOFTBACK) {
1400 		mode &= ~CM_SOFTBACK;
1401 		y = softback_lines;
1402 	} else {
1403 		if (softback_lines)
1404 			fbcon_set_origin(vc);
1405 		y = 0;
1406 	}
1407 
1408 	ops->cursor(vc, info, mode, y, get_color(vc, info, c, 1),
1409 		    get_color(vc, info, c, 0));
1410 }
1411 
1412 static int scrollback_phys_max = 0;
1413 static int scrollback_max = 0;
1414 static int scrollback_current = 0;
1415 
1416 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1417 			   int unit)
1418 {
1419 	struct fbcon_display *p, *t;
1420 	struct vc_data **default_mode, *vc;
1421 	struct vc_data *svc;
1422 	struct fbcon_ops *ops = info->fbcon_par;
1423 	int rows, cols, charcnt = 256;
1424 
1425 	p = &fb_display[unit];
1426 
1427 	if (var_to_display(p, var, info))
1428 		return;
1429 
1430 	vc = vc_cons[unit].d;
1431 
1432 	if (!vc)
1433 		return;
1434 
1435 	default_mode = vc->vc_display_fg;
1436 	svc = *default_mode;
1437 	t = &fb_display[svc->vc_num];
1438 
1439 	if (!vc->vc_font.data) {
1440 		vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1441 		vc->vc_font.width = (*default_mode)->vc_font.width;
1442 		vc->vc_font.height = (*default_mode)->vc_font.height;
1443 		p->userfont = t->userfont;
1444 		if (p->userfont)
1445 			REFCOUNT(p->fontdata)++;
1446 	}
1447 	if (p->userfont)
1448 		charcnt = FNTCHARCNT(p->fontdata);
1449 
1450 	var->activate = FB_ACTIVATE_NOW;
1451 	info->var.activate = var->activate;
1452 	var->yoffset = info->var.yoffset;
1453 	var->xoffset = info->var.xoffset;
1454 	fb_set_var(info, var);
1455 	ops->var = info->var;
1456 	vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1457 	vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1458 	if (charcnt == 256) {
1459 		vc->vc_hi_font_mask = 0;
1460 	} else {
1461 		vc->vc_hi_font_mask = 0x100;
1462 		if (vc->vc_can_do_color)
1463 			vc->vc_complement_mask <<= 1;
1464 	}
1465 
1466 	if (!*svc->vc_uni_pagedir_loc)
1467 		con_set_default_unimap(svc);
1468 	if (!*vc->vc_uni_pagedir_loc)
1469 		con_copy_unimap(vc, svc);
1470 
1471 	cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1472 	rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1473 	cols /= vc->vc_font.width;
1474 	rows /= vc->vc_font.height;
1475 	vc_resize(vc, cols, rows);
1476 
1477 	if (con_is_visible(vc)) {
1478 		update_screen(vc);
1479 		if (softback_buf)
1480 			fbcon_update_softback(vc);
1481 	}
1482 }
1483 
1484 static __inline__ void ywrap_up(struct vc_data *vc, int count)
1485 {
1486 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1487 	struct fbcon_ops *ops = info->fbcon_par;
1488 	struct fbcon_display *p = &fb_display[vc->vc_num];
1489 
1490 	p->yscroll += count;
1491 	if (p->yscroll >= p->vrows)	/* Deal with wrap */
1492 		p->yscroll -= p->vrows;
1493 	ops->var.xoffset = 0;
1494 	ops->var.yoffset = p->yscroll * vc->vc_font.height;
1495 	ops->var.vmode |= FB_VMODE_YWRAP;
1496 	ops->update_start(info);
1497 	scrollback_max += count;
1498 	if (scrollback_max > scrollback_phys_max)
1499 		scrollback_max = scrollback_phys_max;
1500 	scrollback_current = 0;
1501 }
1502 
1503 static __inline__ void ywrap_down(struct vc_data *vc, int count)
1504 {
1505 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1506 	struct fbcon_ops *ops = info->fbcon_par;
1507 	struct fbcon_display *p = &fb_display[vc->vc_num];
1508 
1509 	p->yscroll -= count;
1510 	if (p->yscroll < 0)	/* Deal with wrap */
1511 		p->yscroll += p->vrows;
1512 	ops->var.xoffset = 0;
1513 	ops->var.yoffset = p->yscroll * vc->vc_font.height;
1514 	ops->var.vmode |= FB_VMODE_YWRAP;
1515 	ops->update_start(info);
1516 	scrollback_max -= count;
1517 	if (scrollback_max < 0)
1518 		scrollback_max = 0;
1519 	scrollback_current = 0;
1520 }
1521 
1522 static __inline__ void ypan_up(struct vc_data *vc, int count)
1523 {
1524 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1525 	struct fbcon_display *p = &fb_display[vc->vc_num];
1526 	struct fbcon_ops *ops = info->fbcon_par;
1527 
1528 	p->yscroll += count;
1529 	if (p->yscroll > p->vrows - vc->vc_rows) {
1530 		ops->bmove(vc, info, p->vrows - vc->vc_rows,
1531 			    0, 0, 0, vc->vc_rows, vc->vc_cols);
1532 		p->yscroll -= p->vrows - vc->vc_rows;
1533 	}
1534 
1535 	ops->var.xoffset = 0;
1536 	ops->var.yoffset = p->yscroll * vc->vc_font.height;
1537 	ops->var.vmode &= ~FB_VMODE_YWRAP;
1538 	ops->update_start(info);
1539 	fbcon_clear_margins(vc, 1);
1540 	scrollback_max += count;
1541 	if (scrollback_max > scrollback_phys_max)
1542 		scrollback_max = scrollback_phys_max;
1543 	scrollback_current = 0;
1544 }
1545 
1546 static __inline__ void ypan_up_redraw(struct vc_data *vc, int t, int count)
1547 {
1548 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1549 	struct fbcon_ops *ops = info->fbcon_par;
1550 	struct fbcon_display *p = &fb_display[vc->vc_num];
1551 
1552 	p->yscroll += count;
1553 
1554 	if (p->yscroll > p->vrows - vc->vc_rows) {
1555 		p->yscroll -= p->vrows - vc->vc_rows;
1556 		fbcon_redraw_move(vc, p, t + count, vc->vc_rows - count, t);
1557 	}
1558 
1559 	ops->var.xoffset = 0;
1560 	ops->var.yoffset = p->yscroll * vc->vc_font.height;
1561 	ops->var.vmode &= ~FB_VMODE_YWRAP;
1562 	ops->update_start(info);
1563 	fbcon_clear_margins(vc, 1);
1564 	scrollback_max += count;
1565 	if (scrollback_max > scrollback_phys_max)
1566 		scrollback_max = scrollback_phys_max;
1567 	scrollback_current = 0;
1568 }
1569 
1570 static __inline__ void ypan_down(struct vc_data *vc, int count)
1571 {
1572 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1573 	struct fbcon_display *p = &fb_display[vc->vc_num];
1574 	struct fbcon_ops *ops = info->fbcon_par;
1575 
1576 	p->yscroll -= count;
1577 	if (p->yscroll < 0) {
1578 		ops->bmove(vc, info, 0, 0, p->vrows - vc->vc_rows,
1579 			    0, vc->vc_rows, vc->vc_cols);
1580 		p->yscroll += p->vrows - vc->vc_rows;
1581 	}
1582 
1583 	ops->var.xoffset = 0;
1584 	ops->var.yoffset = p->yscroll * vc->vc_font.height;
1585 	ops->var.vmode &= ~FB_VMODE_YWRAP;
1586 	ops->update_start(info);
1587 	fbcon_clear_margins(vc, 1);
1588 	scrollback_max -= count;
1589 	if (scrollback_max < 0)
1590 		scrollback_max = 0;
1591 	scrollback_current = 0;
1592 }
1593 
1594 static __inline__ void ypan_down_redraw(struct vc_data *vc, int t, int count)
1595 {
1596 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1597 	struct fbcon_ops *ops = info->fbcon_par;
1598 	struct fbcon_display *p = &fb_display[vc->vc_num];
1599 
1600 	p->yscroll -= count;
1601 
1602 	if (p->yscroll < 0) {
1603 		p->yscroll += p->vrows - vc->vc_rows;
1604 		fbcon_redraw_move(vc, p, t, vc->vc_rows - count, t + count);
1605 	}
1606 
1607 	ops->var.xoffset = 0;
1608 	ops->var.yoffset = p->yscroll * vc->vc_font.height;
1609 	ops->var.vmode &= ~FB_VMODE_YWRAP;
1610 	ops->update_start(info);
1611 	fbcon_clear_margins(vc, 1);
1612 	scrollback_max -= count;
1613 	if (scrollback_max < 0)
1614 		scrollback_max = 0;
1615 	scrollback_current = 0;
1616 }
1617 
1618 static void fbcon_redraw_softback(struct vc_data *vc, struct fbcon_display *p,
1619 				  long delta)
1620 {
1621 	int count = vc->vc_rows;
1622 	unsigned short *d, *s;
1623 	unsigned long n;
1624 	int line = 0;
1625 
1626 	d = (u16 *) softback_curr;
1627 	if (d == (u16 *) softback_in)
1628 		d = (u16 *) vc->vc_origin;
1629 	n = softback_curr + delta * vc->vc_size_row;
1630 	softback_lines -= delta;
1631 	if (delta < 0) {
1632 		if (softback_curr < softback_top && n < softback_buf) {
1633 			n += softback_end - softback_buf;
1634 			if (n < softback_top) {
1635 				softback_lines -=
1636 				    (softback_top - n) / vc->vc_size_row;
1637 				n = softback_top;
1638 			}
1639 		} else if (softback_curr >= softback_top
1640 			   && n < softback_top) {
1641 			softback_lines -=
1642 			    (softback_top - n) / vc->vc_size_row;
1643 			n = softback_top;
1644 		}
1645 	} else {
1646 		if (softback_curr > softback_in && n >= softback_end) {
1647 			n += softback_buf - softback_end;
1648 			if (n > softback_in) {
1649 				n = softback_in;
1650 				softback_lines = 0;
1651 			}
1652 		} else if (softback_curr <= softback_in && n > softback_in) {
1653 			n = softback_in;
1654 			softback_lines = 0;
1655 		}
1656 	}
1657 	if (n == softback_curr)
1658 		return;
1659 	softback_curr = n;
1660 	s = (u16 *) softback_curr;
1661 	if (s == (u16 *) softback_in)
1662 		s = (u16 *) vc->vc_origin;
1663 	while (count--) {
1664 		unsigned short *start;
1665 		unsigned short *le;
1666 		unsigned short c;
1667 		int x = 0;
1668 		unsigned short attr = 1;
1669 
1670 		start = s;
1671 		le = advance_row(s, 1);
1672 		do {
1673 			c = scr_readw(s);
1674 			if (attr != (c & 0xff00)) {
1675 				attr = c & 0xff00;
1676 				if (s > start) {
1677 					fbcon_putcs(vc, start, s - start,
1678 						    line, x);
1679 					x += s - start;
1680 					start = s;
1681 				}
1682 			}
1683 			if (c == scr_readw(d)) {
1684 				if (s > start) {
1685 					fbcon_putcs(vc, start, s - start,
1686 						    line, x);
1687 					x += s - start + 1;
1688 					start = s + 1;
1689 				} else {
1690 					x++;
1691 					start++;
1692 				}
1693 			}
1694 			s++;
1695 			d++;
1696 		} while (s < le);
1697 		if (s > start)
1698 			fbcon_putcs(vc, start, s - start, line, x);
1699 		line++;
1700 		if (d == (u16 *) softback_end)
1701 			d = (u16 *) softback_buf;
1702 		if (d == (u16 *) softback_in)
1703 			d = (u16 *) vc->vc_origin;
1704 		if (s == (u16 *) softback_end)
1705 			s = (u16 *) softback_buf;
1706 		if (s == (u16 *) softback_in)
1707 			s = (u16 *) vc->vc_origin;
1708 	}
1709 }
1710 
1711 static void fbcon_redraw_move(struct vc_data *vc, struct fbcon_display *p,
1712 			      int line, int count, int dy)
1713 {
1714 	unsigned short *s = (unsigned short *)
1715 		(vc->vc_origin + vc->vc_size_row * line);
1716 
1717 	while (count--) {
1718 		unsigned short *start = s;
1719 		unsigned short *le = advance_row(s, 1);
1720 		unsigned short c;
1721 		int x = 0;
1722 		unsigned short attr = 1;
1723 
1724 		do {
1725 			c = scr_readw(s);
1726 			if (attr != (c & 0xff00)) {
1727 				attr = c & 0xff00;
1728 				if (s > start) {
1729 					fbcon_putcs(vc, start, s - start,
1730 						    dy, x);
1731 					x += s - start;
1732 					start = s;
1733 				}
1734 			}
1735 			console_conditional_schedule();
1736 			s++;
1737 		} while (s < le);
1738 		if (s > start)
1739 			fbcon_putcs(vc, start, s - start, dy, x);
1740 		console_conditional_schedule();
1741 		dy++;
1742 	}
1743 }
1744 
1745 static void fbcon_redraw_blit(struct vc_data *vc, struct fb_info *info,
1746 			struct fbcon_display *p, int line, int count, int ycount)
1747 {
1748 	int offset = ycount * vc->vc_cols;
1749 	unsigned short *d = (unsigned short *)
1750 	    (vc->vc_origin + vc->vc_size_row * line);
1751 	unsigned short *s = d + offset;
1752 	struct fbcon_ops *ops = info->fbcon_par;
1753 
1754 	while (count--) {
1755 		unsigned short *start = s;
1756 		unsigned short *le = advance_row(s, 1);
1757 		unsigned short c;
1758 		int x = 0;
1759 
1760 		do {
1761 			c = scr_readw(s);
1762 
1763 			if (c == scr_readw(d)) {
1764 				if (s > start) {
1765 					ops->bmove(vc, info, line + ycount, x,
1766 						   line, x, 1, s-start);
1767 					x += s - start + 1;
1768 					start = s + 1;
1769 				} else {
1770 					x++;
1771 					start++;
1772 				}
1773 			}
1774 
1775 			scr_writew(c, d);
1776 			console_conditional_schedule();
1777 			s++;
1778 			d++;
1779 		} while (s < le);
1780 		if (s > start)
1781 			ops->bmove(vc, info, line + ycount, x, line, x, 1,
1782 				   s-start);
1783 		console_conditional_schedule();
1784 		if (ycount > 0)
1785 			line++;
1786 		else {
1787 			line--;
1788 			/* NOTE: We subtract two lines from these pointers */
1789 			s -= vc->vc_size_row;
1790 			d -= vc->vc_size_row;
1791 		}
1792 	}
1793 }
1794 
1795 static void fbcon_redraw(struct vc_data *vc, struct fbcon_display *p,
1796 			 int line, int count, int offset)
1797 {
1798 	unsigned short *d = (unsigned short *)
1799 	    (vc->vc_origin + vc->vc_size_row * line);
1800 	unsigned short *s = d + offset;
1801 
1802 	while (count--) {
1803 		unsigned short *start = s;
1804 		unsigned short *le = advance_row(s, 1);
1805 		unsigned short c;
1806 		int x = 0;
1807 		unsigned short attr = 1;
1808 
1809 		do {
1810 			c = scr_readw(s);
1811 			if (attr != (c & 0xff00)) {
1812 				attr = c & 0xff00;
1813 				if (s > start) {
1814 					fbcon_putcs(vc, start, s - start,
1815 						    line, x);
1816 					x += s - start;
1817 					start = s;
1818 				}
1819 			}
1820 			if (c == scr_readw(d)) {
1821 				if (s > start) {
1822 					fbcon_putcs(vc, start, s - start,
1823 						     line, x);
1824 					x += s - start + 1;
1825 					start = s + 1;
1826 				} else {
1827 					x++;
1828 					start++;
1829 				}
1830 			}
1831 			scr_writew(c, d);
1832 			console_conditional_schedule();
1833 			s++;
1834 			d++;
1835 		} while (s < le);
1836 		if (s > start)
1837 			fbcon_putcs(vc, start, s - start, line, x);
1838 		console_conditional_schedule();
1839 		if (offset > 0)
1840 			line++;
1841 		else {
1842 			line--;
1843 			/* NOTE: We subtract two lines from these pointers */
1844 			s -= vc->vc_size_row;
1845 			d -= vc->vc_size_row;
1846 		}
1847 	}
1848 }
1849 
1850 static inline void fbcon_softback_note(struct vc_data *vc, int t,
1851 				       int count)
1852 {
1853 	unsigned short *p;
1854 
1855 	if (vc->vc_num != fg_console)
1856 		return;
1857 	p = (unsigned short *) (vc->vc_origin + t * vc->vc_size_row);
1858 
1859 	while (count) {
1860 		scr_memcpyw((u16 *) softback_in, p, vc->vc_size_row);
1861 		count--;
1862 		p = advance_row(p, 1);
1863 		softback_in += vc->vc_size_row;
1864 		if (softback_in == softback_end)
1865 			softback_in = softback_buf;
1866 		if (softback_in == softback_top) {
1867 			softback_top += vc->vc_size_row;
1868 			if (softback_top == softback_end)
1869 				softback_top = softback_buf;
1870 		}
1871 	}
1872 	softback_curr = softback_in;
1873 }
1874 
1875 static bool fbcon_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
1876 		enum con_scroll dir, unsigned int count)
1877 {
1878 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1879 	struct fbcon_display *p = &fb_display[vc->vc_num];
1880 	int scroll_partial = info->flags & FBINFO_PARTIAL_PAN_OK;
1881 
1882 	if (fbcon_is_inactive(vc, info))
1883 		return true;
1884 
1885 	fbcon_cursor(vc, CM_ERASE);
1886 
1887 	/*
1888 	 * ++Geert: Only use ywrap/ypan if the console is in text mode
1889 	 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1890 	 *           whole screen (prevents flicker).
1891 	 */
1892 
1893 	switch (dir) {
1894 	case SM_UP:
1895 		if (count > vc->vc_rows)	/* Maximum realistic size */
1896 			count = vc->vc_rows;
1897 		if (softback_top)
1898 			fbcon_softback_note(vc, t, count);
1899 		if (logo_shown >= 0)
1900 			goto redraw_up;
1901 		switch (p->scrollmode) {
1902 		case SCROLL_MOVE:
1903 			fbcon_redraw_blit(vc, info, p, t, b - t - count,
1904 				     count);
1905 			fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1906 			scr_memsetw((unsigned short *) (vc->vc_origin +
1907 							vc->vc_size_row *
1908 							(b - count)),
1909 				    vc->vc_video_erase_char,
1910 				    vc->vc_size_row * count);
1911 			return true;
1912 			break;
1913 
1914 		case SCROLL_WRAP_MOVE:
1915 			if (b - t - count > 3 * vc->vc_rows >> 2) {
1916 				if (t > 0)
1917 					fbcon_bmove(vc, 0, 0, count, 0, t,
1918 						    vc->vc_cols);
1919 				ywrap_up(vc, count);
1920 				if (vc->vc_rows - b > 0)
1921 					fbcon_bmove(vc, b - count, 0, b, 0,
1922 						    vc->vc_rows - b,
1923 						    vc->vc_cols);
1924 			} else if (info->flags & FBINFO_READS_FAST)
1925 				fbcon_bmove(vc, t + count, 0, t, 0,
1926 					    b - t - count, vc->vc_cols);
1927 			else
1928 				goto redraw_up;
1929 			fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1930 			break;
1931 
1932 		case SCROLL_PAN_REDRAW:
1933 			if ((p->yscroll + count <=
1934 			     2 * (p->vrows - vc->vc_rows))
1935 			    && ((!scroll_partial && (b - t == vc->vc_rows))
1936 				|| (scroll_partial
1937 				    && (b - t - count >
1938 					3 * vc->vc_rows >> 2)))) {
1939 				if (t > 0)
1940 					fbcon_redraw_move(vc, p, 0, t, count);
1941 				ypan_up_redraw(vc, t, count);
1942 				if (vc->vc_rows - b > 0)
1943 					fbcon_redraw_move(vc, p, b,
1944 							  vc->vc_rows - b, b);
1945 			} else
1946 				fbcon_redraw_move(vc, p, t + count, b - t - count, t);
1947 			fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1948 			break;
1949 
1950 		case SCROLL_PAN_MOVE:
1951 			if ((p->yscroll + count <=
1952 			     2 * (p->vrows - vc->vc_rows))
1953 			    && ((!scroll_partial && (b - t == vc->vc_rows))
1954 				|| (scroll_partial
1955 				    && (b - t - count >
1956 					3 * vc->vc_rows >> 2)))) {
1957 				if (t > 0)
1958 					fbcon_bmove(vc, 0, 0, count, 0, t,
1959 						    vc->vc_cols);
1960 				ypan_up(vc, count);
1961 				if (vc->vc_rows - b > 0)
1962 					fbcon_bmove(vc, b - count, 0, b, 0,
1963 						    vc->vc_rows - b,
1964 						    vc->vc_cols);
1965 			} else if (info->flags & FBINFO_READS_FAST)
1966 				fbcon_bmove(vc, t + count, 0, t, 0,
1967 					    b - t - count, vc->vc_cols);
1968 			else
1969 				goto redraw_up;
1970 			fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1971 			break;
1972 
1973 		case SCROLL_REDRAW:
1974 		      redraw_up:
1975 			fbcon_redraw(vc, p, t, b - t - count,
1976 				     count * vc->vc_cols);
1977 			fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1978 			scr_memsetw((unsigned short *) (vc->vc_origin +
1979 							vc->vc_size_row *
1980 							(b - count)),
1981 				    vc->vc_video_erase_char,
1982 				    vc->vc_size_row * count);
1983 			return true;
1984 		}
1985 		break;
1986 
1987 	case SM_DOWN:
1988 		if (count > vc->vc_rows)	/* Maximum realistic size */
1989 			count = vc->vc_rows;
1990 		if (logo_shown >= 0)
1991 			goto redraw_down;
1992 		switch (p->scrollmode) {
1993 		case SCROLL_MOVE:
1994 			fbcon_redraw_blit(vc, info, p, b - 1, b - t - count,
1995 				     -count);
1996 			fbcon_clear(vc, t, 0, count, vc->vc_cols);
1997 			scr_memsetw((unsigned short *) (vc->vc_origin +
1998 							vc->vc_size_row *
1999 							t),
2000 				    vc->vc_video_erase_char,
2001 				    vc->vc_size_row * count);
2002 			return true;
2003 			break;
2004 
2005 		case SCROLL_WRAP_MOVE:
2006 			if (b - t - count > 3 * vc->vc_rows >> 2) {
2007 				if (vc->vc_rows - b > 0)
2008 					fbcon_bmove(vc, b, 0, b - count, 0,
2009 						    vc->vc_rows - b,
2010 						    vc->vc_cols);
2011 				ywrap_down(vc, count);
2012 				if (t > 0)
2013 					fbcon_bmove(vc, count, 0, 0, 0, t,
2014 						    vc->vc_cols);
2015 			} else if (info->flags & FBINFO_READS_FAST)
2016 				fbcon_bmove(vc, t, 0, t + count, 0,
2017 					    b - t - count, vc->vc_cols);
2018 			else
2019 				goto redraw_down;
2020 			fbcon_clear(vc, t, 0, count, vc->vc_cols);
2021 			break;
2022 
2023 		case SCROLL_PAN_MOVE:
2024 			if ((count - p->yscroll <= p->vrows - vc->vc_rows)
2025 			    && ((!scroll_partial && (b - t == vc->vc_rows))
2026 				|| (scroll_partial
2027 				    && (b - t - count >
2028 					3 * vc->vc_rows >> 2)))) {
2029 				if (vc->vc_rows - b > 0)
2030 					fbcon_bmove(vc, b, 0, b - count, 0,
2031 						    vc->vc_rows - b,
2032 						    vc->vc_cols);
2033 				ypan_down(vc, count);
2034 				if (t > 0)
2035 					fbcon_bmove(vc, count, 0, 0, 0, t,
2036 						    vc->vc_cols);
2037 			} else if (info->flags & FBINFO_READS_FAST)
2038 				fbcon_bmove(vc, t, 0, t + count, 0,
2039 					    b - t - count, vc->vc_cols);
2040 			else
2041 				goto redraw_down;
2042 			fbcon_clear(vc, t, 0, count, vc->vc_cols);
2043 			break;
2044 
2045 		case SCROLL_PAN_REDRAW:
2046 			if ((count - p->yscroll <= p->vrows - vc->vc_rows)
2047 			    && ((!scroll_partial && (b - t == vc->vc_rows))
2048 				|| (scroll_partial
2049 				    && (b - t - count >
2050 					3 * vc->vc_rows >> 2)))) {
2051 				if (vc->vc_rows - b > 0)
2052 					fbcon_redraw_move(vc, p, b, vc->vc_rows - b,
2053 							  b - count);
2054 				ypan_down_redraw(vc, t, count);
2055 				if (t > 0)
2056 					fbcon_redraw_move(vc, p, count, t, 0);
2057 			} else
2058 				fbcon_redraw_move(vc, p, t, b - t - count, t + count);
2059 			fbcon_clear(vc, t, 0, count, vc->vc_cols);
2060 			break;
2061 
2062 		case SCROLL_REDRAW:
2063 		      redraw_down:
2064 			fbcon_redraw(vc, p, b - 1, b - t - count,
2065 				     -count * vc->vc_cols);
2066 			fbcon_clear(vc, t, 0, count, vc->vc_cols);
2067 			scr_memsetw((unsigned short *) (vc->vc_origin +
2068 							vc->vc_size_row *
2069 							t),
2070 				    vc->vc_video_erase_char,
2071 				    vc->vc_size_row * count);
2072 			return true;
2073 		}
2074 	}
2075 	return false;
2076 }
2077 
2078 
2079 static void fbcon_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx,
2080 			int height, int width)
2081 {
2082 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2083 	struct fbcon_display *p = &fb_display[vc->vc_num];
2084 
2085 	if (fbcon_is_inactive(vc, info))
2086 		return;
2087 
2088 	if (!width || !height)
2089 		return;
2090 
2091 	/*  Split blits that cross physical y_wrap case.
2092 	 *  Pathological case involves 4 blits, better to use recursive
2093 	 *  code rather than unrolled case
2094 	 *
2095 	 *  Recursive invocations don't need to erase the cursor over and
2096 	 *  over again, so we use fbcon_bmove_rec()
2097 	 */
2098 	fbcon_bmove_rec(vc, p, sy, sx, dy, dx, height, width,
2099 			p->vrows - p->yscroll);
2100 }
2101 
2102 static void fbcon_bmove_rec(struct vc_data *vc, struct fbcon_display *p, int sy, int sx,
2103 			    int dy, int dx, int height, int width, u_int y_break)
2104 {
2105 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2106 	struct fbcon_ops *ops = info->fbcon_par;
2107 	u_int b;
2108 
2109 	if (sy < y_break && sy + height > y_break) {
2110 		b = y_break - sy;
2111 		if (dy < sy) {	/* Avoid trashing self */
2112 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2113 					y_break);
2114 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2115 					height - b, width, y_break);
2116 		} else {
2117 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2118 					height - b, width, y_break);
2119 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2120 					y_break);
2121 		}
2122 		return;
2123 	}
2124 
2125 	if (dy < y_break && dy + height > y_break) {
2126 		b = y_break - dy;
2127 		if (dy < sy) {	/* Avoid trashing self */
2128 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2129 					y_break);
2130 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2131 					height - b, width, y_break);
2132 		} else {
2133 			fbcon_bmove_rec(vc, p, sy + b, sx, dy + b, dx,
2134 					height - b, width, y_break);
2135 			fbcon_bmove_rec(vc, p, sy, sx, dy, dx, b, width,
2136 					y_break);
2137 		}
2138 		return;
2139 	}
2140 	ops->bmove(vc, info, real_y(p, sy), sx, real_y(p, dy), dx,
2141 		   height, width);
2142 }
2143 
2144 static void updatescrollmode(struct fbcon_display *p,
2145 					struct fb_info *info,
2146 					struct vc_data *vc)
2147 {
2148 	struct fbcon_ops *ops = info->fbcon_par;
2149 	int fh = vc->vc_font.height;
2150 	int cap = info->flags;
2151 	u16 t = 0;
2152 	int ypan = FBCON_SWAP(ops->rotate, info->fix.ypanstep,
2153 				  info->fix.xpanstep);
2154 	int ywrap = FBCON_SWAP(ops->rotate, info->fix.ywrapstep, t);
2155 	int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2156 	int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
2157 				   info->var.xres_virtual);
2158 	int good_pan = (cap & FBINFO_HWACCEL_YPAN) &&
2159 		divides(ypan, vc->vc_font.height) && vyres > yres;
2160 	int good_wrap = (cap & FBINFO_HWACCEL_YWRAP) &&
2161 		divides(ywrap, vc->vc_font.height) &&
2162 		divides(vc->vc_font.height, vyres) &&
2163 		divides(vc->vc_font.height, yres);
2164 	int reading_fast = cap & FBINFO_READS_FAST;
2165 	int fast_copyarea = (cap & FBINFO_HWACCEL_COPYAREA) &&
2166 		!(cap & FBINFO_HWACCEL_DISABLED);
2167 	int fast_imageblit = (cap & FBINFO_HWACCEL_IMAGEBLIT) &&
2168 		!(cap & FBINFO_HWACCEL_DISABLED);
2169 
2170 	p->vrows = vyres/fh;
2171 	if (yres > (fh * (vc->vc_rows + 1)))
2172 		p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
2173 	if ((yres % fh) && (vyres % fh < yres % fh))
2174 		p->vrows--;
2175 
2176 	if (good_wrap || good_pan) {
2177 		if (reading_fast || fast_copyarea)
2178 			p->scrollmode = good_wrap ?
2179 				SCROLL_WRAP_MOVE : SCROLL_PAN_MOVE;
2180 		else
2181 			p->scrollmode = good_wrap ? SCROLL_REDRAW :
2182 				SCROLL_PAN_REDRAW;
2183 	} else {
2184 		if (reading_fast || (fast_copyarea && !fast_imageblit))
2185 			p->scrollmode = SCROLL_MOVE;
2186 		else
2187 			p->scrollmode = SCROLL_REDRAW;
2188 	}
2189 }
2190 
2191 static int fbcon_resize(struct vc_data *vc, unsigned int width,
2192 			unsigned int height, unsigned int user)
2193 {
2194 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2195 	struct fbcon_ops *ops = info->fbcon_par;
2196 	struct fbcon_display *p = &fb_display[vc->vc_num];
2197 	struct fb_var_screeninfo var = info->var;
2198 	int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
2199 
2200 	virt_w = FBCON_SWAP(ops->rotate, width, height);
2201 	virt_h = FBCON_SWAP(ops->rotate, height, width);
2202 	virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
2203 				 vc->vc_font.height);
2204 	virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
2205 				 vc->vc_font.width);
2206 	var.xres = virt_w * virt_fw;
2207 	var.yres = virt_h * virt_fh;
2208 	x_diff = info->var.xres - var.xres;
2209 	y_diff = info->var.yres - var.yres;
2210 	if (x_diff < 0 || x_diff > virt_fw ||
2211 	    y_diff < 0 || y_diff > virt_fh) {
2212 		const struct fb_videomode *mode;
2213 
2214 		DPRINTK("attempting resize %ix%i\n", var.xres, var.yres);
2215 		mode = fb_find_best_mode(&var, &info->modelist);
2216 		if (mode == NULL)
2217 			return -EINVAL;
2218 		display_to_var(&var, p);
2219 		fb_videomode_to_var(&var, mode);
2220 
2221 		if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
2222 			return -EINVAL;
2223 
2224 		DPRINTK("resize now %ix%i\n", var.xres, var.yres);
2225 		if (con_is_visible(vc)) {
2226 			var.activate = FB_ACTIVATE_NOW |
2227 				FB_ACTIVATE_FORCE;
2228 			fb_set_var(info, &var);
2229 		}
2230 		var_to_display(p, &info->var, info);
2231 		ops->var = info->var;
2232 	}
2233 	updatescrollmode(p, info, vc);
2234 	return 0;
2235 }
2236 
2237 static int fbcon_switch(struct vc_data *vc)
2238 {
2239 	struct fb_info *info, *old_info = NULL;
2240 	struct fbcon_ops *ops;
2241 	struct fbcon_display *p = &fb_display[vc->vc_num];
2242 	struct fb_var_screeninfo var;
2243 	int i, ret, prev_console, charcnt = 256;
2244 
2245 	info = registered_fb[con2fb_map[vc->vc_num]];
2246 	ops = info->fbcon_par;
2247 
2248 	if (softback_top) {
2249 		if (softback_lines)
2250 			fbcon_set_origin(vc);
2251 		softback_top = softback_curr = softback_in = softback_buf;
2252 		softback_lines = 0;
2253 		fbcon_update_softback(vc);
2254 	}
2255 
2256 	if (logo_shown >= 0) {
2257 		struct vc_data *conp2 = vc_cons[logo_shown].d;
2258 
2259 		if (conp2->vc_top == logo_lines
2260 		    && conp2->vc_bottom == conp2->vc_rows)
2261 			conp2->vc_top = 0;
2262 		logo_shown = FBCON_LOGO_CANSHOW;
2263 	}
2264 
2265 	prev_console = ops->currcon;
2266 	if (prev_console != -1)
2267 		old_info = registered_fb[con2fb_map[prev_console]];
2268 	/*
2269 	 * FIXME: If we have multiple fbdev's loaded, we need to
2270 	 * update all info->currcon.  Perhaps, we can place this
2271 	 * in a centralized structure, but this might break some
2272 	 * drivers.
2273 	 *
2274 	 * info->currcon = vc->vc_num;
2275 	 */
2276 	for_each_registered_fb(i) {
2277 		if (registered_fb[i]->fbcon_par) {
2278 			struct fbcon_ops *o = registered_fb[i]->fbcon_par;
2279 
2280 			o->currcon = vc->vc_num;
2281 		}
2282 	}
2283 	memset(&var, 0, sizeof(struct fb_var_screeninfo));
2284 	display_to_var(&var, p);
2285 	var.activate = FB_ACTIVATE_NOW;
2286 
2287 	/*
2288 	 * make sure we don't unnecessarily trip the memcmp()
2289 	 * in fb_set_var()
2290 	 */
2291 	info->var.activate = var.activate;
2292 	var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
2293 	fb_set_var(info, &var);
2294 	ops->var = info->var;
2295 
2296 	if (old_info != NULL && (old_info != info ||
2297 				 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
2298 		if (info->fbops->fb_set_par) {
2299 			ret = info->fbops->fb_set_par(info);
2300 
2301 			if (ret)
2302 				printk(KERN_ERR "fbcon_switch: detected "
2303 					"unhandled fb_set_par error, "
2304 					"error code %d\n", ret);
2305 		}
2306 
2307 		if (old_info != info)
2308 			fbcon_del_cursor_timer(old_info);
2309 	}
2310 
2311 	if (fbcon_is_inactive(vc, info) ||
2312 	    ops->blank_state != FB_BLANK_UNBLANK)
2313 		fbcon_del_cursor_timer(info);
2314 	else
2315 		fbcon_add_cursor_timer(info);
2316 
2317 	set_blitting_type(vc, info);
2318 	ops->cursor_reset = 1;
2319 
2320 	if (ops->rotate_font && ops->rotate_font(info, vc)) {
2321 		ops->rotate = FB_ROTATE_UR;
2322 		set_blitting_type(vc, info);
2323 	}
2324 
2325 	vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
2326 	vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
2327 
2328 	if (p->userfont)
2329 		charcnt = FNTCHARCNT(vc->vc_font.data);
2330 
2331 	if (charcnt > 256)
2332 		vc->vc_complement_mask <<= 1;
2333 
2334 	updatescrollmode(p, info, vc);
2335 
2336 	switch (p->scrollmode) {
2337 	case SCROLL_WRAP_MOVE:
2338 		scrollback_phys_max = p->vrows - vc->vc_rows;
2339 		break;
2340 	case SCROLL_PAN_MOVE:
2341 	case SCROLL_PAN_REDRAW:
2342 		scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
2343 		if (scrollback_phys_max < 0)
2344 			scrollback_phys_max = 0;
2345 		break;
2346 	default:
2347 		scrollback_phys_max = 0;
2348 		break;
2349 	}
2350 
2351 	scrollback_max = 0;
2352 	scrollback_current = 0;
2353 
2354 	if (!fbcon_is_inactive(vc, info)) {
2355 	    ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2356 	    ops->update_start(info);
2357 	}
2358 
2359 	fbcon_set_palette(vc, color_table);
2360 	fbcon_clear_margins(vc, 0);
2361 
2362 	if (logo_shown == FBCON_LOGO_DRAW) {
2363 
2364 		logo_shown = fg_console;
2365 		/* This is protected above by initmem_freed */
2366 		fb_show_logo(info, ops->rotate);
2367 		update_region(vc,
2368 			      vc->vc_origin + vc->vc_size_row * vc->vc_top,
2369 			      vc->vc_size_row * (vc->vc_bottom -
2370 						 vc->vc_top) / 2);
2371 		return 0;
2372 	}
2373 	return 1;
2374 }
2375 
2376 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
2377 				int blank)
2378 {
2379 	if (blank) {
2380 		unsigned short charmask = vc->vc_hi_font_mask ?
2381 			0x1ff : 0xff;
2382 		unsigned short oldc;
2383 
2384 		oldc = vc->vc_video_erase_char;
2385 		vc->vc_video_erase_char &= charmask;
2386 		fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
2387 		vc->vc_video_erase_char = oldc;
2388 	}
2389 }
2390 
2391 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
2392 {
2393 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2394 	struct fbcon_ops *ops = info->fbcon_par;
2395 
2396 	if (mode_switch) {
2397 		struct fb_var_screeninfo var = info->var;
2398 
2399 		ops->graphics = 1;
2400 
2401 		if (!blank) {
2402 			var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
2403 			fb_set_var(info, &var);
2404 			ops->graphics = 0;
2405 			ops->var = info->var;
2406 		}
2407 	}
2408 
2409  	if (!fbcon_is_inactive(vc, info)) {
2410 		if (ops->blank_state != blank) {
2411 			ops->blank_state = blank;
2412 			fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
2413 			ops->cursor_flash = (!blank);
2414 
2415 			if (fb_blank(info, blank))
2416 				fbcon_generic_blank(vc, info, blank);
2417 		}
2418 
2419 		if (!blank)
2420 			update_screen(vc);
2421 	}
2422 
2423 	if (mode_switch || fbcon_is_inactive(vc, info) ||
2424 	    ops->blank_state != FB_BLANK_UNBLANK)
2425 		fbcon_del_cursor_timer(info);
2426 	else
2427 		fbcon_add_cursor_timer(info);
2428 
2429 	return 0;
2430 }
2431 
2432 static int fbcon_debug_enter(struct vc_data *vc)
2433 {
2434 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2435 	struct fbcon_ops *ops = info->fbcon_par;
2436 
2437 	ops->save_graphics = ops->graphics;
2438 	ops->graphics = 0;
2439 	if (info->fbops->fb_debug_enter)
2440 		info->fbops->fb_debug_enter(info);
2441 	fbcon_set_palette(vc, color_table);
2442 	return 0;
2443 }
2444 
2445 static int fbcon_debug_leave(struct vc_data *vc)
2446 {
2447 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2448 	struct fbcon_ops *ops = info->fbcon_par;
2449 
2450 	ops->graphics = ops->save_graphics;
2451 	if (info->fbops->fb_debug_leave)
2452 		info->fbops->fb_debug_leave(info);
2453 	return 0;
2454 }
2455 
2456 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
2457 {
2458 	u8 *fontdata = vc->vc_font.data;
2459 	u8 *data = font->data;
2460 	int i, j;
2461 
2462 	font->width = vc->vc_font.width;
2463 	font->height = vc->vc_font.height;
2464 	font->charcount = vc->vc_hi_font_mask ? 512 : 256;
2465 	if (!font->data)
2466 		return 0;
2467 
2468 	if (font->width <= 8) {
2469 		j = vc->vc_font.height;
2470 		for (i = 0; i < font->charcount; i++) {
2471 			memcpy(data, fontdata, j);
2472 			memset(data + j, 0, 32 - j);
2473 			data += 32;
2474 			fontdata += j;
2475 		}
2476 	} else if (font->width <= 16) {
2477 		j = vc->vc_font.height * 2;
2478 		for (i = 0; i < font->charcount; i++) {
2479 			memcpy(data, fontdata, j);
2480 			memset(data + j, 0, 64 - j);
2481 			data += 64;
2482 			fontdata += j;
2483 		}
2484 	} else if (font->width <= 24) {
2485 		for (i = 0; i < font->charcount; i++) {
2486 			for (j = 0; j < vc->vc_font.height; j++) {
2487 				*data++ = fontdata[0];
2488 				*data++ = fontdata[1];
2489 				*data++ = fontdata[2];
2490 				fontdata += sizeof(u32);
2491 			}
2492 			memset(data, 0, 3 * (32 - j));
2493 			data += 3 * (32 - j);
2494 		}
2495 	} else {
2496 		j = vc->vc_font.height * 4;
2497 		for (i = 0; i < font->charcount; i++) {
2498 			memcpy(data, fontdata, j);
2499 			memset(data + j, 0, 128 - j);
2500 			data += 128;
2501 			fontdata += j;
2502 		}
2503 	}
2504 	return 0;
2505 }
2506 
2507 /* set/clear vc_hi_font_mask and update vc attrs accordingly */
2508 static void set_vc_hi_font(struct vc_data *vc, bool set)
2509 {
2510 	if (!set) {
2511 		vc->vc_hi_font_mask = 0;
2512 		if (vc->vc_can_do_color) {
2513 			vc->vc_complement_mask >>= 1;
2514 			vc->vc_s_complement_mask >>= 1;
2515 		}
2516 
2517 		/* ++Edmund: reorder the attribute bits */
2518 		if (vc->vc_can_do_color) {
2519 			unsigned short *cp =
2520 			    (unsigned short *) vc->vc_origin;
2521 			int count = vc->vc_screenbuf_size / 2;
2522 			unsigned short c;
2523 			for (; count > 0; count--, cp++) {
2524 				c = scr_readw(cp);
2525 				scr_writew(((c & 0xfe00) >> 1) |
2526 					   (c & 0xff), cp);
2527 			}
2528 			c = vc->vc_video_erase_char;
2529 			vc->vc_video_erase_char =
2530 			    ((c & 0xfe00) >> 1) | (c & 0xff);
2531 			vc->vc_attr >>= 1;
2532 		}
2533 	} else {
2534 		vc->vc_hi_font_mask = 0x100;
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 		{
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 				unsigned short newc;
2548 				c = scr_readw(cp);
2549 				if (vc->vc_can_do_color)
2550 					newc =
2551 					    ((c & 0xff00) << 1) | (c &
2552 								   0xff);
2553 				else
2554 					newc = c & ~0x100;
2555 				scr_writew(newc, cp);
2556 			}
2557 			c = vc->vc_video_erase_char;
2558 			if (vc->vc_can_do_color) {
2559 				vc->vc_video_erase_char =
2560 				    ((c & 0xff00) << 1) | (c & 0xff);
2561 				vc->vc_attr <<= 1;
2562 			} else
2563 				vc->vc_video_erase_char = c & ~0x100;
2564 		}
2565 	}
2566 }
2567 
2568 static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2569 			     const u8 * data, int userfont)
2570 {
2571 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2572 	struct fbcon_ops *ops = info->fbcon_par;
2573 	struct fbcon_display *p = &fb_display[vc->vc_num];
2574 	int resize;
2575 	int cnt;
2576 	char *old_data = NULL;
2577 
2578 	if (con_is_visible(vc) && softback_lines)
2579 		fbcon_set_origin(vc);
2580 
2581 	resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2582 	if (p->userfont)
2583 		old_data = vc->vc_font.data;
2584 	if (userfont)
2585 		cnt = FNTCHARCNT(data);
2586 	else
2587 		cnt = 256;
2588 	vc->vc_font.data = (void *)(p->fontdata = data);
2589 	if ((p->userfont = userfont))
2590 		REFCOUNT(data)++;
2591 	vc->vc_font.width = w;
2592 	vc->vc_font.height = h;
2593 	if (vc->vc_hi_font_mask && cnt == 256)
2594 		set_vc_hi_font(vc, false);
2595 	else if (!vc->vc_hi_font_mask && cnt == 512)
2596 		set_vc_hi_font(vc, true);
2597 
2598 	if (resize) {
2599 		int cols, rows;
2600 
2601 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2602 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2603 		cols /= w;
2604 		rows /= h;
2605 		vc_resize(vc, cols, rows);
2606 		if (con_is_visible(vc) && softback_buf)
2607 			fbcon_update_softback(vc);
2608 	} else if (con_is_visible(vc)
2609 		   && vc->vc_mode == KD_TEXT) {
2610 		fbcon_clear_margins(vc, 0);
2611 		update_screen(vc);
2612 	}
2613 
2614 	if (old_data && (--REFCOUNT(old_data) == 0))
2615 		kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2616 	return 0;
2617 }
2618 
2619 static int fbcon_copy_font(struct vc_data *vc, int con)
2620 {
2621 	struct fbcon_display *od = &fb_display[con];
2622 	struct console_font *f = &vc->vc_font;
2623 
2624 	if (od->fontdata == f->data)
2625 		return 0;	/* already the same font... */
2626 	return fbcon_do_set_font(vc, f->width, f->height, od->fontdata, od->userfont);
2627 }
2628 
2629 /*
2630  *  User asked to set font; we are guaranteed that
2631  *	a) width and height are in range 1..32
2632  *	b) charcount does not exceed 512
2633  *  but lets not assume that, since someone might someday want to use larger
2634  *  fonts. And charcount of 512 is small for unicode support.
2635  *
2636  *  However, user space gives the font in 32 rows , regardless of
2637  *  actual font height. So a new API is needed if support for larger fonts
2638  *  is ever implemented.
2639  */
2640 
2641 static int fbcon_set_font(struct vc_data *vc, struct console_font *font,
2642 			  unsigned int flags)
2643 {
2644 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2645 	unsigned charcount = font->charcount;
2646 	int w = font->width;
2647 	int h = font->height;
2648 	int size;
2649 	int i, csum;
2650 	u8 *new_data, *data = font->data;
2651 	int pitch = (font->width+7) >> 3;
2652 
2653 	/* Is there a reason why fbconsole couldn't handle any charcount >256?
2654 	 * If not this check should be changed to charcount < 256 */
2655 	if (charcount != 256 && charcount != 512)
2656 		return -EINVAL;
2657 
2658 	/* Make sure drawing engine can handle the font */
2659 	if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
2660 	    !(info->pixmap.blit_y & (1 << (font->height - 1))))
2661 		return -EINVAL;
2662 
2663 	/* Make sure driver can handle the font length */
2664 	if (fbcon_invalid_charcount(info, charcount))
2665 		return -EINVAL;
2666 
2667 	size = h * pitch * charcount;
2668 
2669 	new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2670 
2671 	if (!new_data)
2672 		return -ENOMEM;
2673 
2674 	new_data += FONT_EXTRA_WORDS * sizeof(int);
2675 	FNTSIZE(new_data) = size;
2676 	FNTCHARCNT(new_data) = charcount;
2677 	REFCOUNT(new_data) = 0;	/* usage counter */
2678 	for (i=0; i< charcount; i++) {
2679 		memcpy(new_data + i*h*pitch, data +  i*32*pitch, h*pitch);
2680 	}
2681 
2682 	/* Since linux has a nice crc32 function use it for counting font
2683 	 * checksums. */
2684 	csum = crc32(0, new_data, size);
2685 
2686 	FNTSUM(new_data) = csum;
2687 	/* Check if the same font is on some other console already */
2688 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2689 		struct vc_data *tmp = vc_cons[i].d;
2690 
2691 		if (fb_display[i].userfont &&
2692 		    fb_display[i].fontdata &&
2693 		    FNTSUM(fb_display[i].fontdata) == csum &&
2694 		    FNTSIZE(fb_display[i].fontdata) == size &&
2695 		    tmp->vc_font.width == w &&
2696 		    !memcmp(fb_display[i].fontdata, new_data, size)) {
2697 			kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2698 			new_data = (u8 *)fb_display[i].fontdata;
2699 			break;
2700 		}
2701 	}
2702 	return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2703 }
2704 
2705 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2706 {
2707 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2708 	const struct font_desc *f;
2709 
2710 	if (!name)
2711 		f = get_default_font(info->var.xres, info->var.yres,
2712 				     info->pixmap.blit_x, info->pixmap.blit_y);
2713 	else if (!(f = find_font(name)))
2714 		return -ENOENT;
2715 
2716 	font->width = f->width;
2717 	font->height = f->height;
2718 	return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2719 }
2720 
2721 static u16 palette_red[16];
2722 static u16 palette_green[16];
2723 static u16 palette_blue[16];
2724 
2725 static struct fb_cmap palette_cmap = {
2726 	0, 16, palette_red, palette_green, palette_blue, NULL
2727 };
2728 
2729 static void fbcon_set_palette(struct vc_data *vc, const unsigned char *table)
2730 {
2731 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2732 	int i, j, k, depth;
2733 	u8 val;
2734 
2735 	if (fbcon_is_inactive(vc, info))
2736 		return;
2737 
2738 	if (!con_is_visible(vc))
2739 		return;
2740 
2741 	depth = fb_get_color_depth(&info->var, &info->fix);
2742 	if (depth > 3) {
2743 		for (i = j = 0; i < 16; i++) {
2744 			k = table[i];
2745 			val = vc->vc_palette[j++];
2746 			palette_red[k] = (val << 8) | val;
2747 			val = vc->vc_palette[j++];
2748 			palette_green[k] = (val << 8) | val;
2749 			val = vc->vc_palette[j++];
2750 			palette_blue[k] = (val << 8) | val;
2751 		}
2752 		palette_cmap.len = 16;
2753 		palette_cmap.start = 0;
2754 	/*
2755 	 * If framebuffer is capable of less than 16 colors,
2756 	 * use default palette of fbcon.
2757 	 */
2758 	} else
2759 		fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2760 
2761 	fb_set_cmap(&palette_cmap, info);
2762 }
2763 
2764 static u16 *fbcon_screen_pos(struct vc_data *vc, int offset)
2765 {
2766 	unsigned long p;
2767 	int line;
2768 
2769 	if (vc->vc_num != fg_console || !softback_lines)
2770 		return (u16 *) (vc->vc_origin + offset);
2771 	line = offset / vc->vc_size_row;
2772 	if (line >= softback_lines)
2773 		return (u16 *) (vc->vc_origin + offset -
2774 				softback_lines * vc->vc_size_row);
2775 	p = softback_curr + offset;
2776 	if (p >= softback_end)
2777 		p += softback_buf - softback_end;
2778 	return (u16 *) p;
2779 }
2780 
2781 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2782 				 int *px, int *py)
2783 {
2784 	unsigned long ret;
2785 	int x, y;
2786 
2787 	if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2788 		unsigned long offset = (pos - vc->vc_origin) / 2;
2789 
2790 		x = offset % vc->vc_cols;
2791 		y = offset / vc->vc_cols;
2792 		if (vc->vc_num == fg_console)
2793 			y += softback_lines;
2794 		ret = pos + (vc->vc_cols - x) * 2;
2795 	} else if (vc->vc_num == fg_console && softback_lines) {
2796 		unsigned long offset = pos - softback_curr;
2797 
2798 		if (pos < softback_curr)
2799 			offset += softback_end - softback_buf;
2800 		offset /= 2;
2801 		x = offset % vc->vc_cols;
2802 		y = offset / vc->vc_cols;
2803 		ret = pos + (vc->vc_cols - x) * 2;
2804 		if (ret == softback_end)
2805 			ret = softback_buf;
2806 		if (ret == softback_in)
2807 			ret = vc->vc_origin;
2808 	} else {
2809 		/* Should not happen */
2810 		x = y = 0;
2811 		ret = vc->vc_origin;
2812 	}
2813 	if (px)
2814 		*px = x;
2815 	if (py)
2816 		*py = y;
2817 	return ret;
2818 }
2819 
2820 /* As we might be inside of softback, we may work with non-contiguous buffer,
2821    that's why we have to use a separate routine. */
2822 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2823 {
2824 	while (cnt--) {
2825 		u16 a = scr_readw(p);
2826 		if (!vc->vc_can_do_color)
2827 			a ^= 0x0800;
2828 		else if (vc->vc_hi_font_mask == 0x100)
2829 			a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2830 			    (((a) & 0x0e00) << 4);
2831 		else
2832 			a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2833 			    (((a) & 0x0700) << 4);
2834 		scr_writew(a, p++);
2835 		if (p == (u16 *) softback_end)
2836 			p = (u16 *) softback_buf;
2837 		if (p == (u16 *) softback_in)
2838 			p = (u16 *) vc->vc_origin;
2839 	}
2840 }
2841 
2842 static void fbcon_scrolldelta(struct vc_data *vc, int lines)
2843 {
2844 	struct fb_info *info = registered_fb[con2fb_map[fg_console]];
2845 	struct fbcon_ops *ops = info->fbcon_par;
2846 	struct fbcon_display *disp = &fb_display[fg_console];
2847 	int offset, limit, scrollback_old;
2848 
2849 	if (softback_top) {
2850 		if (vc->vc_num != fg_console)
2851 			return;
2852 		if (vc->vc_mode != KD_TEXT || !lines)
2853 			return;
2854 		if (logo_shown >= 0) {
2855 			struct vc_data *conp2 = vc_cons[logo_shown].d;
2856 
2857 			if (conp2->vc_top == logo_lines
2858 			    && conp2->vc_bottom == conp2->vc_rows)
2859 				conp2->vc_top = 0;
2860 			if (logo_shown == vc->vc_num) {
2861 				unsigned long p, q;
2862 				int i;
2863 
2864 				p = softback_in;
2865 				q = vc->vc_origin +
2866 				    logo_lines * vc->vc_size_row;
2867 				for (i = 0; i < logo_lines; i++) {
2868 					if (p == softback_top)
2869 						break;
2870 					if (p == softback_buf)
2871 						p = softback_end;
2872 					p -= vc->vc_size_row;
2873 					q -= vc->vc_size_row;
2874 					scr_memcpyw((u16 *) q, (u16 *) p,
2875 						    vc->vc_size_row);
2876 				}
2877 				softback_in = softback_curr = p;
2878 				update_region(vc, vc->vc_origin,
2879 					      logo_lines * vc->vc_cols);
2880 			}
2881 			logo_shown = FBCON_LOGO_CANSHOW;
2882 		}
2883 		fbcon_cursor(vc, CM_ERASE | CM_SOFTBACK);
2884 		fbcon_redraw_softback(vc, disp, lines);
2885 		fbcon_cursor(vc, CM_DRAW | CM_SOFTBACK);
2886 		return;
2887 	}
2888 
2889 	if (!scrollback_phys_max)
2890 		return;
2891 
2892 	scrollback_old = scrollback_current;
2893 	scrollback_current -= lines;
2894 	if (scrollback_current < 0)
2895 		scrollback_current = 0;
2896 	else if (scrollback_current > scrollback_max)
2897 		scrollback_current = scrollback_max;
2898 	if (scrollback_current == scrollback_old)
2899 		return;
2900 
2901 	if (fbcon_is_inactive(vc, info))
2902 		return;
2903 
2904 	fbcon_cursor(vc, CM_ERASE);
2905 
2906 	offset = disp->yscroll - scrollback_current;
2907 	limit = disp->vrows;
2908 	switch (disp->scrollmode) {
2909 	case SCROLL_WRAP_MOVE:
2910 		info->var.vmode |= FB_VMODE_YWRAP;
2911 		break;
2912 	case SCROLL_PAN_MOVE:
2913 	case SCROLL_PAN_REDRAW:
2914 		limit -= vc->vc_rows;
2915 		info->var.vmode &= ~FB_VMODE_YWRAP;
2916 		break;
2917 	}
2918 	if (offset < 0)
2919 		offset += limit;
2920 	else if (offset >= limit)
2921 		offset -= limit;
2922 
2923 	ops->var.xoffset = 0;
2924 	ops->var.yoffset = offset * vc->vc_font.height;
2925 	ops->update_start(info);
2926 
2927 	if (!scrollback_current)
2928 		fbcon_cursor(vc, CM_DRAW);
2929 }
2930 
2931 static int fbcon_set_origin(struct vc_data *vc)
2932 {
2933 	if (softback_lines)
2934 		fbcon_scrolldelta(vc, softback_lines);
2935 	return 0;
2936 }
2937 
2938 void fbcon_suspended(struct fb_info *info)
2939 {
2940 	struct vc_data *vc = NULL;
2941 	struct fbcon_ops *ops = info->fbcon_par;
2942 
2943 	if (!ops || ops->currcon < 0)
2944 		return;
2945 	vc = vc_cons[ops->currcon].d;
2946 
2947 	/* Clear cursor, restore saved data */
2948 	fbcon_cursor(vc, CM_ERASE);
2949 }
2950 
2951 void fbcon_resumed(struct fb_info *info)
2952 {
2953 	struct vc_data *vc;
2954 	struct fbcon_ops *ops = info->fbcon_par;
2955 
2956 	if (!ops || ops->currcon < 0)
2957 		return;
2958 	vc = vc_cons[ops->currcon].d;
2959 
2960 	update_screen(vc);
2961 }
2962 
2963 static void fbcon_modechanged(struct fb_info *info)
2964 {
2965 	struct fbcon_ops *ops = info->fbcon_par;
2966 	struct vc_data *vc;
2967 	struct fbcon_display *p;
2968 	int rows, cols;
2969 
2970 	if (!ops || ops->currcon < 0)
2971 		return;
2972 	vc = vc_cons[ops->currcon].d;
2973 	if (vc->vc_mode != KD_TEXT ||
2974 	    registered_fb[con2fb_map[ops->currcon]] != info)
2975 		return;
2976 
2977 	p = &fb_display[vc->vc_num];
2978 	set_blitting_type(vc, info);
2979 
2980 	if (con_is_visible(vc)) {
2981 		var_to_display(p, &info->var, info);
2982 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2983 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2984 		cols /= vc->vc_font.width;
2985 		rows /= vc->vc_font.height;
2986 		vc_resize(vc, cols, rows);
2987 		updatescrollmode(p, info, vc);
2988 		scrollback_max = 0;
2989 		scrollback_current = 0;
2990 
2991 		if (!fbcon_is_inactive(vc, info)) {
2992 		    ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2993 		    ops->update_start(info);
2994 		}
2995 
2996 		fbcon_set_palette(vc, color_table);
2997 		update_screen(vc);
2998 		if (softback_buf)
2999 			fbcon_update_softback(vc);
3000 	}
3001 }
3002 
3003 static void fbcon_set_all_vcs(struct fb_info *info)
3004 {
3005 	struct fbcon_ops *ops = info->fbcon_par;
3006 	struct vc_data *vc;
3007 	struct fbcon_display *p;
3008 	int i, rows, cols, fg = -1;
3009 
3010 	if (!ops || ops->currcon < 0)
3011 		return;
3012 
3013 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
3014 		vc = vc_cons[i].d;
3015 		if (!vc || vc->vc_mode != KD_TEXT ||
3016 		    registered_fb[con2fb_map[i]] != info)
3017 			continue;
3018 
3019 		if (con_is_visible(vc)) {
3020 			fg = i;
3021 			continue;
3022 		}
3023 
3024 		p = &fb_display[vc->vc_num];
3025 		set_blitting_type(vc, info);
3026 		var_to_display(p, &info->var, info);
3027 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
3028 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
3029 		cols /= vc->vc_font.width;
3030 		rows /= vc->vc_font.height;
3031 		vc_resize(vc, cols, rows);
3032 	}
3033 
3034 	if (fg != -1)
3035 		fbcon_modechanged(info);
3036 }
3037 
3038 
3039 void fbcon_update_vcs(struct fb_info *info, bool all)
3040 {
3041 	if (all)
3042 		fbcon_set_all_vcs(info);
3043 	else
3044 		fbcon_modechanged(info);
3045 }
3046 EXPORT_SYMBOL(fbcon_update_vcs);
3047 
3048 int fbcon_mode_deleted(struct fb_info *info,
3049 		       struct fb_videomode *mode)
3050 {
3051 	struct fb_info *fb_info;
3052 	struct fbcon_display *p;
3053 	int i, j, found = 0;
3054 
3055 	/* before deletion, ensure that mode is not in use */
3056 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
3057 		j = con2fb_map[i];
3058 		if (j == -1)
3059 			continue;
3060 		fb_info = registered_fb[j];
3061 		if (fb_info != info)
3062 			continue;
3063 		p = &fb_display[i];
3064 		if (!p || !p->mode)
3065 			continue;
3066 		if (fb_mode_is_equal(p->mode, mode)) {
3067 			found = 1;
3068 			break;
3069 		}
3070 	}
3071 	return found;
3072 }
3073 
3074 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
3075 static void fbcon_unbind(void)
3076 {
3077 	int ret;
3078 
3079 	ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
3080 				fbcon_is_default);
3081 
3082 	if (!ret)
3083 		fbcon_has_console_bind = 0;
3084 }
3085 #else
3086 static inline void fbcon_unbind(void) {}
3087 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3088 
3089 /* called with console_lock held */
3090 void fbcon_fb_unbind(struct fb_info *info)
3091 {
3092 	int i, new_idx = -1, ret = 0;
3093 	int idx = info->node;
3094 
3095 	WARN_CONSOLE_UNLOCKED();
3096 
3097 	if (!fbcon_has_console_bind)
3098 		return;
3099 
3100 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
3101 		if (con2fb_map[i] != idx &&
3102 		    con2fb_map[i] != -1) {
3103 			new_idx = con2fb_map[i];
3104 			break;
3105 		}
3106 	}
3107 
3108 	if (new_idx != -1) {
3109 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3110 			if (con2fb_map[i] == idx)
3111 				set_con2fb_map(i, new_idx, 0);
3112 		}
3113 	} else {
3114 		struct fb_info *info = registered_fb[idx];
3115 
3116 		/* This is sort of like set_con2fb_map, except it maps
3117 		 * the consoles to no device and then releases the
3118 		 * oldinfo to free memory and cancel the cursor blink
3119 		 * timer. I can imagine this just becoming part of
3120 		 * set_con2fb_map where new_idx is -1
3121 		 */
3122 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3123 			if (con2fb_map[i] == idx) {
3124 				con2fb_map[i] = -1;
3125 				if (!search_fb_in_map(idx)) {
3126 					ret = con2fb_release_oldinfo(vc_cons[i].d,
3127 								     info, NULL, i,
3128 								     idx, 0);
3129 					if (ret) {
3130 						con2fb_map[i] = idx;
3131 						return;
3132 					}
3133 				}
3134 			}
3135 		}
3136 		fbcon_unbind();
3137 	}
3138 }
3139 
3140 /* called with console_lock held */
3141 void fbcon_fb_unregistered(struct fb_info *info)
3142 {
3143 	int i, idx;
3144 
3145 	WARN_CONSOLE_UNLOCKED();
3146 
3147 	if (deferred_takeover)
3148 		return;
3149 
3150 	idx = info->node;
3151 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
3152 		if (con2fb_map[i] == idx)
3153 			con2fb_map[i] = -1;
3154 	}
3155 
3156 	if (idx == info_idx) {
3157 		info_idx = -1;
3158 
3159 		for_each_registered_fb(i) {
3160 			info_idx = i;
3161 			break;
3162 		}
3163 	}
3164 
3165 	if (info_idx != -1) {
3166 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3167 			if (con2fb_map[i] == -1)
3168 				con2fb_map[i] = info_idx;
3169 		}
3170 	}
3171 
3172 	if (primary_device == idx)
3173 		primary_device = -1;
3174 
3175 	if (!num_registered_fb)
3176 		do_unregister_con_driver(&fb_con);
3177 }
3178 
3179 void fbcon_remap_all(struct fb_info *info)
3180 {
3181 	int i, idx = info->node;
3182 
3183 	console_lock();
3184 	if (deferred_takeover) {
3185 		for (i = first_fb_vc; i <= last_fb_vc; i++)
3186 			con2fb_map_boot[i] = idx;
3187 		fbcon_map_override();
3188 		console_unlock();
3189 		return;
3190 	}
3191 
3192 	for (i = first_fb_vc; i <= last_fb_vc; i++)
3193 		set_con2fb_map(i, idx, 0);
3194 
3195 	if (con_is_bound(&fb_con)) {
3196 		printk(KERN_INFO "fbcon: Remapping primary device, "
3197 		       "fb%i, to tty %i-%i\n", idx,
3198 		       first_fb_vc + 1, last_fb_vc + 1);
3199 		info_idx = idx;
3200 	}
3201 	console_unlock();
3202 }
3203 
3204 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
3205 static void fbcon_select_primary(struct fb_info *info)
3206 {
3207 	if (!map_override && primary_device == -1 &&
3208 	    fb_is_primary_device(info)) {
3209 		int i;
3210 
3211 		printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
3212 		       info->fix.id, info->node);
3213 		primary_device = info->node;
3214 
3215 		for (i = first_fb_vc; i <= last_fb_vc; i++)
3216 			con2fb_map_boot[i] = primary_device;
3217 
3218 		if (con_is_bound(&fb_con)) {
3219 			printk(KERN_INFO "fbcon: Remapping primary device, "
3220 			       "fb%i, to tty %i-%i\n", info->node,
3221 			       first_fb_vc + 1, last_fb_vc + 1);
3222 			info_idx = primary_device;
3223 		}
3224 	}
3225 
3226 }
3227 #else
3228 static inline void fbcon_select_primary(struct fb_info *info)
3229 {
3230 	return;
3231 }
3232 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
3233 
3234 /* called with console_lock held */
3235 int fbcon_fb_registered(struct fb_info *info)
3236 {
3237 	int ret = 0, i, idx;
3238 
3239 	WARN_CONSOLE_UNLOCKED();
3240 
3241 	idx = info->node;
3242 	fbcon_select_primary(info);
3243 
3244 	if (deferred_takeover) {
3245 		pr_info("fbcon: Deferring console take-over\n");
3246 		return 0;
3247 	}
3248 
3249 	if (info_idx == -1) {
3250 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3251 			if (con2fb_map_boot[i] == idx) {
3252 				info_idx = idx;
3253 				break;
3254 			}
3255 		}
3256 
3257 		if (info_idx != -1)
3258 			ret = do_fbcon_takeover(1);
3259 	} else {
3260 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3261 			if (con2fb_map_boot[i] == idx)
3262 				set_con2fb_map(i, idx, 0);
3263 		}
3264 	}
3265 
3266 	return ret;
3267 }
3268 
3269 void fbcon_fb_blanked(struct fb_info *info, int blank)
3270 {
3271 	struct fbcon_ops *ops = info->fbcon_par;
3272 	struct vc_data *vc;
3273 
3274 	if (!ops || ops->currcon < 0)
3275 		return;
3276 
3277 	vc = vc_cons[ops->currcon].d;
3278 	if (vc->vc_mode != KD_TEXT ||
3279 			registered_fb[con2fb_map[ops->currcon]] != info)
3280 		return;
3281 
3282 	if (con_is_visible(vc)) {
3283 		if (blank)
3284 			do_blank_screen(0);
3285 		else
3286 			do_unblank_screen(0);
3287 	}
3288 	ops->blank_state = blank;
3289 }
3290 
3291 void fbcon_new_modelist(struct fb_info *info)
3292 {
3293 	int i;
3294 	struct vc_data *vc;
3295 	struct fb_var_screeninfo var;
3296 	const struct fb_videomode *mode;
3297 
3298 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
3299 		if (registered_fb[con2fb_map[i]] != info)
3300 			continue;
3301 		if (!fb_display[i].mode)
3302 			continue;
3303 		vc = vc_cons[i].d;
3304 		display_to_var(&var, &fb_display[i]);
3305 		mode = fb_find_nearest_mode(fb_display[i].mode,
3306 					    &info->modelist);
3307 		fb_videomode_to_var(&var, mode);
3308 		fbcon_set_disp(info, &var, vc->vc_num);
3309 	}
3310 }
3311 
3312 void fbcon_get_requirement(struct fb_info *info,
3313 			   struct fb_blit_caps *caps)
3314 {
3315 	struct vc_data *vc;
3316 	struct fbcon_display *p;
3317 
3318 	if (caps->flags) {
3319 		int i, charcnt;
3320 
3321 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3322 			vc = vc_cons[i].d;
3323 			if (vc && vc->vc_mode == KD_TEXT &&
3324 			    info->node == con2fb_map[i]) {
3325 				p = &fb_display[i];
3326 				caps->x |= 1 << (vc->vc_font.width - 1);
3327 				caps->y |= 1 << (vc->vc_font.height - 1);
3328 				charcnt = (p->userfont) ?
3329 					FNTCHARCNT(p->fontdata) : 256;
3330 				if (caps->len < charcnt)
3331 					caps->len = charcnt;
3332 			}
3333 		}
3334 	} else {
3335 		vc = vc_cons[fg_console].d;
3336 
3337 		if (vc && vc->vc_mode == KD_TEXT &&
3338 		    info->node == con2fb_map[fg_console]) {
3339 			p = &fb_display[fg_console];
3340 			caps->x = 1 << (vc->vc_font.width - 1);
3341 			caps->y = 1 << (vc->vc_font.height - 1);
3342 			caps->len = (p->userfont) ?
3343 				FNTCHARCNT(p->fontdata) : 256;
3344 		}
3345 	}
3346 }
3347 
3348 int fbcon_set_con2fb_map_ioctl(void __user *argp)
3349 {
3350 	struct fb_con2fbmap con2fb;
3351 	int ret;
3352 
3353 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
3354 		return -EFAULT;
3355 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
3356 		return -EINVAL;
3357 	if (con2fb.framebuffer >= FB_MAX)
3358 		return -EINVAL;
3359 	if (!registered_fb[con2fb.framebuffer])
3360 		request_module("fb%d", con2fb.framebuffer);
3361 	if (!registered_fb[con2fb.framebuffer]) {
3362 		return -EINVAL;
3363 	}
3364 
3365 	console_lock();
3366 	ret = set_con2fb_map(con2fb.console - 1,
3367 			     con2fb.framebuffer, 1);
3368 	console_unlock();
3369 
3370 	return ret;
3371 }
3372 
3373 int fbcon_get_con2fb_map_ioctl(void __user *argp)
3374 {
3375 	struct fb_con2fbmap con2fb;
3376 
3377 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
3378 		return -EFAULT;
3379 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
3380 		return -EINVAL;
3381 
3382 	console_lock();
3383 	con2fb.framebuffer = con2fb_map[con2fb.console - 1];
3384 	console_unlock();
3385 
3386 	return copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
3387 }
3388 
3389 /*
3390  *  The console `switch' structure for the frame buffer based console
3391  */
3392 
3393 static const struct consw fb_con = {
3394 	.owner			= THIS_MODULE,
3395 	.con_startup 		= fbcon_startup,
3396 	.con_init 		= fbcon_init,
3397 	.con_deinit 		= fbcon_deinit,
3398 	.con_clear 		= fbcon_clear,
3399 	.con_putc 		= fbcon_putc,
3400 	.con_putcs 		= fbcon_putcs,
3401 	.con_cursor 		= fbcon_cursor,
3402 	.con_scroll 		= fbcon_scroll,
3403 	.con_switch 		= fbcon_switch,
3404 	.con_blank 		= fbcon_blank,
3405 	.con_font_set 		= fbcon_set_font,
3406 	.con_font_get 		= fbcon_get_font,
3407 	.con_font_default	= fbcon_set_def_font,
3408 	.con_font_copy 		= fbcon_copy_font,
3409 	.con_set_palette 	= fbcon_set_palette,
3410 	.con_scrolldelta 	= fbcon_scrolldelta,
3411 	.con_set_origin 	= fbcon_set_origin,
3412 	.con_invert_region 	= fbcon_invert_region,
3413 	.con_screen_pos 	= fbcon_screen_pos,
3414 	.con_getxy 		= fbcon_getxy,
3415 	.con_resize             = fbcon_resize,
3416 	.con_debug_enter	= fbcon_debug_enter,
3417 	.con_debug_leave	= fbcon_debug_leave,
3418 };
3419 
3420 static ssize_t store_rotate(struct device *device,
3421 			    struct device_attribute *attr, const char *buf,
3422 			    size_t count)
3423 {
3424 	struct fb_info *info;
3425 	int rotate, idx;
3426 	char **last = NULL;
3427 
3428 	console_lock();
3429 	idx = con2fb_map[fg_console];
3430 
3431 	if (idx == -1 || registered_fb[idx] == NULL)
3432 		goto err;
3433 
3434 	info = registered_fb[idx];
3435 	rotate = simple_strtoul(buf, last, 0);
3436 	fbcon_rotate(info, rotate);
3437 err:
3438 	console_unlock();
3439 	return count;
3440 }
3441 
3442 static ssize_t store_rotate_all(struct device *device,
3443 				struct device_attribute *attr,const char *buf,
3444 				size_t count)
3445 {
3446 	struct fb_info *info;
3447 	int rotate, idx;
3448 	char **last = NULL;
3449 
3450 	console_lock();
3451 	idx = con2fb_map[fg_console];
3452 
3453 	if (idx == -1 || registered_fb[idx] == NULL)
3454 		goto err;
3455 
3456 	info = registered_fb[idx];
3457 	rotate = simple_strtoul(buf, last, 0);
3458 	fbcon_rotate_all(info, rotate);
3459 err:
3460 	console_unlock();
3461 	return count;
3462 }
3463 
3464 static ssize_t show_rotate(struct device *device,
3465 			   struct device_attribute *attr,char *buf)
3466 {
3467 	struct fb_info *info;
3468 	int rotate = 0, idx;
3469 
3470 	console_lock();
3471 	idx = con2fb_map[fg_console];
3472 
3473 	if (idx == -1 || registered_fb[idx] == NULL)
3474 		goto err;
3475 
3476 	info = registered_fb[idx];
3477 	rotate = fbcon_get_rotate(info);
3478 err:
3479 	console_unlock();
3480 	return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3481 }
3482 
3483 static ssize_t show_cursor_blink(struct device *device,
3484 				 struct device_attribute *attr, char *buf)
3485 {
3486 	struct fb_info *info;
3487 	struct fbcon_ops *ops;
3488 	int idx, blink = -1;
3489 
3490 	console_lock();
3491 	idx = con2fb_map[fg_console];
3492 
3493 	if (idx == -1 || registered_fb[idx] == NULL)
3494 		goto err;
3495 
3496 	info = registered_fb[idx];
3497 	ops = info->fbcon_par;
3498 
3499 	if (!ops)
3500 		goto err;
3501 
3502 	blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3503 err:
3504 	console_unlock();
3505 	return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3506 }
3507 
3508 static ssize_t store_cursor_blink(struct device *device,
3509 				  struct device_attribute *attr,
3510 				  const char *buf, size_t count)
3511 {
3512 	struct fb_info *info;
3513 	int blink, idx;
3514 	char **last = NULL;
3515 
3516 	console_lock();
3517 	idx = con2fb_map[fg_console];
3518 
3519 	if (idx == -1 || registered_fb[idx] == NULL)
3520 		goto err;
3521 
3522 	info = registered_fb[idx];
3523 
3524 	if (!info->fbcon_par)
3525 		goto err;
3526 
3527 	blink = simple_strtoul(buf, last, 0);
3528 
3529 	if (blink) {
3530 		fbcon_cursor_noblink = 0;
3531 		fbcon_add_cursor_timer(info);
3532 	} else {
3533 		fbcon_cursor_noblink = 1;
3534 		fbcon_del_cursor_timer(info);
3535 	}
3536 
3537 err:
3538 	console_unlock();
3539 	return count;
3540 }
3541 
3542 static struct device_attribute device_attrs[] = {
3543 	__ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3544 	__ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3545 	__ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3546 	       store_cursor_blink),
3547 };
3548 
3549 static int fbcon_init_device(void)
3550 {
3551 	int i, error = 0;
3552 
3553 	fbcon_has_sysfs = 1;
3554 
3555 	for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3556 		error = device_create_file(fbcon_device, &device_attrs[i]);
3557 
3558 		if (error)
3559 			break;
3560 	}
3561 
3562 	if (error) {
3563 		while (--i >= 0)
3564 			device_remove_file(fbcon_device, &device_attrs[i]);
3565 
3566 		fbcon_has_sysfs = 0;
3567 	}
3568 
3569 	return 0;
3570 }
3571 
3572 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3573 static void fbcon_register_existing_fbs(struct work_struct *work)
3574 {
3575 	int i;
3576 
3577 	console_lock();
3578 
3579 	for_each_registered_fb(i)
3580 		fbcon_fb_registered(registered_fb[i]);
3581 
3582 	console_unlock();
3583 }
3584 
3585 static struct notifier_block fbcon_output_nb;
3586 static DECLARE_WORK(fbcon_deferred_takeover_work, fbcon_register_existing_fbs);
3587 
3588 static int fbcon_output_notifier(struct notifier_block *nb,
3589 				 unsigned long action, void *data)
3590 {
3591 	WARN_CONSOLE_UNLOCKED();
3592 
3593 	pr_info("fbcon: Taking over console\n");
3594 
3595 	dummycon_unregister_output_notifier(&fbcon_output_nb);
3596 	deferred_takeover = false;
3597 	logo_shown = FBCON_LOGO_DONTSHOW;
3598 
3599 	/* We may get called in atomic context */
3600 	schedule_work(&fbcon_deferred_takeover_work);
3601 
3602 	return NOTIFY_OK;
3603 }
3604 #endif
3605 
3606 static void fbcon_start(void)
3607 {
3608 	WARN_CONSOLE_UNLOCKED();
3609 
3610 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3611 	if (conswitchp != &dummy_con)
3612 		deferred_takeover = false;
3613 
3614 	if (deferred_takeover) {
3615 		fbcon_output_nb.notifier_call = fbcon_output_notifier;
3616 		dummycon_register_output_notifier(&fbcon_output_nb);
3617 		return;
3618 	}
3619 #endif
3620 
3621 	if (num_registered_fb) {
3622 		int i;
3623 
3624 		for_each_registered_fb(i) {
3625 			info_idx = i;
3626 			break;
3627 		}
3628 
3629 		do_fbcon_takeover(0);
3630 	}
3631 }
3632 
3633 static void fbcon_exit(void)
3634 {
3635 	struct fb_info *info;
3636 	int i, j, mapped;
3637 
3638 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3639 	if (deferred_takeover) {
3640 		dummycon_unregister_output_notifier(&fbcon_output_nb);
3641 		deferred_takeover = false;
3642 	}
3643 #endif
3644 
3645 	kvfree((void *)softback_buf);
3646 	softback_buf = 0UL;
3647 
3648 	for_each_registered_fb(i) {
3649 		int pending = 0;
3650 
3651 		mapped = 0;
3652 		info = registered_fb[i];
3653 
3654 		if (info->queue.func)
3655 			pending = cancel_work_sync(&info->queue);
3656 		DPRINTK("fbcon: %s pending work\n", (pending ? "canceled" :
3657 			"no"));
3658 
3659 		for (j = first_fb_vc; j <= last_fb_vc; j++) {
3660 			if (con2fb_map[j] == i) {
3661 				mapped = 1;
3662 				con2fb_map[j] = -1;
3663 			}
3664 		}
3665 
3666 		if (mapped) {
3667 			if (info->fbops->fb_release)
3668 				info->fbops->fb_release(info, 0);
3669 			module_put(info->fbops->owner);
3670 
3671 			if (info->fbcon_par) {
3672 				struct fbcon_ops *ops = info->fbcon_par;
3673 
3674 				fbcon_del_cursor_timer(info);
3675 				kfree(ops->cursor_src);
3676 				kfree(ops->cursor_state.mask);
3677 				kfree(info->fbcon_par);
3678 				info->fbcon_par = NULL;
3679 			}
3680 
3681 			if (info->queue.func == fb_flashcursor)
3682 				info->queue.func = NULL;
3683 		}
3684 	}
3685 }
3686 
3687 void __init fb_console_init(void)
3688 {
3689 	int i;
3690 
3691 	console_lock();
3692 	fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3693 				     "fbcon");
3694 
3695 	if (IS_ERR(fbcon_device)) {
3696 		printk(KERN_WARNING "Unable to create device "
3697 		       "for fbcon; errno = %ld\n",
3698 		       PTR_ERR(fbcon_device));
3699 		fbcon_device = NULL;
3700 	} else
3701 		fbcon_init_device();
3702 
3703 	for (i = 0; i < MAX_NR_CONSOLES; i++)
3704 		con2fb_map[i] = -1;
3705 
3706 	fbcon_start();
3707 	console_unlock();
3708 }
3709 
3710 #ifdef MODULE
3711 
3712 static void __exit fbcon_deinit_device(void)
3713 {
3714 	int i;
3715 
3716 	if (fbcon_has_sysfs) {
3717 		for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3718 			device_remove_file(fbcon_device, &device_attrs[i]);
3719 
3720 		fbcon_has_sysfs = 0;
3721 	}
3722 }
3723 
3724 void __exit fb_console_exit(void)
3725 {
3726 	console_lock();
3727 	fbcon_deinit_device();
3728 	device_destroy(fb_class, MKDEV(0, 0));
3729 	fbcon_exit();
3730 	do_unregister_con_driver(&fb_con);
3731 	console_unlock();
3732 }
3733 #endif
3734