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