blob: 6e9f7090c4512f6fc77dfdcd4e636ec7a7ff83f8 [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
Toshiaki Yamane493aa892012-07-12 22:01:00 +090037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Willy Tarreau7005b582008-11-13 17:18:59 -080039#include <linux/module.h>
40
41#include <linux/types.h>
42#include <linux/errno.h>
43#include <linux/signal.h>
44#include <linux/sched.h>
45#include <linux/spinlock.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080046#include <linux/interrupt.h>
47#include <linux/miscdevice.h>
Willy Tarreau698b1512008-11-22 11:29:33 +010048#include <linux/slab.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080049#include <linux/ioport.h>
50#include <linux/fcntl.h>
51#include <linux/init.h>
52#include <linux/delay.h>
Andy Shevchenkod85170e2010-07-22 19:57:08 +030053#include <linux/kernel.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080054#include <linux/ctype.h>
55#include <linux/parport.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080056#include <linux/list.h>
57#include <linux/notifier.h>
58#include <linux/reboot.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020059#include <generated/utsrelease.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080060
Willy Tarreau698b1512008-11-22 11:29:33 +010061#include <linux/io.h>
Takanori Suzuki48f658b2010-05-08 22:56:24 +090062#include <linux/uaccess.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080063
Willy Tarreau7005b582008-11-13 17:18:59 -080064#define LCD_MINOR 156
65#define KEYPAD_MINOR 185
Willy Tarreau7005b582008-11-13 17:18:59 -080066
67#define PANEL_VERSION "0.9.5"
68
69#define LCD_MAXBYTES 256 /* max burst write */
70
Willy Tarreau7005b582008-11-13 17:18:59 -080071#define KEYPAD_BUFFER 64
Willy Tarreau7005b582008-11-13 17:18:59 -080072
Henri Häkkinen429ccf02010-06-12 00:27:36 +030073/* poll the keyboard this every second */
74#define INPUT_POLL_TIME (HZ/50)
75/* a key starts to repeat after this times INPUT_POLL_TIME */
76#define KEYPAD_REP_START (10)
77/* a key repeats this times INPUT_POLL_TIME */
78#define KEYPAD_REP_DELAY (2)
79
80/* keep the light on this times INPUT_POLL_TIME for each flash */
81#define FLASH_LIGHT_TEMPO (200)
Willy Tarreau7005b582008-11-13 17:18:59 -080082
83/* converts an r_str() input to an active high, bits string : 000BAOSE */
84#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
85
Willy Tarreau698b1512008-11-22 11:29:33 +010086#define PNL_PBUSY 0x80 /* inverted input, active low */
87#define PNL_PACK 0x40 /* direct input, active low */
88#define PNL_POUTPA 0x20 /* direct input, active high */
89#define PNL_PSELECD 0x10 /* direct input, active high */
90#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080091
Willy Tarreau698b1512008-11-22 11:29:33 +010092#define PNL_PBIDIR 0x20 /* bi-directional ports */
Henri Häkkinen429ccf02010-06-12 00:27:36 +030093/* high to read data in or-ed with data out */
94#define PNL_PINTEN 0x10
Willy Tarreau698b1512008-11-22 11:29:33 +010095#define PNL_PSELECP 0x08 /* inverted output, active low */
96#define PNL_PINITP 0x04 /* direct output, active low */
97#define PNL_PAUTOLF 0x02 /* inverted output, active low */
98#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080099
100#define PNL_PD0 0x01
101#define PNL_PD1 0x02
102#define PNL_PD2 0x04
103#define PNL_PD3 0x08
104#define PNL_PD4 0x10
105#define PNL_PD5 0x20
106#define PNL_PD6 0x40
107#define PNL_PD7 0x80
108
109#define PIN_NONE 0
110#define PIN_STROBE 1
111#define PIN_D0 2
112#define PIN_D1 3
113#define PIN_D2 4
114#define PIN_D3 5
115#define PIN_D4 6
116#define PIN_D5 7
117#define PIN_D6 8
118#define PIN_D7 9
119#define PIN_AUTOLF 14
120#define PIN_INITP 16
121#define PIN_SELECP 17
122#define PIN_NOT_SET 127
123
Willy Tarreau7005b582008-11-13 17:18:59 -0800124#define LCD_FLAG_S 0x0001
125#define LCD_FLAG_ID 0x0002
126#define LCD_FLAG_B 0x0004 /* blink on */
127#define LCD_FLAG_C 0x0008 /* cursor on */
128#define LCD_FLAG_D 0x0010 /* display on */
129#define LCD_FLAG_F 0x0020 /* large font mode */
130#define LCD_FLAG_N 0x0040 /* 2-rows mode */
131#define LCD_FLAG_L 0x0080 /* backlight enabled */
132
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300133#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
Willy Tarreau7005b582008-11-13 17:18:59 -0800134#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
135
136/* macros to simplify use of the parallel port */
137#define r_ctr(x) (parport_read_control((x)->port))
138#define r_dtr(x) (parport_read_data((x)->port))
139#define r_str(x) (parport_read_status((x)->port))
Toshiaki Yamane6ebb56d2012-07-25 12:18:12 +0900140#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
141#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
Willy Tarreau7005b582008-11-13 17:18:59 -0800142
143/* this defines which bits are to be used and which ones to be ignored */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300144/* logical or of the output bits involved in the scan matrix */
145static __u8 scan_mask_o;
146/* logical or of the input bits involved in the scan matrix */
147static __u8 scan_mask_i;
Willy Tarreau7005b582008-11-13 17:18:59 -0800148
Willy Tarreau698b1512008-11-22 11:29:33 +0100149typedef __u64 pmask_t;
Willy Tarreau7005b582008-11-13 17:18:59 -0800150
151enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100152 INPUT_TYPE_STD,
153 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800154};
155
156enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100157 INPUT_ST_LOW,
158 INPUT_ST_RISING,
159 INPUT_ST_HIGH,
160 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800161};
162
163struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100164 struct list_head list;
165 pmask_t mask;
166 pmask_t value;
167 enum input_type type;
168 enum input_state state;
169 __u8 rise_time, fall_time;
170 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800171
Willy Tarreau698b1512008-11-22 11:29:33 +0100172 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300173 struct { /* valid when type == INPUT_TYPE_STD */
Willy Tarreau698b1512008-11-22 11:29:33 +0100174 void (*press_fct) (int);
175 void (*release_fct) (int);
176 int press_data;
177 int release_data;
178 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300179 struct { /* valid when type == INPUT_TYPE_KBD */
180 /* strings can be non null-terminated */
Willy Tarreau698b1512008-11-22 11:29:33 +0100181 char press_str[sizeof(void *) + sizeof(int)];
182 char repeat_str[sizeof(void *) + sizeof(int)];
183 char release_str[sizeof(void *) + sizeof(int)];
184 } kbd;
185 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800186};
187
Willy Tarreau698b1512008-11-22 11:29:33 +0100188LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800189
190/* physical contacts history
191 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
192 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
193 * corresponds to the ground.
194 * Within each group, bits are stored in the same order as read on the port :
195 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
196 * So, each __u64 (or pmask_t) is represented like this :
197 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
198 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
199 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300200
201/* what has just been read from the I/O ports */
202static pmask_t phys_read;
203/* previous phys_read */
204static pmask_t phys_read_prev;
205/* stabilized phys_read (phys_read|phys_read_prev) */
206static pmask_t phys_curr;
207/* previous phys_curr */
208static pmask_t phys_prev;
209/* 0 means that at least one logical signal needs be computed */
210static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800211
Willy Tarreau7005b582008-11-13 17:18:59 -0800212/* these variables are specific to the keypad */
213static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100214static int keypad_buflen;
215static int keypad_start;
216static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800217static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800218
219/* lcd-specific variables */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300220
221/* contains the LCD config state */
222static unsigned long int lcd_flags;
223/* contains the LCD X offset */
224static unsigned long int lcd_addr_x;
225/* contains the LCD Y offset */
226static unsigned long int lcd_addr_y;
227/* current escape sequence, 0 terminated */
228static char lcd_escape[LCD_ESCAPE_LEN + 1];
229/* not in escape state. >=0 = escape cmd len */
230static int lcd_escape_len = -1;
Willy Tarreau7005b582008-11-13 17:18:59 -0800231
Willy Tarreau7005b582008-11-13 17:18:59 -0800232/*
233 * Bit masks to convert LCD signals to parallel port outputs.
234 * _d_ are values for data port, _c_ are for control port.
235 * [0] = signal OFF, [1] = signal ON, [2] = mask
236 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100237#define BIT_CLR 0
238#define BIT_SET 1
239#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800240#define BIT_STATES 3
241/*
242 * one entry for each bit on the LCD
243 */
244#define LCD_BIT_E 0
245#define LCD_BIT_RS 1
246#define LCD_BIT_RW 2
247#define LCD_BIT_BL 3
248#define LCD_BIT_CL 4
249#define LCD_BIT_DA 5
250#define LCD_BITS 6
251
252/*
253 * each bit can be either connected to a DATA or CTRL port
254 */
255#define LCD_PORT_C 0
256#define LCD_PORT_D 1
257#define LCD_PORTS 2
258
259static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
260
261/*
262 * LCD protocols
263 */
264#define LCD_PROTO_PARALLEL 0
265#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400266#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800267
268/*
269 * LCD character sets
270 */
271#define LCD_CHARSET_NORMAL 0
272#define LCD_CHARSET_KS0074 1
273
274/*
275 * LCD types
276 */
277#define LCD_TYPE_NONE 0
278#define LCD_TYPE_OLD 1
279#define LCD_TYPE_KS0074 2
280#define LCD_TYPE_HANTRONIX 3
281#define LCD_TYPE_NEXCOM 4
282#define LCD_TYPE_CUSTOM 5
283
284/*
285 * keypad types
286 */
287#define KEYPAD_TYPE_NONE 0
288#define KEYPAD_TYPE_OLD 1
289#define KEYPAD_TYPE_NEW 2
290#define KEYPAD_TYPE_NEXCOM 3
291
292/*
293 * panel profiles
294 */
295#define PANEL_PROFILE_CUSTOM 0
296#define PANEL_PROFILE_OLD 1
297#define PANEL_PROFILE_NEW 2
298#define PANEL_PROFILE_HANTRONIX 3
299#define PANEL_PROFILE_NEXCOM 4
300#define PANEL_PROFILE_LARGE 5
301
302/*
303 * Construct custom config from the kernel's configuration
304 */
305#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
306#define DEFAULT_PARPORT 0
307#define DEFAULT_LCD LCD_TYPE_OLD
308#define DEFAULT_KEYPAD KEYPAD_TYPE_OLD
Willy Tarreau7005b582008-11-13 17:18:59 -0800309#define DEFAULT_LCD_WIDTH 40
310#define DEFAULT_LCD_BWIDTH 40
311#define DEFAULT_LCD_HWIDTH 64
312#define DEFAULT_LCD_HEIGHT 2
313#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
314
315#define DEFAULT_LCD_PIN_E PIN_AUTOLF
316#define DEFAULT_LCD_PIN_RS PIN_SELECP
317#define DEFAULT_LCD_PIN_RW PIN_INITP
318#define DEFAULT_LCD_PIN_SCL PIN_STROBE
319#define DEFAULT_LCD_PIN_SDA PIN_D0
320#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
321#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
322
323#ifdef CONFIG_PANEL_PROFILE
324#undef DEFAULT_PROFILE
325#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
326#endif
327
328#ifdef CONFIG_PANEL_PARPORT
329#undef DEFAULT_PARPORT
330#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
331#endif
332
Willy Tarreau698b1512008-11-22 11:29:33 +0100333#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800334#ifdef CONFIG_PANEL_KEYPAD
335#undef DEFAULT_KEYPAD
336#define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
337#endif
338
Willy Tarreau7005b582008-11-13 17:18:59 -0800339#ifdef CONFIG_PANEL_LCD
340#undef DEFAULT_LCD
341#define DEFAULT_LCD CONFIG_PANEL_LCD
342#endif
343
344#ifdef CONFIG_PANEL_LCD_WIDTH
345#undef DEFAULT_LCD_WIDTH
346#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
347#endif
348
349#ifdef CONFIG_PANEL_LCD_BWIDTH
350#undef DEFAULT_LCD_BWIDTH
351#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
352#endif
353
354#ifdef CONFIG_PANEL_LCD_HWIDTH
355#undef DEFAULT_LCD_HWIDTH
356#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
357#endif
358
359#ifdef CONFIG_PANEL_LCD_HEIGHT
360#undef DEFAULT_LCD_HEIGHT
361#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
362#endif
363
364#ifdef CONFIG_PANEL_LCD_PROTO
365#undef DEFAULT_LCD_PROTO
366#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
367#endif
368
369#ifdef CONFIG_PANEL_LCD_PIN_E
370#undef DEFAULT_LCD_PIN_E
371#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
372#endif
373
374#ifdef CONFIG_PANEL_LCD_PIN_RS
375#undef DEFAULT_LCD_PIN_RS
376#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
377#endif
378
379#ifdef CONFIG_PANEL_LCD_PIN_RW
380#undef DEFAULT_LCD_PIN_RW
381#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
382#endif
383
384#ifdef CONFIG_PANEL_LCD_PIN_SCL
385#undef DEFAULT_LCD_PIN_SCL
386#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
387#endif
388
389#ifdef CONFIG_PANEL_LCD_PIN_SDA
390#undef DEFAULT_LCD_PIN_SDA
391#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
392#endif
393
394#ifdef CONFIG_PANEL_LCD_PIN_BL
395#undef DEFAULT_LCD_PIN_BL
396#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
397#endif
398
399#ifdef CONFIG_PANEL_LCD_CHARSET
400#undef DEFAULT_LCD_CHARSET
Peter Huewea6a6c902009-12-15 06:21:45 +0100401#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800402#endif
403
404#endif /* DEFAULT_PROFILE == 0 */
405
406/* global variables */
Willy Tarreau698b1512008-11-22 11:29:33 +0100407static int keypad_open_cnt; /* #times opened */
408static int lcd_open_cnt; /* #times opened */
Willy Tarreau698b1512008-11-22 11:29:33 +0100409static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800410
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100411static int lcd_initialized;
412static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800413
Willy Tarreau698b1512008-11-22 11:29:33 +0100414static int light_tempo;
Willy Tarreau7005b582008-11-13 17:18:59 -0800415
Willy Tarreau698b1512008-11-22 11:29:33 +0100416static char lcd_must_clear;
417static char lcd_left_shift;
418static char init_in_progress;
Willy Tarreau7005b582008-11-13 17:18:59 -0800419
Willy Tarreau698b1512008-11-22 11:29:33 +0100420static void (*lcd_write_cmd) (int);
421static void (*lcd_write_data) (int);
422static void (*lcd_clear_fast) (void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800423
Willy Tarreau698b1512008-11-22 11:29:33 +0100424static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800425static struct timer_list scan_timer;
426
Willy Tarreau630231772008-11-22 12:52:18 +0100427MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100428
429static int parport = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100430module_param(parport, int, 0000);
431MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100432
433static int lcd_height = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100434module_param(lcd_height, int, 0000);
435MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100436
437static int lcd_width = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100438module_param(lcd_width, int, 0000);
439MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100440
441static int lcd_bwidth = -1; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100442module_param(lcd_bwidth, int, 0000);
443MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100444
445static int lcd_hwidth = -1; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100446module_param(lcd_hwidth, int, 0000);
447MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100448
449static int lcd_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100450module_param(lcd_enabled, int, 0000);
451MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100452
453static int keypad_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100454module_param(keypad_enabled, int, 0000);
455MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100456
457static int lcd_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100458module_param(lcd_type, int, 0000);
459MODULE_PARM_DESC(lcd_type,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300460 "LCD type: 0=none, 1=old //, 2=serial ks0074, "
461 "3=hantronix //, 4=nexcom //, 5=compiled-in");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100462
463static int lcd_proto = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100464module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300465MODULE_PARM_DESC(lcd_proto,
466 "LCD communication: 0=parallel (//), 1=serial,"
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400467 "2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100468
469static int lcd_charset = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100470module_param(lcd_charset, int, 0000);
471MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100472
473static int keypad_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100474module_param(keypad_type, int, 0000);
475MODULE_PARM_DESC(keypad_type,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300476 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, "
477 "3=nexcom 4 keys");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100478
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100479static int profile = DEFAULT_PROFILE;
Willy Tarreau698b1512008-11-22 11:29:33 +0100480module_param(profile, int, 0000);
481MODULE_PARM_DESC(profile,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300482 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
483 "4=16x2 nexcom; default=40x2, old kp");
Willy Tarreau7005b582008-11-13 17:18:59 -0800484
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100485/*
486 * These are the parallel port pins the LCD control signals are connected to.
487 * Set this to 0 if the signal is not used. Set it to its opposite value
488 * (negative) if the signal is negated. -MAXINT is used to indicate that the
489 * pin has not been explicitly specified.
490 *
Willy Tarreau630231772008-11-22 12:52:18 +0100491 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100492 */
493
494static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100495module_param(lcd_e_pin, int, 0000);
496MODULE_PARM_DESC(lcd_e_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300497 "# of the // port pin connected to LCD 'E' signal, "
498 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100499
500static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100501module_param(lcd_rs_pin, int, 0000);
502MODULE_PARM_DESC(lcd_rs_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300503 "# of the // port pin connected to LCD 'RS' signal, "
504 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100505
506static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100507module_param(lcd_rw_pin, int, 0000);
508MODULE_PARM_DESC(lcd_rw_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300509 "# of the // port pin connected to LCD 'RW' signal, "
510 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100511
512static int lcd_bl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100513module_param(lcd_bl_pin, int, 0000);
514MODULE_PARM_DESC(lcd_bl_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300515 "# of the // port pin connected to LCD backlight, "
516 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100517
518static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100519module_param(lcd_da_pin, int, 0000);
520MODULE_PARM_DESC(lcd_da_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300521 "# of the // port pin connected to serial LCD 'SDA' "
522 "signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100523
524static int lcd_cl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100525module_param(lcd_cl_pin, int, 0000);
526MODULE_PARM_DESC(lcd_cl_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300527 "# of the // port pin connected to serial LCD 'SCL' "
528 "signal, with polarity (-17..17)");
Willy Tarreau7005b582008-11-13 17:18:59 -0800529
Willy Tarreau698b1512008-11-22 11:29:33 +0100530static unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800531
532/* for some LCD drivers (ks0074) we need a charset conversion table. */
533static unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100534 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
535 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
536 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
537 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
538 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
539 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
540 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
541 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
542 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
543 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
544 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
545 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
546 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
547 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
548 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
549 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
550 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
551 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
552 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
553 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
554 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
555 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
556 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
557 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
558 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
559 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
560 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
561 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
562 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
563 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
564 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
565 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
566 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800567};
568
569char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100570 {"S0", "Left\n", "Left\n", ""},
571 {"S1", "Down\n", "Down\n", ""},
572 {"S2", "Up\n", "Up\n", ""},
573 {"S3", "Right\n", "Right\n", ""},
574 {"S4", "Esc\n", "Esc\n", ""},
575 {"S5", "Ret\n", "Ret\n", ""},
576 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800577};
578
579/* signals, press, repeat, release */
580char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100581 {"S0", "Left\n", "Left\n", ""},
582 {"S1", "Down\n", "Down\n", ""},
583 {"S2", "Up\n", "Up\n", ""},
584 {"S3", "Right\n", "Right\n", ""},
585 {"S4s5", "", "Esc\n", "Esc\n"},
586 {"s4S5", "", "Ret\n", "Ret\n"},
587 {"S4S5", "Help\n", "", ""},
588 /* add new signals above this line */
589 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800590};
591
592/* signals, press, repeat, release */
593char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100594 {"a-p-e-", "Down\n", "Down\n", ""},
595 {"a-p-E-", "Ret\n", "Ret\n", ""},
596 {"a-P-E-", "Esc\n", "Esc\n", ""},
597 {"a-P-e-", "Up\n", "Up\n", ""},
598 /* add new signals above this line */
599 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800600};
601
602static char (*keypad_profile)[4][9] = old_keypad_profile;
603
604/* FIXME: this should be converted to a bit array containing signals states */
605static struct {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300606 unsigned char e; /* parallel LCD E (data latch on falling edge) */
607 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
608 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
609 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
610 unsigned char cl; /* serial LCD clock (latch on rising edge) */
611 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800612} bits;
613
614static void init_scan_timer(void);
615
616/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100617static int set_data_bits(void)
618{
619 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800620
Willy Tarreau698b1512008-11-22 11:29:33 +0100621 val = r_dtr(pprt);
622 for (bit = 0; bit < LCD_BITS; bit++)
623 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800624
Willy Tarreau698b1512008-11-22 11:29:33 +0100625 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
626 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
627 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
628 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
629 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
630 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800631
Willy Tarreau698b1512008-11-22 11:29:33 +0100632 w_dtr(pprt, val);
633 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800634}
635
636/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100637static int set_ctrl_bits(void)
638{
639 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800640
Willy Tarreau698b1512008-11-22 11:29:33 +0100641 val = r_ctr(pprt);
642 for (bit = 0; bit < LCD_BITS; bit++)
643 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800644
Willy Tarreau698b1512008-11-22 11:29:33 +0100645 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
646 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
647 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
648 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
649 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
650 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800651
Willy Tarreau698b1512008-11-22 11:29:33 +0100652 w_ctr(pprt, val);
653 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800654}
655
656/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530657static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100658{
659 set_data_bits();
660 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800661}
662
663/*
664 * Converts a parallel port pin (from -25 to 25) to data and control ports
665 * masks, and data and control port bits. The signal will be considered
666 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
667 *
668 * Result will be used this way :
669 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
670 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
671 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100672void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
673{
674 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800675
Willy Tarreau698b1512008-11-22 11:29:33 +0100676 d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
677 d_val[2] = c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800678
Willy Tarreau698b1512008-11-22 11:29:33 +0100679 if (pin == 0)
680 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800681
Willy Tarreau698b1512008-11-22 11:29:33 +0100682 inv = (pin < 0);
683 if (inv)
684 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800685
Willy Tarreau698b1512008-11-22 11:29:33 +0100686 d_bit = c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800687
Willy Tarreau698b1512008-11-22 11:29:33 +0100688 switch (pin) {
689 case PIN_STROBE: /* strobe, inverted */
690 c_bit = PNL_PSTROBE;
691 inv = !inv;
692 break;
693 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
694 d_bit = 1 << (pin - 2);
695 break;
696 case PIN_AUTOLF: /* autofeed, inverted */
697 c_bit = PNL_PAUTOLF;
698 inv = !inv;
699 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300700 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100701 c_bit = PNL_PINITP;
702 break;
703 case PIN_SELECP: /* select_in, inverted */
704 c_bit = PNL_PSELECP;
705 inv = !inv;
706 break;
707 default: /* unknown pin, ignore */
708 break;
709 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800710
Willy Tarreau698b1512008-11-22 11:29:33 +0100711 if (c_bit) {
712 c_val[2] &= ~c_bit;
713 c_val[!inv] = c_bit;
714 } else if (d_bit) {
715 d_val[2] &= ~d_bit;
716 d_val[!inv] = d_bit;
717 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800718}
719
720/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100721static void long_sleep(int ms)
722{
Willy Tarreau7005b582008-11-13 17:18:59 -0800723
Willy Tarreau698b1512008-11-22 11:29:33 +0100724 if (in_interrupt())
725 mdelay(ms);
726 else {
727 current->state = TASK_INTERRUPTIBLE;
728 schedule_timeout((ms * HZ + 999) / 1000);
729 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800730}
731
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300732/* send a serial byte to the LCD panel. The caller is responsible for locking
733 if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100734static void lcd_send_serial(int byte)
735{
736 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800737
Willy Tarreau698b1512008-11-22 11:29:33 +0100738 /* the data bit is set on D0, and the clock on STROBE.
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300739 * LCD reads D0 on STROBE's rising edge. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100740 for (bit = 0; bit < 8; bit++) {
741 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530742 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100743 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530744 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300745 udelay(2); /* maintain the data during 2 us before CLK up */
Willy Tarreau698b1512008-11-22 11:29:33 +0100746 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530747 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300748 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100749 byte >>= 1;
750 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800751}
752
753/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100754static void lcd_backlight(int on)
755{
756 if (lcd_bl_pin == PIN_NONE)
757 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800758
Justin P. Mattock6975e182012-03-19 08:17:52 -0700759 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800760 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100761 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530762 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800763 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800764}
765
766/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100767static void lcd_write_cmd_s(int cmd)
768{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800769 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100770 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
771 lcd_send_serial(cmd & 0x0F);
772 lcd_send_serial((cmd >> 4) & 0x0F);
773 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800774 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800775}
776
777/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100778static void lcd_write_data_s(int data)
779{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800780 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100781 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
782 lcd_send_serial(data & 0x0F);
783 lcd_send_serial((data >> 4) & 0x0F);
784 udelay(40); /* the shortest data takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800785 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800786}
787
788/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100789static void lcd_write_cmd_p8(int cmd)
790{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800791 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800792 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100793 w_dtr(pprt, cmd);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300794 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800795
Willy Tarreau698b1512008-11-22 11:29:33 +0100796 bits.e = BIT_SET;
797 bits.rs = BIT_CLR;
798 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800799 set_ctrl_bits();
800
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300801 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800802
803 bits.e = BIT_CLR;
804 set_ctrl_bits();
805
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300806 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800807 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100808}
Willy Tarreau7005b582008-11-13 17:18:59 -0800809
Willy Tarreau698b1512008-11-22 11:29:33 +0100810/* send data to the LCD panel in 8 bits parallel mode */
811static void lcd_write_data_p8(int data)
812{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800813 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100814 /* present the data to the data port */
815 w_dtr(pprt, data);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300816 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100817
818 bits.e = BIT_SET;
819 bits.rs = BIT_SET;
820 bits.rw = BIT_CLR;
821 set_ctrl_bits();
822
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300823 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100824
825 bits.e = BIT_CLR;
826 set_ctrl_bits();
827
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300828 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800829 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100830}
831
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400832/* send a command to the TI LCD panel */
833static void lcd_write_cmd_tilcd(int cmd)
834{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800835 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400836 /* present the data to the control port */
837 w_ctr(pprt, cmd);
838 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800839 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400840}
841
842/* send data to the TI LCD panel */
843static void lcd_write_data_tilcd(int data)
844{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800845 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400846 /* present the data to the data port */
847 w_dtr(pprt, data);
848 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800849 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400850}
851
Willy Tarreau698b1512008-11-22 11:29:33 +0100852static void lcd_gotoxy(void)
853{
854 lcd_write_cmd(0x80 /* set DDRAM address */
855 | (lcd_addr_y ? lcd_hwidth : 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300856 /* we force the cursor to stay at the end of the
857 line if it wants to go farther */
Willy Tarreau698b1512008-11-22 11:29:33 +0100858 | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
859 (lcd_hwidth - 1) : lcd_bwidth - 1));
860}
861
862static void lcd_print(char c)
863{
864 if (lcd_addr_x < lcd_bwidth) {
865 if (lcd_char_conv != NULL)
866 c = lcd_char_conv[(unsigned char)c];
867 lcd_write_data(c);
868 lcd_addr_x++;
869 }
870 /* prevents the cursor from wrapping onto the next line */
871 if (lcd_addr_x == lcd_bwidth)
872 lcd_gotoxy();
873}
874
875/* fills the display with spaces and resets X/Y */
876static void lcd_clear_fast_s(void)
877{
878 int pos;
879 lcd_addr_x = lcd_addr_y = 0;
880 lcd_gotoxy();
881
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800882 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100883 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
884 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
885 lcd_send_serial(' ' & 0x0F);
886 lcd_send_serial((' ' >> 4) & 0x0F);
887 udelay(40); /* the shortest data takes at least 40 us */
888 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800889 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100890
891 lcd_addr_x = lcd_addr_y = 0;
892 lcd_gotoxy();
893}
894
895/* fills the display with spaces and resets X/Y */
896static void lcd_clear_fast_p8(void)
897{
898 int pos;
899 lcd_addr_x = lcd_addr_y = 0;
900 lcd_gotoxy();
901
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800902 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100903 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
904 /* present the data to the data port */
905 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300906
907 /* maintain the data during 20 us before the strobe */
908 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100909
910 bits.e = BIT_SET;
911 bits.rs = BIT_SET;
912 bits.rw = BIT_CLR;
913 set_ctrl_bits();
914
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300915 /* maintain the strobe during 40 us */
916 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100917
918 bits.e = BIT_CLR;
919 set_ctrl_bits();
920
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300921 /* the shortest data takes at least 45 us */
922 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100923 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800924 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100925
926 lcd_addr_x = lcd_addr_y = 0;
927 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800928}
929
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400930/* fills the display with spaces and resets X/Y */
931static void lcd_clear_fast_tilcd(void)
932{
933 int pos;
934 lcd_addr_x = lcd_addr_y = 0;
935 lcd_gotoxy();
936
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800937 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400938 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
939 /* present the data to the data port */
940 w_dtr(pprt, ' ');
941 udelay(60);
942 }
943
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800944 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400945
946 lcd_addr_x = lcd_addr_y = 0;
947 lcd_gotoxy();
948}
949
Willy Tarreau7005b582008-11-13 17:18:59 -0800950/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +0100951static void lcd_clear_display(void)
952{
953 lcd_write_cmd(0x01); /* clear display */
954 lcd_addr_x = lcd_addr_y = 0;
955 /* we must wait a few milliseconds (15) */
956 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -0800957}
958
Willy Tarreau698b1512008-11-22 11:29:33 +0100959static void lcd_init_display(void)
960{
Willy Tarreau7005b582008-11-13 17:18:59 -0800961
Willy Tarreau698b1512008-11-22 11:29:33 +0100962 lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
963 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -0800964
Willy Tarreau698b1512008-11-22 11:29:33 +0100965 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -0800966
Willy Tarreau698b1512008-11-22 11:29:33 +0100967 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);
971 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
972 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800973
Willy Tarreau698b1512008-11-22 11:29:33 +0100974 lcd_write_cmd(0x30 /* set font height and lines number */
975 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
976 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0)
977 );
978 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800979
Willy Tarreau698b1512008-11-22 11:29:33 +0100980 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
981 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800982
Willy Tarreau698b1512008-11-22 11:29:33 +0100983 lcd_write_cmd(0x08 /* set display mode */
984 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
985 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
986 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0)
987 );
Willy Tarreau7005b582008-11-13 17:18:59 -0800988
Willy Tarreau698b1512008-11-22 11:29:33 +0100989 lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -0800990
Willy Tarreau698b1512008-11-22 11:29:33 +0100991 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800992
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300993 /* entry mode set : increment, cursor shifting */
994 lcd_write_cmd(0x06);
Willy Tarreau7005b582008-11-13 17:18:59 -0800995
Willy Tarreau698b1512008-11-22 11:29:33 +0100996 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -0800997}
998
999/*
1000 * These are the file operation function for user access to /dev/lcd
1001 * This function can also be called from inside the kernel, by
1002 * setting file and ppos to NULL.
1003 *
1004 */
1005
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001006static inline int handle_lcd_special_code(void)
1007{
1008 /* LCD special codes */
1009
1010 int processed = 0;
1011
1012 char *esc = lcd_escape + 2;
1013 int oldflags = lcd_flags;
1014
1015 /* check for display mode flags */
1016 switch (*esc) {
1017 case 'D': /* Display ON */
1018 lcd_flags |= LCD_FLAG_D;
1019 processed = 1;
1020 break;
1021 case 'd': /* Display OFF */
1022 lcd_flags &= ~LCD_FLAG_D;
1023 processed = 1;
1024 break;
1025 case 'C': /* Cursor ON */
1026 lcd_flags |= LCD_FLAG_C;
1027 processed = 1;
1028 break;
1029 case 'c': /* Cursor OFF */
1030 lcd_flags &= ~LCD_FLAG_C;
1031 processed = 1;
1032 break;
1033 case 'B': /* Blink ON */
1034 lcd_flags |= LCD_FLAG_B;
1035 processed = 1;
1036 break;
1037 case 'b': /* Blink OFF */
1038 lcd_flags &= ~LCD_FLAG_B;
1039 processed = 1;
1040 break;
1041 case '+': /* Back light ON */
1042 lcd_flags |= LCD_FLAG_L;
1043 processed = 1;
1044 break;
1045 case '-': /* Back light OFF */
1046 lcd_flags &= ~LCD_FLAG_L;
1047 processed = 1;
1048 break;
1049 case '*':
1050 /* flash back light using the keypad timer */
1051 if (scan_timer.function != NULL) {
1052 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1053 lcd_backlight(1);
1054 light_tempo = FLASH_LIGHT_TEMPO;
1055 }
1056 processed = 1;
1057 break;
1058 case 'f': /* Small Font */
1059 lcd_flags &= ~LCD_FLAG_F;
1060 processed = 1;
1061 break;
1062 case 'F': /* Large Font */
1063 lcd_flags |= LCD_FLAG_F;
1064 processed = 1;
1065 break;
1066 case 'n': /* One Line */
1067 lcd_flags &= ~LCD_FLAG_N;
1068 processed = 1;
1069 break;
1070 case 'N': /* Two Lines */
1071 lcd_flags |= LCD_FLAG_N;
1072 break;
1073 case 'l': /* Shift Cursor Left */
1074 if (lcd_addr_x > 0) {
1075 /* back one char if not at end of line */
1076 if (lcd_addr_x < lcd_bwidth)
1077 lcd_write_cmd(0x10);
1078 lcd_addr_x--;
1079 }
1080 processed = 1;
1081 break;
1082 case 'r': /* shift cursor right */
1083 if (lcd_addr_x < lcd_width) {
1084 /* allow the cursor to pass the end of the line */
1085 if (lcd_addr_x <
1086 (lcd_bwidth - 1))
1087 lcd_write_cmd(0x14);
1088 lcd_addr_x++;
1089 }
1090 processed = 1;
1091 break;
1092 case 'L': /* shift display left */
1093 lcd_left_shift++;
1094 lcd_write_cmd(0x18);
1095 processed = 1;
1096 break;
1097 case 'R': /* shift display right */
1098 lcd_left_shift--;
1099 lcd_write_cmd(0x1C);
1100 processed = 1;
1101 break;
1102 case 'k': { /* kill end of line */
1103 int x;
1104 for (x = lcd_addr_x; x < lcd_bwidth; x++)
1105 lcd_write_data(' ');
1106
1107 /* restore cursor position */
1108 lcd_gotoxy();
1109 processed = 1;
1110 break;
1111 }
1112 case 'I': /* reinitialize display */
1113 lcd_init_display();
1114 lcd_left_shift = 0;
1115 processed = 1;
1116 break;
1117 case 'G': {
1118 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1119 * and '7', representing the numerical ASCII code of the
1120 * redefined character, and <xx...xx> a sequence of 16
1121 * hex digits representing 8 bytes for each character.
1122 * Most LCDs will only use 5 lower bits of the 7 first
1123 * bytes.
1124 */
1125
1126 unsigned char cgbytes[8];
1127 unsigned char cgaddr;
1128 int cgoffset;
1129 int shift;
1130 char value;
1131 int addr;
1132
1133 if (strchr(esc, ';') == NULL)
1134 break;
1135
1136 esc++;
1137
1138 cgaddr = *(esc++) - '0';
1139 if (cgaddr > 7) {
1140 processed = 1;
1141 break;
1142 }
1143
1144 cgoffset = 0;
1145 shift = 0;
1146 value = 0;
1147 while (*esc && cgoffset < 8) {
1148 shift ^= 4;
1149 if (*esc >= '0' && *esc <= '9')
1150 value |= (*esc - '0') << shift;
1151 else if (*esc >= 'A' && *esc <= 'Z')
1152 value |= (*esc - 'A' + 10) << shift;
1153 else if (*esc >= 'a' && *esc <= 'z')
1154 value |= (*esc - 'a' + 10) << shift;
1155 else {
1156 esc++;
1157 continue;
1158 }
1159
1160 if (shift == 0) {
1161 cgbytes[cgoffset++] = value;
1162 value = 0;
1163 }
1164
1165 esc++;
1166 }
1167
1168 lcd_write_cmd(0x40 | (cgaddr * 8));
1169 for (addr = 0; addr < cgoffset; addr++)
1170 lcd_write_data(cgbytes[addr]);
1171
1172 /* ensures that we stop writing to CGRAM */
1173 lcd_gotoxy();
1174 processed = 1;
1175 break;
1176 }
1177 case 'x': /* gotoxy : LxXXX[yYYY]; */
1178 case 'y': /* gotoxy : LyYYY[xXXX]; */
1179 if (strchr(esc, ';') == NULL)
1180 break;
1181
1182 while (*esc) {
1183 if (*esc == 'x') {
1184 esc++;
Pelle Windestam12995702011-08-30 20:29:24 +02001185 if (kstrtoul(esc, 10, &lcd_addr_x) < 0)
1186 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001187 } else if (*esc == 'y') {
1188 esc++;
Pelle Windestam12995702011-08-30 20:29:24 +02001189 if (kstrtoul(esc, 10, &lcd_addr_y) < 0)
1190 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001191 } else
1192 break;
1193 }
1194
1195 lcd_gotoxy();
1196 processed = 1;
1197 break;
1198 }
1199
Adam Buchbinderf2635892012-09-19 21:51:07 -04001200 /* Check whether one flag was changed */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001201 if (oldflags != lcd_flags) {
1202 /* check whether one of B,C,D flags were changed */
1203 if ((oldflags ^ lcd_flags) &
1204 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1205 /* set display mode */
1206 lcd_write_cmd(0x08
1207 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
1208 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
1209 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0));
1210 /* check whether one of F,N flags was changed */
1211 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_F | LCD_FLAG_N))
1212 lcd_write_cmd(0x30
1213 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
1214 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001215 /* check whether L flag was changed */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001216 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_L)) {
1217 if (lcd_flags & (LCD_FLAG_L))
1218 lcd_backlight(1);
1219 else if (light_tempo == 0)
1220 /* switch off the light only when the tempo
1221 lighting is gone */
1222 lcd_backlight(0);
1223 }
1224 }
1225
1226 return processed;
1227}
1228
Willy Tarreau698b1512008-11-22 11:29:33 +01001229static ssize_t lcd_write(struct file *file,
1230 const char *buf, size_t count, loff_t *ppos)
1231{
Willy Tarreau698b1512008-11-22 11:29:33 +01001232 const char *tmp = buf;
1233 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001234
Willy Tarreau698b1512008-11-22 11:29:33 +01001235 for (; count-- > 0; (ppos ? (*ppos)++ : 0), ++tmp) {
1236 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001237 /* let's be a little nice with other processes
1238 that need some CPU */
1239 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001240
Willy Tarreau698b1512008-11-22 11:29:33 +01001241 if (ppos == NULL && file == NULL)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001242 /* let's not use get_user() from the kernel ! */
1243 c = *tmp;
Willy Tarreau698b1512008-11-22 11:29:33 +01001244 else if (get_user(c, tmp))
1245 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001246
Willy Tarreau698b1512008-11-22 11:29:33 +01001247 /* first, we'll test if we're in escape mode */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001248 if ((c != '\n') && lcd_escape_len >= 0) {
1249 /* yes, let's add this char to the buffer */
Willy Tarreau698b1512008-11-22 11:29:33 +01001250 lcd_escape[lcd_escape_len++] = c;
1251 lcd_escape[lcd_escape_len] = 0;
1252 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001253 /* aborts any previous escape sequence */
1254 lcd_escape_len = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +01001255
1256 switch (c) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001257 case LCD_ESCAPE_CHAR:
1258 /* start of an escape sequence */
Willy Tarreau698b1512008-11-22 11:29:33 +01001259 lcd_escape_len = 0;
1260 lcd_escape[lcd_escape_len] = 0;
1261 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001262 case '\b':
1263 /* go back one char and clear it */
Willy Tarreau698b1512008-11-22 11:29:33 +01001264 if (lcd_addr_x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001265 /* check if we're not at the
1266 end of the line */
1267 if (lcd_addr_x < lcd_bwidth)
1268 /* back one char */
1269 lcd_write_cmd(0x10);
Willy Tarreau698b1512008-11-22 11:29:33 +01001270 lcd_addr_x--;
1271 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001272 /* replace with a space */
1273 lcd_write_data(' ');
1274 /* back one char again */
1275 lcd_write_cmd(0x10);
Willy Tarreau698b1512008-11-22 11:29:33 +01001276 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001277 case '\014':
1278 /* quickly clear the display */
Willy Tarreau698b1512008-11-22 11:29:33 +01001279 lcd_clear_fast();
1280 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001281 case '\n':
1282 /* flush the remainder of the current line and
1283 go to the beginning of the next line */
Willy Tarreau698b1512008-11-22 11:29:33 +01001284 for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
1285 lcd_write_data(' ');
1286 lcd_addr_x = 0;
1287 lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
1288 lcd_gotoxy();
1289 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001290 case '\r':
1291 /* go to the beginning of the same line */
Willy Tarreau698b1512008-11-22 11:29:33 +01001292 lcd_addr_x = 0;
1293 lcd_gotoxy();
1294 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001295 case '\t':
1296 /* print a space instead of the tab */
Willy Tarreau698b1512008-11-22 11:29:33 +01001297 lcd_print(' ');
1298 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001299 default:
1300 /* simply print this char */
Willy Tarreau698b1512008-11-22 11:29:33 +01001301 lcd_print(c);
1302 break;
1303 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001304 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001305
1306 /* now we'll see if we're in an escape mode and if the current
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001307 escape sequence can be understood. */
1308 if (lcd_escape_len >= 2) {
1309 int processed = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001310
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001311 if (!strcmp(lcd_escape, "[2J")) {
1312 /* clear the display */
1313 lcd_clear_fast();
Willy Tarreau698b1512008-11-22 11:29:33 +01001314 processed = 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001315 } else if (!strcmp(lcd_escape, "[H")) {
1316 /* cursor to home */
Willy Tarreau698b1512008-11-22 11:29:33 +01001317 lcd_addr_x = lcd_addr_y = 0;
1318 lcd_gotoxy();
1319 processed = 1;
1320 }
1321 /* codes starting with ^[[L */
1322 else if ((lcd_escape_len >= 3) &&
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001323 (lcd_escape[0] == '[') &&
1324 (lcd_escape[1] == 'L')) {
1325 processed = handle_lcd_special_code();
Willy Tarreau698b1512008-11-22 11:29:33 +01001326 }
1327
1328 /* LCD special escape codes */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001329 /* flush the escape sequence if it's been processed
1330 or if it is getting too long. */
Willy Tarreau698b1512008-11-22 11:29:33 +01001331 if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1332 lcd_escape_len = -1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001333 } /* escape codes */
Willy Tarreau7005b582008-11-13 17:18:59 -08001334 }
1335
Willy Tarreau698b1512008-11-22 11:29:33 +01001336 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001337}
1338
Willy Tarreau698b1512008-11-22 11:29:33 +01001339static int lcd_open(struct inode *inode, struct file *file)
1340{
1341 if (lcd_open_cnt)
1342 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001343
Willy Tarreau698b1512008-11-22 11:29:33 +01001344 if (file->f_mode & FMODE_READ) /* device is write-only */
1345 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001346
Willy Tarreau698b1512008-11-22 11:29:33 +01001347 if (lcd_must_clear) {
1348 lcd_clear_display();
1349 lcd_must_clear = 0;
1350 }
1351 lcd_open_cnt++;
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001352 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001353}
1354
Willy Tarreau698b1512008-11-22 11:29:33 +01001355static int lcd_release(struct inode *inode, struct file *file)
1356{
1357 lcd_open_cnt--;
1358 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001359}
1360
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001361static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001362 .write = lcd_write,
1363 .open = lcd_open,
1364 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001365 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001366};
1367
1368static struct miscdevice lcd_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001369 LCD_MINOR,
1370 "lcd",
1371 &lcd_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001372};
1373
Willy Tarreau7005b582008-11-13 17:18:59 -08001374/* public function usable from the kernel for any purpose */
Willy Tarreau698b1512008-11-22 11:29:33 +01001375void panel_lcd_print(char *s)
1376{
1377 if (lcd_enabled && lcd_initialized)
1378 lcd_write(NULL, s, strlen(s), NULL);
Willy Tarreau7005b582008-11-13 17:18:59 -08001379}
1380
Willy Tarreau7005b582008-11-13 17:18:59 -08001381/* initialize the LCD driver */
Willy Tarreau698b1512008-11-22 11:29:33 +01001382void lcd_init(void)
1383{
1384 switch (lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001385 case LCD_TYPE_OLD:
1386 /* parallel mode, 8 bits */
Willy Tarreau698b1512008-11-22 11:29:33 +01001387 if (lcd_proto < 0)
1388 lcd_proto = LCD_PROTO_PARALLEL;
1389 if (lcd_charset < 0)
1390 lcd_charset = LCD_CHARSET_NORMAL;
1391 if (lcd_e_pin == PIN_NOT_SET)
1392 lcd_e_pin = PIN_STROBE;
1393 if (lcd_rs_pin == PIN_NOT_SET)
1394 lcd_rs_pin = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001395
Willy Tarreau698b1512008-11-22 11:29:33 +01001396 if (lcd_width < 0)
1397 lcd_width = 40;
1398 if (lcd_bwidth < 0)
1399 lcd_bwidth = 40;
1400 if (lcd_hwidth < 0)
1401 lcd_hwidth = 64;
1402 if (lcd_height < 0)
1403 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001404 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001405 case LCD_TYPE_KS0074:
1406 /* serial mode, ks0074 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001407 if (lcd_proto < 0)
1408 lcd_proto = LCD_PROTO_SERIAL;
1409 if (lcd_charset < 0)
1410 lcd_charset = LCD_CHARSET_KS0074;
1411 if (lcd_bl_pin == PIN_NOT_SET)
1412 lcd_bl_pin = PIN_AUTOLF;
1413 if (lcd_cl_pin == PIN_NOT_SET)
1414 lcd_cl_pin = PIN_STROBE;
1415 if (lcd_da_pin == PIN_NOT_SET)
1416 lcd_da_pin = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001417
Willy Tarreau698b1512008-11-22 11:29:33 +01001418 if (lcd_width < 0)
1419 lcd_width = 16;
1420 if (lcd_bwidth < 0)
1421 lcd_bwidth = 40;
1422 if (lcd_hwidth < 0)
1423 lcd_hwidth = 16;
1424 if (lcd_height < 0)
1425 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001426 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001427 case LCD_TYPE_NEXCOM:
1428 /* parallel mode, 8 bits, generic */
Willy Tarreau698b1512008-11-22 11:29:33 +01001429 if (lcd_proto < 0)
1430 lcd_proto = LCD_PROTO_PARALLEL;
1431 if (lcd_charset < 0)
1432 lcd_charset = LCD_CHARSET_NORMAL;
1433 if (lcd_e_pin == PIN_NOT_SET)
1434 lcd_e_pin = PIN_AUTOLF;
1435 if (lcd_rs_pin == PIN_NOT_SET)
1436 lcd_rs_pin = PIN_SELECP;
1437 if (lcd_rw_pin == PIN_NOT_SET)
1438 lcd_rw_pin = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001439
Willy Tarreau698b1512008-11-22 11:29:33 +01001440 if (lcd_width < 0)
1441 lcd_width = 16;
1442 if (lcd_bwidth < 0)
1443 lcd_bwidth = 40;
1444 if (lcd_hwidth < 0)
1445 lcd_hwidth = 64;
1446 if (lcd_height < 0)
1447 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001448 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001449 case LCD_TYPE_CUSTOM:
1450 /* customer-defined */
Willy Tarreau698b1512008-11-22 11:29:33 +01001451 if (lcd_proto < 0)
1452 lcd_proto = DEFAULT_LCD_PROTO;
1453 if (lcd_charset < 0)
1454 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001455 /* default geometry will be set later */
1456 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001457 case LCD_TYPE_HANTRONIX:
1458 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001459 default:
1460 if (lcd_proto < 0)
1461 lcd_proto = LCD_PROTO_PARALLEL;
1462 if (lcd_charset < 0)
1463 lcd_charset = LCD_CHARSET_NORMAL;
1464 if (lcd_e_pin == PIN_NOT_SET)
1465 lcd_e_pin = PIN_STROBE;
1466 if (lcd_rs_pin == PIN_NOT_SET)
1467 lcd_rs_pin = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001468
Willy Tarreau698b1512008-11-22 11:29:33 +01001469 if (lcd_width < 0)
1470 lcd_width = 16;
1471 if (lcd_bwidth < 0)
1472 lcd_bwidth = 40;
1473 if (lcd_hwidth < 0)
1474 lcd_hwidth = 64;
1475 if (lcd_height < 0)
1476 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001477 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001478 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001479
Willy Tarreau698b1512008-11-22 11:29:33 +01001480 /* this is used to catch wrong and default values */
1481 if (lcd_width <= 0)
1482 lcd_width = DEFAULT_LCD_WIDTH;
1483 if (lcd_bwidth <= 0)
1484 lcd_bwidth = DEFAULT_LCD_BWIDTH;
1485 if (lcd_hwidth <= 0)
1486 lcd_hwidth = DEFAULT_LCD_HWIDTH;
1487 if (lcd_height <= 0)
1488 lcd_height = DEFAULT_LCD_HEIGHT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001489
Willy Tarreau698b1512008-11-22 11:29:33 +01001490 if (lcd_proto == LCD_PROTO_SERIAL) { /* SERIAL */
1491 lcd_write_cmd = lcd_write_cmd_s;
1492 lcd_write_data = lcd_write_data_s;
1493 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001494
Willy Tarreau698b1512008-11-22 11:29:33 +01001495 if (lcd_cl_pin == PIN_NOT_SET)
1496 lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1497 if (lcd_da_pin == PIN_NOT_SET)
1498 lcd_da_pin = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001499
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001500 } else if (lcd_proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001501 lcd_write_cmd = lcd_write_cmd_p8;
1502 lcd_write_data = lcd_write_data_p8;
1503 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001504
Willy Tarreau698b1512008-11-22 11:29:33 +01001505 if (lcd_e_pin == PIN_NOT_SET)
1506 lcd_e_pin = DEFAULT_LCD_PIN_E;
1507 if (lcd_rs_pin == PIN_NOT_SET)
1508 lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1509 if (lcd_rw_pin == PIN_NOT_SET)
1510 lcd_rw_pin = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001511 } else {
1512 lcd_write_cmd = lcd_write_cmd_tilcd;
1513 lcd_write_data = lcd_write_data_tilcd;
1514 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001515 }
1516
1517 if (lcd_bl_pin == PIN_NOT_SET)
1518 lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1519
1520 if (lcd_e_pin == PIN_NOT_SET)
1521 lcd_e_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001522 if (lcd_rs_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001523 lcd_rs_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001524 if (lcd_rw_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001525 lcd_rw_pin = PIN_NONE;
1526 if (lcd_bl_pin == PIN_NOT_SET)
1527 lcd_bl_pin = PIN_NONE;
1528 if (lcd_cl_pin == PIN_NOT_SET)
1529 lcd_cl_pin = PIN_NONE;
1530 if (lcd_da_pin == PIN_NOT_SET)
1531 lcd_da_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001532
Willy Tarreau698b1512008-11-22 11:29:33 +01001533 if (lcd_charset < 0)
1534 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001535
Willy Tarreau698b1512008-11-22 11:29:33 +01001536 if (lcd_charset == LCD_CHARSET_KS0074)
1537 lcd_char_conv = lcd_char_conv_ks0074;
1538 else
1539 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001540
Willy Tarreau698b1512008-11-22 11:29:33 +01001541 if (lcd_bl_pin != PIN_NONE)
1542 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001543
Willy Tarreau698b1512008-11-22 11:29:33 +01001544 pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1545 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1546 pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1547 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1548 pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1549 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1550 pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1551 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1552 pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1553 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1554 pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1555 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001556
Willy Tarreau698b1512008-11-22 11:29:33 +01001557 /* before this line, we must NOT send anything to the display.
1558 * Since lcd_init_display() needs to write data, we have to
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001559 * enable mark the LCD initialized just before. */
Willy Tarreau698b1512008-11-22 11:29:33 +01001560 lcd_initialized = 1;
1561 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001562
Willy Tarreau698b1512008-11-22 11:29:33 +01001563 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001564#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1565#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001566 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001567#endif
1568#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001569 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1570 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001571#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001572 lcd_addr_x = lcd_addr_y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001573 /* clear the display on the next device opening */
1574 lcd_must_clear = 1;
Willy Tarreau698b1512008-11-22 11:29:33 +01001575 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001576}
1577
Willy Tarreau7005b582008-11-13 17:18:59 -08001578/*
1579 * These are the file operation function for user access to /dev/keypad
1580 */
1581
Willy Tarreau698b1512008-11-22 11:29:33 +01001582static ssize_t keypad_read(struct file *file,
1583 char *buf, size_t count, loff_t *ppos)
1584{
Willy Tarreau7005b582008-11-13 17:18:59 -08001585
Willy Tarreau698b1512008-11-22 11:29:33 +01001586 unsigned i = *ppos;
1587 char *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001588
Willy Tarreau698b1512008-11-22 11:29:33 +01001589 if (keypad_buflen == 0) {
1590 if (file->f_flags & O_NONBLOCK)
1591 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001592
Willy Tarreau698b1512008-11-22 11:29:33 +01001593 interruptible_sleep_on(&keypad_read_wait);
1594 if (signal_pending(current))
1595 return -EINTR;
1596 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001597
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001598 for (; count-- > 0 && (keypad_buflen > 0);
1599 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001600 put_user(keypad_buffer[keypad_start], tmp);
1601 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1602 }
1603 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001604
Willy Tarreau698b1512008-11-22 11:29:33 +01001605 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001606}
1607
Willy Tarreau698b1512008-11-22 11:29:33 +01001608static int keypad_open(struct inode *inode, struct file *file)
1609{
Willy Tarreau7005b582008-11-13 17:18:59 -08001610
Willy Tarreau698b1512008-11-22 11:29:33 +01001611 if (keypad_open_cnt)
1612 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001613
Willy Tarreau698b1512008-11-22 11:29:33 +01001614 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1615 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001616
Willy Tarreau698b1512008-11-22 11:29:33 +01001617 keypad_buflen = 0; /* flush the buffer on opening */
1618 keypad_open_cnt++;
1619 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001620}
1621
Willy Tarreau698b1512008-11-22 11:29:33 +01001622static int keypad_release(struct inode *inode, struct file *file)
1623{
1624 keypad_open_cnt--;
1625 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001626}
1627
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001628static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001629 .read = keypad_read, /* read */
1630 .open = keypad_open, /* open */
1631 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001632 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001633};
1634
1635static struct miscdevice keypad_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001636 KEYPAD_MINOR,
1637 "keypad",
1638 &keypad_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001639};
1640
Willy Tarreau698b1512008-11-22 11:29:33 +01001641static void keypad_send_key(char *string, int max_len)
1642{
1643 if (init_in_progress)
1644 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001645
Willy Tarreau698b1512008-11-22 11:29:33 +01001646 /* send the key to the device only if a process is attached to it. */
1647 if (keypad_open_cnt > 0) {
1648 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1649 keypad_buffer[(keypad_start + keypad_buflen++) %
1650 KEYPAD_BUFFER] = *string++;
1651 }
1652 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001653 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001654}
1655
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001656/* this function scans all the bits involving at least one logical signal,
1657 * and puts the results in the bitfield "phys_read" (one bit per established
1658 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001659 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001660 * Note: to debounce input signals, we will only consider as switched a signal
1661 * which is stable across 2 measures. Signals which are different between two
1662 * reads will be kept as they previously were in their logical form (phys_prev).
1663 * A signal which has just switched will have a 1 in
1664 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001665 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001666static void phys_scan_contacts(void)
1667{
1668 int bit, bitval;
1669 char oldval;
1670 char bitmask;
1671 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001672
Willy Tarreau698b1512008-11-22 11:29:33 +01001673 phys_prev = phys_curr;
1674 phys_read_prev = phys_read;
1675 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001676
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001677 /* keep track of old value, with all outputs disabled */
1678 oldval = r_dtr(pprt) | scan_mask_o;
1679 /* activate all keyboard outputs (active low) */
1680 w_dtr(pprt, oldval & ~scan_mask_o);
1681
1682 /* will have a 1 for each bit set to gnd */
1683 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1684 /* disable all matrix signals */
1685 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001686
Willy Tarreau698b1512008-11-22 11:29:33 +01001687 /* now that all outputs are cleared, the only active input bits are
1688 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001689 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001690
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001691 /* 1 for each grounded input */
1692 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1693
1694 /* grounded inputs are signals 40-44 */
1695 phys_read |= (pmask_t) gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001696
Willy Tarreau698b1512008-11-22 11:29:33 +01001697 if (bitmask != gndmask) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001698 /* since clearing the outputs changed some inputs, we know
1699 * that some input signals are currently tied to some outputs.
1700 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001701 */
1702 for (bit = 0; bit < 8; bit++) {
1703 bitval = 1 << bit;
1704
1705 if (!(scan_mask_o & bitval)) /* skip unused bits */
1706 continue;
1707
1708 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1709 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1710 phys_read |= (pmask_t) bitmask << (5 * bit);
1711 }
1712 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001713 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001714 /* this is easy: use old bits when they are flapping,
1715 * use new ones when stable */
1716 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1717 (phys_read & ~(phys_read ^ phys_read_prev));
1718}
1719
1720static inline int input_state_high(struct logical_input *input)
1721{
1722#if 0
1723 /* FIXME:
1724 * this is an invalid test. It tries to catch
1725 * transitions from single-key to multiple-key, but
1726 * doesn't take into account the contacts polarity.
1727 * The only solution to the problem is to parse keys
1728 * from the most complex to the simplest combinations,
1729 * and mark them as 'caught' once a combination
1730 * matches, then unmatch it for all other ones.
1731 */
1732
1733 /* try to catch dangerous transitions cases :
1734 * someone adds a bit, so this signal was a false
1735 * positive resulting from a transition. We should
1736 * invalidate the signal immediately and not call the
1737 * release function.
1738 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1739 */
1740 if (((phys_prev & input->mask) == input->value)
1741 && ((phys_curr & input->mask) > input->value)) {
1742 input->state = INPUT_ST_LOW; /* invalidate */
1743 return 1;
1744 }
1745#endif
1746
1747 if ((phys_curr & input->mask) == input->value) {
1748 if ((input->type == INPUT_TYPE_STD) &&
1749 (input->high_timer == 0)) {
1750 input->high_timer++;
1751 if (input->u.std.press_fct != NULL)
1752 input->u.std.press_fct(input->u.std.press_data);
1753 } else if (input->type == INPUT_TYPE_KBD) {
1754 /* will turn on the light */
1755 keypressed = 1;
1756
1757 if (input->high_timer == 0) {
1758 char *press_str = input->u.kbd.press_str;
1759 if (press_str[0])
1760 keypad_send_key(press_str,
1761 sizeof(press_str));
1762 }
1763
1764 if (input->u.kbd.repeat_str[0]) {
1765 char *repeat_str = input->u.kbd.repeat_str;
1766 if (input->high_timer >= KEYPAD_REP_START) {
1767 input->high_timer -= KEYPAD_REP_DELAY;
1768 keypad_send_key(repeat_str,
1769 sizeof(repeat_str));
1770 }
1771 /* we will need to come back here soon */
1772 inputs_stable = 0;
1773 }
1774
1775 if (input->high_timer < 255)
1776 input->high_timer++;
1777 }
1778 return 1;
1779 } else {
1780 /* else signal falling down. Let's fall through. */
1781 input->state = INPUT_ST_FALLING;
1782 input->fall_timer = 0;
1783 }
1784 return 0;
1785}
1786
1787static inline void input_state_falling(struct logical_input *input)
1788{
1789#if 0
1790 /* FIXME !!! same comment as in input_state_high */
1791 if (((phys_prev & input->mask) == input->value)
1792 && ((phys_curr & input->mask) > input->value)) {
1793 input->state = INPUT_ST_LOW; /* invalidate */
1794 return;
1795 }
1796#endif
1797
1798 if ((phys_curr & input->mask) == input->value) {
1799 if (input->type == INPUT_TYPE_KBD) {
1800 /* will turn on the light */
1801 keypressed = 1;
1802
1803 if (input->u.kbd.repeat_str[0]) {
1804 char *repeat_str = input->u.kbd.repeat_str;
1805 if (input->high_timer >= KEYPAD_REP_START)
1806 input->high_timer -= KEYPAD_REP_DELAY;
1807 keypad_send_key(repeat_str,
1808 sizeof(repeat_str));
1809 /* we will need to come back here soon */
1810 inputs_stable = 0;
1811 }
1812
1813 if (input->high_timer < 255)
1814 input->high_timer++;
1815 }
1816 input->state = INPUT_ST_HIGH;
1817 } else if (input->fall_timer >= input->fall_time) {
1818 /* call release event */
1819 if (input->type == INPUT_TYPE_STD) {
1820 void (*release_fct)(int) = input->u.std.release_fct;
1821 if (release_fct != NULL)
1822 release_fct(input->u.std.release_data);
1823 } else if (input->type == INPUT_TYPE_KBD) {
1824 char *release_str = input->u.kbd.release_str;
1825 if (release_str[0])
1826 keypad_send_key(release_str,
1827 sizeof(release_str));
1828 }
1829
1830 input->state = INPUT_ST_LOW;
1831 } else {
1832 input->fall_timer++;
1833 inputs_stable = 0;
1834 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001835}
1836
Willy Tarreau698b1512008-11-22 11:29:33 +01001837static void panel_process_inputs(void)
1838{
1839 struct list_head *item;
1840 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001841
Willy Tarreau698b1512008-11-22 11:29:33 +01001842 keypressed = 0;
1843 inputs_stable = 1;
1844 list_for_each(item, &logical_inputs) {
1845 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001846
Willy Tarreau698b1512008-11-22 11:29:33 +01001847 switch (input->state) {
1848 case INPUT_ST_LOW:
1849 if ((phys_curr & input->mask) != input->value)
1850 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001851 /* if all needed ones were already set previously,
1852 * this means that this logical signal has been
1853 * activated by the releasing of another combined
1854 * signal, so we don't want to match.
1855 * eg: AB -(release B)-> A -(release A)-> 0 :
1856 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001857 */
1858 if ((phys_prev & input->mask) == input->value)
1859 break;
1860 input->rise_timer = 0;
1861 input->state = INPUT_ST_RISING;
1862 /* no break here, fall through */
1863 case INPUT_ST_RISING:
1864 if ((phys_curr & input->mask) != input->value) {
1865 input->state = INPUT_ST_LOW;
1866 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001867 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001868 if (input->rise_timer < input->rise_time) {
1869 inputs_stable = 0;
1870 input->rise_timer++;
1871 break;
1872 }
1873 input->high_timer = 0;
1874 input->state = INPUT_ST_HIGH;
1875 /* no break here, fall through */
1876 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001877 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001878 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001879 /* no break here, fall through */
1880 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001881 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001882 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001883 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001884}
1885
Willy Tarreau698b1512008-11-22 11:29:33 +01001886static void panel_scan_timer(void)
1887{
Willy Tarreau630231772008-11-22 12:52:18 +01001888 if (keypad_enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001889 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001890 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001891
1892 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001893 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001894 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001895
Willy Tarreau698b1512008-11-22 11:29:33 +01001896 if (!inputs_stable || phys_curr != phys_prev)
1897 panel_process_inputs();
1898 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001899
Willy Tarreau698b1512008-11-22 11:29:33 +01001900 if (lcd_enabled && lcd_initialized) {
1901 if (keypressed) {
1902 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1903 lcd_backlight(1);
1904 light_tempo = FLASH_LIGHT_TEMPO;
1905 } else if (light_tempo > 0) {
1906 light_tempo--;
1907 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1908 lcd_backlight(0);
1909 }
1910 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001911
Willy Tarreau698b1512008-11-22 11:29:33 +01001912 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001913}
1914
Willy Tarreau698b1512008-11-22 11:29:33 +01001915static void init_scan_timer(void)
1916{
1917 if (scan_timer.function != NULL)
1918 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001919
Willy Tarreau698b1512008-11-22 11:29:33 +01001920 init_timer(&scan_timer);
1921 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1922 scan_timer.data = 0;
1923 scan_timer.function = (void *)&panel_scan_timer;
1924 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001925}
1926
1927/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001928 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1929 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08001930 * returns 1 if ok, 0 if error (in which case, nothing is written).
1931 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001932static int input_name2mask(char *name, pmask_t *mask, pmask_t *value,
1933 char *imask, char *omask)
1934{
1935 static char sigtab[10] = "EeSsPpAaBb";
1936 char im, om;
1937 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08001938
Willy Tarreau698b1512008-11-22 11:29:33 +01001939 om = im = m = v = 0ULL;
1940 while (*name) {
1941 int in, out, bit, neg;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001942 for (in = 0; (in < sizeof(sigtab)) &&
1943 (sigtab[in] != *name); in++)
Willy Tarreau698b1512008-11-22 11:29:33 +01001944 ;
1945 if (in >= sizeof(sigtab))
1946 return 0; /* input name not found */
1947 neg = (in & 1); /* odd (lower) names are negated */
1948 in >>= 1;
1949 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08001950
Willy Tarreau698b1512008-11-22 11:29:33 +01001951 name++;
1952 if (isdigit(*name)) {
1953 out = *name - '0';
1954 om |= (1 << out);
1955 } else if (*name == '-')
1956 out = 8;
1957 else
1958 return 0; /* unknown bit name */
1959
1960 bit = (out * 5) + in;
1961
1962 m |= 1ULL << bit;
1963 if (!neg)
1964 v |= 1ULL << bit;
1965 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08001966 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001967 *mask = m;
1968 *value = v;
1969 if (imask)
1970 *imask |= im;
1971 if (omask)
1972 *omask |= om;
1973 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001974}
1975
1976/* tries to bind a key to the signal name <name>. The key will send the
1977 * strings <press>, <repeat>, <release> for these respective events.
1978 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1979 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001980static struct logical_input *panel_bind_key(char *name, char *press,
1981 char *repeat, char *release)
1982{
1983 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001984
Julia Lawall7a6cb0d2010-05-13 22:00:05 +02001985 key = kzalloc(sizeof(struct logical_input), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001986 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01001987 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001988
Willy Tarreau698b1512008-11-22 11:29:33 +01001989 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04001990 &scan_mask_o)) {
1991 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01001992 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04001993 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001994
1995 key->type = INPUT_TYPE_KBD;
1996 key->state = INPUT_ST_LOW;
1997 key->rise_time = 1;
1998 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001999
Willy Tarreau698b1512008-11-22 11:29:33 +01002000 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2001 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2002 strncpy(key->u.kbd.release_str, release,
2003 sizeof(key->u.kbd.release_str));
2004 list_add(&key->list, &logical_inputs);
2005 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002006}
2007
Willy Tarreau630231772008-11-22 12:52:18 +01002008#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002009/* tries to bind a callback function to the signal name <name>. The function
2010 * <press_fct> will be called with the <press_data> arg when the signal is
2011 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002012 * Returns the pointer to the new signal if ok, NULL if the signal could not
2013 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002014 */
2015static struct logical_input *panel_bind_callback(char *name,
Willy Tarreau698b1512008-11-22 11:29:33 +01002016 void (*press_fct) (int),
2017 int press_data,
2018 void (*release_fct) (int),
2019 int release_data)
2020{
2021 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002022
Willy Tarreau698b1512008-11-22 11:29:33 +01002023 callback = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002024 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002025 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002026
Willy Tarreau698b1512008-11-22 11:29:33 +01002027 memset(callback, 0, sizeof(struct logical_input));
2028 if (!input_name2mask(name, &callback->mask, &callback->value,
2029 &scan_mask_i, &scan_mask_o))
2030 return NULL;
2031
2032 callback->type = INPUT_TYPE_STD;
2033 callback->state = INPUT_ST_LOW;
2034 callback->rise_time = 1;
2035 callback->fall_time = 1;
2036 callback->u.std.press_fct = press_fct;
2037 callback->u.std.press_data = press_data;
2038 callback->u.std.release_fct = release_fct;
2039 callback->u.std.release_data = release_data;
2040 list_add(&callback->list, &logical_inputs);
2041 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002042}
Willy Tarreau630231772008-11-22 12:52:18 +01002043#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002044
Willy Tarreau698b1512008-11-22 11:29:33 +01002045static void keypad_init(void)
2046{
2047 int keynum;
2048 init_waitqueue_head(&keypad_read_wait);
2049 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002050
Willy Tarreau698b1512008-11-22 11:29:33 +01002051 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002052
Willy Tarreau698b1512008-11-22 11:29:33 +01002053 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2054 panel_bind_key(keypad_profile[keynum][0],
2055 keypad_profile[keynum][1],
2056 keypad_profile[keynum][2],
2057 keypad_profile[keynum][3]);
2058 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002059
Willy Tarreau698b1512008-11-22 11:29:33 +01002060 init_scan_timer();
2061 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002062}
2063
Willy Tarreau7005b582008-11-13 17:18:59 -08002064/**************************************************/
2065/* device initialization */
2066/**************************************************/
2067
Willy Tarreau698b1512008-11-22 11:29:33 +01002068static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2069 void *unused)
2070{
2071 if (lcd_enabled && lcd_initialized) {
2072 switch (code) {
2073 case SYS_DOWN:
2074 panel_lcd_print
2075 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2076 break;
2077 case SYS_HALT:
2078 panel_lcd_print
2079 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2080 break;
2081 case SYS_POWER_OFF:
2082 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2083 break;
2084 default:
2085 break;
2086 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002087 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002088 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002089}
2090
2091static struct notifier_block panel_notifier = {
2092 panel_notify_sys,
2093 NULL,
2094 0
2095};
2096
Willy Tarreau698b1512008-11-22 11:29:33 +01002097static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002098{
Willy Tarreau698b1512008-11-22 11:29:33 +01002099 if (port->number != parport)
2100 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002101
Willy Tarreau698b1512008-11-22 11:29:33 +01002102 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002103 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2104 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002105 return;
2106 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002107
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002108 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
Willy Tarreau698b1512008-11-22 11:29:33 +01002109 NULL,
2110 /*PARPORT_DEV_EXCL */
2111 0, (void *)&pprt);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002112 if (pprt == NULL) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002113 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2114 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002115 return;
2116 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002117
Willy Tarreau698b1512008-11-22 11:29:33 +01002118 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002119 pr_err("could not claim access to parport%d. Aborting.\n",
2120 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002121 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002122 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002123
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002124 /* must init LCD first, just in case an IRQ from the keypad is
2125 * generated at keypad init
2126 */
Willy Tarreau698b1512008-11-22 11:29:33 +01002127 if (lcd_enabled) {
2128 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002129 if (misc_register(&lcd_dev))
2130 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002131 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002132
Willy Tarreau698b1512008-11-22 11:29:33 +01002133 if (keypad_enabled) {
2134 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002135 if (misc_register(&keypad_dev))
2136 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002137 }
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002138 return;
2139
2140err_lcd_unreg:
2141 if (lcd_enabled)
2142 misc_deregister(&lcd_dev);
2143err_unreg_device:
2144 parport_unregister_device(pprt);
2145 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002146}
2147
Willy Tarreau698b1512008-11-22 11:29:33 +01002148static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002149{
Willy Tarreau698b1512008-11-22 11:29:33 +01002150 if (port->number != parport)
2151 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002152
Willy Tarreau698b1512008-11-22 11:29:33 +01002153 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002154 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2155 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002156 return;
2157 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002158
Peter Huewe0b0595b2009-09-29 01:22:40 +02002159 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002160 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002161 keypad_initialized = 0;
2162 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002163
Peter Huewe0b0595b2009-09-29 01:22:40 +02002164 if (lcd_enabled && lcd_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002165 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002166 lcd_initialized = 0;
2167 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002168
Willy Tarreau698b1512008-11-22 11:29:33 +01002169 parport_release(pprt);
2170 parport_unregister_device(pprt);
2171 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002172}
2173
2174static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002175 .name = "panel",
2176 .attach = panel_attach,
2177 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002178};
2179
2180/* init function */
Willy Tarreau698b1512008-11-22 11:29:33 +01002181int panel_init(void)
2182{
2183 /* for backwards compatibility */
2184 if (keypad_type < 0)
2185 keypad_type = keypad_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002186
Willy Tarreau698b1512008-11-22 11:29:33 +01002187 if (lcd_type < 0)
2188 lcd_type = lcd_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002189
Willy Tarreau698b1512008-11-22 11:29:33 +01002190 if (parport < 0)
2191 parport = DEFAULT_PARPORT;
Willy Tarreau7005b582008-11-13 17:18:59 -08002192
Willy Tarreau698b1512008-11-22 11:29:33 +01002193 /* take care of an eventual profile */
2194 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002195 case PANEL_PROFILE_CUSTOM:
2196 /* custom profile */
Willy Tarreau698b1512008-11-22 11:29:33 +01002197 if (keypad_type < 0)
2198 keypad_type = DEFAULT_KEYPAD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002199 if (lcd_type < 0)
2200 lcd_type = DEFAULT_LCD;
2201 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002202 case PANEL_PROFILE_OLD:
2203 /* 8 bits, 2*16, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002204 if (keypad_type < 0)
2205 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002206 if (lcd_type < 0)
2207 lcd_type = LCD_TYPE_OLD;
2208 if (lcd_width < 0)
2209 lcd_width = 16;
2210 if (lcd_hwidth < 0)
2211 lcd_hwidth = 16;
2212 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002213 case PANEL_PROFILE_NEW:
2214 /* serial, 2*16, new keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002215 if (keypad_type < 0)
2216 keypad_type = KEYPAD_TYPE_NEW;
Willy Tarreau698b1512008-11-22 11:29:33 +01002217 if (lcd_type < 0)
2218 lcd_type = LCD_TYPE_KS0074;
2219 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002220 case PANEL_PROFILE_HANTRONIX:
2221 /* 8 bits, 2*16 hantronix-like, no keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002222 if (keypad_type < 0)
2223 keypad_type = KEYPAD_TYPE_NONE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002224 if (lcd_type < 0)
2225 lcd_type = LCD_TYPE_HANTRONIX;
2226 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002227 case PANEL_PROFILE_NEXCOM:
2228 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Willy Tarreau698b1512008-11-22 11:29:33 +01002229 if (keypad_type < 0)
2230 keypad_type = KEYPAD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002231 if (lcd_type < 0)
2232 lcd_type = LCD_TYPE_NEXCOM;
2233 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002234 case PANEL_PROFILE_LARGE:
2235 /* 8 bits, 2*40, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002236 if (keypad_type < 0)
2237 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002238 if (lcd_type < 0)
2239 lcd_type = LCD_TYPE_OLD;
2240 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002241 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002242
Willy Tarreau698b1512008-11-22 11:29:33 +01002243 lcd_enabled = (lcd_type > 0);
2244 keypad_enabled = (keypad_type > 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08002245
Willy Tarreau698b1512008-11-22 11:29:33 +01002246 switch (keypad_type) {
2247 case KEYPAD_TYPE_OLD:
2248 keypad_profile = old_keypad_profile;
2249 break;
2250 case KEYPAD_TYPE_NEW:
2251 keypad_profile = new_keypad_profile;
2252 break;
2253 case KEYPAD_TYPE_NEXCOM:
2254 keypad_profile = nexcom_keypad_profile;
2255 break;
2256 default:
2257 keypad_profile = NULL;
2258 break;
2259 }
2260
2261 /* tells various subsystems about the fact that we are initializing */
2262 init_in_progress = 1;
2263
2264 if (parport_register_driver(&panel_driver)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002265 pr_err("could not register with parport. Aborting.\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002266 return -EIO;
2267 }
2268
Willy Tarreau630231772008-11-22 12:52:18 +01002269 if (!lcd_enabled && !keypad_enabled) {
2270 /* no device enabled, let's release the parport */
Willy Tarreau698b1512008-11-22 11:29:33 +01002271 if (pprt) {
2272 parport_release(pprt);
2273 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002274 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002275 }
2276 parport_unregister_driver(&panel_driver);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002277 pr_err("driver version " PANEL_VERSION " disabled.\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002278 return -ENODEV;
2279 }
2280
2281 register_reboot_notifier(&panel_notifier);
2282
2283 if (pprt)
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002284 pr_info("driver version " PANEL_VERSION
2285 " registered on parport%d (io=0x%lx).\n", parport,
2286 pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002287 else
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002288 pr_info("driver version " PANEL_VERSION
2289 " not yet registered\n");
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002290 /* tells various subsystems about the fact that initialization
2291 is finished */
Willy Tarreau698b1512008-11-22 11:29:33 +01002292 init_in_progress = 0;
2293 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002294}
2295
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002296static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002297{
2298 return panel_init();
Willy Tarreau7005b582008-11-13 17:18:59 -08002299}
2300
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002301static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002302{
2303 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002304
Willy Tarreau698b1512008-11-22 11:29:33 +01002305 if (scan_timer.function != NULL)
2306 del_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002307
Costantino Leandro57898132009-02-17 11:10:48 -05002308 if (pprt != NULL) {
Peter Huewe0b0595b2009-09-29 01:22:40 +02002309 if (keypad_enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002310 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002311 keypad_initialized = 0;
2312 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002313
Costantino Leandro57898132009-02-17 11:10:48 -05002314 if (lcd_enabled) {
2315 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2316 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2317 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002318 lcd_initialized = 0;
Costantino Leandro57898132009-02-17 11:10:48 -05002319 }
2320
2321 /* TODO: free all input signals */
2322 parport_release(pprt);
2323 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002324 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002325 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002326 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002327}
Willy Tarreau7005b582008-11-13 17:18:59 -08002328
Willy Tarreau7005b582008-11-13 17:18:59 -08002329module_init(panel_init_module);
2330module_exit(panel_cleanup_module);
2331MODULE_AUTHOR("Willy Tarreau");
2332MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002333
2334/*
2335 * Local variables:
2336 * c-indent-level: 4
2337 * tab-width: 8
2338 * End:
2339 */