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