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