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