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