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