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