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