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