Staging: panel: Prevent double-calling of parport_release - fix oops.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / panel / panel.c
blob6474c3a77e6c32fd4285a2a795ecaeae4c2ef0d9
1 /*
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.
24 * FIXME:
25 * - the initialization/deinitialization process is very dirty and should
26 * be rewritten. It may even be buggy.
28 * TODO:
29 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
30 * - make the LCD a part of a virtual screen of Vx*Vy
31 * - make the inputs list smp-safe
32 * - change the keyboard to a double mapping : signals -> key_id -> values
33 * so that applications can change values without knowing signals
37 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/errno.h>
41 #include <linux/signal.h>
42 #include <linux/sched.h>
43 #include <linux/spinlock.h>
44 #include <linux/interrupt.h>
45 #include <linux/miscdevice.h>
46 #include <linux/slab.h>
47 #include <linux/ioport.h>
48 #include <linux/fcntl.h>
49 #include <linux/init.h>
50 #include <linux/delay.h>
51 #include <linux/ctype.h>
52 #include <linux/parport.h>
53 #include <linux/version.h>
54 #include <linux/list.h>
55 #include <linux/notifier.h>
56 #include <linux/reboot.h>
57 #include <generated/utsrelease.h>
59 #include <linux/io.h>
60 #include <linux/uaccess.h>
61 #include <asm/system.h>
63 #define LCD_MINOR 156
64 #define KEYPAD_MINOR 185
66 #define PANEL_VERSION "0.9.5"
68 #define LCD_MAXBYTES 256 /* max burst write */
70 #define KEYPAD_BUFFER 64
71 #define INPUT_POLL_TIME (HZ/50) /* poll the keyboard this every second */
72 #define KEYPAD_REP_START (10) /* a key starts to repeat after this times INPUT_POLL_TIME */
73 #define KEYPAD_REP_DELAY (2) /* a key repeats this times INPUT_POLL_TIME */
75 #define FLASH_LIGHT_TEMPO (200) /* keep the light on this times INPUT_POLL_TIME for each flash */
77 /* converts an r_str() input to an active high, bits string : 000BAOSE */
78 #define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
80 #define PNL_PBUSY 0x80 /* inverted input, active low */
81 #define PNL_PACK 0x40 /* direct input, active low */
82 #define PNL_POUTPA 0x20 /* direct input, active high */
83 #define PNL_PSELECD 0x10 /* direct input, active high */
84 #define PNL_PERRORP 0x08 /* direct input, active low */
86 #define PNL_PBIDIR 0x20 /* bi-directional ports */
87 #define PNL_PINTEN 0x10 /* high to read data in or-ed with data out */
88 #define PNL_PSELECP 0x08 /* inverted output, active low */
89 #define PNL_PINITP 0x04 /* direct output, active low */
90 #define PNL_PAUTOLF 0x02 /* inverted output, active low */
91 #define PNL_PSTROBE 0x01 /* inverted output */
93 #define PNL_PD0 0x01
94 #define PNL_PD1 0x02
95 #define PNL_PD2 0x04
96 #define PNL_PD3 0x08
97 #define PNL_PD4 0x10
98 #define PNL_PD5 0x20
99 #define PNL_PD6 0x40
100 #define PNL_PD7 0x80
102 #define PIN_NONE 0
103 #define PIN_STROBE 1
104 #define PIN_D0 2
105 #define PIN_D1 3
106 #define PIN_D2 4
107 #define PIN_D3 5
108 #define PIN_D4 6
109 #define PIN_D5 7
110 #define PIN_D6 8
111 #define PIN_D7 9
112 #define PIN_AUTOLF 14
113 #define PIN_INITP 16
114 #define PIN_SELECP 17
115 #define PIN_NOT_SET 127
117 #define LCD_FLAG_S 0x0001
118 #define LCD_FLAG_ID 0x0002
119 #define LCD_FLAG_B 0x0004 /* blink on */
120 #define LCD_FLAG_C 0x0008 /* cursor on */
121 #define LCD_FLAG_D 0x0010 /* display on */
122 #define LCD_FLAG_F 0x0020 /* large font mode */
123 #define LCD_FLAG_N 0x0040 /* 2-rows mode */
124 #define LCD_FLAG_L 0x0080 /* backlight enabled */
126 #define LCD_ESCAPE_LEN 24 /* 24 chars max for an LCD escape command */
127 #define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
129 /* macros to simplify use of the parallel port */
130 #define r_ctr(x) (parport_read_control((x)->port))
131 #define r_dtr(x) (parport_read_data((x)->port))
132 #define r_str(x) (parport_read_status((x)->port))
133 #define w_ctr(x, y) do { parport_write_control((x)->port, (y)); } while (0)
134 #define w_dtr(x, y) do { parport_write_data((x)->port, (y)); } while (0)
136 /* this defines which bits are to be used and which ones to be ignored */
137 static __u8 scan_mask_o; /* logical or of the output bits involved in the scan matrix */
138 static __u8 scan_mask_i; /* logical or of the input bits involved in the scan matrix */
140 typedef __u64 pmask_t;
142 enum input_type {
143 INPUT_TYPE_STD,
144 INPUT_TYPE_KBD,
147 enum input_state {
148 INPUT_ST_LOW,
149 INPUT_ST_RISING,
150 INPUT_ST_HIGH,
151 INPUT_ST_FALLING,
154 struct logical_input {
155 struct list_head list;
156 pmask_t mask;
157 pmask_t value;
158 enum input_type type;
159 enum input_state state;
160 __u8 rise_time, fall_time;
161 __u8 rise_timer, fall_timer, high_timer;
163 union {
164 struct { /* this structure is valid when type == INPUT_TYPE_STD */
165 void (*press_fct) (int);
166 void (*release_fct) (int);
167 int press_data;
168 int release_data;
169 } std;
170 struct { /* this structure is valid when type == INPUT_TYPE_KBD */
171 /* strings can be full-length (ie. non null-terminated) */
172 char press_str[sizeof(void *) + sizeof(int)];
173 char repeat_str[sizeof(void *) + sizeof(int)];
174 char release_str[sizeof(void *) + sizeof(int)];
175 } kbd;
176 } u;
179 LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
181 /* physical contacts history
182 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
183 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
184 * corresponds to the ground.
185 * Within each group, bits are stored in the same order as read on the port :
186 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
187 * So, each __u64 (or pmask_t) is represented like this :
188 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
189 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
191 static pmask_t phys_read; /* what has just been read from the I/O ports */
192 static pmask_t phys_read_prev; /* previous phys_read */
193 static pmask_t phys_curr; /* stabilized phys_read (phys_read|phys_read_prev) */
194 static pmask_t phys_prev; /* previous phys_curr */
195 static char inputs_stable; /* 0 means that at least one logical signal needs be computed */
197 /* these variables are specific to the keypad */
198 static char keypad_buffer[KEYPAD_BUFFER];
199 static int keypad_buflen;
200 static int keypad_start;
201 static char keypressed;
202 static wait_queue_head_t keypad_read_wait;
204 /* lcd-specific variables */
205 static unsigned long int lcd_flags; /* contains the LCD config state */
206 static unsigned long int lcd_addr_x; /* contains the LCD X offset */
207 static unsigned long int lcd_addr_y; /* contains the LCD Y offset */
208 static char lcd_escape[LCD_ESCAPE_LEN + 1]; /* current escape sequence, 0 terminated */
209 static int lcd_escape_len = -1; /* not in escape state. >=0 = escape cmd len */
212 * Bit masks to convert LCD signals to parallel port outputs.
213 * _d_ are values for data port, _c_ are for control port.
214 * [0] = signal OFF, [1] = signal ON, [2] = mask
216 #define BIT_CLR 0
217 #define BIT_SET 1
218 #define BIT_MSK 2
219 #define BIT_STATES 3
221 * one entry for each bit on the LCD
223 #define LCD_BIT_E 0
224 #define LCD_BIT_RS 1
225 #define LCD_BIT_RW 2
226 #define LCD_BIT_BL 3
227 #define LCD_BIT_CL 4
228 #define LCD_BIT_DA 5
229 #define LCD_BITS 6
232 * each bit can be either connected to a DATA or CTRL port
234 #define LCD_PORT_C 0
235 #define LCD_PORT_D 1
236 #define LCD_PORTS 2
238 static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
241 * LCD protocols
243 #define LCD_PROTO_PARALLEL 0
244 #define LCD_PROTO_SERIAL 1
245 #define LCD_PROTO_TI_DA8XX_LCD 2
248 * LCD character sets
250 #define LCD_CHARSET_NORMAL 0
251 #define LCD_CHARSET_KS0074 1
254 * LCD types
256 #define LCD_TYPE_NONE 0
257 #define LCD_TYPE_OLD 1
258 #define LCD_TYPE_KS0074 2
259 #define LCD_TYPE_HANTRONIX 3
260 #define LCD_TYPE_NEXCOM 4
261 #define LCD_TYPE_CUSTOM 5
264 * keypad types
266 #define KEYPAD_TYPE_NONE 0
267 #define KEYPAD_TYPE_OLD 1
268 #define KEYPAD_TYPE_NEW 2
269 #define KEYPAD_TYPE_NEXCOM 3
272 * panel profiles
274 #define PANEL_PROFILE_CUSTOM 0
275 #define PANEL_PROFILE_OLD 1
276 #define PANEL_PROFILE_NEW 2
277 #define PANEL_PROFILE_HANTRONIX 3
278 #define PANEL_PROFILE_NEXCOM 4
279 #define PANEL_PROFILE_LARGE 5
282 * Construct custom config from the kernel's configuration
284 #define DEFAULT_PROFILE PANEL_PROFILE_LARGE
285 #define DEFAULT_PARPORT 0
286 #define DEFAULT_LCD LCD_TYPE_OLD
287 #define DEFAULT_KEYPAD KEYPAD_TYPE_OLD
288 #define DEFAULT_LCD_WIDTH 40
289 #define DEFAULT_LCD_BWIDTH 40
290 #define DEFAULT_LCD_HWIDTH 64
291 #define DEFAULT_LCD_HEIGHT 2
292 #define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
294 #define DEFAULT_LCD_PIN_E PIN_AUTOLF
295 #define DEFAULT_LCD_PIN_RS PIN_SELECP
296 #define DEFAULT_LCD_PIN_RW PIN_INITP
297 #define DEFAULT_LCD_PIN_SCL PIN_STROBE
298 #define DEFAULT_LCD_PIN_SDA PIN_D0
299 #define DEFAULT_LCD_PIN_BL PIN_NOT_SET
300 #define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
302 #ifdef CONFIG_PANEL_PROFILE
303 #undef DEFAULT_PROFILE
304 #define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
305 #endif
307 #ifdef CONFIG_PANEL_PARPORT
308 #undef DEFAULT_PARPORT
309 #define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
310 #endif
312 #if DEFAULT_PROFILE == 0 /* custom */
313 #ifdef CONFIG_PANEL_KEYPAD
314 #undef DEFAULT_KEYPAD
315 #define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
316 #endif
318 #ifdef CONFIG_PANEL_LCD
319 #undef DEFAULT_LCD
320 #define DEFAULT_LCD CONFIG_PANEL_LCD
321 #endif
323 #ifdef CONFIG_PANEL_LCD_WIDTH
324 #undef DEFAULT_LCD_WIDTH
325 #define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
326 #endif
328 #ifdef CONFIG_PANEL_LCD_BWIDTH
329 #undef DEFAULT_LCD_BWIDTH
330 #define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
331 #endif
333 #ifdef CONFIG_PANEL_LCD_HWIDTH
334 #undef DEFAULT_LCD_HWIDTH
335 #define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
336 #endif
338 #ifdef CONFIG_PANEL_LCD_HEIGHT
339 #undef DEFAULT_LCD_HEIGHT
340 #define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
341 #endif
343 #ifdef CONFIG_PANEL_LCD_PROTO
344 #undef DEFAULT_LCD_PROTO
345 #define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
346 #endif
348 #ifdef CONFIG_PANEL_LCD_PIN_E
349 #undef DEFAULT_LCD_PIN_E
350 #define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
351 #endif
353 #ifdef CONFIG_PANEL_LCD_PIN_RS
354 #undef DEFAULT_LCD_PIN_RS
355 #define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
356 #endif
358 #ifdef CONFIG_PANEL_LCD_PIN_RW
359 #undef DEFAULT_LCD_PIN_RW
360 #define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
361 #endif
363 #ifdef CONFIG_PANEL_LCD_PIN_SCL
364 #undef DEFAULT_LCD_PIN_SCL
365 #define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
366 #endif
368 #ifdef CONFIG_PANEL_LCD_PIN_SDA
369 #undef DEFAULT_LCD_PIN_SDA
370 #define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
371 #endif
373 #ifdef CONFIG_PANEL_LCD_PIN_BL
374 #undef DEFAULT_LCD_PIN_BL
375 #define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
376 #endif
378 #ifdef CONFIG_PANEL_LCD_CHARSET
379 #undef DEFAULT_LCD_CHARSET
380 #define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
381 #endif
383 #endif /* DEFAULT_PROFILE == 0 */
385 /* global variables */
386 static int keypad_open_cnt; /* #times opened */
387 static int lcd_open_cnt; /* #times opened */
388 static struct pardevice *pprt;
390 static int lcd_initialized;
391 static int keypad_initialized;
393 static int light_tempo;
395 static char lcd_must_clear;
396 static char lcd_left_shift;
397 static char init_in_progress;
399 static void (*lcd_write_cmd) (int);
400 static void (*lcd_write_data) (int);
401 static void (*lcd_clear_fast) (void);
403 static DEFINE_SPINLOCK(pprt_lock);
404 static struct timer_list scan_timer;
406 MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
408 static int parport = -1;
409 module_param(parport, int, 0000);
410 MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
412 static int lcd_height = -1;
413 module_param(lcd_height, int, 0000);
414 MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
416 static int lcd_width = -1;
417 module_param(lcd_width, int, 0000);
418 MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
420 static int lcd_bwidth = -1; /* internal buffer width (usually 40) */
421 module_param(lcd_bwidth, int, 0000);
422 MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
424 static int lcd_hwidth = -1; /* hardware buffer width (usually 64) */
425 module_param(lcd_hwidth, int, 0000);
426 MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
428 static int lcd_enabled = -1;
429 module_param(lcd_enabled, int, 0000);
430 MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
432 static int keypad_enabled = -1;
433 module_param(keypad_enabled, int, 0000);
434 MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
436 static int lcd_type = -1;
437 module_param(lcd_type, int, 0000);
438 MODULE_PARM_DESC(lcd_type,
439 "LCD type: 0=none, 1=old //, 2=serial ks0074, 3=hantronix //, 4=nexcom //, 5=compiled-in");
441 static int lcd_proto = -1;
442 module_param(lcd_proto, int, 0000);
443 MODULE_PARM_DESC(lcd_proto, "LCD communication: 0=parallel (//), 1=serial,"
444 "2=TI LCD Interface");
446 static int lcd_charset = -1;
447 module_param(lcd_charset, int, 0000);
448 MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
450 static int keypad_type = -1;
451 module_param(keypad_type, int, 0000);
452 MODULE_PARM_DESC(keypad_type,
453 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
455 static int profile = DEFAULT_PROFILE;
456 module_param(profile, int, 0000);
457 MODULE_PARM_DESC(profile,
458 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; 4=16x2 nexcom; default=40x2, old kp");
461 * These are the parallel port pins the LCD control signals are connected to.
462 * Set this to 0 if the signal is not used. Set it to its opposite value
463 * (negative) if the signal is negated. -MAXINT is used to indicate that the
464 * pin has not been explicitly specified.
466 * WARNING! no check will be performed about collisions with keypad !
469 static int lcd_e_pin = PIN_NOT_SET;
470 module_param(lcd_e_pin, int, 0000);
471 MODULE_PARM_DESC(lcd_e_pin,
472 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
474 static int lcd_rs_pin = PIN_NOT_SET;
475 module_param(lcd_rs_pin, int, 0000);
476 MODULE_PARM_DESC(lcd_rs_pin,
477 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
479 static int lcd_rw_pin = PIN_NOT_SET;
480 module_param(lcd_rw_pin, int, 0000);
481 MODULE_PARM_DESC(lcd_rw_pin,
482 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
484 static int lcd_bl_pin = PIN_NOT_SET;
485 module_param(lcd_bl_pin, int, 0000);
486 MODULE_PARM_DESC(lcd_bl_pin,
487 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
489 static int lcd_da_pin = PIN_NOT_SET;
490 module_param(lcd_da_pin, int, 0000);
491 MODULE_PARM_DESC(lcd_da_pin,
492 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
494 static int lcd_cl_pin = PIN_NOT_SET;
495 module_param(lcd_cl_pin, int, 0000);
496 MODULE_PARM_DESC(lcd_cl_pin,
497 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
499 static unsigned char *lcd_char_conv;
501 /* for some LCD drivers (ks0074) we need a charset conversion table. */
502 static unsigned char lcd_char_conv_ks0074[256] = {
503 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
504 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
505 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
506 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
507 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
508 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
509 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
510 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
511 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
512 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
513 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
514 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
515 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
516 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
517 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
518 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
519 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
520 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
521 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
522 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
523 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
524 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
525 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
526 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
527 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
528 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
529 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
530 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
531 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
532 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
533 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
534 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
535 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
538 char old_keypad_profile[][4][9] = {
539 {"S0", "Left\n", "Left\n", ""},
540 {"S1", "Down\n", "Down\n", ""},
541 {"S2", "Up\n", "Up\n", ""},
542 {"S3", "Right\n", "Right\n", ""},
543 {"S4", "Esc\n", "Esc\n", ""},
544 {"S5", "Ret\n", "Ret\n", ""},
545 {"", "", "", ""}
548 /* signals, press, repeat, release */
549 char new_keypad_profile[][4][9] = {
550 {"S0", "Left\n", "Left\n", ""},
551 {"S1", "Down\n", "Down\n", ""},
552 {"S2", "Up\n", "Up\n", ""},
553 {"S3", "Right\n", "Right\n", ""},
554 {"S4s5", "", "Esc\n", "Esc\n"},
555 {"s4S5", "", "Ret\n", "Ret\n"},
556 {"S4S5", "Help\n", "", ""},
557 /* add new signals above this line */
558 {"", "", "", ""}
561 /* signals, press, repeat, release */
562 char nexcom_keypad_profile[][4][9] = {
563 {"a-p-e-", "Down\n", "Down\n", ""},
564 {"a-p-E-", "Ret\n", "Ret\n", ""},
565 {"a-P-E-", "Esc\n", "Esc\n", ""},
566 {"a-P-e-", "Up\n", "Up\n", ""},
567 /* add new signals above this line */
568 {"", "", "", ""}
571 static char (*keypad_profile)[4][9] = old_keypad_profile;
573 /* FIXME: this should be converted to a bit array containing signals states */
574 static struct {
575 unsigned char e; /* parallel LCD E (data latch on falling edge) */
576 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
577 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
578 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
579 unsigned char cl; /* serial LCD clock (latch on rising edge) */
580 unsigned char da; /* serial LCD data */
581 } bits;
583 static void init_scan_timer(void);
585 /* sets data port bits according to current signals values */
586 static int set_data_bits(void)
588 int val, bit;
590 val = r_dtr(pprt);
591 for (bit = 0; bit < LCD_BITS; bit++)
592 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
594 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
595 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
596 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
597 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
598 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
599 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
601 w_dtr(pprt, val);
602 return val;
605 /* sets ctrl port bits according to current signals values */
606 static int set_ctrl_bits(void)
608 int val, bit;
610 val = r_ctr(pprt);
611 for (bit = 0; bit < LCD_BITS; bit++)
612 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
614 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
615 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
616 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
617 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
618 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
619 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
621 w_ctr(pprt, val);
622 return val;
625 /* sets ctrl & data port bits according to current signals values */
626 static void panel_set_bits(void)
628 set_data_bits();
629 set_ctrl_bits();
633 * Converts a parallel port pin (from -25 to 25) to data and control ports
634 * masks, and data and control port bits. The signal will be considered
635 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
637 * Result will be used this way :
638 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
639 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
641 void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
643 int d_bit, c_bit, inv;
645 d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
646 d_val[2] = c_val[2] = 0xFF;
648 if (pin == 0)
649 return;
651 inv = (pin < 0);
652 if (inv)
653 pin = -pin;
655 d_bit = c_bit = 0;
657 switch (pin) {
658 case PIN_STROBE: /* strobe, inverted */
659 c_bit = PNL_PSTROBE;
660 inv = !inv;
661 break;
662 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
663 d_bit = 1 << (pin - 2);
664 break;
665 case PIN_AUTOLF: /* autofeed, inverted */
666 c_bit = PNL_PAUTOLF;
667 inv = !inv;
668 break;
669 case PIN_INITP: /* init, direct */
670 c_bit = PNL_PINITP;
671 break;
672 case PIN_SELECP: /* select_in, inverted */
673 c_bit = PNL_PSELECP;
674 inv = !inv;
675 break;
676 default: /* unknown pin, ignore */
677 break;
680 if (c_bit) {
681 c_val[2] &= ~c_bit;
682 c_val[!inv] = c_bit;
683 } else if (d_bit) {
684 d_val[2] &= ~d_bit;
685 d_val[!inv] = d_bit;
689 /* sleeps that many milliseconds with a reschedule */
690 static void long_sleep(int ms)
693 if (in_interrupt())
694 mdelay(ms);
695 else {
696 current->state = TASK_INTERRUPTIBLE;
697 schedule_timeout((ms * HZ + 999) / 1000);
701 /* send a serial byte to the LCD panel. The caller is responsible for locking if needed. */
702 static void lcd_send_serial(int byte)
704 int bit;
706 /* the data bit is set on D0, and the clock on STROBE.
707 * LCD reads D0 on STROBE's rising edge.
709 for (bit = 0; bit < 8; bit++) {
710 bits.cl = BIT_CLR; /* CLK low */
711 panel_set_bits();
712 bits.da = byte & 1;
713 panel_set_bits();
714 udelay(2); /* maintain the data during 2 us before CLK up */
715 bits.cl = BIT_SET; /* CLK high */
716 panel_set_bits();
717 udelay(1); /* maintain the strobe during 1 us */
718 byte >>= 1;
722 /* turn the backlight on or off */
723 static void lcd_backlight(int on)
725 if (lcd_bl_pin == PIN_NONE)
726 return;
728 /* The backlight is activated by seting the AUTOFEED line to +5V */
729 spin_lock(&pprt_lock);
730 bits.bl = on;
731 panel_set_bits();
732 spin_unlock(&pprt_lock);
735 /* send a command to the LCD panel in serial mode */
736 static void lcd_write_cmd_s(int cmd)
738 spin_lock(&pprt_lock);
739 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
740 lcd_send_serial(cmd & 0x0F);
741 lcd_send_serial((cmd >> 4) & 0x0F);
742 udelay(40); /* the shortest command takes at least 40 us */
743 spin_unlock(&pprt_lock);
746 /* send data to the LCD panel in serial mode */
747 static void lcd_write_data_s(int data)
749 spin_lock(&pprt_lock);
750 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
751 lcd_send_serial(data & 0x0F);
752 lcd_send_serial((data >> 4) & 0x0F);
753 udelay(40); /* the shortest data takes at least 40 us */
754 spin_unlock(&pprt_lock);
757 /* send a command to the LCD panel in 8 bits parallel mode */
758 static void lcd_write_cmd_p8(int cmd)
760 spin_lock(&pprt_lock);
761 /* present the data to the data port */
762 w_dtr(pprt, cmd);
763 udelay(20); /* maintain the data during 20 us before the strobe */
765 bits.e = BIT_SET;
766 bits.rs = BIT_CLR;
767 bits.rw = BIT_CLR;
768 set_ctrl_bits();
770 udelay(40); /* maintain the strobe during 40 us */
772 bits.e = BIT_CLR;
773 set_ctrl_bits();
775 udelay(120); /* the shortest command takes at least 120 us */
776 spin_unlock(&pprt_lock);
779 /* send data to the LCD panel in 8 bits parallel mode */
780 static void lcd_write_data_p8(int data)
782 spin_lock(&pprt_lock);
783 /* present the data to the data port */
784 w_dtr(pprt, data);
785 udelay(20); /* maintain the data during 20 us before the strobe */
787 bits.e = BIT_SET;
788 bits.rs = BIT_SET;
789 bits.rw = BIT_CLR;
790 set_ctrl_bits();
792 udelay(40); /* maintain the strobe during 40 us */
794 bits.e = BIT_CLR;
795 set_ctrl_bits();
797 udelay(45); /* the shortest data takes at least 45 us */
798 spin_unlock(&pprt_lock);
801 /* send a command to the TI LCD panel */
802 static void lcd_write_cmd_tilcd(int cmd)
804 spin_lock(&pprt_lock);
805 /* present the data to the control port */
806 w_ctr(pprt, cmd);
807 udelay(60);
808 spin_unlock(&pprt_lock);
811 /* send data to the TI LCD panel */
812 static void lcd_write_data_tilcd(int data)
814 spin_lock(&pprt_lock);
815 /* present the data to the data port */
816 w_dtr(pprt, data);
817 udelay(60);
818 spin_unlock(&pprt_lock);
821 static void lcd_gotoxy(void)
823 lcd_write_cmd(0x80 /* set DDRAM address */
824 | (lcd_addr_y ? lcd_hwidth : 0)
825 /* we force the cursor to stay at the end of the line if it wants to go farther */
826 | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
827 (lcd_hwidth - 1) : lcd_bwidth - 1));
830 static void lcd_print(char c)
832 if (lcd_addr_x < lcd_bwidth) {
833 if (lcd_char_conv != NULL)
834 c = lcd_char_conv[(unsigned char)c];
835 lcd_write_data(c);
836 lcd_addr_x++;
838 /* prevents the cursor from wrapping onto the next line */
839 if (lcd_addr_x == lcd_bwidth)
840 lcd_gotoxy();
843 /* fills the display with spaces and resets X/Y */
844 static void lcd_clear_fast_s(void)
846 int pos;
847 lcd_addr_x = lcd_addr_y = 0;
848 lcd_gotoxy();
850 spin_lock(&pprt_lock);
851 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
852 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
853 lcd_send_serial(' ' & 0x0F);
854 lcd_send_serial((' ' >> 4) & 0x0F);
855 udelay(40); /* the shortest data takes at least 40 us */
857 spin_unlock(&pprt_lock);
859 lcd_addr_x = lcd_addr_y = 0;
860 lcd_gotoxy();
863 /* fills the display with spaces and resets X/Y */
864 static void lcd_clear_fast_p8(void)
866 int pos;
867 lcd_addr_x = lcd_addr_y = 0;
868 lcd_gotoxy();
870 spin_lock(&pprt_lock);
871 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
872 /* present the data to the data port */
873 w_dtr(pprt, ' ');
874 udelay(20); /* maintain the data during 20 us before the strobe */
876 bits.e = BIT_SET;
877 bits.rs = BIT_SET;
878 bits.rw = BIT_CLR;
879 set_ctrl_bits();
881 udelay(40); /* maintain the strobe during 40 us */
883 bits.e = BIT_CLR;
884 set_ctrl_bits();
886 udelay(45); /* the shortest data takes at least 45 us */
888 spin_unlock(&pprt_lock);
890 lcd_addr_x = lcd_addr_y = 0;
891 lcd_gotoxy();
894 /* fills the display with spaces and resets X/Y */
895 static void lcd_clear_fast_tilcd(void)
897 int pos;
898 lcd_addr_x = lcd_addr_y = 0;
899 lcd_gotoxy();
901 spin_lock(&pprt_lock);
902 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
903 /* present the data to the data port */
904 w_dtr(pprt, ' ');
905 udelay(60);
908 spin_unlock(&pprt_lock);
910 lcd_addr_x = lcd_addr_y = 0;
911 lcd_gotoxy();
914 /* clears the display and resets X/Y */
915 static void lcd_clear_display(void)
917 lcd_write_cmd(0x01); /* clear display */
918 lcd_addr_x = lcd_addr_y = 0;
919 /* we must wait a few milliseconds (15) */
920 long_sleep(15);
923 static void lcd_init_display(void)
926 lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
927 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
929 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
931 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
932 long_sleep(10);
933 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
934 long_sleep(10);
935 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
936 long_sleep(10);
938 lcd_write_cmd(0x30 /* set font height and lines number */
939 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
940 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0)
942 long_sleep(10);
944 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
945 long_sleep(10);
947 lcd_write_cmd(0x08 /* set display mode */
948 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
949 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
950 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0)
953 lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
955 long_sleep(10);
957 lcd_write_cmd(0x06); /* entry mode set : increment, cursor shifting */
959 lcd_clear_display();
963 * These are the file operation function for user access to /dev/lcd
964 * This function can also be called from inside the kernel, by
965 * setting file and ppos to NULL.
969 static ssize_t lcd_write(struct file *file,
970 const char *buf, size_t count, loff_t *ppos)
973 const char *tmp = buf;
974 char c;
976 for (; count-- > 0; (ppos ? (*ppos)++ : 0), ++tmp) {
977 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
978 schedule(); /* let's be a little nice with other processes that need some CPU */
980 if (ppos == NULL && file == NULL)
981 c = *tmp; /* let's not use get_user() from the kernel ! */
982 else if (get_user(c, tmp))
983 return -EFAULT;
985 /* first, we'll test if we're in escape mode */
986 if ((c != '\n') && lcd_escape_len >= 0) { /* yes, let's add this char to the buffer */
987 lcd_escape[lcd_escape_len++] = c;
988 lcd_escape[lcd_escape_len] = 0;
989 } else {
990 lcd_escape_len = -1; /* aborts any previous escape sequence */
992 switch (c) {
993 case LCD_ESCAPE_CHAR: /* start of an escape sequence */
994 lcd_escape_len = 0;
995 lcd_escape[lcd_escape_len] = 0;
996 break;
997 case '\b': /* go back one char and clear it */
998 if (lcd_addr_x > 0) {
999 if (lcd_addr_x < lcd_bwidth) /* check if we're not at the end of the line */
1000 lcd_write_cmd(0x10); /* back one char */
1001 lcd_addr_x--;
1003 lcd_write_data(' '); /* replace with a space */
1004 lcd_write_cmd(0x10); /* back one char again */
1005 break;
1006 case '\014': /* quickly clear the display */
1007 lcd_clear_fast();
1008 break;
1009 case '\n': /* flush the remainder of the current line and go to the
1010 beginning of the next line */
1011 for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
1012 lcd_write_data(' ');
1013 lcd_addr_x = 0;
1014 lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
1015 lcd_gotoxy();
1016 break;
1017 case '\r': /* go to the beginning of the same line */
1018 lcd_addr_x = 0;
1019 lcd_gotoxy();
1020 break;
1021 case '\t': /* print a space instead of the tab */
1022 lcd_print(' ');
1023 break;
1024 default: /* simply print this char */
1025 lcd_print(c);
1026 break;
1030 /* now we'll see if we're in an escape mode and if the current
1031 escape sequence can be understood.
1033 if (lcd_escape_len >= 2) { /* minimal length for an escape command */
1034 int processed = 0; /* 1 means the command has been processed */
1036 if (!strcmp(lcd_escape, "[2J")) { /* Clear the display */
1037 lcd_clear_fast(); /* clear display */
1038 processed = 1;
1039 } else if (!strcmp(lcd_escape, "[H")) { /* Cursor to home */
1040 lcd_addr_x = lcd_addr_y = 0;
1041 lcd_gotoxy();
1042 processed = 1;
1044 /* codes starting with ^[[L */
1045 else if ((lcd_escape_len >= 3) &&
1046 (lcd_escape[0] == '[') && (lcd_escape[1] == 'L')) { /* LCD special codes */
1048 char *esc = lcd_escape + 2;
1049 int oldflags = lcd_flags;
1051 /* check for display mode flags */
1052 switch (*esc) {
1053 case 'D': /* Display ON */
1054 lcd_flags |= LCD_FLAG_D;
1055 processed = 1;
1056 break;
1057 case 'd': /* Display OFF */
1058 lcd_flags &= ~LCD_FLAG_D;
1059 processed = 1;
1060 break;
1061 case 'C': /* Cursor ON */
1062 lcd_flags |= LCD_FLAG_C;
1063 processed = 1;
1064 break;
1065 case 'c': /* Cursor OFF */
1066 lcd_flags &= ~LCD_FLAG_C;
1067 processed = 1;
1068 break;
1069 case 'B': /* Blink ON */
1070 lcd_flags |= LCD_FLAG_B;
1071 processed = 1;
1072 break;
1073 case 'b': /* Blink OFF */
1074 lcd_flags &= ~LCD_FLAG_B;
1075 processed = 1;
1076 break;
1077 case '+': /* Back light ON */
1078 lcd_flags |= LCD_FLAG_L;
1079 processed = 1;
1080 break;
1081 case '-': /* Back light OFF */
1082 lcd_flags &= ~LCD_FLAG_L;
1083 processed = 1;
1084 break;
1085 case '*': /* flash back light using the keypad timer */
1086 if (scan_timer.function != NULL) {
1087 if (light_tempo == 0
1088 && ((lcd_flags & LCD_FLAG_L)
1089 == 0))
1090 lcd_backlight(1);
1091 light_tempo = FLASH_LIGHT_TEMPO;
1093 processed = 1;
1094 break;
1095 case 'f': /* Small Font */
1096 lcd_flags &= ~LCD_FLAG_F;
1097 processed = 1;
1098 break;
1099 case 'F': /* Large Font */
1100 lcd_flags |= LCD_FLAG_F;
1101 processed = 1;
1102 break;
1103 case 'n': /* One Line */
1104 lcd_flags &= ~LCD_FLAG_N;
1105 processed = 1;
1106 break;
1107 case 'N': /* Two Lines */
1108 lcd_flags |= LCD_FLAG_N;
1109 break;
1111 case 'l': /* Shift Cursor Left */
1112 if (lcd_addr_x > 0) {
1113 if (lcd_addr_x < lcd_bwidth)
1114 lcd_write_cmd(0x10); /* back one char if not at end of line */
1115 lcd_addr_x--;
1117 processed = 1;
1118 break;
1120 case 'r': /* shift cursor right */
1121 if (lcd_addr_x < lcd_width) {
1122 if (lcd_addr_x < (lcd_bwidth - 1))
1123 lcd_write_cmd(0x14); /* allow the cursor to pass the end of the line */
1124 lcd_addr_x++;
1126 processed = 1;
1127 break;
1129 case 'L': /* shift display left */
1130 lcd_left_shift++;
1131 lcd_write_cmd(0x18);
1132 processed = 1;
1133 break;
1135 case 'R': /* shift display right */
1136 lcd_left_shift--;
1137 lcd_write_cmd(0x1C);
1138 processed = 1;
1139 break;
1141 case 'k':{ /* kill end of line */
1142 int x;
1143 for (x = lcd_addr_x; x < lcd_bwidth; x++)
1144 lcd_write_data(' ');
1145 lcd_gotoxy(); /* restore cursor position */
1146 processed = 1;
1147 break;
1149 case 'I': /* reinitialize display */
1150 lcd_init_display();
1151 lcd_left_shift = 0;
1152 processed = 1;
1153 break;
1155 case 'G': /* Generator : LGcxxxxx...xx; */ {
1156 /* must have <c> between '0' and '7', representing the numerical
1157 * ASCII code of the redefined character, and <xx...xx> a sequence
1158 * of 16 hex digits representing 8 bytes for each character. Most
1159 * LCDs will only use 5 lower bits of the 7 first bytes.
1162 unsigned char cgbytes[8];
1163 unsigned char cgaddr;
1164 int cgoffset;
1165 int shift;
1166 char value;
1167 int addr;
1169 if (strchr(esc, ';') == NULL)
1170 break;
1172 esc++;
1174 cgaddr = *(esc++) - '0';
1175 if (cgaddr > 7) {
1176 processed = 1;
1177 break;
1180 cgoffset = 0;
1181 shift = 0;
1182 value = 0;
1183 while (*esc && cgoffset < 8) {
1184 shift ^= 4;
1185 if (*esc >= '0' && *esc <= '9')
1186 value |= (*esc - '0') << shift;
1187 else if (*esc >= 'A' && *esc <= 'Z')
1188 value |= (*esc - 'A' + 10) << shift;
1189 else if (*esc >= 'a' && *esc <= 'z')
1190 value |= (*esc - 'a' + 10) << shift;
1191 else {
1192 esc++;
1193 continue;
1196 if (shift == 0) {
1197 cgbytes[cgoffset++] = value;
1198 value = 0;
1201 esc++;
1204 lcd_write_cmd(0x40 | (cgaddr * 8));
1205 for (addr = 0; addr < cgoffset; addr++)
1206 lcd_write_data(cgbytes[addr]);
1208 lcd_gotoxy(); /* ensures that we stop writing to CGRAM */
1209 processed = 1;
1210 break;
1212 case 'x': /* gotoxy : LxXXX[yYYY]; */
1213 case 'y': /* gotoxy : LyYYY[xXXX]; */
1214 if (strchr(esc, ';') == NULL)
1215 break;
1217 while (*esc) {
1218 if (*esc == 'x') {
1219 esc++;
1220 lcd_addr_x = 0;
1221 while (isdigit(*esc)) {
1222 lcd_addr_x =
1223 lcd_addr_x *
1224 10 + (*esc -
1225 '0');
1226 esc++;
1228 } else if (*esc == 'y') {
1229 esc++;
1230 lcd_addr_y = 0;
1231 while (isdigit(*esc)) {
1232 lcd_addr_y =
1233 lcd_addr_y *
1234 10 + (*esc -
1235 '0');
1236 esc++;
1238 } else
1239 break;
1242 lcd_gotoxy();
1243 processed = 1;
1244 break;
1245 } /* end of switch */
1247 /* Check wether one flag was changed */
1248 if (oldflags != lcd_flags) {
1249 /* check wether one of B,C,D flags was changed */
1250 if ((oldflags ^ lcd_flags) &
1251 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1252 /* set display mode */
1253 lcd_write_cmd(0x08 |
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 wether one of F,N flags was changed */
1258 else if ((oldflags ^ lcd_flags) &
1259 (LCD_FLAG_F | LCD_FLAG_N))
1260 lcd_write_cmd(0x30 |
1261 ((lcd_flags & LCD_FLAG_F) ? 4 : 0) |
1262 ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
1263 /* check wether L flag was changed */
1264 else if ((oldflags ^ lcd_flags) &
1265 (LCD_FLAG_L)) {
1266 if (lcd_flags & (LCD_FLAG_L))
1267 lcd_backlight(1);
1268 else if (light_tempo == 0) /* switch off the light only when the tempo lighting is gone */
1269 lcd_backlight(0);
1274 /* LCD special escape codes */
1275 /* flush the escape sequence if it's been processed or if it is
1276 getting too long. */
1277 if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1278 lcd_escape_len = -1;
1279 } /* escape codes */
1282 return tmp - buf;
1285 static int lcd_open(struct inode *inode, struct file *file)
1287 if (lcd_open_cnt)
1288 return -EBUSY; /* open only once at a time */
1290 if (file->f_mode & FMODE_READ) /* device is write-only */
1291 return -EPERM;
1293 if (lcd_must_clear) {
1294 lcd_clear_display();
1295 lcd_must_clear = 0;
1297 lcd_open_cnt++;
1298 return 0;
1301 static int lcd_release(struct inode *inode, struct file *file)
1303 lcd_open_cnt--;
1304 return 0;
1307 static struct file_operations lcd_fops = {
1308 .write = lcd_write,
1309 .open = lcd_open,
1310 .release = lcd_release,
1313 static struct miscdevice lcd_dev = {
1314 LCD_MINOR,
1315 "lcd",
1316 &lcd_fops
1319 /* public function usable from the kernel for any purpose */
1320 void panel_lcd_print(char *s)
1322 if (lcd_enabled && lcd_initialized)
1323 lcd_write(NULL, s, strlen(s), NULL);
1326 /* initialize the LCD driver */
1327 void lcd_init(void)
1329 switch (lcd_type) {
1330 case LCD_TYPE_OLD: /* parallel mode, 8 bits */
1331 if (lcd_proto < 0)
1332 lcd_proto = LCD_PROTO_PARALLEL;
1333 if (lcd_charset < 0)
1334 lcd_charset = LCD_CHARSET_NORMAL;
1335 if (lcd_e_pin == PIN_NOT_SET)
1336 lcd_e_pin = PIN_STROBE;
1337 if (lcd_rs_pin == PIN_NOT_SET)
1338 lcd_rs_pin = PIN_AUTOLF;
1340 if (lcd_width < 0)
1341 lcd_width = 40;
1342 if (lcd_bwidth < 0)
1343 lcd_bwidth = 40;
1344 if (lcd_hwidth < 0)
1345 lcd_hwidth = 64;
1346 if (lcd_height < 0)
1347 lcd_height = 2;
1348 break;
1349 case LCD_TYPE_KS0074: /* serial mode, ks0074 */
1350 if (lcd_proto < 0)
1351 lcd_proto = LCD_PROTO_SERIAL;
1352 if (lcd_charset < 0)
1353 lcd_charset = LCD_CHARSET_KS0074;
1354 if (lcd_bl_pin == PIN_NOT_SET)
1355 lcd_bl_pin = PIN_AUTOLF;
1356 if (lcd_cl_pin == PIN_NOT_SET)
1357 lcd_cl_pin = PIN_STROBE;
1358 if (lcd_da_pin == PIN_NOT_SET)
1359 lcd_da_pin = PIN_D0;
1361 if (lcd_width < 0)
1362 lcd_width = 16;
1363 if (lcd_bwidth < 0)
1364 lcd_bwidth = 40;
1365 if (lcd_hwidth < 0)
1366 lcd_hwidth = 16;
1367 if (lcd_height < 0)
1368 lcd_height = 2;
1369 break;
1370 case LCD_TYPE_NEXCOM: /* parallel mode, 8 bits, generic */
1371 if (lcd_proto < 0)
1372 lcd_proto = LCD_PROTO_PARALLEL;
1373 if (lcd_charset < 0)
1374 lcd_charset = LCD_CHARSET_NORMAL;
1375 if (lcd_e_pin == PIN_NOT_SET)
1376 lcd_e_pin = PIN_AUTOLF;
1377 if (lcd_rs_pin == PIN_NOT_SET)
1378 lcd_rs_pin = PIN_SELECP;
1379 if (lcd_rw_pin == PIN_NOT_SET)
1380 lcd_rw_pin = PIN_INITP;
1382 if (lcd_width < 0)
1383 lcd_width = 16;
1384 if (lcd_bwidth < 0)
1385 lcd_bwidth = 40;
1386 if (lcd_hwidth < 0)
1387 lcd_hwidth = 64;
1388 if (lcd_height < 0)
1389 lcd_height = 2;
1390 break;
1391 case LCD_TYPE_CUSTOM: /* customer-defined */
1392 if (lcd_proto < 0)
1393 lcd_proto = DEFAULT_LCD_PROTO;
1394 if (lcd_charset < 0)
1395 lcd_charset = DEFAULT_LCD_CHARSET;
1396 /* default geometry will be set later */
1397 break;
1398 case LCD_TYPE_HANTRONIX: /* parallel mode, 8 bits, hantronix-like */
1399 default:
1400 if (lcd_proto < 0)
1401 lcd_proto = LCD_PROTO_PARALLEL;
1402 if (lcd_charset < 0)
1403 lcd_charset = LCD_CHARSET_NORMAL;
1404 if (lcd_e_pin == PIN_NOT_SET)
1405 lcd_e_pin = PIN_STROBE;
1406 if (lcd_rs_pin == PIN_NOT_SET)
1407 lcd_rs_pin = PIN_SELECP;
1409 if (lcd_width < 0)
1410 lcd_width = 16;
1411 if (lcd_bwidth < 0)
1412 lcd_bwidth = 40;
1413 if (lcd_hwidth < 0)
1414 lcd_hwidth = 64;
1415 if (lcd_height < 0)
1416 lcd_height = 2;
1417 break;
1420 /* this is used to catch wrong and default values */
1421 if (lcd_width <= 0)
1422 lcd_width = DEFAULT_LCD_WIDTH;
1423 if (lcd_bwidth <= 0)
1424 lcd_bwidth = DEFAULT_LCD_BWIDTH;
1425 if (lcd_hwidth <= 0)
1426 lcd_hwidth = DEFAULT_LCD_HWIDTH;
1427 if (lcd_height <= 0)
1428 lcd_height = DEFAULT_LCD_HEIGHT;
1430 if (lcd_proto == LCD_PROTO_SERIAL) { /* SERIAL */
1431 lcd_write_cmd = lcd_write_cmd_s;
1432 lcd_write_data = lcd_write_data_s;
1433 lcd_clear_fast = lcd_clear_fast_s;
1435 if (lcd_cl_pin == PIN_NOT_SET)
1436 lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1437 if (lcd_da_pin == PIN_NOT_SET)
1438 lcd_da_pin = DEFAULT_LCD_PIN_SDA;
1440 } else if (lcd_proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
1441 lcd_write_cmd = lcd_write_cmd_p8;
1442 lcd_write_data = lcd_write_data_p8;
1443 lcd_clear_fast = lcd_clear_fast_p8;
1445 if (lcd_e_pin == PIN_NOT_SET)
1446 lcd_e_pin = DEFAULT_LCD_PIN_E;
1447 if (lcd_rs_pin == PIN_NOT_SET)
1448 lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1449 if (lcd_rw_pin == PIN_NOT_SET)
1450 lcd_rw_pin = DEFAULT_LCD_PIN_RW;
1451 } else {
1452 lcd_write_cmd = lcd_write_cmd_tilcd;
1453 lcd_write_data = lcd_write_data_tilcd;
1454 lcd_clear_fast = lcd_clear_fast_tilcd;
1457 if (lcd_bl_pin == PIN_NOT_SET)
1458 lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1460 if (lcd_e_pin == PIN_NOT_SET)
1461 lcd_e_pin = PIN_NONE;
1462 if (lcd_rs_pin == PIN_NOT_SET)
1463 lcd_rs_pin = PIN_NONE;
1464 if (lcd_rw_pin == PIN_NOT_SET)
1465 lcd_rw_pin = PIN_NONE;
1466 if (lcd_bl_pin == PIN_NOT_SET)
1467 lcd_bl_pin = PIN_NONE;
1468 if (lcd_cl_pin == PIN_NOT_SET)
1469 lcd_cl_pin = PIN_NONE;
1470 if (lcd_da_pin == PIN_NOT_SET)
1471 lcd_da_pin = PIN_NONE;
1473 if (lcd_charset < 0)
1474 lcd_charset = DEFAULT_LCD_CHARSET;
1476 if (lcd_charset == LCD_CHARSET_KS0074)
1477 lcd_char_conv = lcd_char_conv_ks0074;
1478 else
1479 lcd_char_conv = NULL;
1481 if (lcd_bl_pin != PIN_NONE)
1482 init_scan_timer();
1484 pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1485 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1486 pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1487 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1488 pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1489 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1490 pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1491 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1492 pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1493 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1494 pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1495 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
1497 /* before this line, we must NOT send anything to the display.
1498 * Since lcd_init_display() needs to write data, we have to
1499 * enable mark the LCD initialized just before.
1501 lcd_initialized = 1;
1502 lcd_init_display();
1504 /* display a short message */
1505 #ifdef CONFIG_PANEL_CHANGE_MESSAGE
1506 #ifdef CONFIG_PANEL_BOOT_MESSAGE
1507 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
1508 #endif
1509 #else
1510 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1511 PANEL_VERSION);
1512 #endif
1513 lcd_addr_x = lcd_addr_y = 0;
1514 lcd_must_clear = 1; /* clear the display on the next device opening */
1515 lcd_gotoxy();
1519 * These are the file operation function for user access to /dev/keypad
1522 static ssize_t keypad_read(struct file *file,
1523 char *buf, size_t count, loff_t *ppos)
1526 unsigned i = *ppos;
1527 char *tmp = buf;
1529 if (keypad_buflen == 0) {
1530 if (file->f_flags & O_NONBLOCK)
1531 return -EAGAIN;
1533 interruptible_sleep_on(&keypad_read_wait);
1534 if (signal_pending(current))
1535 return -EINTR;
1538 for (; count-- > 0 && (keypad_buflen > 0); ++i, ++tmp, --keypad_buflen) {
1539 put_user(keypad_buffer[keypad_start], tmp);
1540 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1542 *ppos = i;
1544 return tmp - buf;
1547 static int keypad_open(struct inode *inode, struct file *file)
1550 if (keypad_open_cnt)
1551 return -EBUSY; /* open only once at a time */
1553 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1554 return -EPERM;
1556 keypad_buflen = 0; /* flush the buffer on opening */
1557 keypad_open_cnt++;
1558 return 0;
1561 static int keypad_release(struct inode *inode, struct file *file)
1563 keypad_open_cnt--;
1564 return 0;
1567 static struct file_operations keypad_fops = {
1568 .read = keypad_read, /* read */
1569 .open = keypad_open, /* open */
1570 .release = keypad_release, /* close */
1573 static struct miscdevice keypad_dev = {
1574 KEYPAD_MINOR,
1575 "keypad",
1576 &keypad_fops
1579 static void keypad_send_key(char *string, int max_len)
1581 if (init_in_progress)
1582 return;
1584 /* send the key to the device only if a process is attached to it. */
1585 if (keypad_open_cnt > 0) {
1586 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1587 keypad_buffer[(keypad_start + keypad_buflen++) %
1588 KEYPAD_BUFFER] = *string++;
1590 wake_up_interruptible(&keypad_read_wait);
1594 /* this function scans all the bits involving at least one logical signal, and puts the
1595 * results in the bitfield "phys_read" (one bit per established contact), and sets
1596 * "phys_read_prev" to "phys_read".
1598 * Note: to debounce input signals, we will only consider as switched a signal which is
1599 * stable across 2 measures. Signals which are different between two reads will be kept
1600 * as they previously were in their logical form (phys_prev). A signal which has just
1601 * switched will have a 1 in (phys_read ^ phys_read_prev).
1603 static void phys_scan_contacts(void)
1605 int bit, bitval;
1606 char oldval;
1607 char bitmask;
1608 char gndmask;
1610 phys_prev = phys_curr;
1611 phys_read_prev = phys_read;
1612 phys_read = 0; /* flush all signals */
1614 oldval = r_dtr(pprt) | scan_mask_o; /* keep track of old value, with all outputs disabled */
1615 w_dtr(pprt, oldval & ~scan_mask_o); /* activate all keyboard outputs (active low) */
1616 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; /* will have a 1 for each bit set to gnd */
1617 w_dtr(pprt, oldval); /* disable all matrix signals */
1619 /* now that all outputs are cleared, the only active input bits are
1620 * directly connected to the ground
1622 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; /* 1 for each grounded input */
1624 phys_read |= (pmask_t) gndmask << 40; /* grounded inputs are signals 40-44 */
1626 if (bitmask != gndmask) {
1627 /* since clearing the outputs changed some inputs, we know that some
1628 * input signals are currently tied to some outputs. So we'll scan them.
1630 for (bit = 0; bit < 8; bit++) {
1631 bitval = 1 << bit;
1633 if (!(scan_mask_o & bitval)) /* skip unused bits */
1634 continue;
1636 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1637 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1638 phys_read |= (pmask_t) bitmask << (5 * bit);
1640 w_dtr(pprt, oldval); /* disable all outputs */
1642 /* this is easy: use old bits when they are flapping, use new ones when stable */
1643 phys_curr =
1644 (phys_prev & (phys_read ^ phys_read_prev)) | (phys_read &
1645 ~(phys_read ^
1646 phys_read_prev));
1649 static void panel_process_inputs(void)
1651 struct list_head *item;
1652 struct logical_input *input;
1654 #if 0
1655 printk(KERN_DEBUG
1656 "entering panel_process_inputs with pp=%016Lx & pc=%016Lx\n",
1657 phys_prev, phys_curr);
1658 #endif
1660 keypressed = 0;
1661 inputs_stable = 1;
1662 list_for_each(item, &logical_inputs) {
1663 input = list_entry(item, struct logical_input, list);
1665 switch (input->state) {
1666 case INPUT_ST_LOW:
1667 if ((phys_curr & input->mask) != input->value)
1668 break;
1669 /* if all needed ones were already set previously, this means that
1670 * this logical signal has been activated by the releasing of
1671 * another combined signal, so we don't want to match.
1672 * eg: AB -(release B)-> A -(release A)-> 0 : don't match A.
1674 if ((phys_prev & input->mask) == input->value)
1675 break;
1676 input->rise_timer = 0;
1677 input->state = INPUT_ST_RISING;
1678 /* no break here, fall through */
1679 case INPUT_ST_RISING:
1680 if ((phys_curr & input->mask) != input->value) {
1681 input->state = INPUT_ST_LOW;
1682 break;
1684 if (input->rise_timer < input->rise_time) {
1685 inputs_stable = 0;
1686 input->rise_timer++;
1687 break;
1689 input->high_timer = 0;
1690 input->state = INPUT_ST_HIGH;
1691 /* no break here, fall through */
1692 case INPUT_ST_HIGH:
1693 #if 0
1694 /* FIXME:
1695 * this is an invalid test. It tries to catch transitions from single-key
1696 * to multiple-key, but doesn't take into account the contacts polarity.
1697 * The only solution to the problem is to parse keys from the most complex
1698 * to the simplest combinations, and mark them as 'caught' once a combination
1699 * matches, then unmatch it for all other ones.
1702 /* try to catch dangerous transitions cases :
1703 * someone adds a bit, so this signal was a false
1704 * positive resulting from a transition. We should invalidate
1705 * the signal immediately and not call the release function.
1706 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1708 if (((phys_prev & input->mask) == input->value)
1709 && ((phys_curr & input->mask) > input->value)) {
1710 input->state = INPUT_ST_LOW; /* invalidate */
1711 break;
1713 #endif
1715 if ((phys_curr & input->mask) == input->value) {
1716 if ((input->type == INPUT_TYPE_STD)
1717 && (input->high_timer == 0)) {
1718 input->high_timer++;
1719 if (input->u.std.press_fct != NULL)
1720 input->u.std.press_fct(input->u.
1721 std.
1722 press_data);
1723 } else if (input->type == INPUT_TYPE_KBD) {
1724 keypressed = 1; /* will turn on the light */
1726 if (input->high_timer == 0) {
1727 if (input->u.kbd.press_str[0])
1728 keypad_send_key(input->
1729 u.kbd.
1730 press_str,
1731 sizeof
1732 (input->
1733 u.kbd.
1734 press_str));
1737 if (input->u.kbd.repeat_str[0]) {
1738 if (input->high_timer >=
1739 KEYPAD_REP_START) {
1740 input->high_timer -=
1741 KEYPAD_REP_DELAY;
1742 keypad_send_key(input->
1743 u.kbd.
1744 repeat_str,
1745 sizeof
1746 (input->
1747 u.kbd.
1748 repeat_str));
1750 inputs_stable = 0; /* we will need to come back here soon */
1753 if (input->high_timer < 255)
1754 input->high_timer++;
1756 break;
1757 } else {
1758 /* else signal falling down. Let's fall through. */
1759 input->state = INPUT_ST_FALLING;
1760 input->fall_timer = 0;
1762 /* no break here, fall through */
1763 case INPUT_ST_FALLING:
1764 #if 0
1765 /* FIXME !!! same comment as above */
1766 if (((phys_prev & input->mask) == input->value)
1767 && ((phys_curr & input->mask) > input->value)) {
1768 input->state = INPUT_ST_LOW; /* invalidate */
1769 break;
1771 #endif
1773 if ((phys_curr & input->mask) == input->value) {
1774 if (input->type == INPUT_TYPE_KBD) {
1775 keypressed = 1; /* will turn on the light */
1777 if (input->u.kbd.repeat_str[0]) {
1778 if (input->high_timer >= KEYPAD_REP_START)
1779 input->high_timer -= KEYPAD_REP_DELAY;
1780 keypad_send_key(input->u.kbd.repeat_str,
1781 sizeof(input->u.kbd.repeat_str));
1782 inputs_stable = 0; /* we will need to come back here soon */
1785 if (input->high_timer < 255)
1786 input->high_timer++;
1788 input->state = INPUT_ST_HIGH;
1789 break;
1790 } else if (input->fall_timer >= input->fall_time) {
1791 /* call release event */
1792 if (input->type == INPUT_TYPE_STD) {
1793 if (input->u.std.release_fct != NULL)
1794 input->u.std.release_fct(input->u.std.release_data);
1796 } else if (input->type == INPUT_TYPE_KBD) {
1797 if (input->u.kbd.release_str[0])
1798 keypad_send_key(input->u.kbd.release_str,
1799 sizeof(input->u.kbd.release_str));
1802 input->state = INPUT_ST_LOW;
1803 break;
1804 } else {
1805 input->fall_timer++;
1806 inputs_stable = 0;
1807 break;
1813 static void panel_scan_timer(void)
1815 if (keypad_enabled && keypad_initialized) {
1816 if (spin_trylock(&pprt_lock)) {
1817 phys_scan_contacts();
1818 spin_unlock(&pprt_lock); /* no need for the parport anymore */
1821 if (!inputs_stable || phys_curr != phys_prev)
1822 panel_process_inputs();
1825 if (lcd_enabled && lcd_initialized) {
1826 if (keypressed) {
1827 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1828 lcd_backlight(1);
1829 light_tempo = FLASH_LIGHT_TEMPO;
1830 } else if (light_tempo > 0) {
1831 light_tempo--;
1832 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1833 lcd_backlight(0);
1837 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
1840 static void init_scan_timer(void)
1842 if (scan_timer.function != NULL)
1843 return; /* already started */
1845 init_timer(&scan_timer);
1846 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1847 scan_timer.data = 0;
1848 scan_timer.function = (void *)&panel_scan_timer;
1849 add_timer(&scan_timer);
1852 /* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1853 * if <omask> or <imask> are non-null, they will be or'ed with the bits corresponding
1854 * to out and in bits respectively.
1855 * returns 1 if ok, 0 if error (in which case, nothing is written).
1857 static int input_name2mask(char *name, pmask_t *mask, pmask_t *value,
1858 char *imask, char *omask)
1860 static char sigtab[10] = "EeSsPpAaBb";
1861 char im, om;
1862 pmask_t m, v;
1864 om = im = m = v = 0ULL;
1865 while (*name) {
1866 int in, out, bit, neg;
1867 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name); in++)
1869 if (in >= sizeof(sigtab))
1870 return 0; /* input name not found */
1871 neg = (in & 1); /* odd (lower) names are negated */
1872 in >>= 1;
1873 im |= (1 << in);
1875 name++;
1876 if (isdigit(*name)) {
1877 out = *name - '0';
1878 om |= (1 << out);
1879 } else if (*name == '-')
1880 out = 8;
1881 else
1882 return 0; /* unknown bit name */
1884 bit = (out * 5) + in;
1886 m |= 1ULL << bit;
1887 if (!neg)
1888 v |= 1ULL << bit;
1889 name++;
1891 *mask = m;
1892 *value = v;
1893 if (imask)
1894 *imask |= im;
1895 if (omask)
1896 *omask |= om;
1897 return 1;
1900 /* tries to bind a key to the signal name <name>. The key will send the
1901 * strings <press>, <repeat>, <release> for these respective events.
1902 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1904 static struct logical_input *panel_bind_key(char *name, char *press,
1905 char *repeat, char *release)
1907 struct logical_input *key;
1909 key = kzalloc(sizeof(struct logical_input), GFP_KERNEL);
1910 if (!key) {
1911 printk(KERN_ERR "panel: not enough memory\n");
1912 return NULL;
1914 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1915 &scan_mask_o))
1916 return NULL;
1918 key->type = INPUT_TYPE_KBD;
1919 key->state = INPUT_ST_LOW;
1920 key->rise_time = 1;
1921 key->fall_time = 1;
1923 #if 0
1924 printk(KERN_DEBUG "bind: <%s> : m=%016Lx v=%016Lx\n", name, key->mask,
1925 key->value);
1926 #endif
1927 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
1928 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
1929 strncpy(key->u.kbd.release_str, release,
1930 sizeof(key->u.kbd.release_str));
1931 list_add(&key->list, &logical_inputs);
1932 return key;
1935 #if 0
1936 /* tries to bind a callback function to the signal name <name>. The function
1937 * <press_fct> will be called with the <press_data> arg when the signal is
1938 * activated, and so on for <release_fct>/<release_data>
1939 * Returns the pointer to the new signal if ok, NULL if the signal could not be bound.
1941 static struct logical_input *panel_bind_callback(char *name,
1942 void (*press_fct) (int),
1943 int press_data,
1944 void (*release_fct) (int),
1945 int release_data)
1947 struct logical_input *callback;
1949 callback = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
1950 if (!callback) {
1951 printk(KERN_ERR "panel: not enough memory\n");
1952 return NULL;
1954 memset(callback, 0, sizeof(struct logical_input));
1955 if (!input_name2mask(name, &callback->mask, &callback->value,
1956 &scan_mask_i, &scan_mask_o))
1957 return NULL;
1959 callback->type = INPUT_TYPE_STD;
1960 callback->state = INPUT_ST_LOW;
1961 callback->rise_time = 1;
1962 callback->fall_time = 1;
1963 callback->u.std.press_fct = press_fct;
1964 callback->u.std.press_data = press_data;
1965 callback->u.std.release_fct = release_fct;
1966 callback->u.std.release_data = release_data;
1967 list_add(&callback->list, &logical_inputs);
1968 return callback;
1970 #endif
1972 static void keypad_init(void)
1974 int keynum;
1975 init_waitqueue_head(&keypad_read_wait);
1976 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
1978 /* Let's create all known keys */
1980 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
1981 panel_bind_key(keypad_profile[keynum][0],
1982 keypad_profile[keynum][1],
1983 keypad_profile[keynum][2],
1984 keypad_profile[keynum][3]);
1987 init_scan_timer();
1988 keypad_initialized = 1;
1991 /**************************************************/
1992 /* device initialization */
1993 /**************************************************/
1995 static int panel_notify_sys(struct notifier_block *this, unsigned long code,
1996 void *unused)
1998 if (lcd_enabled && lcd_initialized) {
1999 switch (code) {
2000 case SYS_DOWN:
2001 panel_lcd_print
2002 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2003 break;
2004 case SYS_HALT:
2005 panel_lcd_print
2006 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2007 break;
2008 case SYS_POWER_OFF:
2009 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2010 break;
2011 default:
2012 break;
2015 return NOTIFY_DONE;
2018 static struct notifier_block panel_notifier = {
2019 panel_notify_sys,
2020 NULL,
2024 static void panel_attach(struct parport *port)
2026 if (port->number != parport)
2027 return;
2029 if (pprt) {
2030 printk(KERN_ERR
2031 "panel_attach(): port->number=%d parport=%d, already registered !\n",
2032 port->number, parport);
2033 return;
2036 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
2037 NULL,
2038 /*PARPORT_DEV_EXCL */
2039 0, (void *)&pprt);
2041 if (parport_claim(pprt)) {
2042 printk(KERN_ERR
2043 "Panel: could not claim access to parport%d. Aborting.\n",
2044 parport);
2045 return;
2048 /* must init LCD first, just in case an IRQ from the keypad is generated at keypad init */
2049 if (lcd_enabled) {
2050 lcd_init();
2051 misc_register(&lcd_dev);
2054 if (keypad_enabled) {
2055 keypad_init();
2056 misc_register(&keypad_dev);
2060 static void panel_detach(struct parport *port)
2062 if (port->number != parport)
2063 return;
2065 if (!pprt) {
2066 printk(KERN_ERR
2067 "panel_detach(): port->number=%d parport=%d, nothing to unregister.\n",
2068 port->number, parport);
2069 return;
2072 if (keypad_enabled && keypad_initialized) {
2073 misc_deregister(&keypad_dev);
2074 keypad_initialized = 0;
2077 if (lcd_enabled && lcd_initialized) {
2078 misc_deregister(&lcd_dev);
2079 lcd_initialized = 0;
2082 parport_release(pprt);
2083 parport_unregister_device(pprt);
2084 pprt = NULL;
2087 static struct parport_driver panel_driver = {
2088 .name = "panel",
2089 .attach = panel_attach,
2090 .detach = panel_detach,
2093 /* init function */
2094 int panel_init(void)
2096 /* for backwards compatibility */
2097 if (keypad_type < 0)
2098 keypad_type = keypad_enabled;
2100 if (lcd_type < 0)
2101 lcd_type = lcd_enabled;
2103 if (parport < 0)
2104 parport = DEFAULT_PARPORT;
2106 /* take care of an eventual profile */
2107 switch (profile) {
2108 case PANEL_PROFILE_CUSTOM: /* custom profile */
2109 if (keypad_type < 0)
2110 keypad_type = DEFAULT_KEYPAD;
2111 if (lcd_type < 0)
2112 lcd_type = DEFAULT_LCD;
2113 break;
2114 case PANEL_PROFILE_OLD: /* 8 bits, 2*16, old keypad */
2115 if (keypad_type < 0)
2116 keypad_type = KEYPAD_TYPE_OLD;
2117 if (lcd_type < 0)
2118 lcd_type = LCD_TYPE_OLD;
2119 if (lcd_width < 0)
2120 lcd_width = 16;
2121 if (lcd_hwidth < 0)
2122 lcd_hwidth = 16;
2123 break;
2124 case PANEL_PROFILE_NEW: /* serial, 2*16, new keypad */
2125 if (keypad_type < 0)
2126 keypad_type = KEYPAD_TYPE_NEW;
2127 if (lcd_type < 0)
2128 lcd_type = LCD_TYPE_KS0074;
2129 break;
2130 case PANEL_PROFILE_HANTRONIX: /* 8 bits, 2*16 hantronix-like, no keypad */
2131 if (keypad_type < 0)
2132 keypad_type = KEYPAD_TYPE_NONE;
2133 if (lcd_type < 0)
2134 lcd_type = LCD_TYPE_HANTRONIX;
2135 break;
2136 case PANEL_PROFILE_NEXCOM: /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
2137 if (keypad_type < 0)
2138 keypad_type = KEYPAD_TYPE_NEXCOM;
2139 if (lcd_type < 0)
2140 lcd_type = LCD_TYPE_NEXCOM;
2141 break;
2142 case PANEL_PROFILE_LARGE: /* 8 bits, 2*40, old keypad */
2143 if (keypad_type < 0)
2144 keypad_type = KEYPAD_TYPE_OLD;
2145 if (lcd_type < 0)
2146 lcd_type = LCD_TYPE_OLD;
2147 break;
2150 lcd_enabled = (lcd_type > 0);
2151 keypad_enabled = (keypad_type > 0);
2153 switch (keypad_type) {
2154 case KEYPAD_TYPE_OLD:
2155 keypad_profile = old_keypad_profile;
2156 break;
2157 case KEYPAD_TYPE_NEW:
2158 keypad_profile = new_keypad_profile;
2159 break;
2160 case KEYPAD_TYPE_NEXCOM:
2161 keypad_profile = nexcom_keypad_profile;
2162 break;
2163 default:
2164 keypad_profile = NULL;
2165 break;
2168 /* tells various subsystems about the fact that we are initializing */
2169 init_in_progress = 1;
2171 if (parport_register_driver(&panel_driver)) {
2172 printk(KERN_ERR
2173 "Panel: could not register with parport. Aborting.\n");
2174 return -EIO;
2177 if (!lcd_enabled && !keypad_enabled) {
2178 /* no device enabled, let's release the parport */
2179 if (pprt) {
2180 parport_release(pprt);
2181 parport_unregister_device(pprt);
2182 pprt = NULL;
2184 parport_unregister_driver(&panel_driver);
2185 printk(KERN_ERR "Panel driver version " PANEL_VERSION
2186 " disabled.\n");
2187 return -ENODEV;
2190 register_reboot_notifier(&panel_notifier);
2192 if (pprt)
2193 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2194 " registered on parport%d (io=0x%lx).\n", parport,
2195 pprt->port->base);
2196 else
2197 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2198 " not yet registered\n");
2199 /* tells various subsystems about the fact that initialization is finished */
2200 init_in_progress = 0;
2201 return 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);
2216 if (pprt != NULL) {
2217 if (keypad_enabled) {
2218 misc_deregister(&keypad_dev);
2219 keypad_initialized = 0;
2222 if (lcd_enabled) {
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);
2232 pprt = NULL;
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");
2243 * Local variables:
2244 * c-indent-level: 4
2245 * tab-width: 8
2246 * End: