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