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