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