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