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