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