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