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