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