blob: 5ffe269c238298327f07bae3bb0b1dff6e190247 [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
246
247/*
248 * LCD character sets
249 */
250#define LCD_CHARSET_NORMAL 0
251#define LCD_CHARSET_KS0074 1
252
253/*
254 * LCD types
255 */
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
262
263/*
264 * keypad types
265 */
266#define KEYPAD_TYPE_NONE 0
267#define KEYPAD_TYPE_OLD 1
268#define KEYPAD_TYPE_NEW 2
269#define KEYPAD_TYPE_NEXCOM 3
270
271/*
272 * panel profiles
273 */
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
280
281/*
282 * Construct custom config from the kernel's configuration
283 */
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
Willy Tarreau7005b582008-11-13 17:18:59 -0800288#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
293
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
301
302#ifdef CONFIG_PANEL_PROFILE
303#undef DEFAULT_PROFILE
304#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
305#endif
306
307#ifdef CONFIG_PANEL_PARPORT
308#undef DEFAULT_PARPORT
309#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
310#endif
311
Willy Tarreau698b1512008-11-22 11:29:33 +0100312#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800313#ifdef CONFIG_PANEL_KEYPAD
314#undef DEFAULT_KEYPAD
315#define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
316#endif
317
Willy Tarreau7005b582008-11-13 17:18:59 -0800318#ifdef CONFIG_PANEL_LCD
319#undef DEFAULT_LCD
320#define DEFAULT_LCD CONFIG_PANEL_LCD
321#endif
322
323#ifdef CONFIG_PANEL_LCD_WIDTH
324#undef DEFAULT_LCD_WIDTH
325#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
326#endif
327
328#ifdef CONFIG_PANEL_LCD_BWIDTH
329#undef DEFAULT_LCD_BWIDTH
330#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
331#endif
332
333#ifdef CONFIG_PANEL_LCD_HWIDTH
334#undef DEFAULT_LCD_HWIDTH
335#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
336#endif
337
338#ifdef CONFIG_PANEL_LCD_HEIGHT
339#undef DEFAULT_LCD_HEIGHT
340#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
341#endif
342
343#ifdef CONFIG_PANEL_LCD_PROTO
344#undef DEFAULT_LCD_PROTO
345#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
346#endif
347
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
352
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
357
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
362
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
367
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
372
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
377
378#ifdef CONFIG_PANEL_LCD_CHARSET
379#undef DEFAULT_LCD_CHARSET
380#define DEFAULT_LCD_CHARSET
381#endif
382
383#endif /* DEFAULT_PROFILE == 0 */
384
385/* global variables */
Willy Tarreau698b1512008-11-22 11:29:33 +0100386static int keypad_open_cnt; /* #times opened */
387static int lcd_open_cnt; /* #times opened */
Willy Tarreau698b1512008-11-22 11:29:33 +0100388static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800389
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100390static int lcd_initialized;
391static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800392
Willy Tarreau698b1512008-11-22 11:29:33 +0100393static int light_tempo;
Willy Tarreau7005b582008-11-13 17:18:59 -0800394
Willy Tarreau698b1512008-11-22 11:29:33 +0100395static char lcd_must_clear;
396static char lcd_left_shift;
397static char init_in_progress;
Willy Tarreau7005b582008-11-13 17:18:59 -0800398
Willy Tarreau698b1512008-11-22 11:29:33 +0100399static void (*lcd_write_cmd) (int);
400static void (*lcd_write_data) (int);
401static void (*lcd_clear_fast) (void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800402
Willy Tarreau698b1512008-11-22 11:29:33 +0100403static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800404static struct timer_list scan_timer;
405
Willy Tarreau630231772008-11-22 12:52:18 +0100406MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100407
408static int parport = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100409module_param(parport, int, 0000);
410MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100411
412static int lcd_height = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100413module_param(lcd_height, int, 0000);
414MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100415
416static int lcd_width = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100417module_param(lcd_width, int, 0000);
418MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100419
420static int lcd_bwidth = -1; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100421module_param(lcd_bwidth, int, 0000);
422MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100423
424static int lcd_hwidth = -1; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100425module_param(lcd_hwidth, int, 0000);
426MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100427
428static int lcd_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100429module_param(lcd_enabled, int, 0000);
430MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100431
432static int keypad_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100433module_param(keypad_enabled, int, 0000);
434MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100435
436static int lcd_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100437module_param(lcd_type, int, 0000);
438MODULE_PARM_DESC(lcd_type,
439 "LCD type: 0=none, 1=old //, 2=serial ks0074, 3=hantronix //, 4=nexcom //, 5=compiled-in");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100440
441static int lcd_proto = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100442module_param(lcd_proto, int, 0000);
443MODULE_PARM_DESC(lcd_proto, "LCD communication: 0=parallel (//), 1=serial");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100444
445static int lcd_charset = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100446module_param(lcd_charset, int, 0000);
447MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100448
449static int keypad_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100450module_param(keypad_type, int, 0000);
451MODULE_PARM_DESC(keypad_type,
452 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100453
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100454static int profile = DEFAULT_PROFILE;
Willy Tarreau698b1512008-11-22 11:29:33 +0100455module_param(profile, int, 0000);
456MODULE_PARM_DESC(profile,
457 "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 -0800458
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100459/*
460 * These are the parallel port pins the LCD control signals are connected to.
461 * Set this to 0 if the signal is not used. Set it to its opposite value
462 * (negative) if the signal is negated. -MAXINT is used to indicate that the
463 * pin has not been explicitly specified.
464 *
Willy Tarreau630231772008-11-22 12:52:18 +0100465 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100466 */
467
468static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100469module_param(lcd_e_pin, int, 0000);
470MODULE_PARM_DESC(lcd_e_pin,
471 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100472
473static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100474module_param(lcd_rs_pin, int, 0000);
475MODULE_PARM_DESC(lcd_rs_pin,
476 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100477
478static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100479module_param(lcd_rw_pin, int, 0000);
480MODULE_PARM_DESC(lcd_rw_pin,
481 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100482
483static int lcd_bl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100484module_param(lcd_bl_pin, int, 0000);
485MODULE_PARM_DESC(lcd_bl_pin,
486 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100487
488static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100489module_param(lcd_da_pin, int, 0000);
490MODULE_PARM_DESC(lcd_da_pin,
491 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100492
493static int lcd_cl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100494module_param(lcd_cl_pin, int, 0000);
495MODULE_PARM_DESC(lcd_cl_pin,
496 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreau7005b582008-11-13 17:18:59 -0800497
Willy Tarreau698b1512008-11-22 11:29:33 +0100498static unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800499
500/* for some LCD drivers (ks0074) we need a charset conversion table. */
501static unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100502 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
503 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
504 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
505 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
506 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
507 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
508 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
509 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
510 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
511 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
512 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
513 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
514 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
515 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
516 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
517 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
518 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
519 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
520 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
521 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
522 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
523 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
524 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
525 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
526 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
527 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
528 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
529 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
530 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
531 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
532 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
533 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
534 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800535};
536
537char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100538 {"S0", "Left\n", "Left\n", ""},
539 {"S1", "Down\n", "Down\n", ""},
540 {"S2", "Up\n", "Up\n", ""},
541 {"S3", "Right\n", "Right\n", ""},
542 {"S4", "Esc\n", "Esc\n", ""},
543 {"S5", "Ret\n", "Ret\n", ""},
544 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800545};
546
547/* signals, press, repeat, release */
548char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100549 {"S0", "Left\n", "Left\n", ""},
550 {"S1", "Down\n", "Down\n", ""},
551 {"S2", "Up\n", "Up\n", ""},
552 {"S3", "Right\n", "Right\n", ""},
553 {"S4s5", "", "Esc\n", "Esc\n"},
554 {"s4S5", "", "Ret\n", "Ret\n"},
555 {"S4S5", "Help\n", "", ""},
556 /* add new signals above this line */
557 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800558};
559
560/* signals, press, repeat, release */
561char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100562 {"a-p-e-", "Down\n", "Down\n", ""},
563 {"a-p-E-", "Ret\n", "Ret\n", ""},
564 {"a-P-E-", "Esc\n", "Esc\n", ""},
565 {"a-P-e-", "Up\n", "Up\n", ""},
566 /* add new signals above this line */
567 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800568};
569
570static char (*keypad_profile)[4][9] = old_keypad_profile;
571
572/* FIXME: this should be converted to a bit array containing signals states */
573static struct {
Willy Tarreau698b1512008-11-22 11:29:33 +0100574 unsigned char e; /* parallel LCD E (data latch on falling edge) */
575 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
576 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
577 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
578 unsigned char cl; /* serial LCD clock (latch on rising edge) */
579 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800580} bits;
581
582static void init_scan_timer(void);
583
584/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100585static int set_data_bits(void)
586{
587 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800588
Willy Tarreau698b1512008-11-22 11:29:33 +0100589 val = r_dtr(pprt);
590 for (bit = 0; bit < LCD_BITS; bit++)
591 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800592
Willy Tarreau698b1512008-11-22 11:29:33 +0100593 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
594 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
595 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
596 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
597 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
598 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800599
Willy Tarreau698b1512008-11-22 11:29:33 +0100600 w_dtr(pprt, val);
601 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800602}
603
604/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100605static int set_ctrl_bits(void)
606{
607 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800608
Willy Tarreau698b1512008-11-22 11:29:33 +0100609 val = r_ctr(pprt);
610 for (bit = 0; bit < LCD_BITS; bit++)
611 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800612
Willy Tarreau698b1512008-11-22 11:29:33 +0100613 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
614 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
615 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
616 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
617 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
618 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800619
Willy Tarreau698b1512008-11-22 11:29:33 +0100620 w_ctr(pprt, val);
621 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800622}
623
624/* sets ctrl & data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100625static void set_bits(void)
626{
627 set_data_bits();
628 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800629}
630
631/*
632 * Converts a parallel port pin (from -25 to 25) to data and control ports
633 * masks, and data and control port bits. The signal will be considered
634 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
635 *
636 * Result will be used this way :
637 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
638 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
639 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100640void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
641{
642 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800643
Willy Tarreau698b1512008-11-22 11:29:33 +0100644 d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
645 d_val[2] = c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800646
Willy Tarreau698b1512008-11-22 11:29:33 +0100647 if (pin == 0)
648 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800649
Willy Tarreau698b1512008-11-22 11:29:33 +0100650 inv = (pin < 0);
651 if (inv)
652 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800653
Willy Tarreau698b1512008-11-22 11:29:33 +0100654 d_bit = c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800655
Willy Tarreau698b1512008-11-22 11:29:33 +0100656 switch (pin) {
657 case PIN_STROBE: /* strobe, inverted */
658 c_bit = PNL_PSTROBE;
659 inv = !inv;
660 break;
661 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
662 d_bit = 1 << (pin - 2);
663 break;
664 case PIN_AUTOLF: /* autofeed, inverted */
665 c_bit = PNL_PAUTOLF;
666 inv = !inv;
667 break;
668 case PIN_INITP: /* init, direct */
669 c_bit = PNL_PINITP;
670 break;
671 case PIN_SELECP: /* select_in, inverted */
672 c_bit = PNL_PSELECP;
673 inv = !inv;
674 break;
675 default: /* unknown pin, ignore */
676 break;
677 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800678
Willy Tarreau698b1512008-11-22 11:29:33 +0100679 if (c_bit) {
680 c_val[2] &= ~c_bit;
681 c_val[!inv] = c_bit;
682 } else if (d_bit) {
683 d_val[2] &= ~d_bit;
684 d_val[!inv] = d_bit;
685 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800686}
687
688/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100689static void long_sleep(int ms)
690{
Willy Tarreau7005b582008-11-13 17:18:59 -0800691
Willy Tarreau698b1512008-11-22 11:29:33 +0100692 if (in_interrupt())
693 mdelay(ms);
694 else {
695 current->state = TASK_INTERRUPTIBLE;
696 schedule_timeout((ms * HZ + 999) / 1000);
697 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800698}
699
Willy Tarreau7005b582008-11-13 17:18:59 -0800700/* send a serial byte to the LCD panel. The caller is responsible for locking if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100701static void lcd_send_serial(int byte)
702{
703 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800704
Willy Tarreau698b1512008-11-22 11:29:33 +0100705 /* the data bit is set on D0, and the clock on STROBE.
706 * LCD reads D0 on STROBE's rising edge.
707 */
708 for (bit = 0; bit < 8; bit++) {
709 bits.cl = BIT_CLR; /* CLK low */
710 set_bits();
711 bits.da = byte & 1;
712 set_bits();
713 udelay(2); /* maintain the data during 2 us before CLK up */
714 bits.cl = BIT_SET; /* CLK high */
715 set_bits();
716 udelay(1); /* maintain the strobe during 1 us */
717 byte >>= 1;
718 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800719}
720
721/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100722static void lcd_backlight(int on)
723{
724 if (lcd_bl_pin == PIN_NONE)
725 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800726
Willy Tarreau698b1512008-11-22 11:29:33 +0100727 /* The backlight is activated by seting the AUTOFEED line to +5V */
728 spin_lock(&pprt_lock);
729 bits.bl = on;
730 set_bits();
731 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800732}
733
734/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100735static void lcd_write_cmd_s(int cmd)
736{
737 spin_lock(&pprt_lock);
738 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
739 lcd_send_serial(cmd & 0x0F);
740 lcd_send_serial((cmd >> 4) & 0x0F);
741 udelay(40); /* the shortest command takes at least 40 us */
742 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800743}
744
745/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100746static void lcd_write_data_s(int data)
747{
748 spin_lock(&pprt_lock);
749 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
750 lcd_send_serial(data & 0x0F);
751 lcd_send_serial((data >> 4) & 0x0F);
752 udelay(40); /* the shortest data takes at least 40 us */
753 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800754}
755
756/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100757static void lcd_write_cmd_p8(int cmd)
758{
759 spin_lock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800760 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100761 w_dtr(pprt, cmd);
762 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800763
Willy Tarreau698b1512008-11-22 11:29:33 +0100764 bits.e = BIT_SET;
765 bits.rs = BIT_CLR;
766 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800767 set_ctrl_bits();
768
Willy Tarreau698b1512008-11-22 11:29:33 +0100769 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800770
771 bits.e = BIT_CLR;
772 set_ctrl_bits();
773
Willy Tarreau698b1512008-11-22 11:29:33 +0100774 udelay(120); /* the shortest command takes at least 120 us */
775 spin_unlock(&pprt_lock);
776}
Willy Tarreau7005b582008-11-13 17:18:59 -0800777
Willy Tarreau698b1512008-11-22 11:29:33 +0100778/* send data to the LCD panel in 8 bits parallel mode */
779static void lcd_write_data_p8(int data)
780{
781 spin_lock(&pprt_lock);
782 /* present the data to the data port */
783 w_dtr(pprt, data);
784 udelay(20); /* maintain the data during 20 us before the strobe */
785
786 bits.e = BIT_SET;
787 bits.rs = BIT_SET;
788 bits.rw = BIT_CLR;
789 set_ctrl_bits();
790
791 udelay(40); /* maintain the strobe during 40 us */
792
793 bits.e = BIT_CLR;
794 set_ctrl_bits();
795
796 udelay(45); /* the shortest data takes at least 45 us */
797 spin_unlock(&pprt_lock);
798}
799
800static void lcd_gotoxy(void)
801{
802 lcd_write_cmd(0x80 /* set DDRAM address */
803 | (lcd_addr_y ? lcd_hwidth : 0)
804 /* we force the cursor to stay at the end of the line if it wants to go farther */
805 | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
806 (lcd_hwidth - 1) : lcd_bwidth - 1));
807}
808
809static void lcd_print(char c)
810{
811 if (lcd_addr_x < lcd_bwidth) {
812 if (lcd_char_conv != NULL)
813 c = lcd_char_conv[(unsigned char)c];
814 lcd_write_data(c);
815 lcd_addr_x++;
816 }
817 /* prevents the cursor from wrapping onto the next line */
818 if (lcd_addr_x == lcd_bwidth)
819 lcd_gotoxy();
820}
821
822/* fills the display with spaces and resets X/Y */
823static void lcd_clear_fast_s(void)
824{
825 int pos;
826 lcd_addr_x = lcd_addr_y = 0;
827 lcd_gotoxy();
828
829 spin_lock(&pprt_lock);
830 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
831 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
832 lcd_send_serial(' ' & 0x0F);
833 lcd_send_serial((' ' >> 4) & 0x0F);
834 udelay(40); /* the shortest data takes at least 40 us */
835 }
836 spin_unlock(&pprt_lock);
837
838 lcd_addr_x = lcd_addr_y = 0;
839 lcd_gotoxy();
840}
841
842/* fills the display with spaces and resets X/Y */
843static void lcd_clear_fast_p8(void)
844{
845 int pos;
846 lcd_addr_x = lcd_addr_y = 0;
847 lcd_gotoxy();
848
849 spin_lock(&pprt_lock);
850 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
851 /* present the data to the data port */
852 w_dtr(pprt, ' ');
853 udelay(20); /* maintain the data during 20 us before the strobe */
854
855 bits.e = BIT_SET;
856 bits.rs = BIT_SET;
857 bits.rw = BIT_CLR;
858 set_ctrl_bits();
859
860 udelay(40); /* maintain the strobe during 40 us */
861
862 bits.e = BIT_CLR;
863 set_ctrl_bits();
864
865 udelay(45); /* the shortest data takes at least 45 us */
866 }
867 spin_unlock(&pprt_lock);
868
869 lcd_addr_x = lcd_addr_y = 0;
870 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800871}
872
873/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +0100874static void lcd_clear_display(void)
875{
876 lcd_write_cmd(0x01); /* clear display */
877 lcd_addr_x = lcd_addr_y = 0;
878 /* we must wait a few milliseconds (15) */
879 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -0800880}
881
Willy Tarreau698b1512008-11-22 11:29:33 +0100882static void lcd_init_display(void)
883{
Willy Tarreau7005b582008-11-13 17:18:59 -0800884
Willy Tarreau698b1512008-11-22 11:29:33 +0100885 lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
886 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -0800887
Willy Tarreau698b1512008-11-22 11:29:33 +0100888 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -0800889
Willy Tarreau698b1512008-11-22 11:29:33 +0100890 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
891 long_sleep(10);
892 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
893 long_sleep(10);
894 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
895 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800896
Willy Tarreau698b1512008-11-22 11:29:33 +0100897 lcd_write_cmd(0x30 /* set font height and lines number */
898 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
899 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0)
900 );
901 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800902
Willy Tarreau698b1512008-11-22 11:29:33 +0100903 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
904 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800905
Willy Tarreau698b1512008-11-22 11:29:33 +0100906 lcd_write_cmd(0x08 /* set display mode */
907 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
908 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
909 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0)
910 );
Willy Tarreau7005b582008-11-13 17:18:59 -0800911
Willy Tarreau698b1512008-11-22 11:29:33 +0100912 lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -0800913
Willy Tarreau698b1512008-11-22 11:29:33 +0100914 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800915
Willy Tarreau698b1512008-11-22 11:29:33 +0100916 lcd_write_cmd(0x06); /* entry mode set : increment, cursor shifting */
Willy Tarreau7005b582008-11-13 17:18:59 -0800917
Willy Tarreau698b1512008-11-22 11:29:33 +0100918 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -0800919}
920
921/*
922 * These are the file operation function for user access to /dev/lcd
923 * This function can also be called from inside the kernel, by
924 * setting file and ppos to NULL.
925 *
926 */
927
Willy Tarreau698b1512008-11-22 11:29:33 +0100928static ssize_t lcd_write(struct file *file,
929 const char *buf, size_t count, loff_t *ppos)
930{
Willy Tarreau7005b582008-11-13 17:18:59 -0800931
Willy Tarreau698b1512008-11-22 11:29:33 +0100932 const char *tmp = buf;
933 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -0800934
Willy Tarreau698b1512008-11-22 11:29:33 +0100935 for (; count-- > 0; (ppos ? (*ppos)++ : 0), ++tmp) {
936 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
937 schedule(); /* let's be a little nice with other processes that need some CPU */
Willy Tarreau7005b582008-11-13 17:18:59 -0800938
Willy Tarreau698b1512008-11-22 11:29:33 +0100939 if (ppos == NULL && file == NULL)
940 c = *tmp; /* let's not use get_user() from the kernel ! */
941 else if (get_user(c, tmp))
942 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -0800943
Willy Tarreau698b1512008-11-22 11:29:33 +0100944 /* first, we'll test if we're in escape mode */
945 if ((c != '\n') && lcd_escape_len >= 0) { /* yes, let's add this char to the buffer */
946 lcd_escape[lcd_escape_len++] = c;
947 lcd_escape[lcd_escape_len] = 0;
948 } else {
949 lcd_escape_len = -1; /* aborts any previous escape sequence */
950
951 switch (c) {
952 case LCD_ESCAPE_CHAR: /* start of an escape sequence */
953 lcd_escape_len = 0;
954 lcd_escape[lcd_escape_len] = 0;
955 break;
956 case '\b': /* go back one char and clear it */
957 if (lcd_addr_x > 0) {
958 if (lcd_addr_x < lcd_bwidth) /* check if we're not at the end of the line */
959 lcd_write_cmd(0x10); /* back one char */
960 lcd_addr_x--;
961 }
962 lcd_write_data(' '); /* replace with a space */
963 lcd_write_cmd(0x10); /* back one char again */
964 break;
965 case '\014': /* quickly clear the display */
966 lcd_clear_fast();
967 break;
968 case '\n': /* flush the remainder of the current line and go to the
969 beginning of the next line */
970 for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
971 lcd_write_data(' ');
972 lcd_addr_x = 0;
973 lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
974 lcd_gotoxy();
975 break;
976 case '\r': /* go to the beginning of the same line */
977 lcd_addr_x = 0;
978 lcd_gotoxy();
979 break;
980 case '\t': /* print a space instead of the tab */
981 lcd_print(' ');
982 break;
983 default: /* simply print this char */
984 lcd_print(c);
985 break;
986 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800987 }
Willy Tarreau698b1512008-11-22 11:29:33 +0100988
989 /* now we'll see if we're in an escape mode and if the current
990 escape sequence can be understood.
991 */
992 if (lcd_escape_len >= 2) { /* minimal length for an escape command */
993 int processed = 0; /* 1 means the command has been processed */
994
995 if (!strcmp(lcd_escape, "[2J")) { /* Clear the display */
996 lcd_clear_fast(); /* clear display */
997 processed = 1;
998 } else if (!strcmp(lcd_escape, "[H")) { /* Cursor to home */
999 lcd_addr_x = lcd_addr_y = 0;
1000 lcd_gotoxy();
1001 processed = 1;
1002 }
1003 /* codes starting with ^[[L */
1004 else if ((lcd_escape_len >= 3) &&
1005 (lcd_escape[0] == '[') && (lcd_escape[1] == 'L')) { /* LCD special codes */
1006
1007 char *esc = lcd_escape + 2;
1008 int oldflags = lcd_flags;
1009
1010 /* check for display mode flags */
1011 switch (*esc) {
1012 case 'D': /* Display ON */
1013 lcd_flags |= LCD_FLAG_D;
1014 processed = 1;
1015 break;
1016 case 'd': /* Display OFF */
1017 lcd_flags &= ~LCD_FLAG_D;
1018 processed = 1;
1019 break;
1020 case 'C': /* Cursor ON */
1021 lcd_flags |= LCD_FLAG_C;
1022 processed = 1;
1023 break;
1024 case 'c': /* Cursor OFF */
1025 lcd_flags &= ~LCD_FLAG_C;
1026 processed = 1;
1027 break;
1028 case 'B': /* Blink ON */
1029 lcd_flags |= LCD_FLAG_B;
1030 processed = 1;
1031 break;
1032 case 'b': /* Blink OFF */
1033 lcd_flags &= ~LCD_FLAG_B;
1034 processed = 1;
1035 break;
1036 case '+': /* Back light ON */
1037 lcd_flags |= LCD_FLAG_L;
1038 processed = 1;
1039 break;
1040 case '-': /* Back light OFF */
1041 lcd_flags &= ~LCD_FLAG_L;
1042 processed = 1;
1043 break;
1044 case '*': /* flash back light using the keypad timer */
1045 if (scan_timer.function != NULL) {
1046 if (light_tempo == 0
1047 && ((lcd_flags & LCD_FLAG_L)
1048 == 0))
1049 lcd_backlight(1);
1050 light_tempo = FLASH_LIGHT_TEMPO;
1051 }
1052 processed = 1;
1053 break;
1054 case 'f': /* Small Font */
1055 lcd_flags &= ~LCD_FLAG_F;
1056 processed = 1;
1057 break;
1058 case 'F': /* Large Font */
1059 lcd_flags |= LCD_FLAG_F;
1060 processed = 1;
1061 break;
1062 case 'n': /* One Line */
1063 lcd_flags &= ~LCD_FLAG_N;
1064 processed = 1;
1065 break;
1066 case 'N': /* Two Lines */
1067 lcd_flags |= LCD_FLAG_N;
1068 break;
1069
1070 case 'l': /* Shift Cursor Left */
1071 if (lcd_addr_x > 0) {
1072 if (lcd_addr_x < lcd_bwidth)
1073 lcd_write_cmd(0x10); /* back one char if not at end of line */
1074 lcd_addr_x--;
1075 }
1076 processed = 1;
1077 break;
1078
1079 case 'r': /* shift cursor right */
1080 if (lcd_addr_x < lcd_width) {
1081 if (lcd_addr_x < (lcd_bwidth - 1))
1082 lcd_write_cmd(0x14); /* allow the cursor to pass the end of the line */
1083 lcd_addr_x++;
1084 }
1085 processed = 1;
1086 break;
1087
1088 case 'L': /* shift display left */
1089 lcd_left_shift++;
1090 lcd_write_cmd(0x18);
1091 processed = 1;
1092 break;
1093
1094 case 'R': /* shift display right */
1095 lcd_left_shift--;
1096 lcd_write_cmd(0x1C);
1097 processed = 1;
1098 break;
1099
1100 case 'k':{ /* kill end of line */
1101 int x;
1102 for (x = lcd_addr_x; x < lcd_bwidth; x++)
1103 lcd_write_data(' ');
1104 lcd_gotoxy(); /* restore cursor position */
1105 processed = 1;
1106 break;
1107 }
1108 case 'I': /* reinitialize display */
1109 lcd_init_display();
1110 lcd_left_shift = 0;
1111 processed = 1;
1112 break;
1113
1114 case 'G': /* Generator : LGcxxxxx...xx; */ {
1115 /* must have <c> between '0' and '7', representing the numerical
1116 * ASCII code of the redefined character, and <xx...xx> a sequence
1117 * of 16 hex digits representing 8 bytes for each character. Most
1118 * LCDs will only use 5 lower bits of the 7 first bytes.
1119 */
1120
1121 unsigned char cgbytes[8];
1122 unsigned char cgaddr;
1123 int cgoffset;
1124 int shift;
1125 char value;
1126 int addr;
1127
1128 if (strchr(esc, ';') == NULL)
1129 break;
1130
1131 esc++;
1132
1133 cgaddr = *(esc++) - '0';
1134 if (cgaddr > 7) {
1135 processed = 1;
1136 break;
1137 }
1138
1139 cgoffset = 0;
1140 shift = 0;
1141 value = 0;
1142 while (*esc && cgoffset < 8) {
1143 shift ^= 4;
1144 if (*esc >= '0' && *esc <= '9')
1145 value |= (*esc - '0') << shift;
1146 else if (*esc >= 'A' && *esc <= 'Z')
1147 value |= (*esc - 'A' + 10) << shift;
1148 else if (*esc >= 'a' && *esc <= 'z')
1149 value |= (*esc - 'a' + 10) << shift;
1150 else {
1151 esc++;
1152 continue;
1153 }
1154
1155 if (shift == 0) {
1156 cgbytes[cgoffset++] = value;
1157 value = 0;
1158 }
1159
1160 esc++;
1161 }
1162
1163 lcd_write_cmd(0x40 | (cgaddr * 8));
1164 for (addr = 0; addr < cgoffset; addr++)
1165 lcd_write_data(cgbytes[addr]);
1166
1167 lcd_gotoxy(); /* ensures that we stop writing to CGRAM */
1168 processed = 1;
1169 break;
1170 }
1171 case 'x': /* gotoxy : LxXXX[yYYY]; */
1172 case 'y': /* gotoxy : LyYYY[xXXX]; */
1173 if (strchr(esc, ';') == NULL)
1174 break;
1175
1176 while (*esc) {
1177 if (*esc == 'x') {
1178 esc++;
1179 lcd_addr_x = 0;
1180 while (isdigit(*esc)) {
1181 lcd_addr_x =
1182 lcd_addr_x *
1183 10 + (*esc -
1184 '0');
1185 esc++;
1186 }
1187 } else if (*esc == 'y') {
1188 esc++;
1189 lcd_addr_y = 0;
1190 while (isdigit(*esc)) {
1191 lcd_addr_y =
1192 lcd_addr_y *
1193 10 + (*esc -
1194 '0');
1195 esc++;
1196 }
1197 } else
1198 break;
1199 }
1200
1201 lcd_gotoxy();
1202 processed = 1;
1203 break;
1204 } /* end of switch */
1205
1206 /* Check wether one flag was changed */
1207 if (oldflags != lcd_flags) {
1208 /* check wether one of B,C,D flags was changed */
1209 if ((oldflags ^ lcd_flags) &
1210 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1211 /* set display mode */
1212 lcd_write_cmd(0x08 |
1213 ((lcd_flags & LCD_FLAG_D) ? 4 : 0) |
1214 ((lcd_flags & LCD_FLAG_C) ? 2 : 0) |
1215 ((lcd_flags & LCD_FLAG_B) ? 1 : 0));
1216 /* check wether one of F,N flags was changed */
1217 else if ((oldflags ^ lcd_flags) &
1218 (LCD_FLAG_F | LCD_FLAG_N))
1219 lcd_write_cmd(0x30 |
1220 ((lcd_flags & LCD_FLAG_F) ? 4 : 0) |
1221 ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
1222 /* check wether L flag was changed */
1223 else if ((oldflags ^ lcd_flags) &
1224 (LCD_FLAG_L)) {
1225 if (lcd_flags & (LCD_FLAG_L))
1226 lcd_backlight(1);
1227 else if (light_tempo == 0) /* switch off the light only when the tempo lighting is gone */
1228 lcd_backlight(0);
1229 }
1230 }
1231 }
1232
1233 /* LCD special escape codes */
1234 /* flush the escape sequence if it's been processed or if it is
1235 getting too long. */
1236 if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1237 lcd_escape_len = -1;
1238 } /* escape codes */
Willy Tarreau7005b582008-11-13 17:18:59 -08001239 }
1240
Willy Tarreau698b1512008-11-22 11:29:33 +01001241 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001242}
1243
Willy Tarreau698b1512008-11-22 11:29:33 +01001244static int lcd_open(struct inode *inode, struct file *file)
1245{
1246 if (lcd_open_cnt)
1247 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001248
Willy Tarreau698b1512008-11-22 11:29:33 +01001249 if (file->f_mode & FMODE_READ) /* device is write-only */
1250 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001251
Willy Tarreau698b1512008-11-22 11:29:33 +01001252 if (lcd_must_clear) {
1253 lcd_clear_display();
1254 lcd_must_clear = 0;
1255 }
1256 lcd_open_cnt++;
1257 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001258}
1259
Willy Tarreau698b1512008-11-22 11:29:33 +01001260static int lcd_release(struct inode *inode, struct file *file)
1261{
1262 lcd_open_cnt--;
1263 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001264}
1265
Willy Tarreau7005b582008-11-13 17:18:59 -08001266static struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001267 .write = lcd_write,
1268 .open = lcd_open,
1269 .release = lcd_release,
Willy Tarreau7005b582008-11-13 17:18:59 -08001270};
1271
1272static struct miscdevice lcd_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001273 LCD_MINOR,
1274 "lcd",
1275 &lcd_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001276};
1277
Willy Tarreau7005b582008-11-13 17:18:59 -08001278/* public function usable from the kernel for any purpose */
Willy Tarreau698b1512008-11-22 11:29:33 +01001279void panel_lcd_print(char *s)
1280{
1281 if (lcd_enabled && lcd_initialized)
1282 lcd_write(NULL, s, strlen(s), NULL);
Willy Tarreau7005b582008-11-13 17:18:59 -08001283}
1284
Willy Tarreau7005b582008-11-13 17:18:59 -08001285/* initialize the LCD driver */
Willy Tarreau698b1512008-11-22 11:29:33 +01001286void lcd_init(void)
1287{
1288 switch (lcd_type) {
1289 case LCD_TYPE_OLD: /* parallel mode, 8 bits */
1290 if (lcd_proto < 0)
1291 lcd_proto = LCD_PROTO_PARALLEL;
1292 if (lcd_charset < 0)
1293 lcd_charset = LCD_CHARSET_NORMAL;
1294 if (lcd_e_pin == PIN_NOT_SET)
1295 lcd_e_pin = PIN_STROBE;
1296 if (lcd_rs_pin == PIN_NOT_SET)
1297 lcd_rs_pin = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001298
Willy Tarreau698b1512008-11-22 11:29:33 +01001299 if (lcd_width < 0)
1300 lcd_width = 40;
1301 if (lcd_bwidth < 0)
1302 lcd_bwidth = 40;
1303 if (lcd_hwidth < 0)
1304 lcd_hwidth = 64;
1305 if (lcd_height < 0)
1306 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001307 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001308 case LCD_TYPE_KS0074: /* serial mode, ks0074 */
1309 if (lcd_proto < 0)
1310 lcd_proto = LCD_PROTO_SERIAL;
1311 if (lcd_charset < 0)
1312 lcd_charset = LCD_CHARSET_KS0074;
1313 if (lcd_bl_pin == PIN_NOT_SET)
1314 lcd_bl_pin = PIN_AUTOLF;
1315 if (lcd_cl_pin == PIN_NOT_SET)
1316 lcd_cl_pin = PIN_STROBE;
1317 if (lcd_da_pin == PIN_NOT_SET)
1318 lcd_da_pin = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001319
Willy Tarreau698b1512008-11-22 11:29:33 +01001320 if (lcd_width < 0)
1321 lcd_width = 16;
1322 if (lcd_bwidth < 0)
1323 lcd_bwidth = 40;
1324 if (lcd_hwidth < 0)
1325 lcd_hwidth = 16;
1326 if (lcd_height < 0)
1327 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001328 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001329 case LCD_TYPE_NEXCOM: /* parallel mode, 8 bits, generic */
1330 if (lcd_proto < 0)
1331 lcd_proto = LCD_PROTO_PARALLEL;
1332 if (lcd_charset < 0)
1333 lcd_charset = LCD_CHARSET_NORMAL;
1334 if (lcd_e_pin == PIN_NOT_SET)
1335 lcd_e_pin = PIN_AUTOLF;
1336 if (lcd_rs_pin == PIN_NOT_SET)
1337 lcd_rs_pin = PIN_SELECP;
1338 if (lcd_rw_pin == PIN_NOT_SET)
1339 lcd_rw_pin = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001340
Willy Tarreau698b1512008-11-22 11:29:33 +01001341 if (lcd_width < 0)
1342 lcd_width = 16;
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_CUSTOM: /* customer-defined */
1351 if (lcd_proto < 0)
1352 lcd_proto = DEFAULT_LCD_PROTO;
1353 if (lcd_charset < 0)
1354 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001355 /* default geometry will be set later */
1356 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001357 case LCD_TYPE_HANTRONIX: /* parallel mode, 8 bits, hantronix-like */
1358 default:
1359 if (lcd_proto < 0)
1360 lcd_proto = LCD_PROTO_PARALLEL;
1361 if (lcd_charset < 0)
1362 lcd_charset = LCD_CHARSET_NORMAL;
1363 if (lcd_e_pin == PIN_NOT_SET)
1364 lcd_e_pin = PIN_STROBE;
1365 if (lcd_rs_pin == PIN_NOT_SET)
1366 lcd_rs_pin = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001367
Willy Tarreau698b1512008-11-22 11:29:33 +01001368 if (lcd_width < 0)
1369 lcd_width = 16;
1370 if (lcd_bwidth < 0)
1371 lcd_bwidth = 40;
1372 if (lcd_hwidth < 0)
1373 lcd_hwidth = 64;
1374 if (lcd_height < 0)
1375 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001376 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001377 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001378
Willy Tarreau698b1512008-11-22 11:29:33 +01001379 /* this is used to catch wrong and default values */
1380 if (lcd_width <= 0)
1381 lcd_width = DEFAULT_LCD_WIDTH;
1382 if (lcd_bwidth <= 0)
1383 lcd_bwidth = DEFAULT_LCD_BWIDTH;
1384 if (lcd_hwidth <= 0)
1385 lcd_hwidth = DEFAULT_LCD_HWIDTH;
1386 if (lcd_height <= 0)
1387 lcd_height = DEFAULT_LCD_HEIGHT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001388
Willy Tarreau698b1512008-11-22 11:29:33 +01001389 if (lcd_proto == LCD_PROTO_SERIAL) { /* SERIAL */
1390 lcd_write_cmd = lcd_write_cmd_s;
1391 lcd_write_data = lcd_write_data_s;
1392 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001393
Willy Tarreau698b1512008-11-22 11:29:33 +01001394 if (lcd_cl_pin == PIN_NOT_SET)
1395 lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1396 if (lcd_da_pin == PIN_NOT_SET)
1397 lcd_da_pin = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001398
Willy Tarreau698b1512008-11-22 11:29:33 +01001399 } else { /* PARALLEL */
1400 lcd_write_cmd = lcd_write_cmd_p8;
1401 lcd_write_data = lcd_write_data_p8;
1402 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001403
Willy Tarreau698b1512008-11-22 11:29:33 +01001404 if (lcd_e_pin == PIN_NOT_SET)
1405 lcd_e_pin = DEFAULT_LCD_PIN_E;
1406 if (lcd_rs_pin == PIN_NOT_SET)
1407 lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1408 if (lcd_rw_pin == PIN_NOT_SET)
1409 lcd_rw_pin = DEFAULT_LCD_PIN_RW;
1410 }
1411
1412 if (lcd_bl_pin == PIN_NOT_SET)
1413 lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1414
1415 if (lcd_e_pin == PIN_NOT_SET)
1416 lcd_e_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001417 if (lcd_rs_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001418 lcd_rs_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001419 if (lcd_rw_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001420 lcd_rw_pin = PIN_NONE;
1421 if (lcd_bl_pin == PIN_NOT_SET)
1422 lcd_bl_pin = PIN_NONE;
1423 if (lcd_cl_pin == PIN_NOT_SET)
1424 lcd_cl_pin = PIN_NONE;
1425 if (lcd_da_pin == PIN_NOT_SET)
1426 lcd_da_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001427
Willy Tarreau698b1512008-11-22 11:29:33 +01001428 if (lcd_charset < 0)
1429 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001430
Willy Tarreau698b1512008-11-22 11:29:33 +01001431 if (lcd_charset == LCD_CHARSET_KS0074)
1432 lcd_char_conv = lcd_char_conv_ks0074;
1433 else
1434 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001435
Willy Tarreau698b1512008-11-22 11:29:33 +01001436 if (lcd_bl_pin != PIN_NONE)
1437 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001438
Willy Tarreau698b1512008-11-22 11:29:33 +01001439 pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1440 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1441 pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1442 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1443 pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1444 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1445 pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1446 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1447 pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1448 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1449 pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1450 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001451
Willy Tarreau698b1512008-11-22 11:29:33 +01001452 /* before this line, we must NOT send anything to the display.
1453 * Since lcd_init_display() needs to write data, we have to
1454 * enable mark the LCD initialized just before.
1455 */
1456 lcd_initialized = 1;
1457 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001458
Willy Tarreau698b1512008-11-22 11:29:33 +01001459 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001460#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1461#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001462 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001463#endif
1464#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001465 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1466 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001467#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001468 lcd_addr_x = lcd_addr_y = 0;
1469 lcd_must_clear = 1; /* clear the display on the next device opening */
1470 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001471}
1472
Willy Tarreau7005b582008-11-13 17:18:59 -08001473/*
1474 * These are the file operation function for user access to /dev/keypad
1475 */
1476
Willy Tarreau698b1512008-11-22 11:29:33 +01001477static ssize_t keypad_read(struct file *file,
1478 char *buf, size_t count, loff_t *ppos)
1479{
Willy Tarreau7005b582008-11-13 17:18:59 -08001480
Willy Tarreau698b1512008-11-22 11:29:33 +01001481 unsigned i = *ppos;
1482 char *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001483
Willy Tarreau698b1512008-11-22 11:29:33 +01001484 if (keypad_buflen == 0) {
1485 if (file->f_flags & O_NONBLOCK)
1486 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001487
Willy Tarreau698b1512008-11-22 11:29:33 +01001488 interruptible_sleep_on(&keypad_read_wait);
1489 if (signal_pending(current))
1490 return -EINTR;
1491 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001492
Willy Tarreau698b1512008-11-22 11:29:33 +01001493 for (; count-- > 0 && (keypad_buflen > 0); ++i, ++tmp, --keypad_buflen) {
1494 put_user(keypad_buffer[keypad_start], tmp);
1495 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1496 }
1497 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001498
Willy Tarreau698b1512008-11-22 11:29:33 +01001499 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001500}
1501
Willy Tarreau698b1512008-11-22 11:29:33 +01001502static int keypad_open(struct inode *inode, struct file *file)
1503{
Willy Tarreau7005b582008-11-13 17:18:59 -08001504
Willy Tarreau698b1512008-11-22 11:29:33 +01001505 if (keypad_open_cnt)
1506 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001507
Willy Tarreau698b1512008-11-22 11:29:33 +01001508 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1509 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001510
Willy Tarreau698b1512008-11-22 11:29:33 +01001511 keypad_buflen = 0; /* flush the buffer on opening */
1512 keypad_open_cnt++;
1513 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001514}
1515
Willy Tarreau698b1512008-11-22 11:29:33 +01001516static int keypad_release(struct inode *inode, struct file *file)
1517{
1518 keypad_open_cnt--;
1519 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001520}
1521
1522static struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001523 .read = keypad_read, /* read */
1524 .open = keypad_open, /* open */
1525 .release = keypad_release, /* close */
Willy Tarreau7005b582008-11-13 17:18:59 -08001526};
1527
1528static struct miscdevice keypad_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001529 KEYPAD_MINOR,
1530 "keypad",
1531 &keypad_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001532};
1533
Willy Tarreau698b1512008-11-22 11:29:33 +01001534static void keypad_send_key(char *string, int max_len)
1535{
1536 if (init_in_progress)
1537 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001538
Willy Tarreau698b1512008-11-22 11:29:33 +01001539 /* send the key to the device only if a process is attached to it. */
1540 if (keypad_open_cnt > 0) {
1541 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1542 keypad_buffer[(keypad_start + keypad_buflen++) %
1543 KEYPAD_BUFFER] = *string++;
1544 }
1545 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001546 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001547}
1548
Willy Tarreau7005b582008-11-13 17:18:59 -08001549/* this function scans all the bits involving at least one logical signal, and puts the
1550 * results in the bitfield "phys_read" (one bit per established contact), and sets
1551 * "phys_read_prev" to "phys_read".
1552 *
1553 * Note: to debounce input signals, we will only consider as switched a signal which is
1554 * stable across 2 measures. Signals which are different between two reads will be kept
1555 * as they previously were in their logical form (phys_prev). A signal which has just
1556 * switched will have a 1 in (phys_read ^ phys_read_prev).
1557 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001558static void phys_scan_contacts(void)
1559{
1560 int bit, bitval;
1561 char oldval;
1562 char bitmask;
1563 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001564
Willy Tarreau698b1512008-11-22 11:29:33 +01001565 phys_prev = phys_curr;
1566 phys_read_prev = phys_read;
1567 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001568
Willy Tarreau698b1512008-11-22 11:29:33 +01001569 oldval = r_dtr(pprt) | scan_mask_o; /* keep track of old value, with all outputs disabled */
1570 w_dtr(pprt, oldval & ~scan_mask_o); /* activate all keyboard outputs (active low) */
1571 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; /* will have a 1 for each bit set to gnd */
1572 w_dtr(pprt, oldval); /* disable all matrix signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001573
Willy Tarreau698b1512008-11-22 11:29:33 +01001574 /* now that all outputs are cleared, the only active input bits are
1575 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001576 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001577 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; /* 1 for each grounded input */
Willy Tarreau7005b582008-11-13 17:18:59 -08001578
Willy Tarreau698b1512008-11-22 11:29:33 +01001579 phys_read |= (pmask_t) gndmask << 40; /* grounded inputs are signals 40-44 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001580
Willy Tarreau698b1512008-11-22 11:29:33 +01001581 if (bitmask != gndmask) {
1582 /* since clearing the outputs changed some inputs, we know that some
1583 * input signals are currently tied to some outputs. So we'll scan them.
1584 */
1585 for (bit = 0; bit < 8; bit++) {
1586 bitval = 1 << bit;
1587
1588 if (!(scan_mask_o & bitval)) /* skip unused bits */
1589 continue;
1590
1591 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1592 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1593 phys_read |= (pmask_t) bitmask << (5 * bit);
1594 }
1595 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001596 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001597 /* this is easy: use old bits when they are flapping, use new ones when stable */
1598 phys_curr =
1599 (phys_prev & (phys_read ^ phys_read_prev)) | (phys_read &
1600 ~(phys_read ^
1601 phys_read_prev));
Willy Tarreau7005b582008-11-13 17:18:59 -08001602}
1603
Willy Tarreau698b1512008-11-22 11:29:33 +01001604static void panel_process_inputs(void)
1605{
1606 struct list_head *item;
1607 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001608
1609#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001610 printk(KERN_DEBUG
1611 "entering panel_process_inputs with pp=%016Lx & pc=%016Lx\n",
1612 phys_prev, phys_curr);
Willy Tarreau7005b582008-11-13 17:18:59 -08001613#endif
1614
Willy Tarreau698b1512008-11-22 11:29:33 +01001615 keypressed = 0;
1616 inputs_stable = 1;
1617 list_for_each(item, &logical_inputs) {
1618 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001619
Willy Tarreau698b1512008-11-22 11:29:33 +01001620 switch (input->state) {
1621 case INPUT_ST_LOW:
1622 if ((phys_curr & input->mask) != input->value)
1623 break;
1624 /* if all needed ones were already set previously, this means that
1625 * this logical signal has been activated by the releasing of
1626 * another combined signal, so we don't want to match.
1627 * eg: AB -(release B)-> A -(release A)-> 0 : don't match A.
1628 */
1629 if ((phys_prev & input->mask) == input->value)
1630 break;
1631 input->rise_timer = 0;
1632 input->state = INPUT_ST_RISING;
1633 /* no break here, fall through */
1634 case INPUT_ST_RISING:
1635 if ((phys_curr & input->mask) != input->value) {
1636 input->state = INPUT_ST_LOW;
1637 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001638 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001639 if (input->rise_timer < input->rise_time) {
1640 inputs_stable = 0;
1641 input->rise_timer++;
1642 break;
1643 }
1644 input->high_timer = 0;
1645 input->state = INPUT_ST_HIGH;
1646 /* no break here, fall through */
1647 case INPUT_ST_HIGH:
Willy Tarreau7005b582008-11-13 17:18:59 -08001648#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001649 /* FIXME:
1650 * this is an invalid test. It tries to catch transitions from single-key
1651 * to multiple-key, but doesn't take into account the contacts polarity.
1652 * The only solution to the problem is to parse keys from the most complex
1653 * to the simplest combinations, and mark them as 'caught' once a combination
1654 * matches, then unmatch it for all other ones.
1655 */
1656
1657 /* try to catch dangerous transitions cases :
1658 * someone adds a bit, so this signal was a false
1659 * positive resulting from a transition. We should invalidate
1660 * the signal immediately and not call the release function.
1661 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1662 */
1663 if (((phys_prev & input->mask) == input->value)
1664 && ((phys_curr & input->mask) > input->value)) {
1665 input->state = INPUT_ST_LOW; /* invalidate */
1666 break;
1667 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001668#endif
1669
Willy Tarreau698b1512008-11-22 11:29:33 +01001670 if ((phys_curr & input->mask) == input->value) {
1671 if ((input->type == INPUT_TYPE_STD)
1672 && (input->high_timer == 0)) {
1673 input->high_timer++;
1674 if (input->u.std.press_fct != NULL)
1675 input->u.std.press_fct(input->u.
1676 std.
1677 press_data);
1678 } else if (input->type == INPUT_TYPE_KBD) {
1679 keypressed = 1; /* will turn on the light */
Willy Tarreau7005b582008-11-13 17:18:59 -08001680
Willy Tarreau698b1512008-11-22 11:29:33 +01001681 if (input->high_timer == 0) {
1682 if (input->u.kbd.press_str[0])
1683 keypad_send_key(input->
1684 u.kbd.
1685 press_str,
1686 sizeof
1687 (input->
1688 u.kbd.
1689 press_str));
1690 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001691
Willy Tarreau698b1512008-11-22 11:29:33 +01001692 if (input->u.kbd.repeat_str[0]) {
1693 if (input->high_timer >=
1694 KEYPAD_REP_START) {
1695 input->high_timer -=
1696 KEYPAD_REP_DELAY;
1697 keypad_send_key(input->
1698 u.kbd.
1699 repeat_str,
1700 sizeof
1701 (input->
1702 u.kbd.
1703 repeat_str));
1704 }
1705 inputs_stable = 0; /* we will need to come back here soon */
1706 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001707
Willy Tarreau698b1512008-11-22 11:29:33 +01001708 if (input->high_timer < 255)
1709 input->high_timer++;
1710 }
1711 break;
1712 } else {
1713 /* else signal falling down. Let's fall through. */
1714 input->state = INPUT_ST_FALLING;
1715 input->fall_timer = 0;
1716 }
1717 /* no break here, fall through */
1718 case INPUT_ST_FALLING:
1719#if 0
1720 /* FIXME !!! same comment as above */
1721 if (((phys_prev & input->mask) == input->value)
1722 && ((phys_curr & input->mask) > input->value)) {
1723 input->state = INPUT_ST_LOW; /* invalidate */
1724 break;
1725 }
1726#endif
1727
1728 if ((phys_curr & input->mask) == input->value) {
1729 if (input->type == INPUT_TYPE_KBD) {
1730 keypressed = 1; /* will turn on the light */
1731
1732 if (input->u.kbd.repeat_str[0]) {
1733 if (input->high_timer >= KEYPAD_REP_START)
1734 input->high_timer -= KEYPAD_REP_DELAY;
1735 keypad_send_key(input->u.kbd.repeat_str,
1736 sizeof(input->u.kbd.repeat_str));
1737 inputs_stable = 0; /* we will need to come back here soon */
1738 }
1739
1740 if (input->high_timer < 255)
1741 input->high_timer++;
1742 }
1743 input->state = INPUT_ST_HIGH;
1744 break;
1745 } else if (input->fall_timer >= input->fall_time) {
1746 /* call release event */
1747 if (input->type == INPUT_TYPE_STD) {
1748 if (input->u.std.release_fct != NULL)
1749 input->u.std.release_fct(input->u.std.release_data);
1750
1751 } else if (input->type == INPUT_TYPE_KBD) {
1752 if (input->u.kbd.release_str[0])
1753 keypad_send_key(input->u.kbd.release_str,
1754 sizeof(input->u.kbd.release_str));
1755 }
1756
1757 input->state = INPUT_ST_LOW;
1758 break;
1759 } else {
1760 input->fall_timer++;
1761 inputs_stable = 0;
1762 break;
1763 }
1764 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001765 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001766}
1767
Willy Tarreau698b1512008-11-22 11:29:33 +01001768static void panel_scan_timer(void)
1769{
Willy Tarreau630231772008-11-22 12:52:18 +01001770 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001771 if (spin_trylock(&pprt_lock)) {
1772 phys_scan_contacts();
1773 spin_unlock(&pprt_lock); /* no need for the parport anymore */
1774 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001775
Willy Tarreau698b1512008-11-22 11:29:33 +01001776 if (!inputs_stable || phys_curr != phys_prev)
1777 panel_process_inputs();
1778 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001779
Willy Tarreau698b1512008-11-22 11:29:33 +01001780 if (lcd_enabled && lcd_initialized) {
1781 if (keypressed) {
1782 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1783 lcd_backlight(1);
1784 light_tempo = FLASH_LIGHT_TEMPO;
1785 } else if (light_tempo > 0) {
1786 light_tempo--;
1787 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1788 lcd_backlight(0);
1789 }
1790 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001791
Willy Tarreau698b1512008-11-22 11:29:33 +01001792 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001793}
1794
Willy Tarreau698b1512008-11-22 11:29:33 +01001795static void init_scan_timer(void)
1796{
1797 if (scan_timer.function != NULL)
1798 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001799
Willy Tarreau698b1512008-11-22 11:29:33 +01001800 init_timer(&scan_timer);
1801 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1802 scan_timer.data = 0;
1803 scan_timer.function = (void *)&panel_scan_timer;
1804 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001805}
1806
1807/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1808 * if <omask> or <imask> are non-null, they will be or'ed with the bits corresponding
1809 * to out and in bits respectively.
1810 * returns 1 if ok, 0 if error (in which case, nothing is written).
1811 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001812static int input_name2mask(char *name, pmask_t *mask, pmask_t *value,
1813 char *imask, char *omask)
1814{
1815 static char sigtab[10] = "EeSsPpAaBb";
1816 char im, om;
1817 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08001818
Willy Tarreau698b1512008-11-22 11:29:33 +01001819 om = im = m = v = 0ULL;
1820 while (*name) {
1821 int in, out, bit, neg;
1822 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name); in++)
1823 ;
1824 if (in >= sizeof(sigtab))
1825 return 0; /* input name not found */
1826 neg = (in & 1); /* odd (lower) names are negated */
1827 in >>= 1;
1828 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08001829
Willy Tarreau698b1512008-11-22 11:29:33 +01001830 name++;
1831 if (isdigit(*name)) {
1832 out = *name - '0';
1833 om |= (1 << out);
1834 } else if (*name == '-')
1835 out = 8;
1836 else
1837 return 0; /* unknown bit name */
1838
1839 bit = (out * 5) + in;
1840
1841 m |= 1ULL << bit;
1842 if (!neg)
1843 v |= 1ULL << bit;
1844 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08001845 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001846 *mask = m;
1847 *value = v;
1848 if (imask)
1849 *imask |= im;
1850 if (omask)
1851 *omask |= om;
1852 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001853}
1854
1855/* tries to bind a key to the signal name <name>. The key will send the
1856 * strings <press>, <repeat>, <release> for these respective events.
1857 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1858 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001859static struct logical_input *panel_bind_key(char *name, char *press,
1860 char *repeat, char *release)
1861{
1862 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001863
Willy Tarreau698b1512008-11-22 11:29:33 +01001864 key = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
1865 if (!key) {
1866 printk(KERN_ERR "panel: not enough memory\n");
1867 return NULL;
1868 }
1869 memset(key, 0, sizeof(struct logical_input));
1870 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1871 &scan_mask_o))
1872 return NULL;
1873
1874 key->type = INPUT_TYPE_KBD;
1875 key->state = INPUT_ST_LOW;
1876 key->rise_time = 1;
1877 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001878
1879#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001880 printk(KERN_DEBUG "bind: <%s> : m=%016Lx v=%016Lx\n", name, key->mask,
1881 key->value);
Willy Tarreau7005b582008-11-13 17:18:59 -08001882#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001883 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
1884 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
1885 strncpy(key->u.kbd.release_str, release,
1886 sizeof(key->u.kbd.release_str));
1887 list_add(&key->list, &logical_inputs);
1888 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001889}
1890
Willy Tarreau630231772008-11-22 12:52:18 +01001891#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08001892/* tries to bind a callback function to the signal name <name>. The function
1893 * <press_fct> will be called with the <press_data> arg when the signal is
1894 * activated, and so on for <release_fct>/<release_data>
1895 * Returns the pointer to the new signal if ok, NULL if the signal could not be bound.
1896 */
1897static struct logical_input *panel_bind_callback(char *name,
Willy Tarreau698b1512008-11-22 11:29:33 +01001898 void (*press_fct) (int),
1899 int press_data,
1900 void (*release_fct) (int),
1901 int release_data)
1902{
1903 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08001904
Willy Tarreau698b1512008-11-22 11:29:33 +01001905 callback = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
1906 if (!callback) {
1907 printk(KERN_ERR "panel: not enough memory\n");
1908 return NULL;
1909 }
1910 memset(callback, 0, sizeof(struct logical_input));
1911 if (!input_name2mask(name, &callback->mask, &callback->value,
1912 &scan_mask_i, &scan_mask_o))
1913 return NULL;
1914
1915 callback->type = INPUT_TYPE_STD;
1916 callback->state = INPUT_ST_LOW;
1917 callback->rise_time = 1;
1918 callback->fall_time = 1;
1919 callback->u.std.press_fct = press_fct;
1920 callback->u.std.press_data = press_data;
1921 callback->u.std.release_fct = release_fct;
1922 callback->u.std.release_data = release_data;
1923 list_add(&callback->list, &logical_inputs);
1924 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08001925}
Willy Tarreau630231772008-11-22 12:52:18 +01001926#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08001927
Willy Tarreau698b1512008-11-22 11:29:33 +01001928static void keypad_init(void)
1929{
1930 int keynum;
1931 init_waitqueue_head(&keypad_read_wait);
1932 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08001933
Willy Tarreau698b1512008-11-22 11:29:33 +01001934 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08001935
Willy Tarreau698b1512008-11-22 11:29:33 +01001936 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
1937 panel_bind_key(keypad_profile[keynum][0],
1938 keypad_profile[keynum][1],
1939 keypad_profile[keynum][2],
1940 keypad_profile[keynum][3]);
1941 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001942
Willy Tarreau698b1512008-11-22 11:29:33 +01001943 init_scan_timer();
1944 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001945}
1946
Willy Tarreau7005b582008-11-13 17:18:59 -08001947/**************************************************/
1948/* device initialization */
1949/**************************************************/
1950
Willy Tarreau698b1512008-11-22 11:29:33 +01001951static int panel_notify_sys(struct notifier_block *this, unsigned long code,
1952 void *unused)
1953{
1954 if (lcd_enabled && lcd_initialized) {
1955 switch (code) {
1956 case SYS_DOWN:
1957 panel_lcd_print
1958 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
1959 break;
1960 case SYS_HALT:
1961 panel_lcd_print
1962 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
1963 break;
1964 case SYS_POWER_OFF:
1965 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
1966 break;
1967 default:
1968 break;
1969 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001970 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001971 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001972}
1973
1974static struct notifier_block panel_notifier = {
1975 panel_notify_sys,
1976 NULL,
1977 0
1978};
1979
Willy Tarreau698b1512008-11-22 11:29:33 +01001980static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08001981{
Willy Tarreau698b1512008-11-22 11:29:33 +01001982 if (port->number != parport)
1983 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001984
Willy Tarreau698b1512008-11-22 11:29:33 +01001985 if (pprt) {
1986 printk(KERN_ERR
1987 "panel_attach(): port->number=%d parport=%d, already registered !\n",
1988 port->number, parport);
1989 return;
1990 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001991
Willy Tarreau698b1512008-11-22 11:29:33 +01001992 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
1993 NULL,
1994 /*PARPORT_DEV_EXCL */
1995 0, (void *)&pprt);
Willy Tarreau7005b582008-11-13 17:18:59 -08001996
Willy Tarreau698b1512008-11-22 11:29:33 +01001997 if (parport_claim(pprt)) {
1998 printk(KERN_ERR
1999 "Panel: could not claim access to parport%d. Aborting.\n",
2000 parport);
2001 return;
2002 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002003
Willy Tarreau698b1512008-11-22 11:29:33 +01002004 /* must init LCD first, just in case an IRQ from the keypad is generated at keypad init */
2005 if (lcd_enabled) {
2006 lcd_init();
2007 misc_register(&lcd_dev);
2008 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002009
Willy Tarreau698b1512008-11-22 11:29:33 +01002010 if (keypad_enabled) {
2011 keypad_init();
2012 misc_register(&keypad_dev);
2013 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002014}
2015
Willy Tarreau698b1512008-11-22 11:29:33 +01002016static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002017{
Willy Tarreau698b1512008-11-22 11:29:33 +01002018 if (port->number != parport)
2019 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002020
Willy Tarreau698b1512008-11-22 11:29:33 +01002021 if (!pprt) {
2022 printk(KERN_ERR
2023 "panel_detach(): port->number=%d parport=%d, nothing to unregister.\n",
2024 port->number, parport);
2025 return;
2026 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002027
Willy Tarreau698b1512008-11-22 11:29:33 +01002028 if (keypad_enabled && keypad_initialized)
2029 misc_deregister(&keypad_dev);
Willy Tarreau7005b582008-11-13 17:18:59 -08002030
Willy Tarreau698b1512008-11-22 11:29:33 +01002031 if (lcd_enabled && lcd_initialized)
2032 misc_deregister(&lcd_dev);
Willy Tarreau7005b582008-11-13 17:18:59 -08002033
Willy Tarreau698b1512008-11-22 11:29:33 +01002034 parport_release(pprt);
2035 parport_unregister_device(pprt);
2036 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002037}
2038
2039static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002040 .name = "panel",
2041 .attach = panel_attach,
2042 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002043};
2044
2045/* init function */
Willy Tarreau698b1512008-11-22 11:29:33 +01002046int panel_init(void)
2047{
2048 /* for backwards compatibility */
2049 if (keypad_type < 0)
2050 keypad_type = keypad_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002051
Willy Tarreau698b1512008-11-22 11:29:33 +01002052 if (lcd_type < 0)
2053 lcd_type = lcd_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002054
Willy Tarreau698b1512008-11-22 11:29:33 +01002055 if (parport < 0)
2056 parport = DEFAULT_PARPORT;
Willy Tarreau7005b582008-11-13 17:18:59 -08002057
Willy Tarreau698b1512008-11-22 11:29:33 +01002058 /* take care of an eventual profile */
2059 switch (profile) {
2060 case PANEL_PROFILE_CUSTOM: /* custom profile */
2061 if (keypad_type < 0)
2062 keypad_type = DEFAULT_KEYPAD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002063 if (lcd_type < 0)
2064 lcd_type = DEFAULT_LCD;
2065 break;
2066 case PANEL_PROFILE_OLD: /* 8 bits, 2*16, old keypad */
2067 if (keypad_type < 0)
2068 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002069 if (lcd_type < 0)
2070 lcd_type = LCD_TYPE_OLD;
2071 if (lcd_width < 0)
2072 lcd_width = 16;
2073 if (lcd_hwidth < 0)
2074 lcd_hwidth = 16;
2075 break;
2076 case PANEL_PROFILE_NEW: /* serial, 2*16, new keypad */
2077 if (keypad_type < 0)
2078 keypad_type = KEYPAD_TYPE_NEW;
Willy Tarreau698b1512008-11-22 11:29:33 +01002079 if (lcd_type < 0)
2080 lcd_type = LCD_TYPE_KS0074;
2081 break;
2082 case PANEL_PROFILE_HANTRONIX: /* 8 bits, 2*16 hantronix-like, no keypad */
2083 if (keypad_type < 0)
2084 keypad_type = KEYPAD_TYPE_NONE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002085 if (lcd_type < 0)
2086 lcd_type = LCD_TYPE_HANTRONIX;
2087 break;
2088 case PANEL_PROFILE_NEXCOM: /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
2089 if (keypad_type < 0)
2090 keypad_type = KEYPAD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002091 if (lcd_type < 0)
2092 lcd_type = LCD_TYPE_NEXCOM;
2093 break;
2094 case PANEL_PROFILE_LARGE: /* 8 bits, 2*40, old keypad */
2095 if (keypad_type < 0)
2096 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002097 if (lcd_type < 0)
2098 lcd_type = LCD_TYPE_OLD;
2099 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002100 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002101
Willy Tarreau698b1512008-11-22 11:29:33 +01002102 lcd_enabled = (lcd_type > 0);
2103 keypad_enabled = (keypad_type > 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08002104
Willy Tarreau698b1512008-11-22 11:29:33 +01002105 switch (keypad_type) {
2106 case KEYPAD_TYPE_OLD:
2107 keypad_profile = old_keypad_profile;
2108 break;
2109 case KEYPAD_TYPE_NEW:
2110 keypad_profile = new_keypad_profile;
2111 break;
2112 case KEYPAD_TYPE_NEXCOM:
2113 keypad_profile = nexcom_keypad_profile;
2114 break;
2115 default:
2116 keypad_profile = NULL;
2117 break;
2118 }
2119
2120 /* tells various subsystems about the fact that we are initializing */
2121 init_in_progress = 1;
2122
2123 if (parport_register_driver(&panel_driver)) {
2124 printk(KERN_ERR
2125 "Panel: could not register with parport. Aborting.\n");
2126 return -EIO;
2127 }
2128
Willy Tarreau630231772008-11-22 12:52:18 +01002129 if (!lcd_enabled && !keypad_enabled) {
2130 /* no device enabled, let's release the parport */
Willy Tarreau698b1512008-11-22 11:29:33 +01002131 if (pprt) {
2132 parport_release(pprt);
2133 parport_unregister_device(pprt);
2134 }
2135 parport_unregister_driver(&panel_driver);
2136 printk(KERN_ERR "Panel driver version " PANEL_VERSION
2137 " disabled.\n");
2138 return -ENODEV;
2139 }
2140
2141 register_reboot_notifier(&panel_notifier);
2142
2143 if (pprt)
2144 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2145 " registered on parport%d (io=0x%lx).\n", parport,
2146 pprt->port->base);
2147 else
2148 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2149 " not yet registered\n");
2150 /* tells various subsystems about the fact that initialization is finished */
2151 init_in_progress = 0;
2152 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002153}
2154
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002155static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002156{
2157 return panel_init();
Willy Tarreau7005b582008-11-13 17:18:59 -08002158}
2159
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002160static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002161{
2162 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002163
Willy Tarreau698b1512008-11-22 11:29:33 +01002164 if (scan_timer.function != NULL)
2165 del_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002166
Willy Tarreau698b1512008-11-22 11:29:33 +01002167 if (keypad_enabled)
2168 misc_deregister(&keypad_dev);
Willy Tarreau7005b582008-11-13 17:18:59 -08002169
Willy Tarreau698b1512008-11-22 11:29:33 +01002170 if (lcd_enabled) {
2171 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2172 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2173 misc_deregister(&lcd_dev);
2174 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002175
Willy Tarreau698b1512008-11-22 11:29:33 +01002176 /* TODO: free all input signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08002177
Willy Tarreau698b1512008-11-22 11:29:33 +01002178 parport_release(pprt);
2179 parport_unregister_device(pprt);
2180 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002181}
Willy Tarreau7005b582008-11-13 17:18:59 -08002182
Willy Tarreau7005b582008-11-13 17:18:59 -08002183module_init(panel_init_module);
2184module_exit(panel_cleanup_module);
2185MODULE_AUTHOR("Willy Tarreau");
2186MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002187
2188/*
2189 * Local variables:
2190 * c-indent-level: 4
2191 * tab-width: 8
2192 * End:
2193 */