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