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