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