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