blob: dd7d3fde96996102d719984e5cae486acdc4a8b8 [file] [log] [blame]
Willy Tarreau7005b582008-11-13 17:18:59 -08001/*
Willy Tarreau698b1512008-11-22 11:29:33 +01002 * Front panel driver for Linux
3 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
Willy Tarreau7005b582008-11-13 17:18:59 -08004 *
Willy Tarreau698b1512008-11-22 11:29:33 +01005 * 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.
Willy Tarreau7005b582008-11-13 17:18:59 -08009 *
Willy Tarreau698b1512008-11-22 11:29:33 +010010 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
11 * connected to a parallel printer port.
Willy Tarreau7005b582008-11-13 17:18:59 -080012 *
Willy Tarreau698b1512008-11-22 11:29:33 +010013 * 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.
Willy Tarreau7005b582008-11-13 17:18:59 -080016 *
Willy Tarreau698b1512008-11-22 11:29:33 +010017 * 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.
Willy Tarreau7005b582008-11-13 17:18:59 -080020 *
Willy Tarreau698b1512008-11-22 11:29:33 +010021 * Several profiles are provided for commonly found LCD+keypad modules on the
22 * market, such as those found in Nexcom's appliances.
Willy Tarreau7005b582008-11-13 17:18:59 -080023 *
24 * FIXME:
25 * - the initialization/deinitialization process is very dirty and should
26 * be rewritten. It may even be buggy.
27 *
28 * TODO:
29 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
30 * - make the LCD a part of a virtual screen of Vx*Vy
31 * - make the inputs list smp-safe
32 * - change the keyboard to a double mapping : signals -> key_id -> values
33 * so that applications can change values without knowing signals
34 *
35 */
36
37#include <linux/module.h>
38
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/smp_lock.h>
45#include <linux/interrupt.h>
46#include <linux/miscdevice.h>
Willy Tarreau698b1512008-11-22 11:29:33 +010047#include <linux/slab.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080048#include <linux/ioport.h>
49#include <linux/fcntl.h>
50#include <linux/init.h>
51#include <linux/delay.h>
52#include <linux/ctype.h>
53#include <linux/parport.h>
54#include <linux/version.h>
55#include <linux/list.h>
56#include <linux/notifier.h>
57#include <linux/reboot.h>
58#include <linux/utsrelease.h>
59
Willy Tarreau698b1512008-11-22 11:29:33 +010060#include <linux/io.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080061#include <asm/uaccess.h>
62#include <asm/system.h>
63
Willy Tarreau7005b582008-11-13 17:18:59 -080064#define LCD_MINOR 156
65#define KEYPAD_MINOR 185
Willy Tarreau7005b582008-11-13 17:18:59 -080066
67#define PANEL_VERSION "0.9.5"
68
69#define LCD_MAXBYTES 256 /* max burst write */
70
Willy Tarreau7005b582008-11-13 17:18:59 -080071#define KEYPAD_BUFFER 64
72#define INPUT_POLL_TIME (HZ/50) /* poll the keyboard this every second */
73#define KEYPAD_REP_START (10) /* a key starts to repeat after this times INPUT_POLL_TIME */
74#define KEYPAD_REP_DELAY (2) /* a key repeats this times INPUT_POLL_TIME */
75
76#define FLASH_LIGHT_TEMPO (200) /* keep the light on this times INPUT_POLL_TIME for each flash */
77
78/* converts an r_str() input to an active high, bits string : 000BAOSE */
79#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
80
Willy Tarreau698b1512008-11-22 11:29:33 +010081#define PNL_PBUSY 0x80 /* inverted input, active low */
82#define PNL_PACK 0x40 /* direct input, active low */
83#define PNL_POUTPA 0x20 /* direct input, active high */
84#define PNL_PSELECD 0x10 /* direct input, active high */
85#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080086
Willy Tarreau698b1512008-11-22 11:29:33 +010087#define PNL_PBIDIR 0x20 /* bi-directional ports */
88#define PNL_PINTEN 0x10 /* high to read data in or-ed with data out */
89#define PNL_PSELECP 0x08 /* inverted output, active low */
90#define PNL_PINITP 0x04 /* direct output, active low */
91#define PNL_PAUTOLF 0x02 /* inverted output, active low */
92#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080093
94#define PNL_PD0 0x01
95#define PNL_PD1 0x02
96#define PNL_PD2 0x04
97#define PNL_PD3 0x08
98#define PNL_PD4 0x10
99#define PNL_PD5 0x20
100#define PNL_PD6 0x40
101#define PNL_PD7 0x80
102
103#define PIN_NONE 0
104#define PIN_STROBE 1
105#define PIN_D0 2
106#define PIN_D1 3
107#define PIN_D2 4
108#define PIN_D3 5
109#define PIN_D4 6
110#define PIN_D5 7
111#define PIN_D6 8
112#define PIN_D7 9
113#define PIN_AUTOLF 14
114#define PIN_INITP 16
115#define PIN_SELECP 17
116#define PIN_NOT_SET 127
117
Willy Tarreau7005b582008-11-13 17:18:59 -0800118#define LCD_FLAG_S 0x0001
119#define LCD_FLAG_ID 0x0002
120#define LCD_FLAG_B 0x0004 /* blink on */
121#define LCD_FLAG_C 0x0008 /* cursor on */
122#define LCD_FLAG_D 0x0010 /* display on */
123#define LCD_FLAG_F 0x0020 /* large font mode */
124#define LCD_FLAG_N 0x0040 /* 2-rows mode */
125#define LCD_FLAG_L 0x0080 /* backlight enabled */
126
127#define LCD_ESCAPE_LEN 24 /* 24 chars max for an LCD escape command */
128#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
129
130/* macros to simplify use of the parallel port */
131#define r_ctr(x) (parport_read_control((x)->port))
132#define r_dtr(x) (parport_read_data((x)->port))
133#define r_str(x) (parport_read_status((x)->port))
Willy Tarreau698b1512008-11-22 11:29:33 +0100134#define w_ctr(x, y) do { parport_write_control((x)->port, (y)); } while (0)
135#define w_dtr(x, y) do { parport_write_data((x)->port, (y)); } while (0)
Willy Tarreau7005b582008-11-13 17:18:59 -0800136
137/* this defines which bits are to be used and which ones to be ignored */
Willy Tarreau698b1512008-11-22 11:29:33 +0100138static __u8 scan_mask_o; /* logical or of the output bits involved in the scan matrix */
139static __u8 scan_mask_i; /* logical or of the input bits involved in the scan matrix */
Willy Tarreau7005b582008-11-13 17:18:59 -0800140
Willy Tarreau698b1512008-11-22 11:29:33 +0100141typedef __u64 pmask_t;
Willy Tarreau7005b582008-11-13 17:18:59 -0800142
143enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100144 INPUT_TYPE_STD,
145 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800146};
147
148enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100149 INPUT_ST_LOW,
150 INPUT_ST_RISING,
151 INPUT_ST_HIGH,
152 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800153};
154
155struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100156 struct list_head list;
157 pmask_t mask;
158 pmask_t value;
159 enum input_type type;
160 enum input_state state;
161 __u8 rise_time, fall_time;
162 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800163
Willy Tarreau698b1512008-11-22 11:29:33 +0100164 union {
165 struct { /* this structure is valid when type == INPUT_TYPE_STD */
166 void (*press_fct) (int);
167 void (*release_fct) (int);
168 int press_data;
169 int release_data;
170 } std;
171 struct { /* this structure is valid when type == INPUT_TYPE_KBD */
172 /* strings can be full-length (ie. non null-terminated) */
173 char press_str[sizeof(void *) + sizeof(int)];
174 char repeat_str[sizeof(void *) + sizeof(int)];
175 char release_str[sizeof(void *) + sizeof(int)];
176 } kbd;
177 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800178};
179
Willy Tarreau698b1512008-11-22 11:29:33 +0100180LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800181
182/* physical contacts history
183 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
184 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
185 * corresponds to the ground.
186 * Within each group, bits are stored in the same order as read on the port :
187 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
188 * So, each __u64 (or pmask_t) is represented like this :
189 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
190 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
191 */
192static pmask_t phys_read; /* what has just been read from the I/O ports */
Willy Tarreau698b1512008-11-22 11:29:33 +0100193static pmask_t phys_read_prev; /* previous phys_read */
194static pmask_t phys_curr; /* stabilized phys_read (phys_read|phys_read_prev) */
195static pmask_t phys_prev; /* previous phys_curr */
196static char inputs_stable; /* 0 means that at least one logical signal needs be computed */
Willy Tarreau7005b582008-11-13 17:18:59 -0800197
Willy Tarreau7005b582008-11-13 17:18:59 -0800198/* these variables are specific to the keypad */
199static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100200static int keypad_buflen;
201static int keypad_start;
202static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800203static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800204
205/* lcd-specific variables */
Willy Tarreau698b1512008-11-22 11:29:33 +0100206static unsigned long int lcd_flags; /* contains the LCD config state */
207static unsigned long int lcd_addr_x; /* contains the LCD X offset */
208static unsigned long int lcd_addr_y; /* contains the LCD Y offset */
209static char lcd_escape[LCD_ESCAPE_LEN + 1]; /* current escape sequence, 0 terminated */
210static int lcd_escape_len = -1; /* not in escape state. >=0 = escape cmd len */
Willy Tarreau7005b582008-11-13 17:18:59 -0800211
Willy Tarreau7005b582008-11-13 17:18:59 -0800212/*
213 * Bit masks to convert LCD signals to parallel port outputs.
214 * _d_ are values for data port, _c_ are for control port.
215 * [0] = signal OFF, [1] = signal ON, [2] = mask
216 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100217#define BIT_CLR 0
218#define BIT_SET 1
219#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800220#define BIT_STATES 3
221/*
222 * one entry for each bit on the LCD
223 */
224#define LCD_BIT_E 0
225#define LCD_BIT_RS 1
226#define LCD_BIT_RW 2
227#define LCD_BIT_BL 3
228#define LCD_BIT_CL 4
229#define LCD_BIT_DA 5
230#define LCD_BITS 6
231
232/*
233 * each bit can be either connected to a DATA or CTRL port
234 */
235#define LCD_PORT_C 0
236#define LCD_PORT_D 1
237#define LCD_PORTS 2
238
239static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
240
241/*
242 * LCD protocols
243 */
244#define LCD_PROTO_PARALLEL 0
245#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400246#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800247
248/*
249 * LCD character sets
250 */
251#define LCD_CHARSET_NORMAL 0
252#define LCD_CHARSET_KS0074 1
253
254/*
255 * LCD types
256 */
257#define LCD_TYPE_NONE 0
258#define LCD_TYPE_OLD 1
259#define LCD_TYPE_KS0074 2
260#define LCD_TYPE_HANTRONIX 3
261#define LCD_TYPE_NEXCOM 4
262#define LCD_TYPE_CUSTOM 5
263
264/*
265 * keypad types
266 */
267#define KEYPAD_TYPE_NONE 0
268#define KEYPAD_TYPE_OLD 1
269#define KEYPAD_TYPE_NEW 2
270#define KEYPAD_TYPE_NEXCOM 3
271
272/*
273 * panel profiles
274 */
275#define PANEL_PROFILE_CUSTOM 0
276#define PANEL_PROFILE_OLD 1
277#define PANEL_PROFILE_NEW 2
278#define PANEL_PROFILE_HANTRONIX 3
279#define PANEL_PROFILE_NEXCOM 4
280#define PANEL_PROFILE_LARGE 5
281
282/*
283 * Construct custom config from the kernel's configuration
284 */
285#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
286#define DEFAULT_PARPORT 0
287#define DEFAULT_LCD LCD_TYPE_OLD
288#define DEFAULT_KEYPAD KEYPAD_TYPE_OLD
Willy Tarreau7005b582008-11-13 17:18:59 -0800289#define DEFAULT_LCD_WIDTH 40
290#define DEFAULT_LCD_BWIDTH 40
291#define DEFAULT_LCD_HWIDTH 64
292#define DEFAULT_LCD_HEIGHT 2
293#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
294
295#define DEFAULT_LCD_PIN_E PIN_AUTOLF
296#define DEFAULT_LCD_PIN_RS PIN_SELECP
297#define DEFAULT_LCD_PIN_RW PIN_INITP
298#define DEFAULT_LCD_PIN_SCL PIN_STROBE
299#define DEFAULT_LCD_PIN_SDA PIN_D0
300#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
301#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
302
303#ifdef CONFIG_PANEL_PROFILE
304#undef DEFAULT_PROFILE
305#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
306#endif
307
308#ifdef CONFIG_PANEL_PARPORT
309#undef DEFAULT_PARPORT
310#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
311#endif
312
Willy Tarreau698b1512008-11-22 11:29:33 +0100313#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800314#ifdef CONFIG_PANEL_KEYPAD
315#undef DEFAULT_KEYPAD
316#define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
317#endif
318
Willy Tarreau7005b582008-11-13 17:18:59 -0800319#ifdef CONFIG_PANEL_LCD
320#undef DEFAULT_LCD
321#define DEFAULT_LCD CONFIG_PANEL_LCD
322#endif
323
324#ifdef CONFIG_PANEL_LCD_WIDTH
325#undef DEFAULT_LCD_WIDTH
326#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
327#endif
328
329#ifdef CONFIG_PANEL_LCD_BWIDTH
330#undef DEFAULT_LCD_BWIDTH
331#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
332#endif
333
334#ifdef CONFIG_PANEL_LCD_HWIDTH
335#undef DEFAULT_LCD_HWIDTH
336#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
337#endif
338
339#ifdef CONFIG_PANEL_LCD_HEIGHT
340#undef DEFAULT_LCD_HEIGHT
341#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
342#endif
343
344#ifdef CONFIG_PANEL_LCD_PROTO
345#undef DEFAULT_LCD_PROTO
346#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
347#endif
348
349#ifdef CONFIG_PANEL_LCD_PIN_E
350#undef DEFAULT_LCD_PIN_E
351#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
352#endif
353
354#ifdef CONFIG_PANEL_LCD_PIN_RS
355#undef DEFAULT_LCD_PIN_RS
356#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
357#endif
358
359#ifdef CONFIG_PANEL_LCD_PIN_RW
360#undef DEFAULT_LCD_PIN_RW
361#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
362#endif
363
364#ifdef CONFIG_PANEL_LCD_PIN_SCL
365#undef DEFAULT_LCD_PIN_SCL
366#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
367#endif
368
369#ifdef CONFIG_PANEL_LCD_PIN_SDA
370#undef DEFAULT_LCD_PIN_SDA
371#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
372#endif
373
374#ifdef CONFIG_PANEL_LCD_PIN_BL
375#undef DEFAULT_LCD_PIN_BL
376#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
377#endif
378
379#ifdef CONFIG_PANEL_LCD_CHARSET
380#undef DEFAULT_LCD_CHARSET
381#define DEFAULT_LCD_CHARSET
382#endif
383
384#endif /* DEFAULT_PROFILE == 0 */
385
386/* global variables */
Willy Tarreau698b1512008-11-22 11:29:33 +0100387static int keypad_open_cnt; /* #times opened */
388static int lcd_open_cnt; /* #times opened */
Willy Tarreau698b1512008-11-22 11:29:33 +0100389static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800390
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100391static int lcd_initialized;
392static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800393
Willy Tarreau698b1512008-11-22 11:29:33 +0100394static int light_tempo;
Willy Tarreau7005b582008-11-13 17:18:59 -0800395
Willy Tarreau698b1512008-11-22 11:29:33 +0100396static char lcd_must_clear;
397static char lcd_left_shift;
398static char init_in_progress;
Willy Tarreau7005b582008-11-13 17:18:59 -0800399
Willy Tarreau698b1512008-11-22 11:29:33 +0100400static void (*lcd_write_cmd) (int);
401static void (*lcd_write_data) (int);
402static void (*lcd_clear_fast) (void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800403
Willy Tarreau698b1512008-11-22 11:29:33 +0100404static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800405static struct timer_list scan_timer;
406
Willy Tarreau630231772008-11-22 12:52:18 +0100407MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100408
409static int parport = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100410module_param(parport, int, 0000);
411MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100412
413static int lcd_height = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100414module_param(lcd_height, int, 0000);
415MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100416
417static int lcd_width = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100418module_param(lcd_width, int, 0000);
419MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100420
421static int lcd_bwidth = -1; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100422module_param(lcd_bwidth, int, 0000);
423MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100424
425static int lcd_hwidth = -1; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100426module_param(lcd_hwidth, int, 0000);
427MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100428
429static int lcd_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100430module_param(lcd_enabled, int, 0000);
431MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100432
433static int keypad_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100434module_param(keypad_enabled, int, 0000);
435MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100436
437static int lcd_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100438module_param(lcd_type, int, 0000);
439MODULE_PARM_DESC(lcd_type,
440 "LCD type: 0=none, 1=old //, 2=serial ks0074, 3=hantronix //, 4=nexcom //, 5=compiled-in");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100441
442static int lcd_proto = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100443module_param(lcd_proto, int, 0000);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400444MODULE_PARM_DESC(lcd_proto, "LCD communication: 0=parallel (//), 1=serial,"
445 "2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100446
447static int lcd_charset = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100448module_param(lcd_charset, int, 0000);
449MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100450
451static int keypad_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100452module_param(keypad_type, int, 0000);
453MODULE_PARM_DESC(keypad_type,
454 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100455
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100456static int profile = DEFAULT_PROFILE;
Willy Tarreau698b1512008-11-22 11:29:33 +0100457module_param(profile, int, 0000);
458MODULE_PARM_DESC(profile,
459 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; 4=16x2 nexcom; default=40x2, old kp");
Willy Tarreau7005b582008-11-13 17:18:59 -0800460
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100461/*
462 * These are the parallel port pins the LCD control signals are connected to.
463 * Set this to 0 if the signal is not used. Set it to its opposite value
464 * (negative) if the signal is negated. -MAXINT is used to indicate that the
465 * pin has not been explicitly specified.
466 *
Willy Tarreau630231772008-11-22 12:52:18 +0100467 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100468 */
469
470static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100471module_param(lcd_e_pin, int, 0000);
472MODULE_PARM_DESC(lcd_e_pin,
473 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100474
475static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100476module_param(lcd_rs_pin, int, 0000);
477MODULE_PARM_DESC(lcd_rs_pin,
478 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100479
480static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100481module_param(lcd_rw_pin, int, 0000);
482MODULE_PARM_DESC(lcd_rw_pin,
483 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100484
485static int lcd_bl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100486module_param(lcd_bl_pin, int, 0000);
487MODULE_PARM_DESC(lcd_bl_pin,
488 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100489
490static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100491module_param(lcd_da_pin, int, 0000);
492MODULE_PARM_DESC(lcd_da_pin,
493 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100494
495static int lcd_cl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100496module_param(lcd_cl_pin, int, 0000);
497MODULE_PARM_DESC(lcd_cl_pin,
498 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreau7005b582008-11-13 17:18:59 -0800499
Willy Tarreau698b1512008-11-22 11:29:33 +0100500static unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800501
502/* for some LCD drivers (ks0074) we need a charset conversion table. */
503static unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100504 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
505 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
506 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
507 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
508 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
509 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
510 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
511 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
512 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
513 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
514 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
515 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
516 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
517 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
518 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
519 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
520 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
521 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
522 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
523 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
524 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
525 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
526 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
527 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
528 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
529 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
530 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
531 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
532 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
533 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
534 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
535 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
536 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800537};
538
539char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100540 {"S0", "Left\n", "Left\n", ""},
541 {"S1", "Down\n", "Down\n", ""},
542 {"S2", "Up\n", "Up\n", ""},
543 {"S3", "Right\n", "Right\n", ""},
544 {"S4", "Esc\n", "Esc\n", ""},
545 {"S5", "Ret\n", "Ret\n", ""},
546 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800547};
548
549/* signals, press, repeat, release */
550char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100551 {"S0", "Left\n", "Left\n", ""},
552 {"S1", "Down\n", "Down\n", ""},
553 {"S2", "Up\n", "Up\n", ""},
554 {"S3", "Right\n", "Right\n", ""},
555 {"S4s5", "", "Esc\n", "Esc\n"},
556 {"s4S5", "", "Ret\n", "Ret\n"},
557 {"S4S5", "Help\n", "", ""},
558 /* add new signals above this line */
559 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800560};
561
562/* signals, press, repeat, release */
563char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100564 {"a-p-e-", "Down\n", "Down\n", ""},
565 {"a-p-E-", "Ret\n", "Ret\n", ""},
566 {"a-P-E-", "Esc\n", "Esc\n", ""},
567 {"a-P-e-", "Up\n", "Up\n", ""},
568 /* add new signals above this line */
569 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800570};
571
572static char (*keypad_profile)[4][9] = old_keypad_profile;
573
574/* FIXME: this should be converted to a bit array containing signals states */
575static struct {
Willy Tarreau698b1512008-11-22 11:29:33 +0100576 unsigned char e; /* parallel LCD E (data latch on falling edge) */
577 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
578 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
579 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
580 unsigned char cl; /* serial LCD clock (latch on rising edge) */
581 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800582} bits;
583
584static void init_scan_timer(void);
585
586/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100587static int set_data_bits(void)
588{
589 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800590
Willy Tarreau698b1512008-11-22 11:29:33 +0100591 val = r_dtr(pprt);
592 for (bit = 0; bit < LCD_BITS; bit++)
593 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800594
Willy Tarreau698b1512008-11-22 11:29:33 +0100595 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
596 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
597 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
598 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
599 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
600 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800601
Willy Tarreau698b1512008-11-22 11:29:33 +0100602 w_dtr(pprt, val);
603 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800604}
605
606/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100607static int set_ctrl_bits(void)
608{
609 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800610
Willy Tarreau698b1512008-11-22 11:29:33 +0100611 val = r_ctr(pprt);
612 for (bit = 0; bit < LCD_BITS; bit++)
613 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800614
Willy Tarreau698b1512008-11-22 11:29:33 +0100615 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
616 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
617 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
618 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
619 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
620 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800621
Willy Tarreau698b1512008-11-22 11:29:33 +0100622 w_ctr(pprt, val);
623 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800624}
625
626/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530627static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100628{
629 set_data_bits();
630 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800631}
632
633/*
634 * Converts a parallel port pin (from -25 to 25) to data and control ports
635 * masks, and data and control port bits. The signal will be considered
636 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
637 *
638 * Result will be used this way :
639 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
640 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
641 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100642void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
643{
644 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800645
Willy Tarreau698b1512008-11-22 11:29:33 +0100646 d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
647 d_val[2] = c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800648
Willy Tarreau698b1512008-11-22 11:29:33 +0100649 if (pin == 0)
650 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800651
Willy Tarreau698b1512008-11-22 11:29:33 +0100652 inv = (pin < 0);
653 if (inv)
654 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800655
Willy Tarreau698b1512008-11-22 11:29:33 +0100656 d_bit = c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800657
Willy Tarreau698b1512008-11-22 11:29:33 +0100658 switch (pin) {
659 case PIN_STROBE: /* strobe, inverted */
660 c_bit = PNL_PSTROBE;
661 inv = !inv;
662 break;
663 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
664 d_bit = 1 << (pin - 2);
665 break;
666 case PIN_AUTOLF: /* autofeed, inverted */
667 c_bit = PNL_PAUTOLF;
668 inv = !inv;
669 break;
670 case PIN_INITP: /* init, direct */
671 c_bit = PNL_PINITP;
672 break;
673 case PIN_SELECP: /* select_in, inverted */
674 c_bit = PNL_PSELECP;
675 inv = !inv;
676 break;
677 default: /* unknown pin, ignore */
678 break;
679 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800680
Willy Tarreau698b1512008-11-22 11:29:33 +0100681 if (c_bit) {
682 c_val[2] &= ~c_bit;
683 c_val[!inv] = c_bit;
684 } else if (d_bit) {
685 d_val[2] &= ~d_bit;
686 d_val[!inv] = d_bit;
687 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800688}
689
690/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100691static void long_sleep(int ms)
692{
Willy Tarreau7005b582008-11-13 17:18:59 -0800693
Willy Tarreau698b1512008-11-22 11:29:33 +0100694 if (in_interrupt())
695 mdelay(ms);
696 else {
697 current->state = TASK_INTERRUPTIBLE;
698 schedule_timeout((ms * HZ + 999) / 1000);
699 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800700}
701
Willy Tarreau7005b582008-11-13 17:18:59 -0800702/* send a serial byte to the LCD panel. The caller is responsible for locking if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100703static void lcd_send_serial(int byte)
704{
705 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800706
Willy Tarreau698b1512008-11-22 11:29:33 +0100707 /* the data bit is set on D0, and the clock on STROBE.
708 * LCD reads D0 on STROBE's rising edge.
709 */
710 for (bit = 0; bit < 8; bit++) {
711 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530712 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100713 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530714 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100715 udelay(2); /* maintain the data during 2 us before CLK up */
716 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530717 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100718 udelay(1); /* maintain the strobe during 1 us */
719 byte >>= 1;
720 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800721}
722
723/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100724static void lcd_backlight(int on)
725{
726 if (lcd_bl_pin == PIN_NONE)
727 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800728
Willy Tarreau698b1512008-11-22 11:29:33 +0100729 /* The backlight is activated by seting the AUTOFEED line to +5V */
730 spin_lock(&pprt_lock);
731 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530732 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100733 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800734}
735
736/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100737static void lcd_write_cmd_s(int cmd)
738{
739 spin_lock(&pprt_lock);
740 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
741 lcd_send_serial(cmd & 0x0F);
742 lcd_send_serial((cmd >> 4) & 0x0F);
743 udelay(40); /* the shortest command takes at least 40 us */
744 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800745}
746
747/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100748static void lcd_write_data_s(int data)
749{
750 spin_lock(&pprt_lock);
751 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
752 lcd_send_serial(data & 0x0F);
753 lcd_send_serial((data >> 4) & 0x0F);
754 udelay(40); /* the shortest data takes at least 40 us */
755 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800756}
757
758/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100759static void lcd_write_cmd_p8(int cmd)
760{
761 spin_lock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800762 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100763 w_dtr(pprt, cmd);
764 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800765
Willy Tarreau698b1512008-11-22 11:29:33 +0100766 bits.e = BIT_SET;
767 bits.rs = BIT_CLR;
768 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800769 set_ctrl_bits();
770
Willy Tarreau698b1512008-11-22 11:29:33 +0100771 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800772
773 bits.e = BIT_CLR;
774 set_ctrl_bits();
775
Willy Tarreau698b1512008-11-22 11:29:33 +0100776 udelay(120); /* the shortest command takes at least 120 us */
777 spin_unlock(&pprt_lock);
778}
Willy Tarreau7005b582008-11-13 17:18:59 -0800779
Willy Tarreau698b1512008-11-22 11:29:33 +0100780/* send data to the LCD panel in 8 bits parallel mode */
781static void lcd_write_data_p8(int data)
782{
783 spin_lock(&pprt_lock);
784 /* present the data to the data port */
785 w_dtr(pprt, data);
786 udelay(20); /* maintain the data during 20 us before the strobe */
787
788 bits.e = BIT_SET;
789 bits.rs = BIT_SET;
790 bits.rw = BIT_CLR;
791 set_ctrl_bits();
792
793 udelay(40); /* maintain the strobe during 40 us */
794
795 bits.e = BIT_CLR;
796 set_ctrl_bits();
797
798 udelay(45); /* the shortest data takes at least 45 us */
799 spin_unlock(&pprt_lock);
800}
801
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400802/* send a command to the TI LCD panel */
803static void lcd_write_cmd_tilcd(int cmd)
804{
805 spin_lock(&pprt_lock);
806 /* present the data to the control port */
807 w_ctr(pprt, cmd);
808 udelay(60);
809 spin_unlock(&pprt_lock);
810}
811
812/* send data to the TI LCD panel */
813static void lcd_write_data_tilcd(int data)
814{
815 spin_lock(&pprt_lock);
816 /* present the data to the data port */
817 w_dtr(pprt, data);
818 udelay(60);
819 spin_unlock(&pprt_lock);
820}
821
Willy Tarreau698b1512008-11-22 11:29:33 +0100822static void lcd_gotoxy(void)
823{
824 lcd_write_cmd(0x80 /* set DDRAM address */
825 | (lcd_addr_y ? lcd_hwidth : 0)
826 /* we force the cursor to stay at the end of the line if it wants to go farther */
827 | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
828 (lcd_hwidth - 1) : lcd_bwidth - 1));
829}
830
831static void lcd_print(char c)
832{
833 if (lcd_addr_x < lcd_bwidth) {
834 if (lcd_char_conv != NULL)
835 c = lcd_char_conv[(unsigned char)c];
836 lcd_write_data(c);
837 lcd_addr_x++;
838 }
839 /* prevents the cursor from wrapping onto the next line */
840 if (lcd_addr_x == lcd_bwidth)
841 lcd_gotoxy();
842}
843
844/* fills the display with spaces and resets X/Y */
845static void lcd_clear_fast_s(void)
846{
847 int pos;
848 lcd_addr_x = lcd_addr_y = 0;
849 lcd_gotoxy();
850
851 spin_lock(&pprt_lock);
852 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
853 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
854 lcd_send_serial(' ' & 0x0F);
855 lcd_send_serial((' ' >> 4) & 0x0F);
856 udelay(40); /* the shortest data takes at least 40 us */
857 }
858 spin_unlock(&pprt_lock);
859
860 lcd_addr_x = lcd_addr_y = 0;
861 lcd_gotoxy();
862}
863
864/* fills the display with spaces and resets X/Y */
865static void lcd_clear_fast_p8(void)
866{
867 int pos;
868 lcd_addr_x = lcd_addr_y = 0;
869 lcd_gotoxy();
870
871 spin_lock(&pprt_lock);
872 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
873 /* present the data to the data port */
874 w_dtr(pprt, ' ');
875 udelay(20); /* maintain the data during 20 us before the strobe */
876
877 bits.e = BIT_SET;
878 bits.rs = BIT_SET;
879 bits.rw = BIT_CLR;
880 set_ctrl_bits();
881
882 udelay(40); /* maintain the strobe during 40 us */
883
884 bits.e = BIT_CLR;
885 set_ctrl_bits();
886
887 udelay(45); /* the shortest data takes at least 45 us */
888 }
889 spin_unlock(&pprt_lock);
890
891 lcd_addr_x = lcd_addr_y = 0;
892 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800893}
894
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400895/* fills the display with spaces and resets X/Y */
896static void lcd_clear_fast_tilcd(void)
897{
898 int pos;
899 lcd_addr_x = lcd_addr_y = 0;
900 lcd_gotoxy();
901
902 spin_lock(&pprt_lock);
903 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
904 /* present the data to the data port */
905 w_dtr(pprt, ' ');
906 udelay(60);
907 }
908
909 spin_unlock(&pprt_lock);
910
911 lcd_addr_x = lcd_addr_y = 0;
912 lcd_gotoxy();
913}
914
Willy Tarreau7005b582008-11-13 17:18:59 -0800915/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +0100916static void lcd_clear_display(void)
917{
918 lcd_write_cmd(0x01); /* clear display */
919 lcd_addr_x = lcd_addr_y = 0;
920 /* we must wait a few milliseconds (15) */
921 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -0800922}
923
Willy Tarreau698b1512008-11-22 11:29:33 +0100924static void lcd_init_display(void)
925{
Willy Tarreau7005b582008-11-13 17:18:59 -0800926
Willy Tarreau698b1512008-11-22 11:29:33 +0100927 lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
928 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -0800929
Willy Tarreau698b1512008-11-22 11:29:33 +0100930 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -0800931
Willy Tarreau698b1512008-11-22 11:29:33 +0100932 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
933 long_sleep(10);
934 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
935 long_sleep(10);
936 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
937 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800938
Willy Tarreau698b1512008-11-22 11:29:33 +0100939 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)
942 );
943 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800944
Willy Tarreau698b1512008-11-22 11:29:33 +0100945 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
946 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800947
Willy Tarreau698b1512008-11-22 11:29:33 +0100948 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)
952 );
Willy Tarreau7005b582008-11-13 17:18:59 -0800953
Willy Tarreau698b1512008-11-22 11:29:33 +0100954 lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -0800955
Willy Tarreau698b1512008-11-22 11:29:33 +0100956 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800957
Willy Tarreau698b1512008-11-22 11:29:33 +0100958 lcd_write_cmd(0x06); /* entry mode set : increment, cursor shifting */
Willy Tarreau7005b582008-11-13 17:18:59 -0800959
Willy Tarreau698b1512008-11-22 11:29:33 +0100960 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -0800961}
962
963/*
964 * These are the file operation function for user access to /dev/lcd
965 * This function can also be called from inside the kernel, by
966 * setting file and ppos to NULL.
967 *
968 */
969
Willy Tarreau698b1512008-11-22 11:29:33 +0100970static ssize_t lcd_write(struct file *file,
971 const char *buf, size_t count, loff_t *ppos)
972{
Willy Tarreau7005b582008-11-13 17:18:59 -0800973
Willy Tarreau698b1512008-11-22 11:29:33 +0100974 const char *tmp = buf;
975 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -0800976
Willy Tarreau698b1512008-11-22 11:29:33 +0100977 for (; count-- > 0; (ppos ? (*ppos)++ : 0), ++tmp) {
978 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
979 schedule(); /* let's be a little nice with other processes that need some CPU */
Willy Tarreau7005b582008-11-13 17:18:59 -0800980
Willy Tarreau698b1512008-11-22 11:29:33 +0100981 if (ppos == NULL && file == NULL)
982 c = *tmp; /* let's not use get_user() from the kernel ! */
983 else if (get_user(c, tmp))
984 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -0800985
Willy Tarreau698b1512008-11-22 11:29:33 +0100986 /* first, we'll test if we're in escape mode */
987 if ((c != '\n') && lcd_escape_len >= 0) { /* yes, let's add this char to the buffer */
988 lcd_escape[lcd_escape_len++] = c;
989 lcd_escape[lcd_escape_len] = 0;
990 } else {
991 lcd_escape_len = -1; /* aborts any previous escape sequence */
992
993 switch (c) {
994 case LCD_ESCAPE_CHAR: /* start of an escape sequence */
995 lcd_escape_len = 0;
996 lcd_escape[lcd_escape_len] = 0;
997 break;
998 case '\b': /* go back one char and clear it */
999 if (lcd_addr_x > 0) {
1000 if (lcd_addr_x < lcd_bwidth) /* check if we're not at the end of the line */
1001 lcd_write_cmd(0x10); /* back one char */
1002 lcd_addr_x--;
1003 }
1004 lcd_write_data(' '); /* replace with a space */
1005 lcd_write_cmd(0x10); /* back one char again */
1006 break;
1007 case '\014': /* quickly clear the display */
1008 lcd_clear_fast();
1009 break;
1010 case '\n': /* flush the remainder of the current line and go to the
1011 beginning of the next line */
1012 for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
1013 lcd_write_data(' ');
1014 lcd_addr_x = 0;
1015 lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
1016 lcd_gotoxy();
1017 break;
1018 case '\r': /* go to the beginning of the same line */
1019 lcd_addr_x = 0;
1020 lcd_gotoxy();
1021 break;
1022 case '\t': /* print a space instead of the tab */
1023 lcd_print(' ');
1024 break;
1025 default: /* simply print this char */
1026 lcd_print(c);
1027 break;
1028 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001029 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001030
1031 /* now we'll see if we're in an escape mode and if the current
1032 escape sequence can be understood.
1033 */
1034 if (lcd_escape_len >= 2) { /* minimal length for an escape command */
1035 int processed = 0; /* 1 means the command has been processed */
1036
1037 if (!strcmp(lcd_escape, "[2J")) { /* Clear the display */
1038 lcd_clear_fast(); /* clear display */
1039 processed = 1;
1040 } else if (!strcmp(lcd_escape, "[H")) { /* Cursor to home */
1041 lcd_addr_x = lcd_addr_y = 0;
1042 lcd_gotoxy();
1043 processed = 1;
1044 }
1045 /* codes starting with ^[[L */
1046 else if ((lcd_escape_len >= 3) &&
1047 (lcd_escape[0] == '[') && (lcd_escape[1] == 'L')) { /* LCD special codes */
1048
1049 char *esc = lcd_escape + 2;
1050 int oldflags = lcd_flags;
1051
1052 /* check for display mode flags */
1053 switch (*esc) {
1054 case 'D': /* Display ON */
1055 lcd_flags |= LCD_FLAG_D;
1056 processed = 1;
1057 break;
1058 case 'd': /* Display OFF */
1059 lcd_flags &= ~LCD_FLAG_D;
1060 processed = 1;
1061 break;
1062 case 'C': /* Cursor ON */
1063 lcd_flags |= LCD_FLAG_C;
1064 processed = 1;
1065 break;
1066 case 'c': /* Cursor OFF */
1067 lcd_flags &= ~LCD_FLAG_C;
1068 processed = 1;
1069 break;
1070 case 'B': /* Blink ON */
1071 lcd_flags |= LCD_FLAG_B;
1072 processed = 1;
1073 break;
1074 case 'b': /* Blink OFF */
1075 lcd_flags &= ~LCD_FLAG_B;
1076 processed = 1;
1077 break;
1078 case '+': /* Back light ON */
1079 lcd_flags |= LCD_FLAG_L;
1080 processed = 1;
1081 break;
1082 case '-': /* Back light OFF */
1083 lcd_flags &= ~LCD_FLAG_L;
1084 processed = 1;
1085 break;
1086 case '*': /* flash back light using the keypad timer */
1087 if (scan_timer.function != NULL) {
1088 if (light_tempo == 0
1089 && ((lcd_flags & LCD_FLAG_L)
1090 == 0))
1091 lcd_backlight(1);
1092 light_tempo = FLASH_LIGHT_TEMPO;
1093 }
1094 processed = 1;
1095 break;
1096 case 'f': /* Small Font */
1097 lcd_flags &= ~LCD_FLAG_F;
1098 processed = 1;
1099 break;
1100 case 'F': /* Large Font */
1101 lcd_flags |= LCD_FLAG_F;
1102 processed = 1;
1103 break;
1104 case 'n': /* One Line */
1105 lcd_flags &= ~LCD_FLAG_N;
1106 processed = 1;
1107 break;
1108 case 'N': /* Two Lines */
1109 lcd_flags |= LCD_FLAG_N;
1110 break;
1111
1112 case 'l': /* Shift Cursor Left */
1113 if (lcd_addr_x > 0) {
1114 if (lcd_addr_x < lcd_bwidth)
1115 lcd_write_cmd(0x10); /* back one char if not at end of line */
1116 lcd_addr_x--;
1117 }
1118 processed = 1;
1119 break;
1120
1121 case 'r': /* shift cursor right */
1122 if (lcd_addr_x < lcd_width) {
1123 if (lcd_addr_x < (lcd_bwidth - 1))
1124 lcd_write_cmd(0x14); /* allow the cursor to pass the end of the line */
1125 lcd_addr_x++;
1126 }
1127 processed = 1;
1128 break;
1129
1130 case 'L': /* shift display left */
1131 lcd_left_shift++;
1132 lcd_write_cmd(0x18);
1133 processed = 1;
1134 break;
1135
1136 case 'R': /* shift display right */
1137 lcd_left_shift--;
1138 lcd_write_cmd(0x1C);
1139 processed = 1;
1140 break;
1141
1142 case 'k':{ /* kill end of line */
1143 int x;
1144 for (x = lcd_addr_x; x < lcd_bwidth; x++)
1145 lcd_write_data(' ');
1146 lcd_gotoxy(); /* restore cursor position */
1147 processed = 1;
1148 break;
1149 }
1150 case 'I': /* reinitialize display */
1151 lcd_init_display();
1152 lcd_left_shift = 0;
1153 processed = 1;
1154 break;
1155
1156 case 'G': /* Generator : LGcxxxxx...xx; */ {
1157 /* must have <c> between '0' and '7', representing the numerical
1158 * ASCII code of the redefined character, and <xx...xx> a sequence
1159 * of 16 hex digits representing 8 bytes for each character. Most
1160 * LCDs will only use 5 lower bits of the 7 first bytes.
1161 */
1162
1163 unsigned char cgbytes[8];
1164 unsigned char cgaddr;
1165 int cgoffset;
1166 int shift;
1167 char value;
1168 int addr;
1169
1170 if (strchr(esc, ';') == NULL)
1171 break;
1172
1173 esc++;
1174
1175 cgaddr = *(esc++) - '0';
1176 if (cgaddr > 7) {
1177 processed = 1;
1178 break;
1179 }
1180
1181 cgoffset = 0;
1182 shift = 0;
1183 value = 0;
1184 while (*esc && cgoffset < 8) {
1185 shift ^= 4;
1186 if (*esc >= '0' && *esc <= '9')
1187 value |= (*esc - '0') << shift;
1188 else if (*esc >= 'A' && *esc <= 'Z')
1189 value |= (*esc - 'A' + 10) << shift;
1190 else if (*esc >= 'a' && *esc <= 'z')
1191 value |= (*esc - 'a' + 10) << shift;
1192 else {
1193 esc++;
1194 continue;
1195 }
1196
1197 if (shift == 0) {
1198 cgbytes[cgoffset++] = value;
1199 value = 0;
1200 }
1201
1202 esc++;
1203 }
1204
1205 lcd_write_cmd(0x40 | (cgaddr * 8));
1206 for (addr = 0; addr < cgoffset; addr++)
1207 lcd_write_data(cgbytes[addr]);
1208
1209 lcd_gotoxy(); /* ensures that we stop writing to CGRAM */
1210 processed = 1;
1211 break;
1212 }
1213 case 'x': /* gotoxy : LxXXX[yYYY]; */
1214 case 'y': /* gotoxy : LyYYY[xXXX]; */
1215 if (strchr(esc, ';') == NULL)
1216 break;
1217
1218 while (*esc) {
1219 if (*esc == 'x') {
1220 esc++;
1221 lcd_addr_x = 0;
1222 while (isdigit(*esc)) {
1223 lcd_addr_x =
1224 lcd_addr_x *
1225 10 + (*esc -
1226 '0');
1227 esc++;
1228 }
1229 } else if (*esc == 'y') {
1230 esc++;
1231 lcd_addr_y = 0;
1232 while (isdigit(*esc)) {
1233 lcd_addr_y =
1234 lcd_addr_y *
1235 10 + (*esc -
1236 '0');
1237 esc++;
1238 }
1239 } else
1240 break;
1241 }
1242
1243 lcd_gotoxy();
1244 processed = 1;
1245 break;
1246 } /* end of switch */
1247
1248 /* Check wether one flag was changed */
1249 if (oldflags != lcd_flags) {
1250 /* check wether one of B,C,D flags was changed */
1251 if ((oldflags ^ lcd_flags) &
1252 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1253 /* set display mode */
1254 lcd_write_cmd(0x08 |
1255 ((lcd_flags & LCD_FLAG_D) ? 4 : 0) |
1256 ((lcd_flags & LCD_FLAG_C) ? 2 : 0) |
1257 ((lcd_flags & LCD_FLAG_B) ? 1 : 0));
1258 /* check wether one of F,N flags was changed */
1259 else if ((oldflags ^ lcd_flags) &
1260 (LCD_FLAG_F | LCD_FLAG_N))
1261 lcd_write_cmd(0x30 |
1262 ((lcd_flags & LCD_FLAG_F) ? 4 : 0) |
1263 ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
1264 /* check wether L flag was changed */
1265 else if ((oldflags ^ lcd_flags) &
1266 (LCD_FLAG_L)) {
1267 if (lcd_flags & (LCD_FLAG_L))
1268 lcd_backlight(1);
1269 else if (light_tempo == 0) /* switch off the light only when the tempo lighting is gone */
1270 lcd_backlight(0);
1271 }
1272 }
1273 }
1274
1275 /* LCD special escape codes */
1276 /* flush the escape sequence if it's been processed or if it is
1277 getting too long. */
1278 if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1279 lcd_escape_len = -1;
1280 } /* escape codes */
Willy Tarreau7005b582008-11-13 17:18:59 -08001281 }
1282
Willy Tarreau698b1512008-11-22 11:29:33 +01001283 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001284}
1285
Willy Tarreau698b1512008-11-22 11:29:33 +01001286static int lcd_open(struct inode *inode, struct file *file)
1287{
1288 if (lcd_open_cnt)
1289 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001290
Willy Tarreau698b1512008-11-22 11:29:33 +01001291 if (file->f_mode & FMODE_READ) /* device is write-only */
1292 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001293
Willy Tarreau698b1512008-11-22 11:29:33 +01001294 if (lcd_must_clear) {
1295 lcd_clear_display();
1296 lcd_must_clear = 0;
1297 }
1298 lcd_open_cnt++;
1299 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001300}
1301
Willy Tarreau698b1512008-11-22 11:29:33 +01001302static int lcd_release(struct inode *inode, struct file *file)
1303{
1304 lcd_open_cnt--;
1305 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001306}
1307
Willy Tarreau7005b582008-11-13 17:18:59 -08001308static struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001309 .write = lcd_write,
1310 .open = lcd_open,
1311 .release = lcd_release,
Willy Tarreau7005b582008-11-13 17:18:59 -08001312};
1313
1314static struct miscdevice lcd_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001315 LCD_MINOR,
1316 "lcd",
1317 &lcd_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001318};
1319
Willy Tarreau7005b582008-11-13 17:18:59 -08001320/* public function usable from the kernel for any purpose */
Willy Tarreau698b1512008-11-22 11:29:33 +01001321void panel_lcd_print(char *s)
1322{
1323 if (lcd_enabled && lcd_initialized)
1324 lcd_write(NULL, s, strlen(s), NULL);
Willy Tarreau7005b582008-11-13 17:18:59 -08001325}
1326
Willy Tarreau7005b582008-11-13 17:18:59 -08001327/* initialize the LCD driver */
Willy Tarreau698b1512008-11-22 11:29:33 +01001328void lcd_init(void)
1329{
1330 switch (lcd_type) {
1331 case LCD_TYPE_OLD: /* parallel mode, 8 bits */
1332 if (lcd_proto < 0)
1333 lcd_proto = LCD_PROTO_PARALLEL;
1334 if (lcd_charset < 0)
1335 lcd_charset = LCD_CHARSET_NORMAL;
1336 if (lcd_e_pin == PIN_NOT_SET)
1337 lcd_e_pin = PIN_STROBE;
1338 if (lcd_rs_pin == PIN_NOT_SET)
1339 lcd_rs_pin = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001340
Willy Tarreau698b1512008-11-22 11:29:33 +01001341 if (lcd_width < 0)
1342 lcd_width = 40;
1343 if (lcd_bwidth < 0)
1344 lcd_bwidth = 40;
1345 if (lcd_hwidth < 0)
1346 lcd_hwidth = 64;
1347 if (lcd_height < 0)
1348 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001349 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001350 case LCD_TYPE_KS0074: /* serial mode, ks0074 */
1351 if (lcd_proto < 0)
1352 lcd_proto = LCD_PROTO_SERIAL;
1353 if (lcd_charset < 0)
1354 lcd_charset = LCD_CHARSET_KS0074;
1355 if (lcd_bl_pin == PIN_NOT_SET)
1356 lcd_bl_pin = PIN_AUTOLF;
1357 if (lcd_cl_pin == PIN_NOT_SET)
1358 lcd_cl_pin = PIN_STROBE;
1359 if (lcd_da_pin == PIN_NOT_SET)
1360 lcd_da_pin = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001361
Willy Tarreau698b1512008-11-22 11:29:33 +01001362 if (lcd_width < 0)
1363 lcd_width = 16;
1364 if (lcd_bwidth < 0)
1365 lcd_bwidth = 40;
1366 if (lcd_hwidth < 0)
1367 lcd_hwidth = 16;
1368 if (lcd_height < 0)
1369 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001370 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001371 case LCD_TYPE_NEXCOM: /* parallel mode, 8 bits, generic */
1372 if (lcd_proto < 0)
1373 lcd_proto = LCD_PROTO_PARALLEL;
1374 if (lcd_charset < 0)
1375 lcd_charset = LCD_CHARSET_NORMAL;
1376 if (lcd_e_pin == PIN_NOT_SET)
1377 lcd_e_pin = PIN_AUTOLF;
1378 if (lcd_rs_pin == PIN_NOT_SET)
1379 lcd_rs_pin = PIN_SELECP;
1380 if (lcd_rw_pin == PIN_NOT_SET)
1381 lcd_rw_pin = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001382
Willy Tarreau698b1512008-11-22 11:29:33 +01001383 if (lcd_width < 0)
1384 lcd_width = 16;
1385 if (lcd_bwidth < 0)
1386 lcd_bwidth = 40;
1387 if (lcd_hwidth < 0)
1388 lcd_hwidth = 64;
1389 if (lcd_height < 0)
1390 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001391 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001392 case LCD_TYPE_CUSTOM: /* customer-defined */
1393 if (lcd_proto < 0)
1394 lcd_proto = DEFAULT_LCD_PROTO;
1395 if (lcd_charset < 0)
1396 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001397 /* default geometry will be set later */
1398 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001399 case LCD_TYPE_HANTRONIX: /* parallel mode, 8 bits, hantronix-like */
1400 default:
1401 if (lcd_proto < 0)
1402 lcd_proto = LCD_PROTO_PARALLEL;
1403 if (lcd_charset < 0)
1404 lcd_charset = LCD_CHARSET_NORMAL;
1405 if (lcd_e_pin == PIN_NOT_SET)
1406 lcd_e_pin = PIN_STROBE;
1407 if (lcd_rs_pin == PIN_NOT_SET)
1408 lcd_rs_pin = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001409
Willy Tarreau698b1512008-11-22 11:29:33 +01001410 if (lcd_width < 0)
1411 lcd_width = 16;
1412 if (lcd_bwidth < 0)
1413 lcd_bwidth = 40;
1414 if (lcd_hwidth < 0)
1415 lcd_hwidth = 64;
1416 if (lcd_height < 0)
1417 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001418 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001419 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001420
Willy Tarreau698b1512008-11-22 11:29:33 +01001421 /* this is used to catch wrong and default values */
1422 if (lcd_width <= 0)
1423 lcd_width = DEFAULT_LCD_WIDTH;
1424 if (lcd_bwidth <= 0)
1425 lcd_bwidth = DEFAULT_LCD_BWIDTH;
1426 if (lcd_hwidth <= 0)
1427 lcd_hwidth = DEFAULT_LCD_HWIDTH;
1428 if (lcd_height <= 0)
1429 lcd_height = DEFAULT_LCD_HEIGHT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001430
Willy Tarreau698b1512008-11-22 11:29:33 +01001431 if (lcd_proto == LCD_PROTO_SERIAL) { /* SERIAL */
1432 lcd_write_cmd = lcd_write_cmd_s;
1433 lcd_write_data = lcd_write_data_s;
1434 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001435
Willy Tarreau698b1512008-11-22 11:29:33 +01001436 if (lcd_cl_pin == PIN_NOT_SET)
1437 lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1438 if (lcd_da_pin == PIN_NOT_SET)
1439 lcd_da_pin = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001440
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001441 } else if (lcd_proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001442 lcd_write_cmd = lcd_write_cmd_p8;
1443 lcd_write_data = lcd_write_data_p8;
1444 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001445
Willy Tarreau698b1512008-11-22 11:29:33 +01001446 if (lcd_e_pin == PIN_NOT_SET)
1447 lcd_e_pin = DEFAULT_LCD_PIN_E;
1448 if (lcd_rs_pin == PIN_NOT_SET)
1449 lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1450 if (lcd_rw_pin == PIN_NOT_SET)
1451 lcd_rw_pin = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001452 } else {
1453 lcd_write_cmd = lcd_write_cmd_tilcd;
1454 lcd_write_data = lcd_write_data_tilcd;
1455 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001456 }
1457
1458 if (lcd_bl_pin == PIN_NOT_SET)
1459 lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1460
1461 if (lcd_e_pin == PIN_NOT_SET)
1462 lcd_e_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001463 if (lcd_rs_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001464 lcd_rs_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001465 if (lcd_rw_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001466 lcd_rw_pin = PIN_NONE;
1467 if (lcd_bl_pin == PIN_NOT_SET)
1468 lcd_bl_pin = PIN_NONE;
1469 if (lcd_cl_pin == PIN_NOT_SET)
1470 lcd_cl_pin = PIN_NONE;
1471 if (lcd_da_pin == PIN_NOT_SET)
1472 lcd_da_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001473
Willy Tarreau698b1512008-11-22 11:29:33 +01001474 if (lcd_charset < 0)
1475 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001476
Willy Tarreau698b1512008-11-22 11:29:33 +01001477 if (lcd_charset == LCD_CHARSET_KS0074)
1478 lcd_char_conv = lcd_char_conv_ks0074;
1479 else
1480 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001481
Willy Tarreau698b1512008-11-22 11:29:33 +01001482 if (lcd_bl_pin != PIN_NONE)
1483 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001484
Willy Tarreau698b1512008-11-22 11:29:33 +01001485 pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1486 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1487 pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1488 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1489 pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1490 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1491 pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1492 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1493 pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1494 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1495 pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1496 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001497
Willy Tarreau698b1512008-11-22 11:29:33 +01001498 /* before this line, we must NOT send anything to the display.
1499 * Since lcd_init_display() needs to write data, we have to
1500 * enable mark the LCD initialized just before.
1501 */
1502 lcd_initialized = 1;
1503 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001504
Willy Tarreau698b1512008-11-22 11:29:33 +01001505 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001506#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1507#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001508 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001509#endif
1510#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001511 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1512 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001513#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001514 lcd_addr_x = lcd_addr_y = 0;
1515 lcd_must_clear = 1; /* clear the display on the next device opening */
1516 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001517}
1518
Willy Tarreau7005b582008-11-13 17:18:59 -08001519/*
1520 * These are the file operation function for user access to /dev/keypad
1521 */
1522
Willy Tarreau698b1512008-11-22 11:29:33 +01001523static ssize_t keypad_read(struct file *file,
1524 char *buf, size_t count, loff_t *ppos)
1525{
Willy Tarreau7005b582008-11-13 17:18:59 -08001526
Willy Tarreau698b1512008-11-22 11:29:33 +01001527 unsigned i = *ppos;
1528 char *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001529
Willy Tarreau698b1512008-11-22 11:29:33 +01001530 if (keypad_buflen == 0) {
1531 if (file->f_flags & O_NONBLOCK)
1532 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001533
Willy Tarreau698b1512008-11-22 11:29:33 +01001534 interruptible_sleep_on(&keypad_read_wait);
1535 if (signal_pending(current))
1536 return -EINTR;
1537 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001538
Willy Tarreau698b1512008-11-22 11:29:33 +01001539 for (; count-- > 0 && (keypad_buflen > 0); ++i, ++tmp, --keypad_buflen) {
1540 put_user(keypad_buffer[keypad_start], tmp);
1541 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1542 }
1543 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001544
Willy Tarreau698b1512008-11-22 11:29:33 +01001545 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001546}
1547
Willy Tarreau698b1512008-11-22 11:29:33 +01001548static int keypad_open(struct inode *inode, struct file *file)
1549{
Willy Tarreau7005b582008-11-13 17:18:59 -08001550
Willy Tarreau698b1512008-11-22 11:29:33 +01001551 if (keypad_open_cnt)
1552 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001553
Willy Tarreau698b1512008-11-22 11:29:33 +01001554 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1555 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001556
Willy Tarreau698b1512008-11-22 11:29:33 +01001557 keypad_buflen = 0; /* flush the buffer on opening */
1558 keypad_open_cnt++;
1559 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001560}
1561
Willy Tarreau698b1512008-11-22 11:29:33 +01001562static int keypad_release(struct inode *inode, struct file *file)
1563{
1564 keypad_open_cnt--;
1565 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001566}
1567
1568static struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001569 .read = keypad_read, /* read */
1570 .open = keypad_open, /* open */
1571 .release = keypad_release, /* close */
Willy Tarreau7005b582008-11-13 17:18:59 -08001572};
1573
1574static struct miscdevice keypad_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001575 KEYPAD_MINOR,
1576 "keypad",
1577 &keypad_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001578};
1579
Willy Tarreau698b1512008-11-22 11:29:33 +01001580static void keypad_send_key(char *string, int max_len)
1581{
1582 if (init_in_progress)
1583 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001584
Willy Tarreau698b1512008-11-22 11:29:33 +01001585 /* send the key to the device only if a process is attached to it. */
1586 if (keypad_open_cnt > 0) {
1587 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1588 keypad_buffer[(keypad_start + keypad_buflen++) %
1589 KEYPAD_BUFFER] = *string++;
1590 }
1591 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001592 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001593}
1594
Willy Tarreau7005b582008-11-13 17:18:59 -08001595/* this function scans all the bits involving at least one logical signal, and puts the
1596 * results in the bitfield "phys_read" (one bit per established contact), and sets
1597 * "phys_read_prev" to "phys_read".
1598 *
1599 * Note: to debounce input signals, we will only consider as switched a signal which is
1600 * stable across 2 measures. Signals which are different between two reads will be kept
1601 * as they previously were in their logical form (phys_prev). A signal which has just
1602 * switched will have a 1 in (phys_read ^ phys_read_prev).
1603 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001604static void phys_scan_contacts(void)
1605{
1606 int bit, bitval;
1607 char oldval;
1608 char bitmask;
1609 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001610
Willy Tarreau698b1512008-11-22 11:29:33 +01001611 phys_prev = phys_curr;
1612 phys_read_prev = phys_read;
1613 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001614
Willy Tarreau698b1512008-11-22 11:29:33 +01001615 oldval = r_dtr(pprt) | scan_mask_o; /* keep track of old value, with all outputs disabled */
1616 w_dtr(pprt, oldval & ~scan_mask_o); /* activate all keyboard outputs (active low) */
1617 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; /* will have a 1 for each bit set to gnd */
1618 w_dtr(pprt, oldval); /* disable all matrix signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001619
Willy Tarreau698b1512008-11-22 11:29:33 +01001620 /* now that all outputs are cleared, the only active input bits are
1621 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001622 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001623 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; /* 1 for each grounded input */
Willy Tarreau7005b582008-11-13 17:18:59 -08001624
Willy Tarreau698b1512008-11-22 11:29:33 +01001625 phys_read |= (pmask_t) gndmask << 40; /* grounded inputs are signals 40-44 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001626
Willy Tarreau698b1512008-11-22 11:29:33 +01001627 if (bitmask != gndmask) {
1628 /* since clearing the outputs changed some inputs, we know that some
1629 * input signals are currently tied to some outputs. So we'll scan them.
1630 */
1631 for (bit = 0; bit < 8; bit++) {
1632 bitval = 1 << bit;
1633
1634 if (!(scan_mask_o & bitval)) /* skip unused bits */
1635 continue;
1636
1637 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1638 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1639 phys_read |= (pmask_t) bitmask << (5 * bit);
1640 }
1641 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001642 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001643 /* this is easy: use old bits when they are flapping, use new ones when stable */
1644 phys_curr =
1645 (phys_prev & (phys_read ^ phys_read_prev)) | (phys_read &
1646 ~(phys_read ^
1647 phys_read_prev));
Willy Tarreau7005b582008-11-13 17:18:59 -08001648}
1649
Willy Tarreau698b1512008-11-22 11:29:33 +01001650static void panel_process_inputs(void)
1651{
1652 struct list_head *item;
1653 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001654
1655#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001656 printk(KERN_DEBUG
1657 "entering panel_process_inputs with pp=%016Lx & pc=%016Lx\n",
1658 phys_prev, phys_curr);
Willy Tarreau7005b582008-11-13 17:18:59 -08001659#endif
1660
Willy Tarreau698b1512008-11-22 11:29:33 +01001661 keypressed = 0;
1662 inputs_stable = 1;
1663 list_for_each(item, &logical_inputs) {
1664 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001665
Willy Tarreau698b1512008-11-22 11:29:33 +01001666 switch (input->state) {
1667 case INPUT_ST_LOW:
1668 if ((phys_curr & input->mask) != input->value)
1669 break;
1670 /* if all needed ones were already set previously, this means that
1671 * this logical signal has been activated by the releasing of
1672 * another combined signal, so we don't want to match.
1673 * eg: AB -(release B)-> A -(release A)-> 0 : don't match A.
1674 */
1675 if ((phys_prev & input->mask) == input->value)
1676 break;
1677 input->rise_timer = 0;
1678 input->state = INPUT_ST_RISING;
1679 /* no break here, fall through */
1680 case INPUT_ST_RISING:
1681 if ((phys_curr & input->mask) != input->value) {
1682 input->state = INPUT_ST_LOW;
1683 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001684 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001685 if (input->rise_timer < input->rise_time) {
1686 inputs_stable = 0;
1687 input->rise_timer++;
1688 break;
1689 }
1690 input->high_timer = 0;
1691 input->state = INPUT_ST_HIGH;
1692 /* no break here, fall through */
1693 case INPUT_ST_HIGH:
Willy Tarreau7005b582008-11-13 17:18:59 -08001694#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001695 /* FIXME:
1696 * this is an invalid test. It tries to catch transitions from single-key
1697 * to multiple-key, but doesn't take into account the contacts polarity.
1698 * The only solution to the problem is to parse keys from the most complex
1699 * to the simplest combinations, and mark them as 'caught' once a combination
1700 * matches, then unmatch it for all other ones.
1701 */
1702
1703 /* try to catch dangerous transitions cases :
1704 * someone adds a bit, so this signal was a false
1705 * positive resulting from a transition. We should invalidate
1706 * the signal immediately and not call the release function.
1707 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1708 */
1709 if (((phys_prev & input->mask) == input->value)
1710 && ((phys_curr & input->mask) > input->value)) {
1711 input->state = INPUT_ST_LOW; /* invalidate */
1712 break;
1713 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001714#endif
1715
Willy Tarreau698b1512008-11-22 11:29:33 +01001716 if ((phys_curr & input->mask) == input->value) {
1717 if ((input->type == INPUT_TYPE_STD)
1718 && (input->high_timer == 0)) {
1719 input->high_timer++;
1720 if (input->u.std.press_fct != NULL)
1721 input->u.std.press_fct(input->u.
1722 std.
1723 press_data);
1724 } else if (input->type == INPUT_TYPE_KBD) {
1725 keypressed = 1; /* will turn on the light */
Willy Tarreau7005b582008-11-13 17:18:59 -08001726
Willy Tarreau698b1512008-11-22 11:29:33 +01001727 if (input->high_timer == 0) {
1728 if (input->u.kbd.press_str[0])
1729 keypad_send_key(input->
1730 u.kbd.
1731 press_str,
1732 sizeof
1733 (input->
1734 u.kbd.
1735 press_str));
1736 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001737
Willy Tarreau698b1512008-11-22 11:29:33 +01001738 if (input->u.kbd.repeat_str[0]) {
1739 if (input->high_timer >=
1740 KEYPAD_REP_START) {
1741 input->high_timer -=
1742 KEYPAD_REP_DELAY;
1743 keypad_send_key(input->
1744 u.kbd.
1745 repeat_str,
1746 sizeof
1747 (input->
1748 u.kbd.
1749 repeat_str));
1750 }
1751 inputs_stable = 0; /* we will need to come back here soon */
1752 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001753
Willy Tarreau698b1512008-11-22 11:29:33 +01001754 if (input->high_timer < 255)
1755 input->high_timer++;
1756 }
1757 break;
1758 } else {
1759 /* else signal falling down. Let's fall through. */
1760 input->state = INPUT_ST_FALLING;
1761 input->fall_timer = 0;
1762 }
1763 /* no break here, fall through */
1764 case INPUT_ST_FALLING:
1765#if 0
1766 /* FIXME !!! same comment as above */
1767 if (((phys_prev & input->mask) == input->value)
1768 && ((phys_curr & input->mask) > input->value)) {
1769 input->state = INPUT_ST_LOW; /* invalidate */
1770 break;
1771 }
1772#endif
1773
1774 if ((phys_curr & input->mask) == input->value) {
1775 if (input->type == INPUT_TYPE_KBD) {
1776 keypressed = 1; /* will turn on the light */
1777
1778 if (input->u.kbd.repeat_str[0]) {
1779 if (input->high_timer >= KEYPAD_REP_START)
1780 input->high_timer -= KEYPAD_REP_DELAY;
1781 keypad_send_key(input->u.kbd.repeat_str,
1782 sizeof(input->u.kbd.repeat_str));
1783 inputs_stable = 0; /* we will need to come back here soon */
1784 }
1785
1786 if (input->high_timer < 255)
1787 input->high_timer++;
1788 }
1789 input->state = INPUT_ST_HIGH;
1790 break;
1791 } else if (input->fall_timer >= input->fall_time) {
1792 /* call release event */
1793 if (input->type == INPUT_TYPE_STD) {
1794 if (input->u.std.release_fct != NULL)
1795 input->u.std.release_fct(input->u.std.release_data);
1796
1797 } else if (input->type == INPUT_TYPE_KBD) {
1798 if (input->u.kbd.release_str[0])
1799 keypad_send_key(input->u.kbd.release_str,
1800 sizeof(input->u.kbd.release_str));
1801 }
1802
1803 input->state = INPUT_ST_LOW;
1804 break;
1805 } else {
1806 input->fall_timer++;
1807 inputs_stable = 0;
1808 break;
1809 }
1810 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001811 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001812}
1813
Willy Tarreau698b1512008-11-22 11:29:33 +01001814static void panel_scan_timer(void)
1815{
Willy Tarreau630231772008-11-22 12:52:18 +01001816 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001817 if (spin_trylock(&pprt_lock)) {
1818 phys_scan_contacts();
1819 spin_unlock(&pprt_lock); /* no need for the parport anymore */
1820 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001821
Willy Tarreau698b1512008-11-22 11:29:33 +01001822 if (!inputs_stable || phys_curr != phys_prev)
1823 panel_process_inputs();
1824 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001825
Willy Tarreau698b1512008-11-22 11:29:33 +01001826 if (lcd_enabled && lcd_initialized) {
1827 if (keypressed) {
1828 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1829 lcd_backlight(1);
1830 light_tempo = FLASH_LIGHT_TEMPO;
1831 } else if (light_tempo > 0) {
1832 light_tempo--;
1833 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1834 lcd_backlight(0);
1835 }
1836 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001837
Willy Tarreau698b1512008-11-22 11:29:33 +01001838 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001839}
1840
Willy Tarreau698b1512008-11-22 11:29:33 +01001841static void init_scan_timer(void)
1842{
1843 if (scan_timer.function != NULL)
1844 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001845
Willy Tarreau698b1512008-11-22 11:29:33 +01001846 init_timer(&scan_timer);
1847 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1848 scan_timer.data = 0;
1849 scan_timer.function = (void *)&panel_scan_timer;
1850 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001851}
1852
1853/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1854 * if <omask> or <imask> are non-null, they will be or'ed with the bits corresponding
1855 * to out and in bits respectively.
1856 * returns 1 if ok, 0 if error (in which case, nothing is written).
1857 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001858static int input_name2mask(char *name, pmask_t *mask, pmask_t *value,
1859 char *imask, char *omask)
1860{
1861 static char sigtab[10] = "EeSsPpAaBb";
1862 char im, om;
1863 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08001864
Willy Tarreau698b1512008-11-22 11:29:33 +01001865 om = im = m = v = 0ULL;
1866 while (*name) {
1867 int in, out, bit, neg;
1868 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name); in++)
1869 ;
1870 if (in >= sizeof(sigtab))
1871 return 0; /* input name not found */
1872 neg = (in & 1); /* odd (lower) names are negated */
1873 in >>= 1;
1874 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08001875
Willy Tarreau698b1512008-11-22 11:29:33 +01001876 name++;
1877 if (isdigit(*name)) {
1878 out = *name - '0';
1879 om |= (1 << out);
1880 } else if (*name == '-')
1881 out = 8;
1882 else
1883 return 0; /* unknown bit name */
1884
1885 bit = (out * 5) + in;
1886
1887 m |= 1ULL << bit;
1888 if (!neg)
1889 v |= 1ULL << bit;
1890 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08001891 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001892 *mask = m;
1893 *value = v;
1894 if (imask)
1895 *imask |= im;
1896 if (omask)
1897 *omask |= om;
1898 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001899}
1900
1901/* tries to bind a key to the signal name <name>. The key will send the
1902 * strings <press>, <repeat>, <release> for these respective events.
1903 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1904 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001905static struct logical_input *panel_bind_key(char *name, char *press,
1906 char *repeat, char *release)
1907{
1908 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001909
Willy Tarreau698b1512008-11-22 11:29:33 +01001910 key = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
1911 if (!key) {
1912 printk(KERN_ERR "panel: not enough memory\n");
1913 return NULL;
1914 }
1915 memset(key, 0, sizeof(struct logical_input));
1916 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1917 &scan_mask_o))
1918 return NULL;
1919
1920 key->type = INPUT_TYPE_KBD;
1921 key->state = INPUT_ST_LOW;
1922 key->rise_time = 1;
1923 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001924
1925#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001926 printk(KERN_DEBUG "bind: <%s> : m=%016Lx v=%016Lx\n", name, key->mask,
1927 key->value);
Willy Tarreau7005b582008-11-13 17:18:59 -08001928#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001929 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
1930 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
1931 strncpy(key->u.kbd.release_str, release,
1932 sizeof(key->u.kbd.release_str));
1933 list_add(&key->list, &logical_inputs);
1934 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001935}
1936
Willy Tarreau630231772008-11-22 12:52:18 +01001937#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08001938/* tries to bind a callback function to the signal name <name>. The function
1939 * <press_fct> will be called with the <press_data> arg when the signal is
1940 * activated, and so on for <release_fct>/<release_data>
1941 * Returns the pointer to the new signal if ok, NULL if the signal could not be bound.
1942 */
1943static struct logical_input *panel_bind_callback(char *name,
Willy Tarreau698b1512008-11-22 11:29:33 +01001944 void (*press_fct) (int),
1945 int press_data,
1946 void (*release_fct) (int),
1947 int release_data)
1948{
1949 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08001950
Willy Tarreau698b1512008-11-22 11:29:33 +01001951 callback = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
1952 if (!callback) {
1953 printk(KERN_ERR "panel: not enough memory\n");
1954 return NULL;
1955 }
1956 memset(callback, 0, sizeof(struct logical_input));
1957 if (!input_name2mask(name, &callback->mask, &callback->value,
1958 &scan_mask_i, &scan_mask_o))
1959 return NULL;
1960
1961 callback->type = INPUT_TYPE_STD;
1962 callback->state = INPUT_ST_LOW;
1963 callback->rise_time = 1;
1964 callback->fall_time = 1;
1965 callback->u.std.press_fct = press_fct;
1966 callback->u.std.press_data = press_data;
1967 callback->u.std.release_fct = release_fct;
1968 callback->u.std.release_data = release_data;
1969 list_add(&callback->list, &logical_inputs);
1970 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08001971}
Willy Tarreau630231772008-11-22 12:52:18 +01001972#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08001973
Willy Tarreau698b1512008-11-22 11:29:33 +01001974static void keypad_init(void)
1975{
1976 int keynum;
1977 init_waitqueue_head(&keypad_read_wait);
1978 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08001979
Willy Tarreau698b1512008-11-22 11:29:33 +01001980 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08001981
Willy Tarreau698b1512008-11-22 11:29:33 +01001982 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
1983 panel_bind_key(keypad_profile[keynum][0],
1984 keypad_profile[keynum][1],
1985 keypad_profile[keynum][2],
1986 keypad_profile[keynum][3]);
1987 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001988
Willy Tarreau698b1512008-11-22 11:29:33 +01001989 init_scan_timer();
1990 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001991}
1992
Willy Tarreau7005b582008-11-13 17:18:59 -08001993/**************************************************/
1994/* device initialization */
1995/**************************************************/
1996
Willy Tarreau698b1512008-11-22 11:29:33 +01001997static int panel_notify_sys(struct notifier_block *this, unsigned long code,
1998 void *unused)
1999{
2000 if (lcd_enabled && lcd_initialized) {
2001 switch (code) {
2002 case SYS_DOWN:
2003 panel_lcd_print
2004 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2005 break;
2006 case SYS_HALT:
2007 panel_lcd_print
2008 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2009 break;
2010 case SYS_POWER_OFF:
2011 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2012 break;
2013 default:
2014 break;
2015 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002016 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002017 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002018}
2019
2020static struct notifier_block panel_notifier = {
2021 panel_notify_sys,
2022 NULL,
2023 0
2024};
2025
Willy Tarreau698b1512008-11-22 11:29:33 +01002026static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002027{
Willy Tarreau698b1512008-11-22 11:29:33 +01002028 if (port->number != parport)
2029 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002030
Willy Tarreau698b1512008-11-22 11:29:33 +01002031 if (pprt) {
2032 printk(KERN_ERR
2033 "panel_attach(): port->number=%d parport=%d, already registered !\n",
2034 port->number, parport);
2035 return;
2036 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002037
Willy Tarreau698b1512008-11-22 11:29:33 +01002038 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
2039 NULL,
2040 /*PARPORT_DEV_EXCL */
2041 0, (void *)&pprt);
Willy Tarreau7005b582008-11-13 17:18:59 -08002042
Willy Tarreau698b1512008-11-22 11:29:33 +01002043 if (parport_claim(pprt)) {
2044 printk(KERN_ERR
2045 "Panel: could not claim access to parport%d. Aborting.\n",
2046 parport);
2047 return;
2048 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002049
Willy Tarreau698b1512008-11-22 11:29:33 +01002050 /* must init LCD first, just in case an IRQ from the keypad is generated at keypad init */
2051 if (lcd_enabled) {
2052 lcd_init();
2053 misc_register(&lcd_dev);
2054 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002055
Willy Tarreau698b1512008-11-22 11:29:33 +01002056 if (keypad_enabled) {
2057 keypad_init();
2058 misc_register(&keypad_dev);
2059 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002060}
2061
Willy Tarreau698b1512008-11-22 11:29:33 +01002062static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002063{
Willy Tarreau698b1512008-11-22 11:29:33 +01002064 if (port->number != parport)
2065 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002066
Willy Tarreau698b1512008-11-22 11:29:33 +01002067 if (!pprt) {
2068 printk(KERN_ERR
2069 "panel_detach(): port->number=%d parport=%d, nothing to unregister.\n",
2070 port->number, parport);
2071 return;
2072 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002073
Willy Tarreau698b1512008-11-22 11:29:33 +01002074 if (keypad_enabled && keypad_initialized)
2075 misc_deregister(&keypad_dev);
Willy Tarreau7005b582008-11-13 17:18:59 -08002076
Willy Tarreau698b1512008-11-22 11:29:33 +01002077 if (lcd_enabled && lcd_initialized)
2078 misc_deregister(&lcd_dev);
Willy Tarreau7005b582008-11-13 17:18:59 -08002079
Willy Tarreau698b1512008-11-22 11:29:33 +01002080 parport_release(pprt);
2081 parport_unregister_device(pprt);
2082 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002083}
2084
2085static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002086 .name = "panel",
2087 .attach = panel_attach,
2088 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002089};
2090
2091/* init function */
Willy Tarreau698b1512008-11-22 11:29:33 +01002092int panel_init(void)
2093{
2094 /* for backwards compatibility */
2095 if (keypad_type < 0)
2096 keypad_type = keypad_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002097
Willy Tarreau698b1512008-11-22 11:29:33 +01002098 if (lcd_type < 0)
2099 lcd_type = lcd_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002100
Willy Tarreau698b1512008-11-22 11:29:33 +01002101 if (parport < 0)
2102 parport = DEFAULT_PARPORT;
Willy Tarreau7005b582008-11-13 17:18:59 -08002103
Willy Tarreau698b1512008-11-22 11:29:33 +01002104 /* take care of an eventual profile */
2105 switch (profile) {
2106 case PANEL_PROFILE_CUSTOM: /* custom profile */
2107 if (keypad_type < 0)
2108 keypad_type = DEFAULT_KEYPAD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002109 if (lcd_type < 0)
2110 lcd_type = DEFAULT_LCD;
2111 break;
2112 case PANEL_PROFILE_OLD: /* 8 bits, 2*16, old keypad */
2113 if (keypad_type < 0)
2114 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002115 if (lcd_type < 0)
2116 lcd_type = LCD_TYPE_OLD;
2117 if (lcd_width < 0)
2118 lcd_width = 16;
2119 if (lcd_hwidth < 0)
2120 lcd_hwidth = 16;
2121 break;
2122 case PANEL_PROFILE_NEW: /* serial, 2*16, new keypad */
2123 if (keypad_type < 0)
2124 keypad_type = KEYPAD_TYPE_NEW;
Willy Tarreau698b1512008-11-22 11:29:33 +01002125 if (lcd_type < 0)
2126 lcd_type = LCD_TYPE_KS0074;
2127 break;
2128 case PANEL_PROFILE_HANTRONIX: /* 8 bits, 2*16 hantronix-like, no keypad */
2129 if (keypad_type < 0)
2130 keypad_type = KEYPAD_TYPE_NONE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002131 if (lcd_type < 0)
2132 lcd_type = LCD_TYPE_HANTRONIX;
2133 break;
2134 case PANEL_PROFILE_NEXCOM: /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
2135 if (keypad_type < 0)
2136 keypad_type = KEYPAD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002137 if (lcd_type < 0)
2138 lcd_type = LCD_TYPE_NEXCOM;
2139 break;
2140 case PANEL_PROFILE_LARGE: /* 8 bits, 2*40, old keypad */
2141 if (keypad_type < 0)
2142 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002143 if (lcd_type < 0)
2144 lcd_type = LCD_TYPE_OLD;
2145 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002146 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002147
Willy Tarreau698b1512008-11-22 11:29:33 +01002148 lcd_enabled = (lcd_type > 0);
2149 keypad_enabled = (keypad_type > 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08002150
Willy Tarreau698b1512008-11-22 11:29:33 +01002151 switch (keypad_type) {
2152 case KEYPAD_TYPE_OLD:
2153 keypad_profile = old_keypad_profile;
2154 break;
2155 case KEYPAD_TYPE_NEW:
2156 keypad_profile = new_keypad_profile;
2157 break;
2158 case KEYPAD_TYPE_NEXCOM:
2159 keypad_profile = nexcom_keypad_profile;
2160 break;
2161 default:
2162 keypad_profile = NULL;
2163 break;
2164 }
2165
2166 /* tells various subsystems about the fact that we are initializing */
2167 init_in_progress = 1;
2168
2169 if (parport_register_driver(&panel_driver)) {
2170 printk(KERN_ERR
2171 "Panel: could not register with parport. Aborting.\n");
2172 return -EIO;
2173 }
2174
Willy Tarreau630231772008-11-22 12:52:18 +01002175 if (!lcd_enabled && !keypad_enabled) {
2176 /* no device enabled, let's release the parport */
Willy Tarreau698b1512008-11-22 11:29:33 +01002177 if (pprt) {
2178 parport_release(pprt);
2179 parport_unregister_device(pprt);
2180 }
2181 parport_unregister_driver(&panel_driver);
2182 printk(KERN_ERR "Panel driver version " PANEL_VERSION
2183 " disabled.\n");
2184 return -ENODEV;
2185 }
2186
2187 register_reboot_notifier(&panel_notifier);
2188
2189 if (pprt)
2190 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2191 " registered on parport%d (io=0x%lx).\n", parport,
2192 pprt->port->base);
2193 else
2194 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2195 " not yet registered\n");
2196 /* tells various subsystems about the fact that initialization is finished */
2197 init_in_progress = 0;
2198 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002199}
2200
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002201static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002202{
2203 return panel_init();
Willy Tarreau7005b582008-11-13 17:18:59 -08002204}
2205
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002206static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002207{
2208 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002209
Willy Tarreau698b1512008-11-22 11:29:33 +01002210 if (scan_timer.function != NULL)
2211 del_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002212
Costantino Leandro57898132009-02-17 11:10:48 -05002213 if (pprt != NULL) {
2214 if (keypad_enabled)
2215 misc_deregister(&keypad_dev);
Willy Tarreau7005b582008-11-13 17:18:59 -08002216
Costantino Leandro57898132009-02-17 11:10:48 -05002217 if (lcd_enabled) {
2218 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2219 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2220 misc_deregister(&lcd_dev);
2221 }
2222
2223 /* TODO: free all input signals */
2224 parport_release(pprt);
2225 parport_unregister_device(pprt);
Willy Tarreau698b1512008-11-22 11:29:33 +01002226 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002227 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002228}
Willy Tarreau7005b582008-11-13 17:18:59 -08002229
Willy Tarreau7005b582008-11-13 17:18:59 -08002230module_init(panel_init_module);
2231module_exit(panel_cleanup_module);
2232MODULE_AUTHOR("Willy Tarreau");
2233MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002234
2235/*
2236 * Local variables:
2237 * c-indent-level: 4
2238 * tab-width: 8
2239 * End:
2240 */