Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Front panel driver for Linux
0004  * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
0005  * Copyright (C) 2016-2017 Glider bvba
0006  *
0007  * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
0008  * connected to a parallel printer port.
0009  *
0010  * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
0011  * serial module compatible with Samsung's KS0074. The pins may be connected in
0012  * any combination, everything is programmable.
0013  *
0014  * The keypad consists in a matrix of push buttons connecting input pins to
0015  * data output pins or to the ground. The combinations have to be hard-coded
0016  * in the driver, though several profiles exist and adding new ones is easy.
0017  *
0018  * Several profiles are provided for commonly found LCD+keypad modules on the
0019  * market, such as those found in Nexcom's appliances.
0020  *
0021  * FIXME:
0022  *      - the initialization/deinitialization process is very dirty and should
0023  *        be rewritten. It may even be buggy.
0024  *
0025  * TODO:
0026  *  - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
0027  *      - make the LCD a part of a virtual screen of Vx*Vy
0028  *  - make the inputs list smp-safe
0029  *      - change the keyboard to a double mapping : signals -> key_id -> values
0030  *        so that applications can change values without knowing signals
0031  *
0032  */
0033 
0034 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0035 
0036 #include <linux/module.h>
0037 
0038 #include <linux/types.h>
0039 #include <linux/errno.h>
0040 #include <linux/signal.h>
0041 #include <linux/sched.h>
0042 #include <linux/spinlock.h>
0043 #include <linux/interrupt.h>
0044 #include <linux/miscdevice.h>
0045 #include <linux/slab.h>
0046 #include <linux/ioport.h>
0047 #include <linux/fcntl.h>
0048 #include <linux/init.h>
0049 #include <linux/delay.h>
0050 #include <linux/kernel.h>
0051 #include <linux/ctype.h>
0052 #include <linux/parport.h>
0053 #include <linux/list.h>
0054 
0055 #include <linux/io.h>
0056 #include <linux/uaccess.h>
0057 
0058 #include "charlcd.h"
0059 #include "hd44780_common.h"
0060 
0061 #define LCD_MAXBYTES        256 /* max burst write */
0062 
0063 #define KEYPAD_BUFFER       64
0064 
0065 /* poll the keyboard this every second */
0066 #define INPUT_POLL_TIME     (HZ / 50)
0067 /* a key starts to repeat after this times INPUT_POLL_TIME */
0068 #define KEYPAD_REP_START    (10)
0069 /* a key repeats this times INPUT_POLL_TIME */
0070 #define KEYPAD_REP_DELAY    (2)
0071 
0072 /* converts an r_str() input to an active high, bits string : 000BAOSE */
0073 #define PNL_PINPUT(a)       ((((unsigned char)(a)) ^ 0x7F) >> 3)
0074 
0075 #define PNL_PBUSY       0x80    /* inverted input, active low */
0076 #define PNL_PACK        0x40    /* direct input, active low */
0077 #define PNL_POUTPA      0x20    /* direct input, active high */
0078 #define PNL_PSELECD     0x10    /* direct input, active high */
0079 #define PNL_PERRORP     0x08    /* direct input, active low */
0080 
0081 #define PNL_PBIDIR      0x20    /* bi-directional ports */
0082 /* high to read data in or-ed with data out */
0083 #define PNL_PINTEN      0x10
0084 #define PNL_PSELECP     0x08    /* inverted output, active low */
0085 #define PNL_PINITP      0x04    /* direct output, active low */
0086 #define PNL_PAUTOLF     0x02    /* inverted output, active low */
0087 #define PNL_PSTROBE     0x01    /* inverted output */
0088 
0089 #define PNL_PD0         0x01
0090 #define PNL_PD1         0x02
0091 #define PNL_PD2         0x04
0092 #define PNL_PD3         0x08
0093 #define PNL_PD4         0x10
0094 #define PNL_PD5         0x20
0095 #define PNL_PD6         0x40
0096 #define PNL_PD7         0x80
0097 
0098 #define PIN_NONE        0
0099 #define PIN_STROBE      1
0100 #define PIN_D0          2
0101 #define PIN_D1          3
0102 #define PIN_D2          4
0103 #define PIN_D3          5
0104 #define PIN_D4          6
0105 #define PIN_D5          7
0106 #define PIN_D6          8
0107 #define PIN_D7          9
0108 #define PIN_AUTOLF      14
0109 #define PIN_INITP       16
0110 #define PIN_SELECP      17
0111 #define PIN_NOT_SET     127
0112 
0113 #define NOT_SET         -1
0114 
0115 /* macros to simplify use of the parallel port */
0116 #define r_ctr(x)        (parport_read_control((x)->port))
0117 #define r_dtr(x)        (parport_read_data((x)->port))
0118 #define r_str(x)        (parport_read_status((x)->port))
0119 #define w_ctr(x, y)     (parport_write_control((x)->port, (y)))
0120 #define w_dtr(x, y)     (parport_write_data((x)->port, (y)))
0121 
0122 /* this defines which bits are to be used and which ones to be ignored */
0123 /* logical or of the output bits involved in the scan matrix */
0124 static __u8 scan_mask_o;
0125 /* logical or of the input bits involved in the scan matrix */
0126 static __u8 scan_mask_i;
0127 
0128 enum input_type {
0129     INPUT_TYPE_STD,
0130     INPUT_TYPE_KBD,
0131 };
0132 
0133 enum input_state {
0134     INPUT_ST_LOW,
0135     INPUT_ST_RISING,
0136     INPUT_ST_HIGH,
0137     INPUT_ST_FALLING,
0138 };
0139 
0140 struct logical_input {
0141     struct list_head list;
0142     __u64 mask;
0143     __u64 value;
0144     enum input_type type;
0145     enum input_state state;
0146     __u8 rise_time, fall_time;
0147     __u8 rise_timer, fall_timer, high_timer;
0148 
0149     union {
0150         struct {    /* valid when type == INPUT_TYPE_STD */
0151             void (*press_fct)(int);
0152             void (*release_fct)(int);
0153             int press_data;
0154             int release_data;
0155         } std;
0156         struct {    /* valid when type == INPUT_TYPE_KBD */
0157             char press_str[sizeof(void *) + sizeof(int)] __nonstring;
0158             char repeat_str[sizeof(void *) + sizeof(int)] __nonstring;
0159             char release_str[sizeof(void *) + sizeof(int)] __nonstring;
0160         } kbd;
0161     } u;
0162 };
0163 
0164 static LIST_HEAD(logical_inputs);   /* list of all defined logical inputs */
0165 
0166 /* physical contacts history
0167  * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
0168  * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
0169  * corresponds to the ground.
0170  * Within each group, bits are stored in the same order as read on the port :
0171  * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
0172  * So, each __u64 is represented like this :
0173  * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
0174  * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
0175  */
0176 
0177 /* what has just been read from the I/O ports */
0178 static __u64 phys_read;
0179 /* previous phys_read */
0180 static __u64 phys_read_prev;
0181 /* stabilized phys_read (phys_read|phys_read_prev) */
0182 static __u64 phys_curr;
0183 /* previous phys_curr */
0184 static __u64 phys_prev;
0185 /* 0 means that at least one logical signal needs be computed */
0186 static char inputs_stable;
0187 
0188 /* these variables are specific to the keypad */
0189 static struct {
0190     bool enabled;
0191 } keypad;
0192 
0193 static char keypad_buffer[KEYPAD_BUFFER];
0194 static int keypad_buflen;
0195 static int keypad_start;
0196 static char keypressed;
0197 static wait_queue_head_t keypad_read_wait;
0198 
0199 /* lcd-specific variables */
0200 static struct {
0201     bool enabled;
0202     bool initialized;
0203 
0204     int charset;
0205     int proto;
0206 
0207     /* TODO: use union here? */
0208     struct {
0209         int e;
0210         int rs;
0211         int rw;
0212         int cl;
0213         int da;
0214         int bl;
0215     } pins;
0216 
0217     struct charlcd *charlcd;
0218 } lcd;
0219 
0220 /* Needed only for init */
0221 static int selected_lcd_type = NOT_SET;
0222 
0223 /*
0224  * Bit masks to convert LCD signals to parallel port outputs.
0225  * _d_ are values for data port, _c_ are for control port.
0226  * [0] = signal OFF, [1] = signal ON, [2] = mask
0227  */
0228 #define BIT_CLR     0
0229 #define BIT_SET     1
0230 #define BIT_MSK     2
0231 #define BIT_STATES  3
0232 /*
0233  * one entry for each bit on the LCD
0234  */
0235 #define LCD_BIT_E   0
0236 #define LCD_BIT_RS  1
0237 #define LCD_BIT_RW  2
0238 #define LCD_BIT_BL  3
0239 #define LCD_BIT_CL  4
0240 #define LCD_BIT_DA  5
0241 #define LCD_BITS    6
0242 
0243 /*
0244  * each bit can be either connected to a DATA or CTRL port
0245  */
0246 #define LCD_PORT_C  0
0247 #define LCD_PORT_D  1
0248 #define LCD_PORTS   2
0249 
0250 static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
0251 
0252 /*
0253  * LCD protocols
0254  */
0255 #define LCD_PROTO_PARALLEL      0
0256 #define LCD_PROTO_SERIAL        1
0257 #define LCD_PROTO_TI_DA8XX_LCD  2
0258 
0259 /*
0260  * LCD character sets
0261  */
0262 #define LCD_CHARSET_NORMAL      0
0263 #define LCD_CHARSET_KS0074      1
0264 
0265 /*
0266  * LCD types
0267  */
0268 #define LCD_TYPE_NONE       0
0269 #define LCD_TYPE_CUSTOM     1
0270 #define LCD_TYPE_OLD        2
0271 #define LCD_TYPE_KS0074     3
0272 #define LCD_TYPE_HANTRONIX  4
0273 #define LCD_TYPE_NEXCOM     5
0274 
0275 /*
0276  * keypad types
0277  */
0278 #define KEYPAD_TYPE_NONE    0
0279 #define KEYPAD_TYPE_OLD     1
0280 #define KEYPAD_TYPE_NEW     2
0281 #define KEYPAD_TYPE_NEXCOM  3
0282 
0283 /*
0284  * panel profiles
0285  */
0286 #define PANEL_PROFILE_CUSTOM    0
0287 #define PANEL_PROFILE_OLD   1
0288 #define PANEL_PROFILE_NEW   2
0289 #define PANEL_PROFILE_HANTRONIX 3
0290 #define PANEL_PROFILE_NEXCOM    4
0291 #define PANEL_PROFILE_LARGE 5
0292 
0293 /*
0294  * Construct custom config from the kernel's configuration
0295  */
0296 #define DEFAULT_PARPORT         0
0297 #define DEFAULT_PROFILE         PANEL_PROFILE_LARGE
0298 #define DEFAULT_KEYPAD_TYPE     KEYPAD_TYPE_OLD
0299 #define DEFAULT_LCD_TYPE        LCD_TYPE_OLD
0300 #define DEFAULT_LCD_HEIGHT      2
0301 #define DEFAULT_LCD_WIDTH       40
0302 #define DEFAULT_LCD_CHARSET     LCD_CHARSET_NORMAL
0303 #define DEFAULT_LCD_PROTO       LCD_PROTO_PARALLEL
0304 
0305 #define DEFAULT_LCD_PIN_E       PIN_AUTOLF
0306 #define DEFAULT_LCD_PIN_RS      PIN_SELECP
0307 #define DEFAULT_LCD_PIN_RW      PIN_INITP
0308 #define DEFAULT_LCD_PIN_SCL     PIN_STROBE
0309 #define DEFAULT_LCD_PIN_SDA     PIN_D0
0310 #define DEFAULT_LCD_PIN_BL      PIN_NOT_SET
0311 
0312 #ifdef CONFIG_PANEL_PARPORT
0313 #undef DEFAULT_PARPORT
0314 #define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
0315 #endif
0316 
0317 #ifdef CONFIG_PANEL_PROFILE
0318 #undef DEFAULT_PROFILE
0319 #define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
0320 #endif
0321 
0322 #if DEFAULT_PROFILE == 0    /* custom */
0323 #ifdef CONFIG_PANEL_KEYPAD
0324 #undef DEFAULT_KEYPAD_TYPE
0325 #define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
0326 #endif
0327 
0328 #ifdef CONFIG_PANEL_LCD
0329 #undef DEFAULT_LCD_TYPE
0330 #define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
0331 #endif
0332 
0333 #ifdef CONFIG_PANEL_LCD_HEIGHT
0334 #undef DEFAULT_LCD_HEIGHT
0335 #define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
0336 #endif
0337 
0338 #ifdef CONFIG_PANEL_LCD_WIDTH
0339 #undef DEFAULT_LCD_WIDTH
0340 #define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
0341 #endif
0342 
0343 #ifdef CONFIG_PANEL_LCD_BWIDTH
0344 #undef DEFAULT_LCD_BWIDTH
0345 #define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
0346 #endif
0347 
0348 #ifdef CONFIG_PANEL_LCD_HWIDTH
0349 #undef DEFAULT_LCD_HWIDTH
0350 #define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
0351 #endif
0352 
0353 #ifdef CONFIG_PANEL_LCD_CHARSET
0354 #undef DEFAULT_LCD_CHARSET
0355 #define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
0356 #endif
0357 
0358 #ifdef CONFIG_PANEL_LCD_PROTO
0359 #undef DEFAULT_LCD_PROTO
0360 #define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
0361 #endif
0362 
0363 #ifdef CONFIG_PANEL_LCD_PIN_E
0364 #undef DEFAULT_LCD_PIN_E
0365 #define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
0366 #endif
0367 
0368 #ifdef CONFIG_PANEL_LCD_PIN_RS
0369 #undef DEFAULT_LCD_PIN_RS
0370 #define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
0371 #endif
0372 
0373 #ifdef CONFIG_PANEL_LCD_PIN_RW
0374 #undef DEFAULT_LCD_PIN_RW
0375 #define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
0376 #endif
0377 
0378 #ifdef CONFIG_PANEL_LCD_PIN_SCL
0379 #undef DEFAULT_LCD_PIN_SCL
0380 #define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
0381 #endif
0382 
0383 #ifdef CONFIG_PANEL_LCD_PIN_SDA
0384 #undef DEFAULT_LCD_PIN_SDA
0385 #define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
0386 #endif
0387 
0388 #ifdef CONFIG_PANEL_LCD_PIN_BL
0389 #undef DEFAULT_LCD_PIN_BL
0390 #define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
0391 #endif
0392 
0393 #endif /* DEFAULT_PROFILE == 0 */
0394 
0395 /* global variables */
0396 
0397 /* Device single-open policy control */
0398 static atomic_t keypad_available = ATOMIC_INIT(1);
0399 
0400 static struct pardevice *pprt;
0401 
0402 static int keypad_initialized;
0403 
0404 static DEFINE_SPINLOCK(pprt_lock);
0405 static struct timer_list scan_timer;
0406 
0407 MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
0408 
0409 static int parport = DEFAULT_PARPORT;
0410 module_param(parport, int, 0000);
0411 MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
0412 
0413 static int profile = DEFAULT_PROFILE;
0414 module_param(profile, int, 0000);
0415 MODULE_PARM_DESC(profile,
0416          "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
0417          "4=16x2 nexcom; default=40x2, old kp");
0418 
0419 static int keypad_type = NOT_SET;
0420 module_param(keypad_type, int, 0000);
0421 MODULE_PARM_DESC(keypad_type,
0422          "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
0423 
0424 static int lcd_type = NOT_SET;
0425 module_param(lcd_type, int, 0000);
0426 MODULE_PARM_DESC(lcd_type,
0427          "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
0428 
0429 static int lcd_height = NOT_SET;
0430 module_param(lcd_height, int, 0000);
0431 MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
0432 
0433 static int lcd_width = NOT_SET;
0434 module_param(lcd_width, int, 0000);
0435 MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
0436 
0437 static int lcd_bwidth = NOT_SET;    /* internal buffer width (usually 40) */
0438 module_param(lcd_bwidth, int, 0000);
0439 MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
0440 
0441 static int lcd_hwidth = NOT_SET;    /* hardware buffer width (usually 64) */
0442 module_param(lcd_hwidth, int, 0000);
0443 MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
0444 
0445 static int lcd_charset = NOT_SET;
0446 module_param(lcd_charset, int, 0000);
0447 MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
0448 
0449 static int lcd_proto = NOT_SET;
0450 module_param(lcd_proto, int, 0000);
0451 MODULE_PARM_DESC(lcd_proto,
0452          "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
0453 
0454 /*
0455  * These are the parallel port pins the LCD control signals are connected to.
0456  * Set this to 0 if the signal is not used. Set it to its opposite value
0457  * (negative) if the signal is negated. -MAXINT is used to indicate that the
0458  * pin has not been explicitly specified.
0459  *
0460  * WARNING! no check will be performed about collisions with keypad !
0461  */
0462 
0463 static int lcd_e_pin  = PIN_NOT_SET;
0464 module_param(lcd_e_pin, int, 0000);
0465 MODULE_PARM_DESC(lcd_e_pin,
0466          "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
0467 
0468 static int lcd_rs_pin = PIN_NOT_SET;
0469 module_param(lcd_rs_pin, int, 0000);
0470 MODULE_PARM_DESC(lcd_rs_pin,
0471          "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
0472 
0473 static int lcd_rw_pin = PIN_NOT_SET;
0474 module_param(lcd_rw_pin, int, 0000);
0475 MODULE_PARM_DESC(lcd_rw_pin,
0476          "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
0477 
0478 static int lcd_cl_pin = PIN_NOT_SET;
0479 module_param(lcd_cl_pin, int, 0000);
0480 MODULE_PARM_DESC(lcd_cl_pin,
0481          "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
0482 
0483 static int lcd_da_pin = PIN_NOT_SET;
0484 module_param(lcd_da_pin, int, 0000);
0485 MODULE_PARM_DESC(lcd_da_pin,
0486          "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
0487 
0488 static int lcd_bl_pin = PIN_NOT_SET;
0489 module_param(lcd_bl_pin, int, 0000);
0490 MODULE_PARM_DESC(lcd_bl_pin,
0491          "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
0492 
0493 /* Deprecated module parameters - consider not using them anymore */
0494 
0495 static int lcd_enabled = NOT_SET;
0496 module_param(lcd_enabled, int, 0000);
0497 MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
0498 
0499 static int keypad_enabled = NOT_SET;
0500 module_param(keypad_enabled, int, 0000);
0501 MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
0502 
0503 /* for some LCD drivers (ks0074) we need a charset conversion table. */
0504 static const unsigned char lcd_char_conv_ks0074[256] = {
0505     /*          0|8   1|9   2|A   3|B   4|C   5|D   6|E   7|F */
0506     /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0507     /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0508     /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0509     /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0510     /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
0511     /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0512     /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0513     /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0514     /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0515     /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0516     /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
0517     /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
0518     /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0519     /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0520     /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0521     /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
0522     /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0523     /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0524     /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0525     /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0526     /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
0527     /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
0528     /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
0529     /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
0530     /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
0531     /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
0532     /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
0533     /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
0534     /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
0535     /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
0536     /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
0537     /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
0538 };
0539 
0540 static const char old_keypad_profile[][4][9] = {
0541     {"S0", "Left\n", "Left\n", ""},
0542     {"S1", "Down\n", "Down\n", ""},
0543     {"S2", "Up\n", "Up\n", ""},
0544     {"S3", "Right\n", "Right\n", ""},
0545     {"S4", "Esc\n", "Esc\n", ""},
0546     {"S5", "Ret\n", "Ret\n", ""},
0547     {"", "", "", ""}
0548 };
0549 
0550 /* signals, press, repeat, release */
0551 static const char new_keypad_profile[][4][9] = {
0552     {"S0", "Left\n", "Left\n", ""},
0553     {"S1", "Down\n", "Down\n", ""},
0554     {"S2", "Up\n", "Up\n", ""},
0555     {"S3", "Right\n", "Right\n", ""},
0556     {"S4s5", "", "Esc\n", "Esc\n"},
0557     {"s4S5", "", "Ret\n", "Ret\n"},
0558     {"S4S5", "Help\n", "", ""},
0559     /* add new signals above this line */
0560     {"", "", "", ""}
0561 };
0562 
0563 /* signals, press, repeat, release */
0564 static const char nexcom_keypad_profile[][4][9] = {
0565     {"a-p-e-", "Down\n", "Down\n", ""},
0566     {"a-p-E-", "Ret\n", "Ret\n", ""},
0567     {"a-P-E-", "Esc\n", "Esc\n", ""},
0568     {"a-P-e-", "Up\n", "Up\n", ""},
0569     /* add new signals above this line */
0570     {"", "", "", ""}
0571 };
0572 
0573 static const char (*keypad_profile)[4][9] = old_keypad_profile;
0574 
0575 static DECLARE_BITMAP(bits, LCD_BITS);
0576 
0577 static void lcd_get_bits(unsigned int port, int *val)
0578 {
0579     unsigned int bit, state;
0580 
0581     for (bit = 0; bit < LCD_BITS; bit++) {
0582         state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
0583         *val &= lcd_bits[port][bit][BIT_MSK];
0584         *val |= lcd_bits[port][bit][state];
0585     }
0586 }
0587 
0588 /* sets data port bits according to current signals values */
0589 static int set_data_bits(void)
0590 {
0591     int val;
0592 
0593     val = r_dtr(pprt);
0594     lcd_get_bits(LCD_PORT_D, &val);
0595     w_dtr(pprt, val);
0596     return val;
0597 }
0598 
0599 /* sets ctrl port bits according to current signals values */
0600 static int set_ctrl_bits(void)
0601 {
0602     int val;
0603 
0604     val = r_ctr(pprt);
0605     lcd_get_bits(LCD_PORT_C, &val);
0606     w_ctr(pprt, val);
0607     return val;
0608 }
0609 
0610 /* sets ctrl & data port bits according to current signals values */
0611 static void panel_set_bits(void)
0612 {
0613     set_data_bits();
0614     set_ctrl_bits();
0615 }
0616 
0617 /*
0618  * Converts a parallel port pin (from -25 to 25) to data and control ports
0619  * masks, and data and control port bits. The signal will be considered
0620  * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
0621  *
0622  * Result will be used this way :
0623  *   out(dport, in(dport) & d_val[2] | d_val[signal_state])
0624  *   out(cport, in(cport) & c_val[2] | c_val[signal_state])
0625  */
0626 static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
0627 {
0628     int d_bit, c_bit, inv;
0629 
0630     d_val[0] = 0;
0631     c_val[0] = 0;
0632     d_val[1] = 0;
0633     c_val[1] = 0;
0634     d_val[2] = 0xFF;
0635     c_val[2] = 0xFF;
0636 
0637     if (pin == 0)
0638         return;
0639 
0640     inv = (pin < 0);
0641     if (inv)
0642         pin = -pin;
0643 
0644     d_bit = 0;
0645     c_bit = 0;
0646 
0647     switch (pin) {
0648     case PIN_STROBE:    /* strobe, inverted */
0649         c_bit = PNL_PSTROBE;
0650         inv = !inv;
0651         break;
0652     case PIN_D0...PIN_D7:   /* D0 - D7 = 2 - 9 */
0653         d_bit = 1 << (pin - 2);
0654         break;
0655     case PIN_AUTOLF:    /* autofeed, inverted */
0656         c_bit = PNL_PAUTOLF;
0657         inv = !inv;
0658         break;
0659     case PIN_INITP:     /* init, direct */
0660         c_bit = PNL_PINITP;
0661         break;
0662     case PIN_SELECP:    /* select_in, inverted */
0663         c_bit = PNL_PSELECP;
0664         inv = !inv;
0665         break;
0666     default:        /* unknown pin, ignore */
0667         break;
0668     }
0669 
0670     if (c_bit) {
0671         c_val[2] &= ~c_bit;
0672         c_val[!inv] = c_bit;
0673     } else if (d_bit) {
0674         d_val[2] &= ~d_bit;
0675         d_val[!inv] = d_bit;
0676     }
0677 }
0678 
0679 /*
0680  * send a serial byte to the LCD panel. The caller is responsible for locking
0681  * if needed.
0682  */
0683 static void lcd_send_serial(int byte)
0684 {
0685     int bit;
0686 
0687     /*
0688      * the data bit is set on D0, and the clock on STROBE.
0689      * LCD reads D0 on STROBE's rising edge.
0690      */
0691     for (bit = 0; bit < 8; bit++) {
0692         clear_bit(LCD_BIT_CL, bits);    /* CLK low */
0693         panel_set_bits();
0694         if (byte & 1) {
0695             set_bit(LCD_BIT_DA, bits);
0696         } else {
0697             clear_bit(LCD_BIT_DA, bits);
0698         }
0699 
0700         panel_set_bits();
0701         udelay(2);  /* maintain the data during 2 us before CLK up */
0702         set_bit(LCD_BIT_CL, bits);  /* CLK high */
0703         panel_set_bits();
0704         udelay(1);  /* maintain the strobe during 1 us */
0705         byte >>= 1;
0706     }
0707 }
0708 
0709 /* turn the backlight on or off */
0710 static void lcd_backlight(struct charlcd *charlcd, enum charlcd_onoff on)
0711 {
0712     if (lcd.pins.bl == PIN_NONE)
0713         return;
0714 
0715     /* The backlight is activated by setting the AUTOFEED line to +5V  */
0716     spin_lock_irq(&pprt_lock);
0717     if (on)
0718         set_bit(LCD_BIT_BL, bits);
0719     else
0720         clear_bit(LCD_BIT_BL, bits);
0721     panel_set_bits();
0722     spin_unlock_irq(&pprt_lock);
0723 }
0724 
0725 /* send a command to the LCD panel in serial mode */
0726 static void lcd_write_cmd_s(struct hd44780_common *hdc, int cmd)
0727 {
0728     spin_lock_irq(&pprt_lock);
0729     lcd_send_serial(0x1F);  /* R/W=W, RS=0 */
0730     lcd_send_serial(cmd & 0x0F);
0731     lcd_send_serial((cmd >> 4) & 0x0F);
0732     udelay(40);     /* the shortest command takes at least 40 us */
0733     spin_unlock_irq(&pprt_lock);
0734 }
0735 
0736 /* send data to the LCD panel in serial mode */
0737 static void lcd_write_data_s(struct hd44780_common *hdc, int data)
0738 {
0739     spin_lock_irq(&pprt_lock);
0740     lcd_send_serial(0x5F);  /* R/W=W, RS=1 */
0741     lcd_send_serial(data & 0x0F);
0742     lcd_send_serial((data >> 4) & 0x0F);
0743     udelay(40);     /* the shortest data takes at least 40 us */
0744     spin_unlock_irq(&pprt_lock);
0745 }
0746 
0747 /* send a command to the LCD panel in 8 bits parallel mode */
0748 static void lcd_write_cmd_p8(struct hd44780_common *hdc, int cmd)
0749 {
0750     spin_lock_irq(&pprt_lock);
0751     /* present the data to the data port */
0752     w_dtr(pprt, cmd);
0753     udelay(20); /* maintain the data during 20 us before the strobe */
0754 
0755     set_bit(LCD_BIT_E, bits);
0756     clear_bit(LCD_BIT_RS, bits);
0757     clear_bit(LCD_BIT_RW, bits);
0758     set_ctrl_bits();
0759 
0760     udelay(40); /* maintain the strobe during 40 us */
0761 
0762     clear_bit(LCD_BIT_E, bits);
0763     set_ctrl_bits();
0764 
0765     udelay(120);    /* the shortest command takes at least 120 us */
0766     spin_unlock_irq(&pprt_lock);
0767 }
0768 
0769 /* send data to the LCD panel in 8 bits parallel mode */
0770 static void lcd_write_data_p8(struct hd44780_common *hdc, int data)
0771 {
0772     spin_lock_irq(&pprt_lock);
0773     /* present the data to the data port */
0774     w_dtr(pprt, data);
0775     udelay(20); /* maintain the data during 20 us before the strobe */
0776 
0777     set_bit(LCD_BIT_E, bits);
0778     set_bit(LCD_BIT_RS, bits);
0779     clear_bit(LCD_BIT_RW, bits);
0780     set_ctrl_bits();
0781 
0782     udelay(40); /* maintain the strobe during 40 us */
0783 
0784     clear_bit(LCD_BIT_E, bits);
0785     set_ctrl_bits();
0786 
0787     udelay(45); /* the shortest data takes at least 45 us */
0788     spin_unlock_irq(&pprt_lock);
0789 }
0790 
0791 /* send a command to the TI LCD panel */
0792 static void lcd_write_cmd_tilcd(struct hd44780_common *hdc, int cmd)
0793 {
0794     spin_lock_irq(&pprt_lock);
0795     /* present the data to the control port */
0796     w_ctr(pprt, cmd);
0797     udelay(60);
0798     spin_unlock_irq(&pprt_lock);
0799 }
0800 
0801 /* send data to the TI LCD panel */
0802 static void lcd_write_data_tilcd(struct hd44780_common *hdc, int data)
0803 {
0804     spin_lock_irq(&pprt_lock);
0805     /* present the data to the data port */
0806     w_dtr(pprt, data);
0807     udelay(60);
0808     spin_unlock_irq(&pprt_lock);
0809 }
0810 
0811 static const struct charlcd_ops charlcd_ops = {
0812     .backlight  = lcd_backlight,
0813     .print      = hd44780_common_print,
0814     .gotoxy     = hd44780_common_gotoxy,
0815     .home       = hd44780_common_home,
0816     .clear_display  = hd44780_common_clear_display,
0817     .init_display   = hd44780_common_init_display,
0818     .shift_cursor   = hd44780_common_shift_cursor,
0819     .shift_display  = hd44780_common_shift_display,
0820     .display    = hd44780_common_display,
0821     .cursor     = hd44780_common_cursor,
0822     .blink      = hd44780_common_blink,
0823     .fontsize   = hd44780_common_fontsize,
0824     .lines      = hd44780_common_lines,
0825     .redefine_char  = hd44780_common_redefine_char,
0826 };
0827 
0828 /* initialize the LCD driver */
0829 static void lcd_init(void)
0830 {
0831     struct charlcd *charlcd;
0832     struct hd44780_common *hdc;
0833 
0834     hdc = hd44780_common_alloc();
0835     if (!hdc)
0836         return;
0837 
0838     charlcd = charlcd_alloc();
0839     if (!charlcd) {
0840         kfree(hdc);
0841         return;
0842     }
0843 
0844     hdc->hd44780 = &lcd;
0845     charlcd->drvdata = hdc;
0846 
0847     /*
0848      * Init lcd struct with load-time values to preserve exact
0849      * current functionality (at least for now).
0850      */
0851     charlcd->height = lcd_height;
0852     charlcd->width = lcd_width;
0853     hdc->bwidth = lcd_bwidth;
0854     hdc->hwidth = lcd_hwidth;
0855 
0856     switch (selected_lcd_type) {
0857     case LCD_TYPE_OLD:
0858         /* parallel mode, 8 bits */
0859         lcd.proto = LCD_PROTO_PARALLEL;
0860         lcd.charset = LCD_CHARSET_NORMAL;
0861         lcd.pins.e = PIN_STROBE;
0862         lcd.pins.rs = PIN_AUTOLF;
0863 
0864         charlcd->width = 40;
0865         hdc->bwidth = 40;
0866         hdc->hwidth = 64;
0867         charlcd->height = 2;
0868         break;
0869     case LCD_TYPE_KS0074:
0870         /* serial mode, ks0074 */
0871         lcd.proto = LCD_PROTO_SERIAL;
0872         lcd.charset = LCD_CHARSET_KS0074;
0873         lcd.pins.bl = PIN_AUTOLF;
0874         lcd.pins.cl = PIN_STROBE;
0875         lcd.pins.da = PIN_D0;
0876 
0877         charlcd->width = 16;
0878         hdc->bwidth = 40;
0879         hdc->hwidth = 16;
0880         charlcd->height = 2;
0881         break;
0882     case LCD_TYPE_NEXCOM:
0883         /* parallel mode, 8 bits, generic */
0884         lcd.proto = LCD_PROTO_PARALLEL;
0885         lcd.charset = LCD_CHARSET_NORMAL;
0886         lcd.pins.e = PIN_AUTOLF;
0887         lcd.pins.rs = PIN_SELECP;
0888         lcd.pins.rw = PIN_INITP;
0889 
0890         charlcd->width = 16;
0891         hdc->bwidth = 40;
0892         hdc->hwidth = 64;
0893         charlcd->height = 2;
0894         break;
0895     case LCD_TYPE_CUSTOM:
0896         /* customer-defined */
0897         lcd.proto = DEFAULT_LCD_PROTO;
0898         lcd.charset = DEFAULT_LCD_CHARSET;
0899         /* default geometry will be set later */
0900         break;
0901     case LCD_TYPE_HANTRONIX:
0902         /* parallel mode, 8 bits, hantronix-like */
0903     default:
0904         lcd.proto = LCD_PROTO_PARALLEL;
0905         lcd.charset = LCD_CHARSET_NORMAL;
0906         lcd.pins.e = PIN_STROBE;
0907         lcd.pins.rs = PIN_SELECP;
0908 
0909         charlcd->width = 16;
0910         hdc->bwidth = 40;
0911         hdc->hwidth = 64;
0912         charlcd->height = 2;
0913         break;
0914     }
0915 
0916     /* Overwrite with module params set on loading */
0917     if (lcd_height != NOT_SET)
0918         charlcd->height = lcd_height;
0919     if (lcd_width != NOT_SET)
0920         charlcd->width = lcd_width;
0921     if (lcd_bwidth != NOT_SET)
0922         hdc->bwidth = lcd_bwidth;
0923     if (lcd_hwidth != NOT_SET)
0924         hdc->hwidth = lcd_hwidth;
0925     if (lcd_charset != NOT_SET)
0926         lcd.charset = lcd_charset;
0927     if (lcd_proto != NOT_SET)
0928         lcd.proto = lcd_proto;
0929     if (lcd_e_pin != PIN_NOT_SET)
0930         lcd.pins.e = lcd_e_pin;
0931     if (lcd_rs_pin != PIN_NOT_SET)
0932         lcd.pins.rs = lcd_rs_pin;
0933     if (lcd_rw_pin != PIN_NOT_SET)
0934         lcd.pins.rw = lcd_rw_pin;
0935     if (lcd_cl_pin != PIN_NOT_SET)
0936         lcd.pins.cl = lcd_cl_pin;
0937     if (lcd_da_pin != PIN_NOT_SET)
0938         lcd.pins.da = lcd_da_pin;
0939     if (lcd_bl_pin != PIN_NOT_SET)
0940         lcd.pins.bl = lcd_bl_pin;
0941 
0942     /* this is used to catch wrong and default values */
0943     if (charlcd->width <= 0)
0944         charlcd->width = DEFAULT_LCD_WIDTH;
0945     if (hdc->bwidth <= 0)
0946         hdc->bwidth = DEFAULT_LCD_BWIDTH;
0947     if (hdc->hwidth <= 0)
0948         hdc->hwidth = DEFAULT_LCD_HWIDTH;
0949     if (charlcd->height <= 0)
0950         charlcd->height = DEFAULT_LCD_HEIGHT;
0951 
0952     if (lcd.proto == LCD_PROTO_SERIAL) {    /* SERIAL */
0953         charlcd->ops = &charlcd_ops;
0954         hdc->write_data = lcd_write_data_s;
0955         hdc->write_cmd = lcd_write_cmd_s;
0956 
0957         if (lcd.pins.cl == PIN_NOT_SET)
0958             lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
0959         if (lcd.pins.da == PIN_NOT_SET)
0960             lcd.pins.da = DEFAULT_LCD_PIN_SDA;
0961 
0962     } else if (lcd.proto == LCD_PROTO_PARALLEL) {   /* PARALLEL */
0963         charlcd->ops = &charlcd_ops;
0964         hdc->write_data = lcd_write_data_p8;
0965         hdc->write_cmd = lcd_write_cmd_p8;
0966 
0967         if (lcd.pins.e == PIN_NOT_SET)
0968             lcd.pins.e = DEFAULT_LCD_PIN_E;
0969         if (lcd.pins.rs == PIN_NOT_SET)
0970             lcd.pins.rs = DEFAULT_LCD_PIN_RS;
0971         if (lcd.pins.rw == PIN_NOT_SET)
0972             lcd.pins.rw = DEFAULT_LCD_PIN_RW;
0973     } else {
0974         charlcd->ops = &charlcd_ops;
0975         hdc->write_data = lcd_write_data_tilcd;
0976         hdc->write_cmd = lcd_write_cmd_tilcd;
0977     }
0978 
0979     if (lcd.pins.bl == PIN_NOT_SET)
0980         lcd.pins.bl = DEFAULT_LCD_PIN_BL;
0981 
0982     if (lcd.pins.e == PIN_NOT_SET)
0983         lcd.pins.e = PIN_NONE;
0984     if (lcd.pins.rs == PIN_NOT_SET)
0985         lcd.pins.rs = PIN_NONE;
0986     if (lcd.pins.rw == PIN_NOT_SET)
0987         lcd.pins.rw = PIN_NONE;
0988     if (lcd.pins.bl == PIN_NOT_SET)
0989         lcd.pins.bl = PIN_NONE;
0990     if (lcd.pins.cl == PIN_NOT_SET)
0991         lcd.pins.cl = PIN_NONE;
0992     if (lcd.pins.da == PIN_NOT_SET)
0993         lcd.pins.da = PIN_NONE;
0994 
0995     if (lcd.charset == NOT_SET)
0996         lcd.charset = DEFAULT_LCD_CHARSET;
0997 
0998     if (lcd.charset == LCD_CHARSET_KS0074)
0999         charlcd->char_conv = lcd_char_conv_ks0074;
1000     else
1001         charlcd->char_conv = NULL;
1002 
1003     pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1004             lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1005     pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1006             lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1007     pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1008             lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1009     pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1010             lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1011     pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1012             lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1013     pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1014             lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
1015 
1016     lcd.charlcd = charlcd;
1017     lcd.initialized = true;
1018 }
1019 
1020 /*
1021  * These are the file operation function for user access to /dev/keypad
1022  */
1023 
1024 static ssize_t keypad_read(struct file *file,
1025                char __user *buf, size_t count, loff_t *ppos)
1026 {
1027     unsigned i = *ppos;
1028     char __user *tmp = buf;
1029 
1030     if (keypad_buflen == 0) {
1031         if (file->f_flags & O_NONBLOCK)
1032             return -EAGAIN;
1033 
1034         if (wait_event_interruptible(keypad_read_wait,
1035                          keypad_buflen != 0))
1036             return -EINTR;
1037     }
1038 
1039     for (; count-- > 0 && (keypad_buflen > 0);
1040          ++i, ++tmp, --keypad_buflen) {
1041         put_user(keypad_buffer[keypad_start], tmp);
1042         keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1043     }
1044     *ppos = i;
1045 
1046     return tmp - buf;
1047 }
1048 
1049 static int keypad_open(struct inode *inode, struct file *file)
1050 {
1051     int ret;
1052 
1053     ret = -EBUSY;
1054     if (!atomic_dec_and_test(&keypad_available))
1055         goto fail;  /* open only once at a time */
1056 
1057     ret = -EPERM;
1058     if (file->f_mode & FMODE_WRITE) /* device is read-only */
1059         goto fail;
1060 
1061     keypad_buflen = 0;  /* flush the buffer on opening */
1062     return 0;
1063  fail:
1064     atomic_inc(&keypad_available);
1065     return ret;
1066 }
1067 
1068 static int keypad_release(struct inode *inode, struct file *file)
1069 {
1070     atomic_inc(&keypad_available);
1071     return 0;
1072 }
1073 
1074 static const struct file_operations keypad_fops = {
1075     .read    = keypad_read,     /* read */
1076     .open    = keypad_open,     /* open */
1077     .release = keypad_release,  /* close */
1078     .llseek  = default_llseek,
1079 };
1080 
1081 static struct miscdevice keypad_dev = {
1082     .minor  = KEYPAD_MINOR,
1083     .name   = "keypad",
1084     .fops   = &keypad_fops,
1085 };
1086 
1087 static void keypad_send_key(const char *string, int max_len)
1088 {
1089     /* send the key to the device only if a process is attached to it. */
1090     if (!atomic_read(&keypad_available)) {
1091         while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1092             keypad_buffer[(keypad_start + keypad_buflen++) %
1093                       KEYPAD_BUFFER] = *string++;
1094         }
1095         wake_up_interruptible(&keypad_read_wait);
1096     }
1097 }
1098 
1099 /* this function scans all the bits involving at least one logical signal,
1100  * and puts the results in the bitfield "phys_read" (one bit per established
1101  * contact), and sets "phys_read_prev" to "phys_read".
1102  *
1103  * Note: to debounce input signals, we will only consider as switched a signal
1104  * which is stable across 2 measures. Signals which are different between two
1105  * reads will be kept as they previously were in their logical form (phys_prev).
1106  * A signal which has just switched will have a 1 in
1107  * (phys_read ^ phys_read_prev).
1108  */
1109 static void phys_scan_contacts(void)
1110 {
1111     int bit, bitval;
1112     char oldval;
1113     char bitmask;
1114     char gndmask;
1115 
1116     phys_prev = phys_curr;
1117     phys_read_prev = phys_read;
1118     phys_read = 0;      /* flush all signals */
1119 
1120     /* keep track of old value, with all outputs disabled */
1121     oldval = r_dtr(pprt) | scan_mask_o;
1122     /* activate all keyboard outputs (active low) */
1123     w_dtr(pprt, oldval & ~scan_mask_o);
1124 
1125     /* will have a 1 for each bit set to gnd */
1126     bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1127     /* disable all matrix signals */
1128     w_dtr(pprt, oldval);
1129 
1130     /* now that all outputs are cleared, the only active input bits are
1131      * directly connected to the ground
1132      */
1133 
1134     /* 1 for each grounded input */
1135     gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1136 
1137     /* grounded inputs are signals 40-44 */
1138     phys_read |= (__u64)gndmask << 40;
1139 
1140     if (bitmask != gndmask) {
1141         /*
1142          * since clearing the outputs changed some inputs, we know
1143          * that some input signals are currently tied to some outputs.
1144          * So we'll scan them.
1145          */
1146         for (bit = 0; bit < 8; bit++) {
1147             bitval = BIT(bit);
1148 
1149             if (!(scan_mask_o & bitval))    /* skip unused bits */
1150                 continue;
1151 
1152             w_dtr(pprt, oldval & ~bitval);  /* enable this output */
1153             bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1154             phys_read |= (__u64)bitmask << (5 * bit);
1155         }
1156         w_dtr(pprt, oldval);    /* disable all outputs */
1157     }
1158     /*
1159      * this is easy: use old bits when they are flapping,
1160      * use new ones when stable
1161      */
1162     phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1163             (phys_read & ~(phys_read ^ phys_read_prev));
1164 }
1165 
1166 static inline int input_state_high(struct logical_input *input)
1167 {
1168 #if 0
1169     /* FIXME:
1170      * this is an invalid test. It tries to catch
1171      * transitions from single-key to multiple-key, but
1172      * doesn't take into account the contacts polarity.
1173      * The only solution to the problem is to parse keys
1174      * from the most complex to the simplest combinations,
1175      * and mark them as 'caught' once a combination
1176      * matches, then unmatch it for all other ones.
1177      */
1178 
1179     /* try to catch dangerous transitions cases :
1180      * someone adds a bit, so this signal was a false
1181      * positive resulting from a transition. We should
1182      * invalidate the signal immediately and not call the
1183      * release function.
1184      * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1185      */
1186     if (((phys_prev & input->mask) == input->value) &&
1187         ((phys_curr & input->mask) >  input->value)) {
1188         input->state = INPUT_ST_LOW; /* invalidate */
1189         return 1;
1190     }
1191 #endif
1192 
1193     if ((phys_curr & input->mask) == input->value) {
1194         if ((input->type == INPUT_TYPE_STD) &&
1195             (input->high_timer == 0)) {
1196             input->high_timer++;
1197             if (input->u.std.press_fct)
1198                 input->u.std.press_fct(input->u.std.press_data);
1199         } else if (input->type == INPUT_TYPE_KBD) {
1200             /* will turn on the light */
1201             keypressed = 1;
1202 
1203             if (input->high_timer == 0) {
1204                 char *press_str = input->u.kbd.press_str;
1205 
1206                 if (press_str[0]) {
1207                     int s = sizeof(input->u.kbd.press_str);
1208 
1209                     keypad_send_key(press_str, s);
1210                 }
1211             }
1212 
1213             if (input->u.kbd.repeat_str[0]) {
1214                 char *repeat_str = input->u.kbd.repeat_str;
1215 
1216                 if (input->high_timer >= KEYPAD_REP_START) {
1217                     int s = sizeof(input->u.kbd.repeat_str);
1218 
1219                     input->high_timer -= KEYPAD_REP_DELAY;
1220                     keypad_send_key(repeat_str, s);
1221                 }
1222                 /* we will need to come back here soon */
1223                 inputs_stable = 0;
1224             }
1225 
1226             if (input->high_timer < 255)
1227                 input->high_timer++;
1228         }
1229         return 1;
1230     }
1231 
1232     /* else signal falling down. Let's fall through. */
1233     input->state = INPUT_ST_FALLING;
1234     input->fall_timer = 0;
1235 
1236     return 0;
1237 }
1238 
1239 static inline void input_state_falling(struct logical_input *input)
1240 {
1241 #if 0
1242     /* FIXME !!! same comment as in input_state_high */
1243     if (((phys_prev & input->mask) == input->value) &&
1244         ((phys_curr & input->mask) >  input->value)) {
1245         input->state = INPUT_ST_LOW;    /* invalidate */
1246         return;
1247     }
1248 #endif
1249 
1250     if ((phys_curr & input->mask) == input->value) {
1251         if (input->type == INPUT_TYPE_KBD) {
1252             /* will turn on the light */
1253             keypressed = 1;
1254 
1255             if (input->u.kbd.repeat_str[0]) {
1256                 char *repeat_str = input->u.kbd.repeat_str;
1257 
1258                 if (input->high_timer >= KEYPAD_REP_START) {
1259                     int s = sizeof(input->u.kbd.repeat_str);
1260 
1261                     input->high_timer -= KEYPAD_REP_DELAY;
1262                     keypad_send_key(repeat_str, s);
1263                 }
1264                 /* we will need to come back here soon */
1265                 inputs_stable = 0;
1266             }
1267 
1268             if (input->high_timer < 255)
1269                 input->high_timer++;
1270         }
1271         input->state = INPUT_ST_HIGH;
1272     } else if (input->fall_timer >= input->fall_time) {
1273         /* call release event */
1274         if (input->type == INPUT_TYPE_STD) {
1275             void (*release_fct)(int) = input->u.std.release_fct;
1276 
1277             if (release_fct)
1278                 release_fct(input->u.std.release_data);
1279         } else if (input->type == INPUT_TYPE_KBD) {
1280             char *release_str = input->u.kbd.release_str;
1281 
1282             if (release_str[0]) {
1283                 int s = sizeof(input->u.kbd.release_str);
1284 
1285                 keypad_send_key(release_str, s);
1286             }
1287         }
1288 
1289         input->state = INPUT_ST_LOW;
1290     } else {
1291         input->fall_timer++;
1292         inputs_stable = 0;
1293     }
1294 }
1295 
1296 static void panel_process_inputs(void)
1297 {
1298     struct logical_input *input;
1299 
1300     keypressed = 0;
1301     inputs_stable = 1;
1302     list_for_each_entry(input, &logical_inputs, list) {
1303         switch (input->state) {
1304         case INPUT_ST_LOW:
1305             if ((phys_curr & input->mask) != input->value)
1306                 break;
1307             /* if all needed ones were already set previously,
1308              * this means that this logical signal has been
1309              * activated by the releasing of another combined
1310              * signal, so we don't want to match.
1311              * eg: AB -(release B)-> A -(release A)-> 0 :
1312              *     don't match A.
1313              */
1314             if ((phys_prev & input->mask) == input->value)
1315                 break;
1316             input->rise_timer = 0;
1317             input->state = INPUT_ST_RISING;
1318             fallthrough;
1319         case INPUT_ST_RISING:
1320             if ((phys_curr & input->mask) != input->value) {
1321                 input->state = INPUT_ST_LOW;
1322                 break;
1323             }
1324             if (input->rise_timer < input->rise_time) {
1325                 inputs_stable = 0;
1326                 input->rise_timer++;
1327                 break;
1328             }
1329             input->high_timer = 0;
1330             input->state = INPUT_ST_HIGH;
1331             fallthrough;
1332         case INPUT_ST_HIGH:
1333             if (input_state_high(input))
1334                 break;
1335             fallthrough;
1336         case INPUT_ST_FALLING:
1337             input_state_falling(input);
1338         }
1339     }
1340 }
1341 
1342 static void panel_scan_timer(struct timer_list *unused)
1343 {
1344     if (keypad.enabled && keypad_initialized) {
1345         if (spin_trylock_irq(&pprt_lock)) {
1346             phys_scan_contacts();
1347 
1348             /* no need for the parport anymore */
1349             spin_unlock_irq(&pprt_lock);
1350         }
1351 
1352         if (!inputs_stable || phys_curr != phys_prev)
1353             panel_process_inputs();
1354     }
1355 
1356     if (keypressed && lcd.enabled && lcd.initialized)
1357         charlcd_poke(lcd.charlcd);
1358 
1359     mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
1360 }
1361 
1362 static void init_scan_timer(void)
1363 {
1364     if (scan_timer.function)
1365         return;     /* already started */
1366 
1367     timer_setup(&scan_timer, panel_scan_timer, 0);
1368     scan_timer.expires = jiffies + INPUT_POLL_TIME;
1369     add_timer(&scan_timer);
1370 }
1371 
1372 /* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1373  * if <omask> or <imask> are non-null, they will be or'ed with the bits
1374  * corresponding to out and in bits respectively.
1375  * returns 1 if ok, 0 if error (in which case, nothing is written).
1376  */
1377 static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
1378               u8 *imask, u8 *omask)
1379 {
1380     const char sigtab[] = "EeSsPpAaBb";
1381     u8 im, om;
1382     __u64 m, v;
1383 
1384     om = 0;
1385     im = 0;
1386     m = 0ULL;
1387     v = 0ULL;
1388     while (*name) {
1389         int in, out, bit, neg;
1390         const char *idx;
1391 
1392         idx = strchr(sigtab, *name);
1393         if (!idx)
1394             return 0;   /* input name not found */
1395 
1396         in = idx - sigtab;
1397         neg = (in & 1); /* odd (lower) names are negated */
1398         in >>= 1;
1399         im |= BIT(in);
1400 
1401         name++;
1402         if (*name >= '0' && *name <= '7') {
1403             out = *name - '0';
1404             om |= BIT(out);
1405         } else if (*name == '-') {
1406             out = 8;
1407         } else {
1408             return 0;   /* unknown bit name */
1409         }
1410 
1411         bit = (out * 5) + in;
1412 
1413         m |= 1ULL << bit;
1414         if (!neg)
1415             v |= 1ULL << bit;
1416         name++;
1417     }
1418     *mask = m;
1419     *value = v;
1420     if (imask)
1421         *imask |= im;
1422     if (omask)
1423         *omask |= om;
1424     return 1;
1425 }
1426 
1427 /* tries to bind a key to the signal name <name>. The key will send the
1428  * strings <press>, <repeat>, <release> for these respective events.
1429  * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1430  */
1431 static struct logical_input *panel_bind_key(const char *name, const char *press,
1432                         const char *repeat,
1433                         const char *release)
1434 {
1435     struct logical_input *key;
1436 
1437     key = kzalloc(sizeof(*key), GFP_KERNEL);
1438     if (!key)
1439         return NULL;
1440 
1441     if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1442                  &scan_mask_o)) {
1443         kfree(key);
1444         return NULL;
1445     }
1446 
1447     key->type = INPUT_TYPE_KBD;
1448     key->state = INPUT_ST_LOW;
1449     key->rise_time = 1;
1450     key->fall_time = 1;
1451 
1452     strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
1453     strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
1454     strncpy(key->u.kbd.release_str, release,
1455         sizeof(key->u.kbd.release_str));
1456     list_add(&key->list, &logical_inputs);
1457     return key;
1458 }
1459 
1460 #if 0
1461 /* tries to bind a callback function to the signal name <name>. The function
1462  * <press_fct> will be called with the <press_data> arg when the signal is
1463  * activated, and so on for <release_fct>/<release_data>
1464  * Returns the pointer to the new signal if ok, NULL if the signal could not
1465  * be bound.
1466  */
1467 static struct logical_input *panel_bind_callback(char *name,
1468                          void (*press_fct)(int),
1469                          int press_data,
1470                          void (*release_fct)(int),
1471                          int release_data)
1472 {
1473     struct logical_input *callback;
1474 
1475     callback = kmalloc(sizeof(*callback), GFP_KERNEL);
1476     if (!callback)
1477         return NULL;
1478 
1479     memset(callback, 0, sizeof(struct logical_input));
1480     if (!input_name2mask(name, &callback->mask, &callback->value,
1481                  &scan_mask_i, &scan_mask_o))
1482         return NULL;
1483 
1484     callback->type = INPUT_TYPE_STD;
1485     callback->state = INPUT_ST_LOW;
1486     callback->rise_time = 1;
1487     callback->fall_time = 1;
1488     callback->u.std.press_fct = press_fct;
1489     callback->u.std.press_data = press_data;
1490     callback->u.std.release_fct = release_fct;
1491     callback->u.std.release_data = release_data;
1492     list_add(&callback->list, &logical_inputs);
1493     return callback;
1494 }
1495 #endif
1496 
1497 static void keypad_init(void)
1498 {
1499     int keynum;
1500 
1501     init_waitqueue_head(&keypad_read_wait);
1502     keypad_buflen = 0;  /* flushes any eventual noisy keystroke */
1503 
1504     /* Let's create all known keys */
1505 
1506     for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
1507         panel_bind_key(keypad_profile[keynum][0],
1508                    keypad_profile[keynum][1],
1509                    keypad_profile[keynum][2],
1510                    keypad_profile[keynum][3]);
1511     }
1512 
1513     init_scan_timer();
1514     keypad_initialized = 1;
1515 }
1516 
1517 /**************************************************/
1518 /* device initialization                          */
1519 /**************************************************/
1520 
1521 static void panel_attach(struct parport *port)
1522 {
1523     struct pardev_cb panel_cb;
1524 
1525     if (port->number != parport)
1526         return;
1527 
1528     if (pprt) {
1529         pr_err("%s: port->number=%d parport=%d, already registered!\n",
1530                __func__, port->number, parport);
1531         return;
1532     }
1533 
1534     memset(&panel_cb, 0, sizeof(panel_cb));
1535     panel_cb.private = &pprt;
1536     /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
1537 
1538     pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
1539     if (!pprt) {
1540         pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
1541                __func__, port->number, parport);
1542         return;
1543     }
1544 
1545     if (parport_claim(pprt)) {
1546         pr_err("could not claim access to parport%d. Aborting.\n",
1547                parport);
1548         goto err_unreg_device;
1549     }
1550 
1551     /* must init LCD first, just in case an IRQ from the keypad is
1552      * generated at keypad init
1553      */
1554     if (lcd.enabled) {
1555         lcd_init();
1556         if (!lcd.charlcd || charlcd_register(lcd.charlcd))
1557             goto err_unreg_device;
1558     }
1559 
1560     if (keypad.enabled) {
1561         keypad_init();
1562         if (misc_register(&keypad_dev))
1563             goto err_lcd_unreg;
1564     }
1565     return;
1566 
1567 err_lcd_unreg:
1568     if (scan_timer.function)
1569         del_timer_sync(&scan_timer);
1570     if (lcd.enabled)
1571         charlcd_unregister(lcd.charlcd);
1572 err_unreg_device:
1573     kfree(lcd.charlcd);
1574     lcd.charlcd = NULL;
1575     parport_unregister_device(pprt);
1576     pprt = NULL;
1577 }
1578 
1579 static void panel_detach(struct parport *port)
1580 {
1581     if (port->number != parport)
1582         return;
1583 
1584     if (!pprt) {
1585         pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
1586                __func__, port->number, parport);
1587         return;
1588     }
1589     if (scan_timer.function)
1590         del_timer_sync(&scan_timer);
1591 
1592     if (keypad.enabled) {
1593         misc_deregister(&keypad_dev);
1594         keypad_initialized = 0;
1595     }
1596 
1597     if (lcd.enabled) {
1598         charlcd_unregister(lcd.charlcd);
1599         lcd.initialized = false;
1600         kfree(lcd.charlcd->drvdata);
1601         kfree(lcd.charlcd);
1602         lcd.charlcd = NULL;
1603     }
1604 
1605     /* TODO: free all input signals */
1606     parport_release(pprt);
1607     parport_unregister_device(pprt);
1608     pprt = NULL;
1609 }
1610 
1611 static struct parport_driver panel_driver = {
1612     .name = "panel",
1613     .match_port = panel_attach,
1614     .detach = panel_detach,
1615     .devmodel = true,
1616 };
1617 
1618 /* init function */
1619 static int __init panel_init_module(void)
1620 {
1621     int selected_keypad_type = NOT_SET, err;
1622 
1623     /* take care of an eventual profile */
1624     switch (profile) {
1625     case PANEL_PROFILE_CUSTOM:
1626         /* custom profile */
1627         selected_keypad_type = DEFAULT_KEYPAD_TYPE;
1628         selected_lcd_type = DEFAULT_LCD_TYPE;
1629         break;
1630     case PANEL_PROFILE_OLD:
1631         /* 8 bits, 2*16, old keypad */
1632         selected_keypad_type = KEYPAD_TYPE_OLD;
1633         selected_lcd_type = LCD_TYPE_OLD;
1634 
1635         /* TODO: This two are a little hacky, sort it out later */
1636         if (lcd_width == NOT_SET)
1637             lcd_width = 16;
1638         if (lcd_hwidth == NOT_SET)
1639             lcd_hwidth = 16;
1640         break;
1641     case PANEL_PROFILE_NEW:
1642         /* serial, 2*16, new keypad */
1643         selected_keypad_type = KEYPAD_TYPE_NEW;
1644         selected_lcd_type = LCD_TYPE_KS0074;
1645         break;
1646     case PANEL_PROFILE_HANTRONIX:
1647         /* 8 bits, 2*16 hantronix-like, no keypad */
1648         selected_keypad_type = KEYPAD_TYPE_NONE;
1649         selected_lcd_type = LCD_TYPE_HANTRONIX;
1650         break;
1651     case PANEL_PROFILE_NEXCOM:
1652         /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
1653         selected_keypad_type = KEYPAD_TYPE_NEXCOM;
1654         selected_lcd_type = LCD_TYPE_NEXCOM;
1655         break;
1656     case PANEL_PROFILE_LARGE:
1657         /* 8 bits, 2*40, old keypad */
1658         selected_keypad_type = KEYPAD_TYPE_OLD;
1659         selected_lcd_type = LCD_TYPE_OLD;
1660         break;
1661     }
1662 
1663     /*
1664      * Overwrite selection with module param values (both keypad and lcd),
1665      * where the deprecated params have lower prio.
1666      */
1667     if (keypad_enabled != NOT_SET)
1668         selected_keypad_type = keypad_enabled;
1669     if (keypad_type != NOT_SET)
1670         selected_keypad_type = keypad_type;
1671 
1672     keypad.enabled = (selected_keypad_type > 0);
1673 
1674     if (lcd_enabled != NOT_SET)
1675         selected_lcd_type = lcd_enabled;
1676     if (lcd_type != NOT_SET)
1677         selected_lcd_type = lcd_type;
1678 
1679     lcd.enabled = (selected_lcd_type > 0);
1680 
1681     if (lcd.enabled) {
1682         /*
1683          * Init lcd struct with load-time values to preserve exact
1684          * current functionality (at least for now).
1685          */
1686         lcd.charset = lcd_charset;
1687         lcd.proto = lcd_proto;
1688         lcd.pins.e = lcd_e_pin;
1689         lcd.pins.rs = lcd_rs_pin;
1690         lcd.pins.rw = lcd_rw_pin;
1691         lcd.pins.cl = lcd_cl_pin;
1692         lcd.pins.da = lcd_da_pin;
1693         lcd.pins.bl = lcd_bl_pin;
1694     }
1695 
1696     switch (selected_keypad_type) {
1697     case KEYPAD_TYPE_OLD:
1698         keypad_profile = old_keypad_profile;
1699         break;
1700     case KEYPAD_TYPE_NEW:
1701         keypad_profile = new_keypad_profile;
1702         break;
1703     case KEYPAD_TYPE_NEXCOM:
1704         keypad_profile = nexcom_keypad_profile;
1705         break;
1706     default:
1707         keypad_profile = NULL;
1708         break;
1709     }
1710 
1711     if (!lcd.enabled && !keypad.enabled) {
1712         /* no device enabled, let's exit */
1713         pr_err("panel driver disabled.\n");
1714         return -ENODEV;
1715     }
1716 
1717     err = parport_register_driver(&panel_driver);
1718     if (err) {
1719         pr_err("could not register with parport. Aborting.\n");
1720         return err;
1721     }
1722 
1723     if (pprt)
1724         pr_info("panel driver registered on parport%d (io=0x%lx).\n",
1725             parport, pprt->port->base);
1726     else
1727         pr_info("panel driver not yet registered\n");
1728     return 0;
1729 }
1730 
1731 static void __exit panel_cleanup_module(void)
1732 {
1733     parport_unregister_driver(&panel_driver);
1734 }
1735 
1736 module_init(panel_init_module);
1737 module_exit(panel_cleanup_module);
1738 MODULE_AUTHOR("Willy Tarreau");
1739 MODULE_LICENSE("GPL");