Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Front panel driver for Linux
4 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
5 * Copyright (C) 2016-2017 Glider bvba
6 *
7 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
8 * connected to a parallel printer port.
9 *
10 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
11 * serial module compatible with Samsung's KS0074. The pins may be connected in
12 * any combination, everything is programmable.
13 *
14 * The keypad consists in a matrix of push buttons connecting input pins to
15 * data output pins or to the ground. The combinations have to be hard-coded
16 * in the driver, though several profiles exist and adding new ones is easy.
17 *
18 * Several profiles are provided for commonly found LCD+keypad modules on the
19 * market, such as those found in Nexcom's appliances.
20 *
21 * FIXME:
22 * - the initialization/deinitialization process is very dirty and should
23 * be rewritten. It may even be buggy.
24 *
25 * TODO:
26 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
27 * - make the LCD a part of a virtual screen of Vx*Vy
28 * - make the inputs list smp-safe
29 * - change the keyboard to a double mapping : signals -> key_id -> values
30 * so that applications can change values without knowing signals
31 *
32 */
33
34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36#include <linux/module.h>
37
38#include <linux/types.h>
39#include <linux/errno.h>
40#include <linux/signal.h>
41#include <linux/sched.h>
42#include <linux/spinlock.h>
43#include <linux/interrupt.h>
44#include <linux/miscdevice.h>
45#include <linux/slab.h>
46#include <linux/ioport.h>
47#include <linux/fcntl.h>
48#include <linux/init.h>
49#include <linux/delay.h>
50#include <linux/kernel.h>
51#include <linux/ctype.h>
52#include <linux/parport.h>
53#include <linux/list.h>
54
55#include <linux/io.h>
56#include <linux/uaccess.h>
57
58#include "charlcd.h"
59
60#define LCD_MAXBYTES 256 /* max burst write */
61
62#define KEYPAD_BUFFER 64
63
64/* poll the keyboard this every second */
65#define INPUT_POLL_TIME (HZ / 50)
66/* a key starts to repeat after this times INPUT_POLL_TIME */
67#define KEYPAD_REP_START (10)
68/* a key repeats this times INPUT_POLL_TIME */
69#define KEYPAD_REP_DELAY (2)
70
71/* converts an r_str() input to an active high, bits string : 000BAOSE */
72#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
73
74#define PNL_PBUSY 0x80 /* inverted input, active low */
75#define PNL_PACK 0x40 /* direct input, active low */
76#define PNL_POUTPA 0x20 /* direct input, active high */
77#define PNL_PSELECD 0x10 /* direct input, active high */
78#define PNL_PERRORP 0x08 /* direct input, active low */
79
80#define PNL_PBIDIR 0x20 /* bi-directional ports */
81/* high to read data in or-ed with data out */
82#define PNL_PINTEN 0x10
83#define PNL_PSELECP 0x08 /* inverted output, active low */
84#define PNL_PINITP 0x04 /* direct output, active low */
85#define PNL_PAUTOLF 0x02 /* inverted output, active low */
86#define PNL_PSTROBE 0x01 /* inverted output */
87
88#define PNL_PD0 0x01
89#define PNL_PD1 0x02
90#define PNL_PD2 0x04
91#define PNL_PD3 0x08
92#define PNL_PD4 0x10
93#define PNL_PD5 0x20
94#define PNL_PD6 0x40
95#define PNL_PD7 0x80
96
97#define PIN_NONE 0
98#define PIN_STROBE 1
99#define PIN_D0 2
100#define PIN_D1 3
101#define PIN_D2 4
102#define PIN_D3 5
103#define PIN_D4 6
104#define PIN_D5 7
105#define PIN_D6 8
106#define PIN_D7 9
107#define PIN_AUTOLF 14
108#define PIN_INITP 16
109#define PIN_SELECP 17
110#define PIN_NOT_SET 127
111
112#define NOT_SET -1
113
114/* macros to simplify use of the parallel port */
115#define r_ctr(x) (parport_read_control((x)->port))
116#define r_dtr(x) (parport_read_data((x)->port))
117#define r_str(x) (parport_read_status((x)->port))
118#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
119#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
120
121/* this defines which bits are to be used and which ones to be ignored */
122/* logical or of the output bits involved in the scan matrix */
123static __u8 scan_mask_o;
124/* logical or of the input bits involved in the scan matrix */
125static __u8 scan_mask_i;
126
127enum input_type {
128 INPUT_TYPE_STD,
129 INPUT_TYPE_KBD,
130};
131
132enum input_state {
133 INPUT_ST_LOW,
134 INPUT_ST_RISING,
135 INPUT_ST_HIGH,
136 INPUT_ST_FALLING,
137};
138
139struct logical_input {
140 struct list_head list;
141 __u64 mask;
142 __u64 value;
143 enum input_type type;
144 enum input_state state;
145 __u8 rise_time, fall_time;
146 __u8 rise_timer, fall_timer, high_timer;
147
148 union {
149 struct { /* valid when type == INPUT_TYPE_STD */
150 void (*press_fct)(int);
151 void (*release_fct)(int);
152 int press_data;
153 int release_data;
154 } std;
155 struct { /* valid when type == INPUT_TYPE_KBD */
156 char press_str[sizeof(void *) + sizeof(int)] __nonstring;
157 char repeat_str[sizeof(void *) + sizeof(int)] __nonstring;
158 char release_str[sizeof(void *) + sizeof(int)] __nonstring;
159 } kbd;
160 } u;
161};
162
163static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
164
165/* physical contacts history
166 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
167 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
168 * corresponds to the ground.
169 * Within each group, bits are stored in the same order as read on the port :
170 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
171 * So, each __u64 is represented like this :
172 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
173 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
174 */
175
176/* what has just been read from the I/O ports */
177static __u64 phys_read;
178/* previous phys_read */
179static __u64 phys_read_prev;
180/* stabilized phys_read (phys_read|phys_read_prev) */
181static __u64 phys_curr;
182/* previous phys_curr */
183static __u64 phys_prev;
184/* 0 means that at least one logical signal needs be computed */
185static char inputs_stable;
186
187/* these variables are specific to the keypad */
188static struct {
189 bool enabled;
190} keypad;
191
192static char keypad_buffer[KEYPAD_BUFFER];
193static int keypad_buflen;
194static int keypad_start;
195static char keypressed;
196static wait_queue_head_t keypad_read_wait;
197
198/* lcd-specific variables */
199static struct {
200 bool enabled;
201 bool initialized;
202
203 int charset;
204 int proto;
205
206 /* TODO: use union here? */
207 struct {
208 int e;
209 int rs;
210 int rw;
211 int cl;
212 int da;
213 int bl;
214 } pins;
215
216 struct charlcd *charlcd;
217} lcd;
218
219/* Needed only for init */
220static int selected_lcd_type = NOT_SET;
221
222/*
223 * Bit masks to convert LCD signals to parallel port outputs.
224 * _d_ are values for data port, _c_ are for control port.
225 * [0] = signal OFF, [1] = signal ON, [2] = mask
226 */
227#define BIT_CLR 0
228#define BIT_SET 1
229#define BIT_MSK 2
230#define BIT_STATES 3
231/*
232 * one entry for each bit on the LCD
233 */
234#define LCD_BIT_E 0
235#define LCD_BIT_RS 1
236#define LCD_BIT_RW 2
237#define LCD_BIT_BL 3
238#define LCD_BIT_CL 4
239#define LCD_BIT_DA 5
240#define LCD_BITS 6
241
242/*
243 * each bit can be either connected to a DATA or CTRL port
244 */
245#define LCD_PORT_C 0
246#define LCD_PORT_D 1
247#define LCD_PORTS 2
248
249static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
250
251/*
252 * LCD protocols
253 */
254#define LCD_PROTO_PARALLEL 0
255#define LCD_PROTO_SERIAL 1
256#define LCD_PROTO_TI_DA8XX_LCD 2
257
258/*
259 * LCD character sets
260 */
261#define LCD_CHARSET_NORMAL 0
262#define LCD_CHARSET_KS0074 1
263
264/*
265 * LCD types
266 */
267#define LCD_TYPE_NONE 0
268#define LCD_TYPE_CUSTOM 1
269#define LCD_TYPE_OLD 2
270#define LCD_TYPE_KS0074 3
271#define LCD_TYPE_HANTRONIX 4
272#define LCD_TYPE_NEXCOM 5
273
274/*
275 * keypad types
276 */
277#define KEYPAD_TYPE_NONE 0
278#define KEYPAD_TYPE_OLD 1
279#define KEYPAD_TYPE_NEW 2
280#define KEYPAD_TYPE_NEXCOM 3
281
282/*
283 * panel profiles
284 */
285#define PANEL_PROFILE_CUSTOM 0
286#define PANEL_PROFILE_OLD 1
287#define PANEL_PROFILE_NEW 2
288#define PANEL_PROFILE_HANTRONIX 3
289#define PANEL_PROFILE_NEXCOM 4
290#define PANEL_PROFILE_LARGE 5
291
292/*
293 * Construct custom config from the kernel's configuration
294 */
295#define DEFAULT_PARPORT 0
296#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
297#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
298#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
299#define DEFAULT_LCD_HEIGHT 2
300#define DEFAULT_LCD_WIDTH 40
301#define DEFAULT_LCD_BWIDTH 40
302#define DEFAULT_LCD_HWIDTH 64
303#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
304#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
305
306#define DEFAULT_LCD_PIN_E PIN_AUTOLF
307#define DEFAULT_LCD_PIN_RS PIN_SELECP
308#define DEFAULT_LCD_PIN_RW PIN_INITP
309#define DEFAULT_LCD_PIN_SCL PIN_STROBE
310#define DEFAULT_LCD_PIN_SDA PIN_D0
311#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
312
313#ifdef CONFIG_PANEL_PARPORT
314#undef DEFAULT_PARPORT
315#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
316#endif
317
318#ifdef CONFIG_PANEL_PROFILE
319#undef DEFAULT_PROFILE
320#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
321#endif
322
323#if DEFAULT_PROFILE == 0 /* custom */
324#ifdef CONFIG_PANEL_KEYPAD
325#undef DEFAULT_KEYPAD_TYPE
326#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
327#endif
328
329#ifdef CONFIG_PANEL_LCD
330#undef DEFAULT_LCD_TYPE
331#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
332#endif
333
334#ifdef CONFIG_PANEL_LCD_HEIGHT
335#undef DEFAULT_LCD_HEIGHT
336#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
337#endif
338
339#ifdef CONFIG_PANEL_LCD_WIDTH
340#undef DEFAULT_LCD_WIDTH
341#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
342#endif
343
344#ifdef CONFIG_PANEL_LCD_BWIDTH
345#undef DEFAULT_LCD_BWIDTH
346#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
347#endif
348
349#ifdef CONFIG_PANEL_LCD_HWIDTH
350#undef DEFAULT_LCD_HWIDTH
351#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
352#endif
353
354#ifdef CONFIG_PANEL_LCD_CHARSET
355#undef DEFAULT_LCD_CHARSET
356#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
357#endif
358
359#ifdef CONFIG_PANEL_LCD_PROTO
360#undef DEFAULT_LCD_PROTO
361#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
362#endif
363
364#ifdef CONFIG_PANEL_LCD_PIN_E
365#undef DEFAULT_LCD_PIN_E
366#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
367#endif
368
369#ifdef CONFIG_PANEL_LCD_PIN_RS
370#undef DEFAULT_LCD_PIN_RS
371#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
372#endif
373
374#ifdef CONFIG_PANEL_LCD_PIN_RW
375#undef DEFAULT_LCD_PIN_RW
376#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
377#endif
378
379#ifdef CONFIG_PANEL_LCD_PIN_SCL
380#undef DEFAULT_LCD_PIN_SCL
381#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
382#endif
383
384#ifdef CONFIG_PANEL_LCD_PIN_SDA
385#undef DEFAULT_LCD_PIN_SDA
386#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
387#endif
388
389#ifdef CONFIG_PANEL_LCD_PIN_BL
390#undef DEFAULT_LCD_PIN_BL
391#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
392#endif
393
394#endif /* DEFAULT_PROFILE == 0 */
395
396/* global variables */
397
398/* Device single-open policy control */
399static atomic_t keypad_available = ATOMIC_INIT(1);
400
401static struct pardevice *pprt;
402
403static int keypad_initialized;
404
405static DEFINE_SPINLOCK(pprt_lock);
406static struct timer_list scan_timer;
407
408MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
409
410static int parport = DEFAULT_PARPORT;
411module_param(parport, int, 0000);
412MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
413
414static int profile = DEFAULT_PROFILE;
415module_param(profile, int, 0000);
416MODULE_PARM_DESC(profile,
417 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
418 "4=16x2 nexcom; default=40x2, old kp");
419
420static int keypad_type = NOT_SET;
421module_param(keypad_type, int, 0000);
422MODULE_PARM_DESC(keypad_type,
423 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
424
425static int lcd_type = NOT_SET;
426module_param(lcd_type, int, 0000);
427MODULE_PARM_DESC(lcd_type,
428 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
429
430static int lcd_height = NOT_SET;
431module_param(lcd_height, int, 0000);
432MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
433
434static int lcd_width = NOT_SET;
435module_param(lcd_width, int, 0000);
436MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
437
438static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
439module_param(lcd_bwidth, int, 0000);
440MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
441
442static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
443module_param(lcd_hwidth, int, 0000);
444MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
445
446static int lcd_charset = NOT_SET;
447module_param(lcd_charset, int, 0000);
448MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
449
450static int lcd_proto = NOT_SET;
451module_param(lcd_proto, int, 0000);
452MODULE_PARM_DESC(lcd_proto,
453 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
454
455/*
456 * These are the parallel port pins the LCD control signals are connected to.
457 * Set this to 0 if the signal is not used. Set it to its opposite value
458 * (negative) if the signal is negated. -MAXINT is used to indicate that the
459 * pin has not been explicitly specified.
460 *
461 * WARNING! no check will be performed about collisions with keypad !
462 */
463
464static int lcd_e_pin = PIN_NOT_SET;
465module_param(lcd_e_pin, int, 0000);
466MODULE_PARM_DESC(lcd_e_pin,
467 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
468
469static int lcd_rs_pin = PIN_NOT_SET;
470module_param(lcd_rs_pin, int, 0000);
471MODULE_PARM_DESC(lcd_rs_pin,
472 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
473
474static int lcd_rw_pin = PIN_NOT_SET;
475module_param(lcd_rw_pin, int, 0000);
476MODULE_PARM_DESC(lcd_rw_pin,
477 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
478
479static int lcd_cl_pin = PIN_NOT_SET;
480module_param(lcd_cl_pin, int, 0000);
481MODULE_PARM_DESC(lcd_cl_pin,
482 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
483
484static int lcd_da_pin = PIN_NOT_SET;
485module_param(lcd_da_pin, int, 0000);
486MODULE_PARM_DESC(lcd_da_pin,
487 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
488
489static int lcd_bl_pin = PIN_NOT_SET;
490module_param(lcd_bl_pin, int, 0000);
491MODULE_PARM_DESC(lcd_bl_pin,
492 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
493
494/* Deprecated module parameters - consider not using them anymore */
495
496static int lcd_enabled = NOT_SET;
497module_param(lcd_enabled, int, 0000);
498MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
499
500static int keypad_enabled = NOT_SET;
501module_param(keypad_enabled, int, 0000);
502MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
503
504/* for some LCD drivers (ks0074) we need a charset conversion table. */
505static const unsigned char lcd_char_conv_ks0074[256] = {
506 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
507 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
508 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
509 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
510 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
511 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
512 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
513 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
514 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
515 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
516 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
517 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
518 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
519 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
520 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
521 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
522 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
523 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
524 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
525 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
526 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
527 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
528 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
529 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
530 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
531 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
532 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
533 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
534 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
535 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
536 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
537 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
538 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
539};
540
541static const char old_keypad_profile[][4][9] = {
542 {"S0", "Left\n", "Left\n", ""},
543 {"S1", "Down\n", "Down\n", ""},
544 {"S2", "Up\n", "Up\n", ""},
545 {"S3", "Right\n", "Right\n", ""},
546 {"S4", "Esc\n", "Esc\n", ""},
547 {"S5", "Ret\n", "Ret\n", ""},
548 {"", "", "", ""}
549};
550
551/* signals, press, repeat, release */
552static const char new_keypad_profile[][4][9] = {
553 {"S0", "Left\n", "Left\n", ""},
554 {"S1", "Down\n", "Down\n", ""},
555 {"S2", "Up\n", "Up\n", ""},
556 {"S3", "Right\n", "Right\n", ""},
557 {"S4s5", "", "Esc\n", "Esc\n"},
558 {"s4S5", "", "Ret\n", "Ret\n"},
559 {"S4S5", "Help\n", "", ""},
560 /* add new signals above this line */
561 {"", "", "", ""}
562};
563
564/* signals, press, repeat, release */
565static const char nexcom_keypad_profile[][4][9] = {
566 {"a-p-e-", "Down\n", "Down\n", ""},
567 {"a-p-E-", "Ret\n", "Ret\n", ""},
568 {"a-P-E-", "Esc\n", "Esc\n", ""},
569 {"a-P-e-", "Up\n", "Up\n", ""},
570 /* add new signals above this line */
571 {"", "", "", ""}
572};
573
574static const char (*keypad_profile)[4][9] = old_keypad_profile;
575
576static DECLARE_BITMAP(bits, LCD_BITS);
577
578static void lcd_get_bits(unsigned int port, int *val)
579{
580 unsigned int bit, state;
581
582 for (bit = 0; bit < LCD_BITS; bit++) {
583 state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
584 *val &= lcd_bits[port][bit][BIT_MSK];
585 *val |= lcd_bits[port][bit][state];
586 }
587}
588
589/* sets data port bits according to current signals values */
590static int set_data_bits(void)
591{
592 int val;
593
594 val = r_dtr(pprt);
595 lcd_get_bits(LCD_PORT_D, &val);
596 w_dtr(pprt, val);
597 return val;
598}
599
600/* sets ctrl port bits according to current signals values */
601static int set_ctrl_bits(void)
602{
603 int val;
604
605 val = r_ctr(pprt);
606 lcd_get_bits(LCD_PORT_C, &val);
607 w_ctr(pprt, val);
608 return val;
609}
610
611/* sets ctrl & data port bits according to current signals values */
612static void panel_set_bits(void)
613{
614 set_data_bits();
615 set_ctrl_bits();
616}
617
618/*
619 * Converts a parallel port pin (from -25 to 25) to data and control ports
620 * masks, and data and control port bits. The signal will be considered
621 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
622 *
623 * Result will be used this way :
624 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
625 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
626 */
627static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
628{
629 int d_bit, c_bit, inv;
630
631 d_val[0] = 0;
632 c_val[0] = 0;
633 d_val[1] = 0;
634 c_val[1] = 0;
635 d_val[2] = 0xFF;
636 c_val[2] = 0xFF;
637
638 if (pin == 0)
639 return;
640
641 inv = (pin < 0);
642 if (inv)
643 pin = -pin;
644
645 d_bit = 0;
646 c_bit = 0;
647
648 switch (pin) {
649 case PIN_STROBE: /* strobe, inverted */
650 c_bit = PNL_PSTROBE;
651 inv = !inv;
652 break;
653 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
654 d_bit = 1 << (pin - 2);
655 break;
656 case PIN_AUTOLF: /* autofeed, inverted */
657 c_bit = PNL_PAUTOLF;
658 inv = !inv;
659 break;
660 case PIN_INITP: /* init, direct */
661 c_bit = PNL_PINITP;
662 break;
663 case PIN_SELECP: /* select_in, inverted */
664 c_bit = PNL_PSELECP;
665 inv = !inv;
666 break;
667 default: /* unknown pin, ignore */
668 break;
669 }
670
671 if (c_bit) {
672 c_val[2] &= ~c_bit;
673 c_val[!inv] = c_bit;
674 } else if (d_bit) {
675 d_val[2] &= ~d_bit;
676 d_val[!inv] = d_bit;
677 }
678}
679
680/*
681 * send a serial byte to the LCD panel. The caller is responsible for locking
682 * if needed.
683 */
684static void lcd_send_serial(int byte)
685{
686 int bit;
687
688 /*
689 * the data bit is set on D0, and the clock on STROBE.
690 * LCD reads D0 on STROBE's rising edge.
691 */
692 for (bit = 0; bit < 8; bit++) {
693 clear_bit(LCD_BIT_CL, bits); /* CLK low */
694 panel_set_bits();
695 if (byte & 1) {
696 set_bit(LCD_BIT_DA, bits);
697 } else {
698 clear_bit(LCD_BIT_DA, bits);
699 }
700
701 panel_set_bits();
702 udelay(2); /* maintain the data during 2 us before CLK up */
703 set_bit(LCD_BIT_CL, bits); /* CLK high */
704 panel_set_bits();
705 udelay(1); /* maintain the strobe during 1 us */
706 byte >>= 1;
707 }
708}
709
710/* turn the backlight on or off */
711static void lcd_backlight(struct charlcd *charlcd, int on)
712{
713 if (lcd.pins.bl == PIN_NONE)
714 return;
715
716 /* The backlight is activated by setting the AUTOFEED line to +5V */
717 spin_lock_irq(&pprt_lock);
718 if (on)
719 set_bit(LCD_BIT_BL, bits);
720 else
721 clear_bit(LCD_BIT_BL, bits);
722 panel_set_bits();
723 spin_unlock_irq(&pprt_lock);
724}
725
726/* send a command to the LCD panel in serial mode */
727static void lcd_write_cmd_s(struct charlcd *charlcd, int cmd)
728{
729 spin_lock_irq(&pprt_lock);
730 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
731 lcd_send_serial(cmd & 0x0F);
732 lcd_send_serial((cmd >> 4) & 0x0F);
733 udelay(40); /* the shortest command takes at least 40 us */
734 spin_unlock_irq(&pprt_lock);
735}
736
737/* send data to the LCD panel in serial mode */
738static void lcd_write_data_s(struct charlcd *charlcd, int data)
739{
740 spin_lock_irq(&pprt_lock);
741 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
742 lcd_send_serial(data & 0x0F);
743 lcd_send_serial((data >> 4) & 0x0F);
744 udelay(40); /* the shortest data takes at least 40 us */
745 spin_unlock_irq(&pprt_lock);
746}
747
748/* send a command to the LCD panel in 8 bits parallel mode */
749static void lcd_write_cmd_p8(struct charlcd *charlcd, int cmd)
750{
751 spin_lock_irq(&pprt_lock);
752 /* present the data to the data port */
753 w_dtr(pprt, cmd);
754 udelay(20); /* maintain the data during 20 us before the strobe */
755
756 set_bit(LCD_BIT_E, bits);
757 clear_bit(LCD_BIT_RS, bits);
758 clear_bit(LCD_BIT_RW, bits);
759 set_ctrl_bits();
760
761 udelay(40); /* maintain the strobe during 40 us */
762
763 clear_bit(LCD_BIT_E, bits);
764 set_ctrl_bits();
765
766 udelay(120); /* the shortest command takes at least 120 us */
767 spin_unlock_irq(&pprt_lock);
768}
769
770/* send data to the LCD panel in 8 bits parallel mode */
771static void lcd_write_data_p8(struct charlcd *charlcd, int data)
772{
773 spin_lock_irq(&pprt_lock);
774 /* present the data to the data port */
775 w_dtr(pprt, data);
776 udelay(20); /* maintain the data during 20 us before the strobe */
777
778 set_bit(LCD_BIT_E, bits);
779 set_bit(LCD_BIT_RS, bits);
780 clear_bit(LCD_BIT_RW, bits);
781 set_ctrl_bits();
782
783 udelay(40); /* maintain the strobe during 40 us */
784
785 clear_bit(LCD_BIT_E, bits);
786 set_ctrl_bits();
787
788 udelay(45); /* the shortest data takes at least 45 us */
789 spin_unlock_irq(&pprt_lock);
790}
791
792/* send a command to the TI LCD panel */
793static void lcd_write_cmd_tilcd(struct charlcd *charlcd, int cmd)
794{
795 spin_lock_irq(&pprt_lock);
796 /* present the data to the control port */
797 w_ctr(pprt, cmd);
798 udelay(60);
799 spin_unlock_irq(&pprt_lock);
800}
801
802/* send data to the TI LCD panel */
803static void lcd_write_data_tilcd(struct charlcd *charlcd, int data)
804{
805 spin_lock_irq(&pprt_lock);
806 /* present the data to the data port */
807 w_dtr(pprt, data);
808 udelay(60);
809 spin_unlock_irq(&pprt_lock);
810}
811
812/* fills the display with spaces and resets X/Y */
813static void lcd_clear_fast_s(struct charlcd *charlcd)
814{
815 int pos;
816
817 spin_lock_irq(&pprt_lock);
818 for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
819 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
820 lcd_send_serial(' ' & 0x0F);
821 lcd_send_serial((' ' >> 4) & 0x0F);
822 /* the shortest data takes at least 40 us */
823 udelay(40);
824 }
825 spin_unlock_irq(&pprt_lock);
826}
827
828/* fills the display with spaces and resets X/Y */
829static void lcd_clear_fast_p8(struct charlcd *charlcd)
830{
831 int pos;
832
833 spin_lock_irq(&pprt_lock);
834 for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
835 /* present the data to the data port */
836 w_dtr(pprt, ' ');
837
838 /* maintain the data during 20 us before the strobe */
839 udelay(20);
840
841 set_bit(LCD_BIT_E, bits);
842 set_bit(LCD_BIT_RS, bits);
843 clear_bit(LCD_BIT_RW, bits);
844 set_ctrl_bits();
845
846 /* maintain the strobe during 40 us */
847 udelay(40);
848
849 clear_bit(LCD_BIT_E, bits);
850 set_ctrl_bits();
851
852 /* the shortest data takes at least 45 us */
853 udelay(45);
854 }
855 spin_unlock_irq(&pprt_lock);
856}
857
858/* fills the display with spaces and resets X/Y */
859static void lcd_clear_fast_tilcd(struct charlcd *charlcd)
860{
861 int pos;
862
863 spin_lock_irq(&pprt_lock);
864 for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
865 /* present the data to the data port */
866 w_dtr(pprt, ' ');
867 udelay(60);
868 }
869
870 spin_unlock_irq(&pprt_lock);
871}
872
873static const struct charlcd_ops charlcd_serial_ops = {
874 .write_cmd = lcd_write_cmd_s,
875 .write_data = lcd_write_data_s,
876 .clear_fast = lcd_clear_fast_s,
877 .backlight = lcd_backlight,
878};
879
880static const struct charlcd_ops charlcd_parallel_ops = {
881 .write_cmd = lcd_write_cmd_p8,
882 .write_data = lcd_write_data_p8,
883 .clear_fast = lcd_clear_fast_p8,
884 .backlight = lcd_backlight,
885};
886
887static const struct charlcd_ops charlcd_tilcd_ops = {
888 .write_cmd = lcd_write_cmd_tilcd,
889 .write_data = lcd_write_data_tilcd,
890 .clear_fast = lcd_clear_fast_tilcd,
891 .backlight = lcd_backlight,
892};
893
894/* initialize the LCD driver */
895static void lcd_init(void)
896{
897 struct charlcd *charlcd;
898
899 charlcd = charlcd_alloc(0);
900 if (!charlcd)
901 return;
902
903 /*
904 * Init lcd struct with load-time values to preserve exact
905 * current functionality (at least for now).
906 */
907 charlcd->height = lcd_height;
908 charlcd->width = lcd_width;
909 charlcd->bwidth = lcd_bwidth;
910 charlcd->hwidth = lcd_hwidth;
911
912 switch (selected_lcd_type) {
913 case LCD_TYPE_OLD:
914 /* parallel mode, 8 bits */
915 lcd.proto = LCD_PROTO_PARALLEL;
916 lcd.charset = LCD_CHARSET_NORMAL;
917 lcd.pins.e = PIN_STROBE;
918 lcd.pins.rs = PIN_AUTOLF;
919
920 charlcd->width = 40;
921 charlcd->bwidth = 40;
922 charlcd->hwidth = 64;
923 charlcd->height = 2;
924 break;
925 case LCD_TYPE_KS0074:
926 /* serial mode, ks0074 */
927 lcd.proto = LCD_PROTO_SERIAL;
928 lcd.charset = LCD_CHARSET_KS0074;
929 lcd.pins.bl = PIN_AUTOLF;
930 lcd.pins.cl = PIN_STROBE;
931 lcd.pins.da = PIN_D0;
932
933 charlcd->width = 16;
934 charlcd->bwidth = 40;
935 charlcd->hwidth = 16;
936 charlcd->height = 2;
937 break;
938 case LCD_TYPE_NEXCOM:
939 /* parallel mode, 8 bits, generic */
940 lcd.proto = LCD_PROTO_PARALLEL;
941 lcd.charset = LCD_CHARSET_NORMAL;
942 lcd.pins.e = PIN_AUTOLF;
943 lcd.pins.rs = PIN_SELECP;
944 lcd.pins.rw = PIN_INITP;
945
946 charlcd->width = 16;
947 charlcd->bwidth = 40;
948 charlcd->hwidth = 64;
949 charlcd->height = 2;
950 break;
951 case LCD_TYPE_CUSTOM:
952 /* customer-defined */
953 lcd.proto = DEFAULT_LCD_PROTO;
954 lcd.charset = DEFAULT_LCD_CHARSET;
955 /* default geometry will be set later */
956 break;
957 case LCD_TYPE_HANTRONIX:
958 /* parallel mode, 8 bits, hantronix-like */
959 default:
960 lcd.proto = LCD_PROTO_PARALLEL;
961 lcd.charset = LCD_CHARSET_NORMAL;
962 lcd.pins.e = PIN_STROBE;
963 lcd.pins.rs = PIN_SELECP;
964
965 charlcd->width = 16;
966 charlcd->bwidth = 40;
967 charlcd->hwidth = 64;
968 charlcd->height = 2;
969 break;
970 }
971
972 /* Overwrite with module params set on loading */
973 if (lcd_height != NOT_SET)
974 charlcd->height = lcd_height;
975 if (lcd_width != NOT_SET)
976 charlcd->width = lcd_width;
977 if (lcd_bwidth != NOT_SET)
978 charlcd->bwidth = lcd_bwidth;
979 if (lcd_hwidth != NOT_SET)
980 charlcd->hwidth = lcd_hwidth;
981 if (lcd_charset != NOT_SET)
982 lcd.charset = lcd_charset;
983 if (lcd_proto != NOT_SET)
984 lcd.proto = lcd_proto;
985 if (lcd_e_pin != PIN_NOT_SET)
986 lcd.pins.e = lcd_e_pin;
987 if (lcd_rs_pin != PIN_NOT_SET)
988 lcd.pins.rs = lcd_rs_pin;
989 if (lcd_rw_pin != PIN_NOT_SET)
990 lcd.pins.rw = lcd_rw_pin;
991 if (lcd_cl_pin != PIN_NOT_SET)
992 lcd.pins.cl = lcd_cl_pin;
993 if (lcd_da_pin != PIN_NOT_SET)
994 lcd.pins.da = lcd_da_pin;
995 if (lcd_bl_pin != PIN_NOT_SET)
996 lcd.pins.bl = lcd_bl_pin;
997
998 /* this is used to catch wrong and default values */
999 if (charlcd->width <= 0)
1000 charlcd->width = DEFAULT_LCD_WIDTH;
1001 if (charlcd->bwidth <= 0)
1002 charlcd->bwidth = DEFAULT_LCD_BWIDTH;
1003 if (charlcd->hwidth <= 0)
1004 charlcd->hwidth = DEFAULT_LCD_HWIDTH;
1005 if (charlcd->height <= 0)
1006 charlcd->height = DEFAULT_LCD_HEIGHT;
1007
1008 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
1009 charlcd->ops = &charlcd_serial_ops;
1010
1011 if (lcd.pins.cl == PIN_NOT_SET)
1012 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1013 if (lcd.pins.da == PIN_NOT_SET)
1014 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
1015
1016 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
1017 charlcd->ops = &charlcd_parallel_ops;
1018
1019 if (lcd.pins.e == PIN_NOT_SET)
1020 lcd.pins.e = DEFAULT_LCD_PIN_E;
1021 if (lcd.pins.rs == PIN_NOT_SET)
1022 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1023 if (lcd.pins.rw == PIN_NOT_SET)
1024 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
1025 } else {
1026 charlcd->ops = &charlcd_tilcd_ops;
1027 }
1028
1029 if (lcd.pins.bl == PIN_NOT_SET)
1030 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
1031
1032 if (lcd.pins.e == PIN_NOT_SET)
1033 lcd.pins.e = PIN_NONE;
1034 if (lcd.pins.rs == PIN_NOT_SET)
1035 lcd.pins.rs = PIN_NONE;
1036 if (lcd.pins.rw == PIN_NOT_SET)
1037 lcd.pins.rw = PIN_NONE;
1038 if (lcd.pins.bl == PIN_NOT_SET)
1039 lcd.pins.bl = PIN_NONE;
1040 if (lcd.pins.cl == PIN_NOT_SET)
1041 lcd.pins.cl = PIN_NONE;
1042 if (lcd.pins.da == PIN_NOT_SET)
1043 lcd.pins.da = PIN_NONE;
1044
1045 if (lcd.charset == NOT_SET)
1046 lcd.charset = DEFAULT_LCD_CHARSET;
1047
1048 if (lcd.charset == LCD_CHARSET_KS0074)
1049 charlcd->char_conv = lcd_char_conv_ks0074;
1050 else
1051 charlcd->char_conv = NULL;
1052
1053 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1054 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1055 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1056 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1057 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1058 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1059 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1060 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1061 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1062 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1063 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1064 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
1065
1066 lcd.charlcd = charlcd;
1067 lcd.initialized = true;
1068}
1069
1070/*
1071 * These are the file operation function for user access to /dev/keypad
1072 */
1073
1074static ssize_t keypad_read(struct file *file,
1075 char __user *buf, size_t count, loff_t *ppos)
1076{
1077 unsigned i = *ppos;
1078 char __user *tmp = buf;
1079
1080 if (keypad_buflen == 0) {
1081 if (file->f_flags & O_NONBLOCK)
1082 return -EAGAIN;
1083
1084 if (wait_event_interruptible(keypad_read_wait,
1085 keypad_buflen != 0))
1086 return -EINTR;
1087 }
1088
1089 for (; count-- > 0 && (keypad_buflen > 0);
1090 ++i, ++tmp, --keypad_buflen) {
1091 put_user(keypad_buffer[keypad_start], tmp);
1092 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1093 }
1094 *ppos = i;
1095
1096 return tmp - buf;
1097}
1098
1099static int keypad_open(struct inode *inode, struct file *file)
1100{
1101 int ret;
1102
1103 ret = -EBUSY;
1104 if (!atomic_dec_and_test(&keypad_available))
1105 goto fail; /* open only once at a time */
1106
1107 ret = -EPERM;
1108 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1109 goto fail;
1110
1111 keypad_buflen = 0; /* flush the buffer on opening */
1112 return 0;
1113 fail:
1114 atomic_inc(&keypad_available);
1115 return ret;
1116}
1117
1118static int keypad_release(struct inode *inode, struct file *file)
1119{
1120 atomic_inc(&keypad_available);
1121 return 0;
1122}
1123
1124static const struct file_operations keypad_fops = {
1125 .read = keypad_read, /* read */
1126 .open = keypad_open, /* open */
1127 .release = keypad_release, /* close */
1128 .llseek = default_llseek,
1129};
1130
1131static struct miscdevice keypad_dev = {
1132 .minor = KEYPAD_MINOR,
1133 .name = "keypad",
1134 .fops = &keypad_fops,
1135};
1136
1137static void keypad_send_key(const char *string, int max_len)
1138{
1139 /* send the key to the device only if a process is attached to it. */
1140 if (!atomic_read(&keypad_available)) {
1141 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1142 keypad_buffer[(keypad_start + keypad_buflen++) %
1143 KEYPAD_BUFFER] = *string++;
1144 }
1145 wake_up_interruptible(&keypad_read_wait);
1146 }
1147}
1148
1149/* this function scans all the bits involving at least one logical signal,
1150 * and puts the results in the bitfield "phys_read" (one bit per established
1151 * contact), and sets "phys_read_prev" to "phys_read".
1152 *
1153 * Note: to debounce input signals, we will only consider as switched a signal
1154 * which is stable across 2 measures. Signals which are different between two
1155 * reads will be kept as they previously were in their logical form (phys_prev).
1156 * A signal which has just switched will have a 1 in
1157 * (phys_read ^ phys_read_prev).
1158 */
1159static void phys_scan_contacts(void)
1160{
1161 int bit, bitval;
1162 char oldval;
1163 char bitmask;
1164 char gndmask;
1165
1166 phys_prev = phys_curr;
1167 phys_read_prev = phys_read;
1168 phys_read = 0; /* flush all signals */
1169
1170 /* keep track of old value, with all outputs disabled */
1171 oldval = r_dtr(pprt) | scan_mask_o;
1172 /* activate all keyboard outputs (active low) */
1173 w_dtr(pprt, oldval & ~scan_mask_o);
1174
1175 /* will have a 1 for each bit set to gnd */
1176 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1177 /* disable all matrix signals */
1178 w_dtr(pprt, oldval);
1179
1180 /* now that all outputs are cleared, the only active input bits are
1181 * directly connected to the ground
1182 */
1183
1184 /* 1 for each grounded input */
1185 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1186
1187 /* grounded inputs are signals 40-44 */
1188 phys_read |= (__u64)gndmask << 40;
1189
1190 if (bitmask != gndmask) {
1191 /*
1192 * since clearing the outputs changed some inputs, we know
1193 * that some input signals are currently tied to some outputs.
1194 * So we'll scan them.
1195 */
1196 for (bit = 0; bit < 8; bit++) {
1197 bitval = BIT(bit);
1198
1199 if (!(scan_mask_o & bitval)) /* skip unused bits */
1200 continue;
1201
1202 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1203 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1204 phys_read |= (__u64)bitmask << (5 * bit);
1205 }
1206 w_dtr(pprt, oldval); /* disable all outputs */
1207 }
1208 /*
1209 * this is easy: use old bits when they are flapping,
1210 * use new ones when stable
1211 */
1212 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1213 (phys_read & ~(phys_read ^ phys_read_prev));
1214}
1215
1216static inline int input_state_high(struct logical_input *input)
1217{
1218#if 0
1219 /* FIXME:
1220 * this is an invalid test. It tries to catch
1221 * transitions from single-key to multiple-key, but
1222 * doesn't take into account the contacts polarity.
1223 * The only solution to the problem is to parse keys
1224 * from the most complex to the simplest combinations,
1225 * and mark them as 'caught' once a combination
1226 * matches, then unmatch it for all other ones.
1227 */
1228
1229 /* try to catch dangerous transitions cases :
1230 * someone adds a bit, so this signal was a false
1231 * positive resulting from a transition. We should
1232 * invalidate the signal immediately and not call the
1233 * release function.
1234 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1235 */
1236 if (((phys_prev & input->mask) == input->value) &&
1237 ((phys_curr & input->mask) > input->value)) {
1238 input->state = INPUT_ST_LOW; /* invalidate */
1239 return 1;
1240 }
1241#endif
1242
1243 if ((phys_curr & input->mask) == input->value) {
1244 if ((input->type == INPUT_TYPE_STD) &&
1245 (input->high_timer == 0)) {
1246 input->high_timer++;
1247 if (input->u.std.press_fct)
1248 input->u.std.press_fct(input->u.std.press_data);
1249 } else if (input->type == INPUT_TYPE_KBD) {
1250 /* will turn on the light */
1251 keypressed = 1;
1252
1253 if (input->high_timer == 0) {
1254 char *press_str = input->u.kbd.press_str;
1255
1256 if (press_str[0]) {
1257 int s = sizeof(input->u.kbd.press_str);
1258
1259 keypad_send_key(press_str, s);
1260 }
1261 }
1262
1263 if (input->u.kbd.repeat_str[0]) {
1264 char *repeat_str = input->u.kbd.repeat_str;
1265
1266 if (input->high_timer >= KEYPAD_REP_START) {
1267 int s = sizeof(input->u.kbd.repeat_str);
1268
1269 input->high_timer -= KEYPAD_REP_DELAY;
1270 keypad_send_key(repeat_str, s);
1271 }
1272 /* we will need to come back here soon */
1273 inputs_stable = 0;
1274 }
1275
1276 if (input->high_timer < 255)
1277 input->high_timer++;
1278 }
1279 return 1;
1280 }
1281
1282 /* else signal falling down. Let's fall through. */
1283 input->state = INPUT_ST_FALLING;
1284 input->fall_timer = 0;
1285
1286 return 0;
1287}
1288
1289static inline void input_state_falling(struct logical_input *input)
1290{
1291#if 0
1292 /* FIXME !!! same comment as in input_state_high */
1293 if (((phys_prev & input->mask) == input->value) &&
1294 ((phys_curr & input->mask) > input->value)) {
1295 input->state = INPUT_ST_LOW; /* invalidate */
1296 return;
1297 }
1298#endif
1299
1300 if ((phys_curr & input->mask) == input->value) {
1301 if (input->type == INPUT_TYPE_KBD) {
1302 /* will turn on the light */
1303 keypressed = 1;
1304
1305 if (input->u.kbd.repeat_str[0]) {
1306 char *repeat_str = input->u.kbd.repeat_str;
1307
1308 if (input->high_timer >= KEYPAD_REP_START) {
1309 int s = sizeof(input->u.kbd.repeat_str);
1310
1311 input->high_timer -= KEYPAD_REP_DELAY;
1312 keypad_send_key(repeat_str, s);
1313 }
1314 /* we will need to come back here soon */
1315 inputs_stable = 0;
1316 }
1317
1318 if (input->high_timer < 255)
1319 input->high_timer++;
1320 }
1321 input->state = INPUT_ST_HIGH;
1322 } else if (input->fall_timer >= input->fall_time) {
1323 /* call release event */
1324 if (input->type == INPUT_TYPE_STD) {
1325 void (*release_fct)(int) = input->u.std.release_fct;
1326
1327 if (release_fct)
1328 release_fct(input->u.std.release_data);
1329 } else if (input->type == INPUT_TYPE_KBD) {
1330 char *release_str = input->u.kbd.release_str;
1331
1332 if (release_str[0]) {
1333 int s = sizeof(input->u.kbd.release_str);
1334
1335 keypad_send_key(release_str, s);
1336 }
1337 }
1338
1339 input->state = INPUT_ST_LOW;
1340 } else {
1341 input->fall_timer++;
1342 inputs_stable = 0;
1343 }
1344}
1345
1346static void panel_process_inputs(void)
1347{
1348 struct logical_input *input;
1349
1350 keypressed = 0;
1351 inputs_stable = 1;
1352 list_for_each_entry(input, &logical_inputs, list) {
1353 switch (input->state) {
1354 case INPUT_ST_LOW:
1355 if ((phys_curr & input->mask) != input->value)
1356 break;
1357 /* if all needed ones were already set previously,
1358 * this means that this logical signal has been
1359 * activated by the releasing of another combined
1360 * signal, so we don't want to match.
1361 * eg: AB -(release B)-> A -(release A)-> 0 :
1362 * don't match A.
1363 */
1364 if ((phys_prev & input->mask) == input->value)
1365 break;
1366 input->rise_timer = 0;
1367 input->state = INPUT_ST_RISING;
1368 fallthrough;
1369 case INPUT_ST_RISING:
1370 if ((phys_curr & input->mask) != input->value) {
1371 input->state = INPUT_ST_LOW;
1372 break;
1373 }
1374 if (input->rise_timer < input->rise_time) {
1375 inputs_stable = 0;
1376 input->rise_timer++;
1377 break;
1378 }
1379 input->high_timer = 0;
1380 input->state = INPUT_ST_HIGH;
1381 fallthrough;
1382 case INPUT_ST_HIGH:
1383 if (input_state_high(input))
1384 break;
1385 fallthrough;
1386 case INPUT_ST_FALLING:
1387 input_state_falling(input);
1388 }
1389 }
1390}
1391
1392static void panel_scan_timer(struct timer_list *unused)
1393{
1394 if (keypad.enabled && keypad_initialized) {
1395 if (spin_trylock_irq(&pprt_lock)) {
1396 phys_scan_contacts();
1397
1398 /* no need for the parport anymore */
1399 spin_unlock_irq(&pprt_lock);
1400 }
1401
1402 if (!inputs_stable || phys_curr != phys_prev)
1403 panel_process_inputs();
1404 }
1405
1406 if (keypressed && lcd.enabled && lcd.initialized)
1407 charlcd_poke(lcd.charlcd);
1408
1409 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
1410}
1411
1412static void init_scan_timer(void)
1413{
1414 if (scan_timer.function)
1415 return; /* already started */
1416
1417 timer_setup(&scan_timer, panel_scan_timer, 0);
1418 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1419 add_timer(&scan_timer);
1420}
1421
1422/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1423 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1424 * corresponding to out and in bits respectively.
1425 * returns 1 if ok, 0 if error (in which case, nothing is written).
1426 */
1427static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
1428 u8 *imask, u8 *omask)
1429{
1430 const char sigtab[] = "EeSsPpAaBb";
1431 u8 im, om;
1432 __u64 m, v;
1433
1434 om = 0;
1435 im = 0;
1436 m = 0ULL;
1437 v = 0ULL;
1438 while (*name) {
1439 int in, out, bit, neg;
1440 const char *idx;
1441
1442 idx = strchr(sigtab, *name);
1443 if (!idx)
1444 return 0; /* input name not found */
1445
1446 in = idx - sigtab;
1447 neg = (in & 1); /* odd (lower) names are negated */
1448 in >>= 1;
1449 im |= BIT(in);
1450
1451 name++;
1452 if (*name >= '0' && *name <= '7') {
1453 out = *name - '0';
1454 om |= BIT(out);
1455 } else if (*name == '-') {
1456 out = 8;
1457 } else {
1458 return 0; /* unknown bit name */
1459 }
1460
1461 bit = (out * 5) + in;
1462
1463 m |= 1ULL << bit;
1464 if (!neg)
1465 v |= 1ULL << bit;
1466 name++;
1467 }
1468 *mask = m;
1469 *value = v;
1470 if (imask)
1471 *imask |= im;
1472 if (omask)
1473 *omask |= om;
1474 return 1;
1475}
1476
1477/* tries to bind a key to the signal name <name>. The key will send the
1478 * strings <press>, <repeat>, <release> for these respective events.
1479 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1480 */
1481static struct logical_input *panel_bind_key(const char *name, const char *press,
1482 const char *repeat,
1483 const char *release)
1484{
1485 struct logical_input *key;
1486
1487 key = kzalloc(sizeof(*key), GFP_KERNEL);
1488 if (!key)
1489 return NULL;
1490
1491 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1492 &scan_mask_o)) {
1493 kfree(key);
1494 return NULL;
1495 }
1496
1497 key->type = INPUT_TYPE_KBD;
1498 key->state = INPUT_ST_LOW;
1499 key->rise_time = 1;
1500 key->fall_time = 1;
1501
1502 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
1503 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
1504 strncpy(key->u.kbd.release_str, release,
1505 sizeof(key->u.kbd.release_str));
1506 list_add(&key->list, &logical_inputs);
1507 return key;
1508}
1509
1510#if 0
1511/* tries to bind a callback function to the signal name <name>. The function
1512 * <press_fct> will be called with the <press_data> arg when the signal is
1513 * activated, and so on for <release_fct>/<release_data>
1514 * Returns the pointer to the new signal if ok, NULL if the signal could not
1515 * be bound.
1516 */
1517static struct logical_input *panel_bind_callback(char *name,
1518 void (*press_fct)(int),
1519 int press_data,
1520 void (*release_fct)(int),
1521 int release_data)
1522{
1523 struct logical_input *callback;
1524
1525 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
1526 if (!callback)
1527 return NULL;
1528
1529 memset(callback, 0, sizeof(struct logical_input));
1530 if (!input_name2mask(name, &callback->mask, &callback->value,
1531 &scan_mask_i, &scan_mask_o))
1532 return NULL;
1533
1534 callback->type = INPUT_TYPE_STD;
1535 callback->state = INPUT_ST_LOW;
1536 callback->rise_time = 1;
1537 callback->fall_time = 1;
1538 callback->u.std.press_fct = press_fct;
1539 callback->u.std.press_data = press_data;
1540 callback->u.std.release_fct = release_fct;
1541 callback->u.std.release_data = release_data;
1542 list_add(&callback->list, &logical_inputs);
1543 return callback;
1544}
1545#endif
1546
1547static void keypad_init(void)
1548{
1549 int keynum;
1550
1551 init_waitqueue_head(&keypad_read_wait);
1552 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
1553
1554 /* Let's create all known keys */
1555
1556 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
1557 panel_bind_key(keypad_profile[keynum][0],
1558 keypad_profile[keynum][1],
1559 keypad_profile[keynum][2],
1560 keypad_profile[keynum][3]);
1561 }
1562
1563 init_scan_timer();
1564 keypad_initialized = 1;
1565}
1566
1567/**************************************************/
1568/* device initialization */
1569/**************************************************/
1570
1571static void panel_attach(struct parport *port)
1572{
1573 struct pardev_cb panel_cb;
1574
1575 if (port->number != parport)
1576 return;
1577
1578 if (pprt) {
1579 pr_err("%s: port->number=%d parport=%d, already registered!\n",
1580 __func__, port->number, parport);
1581 return;
1582 }
1583
1584 memset(&panel_cb, 0, sizeof(panel_cb));
1585 panel_cb.private = &pprt;
1586 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
1587
1588 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
1589 if (!pprt) {
1590 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
1591 __func__, port->number, parport);
1592 return;
1593 }
1594
1595 if (parport_claim(pprt)) {
1596 pr_err("could not claim access to parport%d. Aborting.\n",
1597 parport);
1598 goto err_unreg_device;
1599 }
1600
1601 /* must init LCD first, just in case an IRQ from the keypad is
1602 * generated at keypad init
1603 */
1604 if (lcd.enabled) {
1605 lcd_init();
1606 if (!lcd.charlcd || charlcd_register(lcd.charlcd))
1607 goto err_unreg_device;
1608 }
1609
1610 if (keypad.enabled) {
1611 keypad_init();
1612 if (misc_register(&keypad_dev))
1613 goto err_lcd_unreg;
1614 }
1615 return;
1616
1617err_lcd_unreg:
1618 if (scan_timer.function)
1619 del_timer_sync(&scan_timer);
1620 if (lcd.enabled)
1621 charlcd_unregister(lcd.charlcd);
1622err_unreg_device:
1623 charlcd_free(lcd.charlcd);
1624 lcd.charlcd = NULL;
1625 parport_unregister_device(pprt);
1626 pprt = NULL;
1627}
1628
1629static void panel_detach(struct parport *port)
1630{
1631 if (port->number != parport)
1632 return;
1633
1634 if (!pprt) {
1635 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
1636 __func__, port->number, parport);
1637 return;
1638 }
1639 if (scan_timer.function)
1640 del_timer_sync(&scan_timer);
1641
1642 if (keypad.enabled) {
1643 misc_deregister(&keypad_dev);
1644 keypad_initialized = 0;
1645 }
1646
1647 if (lcd.enabled) {
1648 charlcd_unregister(lcd.charlcd);
1649 lcd.initialized = false;
1650 charlcd_free(lcd.charlcd);
1651 lcd.charlcd = NULL;
1652 }
1653
1654 /* TODO: free all input signals */
1655 parport_release(pprt);
1656 parport_unregister_device(pprt);
1657 pprt = NULL;
1658}
1659
1660static struct parport_driver panel_driver = {
1661 .name = "panel",
1662 .match_port = panel_attach,
1663 .detach = panel_detach,
1664 .devmodel = true,
1665};
1666
1667/* init function */
1668static int __init panel_init_module(void)
1669{
1670 int selected_keypad_type = NOT_SET, err;
1671
1672 /* take care of an eventual profile */
1673 switch (profile) {
1674 case PANEL_PROFILE_CUSTOM:
1675 /* custom profile */
1676 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
1677 selected_lcd_type = DEFAULT_LCD_TYPE;
1678 break;
1679 case PANEL_PROFILE_OLD:
1680 /* 8 bits, 2*16, old keypad */
1681 selected_keypad_type = KEYPAD_TYPE_OLD;
1682 selected_lcd_type = LCD_TYPE_OLD;
1683
1684 /* TODO: This two are a little hacky, sort it out later */
1685 if (lcd_width == NOT_SET)
1686 lcd_width = 16;
1687 if (lcd_hwidth == NOT_SET)
1688 lcd_hwidth = 16;
1689 break;
1690 case PANEL_PROFILE_NEW:
1691 /* serial, 2*16, new keypad */
1692 selected_keypad_type = KEYPAD_TYPE_NEW;
1693 selected_lcd_type = LCD_TYPE_KS0074;
1694 break;
1695 case PANEL_PROFILE_HANTRONIX:
1696 /* 8 bits, 2*16 hantronix-like, no keypad */
1697 selected_keypad_type = KEYPAD_TYPE_NONE;
1698 selected_lcd_type = LCD_TYPE_HANTRONIX;
1699 break;
1700 case PANEL_PROFILE_NEXCOM:
1701 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
1702 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
1703 selected_lcd_type = LCD_TYPE_NEXCOM;
1704 break;
1705 case PANEL_PROFILE_LARGE:
1706 /* 8 bits, 2*40, old keypad */
1707 selected_keypad_type = KEYPAD_TYPE_OLD;
1708 selected_lcd_type = LCD_TYPE_OLD;
1709 break;
1710 }
1711
1712 /*
1713 * Overwrite selection with module param values (both keypad and lcd),
1714 * where the deprecated params have lower prio.
1715 */
1716 if (keypad_enabled != NOT_SET)
1717 selected_keypad_type = keypad_enabled;
1718 if (keypad_type != NOT_SET)
1719 selected_keypad_type = keypad_type;
1720
1721 keypad.enabled = (selected_keypad_type > 0);
1722
1723 if (lcd_enabled != NOT_SET)
1724 selected_lcd_type = lcd_enabled;
1725 if (lcd_type != NOT_SET)
1726 selected_lcd_type = lcd_type;
1727
1728 lcd.enabled = (selected_lcd_type > 0);
1729
1730 if (lcd.enabled) {
1731 /*
1732 * Init lcd struct with load-time values to preserve exact
1733 * current functionality (at least for now).
1734 */
1735 lcd.charset = lcd_charset;
1736 lcd.proto = lcd_proto;
1737 lcd.pins.e = lcd_e_pin;
1738 lcd.pins.rs = lcd_rs_pin;
1739 lcd.pins.rw = lcd_rw_pin;
1740 lcd.pins.cl = lcd_cl_pin;
1741 lcd.pins.da = lcd_da_pin;
1742 lcd.pins.bl = lcd_bl_pin;
1743 }
1744
1745 switch (selected_keypad_type) {
1746 case KEYPAD_TYPE_OLD:
1747 keypad_profile = old_keypad_profile;
1748 break;
1749 case KEYPAD_TYPE_NEW:
1750 keypad_profile = new_keypad_profile;
1751 break;
1752 case KEYPAD_TYPE_NEXCOM:
1753 keypad_profile = nexcom_keypad_profile;
1754 break;
1755 default:
1756 keypad_profile = NULL;
1757 break;
1758 }
1759
1760 if (!lcd.enabled && !keypad.enabled) {
1761 /* no device enabled, let's exit */
1762 pr_err("panel driver disabled.\n");
1763 return -ENODEV;
1764 }
1765
1766 err = parport_register_driver(&panel_driver);
1767 if (err) {
1768 pr_err("could not register with parport. Aborting.\n");
1769 return err;
1770 }
1771
1772 if (pprt)
1773 pr_info("panel driver registered on parport%d (io=0x%lx).\n",
1774 parport, pprt->port->base);
1775 else
1776 pr_info("panel driver not yet registered\n");
1777 return 0;
1778}
1779
1780static void __exit panel_cleanup_module(void)
1781{
1782 parport_unregister_driver(&panel_driver);
1783}
1784
1785module_init(panel_init_module);
1786module_exit(panel_cleanup_module);
1787MODULE_AUTHOR("Willy Tarreau");
1788MODULE_LICENSE("GPL");
1789
1790/*
1791 * Local variables:
1792 * c-indent-level: 4
1793 * tab-width: 8
1794 * End:
1795 */
1/*
2 * Front panel driver for Linux
3 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
4 * Copyright (C) 2016-2017 Glider bvba
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
12 * connected to a parallel printer port.
13 *
14 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
15 * serial module compatible with Samsung's KS0074. The pins may be connected in
16 * any combination, everything is programmable.
17 *
18 * The keypad consists in a matrix of push buttons connecting input pins to
19 * data output pins or to the ground. The combinations have to be hard-coded
20 * in the driver, though several profiles exist and adding new ones is easy.
21 *
22 * Several profiles are provided for commonly found LCD+keypad modules on the
23 * market, such as those found in Nexcom's appliances.
24 *
25 * FIXME:
26 * - the initialization/deinitialization process is very dirty and should
27 * be rewritten. It may even be buggy.
28 *
29 * TODO:
30 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
31 * - make the LCD a part of a virtual screen of Vx*Vy
32 * - make the inputs list smp-safe
33 * - change the keyboard to a double mapping : signals -> key_id -> values
34 * so that applications can change values without knowing signals
35 *
36 */
37
38#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
40#include <linux/module.h>
41
42#include <linux/types.h>
43#include <linux/errno.h>
44#include <linux/signal.h>
45#include <linux/sched.h>
46#include <linux/spinlock.h>
47#include <linux/interrupt.h>
48#include <linux/miscdevice.h>
49#include <linux/slab.h>
50#include <linux/ioport.h>
51#include <linux/fcntl.h>
52#include <linux/init.h>
53#include <linux/delay.h>
54#include <linux/kernel.h>
55#include <linux/ctype.h>
56#include <linux/parport.h>
57#include <linux/list.h>
58
59#include <linux/io.h>
60#include <linux/uaccess.h>
61
62#include <misc/charlcd.h>
63
64#define KEYPAD_MINOR 185
65
66#define LCD_MAXBYTES 256 /* max burst write */
67
68#define KEYPAD_BUFFER 64
69
70/* poll the keyboard this every second */
71#define INPUT_POLL_TIME (HZ / 50)
72/* a key starts to repeat after this times INPUT_POLL_TIME */
73#define KEYPAD_REP_START (10)
74/* a key repeats this times INPUT_POLL_TIME */
75#define KEYPAD_REP_DELAY (2)
76
77/* converts an r_str() input to an active high, bits string : 000BAOSE */
78#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
79
80#define PNL_PBUSY 0x80 /* inverted input, active low */
81#define PNL_PACK 0x40 /* direct input, active low */
82#define PNL_POUTPA 0x20 /* direct input, active high */
83#define PNL_PSELECD 0x10 /* direct input, active high */
84#define PNL_PERRORP 0x08 /* direct input, active low */
85
86#define PNL_PBIDIR 0x20 /* bi-directional ports */
87/* high to read data in or-ed with data out */
88#define PNL_PINTEN 0x10
89#define PNL_PSELECP 0x08 /* inverted output, active low */
90#define PNL_PINITP 0x04 /* direct output, active low */
91#define PNL_PAUTOLF 0x02 /* inverted output, active low */
92#define PNL_PSTROBE 0x01 /* inverted output */
93
94#define PNL_PD0 0x01
95#define PNL_PD1 0x02
96#define PNL_PD2 0x04
97#define PNL_PD3 0x08
98#define PNL_PD4 0x10
99#define PNL_PD5 0x20
100#define PNL_PD6 0x40
101#define PNL_PD7 0x80
102
103#define PIN_NONE 0
104#define PIN_STROBE 1
105#define PIN_D0 2
106#define PIN_D1 3
107#define PIN_D2 4
108#define PIN_D3 5
109#define PIN_D4 6
110#define PIN_D5 7
111#define PIN_D6 8
112#define PIN_D7 9
113#define PIN_AUTOLF 14
114#define PIN_INITP 16
115#define PIN_SELECP 17
116#define PIN_NOT_SET 127
117
118#define NOT_SET -1
119
120/* macros to simplify use of the parallel port */
121#define r_ctr(x) (parport_read_control((x)->port))
122#define r_dtr(x) (parport_read_data((x)->port))
123#define r_str(x) (parport_read_status((x)->port))
124#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
125#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
126
127/* this defines which bits are to be used and which ones to be ignored */
128/* logical or of the output bits involved in the scan matrix */
129static __u8 scan_mask_o;
130/* logical or of the input bits involved in the scan matrix */
131static __u8 scan_mask_i;
132
133enum input_type {
134 INPUT_TYPE_STD,
135 INPUT_TYPE_KBD,
136};
137
138enum input_state {
139 INPUT_ST_LOW,
140 INPUT_ST_RISING,
141 INPUT_ST_HIGH,
142 INPUT_ST_FALLING,
143};
144
145struct logical_input {
146 struct list_head list;
147 __u64 mask;
148 __u64 value;
149 enum input_type type;
150 enum input_state state;
151 __u8 rise_time, fall_time;
152 __u8 rise_timer, fall_timer, high_timer;
153
154 union {
155 struct { /* valid when type == INPUT_TYPE_STD */
156 void (*press_fct)(int);
157 void (*release_fct)(int);
158 int press_data;
159 int release_data;
160 } std;
161 struct { /* valid when type == INPUT_TYPE_KBD */
162 /* strings can be non null-terminated */
163 char press_str[sizeof(void *) + sizeof(int)];
164 char repeat_str[sizeof(void *) + sizeof(int)];
165 char release_str[sizeof(void *) + sizeof(int)];
166 } kbd;
167 } u;
168};
169
170static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
171
172/* physical contacts history
173 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
174 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
175 * corresponds to the ground.
176 * Within each group, bits are stored in the same order as read on the port :
177 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
178 * So, each __u64 is represented like this :
179 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
180 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
181 */
182
183/* what has just been read from the I/O ports */
184static __u64 phys_read;
185/* previous phys_read */
186static __u64 phys_read_prev;
187/* stabilized phys_read (phys_read|phys_read_prev) */
188static __u64 phys_curr;
189/* previous phys_curr */
190static __u64 phys_prev;
191/* 0 means that at least one logical signal needs be computed */
192static char inputs_stable;
193
194/* these variables are specific to the keypad */
195static struct {
196 bool enabled;
197} keypad;
198
199static char keypad_buffer[KEYPAD_BUFFER];
200static int keypad_buflen;
201static int keypad_start;
202static char keypressed;
203static wait_queue_head_t keypad_read_wait;
204
205/* lcd-specific variables */
206static struct {
207 bool enabled;
208 bool initialized;
209
210 int charset;
211 int proto;
212
213 /* TODO: use union here? */
214 struct {
215 int e;
216 int rs;
217 int rw;
218 int cl;
219 int da;
220 int bl;
221 } pins;
222
223 struct charlcd *charlcd;
224} lcd;
225
226/* Needed only for init */
227static int selected_lcd_type = NOT_SET;
228
229/*
230 * Bit masks to convert LCD signals to parallel port outputs.
231 * _d_ are values for data port, _c_ are for control port.
232 * [0] = signal OFF, [1] = signal ON, [2] = mask
233 */
234#define BIT_CLR 0
235#define BIT_SET 1
236#define BIT_MSK 2
237#define BIT_STATES 3
238/*
239 * one entry for each bit on the LCD
240 */
241#define LCD_BIT_E 0
242#define LCD_BIT_RS 1
243#define LCD_BIT_RW 2
244#define LCD_BIT_BL 3
245#define LCD_BIT_CL 4
246#define LCD_BIT_DA 5
247#define LCD_BITS 6
248
249/*
250 * each bit can be either connected to a DATA or CTRL port
251 */
252#define LCD_PORT_C 0
253#define LCD_PORT_D 1
254#define LCD_PORTS 2
255
256static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
257
258/*
259 * LCD protocols
260 */
261#define LCD_PROTO_PARALLEL 0
262#define LCD_PROTO_SERIAL 1
263#define LCD_PROTO_TI_DA8XX_LCD 2
264
265/*
266 * LCD character sets
267 */
268#define LCD_CHARSET_NORMAL 0
269#define LCD_CHARSET_KS0074 1
270
271/*
272 * LCD types
273 */
274#define LCD_TYPE_NONE 0
275#define LCD_TYPE_CUSTOM 1
276#define LCD_TYPE_OLD 2
277#define LCD_TYPE_KS0074 3
278#define LCD_TYPE_HANTRONIX 4
279#define LCD_TYPE_NEXCOM 5
280
281/*
282 * keypad types
283 */
284#define KEYPAD_TYPE_NONE 0
285#define KEYPAD_TYPE_OLD 1
286#define KEYPAD_TYPE_NEW 2
287#define KEYPAD_TYPE_NEXCOM 3
288
289/*
290 * panel profiles
291 */
292#define PANEL_PROFILE_CUSTOM 0
293#define PANEL_PROFILE_OLD 1
294#define PANEL_PROFILE_NEW 2
295#define PANEL_PROFILE_HANTRONIX 3
296#define PANEL_PROFILE_NEXCOM 4
297#define PANEL_PROFILE_LARGE 5
298
299/*
300 * Construct custom config from the kernel's configuration
301 */
302#define DEFAULT_PARPORT 0
303#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
304#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
305#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
306#define DEFAULT_LCD_HEIGHT 2
307#define DEFAULT_LCD_WIDTH 40
308#define DEFAULT_LCD_BWIDTH 40
309#define DEFAULT_LCD_HWIDTH 64
310#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
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
320#ifdef CONFIG_PANEL_PARPORT
321#undef DEFAULT_PARPORT
322#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
323#endif
324
325#ifdef CONFIG_PANEL_PROFILE
326#undef DEFAULT_PROFILE
327#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
328#endif
329
330#if DEFAULT_PROFILE == 0 /* custom */
331#ifdef CONFIG_PANEL_KEYPAD
332#undef DEFAULT_KEYPAD_TYPE
333#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
334#endif
335
336#ifdef CONFIG_PANEL_LCD
337#undef DEFAULT_LCD_TYPE
338#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
339#endif
340
341#ifdef CONFIG_PANEL_LCD_HEIGHT
342#undef DEFAULT_LCD_HEIGHT
343#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
344#endif
345
346#ifdef CONFIG_PANEL_LCD_WIDTH
347#undef DEFAULT_LCD_WIDTH
348#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
349#endif
350
351#ifdef CONFIG_PANEL_LCD_BWIDTH
352#undef DEFAULT_LCD_BWIDTH
353#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
354#endif
355
356#ifdef CONFIG_PANEL_LCD_HWIDTH
357#undef DEFAULT_LCD_HWIDTH
358#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
359#endif
360
361#ifdef CONFIG_PANEL_LCD_CHARSET
362#undef DEFAULT_LCD_CHARSET
363#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
364#endif
365
366#ifdef CONFIG_PANEL_LCD_PROTO
367#undef DEFAULT_LCD_PROTO
368#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
369#endif
370
371#ifdef CONFIG_PANEL_LCD_PIN_E
372#undef DEFAULT_LCD_PIN_E
373#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
374#endif
375
376#ifdef CONFIG_PANEL_LCD_PIN_RS
377#undef DEFAULT_LCD_PIN_RS
378#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
379#endif
380
381#ifdef CONFIG_PANEL_LCD_PIN_RW
382#undef DEFAULT_LCD_PIN_RW
383#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
384#endif
385
386#ifdef CONFIG_PANEL_LCD_PIN_SCL
387#undef DEFAULT_LCD_PIN_SCL
388#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
389#endif
390
391#ifdef CONFIG_PANEL_LCD_PIN_SDA
392#undef DEFAULT_LCD_PIN_SDA
393#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
394#endif
395
396#ifdef CONFIG_PANEL_LCD_PIN_BL
397#undef DEFAULT_LCD_PIN_BL
398#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
399#endif
400
401#endif /* DEFAULT_PROFILE == 0 */
402
403/* global variables */
404
405/* Device single-open policy control */
406static atomic_t keypad_available = ATOMIC_INIT(1);
407
408static struct pardevice *pprt;
409
410static int keypad_initialized;
411
412static DEFINE_SPINLOCK(pprt_lock);
413static struct timer_list scan_timer;
414
415MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
416
417static int parport = DEFAULT_PARPORT;
418module_param(parport, int, 0000);
419MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
420
421static int profile = DEFAULT_PROFILE;
422module_param(profile, int, 0000);
423MODULE_PARM_DESC(profile,
424 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
425 "4=16x2 nexcom; default=40x2, old kp");
426
427static int keypad_type = NOT_SET;
428module_param(keypad_type, int, 0000);
429MODULE_PARM_DESC(keypad_type,
430 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
431
432static int lcd_type = NOT_SET;
433module_param(lcd_type, int, 0000);
434MODULE_PARM_DESC(lcd_type,
435 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
436
437static int lcd_height = NOT_SET;
438module_param(lcd_height, int, 0000);
439MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
440
441static int lcd_width = NOT_SET;
442module_param(lcd_width, int, 0000);
443MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
444
445static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
446module_param(lcd_bwidth, int, 0000);
447MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
448
449static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
450module_param(lcd_hwidth, int, 0000);
451MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
452
453static int lcd_charset = NOT_SET;
454module_param(lcd_charset, int, 0000);
455MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
456
457static int lcd_proto = NOT_SET;
458module_param(lcd_proto, int, 0000);
459MODULE_PARM_DESC(lcd_proto,
460 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
461
462/*
463 * These are the parallel port pins the LCD control signals are connected to.
464 * Set this to 0 if the signal is not used. Set it to its opposite value
465 * (negative) if the signal is negated. -MAXINT is used to indicate that the
466 * pin has not been explicitly specified.
467 *
468 * WARNING! no check will be performed about collisions with keypad !
469 */
470
471static int lcd_e_pin = PIN_NOT_SET;
472module_param(lcd_e_pin, int, 0000);
473MODULE_PARM_DESC(lcd_e_pin,
474 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
475
476static int lcd_rs_pin = PIN_NOT_SET;
477module_param(lcd_rs_pin, int, 0000);
478MODULE_PARM_DESC(lcd_rs_pin,
479 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
480
481static int lcd_rw_pin = PIN_NOT_SET;
482module_param(lcd_rw_pin, int, 0000);
483MODULE_PARM_DESC(lcd_rw_pin,
484 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
485
486static int lcd_cl_pin = PIN_NOT_SET;
487module_param(lcd_cl_pin, int, 0000);
488MODULE_PARM_DESC(lcd_cl_pin,
489 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
490
491static int lcd_da_pin = PIN_NOT_SET;
492module_param(lcd_da_pin, int, 0000);
493MODULE_PARM_DESC(lcd_da_pin,
494 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
495
496static int lcd_bl_pin = PIN_NOT_SET;
497module_param(lcd_bl_pin, int, 0000);
498MODULE_PARM_DESC(lcd_bl_pin,
499 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
500
501/* Deprecated module parameters - consider not using them anymore */
502
503static int lcd_enabled = NOT_SET;
504module_param(lcd_enabled, int, 0000);
505MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
506
507static int keypad_enabled = NOT_SET;
508module_param(keypad_enabled, int, 0000);
509MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
510
511/* for some LCD drivers (ks0074) we need a charset conversion table. */
512static const unsigned char lcd_char_conv_ks0074[256] = {
513 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
514 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
515 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
516 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
517 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
518 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
519 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
520 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
521 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
522 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
523 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
524 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
525 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
526 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
527 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
528 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
529 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
530 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
531 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
532 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
533 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
534 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
535 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
536 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
537 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
538 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
539 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
540 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
541 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
542 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
543 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
544 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
545 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
546};
547
548static const char old_keypad_profile[][4][9] = {
549 {"S0", "Left\n", "Left\n", ""},
550 {"S1", "Down\n", "Down\n", ""},
551 {"S2", "Up\n", "Up\n", ""},
552 {"S3", "Right\n", "Right\n", ""},
553 {"S4", "Esc\n", "Esc\n", ""},
554 {"S5", "Ret\n", "Ret\n", ""},
555 {"", "", "", ""}
556};
557
558/* signals, press, repeat, release */
559static const char new_keypad_profile[][4][9] = {
560 {"S0", "Left\n", "Left\n", ""},
561 {"S1", "Down\n", "Down\n", ""},
562 {"S2", "Up\n", "Up\n", ""},
563 {"S3", "Right\n", "Right\n", ""},
564 {"S4s5", "", "Esc\n", "Esc\n"},
565 {"s4S5", "", "Ret\n", "Ret\n"},
566 {"S4S5", "Help\n", "", ""},
567 /* add new signals above this line */
568 {"", "", "", ""}
569};
570
571/* signals, press, repeat, release */
572static const char nexcom_keypad_profile[][4][9] = {
573 {"a-p-e-", "Down\n", "Down\n", ""},
574 {"a-p-E-", "Ret\n", "Ret\n", ""},
575 {"a-P-E-", "Esc\n", "Esc\n", ""},
576 {"a-P-e-", "Up\n", "Up\n", ""},
577 /* add new signals above this line */
578 {"", "", "", ""}
579};
580
581static const char (*keypad_profile)[4][9] = old_keypad_profile;
582
583static DECLARE_BITMAP(bits, LCD_BITS);
584
585static void lcd_get_bits(unsigned int port, int *val)
586{
587 unsigned int bit, state;
588
589 for (bit = 0; bit < LCD_BITS; bit++) {
590 state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
591 *val &= lcd_bits[port][bit][BIT_MSK];
592 *val |= lcd_bits[port][bit][state];
593 }
594}
595
596/* sets data port bits according to current signals values */
597static int set_data_bits(void)
598{
599 int val;
600
601 val = r_dtr(pprt);
602 lcd_get_bits(LCD_PORT_D, &val);
603 w_dtr(pprt, val);
604 return val;
605}
606
607/* sets ctrl port bits according to current signals values */
608static int set_ctrl_bits(void)
609{
610 int val;
611
612 val = r_ctr(pprt);
613 lcd_get_bits(LCD_PORT_C, &val);
614 w_ctr(pprt, val);
615 return val;
616}
617
618/* sets ctrl & data port bits according to current signals values */
619static void panel_set_bits(void)
620{
621 set_data_bits();
622 set_ctrl_bits();
623}
624
625/*
626 * Converts a parallel port pin (from -25 to 25) to data and control ports
627 * masks, and data and control port bits. The signal will be considered
628 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
629 *
630 * Result will be used this way :
631 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
632 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
633 */
634static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
635{
636 int d_bit, c_bit, inv;
637
638 d_val[0] = 0;
639 c_val[0] = 0;
640 d_val[1] = 0;
641 c_val[1] = 0;
642 d_val[2] = 0xFF;
643 c_val[2] = 0xFF;
644
645 if (pin == 0)
646 return;
647
648 inv = (pin < 0);
649 if (inv)
650 pin = -pin;
651
652 d_bit = 0;
653 c_bit = 0;
654
655 switch (pin) {
656 case PIN_STROBE: /* strobe, inverted */
657 c_bit = PNL_PSTROBE;
658 inv = !inv;
659 break;
660 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
661 d_bit = 1 << (pin - 2);
662 break;
663 case PIN_AUTOLF: /* autofeed, inverted */
664 c_bit = PNL_PAUTOLF;
665 inv = !inv;
666 break;
667 case PIN_INITP: /* init, direct */
668 c_bit = PNL_PINITP;
669 break;
670 case PIN_SELECP: /* select_in, inverted */
671 c_bit = PNL_PSELECP;
672 inv = !inv;
673 break;
674 default: /* unknown pin, ignore */
675 break;
676 }
677
678 if (c_bit) {
679 c_val[2] &= ~c_bit;
680 c_val[!inv] = c_bit;
681 } else if (d_bit) {
682 d_val[2] &= ~d_bit;
683 d_val[!inv] = d_bit;
684 }
685}
686
687/*
688 * send a serial byte to the LCD panel. The caller is responsible for locking
689 * if needed.
690 */
691static void lcd_send_serial(int byte)
692{
693 int bit;
694
695 /*
696 * the data bit is set on D0, and the clock on STROBE.
697 * LCD reads D0 on STROBE's rising edge.
698 */
699 for (bit = 0; bit < 8; bit++) {
700 clear_bit(LCD_BIT_CL, bits); /* CLK low */
701 panel_set_bits();
702 if (byte & 1) {
703 set_bit(LCD_BIT_DA, bits);
704 } else {
705 clear_bit(LCD_BIT_DA, bits);
706 }
707
708 panel_set_bits();
709 udelay(2); /* maintain the data during 2 us before CLK up */
710 set_bit(LCD_BIT_CL, bits); /* CLK high */
711 panel_set_bits();
712 udelay(1); /* maintain the strobe during 1 us */
713 byte >>= 1;
714 }
715}
716
717/* turn the backlight on or off */
718static void lcd_backlight(struct charlcd *charlcd, int on)
719{
720 if (lcd.pins.bl == PIN_NONE)
721 return;
722
723 /* The backlight is activated by setting the AUTOFEED line to +5V */
724 spin_lock_irq(&pprt_lock);
725 if (on)
726 set_bit(LCD_BIT_BL, bits);
727 else
728 clear_bit(LCD_BIT_BL, bits);
729 panel_set_bits();
730 spin_unlock_irq(&pprt_lock);
731}
732
733/* send a command to the LCD panel in serial mode */
734static void lcd_write_cmd_s(struct charlcd *charlcd, int cmd)
735{
736 spin_lock_irq(&pprt_lock);
737 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
738 lcd_send_serial(cmd & 0x0F);
739 lcd_send_serial((cmd >> 4) & 0x0F);
740 udelay(40); /* the shortest command takes at least 40 us */
741 spin_unlock_irq(&pprt_lock);
742}
743
744/* send data to the LCD panel in serial mode */
745static void lcd_write_data_s(struct charlcd *charlcd, int data)
746{
747 spin_lock_irq(&pprt_lock);
748 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
749 lcd_send_serial(data & 0x0F);
750 lcd_send_serial((data >> 4) & 0x0F);
751 udelay(40); /* the shortest data takes at least 40 us */
752 spin_unlock_irq(&pprt_lock);
753}
754
755/* send a command to the LCD panel in 8 bits parallel mode */
756static void lcd_write_cmd_p8(struct charlcd *charlcd, int cmd)
757{
758 spin_lock_irq(&pprt_lock);
759 /* present the data to the data port */
760 w_dtr(pprt, cmd);
761 udelay(20); /* maintain the data during 20 us before the strobe */
762
763 set_bit(LCD_BIT_E, bits);
764 clear_bit(LCD_BIT_RS, bits);
765 clear_bit(LCD_BIT_RW, bits);
766 set_ctrl_bits();
767
768 udelay(40); /* maintain the strobe during 40 us */
769
770 clear_bit(LCD_BIT_E, bits);
771 set_ctrl_bits();
772
773 udelay(120); /* the shortest command takes at least 120 us */
774 spin_unlock_irq(&pprt_lock);
775}
776
777/* send data to the LCD panel in 8 bits parallel mode */
778static void lcd_write_data_p8(struct charlcd *charlcd, int data)
779{
780 spin_lock_irq(&pprt_lock);
781 /* present the data to the data port */
782 w_dtr(pprt, data);
783 udelay(20); /* maintain the data during 20 us before the strobe */
784
785 set_bit(LCD_BIT_E, bits);
786 set_bit(LCD_BIT_RS, bits);
787 clear_bit(LCD_BIT_RW, bits);
788 set_ctrl_bits();
789
790 udelay(40); /* maintain the strobe during 40 us */
791
792 clear_bit(LCD_BIT_E, bits);
793 set_ctrl_bits();
794
795 udelay(45); /* the shortest data takes at least 45 us */
796 spin_unlock_irq(&pprt_lock);
797}
798
799/* send a command to the TI LCD panel */
800static void lcd_write_cmd_tilcd(struct charlcd *charlcd, int cmd)
801{
802 spin_lock_irq(&pprt_lock);
803 /* present the data to the control port */
804 w_ctr(pprt, cmd);
805 udelay(60);
806 spin_unlock_irq(&pprt_lock);
807}
808
809/* send data to the TI LCD panel */
810static void lcd_write_data_tilcd(struct charlcd *charlcd, int data)
811{
812 spin_lock_irq(&pprt_lock);
813 /* present the data to the data port */
814 w_dtr(pprt, data);
815 udelay(60);
816 spin_unlock_irq(&pprt_lock);
817}
818
819/* fills the display with spaces and resets X/Y */
820static void lcd_clear_fast_s(struct charlcd *charlcd)
821{
822 int pos;
823
824 spin_lock_irq(&pprt_lock);
825 for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
826 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
827 lcd_send_serial(' ' & 0x0F);
828 lcd_send_serial((' ' >> 4) & 0x0F);
829 /* the shortest data takes at least 40 us */
830 udelay(40);
831 }
832 spin_unlock_irq(&pprt_lock);
833}
834
835/* fills the display with spaces and resets X/Y */
836static void lcd_clear_fast_p8(struct charlcd *charlcd)
837{
838 int pos;
839
840 spin_lock_irq(&pprt_lock);
841 for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
842 /* present the data to the data port */
843 w_dtr(pprt, ' ');
844
845 /* maintain the data during 20 us before the strobe */
846 udelay(20);
847
848 set_bit(LCD_BIT_E, bits);
849 set_bit(LCD_BIT_RS, bits);
850 clear_bit(LCD_BIT_RW, bits);
851 set_ctrl_bits();
852
853 /* maintain the strobe during 40 us */
854 udelay(40);
855
856 clear_bit(LCD_BIT_E, bits);
857 set_ctrl_bits();
858
859 /* the shortest data takes at least 45 us */
860 udelay(45);
861 }
862 spin_unlock_irq(&pprt_lock);
863}
864
865/* fills the display with spaces and resets X/Y */
866static void lcd_clear_fast_tilcd(struct charlcd *charlcd)
867{
868 int pos;
869
870 spin_lock_irq(&pprt_lock);
871 for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
872 /* present the data to the data port */
873 w_dtr(pprt, ' ');
874 udelay(60);
875 }
876
877 spin_unlock_irq(&pprt_lock);
878}
879
880static const struct charlcd_ops charlcd_serial_ops = {
881 .write_cmd = lcd_write_cmd_s,
882 .write_data = lcd_write_data_s,
883 .clear_fast = lcd_clear_fast_s,
884 .backlight = lcd_backlight,
885};
886
887static const struct charlcd_ops charlcd_parallel_ops = {
888 .write_cmd = lcd_write_cmd_p8,
889 .write_data = lcd_write_data_p8,
890 .clear_fast = lcd_clear_fast_p8,
891 .backlight = lcd_backlight,
892};
893
894static const struct charlcd_ops charlcd_tilcd_ops = {
895 .write_cmd = lcd_write_cmd_tilcd,
896 .write_data = lcd_write_data_tilcd,
897 .clear_fast = lcd_clear_fast_tilcd,
898 .backlight = lcd_backlight,
899};
900
901/* initialize the LCD driver */
902static void lcd_init(void)
903{
904 struct charlcd *charlcd;
905
906 charlcd = charlcd_alloc(0);
907 if (!charlcd)
908 return;
909
910 /*
911 * Init lcd struct with load-time values to preserve exact
912 * current functionality (at least for now).
913 */
914 charlcd->height = lcd_height;
915 charlcd->width = lcd_width;
916 charlcd->bwidth = lcd_bwidth;
917 charlcd->hwidth = lcd_hwidth;
918
919 switch (selected_lcd_type) {
920 case LCD_TYPE_OLD:
921 /* parallel mode, 8 bits */
922 lcd.proto = LCD_PROTO_PARALLEL;
923 lcd.charset = LCD_CHARSET_NORMAL;
924 lcd.pins.e = PIN_STROBE;
925 lcd.pins.rs = PIN_AUTOLF;
926
927 charlcd->width = 40;
928 charlcd->bwidth = 40;
929 charlcd->hwidth = 64;
930 charlcd->height = 2;
931 break;
932 case LCD_TYPE_KS0074:
933 /* serial mode, ks0074 */
934 lcd.proto = LCD_PROTO_SERIAL;
935 lcd.charset = LCD_CHARSET_KS0074;
936 lcd.pins.bl = PIN_AUTOLF;
937 lcd.pins.cl = PIN_STROBE;
938 lcd.pins.da = PIN_D0;
939
940 charlcd->width = 16;
941 charlcd->bwidth = 40;
942 charlcd->hwidth = 16;
943 charlcd->height = 2;
944 break;
945 case LCD_TYPE_NEXCOM:
946 /* parallel mode, 8 bits, generic */
947 lcd.proto = LCD_PROTO_PARALLEL;
948 lcd.charset = LCD_CHARSET_NORMAL;
949 lcd.pins.e = PIN_AUTOLF;
950 lcd.pins.rs = PIN_SELECP;
951 lcd.pins.rw = PIN_INITP;
952
953 charlcd->width = 16;
954 charlcd->bwidth = 40;
955 charlcd->hwidth = 64;
956 charlcd->height = 2;
957 break;
958 case LCD_TYPE_CUSTOM:
959 /* customer-defined */
960 lcd.proto = DEFAULT_LCD_PROTO;
961 lcd.charset = DEFAULT_LCD_CHARSET;
962 /* default geometry will be set later */
963 break;
964 case LCD_TYPE_HANTRONIX:
965 /* parallel mode, 8 bits, hantronix-like */
966 default:
967 lcd.proto = LCD_PROTO_PARALLEL;
968 lcd.charset = LCD_CHARSET_NORMAL;
969 lcd.pins.e = PIN_STROBE;
970 lcd.pins.rs = PIN_SELECP;
971
972 charlcd->width = 16;
973 charlcd->bwidth = 40;
974 charlcd->hwidth = 64;
975 charlcd->height = 2;
976 break;
977 }
978
979 /* Overwrite with module params set on loading */
980 if (lcd_height != NOT_SET)
981 charlcd->height = lcd_height;
982 if (lcd_width != NOT_SET)
983 charlcd->width = lcd_width;
984 if (lcd_bwidth != NOT_SET)
985 charlcd->bwidth = lcd_bwidth;
986 if (lcd_hwidth != NOT_SET)
987 charlcd->hwidth = lcd_hwidth;
988 if (lcd_charset != NOT_SET)
989 lcd.charset = lcd_charset;
990 if (lcd_proto != NOT_SET)
991 lcd.proto = lcd_proto;
992 if (lcd_e_pin != PIN_NOT_SET)
993 lcd.pins.e = lcd_e_pin;
994 if (lcd_rs_pin != PIN_NOT_SET)
995 lcd.pins.rs = lcd_rs_pin;
996 if (lcd_rw_pin != PIN_NOT_SET)
997 lcd.pins.rw = lcd_rw_pin;
998 if (lcd_cl_pin != PIN_NOT_SET)
999 lcd.pins.cl = lcd_cl_pin;
1000 if (lcd_da_pin != PIN_NOT_SET)
1001 lcd.pins.da = lcd_da_pin;
1002 if (lcd_bl_pin != PIN_NOT_SET)
1003 lcd.pins.bl = lcd_bl_pin;
1004
1005 /* this is used to catch wrong and default values */
1006 if (charlcd->width <= 0)
1007 charlcd->width = DEFAULT_LCD_WIDTH;
1008 if (charlcd->bwidth <= 0)
1009 charlcd->bwidth = DEFAULT_LCD_BWIDTH;
1010 if (charlcd->hwidth <= 0)
1011 charlcd->hwidth = DEFAULT_LCD_HWIDTH;
1012 if (charlcd->height <= 0)
1013 charlcd->height = DEFAULT_LCD_HEIGHT;
1014
1015 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
1016 charlcd->ops = &charlcd_serial_ops;
1017
1018 if (lcd.pins.cl == PIN_NOT_SET)
1019 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1020 if (lcd.pins.da == PIN_NOT_SET)
1021 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
1022
1023 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
1024 charlcd->ops = &charlcd_parallel_ops;
1025
1026 if (lcd.pins.e == PIN_NOT_SET)
1027 lcd.pins.e = DEFAULT_LCD_PIN_E;
1028 if (lcd.pins.rs == PIN_NOT_SET)
1029 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1030 if (lcd.pins.rw == PIN_NOT_SET)
1031 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
1032 } else {
1033 charlcd->ops = &charlcd_tilcd_ops;
1034 }
1035
1036 if (lcd.pins.bl == PIN_NOT_SET)
1037 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
1038
1039 if (lcd.pins.e == PIN_NOT_SET)
1040 lcd.pins.e = PIN_NONE;
1041 if (lcd.pins.rs == PIN_NOT_SET)
1042 lcd.pins.rs = PIN_NONE;
1043 if (lcd.pins.rw == PIN_NOT_SET)
1044 lcd.pins.rw = PIN_NONE;
1045 if (lcd.pins.bl == PIN_NOT_SET)
1046 lcd.pins.bl = PIN_NONE;
1047 if (lcd.pins.cl == PIN_NOT_SET)
1048 lcd.pins.cl = PIN_NONE;
1049 if (lcd.pins.da == PIN_NOT_SET)
1050 lcd.pins.da = PIN_NONE;
1051
1052 if (lcd.charset == NOT_SET)
1053 lcd.charset = DEFAULT_LCD_CHARSET;
1054
1055 if (lcd.charset == LCD_CHARSET_KS0074)
1056 charlcd->char_conv = lcd_char_conv_ks0074;
1057 else
1058 charlcd->char_conv = NULL;
1059
1060 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1061 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1062 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1063 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1064 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1065 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1066 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1067 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1068 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1069 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1070 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1071 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
1072
1073 lcd.charlcd = charlcd;
1074 lcd.initialized = true;
1075}
1076
1077/*
1078 * These are the file operation function for user access to /dev/keypad
1079 */
1080
1081static ssize_t keypad_read(struct file *file,
1082 char __user *buf, size_t count, loff_t *ppos)
1083{
1084 unsigned i = *ppos;
1085 char __user *tmp = buf;
1086
1087 if (keypad_buflen == 0) {
1088 if (file->f_flags & O_NONBLOCK)
1089 return -EAGAIN;
1090
1091 if (wait_event_interruptible(keypad_read_wait,
1092 keypad_buflen != 0))
1093 return -EINTR;
1094 }
1095
1096 for (; count-- > 0 && (keypad_buflen > 0);
1097 ++i, ++tmp, --keypad_buflen) {
1098 put_user(keypad_buffer[keypad_start], tmp);
1099 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1100 }
1101 *ppos = i;
1102
1103 return tmp - buf;
1104}
1105
1106static int keypad_open(struct inode *inode, struct file *file)
1107{
1108 int ret;
1109
1110 ret = -EBUSY;
1111 if (!atomic_dec_and_test(&keypad_available))
1112 goto fail; /* open only once at a time */
1113
1114 ret = -EPERM;
1115 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1116 goto fail;
1117
1118 keypad_buflen = 0; /* flush the buffer on opening */
1119 return 0;
1120 fail:
1121 atomic_inc(&keypad_available);
1122 return ret;
1123}
1124
1125static int keypad_release(struct inode *inode, struct file *file)
1126{
1127 atomic_inc(&keypad_available);
1128 return 0;
1129}
1130
1131static const struct file_operations keypad_fops = {
1132 .read = keypad_read, /* read */
1133 .open = keypad_open, /* open */
1134 .release = keypad_release, /* close */
1135 .llseek = default_llseek,
1136};
1137
1138static struct miscdevice keypad_dev = {
1139 .minor = KEYPAD_MINOR,
1140 .name = "keypad",
1141 .fops = &keypad_fops,
1142};
1143
1144static void keypad_send_key(const char *string, int max_len)
1145{
1146 /* send the key to the device only if a process is attached to it. */
1147 if (!atomic_read(&keypad_available)) {
1148 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1149 keypad_buffer[(keypad_start + keypad_buflen++) %
1150 KEYPAD_BUFFER] = *string++;
1151 }
1152 wake_up_interruptible(&keypad_read_wait);
1153 }
1154}
1155
1156/* this function scans all the bits involving at least one logical signal,
1157 * and puts the results in the bitfield "phys_read" (one bit per established
1158 * contact), and sets "phys_read_prev" to "phys_read".
1159 *
1160 * Note: to debounce input signals, we will only consider as switched a signal
1161 * which is stable across 2 measures. Signals which are different between two
1162 * reads will be kept as they previously were in their logical form (phys_prev).
1163 * A signal which has just switched will have a 1 in
1164 * (phys_read ^ phys_read_prev).
1165 */
1166static void phys_scan_contacts(void)
1167{
1168 int bit, bitval;
1169 char oldval;
1170 char bitmask;
1171 char gndmask;
1172
1173 phys_prev = phys_curr;
1174 phys_read_prev = phys_read;
1175 phys_read = 0; /* flush all signals */
1176
1177 /* keep track of old value, with all outputs disabled */
1178 oldval = r_dtr(pprt) | scan_mask_o;
1179 /* activate all keyboard outputs (active low) */
1180 w_dtr(pprt, oldval & ~scan_mask_o);
1181
1182 /* will have a 1 for each bit set to gnd */
1183 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1184 /* disable all matrix signals */
1185 w_dtr(pprt, oldval);
1186
1187 /* now that all outputs are cleared, the only active input bits are
1188 * directly connected to the ground
1189 */
1190
1191 /* 1 for each grounded input */
1192 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1193
1194 /* grounded inputs are signals 40-44 */
1195 phys_read |= (__u64)gndmask << 40;
1196
1197 if (bitmask != gndmask) {
1198 /*
1199 * since clearing the outputs changed some inputs, we know
1200 * that some input signals are currently tied to some outputs.
1201 * So we'll scan them.
1202 */
1203 for (bit = 0; bit < 8; bit++) {
1204 bitval = BIT(bit);
1205
1206 if (!(scan_mask_o & bitval)) /* skip unused bits */
1207 continue;
1208
1209 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1210 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1211 phys_read |= (__u64)bitmask << (5 * bit);
1212 }
1213 w_dtr(pprt, oldval); /* disable all outputs */
1214 }
1215 /*
1216 * this is easy: use old bits when they are flapping,
1217 * use new ones when stable
1218 */
1219 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1220 (phys_read & ~(phys_read ^ phys_read_prev));
1221}
1222
1223static inline int input_state_high(struct logical_input *input)
1224{
1225#if 0
1226 /* FIXME:
1227 * this is an invalid test. It tries to catch
1228 * transitions from single-key to multiple-key, but
1229 * doesn't take into account the contacts polarity.
1230 * The only solution to the problem is to parse keys
1231 * from the most complex to the simplest combinations,
1232 * and mark them as 'caught' once a combination
1233 * matches, then unmatch it for all other ones.
1234 */
1235
1236 /* try to catch dangerous transitions cases :
1237 * someone adds a bit, so this signal was a false
1238 * positive resulting from a transition. We should
1239 * invalidate the signal immediately and not call the
1240 * release function.
1241 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1242 */
1243 if (((phys_prev & input->mask) == input->value) &&
1244 ((phys_curr & input->mask) > input->value)) {
1245 input->state = INPUT_ST_LOW; /* invalidate */
1246 return 1;
1247 }
1248#endif
1249
1250 if ((phys_curr & input->mask) == input->value) {
1251 if ((input->type == INPUT_TYPE_STD) &&
1252 (input->high_timer == 0)) {
1253 input->high_timer++;
1254 if (input->u.std.press_fct)
1255 input->u.std.press_fct(input->u.std.press_data);
1256 } else if (input->type == INPUT_TYPE_KBD) {
1257 /* will turn on the light */
1258 keypressed = 1;
1259
1260 if (input->high_timer == 0) {
1261 char *press_str = input->u.kbd.press_str;
1262
1263 if (press_str[0]) {
1264 int s = sizeof(input->u.kbd.press_str);
1265
1266 keypad_send_key(press_str, s);
1267 }
1268 }
1269
1270 if (input->u.kbd.repeat_str[0]) {
1271 char *repeat_str = input->u.kbd.repeat_str;
1272
1273 if (input->high_timer >= KEYPAD_REP_START) {
1274 int s = sizeof(input->u.kbd.repeat_str);
1275
1276 input->high_timer -= KEYPAD_REP_DELAY;
1277 keypad_send_key(repeat_str, s);
1278 }
1279 /* we will need to come back here soon */
1280 inputs_stable = 0;
1281 }
1282
1283 if (input->high_timer < 255)
1284 input->high_timer++;
1285 }
1286 return 1;
1287 }
1288
1289 /* else signal falling down. Let's fall through. */
1290 input->state = INPUT_ST_FALLING;
1291 input->fall_timer = 0;
1292
1293 return 0;
1294}
1295
1296static inline void input_state_falling(struct logical_input *input)
1297{
1298#if 0
1299 /* FIXME !!! same comment as in input_state_high */
1300 if (((phys_prev & input->mask) == input->value) &&
1301 ((phys_curr & input->mask) > input->value)) {
1302 input->state = INPUT_ST_LOW; /* invalidate */
1303 return;
1304 }
1305#endif
1306
1307 if ((phys_curr & input->mask) == input->value) {
1308 if (input->type == INPUT_TYPE_KBD) {
1309 /* will turn on the light */
1310 keypressed = 1;
1311
1312 if (input->u.kbd.repeat_str[0]) {
1313 char *repeat_str = input->u.kbd.repeat_str;
1314
1315 if (input->high_timer >= KEYPAD_REP_START) {
1316 int s = sizeof(input->u.kbd.repeat_str);
1317
1318 input->high_timer -= KEYPAD_REP_DELAY;
1319 keypad_send_key(repeat_str, s);
1320 }
1321 /* we will need to come back here soon */
1322 inputs_stable = 0;
1323 }
1324
1325 if (input->high_timer < 255)
1326 input->high_timer++;
1327 }
1328 input->state = INPUT_ST_HIGH;
1329 } else if (input->fall_timer >= input->fall_time) {
1330 /* call release event */
1331 if (input->type == INPUT_TYPE_STD) {
1332 void (*release_fct)(int) = input->u.std.release_fct;
1333
1334 if (release_fct)
1335 release_fct(input->u.std.release_data);
1336 } else if (input->type == INPUT_TYPE_KBD) {
1337 char *release_str = input->u.kbd.release_str;
1338
1339 if (release_str[0]) {
1340 int s = sizeof(input->u.kbd.release_str);
1341
1342 keypad_send_key(release_str, s);
1343 }
1344 }
1345
1346 input->state = INPUT_ST_LOW;
1347 } else {
1348 input->fall_timer++;
1349 inputs_stable = 0;
1350 }
1351}
1352
1353static void panel_process_inputs(void)
1354{
1355 struct logical_input *input;
1356
1357 keypressed = 0;
1358 inputs_stable = 1;
1359 list_for_each_entry(input, &logical_inputs, list) {
1360 switch (input->state) {
1361 case INPUT_ST_LOW:
1362 if ((phys_curr & input->mask) != input->value)
1363 break;
1364 /* if all needed ones were already set previously,
1365 * this means that this logical signal has been
1366 * activated by the releasing of another combined
1367 * signal, so we don't want to match.
1368 * eg: AB -(release B)-> A -(release A)-> 0 :
1369 * don't match A.
1370 */
1371 if ((phys_prev & input->mask) == input->value)
1372 break;
1373 input->rise_timer = 0;
1374 input->state = INPUT_ST_RISING;
1375 /* fall through */
1376 case INPUT_ST_RISING:
1377 if ((phys_curr & input->mask) != input->value) {
1378 input->state = INPUT_ST_LOW;
1379 break;
1380 }
1381 if (input->rise_timer < input->rise_time) {
1382 inputs_stable = 0;
1383 input->rise_timer++;
1384 break;
1385 }
1386 input->high_timer = 0;
1387 input->state = INPUT_ST_HIGH;
1388 /* fall through */
1389 case INPUT_ST_HIGH:
1390 if (input_state_high(input))
1391 break;
1392 /* fall through */
1393 case INPUT_ST_FALLING:
1394 input_state_falling(input);
1395 }
1396 }
1397}
1398
1399static void panel_scan_timer(struct timer_list *unused)
1400{
1401 if (keypad.enabled && keypad_initialized) {
1402 if (spin_trylock_irq(&pprt_lock)) {
1403 phys_scan_contacts();
1404
1405 /* no need for the parport anymore */
1406 spin_unlock_irq(&pprt_lock);
1407 }
1408
1409 if (!inputs_stable || phys_curr != phys_prev)
1410 panel_process_inputs();
1411 }
1412
1413 if (keypressed && lcd.enabled && lcd.initialized)
1414 charlcd_poke(lcd.charlcd);
1415
1416 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
1417}
1418
1419static void init_scan_timer(void)
1420{
1421 if (scan_timer.function)
1422 return; /* already started */
1423
1424 timer_setup(&scan_timer, panel_scan_timer, 0);
1425 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1426 add_timer(&scan_timer);
1427}
1428
1429/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1430 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1431 * corresponding to out and in bits respectively.
1432 * returns 1 if ok, 0 if error (in which case, nothing is written).
1433 */
1434static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
1435 u8 *imask, u8 *omask)
1436{
1437 const char sigtab[] = "EeSsPpAaBb";
1438 u8 im, om;
1439 __u64 m, v;
1440
1441 om = 0;
1442 im = 0;
1443 m = 0ULL;
1444 v = 0ULL;
1445 while (*name) {
1446 int in, out, bit, neg;
1447 const char *idx;
1448
1449 idx = strchr(sigtab, *name);
1450 if (!idx)
1451 return 0; /* input name not found */
1452
1453 in = idx - sigtab;
1454 neg = (in & 1); /* odd (lower) names are negated */
1455 in >>= 1;
1456 im |= BIT(in);
1457
1458 name++;
1459 if (*name >= '0' && *name <= '7') {
1460 out = *name - '0';
1461 om |= BIT(out);
1462 } else if (*name == '-') {
1463 out = 8;
1464 } else {
1465 return 0; /* unknown bit name */
1466 }
1467
1468 bit = (out * 5) + in;
1469
1470 m |= 1ULL << bit;
1471 if (!neg)
1472 v |= 1ULL << bit;
1473 name++;
1474 }
1475 *mask = m;
1476 *value = v;
1477 if (imask)
1478 *imask |= im;
1479 if (omask)
1480 *omask |= om;
1481 return 1;
1482}
1483
1484/* tries to bind a key to the signal name <name>. The key will send the
1485 * strings <press>, <repeat>, <release> for these respective events.
1486 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1487 */
1488static struct logical_input *panel_bind_key(const char *name, const char *press,
1489 const char *repeat,
1490 const char *release)
1491{
1492 struct logical_input *key;
1493
1494 key = kzalloc(sizeof(*key), GFP_KERNEL);
1495 if (!key)
1496 return NULL;
1497
1498 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1499 &scan_mask_o)) {
1500 kfree(key);
1501 return NULL;
1502 }
1503
1504 key->type = INPUT_TYPE_KBD;
1505 key->state = INPUT_ST_LOW;
1506 key->rise_time = 1;
1507 key->fall_time = 1;
1508
1509 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
1510 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
1511 strncpy(key->u.kbd.release_str, release,
1512 sizeof(key->u.kbd.release_str));
1513 list_add(&key->list, &logical_inputs);
1514 return key;
1515}
1516
1517#if 0
1518/* tries to bind a callback function to the signal name <name>. The function
1519 * <press_fct> will be called with the <press_data> arg when the signal is
1520 * activated, and so on for <release_fct>/<release_data>
1521 * Returns the pointer to the new signal if ok, NULL if the signal could not
1522 * be bound.
1523 */
1524static struct logical_input *panel_bind_callback(char *name,
1525 void (*press_fct)(int),
1526 int press_data,
1527 void (*release_fct)(int),
1528 int release_data)
1529{
1530 struct logical_input *callback;
1531
1532 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
1533 if (!callback)
1534 return NULL;
1535
1536 memset(callback, 0, sizeof(struct logical_input));
1537 if (!input_name2mask(name, &callback->mask, &callback->value,
1538 &scan_mask_i, &scan_mask_o))
1539 return NULL;
1540
1541 callback->type = INPUT_TYPE_STD;
1542 callback->state = INPUT_ST_LOW;
1543 callback->rise_time = 1;
1544 callback->fall_time = 1;
1545 callback->u.std.press_fct = press_fct;
1546 callback->u.std.press_data = press_data;
1547 callback->u.std.release_fct = release_fct;
1548 callback->u.std.release_data = release_data;
1549 list_add(&callback->list, &logical_inputs);
1550 return callback;
1551}
1552#endif
1553
1554static void keypad_init(void)
1555{
1556 int keynum;
1557
1558 init_waitqueue_head(&keypad_read_wait);
1559 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
1560
1561 /* Let's create all known keys */
1562
1563 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
1564 panel_bind_key(keypad_profile[keynum][0],
1565 keypad_profile[keynum][1],
1566 keypad_profile[keynum][2],
1567 keypad_profile[keynum][3]);
1568 }
1569
1570 init_scan_timer();
1571 keypad_initialized = 1;
1572}
1573
1574/**************************************************/
1575/* device initialization */
1576/**************************************************/
1577
1578static void panel_attach(struct parport *port)
1579{
1580 struct pardev_cb panel_cb;
1581
1582 if (port->number != parport)
1583 return;
1584
1585 if (pprt) {
1586 pr_err("%s: port->number=%d parport=%d, already registered!\n",
1587 __func__, port->number, parport);
1588 return;
1589 }
1590
1591 memset(&panel_cb, 0, sizeof(panel_cb));
1592 panel_cb.private = &pprt;
1593 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
1594
1595 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
1596 if (!pprt) {
1597 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
1598 __func__, port->number, parport);
1599 return;
1600 }
1601
1602 if (parport_claim(pprt)) {
1603 pr_err("could not claim access to parport%d. Aborting.\n",
1604 parport);
1605 goto err_unreg_device;
1606 }
1607
1608 /* must init LCD first, just in case an IRQ from the keypad is
1609 * generated at keypad init
1610 */
1611 if (lcd.enabled) {
1612 lcd_init();
1613 if (!lcd.charlcd || charlcd_register(lcd.charlcd))
1614 goto err_unreg_device;
1615 }
1616
1617 if (keypad.enabled) {
1618 keypad_init();
1619 if (misc_register(&keypad_dev))
1620 goto err_lcd_unreg;
1621 }
1622 return;
1623
1624err_lcd_unreg:
1625 if (lcd.enabled)
1626 charlcd_unregister(lcd.charlcd);
1627err_unreg_device:
1628 kfree(lcd.charlcd);
1629 lcd.charlcd = NULL;
1630 parport_unregister_device(pprt);
1631 pprt = NULL;
1632}
1633
1634static void panel_detach(struct parport *port)
1635{
1636 if (port->number != parport)
1637 return;
1638
1639 if (!pprt) {
1640 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
1641 __func__, port->number, parport);
1642 return;
1643 }
1644 if (scan_timer.function)
1645 del_timer_sync(&scan_timer);
1646
1647 if (keypad.enabled) {
1648 misc_deregister(&keypad_dev);
1649 keypad_initialized = 0;
1650 }
1651
1652 if (lcd.enabled) {
1653 charlcd_unregister(lcd.charlcd);
1654 lcd.initialized = false;
1655 kfree(lcd.charlcd);
1656 lcd.charlcd = NULL;
1657 }
1658
1659 /* TODO: free all input signals */
1660 parport_release(pprt);
1661 parport_unregister_device(pprt);
1662 pprt = NULL;
1663}
1664
1665static struct parport_driver panel_driver = {
1666 .name = "panel",
1667 .match_port = panel_attach,
1668 .detach = panel_detach,
1669 .devmodel = true,
1670};
1671
1672/* init function */
1673static int __init panel_init_module(void)
1674{
1675 int selected_keypad_type = NOT_SET, err;
1676
1677 /* take care of an eventual profile */
1678 switch (profile) {
1679 case PANEL_PROFILE_CUSTOM:
1680 /* custom profile */
1681 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
1682 selected_lcd_type = DEFAULT_LCD_TYPE;
1683 break;
1684 case PANEL_PROFILE_OLD:
1685 /* 8 bits, 2*16, old keypad */
1686 selected_keypad_type = KEYPAD_TYPE_OLD;
1687 selected_lcd_type = LCD_TYPE_OLD;
1688
1689 /* TODO: This two are a little hacky, sort it out later */
1690 if (lcd_width == NOT_SET)
1691 lcd_width = 16;
1692 if (lcd_hwidth == NOT_SET)
1693 lcd_hwidth = 16;
1694 break;
1695 case PANEL_PROFILE_NEW:
1696 /* serial, 2*16, new keypad */
1697 selected_keypad_type = KEYPAD_TYPE_NEW;
1698 selected_lcd_type = LCD_TYPE_KS0074;
1699 break;
1700 case PANEL_PROFILE_HANTRONIX:
1701 /* 8 bits, 2*16 hantronix-like, no keypad */
1702 selected_keypad_type = KEYPAD_TYPE_NONE;
1703 selected_lcd_type = LCD_TYPE_HANTRONIX;
1704 break;
1705 case PANEL_PROFILE_NEXCOM:
1706 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
1707 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
1708 selected_lcd_type = LCD_TYPE_NEXCOM;
1709 break;
1710 case PANEL_PROFILE_LARGE:
1711 /* 8 bits, 2*40, old keypad */
1712 selected_keypad_type = KEYPAD_TYPE_OLD;
1713 selected_lcd_type = LCD_TYPE_OLD;
1714 break;
1715 }
1716
1717 /*
1718 * Overwrite selection with module param values (both keypad and lcd),
1719 * where the deprecated params have lower prio.
1720 */
1721 if (keypad_enabled != NOT_SET)
1722 selected_keypad_type = keypad_enabled;
1723 if (keypad_type != NOT_SET)
1724 selected_keypad_type = keypad_type;
1725
1726 keypad.enabled = (selected_keypad_type > 0);
1727
1728 if (lcd_enabled != NOT_SET)
1729 selected_lcd_type = lcd_enabled;
1730 if (lcd_type != NOT_SET)
1731 selected_lcd_type = lcd_type;
1732
1733 lcd.enabled = (selected_lcd_type > 0);
1734
1735 if (lcd.enabled) {
1736 /*
1737 * Init lcd struct with load-time values to preserve exact
1738 * current functionality (at least for now).
1739 */
1740 lcd.charset = lcd_charset;
1741 lcd.proto = lcd_proto;
1742 lcd.pins.e = lcd_e_pin;
1743 lcd.pins.rs = lcd_rs_pin;
1744 lcd.pins.rw = lcd_rw_pin;
1745 lcd.pins.cl = lcd_cl_pin;
1746 lcd.pins.da = lcd_da_pin;
1747 lcd.pins.bl = lcd_bl_pin;
1748 }
1749
1750 switch (selected_keypad_type) {
1751 case KEYPAD_TYPE_OLD:
1752 keypad_profile = old_keypad_profile;
1753 break;
1754 case KEYPAD_TYPE_NEW:
1755 keypad_profile = new_keypad_profile;
1756 break;
1757 case KEYPAD_TYPE_NEXCOM:
1758 keypad_profile = nexcom_keypad_profile;
1759 break;
1760 default:
1761 keypad_profile = NULL;
1762 break;
1763 }
1764
1765 if (!lcd.enabled && !keypad.enabled) {
1766 /* no device enabled, let's exit */
1767 pr_err("panel driver disabled.\n");
1768 return -ENODEV;
1769 }
1770
1771 err = parport_register_driver(&panel_driver);
1772 if (err) {
1773 pr_err("could not register with parport. Aborting.\n");
1774 return err;
1775 }
1776
1777 if (pprt)
1778 pr_info("panel driver registered on parport%d (io=0x%lx).\n",
1779 parport, pprt->port->base);
1780 else
1781 pr_info("panel driver not yet registered\n");
1782 return 0;
1783}
1784
1785static void __exit panel_cleanup_module(void)
1786{
1787 parport_unregister_driver(&panel_driver);
1788}
1789
1790module_init(panel_init_module);
1791module_exit(panel_cleanup_module);
1792MODULE_AUTHOR("Willy Tarreau");
1793MODULE_LICENSE("GPL");
1794
1795/*
1796 * Local variables:
1797 * c-indent-level: 4
1798 * tab-width: 8
1799 * End:
1800 */