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