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