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