blob: eba04c0de7eb3f0d323d32f482a6ac1ff1e70d49 [file] [log] [blame]
Miguel Ojeda351f683b2018-02-17 20:33:13 +01001// SPDX-License-Identifier: GPL-2.0+
Willy Tarreau7005b582008-11-13 17:18:59 -08002/*
Willy Tarreau698b1512008-11-22 11:29:33 +01003 * Front panel driver for Linux
4 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01005 * Copyright (C) 2016-2017 Glider bvba
Willy Tarreau7005b582008-11-13 17:18:59 -08006 *
Willy Tarreau698b1512008-11-22 11:29:33 +01007 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
8 * connected to a parallel printer port.
Willy Tarreau7005b582008-11-13 17:18:59 -08009 *
Willy Tarreau698b1512008-11-22 11:29:33 +010010 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
11 * serial module compatible with Samsung's KS0074. The pins may be connected in
12 * any combination, everything is programmable.
Willy Tarreau7005b582008-11-13 17:18:59 -080013 *
Willy Tarreau698b1512008-11-22 11:29:33 +010014 * The keypad consists in a matrix of push buttons connecting input pins to
15 * data output pins or to the ground. The combinations have to be hard-coded
16 * in the driver, though several profiles exist and adding new ones is easy.
Willy Tarreau7005b582008-11-13 17:18:59 -080017 *
Willy Tarreau698b1512008-11-22 11:29:33 +010018 * Several profiles are provided for commonly found LCD+keypad modules on the
19 * market, such as those found in Nexcom's appliances.
Willy Tarreau7005b582008-11-13 17:18:59 -080020 *
21 * FIXME:
22 * - the initialization/deinitialization process is very dirty and should
23 * be rewritten. It may even be buggy.
24 *
25 * TODO:
26 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
27 * - make the LCD a part of a virtual screen of Vx*Vy
28 * - make the inputs list smp-safe
29 * - change the keyboard to a double mapping : signals -> key_id -> values
30 * so that applications can change values without knowing signals
31 *
32 */
33
Toshiaki Yamane493aa892012-07-12 22:01:00 +090034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Willy Tarreau7005b582008-11-13 17:18:59 -080036#include <linux/module.h>
37
38#include <linux/types.h>
39#include <linux/errno.h>
40#include <linux/signal.h>
41#include <linux/sched.h>
42#include <linux/spinlock.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080043#include <linux/interrupt.h>
44#include <linux/miscdevice.h>
Willy Tarreau698b1512008-11-22 11:29:33 +010045#include <linux/slab.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080046#include <linux/ioport.h>
47#include <linux/fcntl.h>
48#include <linux/init.h>
49#include <linux/delay.h>
Andy Shevchenkod85170e2010-07-22 19:57:08 +030050#include <linux/kernel.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080051#include <linux/ctype.h>
52#include <linux/parport.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080053#include <linux/list.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080054
Willy Tarreau698b1512008-11-22 11:29:33 +010055#include <linux/io.h>
Takanori Suzuki48f658b2010-05-08 22:56:24 +090056#include <linux/uaccess.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080057
Masahiro Yamada75354282019-08-06 16:14:44 +090058#include "charlcd.h"
Lars Poeschel718e05e2020-11-03 10:58:05 +010059#include "hd44780_common.h"
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +010060
Willy Tarreau7005b582008-11-13 17:18:59 -080061#define LCD_MAXBYTES 256 /* max burst write */
62
Willy Tarreau7005b582008-11-13 17:18:59 -080063#define KEYPAD_BUFFER 64
Willy Tarreau7005b582008-11-13 17:18:59 -080064
Henri Häkkinen429ccf02010-06-12 00:27:36 +030065/* poll the keyboard this every second */
Sirnam Swetha2b3c9eb2015-10-27 14:54:51 +053066#define INPUT_POLL_TIME (HZ / 50)
Henri Häkkinen429ccf02010-06-12 00:27:36 +030067/* a key starts to repeat after this times INPUT_POLL_TIME */
68#define KEYPAD_REP_START (10)
69/* a key repeats this times INPUT_POLL_TIME */
70#define KEYPAD_REP_DELAY (2)
71
Willy Tarreau7005b582008-11-13 17:18:59 -080072/* converts an r_str() input to an active high, bits string : 000BAOSE */
73#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
74
Willy Tarreau698b1512008-11-22 11:29:33 +010075#define PNL_PBUSY 0x80 /* inverted input, active low */
76#define PNL_PACK 0x40 /* direct input, active low */
77#define PNL_POUTPA 0x20 /* direct input, active high */
78#define PNL_PSELECD 0x10 /* direct input, active high */
79#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080080
Willy Tarreau698b1512008-11-22 11:29:33 +010081#define PNL_PBIDIR 0x20 /* bi-directional ports */
Henri Häkkinen429ccf02010-06-12 00:27:36 +030082/* high to read data in or-ed with data out */
83#define PNL_PINTEN 0x10
Willy Tarreau698b1512008-11-22 11:29:33 +010084#define PNL_PSELECP 0x08 /* inverted output, active low */
85#define PNL_PINITP 0x04 /* direct output, active low */
86#define PNL_PAUTOLF 0x02 /* inverted output, active low */
87#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080088
89#define PNL_PD0 0x01
90#define PNL_PD1 0x02
91#define PNL_PD2 0x04
92#define PNL_PD3 0x08
93#define PNL_PD4 0x10
94#define PNL_PD5 0x20
95#define PNL_PD6 0x40
96#define PNL_PD7 0x80
97
98#define PIN_NONE 0
99#define PIN_STROBE 1
100#define PIN_D0 2
101#define PIN_D1 3
102#define PIN_D2 4
103#define PIN_D3 5
104#define PIN_D4 6
105#define PIN_D5 7
106#define PIN_D6 8
107#define PIN_D7 9
108#define PIN_AUTOLF 14
109#define PIN_INITP 16
110#define PIN_SELECP 17
111#define PIN_NOT_SET 127
112
Mariusz Gorski36277d42014-11-27 22:36:47 +0100113#define NOT_SET -1
114
Willy Tarreau7005b582008-11-13 17:18:59 -0800115/* macros to simplify use of the parallel port */
116#define r_ctr(x) (parport_read_control((x)->port))
117#define r_dtr(x) (parport_read_data((x)->port))
118#define r_str(x) (parport_read_status((x)->port))
Toshiaki Yamane6ebb56d2012-07-25 12:18:12 +0900119#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
120#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
Willy Tarreau7005b582008-11-13 17:18:59 -0800121
122/* this defines which bits are to be used and which ones to be ignored */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300123/* logical or of the output bits involved in the scan matrix */
124static __u8 scan_mask_o;
125/* logical or of the input bits involved in the scan matrix */
126static __u8 scan_mask_i;
Willy Tarreau7005b582008-11-13 17:18:59 -0800127
Willy Tarreau7005b582008-11-13 17:18:59 -0800128enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100129 INPUT_TYPE_STD,
130 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800131};
132
133enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100134 INPUT_ST_LOW,
135 INPUT_ST_RISING,
136 INPUT_ST_HIGH,
137 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800138};
139
140struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100141 struct list_head list;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100142 __u64 mask;
143 __u64 value;
Willy Tarreau698b1512008-11-22 11:29:33 +0100144 enum input_type type;
145 enum input_state state;
146 __u8 rise_time, fall_time;
147 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800148
Willy Tarreau698b1512008-11-22 11:29:33 +0100149 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300150 struct { /* valid when type == INPUT_TYPE_STD */
Monam Agarwal68d386b2014-02-25 19:40:49 +0530151 void (*press_fct)(int);
152 void (*release_fct)(int);
Willy Tarreau698b1512008-11-22 11:29:33 +0100153 int press_data;
154 int release_data;
155 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300156 struct { /* valid when type == INPUT_TYPE_KBD */
Miguel Ojeda98cade02018-08-14 21:38:26 +0200157 char press_str[sizeof(void *) + sizeof(int)] __nonstring;
158 char repeat_str[sizeof(void *) + sizeof(int)] __nonstring;
159 char release_str[sizeof(void *) + sizeof(int)] __nonstring;
Willy Tarreau698b1512008-11-22 11:29:33 +0100160 } kbd;
161 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800162};
163
Peter Huewe36d20412013-02-15 13:47:05 +0100164static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800165
166/* physical contacts history
167 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
168 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
169 * corresponds to the ground.
170 * Within each group, bits are stored in the same order as read on the port :
171 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100172 * So, each __u64 is represented like this :
Willy Tarreau7005b582008-11-13 17:18:59 -0800173 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
174 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
175 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300176
177/* what has just been read from the I/O ports */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100178static __u64 phys_read;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300179/* previous phys_read */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100180static __u64 phys_read_prev;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300181/* stabilized phys_read (phys_read|phys_read_prev) */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100182static __u64 phys_curr;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300183/* previous phys_curr */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100184static __u64 phys_prev;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300185/* 0 means that at least one logical signal needs be computed */
186static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800187
Willy Tarreau7005b582008-11-13 17:18:59 -0800188/* these variables are specific to the keypad */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100189static struct {
190 bool enabled;
191} keypad;
192
Willy Tarreau7005b582008-11-13 17:18:59 -0800193static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100194static int keypad_buflen;
195static int keypad_start;
196static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800197static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800198
199/* lcd-specific variables */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100200static struct {
201 bool enabled;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100202 bool initialized;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100203
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100204 int charset;
205 int proto;
Geert Uytterhoevenfda4ae12017-02-06 15:38:10 +0100206
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100207 /* TODO: use union here? */
208 struct {
209 int e;
210 int rs;
211 int rw;
212 int cl;
213 int da;
214 int bl;
215 } pins;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100216
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100217 struct charlcd *charlcd;
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100218} lcd;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300219
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +0100220/* Needed only for init */
221static int selected_lcd_type = NOT_SET;
222
Willy Tarreau7005b582008-11-13 17:18:59 -0800223/*
224 * Bit masks to convert LCD signals to parallel port outputs.
225 * _d_ are values for data port, _c_ are for control port.
226 * [0] = signal OFF, [1] = signal ON, [2] = mask
227 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100228#define BIT_CLR 0
229#define BIT_SET 1
230#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800231#define BIT_STATES 3
232/*
233 * one entry for each bit on the LCD
234 */
235#define LCD_BIT_E 0
236#define LCD_BIT_RS 1
237#define LCD_BIT_RW 2
238#define LCD_BIT_BL 3
239#define LCD_BIT_CL 4
240#define LCD_BIT_DA 5
241#define LCD_BITS 6
242
243/*
244 * each bit can be either connected to a DATA or CTRL port
245 */
246#define LCD_PORT_C 0
247#define LCD_PORT_D 1
248#define LCD_PORTS 2
249
250static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
251
252/*
253 * LCD protocols
254 */
255#define LCD_PROTO_PARALLEL 0
256#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400257#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800258
259/*
260 * LCD character sets
261 */
262#define LCD_CHARSET_NORMAL 0
263#define LCD_CHARSET_KS0074 1
264
265/*
266 * LCD types
267 */
268#define LCD_TYPE_NONE 0
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530269#define LCD_TYPE_CUSTOM 1
270#define LCD_TYPE_OLD 2
271#define LCD_TYPE_KS0074 3
272#define LCD_TYPE_HANTRONIX 4
273#define LCD_TYPE_NEXCOM 5
Willy Tarreau7005b582008-11-13 17:18:59 -0800274
275/*
276 * keypad types
277 */
278#define KEYPAD_TYPE_NONE 0
279#define KEYPAD_TYPE_OLD 1
280#define KEYPAD_TYPE_NEW 2
281#define KEYPAD_TYPE_NEXCOM 3
282
283/*
284 * panel profiles
285 */
286#define PANEL_PROFILE_CUSTOM 0
287#define PANEL_PROFILE_OLD 1
288#define PANEL_PROFILE_NEW 2
289#define PANEL_PROFILE_HANTRONIX 3
290#define PANEL_PROFILE_NEXCOM 4
291#define PANEL_PROFILE_LARGE 5
292
293/*
294 * Construct custom config from the kernel's configuration
295 */
Willy Tarreau7005b582008-11-13 17:18:59 -0800296#define DEFAULT_PARPORT 0
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100297#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100298#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
299#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100300#define DEFAULT_LCD_HEIGHT 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800301#define DEFAULT_LCD_WIDTH 40
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100302#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
Willy Tarreau7005b582008-11-13 17:18:59 -0800303#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
304
305#define DEFAULT_LCD_PIN_E PIN_AUTOLF
306#define DEFAULT_LCD_PIN_RS PIN_SELECP
307#define DEFAULT_LCD_PIN_RW PIN_INITP
308#define DEFAULT_LCD_PIN_SCL PIN_STROBE
309#define DEFAULT_LCD_PIN_SDA PIN_D0
310#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
Willy Tarreau7005b582008-11-13 17:18:59 -0800311
Willy Tarreau7005b582008-11-13 17:18:59 -0800312#ifdef CONFIG_PANEL_PARPORT
313#undef DEFAULT_PARPORT
314#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
315#endif
316
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100317#ifdef CONFIG_PANEL_PROFILE
318#undef DEFAULT_PROFILE
319#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
320#endif
321
Willy Tarreau698b1512008-11-22 11:29:33 +0100322#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800323#ifdef CONFIG_PANEL_KEYPAD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100324#undef DEFAULT_KEYPAD_TYPE
325#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
Willy Tarreau7005b582008-11-13 17:18:59 -0800326#endif
327
Willy Tarreau7005b582008-11-13 17:18:59 -0800328#ifdef CONFIG_PANEL_LCD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100329#undef DEFAULT_LCD_TYPE
330#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
Willy Tarreau7005b582008-11-13 17:18:59 -0800331#endif
332
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100333#ifdef CONFIG_PANEL_LCD_HEIGHT
334#undef DEFAULT_LCD_HEIGHT
335#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
336#endif
337
Willy Tarreau7005b582008-11-13 17:18:59 -0800338#ifdef CONFIG_PANEL_LCD_WIDTH
339#undef DEFAULT_LCD_WIDTH
340#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
341#endif
342
343#ifdef CONFIG_PANEL_LCD_BWIDTH
344#undef DEFAULT_LCD_BWIDTH
345#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
346#endif
347
348#ifdef CONFIG_PANEL_LCD_HWIDTH
349#undef DEFAULT_LCD_HWIDTH
350#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
351#endif
352
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100353#ifdef CONFIG_PANEL_LCD_CHARSET
354#undef DEFAULT_LCD_CHARSET
355#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800356#endif
357
358#ifdef CONFIG_PANEL_LCD_PROTO
359#undef DEFAULT_LCD_PROTO
360#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
361#endif
362
363#ifdef CONFIG_PANEL_LCD_PIN_E
364#undef DEFAULT_LCD_PIN_E
365#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
366#endif
367
368#ifdef CONFIG_PANEL_LCD_PIN_RS
369#undef DEFAULT_LCD_PIN_RS
370#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
371#endif
372
373#ifdef CONFIG_PANEL_LCD_PIN_RW
374#undef DEFAULT_LCD_PIN_RW
375#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
376#endif
377
378#ifdef CONFIG_PANEL_LCD_PIN_SCL
379#undef DEFAULT_LCD_PIN_SCL
380#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
381#endif
382
383#ifdef CONFIG_PANEL_LCD_PIN_SDA
384#undef DEFAULT_LCD_PIN_SDA
385#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
386#endif
387
388#ifdef CONFIG_PANEL_LCD_PIN_BL
389#undef DEFAULT_LCD_PIN_BL
390#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
391#endif
392
Willy Tarreau7005b582008-11-13 17:18:59 -0800393#endif /* DEFAULT_PROFILE == 0 */
394
395/* global variables */
Mariusz Gorskif4757af2014-11-04 22:47:19 +0100396
397/* Device single-open policy control */
Mariusz Gorskif4757af2014-11-04 22:47:19 +0100398static atomic_t keypad_available = ATOMIC_INIT(1);
399
Willy Tarreau698b1512008-11-22 11:29:33 +0100400static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800401
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100402static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800403
Willy Tarreau698b1512008-11-22 11:29:33 +0100404static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800405static struct timer_list scan_timer;
406
Willy Tarreau630231772008-11-22 12:52:18 +0100407MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100408
Mariusz Gorski59a66a22014-11-27 22:36:45 +0100409static int parport = DEFAULT_PARPORT;
Willy Tarreau698b1512008-11-22 11:29:33 +0100410module_param(parport, int, 0000);
411MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100412
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100413static int profile = DEFAULT_PROFILE;
414module_param(profile, int, 0000);
415MODULE_PARM_DESC(profile,
416 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
417 "4=16x2 nexcom; default=40x2, old kp");
418
Mariusz Gorski36277d42014-11-27 22:36:47 +0100419static int keypad_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100420module_param(keypad_type, int, 0000);
421MODULE_PARM_DESC(keypad_type,
422 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
423
Mariusz Gorski36277d42014-11-27 22:36:47 +0100424static int lcd_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100425module_param(lcd_type, int, 0000);
426MODULE_PARM_DESC(lcd_type,
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530427 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100428
Mariusz Gorski36277d42014-11-27 22:36:47 +0100429static int lcd_height = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100430module_param(lcd_height, int, 0000);
431MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100432
Mariusz Gorski36277d42014-11-27 22:36:47 +0100433static int lcd_width = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100434module_param(lcd_width, int, 0000);
435MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100436
Mariusz Gorski36277d42014-11-27 22:36:47 +0100437static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100438module_param(lcd_bwidth, int, 0000);
439MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100440
Mariusz Gorski36277d42014-11-27 22:36:47 +0100441static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100442module_param(lcd_hwidth, int, 0000);
443MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100444
Mariusz Gorski36277d42014-11-27 22:36:47 +0100445static int lcd_charset = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100446module_param(lcd_charset, int, 0000);
447MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100448
Mariusz Gorski36277d42014-11-27 22:36:47 +0100449static int lcd_proto = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100450module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300451MODULE_PARM_DESC(lcd_proto,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +0200452 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100453
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100454/*
455 * These are the parallel port pins the LCD control signals are connected to.
456 * Set this to 0 if the signal is not used. Set it to its opposite value
457 * (negative) if the signal is negated. -MAXINT is used to indicate that the
458 * pin has not been explicitly specified.
459 *
Willy Tarreau630231772008-11-22 12:52:18 +0100460 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100461 */
462
463static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100464module_param(lcd_e_pin, int, 0000);
465MODULE_PARM_DESC(lcd_e_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530466 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100467
468static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100469module_param(lcd_rs_pin, int, 0000);
470MODULE_PARM_DESC(lcd_rs_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530471 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100472
473static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100474module_param(lcd_rw_pin, int, 0000);
475MODULE_PARM_DESC(lcd_rw_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530476 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100477
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100478static int lcd_cl_pin = PIN_NOT_SET;
479module_param(lcd_cl_pin, int, 0000);
480MODULE_PARM_DESC(lcd_cl_pin,
481 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100482
483static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100484module_param(lcd_da_pin, int, 0000);
485MODULE_PARM_DESC(lcd_da_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530486 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100487
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100488static int lcd_bl_pin = PIN_NOT_SET;
489module_param(lcd_bl_pin, int, 0000);
490MODULE_PARM_DESC(lcd_bl_pin,
491 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
492
493/* Deprecated module parameters - consider not using them anymore */
494
Mariusz Gorski36277d42014-11-27 22:36:47 +0100495static int lcd_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100496module_param(lcd_enabled, int, 0000);
497MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
498
Mariusz Gorski36277d42014-11-27 22:36:47 +0100499static int keypad_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100500module_param(keypad_enabled, int, 0000);
501MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
502
Willy Tarreau7005b582008-11-13 17:18:59 -0800503/* for some LCD drivers (ks0074) we need a charset conversion table. */
Peter Huewe36d20412013-02-15 13:47:05 +0100504static const unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100505 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
506 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
507 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
508 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
509 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
510 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
511 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
512 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
513 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
514 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
515 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
516 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
517 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
518 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
519 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
520 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
521 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
522 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
523 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
524 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
525 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
526 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
527 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
528 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
529 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
530 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
531 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
532 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
533 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
534 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
535 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
536 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
537 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800538};
539
Peter Huewe36d20412013-02-15 13:47:05 +0100540static const char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100541 {"S0", "Left\n", "Left\n", ""},
542 {"S1", "Down\n", "Down\n", ""},
543 {"S2", "Up\n", "Up\n", ""},
544 {"S3", "Right\n", "Right\n", ""},
545 {"S4", "Esc\n", "Esc\n", ""},
546 {"S5", "Ret\n", "Ret\n", ""},
547 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800548};
549
550/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100551static const char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100552 {"S0", "Left\n", "Left\n", ""},
553 {"S1", "Down\n", "Down\n", ""},
554 {"S2", "Up\n", "Up\n", ""},
555 {"S3", "Right\n", "Right\n", ""},
556 {"S4s5", "", "Esc\n", "Esc\n"},
557 {"s4S5", "", "Ret\n", "Ret\n"},
558 {"S4S5", "Help\n", "", ""},
559 /* add new signals above this line */
560 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800561};
562
563/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100564static const char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100565 {"a-p-e-", "Down\n", "Down\n", ""},
566 {"a-p-E-", "Ret\n", "Ret\n", ""},
567 {"a-P-E-", "Esc\n", "Esc\n", ""},
568 {"a-P-e-", "Up\n", "Up\n", ""},
569 /* add new signals above this line */
570 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800571};
572
Peter Huewe36d20412013-02-15 13:47:05 +0100573static const char (*keypad_profile)[4][9] = old_keypad_profile;
Willy Tarreau7005b582008-11-13 17:18:59 -0800574
Daniel Chromikbea74332016-02-11 13:29:23 +0100575static DECLARE_BITMAP(bits, LCD_BITS);
576
577static void lcd_get_bits(unsigned int port, int *val)
578{
579 unsigned int bit, state;
580
581 for (bit = 0; bit < LCD_BITS; bit++) {
582 state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
583 *val &= lcd_bits[port][bit][BIT_MSK];
584 *val |= lcd_bits[port][bit][state];
585 }
586}
Willy Tarreau7005b582008-11-13 17:18:59 -0800587
Willy Tarreau7005b582008-11-13 17:18:59 -0800588/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100589static int set_data_bits(void)
590{
Daniel Chromikbea74332016-02-11 13:29:23 +0100591 int val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800592
Willy Tarreau698b1512008-11-22 11:29:33 +0100593 val = r_dtr(pprt);
Daniel Chromikbea74332016-02-11 13:29:23 +0100594 lcd_get_bits(LCD_PORT_D, &val);
Willy Tarreau698b1512008-11-22 11:29:33 +0100595 w_dtr(pprt, val);
596 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800597}
598
599/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100600static int set_ctrl_bits(void)
601{
Daniel Chromikbea74332016-02-11 13:29:23 +0100602 int val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800603
Willy Tarreau698b1512008-11-22 11:29:33 +0100604 val = r_ctr(pprt);
Daniel Chromikbea74332016-02-11 13:29:23 +0100605 lcd_get_bits(LCD_PORT_C, &val);
Willy Tarreau698b1512008-11-22 11:29:33 +0100606 w_ctr(pprt, val);
607 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800608}
609
610/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530611static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100612{
613 set_data_bits();
614 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800615}
616
617/*
618 * Converts a parallel port pin (from -25 to 25) to data and control ports
619 * masks, and data and control port bits. The signal will be considered
620 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
621 *
622 * Result will be used this way :
623 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
624 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
625 */
Peter Huewe36d20412013-02-15 13:47:05 +0100626static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
Willy Tarreau698b1512008-11-22 11:29:33 +0100627{
628 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800629
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200630 d_val[0] = 0;
631 c_val[0] = 0;
632 d_val[1] = 0;
633 c_val[1] = 0;
634 d_val[2] = 0xFF;
635 c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800636
Willy Tarreau698b1512008-11-22 11:29:33 +0100637 if (pin == 0)
638 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800639
Willy Tarreau698b1512008-11-22 11:29:33 +0100640 inv = (pin < 0);
641 if (inv)
642 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800643
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200644 d_bit = 0;
645 c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800646
Willy Tarreau698b1512008-11-22 11:29:33 +0100647 switch (pin) {
648 case PIN_STROBE: /* strobe, inverted */
649 c_bit = PNL_PSTROBE;
650 inv = !inv;
651 break;
652 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
653 d_bit = 1 << (pin - 2);
654 break;
655 case PIN_AUTOLF: /* autofeed, inverted */
656 c_bit = PNL_PAUTOLF;
657 inv = !inv;
658 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300659 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100660 c_bit = PNL_PINITP;
661 break;
662 case PIN_SELECP: /* select_in, inverted */
663 c_bit = PNL_PSELECP;
664 inv = !inv;
665 break;
666 default: /* unknown pin, ignore */
667 break;
668 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800669
Willy Tarreau698b1512008-11-22 11:29:33 +0100670 if (c_bit) {
671 c_val[2] &= ~c_bit;
672 c_val[!inv] = c_bit;
673 } else if (d_bit) {
674 d_val[2] &= ~d_bit;
675 d_val[!inv] = d_bit;
676 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800677}
678
Alex Wilson881bf2812015-07-31 11:08:29 -0600679/*
680 * send a serial byte to the LCD panel. The caller is responsible for locking
681 * if needed.
682 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100683static void lcd_send_serial(int byte)
684{
685 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800686
Alex Wilson881bf2812015-07-31 11:08:29 -0600687 /*
688 * the data bit is set on D0, and the clock on STROBE.
689 * LCD reads D0 on STROBE's rising edge.
690 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100691 for (bit = 0; bit < 8; bit++) {
Daniel Chromikbea74332016-02-11 13:29:23 +0100692 clear_bit(LCD_BIT_CL, bits); /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530693 panel_set_bits();
Daniel Chromikbea74332016-02-11 13:29:23 +0100694 if (byte & 1) {
695 set_bit(LCD_BIT_DA, bits);
696 } else {
697 clear_bit(LCD_BIT_DA, bits);
698 }
699
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530700 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300701 udelay(2); /* maintain the data during 2 us before CLK up */
Daniel Chromikbea74332016-02-11 13:29:23 +0100702 set_bit(LCD_BIT_CL, bits); /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530703 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300704 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100705 byte >>= 1;
706 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800707}
708
709/* turn the backlight on or off */
Lars Poeschel66ce7d52020-11-03 10:58:04 +0100710static void lcd_backlight(struct charlcd *charlcd, enum charlcd_onoff on)
Willy Tarreau698b1512008-11-22 11:29:33 +0100711{
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100712 if (lcd.pins.bl == PIN_NONE)
713 return;
714
Justin P. Mattock6975e182012-03-19 08:17:52 -0700715 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800716 spin_lock_irq(&pprt_lock);
Daniel Chromikbea74332016-02-11 13:29:23 +0100717 if (on)
718 set_bit(LCD_BIT_BL, bits);
719 else
720 clear_bit(LCD_BIT_BL, bits);
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530721 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800722 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800723}
724
725/* send a command to the LCD panel in serial mode */
Lars Poeschel2c6a82f2020-11-03 10:58:09 +0100726static void lcd_write_cmd_s(struct hd44780_common *hdc, int cmd)
Willy Tarreau698b1512008-11-22 11:29:33 +0100727{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800728 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100729 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
730 lcd_send_serial(cmd & 0x0F);
731 lcd_send_serial((cmd >> 4) & 0x0F);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530732 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800733 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800734}
735
736/* send data to the LCD panel in serial mode */
Lars Poeschel71ff7012020-11-03 10:58:08 +0100737static void lcd_write_data_s(struct hd44780_common *hdc, int data)
Willy Tarreau698b1512008-11-22 11:29:33 +0100738{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800739 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100740 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
741 lcd_send_serial(data & 0x0F);
742 lcd_send_serial((data >> 4) & 0x0F);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530743 udelay(40); /* the shortest data takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800744 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800745}
746
747/* send a command to the LCD panel in 8 bits parallel mode */
Lars Poeschel2c6a82f2020-11-03 10:58:09 +0100748static void lcd_write_cmd_p8(struct hd44780_common *hdc, int cmd)
Willy Tarreau698b1512008-11-22 11:29:33 +0100749{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800750 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800751 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100752 w_dtr(pprt, cmd);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530753 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800754
Daniel Chromikbea74332016-02-11 13:29:23 +0100755 set_bit(LCD_BIT_E, bits);
756 clear_bit(LCD_BIT_RS, bits);
757 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau7005b582008-11-13 17:18:59 -0800758 set_ctrl_bits();
759
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530760 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800761
Daniel Chromikbea74332016-02-11 13:29:23 +0100762 clear_bit(LCD_BIT_E, bits);
Willy Tarreau7005b582008-11-13 17:18:59 -0800763 set_ctrl_bits();
764
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530765 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800766 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100767}
Willy Tarreau7005b582008-11-13 17:18:59 -0800768
Willy Tarreau698b1512008-11-22 11:29:33 +0100769/* send data to the LCD panel in 8 bits parallel mode */
Lars Poeschel71ff7012020-11-03 10:58:08 +0100770static void lcd_write_data_p8(struct hd44780_common *hdc, int data)
Willy Tarreau698b1512008-11-22 11:29:33 +0100771{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800772 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100773 /* present the data to the data port */
774 w_dtr(pprt, data);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530775 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100776
Daniel Chromikbea74332016-02-11 13:29:23 +0100777 set_bit(LCD_BIT_E, bits);
778 set_bit(LCD_BIT_RS, bits);
779 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100780 set_ctrl_bits();
781
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530782 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100783
Daniel Chromikbea74332016-02-11 13:29:23 +0100784 clear_bit(LCD_BIT_E, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100785 set_ctrl_bits();
786
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530787 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800788 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100789}
790
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400791/* send a command to the TI LCD panel */
Lars Poeschel2c6a82f2020-11-03 10:58:09 +0100792static void lcd_write_cmd_tilcd(struct hd44780_common *hdc, int cmd)
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400793{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800794 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400795 /* present the data to the control port */
796 w_ctr(pprt, cmd);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530797 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800798 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400799}
800
801/* send data to the TI LCD panel */
Lars Poeschel71ff7012020-11-03 10:58:08 +0100802static void lcd_write_data_tilcd(struct hd44780_common *hdc, int data)
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400803{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800804 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400805 /* present the data to the data port */
806 w_dtr(pprt, data);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530807 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800808 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400809}
810
Lars Poeschel351dcac2020-11-16 14:41:21 +0100811static const struct charlcd_ops charlcd_ops = {
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100812 .backlight = lcd_backlight,
Lars Poeschel32d917e2020-11-16 14:21:55 +0100813 .print = hd44780_common_print,
Lars Poescheld3a2fb82020-11-03 10:58:12 +0100814 .gotoxy = hd44780_common_gotoxy,
Lars Poeschel88645a82020-11-03 10:58:13 +0100815 .home = hd44780_common_home,
Lars Poeschel45421ff2020-11-03 10:58:14 +0100816 .clear_display = hd44780_common_clear_display,
Lars Poeschel01ec46d2020-11-03 10:58:17 +0100817 .init_display = hd44780_common_init_display,
Lars Poescheld2f21872020-11-03 10:58:18 +0100818 .shift_cursor = hd44780_common_shift_cursor,
819 .shift_display = hd44780_common_shift_display,
820 .display = hd44780_common_display,
821 .cursor = hd44780_common_cursor,
822 .blink = hd44780_common_blink,
823 .fontsize = hd44780_common_fontsize,
824 .lines = hd44780_common_lines,
Lars Poeschel339acb02020-11-03 10:58:20 +0100825 .redefine_char = hd44780_common_redefine_char,
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100826};
Willy Tarreau7005b582008-11-13 17:18:59 -0800827
Willy Tarreau7005b582008-11-13 17:18:59 -0800828/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +0100829static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100830{
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100831 struct charlcd *charlcd;
Lars Poeschel718e05e2020-11-03 10:58:05 +0100832 struct hd44780_common *hdc;
833
834 hdc = hd44780_common_alloc();
835 if (!hdc)
836 return;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100837
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100838 charlcd = charlcd_alloc();
Lars Poeschel718e05e2020-11-03 10:58:05 +0100839 if (!charlcd) {
840 kfree(hdc);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100841 return;
Lars Poeschel718e05e2020-11-03 10:58:05 +0100842 }
843
844 hdc->hd44780 = &lcd;
845 charlcd->drvdata = hdc;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100846
847 /*
848 * Init lcd struct with load-time values to preserve exact
849 * current functionality (at least for now).
850 */
851 charlcd->height = lcd_height;
852 charlcd->width = lcd_width;
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100853 hdc->bwidth = lcd_bwidth;
854 hdc->hwidth = lcd_hwidth;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100855
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +0100856 switch (selected_lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300857 case LCD_TYPE_OLD:
858 /* parallel mode, 8 bits */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100859 lcd.proto = LCD_PROTO_PARALLEL;
860 lcd.charset = LCD_CHARSET_NORMAL;
861 lcd.pins.e = PIN_STROBE;
862 lcd.pins.rs = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800863
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100864 charlcd->width = 40;
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100865 hdc->bwidth = 40;
866 hdc->hwidth = 64;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100867 charlcd->height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -0800868 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300869 case LCD_TYPE_KS0074:
870 /* serial mode, ks0074 */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100871 lcd.proto = LCD_PROTO_SERIAL;
872 lcd.charset = LCD_CHARSET_KS0074;
873 lcd.pins.bl = PIN_AUTOLF;
874 lcd.pins.cl = PIN_STROBE;
875 lcd.pins.da = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800876
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100877 charlcd->width = 16;
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100878 hdc->bwidth = 40;
879 hdc->hwidth = 16;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100880 charlcd->height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -0800881 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300882 case LCD_TYPE_NEXCOM:
883 /* parallel mode, 8 bits, generic */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100884 lcd.proto = LCD_PROTO_PARALLEL;
885 lcd.charset = LCD_CHARSET_NORMAL;
886 lcd.pins.e = PIN_AUTOLF;
887 lcd.pins.rs = PIN_SELECP;
888 lcd.pins.rw = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -0800889
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100890 charlcd->width = 16;
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100891 hdc->bwidth = 40;
892 hdc->hwidth = 64;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100893 charlcd->height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -0800894 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300895 case LCD_TYPE_CUSTOM:
896 /* customer-defined */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100897 lcd.proto = DEFAULT_LCD_PROTO;
898 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -0800899 /* default geometry will be set later */
900 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300901 case LCD_TYPE_HANTRONIX:
902 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +0100903 default:
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100904 lcd.proto = LCD_PROTO_PARALLEL;
905 lcd.charset = LCD_CHARSET_NORMAL;
906 lcd.pins.e = PIN_STROBE;
907 lcd.pins.rs = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -0800908
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100909 charlcd->width = 16;
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100910 hdc->bwidth = 40;
911 hdc->hwidth = 64;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100912 charlcd->height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -0800913 break;
Willy Tarreau698b1512008-11-22 11:29:33 +0100914 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800915
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100916 /* Overwrite with module params set on loading */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +0100917 if (lcd_height != NOT_SET)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100918 charlcd->height = lcd_height;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +0100919 if (lcd_width != NOT_SET)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100920 charlcd->width = lcd_width;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +0100921 if (lcd_bwidth != NOT_SET)
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100922 hdc->bwidth = lcd_bwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +0100923 if (lcd_hwidth != NOT_SET)
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100924 hdc->hwidth = lcd_hwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +0100925 if (lcd_charset != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100926 lcd.charset = lcd_charset;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +0100927 if (lcd_proto != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100928 lcd.proto = lcd_proto;
929 if (lcd_e_pin != PIN_NOT_SET)
930 lcd.pins.e = lcd_e_pin;
931 if (lcd_rs_pin != PIN_NOT_SET)
932 lcd.pins.rs = lcd_rs_pin;
933 if (lcd_rw_pin != PIN_NOT_SET)
934 lcd.pins.rw = lcd_rw_pin;
935 if (lcd_cl_pin != PIN_NOT_SET)
936 lcd.pins.cl = lcd_cl_pin;
937 if (lcd_da_pin != PIN_NOT_SET)
938 lcd.pins.da = lcd_da_pin;
939 if (lcd_bl_pin != PIN_NOT_SET)
940 lcd.pins.bl = lcd_bl_pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800941
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100942 /* this is used to catch wrong and default values */
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100943 if (charlcd->width <= 0)
944 charlcd->width = DEFAULT_LCD_WIDTH;
Lars Poeschel2545c1c2020-11-03 10:58:06 +0100945 if (hdc->bwidth <= 0)
946 hdc->bwidth = DEFAULT_LCD_BWIDTH;
947 if (hdc->hwidth <= 0)
948 hdc->hwidth = DEFAULT_LCD_HWIDTH;
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100949 if (charlcd->height <= 0)
950 charlcd->height = DEFAULT_LCD_HEIGHT;
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100951
952 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
Lars Poeschel351dcac2020-11-16 14:41:21 +0100953 charlcd->ops = &charlcd_ops;
Lars Poeschel71ff7012020-11-03 10:58:08 +0100954 hdc->write_data = lcd_write_data_s;
Lars Poeschel2c6a82f2020-11-03 10:58:09 +0100955 hdc->write_cmd = lcd_write_cmd_s;
Willy Tarreau7005b582008-11-13 17:18:59 -0800956
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100957 if (lcd.pins.cl == PIN_NOT_SET)
958 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
959 if (lcd.pins.da == PIN_NOT_SET)
960 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -0800961
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100962 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Lars Poeschel351dcac2020-11-16 14:41:21 +0100963 charlcd->ops = &charlcd_ops;
Lars Poeschel71ff7012020-11-03 10:58:08 +0100964 hdc->write_data = lcd_write_data_p8;
Lars Poeschel2c6a82f2020-11-03 10:58:09 +0100965 hdc->write_cmd = lcd_write_cmd_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -0800966
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100967 if (lcd.pins.e == PIN_NOT_SET)
968 lcd.pins.e = DEFAULT_LCD_PIN_E;
969 if (lcd.pins.rs == PIN_NOT_SET)
970 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
971 if (lcd.pins.rw == PIN_NOT_SET)
972 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400973 } else {
Lars Poeschel351dcac2020-11-16 14:41:21 +0100974 charlcd->ops = &charlcd_ops;
Lars Poeschel71ff7012020-11-03 10:58:08 +0100975 hdc->write_data = lcd_write_data_tilcd;
Lars Poeschel2c6a82f2020-11-03 10:58:09 +0100976 hdc->write_cmd = lcd_write_cmd_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +0100977 }
978
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100979 if (lcd.pins.bl == PIN_NOT_SET)
980 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
Willy Tarreau698b1512008-11-22 11:29:33 +0100981
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100982 if (lcd.pins.e == PIN_NOT_SET)
983 lcd.pins.e = PIN_NONE;
984 if (lcd.pins.rs == PIN_NOT_SET)
985 lcd.pins.rs = PIN_NONE;
986 if (lcd.pins.rw == PIN_NOT_SET)
987 lcd.pins.rw = PIN_NONE;
988 if (lcd.pins.bl == PIN_NOT_SET)
989 lcd.pins.bl = PIN_NONE;
990 if (lcd.pins.cl == PIN_NOT_SET)
991 lcd.pins.cl = PIN_NONE;
992 if (lcd.pins.da == PIN_NOT_SET)
993 lcd.pins.da = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -0800994
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100995 if (lcd.charset == NOT_SET)
996 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -0800997
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100998 if (lcd.charset == LCD_CHARSET_KS0074)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +0100999 charlcd->char_conv = lcd_char_conv_ks0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01001000 else
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001001 charlcd->char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001002
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001003 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
Willy Tarreau698b1512008-11-22 11:29:33 +01001004 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001005 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
Willy Tarreau698b1512008-11-22 11:29:33 +01001006 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001007 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
Willy Tarreau698b1512008-11-22 11:29:33 +01001008 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001009 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001010 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001011 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001012 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001013 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
Willy Tarreau698b1512008-11-22 11:29:33 +01001014 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001015
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001016 lcd.charlcd = charlcd;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001017 lcd.initialized = true;
Willy Tarreau7005b582008-11-13 17:18:59 -08001018}
1019
Willy Tarreau7005b582008-11-13 17:18:59 -08001020/*
1021 * These are the file operation function for user access to /dev/keypad
1022 */
1023
Willy Tarreau698b1512008-11-22 11:29:33 +01001024static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001025 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001026{
Willy Tarreau698b1512008-11-22 11:29:33 +01001027 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001028 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001029
Willy Tarreau698b1512008-11-22 11:29:33 +01001030 if (keypad_buflen == 0) {
1031 if (file->f_flags & O_NONBLOCK)
1032 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001033
Arnd Bergmann310df692014-01-02 13:07:35 +01001034 if (wait_event_interruptible(keypad_read_wait,
1035 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001036 return -EINTR;
1037 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001038
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001039 for (; count-- > 0 && (keypad_buflen > 0);
1040 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001041 put_user(keypad_buffer[keypad_start], tmp);
1042 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1043 }
1044 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001045
Willy Tarreau698b1512008-11-22 11:29:33 +01001046 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001047}
1048
Willy Tarreau698b1512008-11-22 11:29:33 +01001049static int keypad_open(struct inode *inode, struct file *file)
1050{
Willy Tarreau93dc1772017-09-07 15:37:30 +02001051 int ret;
Willy Tarreau7005b582008-11-13 17:18:59 -08001052
Willy Tarreau93dc1772017-09-07 15:37:30 +02001053 ret = -EBUSY;
1054 if (!atomic_dec_and_test(&keypad_available))
1055 goto fail; /* open only once at a time */
1056
1057 ret = -EPERM;
Willy Tarreau698b1512008-11-22 11:29:33 +01001058 if (file->f_mode & FMODE_WRITE) /* device is read-only */
Willy Tarreau93dc1772017-09-07 15:37:30 +02001059 goto fail;
Willy Tarreau7005b582008-11-13 17:18:59 -08001060
Willy Tarreau698b1512008-11-22 11:29:33 +01001061 keypad_buflen = 0; /* flush the buffer on opening */
Willy Tarreau698b1512008-11-22 11:29:33 +01001062 return 0;
Willy Tarreau93dc1772017-09-07 15:37:30 +02001063 fail:
1064 atomic_inc(&keypad_available);
1065 return ret;
Willy Tarreau7005b582008-11-13 17:18:59 -08001066}
1067
Willy Tarreau698b1512008-11-22 11:29:33 +01001068static int keypad_release(struct inode *inode, struct file *file)
1069{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001070 atomic_inc(&keypad_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001071 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001072}
1073
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001074static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001075 .read = keypad_read, /* read */
1076 .open = keypad_open, /* open */
1077 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001078 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001079};
1080
1081static struct miscdevice keypad_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001082 .minor = KEYPAD_MINOR,
1083 .name = "keypad",
1084 .fops = &keypad_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001085};
1086
Peter Huewe36d20412013-02-15 13:47:05 +01001087static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001088{
Willy Tarreau698b1512008-11-22 11:29:33 +01001089 /* send the key to the device only if a process is attached to it. */
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001090 if (!atomic_read(&keypad_available)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001091 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1092 keypad_buffer[(keypad_start + keypad_buflen++) %
1093 KEYPAD_BUFFER] = *string++;
1094 }
1095 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001096 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001097}
1098
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001099/* this function scans all the bits involving at least one logical signal,
1100 * and puts the results in the bitfield "phys_read" (one bit per established
1101 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001102 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001103 * Note: to debounce input signals, we will only consider as switched a signal
1104 * which is stable across 2 measures. Signals which are different between two
1105 * reads will be kept as they previously were in their logical form (phys_prev).
1106 * A signal which has just switched will have a 1 in
1107 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001108 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001109static void phys_scan_contacts(void)
1110{
1111 int bit, bitval;
1112 char oldval;
1113 char bitmask;
1114 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001115
Willy Tarreau698b1512008-11-22 11:29:33 +01001116 phys_prev = phys_curr;
1117 phys_read_prev = phys_read;
1118 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001119
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001120 /* keep track of old value, with all outputs disabled */
1121 oldval = r_dtr(pprt) | scan_mask_o;
1122 /* activate all keyboard outputs (active low) */
1123 w_dtr(pprt, oldval & ~scan_mask_o);
1124
1125 /* will have a 1 for each bit set to gnd */
1126 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1127 /* disable all matrix signals */
1128 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001129
Willy Tarreau698b1512008-11-22 11:29:33 +01001130 /* now that all outputs are cleared, the only active input bits are
1131 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001132 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001133
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001134 /* 1 for each grounded input */
1135 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1136
1137 /* grounded inputs are signals 40-44 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001138 phys_read |= (__u64)gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001139
Willy Tarreau698b1512008-11-22 11:29:33 +01001140 if (bitmask != gndmask) {
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301141 /*
1142 * since clearing the outputs changed some inputs, we know
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001143 * that some input signals are currently tied to some outputs.
1144 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001145 */
1146 for (bit = 0; bit < 8; bit++) {
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05301147 bitval = BIT(bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001148
1149 if (!(scan_mask_o & bitval)) /* skip unused bits */
1150 continue;
1151
1152 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1153 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001154 phys_read |= (__u64)bitmask << (5 * bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001155 }
1156 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001157 }
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301158 /*
1159 * this is easy: use old bits when they are flapping,
1160 * use new ones when stable
1161 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001162 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1163 (phys_read & ~(phys_read ^ phys_read_prev));
1164}
1165
1166static inline int input_state_high(struct logical_input *input)
1167{
1168#if 0
1169 /* FIXME:
1170 * this is an invalid test. It tries to catch
1171 * transitions from single-key to multiple-key, but
1172 * doesn't take into account the contacts polarity.
1173 * The only solution to the problem is to parse keys
1174 * from the most complex to the simplest combinations,
1175 * and mark them as 'caught' once a combination
1176 * matches, then unmatch it for all other ones.
1177 */
1178
1179 /* try to catch dangerous transitions cases :
1180 * someone adds a bit, so this signal was a false
1181 * positive resulting from a transition. We should
1182 * invalidate the signal immediately and not call the
1183 * release function.
1184 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1185 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001186 if (((phys_prev & input->mask) == input->value) &&
1187 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001188 input->state = INPUT_ST_LOW; /* invalidate */
1189 return 1;
1190 }
1191#endif
1192
1193 if ((phys_curr & input->mask) == input->value) {
1194 if ((input->type == INPUT_TYPE_STD) &&
1195 (input->high_timer == 0)) {
1196 input->high_timer++;
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001197 if (input->u.std.press_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001198 input->u.std.press_fct(input->u.std.press_data);
1199 } else if (input->type == INPUT_TYPE_KBD) {
1200 /* will turn on the light */
1201 keypressed = 1;
1202
1203 if (input->high_timer == 0) {
1204 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001205
Jake Champline6626de2013-05-04 11:21:17 -04001206 if (press_str[0]) {
1207 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001208
Jake Champline6626de2013-05-04 11:21:17 -04001209 keypad_send_key(press_str, s);
1210 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001211 }
1212
1213 if (input->u.kbd.repeat_str[0]) {
1214 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001215
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001216 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001217 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001218
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001219 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001220 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001221 }
1222 /* we will need to come back here soon */
1223 inputs_stable = 0;
1224 }
1225
1226 if (input->high_timer < 255)
1227 input->high_timer++;
1228 }
1229 return 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001230 }
Vincent Heuken083b3632014-07-10 03:34:28 -07001231
1232 /* else signal falling down. Let's fall through. */
1233 input->state = INPUT_ST_FALLING;
1234 input->fall_timer = 0;
1235
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001236 return 0;
1237}
1238
1239static inline void input_state_falling(struct logical_input *input)
1240{
1241#if 0
1242 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001243 if (((phys_prev & input->mask) == input->value) &&
1244 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001245 input->state = INPUT_ST_LOW; /* invalidate */
1246 return;
1247 }
1248#endif
1249
1250 if ((phys_curr & input->mask) == input->value) {
1251 if (input->type == INPUT_TYPE_KBD) {
1252 /* will turn on the light */
1253 keypressed = 1;
1254
1255 if (input->u.kbd.repeat_str[0]) {
1256 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001257
Jake Champline6626de2013-05-04 11:21:17 -04001258 if (input->high_timer >= KEYPAD_REP_START) {
1259 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001260
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001261 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001262 keypad_send_key(repeat_str, s);
1263 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001264 /* we will need to come back here soon */
1265 inputs_stable = 0;
1266 }
1267
1268 if (input->high_timer < 255)
1269 input->high_timer++;
1270 }
1271 input->state = INPUT_ST_HIGH;
1272 } else if (input->fall_timer >= input->fall_time) {
1273 /* call release event */
1274 if (input->type == INPUT_TYPE_STD) {
1275 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001276
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001277 if (release_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001278 release_fct(input->u.std.release_data);
1279 } else if (input->type == INPUT_TYPE_KBD) {
1280 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001281
Jake Champline6626de2013-05-04 11:21:17 -04001282 if (release_str[0]) {
1283 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001284
Jake Champline6626de2013-05-04 11:21:17 -04001285 keypad_send_key(release_str, s);
1286 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001287 }
1288
1289 input->state = INPUT_ST_LOW;
1290 } else {
1291 input->fall_timer++;
1292 inputs_stable = 0;
1293 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001294}
1295
Willy Tarreau698b1512008-11-22 11:29:33 +01001296static void panel_process_inputs(void)
1297{
Willy Tarreau698b1512008-11-22 11:29:33 +01001298 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001299
Willy Tarreau698b1512008-11-22 11:29:33 +01001300 keypressed = 0;
1301 inputs_stable = 1;
Wei Yongjun4654bdb2017-04-25 16:13:34 +00001302 list_for_each_entry(input, &logical_inputs, list) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001303 switch (input->state) {
1304 case INPUT_ST_LOW:
1305 if ((phys_curr & input->mask) != input->value)
1306 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001307 /* if all needed ones were already set previously,
1308 * this means that this logical signal has been
1309 * activated by the releasing of another combined
1310 * signal, so we don't want to match.
1311 * eg: AB -(release B)-> A -(release A)-> 0 :
1312 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001313 */
1314 if ((phys_prev & input->mask) == input->value)
1315 break;
1316 input->rise_timer = 0;
1317 input->state = INPUT_ST_RISING;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001318 fallthrough;
Willy Tarreau698b1512008-11-22 11:29:33 +01001319 case INPUT_ST_RISING:
1320 if ((phys_curr & input->mask) != input->value) {
1321 input->state = INPUT_ST_LOW;
1322 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001323 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001324 if (input->rise_timer < input->rise_time) {
1325 inputs_stable = 0;
1326 input->rise_timer++;
1327 break;
1328 }
1329 input->high_timer = 0;
1330 input->state = INPUT_ST_HIGH;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001331 fallthrough;
Willy Tarreau698b1512008-11-22 11:29:33 +01001332 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001333 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001334 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001335 fallthrough;
Willy Tarreau698b1512008-11-22 11:29:33 +01001336 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001337 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001338 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001339 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001340}
1341
Kees Cook607a6302017-10-19 14:08:22 -07001342static void panel_scan_timer(struct timer_list *unused)
Willy Tarreau698b1512008-11-22 11:29:33 +01001343{
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001344 if (keypad.enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001345 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001346 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001347
1348 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001349 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001350 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001351
Willy Tarreau698b1512008-11-22 11:29:33 +01001352 if (!inputs_stable || phys_curr != phys_prev)
1353 panel_process_inputs();
1354 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001355
Geert Uytterhoevenfda4ae12017-02-06 15:38:10 +01001356 if (keypressed && lcd.enabled && lcd.initialized)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001357 charlcd_poke(lcd.charlcd);
Willy Tarreau7005b582008-11-13 17:18:59 -08001358
Willy Tarreau698b1512008-11-22 11:29:33 +01001359 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001360}
1361
Willy Tarreau698b1512008-11-22 11:29:33 +01001362static void init_scan_timer(void)
1363{
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001364 if (scan_timer.function)
Willy Tarreau698b1512008-11-22 11:29:33 +01001365 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001366
Kees Cook607a6302017-10-19 14:08:22 -07001367 timer_setup(&scan_timer, panel_scan_timer, 0);
Willy Tarreau698b1512008-11-22 11:29:33 +01001368 scan_timer.expires = jiffies + INPUT_POLL_TIME;
Willy Tarreau698b1512008-11-22 11:29:33 +01001369 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001370}
1371
1372/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001373 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1374 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08001375 * returns 1 if ok, 0 if error (in which case, nothing is written).
1376 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001377static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01001378 u8 *imask, u8 *omask)
Willy Tarreau698b1512008-11-22 11:29:33 +01001379{
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01001380 const char sigtab[] = "EeSsPpAaBb";
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01001381 u8 im, om;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001382 __u64 m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08001383
Ksenija Stanojevicd12f27e2016-01-03 20:42:26 +01001384 om = 0;
1385 im = 0;
Dominique van den Broeck2d534262014-05-24 01:35:24 +02001386 m = 0ULL;
1387 v = 0ULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01001388 while (*name) {
1389 int in, out, bit, neg;
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01001390 const char *idx;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001391
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01001392 idx = strchr(sigtab, *name);
1393 if (!idx)
Willy Tarreau698b1512008-11-22 11:29:33 +01001394 return 0; /* input name not found */
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01001395
1396 in = idx - sigtab;
Willy Tarreau698b1512008-11-22 11:29:33 +01001397 neg = (in & 1); /* odd (lower) names are negated */
1398 in >>= 1;
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05301399 im |= BIT(in);
Willy Tarreau7005b582008-11-13 17:18:59 -08001400
Willy Tarreau698b1512008-11-22 11:29:33 +01001401 name++;
Ksenija Stanojevic52ebf932016-01-03 20:43:27 +01001402 if (*name >= '0' && *name <= '7') {
Willy Tarreau698b1512008-11-22 11:29:33 +01001403 out = *name - '0';
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05301404 om |= BIT(out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001405 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01001406 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001407 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01001408 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001409 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001410
1411 bit = (out * 5) + in;
1412
1413 m |= 1ULL << bit;
1414 if (!neg)
1415 v |= 1ULL << bit;
1416 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08001417 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001418 *mask = m;
1419 *value = v;
1420 if (imask)
1421 *imask |= im;
1422 if (omask)
1423 *omask |= om;
1424 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001425}
1426
1427/* tries to bind a key to the signal name <name>. The key will send the
1428 * strings <press>, <repeat>, <release> for these respective events.
1429 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1430 */
Peter Huewe36d20412013-02-15 13:47:05 +01001431static struct logical_input *panel_bind_key(const char *name, const char *press,
1432 const char *repeat,
1433 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01001434{
1435 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001436
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001437 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001438 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01001439 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001440
Willy Tarreau698b1512008-11-22 11:29:33 +01001441 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04001442 &scan_mask_o)) {
1443 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01001444 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04001445 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001446
1447 key->type = INPUT_TYPE_KBD;
1448 key->state = INPUT_ST_LOW;
1449 key->rise_time = 1;
1450 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001451
Willy Tarreau698b1512008-11-22 11:29:33 +01001452 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
1453 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
1454 strncpy(key->u.kbd.release_str, release,
1455 sizeof(key->u.kbd.release_str));
1456 list_add(&key->list, &logical_inputs);
1457 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001458}
1459
Willy Tarreau630231772008-11-22 12:52:18 +01001460#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08001461/* tries to bind a callback function to the signal name <name>. The function
1462 * <press_fct> will be called with the <press_data> arg when the signal is
1463 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001464 * Returns the pointer to the new signal if ok, NULL if the signal could not
1465 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08001466 */
1467static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05301468 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01001469 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05301470 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01001471 int release_data)
1472{
1473 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08001474
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001475 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001476 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01001477 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001478
Willy Tarreau698b1512008-11-22 11:29:33 +01001479 memset(callback, 0, sizeof(struct logical_input));
1480 if (!input_name2mask(name, &callback->mask, &callback->value,
1481 &scan_mask_i, &scan_mask_o))
1482 return NULL;
1483
1484 callback->type = INPUT_TYPE_STD;
1485 callback->state = INPUT_ST_LOW;
1486 callback->rise_time = 1;
1487 callback->fall_time = 1;
1488 callback->u.std.press_fct = press_fct;
1489 callback->u.std.press_data = press_data;
1490 callback->u.std.release_fct = release_fct;
1491 callback->u.std.release_data = release_data;
1492 list_add(&callback->list, &logical_inputs);
1493 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08001494}
Willy Tarreau630231772008-11-22 12:52:18 +01001495#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08001496
Willy Tarreau698b1512008-11-22 11:29:33 +01001497static void keypad_init(void)
1498{
1499 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001500
Willy Tarreau698b1512008-11-22 11:29:33 +01001501 init_waitqueue_head(&keypad_read_wait);
1502 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08001503
Willy Tarreau698b1512008-11-22 11:29:33 +01001504 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08001505
Willy Tarreau698b1512008-11-22 11:29:33 +01001506 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
1507 panel_bind_key(keypad_profile[keynum][0],
1508 keypad_profile[keynum][1],
1509 keypad_profile[keynum][2],
1510 keypad_profile[keynum][3]);
1511 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001512
Willy Tarreau698b1512008-11-22 11:29:33 +01001513 init_scan_timer();
1514 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001515}
1516
Willy Tarreau7005b582008-11-13 17:18:59 -08001517/**************************************************/
1518/* device initialization */
1519/**************************************************/
1520
Willy Tarreau698b1512008-11-22 11:29:33 +01001521static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08001522{
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05301523 struct pardev_cb panel_cb;
1524
Willy Tarreau698b1512008-11-22 11:29:33 +01001525 if (port->number != parport)
1526 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001527
Willy Tarreau698b1512008-11-22 11:29:33 +01001528 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001529 pr_err("%s: port->number=%d parport=%d, already registered!\n",
1530 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01001531 return;
1532 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001533
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05301534 memset(&panel_cb, 0, sizeof(panel_cb));
1535 panel_cb.private = &pprt;
1536 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
1537
1538 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001539 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001540 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
1541 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04001542 return;
1543 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001544
Willy Tarreau698b1512008-11-22 11:29:33 +01001545 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001546 pr_err("could not claim access to parport%d. Aborting.\n",
1547 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04001548 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01001549 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001550
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001551 /* must init LCD first, just in case an IRQ from the keypad is
1552 * generated at keypad init
1553 */
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001554 if (lcd.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001555 lcd_init();
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001556 if (!lcd.charlcd || charlcd_register(lcd.charlcd))
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04001557 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01001558 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001559
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001560 if (keypad.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001561 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04001562 if (misc_register(&keypad_dev))
1563 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01001564 }
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04001565 return;
1566
1567err_lcd_unreg:
zhengbinb33d5672019-07-08 20:42:18 +08001568 if (scan_timer.function)
1569 del_timer_sync(&scan_timer);
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001570 if (lcd.enabled)
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001571 charlcd_unregister(lcd.charlcd);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04001572err_unreg_device:
Lars Poeschel718e05e2020-11-03 10:58:05 +01001573 kfree(lcd.charlcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001574 lcd.charlcd = NULL;
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04001575 parport_unregister_device(pprt);
1576 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001577}
1578
Willy Tarreau698b1512008-11-22 11:29:33 +01001579static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08001580{
Willy Tarreau698b1512008-11-22 11:29:33 +01001581 if (port->number != parport)
1582 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001583
Willy Tarreau698b1512008-11-22 11:29:33 +01001584 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001585 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
1586 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01001587 return;
1588 }
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001589 if (scan_timer.function)
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05301590 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001591
Geert Uytterhoeven3f77b432017-02-06 15:38:08 +01001592 if (keypad.enabled) {
1593 misc_deregister(&keypad_dev);
1594 keypad_initialized = 0;
Peter Huewe0b0595b2009-09-29 01:22:40 +02001595 }
Geert Uytterhoeven3f77b432017-02-06 15:38:08 +01001596
1597 if (lcd.enabled) {
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001598 charlcd_unregister(lcd.charlcd);
Geert Uytterhoeven3f77b432017-02-06 15:38:08 +01001599 lcd.initialized = false;
Lars Poeschel718e05e2020-11-03 10:58:05 +01001600 kfree(lcd.charlcd->drvdata);
1601 kfree(lcd.charlcd);
Geert Uytterhoeven39f8ea42017-03-10 15:15:17 +01001602 lcd.charlcd = NULL;
Geert Uytterhoeven3f77b432017-02-06 15:38:08 +01001603 }
1604
1605 /* TODO: free all input signals */
1606 parport_release(pprt);
1607 parport_unregister_device(pprt);
1608 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001609}
1610
1611static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001612 .name = "panel",
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05301613 .match_port = panel_attach,
Willy Tarreau698b1512008-11-22 11:29:33 +01001614 .detach = panel_detach,
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05301615 .devmodel = true,
Willy Tarreau7005b582008-11-13 17:18:59 -08001616};
1617
1618/* init function */
Mariusz Gorskid9114762014-11-27 22:36:46 +01001619static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001620{
Sudip Mukherjeee1342012015-03-09 20:08:24 +05301621 int selected_keypad_type = NOT_SET, err;
Willy Tarreau7005b582008-11-13 17:18:59 -08001622
Willy Tarreau698b1512008-11-22 11:29:33 +01001623 /* take care of an eventual profile */
1624 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001625 case PANEL_PROFILE_CUSTOM:
1626 /* custom profile */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001627 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
1628 selected_lcd_type = DEFAULT_LCD_TYPE;
Willy Tarreau698b1512008-11-22 11:29:33 +01001629 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001630 case PANEL_PROFILE_OLD:
1631 /* 8 bits, 2*16, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001632 selected_keypad_type = KEYPAD_TYPE_OLD;
1633 selected_lcd_type = LCD_TYPE_OLD;
1634
1635 /* TODO: This two are a little hacky, sort it out later */
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01001636 if (lcd_width == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001637 lcd_width = 16;
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01001638 if (lcd_hwidth == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001639 lcd_hwidth = 16;
1640 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001641 case PANEL_PROFILE_NEW:
1642 /* serial, 2*16, new keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001643 selected_keypad_type = KEYPAD_TYPE_NEW;
1644 selected_lcd_type = LCD_TYPE_KS0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01001645 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001646 case PANEL_PROFILE_HANTRONIX:
1647 /* 8 bits, 2*16 hantronix-like, no keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001648 selected_keypad_type = KEYPAD_TYPE_NONE;
1649 selected_lcd_type = LCD_TYPE_HANTRONIX;
Willy Tarreau698b1512008-11-22 11:29:33 +01001650 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001651 case PANEL_PROFILE_NEXCOM:
1652 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001653 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
1654 selected_lcd_type = LCD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01001655 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001656 case PANEL_PROFILE_LARGE:
1657 /* 8 bits, 2*40, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001658 selected_keypad_type = KEYPAD_TYPE_OLD;
1659 selected_lcd_type = LCD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01001660 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001661 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001662
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001663 /*
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001664 * Overwrite selection with module param values (both keypad and lcd),
1665 * where the deprecated params have lower prio.
1666 */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001667 if (keypad_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001668 selected_keypad_type = keypad_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001669 if (keypad_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001670 selected_keypad_type = keypad_type;
Willy Tarreau7005b582008-11-13 17:18:59 -08001671
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001672 keypad.enabled = (selected_keypad_type > 0);
1673
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001674 if (lcd_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001675 selected_lcd_type = lcd_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001676 if (lcd_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001677 selected_lcd_type = lcd_type;
1678
1679 lcd.enabled = (selected_lcd_type > 0);
1680
Sudip Mukherjee733345e2015-02-11 16:57:08 +05301681 if (lcd.enabled) {
1682 /*
1683 * Init lcd struct with load-time values to preserve exact
1684 * current functionality (at least for now).
1685 */
Sudip Mukherjee733345e2015-02-11 16:57:08 +05301686 lcd.charset = lcd_charset;
1687 lcd.proto = lcd_proto;
1688 lcd.pins.e = lcd_e_pin;
1689 lcd.pins.rs = lcd_rs_pin;
1690 lcd.pins.rw = lcd_rw_pin;
1691 lcd.pins.cl = lcd_cl_pin;
1692 lcd.pins.da = lcd_da_pin;
1693 lcd.pins.bl = lcd_bl_pin;
Sudip Mukherjee733345e2015-02-11 16:57:08 +05301694 }
1695
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001696 switch (selected_keypad_type) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001697 case KEYPAD_TYPE_OLD:
1698 keypad_profile = old_keypad_profile;
1699 break;
1700 case KEYPAD_TYPE_NEW:
1701 keypad_profile = new_keypad_profile;
1702 break;
1703 case KEYPAD_TYPE_NEXCOM:
1704 keypad_profile = nexcom_keypad_profile;
1705 break;
1706 default:
1707 keypad_profile = NULL;
1708 break;
1709 }
1710
Sudip Mukherjeef43de772015-02-10 17:26:02 +05301711 if (!lcd.enabled && !keypad.enabled) {
1712 /* no device enabled, let's exit */
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01001713 pr_err("panel driver disabled.\n");
Sudip Mukherjeef43de772015-02-10 17:26:02 +05301714 return -ENODEV;
1715 }
1716
Sudip Mukherjeee1342012015-03-09 20:08:24 +05301717 err = parport_register_driver(&panel_driver);
1718 if (err) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09001719 pr_err("could not register with parport. Aborting.\n");
Sudip Mukherjeee1342012015-03-09 20:08:24 +05301720 return err;
Willy Tarreau698b1512008-11-22 11:29:33 +01001721 }
1722
Willy Tarreau698b1512008-11-22 11:29:33 +01001723 if (pprt)
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01001724 pr_info("panel driver registered on parport%d (io=0x%lx).\n",
1725 parport, pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01001726 else
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01001727 pr_info("panel driver not yet registered\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01001728 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001729}
1730
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01001731static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001732{
Willy Tarreau698b1512008-11-22 11:29:33 +01001733 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08001734}
Willy Tarreau7005b582008-11-13 17:18:59 -08001735
Willy Tarreau7005b582008-11-13 17:18:59 -08001736module_init(panel_init_module);
1737module_exit(panel_cleanup_module);
1738MODULE_AUTHOR("Willy Tarreau");
1739MODULE_LICENSE("GPL");