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