• 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 	u8 *old_data = vc->vc_font.data;
2413 
2414 	resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
2415 	vc->vc_font.data = (void *)(p->fontdata = data);
2416 	old_userfont = p->userfont;
2417 	if ((p->userfont = userfont))
2418 		REFCOUNT(data)++;
2419 
2420 	old_width = vc->vc_font.width;
2421 	old_height = vc->vc_font.height;
2422 	old_charcount = vc->vc_font.charcount;
2423 
2424 	vc->vc_font.width = w;
2425 	vc->vc_font.height = h;
2426 	vc->vc_font.charcount = charcount;
2427 	if (vc->vc_hi_font_mask && charcount == 256)
2428 		set_vc_hi_font(vc, false);
2429 	else if (!vc->vc_hi_font_mask && charcount == 512)
2430 		set_vc_hi_font(vc, true);
2431 
2432 	if (resize) {
2433 		int cols, rows;
2434 
2435 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2436 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2437 		cols /= w;
2438 		rows /= h;
2439 		ret = vc_resize(vc, cols, rows);
2440 		if (ret)
2441 			goto err_out;
2442 	} else if (con_is_visible(vc)
2443 		   && vc->vc_mode == KD_TEXT) {
2444 		fbcon_clear_margins(vc, 0);
2445 		update_screen(vc);
2446 	}
2447 
2448 	if (old_userfont && (--REFCOUNT(old_data) == 0))
2449 		kfree(old_data - FONT_EXTRA_WORDS * sizeof(int));
2450 	return 0;
2451 
2452 err_out:
2453 	p->fontdata = old_data;
2454 	vc->vc_font.data = old_data;
2455 
2456 	if (userfont) {
2457 		p->userfont = old_userfont;
2458 		if (--REFCOUNT(data) == 0)
2459 			kfree(data - FONT_EXTRA_WORDS * sizeof(int));
2460 	}
2461 
2462 	vc->vc_font.width = old_width;
2463 	vc->vc_font.height = old_height;
2464 	vc->vc_font.charcount = old_charcount;
2465 
2466 	return ret;
2467 }
2468 
2469 /*
2470  *  User asked to set font; we are guaranteed that
2471  *	a) width and height are in range 1..32
2472  *	b) charcount does not exceed 512
2473  *  but lets not assume that, since someone might someday want to use larger
2474  *  fonts. And charcount of 512 is small for unicode support.
2475  *
2476  *  However, user space gives the font in 32 rows , regardless of
2477  *  actual font height. So a new API is needed if support for larger fonts
2478  *  is ever implemented.
2479  */
2480 
fbcon_set_font(struct vc_data * vc,struct console_font * font,unsigned int flags)2481 static int fbcon_set_font(struct vc_data *vc, struct console_font *font,
2482 			  unsigned int flags)
2483 {
2484 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2485 	unsigned charcount = font->charcount;
2486 	int w = font->width;
2487 	int h = font->height;
2488 	int size;
2489 	int i, csum;
2490 	u8 *new_data, *data = font->data;
2491 	int pitch = PITCH(font->width);
2492 
2493 	/* Is there a reason why fbconsole couldn't handle any charcount >256?
2494 	 * If not this check should be changed to charcount < 256 */
2495 	if (charcount != 256 && charcount != 512)
2496 		return -EINVAL;
2497 
2498 	/* font bigger than screen resolution ? */
2499 	if (w > FBCON_SWAP(info->var.rotate, info->var.xres, info->var.yres) ||
2500 	    h > FBCON_SWAP(info->var.rotate, info->var.yres, info->var.xres))
2501 		return -EINVAL;
2502 
2503 	if (font->width > 32 || font->height > 32)
2504 		return -EINVAL;
2505 
2506 	/* Make sure drawing engine can handle the font */
2507 	if (!(info->pixmap.blit_x & BIT(font->width - 1)) ||
2508 	    !(info->pixmap.blit_y & BIT(font->height - 1)))
2509 		return -EINVAL;
2510 
2511 	/* Make sure driver can handle the font length */
2512 	if (fbcon_invalid_charcount(info, charcount))
2513 		return -EINVAL;
2514 
2515 	size = CALC_FONTSZ(h, pitch, charcount);
2516 
2517 	new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER);
2518 
2519 	if (!new_data)
2520 		return -ENOMEM;
2521 
2522 	memset(new_data, 0, FONT_EXTRA_WORDS * sizeof(int));
2523 
2524 	new_data += FONT_EXTRA_WORDS * sizeof(int);
2525 	FNTSIZE(new_data) = size;
2526 	REFCOUNT(new_data) = 0;	/* usage counter */
2527 	for (i=0; i< charcount; i++) {
2528 		memcpy(new_data + i*h*pitch, data +  i*32*pitch, h*pitch);
2529 	}
2530 
2531 	/* Since linux has a nice crc32 function use it for counting font
2532 	 * checksums. */
2533 	csum = crc32(0, new_data, size);
2534 
2535 	FNTSUM(new_data) = csum;
2536 	/* Check if the same font is on some other console already */
2537 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2538 		struct vc_data *tmp = vc_cons[i].d;
2539 
2540 		if (fb_display[i].userfont &&
2541 		    fb_display[i].fontdata &&
2542 		    FNTSUM(fb_display[i].fontdata) == csum &&
2543 		    FNTSIZE(fb_display[i].fontdata) == size &&
2544 		    tmp->vc_font.width == w &&
2545 		    !memcmp(fb_display[i].fontdata, new_data, size)) {
2546 			kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
2547 			new_data = (u8 *)fb_display[i].fontdata;
2548 			break;
2549 		}
2550 	}
2551 	return fbcon_do_set_font(vc, font->width, font->height, charcount, new_data, 1);
2552 }
2553 
fbcon_set_def_font(struct vc_data * vc,struct console_font * font,char * name)2554 static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
2555 {
2556 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2557 	const struct font_desc *f;
2558 
2559 	if (!name)
2560 		f = get_default_font(info->var.xres, info->var.yres,
2561 				     info->pixmap.blit_x, info->pixmap.blit_y);
2562 	else if (!(f = find_font(name)))
2563 		return -ENOENT;
2564 
2565 	font->width = f->width;
2566 	font->height = f->height;
2567 	return fbcon_do_set_font(vc, f->width, f->height, f->charcount, f->data, 0);
2568 }
2569 
2570 static u16 palette_red[16];
2571 static u16 palette_green[16];
2572 static u16 palette_blue[16];
2573 
2574 static struct fb_cmap palette_cmap = {
2575 	0, 16, palette_red, palette_green, palette_blue, NULL
2576 };
2577 
fbcon_set_palette(struct vc_data * vc,const unsigned char * table)2578 static void fbcon_set_palette(struct vc_data *vc, const unsigned char *table)
2579 {
2580 	struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
2581 	int i, j, k, depth;
2582 	u8 val;
2583 
2584 	if (fbcon_is_inactive(vc, info))
2585 		return;
2586 
2587 	if (!con_is_visible(vc))
2588 		return;
2589 
2590 	depth = fb_get_color_depth(&info->var, &info->fix);
2591 	if (depth > 3) {
2592 		for (i = j = 0; i < 16; i++) {
2593 			k = table[i];
2594 			val = vc->vc_palette[j++];
2595 			palette_red[k] = (val << 8) | val;
2596 			val = vc->vc_palette[j++];
2597 			palette_green[k] = (val << 8) | val;
2598 			val = vc->vc_palette[j++];
2599 			palette_blue[k] = (val << 8) | val;
2600 		}
2601 		palette_cmap.len = 16;
2602 		palette_cmap.start = 0;
2603 	/*
2604 	 * If framebuffer is capable of less than 16 colors,
2605 	 * use default palette of fbcon.
2606 	 */
2607 	} else
2608 		fb_copy_cmap(fb_default_cmap(1 << depth), &palette_cmap);
2609 
2610 	fb_set_cmap(&palette_cmap, info);
2611 }
2612 
fbcon_screen_pos(const struct vc_data * vc,int offset)2613 static u16 *fbcon_screen_pos(const struct vc_data *vc, int offset)
2614 {
2615 	return (u16 *) (vc->vc_origin + offset);
2616 }
2617 
fbcon_getxy(struct vc_data * vc,unsigned long pos,int * px,int * py)2618 static unsigned long fbcon_getxy(struct vc_data *vc, unsigned long pos,
2619 				 int *px, int *py)
2620 {
2621 	unsigned long ret;
2622 	int x, y;
2623 
2624 	if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
2625 		unsigned long offset = (pos - vc->vc_origin) / 2;
2626 
2627 		x = offset % vc->vc_cols;
2628 		y = offset / vc->vc_cols;
2629 		ret = pos + (vc->vc_cols - x) * 2;
2630 	} else {
2631 		/* Should not happen */
2632 		x = y = 0;
2633 		ret = vc->vc_origin;
2634 	}
2635 	if (px)
2636 		*px = x;
2637 	if (py)
2638 		*py = y;
2639 	return ret;
2640 }
2641 
2642 /* As we might be inside of softback, we may work with non-contiguous buffer,
2643    that's why we have to use a separate routine. */
fbcon_invert_region(struct vc_data * vc,u16 * p,int cnt)2644 static void fbcon_invert_region(struct vc_data *vc, u16 * p, int cnt)
2645 {
2646 	while (cnt--) {
2647 		u16 a = scr_readw(p);
2648 		if (!vc->vc_can_do_color)
2649 			a ^= 0x0800;
2650 		else if (vc->vc_hi_font_mask == 0x100)
2651 			a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) |
2652 			    (((a) & 0x0e00) << 4);
2653 		else
2654 			a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
2655 			    (((a) & 0x0700) << 4);
2656 		scr_writew(a, p++);
2657 	}
2658 }
2659 
fbcon_suspended(struct fb_info * info)2660 void fbcon_suspended(struct fb_info *info)
2661 {
2662 	struct vc_data *vc = NULL;
2663 	struct fbcon_ops *ops = info->fbcon_par;
2664 
2665 	if (!ops || ops->currcon < 0)
2666 		return;
2667 	vc = vc_cons[ops->currcon].d;
2668 
2669 	/* Clear cursor, restore saved data */
2670 	fbcon_cursor(vc, CM_ERASE);
2671 }
2672 
fbcon_resumed(struct fb_info * info)2673 void fbcon_resumed(struct fb_info *info)
2674 {
2675 	struct vc_data *vc;
2676 	struct fbcon_ops *ops = info->fbcon_par;
2677 
2678 	if (!ops || ops->currcon < 0)
2679 		return;
2680 	vc = vc_cons[ops->currcon].d;
2681 
2682 	update_screen(vc);
2683 }
2684 
fbcon_modechanged(struct fb_info * info)2685 static void fbcon_modechanged(struct fb_info *info)
2686 {
2687 	struct fbcon_ops *ops = info->fbcon_par;
2688 	struct vc_data *vc;
2689 	struct fbcon_display *p;
2690 	int rows, cols;
2691 
2692 	if (!ops || ops->currcon < 0)
2693 		return;
2694 	vc = vc_cons[ops->currcon].d;
2695 	if (vc->vc_mode != KD_TEXT ||
2696 	    registered_fb[con2fb_map[ops->currcon]] != info)
2697 		return;
2698 
2699 	p = &fb_display[vc->vc_num];
2700 	set_blitting_type(vc, info);
2701 
2702 	if (con_is_visible(vc)) {
2703 		var_to_display(p, &info->var, info);
2704 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2705 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2706 		cols /= vc->vc_font.width;
2707 		rows /= vc->vc_font.height;
2708 		vc_resize(vc, cols, rows);
2709 		updatescrollmode(p, info, vc);
2710 		scrollback_max = 0;
2711 		scrollback_current = 0;
2712 
2713 		if (!fbcon_is_inactive(vc, info)) {
2714 		    ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
2715 		    ops->update_start(info);
2716 		}
2717 
2718 		fbcon_set_palette(vc, color_table);
2719 		update_screen(vc);
2720 	}
2721 }
2722 
fbcon_set_all_vcs(struct fb_info * info)2723 static void fbcon_set_all_vcs(struct fb_info *info)
2724 {
2725 	struct fbcon_ops *ops = info->fbcon_par;
2726 	struct vc_data *vc;
2727 	struct fbcon_display *p;
2728 	int i, rows, cols, fg = -1;
2729 
2730 	if (!ops || ops->currcon < 0)
2731 		return;
2732 
2733 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2734 		vc = vc_cons[i].d;
2735 		if (!vc || vc->vc_mode != KD_TEXT ||
2736 		    registered_fb[con2fb_map[i]] != info)
2737 			continue;
2738 
2739 		if (con_is_visible(vc)) {
2740 			fg = i;
2741 			continue;
2742 		}
2743 
2744 		p = &fb_display[vc->vc_num];
2745 		set_blitting_type(vc, info);
2746 		var_to_display(p, &info->var, info);
2747 		cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
2748 		rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
2749 		cols /= vc->vc_font.width;
2750 		rows /= vc->vc_font.height;
2751 		vc_resize(vc, cols, rows);
2752 	}
2753 
2754 	if (fg != -1)
2755 		fbcon_modechanged(info);
2756 }
2757 
2758 
fbcon_update_vcs(struct fb_info * info,bool all)2759 void fbcon_update_vcs(struct fb_info *info, bool all)
2760 {
2761 	if (all)
2762 		fbcon_set_all_vcs(info);
2763 	else
2764 		fbcon_modechanged(info);
2765 }
2766 EXPORT_SYMBOL(fbcon_update_vcs);
2767 
2768 /* let fbcon check if it supports a new screen resolution */
fbcon_modechange_possible(struct fb_info * info,struct fb_var_screeninfo * var)2769 int fbcon_modechange_possible(struct fb_info *info, struct fb_var_screeninfo *var)
2770 {
2771 	struct fbcon_ops *ops = info->fbcon_par;
2772 	struct vc_data *vc;
2773 	unsigned int i;
2774 
2775 	WARN_CONSOLE_UNLOCKED();
2776 
2777 	if (!ops)
2778 		return 0;
2779 
2780 	/* prevent setting a screen size which is smaller than font size */
2781 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2782 		vc = vc_cons[i].d;
2783 		if (!vc || vc->vc_mode != KD_TEXT ||
2784 			   registered_fb[con2fb_map[i]] != info)
2785 			continue;
2786 
2787 		if (vc->vc_font.width  > FBCON_SWAP(var->rotate, var->xres, var->yres) ||
2788 		    vc->vc_font.height > FBCON_SWAP(var->rotate, var->yres, var->xres))
2789 			return -EINVAL;
2790 	}
2791 
2792 	return 0;
2793 }
2794 EXPORT_SYMBOL_GPL(fbcon_modechange_possible);
2795 
fbcon_mode_deleted(struct fb_info * info,struct fb_videomode * mode)2796 int fbcon_mode_deleted(struct fb_info *info,
2797 		       struct fb_videomode *mode)
2798 {
2799 	struct fb_info *fb_info;
2800 	struct fbcon_display *p;
2801 	int i, j, found = 0;
2802 
2803 	/* before deletion, ensure that mode is not in use */
2804 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2805 		j = con2fb_map[i];
2806 		if (j == -1)
2807 			continue;
2808 		fb_info = registered_fb[j];
2809 		if (fb_info != info)
2810 			continue;
2811 		p = &fb_display[i];
2812 		if (!p || !p->mode)
2813 			continue;
2814 		if (fb_mode_is_equal(p->mode, mode)) {
2815 			found = 1;
2816 			break;
2817 		}
2818 	}
2819 	return found;
2820 }
2821 
2822 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
fbcon_unbind(void)2823 static void fbcon_unbind(void)
2824 {
2825 	int ret;
2826 
2827 	ret = do_unbind_con_driver(&fb_con, first_fb_vc, last_fb_vc,
2828 				fbcon_is_default);
2829 
2830 	if (!ret)
2831 		fbcon_has_console_bind = 0;
2832 }
2833 #else
fbcon_unbind(void)2834 static inline void fbcon_unbind(void) {}
2835 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
2836 
2837 /* called with console_lock held */
fbcon_fb_unbind(struct fb_info * info)2838 void fbcon_fb_unbind(struct fb_info *info)
2839 {
2840 	int i, new_idx = -1, ret = 0;
2841 	int idx = info->node;
2842 
2843 	WARN_CONSOLE_UNLOCKED();
2844 
2845 	if (!fbcon_has_console_bind)
2846 		return;
2847 
2848 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2849 		if (con2fb_map[i] != idx &&
2850 		    con2fb_map[i] != -1) {
2851 			new_idx = con2fb_map[i];
2852 			break;
2853 		}
2854 	}
2855 
2856 	if (new_idx != -1) {
2857 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2858 			if (con2fb_map[i] == idx)
2859 				set_con2fb_map(i, new_idx, 0);
2860 		}
2861 	} else {
2862 		struct fb_info *info = registered_fb[idx];
2863 
2864 		/* This is sort of like set_con2fb_map, except it maps
2865 		 * the consoles to no device and then releases the
2866 		 * oldinfo to free memory and cancel the cursor blink
2867 		 * timer. I can imagine this just becoming part of
2868 		 * set_con2fb_map where new_idx is -1
2869 		 */
2870 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2871 			if (con2fb_map[i] == idx) {
2872 				con2fb_map[i] = -1;
2873 				if (!search_fb_in_map(idx)) {
2874 					ret = con2fb_release_oldinfo(vc_cons[i].d,
2875 								     info, NULL, i,
2876 								     idx, 0);
2877 					if (ret) {
2878 						con2fb_map[i] = idx;
2879 						return;
2880 					}
2881 				}
2882 			}
2883 		}
2884 		fbcon_unbind();
2885 	}
2886 }
2887 
2888 /* called with console_lock held */
fbcon_fb_unregistered(struct fb_info * info)2889 void fbcon_fb_unregistered(struct fb_info *info)
2890 {
2891 	int i, idx;
2892 
2893 	WARN_CONSOLE_UNLOCKED();
2894 
2895 	if (deferred_takeover)
2896 		return;
2897 
2898 	idx = info->node;
2899 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
2900 		if (con2fb_map[i] == idx)
2901 			con2fb_map[i] = -1;
2902 	}
2903 
2904 	if (idx == info_idx) {
2905 		info_idx = -1;
2906 
2907 		for_each_registered_fb(i) {
2908 			info_idx = i;
2909 			break;
2910 		}
2911 	}
2912 
2913 	if (info_idx != -1) {
2914 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2915 			if (con2fb_map[i] == -1)
2916 				con2fb_map[i] = info_idx;
2917 		}
2918 	}
2919 
2920 	if (primary_device == idx)
2921 		primary_device = -1;
2922 
2923 	if (!num_registered_fb)
2924 		do_unregister_con_driver(&fb_con);
2925 }
2926 
fbcon_remap_all(struct fb_info * info)2927 void fbcon_remap_all(struct fb_info *info)
2928 {
2929 	int i, idx = info->node;
2930 
2931 	console_lock();
2932 	if (deferred_takeover) {
2933 		for (i = first_fb_vc; i <= last_fb_vc; i++)
2934 			con2fb_map_boot[i] = idx;
2935 		fbcon_map_override();
2936 		console_unlock();
2937 		return;
2938 	}
2939 
2940 	for (i = first_fb_vc; i <= last_fb_vc; i++)
2941 		set_con2fb_map(i, idx, 0);
2942 
2943 	if (con_is_bound(&fb_con)) {
2944 		printk(KERN_INFO "fbcon: Remapping primary device, "
2945 		       "fb%i, to tty %i-%i\n", idx,
2946 		       first_fb_vc + 1, last_fb_vc + 1);
2947 		info_idx = idx;
2948 	}
2949 	console_unlock();
2950 }
2951 
2952 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY
fbcon_select_primary(struct fb_info * info)2953 static void fbcon_select_primary(struct fb_info *info)
2954 {
2955 	if (!map_override && primary_device == -1 &&
2956 	    fb_is_primary_device(info)) {
2957 		int i;
2958 
2959 		printk(KERN_INFO "fbcon: %s (fb%i) is primary device\n",
2960 		       info->fix.id, info->node);
2961 		primary_device = info->node;
2962 
2963 		for (i = first_fb_vc; i <= last_fb_vc; i++)
2964 			con2fb_map_boot[i] = primary_device;
2965 
2966 		if (con_is_bound(&fb_con)) {
2967 			printk(KERN_INFO "fbcon: Remapping primary device, "
2968 			       "fb%i, to tty %i-%i\n", info->node,
2969 			       first_fb_vc + 1, last_fb_vc + 1);
2970 			info_idx = primary_device;
2971 		}
2972 	}
2973 
2974 }
2975 #else
fbcon_select_primary(struct fb_info * info)2976 static inline void fbcon_select_primary(struct fb_info *info)
2977 {
2978 	return;
2979 }
2980 #endif /* CONFIG_FRAMEBUFFER_DETECT_PRIMARY */
2981 
2982 /* called with console_lock held */
fbcon_fb_registered(struct fb_info * info)2983 int fbcon_fb_registered(struct fb_info *info)
2984 {
2985 	int ret = 0, i, idx;
2986 
2987 	WARN_CONSOLE_UNLOCKED();
2988 
2989 	idx = info->node;
2990 	fbcon_select_primary(info);
2991 
2992 	if (deferred_takeover) {
2993 		pr_info("fbcon: Deferring console take-over\n");
2994 		return 0;
2995 	}
2996 
2997 	if (info_idx == -1) {
2998 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
2999 			if (con2fb_map_boot[i] == idx) {
3000 				info_idx = idx;
3001 				break;
3002 			}
3003 		}
3004 
3005 		if (info_idx != -1)
3006 			ret = do_fbcon_takeover(1);
3007 	} else {
3008 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3009 			if (con2fb_map_boot[i] == idx)
3010 				set_con2fb_map(i, idx, 0);
3011 		}
3012 	}
3013 
3014 	return ret;
3015 }
3016 
fbcon_fb_blanked(struct fb_info * info,int blank)3017 void fbcon_fb_blanked(struct fb_info *info, int blank)
3018 {
3019 	struct fbcon_ops *ops = info->fbcon_par;
3020 	struct vc_data *vc;
3021 
3022 	if (!ops || ops->currcon < 0)
3023 		return;
3024 
3025 	vc = vc_cons[ops->currcon].d;
3026 	if (vc->vc_mode != KD_TEXT ||
3027 			registered_fb[con2fb_map[ops->currcon]] != info)
3028 		return;
3029 
3030 	if (con_is_visible(vc)) {
3031 		if (blank)
3032 			do_blank_screen(0);
3033 		else
3034 			do_unblank_screen(0);
3035 	}
3036 	ops->blank_state = blank;
3037 }
3038 
fbcon_new_modelist(struct fb_info * info)3039 void fbcon_new_modelist(struct fb_info *info)
3040 {
3041 	int i;
3042 	struct vc_data *vc;
3043 	struct fb_var_screeninfo var;
3044 	const struct fb_videomode *mode;
3045 
3046 	for (i = first_fb_vc; i <= last_fb_vc; i++) {
3047 		if (registered_fb[con2fb_map[i]] != info)
3048 			continue;
3049 		if (!fb_display[i].mode)
3050 			continue;
3051 		vc = vc_cons[i].d;
3052 		display_to_var(&var, &fb_display[i]);
3053 		mode = fb_find_nearest_mode(fb_display[i].mode,
3054 					    &info->modelist);
3055 		fb_videomode_to_var(&var, mode);
3056 		fbcon_set_disp(info, &var, vc->vc_num);
3057 	}
3058 }
3059 
fbcon_get_requirement(struct fb_info * info,struct fb_blit_caps * caps)3060 void fbcon_get_requirement(struct fb_info *info,
3061 			   struct fb_blit_caps *caps)
3062 {
3063 	struct vc_data *vc;
3064 
3065 	if (caps->flags) {
3066 		int i, charcnt;
3067 
3068 		for (i = first_fb_vc; i <= last_fb_vc; i++) {
3069 			vc = vc_cons[i].d;
3070 			if (vc && vc->vc_mode == KD_TEXT &&
3071 			    info->node == con2fb_map[i]) {
3072 				caps->x |= 1 << (vc->vc_font.width - 1);
3073 				caps->y |= 1 << (vc->vc_font.height - 1);
3074 				charcnt = vc->vc_font.charcount;
3075 				if (caps->len < charcnt)
3076 					caps->len = charcnt;
3077 			}
3078 		}
3079 	} else {
3080 		vc = vc_cons[fg_console].d;
3081 
3082 		if (vc && vc->vc_mode == KD_TEXT &&
3083 		    info->node == con2fb_map[fg_console]) {
3084 			caps->x = 1 << (vc->vc_font.width - 1);
3085 			caps->y = 1 << (vc->vc_font.height - 1);
3086 			caps->len = vc->vc_font.charcount;
3087 		}
3088 	}
3089 }
3090 
fbcon_set_con2fb_map_ioctl(void __user * argp)3091 int fbcon_set_con2fb_map_ioctl(void __user *argp)
3092 {
3093 	struct fb_con2fbmap con2fb;
3094 	int ret;
3095 
3096 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
3097 		return -EFAULT;
3098 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
3099 		return -EINVAL;
3100 	if (con2fb.framebuffer >= FB_MAX)
3101 		return -EINVAL;
3102 	if (!registered_fb[con2fb.framebuffer])
3103 		request_module("fb%d", con2fb.framebuffer);
3104 	if (!registered_fb[con2fb.framebuffer]) {
3105 		return -EINVAL;
3106 	}
3107 
3108 	console_lock();
3109 	ret = set_con2fb_map(con2fb.console - 1,
3110 			     con2fb.framebuffer, 1);
3111 	console_unlock();
3112 
3113 	return ret;
3114 }
3115 
fbcon_get_con2fb_map_ioctl(void __user * argp)3116 int fbcon_get_con2fb_map_ioctl(void __user *argp)
3117 {
3118 	struct fb_con2fbmap con2fb;
3119 
3120 	if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
3121 		return -EFAULT;
3122 	if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
3123 		return -EINVAL;
3124 
3125 	console_lock();
3126 	con2fb.framebuffer = con2fb_map[con2fb.console - 1];
3127 	console_unlock();
3128 
3129 	return copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
3130 }
3131 
3132 /*
3133  *  The console `switch' structure for the frame buffer based console
3134  */
3135 
3136 static const struct consw fb_con = {
3137 	.owner			= THIS_MODULE,
3138 	.con_startup 		= fbcon_startup,
3139 	.con_init 		= fbcon_init,
3140 	.con_deinit 		= fbcon_deinit,
3141 	.con_clear 		= fbcon_clear,
3142 	.con_putc 		= fbcon_putc,
3143 	.con_putcs 		= fbcon_putcs,
3144 	.con_cursor 		= fbcon_cursor,
3145 	.con_scroll 		= fbcon_scroll,
3146 	.con_switch 		= fbcon_switch,
3147 	.con_blank 		= fbcon_blank,
3148 	.con_font_set 		= fbcon_set_font,
3149 	.con_font_get 		= fbcon_get_font,
3150 	.con_font_default	= fbcon_set_def_font,
3151 	.con_set_palette 	= fbcon_set_palette,
3152 	.con_invert_region 	= fbcon_invert_region,
3153 	.con_screen_pos 	= fbcon_screen_pos,
3154 	.con_getxy 		= fbcon_getxy,
3155 	.con_resize             = fbcon_resize,
3156 	.con_debug_enter	= fbcon_debug_enter,
3157 	.con_debug_leave	= fbcon_debug_leave,
3158 };
3159 
store_rotate(struct device * device,struct device_attribute * attr,const char * buf,size_t count)3160 static ssize_t store_rotate(struct device *device,
3161 			    struct device_attribute *attr, const char *buf,
3162 			    size_t count)
3163 {
3164 	struct fb_info *info;
3165 	int rotate, idx;
3166 	char **last = NULL;
3167 
3168 	console_lock();
3169 	idx = con2fb_map[fg_console];
3170 
3171 	if (idx == -1 || registered_fb[idx] == NULL)
3172 		goto err;
3173 
3174 	info = registered_fb[idx];
3175 	rotate = simple_strtoul(buf, last, 0);
3176 	fbcon_rotate(info, rotate);
3177 err:
3178 	console_unlock();
3179 	return count;
3180 }
3181 
store_rotate_all(struct device * device,struct device_attribute * attr,const char * buf,size_t count)3182 static ssize_t store_rotate_all(struct device *device,
3183 				struct device_attribute *attr,const char *buf,
3184 				size_t count)
3185 {
3186 	struct fb_info *info;
3187 	int rotate, idx;
3188 	char **last = NULL;
3189 
3190 	console_lock();
3191 	idx = con2fb_map[fg_console];
3192 
3193 	if (idx == -1 || registered_fb[idx] == NULL)
3194 		goto err;
3195 
3196 	info = registered_fb[idx];
3197 	rotate = simple_strtoul(buf, last, 0);
3198 	fbcon_rotate_all(info, rotate);
3199 err:
3200 	console_unlock();
3201 	return count;
3202 }
3203 
show_rotate(struct device * device,struct device_attribute * attr,char * buf)3204 static ssize_t show_rotate(struct device *device,
3205 			   struct device_attribute *attr,char *buf)
3206 {
3207 	struct fb_info *info;
3208 	int rotate = 0, idx;
3209 
3210 	console_lock();
3211 	idx = con2fb_map[fg_console];
3212 
3213 	if (idx == -1 || registered_fb[idx] == NULL)
3214 		goto err;
3215 
3216 	info = registered_fb[idx];
3217 	rotate = fbcon_get_rotate(info);
3218 err:
3219 	console_unlock();
3220 	return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
3221 }
3222 
show_cursor_blink(struct device * device,struct device_attribute * attr,char * buf)3223 static ssize_t show_cursor_blink(struct device *device,
3224 				 struct device_attribute *attr, char *buf)
3225 {
3226 	struct fb_info *info;
3227 	struct fbcon_ops *ops;
3228 	int idx, blink = -1;
3229 
3230 	console_lock();
3231 	idx = con2fb_map[fg_console];
3232 
3233 	if (idx == -1 || registered_fb[idx] == NULL)
3234 		goto err;
3235 
3236 	info = registered_fb[idx];
3237 	ops = info->fbcon_par;
3238 
3239 	if (!ops)
3240 		goto err;
3241 
3242 	blink = (ops->flags & FBCON_FLAGS_CURSOR_TIMER) ? 1 : 0;
3243 err:
3244 	console_unlock();
3245 	return snprintf(buf, PAGE_SIZE, "%d\n", blink);
3246 }
3247 
store_cursor_blink(struct device * device,struct device_attribute * attr,const char * buf,size_t count)3248 static ssize_t store_cursor_blink(struct device *device,
3249 				  struct device_attribute *attr,
3250 				  const char *buf, size_t count)
3251 {
3252 	struct fb_info *info;
3253 	int blink, idx;
3254 	char **last = NULL;
3255 
3256 	console_lock();
3257 	idx = con2fb_map[fg_console];
3258 
3259 	if (idx == -1 || registered_fb[idx] == NULL)
3260 		goto err;
3261 
3262 	info = registered_fb[idx];
3263 
3264 	if (!info->fbcon_par)
3265 		goto err;
3266 
3267 	blink = simple_strtoul(buf, last, 0);
3268 
3269 	if (blink) {
3270 		fbcon_cursor_noblink = 0;
3271 		fbcon_add_cursor_timer(info);
3272 	} else {
3273 		fbcon_cursor_noblink = 1;
3274 		fbcon_del_cursor_timer(info);
3275 	}
3276 
3277 err:
3278 	console_unlock();
3279 	return count;
3280 }
3281 
3282 static struct device_attribute device_attrs[] = {
3283 	__ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
3284 	__ATTR(rotate_all, S_IWUSR, NULL, store_rotate_all),
3285 	__ATTR(cursor_blink, S_IRUGO|S_IWUSR, show_cursor_blink,
3286 	       store_cursor_blink),
3287 };
3288 
fbcon_init_device(void)3289 static int fbcon_init_device(void)
3290 {
3291 	int i, error = 0;
3292 
3293 	fbcon_has_sysfs = 1;
3294 
3295 	for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3296 		error = device_create_file(fbcon_device, &device_attrs[i]);
3297 
3298 		if (error)
3299 			break;
3300 	}
3301 
3302 	if (error) {
3303 		while (--i >= 0)
3304 			device_remove_file(fbcon_device, &device_attrs[i]);
3305 
3306 		fbcon_has_sysfs = 0;
3307 	}
3308 
3309 	return 0;
3310 }
3311 
3312 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
fbcon_register_existing_fbs(struct work_struct * work)3313 static void fbcon_register_existing_fbs(struct work_struct *work)
3314 {
3315 	int i;
3316 
3317 	console_lock();
3318 
3319 	deferred_takeover = false;
3320 	logo_shown = FBCON_LOGO_DONTSHOW;
3321 
3322 	for_each_registered_fb(i)
3323 		fbcon_fb_registered(registered_fb[i]);
3324 
3325 	console_unlock();
3326 }
3327 
3328 static struct notifier_block fbcon_output_nb;
3329 static DECLARE_WORK(fbcon_deferred_takeover_work, fbcon_register_existing_fbs);
3330 
fbcon_output_notifier(struct notifier_block * nb,unsigned long action,void * data)3331 static int fbcon_output_notifier(struct notifier_block *nb,
3332 				 unsigned long action, void *data)
3333 {
3334 	WARN_CONSOLE_UNLOCKED();
3335 
3336 	pr_info("fbcon: Taking over console\n");
3337 
3338 	dummycon_unregister_output_notifier(&fbcon_output_nb);
3339 
3340 	/* We may get called in atomic context */
3341 	schedule_work(&fbcon_deferred_takeover_work);
3342 
3343 	return NOTIFY_OK;
3344 }
3345 #endif
3346 
fbcon_start(void)3347 static void fbcon_start(void)
3348 {
3349 	WARN_CONSOLE_UNLOCKED();
3350 
3351 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3352 	if (conswitchp != &dummy_con)
3353 		deferred_takeover = false;
3354 
3355 	if (deferred_takeover) {
3356 		fbcon_output_nb.notifier_call = fbcon_output_notifier;
3357 		dummycon_register_output_notifier(&fbcon_output_nb);
3358 		return;
3359 	}
3360 #endif
3361 
3362 	if (num_registered_fb) {
3363 		int i;
3364 
3365 		for_each_registered_fb(i) {
3366 			info_idx = i;
3367 			break;
3368 		}
3369 
3370 		do_fbcon_takeover(0);
3371 	}
3372 }
3373 
fbcon_exit(void)3374 static void fbcon_exit(void)
3375 {
3376 	struct fb_info *info;
3377 	int i, j, mapped;
3378 
3379 #ifdef CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER
3380 	if (deferred_takeover) {
3381 		dummycon_unregister_output_notifier(&fbcon_output_nb);
3382 		deferred_takeover = false;
3383 	}
3384 #endif
3385 
3386 	for_each_registered_fb(i) {
3387 		int pending = 0;
3388 
3389 		mapped = 0;
3390 		info = registered_fb[i];
3391 
3392 		if (info->queue.func)
3393 			pending = cancel_work_sync(&info->queue);
3394 		pr_debug("fbcon: %s pending work\n", (pending ? "canceled" : "no"));
3395 
3396 		for (j = first_fb_vc; j <= last_fb_vc; j++) {
3397 			if (con2fb_map[j] == i) {
3398 				mapped = 1;
3399 				con2fb_map[j] = -1;
3400 			}
3401 		}
3402 
3403 		if (mapped) {
3404 			if (info->fbops->fb_release)
3405 				info->fbops->fb_release(info, 0);
3406 			module_put(info->fbops->owner);
3407 
3408 			if (info->fbcon_par) {
3409 				struct fbcon_ops *ops = info->fbcon_par;
3410 
3411 				fbcon_del_cursor_timer(info);
3412 				kfree(ops->cursor_src);
3413 				kfree(ops->cursor_state.mask);
3414 				kfree(info->fbcon_par);
3415 				info->fbcon_par = NULL;
3416 			}
3417 
3418 			if (info->queue.func == fb_flashcursor)
3419 				info->queue.func = NULL;
3420 		}
3421 	}
3422 }
3423 
fb_console_init(void)3424 void __init fb_console_init(void)
3425 {
3426 	int i;
3427 
3428 	console_lock();
3429 	fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
3430 				     "fbcon");
3431 
3432 	if (IS_ERR(fbcon_device)) {
3433 		printk(KERN_WARNING "Unable to create device "
3434 		       "for fbcon; errno = %ld\n",
3435 		       PTR_ERR(fbcon_device));
3436 		fbcon_device = NULL;
3437 	} else
3438 		fbcon_init_device();
3439 
3440 	for (i = 0; i < MAX_NR_CONSOLES; i++)
3441 		con2fb_map[i] = -1;
3442 
3443 	fbcon_start();
3444 	console_unlock();
3445 }
3446 
3447 #ifdef MODULE
3448 
fbcon_deinit_device(void)3449 static void __exit fbcon_deinit_device(void)
3450 {
3451 	int i;
3452 
3453 	if (fbcon_has_sysfs) {
3454 		for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3455 			device_remove_file(fbcon_device, &device_attrs[i]);
3456 
3457 		fbcon_has_sysfs = 0;
3458 	}
3459 }
3460 
fb_console_exit(void)3461 void __exit fb_console_exit(void)
3462 {
3463 	console_lock();
3464 	fbcon_deinit_device();
3465 	device_destroy(fb_class, MKDEV(0, 0));
3466 	fbcon_exit();
3467 	do_unregister_con_driver(&fb_con);
3468 	console_unlock();
3469 }
3470 #endif
3471