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