2 * Front panel driver for Linux
3 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
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.
10 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
11 * connected to a parallel printer port.
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.
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.
21 * Several profiles are provided for commonly found LCD+keypad modules on the
22 * market, such as those found in Nexcom's appliances.
25 * - the initialization/deinitialization process is very dirty and should
26 * be rewritten. It may even be buggy.
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
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/module.h>
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>
62 #include <linux/uaccess.h>
65 #define KEYPAD_MINOR 185
67 #define PANEL_VERSION "0.9.5"
69 #define LCD_MAXBYTES 256 /* max burst write */
71 #define KEYPAD_BUFFER 64
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)
80 /* keep the light on this times INPUT_POLL_TIME for each flash */
81 #define FLASH_LIGHT_TEMPO (200)
83 /* converts an r_str() input to an active high, bits string : 000BAOSE */
84 #define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
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 */
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 */
119 #define PIN_AUTOLF 14
121 #define PIN_SELECP 17
122 #define PIN_NOT_SET 127
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 */
133 #define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
134 #define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
138 /* macros to simplify use of the parallel port */
139 #define r_ctr(x) (parport_read_control((x)->port))
140 #define r_dtr(x) (parport_read_data((x)->port))
141 #define r_str(x) (parport_read_status((x)->port))
142 #define w_ctr(x, y) (parport_write_control((x)->port, (y)))
143 #define w_dtr(x, y) (parport_write_data((x)->port, (y)))
145 /* this defines which bits are to be used and which ones to be ignored */
146 /* logical or of the output bits involved in the scan matrix */
147 static __u8 scan_mask_o;
148 /* logical or of the input bits involved in the scan matrix */
149 static __u8 scan_mask_i;
151 typedef __u64 pmask_t;
165 struct logical_input {
166 struct list_head list;
169 enum input_type type;
170 enum input_state state;
171 __u8 rise_time, fall_time;
172 __u8 rise_timer, fall_timer, high_timer;
175 struct { /* valid when type == INPUT_TYPE_STD */
176 void (*press_fct)(int);
177 void (*release_fct)(int);
181 struct { /* valid when type == INPUT_TYPE_KBD */
182 /* strings can be non null-terminated */
183 char press_str[sizeof(void *) + sizeof(int)];
184 char repeat_str[sizeof(void *) + sizeof(int)];
185 char release_str[sizeof(void *) + sizeof(int)];
190 static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
192 /* physical contacts history
193 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
194 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
195 * corresponds to the ground.
196 * Within each group, bits are stored in the same order as read on the port :
197 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
198 * So, each __u64 (or pmask_t) is represented like this :
199 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
200 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
203 /* what has just been read from the I/O ports */
204 static pmask_t phys_read;
205 /* previous phys_read */
206 static pmask_t phys_read_prev;
207 /* stabilized phys_read (phys_read|phys_read_prev) */
208 static pmask_t phys_curr;
209 /* previous phys_curr */
210 static pmask_t phys_prev;
211 /* 0 means that at least one logical signal needs be computed */
212 static char inputs_stable;
214 /* these variables are specific to the keypad */
219 static char keypad_buffer[KEYPAD_BUFFER];
220 static int keypad_buflen;
221 static int keypad_start;
222 static char keypressed;
223 static wait_queue_head_t keypad_read_wait;
225 /* lcd-specific variables */
231 /* TODO: use bool here? */
242 /* TODO: use union here? */
252 /* contains the LCD config state */
253 unsigned long int flags;
255 /* Contains the LCD X and Y offset */
261 /* Current escape sequence and it's length or -1 if outside */
263 char buf[LCD_ESCAPE_LEN + 1];
268 /* Needed only for init */
269 static int selected_lcd_type = NOT_SET;
272 * Bit masks to convert LCD signals to parallel port outputs.
273 * _d_ are values for data port, _c_ are for control port.
274 * [0] = signal OFF, [1] = signal ON, [2] = mask
281 * one entry for each bit on the LCD
292 * each bit can be either connected to a DATA or CTRL port
298 static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
303 #define LCD_PROTO_PARALLEL 0
304 #define LCD_PROTO_SERIAL 1
305 #define LCD_PROTO_TI_DA8XX_LCD 2
310 #define LCD_CHARSET_NORMAL 0
311 #define LCD_CHARSET_KS0074 1
316 #define LCD_TYPE_NONE 0
317 #define LCD_TYPE_OLD 1
318 #define LCD_TYPE_KS0074 2
319 #define LCD_TYPE_HANTRONIX 3
320 #define LCD_TYPE_NEXCOM 4
321 #define LCD_TYPE_CUSTOM 5
326 #define KEYPAD_TYPE_NONE 0
327 #define KEYPAD_TYPE_OLD 1
328 #define KEYPAD_TYPE_NEW 2
329 #define KEYPAD_TYPE_NEXCOM 3
334 #define PANEL_PROFILE_CUSTOM 0
335 #define PANEL_PROFILE_OLD 1
336 #define PANEL_PROFILE_NEW 2
337 #define PANEL_PROFILE_HANTRONIX 3
338 #define PANEL_PROFILE_NEXCOM 4
339 #define PANEL_PROFILE_LARGE 5
342 * Construct custom config from the kernel's configuration
344 #define DEFAULT_PARPORT 0
345 #define DEFAULT_PROFILE PANEL_PROFILE_LARGE
346 #define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
347 #define DEFAULT_LCD_TYPE LCD_TYPE_OLD
348 #define DEFAULT_LCD_HEIGHT 2
349 #define DEFAULT_LCD_WIDTH 40
350 #define DEFAULT_LCD_BWIDTH 40
351 #define DEFAULT_LCD_HWIDTH 64
352 #define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
353 #define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
355 #define DEFAULT_LCD_PIN_E PIN_AUTOLF
356 #define DEFAULT_LCD_PIN_RS PIN_SELECP
357 #define DEFAULT_LCD_PIN_RW PIN_INITP
358 #define DEFAULT_LCD_PIN_SCL PIN_STROBE
359 #define DEFAULT_LCD_PIN_SDA PIN_D0
360 #define DEFAULT_LCD_PIN_BL PIN_NOT_SET
362 #ifdef CONFIG_PANEL_PARPORT
363 #undef DEFAULT_PARPORT
364 #define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
367 #ifdef CONFIG_PANEL_PROFILE
368 #undef DEFAULT_PROFILE
369 #define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
372 #if DEFAULT_PROFILE == 0 /* custom */
373 #ifdef CONFIG_PANEL_KEYPAD
374 #undef DEFAULT_KEYPAD_TYPE
375 #define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
378 #ifdef CONFIG_PANEL_LCD
379 #undef DEFAULT_LCD_TYPE
380 #define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
383 #ifdef CONFIG_PANEL_LCD_HEIGHT
384 #undef DEFAULT_LCD_HEIGHT
385 #define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
388 #ifdef CONFIG_PANEL_LCD_WIDTH
389 #undef DEFAULT_LCD_WIDTH
390 #define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
393 #ifdef CONFIG_PANEL_LCD_BWIDTH
394 #undef DEFAULT_LCD_BWIDTH
395 #define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
398 #ifdef CONFIG_PANEL_LCD_HWIDTH
399 #undef DEFAULT_LCD_HWIDTH
400 #define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
403 #ifdef CONFIG_PANEL_LCD_CHARSET
404 #undef DEFAULT_LCD_CHARSET
405 #define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
408 #ifdef CONFIG_PANEL_LCD_PROTO
409 #undef DEFAULT_LCD_PROTO
410 #define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
413 #ifdef CONFIG_PANEL_LCD_PIN_E
414 #undef DEFAULT_LCD_PIN_E
415 #define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
418 #ifdef CONFIG_PANEL_LCD_PIN_RS
419 #undef DEFAULT_LCD_PIN_RS
420 #define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
423 #ifdef CONFIG_PANEL_LCD_PIN_RW
424 #undef DEFAULT_LCD_PIN_RW
425 #define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
428 #ifdef CONFIG_PANEL_LCD_PIN_SCL
429 #undef DEFAULT_LCD_PIN_SCL
430 #define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
433 #ifdef CONFIG_PANEL_LCD_PIN_SDA
434 #undef DEFAULT_LCD_PIN_SDA
435 #define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
438 #ifdef CONFIG_PANEL_LCD_PIN_BL
439 #undef DEFAULT_LCD_PIN_BL
440 #define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
443 #endif /* DEFAULT_PROFILE == 0 */
445 /* global variables */
447 /* Device single-open policy control */
448 static atomic_t lcd_available = ATOMIC_INIT(1);
449 static atomic_t keypad_available = ATOMIC_INIT(1);
451 static struct pardevice *pprt;
453 static int keypad_initialized;
455 static char init_in_progress;
457 static void (*lcd_write_cmd)(int);
458 static void (*lcd_write_data)(int);
459 static void (*lcd_clear_fast)(void);
461 static DEFINE_SPINLOCK(pprt_lock);
462 static struct timer_list scan_timer;
464 MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
466 static int parport = DEFAULT_PARPORT;
467 module_param(parport, int, 0000);
468 MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
470 static int profile = DEFAULT_PROFILE;
471 module_param(profile, int, 0000);
472 MODULE_PARM_DESC(profile,
473 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
474 "4=16x2 nexcom; default=40x2, old kp");
476 static int keypad_type = NOT_SET;
477 module_param(keypad_type, int, 0000);
478 MODULE_PARM_DESC(keypad_type,
479 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
481 static int lcd_type = NOT_SET;
482 module_param(lcd_type, int, 0000);
483 MODULE_PARM_DESC(lcd_type,
484 "LCD type: 0=none, 1=old //, 2=serial ks0074, 3=hantronix //, 4=nexcom //, 5=compiled-in");
486 static int lcd_height = NOT_SET;
487 module_param(lcd_height, int, 0000);
488 MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
490 static int lcd_width = NOT_SET;
491 module_param(lcd_width, int, 0000);
492 MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
494 static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
495 module_param(lcd_bwidth, int, 0000);
496 MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
498 static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
499 module_param(lcd_hwidth, int, 0000);
500 MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
502 static int lcd_charset = NOT_SET;
503 module_param(lcd_charset, int, 0000);
504 MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
506 static int lcd_proto = NOT_SET;
507 module_param(lcd_proto, int, 0000);
508 MODULE_PARM_DESC(lcd_proto,
509 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
512 * These are the parallel port pins the LCD control signals are connected to.
513 * Set this to 0 if the signal is not used. Set it to its opposite value
514 * (negative) if the signal is negated. -MAXINT is used to indicate that the
515 * pin has not been explicitly specified.
517 * WARNING! no check will be performed about collisions with keypad !
520 static int lcd_e_pin = PIN_NOT_SET;
521 module_param(lcd_e_pin, int, 0000);
522 MODULE_PARM_DESC(lcd_e_pin,
523 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
525 static int lcd_rs_pin = PIN_NOT_SET;
526 module_param(lcd_rs_pin, int, 0000);
527 MODULE_PARM_DESC(lcd_rs_pin,
528 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
530 static int lcd_rw_pin = PIN_NOT_SET;
531 module_param(lcd_rw_pin, int, 0000);
532 MODULE_PARM_DESC(lcd_rw_pin,
533 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
535 static int lcd_cl_pin = PIN_NOT_SET;
536 module_param(lcd_cl_pin, int, 0000);
537 MODULE_PARM_DESC(lcd_cl_pin,
538 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
540 static int lcd_da_pin = PIN_NOT_SET;
541 module_param(lcd_da_pin, int, 0000);
542 MODULE_PARM_DESC(lcd_da_pin,
543 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
545 static int lcd_bl_pin = PIN_NOT_SET;
546 module_param(lcd_bl_pin, int, 0000);
547 MODULE_PARM_DESC(lcd_bl_pin,
548 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
550 /* Deprecated module parameters - consider not using them anymore */
552 static int lcd_enabled = NOT_SET;
553 module_param(lcd_enabled, int, 0000);
554 MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
556 static int keypad_enabled = NOT_SET;
557 module_param(keypad_enabled, int, 0000);
558 MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
561 static const unsigned char *lcd_char_conv;
563 /* for some LCD drivers (ks0074) we need a charset conversion table. */
564 static const unsigned char lcd_char_conv_ks0074[256] = {
565 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
566 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
567 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
568 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
569 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
570 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
571 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
572 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
573 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
574 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
575 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
576 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
577 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
578 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
579 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
580 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
581 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
582 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
583 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
584 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
585 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
586 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
587 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
588 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
589 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
590 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
591 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
592 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
593 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
594 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
595 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
596 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
597 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
600 static const char old_keypad_profile[][4][9] = {
601 {"S0", "Left\n", "Left\n", ""},
602 {"S1", "Down\n", "Down\n", ""},
603 {"S2", "Up\n", "Up\n", ""},
604 {"S3", "Right\n", "Right\n", ""},
605 {"S4", "Esc\n", "Esc\n", ""},
606 {"S5", "Ret\n", "Ret\n", ""},
610 /* signals, press, repeat, release */
611 static const char new_keypad_profile[][4][9] = {
612 {"S0", "Left\n", "Left\n", ""},
613 {"S1", "Down\n", "Down\n", ""},
614 {"S2", "Up\n", "Up\n", ""},
615 {"S3", "Right\n", "Right\n", ""},
616 {"S4s5", "", "Esc\n", "Esc\n"},
617 {"s4S5", "", "Ret\n", "Ret\n"},
618 {"S4S5", "Help\n", "", ""},
619 /* add new signals above this line */
623 /* signals, press, repeat, release */
624 static const char nexcom_keypad_profile[][4][9] = {
625 {"a-p-e-", "Down\n", "Down\n", ""},
626 {"a-p-E-", "Ret\n", "Ret\n", ""},
627 {"a-P-E-", "Esc\n", "Esc\n", ""},
628 {"a-P-e-", "Up\n", "Up\n", ""},
629 /* add new signals above this line */
633 static const char (*keypad_profile)[4][9] = old_keypad_profile;
635 /* FIXME: this should be converted to a bit array containing signals states */
637 unsigned char e; /* parallel LCD E (data latch on falling edge) */
638 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
639 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
640 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
641 unsigned char cl; /* serial LCD clock (latch on rising edge) */
642 unsigned char da; /* serial LCD data */
645 static void init_scan_timer(void);
647 /* sets data port bits according to current signals values */
648 static int set_data_bits(void)
653 for (bit = 0; bit < LCD_BITS; bit++)
654 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
656 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
657 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
658 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
659 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
660 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
661 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
667 /* sets ctrl port bits according to current signals values */
668 static int set_ctrl_bits(void)
673 for (bit = 0; bit < LCD_BITS; bit++)
674 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
676 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
677 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
678 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
679 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
680 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
681 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
687 /* sets ctrl & data port bits according to current signals values */
688 static void panel_set_bits(void)
695 * Converts a parallel port pin (from -25 to 25) to data and control ports
696 * masks, and data and control port bits. The signal will be considered
697 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
699 * Result will be used this way :
700 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
701 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
703 static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
705 int d_bit, c_bit, inv;
725 case PIN_STROBE: /* strobe, inverted */
729 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
730 d_bit = 1 << (pin - 2);
732 case PIN_AUTOLF: /* autofeed, inverted */
736 case PIN_INITP: /* init, direct */
739 case PIN_SELECP: /* select_in, inverted */
743 default: /* unknown pin, ignore */
756 /* sleeps that many milliseconds with a reschedule */
757 static void long_sleep(int ms)
759 if (in_interrupt()) {
762 current->state = TASK_INTERRUPTIBLE;
763 schedule_timeout((ms * HZ + 999) / 1000);
767 /* send a serial byte to the LCD panel. The caller is responsible for locking
769 static void lcd_send_serial(int byte)
773 /* the data bit is set on D0, and the clock on STROBE.
774 * LCD reads D0 on STROBE's rising edge. */
775 for (bit = 0; bit < 8; bit++) {
776 bits.cl = BIT_CLR; /* CLK low */
780 udelay(2); /* maintain the data during 2 us before CLK up */
781 bits.cl = BIT_SET; /* CLK high */
783 udelay(1); /* maintain the strobe during 1 us */
788 /* turn the backlight on or off */
789 static void lcd_backlight(int on)
791 if (lcd.pins.bl == PIN_NONE)
794 /* The backlight is activated by setting the AUTOFEED line to +5V */
795 spin_lock_irq(&pprt_lock);
798 spin_unlock_irq(&pprt_lock);
801 /* send a command to the LCD panel in serial mode */
802 static void lcd_write_cmd_s(int cmd)
804 spin_lock_irq(&pprt_lock);
805 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
806 lcd_send_serial(cmd & 0x0F);
807 lcd_send_serial((cmd >> 4) & 0x0F);
808 udelay(40); /* the shortest command takes at least 40 us */
809 spin_unlock_irq(&pprt_lock);
812 /* send data to the LCD panel in serial mode */
813 static void lcd_write_data_s(int data)
815 spin_lock_irq(&pprt_lock);
816 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
817 lcd_send_serial(data & 0x0F);
818 lcd_send_serial((data >> 4) & 0x0F);
819 udelay(40); /* the shortest data takes at least 40 us */
820 spin_unlock_irq(&pprt_lock);
823 /* send a command to the LCD panel in 8 bits parallel mode */
824 static void lcd_write_cmd_p8(int cmd)
826 spin_lock_irq(&pprt_lock);
827 /* present the data to the data port */
829 udelay(20); /* maintain the data during 20 us before the strobe */
836 udelay(40); /* maintain the strobe during 40 us */
841 udelay(120); /* the shortest command takes at least 120 us */
842 spin_unlock_irq(&pprt_lock);
845 /* send data to the LCD panel in 8 bits parallel mode */
846 static void lcd_write_data_p8(int data)
848 spin_lock_irq(&pprt_lock);
849 /* present the data to the data port */
851 udelay(20); /* maintain the data during 20 us before the strobe */
858 udelay(40); /* maintain the strobe during 40 us */
863 udelay(45); /* the shortest data takes at least 45 us */
864 spin_unlock_irq(&pprt_lock);
867 /* send a command to the TI LCD panel */
868 static void lcd_write_cmd_tilcd(int cmd)
870 spin_lock_irq(&pprt_lock);
871 /* present the data to the control port */
874 spin_unlock_irq(&pprt_lock);
877 /* send data to the TI LCD panel */
878 static void lcd_write_data_tilcd(int data)
880 spin_lock_irq(&pprt_lock);
881 /* present the data to the data port */
884 spin_unlock_irq(&pprt_lock);
887 static void lcd_gotoxy(void)
889 lcd_write_cmd(0x80 /* set DDRAM address */
890 | (lcd.addr.y ? lcd.hwidth : 0)
891 /* we force the cursor to stay at the end of the
892 line if it wants to go farther */
893 | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
894 (lcd.hwidth - 1) : lcd.bwidth - 1));
897 static void lcd_print(char c)
899 if (lcd.addr.x < lcd.bwidth) {
900 if (lcd_char_conv != NULL)
901 c = lcd_char_conv[(unsigned char)c];
905 /* prevents the cursor from wrapping onto the next line */
906 if (lcd.addr.x == lcd.bwidth)
910 /* fills the display with spaces and resets X/Y */
911 static void lcd_clear_fast_s(void)
919 spin_lock_irq(&pprt_lock);
920 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
921 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
922 lcd_send_serial(' ' & 0x0F);
923 lcd_send_serial((' ' >> 4) & 0x0F);
924 udelay(40); /* the shortest data takes at least 40 us */
926 spin_unlock_irq(&pprt_lock);
933 /* fills the display with spaces and resets X/Y */
934 static void lcd_clear_fast_p8(void)
942 spin_lock_irq(&pprt_lock);
943 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
944 /* present the data to the data port */
947 /* maintain the data during 20 us before the strobe */
955 /* maintain the strobe during 40 us */
961 /* the shortest data takes at least 45 us */
964 spin_unlock_irq(&pprt_lock);
971 /* fills the display with spaces and resets X/Y */
972 static void lcd_clear_fast_tilcd(void)
980 spin_lock_irq(&pprt_lock);
981 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
982 /* present the data to the data port */
987 spin_unlock_irq(&pprt_lock);
994 /* clears the display and resets X/Y */
995 static void lcd_clear_display(void)
997 lcd_write_cmd(0x01); /* clear display */
1000 /* we must wait a few milliseconds (15) */
1004 static void lcd_init_display(void)
1006 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
1007 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
1009 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
1011 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
1013 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
1015 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
1018 lcd_write_cmd(0x30 /* set font height and lines number */
1019 | ((lcd.flags & LCD_FLAG_F) ? 4 : 0)
1020 | ((lcd.flags & LCD_FLAG_N) ? 8 : 0)
1024 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
1027 lcd_write_cmd(0x08 /* set display mode */
1028 | ((lcd.flags & LCD_FLAG_D) ? 4 : 0)
1029 | ((lcd.flags & LCD_FLAG_C) ? 2 : 0)
1030 | ((lcd.flags & LCD_FLAG_B) ? 1 : 0)
1033 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
1037 /* entry mode set : increment, cursor shifting */
1038 lcd_write_cmd(0x06);
1040 lcd_clear_display();
1044 * These are the file operation function for user access to /dev/lcd
1045 * This function can also be called from inside the kernel, by
1046 * setting file and ppos to NULL.
1050 static inline int handle_lcd_special_code(void)
1052 /* LCD special codes */
1056 char *esc = lcd.esc_seq.buf + 2;
1057 int oldflags = lcd.flags;
1059 /* check for display mode flags */
1061 case 'D': /* Display ON */
1062 lcd.flags |= LCD_FLAG_D;
1065 case 'd': /* Display OFF */
1066 lcd.flags &= ~LCD_FLAG_D;
1069 case 'C': /* Cursor ON */
1070 lcd.flags |= LCD_FLAG_C;
1073 case 'c': /* Cursor OFF */
1074 lcd.flags &= ~LCD_FLAG_C;
1077 case 'B': /* Blink ON */
1078 lcd.flags |= LCD_FLAG_B;
1081 case 'b': /* Blink OFF */
1082 lcd.flags &= ~LCD_FLAG_B;
1085 case '+': /* Back light ON */
1086 lcd.flags |= LCD_FLAG_L;
1089 case '-': /* Back light OFF */
1090 lcd.flags &= ~LCD_FLAG_L;
1094 /* flash back light using the keypad timer */
1095 if (scan_timer.function != NULL) {
1096 if (lcd.light_tempo == 0
1097 && ((lcd.flags & LCD_FLAG_L) == 0))
1099 lcd.light_tempo = FLASH_LIGHT_TEMPO;
1103 case 'f': /* Small Font */
1104 lcd.flags &= ~LCD_FLAG_F;
1107 case 'F': /* Large Font */
1108 lcd.flags |= LCD_FLAG_F;
1111 case 'n': /* One Line */
1112 lcd.flags &= ~LCD_FLAG_N;
1115 case 'N': /* Two Lines */
1116 lcd.flags |= LCD_FLAG_N;
1118 case 'l': /* Shift Cursor Left */
1119 if (lcd.addr.x > 0) {
1120 /* back one char if not at end of line */
1121 if (lcd.addr.x < lcd.bwidth)
1122 lcd_write_cmd(0x10);
1127 case 'r': /* shift cursor right */
1128 if (lcd.addr.x < lcd.width) {
1129 /* allow the cursor to pass the end of the line */
1132 lcd_write_cmd(0x14);
1137 case 'L': /* shift display left */
1139 lcd_write_cmd(0x18);
1142 case 'R': /* shift display right */
1144 lcd_write_cmd(0x1C);
1147 case 'k': { /* kill end of line */
1150 for (x = lcd.addr.x; x < lcd.bwidth; x++)
1151 lcd_write_data(' ');
1153 /* restore cursor position */
1158 case 'I': /* reinitialize display */
1164 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1165 * and '7', representing the numerical ASCII code of the
1166 * redefined character, and <xx...xx> a sequence of 16
1167 * hex digits representing 8 bytes for each character.
1168 * Most LCDs will only use 5 lower bits of the 7 first
1172 unsigned char cgbytes[8];
1173 unsigned char cgaddr;
1179 if (strchr(esc, ';') == NULL)
1184 cgaddr = *(esc++) - '0';
1193 while (*esc && cgoffset < 8) {
1195 if (*esc >= '0' && *esc <= '9') {
1196 value |= (*esc - '0') << shift;
1197 } else if (*esc >= 'A' && *esc <= 'Z') {
1198 value |= (*esc - 'A' + 10) << shift;
1199 } else if (*esc >= 'a' && *esc <= 'z') {
1200 value |= (*esc - 'a' + 10) << shift;
1207 cgbytes[cgoffset++] = value;
1214 lcd_write_cmd(0x40 | (cgaddr * 8));
1215 for (addr = 0; addr < cgoffset; addr++)
1216 lcd_write_data(cgbytes[addr]);
1218 /* ensures that we stop writing to CGRAM */
1223 case 'x': /* gotoxy : LxXXX[yYYY]; */
1224 case 'y': /* gotoxy : LyYYY[xXXX]; */
1225 if (strchr(esc, ';') == NULL)
1231 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
1233 } else if (*esc == 'y') {
1235 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
1247 /* Check whether one flag was changed */
1248 if (oldflags != lcd.flags) {
1249 /* check whether one of B,C,D flags were changed */
1250 if ((oldflags ^ lcd.flags) &
1251 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1252 /* set display mode */
1254 | ((lcd.flags & LCD_FLAG_D) ? 4 : 0)
1255 | ((lcd.flags & LCD_FLAG_C) ? 2 : 0)
1256 | ((lcd.flags & LCD_FLAG_B) ? 1 : 0));
1257 /* check whether one of F,N flags was changed */
1258 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
1260 | ((lcd.flags & LCD_FLAG_F) ? 4 : 0)
1261 | ((lcd.flags & LCD_FLAG_N) ? 8 : 0));
1262 /* check whether L flag was changed */
1263 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1264 if (lcd.flags & (LCD_FLAG_L))
1266 else if (lcd.light_tempo == 0)
1267 /* switch off the light only when the tempo
1276 static void lcd_write_char(char c)
1278 /* first, we'll test if we're in escape mode */
1279 if ((c != '\n') && lcd.esc_seq.len >= 0) {
1280 /* yes, let's add this char to the buffer */
1281 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1282 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
1284 /* aborts any previous escape sequence */
1285 lcd.esc_seq.len = -1;
1288 case LCD_ESCAPE_CHAR:
1289 /* start of an escape sequence */
1290 lcd.esc_seq.len = 0;
1291 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
1294 /* go back one char and clear it */
1295 if (lcd.addr.x > 0) {
1296 /* check if we're not at the
1298 if (lcd.addr.x < lcd.bwidth)
1300 lcd_write_cmd(0x10);
1303 /* replace with a space */
1304 lcd_write_data(' ');
1305 /* back one char again */
1306 lcd_write_cmd(0x10);
1309 /* quickly clear the display */
1313 /* flush the remainder of the current line and
1314 go to the beginning of the next line */
1315 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
1316 lcd_write_data(' ');
1318 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
1322 /* go to the beginning of the same line */
1327 /* print a space instead of the tab */
1331 /* simply print this char */
1337 /* now we'll see if we're in an escape mode and if the current
1338 escape sequence can be understood. */
1339 if (lcd.esc_seq.len >= 2) {
1342 if (!strcmp(lcd.esc_seq.buf, "[2J")) {
1343 /* clear the display */
1346 } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
1347 /* cursor to home */
1353 /* codes starting with ^[[L */
1354 else if ((lcd.esc_seq.len >= 3) &&
1355 (lcd.esc_seq.buf[0] == '[') &&
1356 (lcd.esc_seq.buf[1] == 'L')) {
1357 processed = handle_lcd_special_code();
1360 /* LCD special escape codes */
1361 /* flush the escape sequence if it's been processed
1362 or if it is getting too long. */
1363 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1364 lcd.esc_seq.len = -1;
1365 } /* escape codes */
1368 static ssize_t lcd_write(struct file *file,
1369 const char __user *buf, size_t count, loff_t *ppos)
1371 const char __user *tmp = buf;
1374 for (; count-- > 0; (*ppos)++, tmp++) {
1375 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
1376 /* let's be a little nice with other processes
1377 that need some CPU */
1380 if (get_user(c, tmp))
1389 static int lcd_open(struct inode *inode, struct file *file)
1391 if (!atomic_dec_and_test(&lcd_available))
1392 return -EBUSY; /* open only once at a time */
1394 if (file->f_mode & FMODE_READ) /* device is write-only */
1397 if (lcd.must_clear) {
1398 lcd_clear_display();
1399 lcd.must_clear = false;
1401 return nonseekable_open(inode, file);
1404 static int lcd_release(struct inode *inode, struct file *file)
1406 atomic_inc(&lcd_available);
1410 static const struct file_operations lcd_fops = {
1413 .release = lcd_release,
1414 .llseek = no_llseek,
1417 static struct miscdevice lcd_dev = {
1423 /* public function usable from the kernel for any purpose */
1424 static void panel_lcd_print(const char *s)
1426 const char *tmp = s;
1427 int count = strlen(s);
1429 if (lcd.enabled && lcd.initialized) {
1430 for (; count-- > 0; tmp++) {
1431 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
1432 /* let's be a little nice with other processes
1433 that need some CPU */
1436 lcd_write_char(*tmp);
1441 /* initialize the LCD driver */
1442 static void lcd_init(void)
1444 switch (selected_lcd_type) {
1446 /* parallel mode, 8 bits */
1447 lcd.proto = LCD_PROTO_PARALLEL;
1448 lcd.charset = LCD_CHARSET_NORMAL;
1449 lcd.pins.e = PIN_STROBE;
1450 lcd.pins.rs = PIN_AUTOLF;
1457 case LCD_TYPE_KS0074:
1458 /* serial mode, ks0074 */
1459 lcd.proto = LCD_PROTO_SERIAL;
1460 lcd.charset = LCD_CHARSET_KS0074;
1461 lcd.pins.bl = PIN_AUTOLF;
1462 lcd.pins.cl = PIN_STROBE;
1463 lcd.pins.da = PIN_D0;
1470 case LCD_TYPE_NEXCOM:
1471 /* parallel mode, 8 bits, generic */
1472 lcd.proto = LCD_PROTO_PARALLEL;
1473 lcd.charset = LCD_CHARSET_NORMAL;
1474 lcd.pins.e = PIN_AUTOLF;
1475 lcd.pins.rs = PIN_SELECP;
1476 lcd.pins.rw = PIN_INITP;
1483 case LCD_TYPE_CUSTOM:
1484 /* customer-defined */
1485 lcd.proto = DEFAULT_LCD_PROTO;
1486 lcd.charset = DEFAULT_LCD_CHARSET;
1487 /* default geometry will be set later */
1489 case LCD_TYPE_HANTRONIX:
1490 /* parallel mode, 8 bits, hantronix-like */
1492 lcd.proto = LCD_PROTO_PARALLEL;
1493 lcd.charset = LCD_CHARSET_NORMAL;
1494 lcd.pins.e = PIN_STROBE;
1495 lcd.pins.rs = PIN_SELECP;
1504 /* Overwrite with module params set on loading */
1505 if (lcd_height != NOT_SET)
1506 lcd.height = lcd_height;
1507 if (lcd_width != NOT_SET)
1508 lcd.width = lcd_width;
1509 if (lcd_bwidth != NOT_SET)
1510 lcd.bwidth = lcd_bwidth;
1511 if (lcd_hwidth != NOT_SET)
1512 lcd.hwidth = lcd_hwidth;
1513 if (lcd_charset != NOT_SET)
1514 lcd.charset = lcd_charset;
1515 if (lcd_proto != NOT_SET)
1516 lcd.proto = lcd_proto;
1517 if (lcd_e_pin != PIN_NOT_SET)
1518 lcd.pins.e = lcd_e_pin;
1519 if (lcd_rs_pin != PIN_NOT_SET)
1520 lcd.pins.rs = lcd_rs_pin;
1521 if (lcd_rw_pin != PIN_NOT_SET)
1522 lcd.pins.rw = lcd_rw_pin;
1523 if (lcd_cl_pin != PIN_NOT_SET)
1524 lcd.pins.cl = lcd_cl_pin;
1525 if (lcd_da_pin != PIN_NOT_SET)
1526 lcd.pins.da = lcd_da_pin;
1527 if (lcd_bl_pin != PIN_NOT_SET)
1528 lcd.pins.bl = lcd_bl_pin;
1530 /* this is used to catch wrong and default values */
1532 lcd.width = DEFAULT_LCD_WIDTH;
1533 if (lcd.bwidth <= 0)
1534 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1535 if (lcd.hwidth <= 0)
1536 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1537 if (lcd.height <= 0)
1538 lcd.height = DEFAULT_LCD_HEIGHT;
1540 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
1541 lcd_write_cmd = lcd_write_cmd_s;
1542 lcd_write_data = lcd_write_data_s;
1543 lcd_clear_fast = lcd_clear_fast_s;
1545 if (lcd.pins.cl == PIN_NOT_SET)
1546 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1547 if (lcd.pins.da == PIN_NOT_SET)
1548 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
1550 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
1551 lcd_write_cmd = lcd_write_cmd_p8;
1552 lcd_write_data = lcd_write_data_p8;
1553 lcd_clear_fast = lcd_clear_fast_p8;
1555 if (lcd.pins.e == PIN_NOT_SET)
1556 lcd.pins.e = DEFAULT_LCD_PIN_E;
1557 if (lcd.pins.rs == PIN_NOT_SET)
1558 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1559 if (lcd.pins.rw == PIN_NOT_SET)
1560 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
1562 lcd_write_cmd = lcd_write_cmd_tilcd;
1563 lcd_write_data = lcd_write_data_tilcd;
1564 lcd_clear_fast = lcd_clear_fast_tilcd;
1567 if (lcd.pins.bl == PIN_NOT_SET)
1568 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
1570 if (lcd.pins.e == PIN_NOT_SET)
1571 lcd.pins.e = PIN_NONE;
1572 if (lcd.pins.rs == PIN_NOT_SET)
1573 lcd.pins.rs = PIN_NONE;
1574 if (lcd.pins.rw == PIN_NOT_SET)
1575 lcd.pins.rw = PIN_NONE;
1576 if (lcd.pins.bl == PIN_NOT_SET)
1577 lcd.pins.bl = PIN_NONE;
1578 if (lcd.pins.cl == PIN_NOT_SET)
1579 lcd.pins.cl = PIN_NONE;
1580 if (lcd.pins.da == PIN_NOT_SET)
1581 lcd.pins.da = PIN_NONE;
1583 if (lcd.charset == NOT_SET)
1584 lcd.charset = DEFAULT_LCD_CHARSET;
1586 if (lcd.charset == LCD_CHARSET_KS0074)
1587 lcd_char_conv = lcd_char_conv_ks0074;
1589 lcd_char_conv = NULL;
1591 if (lcd.pins.bl != PIN_NONE)
1594 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1595 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1596 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1597 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1598 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1599 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1600 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1601 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1602 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1603 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1604 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1605 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
1607 /* before this line, we must NOT send anything to the display.
1608 * Since lcd_init_display() needs to write data, we have to
1609 * enable mark the LCD initialized just before. */
1610 lcd.initialized = true;
1613 /* display a short message */
1614 #ifdef CONFIG_PANEL_CHANGE_MESSAGE
1615 #ifdef CONFIG_PANEL_BOOT_MESSAGE
1616 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
1619 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1624 /* clear the display on the next device opening */
1625 lcd.must_clear = true;
1630 * These are the file operation function for user access to /dev/keypad
1633 static ssize_t keypad_read(struct file *file,
1634 char __user *buf, size_t count, loff_t *ppos)
1637 char __user *tmp = buf;
1639 if (keypad_buflen == 0) {
1640 if (file->f_flags & O_NONBLOCK)
1643 if (wait_event_interruptible(keypad_read_wait,
1644 keypad_buflen != 0))
1648 for (; count-- > 0 && (keypad_buflen > 0);
1649 ++i, ++tmp, --keypad_buflen) {
1650 put_user(keypad_buffer[keypad_start], tmp);
1651 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1658 static int keypad_open(struct inode *inode, struct file *file)
1660 if (!atomic_dec_and_test(&keypad_available))
1661 return -EBUSY; /* open only once at a time */
1663 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1666 keypad_buflen = 0; /* flush the buffer on opening */
1670 static int keypad_release(struct inode *inode, struct file *file)
1672 atomic_inc(&keypad_available);
1676 static const struct file_operations keypad_fops = {
1677 .read = keypad_read, /* read */
1678 .open = keypad_open, /* open */
1679 .release = keypad_release, /* close */
1680 .llseek = default_llseek,
1683 static struct miscdevice keypad_dev = {
1684 .minor = KEYPAD_MINOR,
1686 .fops = &keypad_fops,
1689 static void keypad_send_key(const char *string, int max_len)
1691 if (init_in_progress)
1694 /* send the key to the device only if a process is attached to it. */
1695 if (!atomic_read(&keypad_available)) {
1696 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1697 keypad_buffer[(keypad_start + keypad_buflen++) %
1698 KEYPAD_BUFFER] = *string++;
1700 wake_up_interruptible(&keypad_read_wait);
1704 /* this function scans all the bits involving at least one logical signal,
1705 * and puts the results in the bitfield "phys_read" (one bit per established
1706 * contact), and sets "phys_read_prev" to "phys_read".
1708 * Note: to debounce input signals, we will only consider as switched a signal
1709 * which is stable across 2 measures. Signals which are different between two
1710 * reads will be kept as they previously were in their logical form (phys_prev).
1711 * A signal which has just switched will have a 1 in
1712 * (phys_read ^ phys_read_prev).
1714 static void phys_scan_contacts(void)
1721 phys_prev = phys_curr;
1722 phys_read_prev = phys_read;
1723 phys_read = 0; /* flush all signals */
1725 /* keep track of old value, with all outputs disabled */
1726 oldval = r_dtr(pprt) | scan_mask_o;
1727 /* activate all keyboard outputs (active low) */
1728 w_dtr(pprt, oldval & ~scan_mask_o);
1730 /* will have a 1 for each bit set to gnd */
1731 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1732 /* disable all matrix signals */
1733 w_dtr(pprt, oldval);
1735 /* now that all outputs are cleared, the only active input bits are
1736 * directly connected to the ground
1739 /* 1 for each grounded input */
1740 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1742 /* grounded inputs are signals 40-44 */
1743 phys_read |= (pmask_t) gndmask << 40;
1745 if (bitmask != gndmask) {
1746 /* since clearing the outputs changed some inputs, we know
1747 * that some input signals are currently tied to some outputs.
1748 * So we'll scan them.
1750 for (bit = 0; bit < 8; bit++) {
1753 if (!(scan_mask_o & bitval)) /* skip unused bits */
1756 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1757 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1758 phys_read |= (pmask_t) bitmask << (5 * bit);
1760 w_dtr(pprt, oldval); /* disable all outputs */
1762 /* this is easy: use old bits when they are flapping,
1763 * use new ones when stable */
1764 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1765 (phys_read & ~(phys_read ^ phys_read_prev));
1768 static inline int input_state_high(struct logical_input *input)
1772 * this is an invalid test. It tries to catch
1773 * transitions from single-key to multiple-key, but
1774 * doesn't take into account the contacts polarity.
1775 * The only solution to the problem is to parse keys
1776 * from the most complex to the simplest combinations,
1777 * and mark them as 'caught' once a combination
1778 * matches, then unmatch it for all other ones.
1781 /* try to catch dangerous transitions cases :
1782 * someone adds a bit, so this signal was a false
1783 * positive resulting from a transition. We should
1784 * invalidate the signal immediately and not call the
1786 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1788 if (((phys_prev & input->mask) == input->value) &&
1789 ((phys_curr & input->mask) > input->value)) {
1790 input->state = INPUT_ST_LOW; /* invalidate */
1795 if ((phys_curr & input->mask) == input->value) {
1796 if ((input->type == INPUT_TYPE_STD) &&
1797 (input->high_timer == 0)) {
1798 input->high_timer++;
1799 if (input->u.std.press_fct != NULL)
1800 input->u.std.press_fct(input->u.std.press_data);
1801 } else if (input->type == INPUT_TYPE_KBD) {
1802 /* will turn on the light */
1805 if (input->high_timer == 0) {
1806 char *press_str = input->u.kbd.press_str;
1809 int s = sizeof(input->u.kbd.press_str);
1811 keypad_send_key(press_str, s);
1815 if (input->u.kbd.repeat_str[0]) {
1816 char *repeat_str = input->u.kbd.repeat_str;
1818 if (input->high_timer >= KEYPAD_REP_START) {
1819 int s = sizeof(input->u.kbd.repeat_str);
1821 input->high_timer -= KEYPAD_REP_DELAY;
1822 keypad_send_key(repeat_str, s);
1824 /* we will need to come back here soon */
1828 if (input->high_timer < 255)
1829 input->high_timer++;
1834 /* else signal falling down. Let's fall through. */
1835 input->state = INPUT_ST_FALLING;
1836 input->fall_timer = 0;
1841 static inline void input_state_falling(struct logical_input *input)
1844 /* FIXME !!! same comment as in input_state_high */
1845 if (((phys_prev & input->mask) == input->value) &&
1846 ((phys_curr & input->mask) > input->value)) {
1847 input->state = INPUT_ST_LOW; /* invalidate */
1852 if ((phys_curr & input->mask) == input->value) {
1853 if (input->type == INPUT_TYPE_KBD) {
1854 /* will turn on the light */
1857 if (input->u.kbd.repeat_str[0]) {
1858 char *repeat_str = input->u.kbd.repeat_str;
1860 if (input->high_timer >= KEYPAD_REP_START) {
1861 int s = sizeof(input->u.kbd.repeat_str);
1863 input->high_timer -= KEYPAD_REP_DELAY;
1864 keypad_send_key(repeat_str, s);
1866 /* we will need to come back here soon */
1870 if (input->high_timer < 255)
1871 input->high_timer++;
1873 input->state = INPUT_ST_HIGH;
1874 } else if (input->fall_timer >= input->fall_time) {
1875 /* call release event */
1876 if (input->type == INPUT_TYPE_STD) {
1877 void (*release_fct)(int) = input->u.std.release_fct;
1879 if (release_fct != NULL)
1880 release_fct(input->u.std.release_data);
1881 } else if (input->type == INPUT_TYPE_KBD) {
1882 char *release_str = input->u.kbd.release_str;
1884 if (release_str[0]) {
1885 int s = sizeof(input->u.kbd.release_str);
1887 keypad_send_key(release_str, s);
1891 input->state = INPUT_ST_LOW;
1893 input->fall_timer++;
1898 static void panel_process_inputs(void)
1900 struct list_head *item;
1901 struct logical_input *input;
1905 list_for_each(item, &logical_inputs) {
1906 input = list_entry(item, struct logical_input, list);
1908 switch (input->state) {
1910 if ((phys_curr & input->mask) != input->value)
1912 /* if all needed ones were already set previously,
1913 * this means that this logical signal has been
1914 * activated by the releasing of another combined
1915 * signal, so we don't want to match.
1916 * eg: AB -(release B)-> A -(release A)-> 0 :
1919 if ((phys_prev & input->mask) == input->value)
1921 input->rise_timer = 0;
1922 input->state = INPUT_ST_RISING;
1923 /* no break here, fall through */
1924 case INPUT_ST_RISING:
1925 if ((phys_curr & input->mask) != input->value) {
1926 input->state = INPUT_ST_LOW;
1929 if (input->rise_timer < input->rise_time) {
1931 input->rise_timer++;
1934 input->high_timer = 0;
1935 input->state = INPUT_ST_HIGH;
1936 /* no break here, fall through */
1938 if (input_state_high(input))
1940 /* no break here, fall through */
1941 case INPUT_ST_FALLING:
1942 input_state_falling(input);
1947 static void panel_scan_timer(void)
1949 if (keypad.enabled && keypad_initialized) {
1950 if (spin_trylock_irq(&pprt_lock)) {
1951 phys_scan_contacts();
1953 /* no need for the parport anymore */
1954 spin_unlock_irq(&pprt_lock);
1957 if (!inputs_stable || phys_curr != phys_prev)
1958 panel_process_inputs();
1961 if (lcd.enabled && lcd.initialized) {
1963 if (lcd.light_tempo == 0
1964 && ((lcd.flags & LCD_FLAG_L) == 0))
1966 lcd.light_tempo = FLASH_LIGHT_TEMPO;
1967 } else if (lcd.light_tempo > 0) {
1969 if (lcd.light_tempo == 0
1970 && ((lcd.flags & LCD_FLAG_L) == 0))
1975 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
1978 static void init_scan_timer(void)
1980 if (scan_timer.function != NULL)
1981 return; /* already started */
1983 init_timer(&scan_timer);
1984 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1985 scan_timer.data = 0;
1986 scan_timer.function = (void *)&panel_scan_timer;
1987 add_timer(&scan_timer);
1990 /* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1991 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1992 * corresponding to out and in bits respectively.
1993 * returns 1 if ok, 0 if error (in which case, nothing is written).
1995 static int input_name2mask(const char *name, pmask_t *mask, pmask_t *value,
1996 char *imask, char *omask)
1998 static char sigtab[10] = "EeSsPpAaBb";
2007 int in, out, bit, neg;
2009 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name);
2013 if (in >= sizeof(sigtab))
2014 return 0; /* input name not found */
2015 neg = (in & 1); /* odd (lower) names are negated */
2020 if (isdigit(*name)) {
2023 } else if (*name == '-') {
2026 return 0; /* unknown bit name */
2029 bit = (out * 5) + in;
2045 /* tries to bind a key to the signal name <name>. The key will send the
2046 * strings <press>, <repeat>, <release> for these respective events.
2047 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2049 static struct logical_input *panel_bind_key(const char *name, const char *press,
2051 const char *release)
2053 struct logical_input *key;
2055 key = kzalloc(sizeof(*key), GFP_KERNEL);
2059 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
2065 key->type = INPUT_TYPE_KBD;
2066 key->state = INPUT_ST_LOW;
2070 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2071 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2072 strncpy(key->u.kbd.release_str, release,
2073 sizeof(key->u.kbd.release_str));
2074 list_add(&key->list, &logical_inputs);
2079 /* tries to bind a callback function to the signal name <name>. The function
2080 * <press_fct> will be called with the <press_data> arg when the signal is
2081 * activated, and so on for <release_fct>/<release_data>
2082 * Returns the pointer to the new signal if ok, NULL if the signal could not
2085 static struct logical_input *panel_bind_callback(char *name,
2086 void (*press_fct)(int),
2088 void (*release_fct)(int),
2091 struct logical_input *callback;
2093 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
2097 memset(callback, 0, sizeof(struct logical_input));
2098 if (!input_name2mask(name, &callback->mask, &callback->value,
2099 &scan_mask_i, &scan_mask_o))
2102 callback->type = INPUT_TYPE_STD;
2103 callback->state = INPUT_ST_LOW;
2104 callback->rise_time = 1;
2105 callback->fall_time = 1;
2106 callback->u.std.press_fct = press_fct;
2107 callback->u.std.press_data = press_data;
2108 callback->u.std.release_fct = release_fct;
2109 callback->u.std.release_data = release_data;
2110 list_add(&callback->list, &logical_inputs);
2115 static void keypad_init(void)
2119 init_waitqueue_head(&keypad_read_wait);
2120 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
2122 /* Let's create all known keys */
2124 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2125 panel_bind_key(keypad_profile[keynum][0],
2126 keypad_profile[keynum][1],
2127 keypad_profile[keynum][2],
2128 keypad_profile[keynum][3]);
2132 keypad_initialized = 1;
2135 /**************************************************/
2136 /* device initialization */
2137 /**************************************************/
2139 static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2142 if (lcd.enabled && lcd.initialized) {
2146 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2150 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2153 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2162 static struct notifier_block panel_notifier = {
2168 static void panel_attach(struct parport *port)
2170 if (port->number != parport)
2174 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2175 __func__, port->number, parport);
2179 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
2181 /*PARPORT_DEV_EXCL */
2184 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2185 __func__, port->number, parport);
2189 if (parport_claim(pprt)) {
2190 pr_err("could not claim access to parport%d. Aborting.\n",
2192 goto err_unreg_device;
2195 /* must init LCD first, just in case an IRQ from the keypad is
2196 * generated at keypad init
2200 if (misc_register(&lcd_dev))
2201 goto err_unreg_device;
2204 if (keypad.enabled) {
2206 if (misc_register(&keypad_dev))
2213 misc_deregister(&lcd_dev);
2215 parport_unregister_device(pprt);
2219 static void panel_detach(struct parport *port)
2221 if (port->number != parport)
2225 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2226 __func__, port->number, parport);
2230 if (keypad.enabled && keypad_initialized) {
2231 misc_deregister(&keypad_dev);
2232 keypad_initialized = 0;
2235 if (lcd.enabled && lcd.initialized) {
2236 misc_deregister(&lcd_dev);
2237 lcd.initialized = false;
2240 parport_release(pprt);
2241 parport_unregister_device(pprt);
2245 static struct parport_driver panel_driver = {
2247 .attach = panel_attach,
2248 .detach = panel_detach,
2252 static int __init panel_init_module(void)
2254 int selected_keypad_type = NOT_SET;
2256 /* take care of an eventual profile */
2258 case PANEL_PROFILE_CUSTOM:
2259 /* custom profile */
2260 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2261 selected_lcd_type = DEFAULT_LCD_TYPE;
2263 case PANEL_PROFILE_OLD:
2264 /* 8 bits, 2*16, old keypad */
2265 selected_keypad_type = KEYPAD_TYPE_OLD;
2266 selected_lcd_type = LCD_TYPE_OLD;
2268 /* TODO: This two are a little hacky, sort it out later */
2269 if (lcd_width == NOT_SET)
2271 if (lcd_hwidth == NOT_SET)
2274 case PANEL_PROFILE_NEW:
2275 /* serial, 2*16, new keypad */
2276 selected_keypad_type = KEYPAD_TYPE_NEW;
2277 selected_lcd_type = LCD_TYPE_KS0074;
2279 case PANEL_PROFILE_HANTRONIX:
2280 /* 8 bits, 2*16 hantronix-like, no keypad */
2281 selected_keypad_type = KEYPAD_TYPE_NONE;
2282 selected_lcd_type = LCD_TYPE_HANTRONIX;
2284 case PANEL_PROFILE_NEXCOM:
2285 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
2286 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2287 selected_lcd_type = LCD_TYPE_NEXCOM;
2289 case PANEL_PROFILE_LARGE:
2290 /* 8 bits, 2*40, old keypad */
2291 selected_keypad_type = KEYPAD_TYPE_OLD;
2292 selected_lcd_type = LCD_TYPE_OLD;
2297 * Init lcd struct with load-time values to preserve exact current
2298 * functionality (at least for now).
2300 lcd.height = lcd_height;
2301 lcd.width = lcd_width;
2302 lcd.bwidth = lcd_bwidth;
2303 lcd.hwidth = lcd_hwidth;
2304 lcd.charset = lcd_charset;
2305 lcd.proto = lcd_proto;
2306 lcd.pins.e = lcd_e_pin;
2307 lcd.pins.rs = lcd_rs_pin;
2308 lcd.pins.rw = lcd_rw_pin;
2309 lcd.pins.cl = lcd_cl_pin;
2310 lcd.pins.da = lcd_da_pin;
2311 lcd.pins.bl = lcd_bl_pin;
2313 /* Leave it for now, just in case */
2314 lcd.esc_seq.len = -1;
2317 * Overwrite selection with module param values (both keypad and lcd),
2318 * where the deprecated params have lower prio.
2320 if (keypad_enabled != NOT_SET)
2321 selected_keypad_type = keypad_enabled;
2322 if (keypad_type != NOT_SET)
2323 selected_keypad_type = keypad_type;
2325 keypad.enabled = (selected_keypad_type > 0);
2327 if (lcd_enabled != NOT_SET)
2328 selected_lcd_type = lcd_enabled;
2329 if (lcd_type != NOT_SET)
2330 selected_lcd_type = lcd_type;
2332 lcd.enabled = (selected_lcd_type > 0);
2334 switch (selected_keypad_type) {
2335 case KEYPAD_TYPE_OLD:
2336 keypad_profile = old_keypad_profile;
2338 case KEYPAD_TYPE_NEW:
2339 keypad_profile = new_keypad_profile;
2341 case KEYPAD_TYPE_NEXCOM:
2342 keypad_profile = nexcom_keypad_profile;
2345 keypad_profile = NULL;
2349 /* tells various subsystems about the fact that we are initializing */
2350 init_in_progress = 1;
2352 if (parport_register_driver(&panel_driver)) {
2353 pr_err("could not register with parport. Aborting.\n");
2357 if (!lcd.enabled && !keypad.enabled) {
2358 /* no device enabled, let's release the parport */
2360 parport_release(pprt);
2361 parport_unregister_device(pprt);
2364 parport_unregister_driver(&panel_driver);
2365 pr_err("driver version " PANEL_VERSION " disabled.\n");
2369 register_reboot_notifier(&panel_notifier);
2372 pr_info("driver version " PANEL_VERSION
2373 " registered on parport%d (io=0x%lx).\n", parport,
2376 pr_info("driver version " PANEL_VERSION
2377 " not yet registered\n");
2378 /* tells various subsystems about the fact that initialization
2380 init_in_progress = 0;
2384 static void __exit panel_cleanup_module(void)
2386 unregister_reboot_notifier(&panel_notifier);
2388 if (scan_timer.function != NULL)
2389 del_timer_sync(&scan_timer);
2392 if (keypad.enabled) {
2393 misc_deregister(&keypad_dev);
2394 keypad_initialized = 0;
2398 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2399 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2400 misc_deregister(&lcd_dev);
2401 lcd.initialized = false;
2404 /* TODO: free all input signals */
2405 parport_release(pprt);
2406 parport_unregister_device(pprt);
2409 parport_unregister_driver(&panel_driver);
2412 module_init(panel_init_module);
2413 module_exit(panel_cleanup_module);
2414 MODULE_AUTHOR("Willy Tarreau");
2415 MODULE_LICENSE("GPL");