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