xref: /openbmc/linux/drivers/video/fbdev/core/fbcon.c (revision e7f127b2)
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 #include <linux/module.h>
60 #include <linux/types.h>
61 #include <linux/fs.h>
62 #include <linux/kernel.h>
63 #include <linux/delay.h>	/* MSch: for IRQ probe */
64 #include <linux/console.h>
65 #include <linux/string.h>
66 #include <linux/kd.h>
67 #include <linux/slab.h>
68 #include <linux/fb.h>
69 #include <linux/fbcon.h>
70 #include <linux/vt_kern.h>
71 #include <linux/selection.h>
72 #include <linux/font.h>
73 #include <linux/smp.h>
74 #include <linux/init.h>
75 #include <linux/interrupt.h>
76 #include <linux/crc32.h> /* For counting font checksums */
77 #include <linux/uaccess.h>
78 #include <asm/fb.h>
79 #include <asm/irq.h>
80 
81 #include "fbcon.h"
82 
83 /*
84  * FIXME: Locking
85  *
86  * - fbcon state itself is protected by the console_lock, and the code does a
87  *   pretty good job at making sure that lock is held everywhere it's needed.
88  *
89  * - access to the registered_fb array is entirely unprotected. This should use
90  *   proper object lifetime handling, i.e. get/put_fb_info. This also means
91  *   switching from indices to proper pointers for fb_info everywhere.
92  *
93  * - fbcon doesn't bother with fb_lock/unlock at all. This is buggy, since it
94  *   means concurrent access to the same fbdev from both fbcon and userspace
95  *   will blow up. To fix this all fbcon calls from fbmem.c need to be moved out
96  *   of fb_lock/unlock protected sections, since otherwise we'll recurse and
97  *   deadlock eventually. Aside: Due to these deadlock issues the fbdev code in
98  *   fbmem.c cannot use locking asserts, and there's lots of callers which get
99  *   the rules wrong, e.g. fbsysfs.c entirely missed fb_lock/unlock calls too.
100  */
101 
102 enum {
103 	FBCON_LOGO_CANSHOW	= -1,	/* the logo can be shown */
104 	FBCON_LOGO_DRAW		= -2,	/* draw the logo to a console */
105 	FBCON_LOGO_DONTSHOW	= -3	/* do not show the logo */
106 };
107 
108 static struct fbcon_display fb_display[MAX_NR_CONSOLES];
109 
110 static signed char con2fb_map[MAX_NR_CONSOLES];
111 static signed char con2fb_map_boot[MAX_NR_CONSOLES];
112 
113 static int logo_lines;
114 /* logo_shown is an index to vc_cons when >= 0; otherwise follows FBCON_LOGO
115    enums.  */
116 static int logo_shown = FBCON_LOGO_CANSHOW;
117 /* console mappings */
118 static int first_fb_vc;
119 static int last_fb_vc = MAX_NR_CONSOLES - 1;
120 static int fbcon_is_default = 1;
121 static int primary_device = -1;
122 static int fbcon_has_console_bind;
123 
124 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
125 static int map_override;
126 
127 static inline void fbcon_map_override(void)
128 {
129 	map_override = 1;
130 }
131 #else
132 static inline void fbcon_map_override(void)
133 {
134 }
135 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY */
136 
137 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
138 static bool deferred_takeover = true;
139 #else
140 #define deferred_takeover false
141 #endif
142 
143 /* font data */
144 static char fontname[40];
145 
146 /* current fb_info */
147 static int info_idx = -1;
148 
149 /* console rotation */
150 static int initial_rotation = -1;
151 static int fbcon_has_sysfs;
152 static int margin_color;
153 
154 static const struct consw fb_con;
155 
156 #define advance_row(p, delta) (unsigned short *)((unsigned long)(p) + (delta) * vc->vc_size_row)
157 
158 static int fbcon_cursor_noblink;
159 
160 #define divides(a, b)	((!(a) || (b)%(a)) ? 0 : 1)
161 
162 /*
163  *  Interface used by the world
164  */
165 
166 static const char *fbcon_startup(void);
167 static void fbcon_init(struct vc_data *vc, int init);
168 static void fbcon_deinit(struct vc_data *vc);
169 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
170 			int width);
171 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos);
172 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
173 			int count, int ypos, int xpos);
174 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only);
175 static void fbcon_cursor(struct vc_data *vc, int mode);
176 static int fbcon_switch(struct vc_data *vc);
177 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch);
178 static void fbcon_set_palette(struct vc_data *vc, const unsigned char *table);
179 
180 /*
181  *  Internal routines
182  */
183 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
184 			   int unit);
185 static void fbcon_modechanged(struct fb_info *info);
186 static void fbcon_set_all_vcs(struct fb_info *info);
187 static void fbcon_start(void);
188 static void fbcon_exit(void);
189 static struct device *fbcon_device;
190 
191 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
192 static inline void fbcon_set_rotation(struct fb_info *info)
193 {
194 	struct fbcon_ops *ops = info->fbcon_par;
195 
196 	if (!(info->flags & FBINFO_MISC_TILEBLITTING) &&
197 	    ops->p->con_rotate < 4)
198 		ops->rotate = ops->p->con_rotate;
199 	else
200 		ops->rotate = 0;
201 }
202 
203 static void fbcon_rotate(struct fb_info *info, u32 rotate)
204 {
205 	struct fbcon_ops *ops= info->fbcon_par;
206 	struct fb_info *fb_info;
207 
208 	if (!ops || ops->currcon == -1)
209 		return;
210 
211 	fb_info = registered_fb[con2fb_map[ops->currcon]];
212 
213 	if (info == fb_info) {
214 		struct fbcon_display *p = &fb_display[ops->currcon];
215 
216 		if (rotate < 4)
217 			p->con_rotate = rotate;
218 		else
219 			p->con_rotate = 0;
220 
221 		fbcon_modechanged(info);
222 	}
223 }
224 
225 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
226 {
227 	struct fbcon_ops *ops = info->fbcon_par;
228 	struct vc_data *vc;
229 	struct fbcon_display *p;
230 	int i;
231 
232 	if (!ops || ops->currcon < 0 || rotate > 3)
233 		return;
234 
235 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
236 		vc = vc_cons[i].d;
237 		if (!vc || vc->vc_mode != KD_TEXT ||
238 		    registered_fb[con2fb_map[i]] != info)
239 			continue;
240 
241 		p = &fb_display[vc->vc_num];
242 		p->con_rotate = rotate;
243 	}
244 
245 	fbcon_set_all_vcs(info);
246 }
247 #else
248 static inline void fbcon_set_rotation(struct fb_info *info)
249 {
250 	struct fbcon_ops *ops = info->fbcon_par;
251 
252 	ops->rotate = FB_ROTATE_UR;
253 }
254 
255 static void fbcon_rotate(struct fb_info *info, u32 rotate)
256 {
257 	return;
258 }
259 
260 static void fbcon_rotate_all(struct fb_info *info, u32 rotate)
261 {
262 	return;
263 }
264 #endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */
265 
266 static int fbcon_get_rotate(struct fb_info *info)
267 {
268 	struct fbcon_ops *ops = info->fbcon_par;
269 
270 	return (ops) ? ops->rotate : 0;
271 }
272 
273 static inline int fbcon_is_inactive(struct vc_data *vc, struct fb_info *info)
274 {
275 	struct fbcon_ops *ops = info->fbcon_par;
276 
277 	return (info->state != FBINFO_STATE_RUNNING ||
278 		vc->vc_mode != KD_TEXT || ops->graphics);
279 }
280 
281 static int get_color(struct vc_data *vc, struct fb_info *info,
282 	      u16 c, int is_fg)
283 {
284 	int depth = fb_get_color_depth(&info->var, &info->fix);
285 	int color = 0;
286 
287 	if (console_blanked) {
288 		unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
289 
290 		c = vc->vc_video_erase_char & charmask;
291 	}
292 
293 	if (depth != 1)
294 		color = (is_fg) ? attr_fgcol((vc->vc_hi_font_mask) ? 9 : 8, c)
295 			: attr_bgcol((vc->vc_hi_font_mask) ? 13 : 12, c);
296 
297 	switch (depth) {
298 	case 1:
299 	{
300 		int col = mono_col(info);
301 		/* 0 or 1 */
302 		int fg = (info->fix.visual != FB_VISUAL_MONO01) ? col : 0;
303 		int bg = (info->fix.visual != FB_VISUAL_MONO01) ? 0 : col;
304 
305 		if (console_blanked)
306 			fg = bg;
307 
308 		color = (is_fg) ? fg : bg;
309 		break;
310 	}
311 	case 2:
312 		/*
313 		 * Scale down 16-colors to 4 colors. Default 4-color palette
314 		 * is grayscale. However, simply dividing the values by 4
315 		 * will not work, as colors 1, 2 and 3 will be scaled-down
316 		 * to zero rendering them invisible.  So empirically convert
317 		 * colors to a sane 4-level grayscale.
318 		 */
319 		switch (color) {
320 		case 0:
321 			color = 0; /* black */
322 			break;
323 		case 1 ... 6:
324 			color = 2; /* white */
325 			break;
326 		case 7 ... 8:
327 			color = 1; /* gray */
328 			break;
329 		default:
330 			color = 3; /* intense white */
331 			break;
332 		}
333 		break;
334 	case 3:
335 		/*
336 		 * Last 8 entries of default 16-color palette is a more intense
337 		 * version of the first 8 (i.e., same chrominance, different
338 		 * luminance).
339 		 */
340 		color &= 7;
341 		break;
342 	}
343 
344 
345 	return color;
346 }
347 
348 static void fb_flashcursor(struct work_struct *work)
349 {
350 	struct fb_info *info = container_of(work, struct fb_info, queue);
351 	struct fbcon_ops *ops = info->fbcon_par;
352 	struct vc_data *vc = NULL;
353 	int c;
354 	int mode;
355 	int ret;
356 
357 	/* FIXME: we should sort out the unbind locking instead */
358 	/* instead we just fail to flash the cursor if we can't get
359 	 * the lock instead of blocking fbcon deinit */
360 	ret = console_trylock();
361 	if (ret == 0)
362 		return;
363 
364 	if (ops && ops->currcon != -1)
365 		vc = vc_cons[ops->currcon].d;
366 
367 	if (!vc || !con_is_visible(vc) ||
368  	    registered_fb[con2fb_map[vc->vc_num]] != info ||
369 	    vc->vc_deccm != 1) {
370 		console_unlock();
371 		return;
372 	}
373 
374 	c = scr_readw((u16 *) vc->vc_pos);
375 	mode = (!ops->cursor_flash || ops->cursor_state.enable) ?
376 		CM_ERASE : CM_DRAW;
377 	ops->cursor(vc, info, mode, get_color(vc, info, c, 1),
378 		    get_color(vc, info, c, 0));
379 	console_unlock();
380 }
381 
382 static void cursor_timer_handler(struct timer_list *t)
383 {
384 	struct fbcon_ops *ops = from_timer(ops, t, cursor_timer);
385 	struct fb_info *info = ops->info;
386 
387 	queue_work(system_power_efficient_wq, &info->queue);
388 	mod_timer(&ops->cursor_timer, jiffies + ops->cur_blink_jiffies);
389 }
390 
391 static void fbcon_add_cursor_timer(struct fb_info *info)
392 {
393 	struct fbcon_ops *ops = info->fbcon_par;
394 
395 	if ((!info->queue.func || info->queue.func == fb_flashcursor) &&
396 	    !(ops->flags & FBCON_FLAGS_CURSOR_TIMER) &&
397 	    !fbcon_cursor_noblink) {
398 		if (!info->queue.func)
399 			INIT_WORK(&info->queue, fb_flashcursor);
400 
401 		timer_setup(&ops->cursor_timer, cursor_timer_handler, 0);
402 		mod_timer(&ops->cursor_timer, jiffies + ops->cur_blink_jiffies);
403 		ops->flags |= FBCON_FLAGS_CURSOR_TIMER;
404 	}
405 }
406 
407 static void fbcon_del_cursor_timer(struct fb_info *info)
408 {
409 	struct fbcon_ops *ops = info->fbcon_par;
410 
411 	if (info->queue.func == fb_flashcursor &&
412 	    ops->flags & FBCON_FLAGS_CURSOR_TIMER) {
413 		del_timer_sync(&ops->cursor_timer);
414 		ops->flags &= ~FBCON_FLAGS_CURSOR_TIMER;
415 	}
416 }
417 
418 #ifndef MODULE
419 static int __init fb_console_setup(char *this_opt)
420 {
421 	char *options;
422 	int i, j;
423 
424 	if (!this_opt || !*this_opt)
425 		return 1;
426 
427 	while ((options = strsep(&this_opt, ",")) != NULL) {
428 		if (!strncmp(options, "font:", 5)) {
429 			strlcpy(fontname, options + 5, sizeof(fontname));
430 			continue;
431 		}
432 
433 		if (!strncmp(options, "scrollback:", 11)) {
434 			pr_warn("Ignoring scrollback size option\n");
435 			continue;
436 		}
437 
438 		if (!strncmp(options, "map:", 4)) {
439 			options += 4;
440 			if (*options) {
441 				for (i = 0, j = 0; i < MAX_NR_CONSOLES; i++) {
442 					if (!options[j])
443 						j = 0;
444 					con2fb_map_boot[i] =
445 						(options[j++]-'0') % FB_MAX;
446 				}
447 
448 				fbcon_map_override();
449 			}
450 			continue;
451 		}
452 
453 		if (!strncmp(options, "vc:", 3)) {
454 			options += 3;
455 			if (*options)
456 				first_fb_vc = simple_strtoul(options, &options, 10) - 1;
457 			if (first_fb_vc < 0)
458 				first_fb_vc = 0;
459 			if (*options++ == '-')
460 				last_fb_vc = simple_strtoul(options, &options, 10) - 1;
461 			fbcon_is_default = 0;
462 			continue;
463 		}
464 
465 		if (!strncmp(options, "rotate:", 7)) {
466 			options += 7;
467 			if (*options)
468 				initial_rotation = simple_strtoul(options, &options, 0);
469 			if (initial_rotation > 3)
470 				initial_rotation = 0;
471 			continue;
472 		}
473 
474 		if (!strncmp(options, "margin:", 7)) {
475 			options += 7;
476 			if (*options)
477 				margin_color = simple_strtoul(options, &options, 0);
478 			continue;
479 		}
480 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
481 		if (!strcmp(options, "nodefer")) {
482 			deferred_takeover = false;
483 			continue;
484 		}
485 #endif
486 
487 		if (!strncmp(options, "logo-pos:", 9)) {
488 			options += 9;
489 			if (!strcmp(options, "center"))
490 				fb_center_logo = true;
491 			continue;
492 		}
493 
494 		if (!strncmp(options, "logo-count:", 11)) {
495 			options += 11;
496 			if (*options)
497 				fb_logo_count = simple_strtol(options, &options, 0);
498 			continue;
499 		}
500 	}
501 	return 1;
502 }
503 
504 __setup("fbcon=", fb_console_setup);
505 #endif
506 
507 static int search_fb_in_map(int idx)
508 {
509 	int i, retval = 0;
510 
511 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
512 		if (con2fb_map[i] == idx)
513 			retval = 1;
514 	}
515 	return retval;
516 }
517 
518 static int search_for_mapped_con(void)
519 {
520 	int i, retval = 0;
521 
522 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
523 		if (con2fb_map[i] != -1)
524 			retval = 1;
525 	}
526 	return retval;
527 }
528 
529 static int do_fbcon_takeover(int show_logo)
530 {
531 	int err, i;
532 
533 	if (!num_registered_fb)
534 		return -ENODEV;
535 
536 	if (!show_logo)
537 		logo_shown = FBCON_LOGO_DONTSHOW;
538 
539 	for (i = first_fb_vc; i <= last_fb_vc; i++)
540 		con2fb_map[i] = info_idx;
541 
542 	err = do_take_over_console(&fb_con, first_fb_vc, last_fb_vc,
543 				fbcon_is_default);
544 
545 	if (err) {
546 		for (i = first_fb_vc; i <= last_fb_vc; i++)
547 			con2fb_map[i] = -1;
548 		info_idx = -1;
549 	} else {
550 		fbcon_has_console_bind = 1;
551 	}
552 
553 	return err;
554 }
555 
556 #ifdef MODULE
557 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
558 			       int cols, int rows, int new_cols, int new_rows)
559 {
560 	logo_shown = FBCON_LOGO_DONTSHOW;
561 }
562 #else
563 static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
564 			       int cols, int rows, int new_cols, int new_rows)
565 {
566 	/* Need to make room for the logo */
567 	struct fbcon_ops *ops = info->fbcon_par;
568 	int cnt, erase = vc->vc_video_erase_char, step;
569 	unsigned short *save = NULL, *r, *q;
570 	int logo_height;
571 
572 	if (info->fbops->owner) {
573 		logo_shown = FBCON_LOGO_DONTSHOW;
574 		return;
575 	}
576 
577 	/*
578 	 * remove underline attribute from erase character
579 	 * if black and white framebuffer.
580 	 */
581 	if (fb_get_color_depth(&info->var, &info->fix) == 1)
582 		erase &= ~0x400;
583 	logo_height = fb_prepare_logo(info, ops->rotate);
584 	logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
585 	q = (unsigned short *) (vc->vc_origin +
586 				vc->vc_size_row * rows);
587 	step = logo_lines * cols;
588 	for (r = q - logo_lines * cols; r < q; r++)
589 		if (scr_readw(r) != vc->vc_video_erase_char)
590 			break;
591 	if (r != q && new_rows >= rows + logo_lines) {
592 		save = kmalloc(array3_size(logo_lines, new_cols, 2),
593 			       GFP_KERNEL);
594 		if (save) {
595 			int i = cols < new_cols ? cols : new_cols;
596 			scr_memsetw(save, erase, array3_size(logo_lines, new_cols, 2));
597 			r = q - step;
598 			for (cnt = 0; cnt < logo_lines; cnt++, r += i)
599 				scr_memcpyw(save + cnt * new_cols, r, 2 * i);
600 			r = q;
601 		}
602 	}
603 	if (r == q) {
604 		/* We can scroll screen down */
605 		r = q - step - cols;
606 		for (cnt = rows - logo_lines; cnt > 0; cnt--) {
607 			scr_memcpyw(r + step, r, vc->vc_size_row);
608 			r -= cols;
609 		}
610 		if (!save) {
611 			int lines;
612 			if (vc->state.y + logo_lines >= rows)
613 				lines = rows - vc->state.y - 1;
614 			else
615 				lines = logo_lines;
616 			vc->state.y += lines;
617 			vc->vc_pos += lines * vc->vc_size_row;
618 		}
619 	}
620 	scr_memsetw((unsigned short *) vc->vc_origin,
621 		    erase,
622 		    vc->vc_size_row * logo_lines);
623 
624 	if (con_is_visible(vc) && vc->vc_mode == KD_TEXT) {
625 		fbcon_clear_margins(vc, 0);
626 		update_screen(vc);
627 	}
628 
629 	if (save) {
630 		q = (unsigned short *) (vc->vc_origin +
631 					vc->vc_size_row *
632 					rows);
633 		scr_memcpyw(q, save, array3_size(logo_lines, new_cols, 2));
634 		vc->state.y += logo_lines;
635 		vc->vc_pos += logo_lines * vc->vc_size_row;
636 		kfree(save);
637 	}
638 
639 	if (logo_shown == FBCON_LOGO_DONTSHOW)
640 		return;
641 
642 	if (logo_lines > vc->vc_bottom) {
643 		logo_shown = FBCON_LOGO_CANSHOW;
644 		printk(KERN_INFO
645 		       "fbcon_init: disable boot-logo (boot-logo bigger than screen).\n");
646 	} else {
647 		logo_shown = FBCON_LOGO_DRAW;
648 		vc->vc_top = logo_lines;
649 	}
650 }
651 #endif /* MODULE */
652 
653 #ifdef CONFIG_FB_TILEBLITTING
654 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
655 {
656 	struct fbcon_ops *ops = info->fbcon_par;
657 
658 	ops->p = &fb_display[vc->vc_num];
659 
660 	if ((info->flags & FBINFO_MISC_TILEBLITTING))
661 		fbcon_set_tileops(vc, info);
662 	else {
663 		fbcon_set_rotation(info);
664 		fbcon_set_bitops(ops);
665 	}
666 }
667 
668 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
669 {
670 	int err = 0;
671 
672 	if (info->flags & FBINFO_MISC_TILEBLITTING &&
673 	    info->tileops->fb_get_tilemax(info) < charcount)
674 		err = 1;
675 
676 	return err;
677 }
678 #else
679 static void set_blitting_type(struct vc_data *vc, struct fb_info *info)
680 {
681 	struct fbcon_ops *ops = info->fbcon_par;
682 
683 	info->flags &= ~FBINFO_MISC_TILEBLITTING;
684 	ops->p = &fb_display[vc->vc_num];
685 	fbcon_set_rotation(info);
686 	fbcon_set_bitops(ops);
687 }
688 
689 static int fbcon_invalid_charcount(struct fb_info *info, unsigned charcount)
690 {
691 	return 0;
692 }
693 
694 #endif /* CONFIG_MISC_TILEBLITTING */
695 
696 
697 static int con2fb_acquire_newinfo(struct vc_data *vc, struct fb_info *info,
698 				  int unit, int oldidx)
699 {
700 	struct fbcon_ops *ops = NULL;
701 	int err = 0;
702 
703 	if (!try_module_get(info->fbops->owner))
704 		err = -ENODEV;
705 
706 	if (!err && info->fbops->fb_open &&
707 	    info->fbops->fb_open(info, 0))
708 		err = -ENODEV;
709 
710 	if (!err) {
711 		ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
712 		if (!ops)
713 			err = -ENOMEM;
714 	}
715 
716 	if (!err) {
717 		ops->cur_blink_jiffies = HZ / 5;
718 		ops->info = info;
719 		info->fbcon_par = ops;
720 
721 		if (vc)
722 			set_blitting_type(vc, info);
723 	}
724 
725 	if (err) {
726 		con2fb_map[unit] = oldidx;
727 		module_put(info->fbops->owner);
728 	}
729 
730 	return err;
731 }
732 
733 static int con2fb_release_oldinfo(struct vc_data *vc, struct fb_info *oldinfo,
734 				  struct fb_info *newinfo, int unit,
735 				  int oldidx, int found)
736 {
737 	struct fbcon_ops *ops = oldinfo->fbcon_par;
738 	int err = 0, ret;
739 
740 	if (oldinfo->fbops->fb_release &&
741 	    oldinfo->fbops->fb_release(oldinfo, 0)) {
742 		con2fb_map[unit] = oldidx;
743 		if (!found && newinfo->fbops->fb_release)
744 			newinfo->fbops->fb_release(newinfo, 0);
745 		if (!found)
746 			module_put(newinfo->fbops->owner);
747 		err = -ENODEV;
748 	}
749 
750 	if (!err) {
751 		fbcon_del_cursor_timer(oldinfo);
752 		kfree(ops->cursor_state.mask);
753 		kfree(ops->cursor_data);
754 		kfree(ops->cursor_src);
755 		kfree(ops->fontbuffer);
756 		kfree(oldinfo->fbcon_par);
757 		oldinfo->fbcon_par = NULL;
758 		module_put(oldinfo->fbops->owner);
759 		/*
760 		  If oldinfo and newinfo are driving the same hardware,
761 		  the fb_release() method of oldinfo may attempt to
762 		  restore the hardware state.  This will leave the
763 		  newinfo in an undefined state. Thus, a call to
764 		  fb_set_par() may be needed for the newinfo.
765 		*/
766 		if (newinfo && newinfo->fbops->fb_set_par) {
767 			ret = newinfo->fbops->fb_set_par(newinfo);
768 
769 			if (ret)
770 				printk(KERN_ERR "con2fb_release_oldinfo: "
771 					"detected unhandled fb_set_par error, "
772 					"error code %d\n", ret);
773 		}
774 	}
775 
776 	return err;
777 }
778 
779 static void con2fb_init_display(struct vc_data *vc, struct fb_info *info,
780 				int unit, int show_logo)
781 {
782 	struct fbcon_ops *ops = info->fbcon_par;
783 	int ret;
784 
785 	ops->currcon = fg_console;
786 
787 	if (info->fbops->fb_set_par && !(ops->flags & FBCON_FLAGS_INIT)) {
788 		ret = info->fbops->fb_set_par(info);
789 
790 		if (ret)
791 			printk(KERN_ERR "con2fb_init_display: detected "
792 				"unhandled fb_set_par error, "
793 				"error code %d\n", ret);
794 	}
795 
796 	ops->flags |= FBCON_FLAGS_INIT;
797 	ops->graphics = 0;
798 	fbcon_set_disp(info, &info->var, unit);
799 
800 	if (show_logo) {
801 		struct vc_data *fg_vc = vc_cons[fg_console].d;
802 		struct fb_info *fg_info =
803 			registered_fb[con2fb_map[fg_console]];
804 
805 		fbcon_prepare_logo(fg_vc, fg_info, fg_vc->vc_cols,
806 				   fg_vc->vc_rows, fg_vc->vc_cols,
807 				   fg_vc->vc_rows);
808 	}
809 
810 	update_screen(vc_cons[fg_console].d);
811 }
812 
813 /**
814  *	set_con2fb_map - map console to frame buffer device
815  *	@unit: virtual console number to map
816  *	@newidx: frame buffer index to map virtual console to
817  *      @user: user request
818  *
819  *	Maps a virtual console @unit to a frame buffer device
820  *	@newidx.
821  *
822  *	This should be called with the console lock held.
823  */
824 static int set_con2fb_map(int unit, int newidx, int user)
825 {
826 	struct vc_data *vc = vc_cons[unit].d;
827 	int oldidx = con2fb_map[unit];
828 	struct fb_info *info = registered_fb[newidx];
829 	struct fb_info *oldinfo = NULL;
830 	int found, err = 0;
831 
832 	WARN_CONSOLE_UNLOCKED();
833 
834 	if (oldidx == newidx)
835 		return 0;
836 
837 	if (!info)
838 		return -EINVAL;
839 
840 	if (!search_for_mapped_con() || !con_is_bound(&fb_con)) {
841 		info_idx = newidx;
842 		return do_fbcon_takeover(0);
843 	}
844 
845 	if (oldidx != -1)
846 		oldinfo = registered_fb[oldidx];
847 
848 	found = search_fb_in_map(newidx);
849 
850 	con2fb_map[unit] = newidx;
851 	if (!err && !found)
852 		err = con2fb_acquire_newinfo(vc, info, unit, oldidx);
853 
854 	/*
855 	 * If old fb is not mapped to any of the consoles,
856 	 * fbcon should release it.
857 	 */
858 	if (!err && oldinfo && !search_fb_in_map(oldidx))
859 		err = con2fb_release_oldinfo(vc, oldinfo, info, unit, oldidx,
860 					     found);
861 
862 	if (!err) {
863 		int show_logo = (fg_console == 0 && !user &&
864 				 logo_shown != FBCON_LOGO_DONTSHOW);
865 
866 		if (!found)
867 			fbcon_add_cursor_timer(info);
868 		con2fb_map_boot[unit] = newidx;
869 		con2fb_init_display(vc, info, unit, show_logo);
870 	}
871 
872 	if (!search_fb_in_map(info_idx))
873 		info_idx = newidx;
874 
875 	return err;
876 }
877 
878 /*
879  *  Low Level Operations
880  */
881 /* NOTE: fbcon cannot be __init: it may be called from do_take_over_console later */
882 static int var_to_display(struct fbcon_display *disp,
883 			  struct fb_var_screeninfo *var,
884 			  struct fb_info *info)
885 {
886 	disp->xres_virtual = var->xres_virtual;
887 	disp->yres_virtual = var->yres_virtual;
888 	disp->bits_per_pixel = var->bits_per_pixel;
889 	disp->grayscale = var->grayscale;
890 	disp->nonstd = var->nonstd;
891 	disp->accel_flags = var->accel_flags;
892 	disp->height = var->height;
893 	disp->width = var->width;
894 	disp->red = var->red;
895 	disp->green = var->green;
896 	disp->blue = var->blue;
897 	disp->transp = var->transp;
898 	disp->rotate = var->rotate;
899 	disp->mode = fb_match_mode(var, &info->modelist);
900 	if (disp->mode == NULL)
901 		/* This should not happen */
902 		return -EINVAL;
903 	return 0;
904 }
905 
906 static void display_to_var(struct fb_var_screeninfo *var,
907 			   struct fbcon_display *disp)
908 {
909 	fb_videomode_to_var(var, disp->mode);
910 	var->xres_virtual = disp->xres_virtual;
911 	var->yres_virtual = disp->yres_virtual;
912 	var->bits_per_pixel = disp->bits_per_pixel;
913 	var->grayscale = disp->grayscale;
914 	var->nonstd = disp->nonstd;
915 	var->accel_flags = disp->accel_flags;
916 	var->height = disp->height;
917 	var->width = disp->width;
918 	var->red = disp->red;
919 	var->green = disp->green;
920 	var->blue = disp->blue;
921 	var->transp = disp->transp;
922 	var->rotate = disp->rotate;
923 }
924 
925 static const char *fbcon_startup(void)
926 {
927 	const char *display_desc = "frame buffer device";
928 	struct fbcon_display *p = &fb_display[fg_console];
929 	struct vc_data *vc = vc_cons[fg_console].d;
930 	const struct font_desc *font = NULL;
931 	struct module *owner;
932 	struct fb_info *info = NULL;
933 	struct fbcon_ops *ops;
934 	int rows, cols;
935 
936 	/*
937 	 *  If num_registered_fb is zero, this is a call for the dummy part.
938 	 *  The frame buffer devices weren't initialized yet.
939 	 */
940 	if (!num_registered_fb || info_idx == -1)
941 		return display_desc;
942 	/*
943 	 * Instead of blindly using registered_fb[0], we use info_idx, set by
944 	 * fb_console_init();
945 	 */
946 	info = registered_fb[info_idx];
947 	if (!info)
948 		return NULL;
949 
950 	owner = info->fbops->owner;
951 	if (!try_module_get(owner))
952 		return NULL;
953 	if (info->fbops->fb_open && info->fbops->fb_open(info, 0)) {
954 		module_put(owner);
955 		return NULL;
956 	}
957 
958 	ops = kzalloc(sizeof(struct fbcon_ops), GFP_KERNEL);
959 	if (!ops) {
960 		module_put(owner);
961 		return NULL;
962 	}
963 
964 	ops->currcon = -1;
965 	ops->graphics = 1;
966 	ops->cur_rotate = -1;
967 	ops->cur_blink_jiffies = HZ / 5;
968 	ops->info = info;
969 	info->fbcon_par = ops;
970 
971 	p->con_rotate = initial_rotation;
972 	if (p->con_rotate == -1)
973 		p->con_rotate = info->fbcon_rotate_hint;
974 	if (p->con_rotate == -1)
975 		p->con_rotate = FB_ROTATE_UR;
976 
977 	set_blitting_type(vc, info);
978 
979 	/* Setup default font */
980 	if (!p->fontdata && !vc->vc_font.data) {
981 		if (!fontname[0] || !(font = find_font(fontname)))
982 			font = get_default_font(info->var.xres,
983 						info->var.yres,
984 						info->pixmap.blit_x,
985 						info->pixmap.blit_y);
986 		vc->vc_font.width = font->width;
987 		vc->vc_font.height = font->height;
988 		vc->vc_font.data = (void *)(p->fontdata = font->data);
989 		vc->vc_font.charcount = font->charcount;
990 	} else {
991 		p->fontdata = vc->vc_font.data;
992 	}
993 
994 	cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
995 	rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
996 	cols /= vc->vc_font.width;
997 	rows /= vc->vc_font.height;
998 	vc_resize(vc, cols, rows);
999 
1000 	pr_debug("mode:   %s\n", info->fix.id);
1001 	pr_debug("visual: %d\n", info->fix.visual);
1002 	pr_debug("res:    %dx%d-%d\n", info->var.xres,
1003 		 info->var.yres,
1004 		 info->var.bits_per_pixel);
1005 
1006 	fbcon_add_cursor_timer(info);
1007 	return display_desc;
1008 }
1009 
1010 static void fbcon_init(struct vc_data *vc, int init)
1011 {
1012 	struct fb_info *info;
1013 	struct fbcon_ops *ops;
1014 	struct vc_data **default_mode = vc->vc_display_fg;
1015 	struct vc_data *svc = *default_mode;
1016 	struct fbcon_display *t, *p = &fb_display[vc->vc_num];
1017 	int logo = 1, new_rows, new_cols, rows, cols;
1018 	int ret;
1019 
1020 	if (WARN_ON(info_idx == -1))
1021 	    return;
1022 
1023 	if (con2fb_map[vc->vc_num] == -1)
1024 		con2fb_map[vc->vc_num] = info_idx;
1025 
1026 	info = registered_fb[con2fb_map[vc->vc_num]];
1027 
1028 	if (logo_shown < 0 && console_loglevel <= CONSOLE_LOGLEVEL_QUIET)
1029 		logo_shown = FBCON_LOGO_DONTSHOW;
1030 
1031 	if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
1032 	    (info->fix.type == FB_TYPE_TEXT))
1033 		logo = 0;
1034 
1035 	if (var_to_display(p, &info->var, info))
1036 		return;
1037 
1038 	if (!info->fbcon_par)
1039 		con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);
1040 
1041 	/* If we are not the first console on this
1042 	   fb, copy the font from that console */
1043 	t = &fb_display[fg_console];
1044 	if (!p->fontdata) {
1045 		if (t->fontdata) {
1046 			struct vc_data *fvc = vc_cons[fg_console].d;
1047 
1048 			vc->vc_font.data = (void *)(p->fontdata =
1049 						    fvc->vc_font.data);
1050 			vc->vc_font.width = fvc->vc_font.width;
1051 			vc->vc_font.height = fvc->vc_font.height;
1052 			vc->vc_font.charcount = fvc->vc_font.charcount;
1053 			p->userfont = t->userfont;
1054 
1055 			if (p->userfont)
1056 				REFCOUNT(p->fontdata)++;
1057 		} else {
1058 			const struct font_desc *font = NULL;
1059 
1060 			if (!fontname[0] || !(font = find_font(fontname)))
1061 				font = get_default_font(info->var.xres,
1062 							info->var.yres,
1063 							info->pixmap.blit_x,
1064 							info->pixmap.blit_y);
1065 			vc->vc_font.width = font->width;
1066 			vc->vc_font.height = font->height;
1067 			vc->vc_font.data = (void *)(p->fontdata = font->data);
1068 			vc->vc_font.charcount = font->charcount;
1069 		}
1070 	}
1071 
1072 	vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1073 	vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1074 	if (vc->vc_font.charcount == 256) {
1075 		vc->vc_hi_font_mask = 0;
1076 	} else {
1077 		vc->vc_hi_font_mask = 0x100;
1078 		if (vc->vc_can_do_color)
1079 			vc->vc_complement_mask <<= 1;
1080 	}
1081 
1082 	if (!*svc->vc_uni_pagedir_loc)
1083 		con_set_default_unimap(svc);
1084 	if (!*vc->vc_uni_pagedir_loc)
1085 		con_copy_unimap(vc, svc);
1086 
1087 	ops = info->fbcon_par;
1088 	ops->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms);
1089 
1090 	p->con_rotate = initial_rotation;
1091 	if (p->con_rotate == -1)
1092 		p->con_rotate = info->fbcon_rotate_hint;
1093 	if (p->con_rotate == -1)
1094 		p->con_rotate = FB_ROTATE_UR;
1095 
1096 	set_blitting_type(vc, info);
1097 
1098 	cols = vc->vc_cols;
1099 	rows = vc->vc_rows;
1100 	new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1101 	new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1102 	new_cols /= vc->vc_font.width;
1103 	new_rows /= vc->vc_font.height;
1104 
1105 	/*
1106 	 * We must always set the mode. The mode of the previous console
1107 	 * driver could be in the same resolution but we are using different
1108 	 * hardware so we have to initialize the hardware.
1109 	 *
1110 	 * We need to do it in fbcon_init() to prevent screen corruption.
1111 	 */
1112 	if (con_is_visible(vc) && vc->vc_mode == KD_TEXT) {
1113 		if (info->fbops->fb_set_par &&
1114 		    !(ops->flags & FBCON_FLAGS_INIT)) {
1115 			ret = info->fbops->fb_set_par(info);
1116 
1117 			if (ret)
1118 				printk(KERN_ERR "fbcon_init: detected "
1119 					"unhandled fb_set_par error, "
1120 					"error code %d\n", ret);
1121 		}
1122 
1123 		ops->flags |= FBCON_FLAGS_INIT;
1124 	}
1125 
1126 	ops->graphics = 0;
1127 
1128 	/*
1129 	 *  ++guenther: console.c:vc_allocate() relies on initializing
1130 	 *  vc_{cols,rows}, but we must not set those if we are only
1131 	 *  resizing the console.
1132 	 */
1133 	if (init) {
1134 		vc->vc_cols = new_cols;
1135 		vc->vc_rows = new_rows;
1136 	} else
1137 		vc_resize(vc, new_cols, new_rows);
1138 
1139 	if (logo)
1140 		fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);
1141 
1142 	if (ops->rotate_font && ops->rotate_font(info, vc)) {
1143 		ops->rotate = FB_ROTATE_UR;
1144 		set_blitting_type(vc, info);
1145 	}
1146 
1147 	ops->p = &fb_display[fg_console];
1148 }
1149 
1150 static void fbcon_free_font(struct fbcon_display *p, bool freefont)
1151 {
1152 	if (freefont && p->userfont && p->fontdata && (--REFCOUNT(p->fontdata) == 0))
1153 		kfree(p->fontdata - FONT_EXTRA_WORDS * sizeof(int));
1154 	p->fontdata = NULL;
1155 	p->userfont = 0;
1156 }
1157 
1158 static void set_vc_hi_font(struct vc_data *vc, bool set);
1159 
1160 static void fbcon_deinit(struct vc_data *vc)
1161 {
1162 	struct fbcon_display *p = &fb_display[vc->vc_num];
1163 	struct fb_info *info;
1164 	struct fbcon_ops *ops;
1165 	int idx;
1166 	bool free_font = true;
1167 
1168 	idx = con2fb_map[vc->vc_num];
1169 
1170 	if (idx == -1)
1171 		goto finished;
1172 
1173 	info = registered_fb[idx];
1174 
1175 	if (!info)
1176 		goto finished;
1177 
1178 	if (info->flags & FBINFO_MISC_FIRMWARE)
1179 		free_font = false;
1180 	ops = info->fbcon_par;
1181 
1182 	if (!ops)
1183 		goto finished;
1184 
1185 	if (con_is_visible(vc))
1186 		fbcon_del_cursor_timer(info);
1187 
1188 	ops->flags &= ~FBCON_FLAGS_INIT;
1189 finished:
1190 
1191 	fbcon_free_font(p, free_font);
1192 	if (free_font)
1193 		vc->vc_font.data = NULL;
1194 
1195 	if (vc->vc_hi_font_mask && vc->vc_screenbuf)
1196 		set_vc_hi_font(vc, false);
1197 
1198 	if (!con_is_bound(&fb_con))
1199 		fbcon_exit();
1200 
1201 	if (vc->vc_num == logo_shown)
1202 		logo_shown = FBCON_LOGO_CANSHOW;
1203 
1204 	return;
1205 }
1206 
1207 /* ====================================================================== */
1208 
1209 /*  fbcon_XXX routines - interface used by the world
1210  *
1211  *  This system is now divided into two levels because of complications
1212  *  caused by hardware scrolling. Top level functions:
1213  *
1214  *	fbcon_clear(), fbcon_putc(), fbcon_clear_margins()
1215  *
1216  *  handles y values in range [0, scr_height-1] that correspond to real
1217  *  screen positions. y_wrap shift means that first line of bitmap may be
1218  *  anywhere on this display. These functions convert lineoffsets to
1219  *  bitmap offsets and deal with the wrap-around case by splitting blits.
1220  *
1221  *	fbcon_clear_physical_8()    -- of original fbcon_XXX fns.
1222  *	fbcon_putc_physical_8()	    -- (font width != 8) may be added later
1223  *
1224  *  WARNING:
1225  *
1226  *  At the moment fbcon_putc() cannot blit across vertical wrap boundary
1227  *  Implies should only really hardware scroll in rows. Only reason for
1228  *  restriction is simplicity & efficiency at the moment.
1229  */
1230 
1231 static void fbcon_clear(struct vc_data *vc, int sy, int sx, int height,
1232 			int width)
1233 {
1234 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1235 	struct fbcon_ops *ops = info->fbcon_par;
1236 
1237 	struct fbcon_display *p = &fb_display[vc->vc_num];
1238 	u_int y_break;
1239 
1240 	if (fbcon_is_inactive(vc, info))
1241 		return;
1242 
1243 	if (!height || !width)
1244 		return;
1245 
1246 	if (sy < vc->vc_top && vc->vc_top == logo_lines) {
1247 		vc->vc_top = 0;
1248 		/*
1249 		 * If the font dimensions are not an integral of the display
1250 		 * dimensions then the ops->clear below won't end up clearing
1251 		 * the margins.  Call clear_margins here in case the logo
1252 		 * bitmap stretched into the margin area.
1253 		 */
1254 		fbcon_clear_margins(vc, 0);
1255 	}
1256 
1257 	/* Split blits that cross physical y_wrap boundary */
1258 
1259 	y_break = p->vrows - p->yscroll;
1260 	if (sy < y_break && sy + height - 1 >= y_break) {
1261 		u_int b = y_break - sy;
1262 		ops->clear(vc, info, real_y(p, sy), sx, b, width);
1263 		ops->clear(vc, info, real_y(p, sy + b), sx, height - b,
1264 				 width);
1265 	} else
1266 		ops->clear(vc, info, real_y(p, sy), sx, height, width);
1267 }
1268 
1269 static void fbcon_putcs(struct vc_data *vc, const unsigned short *s,
1270 			int count, int ypos, int xpos)
1271 {
1272 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1273 	struct fbcon_display *p = &fb_display[vc->vc_num];
1274 	struct fbcon_ops *ops = info->fbcon_par;
1275 
1276 	if (!fbcon_is_inactive(vc, info))
1277 		ops->putcs(vc, info, s, count, real_y(p, ypos), xpos,
1278 			   get_color(vc, info, scr_readw(s), 1),
1279 			   get_color(vc, info, scr_readw(s), 0));
1280 }
1281 
1282 static void fbcon_putc(struct vc_data *vc, int c, int ypos, int xpos)
1283 {
1284 	unsigned short chr;
1285 
1286 	scr_writew(c, &chr);
1287 	fbcon_putcs(vc, &chr, 1, ypos, xpos);
1288 }
1289 
1290 static void fbcon_clear_margins(struct vc_data *vc, int bottom_only)
1291 {
1292 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1293 	struct fbcon_ops *ops = info->fbcon_par;
1294 
1295 	if (!fbcon_is_inactive(vc, info))
1296 		ops->clear_margins(vc, info, margin_color, bottom_only);
1297 }
1298 
1299 static void fbcon_cursor(struct vc_data *vc, int mode)
1300 {
1301 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1302 	struct fbcon_ops *ops = info->fbcon_par;
1303  	int c = scr_readw((u16 *) vc->vc_pos);
1304 
1305 	ops->cur_blink_jiffies = msecs_to_jiffies(vc->vc_cur_blink_ms);
1306 
1307 	if (fbcon_is_inactive(vc, info) || vc->vc_deccm != 1)
1308 		return;
1309 
1310 	if (vc->vc_cursor_type & CUR_SW)
1311 		fbcon_del_cursor_timer(info);
1312 	else
1313 		fbcon_add_cursor_timer(info);
1314 
1315 	ops->cursor_flash = (mode == CM_ERASE) ? 0 : 1;
1316 
1317 	if (!ops->cursor)
1318 		return;
1319 
1320 	ops->cursor(vc, info, mode, get_color(vc, info, c, 1),
1321 		    get_color(vc, info, c, 0));
1322 }
1323 
1324 static int scrollback_phys_max = 0;
1325 static int scrollback_max = 0;
1326 static int scrollback_current = 0;
1327 
1328 static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
1329 			   int unit)
1330 {
1331 	struct fbcon_display *p, *t;
1332 	struct vc_data **default_mode, *vc;
1333 	struct vc_data *svc;
1334 	struct fbcon_ops *ops = info->fbcon_par;
1335 	int rows, cols;
1336 
1337 	p = &fb_display[unit];
1338 
1339 	if (var_to_display(p, var, info))
1340 		return;
1341 
1342 	vc = vc_cons[unit].d;
1343 
1344 	if (!vc)
1345 		return;
1346 
1347 	default_mode = vc->vc_display_fg;
1348 	svc = *default_mode;
1349 	t = &fb_display[svc->vc_num];
1350 
1351 	if (!vc->vc_font.data) {
1352 		vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
1353 		vc->vc_font.width = (*default_mode)->vc_font.width;
1354 		vc->vc_font.height = (*default_mode)->vc_font.height;
1355 		vc->vc_font.charcount = (*default_mode)->vc_font.charcount;
1356 		p->userfont = t->userfont;
1357 		if (p->userfont)
1358 			REFCOUNT(p->fontdata)++;
1359 	}
1360 
1361 	var->activate = FB_ACTIVATE_NOW;
1362 	info->var.activate = var->activate;
1363 	var->yoffset = info->var.yoffset;
1364 	var->xoffset = info->var.xoffset;
1365 	fb_set_var(info, var);
1366 	ops->var = info->var;
1367 	vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1368 	vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1369 	if (vc->vc_font.charcount == 256) {
1370 		vc->vc_hi_font_mask = 0;
1371 	} else {
1372 		vc->vc_hi_font_mask = 0x100;
1373 		if (vc->vc_can_do_color)
1374 			vc->vc_complement_mask <<= 1;
1375 	}
1376 
1377 	if (!*svc->vc_uni_pagedir_loc)
1378 		con_set_default_unimap(svc);
1379 	if (!*vc->vc_uni_pagedir_loc)
1380 		con_copy_unimap(vc, svc);
1381 
1382 	cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1383 	rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1384 	cols /= vc->vc_font.width;
1385 	rows /= vc->vc_font.height;
1386 	vc_resize(vc, cols, rows);
1387 
1388 	if (con_is_visible(vc)) {
1389 		update_screen(vc);
1390 	}
1391 }
1392 
1393 static void fbcon_redraw(struct vc_data *vc, struct fbcon_display *p,
1394 			 int line, int count, int offset)
1395 {
1396 	unsigned short *d = (unsigned short *)
1397 	    (vc->vc_origin + vc->vc_size_row * line);
1398 	unsigned short *s = d + offset;
1399 
1400 	while (count--) {
1401 		unsigned short *start = s;
1402 		unsigned short *le = advance_row(s, 1);
1403 		unsigned short c;
1404 		int x = 0;
1405 		unsigned short attr = 1;
1406 
1407 		do {
1408 			c = scr_readw(s);
1409 			if (attr != (c & 0xff00)) {
1410 				attr = c & 0xff00;
1411 				if (s > start) {
1412 					fbcon_putcs(vc, start, s - start,
1413 						    line, x);
1414 					x += s - start;
1415 					start = s;
1416 				}
1417 			}
1418 			if (c == scr_readw(d)) {
1419 				if (s > start) {
1420 					fbcon_putcs(vc, start, s - start,
1421 						     line, x);
1422 					x += s - start + 1;
1423 					start = s + 1;
1424 				} else {
1425 					x++;
1426 					start++;
1427 				}
1428 			}
1429 			scr_writew(c, d);
1430 			console_conditional_schedule();
1431 			s++;
1432 			d++;
1433 		} while (s < le);
1434 		if (s > start)
1435 			fbcon_putcs(vc, start, s - start, line, x);
1436 		console_conditional_schedule();
1437 		if (offset > 0)
1438 			line++;
1439 		else {
1440 			line--;
1441 			/* NOTE: We subtract two lines from these pointers */
1442 			s -= vc->vc_size_row;
1443 			d -= vc->vc_size_row;
1444 		}
1445 	}
1446 }
1447 
1448 static bool fbcon_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
1449 		enum con_scroll dir, unsigned int count)
1450 {
1451 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1452 	struct fbcon_display *p = &fb_display[vc->vc_num];
1453 
1454 	if (fbcon_is_inactive(vc, info))
1455 		return true;
1456 
1457 	fbcon_cursor(vc, CM_ERASE);
1458 
1459 	/*
1460 	 * ++Geert: Only use ywrap/ypan if the console is in text mode
1461 	 * ++Andrew: Only use ypan on hardware text mode when scrolling the
1462 	 *           whole screen (prevents flicker).
1463 	 */
1464 
1465 	switch (dir) {
1466 	case SM_UP:
1467 		if (count > vc->vc_rows)	/* Maximum realistic size */
1468 			count = vc->vc_rows;
1469 		fbcon_redraw(vc, p, t, b - t - count,
1470 			     count * vc->vc_cols);
1471 		fbcon_clear(vc, b - count, 0, count, vc->vc_cols);
1472 		scr_memsetw((unsigned short *) (vc->vc_origin +
1473 						vc->vc_size_row *
1474 						(b - count)),
1475 			    vc->vc_video_erase_char,
1476 			    vc->vc_size_row * count);
1477 		return true;
1478 
1479 	case SM_DOWN:
1480 		if (count > vc->vc_rows)	/* Maximum realistic size */
1481 			count = vc->vc_rows;
1482 		fbcon_redraw(vc, p, b - 1, b - t - count,
1483 			     -count * vc->vc_cols);
1484 		fbcon_clear(vc, t, 0, count, vc->vc_cols);
1485 		scr_memsetw((unsigned short *) (vc->vc_origin +
1486 						vc->vc_size_row *
1487 						t),
1488 			    vc->vc_video_erase_char,
1489 			    vc->vc_size_row * count);
1490 		return true;
1491 	}
1492 	return false;
1493 }
1494 
1495 static void updatescrollmode(struct fbcon_display *p,
1496 					struct fb_info *info,
1497 					struct vc_data *vc)
1498 {
1499 	struct fbcon_ops *ops = info->fbcon_par;
1500 	int fh = vc->vc_font.height;
1501 	int yres = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1502 	int vyres = FBCON_SWAP(ops->rotate, info->var.yres_virtual,
1503 				   info->var.xres_virtual);
1504 
1505 	p->vrows = vyres/fh;
1506 	if (yres > (fh * (vc->vc_rows + 1)))
1507 		p->vrows -= (yres - (fh * vc->vc_rows)) / fh;
1508 	if ((yres % fh) && (vyres % fh < yres % fh))
1509 		p->vrows--;
1510 }
1511 
1512 #define PITCH(w) (((w) + 7) >> 3)
1513 #define CALC_FONTSZ(h, p, c) ((h) * (p) * (c)) /* size = height * pitch * charcount */
1514 
1515 static int fbcon_resize(struct vc_data *vc, unsigned int width,
1516 			unsigned int height, unsigned int user)
1517 {
1518 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1519 	struct fbcon_ops *ops = info->fbcon_par;
1520 	struct fbcon_display *p = &fb_display[vc->vc_num];
1521 	struct fb_var_screeninfo var = info->var;
1522 	int x_diff, y_diff, virt_w, virt_h, virt_fw, virt_fh;
1523 
1524 	if (p->userfont && FNTSIZE(vc->vc_font.data)) {
1525 		int size;
1526 		int pitch = PITCH(vc->vc_font.width);
1527 
1528 		/*
1529 		 * If user font, ensure that a possible change to user font
1530 		 * height or width will not allow a font data out-of-bounds access.
1531 		 * NOTE: must use original charcount in calculation as font
1532 		 * charcount can change and cannot be used to determine the
1533 		 * font data allocated size.
1534 		 */
1535 		if (pitch <= 0)
1536 			return -EINVAL;
1537 		size = CALC_FONTSZ(vc->vc_font.height, pitch, vc->vc_font.charcount);
1538 		if (size > FNTSIZE(vc->vc_font.data))
1539 			return -EINVAL;
1540 	}
1541 
1542 	virt_w = FBCON_SWAP(ops->rotate, width, height);
1543 	virt_h = FBCON_SWAP(ops->rotate, height, width);
1544 	virt_fw = FBCON_SWAP(ops->rotate, vc->vc_font.width,
1545 				 vc->vc_font.height);
1546 	virt_fh = FBCON_SWAP(ops->rotate, vc->vc_font.height,
1547 				 vc->vc_font.width);
1548 	var.xres = virt_w * virt_fw;
1549 	var.yres = virt_h * virt_fh;
1550 	x_diff = info->var.xres - var.xres;
1551 	y_diff = info->var.yres - var.yres;
1552 	if (x_diff < 0 || x_diff > virt_fw ||
1553 	    y_diff < 0 || y_diff > virt_fh) {
1554 		const struct fb_videomode *mode;
1555 
1556 		pr_debug("attempting resize %ix%i\n", var.xres, var.yres);
1557 		mode = fb_find_best_mode(&var, &info->modelist);
1558 		if (mode == NULL)
1559 			return -EINVAL;
1560 		display_to_var(&var, p);
1561 		fb_videomode_to_var(&var, mode);
1562 
1563 		if (virt_w > var.xres/virt_fw || virt_h > var.yres/virt_fh)
1564 			return -EINVAL;
1565 
1566 		pr_debug("resize now %ix%i\n", var.xres, var.yres);
1567 		if (con_is_visible(vc) && vc->vc_mode == KD_TEXT) {
1568 			var.activate = FB_ACTIVATE_NOW |
1569 				FB_ACTIVATE_FORCE;
1570 			fb_set_var(info, &var);
1571 		}
1572 		var_to_display(p, &info->var, info);
1573 		ops->var = info->var;
1574 	}
1575 	updatescrollmode(p, info, vc);
1576 	return 0;
1577 }
1578 
1579 static int fbcon_switch(struct vc_data *vc)
1580 {
1581 	struct fb_info *info, *old_info = NULL;
1582 	struct fbcon_ops *ops;
1583 	struct fbcon_display *p = &fb_display[vc->vc_num];
1584 	struct fb_var_screeninfo var;
1585 	int i, ret, prev_console;
1586 
1587 	info = registered_fb[con2fb_map[vc->vc_num]];
1588 	ops = info->fbcon_par;
1589 
1590 	if (logo_shown >= 0) {
1591 		struct vc_data *conp2 = vc_cons[logo_shown].d;
1592 
1593 		if (conp2->vc_top == logo_lines
1594 		    && conp2->vc_bottom == conp2->vc_rows)
1595 			conp2->vc_top = 0;
1596 		logo_shown = FBCON_LOGO_CANSHOW;
1597 	}
1598 
1599 	prev_console = ops->currcon;
1600 	if (prev_console != -1)
1601 		old_info = registered_fb[con2fb_map[prev_console]];
1602 	/*
1603 	 * FIXME: If we have multiple fbdev's loaded, we need to
1604 	 * update all info->currcon.  Perhaps, we can place this
1605 	 * in a centralized structure, but this might break some
1606 	 * drivers.
1607 	 *
1608 	 * info->currcon = vc->vc_num;
1609 	 */
1610 	for_each_registered_fb(i) {
1611 		if (registered_fb[i]->fbcon_par) {
1612 			struct fbcon_ops *o = registered_fb[i]->fbcon_par;
1613 
1614 			o->currcon = vc->vc_num;
1615 		}
1616 	}
1617 	memset(&var, 0, sizeof(struct fb_var_screeninfo));
1618 	display_to_var(&var, p);
1619 	var.activate = FB_ACTIVATE_NOW;
1620 
1621 	/*
1622 	 * make sure we don't unnecessarily trip the memcmp()
1623 	 * in fb_set_var()
1624 	 */
1625 	info->var.activate = var.activate;
1626 	var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
1627 	fb_set_var(info, &var);
1628 	ops->var = info->var;
1629 
1630 	if (old_info != NULL && (old_info != info ||
1631 				 info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
1632 		if (info->fbops->fb_set_par) {
1633 			ret = info->fbops->fb_set_par(info);
1634 
1635 			if (ret)
1636 				printk(KERN_ERR "fbcon_switch: detected "
1637 					"unhandled fb_set_par error, "
1638 					"error code %d\n", ret);
1639 		}
1640 
1641 		if (old_info != info)
1642 			fbcon_del_cursor_timer(old_info);
1643 	}
1644 
1645 	if (fbcon_is_inactive(vc, info) ||
1646 	    ops->blank_state != FB_BLANK_UNBLANK)
1647 		fbcon_del_cursor_timer(info);
1648 	else
1649 		fbcon_add_cursor_timer(info);
1650 
1651 	set_blitting_type(vc, info);
1652 	ops->cursor_reset = 1;
1653 
1654 	if (ops->rotate_font && ops->rotate_font(info, vc)) {
1655 		ops->rotate = FB_ROTATE_UR;
1656 		set_blitting_type(vc, info);
1657 	}
1658 
1659 	vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
1660 	vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1661 
1662 	if (vc->vc_font.charcount > 256)
1663 		vc->vc_complement_mask <<= 1;
1664 
1665 	updatescrollmode(p, info, vc);
1666 
1667 	scrollback_phys_max = 0;
1668 	scrollback_max = 0;
1669 	scrollback_current = 0;
1670 
1671 	if (!fbcon_is_inactive(vc, info)) {
1672 	    ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
1673 	    ops->update_start(info);
1674 	}
1675 
1676 	fbcon_set_palette(vc, color_table);
1677 	fbcon_clear_margins(vc, 0);
1678 
1679 	if (logo_shown == FBCON_LOGO_DRAW) {
1680 
1681 		logo_shown = fg_console;
1682 		/* This is protected above by initmem_freed */
1683 		fb_show_logo(info, ops->rotate);
1684 		update_region(vc,
1685 			      vc->vc_origin + vc->vc_size_row * vc->vc_top,
1686 			      vc->vc_size_row * (vc->vc_bottom -
1687 						 vc->vc_top) / 2);
1688 		return 0;
1689 	}
1690 	return 1;
1691 }
1692 
1693 static void fbcon_generic_blank(struct vc_data *vc, struct fb_info *info,
1694 				int blank)
1695 {
1696 	if (blank) {
1697 		unsigned short charmask = vc->vc_hi_font_mask ?
1698 			0x1ff : 0xff;
1699 		unsigned short oldc;
1700 
1701 		oldc = vc->vc_video_erase_char;
1702 		vc->vc_video_erase_char &= charmask;
1703 		fbcon_clear(vc, 0, 0, vc->vc_rows, vc->vc_cols);
1704 		vc->vc_video_erase_char = oldc;
1705 	}
1706 }
1707 
1708 static int fbcon_blank(struct vc_data *vc, int blank, int mode_switch)
1709 {
1710 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1711 	struct fbcon_ops *ops = info->fbcon_par;
1712 
1713 	if (mode_switch) {
1714 		struct fb_var_screeninfo var = info->var;
1715 
1716 		ops->graphics = 1;
1717 
1718 		if (!blank) {
1719 			var.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE |
1720 				FB_ACTIVATE_KD_TEXT;
1721 			fb_set_var(info, &var);
1722 			ops->graphics = 0;
1723 			ops->var = info->var;
1724 		}
1725 	}
1726 
1727  	if (!fbcon_is_inactive(vc, info)) {
1728 		if (ops->blank_state != blank) {
1729 			ops->blank_state = blank;
1730 			fbcon_cursor(vc, blank ? CM_ERASE : CM_DRAW);
1731 			ops->cursor_flash = (!blank);
1732 
1733 			if (fb_blank(info, blank))
1734 				fbcon_generic_blank(vc, info, blank);
1735 		}
1736 
1737 		if (!blank)
1738 			update_screen(vc);
1739 	}
1740 
1741 	if (mode_switch || fbcon_is_inactive(vc, info) ||
1742 	    ops->blank_state != FB_BLANK_UNBLANK)
1743 		fbcon_del_cursor_timer(info);
1744 	else
1745 		fbcon_add_cursor_timer(info);
1746 
1747 	return 0;
1748 }
1749 
1750 static int fbcon_debug_enter(struct vc_data *vc)
1751 {
1752 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1753 	struct fbcon_ops *ops = info->fbcon_par;
1754 
1755 	ops->save_graphics = ops->graphics;
1756 	ops->graphics = 0;
1757 	if (info->fbops->fb_debug_enter)
1758 		info->fbops->fb_debug_enter(info);
1759 	fbcon_set_palette(vc, color_table);
1760 	return 0;
1761 }
1762 
1763 static int fbcon_debug_leave(struct vc_data *vc)
1764 {
1765 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1766 	struct fbcon_ops *ops = info->fbcon_par;
1767 
1768 	ops->graphics = ops->save_graphics;
1769 	if (info->fbops->fb_debug_leave)
1770 		info->fbops->fb_debug_leave(info);
1771 	return 0;
1772 }
1773 
1774 static int fbcon_get_font(struct vc_data *vc, struct console_font *font)
1775 {
1776 	u8 *fontdata = vc->vc_font.data;
1777 	u8 *data = font->data;
1778 	int i, j;
1779 
1780 	font->width = vc->vc_font.width;
1781 	font->height = vc->vc_font.height;
1782 	font->charcount = vc->vc_hi_font_mask ? 512 : 256;
1783 	if (!font->data)
1784 		return 0;
1785 
1786 	if (font->width <= 8) {
1787 		j = vc->vc_font.height;
1788 		if (font->charcount * j > FNTSIZE(fontdata))
1789 			return -EINVAL;
1790 
1791 		for (i = 0; i < font->charcount; i++) {
1792 			memcpy(data, fontdata, j);
1793 			memset(data + j, 0, 32 - j);
1794 			data += 32;
1795 			fontdata += j;
1796 		}
1797 	} else if (font->width <= 16) {
1798 		j = vc->vc_font.height * 2;
1799 		if (font->charcount * j > FNTSIZE(fontdata))
1800 			return -EINVAL;
1801 
1802 		for (i = 0; i < font->charcount; i++) {
1803 			memcpy(data, fontdata, j);
1804 			memset(data + j, 0, 64 - j);
1805 			data += 64;
1806 			fontdata += j;
1807 		}
1808 	} else if (font->width <= 24) {
1809 		if (font->charcount * (vc->vc_font.height * sizeof(u32)) > FNTSIZE(fontdata))
1810 			return -EINVAL;
1811 
1812 		for (i = 0; i < font->charcount; i++) {
1813 			for (j = 0; j < vc->vc_font.height; j++) {
1814 				*data++ = fontdata[0];
1815 				*data++ = fontdata[1];
1816 				*data++ = fontdata[2];
1817 				fontdata += sizeof(u32);
1818 			}
1819 			memset(data, 0, 3 * (32 - j));
1820 			data += 3 * (32 - j);
1821 		}
1822 	} else {
1823 		j = vc->vc_font.height * 4;
1824 		if (font->charcount * j > FNTSIZE(fontdata))
1825 			return -EINVAL;
1826 
1827 		for (i = 0; i < font->charcount; i++) {
1828 			memcpy(data, fontdata, j);
1829 			memset(data + j, 0, 128 - j);
1830 			data += 128;
1831 			fontdata += j;
1832 		}
1833 	}
1834 	return 0;
1835 }
1836 
1837 /* set/clear vc_hi_font_mask and update vc attrs accordingly */
1838 static void set_vc_hi_font(struct vc_data *vc, bool set)
1839 {
1840 	if (!set) {
1841 		vc->vc_hi_font_mask = 0;
1842 		if (vc->vc_can_do_color) {
1843 			vc->vc_complement_mask >>= 1;
1844 			vc->vc_s_complement_mask >>= 1;
1845 		}
1846 
1847 		/* ++Edmund: reorder the attribute bits */
1848 		if (vc->vc_can_do_color) {
1849 			unsigned short *cp =
1850 			    (unsigned short *) vc->vc_origin;
1851 			int count = vc->vc_screenbuf_size / 2;
1852 			unsigned short c;
1853 			for (; count > 0; count--, cp++) {
1854 				c = scr_readw(cp);
1855 				scr_writew(((c & 0xfe00) >> 1) |
1856 					   (c & 0xff), cp);
1857 			}
1858 			c = vc->vc_video_erase_char;
1859 			vc->vc_video_erase_char =
1860 			    ((c & 0xfe00) >> 1) | (c & 0xff);
1861 			vc->vc_attr >>= 1;
1862 		}
1863 	} else {
1864 		vc->vc_hi_font_mask = 0x100;
1865 		if (vc->vc_can_do_color) {
1866 			vc->vc_complement_mask <<= 1;
1867 			vc->vc_s_complement_mask <<= 1;
1868 		}
1869 
1870 		/* ++Edmund: reorder the attribute bits */
1871 		{
1872 			unsigned short *cp =
1873 			    (unsigned short *) vc->vc_origin;
1874 			int count = vc->vc_screenbuf_size / 2;
1875 			unsigned short c;
1876 			for (; count > 0; count--, cp++) {
1877 				unsigned short newc;
1878 				c = scr_readw(cp);
1879 				if (vc->vc_can_do_color)
1880 					newc =
1881 					    ((c & 0xff00) << 1) | (c &
1882 								   0xff);
1883 				else
1884 					newc = c & ~0x100;
1885 				scr_writew(newc, cp);
1886 			}
1887 			c = vc->vc_video_erase_char;
1888 			if (vc->vc_can_do_color) {
1889 				vc->vc_video_erase_char =
1890 				    ((c & 0xff00) << 1) | (c & 0xff);
1891 				vc->vc_attr <<= 1;
1892 			} else
1893 				vc->vc_video_erase_char = c & ~0x100;
1894 		}
1895 	}
1896 }
1897 
1898 static int fbcon_do_set_font(struct vc_data *vc, int w, int h, int charcount,
1899 			     const u8 * data, int userfont)
1900 {
1901 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1902 	struct fbcon_ops *ops = info->fbcon_par;
1903 	struct fbcon_display *p = &fb_display[vc->vc_num];
1904 	int resize;
1905 	char *old_data = NULL;
1906 
1907 	resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
1908 	if (p->userfont)
1909 		old_data = vc->vc_font.data;
1910 	vc->vc_font.data = (void *)(p->fontdata = data);
1911 	if ((p->userfont = userfont))
1912 		REFCOUNT(data)++;
1913 	vc->vc_font.width = w;
1914 	vc->vc_font.height = h;
1915 	vc->vc_font.charcount = charcount;
1916 	if (vc->vc_hi_font_mask && charcount == 256)
1917 		set_vc_hi_font(vc, false);
1918 	else if (!vc->vc_hi_font_mask && charcount == 512)
1919 		set_vc_hi_font(vc, true);
1920 
1921 	if (resize) {
1922 		int cols, rows;
1923 
1924 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
1925 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
1926 		cols /= w;
1927 		rows /= h;
1928 		vc_resize(vc, cols, rows);
1929 	} else if (con_is_visible(vc)
1930 		   && vc->vc_mode == KD_TEXT) {
1931 		fbcon_clear_margins(vc, 0);
1932 		update_screen(vc);
1933 	}
1934 
1935 	if (old_data && (--REFCOUNT(old_data) == 0))
1936 		kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
1937 	return 0;
1938 }
1939 
1940 /*
1941  *  User asked to set font; we are guaranteed that
1942  *	a) width and height are in range 1..32
1943  *	b) charcount does not exceed 512
1944  *  but lets not assume that, since someone might someday want to use larger
1945  *  fonts. And charcount of 512 is small for unicode support.
1946  *
1947  *  However, user space gives the font in 32 rows , regardless of
1948  *  actual font height. So a new API is needed if support for larger fonts
1949  *  is ever implemented.
1950  */
1951 
1952 static int fbcon_set_font(struct vc_data *vc, struct console_font *font,
1953 			  unsigned int flags)
1954 {
1955 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
1956 	unsigned charcount = font->charcount;
1957 	int w = font->width;
1958 	int h = font->height;
1959 	int size;
1960 	int i, csum;
1961 	u8 *new_data, *data = font->data;
1962 	int pitch = PITCH(font->width);
1963 
1964 	/* Is there a reason why fbconsole couldn't handle any charcount >256?
1965 	 * If not this check should be changed to charcount < 256 */
1966 	if (charcount != 256 && charcount != 512)
1967 		return -EINVAL;
1968 
1969 	/* Make sure drawing engine can handle the font */
1970 	if (!(info->pixmap.blit_x & (1 << (font->width - 1))) ||
1971 	    !(info->pixmap.blit_y & (1 << (font->height - 1))))
1972 		return -EINVAL;
1973 
1974 	/* Make sure driver can handle the font length */
1975 	if (fbcon_invalid_charcount(info, charcount))
1976 		return -EINVAL;
1977 
1978 	size = CALC_FONTSZ(h, pitch, charcount);
1979 
1980 	new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
1981 
1982 	if (!new_data)
1983 		return -ENOMEM;
1984 
1985 	memset(new_data, 0, FONT_EXTRA_WORDS * sizeof(int));
1986 
1987 	new_data += FONT_EXTRA_WORDS * sizeof(int);
1988 	FNTSIZE(new_data) = size;
1989 	REFCOUNT(new_data) = 0;	/* usage counter */
1990 	for (i=0; i< charcount; i++) {
1991 		memcpy(new_data + i*h*pitch, data +  i*32*pitch, h*pitch);
1992 	}
1993 
1994 	/* Since linux has a nice crc32 function use it for counting font
1995 	 * checksums. */
1996 	csum = crc32(0, new_data, size);
1997 
1998 	FNTSUM(new_data) = csum;
1999 	/* Check if the same font is on some other console already */
2000 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2001 		struct vc_data *tmp = vc_cons[i].d;
2002 
2003 		if (fb_display[i].userfont &&
2004 		    fb_display[i].fontdata &&
2005 		    FNTSUM(fb_display[i].fontdata) == csum &&
2006 		    FNTSIZE(fb_display[i].fontdata) == size &&
2007 		    tmp->vc_font.width == w &&
2008 		    !memcmp(fb_display[i].fontdata, new_data, size)) {
2009 			kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2010 			new_data = (u8 *)fb_display[i].fontdata;
2011 			break;
2012 		}
2013 	}
2014 	return fbcon_do_set_font(vc, font->width, font->height, charcount, new_data, 1);
2015 }
2016 
2017 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2018 {
2019 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2020 	const struct font_desc *f;
2021 
2022 	if (!name)
2023 		f = get_default_font(info->var.xres, info->var.yres,
2024 				     info->pixmap.blit_x, info->pixmap.blit_y);
2025 	else if (!(f = find_font(name)))
2026 		return -ENOENT;
2027 
2028 	font->width = f->width;
2029 	font->height = f->height;
2030 	return fbcon_do_set_font(vc, f->width, f->height, f->charcount, f->data, 0);
2031 }
2032 
2033 static u16 palette_red[16];
2034 static u16 palette_green[16];
2035 static u16 palette_blue[16];
2036 
2037 static struct fb_cmap palette_cmap = {
2038 	0, 16, palette_red, palette_green, palette_blue, NULL
2039 };
2040 
2041 static void fbcon_set_palette(struct vc_data *vc, const unsigned char *table)
2042 {
2043 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2044 	int i, j, k, depth;
2045 	u8 val;
2046 
2047 	if (fbcon_is_inactive(vc, info))
2048 		return;
2049 
2050 	if (!con_is_visible(vc))
2051 		return;
2052 
2053 	depth = fb_get_color_depth(&info->var, &info->fix);
2054 	if (depth > 3) {
2055 		for (i = j = 0; i < 16; i++) {
2056 			k = table[i];
2057 			val = vc->vc_palette[j++];
2058 			palette_red[k] = (val << 8) | val;
2059 			val = vc->vc_palette[j++];
2060 			palette_green[k] = (val << 8) | val;
2061 			val = vc->vc_palette[j++];
2062 			palette_blue[k] = (val << 8) | val;
2063 		}
2064 		palette_cmap.len = 16;
2065 		palette_cmap.start = 0;
2066 	/*
2067 	 * If framebuffer is capable of less than 16 colors,
2068 	 * use default palette of fbcon.
2069 	 */
2070 	} else
2071 		fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2072 
2073 	fb_set_cmap(&palette_cmap, info);
2074 }
2075 
2076 static u16 *fbcon_screen_pos(const struct vc_data *vc, int offset)
2077 {
2078 	return (u16 *) (vc->vc_origin + offset);
2079 }
2080 
2081 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2082 				 int *px, int *py)
2083 {
2084 	unsigned long ret;
2085 	int x, y;
2086 
2087 	if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2088 		unsigned long offset = (pos - vc->vc_origin) / 2;
2089 
2090 		x = offset % vc->vc_cols;
2091 		y = offset / vc->vc_cols;
2092 		ret = pos + (vc->vc_cols - x) * 2;
2093 	} else {
2094 		/* Should not happen */
2095 		x = y = 0;
2096 		ret = vc->vc_origin;
2097 	}
2098 	if (px)
2099 		*px = x;
2100 	if (py)
2101 		*py = y;
2102 	return ret;
2103 }
2104 
2105 /* As we might be inside of softback, we may work with non-contiguous buffer,
2106    that's why we have to use a separate routine. */
2107 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2108 {
2109 	while (cnt--) {
2110 		u16 a = scr_readw(p);
2111 		if (!vc->vc_can_do_color)
2112 			a ^= 0x0800;
2113 		else if (vc->vc_hi_font_mask == 0x100)
2114 			a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2115 			    (((a) & 0x0e00) << 4);
2116 		else
2117 			a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2118 			    (((a) & 0x0700) << 4);
2119 		scr_writew(a, p++);
2120 	}
2121 }
2122 
2123 void fbcon_suspended(struct fb_info *info)
2124 {
2125 	struct vc_data *vc = NULL;
2126 	struct fbcon_ops *ops = info->fbcon_par;
2127 
2128 	if (!ops || ops->currcon < 0)
2129 		return;
2130 	vc = vc_cons[ops->currcon].d;
2131 
2132 	/* Clear cursor, restore saved data */
2133 	fbcon_cursor(vc, CM_ERASE);
2134 }
2135 
2136 void fbcon_resumed(struct fb_info *info)
2137 {
2138 	struct vc_data *vc;
2139 	struct fbcon_ops *ops = info->fbcon_par;
2140 
2141 	if (!ops || ops->currcon < 0)
2142 		return;
2143 	vc = vc_cons[ops->currcon].d;
2144 
2145 	update_screen(vc);
2146 }
2147 
2148 static void fbcon_modechanged(struct fb_info *info)
2149 {
2150 	struct fbcon_ops *ops = info->fbcon_par;
2151 	struct vc_data *vc;
2152 	struct fbcon_display *p;
2153 	int rows, cols;
2154 
2155 	if (!ops || ops->currcon < 0)
2156 		return;
2157 	vc = vc_cons[ops->currcon].d;
2158 	if (vc->vc_mode != KD_TEXT ||
2159 	    registered_fb[con2fb_map[ops->currcon]] != info)
2160 		return;
2161 
2162 	p = &fb_display[vc->vc_num];
2163 	set_blitting_type(vc, info);
2164 
2165 	if (con_is_visible(vc)) {
2166 		var_to_display(p, &info->var, info);
2167 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2168 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2169 		cols /= vc->vc_font.width;
2170 		rows /= vc->vc_font.height;
2171 		vc_resize(vc, cols, rows);
2172 		updatescrollmode(p, info, vc);
2173 		scrollback_max = 0;
2174 		scrollback_current = 0;
2175 
2176 		if (!fbcon_is_inactive(vc, info)) {
2177 		    ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2178 		    ops->update_start(info);
2179 		}
2180 
2181 		fbcon_set_palette(vc, color_table);
2182 		update_screen(vc);
2183 	}
2184 }
2185 
2186 static void fbcon_set_all_vcs(struct fb_info *info)
2187 {
2188 	struct fbcon_ops *ops = info->fbcon_par;
2189 	struct vc_data *vc;
2190 	struct fbcon_display *p;
2191 	int i, rows, cols, fg = -1;
2192 
2193 	if (!ops || ops->currcon < 0)
2194 		return;
2195 
2196 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2197 		vc = vc_cons[i].d;
2198 		if (!vc || vc->vc_mode != KD_TEXT ||
2199 		    registered_fb[con2fb_map[i]] != info)
2200 			continue;
2201 
2202 		if (con_is_visible(vc)) {
2203 			fg = i;
2204 			continue;
2205 		}
2206 
2207 		p = &fb_display[vc->vc_num];
2208 		set_blitting_type(vc, info);
2209 		var_to_display(p, &info->var, info);
2210 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2211 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2212 		cols /= vc->vc_font.width;
2213 		rows /= vc->vc_font.height;
2214 		vc_resize(vc, cols, rows);
2215 	}
2216 
2217 	if (fg != -1)
2218 		fbcon_modechanged(info);
2219 }
2220 
2221 
2222 void fbcon_update_vcs(struct fb_info *info, bool all)
2223 {
2224 	if (all)
2225 		fbcon_set_all_vcs(info);
2226 	else
2227 		fbcon_modechanged(info);
2228 }
2229 EXPORT_SYMBOL(fbcon_update_vcs);
2230 
2231 int fbcon_mode_deleted(struct fb_info *info,
2232 		       struct fb_videomode *mode)
2233 {
2234 	struct fb_info *fb_info;
2235 	struct fbcon_display *p;
2236 	int i, j, found = 0;
2237 
2238 	/* before deletion, ensure that mode is not in use */
2239 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2240 		j = con2fb_map[i];
2241 		if (j == -1)
2242 			continue;
2243 		fb_info = registered_fb[j];
2244 		if (fb_info != info)
2245 			continue;
2246 		p = &fb_display[i];
2247 		if (!p || !p->mode)
2248 			continue;
2249 		if (fb_mode_is_equal(p->mode, mode)) {
2250 			found = 1;
2251 			break;
2252 		}
2253 	}
2254 	return found;
2255 }
2256 
2257 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
2258 static void fbcon_unbind(void)
2259 {
2260 	int ret;
2261 
2262 	ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
2263 				fbcon_is_default);
2264 
2265 	if (!ret)
2266 		fbcon_has_console_bind = 0;
2267 }
2268 #else
2269 static inline void fbcon_unbind(void) {}
2270 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
2271 
2272 /* called with console_lock held */
2273 void fbcon_fb_unbind(struct fb_info *info)
2274 {
2275 	int i, new_idx = -1, ret = 0;
2276 	int idx = info->node;
2277 
2278 	WARN_CONSOLE_UNLOCKED();
2279 
2280 	if (!fbcon_has_console_bind)
2281 		return;
2282 
2283 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2284 		if (con2fb_map[i] != idx &&
2285 		    con2fb_map[i] != -1) {
2286 			new_idx = con2fb_map[i];
2287 			break;
2288 		}
2289 	}
2290 
2291 	if (new_idx != -1) {
2292 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2293 			if (con2fb_map[i] == idx)
2294 				set_con2fb_map(i, new_idx, 0);
2295 		}
2296 	} else {
2297 		struct fb_info *info = registered_fb[idx];
2298 
2299 		/* This is sort of like set_con2fb_map, except it maps
2300 		 * the consoles to no device and then releases the
2301 		 * oldinfo to free memory and cancel the cursor blink
2302 		 * timer. I can imagine this just becoming part of
2303 		 * set_con2fb_map where new_idx is -1
2304 		 */
2305 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2306 			if (con2fb_map[i] == idx) {
2307 				con2fb_map[i] = -1;
2308 				if (!search_fb_in_map(idx)) {
2309 					ret = con2fb_release_oldinfo(vc_cons[i].d,
2310 								     info, NULL, i,
2311 								     idx, 0);
2312 					if (ret) {
2313 						con2fb_map[i] = idx;
2314 						return;
2315 					}
2316 				}
2317 			}
2318 		}
2319 		fbcon_unbind();
2320 	}
2321 }
2322 
2323 /* called with console_lock held */
2324 void fbcon_fb_unregistered(struct fb_info *info)
2325 {
2326 	int i, idx;
2327 
2328 	WARN_CONSOLE_UNLOCKED();
2329 
2330 	if (deferred_takeover)
2331 		return;
2332 
2333 	idx = info->node;
2334 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2335 		if (con2fb_map[i] == idx)
2336 			con2fb_map[i] = -1;
2337 	}
2338 
2339 	if (idx == info_idx) {
2340 		info_idx = -1;
2341 
2342 		for_each_registered_fb(i) {
2343 			info_idx = i;
2344 			break;
2345 		}
2346 	}
2347 
2348 	if (info_idx != -1) {
2349 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2350 			if (con2fb_map[i] == -1)
2351 				con2fb_map[i] = info_idx;
2352 		}
2353 	}
2354 
2355 	if (primary_device == idx)
2356 		primary_device = -1;
2357 
2358 	if (!num_registered_fb)
2359 		do_unregister_con_driver(&fb_con);
2360 }
2361 
2362 void fbcon_remap_all(struct fb_info *info)
2363 {
2364 	int i, idx = info->node;
2365 
2366 	console_lock();
2367 	if (deferred_takeover) {
2368 		for (i = first_fb_vc; i <= last_fb_vc; i++)
2369 			con2fb_map_boot[i] = idx;
2370 		fbcon_map_override();
2371 		console_unlock();
2372 		return;
2373 	}
2374 
2375 	for (i = first_fb_vc; i <= last_fb_vc; i++)
2376 		set_con2fb_map(i, idx, 0);
2377 
2378 	if (con_is_bound(&fb_con)) {
2379 		printk(KERN_INFO "fbcon: Remapping primary device, "
2380 		       "fb%i, to tty %i-%i\n", idx,
2381 		       first_fb_vc + 1, last_fb_vc + 1);
2382 		info_idx = idx;
2383 	}
2384 	console_unlock();
2385 }
2386 
2387 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
2388 static void fbcon_select_primary(struct fb_info *info)
2389 {
2390 	if (!map_override && primary_device == -1 &&
2391 	    fb_is_primary_device(info)) {
2392 		int i;
2393 
2394 		printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
2395 		       info->fix.id, info->node);
2396 		primary_device = info->node;
2397 
2398 		for (i = first_fb_vc; i <= last_fb_vc; i++)
2399 			con2fb_map_boot[i] = primary_device;
2400 
2401 		if (con_is_bound(&fb_con)) {
2402 			printk(KERN_INFO "fbcon: Remapping primary device, "
2403 			       "fb%i, to tty %i-%i\n", info->node,
2404 			       first_fb_vc + 1, last_fb_vc + 1);
2405 			info_idx = primary_device;
2406 		}
2407 	}
2408 
2409 }
2410 #else
2411 static inline void fbcon_select_primary(struct fb_info *info)
2412 {
2413 	return;
2414 }
2415 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
2416 
2417 /* called with console_lock held */
2418 int fbcon_fb_registered(struct fb_info *info)
2419 {
2420 	int ret = 0, i, idx;
2421 
2422 	WARN_CONSOLE_UNLOCKED();
2423 
2424 	idx = info->node;
2425 	fbcon_select_primary(info);
2426 
2427 	if (deferred_takeover) {
2428 		pr_info("fbcon: Deferring console take-over\n");
2429 		return 0;
2430 	}
2431 
2432 	if (info_idx == -1) {
2433 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2434 			if (con2fb_map_boot[i] == idx) {
2435 				info_idx = idx;
2436 				break;
2437 			}
2438 		}
2439 
2440 		if (info_idx != -1)
2441 			ret = do_fbcon_takeover(1);
2442 	} else {
2443 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2444 			if (con2fb_map_boot[i] == idx)
2445 				set_con2fb_map(i, idx, 0);
2446 		}
2447 	}
2448 
2449 	return ret;
2450 }
2451 
2452 void fbcon_fb_blanked(struct fb_info *info, int blank)
2453 {
2454 	struct fbcon_ops *ops = info->fbcon_par;
2455 	struct vc_data *vc;
2456 
2457 	if (!ops || ops->currcon < 0)
2458 		return;
2459 
2460 	vc = vc_cons[ops->currcon].d;
2461 	if (vc->vc_mode != KD_TEXT ||
2462 			registered_fb[con2fb_map[ops->currcon]] != info)
2463 		return;
2464 
2465 	if (con_is_visible(vc)) {
2466 		if (blank)
2467 			do_blank_screen(0);
2468 		else
2469 			do_unblank_screen(0);
2470 	}
2471 	ops->blank_state = blank;
2472 }
2473 
2474 void fbcon_new_modelist(struct fb_info *info)
2475 {
2476 	int i;
2477 	struct vc_data *vc;
2478 	struct fb_var_screeninfo var;
2479 	const struct fb_videomode *mode;
2480 
2481 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2482 		if (registered_fb[con2fb_map[i]] != info)
2483 			continue;
2484 		if (!fb_display[i].mode)
2485 			continue;
2486 		vc = vc_cons[i].d;
2487 		display_to_var(&var, &fb_display[i]);
2488 		mode = fb_find_nearest_mode(fb_display[i].mode,
2489 					    &info->modelist);
2490 		fb_videomode_to_var(&var, mode);
2491 		fbcon_set_disp(info, &var, vc->vc_num);
2492 	}
2493 }
2494 
2495 void fbcon_get_requirement(struct fb_info *info,
2496 			   struct fb_blit_caps *caps)
2497 {
2498 	struct vc_data *vc;
2499 
2500 	if (caps->flags) {
2501 		int i, charcnt;
2502 
2503 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2504 			vc = vc_cons[i].d;
2505 			if (vc && vc->vc_mode == KD_TEXT &&
2506 			    info->node == con2fb_map[i]) {
2507 				caps->x |= 1 << (vc->vc_font.width - 1);
2508 				caps->y |= 1 << (vc->vc_font.height - 1);
2509 				charcnt = vc->vc_font.charcount;
2510 				if (caps->len < charcnt)
2511 					caps->len = charcnt;
2512 			}
2513 		}
2514 	} else {
2515 		vc = vc_cons[fg_console].d;
2516 
2517 		if (vc && vc->vc_mode == KD_TEXT &&
2518 		    info->node == con2fb_map[fg_console]) {
2519 			caps->x = 1 << (vc->vc_font.width - 1);
2520 			caps->y = 1 << (vc->vc_font.height - 1);
2521 			caps->len = vc->vc_font.charcount;
2522 		}
2523 	}
2524 }
2525 
2526 int fbcon_set_con2fb_map_ioctl(void __user *argp)
2527 {
2528 	struct fb_con2fbmap con2fb;
2529 	int ret;
2530 
2531 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
2532 		return -EFAULT;
2533 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
2534 		return -EINVAL;
2535 	if (con2fb.framebuffer >= FB_MAX)
2536 		return -EINVAL;
2537 	if (!registered_fb[con2fb.framebuffer])
2538 		request_module("fb%d", con2fb.framebuffer);
2539 	if (!registered_fb[con2fb.framebuffer]) {
2540 		return -EINVAL;
2541 	}
2542 
2543 	console_lock();
2544 	ret = set_con2fb_map(con2fb.console - 1,
2545 			     con2fb.framebuffer, 1);
2546 	console_unlock();
2547 
2548 	return ret;
2549 }
2550 
2551 int fbcon_get_con2fb_map_ioctl(void __user *argp)
2552 {
2553 	struct fb_con2fbmap con2fb;
2554 
2555 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
2556 		return -EFAULT;
2557 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
2558 		return -EINVAL;
2559 
2560 	console_lock();
2561 	con2fb.framebuffer = con2fb_map[con2fb.console - 1];
2562 	console_unlock();
2563 
2564 	return copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
2565 }
2566 
2567 /*
2568  *  The console `switch' structure for the frame buffer based console
2569  */
2570 
2571 static const struct consw fb_con = {
2572 	.owner			= THIS_MODULE,
2573 	.con_startup 		= fbcon_startup,
2574 	.con_init 		= fbcon_init,
2575 	.con_deinit 		= fbcon_deinit,
2576 	.con_clear 		= fbcon_clear,
2577 	.con_putc 		= fbcon_putc,
2578 	.con_putcs 		= fbcon_putcs,
2579 	.con_cursor 		= fbcon_cursor,
2580 	.con_scroll 		= fbcon_scroll,
2581 	.con_switch 		= fbcon_switch,
2582 	.con_blank 		= fbcon_blank,
2583 	.con_font_set 		= fbcon_set_font,
2584 	.con_font_get 		= fbcon_get_font,
2585 	.con_font_default	= fbcon_set_def_font,
2586 	.con_set_palette 	= fbcon_set_palette,
2587 	.con_invert_region 	= fbcon_invert_region,
2588 	.con_screen_pos 	= fbcon_screen_pos,
2589 	.con_getxy 		= fbcon_getxy,
2590 	.con_resize             = fbcon_resize,
2591 	.con_debug_enter	= fbcon_debug_enter,
2592 	.con_debug_leave	= fbcon_debug_leave,
2593 };
2594 
2595 static ssize_t store_rotate(struct device *device,
2596 			    struct device_attribute *attr, const char *buf,
2597 			    size_t count)
2598 {
2599 	struct fb_info *info;
2600 	int rotate, idx;
2601 	char **last = NULL;
2602 
2603 	console_lock();
2604 	idx = con2fb_map[fg_console];
2605 
2606 	if (idx == -1 || registered_fb[idx] == NULL)
2607 		goto err;
2608 
2609 	info = registered_fb[idx];
2610 	rotate = simple_strtoul(buf, last, 0);
2611 	fbcon_rotate(info, rotate);
2612 err:
2613 	console_unlock();
2614 	return count;
2615 }
2616 
2617 static ssize_t store_rotate_all(struct device *device,
2618 				struct device_attribute *attr,const char *buf,
2619 				size_t count)
2620 {
2621 	struct fb_info *info;
2622 	int rotate, idx;
2623 	char **last = NULL;
2624 
2625 	console_lock();
2626 	idx = con2fb_map[fg_console];
2627 
2628 	if (idx == -1 || registered_fb[idx] == NULL)
2629 		goto err;
2630 
2631 	info = registered_fb[idx];
2632 	rotate = simple_strtoul(buf, last, 0);
2633 	fbcon_rotate_all(info, rotate);
2634 err:
2635 	console_unlock();
2636 	return count;
2637 }
2638 
2639 static ssize_t show_rotate(struct device *device,
2640 			   struct device_attribute *attr,char *buf)
2641 {
2642 	struct fb_info *info;
2643 	int rotate = 0, idx;
2644 
2645 	console_lock();
2646 	idx = con2fb_map[fg_console];
2647 
2648 	if (idx == -1 || registered_fb[idx] == NULL)
2649 		goto err;
2650 
2651 	info = registered_fb[idx];
2652 	rotate = fbcon_get_rotate(info);
2653 err:
2654 	console_unlock();
2655 	return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
2656 }
2657 
2658 static ssize_t show_cursor_blink(struct device *device,
2659 				 struct device_attribute *attr, char *buf)
2660 {
2661 	struct fb_info *info;
2662 	struct fbcon_ops *ops;
2663 	int idx, blink = -1;
2664 
2665 	console_lock();
2666 	idx = con2fb_map[fg_console];
2667 
2668 	if (idx == -1 || registered_fb[idx] == NULL)
2669 		goto err;
2670 
2671 	info = registered_fb[idx];
2672 	ops = info->fbcon_par;
2673 
2674 	if (!ops)
2675 		goto err;
2676 
2677 	blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
2678 err:
2679 	console_unlock();
2680 	return snprintf(buf, PAGE_SIZE, "%d\n", blink);
2681 }
2682 
2683 static ssize_t store_cursor_blink(struct device *device,
2684 				  struct device_attribute *attr,
2685 				  const char *buf, size_t count)
2686 {
2687 	struct fb_info *info;
2688 	int blink, idx;
2689 	char **last = NULL;
2690 
2691 	console_lock();
2692 	idx = con2fb_map[fg_console];
2693 
2694 	if (idx == -1 || registered_fb[idx] == NULL)
2695 		goto err;
2696 
2697 	info = registered_fb[idx];
2698 
2699 	if (!info->fbcon_par)
2700 		goto err;
2701 
2702 	blink = simple_strtoul(buf, last, 0);
2703 
2704 	if (blink) {
2705 		fbcon_cursor_noblink = 0;
2706 		fbcon_add_cursor_timer(info);
2707 	} else {
2708 		fbcon_cursor_noblink = 1;
2709 		fbcon_del_cursor_timer(info);
2710 	}
2711 
2712 err:
2713 	console_unlock();
2714 	return count;
2715 }
2716 
2717 static struct device_attribute device_attrs[] = {
2718 	__ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
2719 	__ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
2720 	__ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
2721 	       store_cursor_blink),
2722 };
2723 
2724 static int fbcon_init_device(void)
2725 {
2726 	int i, error = 0;
2727 
2728 	fbcon_has_sysfs = 1;
2729 
2730 	for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
2731 		error = device_create_file(fbcon_device, &device_attrs[i]);
2732 
2733 		if (error)
2734 			break;
2735 	}
2736 
2737 	if (error) {
2738 		while (--i >= 0)
2739 			device_remove_file(fbcon_device, &device_attrs[i]);
2740 
2741 		fbcon_has_sysfs = 0;
2742 	}
2743 
2744 	return 0;
2745 }
2746 
2747 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
2748 static void fbcon_register_existing_fbs(struct work_struct *work)
2749 {
2750 	int i;
2751 
2752 	console_lock();
2753 
2754 	for_each_registered_fb(i)
2755 		fbcon_fb_registered(registered_fb[i]);
2756 
2757 	console_unlock();
2758 }
2759 
2760 static struct notifier_block fbcon_output_nb;
2761 static DECLARE_WORK(fbcon_deferred_takeover_work, fbcon_register_existing_fbs);
2762 
2763 static int fbcon_output_notifier(struct notifier_block *nb,
2764 				 unsigned long action, void *data)
2765 {
2766 	WARN_CONSOLE_UNLOCKED();
2767 
2768 	pr_info("fbcon: Taking over console\n");
2769 
2770 	dummycon_unregister_output_notifier(&fbcon_output_nb);
2771 	deferred_takeover = false;
2772 	logo_shown = FBCON_LOGO_DONTSHOW;
2773 
2774 	/* We may get called in atomic context */
2775 	schedule_work(&fbcon_deferred_takeover_work);
2776 
2777 	return NOTIFY_OK;
2778 }
2779 #endif
2780 
2781 static void fbcon_start(void)
2782 {
2783 	WARN_CONSOLE_UNLOCKED();
2784 
2785 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
2786 	if (conswitchp != &dummy_con)
2787 		deferred_takeover = false;
2788 
2789 	if (deferred_takeover) {
2790 		fbcon_output_nb.notifier_call = fbcon_output_notifier;
2791 		dummycon_register_output_notifier(&fbcon_output_nb);
2792 		return;
2793 	}
2794 #endif
2795 
2796 	if (num_registered_fb) {
2797 		int i;
2798 
2799 		for_each_registered_fb(i) {
2800 			info_idx = i;
2801 			break;
2802 		}
2803 
2804 		do_fbcon_takeover(0);
2805 	}
2806 }
2807 
2808 static void fbcon_exit(void)
2809 {
2810 	struct fb_info *info;
2811 	int i, j, mapped;
2812 
2813 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
2814 	if (deferred_takeover) {
2815 		dummycon_unregister_output_notifier(&fbcon_output_nb);
2816 		deferred_takeover = false;
2817 	}
2818 #endif
2819 
2820 	for_each_registered_fb(i) {
2821 		int pending = 0;
2822 
2823 		mapped = 0;
2824 		info = registered_fb[i];
2825 
2826 		if (info->queue.func)
2827 			pending = cancel_work_sync(&info->queue);
2828 		pr_debug("fbcon: %s pending work\n", (pending ? "canceled" : "no"));
2829 
2830 		for (j = first_fb_vc; j <= last_fb_vc; j++) {
2831 			if (con2fb_map[j] == i) {
2832 				mapped = 1;
2833 				con2fb_map[j] = -1;
2834 			}
2835 		}
2836 
2837 		if (mapped) {
2838 			if (info->fbops->fb_release)
2839 				info->fbops->fb_release(info, 0);
2840 			module_put(info->fbops->owner);
2841 
2842 			if (info->fbcon_par) {
2843 				struct fbcon_ops *ops = info->fbcon_par;
2844 
2845 				fbcon_del_cursor_timer(info);
2846 				kfree(ops->cursor_src);
2847 				kfree(ops->cursor_state.mask);
2848 				kfree(info->fbcon_par);
2849 				info->fbcon_par = NULL;
2850 			}
2851 
2852 			if (info->queue.func == fb_flashcursor)
2853 				info->queue.func = NULL;
2854 		}
2855 	}
2856 }
2857 
2858 void __init fb_console_init(void)
2859 {
2860 	int i;
2861 
2862 	console_lock();
2863 	fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
2864 				     "fbcon");
2865 
2866 	if (IS_ERR(fbcon_device)) {
2867 		printk(KERN_WARNING "Unable to create device "
2868 		       "for fbcon; errno = %ld\n",
2869 		       PTR_ERR(fbcon_device));
2870 		fbcon_device = NULL;
2871 	} else
2872 		fbcon_init_device();
2873 
2874 	for (i = 0; i < MAX_NR_CONSOLES; i++)
2875 		con2fb_map[i] = -1;
2876 
2877 	fbcon_start();
2878 	console_unlock();
2879 }
2880 
2881 #ifdef MODULE
2882 
2883 static void __exit fbcon_deinit_device(void)
2884 {
2885 	int i;
2886 
2887 	if (fbcon_has_sysfs) {
2888 		for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
2889 			device_remove_file(fbcon_device, &device_attrs[i]);
2890 
2891 		fbcon_has_sysfs = 0;
2892 	}
2893 }
2894 
2895 void __exit fb_console_exit(void)
2896 {
2897 	console_lock();
2898 	fbcon_deinit_device();
2899 	device_destroy(fb_class, MKDEV(0, 0));
2900 	fbcon_exit();
2901 	do_unregister_con_driver(&fb_con);
2902 	console_unlock();
2903 }
2904 #endif
2905