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