blob: 0cc5e084653ac8022a2544a9b0b625aac63d3d11 [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 */
Monam Agarwal68d386b2014-02-25 19:40:49 +0530174 void (*press_fct)(int);
175 void (*release_fct)(int);
Willy Tarreau698b1512008-11-22 11:29:33 +0100176 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
Peter Huewe36d20412013-02-15 13:47:05 +0100188static LIST_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
Monam Agarwal68d386b2014-02-25 19:40:49 +0530420static 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,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530460 "LCD type: 0=none, 1=old //, 2=serial ks0074, 3=hantronix //, 4=nexcom //, 5=compiled-in");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100461
462static int lcd_proto = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100463module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300464MODULE_PARM_DESC(lcd_proto,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +0200465 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100466
467static int lcd_charset = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100468module_param(lcd_charset, int, 0000);
469MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100470
471static int keypad_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100472module_param(keypad_type, int, 0000);
473MODULE_PARM_DESC(keypad_type,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530474 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100475
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100476static int profile = DEFAULT_PROFILE;
Willy Tarreau698b1512008-11-22 11:29:33 +0100477module_param(profile, int, 0000);
478MODULE_PARM_DESC(profile,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300479 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
480 "4=16x2 nexcom; default=40x2, old kp");
Willy Tarreau7005b582008-11-13 17:18:59 -0800481
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100482/*
483 * These are the parallel port pins the LCD control signals are connected to.
484 * Set this to 0 if the signal is not used. Set it to its opposite value
485 * (negative) if the signal is negated. -MAXINT is used to indicate that the
486 * pin has not been explicitly specified.
487 *
Willy Tarreau630231772008-11-22 12:52:18 +0100488 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100489 */
490
491static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100492module_param(lcd_e_pin, int, 0000);
493MODULE_PARM_DESC(lcd_e_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530494 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100495
496static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100497module_param(lcd_rs_pin, int, 0000);
498MODULE_PARM_DESC(lcd_rs_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530499 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100500
501static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100502module_param(lcd_rw_pin, int, 0000);
503MODULE_PARM_DESC(lcd_rw_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530504 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100505
506static int lcd_bl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100507module_param(lcd_bl_pin, int, 0000);
508MODULE_PARM_DESC(lcd_bl_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530509 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100510
511static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100512module_param(lcd_da_pin, int, 0000);
513MODULE_PARM_DESC(lcd_da_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530514 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100515
516static int lcd_cl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100517module_param(lcd_cl_pin, int, 0000);
518MODULE_PARM_DESC(lcd_cl_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530519 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreau7005b582008-11-13 17:18:59 -0800520
Peter Huewe36d20412013-02-15 13:47:05 +0100521static const unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800522
523/* for some LCD drivers (ks0074) we need a charset conversion table. */
Peter Huewe36d20412013-02-15 13:47:05 +0100524static const unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100525 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
526 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
527 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
528 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
529 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
530 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
531 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
532 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
533 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
534 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
535 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
536 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
537 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
538 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
539 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
540 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
541 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
542 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
543 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
544 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
545 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
546 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
547 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
548 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
549 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
550 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
551 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
552 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
553 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
554 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
555 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
556 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
557 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800558};
559
Peter Huewe36d20412013-02-15 13:47:05 +0100560static const char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100561 {"S0", "Left\n", "Left\n", ""},
562 {"S1", "Down\n", "Down\n", ""},
563 {"S2", "Up\n", "Up\n", ""},
564 {"S3", "Right\n", "Right\n", ""},
565 {"S4", "Esc\n", "Esc\n", ""},
566 {"S5", "Ret\n", "Ret\n", ""},
567 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800568};
569
570/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100571static const char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100572 {"S0", "Left\n", "Left\n", ""},
573 {"S1", "Down\n", "Down\n", ""},
574 {"S2", "Up\n", "Up\n", ""},
575 {"S3", "Right\n", "Right\n", ""},
576 {"S4s5", "", "Esc\n", "Esc\n"},
577 {"s4S5", "", "Ret\n", "Ret\n"},
578 {"S4S5", "Help\n", "", ""},
579 /* add new signals above this line */
580 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800581};
582
583/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100584static const char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100585 {"a-p-e-", "Down\n", "Down\n", ""},
586 {"a-p-E-", "Ret\n", "Ret\n", ""},
587 {"a-P-E-", "Esc\n", "Esc\n", ""},
588 {"a-P-e-", "Up\n", "Up\n", ""},
589 /* add new signals above this line */
590 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800591};
592
Peter Huewe36d20412013-02-15 13:47:05 +0100593static const char (*keypad_profile)[4][9] = old_keypad_profile;
Willy Tarreau7005b582008-11-13 17:18:59 -0800594
595/* FIXME: this should be converted to a bit array containing signals states */
596static struct {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300597 unsigned char e; /* parallel LCD E (data latch on falling edge) */
598 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
599 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
600 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
601 unsigned char cl; /* serial LCD clock (latch on rising edge) */
602 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800603} bits;
604
605static void init_scan_timer(void);
606
607/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100608static int set_data_bits(void)
609{
610 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800611
Willy Tarreau698b1512008-11-22 11:29:33 +0100612 val = r_dtr(pprt);
613 for (bit = 0; bit < LCD_BITS; bit++)
614 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800615
Willy Tarreau698b1512008-11-22 11:29:33 +0100616 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
617 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
618 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
619 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
620 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
621 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800622
Willy Tarreau698b1512008-11-22 11:29:33 +0100623 w_dtr(pprt, val);
624 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800625}
626
627/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100628static int set_ctrl_bits(void)
629{
630 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800631
Willy Tarreau698b1512008-11-22 11:29:33 +0100632 val = r_ctr(pprt);
633 for (bit = 0; bit < LCD_BITS; bit++)
634 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800635
Willy Tarreau698b1512008-11-22 11:29:33 +0100636 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
637 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
638 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
639 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
640 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
641 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800642
Willy Tarreau698b1512008-11-22 11:29:33 +0100643 w_ctr(pprt, val);
644 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800645}
646
647/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530648static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100649{
650 set_data_bits();
651 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800652}
653
654/*
655 * Converts a parallel port pin (from -25 to 25) to data and control ports
656 * masks, and data and control port bits. The signal will be considered
657 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
658 *
659 * Result will be used this way :
660 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
661 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
662 */
Peter Huewe36d20412013-02-15 13:47:05 +0100663static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
Willy Tarreau698b1512008-11-22 11:29:33 +0100664{
665 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800666
Willy Tarreau698b1512008-11-22 11:29:33 +0100667 d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
668 d_val[2] = c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800669
Willy Tarreau698b1512008-11-22 11:29:33 +0100670 if (pin == 0)
671 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800672
Willy Tarreau698b1512008-11-22 11:29:33 +0100673 inv = (pin < 0);
674 if (inv)
675 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800676
Willy Tarreau698b1512008-11-22 11:29:33 +0100677 d_bit = c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800678
Willy Tarreau698b1512008-11-22 11:29:33 +0100679 switch (pin) {
680 case PIN_STROBE: /* strobe, inverted */
681 c_bit = PNL_PSTROBE;
682 inv = !inv;
683 break;
684 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
685 d_bit = 1 << (pin - 2);
686 break;
687 case PIN_AUTOLF: /* autofeed, inverted */
688 c_bit = PNL_PAUTOLF;
689 inv = !inv;
690 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300691 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100692 c_bit = PNL_PINITP;
693 break;
694 case PIN_SELECP: /* select_in, inverted */
695 c_bit = PNL_PSELECP;
696 inv = !inv;
697 break;
698 default: /* unknown pin, ignore */
699 break;
700 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800701
Willy Tarreau698b1512008-11-22 11:29:33 +0100702 if (c_bit) {
703 c_val[2] &= ~c_bit;
704 c_val[!inv] = c_bit;
705 } else if (d_bit) {
706 d_val[2] &= ~d_bit;
707 d_val[!inv] = d_bit;
708 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800709}
710
711/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100712static void long_sleep(int ms)
713{
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200714 if (in_interrupt()) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100715 mdelay(ms);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200716 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +0100717 current->state = TASK_INTERRUPTIBLE;
718 schedule_timeout((ms * HZ + 999) / 1000);
719 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800720}
721
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300722/* send a serial byte to the LCD panel. The caller is responsible for locking
723 if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100724static void lcd_send_serial(int byte)
725{
726 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800727
Willy Tarreau698b1512008-11-22 11:29:33 +0100728 /* the data bit is set on D0, and the clock on STROBE.
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300729 * LCD reads D0 on STROBE's rising edge. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100730 for (bit = 0; bit < 8; bit++) {
731 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530732 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100733 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530734 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300735 udelay(2); /* maintain the data during 2 us before CLK up */
Willy Tarreau698b1512008-11-22 11:29:33 +0100736 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530737 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300738 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100739 byte >>= 1;
740 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800741}
742
743/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100744static void lcd_backlight(int on)
745{
746 if (lcd_bl_pin == PIN_NONE)
747 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800748
Justin P. Mattock6975e182012-03-19 08:17:52 -0700749 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800750 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100751 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530752 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800753 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800754}
755
756/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100757static void lcd_write_cmd_s(int cmd)
758{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800759 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100760 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
761 lcd_send_serial(cmd & 0x0F);
762 lcd_send_serial((cmd >> 4) & 0x0F);
763 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800764 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800765}
766
767/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100768static void lcd_write_data_s(int data)
769{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800770 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100771 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
772 lcd_send_serial(data & 0x0F);
773 lcd_send_serial((data >> 4) & 0x0F);
774 udelay(40); /* the shortest data takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800775 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800776}
777
778/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100779static void lcd_write_cmd_p8(int cmd)
780{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800781 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800782 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100783 w_dtr(pprt, cmd);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300784 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800785
Willy Tarreau698b1512008-11-22 11:29:33 +0100786 bits.e = BIT_SET;
787 bits.rs = BIT_CLR;
788 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800789 set_ctrl_bits();
790
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300791 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800792
793 bits.e = BIT_CLR;
794 set_ctrl_bits();
795
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300796 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800797 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100798}
Willy Tarreau7005b582008-11-13 17:18:59 -0800799
Willy Tarreau698b1512008-11-22 11:29:33 +0100800/* send data to the LCD panel in 8 bits parallel mode */
801static void lcd_write_data_p8(int data)
802{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800803 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100804 /* present the data to the data port */
805 w_dtr(pprt, data);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300806 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100807
808 bits.e = BIT_SET;
809 bits.rs = BIT_SET;
810 bits.rw = BIT_CLR;
811 set_ctrl_bits();
812
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300813 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100814
815 bits.e = BIT_CLR;
816 set_ctrl_bits();
817
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300818 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800819 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100820}
821
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400822/* send a command to the TI LCD panel */
823static void lcd_write_cmd_tilcd(int cmd)
824{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800825 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400826 /* present the data to the control port */
827 w_ctr(pprt, cmd);
828 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800829 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400830}
831
832/* send data to the TI LCD panel */
833static void lcd_write_data_tilcd(int data)
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 data port */
837 w_dtr(pprt, data);
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
Willy Tarreau698b1512008-11-22 11:29:33 +0100842static void lcd_gotoxy(void)
843{
844 lcd_write_cmd(0x80 /* set DDRAM address */
845 | (lcd_addr_y ? lcd_hwidth : 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300846 /* we force the cursor to stay at the end of the
847 line if it wants to go farther */
Willy Tarreau698b1512008-11-22 11:29:33 +0100848 | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
849 (lcd_hwidth - 1) : lcd_bwidth - 1));
850}
851
852static void lcd_print(char c)
853{
854 if (lcd_addr_x < lcd_bwidth) {
855 if (lcd_char_conv != NULL)
856 c = lcd_char_conv[(unsigned char)c];
857 lcd_write_data(c);
858 lcd_addr_x++;
859 }
860 /* prevents the cursor from wrapping onto the next line */
861 if (lcd_addr_x == lcd_bwidth)
862 lcd_gotoxy();
863}
864
865/* fills the display with spaces and resets X/Y */
866static void lcd_clear_fast_s(void)
867{
868 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200869
Willy Tarreau698b1512008-11-22 11:29:33 +0100870 lcd_addr_x = lcd_addr_y = 0;
871 lcd_gotoxy();
872
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800873 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100874 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
875 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
876 lcd_send_serial(' ' & 0x0F);
877 lcd_send_serial((' ' >> 4) & 0x0F);
878 udelay(40); /* the shortest data takes at least 40 us */
879 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800880 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100881
882 lcd_addr_x = lcd_addr_y = 0;
883 lcd_gotoxy();
884}
885
886/* fills the display with spaces and resets X/Y */
887static void lcd_clear_fast_p8(void)
888{
889 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200890
Willy Tarreau698b1512008-11-22 11:29:33 +0100891 lcd_addr_x = lcd_addr_y = 0;
892 lcd_gotoxy();
893
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800894 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100895 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
896 /* present the data to the data port */
897 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300898
899 /* maintain the data during 20 us before the strobe */
900 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100901
902 bits.e = BIT_SET;
903 bits.rs = BIT_SET;
904 bits.rw = BIT_CLR;
905 set_ctrl_bits();
906
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300907 /* maintain the strobe during 40 us */
908 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100909
910 bits.e = BIT_CLR;
911 set_ctrl_bits();
912
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300913 /* the shortest data takes at least 45 us */
914 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100915 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800916 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100917
918 lcd_addr_x = lcd_addr_y = 0;
919 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800920}
921
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400922/* fills the display with spaces and resets X/Y */
923static void lcd_clear_fast_tilcd(void)
924{
925 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200926
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400927 lcd_addr_x = lcd_addr_y = 0;
928 lcd_gotoxy();
929
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800930 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400931 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
932 /* present the data to the data port */
933 w_dtr(pprt, ' ');
934 udelay(60);
935 }
936
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800937 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400938
939 lcd_addr_x = lcd_addr_y = 0;
940 lcd_gotoxy();
941}
942
Willy Tarreau7005b582008-11-13 17:18:59 -0800943/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +0100944static void lcd_clear_display(void)
945{
946 lcd_write_cmd(0x01); /* clear display */
947 lcd_addr_x = lcd_addr_y = 0;
948 /* we must wait a few milliseconds (15) */
949 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -0800950}
951
Willy Tarreau698b1512008-11-22 11:29:33 +0100952static void lcd_init_display(void)
953{
Willy Tarreau698b1512008-11-22 11:29:33 +0100954 lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
955 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -0800956
Willy Tarreau698b1512008-11-22 11:29:33 +0100957 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -0800958
Willy Tarreau698b1512008-11-22 11:29:33 +0100959 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
960 long_sleep(10);
961 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
962 long_sleep(10);
963 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
964 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800965
Willy Tarreau698b1512008-11-22 11:29:33 +0100966 lcd_write_cmd(0x30 /* set font height and lines number */
967 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
968 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0)
969 );
970 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800971
Willy Tarreau698b1512008-11-22 11:29:33 +0100972 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
973 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800974
Willy Tarreau698b1512008-11-22 11:29:33 +0100975 lcd_write_cmd(0x08 /* set display mode */
976 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
977 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
978 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0)
979 );
Willy Tarreau7005b582008-11-13 17:18:59 -0800980
Willy Tarreau698b1512008-11-22 11:29:33 +0100981 lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -0800982
Willy Tarreau698b1512008-11-22 11:29:33 +0100983 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800984
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300985 /* entry mode set : increment, cursor shifting */
986 lcd_write_cmd(0x06);
Willy Tarreau7005b582008-11-13 17:18:59 -0800987
Willy Tarreau698b1512008-11-22 11:29:33 +0100988 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -0800989}
990
991/*
992 * These are the file operation function for user access to /dev/lcd
993 * This function can also be called from inside the kernel, by
994 * setting file and ppos to NULL.
995 *
996 */
997
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300998static inline int handle_lcd_special_code(void)
999{
1000 /* LCD special codes */
1001
1002 int processed = 0;
1003
1004 char *esc = lcd_escape + 2;
1005 int oldflags = lcd_flags;
1006
1007 /* check for display mode flags */
1008 switch (*esc) {
1009 case 'D': /* Display ON */
1010 lcd_flags |= LCD_FLAG_D;
1011 processed = 1;
1012 break;
1013 case 'd': /* Display OFF */
1014 lcd_flags &= ~LCD_FLAG_D;
1015 processed = 1;
1016 break;
1017 case 'C': /* Cursor ON */
1018 lcd_flags |= LCD_FLAG_C;
1019 processed = 1;
1020 break;
1021 case 'c': /* Cursor OFF */
1022 lcd_flags &= ~LCD_FLAG_C;
1023 processed = 1;
1024 break;
1025 case 'B': /* Blink ON */
1026 lcd_flags |= LCD_FLAG_B;
1027 processed = 1;
1028 break;
1029 case 'b': /* Blink OFF */
1030 lcd_flags &= ~LCD_FLAG_B;
1031 processed = 1;
1032 break;
1033 case '+': /* Back light ON */
1034 lcd_flags |= LCD_FLAG_L;
1035 processed = 1;
1036 break;
1037 case '-': /* Back light OFF */
1038 lcd_flags &= ~LCD_FLAG_L;
1039 processed = 1;
1040 break;
1041 case '*':
1042 /* flash back light using the keypad timer */
1043 if (scan_timer.function != NULL) {
1044 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1045 lcd_backlight(1);
1046 light_tempo = FLASH_LIGHT_TEMPO;
1047 }
1048 processed = 1;
1049 break;
1050 case 'f': /* Small Font */
1051 lcd_flags &= ~LCD_FLAG_F;
1052 processed = 1;
1053 break;
1054 case 'F': /* Large Font */
1055 lcd_flags |= LCD_FLAG_F;
1056 processed = 1;
1057 break;
1058 case 'n': /* One Line */
1059 lcd_flags &= ~LCD_FLAG_N;
1060 processed = 1;
1061 break;
1062 case 'N': /* Two Lines */
1063 lcd_flags |= LCD_FLAG_N;
1064 break;
1065 case 'l': /* Shift Cursor Left */
1066 if (lcd_addr_x > 0) {
1067 /* back one char if not at end of line */
1068 if (lcd_addr_x < lcd_bwidth)
1069 lcd_write_cmd(0x10);
1070 lcd_addr_x--;
1071 }
1072 processed = 1;
1073 break;
1074 case 'r': /* shift cursor right */
1075 if (lcd_addr_x < lcd_width) {
1076 /* allow the cursor to pass the end of the line */
1077 if (lcd_addr_x <
1078 (lcd_bwidth - 1))
1079 lcd_write_cmd(0x14);
1080 lcd_addr_x++;
1081 }
1082 processed = 1;
1083 break;
1084 case 'L': /* shift display left */
1085 lcd_left_shift++;
1086 lcd_write_cmd(0x18);
1087 processed = 1;
1088 break;
1089 case 'R': /* shift display right */
1090 lcd_left_shift--;
1091 lcd_write_cmd(0x1C);
1092 processed = 1;
1093 break;
1094 case 'k': { /* kill end of line */
1095 int x;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001096
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001097 for (x = lcd_addr_x; x < lcd_bwidth; x++)
1098 lcd_write_data(' ');
1099
1100 /* restore cursor position */
1101 lcd_gotoxy();
1102 processed = 1;
1103 break;
1104 }
1105 case 'I': /* reinitialize display */
1106 lcd_init_display();
1107 lcd_left_shift = 0;
1108 processed = 1;
1109 break;
1110 case 'G': {
1111 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1112 * and '7', representing the numerical ASCII code of the
1113 * redefined character, and <xx...xx> a sequence of 16
1114 * hex digits representing 8 bytes for each character.
1115 * Most LCDs will only use 5 lower bits of the 7 first
1116 * bytes.
1117 */
1118
1119 unsigned char cgbytes[8];
1120 unsigned char cgaddr;
1121 int cgoffset;
1122 int shift;
1123 char value;
1124 int addr;
1125
1126 if (strchr(esc, ';') == NULL)
1127 break;
1128
1129 esc++;
1130
1131 cgaddr = *(esc++) - '0';
1132 if (cgaddr > 7) {
1133 processed = 1;
1134 break;
1135 }
1136
1137 cgoffset = 0;
1138 shift = 0;
1139 value = 0;
1140 while (*esc && cgoffset < 8) {
1141 shift ^= 4;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001142 if (*esc >= '0' && *esc <= '9') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001143 value |= (*esc - '0') << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001144 } else if (*esc >= 'A' && *esc <= 'Z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001145 value |= (*esc - 'A' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001146 } else if (*esc >= 'a' && *esc <= 'z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001147 value |= (*esc - 'a' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001148 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001149 esc++;
1150 continue;
1151 }
1152
1153 if (shift == 0) {
1154 cgbytes[cgoffset++] = value;
1155 value = 0;
1156 }
1157
1158 esc++;
1159 }
1160
1161 lcd_write_cmd(0x40 | (cgaddr * 8));
1162 for (addr = 0; addr < cgoffset; addr++)
1163 lcd_write_data(cgbytes[addr]);
1164
1165 /* ensures that we stop writing to CGRAM */
1166 lcd_gotoxy();
1167 processed = 1;
1168 break;
1169 }
1170 case 'x': /* gotoxy : LxXXX[yYYY]; */
1171 case 'y': /* gotoxy : LyYYY[xXXX]; */
1172 if (strchr(esc, ';') == NULL)
1173 break;
1174
1175 while (*esc) {
1176 if (*esc == 'x') {
1177 esc++;
Pelle Windestam12995702011-08-30 20:29:24 +02001178 if (kstrtoul(esc, 10, &lcd_addr_x) < 0)
1179 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001180 } else if (*esc == 'y') {
1181 esc++;
Pelle Windestam12995702011-08-30 20:29:24 +02001182 if (kstrtoul(esc, 10, &lcd_addr_y) < 0)
1183 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001184 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001185 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001186 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001187 }
1188
1189 lcd_gotoxy();
1190 processed = 1;
1191 break;
1192 }
1193
Adam Buchbinderf2635892012-09-19 21:51:07 -04001194 /* Check whether one flag was changed */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001195 if (oldflags != lcd_flags) {
1196 /* check whether one of B,C,D flags were changed */
1197 if ((oldflags ^ lcd_flags) &
1198 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1199 /* set display mode */
1200 lcd_write_cmd(0x08
1201 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
1202 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
1203 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0));
1204 /* check whether one of F,N flags was changed */
1205 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_F | LCD_FLAG_N))
1206 lcd_write_cmd(0x30
1207 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
1208 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001209 /* check whether L flag was changed */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001210 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_L)) {
1211 if (lcd_flags & (LCD_FLAG_L))
1212 lcd_backlight(1);
1213 else if (light_tempo == 0)
1214 /* switch off the light only when the tempo
1215 lighting is gone */
1216 lcd_backlight(0);
1217 }
1218 }
1219
1220 return processed;
1221}
1222
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001223static void lcd_write_char(char c)
Willy Tarreau698b1512008-11-22 11:29:33 +01001224{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001225 /* first, we'll test if we're in escape mode */
1226 if ((c != '\n') && lcd_escape_len >= 0) {
1227 /* yes, let's add this char to the buffer */
1228 lcd_escape[lcd_escape_len++] = c;
1229 lcd_escape[lcd_escape_len] = 0;
1230 } else {
1231 /* aborts any previous escape sequence */
1232 lcd_escape_len = -1;
1233
1234 switch (c) {
1235 case LCD_ESCAPE_CHAR:
1236 /* start of an escape sequence */
1237 lcd_escape_len = 0;
1238 lcd_escape[lcd_escape_len] = 0;
1239 break;
1240 case '\b':
1241 /* go back one char and clear it */
1242 if (lcd_addr_x > 0) {
1243 /* check if we're not at the
1244 end of the line */
1245 if (lcd_addr_x < lcd_bwidth)
1246 /* back one char */
1247 lcd_write_cmd(0x10);
1248 lcd_addr_x--;
1249 }
1250 /* replace with a space */
1251 lcd_write_data(' ');
1252 /* back one char again */
1253 lcd_write_cmd(0x10);
1254 break;
1255 case '\014':
1256 /* quickly clear the display */
1257 lcd_clear_fast();
1258 break;
1259 case '\n':
1260 /* flush the remainder of the current line and
1261 go to the beginning of the next line */
1262 for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
1263 lcd_write_data(' ');
1264 lcd_addr_x = 0;
1265 lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
1266 lcd_gotoxy();
1267 break;
1268 case '\r':
1269 /* go to the beginning of the same line */
1270 lcd_addr_x = 0;
1271 lcd_gotoxy();
1272 break;
1273 case '\t':
1274 /* print a space instead of the tab */
1275 lcd_print(' ');
1276 break;
1277 default:
1278 /* simply print this char */
1279 lcd_print(c);
1280 break;
1281 }
1282 }
1283
1284 /* now we'll see if we're in an escape mode and if the current
1285 escape sequence can be understood. */
1286 if (lcd_escape_len >= 2) {
1287 int processed = 0;
1288
1289 if (!strcmp(lcd_escape, "[2J")) {
1290 /* clear the display */
1291 lcd_clear_fast();
1292 processed = 1;
1293 } else if (!strcmp(lcd_escape, "[H")) {
1294 /* cursor to home */
1295 lcd_addr_x = lcd_addr_y = 0;
1296 lcd_gotoxy();
1297 processed = 1;
1298 }
1299 /* codes starting with ^[[L */
1300 else if ((lcd_escape_len >= 3) &&
1301 (lcd_escape[0] == '[') &&
1302 (lcd_escape[1] == 'L')) {
1303 processed = handle_lcd_special_code();
1304 }
1305
1306 /* LCD special escape codes */
1307 /* flush the escape sequence if it's been processed
1308 or if it is getting too long. */
1309 if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1310 lcd_escape_len = -1;
1311 } /* escape codes */
1312}
1313
1314static ssize_t lcd_write(struct file *file,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001315 const char __user *buf, size_t count, loff_t *ppos)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001316{
1317 const char __user *tmp = buf;
Willy Tarreau698b1512008-11-22 11:29:33 +01001318 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001319
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001320 for (; count-- > 0; (*ppos)++, tmp++) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001321 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001322 /* let's be a little nice with other processes
1323 that need some CPU */
1324 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001325
Bastien Armand6a4193a2014-04-23 19:42:11 +02001326 if (get_user(c, tmp))
Willy Tarreau698b1512008-11-22 11:29:33 +01001327 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001328
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001329 lcd_write_char(c);
Willy Tarreau7005b582008-11-13 17:18:59 -08001330 }
1331
Willy Tarreau698b1512008-11-22 11:29:33 +01001332 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001333}
1334
Willy Tarreau698b1512008-11-22 11:29:33 +01001335static int lcd_open(struct inode *inode, struct file *file)
1336{
1337 if (lcd_open_cnt)
1338 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001339
Willy Tarreau698b1512008-11-22 11:29:33 +01001340 if (file->f_mode & FMODE_READ) /* device is write-only */
1341 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001342
Willy Tarreau698b1512008-11-22 11:29:33 +01001343 if (lcd_must_clear) {
1344 lcd_clear_display();
1345 lcd_must_clear = 0;
1346 }
1347 lcd_open_cnt++;
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001348 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001349}
1350
Willy Tarreau698b1512008-11-22 11:29:33 +01001351static int lcd_release(struct inode *inode, struct file *file)
1352{
1353 lcd_open_cnt--;
1354 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001355}
1356
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001357static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001358 .write = lcd_write,
1359 .open = lcd_open,
1360 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001361 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001362};
1363
1364static struct miscdevice lcd_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001365 LCD_MINOR,
1366 "lcd",
1367 &lcd_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001368};
1369
Willy Tarreau7005b582008-11-13 17:18:59 -08001370/* public function usable from the kernel for any purpose */
Peter Huewe36d20412013-02-15 13:47:05 +01001371static void panel_lcd_print(const char *s)
Willy Tarreau698b1512008-11-22 11:29:33 +01001372{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001373 const char *tmp = s;
1374 int count = strlen(s);
1375
1376 if (lcd_enabled && lcd_initialized) {
1377 for (; count-- > 0; tmp++) {
1378 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
1379 /* let's be a little nice with other processes
1380 that need some CPU */
1381 schedule();
1382
1383 lcd_write_char(*tmp);
1384 }
1385 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001386}
1387
Willy Tarreau7005b582008-11-13 17:18:59 -08001388/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +01001389static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001390{
1391 switch (lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001392 case LCD_TYPE_OLD:
1393 /* parallel mode, 8 bits */
Willy Tarreau698b1512008-11-22 11:29:33 +01001394 if (lcd_proto < 0)
1395 lcd_proto = LCD_PROTO_PARALLEL;
1396 if (lcd_charset < 0)
1397 lcd_charset = LCD_CHARSET_NORMAL;
1398 if (lcd_e_pin == PIN_NOT_SET)
1399 lcd_e_pin = PIN_STROBE;
1400 if (lcd_rs_pin == PIN_NOT_SET)
1401 lcd_rs_pin = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001402
Willy Tarreau698b1512008-11-22 11:29:33 +01001403 if (lcd_width < 0)
1404 lcd_width = 40;
1405 if (lcd_bwidth < 0)
1406 lcd_bwidth = 40;
1407 if (lcd_hwidth < 0)
1408 lcd_hwidth = 64;
1409 if (lcd_height < 0)
1410 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001411 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001412 case LCD_TYPE_KS0074:
1413 /* serial mode, ks0074 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001414 if (lcd_proto < 0)
1415 lcd_proto = LCD_PROTO_SERIAL;
1416 if (lcd_charset < 0)
1417 lcd_charset = LCD_CHARSET_KS0074;
1418 if (lcd_bl_pin == PIN_NOT_SET)
1419 lcd_bl_pin = PIN_AUTOLF;
1420 if (lcd_cl_pin == PIN_NOT_SET)
1421 lcd_cl_pin = PIN_STROBE;
1422 if (lcd_da_pin == PIN_NOT_SET)
1423 lcd_da_pin = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001424
Willy Tarreau698b1512008-11-22 11:29:33 +01001425 if (lcd_width < 0)
1426 lcd_width = 16;
1427 if (lcd_bwidth < 0)
1428 lcd_bwidth = 40;
1429 if (lcd_hwidth < 0)
1430 lcd_hwidth = 16;
1431 if (lcd_height < 0)
1432 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001433 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001434 case LCD_TYPE_NEXCOM:
1435 /* parallel mode, 8 bits, generic */
Willy Tarreau698b1512008-11-22 11:29:33 +01001436 if (lcd_proto < 0)
1437 lcd_proto = LCD_PROTO_PARALLEL;
1438 if (lcd_charset < 0)
1439 lcd_charset = LCD_CHARSET_NORMAL;
1440 if (lcd_e_pin == PIN_NOT_SET)
1441 lcd_e_pin = PIN_AUTOLF;
1442 if (lcd_rs_pin == PIN_NOT_SET)
1443 lcd_rs_pin = PIN_SELECP;
1444 if (lcd_rw_pin == PIN_NOT_SET)
1445 lcd_rw_pin = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001446
Willy Tarreau698b1512008-11-22 11:29:33 +01001447 if (lcd_width < 0)
1448 lcd_width = 16;
1449 if (lcd_bwidth < 0)
1450 lcd_bwidth = 40;
1451 if (lcd_hwidth < 0)
1452 lcd_hwidth = 64;
1453 if (lcd_height < 0)
1454 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001455 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001456 case LCD_TYPE_CUSTOM:
1457 /* customer-defined */
Willy Tarreau698b1512008-11-22 11:29:33 +01001458 if (lcd_proto < 0)
1459 lcd_proto = DEFAULT_LCD_PROTO;
1460 if (lcd_charset < 0)
1461 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001462 /* default geometry will be set later */
1463 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001464 case LCD_TYPE_HANTRONIX:
1465 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001466 default:
1467 if (lcd_proto < 0)
1468 lcd_proto = LCD_PROTO_PARALLEL;
1469 if (lcd_charset < 0)
1470 lcd_charset = LCD_CHARSET_NORMAL;
1471 if (lcd_e_pin == PIN_NOT_SET)
1472 lcd_e_pin = PIN_STROBE;
1473 if (lcd_rs_pin == PIN_NOT_SET)
1474 lcd_rs_pin = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001475
Willy Tarreau698b1512008-11-22 11:29:33 +01001476 if (lcd_width < 0)
1477 lcd_width = 16;
1478 if (lcd_bwidth < 0)
1479 lcd_bwidth = 40;
1480 if (lcd_hwidth < 0)
1481 lcd_hwidth = 64;
1482 if (lcd_height < 0)
1483 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001484 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001485 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001486
Willy Tarreau698b1512008-11-22 11:29:33 +01001487 /* this is used to catch wrong and default values */
1488 if (lcd_width <= 0)
1489 lcd_width = DEFAULT_LCD_WIDTH;
1490 if (lcd_bwidth <= 0)
1491 lcd_bwidth = DEFAULT_LCD_BWIDTH;
1492 if (lcd_hwidth <= 0)
1493 lcd_hwidth = DEFAULT_LCD_HWIDTH;
1494 if (lcd_height <= 0)
1495 lcd_height = DEFAULT_LCD_HEIGHT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001496
Willy Tarreau698b1512008-11-22 11:29:33 +01001497 if (lcd_proto == LCD_PROTO_SERIAL) { /* SERIAL */
1498 lcd_write_cmd = lcd_write_cmd_s;
1499 lcd_write_data = lcd_write_data_s;
1500 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001501
Willy Tarreau698b1512008-11-22 11:29:33 +01001502 if (lcd_cl_pin == PIN_NOT_SET)
1503 lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1504 if (lcd_da_pin == PIN_NOT_SET)
1505 lcd_da_pin = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001506
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001507 } else if (lcd_proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001508 lcd_write_cmd = lcd_write_cmd_p8;
1509 lcd_write_data = lcd_write_data_p8;
1510 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001511
Willy Tarreau698b1512008-11-22 11:29:33 +01001512 if (lcd_e_pin == PIN_NOT_SET)
1513 lcd_e_pin = DEFAULT_LCD_PIN_E;
1514 if (lcd_rs_pin == PIN_NOT_SET)
1515 lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1516 if (lcd_rw_pin == PIN_NOT_SET)
1517 lcd_rw_pin = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001518 } else {
1519 lcd_write_cmd = lcd_write_cmd_tilcd;
1520 lcd_write_data = lcd_write_data_tilcd;
1521 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001522 }
1523
1524 if (lcd_bl_pin == PIN_NOT_SET)
1525 lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1526
1527 if (lcd_e_pin == PIN_NOT_SET)
1528 lcd_e_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001529 if (lcd_rs_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001530 lcd_rs_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001531 if (lcd_rw_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001532 lcd_rw_pin = PIN_NONE;
1533 if (lcd_bl_pin == PIN_NOT_SET)
1534 lcd_bl_pin = PIN_NONE;
1535 if (lcd_cl_pin == PIN_NOT_SET)
1536 lcd_cl_pin = PIN_NONE;
1537 if (lcd_da_pin == PIN_NOT_SET)
1538 lcd_da_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001539
Willy Tarreau698b1512008-11-22 11:29:33 +01001540 if (lcd_charset < 0)
1541 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001542
Willy Tarreau698b1512008-11-22 11:29:33 +01001543 if (lcd_charset == LCD_CHARSET_KS0074)
1544 lcd_char_conv = lcd_char_conv_ks0074;
1545 else
1546 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001547
Willy Tarreau698b1512008-11-22 11:29:33 +01001548 if (lcd_bl_pin != PIN_NONE)
1549 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001550
Willy Tarreau698b1512008-11-22 11:29:33 +01001551 pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1552 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1553 pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1554 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1555 pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1556 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1557 pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1558 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1559 pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1560 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1561 pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1562 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001563
Willy Tarreau698b1512008-11-22 11:29:33 +01001564 /* before this line, we must NOT send anything to the display.
1565 * Since lcd_init_display() needs to write data, we have to
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001566 * enable mark the LCD initialized just before. */
Willy Tarreau698b1512008-11-22 11:29:33 +01001567 lcd_initialized = 1;
1568 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001569
Willy Tarreau698b1512008-11-22 11:29:33 +01001570 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001571#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1572#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001573 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001574#endif
1575#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001576 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1577 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001578#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001579 lcd_addr_x = lcd_addr_y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001580 /* clear the display on the next device opening */
1581 lcd_must_clear = 1;
Willy Tarreau698b1512008-11-22 11:29:33 +01001582 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001583}
1584
Willy Tarreau7005b582008-11-13 17:18:59 -08001585/*
1586 * These are the file operation function for user access to /dev/keypad
1587 */
1588
Willy Tarreau698b1512008-11-22 11:29:33 +01001589static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001590 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001591{
Willy Tarreau698b1512008-11-22 11:29:33 +01001592 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001593 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001594
Willy Tarreau698b1512008-11-22 11:29:33 +01001595 if (keypad_buflen == 0) {
1596 if (file->f_flags & O_NONBLOCK)
1597 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001598
Arnd Bergmann310df692014-01-02 13:07:35 +01001599 if (wait_event_interruptible(keypad_read_wait,
1600 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001601 return -EINTR;
1602 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001603
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001604 for (; count-- > 0 && (keypad_buflen > 0);
1605 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001606 put_user(keypad_buffer[keypad_start], tmp);
1607 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1608 }
1609 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001610
Willy Tarreau698b1512008-11-22 11:29:33 +01001611 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001612}
1613
Willy Tarreau698b1512008-11-22 11:29:33 +01001614static int keypad_open(struct inode *inode, struct file *file)
1615{
Willy Tarreau698b1512008-11-22 11:29:33 +01001616 if (keypad_open_cnt)
1617 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001618
Willy Tarreau698b1512008-11-22 11:29:33 +01001619 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1620 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001621
Willy Tarreau698b1512008-11-22 11:29:33 +01001622 keypad_buflen = 0; /* flush the buffer on opening */
1623 keypad_open_cnt++;
1624 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001625}
1626
Willy Tarreau698b1512008-11-22 11:29:33 +01001627static int keypad_release(struct inode *inode, struct file *file)
1628{
1629 keypad_open_cnt--;
1630 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001631}
1632
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001633static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001634 .read = keypad_read, /* read */
1635 .open = keypad_open, /* open */
1636 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001637 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001638};
1639
1640static struct miscdevice keypad_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001641 KEYPAD_MINOR,
1642 "keypad",
1643 &keypad_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001644};
1645
Peter Huewe36d20412013-02-15 13:47:05 +01001646static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001647{
1648 if (init_in_progress)
1649 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001650
Willy Tarreau698b1512008-11-22 11:29:33 +01001651 /* send the key to the device only if a process is attached to it. */
1652 if (keypad_open_cnt > 0) {
1653 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1654 keypad_buffer[(keypad_start + keypad_buflen++) %
1655 KEYPAD_BUFFER] = *string++;
1656 }
1657 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001658 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001659}
1660
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001661/* this function scans all the bits involving at least one logical signal,
1662 * and puts the results in the bitfield "phys_read" (one bit per established
1663 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001664 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001665 * Note: to debounce input signals, we will only consider as switched a signal
1666 * which is stable across 2 measures. Signals which are different between two
1667 * reads will be kept as they previously were in their logical form (phys_prev).
1668 * A signal which has just switched will have a 1 in
1669 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001670 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001671static void phys_scan_contacts(void)
1672{
1673 int bit, bitval;
1674 char oldval;
1675 char bitmask;
1676 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001677
Willy Tarreau698b1512008-11-22 11:29:33 +01001678 phys_prev = phys_curr;
1679 phys_read_prev = phys_read;
1680 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001681
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001682 /* keep track of old value, with all outputs disabled */
1683 oldval = r_dtr(pprt) | scan_mask_o;
1684 /* activate all keyboard outputs (active low) */
1685 w_dtr(pprt, oldval & ~scan_mask_o);
1686
1687 /* will have a 1 for each bit set to gnd */
1688 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1689 /* disable all matrix signals */
1690 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001691
Willy Tarreau698b1512008-11-22 11:29:33 +01001692 /* now that all outputs are cleared, the only active input bits are
1693 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001694 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001695
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001696 /* 1 for each grounded input */
1697 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1698
1699 /* grounded inputs are signals 40-44 */
1700 phys_read |= (pmask_t) gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001701
Willy Tarreau698b1512008-11-22 11:29:33 +01001702 if (bitmask != gndmask) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001703 /* since clearing the outputs changed some inputs, we know
1704 * that some input signals are currently tied to some outputs.
1705 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001706 */
1707 for (bit = 0; bit < 8; bit++) {
1708 bitval = 1 << bit;
1709
1710 if (!(scan_mask_o & bitval)) /* skip unused bits */
1711 continue;
1712
1713 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1714 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1715 phys_read |= (pmask_t) bitmask << (5 * bit);
1716 }
1717 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001718 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001719 /* this is easy: use old bits when they are flapping,
1720 * use new ones when stable */
1721 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1722 (phys_read & ~(phys_read ^ phys_read_prev));
1723}
1724
1725static inline int input_state_high(struct logical_input *input)
1726{
1727#if 0
1728 /* FIXME:
1729 * this is an invalid test. It tries to catch
1730 * transitions from single-key to multiple-key, but
1731 * doesn't take into account the contacts polarity.
1732 * The only solution to the problem is to parse keys
1733 * from the most complex to the simplest combinations,
1734 * and mark them as 'caught' once a combination
1735 * matches, then unmatch it for all other ones.
1736 */
1737
1738 /* try to catch dangerous transitions cases :
1739 * someone adds a bit, so this signal was a false
1740 * positive resulting from a transition. We should
1741 * invalidate the signal immediately and not call the
1742 * release function.
1743 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1744 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001745 if (((phys_prev & input->mask) == input->value) &&
1746 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001747 input->state = INPUT_ST_LOW; /* invalidate */
1748 return 1;
1749 }
1750#endif
1751
1752 if ((phys_curr & input->mask) == input->value) {
1753 if ((input->type == INPUT_TYPE_STD) &&
1754 (input->high_timer == 0)) {
1755 input->high_timer++;
1756 if (input->u.std.press_fct != NULL)
1757 input->u.std.press_fct(input->u.std.press_data);
1758 } else if (input->type == INPUT_TYPE_KBD) {
1759 /* will turn on the light */
1760 keypressed = 1;
1761
1762 if (input->high_timer == 0) {
1763 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001764
Jake Champline6626de2013-05-04 11:21:17 -04001765 if (press_str[0]) {
1766 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001767
Jake Champline6626de2013-05-04 11:21:17 -04001768 keypad_send_key(press_str, s);
1769 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001770 }
1771
1772 if (input->u.kbd.repeat_str[0]) {
1773 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001774
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001775 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001776 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001777
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001778 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001779 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001780 }
1781 /* we will need to come back here soon */
1782 inputs_stable = 0;
1783 }
1784
1785 if (input->high_timer < 255)
1786 input->high_timer++;
1787 }
1788 return 1;
1789 } else {
1790 /* else signal falling down. Let's fall through. */
1791 input->state = INPUT_ST_FALLING;
1792 input->fall_timer = 0;
1793 }
1794 return 0;
1795}
1796
1797static inline void input_state_falling(struct logical_input *input)
1798{
1799#if 0
1800 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001801 if (((phys_prev & input->mask) == input->value) &&
1802 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001803 input->state = INPUT_ST_LOW; /* invalidate */
1804 return;
1805 }
1806#endif
1807
1808 if ((phys_curr & input->mask) == input->value) {
1809 if (input->type == INPUT_TYPE_KBD) {
1810 /* will turn on the light */
1811 keypressed = 1;
1812
1813 if (input->u.kbd.repeat_str[0]) {
1814 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001815
Jake Champline6626de2013-05-04 11:21:17 -04001816 if (input->high_timer >= KEYPAD_REP_START) {
1817 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001818
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001819 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001820 keypad_send_key(repeat_str, s);
1821 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001822 /* we will need to come back here soon */
1823 inputs_stable = 0;
1824 }
1825
1826 if (input->high_timer < 255)
1827 input->high_timer++;
1828 }
1829 input->state = INPUT_ST_HIGH;
1830 } else if (input->fall_timer >= input->fall_time) {
1831 /* call release event */
1832 if (input->type == INPUT_TYPE_STD) {
1833 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001834
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001835 if (release_fct != NULL)
1836 release_fct(input->u.std.release_data);
1837 } else if (input->type == INPUT_TYPE_KBD) {
1838 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001839
Jake Champline6626de2013-05-04 11:21:17 -04001840 if (release_str[0]) {
1841 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001842
Jake Champline6626de2013-05-04 11:21:17 -04001843 keypad_send_key(release_str, s);
1844 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001845 }
1846
1847 input->state = INPUT_ST_LOW;
1848 } else {
1849 input->fall_timer++;
1850 inputs_stable = 0;
1851 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001852}
1853
Willy Tarreau698b1512008-11-22 11:29:33 +01001854static void panel_process_inputs(void)
1855{
1856 struct list_head *item;
1857 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001858
Willy Tarreau698b1512008-11-22 11:29:33 +01001859 keypressed = 0;
1860 inputs_stable = 1;
1861 list_for_each(item, &logical_inputs) {
1862 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001863
Willy Tarreau698b1512008-11-22 11:29:33 +01001864 switch (input->state) {
1865 case INPUT_ST_LOW:
1866 if ((phys_curr & input->mask) != input->value)
1867 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001868 /* if all needed ones were already set previously,
1869 * this means that this logical signal has been
1870 * activated by the releasing of another combined
1871 * signal, so we don't want to match.
1872 * eg: AB -(release B)-> A -(release A)-> 0 :
1873 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001874 */
1875 if ((phys_prev & input->mask) == input->value)
1876 break;
1877 input->rise_timer = 0;
1878 input->state = INPUT_ST_RISING;
1879 /* no break here, fall through */
1880 case INPUT_ST_RISING:
1881 if ((phys_curr & input->mask) != input->value) {
1882 input->state = INPUT_ST_LOW;
1883 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001884 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001885 if (input->rise_timer < input->rise_time) {
1886 inputs_stable = 0;
1887 input->rise_timer++;
1888 break;
1889 }
1890 input->high_timer = 0;
1891 input->state = INPUT_ST_HIGH;
1892 /* no break here, fall through */
1893 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001894 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001895 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001896 /* no break here, fall through */
1897 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001898 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001899 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001900 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001901}
1902
Willy Tarreau698b1512008-11-22 11:29:33 +01001903static void panel_scan_timer(void)
1904{
Willy Tarreau630231772008-11-22 12:52:18 +01001905 if (keypad_enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001906 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001907 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001908
1909 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001910 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001911 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001912
Willy Tarreau698b1512008-11-22 11:29:33 +01001913 if (!inputs_stable || phys_curr != phys_prev)
1914 panel_process_inputs();
1915 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001916
Willy Tarreau698b1512008-11-22 11:29:33 +01001917 if (lcd_enabled && lcd_initialized) {
1918 if (keypressed) {
1919 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1920 lcd_backlight(1);
1921 light_tempo = FLASH_LIGHT_TEMPO;
1922 } else if (light_tempo > 0) {
1923 light_tempo--;
1924 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1925 lcd_backlight(0);
1926 }
1927 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001928
Willy Tarreau698b1512008-11-22 11:29:33 +01001929 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001930}
1931
Willy Tarreau698b1512008-11-22 11:29:33 +01001932static void init_scan_timer(void)
1933{
1934 if (scan_timer.function != NULL)
1935 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001936
Willy Tarreau698b1512008-11-22 11:29:33 +01001937 init_timer(&scan_timer);
1938 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1939 scan_timer.data = 0;
1940 scan_timer.function = (void *)&panel_scan_timer;
1941 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001942}
1943
1944/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001945 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1946 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08001947 * returns 1 if ok, 0 if error (in which case, nothing is written).
1948 */
Peter Huewe36d20412013-02-15 13:47:05 +01001949static int input_name2mask(const char *name, pmask_t *mask, pmask_t *value,
Willy Tarreau698b1512008-11-22 11:29:33 +01001950 char *imask, char *omask)
1951{
1952 static char sigtab[10] = "EeSsPpAaBb";
1953 char im, om;
1954 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08001955
Willy Tarreau698b1512008-11-22 11:29:33 +01001956 om = im = m = v = 0ULL;
1957 while (*name) {
1958 int in, out, bit, neg;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001959
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001960 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name);
1961 in++)
Willy Tarreau698b1512008-11-22 11:29:33 +01001962 ;
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001963
Willy Tarreau698b1512008-11-22 11:29:33 +01001964 if (in >= sizeof(sigtab))
1965 return 0; /* input name not found */
1966 neg = (in & 1); /* odd (lower) names are negated */
1967 in >>= 1;
1968 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08001969
Willy Tarreau698b1512008-11-22 11:29:33 +01001970 name++;
1971 if (isdigit(*name)) {
1972 out = *name - '0';
1973 om |= (1 << out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001974 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01001975 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001976 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01001977 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001978 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001979
1980 bit = (out * 5) + in;
1981
1982 m |= 1ULL << bit;
1983 if (!neg)
1984 v |= 1ULL << bit;
1985 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08001986 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001987 *mask = m;
1988 *value = v;
1989 if (imask)
1990 *imask |= im;
1991 if (omask)
1992 *omask |= om;
1993 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001994}
1995
1996/* tries to bind a key to the signal name <name>. The key will send the
1997 * strings <press>, <repeat>, <release> for these respective events.
1998 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1999 */
Peter Huewe36d20412013-02-15 13:47:05 +01002000static struct logical_input *panel_bind_key(const char *name, const char *press,
2001 const char *repeat,
2002 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01002003{
2004 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002005
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002006 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002007 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01002008 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002009
Willy Tarreau698b1512008-11-22 11:29:33 +01002010 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002011 &scan_mask_o)) {
2012 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01002013 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002014 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002015
2016 key->type = INPUT_TYPE_KBD;
2017 key->state = INPUT_ST_LOW;
2018 key->rise_time = 1;
2019 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002020
Willy Tarreau698b1512008-11-22 11:29:33 +01002021 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2022 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2023 strncpy(key->u.kbd.release_str, release,
2024 sizeof(key->u.kbd.release_str));
2025 list_add(&key->list, &logical_inputs);
2026 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002027}
2028
Willy Tarreau630231772008-11-22 12:52:18 +01002029#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002030/* tries to bind a callback function to the signal name <name>. The function
2031 * <press_fct> will be called with the <press_data> arg when the signal is
2032 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002033 * Returns the pointer to the new signal if ok, NULL if the signal could not
2034 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002035 */
2036static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302037 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002038 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302039 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002040 int release_data)
2041{
2042 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002043
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002044 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002045 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002046 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002047
Willy Tarreau698b1512008-11-22 11:29:33 +01002048 memset(callback, 0, sizeof(struct logical_input));
2049 if (!input_name2mask(name, &callback->mask, &callback->value,
2050 &scan_mask_i, &scan_mask_o))
2051 return NULL;
2052
2053 callback->type = INPUT_TYPE_STD;
2054 callback->state = INPUT_ST_LOW;
2055 callback->rise_time = 1;
2056 callback->fall_time = 1;
2057 callback->u.std.press_fct = press_fct;
2058 callback->u.std.press_data = press_data;
2059 callback->u.std.release_fct = release_fct;
2060 callback->u.std.release_data = release_data;
2061 list_add(&callback->list, &logical_inputs);
2062 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002063}
Willy Tarreau630231772008-11-22 12:52:18 +01002064#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002065
Willy Tarreau698b1512008-11-22 11:29:33 +01002066static void keypad_init(void)
2067{
2068 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002069
Willy Tarreau698b1512008-11-22 11:29:33 +01002070 init_waitqueue_head(&keypad_read_wait);
2071 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002072
Willy Tarreau698b1512008-11-22 11:29:33 +01002073 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002074
Willy Tarreau698b1512008-11-22 11:29:33 +01002075 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2076 panel_bind_key(keypad_profile[keynum][0],
2077 keypad_profile[keynum][1],
2078 keypad_profile[keynum][2],
2079 keypad_profile[keynum][3]);
2080 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002081
Willy Tarreau698b1512008-11-22 11:29:33 +01002082 init_scan_timer();
2083 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002084}
2085
Willy Tarreau7005b582008-11-13 17:18:59 -08002086/**************************************************/
2087/* device initialization */
2088/**************************************************/
2089
Willy Tarreau698b1512008-11-22 11:29:33 +01002090static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2091 void *unused)
2092{
2093 if (lcd_enabled && lcd_initialized) {
2094 switch (code) {
2095 case SYS_DOWN:
2096 panel_lcd_print
2097 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2098 break;
2099 case SYS_HALT:
2100 panel_lcd_print
2101 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2102 break;
2103 case SYS_POWER_OFF:
2104 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2105 break;
2106 default:
2107 break;
2108 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002109 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002110 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002111}
2112
2113static struct notifier_block panel_notifier = {
2114 panel_notify_sys,
2115 NULL,
2116 0
2117};
2118
Willy Tarreau698b1512008-11-22 11:29:33 +01002119static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002120{
Willy Tarreau698b1512008-11-22 11:29:33 +01002121 if (port->number != parport)
2122 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002123
Willy Tarreau698b1512008-11-22 11:29:33 +01002124 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002125 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2126 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002127 return;
2128 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002129
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002130 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
Willy Tarreau698b1512008-11-22 11:29:33 +01002131 NULL,
2132 /*PARPORT_DEV_EXCL */
2133 0, (void *)&pprt);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002134 if (pprt == NULL) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002135 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2136 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002137 return;
2138 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002139
Willy Tarreau698b1512008-11-22 11:29:33 +01002140 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002141 pr_err("could not claim access to parport%d. Aborting.\n",
2142 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002143 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002144 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002145
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002146 /* must init LCD first, just in case an IRQ from the keypad is
2147 * generated at keypad init
2148 */
Willy Tarreau698b1512008-11-22 11:29:33 +01002149 if (lcd_enabled) {
2150 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002151 if (misc_register(&lcd_dev))
2152 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002153 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002154
Willy Tarreau698b1512008-11-22 11:29:33 +01002155 if (keypad_enabled) {
2156 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002157 if (misc_register(&keypad_dev))
2158 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002159 }
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002160 return;
2161
2162err_lcd_unreg:
2163 if (lcd_enabled)
2164 misc_deregister(&lcd_dev);
2165err_unreg_device:
2166 parport_unregister_device(pprt);
2167 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002168}
2169
Willy Tarreau698b1512008-11-22 11:29:33 +01002170static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002171{
Willy Tarreau698b1512008-11-22 11:29:33 +01002172 if (port->number != parport)
2173 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002174
Willy Tarreau698b1512008-11-22 11:29:33 +01002175 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002176 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2177 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002178 return;
2179 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002180
Peter Huewe0b0595b2009-09-29 01:22:40 +02002181 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002182 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002183 keypad_initialized = 0;
2184 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002185
Peter Huewe0b0595b2009-09-29 01:22:40 +02002186 if (lcd_enabled && lcd_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002187 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002188 lcd_initialized = 0;
2189 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002190
Willy Tarreau698b1512008-11-22 11:29:33 +01002191 parport_release(pprt);
2192 parport_unregister_device(pprt);
2193 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002194}
2195
2196static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002197 .name = "panel",
2198 .attach = panel_attach,
2199 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002200};
2201
2202/* init function */
Peter Huewe36d20412013-02-15 13:47:05 +01002203static int panel_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002204{
2205 /* for backwards compatibility */
2206 if (keypad_type < 0)
2207 keypad_type = keypad_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002208
Willy Tarreau698b1512008-11-22 11:29:33 +01002209 if (lcd_type < 0)
2210 lcd_type = lcd_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002211
Willy Tarreau698b1512008-11-22 11:29:33 +01002212 if (parport < 0)
2213 parport = DEFAULT_PARPORT;
Willy Tarreau7005b582008-11-13 17:18:59 -08002214
Willy Tarreau698b1512008-11-22 11:29:33 +01002215 /* take care of an eventual profile */
2216 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002217 case PANEL_PROFILE_CUSTOM:
2218 /* custom profile */
Willy Tarreau698b1512008-11-22 11:29:33 +01002219 if (keypad_type < 0)
2220 keypad_type = DEFAULT_KEYPAD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002221 if (lcd_type < 0)
2222 lcd_type = DEFAULT_LCD;
2223 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002224 case PANEL_PROFILE_OLD:
2225 /* 8 bits, 2*16, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002226 if (keypad_type < 0)
2227 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002228 if (lcd_type < 0)
2229 lcd_type = LCD_TYPE_OLD;
2230 if (lcd_width < 0)
2231 lcd_width = 16;
2232 if (lcd_hwidth < 0)
2233 lcd_hwidth = 16;
2234 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002235 case PANEL_PROFILE_NEW:
2236 /* serial, 2*16, new keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002237 if (keypad_type < 0)
2238 keypad_type = KEYPAD_TYPE_NEW;
Willy Tarreau698b1512008-11-22 11:29:33 +01002239 if (lcd_type < 0)
2240 lcd_type = LCD_TYPE_KS0074;
2241 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002242 case PANEL_PROFILE_HANTRONIX:
2243 /* 8 bits, 2*16 hantronix-like, no keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002244 if (keypad_type < 0)
2245 keypad_type = KEYPAD_TYPE_NONE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002246 if (lcd_type < 0)
2247 lcd_type = LCD_TYPE_HANTRONIX;
2248 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002249 case PANEL_PROFILE_NEXCOM:
2250 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Willy Tarreau698b1512008-11-22 11:29:33 +01002251 if (keypad_type < 0)
2252 keypad_type = KEYPAD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002253 if (lcd_type < 0)
2254 lcd_type = LCD_TYPE_NEXCOM;
2255 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002256 case PANEL_PROFILE_LARGE:
2257 /* 8 bits, 2*40, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002258 if (keypad_type < 0)
2259 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002260 if (lcd_type < 0)
2261 lcd_type = LCD_TYPE_OLD;
2262 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002263 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002264
Willy Tarreau698b1512008-11-22 11:29:33 +01002265 lcd_enabled = (lcd_type > 0);
2266 keypad_enabled = (keypad_type > 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08002267
Willy Tarreau698b1512008-11-22 11:29:33 +01002268 switch (keypad_type) {
2269 case KEYPAD_TYPE_OLD:
2270 keypad_profile = old_keypad_profile;
2271 break;
2272 case KEYPAD_TYPE_NEW:
2273 keypad_profile = new_keypad_profile;
2274 break;
2275 case KEYPAD_TYPE_NEXCOM:
2276 keypad_profile = nexcom_keypad_profile;
2277 break;
2278 default:
2279 keypad_profile = NULL;
2280 break;
2281 }
2282
2283 /* tells various subsystems about the fact that we are initializing */
2284 init_in_progress = 1;
2285
2286 if (parport_register_driver(&panel_driver)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002287 pr_err("could not register with parport. Aborting.\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002288 return -EIO;
2289 }
2290
Willy Tarreau630231772008-11-22 12:52:18 +01002291 if (!lcd_enabled && !keypad_enabled) {
2292 /* no device enabled, let's release the parport */
Willy Tarreau698b1512008-11-22 11:29:33 +01002293 if (pprt) {
2294 parport_release(pprt);
2295 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002296 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002297 }
2298 parport_unregister_driver(&panel_driver);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002299 pr_err("driver version " PANEL_VERSION " disabled.\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002300 return -ENODEV;
2301 }
2302
2303 register_reboot_notifier(&panel_notifier);
2304
2305 if (pprt)
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002306 pr_info("driver version " PANEL_VERSION
2307 " registered on parport%d (io=0x%lx).\n", parport,
2308 pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002309 else
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002310 pr_info("driver version " PANEL_VERSION
2311 " not yet registered\n");
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002312 /* tells various subsystems about the fact that initialization
2313 is finished */
Willy Tarreau698b1512008-11-22 11:29:33 +01002314 init_in_progress = 0;
2315 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002316}
2317
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002318static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002319{
2320 return panel_init();
Willy Tarreau7005b582008-11-13 17:18:59 -08002321}
2322
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002323static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002324{
2325 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002326
Willy Tarreau698b1512008-11-22 11:29:33 +01002327 if (scan_timer.function != NULL)
Julia Lawalle112f892014-03-26 22:33:41 +01002328 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002329
Costantino Leandro57898132009-02-17 11:10:48 -05002330 if (pprt != NULL) {
Peter Huewe0b0595b2009-09-29 01:22:40 +02002331 if (keypad_enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002332 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002333 keypad_initialized = 0;
2334 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002335
Costantino Leandro57898132009-02-17 11:10:48 -05002336 if (lcd_enabled) {
2337 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2338 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2339 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002340 lcd_initialized = 0;
Costantino Leandro57898132009-02-17 11:10:48 -05002341 }
2342
2343 /* TODO: free all input signals */
2344 parport_release(pprt);
2345 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002346 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002347 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002348 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002349}
Willy Tarreau7005b582008-11-13 17:18:59 -08002350
Willy Tarreau7005b582008-11-13 17:18:59 -08002351module_init(panel_init_module);
2352module_exit(panel_cleanup_module);
2353MODULE_AUTHOR("Willy Tarreau");
2354MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002355
2356/*
2357 * Local variables:
2358 * c-indent-level: 4
2359 * tab-width: 8
2360 * End:
2361 */