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