3 #include <linux/module.h>
5 #include <linux/types.h>
6 #include <linux/errno.h>
7 #include <linux/signal.h>
8 #include <linux/sched.h>
9 #include <linux/spinlock.h>
10 #include <linux/interrupt.h>
11 #include <linux/miscdevice.h>
12 #include <linux/slab.h>
13 #include <linux/ioport.h>
14 #include <linux/fcntl.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/ctype.h>
19 #include <linux/parport.h>
20 #include <linux/version.h>
21 #include <linux/list.h>
22 #include <linux/notifier.h>
23 #include <linux/reboot.h>
24 #include <generated/utsrelease.h>
27 #include <linux/uaccess.h>
28 #include <asm/system.h>
31 #define KEYPAD_MINOR 185
33 #define PANEL_VERSION "0.9.5"
35 #define LCD_MAXBYTES 256 /* max burst write */
37 #define KEYPAD_BUFFER 64
39 /* poll the keyboard this every second */
40 #define INPUT_POLL_TIME (HZ/50)
41 /* a key starts to repeat after this times INPUT_POLL_TIME */
42 #define KEYPAD_REP_START (10)
43 /* a key repeats this times INPUT_POLL_TIME */
44 #define KEYPAD_REP_DELAY (2)
46 /* keep the light on this times INPUT_POLL_TIME for each flash */
47 #define FLASH_LIGHT_TEMPO (200)
49 /* converts an r_str() input to an active high, bits string : 000BAOSE */
50 #define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
52 #define PNL_PBUSY 0x80 /* inverted input, active low */
53 #define PNL_PACK 0x40 /* direct input, active low */
54 #define PNL_POUTPA 0x20 /* direct input, active high */
55 #define PNL_PSELECD 0x10 /* direct input, active high */
56 #define PNL_PERRORP 0x08 /* direct input, active low */
58 #define PNL_PBIDIR 0x20 /* bi-directional ports */
59 /* high to read data in or-ed with data out */
60 #define PNL_PINTEN 0x10
61 #define PNL_PSELECP 0x08 /* inverted output, active low */
62 #define PNL_PINITP 0x04 /* direct output, active low */
63 #define PNL_PAUTOLF 0x02 /* inverted output, active low */
64 #define PNL_PSTROBE 0x01 /* inverted output */
88 #define PIN_NOT_SET 127
90 #define LCD_FLAG_S 0x0001
91 #define LCD_FLAG_ID 0x0002
92 #define LCD_FLAG_B 0x0004 /* blink on */
93 #define LCD_FLAG_C 0x0008 /* cursor on */
94 #define LCD_FLAG_D 0x0010 /* display on */
95 #define LCD_FLAG_F 0x0020 /* large font mode */
96 #define LCD_FLAG_N 0x0040 /* 2-rows mode */
97 #define LCD_FLAG_L 0x0080 /* backlight enabled */
99 #define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
100 #define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
102 /* macros to simplify use of the parallel port */
103 #define r_ctr(x) (parport_read_control((x)->port))
104 #define r_dtr(x) (parport_read_data((x)->port))
105 #define r_str(x) (parport_read_status((x)->port))
106 #define w_ctr(x, y) do { parport_write_control((x)->port, (y)); } while (0)
107 #define w_dtr(x, y) do { parport_write_data((x)->port, (y)); } while (0)
109 /* this defines which bits are to be used and which ones to be ignored */
110 /* logical or of the output bits involved in the scan matrix */
111 static __u8 scan_mask_o
;
112 /* logical or of the input bits involved in the scan matrix */
113 static __u8 scan_mask_i
;
115 typedef __u64 pmask_t
;
129 struct logical_input
{
130 struct list_head list
;
133 enum input_type type
;
134 enum input_state state
;
135 __u8 rise_time
, fall_time
;
136 __u8 rise_timer
, fall_timer
, high_timer
;
139 struct { /* valid when type == INPUT_TYPE_STD */
140 void (*press_fct
) (int);
141 void (*release_fct
) (int);
145 struct { /* valid when type == INPUT_TYPE_KBD */
146 /* strings can be non null-terminated */
147 char press_str
[sizeof(void *) + sizeof(int)];
148 char repeat_str
[sizeof(void *) + sizeof(int)];
149 char release_str
[sizeof(void *) + sizeof(int)];
154 LIST_HEAD(logical_inputs
); /* list of all defined logical inputs */
156 /* physical contacts history
157 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
158 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
159 * corresponds to the ground.
160 * Within each group, bits are stored in the same order as read on the port :
161 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
162 * So, each __u64 (or pmask_t) is represented like this :
163 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
164 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
167 /* what has just been read from the I/O ports */
168 static pmask_t phys_read
;
169 /* previous phys_read */
170 static pmask_t phys_read_prev
;
171 /* stabilized phys_read (phys_read|phys_read_prev) */
172 static pmask_t phys_curr
;
173 /* previous phys_curr */
174 static pmask_t phys_prev
;
175 /* 0 means that at least one logical signal needs be computed */
176 static char inputs_stable
;
178 /* these variables are specific to the keypad */
179 static char keypad_buffer
[KEYPAD_BUFFER
];
180 static int keypad_buflen
;
181 static int keypad_start
;
182 static char keypressed
;
183 static wait_queue_head_t keypad_read_wait
;
185 /* lcd-specific variables */
187 /* contains the LCD config state */
188 static unsigned long int lcd_flags
;
189 /* contains the LCD X offset */
190 static unsigned long int lcd_addr_x
;
191 /* contains the LCD Y offset */
192 static unsigned long int lcd_addr_y
;
193 /* current escape sequence, 0 terminated */
194 static char lcd_escape
[LCD_ESCAPE_LEN
+ 1];
195 /* not in escape state. >=0 = escape cmd len */
196 static int lcd_escape_len
= -1;
199 * Bit masks to convert LCD signals to parallel port outputs.
200 * _d_ are values for data port, _c_ are for control port.
201 * [0] = signal OFF, [1] = signal ON, [2] = mask
208 * one entry for each bit on the LCD
219 * each bit can be either connected to a DATA or CTRL port
225 static unsigned char lcd_bits
[LCD_PORTS
][LCD_BITS
][BIT_STATES
];
230 #define LCD_PROTO_PARALLEL 0
231 #define LCD_PROTO_SERIAL 1
232 #define LCD_PROTO_TI_DA8XX_LCD 2
237 #define LCD_CHARSET_NORMAL 0
238 #define LCD_CHARSET_KS0074 1
243 #define LCD_TYPE_NONE 0
244 #define LCD_TYPE_OLD 1
245 #define LCD_TYPE_KS0074 2
246 #define LCD_TYPE_HANTRONIX 3
247 #define LCD_TYPE_NEXCOM 4
248 #define LCD_TYPE_CUSTOM 5
253 #define KEYPAD_TYPE_NONE 0
254 #define KEYPAD_TYPE_OLD 1
255 #define KEYPAD_TYPE_NEW 2
256 #define KEYPAD_TYPE_NEXCOM 3
261 #define PANEL_PROFILE_CUSTOM 0
262 #define PANEL_PROFILE_OLD 1
263 #define PANEL_PROFILE_NEW 2
264 #define PANEL_PROFILE_HANTRONIX 3
265 #define PANEL_PROFILE_NEXCOM 4
266 #define PANEL_PROFILE_LARGE 5
269 * Construct custom config from the kernel's configuration
271 #define DEFAULT_PROFILE PANEL_PROFILE_LARGE
272 #define DEFAULT_PARPORT 0
273 #define DEFAULT_LCD LCD_TYPE_OLD
274 #define DEFAULT_KEYPAD KEYPAD_TYPE_OLD
275 #define DEFAULT_LCD_WIDTH 40
276 #define DEFAULT_LCD_BWIDTH 40
277 #define DEFAULT_LCD_HWIDTH 64
278 #define DEFAULT_LCD_HEIGHT 2
279 #define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
281 #define DEFAULT_LCD_PIN_E PIN_AUTOLF
282 #define DEFAULT_LCD_PIN_RS PIN_SELECP
283 #define DEFAULT_LCD_PIN_RW PIN_INITP
284 #define DEFAULT_LCD_PIN_SCL PIN_STROBE
285 #define DEFAULT_LCD_PIN_SDA PIN_D0
286 #define DEFAULT_LCD_PIN_BL PIN_NOT_SET
287 #define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
289 #ifdef CONFIG_PANEL_PROFILE
290 #undef DEFAULT_PROFILE
291 #define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
294 #ifdef CONFIG_PANEL_PARPORT
295 #undef DEFAULT_PARPORT
296 #define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
299 #if DEFAULT_PROFILE == 0 /* custom */
300 #ifdef CONFIG_PANEL_KEYPAD
301 #undef DEFAULT_KEYPAD
302 #define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
305 #ifdef CONFIG_PANEL_LCD
307 #define DEFAULT_LCD CONFIG_PANEL_LCD
310 #ifdef CONFIG_PANEL_LCD_WIDTH
311 #undef DEFAULT_LCD_WIDTH
312 #define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
315 #ifdef CONFIG_PANEL_LCD_BWIDTH
316 #undef DEFAULT_LCD_BWIDTH
317 #define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
320 #ifdef CONFIG_PANEL_LCD_HWIDTH
321 #undef DEFAULT_LCD_HWIDTH
322 #define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
325 #ifdef CONFIG_PANEL_LCD_HEIGHT
326 #undef DEFAULT_LCD_HEIGHT
327 #define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
330 #ifdef CONFIG_PANEL_LCD_PROTO
331 #undef DEFAULT_LCD_PROTO
332 #define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
335 #ifdef CONFIG_PANEL_LCD_PIN_E
336 #undef DEFAULT_LCD_PIN_E
337 #define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
340 #ifdef CONFIG_PANEL_LCD_PIN_RS
341 #undef DEFAULT_LCD_PIN_RS
342 #define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
345 #ifdef CONFIG_PANEL_LCD_PIN_RW
346 #undef DEFAULT_LCD_PIN_RW
347 #define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
350 #ifdef CONFIG_PANEL_LCD_PIN_SCL
351 #undef DEFAULT_LCD_PIN_SCL
352 #define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
355 #ifdef CONFIG_PANEL_LCD_PIN_SDA
356 #undef DEFAULT_LCD_PIN_SDA
357 #define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
360 #ifdef CONFIG_PANEL_LCD_PIN_BL
361 #undef DEFAULT_LCD_PIN_BL
362 #define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
365 #ifdef CONFIG_PANEL_LCD_CHARSET
366 #undef DEFAULT_LCD_CHARSET
367 #define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
370 #endif /* DEFAULT_PROFILE == 0 */
372 /* global variables */
373 static int keypad_open_cnt
; /* #times opened */
374 static int lcd_open_cnt
; /* #times opened */
375 static struct pardevice
*pprt
;
377 static int lcd_initialized
;
378 static int keypad_initialized
;
380 static int light_tempo
;
382 static char lcd_must_clear
;
383 static char lcd_left_shift
;
384 static char init_in_progress
;
386 static void (*lcd_write_cmd
) (int);
387 static void (*lcd_write_data
) (int);
388 static void (*lcd_clear_fast
) (void);
390 static DEFINE_SPINLOCK(pprt_lock
);
391 static struct timer_list scan_timer
;
393 MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
395 static int parport
= -1;
396 module_param(parport
, int, 0000);
397 MODULE_PARM_DESC(parport
, "Parallel port index (0=lpt1, 1=lpt2, ...)");
399 static int lcd_height
= -1;
400 module_param(lcd_height
, int, 0000);
401 MODULE_PARM_DESC(lcd_height
, "Number of lines on the LCD");
403 static int lcd_width
= -1;
404 module_param(lcd_width
, int, 0000);
405 MODULE_PARM_DESC(lcd_width
, "Number of columns on the LCD");
407 static int lcd_bwidth
= -1; /* internal buffer width (usually 40) */
408 module_param(lcd_bwidth
, int, 0000);
409 MODULE_PARM_DESC(lcd_bwidth
, "Internal LCD line width (40)");
411 static int lcd_hwidth
= -1; /* hardware buffer width (usually 64) */
412 module_param(lcd_hwidth
, int, 0000);
413 MODULE_PARM_DESC(lcd_hwidth
, "LCD line hardware address (64)");
415 static int lcd_enabled
= -1;
416 module_param(lcd_enabled
, int, 0000);
417 MODULE_PARM_DESC(lcd_enabled
, "Deprecated option, use lcd_type instead");
419 static int keypad_enabled
= -1;
420 module_param(keypad_enabled
, int, 0000);
421 MODULE_PARM_DESC(keypad_enabled
, "Deprecated option, use keypad_type instead");
423 static int lcd_type
= -1;
424 module_param(lcd_type
, int, 0000);
425 MODULE_PARM_DESC(lcd_type
,
426 "LCD type: 0=none, 1=old //, 2=serial ks0074, "
427 "3=hantronix //, 4=nexcom //, 5=compiled-in");
429 static int lcd_proto
= -1;
430 module_param(lcd_proto
, int, 0000);
431 MODULE_PARM_DESC(lcd_proto
,
432 "LCD communication: 0=parallel (//), 1=serial,"
433 "2=TI LCD Interface");
435 static int lcd_charset
= -1;
436 module_param(lcd_charset
, int, 0000);
437 MODULE_PARM_DESC(lcd_charset
, "LCD character set: 0=standard, 1=KS0074");
439 static int keypad_type
= -1;
440 module_param(keypad_type
, int, 0000);
441 MODULE_PARM_DESC(keypad_type
,
442 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, "
445 static int profile
= DEFAULT_PROFILE
;
446 module_param(profile
, int, 0000);
447 MODULE_PARM_DESC(profile
,
448 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
449 "4=16x2 nexcom; default=40x2, old kp");
452 * These are the parallel port pins the LCD control signals are connected to.
453 * Set this to 0 if the signal is not used. Set it to its opposite value
454 * (negative) if the signal is negated. -MAXINT is used to indicate that the
455 * pin has not been explicitly specified.
457 * WARNING! no check will be performed about collisions with keypad !
460 static int lcd_e_pin
= PIN_NOT_SET
;
461 module_param(lcd_e_pin
, int, 0000);
462 MODULE_PARM_DESC(lcd_e_pin
,
463 "# of the // port pin connected to LCD 'E' signal, "
464 "with polarity (-17..17)");
466 static int lcd_rs_pin
= PIN_NOT_SET
;
467 module_param(lcd_rs_pin
, int, 0000);
468 MODULE_PARM_DESC(lcd_rs_pin
,
469 "# of the // port pin connected to LCD 'RS' signal, "
470 "with polarity (-17..17)");
472 static int lcd_rw_pin
= PIN_NOT_SET
;
473 module_param(lcd_rw_pin
, int, 0000);
474 MODULE_PARM_DESC(lcd_rw_pin
,
475 "# of the // port pin connected to LCD 'RW' signal, "
476 "with polarity (-17..17)");
478 static int lcd_bl_pin
= PIN_NOT_SET
;
479 module_param(lcd_bl_pin
, int, 0000);
480 MODULE_PARM_DESC(lcd_bl_pin
,
481 "# of the // port pin connected to LCD backlight, "
482 "with polarity (-17..17)");
484 static int lcd_da_pin
= PIN_NOT_SET
;
485 module_param(lcd_da_pin
, int, 0000);
486 MODULE_PARM_DESC(lcd_da_pin
,
487 "# of the // port pin connected to serial LCD 'SDA' "
488 "signal, with polarity (-17..17)");
490 static int lcd_cl_pin
= PIN_NOT_SET
;
491 module_param(lcd_cl_pin
, int, 0000);
492 MODULE_PARM_DESC(lcd_cl_pin
,
493 "# of the // port pin connected to serial LCD 'SCL' "
494 "signal, with polarity (-17..17)");
496 static unsigned char *lcd_char_conv
;
498 /* for some LCD drivers (ks0074) we need a charset conversion table. */
499 static unsigned char lcd_char_conv_ks0074
[256] = {
500 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
501 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
502 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
503 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
504 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
505 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
506 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
507 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
508 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
509 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
510 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
511 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
512 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
513 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
514 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
515 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
516 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
517 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
518 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
519 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
520 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
521 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
522 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
523 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
524 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
525 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
526 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
527 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
528 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
529 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
530 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
531 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
532 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
535 char old_keypad_profile
[][4][9] = {
536 {"S0", "Left\n", "Left\n", ""},
537 {"S1", "Down\n", "Down\n", ""},
538 {"S2", "Up\n", "Up\n", ""},
539 {"S3", "Right\n", "Right\n", ""},
540 {"S4", "Esc\n", "Esc\n", ""},
541 {"S5", "Ret\n", "Ret\n", ""},
545 /* signals, press, repeat, release */
546 char new_keypad_profile
[][4][9] = {
547 {"S0", "Left\n", "Left\n", ""},
548 {"S1", "Down\n", "Down\n", ""},
549 {"S2", "Up\n", "Up\n", ""},
550 {"S3", "Right\n", "Right\n", ""},
551 {"S4s5", "", "Esc\n", "Esc\n"},
552 {"s4S5", "", "Ret\n", "Ret\n"},
553 {"S4S5", "Help\n", "", ""},
554 /* add new signals above this line */
558 /* signals, press, repeat, release */
559 char nexcom_keypad_profile
[][4][9] = {
560 {"a-p-e-", "Down\n", "Down\n", ""},
561 {"a-p-E-", "Ret\n", "Ret\n", ""},
562 {"a-P-E-", "Esc\n", "Esc\n", ""},
563 {"a-P-e-", "Up\n", "Up\n", ""},
564 /* add new signals above this line */
568 static char (*keypad_profile
)[4][9] = old_keypad_profile
;
571 unsigned char e
; /* parallel LCD E (data latch on falling edge) */
572 unsigned char rs
; /* parallel LCD RS (0 = cmd, 1 = data) */
573 unsigned char rw
; /* parallel LCD R/W (0 = W, 1 = R) */
574 unsigned char bl
; /* parallel LCD backlight (0 = off, 1 = on) */
575 unsigned char cl
; /* serial LCD clock (latch on rising edge) */
576 unsigned char da
; /* serial LCD data */
579 static void init_scan_timer(void);
581 /* sets data port bits according to current signals values */
582 static int set_data_bits(void)
587 for (bit
= 0; bit
< LCD_BITS
; bit
++)
588 val
&= lcd_bits
[LCD_PORT_D
][bit
][BIT_MSK
];
590 val
|= lcd_bits
[LCD_PORT_D
][LCD_BIT_E
][bits
.e
]
591 | lcd_bits
[LCD_PORT_D
][LCD_BIT_RS
][bits
.rs
]
592 | lcd_bits
[LCD_PORT_D
][LCD_BIT_RW
][bits
.rw
]
593 | lcd_bits
[LCD_PORT_D
][LCD_BIT_BL
][bits
.bl
]
594 | lcd_bits
[LCD_PORT_D
][LCD_BIT_CL
][bits
.cl
]
595 | lcd_bits
[LCD_PORT_D
][LCD_BIT_DA
][bits
.da
];
601 /* sets ctrl port bits according to current signals values */
602 static int set_ctrl_bits(void)
607 for (bit
= 0; bit
< LCD_BITS
; bit
++)
608 val
&= lcd_bits
[LCD_PORT_C
][bit
][BIT_MSK
];
610 val
|= lcd_bits
[LCD_PORT_C
][LCD_BIT_E
][bits
.e
]
611 | lcd_bits
[LCD_PORT_C
][LCD_BIT_RS
][bits
.rs
]
612 | lcd_bits
[LCD_PORT_C
][LCD_BIT_RW
][bits
.rw
]
613 | lcd_bits
[LCD_PORT_C
][LCD_BIT_BL
][bits
.bl
]
614 | lcd_bits
[LCD_PORT_C
][LCD_BIT_CL
][bits
.cl
]
615 | lcd_bits
[LCD_PORT_C
][LCD_BIT_DA
][bits
.da
];
621 /* sets ctrl & data port bits according to current signals values */
622 static void panel_set_bits(void)
629 * Converts a parallel port pin (from -25 to 25) to data and control ports
630 * masks, and data and control port bits. The signal will be considered
631 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
633 * Result will be used this way :
634 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
635 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
637 void pin_to_bits(int pin
, unsigned char *d_val
, unsigned char *c_val
)
639 int d_bit
, c_bit
, inv
;
641 d_val
[0] = c_val
[0] = d_val
[1] = c_val
[1] = 0;
642 d_val
[2] = c_val
[2] = 0xFF;
654 case PIN_STROBE
: /* strobe, inverted */
658 case PIN_D0
...PIN_D7
: /* D0 - D7 = 2 - 9 */
659 d_bit
= 1 << (pin
- 2);
661 case PIN_AUTOLF
: /* autofeed, inverted */
665 case PIN_INITP
: /* init, direct */
668 case PIN_SELECP
: /* select_in, inverted */
672 default: /* unknown pin, ignore */
685 /* sleeps that many milliseconds with a reschedule */
686 static void long_sleep(int ms
)
692 current
->state
= TASK_INTERRUPTIBLE
;
693 schedule_timeout((ms
* HZ
+ 999) / 1000);
697 /* send a serial byte to the LCD panel. The caller is responsible for locking
699 static void lcd_send_serial(int byte
)
703 /* the data bit is set on D0, and the clock on STROBE.
704 * LCD reads D0 on STROBE's rising edge. */
705 for (bit
= 0; bit
< 8; bit
++) {
706 bits
.cl
= BIT_CLR
; /* CLK low */
710 udelay(2); /* maintain the data during 2 us before CLK up */
711 bits
.cl
= BIT_SET
; /* CLK high */
713 udelay(1); /* maintain the strobe during 1 us */
718 /* turn the backlight on or off */
719 static void lcd_backlight(int on
)
721 if (lcd_bl_pin
== PIN_NONE
)
724 /* The backlight is activated by seting the AUTOFEED line to +5V */
725 spin_lock(&pprt_lock
);
728 spin_unlock(&pprt_lock
);
731 /* send a command to the LCD panel in serial mode */
732 static void lcd_write_cmd_s(int cmd
)
734 spin_lock(&pprt_lock
);
735 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
736 lcd_send_serial(cmd
& 0x0F);
737 lcd_send_serial((cmd
>> 4) & 0x0F);
738 udelay(40); /* the shortest command takes at least 40 us */
739 spin_unlock(&pprt_lock
);
742 /* send data to the LCD panel in serial mode */
743 static void lcd_write_data_s(int data
)
745 spin_lock(&pprt_lock
);
746 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
747 lcd_send_serial(data
& 0x0F);
748 lcd_send_serial((data
>> 4) & 0x0F);
749 udelay(40); /* the shortest data takes at least 40 us */
750 spin_unlock(&pprt_lock
);
753 /* send a command to the LCD panel in 8 bits parallel mode */
754 static void lcd_write_cmd_p8(int cmd
)
756 spin_lock(&pprt_lock
);
757 /* present the data to the data port */
759 udelay(20); /* maintain the data during 20 us before the strobe */
766 udelay(40); /* maintain the strobe during 40 us */
771 udelay(120); /* the shortest command takes at least 120 us */
772 spin_unlock(&pprt_lock
);
775 /* send data to the LCD panel in 8 bits parallel mode */
776 static void lcd_write_data_p8(int data
)
778 spin_lock(&pprt_lock
);
779 /* present the data to the data port */
781 udelay(20); /* maintain the data during 20 us before the strobe */
788 udelay(40); /* maintain the strobe during 40 us */
793 udelay(45); /* the shortest data takes at least 45 us */
794 spin_unlock(&pprt_lock
);
797 /* send a command to the TI LCD panel */
798 static void lcd_write_cmd_tilcd(int cmd
)
800 spin_lock(&pprt_lock
);
801 /* present the data to the control port */
804 spin_unlock(&pprt_lock
);
807 /* send data to the TI LCD panel */
808 static void lcd_write_data_tilcd(int data
)
810 spin_lock(&pprt_lock
);
811 /* present the data to the data port */
814 spin_unlock(&pprt_lock
);
817 static void lcd_gotoxy(void)
819 lcd_write_cmd(0x80 /* set DDRAM address */
820 | (lcd_addr_y
? lcd_hwidth
: 0)
821 /* we force the cursor to stay at the end of the
822 line if it wants to go farther */
823 | ((lcd_addr_x
< lcd_bwidth
) ? lcd_addr_x
&
824 (lcd_hwidth
- 1) : lcd_bwidth
- 1));
827 static void lcd_print(char c
)
829 if (lcd_addr_x
< lcd_bwidth
) {
830 if (lcd_char_conv
!= NULL
)
831 c
= lcd_char_conv
[(unsigned char)c
];
835 /* prevents the cursor from wrapping onto the next line */
836 if (lcd_addr_x
== lcd_bwidth
)
840 /* fills the display with spaces and resets X/Y */
841 static void lcd_clear_fast_s(void)
844 lcd_addr_x
= lcd_addr_y
= 0;
847 spin_lock(&pprt_lock
);
848 for (pos
= 0; pos
< lcd_height
* lcd_hwidth
; pos
++) {
849 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
850 lcd_send_serial(' ' & 0x0F);
851 lcd_send_serial((' ' >> 4) & 0x0F);
852 udelay(40); /* the shortest data takes at least 40 us */
854 spin_unlock(&pprt_lock
);
856 lcd_addr_x
= lcd_addr_y
= 0;
860 /* fills the display with spaces and resets X/Y */
861 static void lcd_clear_fast_p8(void)
864 lcd_addr_x
= lcd_addr_y
= 0;
867 spin_lock(&pprt_lock
);
868 for (pos
= 0; pos
< lcd_height
* lcd_hwidth
; pos
++) {
869 /* present the data to the data port */
872 /* maintain the data during 20 us before the strobe */
880 /* maintain the strobe during 40 us */
886 /* the shortest data takes at least 45 us */
889 spin_unlock(&pprt_lock
);
891 lcd_addr_x
= lcd_addr_y
= 0;
895 /* fills the display with spaces and resets X/Y */
896 static void lcd_clear_fast_tilcd(void)
899 lcd_addr_x
= lcd_addr_y
= 0;
902 spin_lock(&pprt_lock
);
903 for (pos
= 0; pos
< lcd_height
* lcd_hwidth
; pos
++) {
904 /* present the data to the data port */
909 spin_unlock(&pprt_lock
);
911 lcd_addr_x
= lcd_addr_y
= 0;
915 /* clears the display and resets X/Y */
916 static void lcd_clear_display(void)
918 lcd_write_cmd(0x01); /* clear display */
919 lcd_addr_x
= lcd_addr_y
= 0;
920 /* we must wait a few milliseconds (15) */
924 static void lcd_init_display(void)
927 lcd_flags
= ((lcd_height
> 1) ? LCD_FLAG_N
: 0)
928 | LCD_FLAG_D
| LCD_FLAG_C
| LCD_FLAG_B
;
930 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
932 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
934 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
936 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
939 lcd_write_cmd(0x30 /* set font height and lines number */
940 | ((lcd_flags
& LCD_FLAG_F
) ? 4 : 0)
941 | ((lcd_flags
& LCD_FLAG_N
) ? 8 : 0)
945 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
948 lcd_write_cmd(0x08 /* set display mode */
949 | ((lcd_flags
& LCD_FLAG_D
) ? 4 : 0)
950 | ((lcd_flags
& LCD_FLAG_C
) ? 2 : 0)
951 | ((lcd_flags
& LCD_FLAG_B
) ? 1 : 0)
954 lcd_backlight((lcd_flags
& LCD_FLAG_L
) ? 1 : 0);
958 /* entry mode set : increment, cursor shifting */
965 * These are the file operation function for user access to /dev/lcd
966 * This function can also be called from inside the kernel, by
967 * setting file and ppos to NULL.
971 static inline int handle_lcd_special_code(void)
973 /* LCD special codes */
977 char *esc
= lcd_escape
+ 2;
978 int oldflags
= lcd_flags
;
980 /* check for display mode flags */
982 case 'D': /* Display ON */
983 lcd_flags
|= LCD_FLAG_D
;
986 case 'd': /* Display OFF */
987 lcd_flags
&= ~LCD_FLAG_D
;
990 case 'C': /* Cursor ON */
991 lcd_flags
|= LCD_FLAG_C
;
994 case 'c': /* Cursor OFF */
995 lcd_flags
&= ~LCD_FLAG_C
;
998 case 'B': /* Blink ON */
999 lcd_flags
|= LCD_FLAG_B
;
1002 case 'b': /* Blink OFF */
1003 lcd_flags
&= ~LCD_FLAG_B
;
1006 case '+': /* Back light ON */
1007 lcd_flags
|= LCD_FLAG_L
;
1010 case '-': /* Back light OFF */
1011 lcd_flags
&= ~LCD_FLAG_L
;
1015 /* flash back light using the keypad timer */
1016 if (scan_timer
.function
!= NULL
) {
1017 if (light_tempo
== 0 && ((lcd_flags
& LCD_FLAG_L
) == 0))
1019 light_tempo
= FLASH_LIGHT_TEMPO
;
1023 case 'f': /* Small Font */
1024 lcd_flags
&= ~LCD_FLAG_F
;
1027 case 'F': /* Large Font */
1028 lcd_flags
|= LCD_FLAG_F
;
1031 case 'n': /* One Line */
1032 lcd_flags
&= ~LCD_FLAG_N
;
1035 case 'N': /* Two Lines */
1036 lcd_flags
|= LCD_FLAG_N
;
1038 case 'l': /* Shift Cursor Left */
1039 if (lcd_addr_x
> 0) {
1040 /* back one char if not at end of line */
1041 if (lcd_addr_x
< lcd_bwidth
)
1042 lcd_write_cmd(0x10);
1047 case 'r': /* shift cursor right */
1048 if (lcd_addr_x
< lcd_width
) {
1049 /* allow the cursor to pass the end of the line */
1052 lcd_write_cmd(0x14);
1057 case 'L': /* shift display left */
1059 lcd_write_cmd(0x18);
1062 case 'R': /* shift display right */
1064 lcd_write_cmd(0x1C);
1067 case 'k': { /* kill end of line */
1069 for (x
= lcd_addr_x
; x
< lcd_bwidth
; x
++)
1070 lcd_write_data(' ');
1072 /* restore cursor position */
1077 case 'I': /* reinitialize display */
1083 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1084 * and '7', representing the numerical ASCII code of the
1085 * redefined character, and <xx...xx> a sequence of 16
1086 * hex digits representing 8 bytes for each character.
1087 * Most LCDs will only use 5 lower bits of the 7 first
1091 unsigned char cgbytes
[8];
1092 unsigned char cgaddr
;
1098 if (strchr(esc
, ';') == NULL
)
1103 cgaddr
= *(esc
++) - '0';
1112 while (*esc
&& cgoffset
< 8) {
1114 if (*esc
>= '0' && *esc
<= '9')
1115 value
|= (*esc
- '0') << shift
;
1116 else if (*esc
>= 'A' && *esc
<= 'Z')
1117 value
|= (*esc
- 'A' + 10) << shift
;
1118 else if (*esc
>= 'a' && *esc
<= 'z')
1119 value
|= (*esc
- 'a' + 10) << shift
;
1126 cgbytes
[cgoffset
++] = value
;
1133 lcd_write_cmd(0x40 | (cgaddr
* 8));
1134 for (addr
= 0; addr
< cgoffset
; addr
++)
1135 lcd_write_data(cgbytes
[addr
]);
1137 /* ensures that we stop writing to CGRAM */
1142 case 'x': /* gotoxy : LxXXX[yYYY]; */
1144 if (strchr(esc
, ';') == NULL
)
1152 lcd_addr_x
= simple_strtoul(esc
, &endp
, 10);
1154 } else if (*esc
== 'y') {
1156 lcd_addr_y
= simple_strtoul(esc
, &endp
, 10);
1167 /* Check wether one flag was changed */
1168 if (oldflags
!= lcd_flags
) {
1169 /* check whether one of B,C,D flags were changed */
1170 if ((oldflags
^ lcd_flags
) &
1171 (LCD_FLAG_B
| LCD_FLAG_C
| LCD_FLAG_D
))
1172 /* set display mode */
1174 | ((lcd_flags
& LCD_FLAG_D
) ? 4 : 0)
1175 | ((lcd_flags
& LCD_FLAG_C
) ? 2 : 0)
1176 | ((lcd_flags
& LCD_FLAG_B
) ? 1 : 0));
1177 /* check whether one of F,N flags was changed */
1178 else if ((oldflags
^ lcd_flags
) & (LCD_FLAG_F
| LCD_FLAG_N
))
1180 | ((lcd_flags
& LCD_FLAG_F
) ? 4 : 0)
1181 | ((lcd_flags
& LCD_FLAG_N
) ? 8 : 0));
1182 /* check wether L flag was changed */
1183 else if ((oldflags
^ lcd_flags
) & (LCD_FLAG_L
)) {
1184 if (lcd_flags
& (LCD_FLAG_L
))
1186 else if (light_tempo
== 0)
1187 /* switch off the light only when the tempo
1196 static ssize_t
lcd_write(struct file
*file
,
1197 const char *buf
, size_t count
, loff_t
*ppos
)
1199 const char *tmp
= buf
;
1202 for (; count
-- > 0; (ppos
? (*ppos
)++ : 0), ++tmp
) {
1203 if (!in_interrupt() && (((count
+ 1) & 0x1f) == 0))
1204 /* let's be a little nice with other processes
1205 that need some CPU */
1208 if (ppos
== NULL
&& file
== NULL
)
1209 /* let's not use get_user() from the kernel ! */
1211 else if (get_user(c
, tmp
))
1214 /* first, we'll test if we're in escape mode */
1215 if ((c
!= '\n') && lcd_escape_len
>= 0) {
1216 /* yes, let's add this char to the buffer */
1217 lcd_escape
[lcd_escape_len
++] = c
;
1218 lcd_escape
[lcd_escape_len
] = 0;
1220 /* aborts any previous escape sequence */
1221 lcd_escape_len
= -1;
1224 case LCD_ESCAPE_CHAR
:
1225 /* start of an escape sequence */
1227 lcd_escape
[lcd_escape_len
] = 0;
1230 /* go back one char and clear it */
1231 if (lcd_addr_x
> 0) {
1232 /* check if we're not at the
1234 if (lcd_addr_x
< lcd_bwidth
)
1236 lcd_write_cmd(0x10);
1239 /* replace with a space */
1240 lcd_write_data(' ');
1241 /* back one char again */
1242 lcd_write_cmd(0x10);
1245 /* quickly clear the display */
1249 /* flush the remainder of the current line and
1250 go to the beginning of the next line */
1251 for (; lcd_addr_x
< lcd_bwidth
; lcd_addr_x
++)
1252 lcd_write_data(' ');
1254 lcd_addr_y
= (lcd_addr_y
+ 1) % lcd_height
;
1258 /* go to the beginning of the same line */
1263 /* print a space instead of the tab */
1267 /* simply print this char */
1273 /* now we'll see if we're in an escape mode and if the current
1274 escape sequence can be understood. */
1275 if (lcd_escape_len
>= 2) {
1278 if (!strcmp(lcd_escape
, "[2J")) {
1279 /* clear the display */
1282 } else if (!strcmp(lcd_escape
, "[H")) {
1283 /* cursor to home */
1284 lcd_addr_x
= lcd_addr_y
= 0;
1288 /* codes starting with ^[[L */
1289 else if ((lcd_escape_len
>= 3) &&
1290 (lcd_escape
[0] == '[') &&
1291 (lcd_escape
[1] == 'L')) {
1292 processed
= handle_lcd_special_code();
1295 /* LCD special escape codes */
1296 /* flush the escape sequence if it's been processed
1297 or if it is getting too long. */
1298 if (processed
|| (lcd_escape_len
>= LCD_ESCAPE_LEN
))
1299 lcd_escape_len
= -1;
1300 } /* escape codes */
1306 static int lcd_open(struct inode
*inode
, struct file
*file
)
1309 return -EBUSY
; /* open only once at a time */
1311 if (file
->f_mode
& FMODE_READ
) /* device is write-only */
1314 if (lcd_must_clear
) {
1315 lcd_clear_display();
1319 return nonseekable_open(inode
, file
);
1322 static int lcd_release(struct inode
*inode
, struct file
*file
)
1328 static const struct file_operations lcd_fops
= {
1331 .release
= lcd_release
,
1332 .llseek
= no_llseek
,
1335 static struct miscdevice lcd_dev
= {
1341 /* public function usable from the kernel for any purpose */
1342 void panel_lcd_print(char *s
)
1344 if (lcd_enabled
&& lcd_initialized
)
1345 lcd_write(NULL
, s
, strlen(s
), NULL
);
1348 /* initialize the LCD driver */
1353 /* parallel mode, 8 bits */
1355 lcd_proto
= LCD_PROTO_PARALLEL
;
1356 if (lcd_charset
< 0)
1357 lcd_charset
= LCD_CHARSET_NORMAL
;
1358 if (lcd_e_pin
== PIN_NOT_SET
)
1359 lcd_e_pin
= PIN_STROBE
;
1360 if (lcd_rs_pin
== PIN_NOT_SET
)
1361 lcd_rs_pin
= PIN_AUTOLF
;
1372 case LCD_TYPE_KS0074
:
1373 /* serial mode, ks0074 */
1375 lcd_proto
= LCD_PROTO_SERIAL
;
1376 if (lcd_charset
< 0)
1377 lcd_charset
= LCD_CHARSET_KS0074
;
1378 if (lcd_bl_pin
== PIN_NOT_SET
)
1379 lcd_bl_pin
= PIN_AUTOLF
;
1380 if (lcd_cl_pin
== PIN_NOT_SET
)
1381 lcd_cl_pin
= PIN_STROBE
;
1382 if (lcd_da_pin
== PIN_NOT_SET
)
1383 lcd_da_pin
= PIN_D0
;
1394 case LCD_TYPE_NEXCOM
:
1395 /* parallel mode, 8 bits, generic */
1397 lcd_proto
= LCD_PROTO_PARALLEL
;
1398 if (lcd_charset
< 0)
1399 lcd_charset
= LCD_CHARSET_NORMAL
;
1400 if (lcd_e_pin
== PIN_NOT_SET
)
1401 lcd_e_pin
= PIN_AUTOLF
;
1402 if (lcd_rs_pin
== PIN_NOT_SET
)
1403 lcd_rs_pin
= PIN_SELECP
;
1404 if (lcd_rw_pin
== PIN_NOT_SET
)
1405 lcd_rw_pin
= PIN_INITP
;
1416 case LCD_TYPE_CUSTOM
:
1417 /* customer-defined */
1419 lcd_proto
= DEFAULT_LCD_PROTO
;
1420 if (lcd_charset
< 0)
1421 lcd_charset
= DEFAULT_LCD_CHARSET
;
1422 /* default geometry will be set later */
1424 case LCD_TYPE_HANTRONIX
:
1425 /* parallel mode, 8 bits, hantronix-like */
1428 lcd_proto
= LCD_PROTO_PARALLEL
;
1429 if (lcd_charset
< 0)
1430 lcd_charset
= LCD_CHARSET_NORMAL
;
1431 if (lcd_e_pin
== PIN_NOT_SET
)
1432 lcd_e_pin
= PIN_STROBE
;
1433 if (lcd_rs_pin
== PIN_NOT_SET
)
1434 lcd_rs_pin
= PIN_SELECP
;
1447 /* this is used to catch wrong and default values */
1449 lcd_width
= DEFAULT_LCD_WIDTH
;
1450 if (lcd_bwidth
<= 0)
1451 lcd_bwidth
= DEFAULT_LCD_BWIDTH
;
1452 if (lcd_hwidth
<= 0)
1453 lcd_hwidth
= DEFAULT_LCD_HWIDTH
;
1454 if (lcd_height
<= 0)
1455 lcd_height
= DEFAULT_LCD_HEIGHT
;
1457 if (lcd_proto
== LCD_PROTO_SERIAL
) { /* SERIAL */
1458 lcd_write_cmd
= lcd_write_cmd_s
;
1459 lcd_write_data
= lcd_write_data_s
;
1460 lcd_clear_fast
= lcd_clear_fast_s
;
1462 if (lcd_cl_pin
== PIN_NOT_SET
)
1463 lcd_cl_pin
= DEFAULT_LCD_PIN_SCL
;
1464 if (lcd_da_pin
== PIN_NOT_SET
)
1465 lcd_da_pin
= DEFAULT_LCD_PIN_SDA
;
1467 } else if (lcd_proto
== LCD_PROTO_PARALLEL
) { /* PARALLEL */
1468 lcd_write_cmd
= lcd_write_cmd_p8
;
1469 lcd_write_data
= lcd_write_data_p8
;
1470 lcd_clear_fast
= lcd_clear_fast_p8
;
1472 if (lcd_e_pin
== PIN_NOT_SET
)
1473 lcd_e_pin
= DEFAULT_LCD_PIN_E
;
1474 if (lcd_rs_pin
== PIN_NOT_SET
)
1475 lcd_rs_pin
= DEFAULT_LCD_PIN_RS
;
1476 if (lcd_rw_pin
== PIN_NOT_SET
)
1477 lcd_rw_pin
= DEFAULT_LCD_PIN_RW
;
1479 lcd_write_cmd
= lcd_write_cmd_tilcd
;
1480 lcd_write_data
= lcd_write_data_tilcd
;
1481 lcd_clear_fast
= lcd_clear_fast_tilcd
;
1484 if (lcd_bl_pin
== PIN_NOT_SET
)
1485 lcd_bl_pin
= DEFAULT_LCD_PIN_BL
;
1487 if (lcd_e_pin
== PIN_NOT_SET
)
1488 lcd_e_pin
= PIN_NONE
;
1489 if (lcd_rs_pin
== PIN_NOT_SET
)
1490 lcd_rs_pin
= PIN_NONE
;
1491 if (lcd_rw_pin
== PIN_NOT_SET
)
1492 lcd_rw_pin
= PIN_NONE
;
1493 if (lcd_bl_pin
== PIN_NOT_SET
)
1494 lcd_bl_pin
= PIN_NONE
;
1495 if (lcd_cl_pin
== PIN_NOT_SET
)
1496 lcd_cl_pin
= PIN_NONE
;
1497 if (lcd_da_pin
== PIN_NOT_SET
)
1498 lcd_da_pin
= PIN_NONE
;
1500 if (lcd_charset
< 0)
1501 lcd_charset
= DEFAULT_LCD_CHARSET
;
1503 if (lcd_charset
== LCD_CHARSET_KS0074
)
1504 lcd_char_conv
= lcd_char_conv_ks0074
;
1506 lcd_char_conv
= NULL
;
1508 if (lcd_bl_pin
!= PIN_NONE
)
1511 pin_to_bits(lcd_e_pin
, lcd_bits
[LCD_PORT_D
][LCD_BIT_E
],
1512 lcd_bits
[LCD_PORT_C
][LCD_BIT_E
]);
1513 pin_to_bits(lcd_rs_pin
, lcd_bits
[LCD_PORT_D
][LCD_BIT_RS
],
1514 lcd_bits
[LCD_PORT_C
][LCD_BIT_RS
]);
1515 pin_to_bits(lcd_rw_pin
, lcd_bits
[LCD_PORT_D
][LCD_BIT_RW
],
1516 lcd_bits
[LCD_PORT_C
][LCD_BIT_RW
]);
1517 pin_to_bits(lcd_bl_pin
, lcd_bits
[LCD_PORT_D
][LCD_BIT_BL
],
1518 lcd_bits
[LCD_PORT_C
][LCD_BIT_BL
]);
1519 pin_to_bits(lcd_cl_pin
, lcd_bits
[LCD_PORT_D
][LCD_BIT_CL
],
1520 lcd_bits
[LCD_PORT_C
][LCD_BIT_CL
]);
1521 pin_to_bits(lcd_da_pin
, lcd_bits
[LCD_PORT_D
][LCD_BIT_DA
],
1522 lcd_bits
[LCD_PORT_C
][LCD_BIT_DA
]);
1524 /* before this line, we must NOT send anything to the display.
1525 * Since lcd_init_display() needs to write data, we have to
1526 * enable mark the LCD initialized just before. */
1527 lcd_initialized
= 1;
1530 /* display a short message */
1531 #ifdef CONFIG_PANEL_CHANGE_MESSAGE
1532 #ifdef CONFIG_PANEL_BOOT_MESSAGE
1533 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE
);
1536 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE
"\nPanel-"
1539 lcd_addr_x
= lcd_addr_y
= 0;
1540 /* clear the display on the next device opening */
1546 * These are the file operation function for user access to /dev/keypad
1549 static ssize_t
keypad_read(struct file
*file
,
1550 char *buf
, size_t count
, loff_t
*ppos
)
1556 if (keypad_buflen
== 0) {
1557 if (file
->f_flags
& O_NONBLOCK
)
1560 interruptible_sleep_on(&keypad_read_wait
);
1561 if (signal_pending(current
))
1565 for (; count
-- > 0 && (keypad_buflen
> 0);
1566 ++i
, ++tmp
, --keypad_buflen
) {
1567 put_user(keypad_buffer
[keypad_start
], tmp
);
1568 keypad_start
= (keypad_start
+ 1) % KEYPAD_BUFFER
;
1575 static int keypad_open(struct inode
*inode
, struct file
*file
)
1578 if (keypad_open_cnt
)
1579 return -EBUSY
; /* open only once at a time */
1581 if (file
->f_mode
& FMODE_WRITE
) /* device is read-only */
1584 keypad_buflen
= 0; /* flush the buffer on opening */
1589 static int keypad_release(struct inode
*inode
, struct file
*file
)
1595 static const struct file_operations keypad_fops
= {
1596 .read
= keypad_read
, /* read */
1597 .open
= keypad_open
, /* open */
1598 .release
= keypad_release
, /* close */
1601 static struct miscdevice keypad_dev
= {
1607 static void keypad_send_key(char *string
, int max_len
)
1609 if (init_in_progress
)
1612 /* send the key to the device only if a process is attached to it. */
1613 if (keypad_open_cnt
> 0) {
1614 while (max_len
-- && keypad_buflen
< KEYPAD_BUFFER
&& *string
) {
1615 keypad_buffer
[(keypad_start
+ keypad_buflen
++) %
1616 KEYPAD_BUFFER
] = *string
++;
1618 wake_up_interruptible(&keypad_read_wait
);
1622 /* this function scans all the bits involving at least one logical signal,
1623 * and puts the results in the bitfield "phys_read" (one bit per established
1624 * contact), and sets "phys_read_prev" to "phys_read".
1626 * Note: to debounce input signals, we will only consider as switched a signal
1627 * which is stable across 2 measures. Signals which are different between two
1628 * reads will be kept as they previously were in their logical form (phys_prev).
1629 * A signal which has just switched will have a 1 in
1630 * (phys_read ^ phys_read_prev).
1632 static void phys_scan_contacts(void)
1639 phys_prev
= phys_curr
;
1640 phys_read_prev
= phys_read
;
1641 phys_read
= 0; /* flush all signals */
1643 /* keep track of old value, with all outputs disabled */
1644 oldval
= r_dtr(pprt
) | scan_mask_o
;
1645 /* activate all keyboard outputs (active low) */
1646 w_dtr(pprt
, oldval
& ~scan_mask_o
);
1648 /* will have a 1 for each bit set to gnd */
1649 bitmask
= PNL_PINPUT(r_str(pprt
)) & scan_mask_i
;
1650 /* disable all matrix signals */
1651 w_dtr(pprt
, oldval
);
1653 /* now that all outputs are cleared, the only active input bits are
1654 * directly connected to the ground
1657 /* 1 for each grounded input */
1658 gndmask
= PNL_PINPUT(r_str(pprt
)) & scan_mask_i
;
1660 /* grounded inputs are signals 40-44 */
1661 phys_read
|= (pmask_t
) gndmask
<< 40;
1663 if (bitmask
!= gndmask
) {
1664 /* since clearing the outputs changed some inputs, we know
1665 * that some input signals are currently tied to some outputs.
1666 * So we'll scan them.
1668 for (bit
= 0; bit
< 8; bit
++) {
1671 if (!(scan_mask_o
& bitval
)) /* skip unused bits */
1674 w_dtr(pprt
, oldval
& ~bitval
); /* enable this output */
1675 bitmask
= PNL_PINPUT(r_str(pprt
)) & ~gndmask
;
1676 phys_read
|= (pmask_t
) bitmask
<< (5 * bit
);
1678 w_dtr(pprt
, oldval
); /* disable all outputs */
1680 /* this is easy: use old bits when they are flapping,
1681 * use new ones when stable */
1682 phys_curr
= (phys_prev
& (phys_read
^ phys_read_prev
)) |
1683 (phys_read
& ~(phys_read
^ phys_read_prev
));
1686 static inline int input_state_high(struct logical_input
*input
)
1689 if ((phys_curr
& input
->mask
) == input
->value
) {
1690 if ((input
->type
== INPUT_TYPE_STD
) &&
1691 (input
->high_timer
== 0)) {
1692 input
->high_timer
++;
1693 if (input
->u
.std
.press_fct
!= NULL
)
1694 input
->u
.std
.press_fct(input
->u
.std
.press_data
);
1695 } else if (input
->type
== INPUT_TYPE_KBD
) {
1696 /* will turn on the light */
1699 if (input
->high_timer
== 0) {
1700 char *press_str
= input
->u
.kbd
.press_str
;
1702 keypad_send_key(press_str
,
1706 if (input
->u
.kbd
.repeat_str
[0]) {
1707 char *repeat_str
= input
->u
.kbd
.repeat_str
;
1708 if (input
->high_timer
>= KEYPAD_REP_START
) {
1709 input
->high_timer
-= KEYPAD_REP_DELAY
;
1710 keypad_send_key(repeat_str
,
1711 sizeof(repeat_str
));
1713 /* we will need to come back here soon */
1717 if (input
->high_timer
< 255)
1718 input
->high_timer
++;
1722 /* else signal falling down. Let's fall through. */
1723 input
->state
= INPUT_ST_FALLING
;
1724 input
->fall_timer
= 0;
1729 static inline void input_state_falling(struct logical_input
*input
)
1732 if ((phys_curr
& input
->mask
) == input
->value
) {
1733 if (input
->type
== INPUT_TYPE_KBD
) {
1734 /* will turn on the light */
1737 if (input
->u
.kbd
.repeat_str
[0]) {
1738 char *repeat_str
= input
->u
.kbd
.repeat_str
;
1739 if (input
->high_timer
>= KEYPAD_REP_START
)
1740 input
->high_timer
-= KEYPAD_REP_DELAY
;
1741 keypad_send_key(repeat_str
,
1742 sizeof(repeat_str
));
1743 /* we will need to come back here soon */
1747 if (input
->high_timer
< 255)
1748 input
->high_timer
++;
1750 input
->state
= INPUT_ST_HIGH
;
1751 } else if (input
->fall_timer
>= input
->fall_time
) {
1752 /* call release event */
1753 if (input
->type
== INPUT_TYPE_STD
) {
1754 void (*release_fct
)(int) = input
->u
.std
.release_fct
;
1755 if (release_fct
!= NULL
)
1756 release_fct(input
->u
.std
.release_data
);
1757 } else if (input
->type
== INPUT_TYPE_KBD
) {
1758 char *release_str
= input
->u
.kbd
.release_str
;
1760 keypad_send_key(release_str
,
1761 sizeof(release_str
));
1764 input
->state
= INPUT_ST_LOW
;
1766 input
->fall_timer
++;
1771 static void panel_process_inputs(void)
1773 struct list_head
*item
;
1774 struct logical_input
*input
;
1779 list_for_each(item
, &logical_inputs
) {
1780 input
= list_entry(item
, struct logical_input
, list
);
1782 switch (input
->state
) {
1784 if ((phys_curr
& input
->mask
) != input
->value
)
1786 /* if all needed ones were already set previously,
1787 * this means that this logical signal has been
1788 * activated by the releasing of another combined
1789 * signal, so we don't want to match.
1790 * eg: AB -(release B)-> A -(release A)-> 0 :
1793 if ((phys_prev
& input
->mask
) == input
->value
)
1795 input
->rise_timer
= 0;
1796 input
->state
= INPUT_ST_RISING
;
1797 /* no break here, fall through */
1798 case INPUT_ST_RISING
:
1799 if ((phys_curr
& input
->mask
) != input
->value
) {
1800 input
->state
= INPUT_ST_LOW
;
1803 if (input
->rise_timer
< input
->rise_time
) {
1805 input
->rise_timer
++;
1808 input
->high_timer
= 0;
1809 input
->state
= INPUT_ST_HIGH
;
1810 /* no break here, fall through */
1812 if (input_state_high(input
))
1814 /* no break here, fall through */
1815 case INPUT_ST_FALLING
:
1816 input_state_falling(input
);
1821 static void panel_scan_timer(void)
1823 if (keypad_enabled
&& keypad_initialized
) {
1824 if (spin_trylock(&pprt_lock
)) {
1825 phys_scan_contacts();
1827 /* no need for the parport anymore */
1828 spin_unlock(&pprt_lock
);
1831 if (!inputs_stable
|| phys_curr
!= phys_prev
)
1832 panel_process_inputs();
1835 if (lcd_enabled
&& lcd_initialized
) {
1837 if (light_tempo
== 0 && ((lcd_flags
& LCD_FLAG_L
) == 0))
1839 light_tempo
= FLASH_LIGHT_TEMPO
;
1840 } else if (light_tempo
> 0) {
1842 if (light_tempo
== 0 && ((lcd_flags
& LCD_FLAG_L
) == 0))
1847 mod_timer(&scan_timer
, jiffies
+ INPUT_POLL_TIME
);
1850 static void init_scan_timer(void)
1852 if (scan_timer
.function
!= NULL
)
1853 return; /* already started */
1855 init_timer(&scan_timer
);
1856 scan_timer
.expires
= jiffies
+ INPUT_POLL_TIME
;
1857 scan_timer
.data
= 0;
1858 scan_timer
.function
= (void *)&panel_scan_timer
;
1859 add_timer(&scan_timer
);
1862 /* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1863 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1864 * corresponding to out and in bits respectively.
1865 * returns 1 if ok, 0 if error (in which case, nothing is written).
1867 static int input_name2mask(char *name
, pmask_t
*mask
, pmask_t
*value
,
1868 char *imask
, char *omask
)
1870 static char sigtab
[10] = "EeSsPpAaBb";
1874 om
= im
= m
= v
= 0ULL;
1876 int in
, out
, bit
, neg
;
1877 for (in
= 0; (in
< sizeof(sigtab
)) &&
1878 (sigtab
[in
] != *name
); in
++)
1880 if (in
>= sizeof(sigtab
))
1881 return 0; /* input name not found */
1882 neg
= (in
& 1); /* odd (lower) names are negated */
1887 if (isdigit(*name
)) {
1890 } else if (*name
== '-')
1893 return 0; /* unknown bit name */
1895 bit
= (out
* 5) + in
;
1911 /* tries to bind a key to the signal name <name>. The key will send the
1912 * strings <press>, <repeat>, <release> for these respective events.
1913 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1915 static struct logical_input
*panel_bind_key(char *name
, char *press
,
1916 char *repeat
, char *release
)
1918 struct logical_input
*key
;
1920 key
= kzalloc(sizeof(struct logical_input
), GFP_KERNEL
);
1922 printk(KERN_ERR
"panel: not enough memory\n");
1925 if (!input_name2mask(name
, &key
->mask
, &key
->value
, &scan_mask_i
,
1931 key
->type
= INPUT_TYPE_KBD
;
1932 key
->state
= INPUT_ST_LOW
;
1936 strncpy(key
->u
.kbd
.press_str
, press
, sizeof(key
->u
.kbd
.press_str
));
1937 strncpy(key
->u
.kbd
.repeat_str
, repeat
, sizeof(key
->u
.kbd
.repeat_str
));
1938 strncpy(key
->u
.kbd
.release_str
, release
,
1939 sizeof(key
->u
.kbd
.release_str
));
1940 list_add(&key
->list
, &logical_inputs
);
1945 static void keypad_init(void)
1948 init_waitqueue_head(&keypad_read_wait
);
1949 keypad_buflen
= 0; /* flushes any eventual noisy keystroke */
1951 /* Let's create all known keys */
1953 for (keynum
= 0; keypad_profile
[keynum
][0][0]; keynum
++) {
1954 panel_bind_key(keypad_profile
[keynum
][0],
1955 keypad_profile
[keynum
][1],
1956 keypad_profile
[keynum
][2],
1957 keypad_profile
[keynum
][3]);
1961 keypad_initialized
= 1;
1964 /**************************************************/
1965 /* device initialization */
1966 /**************************************************/
1968 static int panel_notify_sys(struct notifier_block
*this, unsigned long code
,
1971 if (lcd_enabled
&& lcd_initialized
) {
1975 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
1979 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
1982 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
1991 static struct notifier_block panel_notifier
= {
1997 static void panel_attach(struct parport
*port
)
1999 if (port
->number
!= parport
)
2004 "panel_attach(): port->number=%d parport=%d, "
2005 "already registered !\n",
2006 port
->number
, parport
);
2010 pprt
= parport_register_device(port
, "panel", NULL
, NULL
, /* pf, kf */
2012 /*PARPORT_DEV_EXCL */
2015 pr_err("panel_attach(): port->number=%d parport=%d, "
2016 "parport_register_device() failed\n",
2017 port
->number
, parport
);
2021 if (parport_claim(pprt
)) {
2023 "Panel: could not claim access to parport%d. "
2024 "Aborting.\n", parport
);
2025 goto err_unreg_device
;
2028 /* must init LCD first, just in case an IRQ from the keypad is
2029 * generated at keypad init
2033 if (misc_register(&lcd_dev
))
2034 goto err_unreg_device
;
2037 if (keypad_enabled
) {
2039 if (misc_register(&keypad_dev
))
2046 misc_deregister(&lcd_dev
);
2048 parport_unregister_device(pprt
);
2052 static void panel_detach(struct parport
*port
)
2054 if (port
->number
!= parport
)
2059 "panel_detach(): port->number=%d parport=%d, "
2060 "nothing to unregister.\n",
2061 port
->number
, parport
);
2065 if (keypad_enabled
&& keypad_initialized
) {
2066 misc_deregister(&keypad_dev
);
2067 keypad_initialized
= 0;
2070 if (lcd_enabled
&& lcd_initialized
) {
2071 misc_deregister(&lcd_dev
);
2072 lcd_initialized
= 0;
2075 parport_release(pprt
);
2076 parport_unregister_device(pprt
);
2080 static struct parport_driver panel_driver
= {
2082 .attach
= panel_attach
,
2083 .detach
= panel_detach
,
2087 int panel_init(void)
2089 /* for backwards compatibility */
2090 if (keypad_type
< 0)
2091 keypad_type
= keypad_enabled
;
2094 lcd_type
= lcd_enabled
;
2097 parport
= DEFAULT_PARPORT
;
2099 /* take care of an eventual profile */
2101 case PANEL_PROFILE_CUSTOM
:
2102 /* custom profile */
2103 if (keypad_type
< 0)
2104 keypad_type
= DEFAULT_KEYPAD
;
2106 lcd_type
= DEFAULT_LCD
;
2108 case PANEL_PROFILE_OLD
:
2109 /* 8 bits, 2*16, old keypad */
2110 if (keypad_type
< 0)
2111 keypad_type
= KEYPAD_TYPE_OLD
;
2113 lcd_type
= LCD_TYPE_OLD
;
2119 case PANEL_PROFILE_NEW
:
2120 /* serial, 2*16, new keypad */
2121 if (keypad_type
< 0)
2122 keypad_type
= KEYPAD_TYPE_NEW
;
2124 lcd_type
= LCD_TYPE_KS0074
;
2126 case PANEL_PROFILE_HANTRONIX
:
2127 /* 8 bits, 2*16 hantronix-like, no keypad */
2128 if (keypad_type
< 0)
2129 keypad_type
= KEYPAD_TYPE_NONE
;
2131 lcd_type
= LCD_TYPE_HANTRONIX
;
2133 case PANEL_PROFILE_NEXCOM
:
2134 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
2135 if (keypad_type
< 0)
2136 keypad_type
= KEYPAD_TYPE_NEXCOM
;
2138 lcd_type
= LCD_TYPE_NEXCOM
;
2140 case PANEL_PROFILE_LARGE
:
2141 /* 8 bits, 2*40, old keypad */
2142 if (keypad_type
< 0)
2143 keypad_type
= KEYPAD_TYPE_OLD
;
2145 lcd_type
= LCD_TYPE_OLD
;
2149 lcd_enabled
= (lcd_type
> 0);
2150 keypad_enabled
= (keypad_type
> 0);
2152 switch (keypad_type
) {
2153 case KEYPAD_TYPE_OLD
:
2154 keypad_profile
= old_keypad_profile
;
2156 case KEYPAD_TYPE_NEW
:
2157 keypad_profile
= new_keypad_profile
;
2159 case KEYPAD_TYPE_NEXCOM
:
2160 keypad_profile
= nexcom_keypad_profile
;
2163 keypad_profile
= NULL
;
2167 /* tells various subsystems about the fact that we are initializing */
2168 init_in_progress
= 1;
2170 if (parport_register_driver(&panel_driver
)) {
2172 "Panel: could not register with parport. Aborting.\n");
2176 if (!lcd_enabled
&& !keypad_enabled
) {
2177 /* no device enabled, let's release the parport */
2179 parport_release(pprt
);
2180 parport_unregister_device(pprt
);
2183 parport_unregister_driver(&panel_driver
);
2184 printk(KERN_ERR
"Panel driver version " PANEL_VERSION
2189 register_reboot_notifier(&panel_notifier
);
2192 printk(KERN_INFO
"Panel driver version " PANEL_VERSION
2193 " registered on parport%d (io=0x%lx).\n", parport
,
2196 printk(KERN_INFO
"Panel driver version " PANEL_VERSION
2197 " not yet registered\n");
2198 /* tells various subsystems about the fact that initialization
2200 init_in_progress
= 0;
2204 static int __init
panel_init_module(void)
2206 return panel_init();
2209 static void __exit
panel_cleanup_module(void)
2211 unregister_reboot_notifier(&panel_notifier
);
2213 if (scan_timer
.function
!= NULL
)
2214 del_timer(&scan_timer
);
2217 if (keypad_enabled
) {
2218 misc_deregister(&keypad_dev
);
2219 keypad_initialized
= 0;
2223 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2224 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2225 misc_deregister(&lcd_dev
);
2226 lcd_initialized
= 0;
2229 /* TODO: free all input signals */
2230 parport_release(pprt
);
2231 parport_unregister_device(pprt
);
2234 parport_unregister_driver(&panel_driver
);
2237 module_init(panel_init_module
);
2238 module_exit(panel_cleanup_module
);
2239 MODULE_AUTHOR("Willy Tarreau");
2240 MODULE_LICENSE("GPL");