linux/drivers/staging/panel/panel.c
<<
>>
Prefs
   1/*
   2 * Front panel driver for Linux
   3 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
   4 *
   5 * This program is free software; you can redistribute it and/or
   6 * modify it under the terms of the GNU General Public License
   7 * as published by the Free Software Foundation; either version
   8 * 2 of the License, or (at your option) any later version.
   9 *
  10 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
  11 * connected to a parallel printer port.
  12 *
  13 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
  14 * serial module compatible with Samsung's KS0074. The pins may be connected in
  15 * any combination, everything is programmable.
  16 *
  17 * The keypad consists in a matrix of push buttons connecting input pins to
  18 * data output pins or to the ground. The combinations have to be hard-coded
  19 * in the driver, though several profiles exist and adding new ones is easy.
  20 *
  21 * Several profiles are provided for commonly found LCD+keypad modules on the
  22 * market, such as those found in Nexcom's appliances.
  23 *
  24 * FIXME:
  25 *      - the initialization/deinitialization process is very dirty and should
  26 *        be rewritten. It may even be buggy.
  27 *
  28 * TODO:
  29 *      - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
  30 *      - make the LCD a part of a virtual screen of Vx*Vy
  31 *      - make the inputs list smp-safe
  32 *      - change the keyboard to a double mapping : signals -> key_id -> values
  33 *        so that applications can change values without knowing signals
  34 *
  35 */
  36
  37#include <linux/module.h>
  38
  39#include <linux/types.h>
  40#include <linux/errno.h>
  41#include <linux/signal.h>
  42#include <linux/sched.h>
  43#include <linux/spinlock.h>
  44#include <linux/interrupt.h>
  45#include <linux/miscdevice.h>
  46#include <linux/slab.h>
  47#include <linux/ioport.h>
  48#include <linux/fcntl.h>
  49#include <linux/init.h>
  50#include <linux/delay.h>
  51#include <linux/kernel.h>
  52#include <linux/ctype.h>
  53#include <linux/parport.h>
  54#include <linux/list.h>
  55#include <linux/notifier.h>
  56#include <linux/reboot.h>
  57#include <generated/utsrelease.h>
  58
  59#include <linux/io.h>
  60#include <linux/uaccess.h>
  61
  62#define LCD_MINOR               156
  63#define KEYPAD_MINOR            185
  64
  65#define PANEL_VERSION           "0.9.5"
  66
  67#define LCD_MAXBYTES            256     /* max burst write */
  68
  69#define KEYPAD_BUFFER           64
  70
  71/* poll the keyboard this every second */
  72#define INPUT_POLL_TIME         (HZ/50)
  73/* a key starts to repeat after this times INPUT_POLL_TIME */
  74#define KEYPAD_REP_START        (10)
  75/* a key repeats this times INPUT_POLL_TIME */
  76#define KEYPAD_REP_DELAY        (2)
  77
  78/* keep the light on this times INPUT_POLL_TIME for each flash */
  79#define FLASH_LIGHT_TEMPO       (200)
  80
  81/* converts an r_str() input to an active high, bits string : 000BAOSE */
  82#define PNL_PINPUT(a)           ((((unsigned char)(a)) ^ 0x7F) >> 3)
  83
  84#define PNL_PBUSY               0x80    /* inverted input, active low */
  85#define PNL_PACK                0x40    /* direct input, active low */
  86#define PNL_POUTPA              0x20    /* direct input, active high */
  87#define PNL_PSELECD             0x10    /* direct input, active high */
  88#define PNL_PERRORP             0x08    /* direct input, active low */
  89
  90#define PNL_PBIDIR              0x20    /* bi-directional ports */
  91/* high to read data in or-ed with data out */
  92#define PNL_PINTEN              0x10
  93#define PNL_PSELECP             0x08    /* inverted output, active low */
  94#define PNL_PINITP              0x04    /* direct output, active low */
  95#define PNL_PAUTOLF             0x02    /* inverted output, active low */
  96#define PNL_PSTROBE             0x01    /* inverted output */
  97
  98#define PNL_PD0                 0x01
  99#define PNL_PD1                 0x02
 100#define PNL_PD2                 0x04
 101#define PNL_PD3                 0x08
 102#define PNL_PD4                 0x10
 103#define PNL_PD5                 0x20
 104#define PNL_PD6                 0x40
 105#define PNL_PD7                 0x80
 106
 107#define PIN_NONE                0
 108#define PIN_STROBE              1
 109#define PIN_D0                  2
 110#define PIN_D1                  3
 111#define PIN_D2                  4
 112#define PIN_D3                  5
 113#define PIN_D4                  6
 114#define PIN_D5                  7
 115#define PIN_D6                  8
 116#define PIN_D7                  9
 117#define PIN_AUTOLF              14
 118#define PIN_INITP               16
 119#define PIN_SELECP              17
 120#define PIN_NOT_SET             127
 121
 122#define LCD_FLAG_S              0x0001
 123#define LCD_FLAG_ID             0x0002
 124#define LCD_FLAG_B              0x0004  /* blink on */
 125#define LCD_FLAG_C              0x0008  /* cursor on */
 126#define LCD_FLAG_D              0x0010  /* display on */
 127#define LCD_FLAG_F              0x0020  /* large font mode */
 128#define LCD_FLAG_N              0x0040  /* 2-rows mode */
 129#define LCD_FLAG_L              0x0080  /* backlight enabled */
 130
 131#define LCD_ESCAPE_LEN          24      /* max chars for LCD escape command */
 132#define LCD_ESCAPE_CHAR 27      /* use char 27 for escape command */
 133
 134/* macros to simplify use of the parallel port */
 135#define r_ctr(x)        (parport_read_control((x)->port))
 136#define r_dtr(x)        (parport_read_data((x)->port))
 137#define r_str(x)        (parport_read_status((x)->port))
 138#define w_ctr(x, y)     do { parport_write_control((x)->port, (y)); } while (0)
 139#define w_dtr(x, y)     do { parport_write_data((x)->port, (y)); } while (0)
 140
 141/* this defines which bits are to be used and which ones to be ignored */
 142/* logical or of the output bits involved in the scan matrix */
 143static __u8 scan_mask_o;
 144/* logical or of the input bits involved in the scan matrix */
 145static __u8 scan_mask_i;
 146
 147typedef __u64 pmask_t;
 148
 149enum input_type {
 150        INPUT_TYPE_STD,
 151        INPUT_TYPE_KBD,
 152};
 153
 154enum input_state {
 155        INPUT_ST_LOW,
 156        INPUT_ST_RISING,
 157        INPUT_ST_HIGH,
 158        INPUT_ST_FALLING,
 159};
 160
 161struct logical_input {
 162        struct list_head list;
 163        pmask_t mask;
 164        pmask_t value;
 165        enum input_type type;
 166        enum input_state state;
 167        __u8 rise_time, fall_time;
 168        __u8 rise_timer, fall_timer, high_timer;
 169
 170        union {
 171                struct {        /* valid when type == INPUT_TYPE_STD */
 172                        void (*press_fct) (int);
 173                        void (*release_fct) (int);
 174                        int press_data;
 175                        int release_data;
 176                } std;
 177                struct {        /* valid when type == INPUT_TYPE_KBD */
 178                        /* strings can be non null-terminated */
 179                        char press_str[sizeof(void *) + sizeof(int)];
 180                        char repeat_str[sizeof(void *) + sizeof(int)];
 181                        char release_str[sizeof(void *) + sizeof(int)];
 182                } kbd;
 183        } u;
 184};
 185
 186LIST_HEAD(logical_inputs);      /* list of all defined logical inputs */
 187
 188/* physical contacts history
 189 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
 190 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
 191 * corresponds to the ground.
 192 * Within each group, bits are stored in the same order as read on the port :
 193 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
 194 * So, each __u64 (or pmask_t) is represented like this :
 195 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
 196 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
 197 */
 198
 199/* what has just been read from the I/O ports */
 200static pmask_t phys_read;
 201/* previous phys_read */
 202static pmask_t phys_read_prev;
 203/* stabilized phys_read (phys_read|phys_read_prev) */
 204static pmask_t phys_curr;
 205/* previous phys_curr */
 206static pmask_t phys_prev;
 207/* 0 means that at least one logical signal needs be computed */
 208static char inputs_stable;
 209
 210/* these variables are specific to the keypad */
 211static char keypad_buffer[KEYPAD_BUFFER];
 212static int keypad_buflen;
 213static int keypad_start;
 214static char keypressed;
 215static wait_queue_head_t keypad_read_wait;
 216
 217/* lcd-specific variables */
 218
 219/* contains the LCD config state */
 220static unsigned long int lcd_flags;
 221/* contains the LCD X offset */
 222static unsigned long int lcd_addr_x;
 223/* contains the LCD Y offset */
 224static unsigned long int lcd_addr_y;
 225/* current escape sequence, 0 terminated */
 226static char lcd_escape[LCD_ESCAPE_LEN + 1];
 227/* not in escape state. >=0 = escape cmd len */
 228static int lcd_escape_len = -1;
 229
 230/*
 231 * Bit masks to convert LCD signals to parallel port outputs.
 232 * _d_ are values for data port, _c_ are for control port.
 233 * [0] = signal OFF, [1] = signal ON, [2] = mask
 234 */
 235#define BIT_CLR         0
 236#define BIT_SET         1
 237#define BIT_MSK         2
 238#define BIT_STATES      3
 239/*
 240 * one entry for each bit on the LCD
 241 */
 242#define LCD_BIT_E       0
 243#define LCD_BIT_RS      1
 244#define LCD_BIT_RW      2
 245#define LCD_BIT_BL      3
 246#define LCD_BIT_CL      4
 247#define LCD_BIT_DA      5
 248#define LCD_BITS        6
 249
 250/*
 251 * each bit can be either connected to a DATA or CTRL port
 252 */
 253#define LCD_PORT_C      0
 254#define LCD_PORT_D      1
 255#define LCD_PORTS       2
 256
 257static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
 258
 259/*
 260 * LCD protocols
 261 */
 262#define LCD_PROTO_PARALLEL      0
 263#define LCD_PROTO_SERIAL        1
 264#define LCD_PROTO_TI_DA8XX_LCD  2
 265
 266/*
 267 * LCD character sets
 268 */
 269#define LCD_CHARSET_NORMAL      0
 270#define LCD_CHARSET_KS0074      1
 271
 272/*
 273 * LCD types
 274 */
 275#define LCD_TYPE_NONE           0
 276#define LCD_TYPE_OLD            1
 277#define LCD_TYPE_KS0074         2
 278#define LCD_TYPE_HANTRONIX      3
 279#define LCD_TYPE_NEXCOM         4
 280#define LCD_TYPE_CUSTOM         5
 281
 282/*
 283 * keypad types
 284 */
 285#define KEYPAD_TYPE_NONE        0
 286#define KEYPAD_TYPE_OLD         1
 287#define KEYPAD_TYPE_NEW         2
 288#define KEYPAD_TYPE_NEXCOM      3
 289
 290/*
 291 * panel profiles
 292 */
 293#define PANEL_PROFILE_CUSTOM    0
 294#define PANEL_PROFILE_OLD       1
 295#define PANEL_PROFILE_NEW       2
 296#define PANEL_PROFILE_HANTRONIX 3
 297#define PANEL_PROFILE_NEXCOM    4
 298#define PANEL_PROFILE_LARGE     5
 299
 300/*
 301 * Construct custom config from the kernel's configuration
 302 */
 303#define DEFAULT_PROFILE         PANEL_PROFILE_LARGE
 304#define DEFAULT_PARPORT         0
 305#define DEFAULT_LCD             LCD_TYPE_OLD
 306#define DEFAULT_KEYPAD          KEYPAD_TYPE_OLD
 307#define DEFAULT_LCD_WIDTH       40
 308#define DEFAULT_LCD_BWIDTH      40
 309#define DEFAULT_LCD_HWIDTH      64
 310#define DEFAULT_LCD_HEIGHT      2
 311#define DEFAULT_LCD_PROTO       LCD_PROTO_PARALLEL
 312
 313#define DEFAULT_LCD_PIN_E       PIN_AUTOLF
 314#define DEFAULT_LCD_PIN_RS      PIN_SELECP
 315#define DEFAULT_LCD_PIN_RW      PIN_INITP
 316#define DEFAULT_LCD_PIN_SCL     PIN_STROBE
 317#define DEFAULT_LCD_PIN_SDA     PIN_D0
 318#define DEFAULT_LCD_PIN_BL      PIN_NOT_SET
 319#define DEFAULT_LCD_CHARSET     LCD_CHARSET_NORMAL
 320
 321#ifdef CONFIG_PANEL_PROFILE
 322#undef DEFAULT_PROFILE
 323#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
 324#endif
 325
 326#ifdef CONFIG_PANEL_PARPORT
 327#undef DEFAULT_PARPORT
 328#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
 329#endif
 330
 331#if DEFAULT_PROFILE == 0        /* custom */
 332#ifdef CONFIG_PANEL_KEYPAD
 333#undef DEFAULT_KEYPAD
 334#define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
 335#endif
 336
 337#ifdef CONFIG_PANEL_LCD
 338#undef DEFAULT_LCD
 339#define DEFAULT_LCD CONFIG_PANEL_LCD
 340#endif
 341
 342#ifdef CONFIG_PANEL_LCD_WIDTH
 343#undef DEFAULT_LCD_WIDTH
 344#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
 345#endif
 346
 347#ifdef CONFIG_PANEL_LCD_BWIDTH
 348#undef DEFAULT_LCD_BWIDTH
 349#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
 350#endif
 351
 352#ifdef CONFIG_PANEL_LCD_HWIDTH
 353#undef DEFAULT_LCD_HWIDTH
 354#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
 355#endif
 356
 357#ifdef CONFIG_PANEL_LCD_HEIGHT
 358#undef DEFAULT_LCD_HEIGHT
 359#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
 360#endif
 361
 362#ifdef CONFIG_PANEL_LCD_PROTO
 363#undef DEFAULT_LCD_PROTO
 364#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
 365#endif
 366
 367#ifdef CONFIG_PANEL_LCD_PIN_E
 368#undef DEFAULT_LCD_PIN_E
 369#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
 370#endif
 371
 372#ifdef CONFIG_PANEL_LCD_PIN_RS
 373#undef DEFAULT_LCD_PIN_RS
 374#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
 375#endif
 376
 377#ifdef CONFIG_PANEL_LCD_PIN_RW
 378#undef DEFAULT_LCD_PIN_RW
 379#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
 380#endif
 381
 382#ifdef CONFIG_PANEL_LCD_PIN_SCL
 383#undef DEFAULT_LCD_PIN_SCL
 384#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
 385#endif
 386
 387#ifdef CONFIG_PANEL_LCD_PIN_SDA
 388#undef DEFAULT_LCD_PIN_SDA
 389#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
 390#endif
 391
 392#ifdef CONFIG_PANEL_LCD_PIN_BL
 393#undef DEFAULT_LCD_PIN_BL
 394#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
 395#endif
 396
 397#ifdef CONFIG_PANEL_LCD_CHARSET
 398#undef DEFAULT_LCD_CHARSET
 399#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
 400#endif
 401
 402#endif /* DEFAULT_PROFILE == 0 */
 403
 404/* global variables */
 405static int keypad_open_cnt;     /* #times opened */
 406static int lcd_open_cnt;        /* #times opened */
 407static struct pardevice *pprt;
 408
 409static int lcd_initialized;
 410static int keypad_initialized;
 411
 412static int light_tempo;
 413
 414static char lcd_must_clear;
 415static char lcd_left_shift;
 416static char init_in_progress;
 417
 418static void (*lcd_write_cmd) (int);
 419static void (*lcd_write_data) (int);
 420static void (*lcd_clear_fast) (void);
 421
 422static DEFINE_SPINLOCK(pprt_lock);
 423static struct timer_list scan_timer;
 424
 425MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
 426
 427static int parport = -1;
 428module_param(parport, int, 0000);
 429MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
 430
 431static int lcd_height = -1;
 432module_param(lcd_height, int, 0000);
 433MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
 434
 435static int lcd_width = -1;
 436module_param(lcd_width, int, 0000);
 437MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
 438
 439static int lcd_bwidth = -1;     /* internal buffer width (usually 40) */
 440module_param(lcd_bwidth, int, 0000);
 441MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
 442
 443static int lcd_hwidth = -1;     /* hardware buffer width (usually 64) */
 444module_param(lcd_hwidth, int, 0000);
 445MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
 446
 447static int lcd_enabled = -1;
 448module_param(lcd_enabled, int, 0000);
 449MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
 450
 451static int keypad_enabled = -1;
 452module_param(keypad_enabled, int, 0000);
 453MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
 454
 455static int lcd_type = -1;
 456module_param(lcd_type, int, 0000);
 457MODULE_PARM_DESC(lcd_type,
 458                 "LCD type: 0=none, 1=old //, 2=serial ks0074, "
 459                 "3=hantronix //, 4=nexcom //, 5=compiled-in");
 460
 461static int lcd_proto = -1;
 462module_param(lcd_proto, int, 0000);
 463MODULE_PARM_DESC(lcd_proto,
 464                "LCD communication: 0=parallel (//), 1=serial,"
 465                "2=TI LCD Interface");
 466
 467static int lcd_charset = -1;
 468module_param(lcd_charset, int, 0000);
 469MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
 470
 471static int keypad_type = -1;
 472module_param(keypad_type, int, 0000);
 473MODULE_PARM_DESC(keypad_type,
 474                 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, "
 475                 "3=nexcom 4 keys");
 476
 477static int profile = DEFAULT_PROFILE;
 478module_param(profile, int, 0000);
 479MODULE_PARM_DESC(profile,
 480                 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
 481                 "4=16x2 nexcom; default=40x2, old kp");
 482
 483/*
 484 * These are the parallel port pins the LCD control signals are connected to.
 485 * Set this to 0 if the signal is not used. Set it to its opposite value
 486 * (negative) if the signal is negated. -MAXINT is used to indicate that the
 487 * pin has not been explicitly specified.
 488 *
 489 * WARNING! no check will be performed about collisions with keypad !
 490 */
 491
 492static int lcd_e_pin  = PIN_NOT_SET;
 493module_param(lcd_e_pin, int, 0000);
 494MODULE_PARM_DESC(lcd_e_pin,
 495                 "# of the // port pin connected to LCD 'E' signal, "
 496                 "with polarity (-17..17)");
 497
 498static int lcd_rs_pin = PIN_NOT_SET;
 499module_param(lcd_rs_pin, int, 0000);
 500MODULE_PARM_DESC(lcd_rs_pin,
 501                 "# of the // port pin connected to LCD 'RS' signal, "
 502                 "with polarity (-17..17)");
 503
 504static int lcd_rw_pin = PIN_NOT_SET;
 505module_param(lcd_rw_pin, int, 0000);
 506MODULE_PARM_DESC(lcd_rw_pin,
 507                 "# of the // port pin connected to LCD 'RW' signal, "
 508                 "with polarity (-17..17)");
 509
 510static int lcd_bl_pin = PIN_NOT_SET;
 511module_param(lcd_bl_pin, int, 0000);
 512MODULE_PARM_DESC(lcd_bl_pin,
 513                 "# of the // port pin connected to LCD backlight, "
 514                 "with polarity (-17..17)");
 515
 516static int lcd_da_pin = PIN_NOT_SET;
 517module_param(lcd_da_pin, int, 0000);
 518MODULE_PARM_DESC(lcd_da_pin,
 519                 "# of the // port pin connected to serial LCD 'SDA' "
 520                 "signal, with polarity (-17..17)");
 521
 522static int lcd_cl_pin = PIN_NOT_SET;
 523module_param(lcd_cl_pin, int, 0000);
 524MODULE_PARM_DESC(lcd_cl_pin,
 525                 "# of the // port pin connected to serial LCD 'SCL' "
 526                 "signal, with polarity (-17..17)");
 527
 528static unsigned char *lcd_char_conv;
 529
 530/* for some LCD drivers (ks0074) we need a charset conversion table. */
 531static unsigned char lcd_char_conv_ks0074[256] = {
 532        /*          0|8   1|9   2|A   3|B   4|C   5|D   6|E   7|F */
 533        /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 534        /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 535        /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 536        /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
 537        /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
 538        /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
 539        /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
 540        /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
 541        /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
 542        /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
 543        /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
 544        /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
 545        /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
 546        /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
 547        /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
 548        /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
 549        /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
 550        /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
 551        /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
 552        /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
 553        /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
 554        /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
 555        /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
 556        /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
 557        /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
 558        /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
 559        /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
 560        /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
 561        /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
 562        /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
 563        /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
 564        /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
 565};
 566
 567char old_keypad_profile[][4][9] = {
 568        {"S0", "Left\n", "Left\n", ""},
 569        {"S1", "Down\n", "Down\n", ""},
 570        {"S2", "Up\n", "Up\n", ""},
 571        {"S3", "Right\n", "Right\n", ""},
 572        {"S4", "Esc\n", "Esc\n", ""},
 573        {"S5", "Ret\n", "Ret\n", ""},
 574        {"", "", "", ""}
 575};
 576
 577/* signals, press, repeat, release */
 578char new_keypad_profile[][4][9] = {
 579        {"S0", "Left\n", "Left\n", ""},
 580        {"S1", "Down\n", "Down\n", ""},
 581        {"S2", "Up\n", "Up\n", ""},
 582        {"S3", "Right\n", "Right\n", ""},
 583        {"S4s5", "", "Esc\n", "Esc\n"},
 584        {"s4S5", "", "Ret\n", "Ret\n"},
 585        {"S4S5", "Help\n", "", ""},
 586        /* add new signals above this line */
 587        {"", "", "", ""}
 588};
 589
 590/* signals, press, repeat, release */
 591char nexcom_keypad_profile[][4][9] = {
 592        {"a-p-e-", "Down\n", "Down\n", ""},
 593        {"a-p-E-", "Ret\n", "Ret\n", ""},
 594        {"a-P-E-", "Esc\n", "Esc\n", ""},
 595        {"a-P-e-", "Up\n", "Up\n", ""},
 596        /* add new signals above this line */
 597        {"", "", "", ""}
 598};
 599
 600static char (*keypad_profile)[4][9] = old_keypad_profile;
 601
 602/* FIXME: this should be converted to a bit array containing signals states */
 603static struct {
 604        unsigned char e;  /* parallel LCD E (data latch on falling edge) */
 605        unsigned char rs; /* parallel LCD RS  (0 = cmd, 1 = data) */
 606        unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
 607        unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
 608        unsigned char cl; /* serial LCD clock (latch on rising edge) */
 609        unsigned char da; /* serial LCD data */
 610} bits;
 611
 612static void init_scan_timer(void);
 613
 614/* sets data port bits according to current signals values */
 615static int set_data_bits(void)
 616{
 617        int val, bit;
 618
 619        val = r_dtr(pprt);
 620        for (bit = 0; bit < LCD_BITS; bit++)
 621                val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
 622
 623        val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
 624            | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
 625            | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
 626            | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
 627            | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
 628            | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
 629
 630        w_dtr(pprt, val);
 631        return val;
 632}
 633
 634/* sets ctrl port bits according to current signals values */
 635static int set_ctrl_bits(void)
 636{
 637        int val, bit;
 638
 639        val = r_ctr(pprt);
 640        for (bit = 0; bit < LCD_BITS; bit++)
 641                val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
 642
 643        val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
 644            | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
 645            | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
 646            | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
 647            | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
 648            | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
 649
 650        w_ctr(pprt, val);
 651        return val;
 652}
 653
 654/* sets ctrl & data port bits according to current signals values */
 655static void panel_set_bits(void)
 656{
 657        set_data_bits();
 658        set_ctrl_bits();
 659}
 660
 661/*
 662 * Converts a parallel port pin (from -25 to 25) to data and control ports
 663 * masks, and data and control port bits. The signal will be considered
 664 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
 665 *
 666 * Result will be used this way :
 667 *   out(dport, in(dport) & d_val[2] | d_val[signal_state])
 668 *   out(cport, in(cport) & c_val[2] | c_val[signal_state])
 669 */
 670void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
 671{
 672        int d_bit, c_bit, inv;
 673
 674        d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
 675        d_val[2] = c_val[2] = 0xFF;
 676
 677        if (pin == 0)
 678                return;
 679
 680        inv = (pin < 0);
 681        if (inv)
 682                pin = -pin;
 683
 684        d_bit = c_bit = 0;
 685
 686        switch (pin) {
 687        case PIN_STROBE:        /* strobe, inverted */
 688                c_bit = PNL_PSTROBE;
 689                inv = !inv;
 690                break;
 691        case PIN_D0...PIN_D7:   /* D0 - D7 = 2 - 9 */
 692                d_bit = 1 << (pin - 2);
 693                break;
 694        case PIN_AUTOLF:        /* autofeed, inverted */
 695                c_bit = PNL_PAUTOLF;
 696                inv = !inv;
 697                break;
 698        case PIN_INITP:         /* init, direct */
 699                c_bit = PNL_PINITP;
 700                break;
 701        case PIN_SELECP:        /* select_in, inverted */
 702                c_bit = PNL_PSELECP;
 703                inv = !inv;
 704                break;
 705        default:                /* unknown pin, ignore */
 706                break;
 707        }
 708
 709        if (c_bit) {
 710                c_val[2] &= ~c_bit;
 711                c_val[!inv] = c_bit;
 712        } else if (d_bit) {
 713                d_val[2] &= ~d_bit;
 714                d_val[!inv] = d_bit;
 715        }
 716}
 717
 718/* sleeps that many milliseconds with a reschedule */
 719static void long_sleep(int ms)
 720{
 721
 722        if (in_interrupt())
 723                mdelay(ms);
 724        else {
 725                current->state = TASK_INTERRUPTIBLE;
 726                schedule_timeout((ms * HZ + 999) / 1000);
 727        }
 728}
 729
 730/* send a serial byte to the LCD panel. The caller is responsible for locking
 731   if needed. */
 732static void lcd_send_serial(int byte)
 733{
 734        int bit;
 735
 736        /* the data bit is set on D0, and the clock on STROBE.
 737         * LCD reads D0 on STROBE's rising edge. */
 738        for (bit = 0; bit < 8; bit++) {
 739                bits.cl = BIT_CLR;      /* CLK low */
 740                panel_set_bits();
 741                bits.da = byte & 1;
 742                panel_set_bits();
 743                udelay(2);  /* maintain the data during 2 us before CLK up */
 744                bits.cl = BIT_SET;      /* CLK high */
 745                panel_set_bits();
 746                udelay(1);  /* maintain the strobe during 1 us */
 747                byte >>= 1;
 748        }
 749}
 750
 751/* turn the backlight on or off */
 752static void lcd_backlight(int on)
 753{
 754        if (lcd_bl_pin == PIN_NONE)
 755                return;
 756
 757        /* The backlight is activated by seting the AUTOFEED line to +5V  */
 758        spin_lock(&pprt_lock);
 759        bits.bl = on;
 760        panel_set_bits();
 761        spin_unlock(&pprt_lock);
 762}
 763
 764/* send a command to the LCD panel in serial mode */
 765static void lcd_write_cmd_s(int cmd)
 766{
 767        spin_lock(&pprt_lock);
 768        lcd_send_serial(0x1F);  /* R/W=W, RS=0 */
 769        lcd_send_serial(cmd & 0x0F);
 770        lcd_send_serial((cmd >> 4) & 0x0F);
 771        udelay(40);             /* the shortest command takes at least 40 us */
 772        spin_unlock(&pprt_lock);
 773}
 774
 775/* send data to the LCD panel in serial mode */
 776static void lcd_write_data_s(int data)
 777{
 778        spin_lock(&pprt_lock);
 779        lcd_send_serial(0x5F);  /* R/W=W, RS=1 */
 780        lcd_send_serial(data & 0x0F);
 781        lcd_send_serial((data >> 4) & 0x0F);
 782        udelay(40);             /* the shortest data takes at least 40 us */
 783        spin_unlock(&pprt_lock);
 784}
 785
 786/* send a command to the LCD panel in 8 bits parallel mode */
 787static void lcd_write_cmd_p8(int cmd)
 788{
 789        spin_lock(&pprt_lock);
 790        /* present the data to the data port */
 791        w_dtr(pprt, cmd);
 792        udelay(20);     /* maintain the data during 20 us before the strobe */
 793
 794        bits.e = BIT_SET;
 795        bits.rs = BIT_CLR;
 796        bits.rw = BIT_CLR;
 797        set_ctrl_bits();
 798
 799        udelay(40);     /* maintain the strobe during 40 us */
 800
 801        bits.e = BIT_CLR;
 802        set_ctrl_bits();
 803
 804        udelay(120);    /* the shortest command takes at least 120 us */
 805        spin_unlock(&pprt_lock);
 806}
 807
 808/* send data to the LCD panel in 8 bits parallel mode */
 809static void lcd_write_data_p8(int data)
 810{
 811        spin_lock(&pprt_lock);
 812        /* present the data to the data port */
 813        w_dtr(pprt, data);
 814        udelay(20);     /* maintain the data during 20 us before the strobe */
 815
 816        bits.e = BIT_SET;
 817        bits.rs = BIT_SET;
 818        bits.rw = BIT_CLR;
 819        set_ctrl_bits();
 820
 821        udelay(40);     /* maintain the strobe during 40 us */
 822
 823        bits.e = BIT_CLR;
 824        set_ctrl_bits();
 825
 826        udelay(45);     /* the shortest data takes at least 45 us */
 827        spin_unlock(&pprt_lock);
 828}
 829
 830/* send a command to the TI LCD panel */
 831static void lcd_write_cmd_tilcd(int cmd)
 832{
 833        spin_lock(&pprt_lock);
 834        /* present the data to the control port */
 835        w_ctr(pprt, cmd);
 836        udelay(60);
 837        spin_unlock(&pprt_lock);
 838}
 839
 840/* send data to the TI LCD panel */
 841static void lcd_write_data_tilcd(int data)
 842{
 843        spin_lock(&pprt_lock);
 844        /* present the data to the data port */
 845        w_dtr(pprt, data);
 846        udelay(60);
 847        spin_unlock(&pprt_lock);
 848}
 849
 850static void lcd_gotoxy(void)
 851{
 852        lcd_write_cmd(0x80      /* set DDRAM address */
 853                      | (lcd_addr_y ? lcd_hwidth : 0)
 854                      /* we force the cursor to stay at the end of the
 855                         line if it wants to go farther */
 856                      | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
 857                         (lcd_hwidth - 1) : lcd_bwidth - 1));
 858}
 859
 860static void lcd_print(char c)
 861{
 862        if (lcd_addr_x < lcd_bwidth) {
 863                if (lcd_char_conv != NULL)
 864                        c = lcd_char_conv[(unsigned char)c];
 865                lcd_write_data(c);
 866                lcd_addr_x++;
 867        }
 868        /* prevents the cursor from wrapping onto the next line */
 869        if (lcd_addr_x == lcd_bwidth)
 870                lcd_gotoxy();
 871}
 872
 873/* fills the display with spaces and resets X/Y */
 874static void lcd_clear_fast_s(void)
 875{
 876        int pos;
 877        lcd_addr_x = lcd_addr_y = 0;
 878        lcd_gotoxy();
 879
 880        spin_lock(&pprt_lock);
 881        for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
 882                lcd_send_serial(0x5F);  /* R/W=W, RS=1 */
 883                lcd_send_serial(' ' & 0x0F);
 884                lcd_send_serial((' ' >> 4) & 0x0F);
 885                udelay(40);     /* the shortest data takes at least 40 us */
 886        }
 887        spin_unlock(&pprt_lock);
 888
 889        lcd_addr_x = lcd_addr_y = 0;
 890        lcd_gotoxy();
 891}
 892
 893/* fills the display with spaces and resets X/Y */
 894static void lcd_clear_fast_p8(void)
 895{
 896        int pos;
 897        lcd_addr_x = lcd_addr_y = 0;
 898        lcd_gotoxy();
 899
 900        spin_lock(&pprt_lock);
 901        for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
 902                /* present the data to the data port */
 903                w_dtr(pprt, ' ');
 904
 905                /* maintain the data during 20 us before the strobe */
 906                udelay(20);
 907
 908                bits.e = BIT_SET;
 909                bits.rs = BIT_SET;
 910                bits.rw = BIT_CLR;
 911                set_ctrl_bits();
 912
 913                /* maintain the strobe during 40 us */
 914                udelay(40);
 915
 916                bits.e = BIT_CLR;
 917                set_ctrl_bits();
 918
 919                /* the shortest data takes at least 45 us */
 920                udelay(45);
 921        }
 922        spin_unlock(&pprt_lock);
 923
 924        lcd_addr_x = lcd_addr_y = 0;
 925        lcd_gotoxy();
 926}
 927
 928/* fills the display with spaces and resets X/Y */
 929static void lcd_clear_fast_tilcd(void)
 930{
 931        int pos;
 932        lcd_addr_x = lcd_addr_y = 0;
 933        lcd_gotoxy();
 934
 935        spin_lock(&pprt_lock);
 936        for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
 937                /* present the data to the data port */
 938                w_dtr(pprt, ' ');
 939                udelay(60);
 940        }
 941
 942        spin_unlock(&pprt_lock);
 943
 944        lcd_addr_x = lcd_addr_y = 0;
 945        lcd_gotoxy();
 946}
 947
 948/* clears the display and resets X/Y */
 949static void lcd_clear_display(void)
 950{
 951        lcd_write_cmd(0x01);    /* clear display */
 952        lcd_addr_x = lcd_addr_y = 0;
 953        /* we must wait a few milliseconds (15) */
 954        long_sleep(15);
 955}
 956
 957static void lcd_init_display(void)
 958{
 959
 960        lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
 961            | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
 962
 963        long_sleep(20);         /* wait 20 ms after power-up for the paranoid */
 964
 965        lcd_write_cmd(0x30);    /* 8bits, 1 line, small fonts */
 966        long_sleep(10);
 967        lcd_write_cmd(0x30);    /* 8bits, 1 line, small fonts */
 968        long_sleep(10);
 969        lcd_write_cmd(0x30);    /* 8bits, 1 line, small fonts */
 970        long_sleep(10);
 971
 972        lcd_write_cmd(0x30      /* set font height and lines number */
 973                      | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
 974                      | ((lcd_flags & LCD_FLAG_N) ? 8 : 0)
 975            );
 976        long_sleep(10);
 977
 978        lcd_write_cmd(0x08);    /* display off, cursor off, blink off */
 979        long_sleep(10);
 980
 981        lcd_write_cmd(0x08      /* set display mode */
 982                      | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
 983                      | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
 984                      | ((lcd_flags & LCD_FLAG_B) ? 1 : 0)
 985            );
 986
 987        lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
 988
 989        long_sleep(10);
 990
 991        /* entry mode set : increment, cursor shifting */
 992        lcd_write_cmd(0x06);
 993
 994        lcd_clear_display();
 995}
 996
 997/*
 998 * These are the file operation function for user access to /dev/lcd
 999 * This function can also be called from inside the kernel, by
1000 * setting file and ppos to NULL.
1001 *
1002 */
1003
1004static inline int handle_lcd_special_code(void)
1005{
1006        /* LCD special codes */
1007
1008        int processed = 0;
1009
1010        char *esc = lcd_escape + 2;
1011        int oldflags = lcd_flags;
1012
1013        /* check for display mode flags */
1014        switch (*esc) {
1015        case 'D':       /* Display ON */
1016                lcd_flags |= LCD_FLAG_D;
1017                processed = 1;
1018                break;
1019        case 'd':       /* Display OFF */
1020                lcd_flags &= ~LCD_FLAG_D;
1021                processed = 1;
1022                break;
1023        case 'C':       /* Cursor ON */
1024                lcd_flags |= LCD_FLAG_C;
1025                processed = 1;
1026                break;
1027        case 'c':       /* Cursor OFF */
1028                lcd_flags &= ~LCD_FLAG_C;
1029                processed = 1;
1030                break;
1031        case 'B':       /* Blink ON */
1032                lcd_flags |= LCD_FLAG_B;
1033                processed = 1;
1034                break;
1035        case 'b':       /* Blink OFF */
1036                lcd_flags &= ~LCD_FLAG_B;
1037                processed = 1;
1038                break;
1039        case '+':       /* Back light ON */
1040                lcd_flags |= LCD_FLAG_L;
1041                processed = 1;
1042                break;
1043        case '-':       /* Back light OFF */
1044                lcd_flags &= ~LCD_FLAG_L;
1045                processed = 1;
1046                break;
1047        case '*':
1048                /* flash back light using the keypad timer */
1049                if (scan_timer.function != NULL) {
1050                        if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1051                                lcd_backlight(1);
1052                        light_tempo = FLASH_LIGHT_TEMPO;
1053                }
1054                processed = 1;
1055                break;
1056        case 'f':       /* Small Font */
1057                lcd_flags &= ~LCD_FLAG_F;
1058                processed = 1;
1059                break;
1060        case 'F':       /* Large Font */
1061                lcd_flags |= LCD_FLAG_F;
1062                processed = 1;
1063                break;
1064        case 'n':       /* One Line */
1065                lcd_flags &= ~LCD_FLAG_N;
1066                processed = 1;
1067                break;
1068        case 'N':       /* Two Lines */
1069                lcd_flags |= LCD_FLAG_N;
1070                break;
1071        case 'l':       /* Shift Cursor Left */
1072                if (lcd_addr_x > 0) {
1073                        /* back one char if not at end of line */
1074                        if (lcd_addr_x < lcd_bwidth)
1075                                lcd_write_cmd(0x10);
1076                        lcd_addr_x--;
1077                }
1078                processed = 1;
1079                break;
1080        case 'r':       /* shift cursor right */
1081                if (lcd_addr_x < lcd_width) {
1082                        /* allow the cursor to pass the end of the line */
1083                        if (lcd_addr_x <
1084                            (lcd_bwidth - 1))
1085                                lcd_write_cmd(0x14);
1086                        lcd_addr_x++;
1087                }
1088                processed = 1;
1089                break;
1090        case 'L':       /* shift display left */
1091                lcd_left_shift++;
1092                lcd_write_cmd(0x18);
1093                processed = 1;
1094                break;
1095        case 'R':       /* shift display right */
1096                lcd_left_shift--;
1097                lcd_write_cmd(0x1C);
1098                processed = 1;
1099                break;
1100        case 'k': {     /* kill end of line */
1101                int x;
1102                for (x = lcd_addr_x; x < lcd_bwidth; x++)
1103                        lcd_write_data(' ');
1104
1105                /* restore cursor position */
1106                lcd_gotoxy();
1107                processed = 1;
1108                break;
1109        }
1110        case 'I':       /* reinitialize display */
1111                lcd_init_display();
1112                lcd_left_shift = 0;
1113                processed = 1;
1114                break;
1115        case 'G': {
1116                /* Generator : LGcxxxxx...xx; must have <c> between '0'
1117                 * and '7', representing the numerical ASCII code of the
1118                 * redefined character, and <xx...xx> a sequence of 16
1119                 * hex digits representing 8 bytes for each character.
1120                 * Most LCDs will only use 5 lower bits of the 7 first
1121                 * bytes.
1122                 */
1123
1124                unsigned char cgbytes[8];
1125                unsigned char cgaddr;
1126                int cgoffset;
1127                int shift;
1128                char value;
1129                int addr;
1130
1131                if (strchr(esc, ';') == NULL)
1132                        break;
1133
1134                esc++;
1135
1136                cgaddr = *(esc++) - '0';
1137                if (cgaddr > 7) {
1138                        processed = 1;
1139                        break;
1140                }
1141
1142                cgoffset = 0;
1143                shift = 0;
1144                value = 0;
1145                while (*esc && cgoffset < 8) {
1146                        shift ^= 4;
1147                        if (*esc >= '0' && *esc <= '9')
1148                                value |= (*esc - '0') << shift;
1149                        else if (*esc >= 'A' && *esc <= 'Z')
1150                                value |= (*esc - 'A' + 10) << shift;
1151                        else if (*esc >= 'a' && *esc <= 'z')
1152                                value |= (*esc - 'a' + 10) << shift;
1153                        else {
1154                                esc++;
1155                                continue;
1156                        }
1157
1158                        if (shift == 0) {
1159                                cgbytes[cgoffset++] = value;
1160                                value = 0;
1161                        }
1162
1163                        esc++;
1164                }
1165
1166                lcd_write_cmd(0x40 | (cgaddr * 8));
1167                for (addr = 0; addr < cgoffset; addr++)
1168                        lcd_write_data(cgbytes[addr]);
1169
1170                /* ensures that we stop writing to CGRAM */
1171                lcd_gotoxy();
1172                processed = 1;
1173                break;
1174        }
1175        case 'x':       /* gotoxy : LxXXX[yYYY]; */
1176        case 'y':       /* gotoxy : LyYYY[xXXX]; */
1177                if (strchr(esc, ';') == NULL)
1178                        break;
1179
1180                while (*esc) {
1181                        if (*esc == 'x') {
1182                                esc++;
1183                                if (kstrtoul(esc, 10, &lcd_addr_x) < 0)
1184                                        break;
1185                        } else if (*esc == 'y') {
1186                                esc++;
1187                                if (kstrtoul(esc, 10, &lcd_addr_y) < 0)
1188                                        break;
1189                        } else
1190                                break;
1191                }
1192
1193                lcd_gotoxy();
1194                processed = 1;
1195                break;
1196        }
1197
1198        /* Check wether one flag was changed */
1199        if (oldflags != lcd_flags) {
1200                /* check whether one of B,C,D flags were changed */
1201                if ((oldflags ^ lcd_flags) &
1202                    (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1203                        /* set display mode */
1204                        lcd_write_cmd(0x08
1205                                      | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
1206                                      | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
1207                                      | ((lcd_flags & LCD_FLAG_B) ? 1 : 0));
1208                /* check whether one of F,N flags was changed */
1209                else if ((oldflags ^ lcd_flags) & (LCD_FLAG_F | LCD_FLAG_N))
1210                        lcd_write_cmd(0x30
1211                                      | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
1212                                      | ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
1213                /* check wether L flag was changed */
1214                else if ((oldflags ^ lcd_flags) & (LCD_FLAG_L)) {
1215                        if (lcd_flags & (LCD_FLAG_L))
1216                                lcd_backlight(1);
1217                        else if (light_tempo == 0)
1218                                /* switch off the light only when the tempo
1219                                   lighting is gone */
1220                                lcd_backlight(0);
1221                }
1222        }
1223
1224        return processed;
1225}
1226
1227static ssize_t lcd_write(struct file *file,
1228                         const char *buf, size_t count, loff_t *ppos)
1229{
1230        const char *tmp = buf;
1231        char c;
1232
1233        for (; count-- > 0; (ppos ? (*ppos)++ : 0), ++tmp) {
1234                if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
1235                        /* let's be a little nice with other processes
1236                           that need some CPU */
1237                        schedule();
1238
1239                if (ppos == NULL && file == NULL)
1240                        /* let's not use get_user() from the kernel ! */
1241                        c = *tmp;
1242                else if (get_user(c, tmp))
1243                        return -EFAULT;
1244
1245                /* first, we'll test if we're in escape mode */
1246                if ((c != '\n') && lcd_escape_len >= 0) {
1247                        /* yes, let's add this char to the buffer */
1248                        lcd_escape[lcd_escape_len++] = c;
1249                        lcd_escape[lcd_escape_len] = 0;
1250                } else {
1251                        /* aborts any previous escape sequence */
1252                        lcd_escape_len = -1;
1253
1254                        switch (c) {
1255                        case LCD_ESCAPE_CHAR:
1256                                /* start of an escape sequence */
1257                                lcd_escape_len = 0;
1258                                lcd_escape[lcd_escape_len] = 0;
1259                                break;
1260                        case '\b':
1261                                /* go back one char and clear it */
1262                                if (lcd_addr_x > 0) {
1263                                        /* check if we're not at the
1264                                           end of the line */
1265                                        if (lcd_addr_x < lcd_bwidth)
1266                                                /* back one char */
1267                                                lcd_write_cmd(0x10);
1268                                        lcd_addr_x--;
1269                                }
1270                                /* replace with a space */
1271                                lcd_write_data(' ');
1272                                /* back one char again */
1273                                lcd_write_cmd(0x10);
1274                                break;
1275                        case '\014':
1276                                /* quickly clear the display */
1277                                lcd_clear_fast();
1278                                break;
1279                        case '\n':
1280                                /* flush the remainder of the current line and
1281                                   go to the beginning of the next line */
1282                                for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
1283                                        lcd_write_data(' ');
1284                                lcd_addr_x = 0;
1285                                lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
1286                                lcd_gotoxy();
1287                                break;
1288                        case '\r':
1289                                /* go to the beginning of the same line */
1290                                lcd_addr_x = 0;
1291                                lcd_gotoxy();
1292                                break;
1293                        case '\t':
1294                                /* print a space instead of the tab */
1295                                lcd_print(' ');
1296                                break;
1297                        default:
1298                                /* simply print this char */
1299                                lcd_print(c);
1300                                break;
1301                        }
1302                }
1303
1304                /* now we'll see if we're in an escape mode and if the current
1305                   escape sequence can be understood. */
1306                if (lcd_escape_len >= 2) {
1307                        int processed = 0;
1308
1309                        if (!strcmp(lcd_escape, "[2J")) {
1310                                /* clear the display */
1311                                lcd_clear_fast();
1312                                processed = 1;
1313                        } else if (!strcmp(lcd_escape, "[H")) {
1314                                /* cursor to home */
1315                                lcd_addr_x = lcd_addr_y = 0;
1316                                lcd_gotoxy();
1317                                processed = 1;
1318                        }
1319                        /* codes starting with ^[[L */
1320                        else if ((lcd_escape_len >= 3) &&
1321                                 (lcd_escape[0] == '[') &&
1322                                 (lcd_escape[1] == 'L')) {
1323                                processed = handle_lcd_special_code();
1324                        }
1325
1326                        /* LCD special escape codes */
1327                        /* flush the escape sequence if it's been processed
1328                           or if it is getting too long. */
1329                        if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1330                                lcd_escape_len = -1;
1331                } /* escape codes */
1332        }
1333
1334        return tmp - buf;
1335}
1336
1337static int lcd_open(struct inode *inode, struct file *file)
1338{
1339        if (lcd_open_cnt)
1340                return -EBUSY;  /* open only once at a time */
1341
1342        if (file->f_mode & FMODE_READ)  /* device is write-only */
1343                return -EPERM;
1344
1345        if (lcd_must_clear) {
1346                lcd_clear_display();
1347                lcd_must_clear = 0;
1348        }
1349        lcd_open_cnt++;
1350        return nonseekable_open(inode, file);
1351}
1352
1353static int lcd_release(struct inode *inode, struct file *file)
1354{
1355        lcd_open_cnt--;
1356        return 0;
1357}
1358
1359static const struct file_operations lcd_fops = {
1360        .write   = lcd_write,
1361        .open    = lcd_open,
1362        .release = lcd_release,
1363        .llseek  = no_llseek,
1364};
1365
1366static struct miscdevice lcd_dev = {
1367        LCD_MINOR,
1368        "lcd",
1369        &lcd_fops
1370};
1371
1372/* public function usable from the kernel for any purpose */
1373void panel_lcd_print(char *s)
1374{
1375        if (lcd_enabled && lcd_initialized)
1376                lcd_write(NULL, s, strlen(s), NULL);
1377}
1378
1379/* initialize the LCD driver */
1380void lcd_init(void)
1381{
1382        switch (lcd_type) {
1383        case LCD_TYPE_OLD:
1384                /* parallel mode, 8 bits */
1385                if (lcd_proto < 0)
1386                        lcd_proto = LCD_PROTO_PARALLEL;
1387                if (lcd_charset < 0)
1388                        lcd_charset = LCD_CHARSET_NORMAL;
1389                if (lcd_e_pin == PIN_NOT_SET)
1390                        lcd_e_pin = PIN_STROBE;
1391                if (lcd_rs_pin == PIN_NOT_SET)
1392                        lcd_rs_pin = PIN_AUTOLF;
1393
1394                if (lcd_width < 0)
1395                        lcd_width = 40;
1396                if (lcd_bwidth < 0)
1397                        lcd_bwidth = 40;
1398                if (lcd_hwidth < 0)
1399                        lcd_hwidth = 64;
1400                if (lcd_height < 0)
1401                        lcd_height = 2;
1402                break;
1403        case LCD_TYPE_KS0074:
1404                /* serial mode, ks0074 */
1405                if (lcd_proto < 0)
1406                        lcd_proto = LCD_PROTO_SERIAL;
1407                if (lcd_charset < 0)
1408                        lcd_charset = LCD_CHARSET_KS0074;
1409                if (lcd_bl_pin == PIN_NOT_SET)
1410                        lcd_bl_pin = PIN_AUTOLF;
1411                if (lcd_cl_pin == PIN_NOT_SET)
1412                        lcd_cl_pin = PIN_STROBE;
1413                if (lcd_da_pin == PIN_NOT_SET)
1414                        lcd_da_pin = PIN_D0;
1415
1416                if (lcd_width < 0)
1417                        lcd_width = 16;
1418                if (lcd_bwidth < 0)
1419                        lcd_bwidth = 40;
1420                if (lcd_hwidth < 0)
1421                        lcd_hwidth = 16;
1422                if (lcd_height < 0)
1423                        lcd_height = 2;
1424                break;
1425        case LCD_TYPE_NEXCOM:
1426                /* parallel mode, 8 bits, generic */
1427                if (lcd_proto < 0)
1428                        lcd_proto = LCD_PROTO_PARALLEL;
1429                if (lcd_charset < 0)
1430                        lcd_charset = LCD_CHARSET_NORMAL;
1431                if (lcd_e_pin == PIN_NOT_SET)
1432                        lcd_e_pin = PIN_AUTOLF;
1433                if (lcd_rs_pin == PIN_NOT_SET)
1434                        lcd_rs_pin = PIN_SELECP;
1435                if (lcd_rw_pin == PIN_NOT_SET)
1436                        lcd_rw_pin = PIN_INITP;
1437
1438                if (lcd_width < 0)
1439                        lcd_width = 16;
1440                if (lcd_bwidth < 0)
1441                        lcd_bwidth = 40;
1442                if (lcd_hwidth < 0)
1443                        lcd_hwidth = 64;
1444                if (lcd_height < 0)
1445                        lcd_height = 2;
1446                break;
1447        case LCD_TYPE_CUSTOM:
1448                /* customer-defined */
1449                if (lcd_proto < 0)
1450                        lcd_proto = DEFAULT_LCD_PROTO;
1451                if (lcd_charset < 0)
1452                        lcd_charset = DEFAULT_LCD_CHARSET;
1453                /* default geometry will be set later */
1454                break;
1455        case LCD_TYPE_HANTRONIX:
1456                /* parallel mode, 8 bits, hantronix-like */
1457        default:
1458                if (lcd_proto < 0)
1459                        lcd_proto = LCD_PROTO_PARALLEL;
1460                if (lcd_charset < 0)
1461                        lcd_charset = LCD_CHARSET_NORMAL;
1462                if (lcd_e_pin == PIN_NOT_SET)
1463                        lcd_e_pin = PIN_STROBE;
1464                if (lcd_rs_pin == PIN_NOT_SET)
1465                        lcd_rs_pin = PIN_SELECP;
1466
1467                if (lcd_width < 0)
1468                        lcd_width = 16;
1469                if (lcd_bwidth < 0)
1470                        lcd_bwidth = 40;
1471                if (lcd_hwidth < 0)
1472                        lcd_hwidth = 64;
1473                if (lcd_height < 0)
1474                        lcd_height = 2;
1475                break;
1476        }
1477
1478        /* this is used to catch wrong and default values */
1479        if (lcd_width <= 0)
1480                lcd_width = DEFAULT_LCD_WIDTH;
1481        if (lcd_bwidth <= 0)
1482                lcd_bwidth = DEFAULT_LCD_BWIDTH;
1483        if (lcd_hwidth <= 0)
1484                lcd_hwidth = DEFAULT_LCD_HWIDTH;
1485        if (lcd_height <= 0)
1486                lcd_height = DEFAULT_LCD_HEIGHT;
1487
1488        if (lcd_proto == LCD_PROTO_SERIAL) {    /* SERIAL */
1489                lcd_write_cmd = lcd_write_cmd_s;
1490                lcd_write_data = lcd_write_data_s;
1491                lcd_clear_fast = lcd_clear_fast_s;
1492
1493                if (lcd_cl_pin == PIN_NOT_SET)
1494                        lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1495                if (lcd_da_pin == PIN_NOT_SET)
1496                        lcd_da_pin = DEFAULT_LCD_PIN_SDA;
1497
1498        } else if (lcd_proto == LCD_PROTO_PARALLEL) {   /* PARALLEL */
1499                lcd_write_cmd = lcd_write_cmd_p8;
1500                lcd_write_data = lcd_write_data_p8;
1501                lcd_clear_fast = lcd_clear_fast_p8;
1502
1503                if (lcd_e_pin == PIN_NOT_SET)
1504                        lcd_e_pin = DEFAULT_LCD_PIN_E;
1505                if (lcd_rs_pin == PIN_NOT_SET)
1506                        lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1507                if (lcd_rw_pin == PIN_NOT_SET)
1508                        lcd_rw_pin = DEFAULT_LCD_PIN_RW;
1509        } else {
1510                lcd_write_cmd = lcd_write_cmd_tilcd;
1511                lcd_write_data = lcd_write_data_tilcd;
1512                lcd_clear_fast = lcd_clear_fast_tilcd;
1513        }
1514
1515        if (lcd_bl_pin == PIN_NOT_SET)
1516                lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1517
1518        if (lcd_e_pin == PIN_NOT_SET)
1519                lcd_e_pin = PIN_NONE;
1520        if (lcd_rs_pin == PIN_NOT_SET)
1521                lcd_rs_pin = PIN_NONE;
1522        if (lcd_rw_pin == PIN_NOT_SET)
1523                lcd_rw_pin = PIN_NONE;
1524        if (lcd_bl_pin == PIN_NOT_SET)
1525                lcd_bl_pin = PIN_NONE;
1526        if (lcd_cl_pin == PIN_NOT_SET)
1527                lcd_cl_pin = PIN_NONE;
1528        if (lcd_da_pin == PIN_NOT_SET)
1529                lcd_da_pin = PIN_NONE;
1530
1531        if (lcd_charset < 0)
1532                lcd_charset = DEFAULT_LCD_CHARSET;
1533
1534        if (lcd_charset == LCD_CHARSET_KS0074)
1535                lcd_char_conv = lcd_char_conv_ks0074;
1536        else
1537                lcd_char_conv = NULL;
1538
1539        if (lcd_bl_pin != PIN_NONE)
1540                init_scan_timer();
1541
1542        pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1543                    lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1544        pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1545                    lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1546        pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1547                    lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1548        pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1549                    lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1550        pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1551                    lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1552        pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1553                    lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
1554
1555        /* before this line, we must NOT send anything to the display.
1556         * Since lcd_init_display() needs to write data, we have to
1557         * enable mark the LCD initialized just before. */
1558        lcd_initialized = 1;
1559        lcd_init_display();
1560
1561        /* display a short message */
1562#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1563#ifdef CONFIG_PANEL_BOOT_MESSAGE
1564        panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
1565#endif
1566#else
1567        panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1568                        PANEL_VERSION);
1569#endif
1570        lcd_addr_x = lcd_addr_y = 0;
1571        /* clear the display on the next device opening */
1572        lcd_must_clear = 1;
1573        lcd_gotoxy();
1574}
1575
1576/*
1577 * These are the file operation function for user access to /dev/keypad
1578 */
1579
1580static ssize_t keypad_read(struct file *file,
1581                           char *buf, size_t count, loff_t *ppos)
1582{
1583
1584        unsigned i = *ppos;
1585        char *tmp = buf;
1586
1587        if (keypad_buflen == 0) {
1588                if (file->f_flags & O_NONBLOCK)
1589                        return -EAGAIN;
1590
1591                interruptible_sleep_on(&keypad_read_wait);
1592                if (signal_pending(current))
1593                        return -EINTR;
1594        }
1595
1596        for (; count-- > 0 && (keypad_buflen > 0);
1597             ++i, ++tmp, --keypad_buflen) {
1598                put_user(keypad_buffer[keypad_start], tmp);
1599                keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1600        }
1601        *ppos = i;
1602
1603        return tmp - buf;
1604}
1605
1606static int keypad_open(struct inode *inode, struct file *file)
1607{
1608
1609        if (keypad_open_cnt)
1610                return -EBUSY;  /* open only once at a time */
1611
1612        if (file->f_mode & FMODE_WRITE) /* device is read-only */
1613                return -EPERM;
1614
1615        keypad_buflen = 0;      /* flush the buffer on opening */
1616        keypad_open_cnt++;
1617        return 0;
1618}
1619
1620static int keypad_release(struct inode *inode, struct file *file)
1621{
1622        keypad_open_cnt--;
1623        return 0;
1624}
1625
1626static const struct file_operations keypad_fops = {
1627        .read    = keypad_read,         /* read */
1628        .open    = keypad_open,         /* open */
1629        .release = keypad_release,      /* close */
1630        .llseek  = default_llseek,
1631};
1632
1633static struct miscdevice keypad_dev = {
1634        KEYPAD_MINOR,
1635        "keypad",
1636        &keypad_fops
1637};
1638
1639static void keypad_send_key(char *string, int max_len)
1640{
1641        if (init_in_progress)
1642                return;
1643
1644        /* send the key to the device only if a process is attached to it. */
1645        if (keypad_open_cnt > 0) {
1646                while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1647                        keypad_buffer[(keypad_start + keypad_buflen++) %
1648                                      KEYPAD_BUFFER] = *string++;
1649                }
1650                wake_up_interruptible(&keypad_read_wait);
1651        }
1652}
1653
1654/* this function scans all the bits involving at least one logical signal,
1655 * and puts the results in the bitfield "phys_read" (one bit per established
1656 * contact), and sets "phys_read_prev" to "phys_read".
1657 *
1658 * Note: to debounce input signals, we will only consider as switched a signal
1659 * which is stable across 2 measures. Signals which are different between two
1660 * reads will be kept as they previously were in their logical form (phys_prev).
1661 * A signal which has just switched will have a 1 in
1662 * (phys_read ^ phys_read_prev).
1663 */
1664static void phys_scan_contacts(void)
1665{
1666        int bit, bitval;
1667        char oldval;
1668        char bitmask;
1669        char gndmask;
1670
1671        phys_prev = phys_curr;
1672        phys_read_prev = phys_read;
1673        phys_read = 0;          /* flush all signals */
1674
1675        /* keep track of old value, with all outputs disabled */
1676        oldval = r_dtr(pprt) | scan_mask_o;
1677        /* activate all keyboard outputs (active low) */
1678        w_dtr(pprt, oldval & ~scan_mask_o);
1679
1680        /* will have a 1 for each bit set to gnd */
1681        bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1682        /* disable all matrix signals */
1683        w_dtr(pprt, oldval);
1684
1685        /* now that all outputs are cleared, the only active input bits are
1686         * directly connected to the ground
1687         */
1688
1689        /* 1 for each grounded input */
1690        gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1691
1692        /* grounded inputs are signals 40-44 */
1693        phys_read |= (pmask_t) gndmask << 40;
1694
1695        if (bitmask != gndmask) {
1696                /* since clearing the outputs changed some inputs, we know
1697                 * that some input signals are currently tied to some outputs.
1698                 * So we'll scan them.
1699                 */
1700                for (bit = 0; bit < 8; bit++) {
1701                        bitval = 1 << bit;
1702
1703                        if (!(scan_mask_o & bitval))    /* skip unused bits */
1704                                continue;
1705
1706                        w_dtr(pprt, oldval & ~bitval);  /* enable this output */
1707                        bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1708                        phys_read |= (pmask_t) bitmask << (5 * bit);
1709                }
1710                w_dtr(pprt, oldval);    /* disable all outputs */
1711        }
1712        /* this is easy: use old bits when they are flapping,
1713         * use new ones when stable */
1714        phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1715                    (phys_read & ~(phys_read ^ phys_read_prev));
1716}
1717
1718static inline int input_state_high(struct logical_input *input)
1719{
1720#if 0
1721        /* FIXME:
1722         * this is an invalid test. It tries to catch
1723         * transitions from single-key to multiple-key, but
1724         * doesn't take into account the contacts polarity.
1725         * The only solution to the problem is to parse keys
1726         * from the most complex to the simplest combinations,
1727         * and mark them as 'caught' once a combination
1728         * matches, then unmatch it for all other ones.
1729         */
1730
1731        /* try to catch dangerous transitions cases :
1732         * someone adds a bit, so this signal was a false
1733         * positive resulting from a transition. We should
1734         * invalidate the signal immediately and not call the
1735         * release function.
1736         * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1737         */
1738        if (((phys_prev & input->mask) == input->value)
1739            && ((phys_curr & input->mask) > input->value)) {
1740                input->state = INPUT_ST_LOW; /* invalidate */
1741                return 1;
1742        }
1743#endif
1744
1745        if ((phys_curr & input->mask) == input->value) {
1746                if ((input->type == INPUT_TYPE_STD) &&
1747                    (input->high_timer == 0)) {
1748                        input->high_timer++;
1749                        if (input->u.std.press_fct != NULL)
1750                                input->u.std.press_fct(input->u.std.press_data);
1751                } else if (input->type == INPUT_TYPE_KBD) {
1752                        /* will turn on the light */
1753                        keypressed = 1;
1754
1755                        if (input->high_timer == 0) {
1756                                char *press_str = input->u.kbd.press_str;
1757                                if (press_str[0])
1758                                        keypad_send_key(press_str,
1759                                                        sizeof(press_str));
1760                        }
1761
1762                        if (input->u.kbd.repeat_str[0]) {
1763                                char *repeat_str = input->u.kbd.repeat_str;
1764                                if (input->high_timer >= KEYPAD_REP_START) {
1765                                        input->high_timer -= KEYPAD_REP_DELAY;
1766                                        keypad_send_key(repeat_str,
1767                                                        sizeof(repeat_str));
1768                                }
1769                                /* we will need to come back here soon */
1770                                inputs_stable = 0;
1771                        }
1772
1773                        if (input->high_timer < 255)
1774                                input->high_timer++;
1775                }
1776                return 1;
1777        } else {
1778                /* else signal falling down. Let's fall through. */
1779                input->state = INPUT_ST_FALLING;
1780                input->fall_timer = 0;
1781        }
1782        return 0;
1783}
1784
1785static inline void input_state_falling(struct logical_input *input)
1786{
1787#if 0
1788        /* FIXME !!! same comment as in input_state_high */
1789        if (((phys_prev & input->mask) == input->value)
1790            && ((phys_curr & input->mask) > input->value)) {
1791                input->state = INPUT_ST_LOW;    /* invalidate */
1792                return;
1793        }
1794#endif
1795
1796        if ((phys_curr & input->mask) == input->value) {
1797                if (input->type == INPUT_TYPE_KBD) {
1798                        /* will turn on the light */
1799                        keypressed = 1;
1800
1801                        if (input->u.kbd.repeat_str[0]) {
1802                                char *repeat_str = input->u.kbd.repeat_str;
1803                                if (input->high_timer >= KEYPAD_REP_START)
1804                                        input->high_timer -= KEYPAD_REP_DELAY;
1805                                        keypad_send_key(repeat_str,
1806                                                        sizeof(repeat_str));
1807                                /* we will need to come back here soon */
1808                                inputs_stable = 0;
1809                        }
1810
1811                        if (input->high_timer < 255)
1812                                input->high_timer++;
1813                }
1814                input->state = INPUT_ST_HIGH;
1815        } else if (input->fall_timer >= input->fall_time) {
1816                /* call release event */
1817                if (input->type == INPUT_TYPE_STD) {
1818                        void (*release_fct)(int) = input->u.std.release_fct;
1819                        if (release_fct != NULL)
1820                                release_fct(input->u.std.release_data);
1821                } else if (input->type == INPUT_TYPE_KBD) {
1822                        char *release_str = input->u.kbd.release_str;
1823                        if (release_str[0])
1824                                keypad_send_key(release_str,
1825                                                sizeof(release_str));
1826                }
1827
1828                input->state = INPUT_ST_LOW;
1829        } else {
1830                input->fall_timer++;
1831                inputs_stable = 0;
1832        }
1833}
1834
1835static void panel_process_inputs(void)
1836{
1837        struct list_head *item;
1838        struct logical_input *input;
1839
1840#if 0
1841        printk(KERN_DEBUG
1842               "entering panel_process_inputs with pp=%016Lx & pc=%016Lx\n",
1843               phys_prev, phys_curr);
1844#endif
1845
1846        keypressed = 0;
1847        inputs_stable = 1;
1848        list_for_each(item, &logical_inputs) {
1849                input = list_entry(item, struct logical_input, list);
1850
1851                switch (input->state) {
1852                case INPUT_ST_LOW:
1853                        if ((phys_curr & input->mask) != input->value)
1854                                break;
1855                        /* if all needed ones were already set previously,
1856                         * this means that this logical signal has been
1857                         * activated by the releasing of another combined
1858                         * signal, so we don't want to match.
1859                         * eg: AB -(release B)-> A -(release A)-> 0 :
1860                         *     don't match A.
1861                         */
1862                        if ((phys_prev & input->mask) == input->value)
1863                                break;
1864                        input->rise_timer = 0;
1865                        input->state = INPUT_ST_RISING;
1866                        /* no break here, fall through */
1867                case INPUT_ST_RISING:
1868                        if ((phys_curr & input->mask) != input->value) {
1869                                input->state = INPUT_ST_LOW;
1870                                break;
1871                        }
1872                        if (input->rise_timer < input->rise_time) {
1873                                inputs_stable = 0;
1874                                input->rise_timer++;
1875                                break;
1876                        }
1877                        input->high_timer = 0;
1878                        input->state = INPUT_ST_HIGH;
1879                        /* no break here, fall through */
1880                case INPUT_ST_HIGH:
1881                        if (input_state_high(input))
1882                                break;
1883                        /* no break here, fall through */
1884                case INPUT_ST_FALLING:
1885                        input_state_falling(input);
1886                }
1887        }
1888}
1889
1890static void panel_scan_timer(void)
1891{
1892        if (keypad_enabled && keypad_initialized) {
1893                if (spin_trylock(&pprt_lock)) {
1894                        phys_scan_contacts();
1895
1896                        /* no need for the parport anymore */
1897                        spin_unlock(&pprt_lock);
1898                }
1899
1900                if (!inputs_stable || phys_curr != phys_prev)
1901                        panel_process_inputs();
1902        }
1903
1904        if (lcd_enabled && lcd_initialized) {
1905                if (keypressed) {
1906                        if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1907                                lcd_backlight(1);
1908                        light_tempo = FLASH_LIGHT_TEMPO;
1909                } else if (light_tempo > 0) {
1910                        light_tempo--;
1911                        if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1912                                lcd_backlight(0);
1913                }
1914        }
1915
1916        mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
1917}
1918
1919static void init_scan_timer(void)
1920{
1921        if (scan_timer.function != NULL)
1922                return;         /* already started */
1923
1924        init_timer(&scan_timer);
1925        scan_timer.expires = jiffies + INPUT_POLL_TIME;
1926        scan_timer.data = 0;
1927        scan_timer.function = (void *)&panel_scan_timer;
1928        add_timer(&scan_timer);
1929}
1930
1931/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1932 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1933 * corresponding to out and in bits respectively.
1934 * returns 1 if ok, 0 if error (in which case, nothing is written).
1935 */
1936static int input_name2mask(char *name, pmask_t *mask, pmask_t *value,
1937                           char *imask, char *omask)
1938{
1939        static char sigtab[10] = "EeSsPpAaBb";
1940        char im, om;
1941        pmask_t m, v;
1942
1943        om = im = m = v = 0ULL;
1944        while (*name) {
1945                int in, out, bit, neg;
1946                for (in = 0; (in < sizeof(sigtab)) &&
1947                             (sigtab[in] != *name); in++)
1948                        ;
1949                if (in >= sizeof(sigtab))
1950                        return 0;       /* input name not found */
1951                neg = (in & 1); /* odd (lower) names are negated */
1952                in >>= 1;
1953                im |= (1 << in);
1954
1955                name++;
1956                if (isdigit(*name)) {
1957                        out = *name - '0';
1958                        om |= (1 << out);
1959                } else if (*name == '-')
1960                        out = 8;
1961                else
1962                        return 0;       /* unknown bit name */
1963
1964                bit = (out * 5) + in;
1965
1966                m |= 1ULL << bit;
1967                if (!neg)
1968                        v |= 1ULL << bit;
1969                name++;
1970        }
1971        *mask = m;
1972        *value = v;
1973        if (imask)
1974                *imask |= im;
1975        if (omask)
1976                *omask |= om;
1977        return 1;
1978}
1979
1980/* tries to bind a key to the signal name <name>. The key will send the
1981 * strings <press>, <repeat>, <release> for these respective events.
1982 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1983 */
1984static struct logical_input *panel_bind_key(char *name, char *press,
1985                                            char *repeat, char *release)
1986{
1987        struct logical_input *key;
1988
1989        key = kzalloc(sizeof(struct logical_input), GFP_KERNEL);
1990        if (!key) {
1991                printk(KERN_ERR "panel: not enough memory\n");
1992                return NULL;
1993        }
1994        if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1995                             &scan_mask_o)) {
1996                kfree(key);
1997                return NULL;
1998        }
1999
2000        key->type = INPUT_TYPE_KBD;
2001        key->state = INPUT_ST_LOW;
2002        key->rise_time = 1;
2003        key->fall_time = 1;
2004
2005#if 0
2006        printk(KERN_DEBUG "bind: <%s> : m=%016Lx v=%016Lx\n", name, key->mask,
2007               key->value);
2008#endif
2009        strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2010        strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2011        strncpy(key->u.kbd.release_str, release,
2012                sizeof(key->u.kbd.release_str));
2013        list_add(&key->list, &logical_inputs);
2014        return key;
2015}
2016
2017#if 0
2018/* tries to bind a callback function to the signal name <name>. The function
2019 * <press_fct> will be called with the <press_data> arg when the signal is
2020 * activated, and so on for <release_fct>/<release_data>
2021 * Returns the pointer to the new signal if ok, NULL if the signal could not
2022 * be bound.
2023 */
2024static struct logical_input *panel_bind_callback(char *name,
2025                                                 void (*press_fct) (int),
2026                                                 int press_data,
2027                                                 void (*release_fct) (int),
2028                                                 int release_data)
2029{
2030        struct logical_input *callback;
2031
2032        callback = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
2033        if (!callback) {
2034                printk(KERN_ERR "panel: not enough memory\n");
2035                return NULL;
2036        }
2037        memset(callback, 0, sizeof(struct logical_input));
2038        if (!input_name2mask(name, &callback->mask, &callback->value,
2039                             &scan_mask_i, &scan_mask_o))
2040                return NULL;
2041
2042        callback->type = INPUT_TYPE_STD;
2043        callback->state = INPUT_ST_LOW;
2044        callback->rise_time = 1;
2045        callback->fall_time = 1;
2046        callback->u.std.press_fct = press_fct;
2047        callback->u.std.press_data = press_data;
2048        callback->u.std.release_fct = release_fct;
2049        callback->u.std.release_data = release_data;
2050        list_add(&callback->list, &logical_inputs);
2051        return callback;
2052}
2053#endif
2054
2055static void keypad_init(void)
2056{
2057        int keynum;
2058        init_waitqueue_head(&keypad_read_wait);
2059        keypad_buflen = 0;      /* flushes any eventual noisy keystroke */
2060
2061        /* Let's create all known keys */
2062
2063        for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2064                panel_bind_key(keypad_profile[keynum][0],
2065                               keypad_profile[keynum][1],
2066                               keypad_profile[keynum][2],
2067                               keypad_profile[keynum][3]);
2068        }
2069
2070        init_scan_timer();
2071        keypad_initialized = 1;
2072}
2073
2074/**************************************************/
2075/* device initialization                          */
2076/**************************************************/
2077
2078static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2079                            void *unused)
2080{
2081        if (lcd_enabled && lcd_initialized) {
2082                switch (code) {
2083                case SYS_DOWN:
2084                        panel_lcd_print
2085                            ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2086                        break;
2087                case SYS_HALT:
2088                        panel_lcd_print
2089                            ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2090                        break;
2091                case SYS_POWER_OFF:
2092                        panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2093                        break;
2094                default:
2095                        break;
2096                }
2097        }
2098        return NOTIFY_DONE;
2099}
2100
2101static struct notifier_block panel_notifier = {
2102        panel_notify_sys,
2103        NULL,
2104        0
2105};
2106
2107static void panel_attach(struct parport *port)
2108{
2109        if (port->number != parport)
2110                return;
2111
2112        if (pprt) {
2113                printk(KERN_ERR
2114                       "panel_attach(): port->number=%d parport=%d, "
2115                       "already registered !\n",
2116                       port->number, parport);
2117                return;
2118        }
2119
2120        pprt = parport_register_device(port, "panel", NULL, NULL,  /* pf, kf */
2121                                       NULL,
2122                                       /*PARPORT_DEV_EXCL */
2123                                       0, (void *)&pprt);
2124        if (pprt == NULL) {
2125                pr_err("panel_attach(): port->number=%d parport=%d, "
2126                       "parport_register_device() failed\n",
2127                       port->number, parport);
2128                return;
2129        }
2130
2131        if (parport_claim(pprt)) {
2132                printk(KERN_ERR
2133                       "Panel: could not claim access to parport%d. "
2134                       "Aborting.\n", parport);
2135                goto err_unreg_device;
2136        }
2137
2138        /* must init LCD first, just in case an IRQ from the keypad is
2139         * generated at keypad init
2140         */
2141        if (lcd_enabled) {
2142                lcd_init();
2143                if (misc_register(&lcd_dev))
2144                        goto err_unreg_device;
2145        }
2146
2147        if (keypad_enabled) {
2148                keypad_init();
2149                if (misc_register(&keypad_dev))
2150                        goto err_lcd_unreg;
2151        }
2152        return;
2153
2154err_lcd_unreg:
2155        if (lcd_enabled)
2156                misc_deregister(&lcd_dev);
2157err_unreg_device:
2158        parport_unregister_device(pprt);
2159        pprt = NULL;
2160}
2161
2162static void panel_detach(struct parport *port)
2163{
2164        if (port->number != parport)
2165                return;
2166
2167        if (!pprt) {
2168                printk(KERN_ERR
2169                       "panel_detach(): port->number=%d parport=%d, "
2170                       "nothing to unregister.\n",
2171                       port->number, parport);
2172                return;
2173        }
2174
2175        if (keypad_enabled && keypad_initialized) {
2176                misc_deregister(&keypad_dev);
2177                keypad_initialized = 0;
2178        }
2179
2180        if (lcd_enabled && lcd_initialized) {
2181                misc_deregister(&lcd_dev);
2182                lcd_initialized = 0;
2183        }
2184
2185        parport_release(pprt);
2186        parport_unregister_device(pprt);
2187        pprt = NULL;
2188}
2189
2190static struct parport_driver panel_driver = {
2191        .name = "panel",
2192        .attach = panel_attach,
2193        .detach = panel_detach,
2194};
2195
2196/* init function */
2197int panel_init(void)
2198{
2199        /* for backwards compatibility */
2200        if (keypad_type < 0)
2201                keypad_type = keypad_enabled;
2202
2203        if (lcd_type < 0)
2204                lcd_type = lcd_enabled;
2205
2206        if (parport < 0)
2207                parport = DEFAULT_PARPORT;
2208
2209        /* take care of an eventual profile */
2210        switch (profile) {
2211        case PANEL_PROFILE_CUSTOM:
2212                /* custom profile */
2213                if (keypad_type < 0)
2214                        keypad_type = DEFAULT_KEYPAD;
2215                if (lcd_type < 0)
2216                        lcd_type = DEFAULT_LCD;
2217                break;
2218        case PANEL_PROFILE_OLD:
2219                /* 8 bits, 2*16, old keypad */
2220                if (keypad_type < 0)
2221                        keypad_type = KEYPAD_TYPE_OLD;
2222                if (lcd_type < 0)
2223                        lcd_type = LCD_TYPE_OLD;
2224                if (lcd_width < 0)
2225                        lcd_width = 16;
2226                if (lcd_hwidth < 0)
2227                        lcd_hwidth = 16;
2228                break;
2229        case PANEL_PROFILE_NEW:
2230                /* serial, 2*16, new keypad */
2231                if (keypad_type < 0)
2232                        keypad_type = KEYPAD_TYPE_NEW;
2233                if (lcd_type < 0)
2234                        lcd_type = LCD_TYPE_KS0074;
2235                break;
2236        case PANEL_PROFILE_HANTRONIX:
2237                /* 8 bits, 2*16 hantronix-like, no keypad */
2238                if (keypad_type < 0)
2239                        keypad_type = KEYPAD_TYPE_NONE;
2240                if (lcd_type < 0)
2241                        lcd_type = LCD_TYPE_HANTRONIX;
2242                break;
2243        case PANEL_PROFILE_NEXCOM:
2244                /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
2245                if (keypad_type < 0)
2246                        keypad_type = KEYPAD_TYPE_NEXCOM;
2247                if (lcd_type < 0)
2248                        lcd_type = LCD_TYPE_NEXCOM;
2249                break;
2250        case PANEL_PROFILE_LARGE:
2251                /* 8 bits, 2*40, old keypad */
2252                if (keypad_type < 0)
2253                        keypad_type = KEYPAD_TYPE_OLD;
2254                if (lcd_type < 0)
2255                        lcd_type = LCD_TYPE_OLD;
2256                break;
2257        }
2258
2259        lcd_enabled = (lcd_type > 0);
2260        keypad_enabled = (keypad_type > 0);
2261
2262        switch (keypad_type) {
2263        case KEYPAD_TYPE_OLD:
2264                keypad_profile = old_keypad_profile;
2265                break;
2266        case KEYPAD_TYPE_NEW:
2267                keypad_profile = new_keypad_profile;
2268                break;
2269        case KEYPAD_TYPE_NEXCOM:
2270                keypad_profile = nexcom_keypad_profile;
2271                break;
2272        default:
2273                keypad_profile = NULL;
2274                break;
2275        }
2276
2277        /* tells various subsystems about the fact that we are initializing */
2278        init_in_progress = 1;
2279
2280        if (parport_register_driver(&panel_driver)) {
2281                printk(KERN_ERR
2282                       "Panel: could not register with parport. Aborting.\n");
2283                return -EIO;
2284        }
2285
2286        if (!lcd_enabled && !keypad_enabled) {
2287                /* no device enabled, let's release the parport */
2288                if (pprt) {
2289                        parport_release(pprt);
2290                        parport_unregister_device(pprt);
2291                        pprt = NULL;
2292                }
2293                parport_unregister_driver(&panel_driver);
2294                printk(KERN_ERR "Panel driver version " PANEL_VERSION
2295                       " disabled.\n");
2296                return -ENODEV;
2297        }
2298
2299        register_reboot_notifier(&panel_notifier);
2300
2301        if (pprt)
2302                printk(KERN_INFO "Panel driver version " PANEL_VERSION
2303                       " registered on parport%d (io=0x%lx).\n", parport,
2304                       pprt->port->base);
2305        else
2306                printk(KERN_INFO "Panel driver version " PANEL_VERSION
2307                       " not yet registered\n");
2308        /* tells various subsystems about the fact that initialization
2309           is finished */
2310        init_in_progress = 0;
2311        return 0;
2312}
2313
2314static int __init panel_init_module(void)
2315{
2316        return panel_init();
2317}
2318
2319static void __exit panel_cleanup_module(void)
2320{
2321        unregister_reboot_notifier(&panel_notifier);
2322
2323        if (scan_timer.function != NULL)
2324                del_timer(&scan_timer);
2325
2326        if (pprt != NULL) {
2327                if (keypad_enabled) {
2328                        misc_deregister(&keypad_dev);
2329                        keypad_initialized = 0;
2330                }
2331
2332                if (lcd_enabled) {
2333                        panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2334                                        "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2335                        misc_deregister(&lcd_dev);
2336                        lcd_initialized = 0;
2337                }
2338
2339                /* TODO: free all input signals */
2340                parport_release(pprt);
2341                parport_unregister_device(pprt);
2342                pprt = NULL;
2343        }
2344        parport_unregister_driver(&panel_driver);
2345}
2346
2347module_init(panel_init_module);
2348module_exit(panel_cleanup_module);
2349MODULE_AUTHOR("Willy Tarreau");
2350MODULE_LICENSE("GPL");
2351
2352/*
2353 * Local variables:
2354 *  c-indent-level: 4
2355 *  tab-width: 8
2356 * End:
2357 */
2358