blob: 6183573f112f3487ac0caf0add169c521115f4b5 [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>
Willy Tarreau7005b582008-11-13 17:18:59 -080044#include <linux/interrupt.h>
45#include <linux/miscdevice.h>
Willy Tarreau698b1512008-11-22 11:29:33 +010046#include <linux/slab.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080047#include <linux/ioport.h>
48#include <linux/fcntl.h>
49#include <linux/init.h>
50#include <linux/delay.h>
Andy Shevchenkod85170e2010-07-22 19:57:08 +030051#include <linux/kernel.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080052#include <linux/ctype.h>
53#include <linux/parport.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080054#include <linux/list.h>
55#include <linux/notifier.h>
56#include <linux/reboot.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020057#include <generated/utsrelease.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080058
Willy Tarreau698b1512008-11-22 11:29:33 +010059#include <linux/io.h>
Takanori Suzuki48f658b2010-05-08 22:56:24 +090060#include <linux/uaccess.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080061
Willy Tarreau7005b582008-11-13 17:18:59 -080062#define LCD_MINOR 156
63#define KEYPAD_MINOR 185
Willy Tarreau7005b582008-11-13 17:18:59 -080064
65#define PANEL_VERSION "0.9.5"
66
67#define LCD_MAXBYTES 256 /* max burst write */
68
Willy Tarreau7005b582008-11-13 17:18:59 -080069#define KEYPAD_BUFFER 64
Willy Tarreau7005b582008-11-13 17:18:59 -080070
Henri Häkkinen429ccf02010-06-12 00:27:36 +030071/* poll the keyboard this every second */
72#define INPUT_POLL_TIME (HZ/50)
73/* a key starts to repeat after this times INPUT_POLL_TIME */
74#define KEYPAD_REP_START (10)
75/* a key repeats this times INPUT_POLL_TIME */
76#define KEYPAD_REP_DELAY (2)
77
78/* keep the light on this times INPUT_POLL_TIME for each flash */
79#define FLASH_LIGHT_TEMPO (200)
Willy Tarreau7005b582008-11-13 17:18:59 -080080
81/* converts an r_str() input to an active high, bits string : 000BAOSE */
82#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
83
Willy Tarreau698b1512008-11-22 11:29:33 +010084#define PNL_PBUSY 0x80 /* inverted input, active low */
85#define PNL_PACK 0x40 /* direct input, active low */
86#define PNL_POUTPA 0x20 /* direct input, active high */
87#define PNL_PSELECD 0x10 /* direct input, active high */
88#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080089
Willy Tarreau698b1512008-11-22 11:29:33 +010090#define PNL_PBIDIR 0x20 /* bi-directional ports */
Henri Häkkinen429ccf02010-06-12 00:27:36 +030091/* high to read data in or-ed with data out */
92#define PNL_PINTEN 0x10
Willy Tarreau698b1512008-11-22 11:29:33 +010093#define PNL_PSELECP 0x08 /* inverted output, active low */
94#define PNL_PINITP 0x04 /* direct output, active low */
95#define PNL_PAUTOLF 0x02 /* inverted output, active low */
96#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080097
98#define PNL_PD0 0x01
99#define PNL_PD1 0x02
100#define PNL_PD2 0x04
101#define PNL_PD3 0x08
102#define PNL_PD4 0x10
103#define PNL_PD5 0x20
104#define PNL_PD6 0x40
105#define PNL_PD7 0x80
106
107#define PIN_NONE 0
108#define PIN_STROBE 1
109#define PIN_D0 2
110#define PIN_D1 3
111#define PIN_D2 4
112#define PIN_D3 5
113#define PIN_D4 6
114#define PIN_D5 7
115#define PIN_D6 8
116#define PIN_D7 9
117#define PIN_AUTOLF 14
118#define PIN_INITP 16
119#define PIN_SELECP 17
120#define PIN_NOT_SET 127
121
Willy Tarreau7005b582008-11-13 17:18:59 -0800122#define LCD_FLAG_S 0x0001
123#define LCD_FLAG_ID 0x0002
124#define LCD_FLAG_B 0x0004 /* blink on */
125#define LCD_FLAG_C 0x0008 /* cursor on */
126#define LCD_FLAG_D 0x0010 /* display on */
127#define LCD_FLAG_F 0x0020 /* large font mode */
128#define LCD_FLAG_N 0x0040 /* 2-rows mode */
129#define LCD_FLAG_L 0x0080 /* backlight enabled */
130
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300131#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
Willy Tarreau7005b582008-11-13 17:18:59 -0800132#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
133
134/* macros to simplify use of the parallel port */
135#define r_ctr(x) (parport_read_control((x)->port))
136#define r_dtr(x) (parport_read_data((x)->port))
137#define r_str(x) (parport_read_status((x)->port))
Willy Tarreau698b1512008-11-22 11:29:33 +0100138#define w_ctr(x, y) do { parport_write_control((x)->port, (y)); } while (0)
139#define w_dtr(x, y) do { parport_write_data((x)->port, (y)); } while (0)
Willy Tarreau7005b582008-11-13 17:18:59 -0800140
141/* this defines which bits are to be used and which ones to be ignored */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300142/* logical or of the output bits involved in the scan matrix */
143static __u8 scan_mask_o;
144/* logical or of the input bits involved in the scan matrix */
145static __u8 scan_mask_i;
Willy Tarreau7005b582008-11-13 17:18:59 -0800146
Willy Tarreau698b1512008-11-22 11:29:33 +0100147typedef __u64 pmask_t;
Willy Tarreau7005b582008-11-13 17:18:59 -0800148
149enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100150 INPUT_TYPE_STD,
151 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800152};
153
154enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100155 INPUT_ST_LOW,
156 INPUT_ST_RISING,
157 INPUT_ST_HIGH,
158 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800159};
160
161struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100162 struct list_head list;
163 pmask_t mask;
164 pmask_t value;
165 enum input_type type;
166 enum input_state state;
167 __u8 rise_time, fall_time;
168 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800169
Willy Tarreau698b1512008-11-22 11:29:33 +0100170 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300171 struct { /* valid when type == INPUT_TYPE_STD */
Willy Tarreau698b1512008-11-22 11:29:33 +0100172 void (*press_fct) (int);
173 void (*release_fct) (int);
174 int press_data;
175 int release_data;
176 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300177 struct { /* valid when type == INPUT_TYPE_KBD */
178 /* strings can be non null-terminated */
Willy Tarreau698b1512008-11-22 11:29:33 +0100179 char press_str[sizeof(void *) + sizeof(int)];
180 char repeat_str[sizeof(void *) + sizeof(int)];
181 char release_str[sizeof(void *) + sizeof(int)];
182 } kbd;
183 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800184};
185
Willy Tarreau698b1512008-11-22 11:29:33 +0100186LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800187
188/* physical contacts history
189 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
190 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
191 * corresponds to the ground.
192 * Within each group, bits are stored in the same order as read on the port :
193 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
194 * So, each __u64 (or pmask_t) is represented like this :
195 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
196 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
197 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300198
199/* what has just been read from the I/O ports */
200static pmask_t phys_read;
201/* previous phys_read */
202static pmask_t phys_read_prev;
203/* stabilized phys_read (phys_read|phys_read_prev) */
204static pmask_t phys_curr;
205/* previous phys_curr */
206static pmask_t phys_prev;
207/* 0 means that at least one logical signal needs be computed */
208static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800209
Willy Tarreau7005b582008-11-13 17:18:59 -0800210/* these variables are specific to the keypad */
211static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100212static int keypad_buflen;
213static int keypad_start;
214static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800215static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800216
217/* lcd-specific variables */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300218
219/* contains the LCD config state */
220static unsigned long int lcd_flags;
221/* contains the LCD X offset */
222static unsigned long int lcd_addr_x;
223/* contains the LCD Y offset */
224static unsigned long int lcd_addr_y;
225/* current escape sequence, 0 terminated */
226static char lcd_escape[LCD_ESCAPE_LEN + 1];
227/* not in escape state. >=0 = escape cmd len */
228static int lcd_escape_len = -1;
Willy Tarreau7005b582008-11-13 17:18:59 -0800229
Willy Tarreau7005b582008-11-13 17:18:59 -0800230/*
231 * Bit masks to convert LCD signals to parallel port outputs.
232 * _d_ are values for data port, _c_ are for control port.
233 * [0] = signal OFF, [1] = signal ON, [2] = mask
234 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100235#define BIT_CLR 0
236#define BIT_SET 1
237#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800238#define BIT_STATES 3
239/*
240 * one entry for each bit on the LCD
241 */
242#define LCD_BIT_E 0
243#define LCD_BIT_RS 1
244#define LCD_BIT_RW 2
245#define LCD_BIT_BL 3
246#define LCD_BIT_CL 4
247#define LCD_BIT_DA 5
248#define LCD_BITS 6
249
250/*
251 * each bit can be either connected to a DATA or CTRL port
252 */
253#define LCD_PORT_C 0
254#define LCD_PORT_D 1
255#define LCD_PORTS 2
256
257static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
258
259/*
260 * LCD protocols
261 */
262#define LCD_PROTO_PARALLEL 0
263#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400264#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800265
266/*
267 * LCD character sets
268 */
269#define LCD_CHARSET_NORMAL 0
270#define LCD_CHARSET_KS0074 1
271
272/*
273 * LCD types
274 */
275#define LCD_TYPE_NONE 0
276#define LCD_TYPE_OLD 1
277#define LCD_TYPE_KS0074 2
278#define LCD_TYPE_HANTRONIX 3
279#define LCD_TYPE_NEXCOM 4
280#define LCD_TYPE_CUSTOM 5
281
282/*
283 * keypad types
284 */
285#define KEYPAD_TYPE_NONE 0
286#define KEYPAD_TYPE_OLD 1
287#define KEYPAD_TYPE_NEW 2
288#define KEYPAD_TYPE_NEXCOM 3
289
290/*
291 * panel profiles
292 */
293#define PANEL_PROFILE_CUSTOM 0
294#define PANEL_PROFILE_OLD 1
295#define PANEL_PROFILE_NEW 2
296#define PANEL_PROFILE_HANTRONIX 3
297#define PANEL_PROFILE_NEXCOM 4
298#define PANEL_PROFILE_LARGE 5
299
300/*
301 * Construct custom config from the kernel's configuration
302 */
303#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
304#define DEFAULT_PARPORT 0
305#define DEFAULT_LCD LCD_TYPE_OLD
306#define DEFAULT_KEYPAD KEYPAD_TYPE_OLD
Willy Tarreau7005b582008-11-13 17:18:59 -0800307#define DEFAULT_LCD_WIDTH 40
308#define DEFAULT_LCD_BWIDTH 40
309#define DEFAULT_LCD_HWIDTH 64
310#define DEFAULT_LCD_HEIGHT 2
311#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
312
313#define DEFAULT_LCD_PIN_E PIN_AUTOLF
314#define DEFAULT_LCD_PIN_RS PIN_SELECP
315#define DEFAULT_LCD_PIN_RW PIN_INITP
316#define DEFAULT_LCD_PIN_SCL PIN_STROBE
317#define DEFAULT_LCD_PIN_SDA PIN_D0
318#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
319#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
320
321#ifdef CONFIG_PANEL_PROFILE
322#undef DEFAULT_PROFILE
323#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
324#endif
325
326#ifdef CONFIG_PANEL_PARPORT
327#undef DEFAULT_PARPORT
328#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
329#endif
330
Willy Tarreau698b1512008-11-22 11:29:33 +0100331#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800332#ifdef CONFIG_PANEL_KEYPAD
333#undef DEFAULT_KEYPAD
334#define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
335#endif
336
Willy Tarreau7005b582008-11-13 17:18:59 -0800337#ifdef CONFIG_PANEL_LCD
338#undef DEFAULT_LCD
339#define DEFAULT_LCD CONFIG_PANEL_LCD
340#endif
341
342#ifdef CONFIG_PANEL_LCD_WIDTH
343#undef DEFAULT_LCD_WIDTH
344#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
345#endif
346
347#ifdef CONFIG_PANEL_LCD_BWIDTH
348#undef DEFAULT_LCD_BWIDTH
349#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
350#endif
351
352#ifdef CONFIG_PANEL_LCD_HWIDTH
353#undef DEFAULT_LCD_HWIDTH
354#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
355#endif
356
357#ifdef CONFIG_PANEL_LCD_HEIGHT
358#undef DEFAULT_LCD_HEIGHT
359#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
360#endif
361
362#ifdef CONFIG_PANEL_LCD_PROTO
363#undef DEFAULT_LCD_PROTO
364#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
365#endif
366
367#ifdef CONFIG_PANEL_LCD_PIN_E
368#undef DEFAULT_LCD_PIN_E
369#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
370#endif
371
372#ifdef CONFIG_PANEL_LCD_PIN_RS
373#undef DEFAULT_LCD_PIN_RS
374#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
375#endif
376
377#ifdef CONFIG_PANEL_LCD_PIN_RW
378#undef DEFAULT_LCD_PIN_RW
379#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
380#endif
381
382#ifdef CONFIG_PANEL_LCD_PIN_SCL
383#undef DEFAULT_LCD_PIN_SCL
384#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
385#endif
386
387#ifdef CONFIG_PANEL_LCD_PIN_SDA
388#undef DEFAULT_LCD_PIN_SDA
389#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
390#endif
391
392#ifdef CONFIG_PANEL_LCD_PIN_BL
393#undef DEFAULT_LCD_PIN_BL
394#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
395#endif
396
397#ifdef CONFIG_PANEL_LCD_CHARSET
398#undef DEFAULT_LCD_CHARSET
Peter Huewea6a6c902009-12-15 06:21:45 +0100399#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800400#endif
401
402#endif /* DEFAULT_PROFILE == 0 */
403
404/* global variables */
Willy Tarreau698b1512008-11-22 11:29:33 +0100405static int keypad_open_cnt; /* #times opened */
406static int lcd_open_cnt; /* #times opened */
Willy Tarreau698b1512008-11-22 11:29:33 +0100407static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800408
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100409static int lcd_initialized;
410static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800411
Willy Tarreau698b1512008-11-22 11:29:33 +0100412static int light_tempo;
Willy Tarreau7005b582008-11-13 17:18:59 -0800413
Willy Tarreau698b1512008-11-22 11:29:33 +0100414static char lcd_must_clear;
415static char lcd_left_shift;
416static char init_in_progress;
Willy Tarreau7005b582008-11-13 17:18:59 -0800417
Willy Tarreau698b1512008-11-22 11:29:33 +0100418static void (*lcd_write_cmd) (int);
419static void (*lcd_write_data) (int);
420static void (*lcd_clear_fast) (void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800421
Willy Tarreau698b1512008-11-22 11:29:33 +0100422static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800423static struct timer_list scan_timer;
424
Willy Tarreau630231772008-11-22 12:52:18 +0100425MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100426
427static int parport = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100428module_param(parport, int, 0000);
429MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100430
431static int lcd_height = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100432module_param(lcd_height, int, 0000);
433MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100434
435static int lcd_width = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100436module_param(lcd_width, int, 0000);
437MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100438
439static int lcd_bwidth = -1; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100440module_param(lcd_bwidth, int, 0000);
441MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100442
443static int lcd_hwidth = -1; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100444module_param(lcd_hwidth, int, 0000);
445MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100446
447static int lcd_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100448module_param(lcd_enabled, int, 0000);
449MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100450
451static int keypad_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100452module_param(keypad_enabled, int, 0000);
453MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100454
455static int lcd_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100456module_param(lcd_type, int, 0000);
457MODULE_PARM_DESC(lcd_type,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300458 "LCD type: 0=none, 1=old //, 2=serial ks0074, "
459 "3=hantronix //, 4=nexcom //, 5=compiled-in");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100460
461static int lcd_proto = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100462module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300463MODULE_PARM_DESC(lcd_proto,
464 "LCD communication: 0=parallel (//), 1=serial,"
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400465 "2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100466
467static int lcd_charset = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100468module_param(lcd_charset, int, 0000);
469MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100470
471static int keypad_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100472module_param(keypad_type, int, 0000);
473MODULE_PARM_DESC(keypad_type,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300474 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, "
475 "3=nexcom 4 keys");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100476
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100477static int profile = DEFAULT_PROFILE;
Willy Tarreau698b1512008-11-22 11:29:33 +0100478module_param(profile, int, 0000);
479MODULE_PARM_DESC(profile,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300480 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
481 "4=16x2 nexcom; default=40x2, old kp");
Willy Tarreau7005b582008-11-13 17:18:59 -0800482
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100483/*
484 * These are the parallel port pins the LCD control signals are connected to.
485 * Set this to 0 if the signal is not used. Set it to its opposite value
486 * (negative) if the signal is negated. -MAXINT is used to indicate that the
487 * pin has not been explicitly specified.
488 *
Willy Tarreau630231772008-11-22 12:52:18 +0100489 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100490 */
491
492static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100493module_param(lcd_e_pin, int, 0000);
494MODULE_PARM_DESC(lcd_e_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300495 "# of the // port pin connected to LCD 'E' signal, "
496 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100497
498static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100499module_param(lcd_rs_pin, int, 0000);
500MODULE_PARM_DESC(lcd_rs_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300501 "# of the // port pin connected to LCD 'RS' signal, "
502 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100503
504static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100505module_param(lcd_rw_pin, int, 0000);
506MODULE_PARM_DESC(lcd_rw_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300507 "# of the // port pin connected to LCD 'RW' signal, "
508 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100509
510static int lcd_bl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100511module_param(lcd_bl_pin, int, 0000);
512MODULE_PARM_DESC(lcd_bl_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300513 "# of the // port pin connected to LCD backlight, "
514 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100515
516static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100517module_param(lcd_da_pin, int, 0000);
518MODULE_PARM_DESC(lcd_da_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300519 "# of the // port pin connected to serial LCD 'SDA' "
520 "signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100521
522static int lcd_cl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100523module_param(lcd_cl_pin, int, 0000);
524MODULE_PARM_DESC(lcd_cl_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300525 "# of the // port pin connected to serial LCD 'SCL' "
526 "signal, with polarity (-17..17)");
Willy Tarreau7005b582008-11-13 17:18:59 -0800527
Willy Tarreau698b1512008-11-22 11:29:33 +0100528static unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800529
530/* for some LCD drivers (ks0074) we need a charset conversion table. */
531static unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100532 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
533 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
534 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
535 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
536 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
537 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
538 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
539 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
540 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
541 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
542 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
543 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
544 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
545 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
546 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
547 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
548 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
549 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
550 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
551 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
552 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
553 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
554 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
555 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
556 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
557 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
558 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
559 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
560 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
561 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
562 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
563 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
564 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800565};
566
567char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100568 {"S0", "Left\n", "Left\n", ""},
569 {"S1", "Down\n", "Down\n", ""},
570 {"S2", "Up\n", "Up\n", ""},
571 {"S3", "Right\n", "Right\n", ""},
572 {"S4", "Esc\n", "Esc\n", ""},
573 {"S5", "Ret\n", "Ret\n", ""},
574 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800575};
576
577/* signals, press, repeat, release */
578char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100579 {"S0", "Left\n", "Left\n", ""},
580 {"S1", "Down\n", "Down\n", ""},
581 {"S2", "Up\n", "Up\n", ""},
582 {"S3", "Right\n", "Right\n", ""},
583 {"S4s5", "", "Esc\n", "Esc\n"},
584 {"s4S5", "", "Ret\n", "Ret\n"},
585 {"S4S5", "Help\n", "", ""},
586 /* add new signals above this line */
587 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800588};
589
590/* signals, press, repeat, release */
591char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100592 {"a-p-e-", "Down\n", "Down\n", ""},
593 {"a-p-E-", "Ret\n", "Ret\n", ""},
594 {"a-P-E-", "Esc\n", "Esc\n", ""},
595 {"a-P-e-", "Up\n", "Up\n", ""},
596 /* add new signals above this line */
597 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800598};
599
600static char (*keypad_profile)[4][9] = old_keypad_profile;
601
602/* FIXME: this should be converted to a bit array containing signals states */
603static struct {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300604 unsigned char e; /* parallel LCD E (data latch on falling edge) */
605 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
606 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
607 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
608 unsigned char cl; /* serial LCD clock (latch on rising edge) */
609 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800610} bits;
611
612static void init_scan_timer(void);
613
614/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100615static int set_data_bits(void)
616{
617 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800618
Willy Tarreau698b1512008-11-22 11:29:33 +0100619 val = r_dtr(pprt);
620 for (bit = 0; bit < LCD_BITS; bit++)
621 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800622
Willy Tarreau698b1512008-11-22 11:29:33 +0100623 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
624 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
625 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
626 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
627 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
628 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800629
Willy Tarreau698b1512008-11-22 11:29:33 +0100630 w_dtr(pprt, val);
631 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800632}
633
634/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100635static int set_ctrl_bits(void)
636{
637 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800638
Willy Tarreau698b1512008-11-22 11:29:33 +0100639 val = r_ctr(pprt);
640 for (bit = 0; bit < LCD_BITS; bit++)
641 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800642
Willy Tarreau698b1512008-11-22 11:29:33 +0100643 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
644 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
645 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
646 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
647 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
648 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800649
Willy Tarreau698b1512008-11-22 11:29:33 +0100650 w_ctr(pprt, val);
651 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800652}
653
654/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530655static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100656{
657 set_data_bits();
658 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800659}
660
661/*
662 * Converts a parallel port pin (from -25 to 25) to data and control ports
663 * masks, and data and control port bits. The signal will be considered
664 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
665 *
666 * Result will be used this way :
667 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
668 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
669 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100670void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
671{
672 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800673
Willy Tarreau698b1512008-11-22 11:29:33 +0100674 d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
675 d_val[2] = c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800676
Willy Tarreau698b1512008-11-22 11:29:33 +0100677 if (pin == 0)
678 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800679
Willy Tarreau698b1512008-11-22 11:29:33 +0100680 inv = (pin < 0);
681 if (inv)
682 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800683
Willy Tarreau698b1512008-11-22 11:29:33 +0100684 d_bit = c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800685
Willy Tarreau698b1512008-11-22 11:29:33 +0100686 switch (pin) {
687 case PIN_STROBE: /* strobe, inverted */
688 c_bit = PNL_PSTROBE;
689 inv = !inv;
690 break;
691 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
692 d_bit = 1 << (pin - 2);
693 break;
694 case PIN_AUTOLF: /* autofeed, inverted */
695 c_bit = PNL_PAUTOLF;
696 inv = !inv;
697 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300698 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100699 c_bit = PNL_PINITP;
700 break;
701 case PIN_SELECP: /* select_in, inverted */
702 c_bit = PNL_PSELECP;
703 inv = !inv;
704 break;
705 default: /* unknown pin, ignore */
706 break;
707 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800708
Willy Tarreau698b1512008-11-22 11:29:33 +0100709 if (c_bit) {
710 c_val[2] &= ~c_bit;
711 c_val[!inv] = c_bit;
712 } else if (d_bit) {
713 d_val[2] &= ~d_bit;
714 d_val[!inv] = d_bit;
715 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800716}
717
718/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100719static void long_sleep(int ms)
720{
Willy Tarreau7005b582008-11-13 17:18:59 -0800721
Willy Tarreau698b1512008-11-22 11:29:33 +0100722 if (in_interrupt())
723 mdelay(ms);
724 else {
725 current->state = TASK_INTERRUPTIBLE;
726 schedule_timeout((ms * HZ + 999) / 1000);
727 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800728}
729
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300730/* send a serial byte to the LCD panel. The caller is responsible for locking
731 if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100732static void lcd_send_serial(int byte)
733{
734 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800735
Willy Tarreau698b1512008-11-22 11:29:33 +0100736 /* the data bit is set on D0, and the clock on STROBE.
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300737 * LCD reads D0 on STROBE's rising edge. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100738 for (bit = 0; bit < 8; bit++) {
739 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530740 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100741 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530742 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300743 udelay(2); /* maintain the data during 2 us before CLK up */
Willy Tarreau698b1512008-11-22 11:29:33 +0100744 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530745 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300746 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100747 byte >>= 1;
748 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800749}
750
751/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100752static void lcd_backlight(int on)
753{
754 if (lcd_bl_pin == PIN_NONE)
755 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800756
Willy Tarreau698b1512008-11-22 11:29:33 +0100757 /* The backlight is activated by seting the AUTOFEED line to +5V */
758 spin_lock(&pprt_lock);
759 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530760 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100761 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800762}
763
764/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100765static void lcd_write_cmd_s(int cmd)
766{
767 spin_lock(&pprt_lock);
768 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
769 lcd_send_serial(cmd & 0x0F);
770 lcd_send_serial((cmd >> 4) & 0x0F);
771 udelay(40); /* the shortest command takes at least 40 us */
772 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800773}
774
775/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100776static void lcd_write_data_s(int data)
777{
778 spin_lock(&pprt_lock);
779 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
780 lcd_send_serial(data & 0x0F);
781 lcd_send_serial((data >> 4) & 0x0F);
782 udelay(40); /* the shortest data takes at least 40 us */
783 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800784}
785
786/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100787static void lcd_write_cmd_p8(int cmd)
788{
789 spin_lock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800790 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100791 w_dtr(pprt, cmd);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300792 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800793
Willy Tarreau698b1512008-11-22 11:29:33 +0100794 bits.e = BIT_SET;
795 bits.rs = BIT_CLR;
796 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800797 set_ctrl_bits();
798
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300799 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800800
801 bits.e = BIT_CLR;
802 set_ctrl_bits();
803
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300804 udelay(120); /* the shortest command takes at least 120 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100805 spin_unlock(&pprt_lock);
806}
Willy Tarreau7005b582008-11-13 17:18:59 -0800807
Willy Tarreau698b1512008-11-22 11:29:33 +0100808/* send data to the LCD panel in 8 bits parallel mode */
809static void lcd_write_data_p8(int data)
810{
811 spin_lock(&pprt_lock);
812 /* present the data to the data port */
813 w_dtr(pprt, data);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300814 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100815
816 bits.e = BIT_SET;
817 bits.rs = BIT_SET;
818 bits.rw = BIT_CLR;
819 set_ctrl_bits();
820
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300821 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100822
823 bits.e = BIT_CLR;
824 set_ctrl_bits();
825
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300826 udelay(45); /* the shortest data takes at least 45 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100827 spin_unlock(&pprt_lock);
828}
829
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400830/* send a command to the TI LCD panel */
831static void lcd_write_cmd_tilcd(int cmd)
832{
833 spin_lock(&pprt_lock);
834 /* present the data to the control port */
835 w_ctr(pprt, cmd);
836 udelay(60);
837 spin_unlock(&pprt_lock);
838}
839
840/* send data to the TI LCD panel */
841static void lcd_write_data_tilcd(int data)
842{
843 spin_lock(&pprt_lock);
844 /* present the data to the data port */
845 w_dtr(pprt, data);
846 udelay(60);
847 spin_unlock(&pprt_lock);
848}
849
Willy Tarreau698b1512008-11-22 11:29:33 +0100850static void lcd_gotoxy(void)
851{
852 lcd_write_cmd(0x80 /* set DDRAM address */
853 | (lcd_addr_y ? lcd_hwidth : 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300854 /* we force the cursor to stay at the end of the
855 line if it wants to go farther */
Willy Tarreau698b1512008-11-22 11:29:33 +0100856 | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
857 (lcd_hwidth - 1) : lcd_bwidth - 1));
858}
859
860static void lcd_print(char c)
861{
862 if (lcd_addr_x < lcd_bwidth) {
863 if (lcd_char_conv != NULL)
864 c = lcd_char_conv[(unsigned char)c];
865 lcd_write_data(c);
866 lcd_addr_x++;
867 }
868 /* prevents the cursor from wrapping onto the next line */
869 if (lcd_addr_x == lcd_bwidth)
870 lcd_gotoxy();
871}
872
873/* fills the display with spaces and resets X/Y */
874static void lcd_clear_fast_s(void)
875{
876 int pos;
877 lcd_addr_x = lcd_addr_y = 0;
878 lcd_gotoxy();
879
880 spin_lock(&pprt_lock);
881 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
882 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
883 lcd_send_serial(' ' & 0x0F);
884 lcd_send_serial((' ' >> 4) & 0x0F);
885 udelay(40); /* the shortest data takes at least 40 us */
886 }
887 spin_unlock(&pprt_lock);
888
889 lcd_addr_x = lcd_addr_y = 0;
890 lcd_gotoxy();
891}
892
893/* fills the display with spaces and resets X/Y */
894static void lcd_clear_fast_p8(void)
895{
896 int pos;
897 lcd_addr_x = lcd_addr_y = 0;
898 lcd_gotoxy();
899
900 spin_lock(&pprt_lock);
901 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
902 /* present the data to the data port */
903 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300904
905 /* maintain the data during 20 us before the strobe */
906 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100907
908 bits.e = BIT_SET;
909 bits.rs = BIT_SET;
910 bits.rw = BIT_CLR;
911 set_ctrl_bits();
912
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300913 /* maintain the strobe during 40 us */
914 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100915
916 bits.e = BIT_CLR;
917 set_ctrl_bits();
918
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300919 /* the shortest data takes at least 45 us */
920 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100921 }
922 spin_unlock(&pprt_lock);
923
924 lcd_addr_x = lcd_addr_y = 0;
925 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800926}
927
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400928/* fills the display with spaces and resets X/Y */
929static void lcd_clear_fast_tilcd(void)
930{
931 int pos;
932 lcd_addr_x = lcd_addr_y = 0;
933 lcd_gotoxy();
934
935 spin_lock(&pprt_lock);
936 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
937 /* present the data to the data port */
938 w_dtr(pprt, ' ');
939 udelay(60);
940 }
941
942 spin_unlock(&pprt_lock);
943
944 lcd_addr_x = lcd_addr_y = 0;
945 lcd_gotoxy();
946}
947
Willy Tarreau7005b582008-11-13 17:18:59 -0800948/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +0100949static void lcd_clear_display(void)
950{
951 lcd_write_cmd(0x01); /* clear display */
952 lcd_addr_x = lcd_addr_y = 0;
953 /* we must wait a few milliseconds (15) */
954 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -0800955}
956
Willy Tarreau698b1512008-11-22 11:29:33 +0100957static void lcd_init_display(void)
958{
Willy Tarreau7005b582008-11-13 17:18:59 -0800959
Willy Tarreau698b1512008-11-22 11:29:33 +0100960 lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
961 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -0800962
Willy Tarreau698b1512008-11-22 11:29:33 +0100963 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -0800964
Willy Tarreau698b1512008-11-22 11:29:33 +0100965 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
966 long_sleep(10);
967 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
968 long_sleep(10);
969 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
970 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800971
Willy Tarreau698b1512008-11-22 11:29:33 +0100972 lcd_write_cmd(0x30 /* set font height and lines number */
973 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
974 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0)
975 );
976 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800977
Willy Tarreau698b1512008-11-22 11:29:33 +0100978 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
979 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800980
Willy Tarreau698b1512008-11-22 11:29:33 +0100981 lcd_write_cmd(0x08 /* set display mode */
982 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
983 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
984 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0)
985 );
Willy Tarreau7005b582008-11-13 17:18:59 -0800986
Willy Tarreau698b1512008-11-22 11:29:33 +0100987 lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -0800988
Willy Tarreau698b1512008-11-22 11:29:33 +0100989 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800990
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300991 /* entry mode set : increment, cursor shifting */
992 lcd_write_cmd(0x06);
Willy Tarreau7005b582008-11-13 17:18:59 -0800993
Willy Tarreau698b1512008-11-22 11:29:33 +0100994 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -0800995}
996
997/*
998 * These are the file operation function for user access to /dev/lcd
999 * This function can also be called from inside the kernel, by
1000 * setting file and ppos to NULL.
1001 *
1002 */
1003
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001004static inline int handle_lcd_special_code(void)
1005{
1006 /* LCD special codes */
1007
1008 int processed = 0;
1009
1010 char *esc = lcd_escape + 2;
1011 int oldflags = lcd_flags;
1012
1013 /* check for display mode flags */
1014 switch (*esc) {
1015 case 'D': /* Display ON */
1016 lcd_flags |= LCD_FLAG_D;
1017 processed = 1;
1018 break;
1019 case 'd': /* Display OFF */
1020 lcd_flags &= ~LCD_FLAG_D;
1021 processed = 1;
1022 break;
1023 case 'C': /* Cursor ON */
1024 lcd_flags |= LCD_FLAG_C;
1025 processed = 1;
1026 break;
1027 case 'c': /* Cursor OFF */
1028 lcd_flags &= ~LCD_FLAG_C;
1029 processed = 1;
1030 break;
1031 case 'B': /* Blink ON */
1032 lcd_flags |= LCD_FLAG_B;
1033 processed = 1;
1034 break;
1035 case 'b': /* Blink OFF */
1036 lcd_flags &= ~LCD_FLAG_B;
1037 processed = 1;
1038 break;
1039 case '+': /* Back light ON */
1040 lcd_flags |= LCD_FLAG_L;
1041 processed = 1;
1042 break;
1043 case '-': /* Back light OFF */
1044 lcd_flags &= ~LCD_FLAG_L;
1045 processed = 1;
1046 break;
1047 case '*':
1048 /* flash back light using the keypad timer */
1049 if (scan_timer.function != NULL) {
1050 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1051 lcd_backlight(1);
1052 light_tempo = FLASH_LIGHT_TEMPO;
1053 }
1054 processed = 1;
1055 break;
1056 case 'f': /* Small Font */
1057 lcd_flags &= ~LCD_FLAG_F;
1058 processed = 1;
1059 break;
1060 case 'F': /* Large Font */
1061 lcd_flags |= LCD_FLAG_F;
1062 processed = 1;
1063 break;
1064 case 'n': /* One Line */
1065 lcd_flags &= ~LCD_FLAG_N;
1066 processed = 1;
1067 break;
1068 case 'N': /* Two Lines */
1069 lcd_flags |= LCD_FLAG_N;
1070 break;
1071 case 'l': /* Shift Cursor Left */
1072 if (lcd_addr_x > 0) {
1073 /* back one char if not at end of line */
1074 if (lcd_addr_x < lcd_bwidth)
1075 lcd_write_cmd(0x10);
1076 lcd_addr_x--;
1077 }
1078 processed = 1;
1079 break;
1080 case 'r': /* shift cursor right */
1081 if (lcd_addr_x < lcd_width) {
1082 /* allow the cursor to pass the end of the line */
1083 if (lcd_addr_x <
1084 (lcd_bwidth - 1))
1085 lcd_write_cmd(0x14);
1086 lcd_addr_x++;
1087 }
1088 processed = 1;
1089 break;
1090 case 'L': /* shift display left */
1091 lcd_left_shift++;
1092 lcd_write_cmd(0x18);
1093 processed = 1;
1094 break;
1095 case 'R': /* shift display right */
1096 lcd_left_shift--;
1097 lcd_write_cmd(0x1C);
1098 processed = 1;
1099 break;
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
1105 /* restore cursor position */
1106 lcd_gotoxy();
1107 processed = 1;
1108 break;
1109 }
1110 case 'I': /* reinitialize display */
1111 lcd_init_display();
1112 lcd_left_shift = 0;
1113 processed = 1;
1114 break;
1115 case 'G': {
1116 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1117 * and '7', representing the numerical ASCII code of the
1118 * redefined character, and <xx...xx> a sequence of 16
1119 * hex digits representing 8 bytes for each character.
1120 * Most LCDs will only use 5 lower bits of the 7 first
1121 * bytes.
1122 */
1123
1124 unsigned char cgbytes[8];
1125 unsigned char cgaddr;
1126 int cgoffset;
1127 int shift;
1128 char value;
1129 int addr;
1130
1131 if (strchr(esc, ';') == NULL)
1132 break;
1133
1134 esc++;
1135
1136 cgaddr = *(esc++) - '0';
1137 if (cgaddr > 7) {
1138 processed = 1;
1139 break;
1140 }
1141
1142 cgoffset = 0;
1143 shift = 0;
1144 value = 0;
1145 while (*esc && cgoffset < 8) {
1146 shift ^= 4;
1147 if (*esc >= '0' && *esc <= '9')
1148 value |= (*esc - '0') << shift;
1149 else if (*esc >= 'A' && *esc <= 'Z')
1150 value |= (*esc - 'A' + 10) << shift;
1151 else if (*esc >= 'a' && *esc <= 'z')
1152 value |= (*esc - 'a' + 10) << shift;
1153 else {
1154 esc++;
1155 continue;
1156 }
1157
1158 if (shift == 0) {
1159 cgbytes[cgoffset++] = value;
1160 value = 0;
1161 }
1162
1163 esc++;
1164 }
1165
1166 lcd_write_cmd(0x40 | (cgaddr * 8));
1167 for (addr = 0; addr < cgoffset; addr++)
1168 lcd_write_data(cgbytes[addr]);
1169
1170 /* ensures that we stop writing to CGRAM */
1171 lcd_gotoxy();
1172 processed = 1;
1173 break;
1174 }
1175 case 'x': /* gotoxy : LxXXX[yYYY]; */
1176 case 'y': /* gotoxy : LyYYY[xXXX]; */
1177 if (strchr(esc, ';') == NULL)
1178 break;
1179
1180 while (*esc) {
1181 if (*esc == 'x') {
1182 esc++;
Pelle Windestam12995702011-08-30 20:29:24 +02001183 if (kstrtoul(esc, 10, &lcd_addr_x) < 0)
1184 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001185 } else if (*esc == 'y') {
1186 esc++;
Pelle Windestam12995702011-08-30 20:29:24 +02001187 if (kstrtoul(esc, 10, &lcd_addr_y) < 0)
1188 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001189 } else
1190 break;
1191 }
1192
1193 lcd_gotoxy();
1194 processed = 1;
1195 break;
1196 }
1197
1198 /* Check wether one flag was changed */
1199 if (oldflags != lcd_flags) {
1200 /* check whether one of B,C,D flags were changed */
1201 if ((oldflags ^ lcd_flags) &
1202 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1203 /* set display mode */
1204 lcd_write_cmd(0x08
1205 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
1206 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
1207 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0));
1208 /* check whether one of F,N flags was changed */
1209 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_F | LCD_FLAG_N))
1210 lcd_write_cmd(0x30
1211 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
1212 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
1213 /* check wether L flag was changed */
1214 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_L)) {
1215 if (lcd_flags & (LCD_FLAG_L))
1216 lcd_backlight(1);
1217 else if (light_tempo == 0)
1218 /* switch off the light only when the tempo
1219 lighting is gone */
1220 lcd_backlight(0);
1221 }
1222 }
1223
1224 return processed;
1225}
1226
Willy Tarreau698b1512008-11-22 11:29:33 +01001227static ssize_t lcd_write(struct file *file,
1228 const char *buf, size_t count, loff_t *ppos)
1229{
Willy Tarreau698b1512008-11-22 11:29:33 +01001230 const char *tmp = buf;
1231 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001232
Willy Tarreau698b1512008-11-22 11:29:33 +01001233 for (; count-- > 0; (ppos ? (*ppos)++ : 0), ++tmp) {
1234 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001235 /* let's be a little nice with other processes
1236 that need some CPU */
1237 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001238
Willy Tarreau698b1512008-11-22 11:29:33 +01001239 if (ppos == NULL && file == NULL)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001240 /* let's not use get_user() from the kernel ! */
1241 c = *tmp;
Willy Tarreau698b1512008-11-22 11:29:33 +01001242 else if (get_user(c, tmp))
1243 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001244
Willy Tarreau698b1512008-11-22 11:29:33 +01001245 /* first, we'll test if we're in escape mode */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001246 if ((c != '\n') && lcd_escape_len >= 0) {
1247 /* yes, let's add this char to the buffer */
Willy Tarreau698b1512008-11-22 11:29:33 +01001248 lcd_escape[lcd_escape_len++] = c;
1249 lcd_escape[lcd_escape_len] = 0;
1250 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001251 /* aborts any previous escape sequence */
1252 lcd_escape_len = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +01001253
1254 switch (c) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001255 case LCD_ESCAPE_CHAR:
1256 /* start of an escape sequence */
Willy Tarreau698b1512008-11-22 11:29:33 +01001257 lcd_escape_len = 0;
1258 lcd_escape[lcd_escape_len] = 0;
1259 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001260 case '\b':
1261 /* go back one char and clear it */
Willy Tarreau698b1512008-11-22 11:29:33 +01001262 if (lcd_addr_x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001263 /* check if we're not at the
1264 end of the line */
1265 if (lcd_addr_x < lcd_bwidth)
1266 /* back one char */
1267 lcd_write_cmd(0x10);
Willy Tarreau698b1512008-11-22 11:29:33 +01001268 lcd_addr_x--;
1269 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001270 /* replace with a space */
1271 lcd_write_data(' ');
1272 /* back one char again */
1273 lcd_write_cmd(0x10);
Willy Tarreau698b1512008-11-22 11:29:33 +01001274 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001275 case '\014':
1276 /* quickly clear the display */
Willy Tarreau698b1512008-11-22 11:29:33 +01001277 lcd_clear_fast();
1278 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001279 case '\n':
1280 /* flush the remainder of the current line and
1281 go to the beginning of the next line */
Willy Tarreau698b1512008-11-22 11:29:33 +01001282 for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
1283 lcd_write_data(' ');
1284 lcd_addr_x = 0;
1285 lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
1286 lcd_gotoxy();
1287 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001288 case '\r':
1289 /* go to the beginning of the same line */
Willy Tarreau698b1512008-11-22 11:29:33 +01001290 lcd_addr_x = 0;
1291 lcd_gotoxy();
1292 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001293 case '\t':
1294 /* print a space instead of the tab */
Willy Tarreau698b1512008-11-22 11:29:33 +01001295 lcd_print(' ');
1296 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001297 default:
1298 /* simply print this char */
Willy Tarreau698b1512008-11-22 11:29:33 +01001299 lcd_print(c);
1300 break;
1301 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001302 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001303
1304 /* now we'll see if we're in an escape mode and if the current
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001305 escape sequence can be understood. */
1306 if (lcd_escape_len >= 2) {
1307 int processed = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001308
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001309 if (!strcmp(lcd_escape, "[2J")) {
1310 /* clear the display */
1311 lcd_clear_fast();
Willy Tarreau698b1512008-11-22 11:29:33 +01001312 processed = 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001313 } else if (!strcmp(lcd_escape, "[H")) {
1314 /* cursor to home */
Willy Tarreau698b1512008-11-22 11:29:33 +01001315 lcd_addr_x = lcd_addr_y = 0;
1316 lcd_gotoxy();
1317 processed = 1;
1318 }
1319 /* codes starting with ^[[L */
1320 else if ((lcd_escape_len >= 3) &&
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001321 (lcd_escape[0] == '[') &&
1322 (lcd_escape[1] == 'L')) {
1323 processed = handle_lcd_special_code();
Willy Tarreau698b1512008-11-22 11:29:33 +01001324 }
1325
1326 /* LCD special escape codes */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001327 /* flush the escape sequence if it's been processed
1328 or if it is getting too long. */
Willy Tarreau698b1512008-11-22 11:29:33 +01001329 if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1330 lcd_escape_len = -1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001331 } /* escape codes */
Willy Tarreau7005b582008-11-13 17:18:59 -08001332 }
1333
Willy Tarreau698b1512008-11-22 11:29:33 +01001334 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001335}
1336
Willy Tarreau698b1512008-11-22 11:29:33 +01001337static int lcd_open(struct inode *inode, struct file *file)
1338{
1339 if (lcd_open_cnt)
1340 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001341
Willy Tarreau698b1512008-11-22 11:29:33 +01001342 if (file->f_mode & FMODE_READ) /* device is write-only */
1343 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001344
Willy Tarreau698b1512008-11-22 11:29:33 +01001345 if (lcd_must_clear) {
1346 lcd_clear_display();
1347 lcd_must_clear = 0;
1348 }
1349 lcd_open_cnt++;
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001350 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001351}
1352
Willy Tarreau698b1512008-11-22 11:29:33 +01001353static int lcd_release(struct inode *inode, struct file *file)
1354{
1355 lcd_open_cnt--;
1356 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001357}
1358
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001359static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001360 .write = lcd_write,
1361 .open = lcd_open,
1362 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001363 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001364};
1365
1366static struct miscdevice lcd_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001367 LCD_MINOR,
1368 "lcd",
1369 &lcd_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001370};
1371
Willy Tarreau7005b582008-11-13 17:18:59 -08001372/* public function usable from the kernel for any purpose */
Willy Tarreau698b1512008-11-22 11:29:33 +01001373void panel_lcd_print(char *s)
1374{
1375 if (lcd_enabled && lcd_initialized)
1376 lcd_write(NULL, s, strlen(s), NULL);
Willy Tarreau7005b582008-11-13 17:18:59 -08001377}
1378
Willy Tarreau7005b582008-11-13 17:18:59 -08001379/* initialize the LCD driver */
Willy Tarreau698b1512008-11-22 11:29:33 +01001380void lcd_init(void)
1381{
1382 switch (lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001383 case LCD_TYPE_OLD:
1384 /* parallel mode, 8 bits */
Willy Tarreau698b1512008-11-22 11:29:33 +01001385 if (lcd_proto < 0)
1386 lcd_proto = LCD_PROTO_PARALLEL;
1387 if (lcd_charset < 0)
1388 lcd_charset = LCD_CHARSET_NORMAL;
1389 if (lcd_e_pin == PIN_NOT_SET)
1390 lcd_e_pin = PIN_STROBE;
1391 if (lcd_rs_pin == PIN_NOT_SET)
1392 lcd_rs_pin = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001393
Willy Tarreau698b1512008-11-22 11:29:33 +01001394 if (lcd_width < 0)
1395 lcd_width = 40;
1396 if (lcd_bwidth < 0)
1397 lcd_bwidth = 40;
1398 if (lcd_hwidth < 0)
1399 lcd_hwidth = 64;
1400 if (lcd_height < 0)
1401 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001402 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001403 case LCD_TYPE_KS0074:
1404 /* serial mode, ks0074 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001405 if (lcd_proto < 0)
1406 lcd_proto = LCD_PROTO_SERIAL;
1407 if (lcd_charset < 0)
1408 lcd_charset = LCD_CHARSET_KS0074;
1409 if (lcd_bl_pin == PIN_NOT_SET)
1410 lcd_bl_pin = PIN_AUTOLF;
1411 if (lcd_cl_pin == PIN_NOT_SET)
1412 lcd_cl_pin = PIN_STROBE;
1413 if (lcd_da_pin == PIN_NOT_SET)
1414 lcd_da_pin = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001415
Willy Tarreau698b1512008-11-22 11:29:33 +01001416 if (lcd_width < 0)
1417 lcd_width = 16;
1418 if (lcd_bwidth < 0)
1419 lcd_bwidth = 40;
1420 if (lcd_hwidth < 0)
1421 lcd_hwidth = 16;
1422 if (lcd_height < 0)
1423 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001424 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001425 case LCD_TYPE_NEXCOM:
1426 /* parallel mode, 8 bits, generic */
Willy Tarreau698b1512008-11-22 11:29:33 +01001427 if (lcd_proto < 0)
1428 lcd_proto = LCD_PROTO_PARALLEL;
1429 if (lcd_charset < 0)
1430 lcd_charset = LCD_CHARSET_NORMAL;
1431 if (lcd_e_pin == PIN_NOT_SET)
1432 lcd_e_pin = PIN_AUTOLF;
1433 if (lcd_rs_pin == PIN_NOT_SET)
1434 lcd_rs_pin = PIN_SELECP;
1435 if (lcd_rw_pin == PIN_NOT_SET)
1436 lcd_rw_pin = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001437
Willy Tarreau698b1512008-11-22 11:29:33 +01001438 if (lcd_width < 0)
1439 lcd_width = 16;
1440 if (lcd_bwidth < 0)
1441 lcd_bwidth = 40;
1442 if (lcd_hwidth < 0)
1443 lcd_hwidth = 64;
1444 if (lcd_height < 0)
1445 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001446 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001447 case LCD_TYPE_CUSTOM:
1448 /* customer-defined */
Willy Tarreau698b1512008-11-22 11:29:33 +01001449 if (lcd_proto < 0)
1450 lcd_proto = DEFAULT_LCD_PROTO;
1451 if (lcd_charset < 0)
1452 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001453 /* default geometry will be set later */
1454 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001455 case LCD_TYPE_HANTRONIX:
1456 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001457 default:
1458 if (lcd_proto < 0)
1459 lcd_proto = LCD_PROTO_PARALLEL;
1460 if (lcd_charset < 0)
1461 lcd_charset = LCD_CHARSET_NORMAL;
1462 if (lcd_e_pin == PIN_NOT_SET)
1463 lcd_e_pin = PIN_STROBE;
1464 if (lcd_rs_pin == PIN_NOT_SET)
1465 lcd_rs_pin = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001466
Willy Tarreau698b1512008-11-22 11:29:33 +01001467 if (lcd_width < 0)
1468 lcd_width = 16;
1469 if (lcd_bwidth < 0)
1470 lcd_bwidth = 40;
1471 if (lcd_hwidth < 0)
1472 lcd_hwidth = 64;
1473 if (lcd_height < 0)
1474 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001475 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001476 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001477
Willy Tarreau698b1512008-11-22 11:29:33 +01001478 /* this is used to catch wrong and default values */
1479 if (lcd_width <= 0)
1480 lcd_width = DEFAULT_LCD_WIDTH;
1481 if (lcd_bwidth <= 0)
1482 lcd_bwidth = DEFAULT_LCD_BWIDTH;
1483 if (lcd_hwidth <= 0)
1484 lcd_hwidth = DEFAULT_LCD_HWIDTH;
1485 if (lcd_height <= 0)
1486 lcd_height = DEFAULT_LCD_HEIGHT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001487
Willy Tarreau698b1512008-11-22 11:29:33 +01001488 if (lcd_proto == LCD_PROTO_SERIAL) { /* SERIAL */
1489 lcd_write_cmd = lcd_write_cmd_s;
1490 lcd_write_data = lcd_write_data_s;
1491 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001492
Willy Tarreau698b1512008-11-22 11:29:33 +01001493 if (lcd_cl_pin == PIN_NOT_SET)
1494 lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1495 if (lcd_da_pin == PIN_NOT_SET)
1496 lcd_da_pin = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001497
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001498 } else if (lcd_proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001499 lcd_write_cmd = lcd_write_cmd_p8;
1500 lcd_write_data = lcd_write_data_p8;
1501 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001502
Willy Tarreau698b1512008-11-22 11:29:33 +01001503 if (lcd_e_pin == PIN_NOT_SET)
1504 lcd_e_pin = DEFAULT_LCD_PIN_E;
1505 if (lcd_rs_pin == PIN_NOT_SET)
1506 lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1507 if (lcd_rw_pin == PIN_NOT_SET)
1508 lcd_rw_pin = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001509 } else {
1510 lcd_write_cmd = lcd_write_cmd_tilcd;
1511 lcd_write_data = lcd_write_data_tilcd;
1512 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001513 }
1514
1515 if (lcd_bl_pin == PIN_NOT_SET)
1516 lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1517
1518 if (lcd_e_pin == PIN_NOT_SET)
1519 lcd_e_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001520 if (lcd_rs_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001521 lcd_rs_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001522 if (lcd_rw_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001523 lcd_rw_pin = PIN_NONE;
1524 if (lcd_bl_pin == PIN_NOT_SET)
1525 lcd_bl_pin = PIN_NONE;
1526 if (lcd_cl_pin == PIN_NOT_SET)
1527 lcd_cl_pin = PIN_NONE;
1528 if (lcd_da_pin == PIN_NOT_SET)
1529 lcd_da_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001530
Willy Tarreau698b1512008-11-22 11:29:33 +01001531 if (lcd_charset < 0)
1532 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001533
Willy Tarreau698b1512008-11-22 11:29:33 +01001534 if (lcd_charset == LCD_CHARSET_KS0074)
1535 lcd_char_conv = lcd_char_conv_ks0074;
1536 else
1537 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001538
Willy Tarreau698b1512008-11-22 11:29:33 +01001539 if (lcd_bl_pin != PIN_NONE)
1540 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001541
Willy Tarreau698b1512008-11-22 11:29:33 +01001542 pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1543 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1544 pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1545 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1546 pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1547 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1548 pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1549 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1550 pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1551 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1552 pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1553 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001554
Willy Tarreau698b1512008-11-22 11:29:33 +01001555 /* before this line, we must NOT send anything to the display.
1556 * Since lcd_init_display() needs to write data, we have to
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001557 * enable mark the LCD initialized just before. */
Willy Tarreau698b1512008-11-22 11:29:33 +01001558 lcd_initialized = 1;
1559 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001560
Willy Tarreau698b1512008-11-22 11:29:33 +01001561 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001562#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1563#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001564 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001565#endif
1566#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001567 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1568 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001569#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001570 lcd_addr_x = lcd_addr_y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001571 /* clear the display on the next device opening */
1572 lcd_must_clear = 1;
Willy Tarreau698b1512008-11-22 11:29:33 +01001573 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001574}
1575
Willy Tarreau7005b582008-11-13 17:18:59 -08001576/*
1577 * These are the file operation function for user access to /dev/keypad
1578 */
1579
Willy Tarreau698b1512008-11-22 11:29:33 +01001580static ssize_t keypad_read(struct file *file,
1581 char *buf, size_t count, loff_t *ppos)
1582{
Willy Tarreau7005b582008-11-13 17:18:59 -08001583
Willy Tarreau698b1512008-11-22 11:29:33 +01001584 unsigned i = *ppos;
1585 char *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001586
Willy Tarreau698b1512008-11-22 11:29:33 +01001587 if (keypad_buflen == 0) {
1588 if (file->f_flags & O_NONBLOCK)
1589 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001590
Willy Tarreau698b1512008-11-22 11:29:33 +01001591 interruptible_sleep_on(&keypad_read_wait);
1592 if (signal_pending(current))
1593 return -EINTR;
1594 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001595
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001596 for (; count-- > 0 && (keypad_buflen > 0);
1597 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001598 put_user(keypad_buffer[keypad_start], tmp);
1599 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1600 }
1601 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001602
Willy Tarreau698b1512008-11-22 11:29:33 +01001603 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001604}
1605
Willy Tarreau698b1512008-11-22 11:29:33 +01001606static int keypad_open(struct inode *inode, struct file *file)
1607{
Willy Tarreau7005b582008-11-13 17:18:59 -08001608
Willy Tarreau698b1512008-11-22 11:29:33 +01001609 if (keypad_open_cnt)
1610 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001611
Willy Tarreau698b1512008-11-22 11:29:33 +01001612 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1613 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001614
Willy Tarreau698b1512008-11-22 11:29:33 +01001615 keypad_buflen = 0; /* flush the buffer on opening */
1616 keypad_open_cnt++;
1617 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001618}
1619
Willy Tarreau698b1512008-11-22 11:29:33 +01001620static int keypad_release(struct inode *inode, struct file *file)
1621{
1622 keypad_open_cnt--;
1623 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001624}
1625
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001626static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001627 .read = keypad_read, /* read */
1628 .open = keypad_open, /* open */
1629 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001630 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001631};
1632
1633static struct miscdevice keypad_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001634 KEYPAD_MINOR,
1635 "keypad",
1636 &keypad_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001637};
1638
Willy Tarreau698b1512008-11-22 11:29:33 +01001639static void keypad_send_key(char *string, int max_len)
1640{
1641 if (init_in_progress)
1642 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001643
Willy Tarreau698b1512008-11-22 11:29:33 +01001644 /* send the key to the device only if a process is attached to it. */
1645 if (keypad_open_cnt > 0) {
1646 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1647 keypad_buffer[(keypad_start + keypad_buflen++) %
1648 KEYPAD_BUFFER] = *string++;
1649 }
1650 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001651 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001652}
1653
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001654/* this function scans all the bits involving at least one logical signal,
1655 * and puts the results in the bitfield "phys_read" (one bit per established
1656 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001657 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001658 * Note: to debounce input signals, we will only consider as switched a signal
1659 * which is stable across 2 measures. Signals which are different between two
1660 * reads will be kept as they previously were in their logical form (phys_prev).
1661 * A signal which has just switched will have a 1 in
1662 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001663 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001664static void phys_scan_contacts(void)
1665{
1666 int bit, bitval;
1667 char oldval;
1668 char bitmask;
1669 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001670
Willy Tarreau698b1512008-11-22 11:29:33 +01001671 phys_prev = phys_curr;
1672 phys_read_prev = phys_read;
1673 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001674
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001675 /* keep track of old value, with all outputs disabled */
1676 oldval = r_dtr(pprt) | scan_mask_o;
1677 /* activate all keyboard outputs (active low) */
1678 w_dtr(pprt, oldval & ~scan_mask_o);
1679
1680 /* will have a 1 for each bit set to gnd */
1681 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1682 /* disable all matrix signals */
1683 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001684
Willy Tarreau698b1512008-11-22 11:29:33 +01001685 /* now that all outputs are cleared, the only active input bits are
1686 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001687 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001688
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001689 /* 1 for each grounded input */
1690 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1691
1692 /* grounded inputs are signals 40-44 */
1693 phys_read |= (pmask_t) gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001694
Willy Tarreau698b1512008-11-22 11:29:33 +01001695 if (bitmask != gndmask) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001696 /* since clearing the outputs changed some inputs, we know
1697 * that some input signals are currently tied to some outputs.
1698 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001699 */
1700 for (bit = 0; bit < 8; bit++) {
1701 bitval = 1 << bit;
1702
1703 if (!(scan_mask_o & bitval)) /* skip unused bits */
1704 continue;
1705
1706 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1707 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1708 phys_read |= (pmask_t) bitmask << (5 * bit);
1709 }
1710 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001711 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001712 /* this is easy: use old bits when they are flapping,
1713 * use new ones when stable */
1714 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1715 (phys_read & ~(phys_read ^ phys_read_prev));
1716}
1717
1718static inline int input_state_high(struct logical_input *input)
1719{
1720#if 0
1721 /* FIXME:
1722 * this is an invalid test. It tries to catch
1723 * transitions from single-key to multiple-key, but
1724 * doesn't take into account the contacts polarity.
1725 * The only solution to the problem is to parse keys
1726 * from the most complex to the simplest combinations,
1727 * and mark them as 'caught' once a combination
1728 * matches, then unmatch it for all other ones.
1729 */
1730
1731 /* try to catch dangerous transitions cases :
1732 * someone adds a bit, so this signal was a false
1733 * positive resulting from a transition. We should
1734 * invalidate the signal immediately and not call the
1735 * release function.
1736 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1737 */
1738 if (((phys_prev & input->mask) == input->value)
1739 && ((phys_curr & input->mask) > input->value)) {
1740 input->state = INPUT_ST_LOW; /* invalidate */
1741 return 1;
1742 }
1743#endif
1744
1745 if ((phys_curr & input->mask) == input->value) {
1746 if ((input->type == INPUT_TYPE_STD) &&
1747 (input->high_timer == 0)) {
1748 input->high_timer++;
1749 if (input->u.std.press_fct != NULL)
1750 input->u.std.press_fct(input->u.std.press_data);
1751 } else if (input->type == INPUT_TYPE_KBD) {
1752 /* will turn on the light */
1753 keypressed = 1;
1754
1755 if (input->high_timer == 0) {
1756 char *press_str = input->u.kbd.press_str;
1757 if (press_str[0])
1758 keypad_send_key(press_str,
1759 sizeof(press_str));
1760 }
1761
1762 if (input->u.kbd.repeat_str[0]) {
1763 char *repeat_str = input->u.kbd.repeat_str;
1764 if (input->high_timer >= KEYPAD_REP_START) {
1765 input->high_timer -= KEYPAD_REP_DELAY;
1766 keypad_send_key(repeat_str,
1767 sizeof(repeat_str));
1768 }
1769 /* we will need to come back here soon */
1770 inputs_stable = 0;
1771 }
1772
1773 if (input->high_timer < 255)
1774 input->high_timer++;
1775 }
1776 return 1;
1777 } else {
1778 /* else signal falling down. Let's fall through. */
1779 input->state = INPUT_ST_FALLING;
1780 input->fall_timer = 0;
1781 }
1782 return 0;
1783}
1784
1785static inline void input_state_falling(struct logical_input *input)
1786{
1787#if 0
1788 /* FIXME !!! same comment as in input_state_high */
1789 if (((phys_prev & input->mask) == input->value)
1790 && ((phys_curr & input->mask) > input->value)) {
1791 input->state = INPUT_ST_LOW; /* invalidate */
1792 return;
1793 }
1794#endif
1795
1796 if ((phys_curr & input->mask) == input->value) {
1797 if (input->type == INPUT_TYPE_KBD) {
1798 /* will turn on the light */
1799 keypressed = 1;
1800
1801 if (input->u.kbd.repeat_str[0]) {
1802 char *repeat_str = input->u.kbd.repeat_str;
1803 if (input->high_timer >= KEYPAD_REP_START)
1804 input->high_timer -= KEYPAD_REP_DELAY;
1805 keypad_send_key(repeat_str,
1806 sizeof(repeat_str));
1807 /* we will need to come back here soon */
1808 inputs_stable = 0;
1809 }
1810
1811 if (input->high_timer < 255)
1812 input->high_timer++;
1813 }
1814 input->state = INPUT_ST_HIGH;
1815 } else if (input->fall_timer >= input->fall_time) {
1816 /* call release event */
1817 if (input->type == INPUT_TYPE_STD) {
1818 void (*release_fct)(int) = input->u.std.release_fct;
1819 if (release_fct != NULL)
1820 release_fct(input->u.std.release_data);
1821 } else if (input->type == INPUT_TYPE_KBD) {
1822 char *release_str = input->u.kbd.release_str;
1823 if (release_str[0])
1824 keypad_send_key(release_str,
1825 sizeof(release_str));
1826 }
1827
1828 input->state = INPUT_ST_LOW;
1829 } else {
1830 input->fall_timer++;
1831 inputs_stable = 0;
1832 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001833}
1834
Willy Tarreau698b1512008-11-22 11:29:33 +01001835static void panel_process_inputs(void)
1836{
1837 struct list_head *item;
1838 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001839
1840#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001841 printk(KERN_DEBUG
1842 "entering panel_process_inputs with pp=%016Lx & pc=%016Lx\n",
1843 phys_prev, phys_curr);
Willy Tarreau7005b582008-11-13 17:18:59 -08001844#endif
1845
Willy Tarreau698b1512008-11-22 11:29:33 +01001846 keypressed = 0;
1847 inputs_stable = 1;
1848 list_for_each(item, &logical_inputs) {
1849 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001850
Willy Tarreau698b1512008-11-22 11:29:33 +01001851 switch (input->state) {
1852 case INPUT_ST_LOW:
1853 if ((phys_curr & input->mask) != input->value)
1854 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001855 /* if all needed ones were already set previously,
1856 * this means that this logical signal has been
1857 * activated by the releasing of another combined
1858 * signal, so we don't want to match.
1859 * eg: AB -(release B)-> A -(release A)-> 0 :
1860 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001861 */
1862 if ((phys_prev & input->mask) == input->value)
1863 break;
1864 input->rise_timer = 0;
1865 input->state = INPUT_ST_RISING;
1866 /* no break here, fall through */
1867 case INPUT_ST_RISING:
1868 if ((phys_curr & input->mask) != input->value) {
1869 input->state = INPUT_ST_LOW;
1870 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001871 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001872 if (input->rise_timer < input->rise_time) {
1873 inputs_stable = 0;
1874 input->rise_timer++;
1875 break;
1876 }
1877 input->high_timer = 0;
1878 input->state = INPUT_ST_HIGH;
1879 /* no break here, fall through */
1880 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001881 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001882 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001883 /* no break here, fall through */
1884 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001885 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001886 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001887 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001888}
1889
Willy Tarreau698b1512008-11-22 11:29:33 +01001890static void panel_scan_timer(void)
1891{
Willy Tarreau630231772008-11-22 12:52:18 +01001892 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001893 if (spin_trylock(&pprt_lock)) {
1894 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001895
1896 /* no need for the parport anymore */
1897 spin_unlock(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001898 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001899
Willy Tarreau698b1512008-11-22 11:29:33 +01001900 if (!inputs_stable || phys_curr != phys_prev)
1901 panel_process_inputs();
1902 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001903
Willy Tarreau698b1512008-11-22 11:29:33 +01001904 if (lcd_enabled && lcd_initialized) {
1905 if (keypressed) {
1906 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1907 lcd_backlight(1);
1908 light_tempo = FLASH_LIGHT_TEMPO;
1909 } else if (light_tempo > 0) {
1910 light_tempo--;
1911 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1912 lcd_backlight(0);
1913 }
1914 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001915
Willy Tarreau698b1512008-11-22 11:29:33 +01001916 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001917}
1918
Willy Tarreau698b1512008-11-22 11:29:33 +01001919static void init_scan_timer(void)
1920{
1921 if (scan_timer.function != NULL)
1922 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001923
Willy Tarreau698b1512008-11-22 11:29:33 +01001924 init_timer(&scan_timer);
1925 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1926 scan_timer.data = 0;
1927 scan_timer.function = (void *)&panel_scan_timer;
1928 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001929}
1930
1931/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001932 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1933 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08001934 * returns 1 if ok, 0 if error (in which case, nothing is written).
1935 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001936static int input_name2mask(char *name, pmask_t *mask, pmask_t *value,
1937 char *imask, char *omask)
1938{
1939 static char sigtab[10] = "EeSsPpAaBb";
1940 char im, om;
1941 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08001942
Willy Tarreau698b1512008-11-22 11:29:33 +01001943 om = im = m = v = 0ULL;
1944 while (*name) {
1945 int in, out, bit, neg;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001946 for (in = 0; (in < sizeof(sigtab)) &&
1947 (sigtab[in] != *name); in++)
Willy Tarreau698b1512008-11-22 11:29:33 +01001948 ;
1949 if (in >= sizeof(sigtab))
1950 return 0; /* input name not found */
1951 neg = (in & 1); /* odd (lower) names are negated */
1952 in >>= 1;
1953 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08001954
Willy Tarreau698b1512008-11-22 11:29:33 +01001955 name++;
1956 if (isdigit(*name)) {
1957 out = *name - '0';
1958 om |= (1 << out);
1959 } else if (*name == '-')
1960 out = 8;
1961 else
1962 return 0; /* unknown bit name */
1963
1964 bit = (out * 5) + in;
1965
1966 m |= 1ULL << bit;
1967 if (!neg)
1968 v |= 1ULL << bit;
1969 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08001970 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001971 *mask = m;
1972 *value = v;
1973 if (imask)
1974 *imask |= im;
1975 if (omask)
1976 *omask |= om;
1977 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001978}
1979
1980/* tries to bind a key to the signal name <name>. The key will send the
1981 * strings <press>, <repeat>, <release> for these respective events.
1982 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1983 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001984static struct logical_input *panel_bind_key(char *name, char *press,
1985 char *repeat, char *release)
1986{
1987 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001988
Julia Lawall7a6cb0d2010-05-13 22:00:05 +02001989 key = kzalloc(sizeof(struct logical_input), GFP_KERNEL);
Willy Tarreau698b1512008-11-22 11:29:33 +01001990 if (!key) {
1991 printk(KERN_ERR "panel: not enough memory\n");
1992 return NULL;
1993 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001994 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04001995 &scan_mask_o)) {
1996 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01001997 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04001998 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001999
2000 key->type = INPUT_TYPE_KBD;
2001 key->state = INPUT_ST_LOW;
2002 key->rise_time = 1;
2003 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002004
2005#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01002006 printk(KERN_DEBUG "bind: <%s> : m=%016Lx v=%016Lx\n", name, key->mask,
2007 key->value);
Willy Tarreau7005b582008-11-13 17:18:59 -08002008#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01002009 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2010 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2011 strncpy(key->u.kbd.release_str, release,
2012 sizeof(key->u.kbd.release_str));
2013 list_add(&key->list, &logical_inputs);
2014 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002015}
2016
Willy Tarreau630231772008-11-22 12:52:18 +01002017#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002018/* tries to bind a callback function to the signal name <name>. The function
2019 * <press_fct> will be called with the <press_data> arg when the signal is
2020 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002021 * Returns the pointer to the new signal if ok, NULL if the signal could not
2022 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002023 */
2024static struct logical_input *panel_bind_callback(char *name,
Willy Tarreau698b1512008-11-22 11:29:33 +01002025 void (*press_fct) (int),
2026 int press_data,
2027 void (*release_fct) (int),
2028 int release_data)
2029{
2030 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002031
Willy Tarreau698b1512008-11-22 11:29:33 +01002032 callback = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
2033 if (!callback) {
2034 printk(KERN_ERR "panel: not enough memory\n");
2035 return NULL;
2036 }
2037 memset(callback, 0, sizeof(struct logical_input));
2038 if (!input_name2mask(name, &callback->mask, &callback->value,
2039 &scan_mask_i, &scan_mask_o))
2040 return NULL;
2041
2042 callback->type = INPUT_TYPE_STD;
2043 callback->state = INPUT_ST_LOW;
2044 callback->rise_time = 1;
2045 callback->fall_time = 1;
2046 callback->u.std.press_fct = press_fct;
2047 callback->u.std.press_data = press_data;
2048 callback->u.std.release_fct = release_fct;
2049 callback->u.std.release_data = release_data;
2050 list_add(&callback->list, &logical_inputs);
2051 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002052}
Willy Tarreau630231772008-11-22 12:52:18 +01002053#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002054
Willy Tarreau698b1512008-11-22 11:29:33 +01002055static void keypad_init(void)
2056{
2057 int keynum;
2058 init_waitqueue_head(&keypad_read_wait);
2059 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002060
Willy Tarreau698b1512008-11-22 11:29:33 +01002061 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002062
Willy Tarreau698b1512008-11-22 11:29:33 +01002063 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2064 panel_bind_key(keypad_profile[keynum][0],
2065 keypad_profile[keynum][1],
2066 keypad_profile[keynum][2],
2067 keypad_profile[keynum][3]);
2068 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002069
Willy Tarreau698b1512008-11-22 11:29:33 +01002070 init_scan_timer();
2071 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002072}
2073
Willy Tarreau7005b582008-11-13 17:18:59 -08002074/**************************************************/
2075/* device initialization */
2076/**************************************************/
2077
Willy Tarreau698b1512008-11-22 11:29:33 +01002078static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2079 void *unused)
2080{
2081 if (lcd_enabled && lcd_initialized) {
2082 switch (code) {
2083 case SYS_DOWN:
2084 panel_lcd_print
2085 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2086 break;
2087 case SYS_HALT:
2088 panel_lcd_print
2089 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2090 break;
2091 case SYS_POWER_OFF:
2092 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2093 break;
2094 default:
2095 break;
2096 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002097 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002098 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002099}
2100
2101static struct notifier_block panel_notifier = {
2102 panel_notify_sys,
2103 NULL,
2104 0
2105};
2106
Willy Tarreau698b1512008-11-22 11:29:33 +01002107static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002108{
Willy Tarreau698b1512008-11-22 11:29:33 +01002109 if (port->number != parport)
2110 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002111
Willy Tarreau698b1512008-11-22 11:29:33 +01002112 if (pprt) {
2113 printk(KERN_ERR
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002114 "panel_attach(): port->number=%d parport=%d, "
2115 "already registered !\n",
Willy Tarreau698b1512008-11-22 11:29:33 +01002116 port->number, parport);
2117 return;
2118 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002119
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002120 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
Willy Tarreau698b1512008-11-22 11:29:33 +01002121 NULL,
2122 /*PARPORT_DEV_EXCL */
2123 0, (void *)&pprt);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002124 if (pprt == NULL) {
2125 pr_err("panel_attach(): port->number=%d parport=%d, "
2126 "parport_register_device() failed\n",
2127 port->number, parport);
2128 return;
2129 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002130
Willy Tarreau698b1512008-11-22 11:29:33 +01002131 if (parport_claim(pprt)) {
2132 printk(KERN_ERR
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002133 "Panel: could not claim access to parport%d. "
2134 "Aborting.\n", parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002135 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002136 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002137
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002138 /* must init LCD first, just in case an IRQ from the keypad is
2139 * generated at keypad init
2140 */
Willy Tarreau698b1512008-11-22 11:29:33 +01002141 if (lcd_enabled) {
2142 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002143 if (misc_register(&lcd_dev))
2144 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002145 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002146
Willy Tarreau698b1512008-11-22 11:29:33 +01002147 if (keypad_enabled) {
2148 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002149 if (misc_register(&keypad_dev))
2150 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002151 }
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002152 return;
2153
2154err_lcd_unreg:
2155 if (lcd_enabled)
2156 misc_deregister(&lcd_dev);
2157err_unreg_device:
2158 parport_unregister_device(pprt);
2159 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002160}
2161
Willy Tarreau698b1512008-11-22 11:29:33 +01002162static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002163{
Willy Tarreau698b1512008-11-22 11:29:33 +01002164 if (port->number != parport)
2165 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002166
Willy Tarreau698b1512008-11-22 11:29:33 +01002167 if (!pprt) {
2168 printk(KERN_ERR
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002169 "panel_detach(): port->number=%d parport=%d, "
2170 "nothing to unregister.\n",
Willy Tarreau698b1512008-11-22 11:29:33 +01002171 port->number, parport);
2172 return;
2173 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002174
Peter Huewe0b0595b2009-09-29 01:22:40 +02002175 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002176 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002177 keypad_initialized = 0;
2178 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002179
Peter Huewe0b0595b2009-09-29 01:22:40 +02002180 if (lcd_enabled && lcd_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002181 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002182 lcd_initialized = 0;
2183 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002184
Willy Tarreau698b1512008-11-22 11:29:33 +01002185 parport_release(pprt);
2186 parport_unregister_device(pprt);
2187 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002188}
2189
2190static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002191 .name = "panel",
2192 .attach = panel_attach,
2193 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002194};
2195
2196/* init function */
Willy Tarreau698b1512008-11-22 11:29:33 +01002197int panel_init(void)
2198{
2199 /* for backwards compatibility */
2200 if (keypad_type < 0)
2201 keypad_type = keypad_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002202
Willy Tarreau698b1512008-11-22 11:29:33 +01002203 if (lcd_type < 0)
2204 lcd_type = lcd_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002205
Willy Tarreau698b1512008-11-22 11:29:33 +01002206 if (parport < 0)
2207 parport = DEFAULT_PARPORT;
Willy Tarreau7005b582008-11-13 17:18:59 -08002208
Willy Tarreau698b1512008-11-22 11:29:33 +01002209 /* take care of an eventual profile */
2210 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002211 case PANEL_PROFILE_CUSTOM:
2212 /* custom profile */
Willy Tarreau698b1512008-11-22 11:29:33 +01002213 if (keypad_type < 0)
2214 keypad_type = DEFAULT_KEYPAD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002215 if (lcd_type < 0)
2216 lcd_type = DEFAULT_LCD;
2217 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002218 case PANEL_PROFILE_OLD:
2219 /* 8 bits, 2*16, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002220 if (keypad_type < 0)
2221 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002222 if (lcd_type < 0)
2223 lcd_type = LCD_TYPE_OLD;
2224 if (lcd_width < 0)
2225 lcd_width = 16;
2226 if (lcd_hwidth < 0)
2227 lcd_hwidth = 16;
2228 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002229 case PANEL_PROFILE_NEW:
2230 /* serial, 2*16, new keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002231 if (keypad_type < 0)
2232 keypad_type = KEYPAD_TYPE_NEW;
Willy Tarreau698b1512008-11-22 11:29:33 +01002233 if (lcd_type < 0)
2234 lcd_type = LCD_TYPE_KS0074;
2235 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002236 case PANEL_PROFILE_HANTRONIX:
2237 /* 8 bits, 2*16 hantronix-like, no keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002238 if (keypad_type < 0)
2239 keypad_type = KEYPAD_TYPE_NONE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002240 if (lcd_type < 0)
2241 lcd_type = LCD_TYPE_HANTRONIX;
2242 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002243 case PANEL_PROFILE_NEXCOM:
2244 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Willy Tarreau698b1512008-11-22 11:29:33 +01002245 if (keypad_type < 0)
2246 keypad_type = KEYPAD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002247 if (lcd_type < 0)
2248 lcd_type = LCD_TYPE_NEXCOM;
2249 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002250 case PANEL_PROFILE_LARGE:
2251 /* 8 bits, 2*40, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002252 if (keypad_type < 0)
2253 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002254 if (lcd_type < 0)
2255 lcd_type = LCD_TYPE_OLD;
2256 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002257 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002258
Willy Tarreau698b1512008-11-22 11:29:33 +01002259 lcd_enabled = (lcd_type > 0);
2260 keypad_enabled = (keypad_type > 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08002261
Willy Tarreau698b1512008-11-22 11:29:33 +01002262 switch (keypad_type) {
2263 case KEYPAD_TYPE_OLD:
2264 keypad_profile = old_keypad_profile;
2265 break;
2266 case KEYPAD_TYPE_NEW:
2267 keypad_profile = new_keypad_profile;
2268 break;
2269 case KEYPAD_TYPE_NEXCOM:
2270 keypad_profile = nexcom_keypad_profile;
2271 break;
2272 default:
2273 keypad_profile = NULL;
2274 break;
2275 }
2276
2277 /* tells various subsystems about the fact that we are initializing */
2278 init_in_progress = 1;
2279
2280 if (parport_register_driver(&panel_driver)) {
2281 printk(KERN_ERR
2282 "Panel: could not register with parport. Aborting.\n");
2283 return -EIO;
2284 }
2285
Willy Tarreau630231772008-11-22 12:52:18 +01002286 if (!lcd_enabled && !keypad_enabled) {
2287 /* no device enabled, let's release the parport */
Willy Tarreau698b1512008-11-22 11:29:33 +01002288 if (pprt) {
2289 parport_release(pprt);
2290 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002291 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002292 }
2293 parport_unregister_driver(&panel_driver);
2294 printk(KERN_ERR "Panel driver version " PANEL_VERSION
2295 " disabled.\n");
2296 return -ENODEV;
2297 }
2298
2299 register_reboot_notifier(&panel_notifier);
2300
2301 if (pprt)
2302 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2303 " registered on parport%d (io=0x%lx).\n", parport,
2304 pprt->port->base);
2305 else
2306 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2307 " not yet registered\n");
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002308 /* tells various subsystems about the fact that initialization
2309 is finished */
Willy Tarreau698b1512008-11-22 11:29:33 +01002310 init_in_progress = 0;
2311 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002312}
2313
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002314static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002315{
2316 return panel_init();
Willy Tarreau7005b582008-11-13 17:18:59 -08002317}
2318
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002319static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002320{
2321 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002322
Willy Tarreau698b1512008-11-22 11:29:33 +01002323 if (scan_timer.function != NULL)
2324 del_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002325
Costantino Leandro57898132009-02-17 11:10:48 -05002326 if (pprt != NULL) {
Peter Huewe0b0595b2009-09-29 01:22:40 +02002327 if (keypad_enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002328 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002329 keypad_initialized = 0;
2330 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002331
Costantino Leandro57898132009-02-17 11:10:48 -05002332 if (lcd_enabled) {
2333 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2334 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2335 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002336 lcd_initialized = 0;
Costantino Leandro57898132009-02-17 11:10:48 -05002337 }
2338
2339 /* TODO: free all input signals */
2340 parport_release(pprt);
2341 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002342 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002343 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002344 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002345}
Willy Tarreau7005b582008-11-13 17:18:59 -08002346
Willy Tarreau7005b582008-11-13 17:18:59 -08002347module_init(panel_init_module);
2348module_exit(panel_cleanup_module);
2349MODULE_AUTHOR("Willy Tarreau");
2350MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002351
2352/*
2353 * Local variables:
2354 * c-indent-level: 4
2355 * tab-width: 8
2356 * End:
2357 */