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