blob: 9c7b00951c22b1c27ec2b69b0843359f3e02def9 [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
Willy Tarreau7005b582008-11-13 17:18:59 -080067#define LCD_MAXBYTES 256 /* max burst write */
68
Willy Tarreau7005b582008-11-13 17:18:59 -080069#define KEYPAD_BUFFER 64
Willy Tarreau7005b582008-11-13 17:18:59 -080070
Henri Häkkinen429ccf02010-06-12 00:27:36 +030071/* poll the keyboard this every second */
Sirnam Swetha2b3c9eb2015-10-27 14:54:51 +053072#define INPUT_POLL_TIME (HZ / 50)
Henri Häkkinen429ccf02010-06-12 00:27:36 +030073/* a key starts to repeat after this times INPUT_POLL_TIME */
74#define KEYPAD_REP_START (10)
75/* a key repeats this times INPUT_POLL_TIME */
76#define KEYPAD_REP_DELAY (2)
77
78/* keep the light on this times INPUT_POLL_TIME for each flash */
79#define FLASH_LIGHT_TEMPO (200)
Willy Tarreau7005b582008-11-13 17:18:59 -080080
81/* converts an r_str() input to an active high, bits string : 000BAOSE */
82#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
83
Willy Tarreau698b1512008-11-22 11:29:33 +010084#define PNL_PBUSY 0x80 /* inverted input, active low */
85#define PNL_PACK 0x40 /* direct input, active low */
86#define PNL_POUTPA 0x20 /* direct input, active high */
87#define PNL_PSELECD 0x10 /* direct input, active high */
88#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080089
Willy Tarreau698b1512008-11-22 11:29:33 +010090#define PNL_PBIDIR 0x20 /* bi-directional ports */
Henri Häkkinen429ccf02010-06-12 00:27:36 +030091/* high to read data in or-ed with data out */
92#define PNL_PINTEN 0x10
Willy Tarreau698b1512008-11-22 11:29:33 +010093#define PNL_PSELECP 0x08 /* inverted output, active low */
94#define PNL_PINITP 0x04 /* direct output, active low */
95#define PNL_PAUTOLF 0x02 /* inverted output, active low */
96#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080097
98#define PNL_PD0 0x01
99#define PNL_PD1 0x02
100#define PNL_PD2 0x04
101#define PNL_PD3 0x08
102#define PNL_PD4 0x10
103#define PNL_PD5 0x20
104#define PNL_PD6 0x40
105#define PNL_PD7 0x80
106
107#define PIN_NONE 0
108#define PIN_STROBE 1
109#define PIN_D0 2
110#define PIN_D1 3
111#define PIN_D2 4
112#define PIN_D3 5
113#define PIN_D4 6
114#define PIN_D5 7
115#define PIN_D6 8
116#define PIN_D7 9
117#define PIN_AUTOLF 14
118#define PIN_INITP 16
119#define PIN_SELECP 17
120#define PIN_NOT_SET 127
121
Willy Tarreau7005b582008-11-13 17:18:59 -0800122#define LCD_FLAG_B 0x0004 /* blink on */
123#define LCD_FLAG_C 0x0008 /* cursor on */
124#define LCD_FLAG_D 0x0010 /* display on */
125#define LCD_FLAG_F 0x0020 /* large font mode */
126#define LCD_FLAG_N 0x0040 /* 2-rows mode */
127#define LCD_FLAG_L 0x0080 /* backlight enabled */
128
Mariusz Gorski21149242014-12-05 22:28:19 +0100129/* LCD commands */
130#define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
131
132#define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
133#define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
134
135#define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
136#define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
137#define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
138#define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
139
140#define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
141#define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
142#define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
143
144#define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
145#define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
146#define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
147#define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
148
149#define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
150
151#define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
152
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300153#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
Willy Tarreau7005b582008-11-13 17:18:59 -0800154#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
155
Mariusz Gorski36277d42014-11-27 22:36:47 +0100156#define NOT_SET -1
157
Willy Tarreau7005b582008-11-13 17:18:59 -0800158/* macros to simplify use of the parallel port */
159#define r_ctr(x) (parport_read_control((x)->port))
160#define r_dtr(x) (parport_read_data((x)->port))
161#define r_str(x) (parport_read_status((x)->port))
Toshiaki Yamane6ebb56d2012-07-25 12:18:12 +0900162#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
163#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
Willy Tarreau7005b582008-11-13 17:18:59 -0800164
165/* this defines which bits are to be used and which ones to be ignored */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300166/* logical or of the output bits involved in the scan matrix */
167static __u8 scan_mask_o;
168/* logical or of the input bits involved in the scan matrix */
169static __u8 scan_mask_i;
Willy Tarreau7005b582008-11-13 17:18:59 -0800170
Willy Tarreau7005b582008-11-13 17:18:59 -0800171enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100172 INPUT_TYPE_STD,
173 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800174};
175
176enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100177 INPUT_ST_LOW,
178 INPUT_ST_RISING,
179 INPUT_ST_HIGH,
180 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800181};
182
183struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100184 struct list_head list;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100185 __u64 mask;
186 __u64 value;
Willy Tarreau698b1512008-11-22 11:29:33 +0100187 enum input_type type;
188 enum input_state state;
189 __u8 rise_time, fall_time;
190 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800191
Willy Tarreau698b1512008-11-22 11:29:33 +0100192 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300193 struct { /* valid when type == INPUT_TYPE_STD */
Monam Agarwal68d386b2014-02-25 19:40:49 +0530194 void (*press_fct)(int);
195 void (*release_fct)(int);
Willy Tarreau698b1512008-11-22 11:29:33 +0100196 int press_data;
197 int release_data;
198 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300199 struct { /* valid when type == INPUT_TYPE_KBD */
200 /* strings can be non null-terminated */
Willy Tarreau698b1512008-11-22 11:29:33 +0100201 char press_str[sizeof(void *) + sizeof(int)];
202 char repeat_str[sizeof(void *) + sizeof(int)];
203 char release_str[sizeof(void *) + sizeof(int)];
204 } kbd;
205 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800206};
207
Peter Huewe36d20412013-02-15 13:47:05 +0100208static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800209
210/* physical contacts history
211 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
212 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
213 * corresponds to the ground.
214 * Within each group, bits are stored in the same order as read on the port :
215 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100216 * So, each __u64 is represented like this :
Willy Tarreau7005b582008-11-13 17:18:59 -0800217 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
218 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
219 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300220
221/* what has just been read from the I/O ports */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100222static __u64 phys_read;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300223/* previous phys_read */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100224static __u64 phys_read_prev;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300225/* stabilized phys_read (phys_read|phys_read_prev) */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100226static __u64 phys_curr;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300227/* previous phys_curr */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100228static __u64 phys_prev;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300229/* 0 means that at least one logical signal needs be computed */
230static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800231
Willy Tarreau7005b582008-11-13 17:18:59 -0800232/* these variables are specific to the keypad */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100233static struct {
234 bool enabled;
235} keypad;
236
Willy Tarreau7005b582008-11-13 17:18:59 -0800237static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100238static int keypad_buflen;
239static int keypad_start;
240static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800241static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800242
243/* lcd-specific variables */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100244static struct {
245 bool enabled;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100246 bool initialized;
247 bool must_clear;
248
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100249 int height;
250 int width;
251 int bwidth;
252 int hwidth;
253 int charset;
254 int proto;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100255 int light_tempo;
256
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100257 /* TODO: use union here? */
258 struct {
259 int e;
260 int rs;
261 int rw;
262 int cl;
263 int da;
264 int bl;
265 } pins;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100266
267 /* contains the LCD config state */
268 unsigned long int flags;
269
270 /* Contains the LCD X and Y offset */
271 struct {
272 unsigned long int x;
273 unsigned long int y;
274 } addr;
275
276 /* Current escape sequence and it's length or -1 if outside */
277 struct {
278 char buf[LCD_ESCAPE_LEN + 1];
279 int len;
280 } esc_seq;
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100281} lcd;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300282
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +0100283/* Needed only for init */
284static int selected_lcd_type = NOT_SET;
285
Willy Tarreau7005b582008-11-13 17:18:59 -0800286/*
287 * Bit masks to convert LCD signals to parallel port outputs.
288 * _d_ are values for data port, _c_ are for control port.
289 * [0] = signal OFF, [1] = signal ON, [2] = mask
290 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100291#define BIT_CLR 0
292#define BIT_SET 1
293#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800294#define BIT_STATES 3
295/*
296 * one entry for each bit on the LCD
297 */
298#define LCD_BIT_E 0
299#define LCD_BIT_RS 1
300#define LCD_BIT_RW 2
301#define LCD_BIT_BL 3
302#define LCD_BIT_CL 4
303#define LCD_BIT_DA 5
304#define LCD_BITS 6
305
306/*
307 * each bit can be either connected to a DATA or CTRL port
308 */
309#define LCD_PORT_C 0
310#define LCD_PORT_D 1
311#define LCD_PORTS 2
312
313static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
314
315/*
316 * LCD protocols
317 */
318#define LCD_PROTO_PARALLEL 0
319#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400320#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800321
322/*
323 * LCD character sets
324 */
325#define LCD_CHARSET_NORMAL 0
326#define LCD_CHARSET_KS0074 1
327
328/*
329 * LCD types
330 */
331#define LCD_TYPE_NONE 0
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530332#define LCD_TYPE_CUSTOM 1
333#define LCD_TYPE_OLD 2
334#define LCD_TYPE_KS0074 3
335#define LCD_TYPE_HANTRONIX 4
336#define LCD_TYPE_NEXCOM 5
Willy Tarreau7005b582008-11-13 17:18:59 -0800337
338/*
339 * keypad types
340 */
341#define KEYPAD_TYPE_NONE 0
342#define KEYPAD_TYPE_OLD 1
343#define KEYPAD_TYPE_NEW 2
344#define KEYPAD_TYPE_NEXCOM 3
345
346/*
347 * panel profiles
348 */
349#define PANEL_PROFILE_CUSTOM 0
350#define PANEL_PROFILE_OLD 1
351#define PANEL_PROFILE_NEW 2
352#define PANEL_PROFILE_HANTRONIX 3
353#define PANEL_PROFILE_NEXCOM 4
354#define PANEL_PROFILE_LARGE 5
355
356/*
357 * Construct custom config from the kernel's configuration
358 */
Willy Tarreau7005b582008-11-13 17:18:59 -0800359#define DEFAULT_PARPORT 0
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100360#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100361#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
362#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100363#define DEFAULT_LCD_HEIGHT 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800364#define DEFAULT_LCD_WIDTH 40
365#define DEFAULT_LCD_BWIDTH 40
366#define DEFAULT_LCD_HWIDTH 64
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100367#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
Willy Tarreau7005b582008-11-13 17:18:59 -0800368#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
369
370#define DEFAULT_LCD_PIN_E PIN_AUTOLF
371#define DEFAULT_LCD_PIN_RS PIN_SELECP
372#define DEFAULT_LCD_PIN_RW PIN_INITP
373#define DEFAULT_LCD_PIN_SCL PIN_STROBE
374#define DEFAULT_LCD_PIN_SDA PIN_D0
375#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
Willy Tarreau7005b582008-11-13 17:18:59 -0800376
Willy Tarreau7005b582008-11-13 17:18:59 -0800377#ifdef CONFIG_PANEL_PARPORT
378#undef DEFAULT_PARPORT
379#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
380#endif
381
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100382#ifdef CONFIG_PANEL_PROFILE
383#undef DEFAULT_PROFILE
384#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
385#endif
386
Willy Tarreau698b1512008-11-22 11:29:33 +0100387#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800388#ifdef CONFIG_PANEL_KEYPAD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100389#undef DEFAULT_KEYPAD_TYPE
390#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
Willy Tarreau7005b582008-11-13 17:18:59 -0800391#endif
392
Willy Tarreau7005b582008-11-13 17:18:59 -0800393#ifdef CONFIG_PANEL_LCD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100394#undef DEFAULT_LCD_TYPE
395#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
Willy Tarreau7005b582008-11-13 17:18:59 -0800396#endif
397
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100398#ifdef CONFIG_PANEL_LCD_HEIGHT
399#undef DEFAULT_LCD_HEIGHT
400#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
401#endif
402
Willy Tarreau7005b582008-11-13 17:18:59 -0800403#ifdef CONFIG_PANEL_LCD_WIDTH
404#undef DEFAULT_LCD_WIDTH
405#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
406#endif
407
408#ifdef CONFIG_PANEL_LCD_BWIDTH
409#undef DEFAULT_LCD_BWIDTH
410#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
411#endif
412
413#ifdef CONFIG_PANEL_LCD_HWIDTH
414#undef DEFAULT_LCD_HWIDTH
415#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
416#endif
417
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100418#ifdef CONFIG_PANEL_LCD_CHARSET
419#undef DEFAULT_LCD_CHARSET
420#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800421#endif
422
423#ifdef CONFIG_PANEL_LCD_PROTO
424#undef DEFAULT_LCD_PROTO
425#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
426#endif
427
428#ifdef CONFIG_PANEL_LCD_PIN_E
429#undef DEFAULT_LCD_PIN_E
430#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
431#endif
432
433#ifdef CONFIG_PANEL_LCD_PIN_RS
434#undef DEFAULT_LCD_PIN_RS
435#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
436#endif
437
438#ifdef CONFIG_PANEL_LCD_PIN_RW
439#undef DEFAULT_LCD_PIN_RW
440#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
441#endif
442
443#ifdef CONFIG_PANEL_LCD_PIN_SCL
444#undef DEFAULT_LCD_PIN_SCL
445#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
446#endif
447
448#ifdef CONFIG_PANEL_LCD_PIN_SDA
449#undef DEFAULT_LCD_PIN_SDA
450#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
451#endif
452
453#ifdef CONFIG_PANEL_LCD_PIN_BL
454#undef DEFAULT_LCD_PIN_BL
455#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
456#endif
457
Willy Tarreau7005b582008-11-13 17:18:59 -0800458#endif /* DEFAULT_PROFILE == 0 */
459
460/* global variables */
Mariusz Gorskif4757af2014-11-04 22:47:19 +0100461
462/* Device single-open policy control */
463static atomic_t lcd_available = ATOMIC_INIT(1);
464static atomic_t keypad_available = ATOMIC_INIT(1);
465
Willy Tarreau698b1512008-11-22 11:29:33 +0100466static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800467
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100468static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800469
Monam Agarwal68d386b2014-02-25 19:40:49 +0530470static void (*lcd_write_cmd)(int);
471static void (*lcd_write_data)(int);
472static void (*lcd_clear_fast)(void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800473
Willy Tarreau698b1512008-11-22 11:29:33 +0100474static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800475static struct timer_list scan_timer;
476
Willy Tarreau630231772008-11-22 12:52:18 +0100477MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100478
Mariusz Gorski59a66a22014-11-27 22:36:45 +0100479static int parport = DEFAULT_PARPORT;
Willy Tarreau698b1512008-11-22 11:29:33 +0100480module_param(parport, int, 0000);
481MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100482
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100483static int profile = DEFAULT_PROFILE;
484module_param(profile, int, 0000);
485MODULE_PARM_DESC(profile,
486 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
487 "4=16x2 nexcom; default=40x2, old kp");
488
Mariusz Gorski36277d42014-11-27 22:36:47 +0100489static int keypad_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100490module_param(keypad_type, int, 0000);
491MODULE_PARM_DESC(keypad_type,
492 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
493
Mariusz Gorski36277d42014-11-27 22:36:47 +0100494static int lcd_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100495module_param(lcd_type, int, 0000);
496MODULE_PARM_DESC(lcd_type,
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530497 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100498
Mariusz Gorski36277d42014-11-27 22:36:47 +0100499static int lcd_height = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100500module_param(lcd_height, int, 0000);
501MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100502
Mariusz Gorski36277d42014-11-27 22:36:47 +0100503static int lcd_width = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100504module_param(lcd_width, int, 0000);
505MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100506
Mariusz Gorski36277d42014-11-27 22:36:47 +0100507static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100508module_param(lcd_bwidth, int, 0000);
509MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100510
Mariusz Gorski36277d42014-11-27 22:36:47 +0100511static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100512module_param(lcd_hwidth, int, 0000);
513MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100514
Mariusz Gorski36277d42014-11-27 22:36:47 +0100515static int lcd_charset = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100516module_param(lcd_charset, int, 0000);
517MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100518
Mariusz Gorski36277d42014-11-27 22:36:47 +0100519static int lcd_proto = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100520module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300521MODULE_PARM_DESC(lcd_proto,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +0200522 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100523
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100524/*
525 * These are the parallel port pins the LCD control signals are connected to.
526 * Set this to 0 if the signal is not used. Set it to its opposite value
527 * (negative) if the signal is negated. -MAXINT is used to indicate that the
528 * pin has not been explicitly specified.
529 *
Willy Tarreau630231772008-11-22 12:52:18 +0100530 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100531 */
532
533static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100534module_param(lcd_e_pin, int, 0000);
535MODULE_PARM_DESC(lcd_e_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530536 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100537
538static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100539module_param(lcd_rs_pin, int, 0000);
540MODULE_PARM_DESC(lcd_rs_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530541 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100542
543static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100544module_param(lcd_rw_pin, int, 0000);
545MODULE_PARM_DESC(lcd_rw_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530546 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100547
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100548static int lcd_cl_pin = PIN_NOT_SET;
549module_param(lcd_cl_pin, int, 0000);
550MODULE_PARM_DESC(lcd_cl_pin,
551 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100552
553static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100554module_param(lcd_da_pin, int, 0000);
555MODULE_PARM_DESC(lcd_da_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530556 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100557
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100558static int lcd_bl_pin = PIN_NOT_SET;
559module_param(lcd_bl_pin, int, 0000);
560MODULE_PARM_DESC(lcd_bl_pin,
561 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
562
563/* Deprecated module parameters - consider not using them anymore */
564
Mariusz Gorski36277d42014-11-27 22:36:47 +0100565static int lcd_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100566module_param(lcd_enabled, int, 0000);
567MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
568
Mariusz Gorski36277d42014-11-27 22:36:47 +0100569static int keypad_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100570module_param(keypad_enabled, int, 0000);
571MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
572
Peter Huewe36d20412013-02-15 13:47:05 +0100573static const unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800574
575/* for some LCD drivers (ks0074) we need a charset conversion table. */
Peter Huewe36d20412013-02-15 13:47:05 +0100576static const unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100577 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
578 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
579 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
580 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
581 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
582 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
583 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
584 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
585 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
586 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
587 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
588 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
589 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
590 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
591 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
592 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
593 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
594 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
595 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
596 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
597 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
598 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
599 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
600 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
601 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
602 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
603 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
604 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
605 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
606 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
607 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
608 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
609 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800610};
611
Peter Huewe36d20412013-02-15 13:47:05 +0100612static const char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100613 {"S0", "Left\n", "Left\n", ""},
614 {"S1", "Down\n", "Down\n", ""},
615 {"S2", "Up\n", "Up\n", ""},
616 {"S3", "Right\n", "Right\n", ""},
617 {"S4", "Esc\n", "Esc\n", ""},
618 {"S5", "Ret\n", "Ret\n", ""},
619 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800620};
621
622/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100623static const char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100624 {"S0", "Left\n", "Left\n", ""},
625 {"S1", "Down\n", "Down\n", ""},
626 {"S2", "Up\n", "Up\n", ""},
627 {"S3", "Right\n", "Right\n", ""},
628 {"S4s5", "", "Esc\n", "Esc\n"},
629 {"s4S5", "", "Ret\n", "Ret\n"},
630 {"S4S5", "Help\n", "", ""},
631 /* add new signals above this line */
632 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800633};
634
635/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100636static const char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100637 {"a-p-e-", "Down\n", "Down\n", ""},
638 {"a-p-E-", "Ret\n", "Ret\n", ""},
639 {"a-P-E-", "Esc\n", "Esc\n", ""},
640 {"a-P-e-", "Up\n", "Up\n", ""},
641 /* add new signals above this line */
642 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800643};
644
Peter Huewe36d20412013-02-15 13:47:05 +0100645static const char (*keypad_profile)[4][9] = old_keypad_profile;
Willy Tarreau7005b582008-11-13 17:18:59 -0800646
Daniel Chromikbea74332016-02-11 13:29:23 +0100647static DECLARE_BITMAP(bits, LCD_BITS);
648
649static void lcd_get_bits(unsigned int port, int *val)
650{
651 unsigned int bit, state;
652
653 for (bit = 0; bit < LCD_BITS; bit++) {
654 state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
655 *val &= lcd_bits[port][bit][BIT_MSK];
656 *val |= lcd_bits[port][bit][state];
657 }
658}
Willy Tarreau7005b582008-11-13 17:18:59 -0800659
660static void init_scan_timer(void);
661
662/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100663static int set_data_bits(void)
664{
Daniel Chromikbea74332016-02-11 13:29:23 +0100665 int val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800666
Willy Tarreau698b1512008-11-22 11:29:33 +0100667 val = r_dtr(pprt);
Daniel Chromikbea74332016-02-11 13:29:23 +0100668 lcd_get_bits(LCD_PORT_D, &val);
Willy Tarreau698b1512008-11-22 11:29:33 +0100669 w_dtr(pprt, val);
670 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800671}
672
673/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100674static int set_ctrl_bits(void)
675{
Daniel Chromikbea74332016-02-11 13:29:23 +0100676 int val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800677
Willy Tarreau698b1512008-11-22 11:29:33 +0100678 val = r_ctr(pprt);
Daniel Chromikbea74332016-02-11 13:29:23 +0100679 lcd_get_bits(LCD_PORT_C, &val);
Willy Tarreau698b1512008-11-22 11:29:33 +0100680 w_ctr(pprt, val);
681 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800682}
683
684/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530685static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100686{
687 set_data_bits();
688 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800689}
690
691/*
692 * Converts a parallel port pin (from -25 to 25) to data and control ports
693 * masks, and data and control port bits. The signal will be considered
694 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
695 *
696 * Result will be used this way :
697 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
698 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
699 */
Peter Huewe36d20412013-02-15 13:47:05 +0100700static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
Willy Tarreau698b1512008-11-22 11:29:33 +0100701{
702 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800703
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200704 d_val[0] = 0;
705 c_val[0] = 0;
706 d_val[1] = 0;
707 c_val[1] = 0;
708 d_val[2] = 0xFF;
709 c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800710
Willy Tarreau698b1512008-11-22 11:29:33 +0100711 if (pin == 0)
712 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800713
Willy Tarreau698b1512008-11-22 11:29:33 +0100714 inv = (pin < 0);
715 if (inv)
716 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800717
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200718 d_bit = 0;
719 c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800720
Willy Tarreau698b1512008-11-22 11:29:33 +0100721 switch (pin) {
722 case PIN_STROBE: /* strobe, inverted */
723 c_bit = PNL_PSTROBE;
724 inv = !inv;
725 break;
726 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
727 d_bit = 1 << (pin - 2);
728 break;
729 case PIN_AUTOLF: /* autofeed, inverted */
730 c_bit = PNL_PAUTOLF;
731 inv = !inv;
732 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300733 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100734 c_bit = PNL_PINITP;
735 break;
736 case PIN_SELECP: /* select_in, inverted */
737 c_bit = PNL_PSELECP;
738 inv = !inv;
739 break;
740 default: /* unknown pin, ignore */
741 break;
742 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800743
Willy Tarreau698b1512008-11-22 11:29:33 +0100744 if (c_bit) {
745 c_val[2] &= ~c_bit;
746 c_val[!inv] = c_bit;
747 } else if (d_bit) {
748 d_val[2] &= ~d_bit;
749 d_val[!inv] = d_bit;
750 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800751}
752
753/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100754static void long_sleep(int ms)
755{
Nicholas Mc Guire895875a2015-05-29 19:02:32 +0200756 if (in_interrupt())
Willy Tarreau698b1512008-11-22 11:29:33 +0100757 mdelay(ms);
Nicholas Mc Guire895875a2015-05-29 19:02:32 +0200758 else
759 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Willy Tarreau7005b582008-11-13 17:18:59 -0800760}
761
Alex Wilson881bf2812015-07-31 11:08:29 -0600762/*
763 * send a serial byte to the LCD panel. The caller is responsible for locking
764 * if needed.
765 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100766static void lcd_send_serial(int byte)
767{
768 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800769
Alex Wilson881bf2812015-07-31 11:08:29 -0600770 /*
771 * the data bit is set on D0, and the clock on STROBE.
772 * LCD reads D0 on STROBE's rising edge.
773 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100774 for (bit = 0; bit < 8; bit++) {
Daniel Chromikbea74332016-02-11 13:29:23 +0100775 clear_bit(LCD_BIT_CL, bits); /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530776 panel_set_bits();
Daniel Chromikbea74332016-02-11 13:29:23 +0100777 if (byte & 1) {
778 set_bit(LCD_BIT_DA, bits);
779 } else {
780 clear_bit(LCD_BIT_DA, bits);
781 }
782
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530783 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300784 udelay(2); /* maintain the data during 2 us before CLK up */
Daniel Chromikbea74332016-02-11 13:29:23 +0100785 set_bit(LCD_BIT_CL, bits); /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530786 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300787 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100788 byte >>= 1;
789 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800790}
791
792/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100793static void lcd_backlight(int on)
794{
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100795 if (lcd.pins.bl == PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +0100796 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800797
Justin P. Mattock6975e182012-03-19 08:17:52 -0700798 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800799 spin_lock_irq(&pprt_lock);
Daniel Chromikbea74332016-02-11 13:29:23 +0100800 if (on)
801 set_bit(LCD_BIT_BL, bits);
802 else
803 clear_bit(LCD_BIT_BL, bits);
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530804 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800805 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800806}
807
808/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100809static void lcd_write_cmd_s(int cmd)
810{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800811 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100812 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
813 lcd_send_serial(cmd & 0x0F);
814 lcd_send_serial((cmd >> 4) & 0x0F);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530815 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800816 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800817}
818
819/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100820static void lcd_write_data_s(int data)
821{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800822 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100823 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
824 lcd_send_serial(data & 0x0F);
825 lcd_send_serial((data >> 4) & 0x0F);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530826 udelay(40); /* the shortest data takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800827 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800828}
829
830/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100831static void lcd_write_cmd_p8(int cmd)
832{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800833 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800834 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100835 w_dtr(pprt, cmd);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530836 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800837
Daniel Chromikbea74332016-02-11 13:29:23 +0100838 set_bit(LCD_BIT_E, bits);
839 clear_bit(LCD_BIT_RS, bits);
840 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau7005b582008-11-13 17:18:59 -0800841 set_ctrl_bits();
842
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530843 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800844
Daniel Chromikbea74332016-02-11 13:29:23 +0100845 clear_bit(LCD_BIT_E, bits);
Willy Tarreau7005b582008-11-13 17:18:59 -0800846 set_ctrl_bits();
847
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530848 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800849 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100850}
Willy Tarreau7005b582008-11-13 17:18:59 -0800851
Willy Tarreau698b1512008-11-22 11:29:33 +0100852/* send data to the LCD panel in 8 bits parallel mode */
853static void lcd_write_data_p8(int data)
854{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800855 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100856 /* present the data to the data port */
857 w_dtr(pprt, data);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530858 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100859
Daniel Chromikbea74332016-02-11 13:29:23 +0100860 set_bit(LCD_BIT_E, bits);
861 set_bit(LCD_BIT_RS, bits);
862 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100863 set_ctrl_bits();
864
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530865 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100866
Daniel Chromikbea74332016-02-11 13:29:23 +0100867 clear_bit(LCD_BIT_E, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100868 set_ctrl_bits();
869
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530870 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800871 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100872}
873
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400874/* send a command to the TI LCD panel */
875static void lcd_write_cmd_tilcd(int cmd)
876{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800877 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400878 /* present the data to the control port */
879 w_ctr(pprt, cmd);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530880 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800881 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400882}
883
884/* send data to the TI LCD panel */
885static void lcd_write_data_tilcd(int data)
886{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800887 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400888 /* present the data to the data port */
889 w_dtr(pprt, data);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530890 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800891 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400892}
893
Willy Tarreau698b1512008-11-22 11:29:33 +0100894static void lcd_gotoxy(void)
895{
Mariusz Gorski21149242014-12-05 22:28:19 +0100896 lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100897 | (lcd.addr.y ? lcd.hwidth : 0)
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +0530898 /*
899 * we force the cursor to stay at the end of the
900 * line if it wants to go farther
901 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100902 | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100903 (lcd.hwidth - 1) : lcd.bwidth - 1));
Willy Tarreau698b1512008-11-22 11:29:33 +0100904}
905
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +0100906static void lcd_home(void)
907{
908 lcd.addr.x = 0;
909 lcd.addr.y = 0;
910 lcd_gotoxy();
911}
912
Willy Tarreau698b1512008-11-22 11:29:33 +0100913static void lcd_print(char c)
914{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100915 if (lcd.addr.x < lcd.bwidth) {
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +0300916 if (lcd_char_conv)
Willy Tarreau698b1512008-11-22 11:29:33 +0100917 c = lcd_char_conv[(unsigned char)c];
918 lcd_write_data(c);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100919 lcd.addr.x++;
Willy Tarreau698b1512008-11-22 11:29:33 +0100920 }
921 /* prevents the cursor from wrapping onto the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100922 if (lcd.addr.x == lcd.bwidth)
Willy Tarreau698b1512008-11-22 11:29:33 +0100923 lcd_gotoxy();
924}
925
926/* fills the display with spaces and resets X/Y */
927static void lcd_clear_fast_s(void)
928{
929 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200930
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +0100931 lcd_home();
Willy Tarreau698b1512008-11-22 11:29:33 +0100932
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800933 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100934 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100935 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
936 lcd_send_serial(' ' & 0x0F);
937 lcd_send_serial((' ' >> 4) & 0x0F);
Ricardo Ruedasdf44f152015-12-08 22:30:40 +0100938 /* the shortest data takes at least 40 us */
Greg Kroah-Hartman4cff7ad2016-02-01 12:50:26 -0800939 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100940 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800941 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100942
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +0100943 lcd_home();
Willy Tarreau698b1512008-11-22 11:29:33 +0100944}
945
946/* fills the display with spaces and resets X/Y */
947static void lcd_clear_fast_p8(void)
948{
949 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200950
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +0100951 lcd_home();
Willy Tarreau698b1512008-11-22 11:29:33 +0100952
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800953 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100954 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100955 /* present the data to the data port */
956 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300957
958 /* maintain the data during 20 us before the strobe */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530959 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100960
Daniel Chromikbea74332016-02-11 13:29:23 +0100961 set_bit(LCD_BIT_E, bits);
962 set_bit(LCD_BIT_RS, bits);
963 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100964 set_ctrl_bits();
965
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300966 /* maintain the strobe during 40 us */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530967 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100968
Daniel Chromikbea74332016-02-11 13:29:23 +0100969 clear_bit(LCD_BIT_E, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100970 set_ctrl_bits();
971
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300972 /* the shortest data takes at least 45 us */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530973 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100974 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800975 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100976
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +0100977 lcd_home();
Willy Tarreau7005b582008-11-13 17:18:59 -0800978}
979
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400980/* fills the display with spaces and resets X/Y */
981static void lcd_clear_fast_tilcd(void)
982{
983 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200984
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +0100985 lcd_home();
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400986
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800987 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100988 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400989 /* present the data to the data port */
990 w_dtr(pprt, ' ');
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530991 udelay(60);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400992 }
993
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800994 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400995
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +0100996 lcd_home();
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400997}
998
Willy Tarreau7005b582008-11-13 17:18:59 -0800999/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +01001000static void lcd_clear_display(void)
1001{
Mariusz Gorski21149242014-12-05 22:28:19 +01001002 lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001003 lcd.addr.x = 0;
1004 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001005 /* we must wait a few milliseconds (15) */
1006 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -08001007}
1008
Willy Tarreau698b1512008-11-22 11:29:33 +01001009static void lcd_init_display(void)
1010{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001011 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001012 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -08001013
Willy Tarreau698b1512008-11-22 11:29:33 +01001014 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -08001015
Mariusz Gorski21149242014-12-05 22:28:19 +01001016 /* 8bits, 1 line, small fonts; let's do it 3 times */
1017 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001018 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001019 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001020 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001021 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001022 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001023
Mariusz Gorski21149242014-12-05 22:28:19 +01001024 /* set font height and lines number */
1025 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
1026 | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
1027 | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001028 );
1029 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001030
Mariusz Gorski21149242014-12-05 22:28:19 +01001031 /* display off, cursor off, blink off */
1032 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
Willy Tarreau698b1512008-11-22 11:29:33 +01001033 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001034
Mariusz Gorski21149242014-12-05 22:28:19 +01001035 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
1036 | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
1037 | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
1038 | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001039 );
Willy Tarreau7005b582008-11-13 17:18:59 -08001040
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001041 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08001042
Willy Tarreau698b1512008-11-22 11:29:33 +01001043 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001044
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001045 /* entry mode set : increment, cursor shifting */
Mariusz Gorski21149242014-12-05 22:28:19 +01001046 lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
Willy Tarreau7005b582008-11-13 17:18:59 -08001047
Willy Tarreau698b1512008-11-22 11:29:33 +01001048 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001049}
1050
1051/*
1052 * These are the file operation function for user access to /dev/lcd
1053 * This function can also be called from inside the kernel, by
1054 * setting file and ppos to NULL.
1055 *
1056 */
1057
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001058static inline int handle_lcd_special_code(void)
1059{
1060 /* LCD special codes */
1061
1062 int processed = 0;
1063
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001064 char *esc = lcd.esc_seq.buf + 2;
1065 int oldflags = lcd.flags;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001066
1067 /* check for display mode flags */
1068 switch (*esc) {
1069 case 'D': /* Display ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001070 lcd.flags |= LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001071 processed = 1;
1072 break;
1073 case 'd': /* Display OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001074 lcd.flags &= ~LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001075 processed = 1;
1076 break;
1077 case 'C': /* Cursor ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001078 lcd.flags |= LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001079 processed = 1;
1080 break;
1081 case 'c': /* Cursor OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001082 lcd.flags &= ~LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001083 processed = 1;
1084 break;
1085 case 'B': /* Blink ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001086 lcd.flags |= LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001087 processed = 1;
1088 break;
1089 case 'b': /* Blink OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001090 lcd.flags &= ~LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001091 processed = 1;
1092 break;
1093 case '+': /* Back light ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001094 lcd.flags |= LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001095 processed = 1;
1096 break;
1097 case '-': /* Back light OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001098 lcd.flags &= ~LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001099 processed = 1;
1100 break;
1101 case '*':
1102 /* flash back light using the keypad timer */
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001103 if (scan_timer.function) {
Sirnam Swetha832bf282015-10-27 14:56:13 +05301104 if (lcd.light_tempo == 0 &&
1105 ((lcd.flags & LCD_FLAG_L) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001106 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001107 lcd.light_tempo = FLASH_LIGHT_TEMPO;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001108 }
1109 processed = 1;
1110 break;
1111 case 'f': /* Small Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001112 lcd.flags &= ~LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001113 processed = 1;
1114 break;
1115 case 'F': /* Large Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001116 lcd.flags |= LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001117 processed = 1;
1118 break;
1119 case 'n': /* One Line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001120 lcd.flags &= ~LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001121 processed = 1;
1122 break;
1123 case 'N': /* Two Lines */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001124 lcd.flags |= LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001125 break;
1126 case 'l': /* Shift Cursor Left */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001127 if (lcd.addr.x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001128 /* back one char if not at end of line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001129 if (lcd.addr.x < lcd.bwidth)
Mariusz Gorski21149242014-12-05 22:28:19 +01001130 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001131 lcd.addr.x--;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001132 }
1133 processed = 1;
1134 break;
1135 case 'r': /* shift cursor right */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001136 if (lcd.addr.x < lcd.width) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001137 /* allow the cursor to pass the end of the line */
Mariusz Gorski21149242014-12-05 22:28:19 +01001138 if (lcd.addr.x < (lcd.bwidth - 1))
1139 lcd_write_cmd(LCD_CMD_SHIFT |
1140 LCD_CMD_SHIFT_RIGHT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001141 lcd.addr.x++;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001142 }
1143 processed = 1;
1144 break;
1145 case 'L': /* shift display left */
Mariusz Gorski21149242014-12-05 22:28:19 +01001146 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001147 processed = 1;
1148 break;
1149 case 'R': /* shift display right */
Mariusz Gorski21149242014-12-05 22:28:19 +01001150 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
1151 LCD_CMD_SHIFT_RIGHT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001152 processed = 1;
1153 break;
1154 case 'k': { /* kill end of line */
1155 int x;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001156
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001157 for (x = lcd.addr.x; x < lcd.bwidth; x++)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001158 lcd_write_data(' ');
1159
1160 /* restore cursor position */
1161 lcd_gotoxy();
1162 processed = 1;
1163 break;
1164 }
1165 case 'I': /* reinitialize display */
1166 lcd_init_display();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001167 processed = 1;
1168 break;
1169 case 'G': {
1170 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1171 * and '7', representing the numerical ASCII code of the
1172 * redefined character, and <xx...xx> a sequence of 16
1173 * hex digits representing 8 bytes for each character.
1174 * Most LCDs will only use 5 lower bits of the 7 first
1175 * bytes.
1176 */
1177
1178 unsigned char cgbytes[8];
1179 unsigned char cgaddr;
1180 int cgoffset;
1181 int shift;
1182 char value;
1183 int addr;
1184
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001185 if (!strchr(esc, ';'))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001186 break;
1187
1188 esc++;
1189
1190 cgaddr = *(esc++) - '0';
1191 if (cgaddr > 7) {
1192 processed = 1;
1193 break;
1194 }
1195
1196 cgoffset = 0;
1197 shift = 0;
1198 value = 0;
1199 while (*esc && cgoffset < 8) {
1200 shift ^= 4;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001201 if (*esc >= '0' && *esc <= '9') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001202 value |= (*esc - '0') << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001203 } else if (*esc >= 'A' && *esc <= 'Z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001204 value |= (*esc - 'A' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001205 } else if (*esc >= 'a' && *esc <= 'z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001206 value |= (*esc - 'a' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001207 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001208 esc++;
1209 continue;
1210 }
1211
1212 if (shift == 0) {
1213 cgbytes[cgoffset++] = value;
1214 value = 0;
1215 }
1216
1217 esc++;
1218 }
1219
Mariusz Gorski21149242014-12-05 22:28:19 +01001220 lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001221 for (addr = 0; addr < cgoffset; addr++)
1222 lcd_write_data(cgbytes[addr]);
1223
1224 /* ensures that we stop writing to CGRAM */
1225 lcd_gotoxy();
1226 processed = 1;
1227 break;
1228 }
1229 case 'x': /* gotoxy : LxXXX[yYYY]; */
1230 case 'y': /* gotoxy : LyYYY[xXXX]; */
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001231 if (!strchr(esc, ';'))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001232 break;
1233
1234 while (*esc) {
1235 if (*esc == 'x') {
1236 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001237 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001238 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001239 } else if (*esc == 'y') {
1240 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001241 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001242 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001243 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001244 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001245 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001246 }
1247
1248 lcd_gotoxy();
1249 processed = 1;
1250 break;
1251 }
1252
Mariusz Gorski21149242014-12-05 22:28:19 +01001253 /* TODO: This indent party here got ugly, clean it! */
Adam Buchbinderf2635892012-09-19 21:51:07 -04001254 /* Check whether one flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001255 if (oldflags != lcd.flags) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001256 /* check whether one of B,C,D flags were changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001257 if ((oldflags ^ lcd.flags) &
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001258 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1259 /* set display mode */
Mariusz Gorski21149242014-12-05 22:28:19 +01001260 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
1261 | ((lcd.flags & LCD_FLAG_D)
1262 ? LCD_CMD_DISPLAY_ON : 0)
1263 | ((lcd.flags & LCD_FLAG_C)
1264 ? LCD_CMD_CURSOR_ON : 0)
1265 | ((lcd.flags & LCD_FLAG_B)
1266 ? LCD_CMD_BLINK_ON : 0));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001267 /* check whether one of F,N flags was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001268 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
Mariusz Gorski21149242014-12-05 22:28:19 +01001269 lcd_write_cmd(LCD_CMD_FUNCTION_SET
1270 | LCD_CMD_DATA_LEN_8BITS
1271 | ((lcd.flags & LCD_FLAG_F)
Mariusz Gorski21149242014-12-05 22:28:19 +01001272 ? LCD_CMD_FONT_5X10_DOTS
Geert Uytterhoeven84a1ed02017-02-06 15:38:03 +01001273 : 0)
1274 | ((lcd.flags & LCD_FLAG_N)
1275 ? LCD_CMD_TWO_LINES
Mariusz Gorski21149242014-12-05 22:28:19 +01001276 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001277 /* check whether L flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001278 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1279 if (lcd.flags & (LCD_FLAG_L))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001280 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001281 else if (lcd.light_tempo == 0)
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301282 /*
1283 * switch off the light only when the tempo
1284 * lighting is gone
1285 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001286 lcd_backlight(0);
1287 }
1288 }
1289
1290 return processed;
1291}
1292
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001293static void lcd_write_char(char c)
Willy Tarreau698b1512008-11-22 11:29:33 +01001294{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001295 /* first, we'll test if we're in escape mode */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001296 if ((c != '\n') && lcd.esc_seq.len >= 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001297 /* yes, let's add this char to the buffer */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001298 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1299 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001300 } else {
1301 /* aborts any previous escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001302 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001303
1304 switch (c) {
1305 case LCD_ESCAPE_CHAR:
1306 /* start of an escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001307 lcd.esc_seq.len = 0;
1308 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001309 break;
1310 case '\b':
1311 /* go back one char and clear it */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001312 if (lcd.addr.x > 0) {
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301313 /*
1314 * check if we're not at the
1315 * end of the line
1316 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001317 if (lcd.addr.x < lcd.bwidth)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001318 /* back one char */
Mariusz Gorski21149242014-12-05 22:28:19 +01001319 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001320 lcd.addr.x--;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001321 }
1322 /* replace with a space */
1323 lcd_write_data(' ');
1324 /* back one char again */
Mariusz Gorski21149242014-12-05 22:28:19 +01001325 lcd_write_cmd(LCD_CMD_SHIFT);
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001326 break;
1327 case '\014':
1328 /* quickly clear the display */
1329 lcd_clear_fast();
1330 break;
1331 case '\n':
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301332 /*
1333 * flush the remainder of the current line and
1334 * go to the beginning of the next line
1335 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001336 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001337 lcd_write_data(' ');
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001338 lcd.addr.x = 0;
1339 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001340 lcd_gotoxy();
1341 break;
1342 case '\r':
1343 /* go to the beginning of the same line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001344 lcd.addr.x = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001345 lcd_gotoxy();
1346 break;
1347 case '\t':
1348 /* print a space instead of the tab */
1349 lcd_print(' ');
1350 break;
1351 default:
1352 /* simply print this char */
1353 lcd_print(c);
1354 break;
1355 }
1356 }
1357
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301358 /*
1359 * now we'll see if we're in an escape mode and if the current
1360 * escape sequence can be understood.
1361 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001362 if (lcd.esc_seq.len >= 2) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001363 int processed = 0;
1364
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001365 if (!strcmp(lcd.esc_seq.buf, "[2J")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001366 /* clear the display */
1367 lcd_clear_fast();
1368 processed = 1;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001369 } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001370 /* cursor to home */
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +01001371 lcd_home();
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001372 processed = 1;
1373 }
1374 /* codes starting with ^[[L */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001375 else if ((lcd.esc_seq.len >= 3) &&
1376 (lcd.esc_seq.buf[0] == '[') &&
1377 (lcd.esc_seq.buf[1] == 'L')) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001378 processed = handle_lcd_special_code();
1379 }
1380
1381 /* LCD special escape codes */
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301382 /*
1383 * flush the escape sequence if it's been processed
1384 * or if it is getting too long.
1385 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001386 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1387 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001388 } /* escape codes */
1389}
1390
1391static ssize_t lcd_write(struct file *file,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001392 const char __user *buf, size_t count, loff_t *ppos)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001393{
1394 const char __user *tmp = buf;
Willy Tarreau698b1512008-11-22 11:29:33 +01001395 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001396
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001397 for (; count-- > 0; (*ppos)++, tmp++) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001398 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301399 /*
1400 * let's be a little nice with other processes
1401 * that need some CPU
1402 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001403 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001404
Bastien Armand6a4193a2014-04-23 19:42:11 +02001405 if (get_user(c, tmp))
Willy Tarreau698b1512008-11-22 11:29:33 +01001406 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001407
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001408 lcd_write_char(c);
Willy Tarreau7005b582008-11-13 17:18:59 -08001409 }
1410
Willy Tarreau698b1512008-11-22 11:29:33 +01001411 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001412}
1413
Willy Tarreau698b1512008-11-22 11:29:33 +01001414static int lcd_open(struct inode *inode, struct file *file)
1415{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001416 if (!atomic_dec_and_test(&lcd_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001417 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001418
Willy Tarreau698b1512008-11-22 11:29:33 +01001419 if (file->f_mode & FMODE_READ) /* device is write-only */
1420 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001421
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001422 if (lcd.must_clear) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001423 lcd_clear_display();
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001424 lcd.must_clear = false;
Willy Tarreau698b1512008-11-22 11:29:33 +01001425 }
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001426 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001427}
1428
Willy Tarreau698b1512008-11-22 11:29:33 +01001429static int lcd_release(struct inode *inode, struct file *file)
1430{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001431 atomic_inc(&lcd_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001432 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001433}
1434
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001435static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001436 .write = lcd_write,
1437 .open = lcd_open,
1438 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001439 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001440};
1441
1442static struct miscdevice lcd_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001443 .minor = LCD_MINOR,
1444 .name = "lcd",
1445 .fops = &lcd_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001446};
1447
Willy Tarreau7005b582008-11-13 17:18:59 -08001448/* public function usable from the kernel for any purpose */
Peter Huewe36d20412013-02-15 13:47:05 +01001449static void panel_lcd_print(const char *s)
Willy Tarreau698b1512008-11-22 11:29:33 +01001450{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001451 const char *tmp = s;
1452 int count = strlen(s);
1453
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001454 if (lcd.enabled && lcd.initialized) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001455 for (; count-- > 0; tmp++) {
1456 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301457 /*
1458 * let's be a little nice with other processes
1459 * that need some CPU
1460 */
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001461 schedule();
1462
1463 lcd_write_char(*tmp);
1464 }
1465 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001466}
1467
Willy Tarreau7005b582008-11-13 17:18:59 -08001468/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +01001469static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001470{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001471 switch (selected_lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001472 case LCD_TYPE_OLD:
1473 /* parallel mode, 8 bits */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001474 lcd.proto = LCD_PROTO_PARALLEL;
1475 lcd.charset = LCD_CHARSET_NORMAL;
1476 lcd.pins.e = PIN_STROBE;
1477 lcd.pins.rs = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001478
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001479 lcd.width = 40;
1480 lcd.bwidth = 40;
1481 lcd.hwidth = 64;
1482 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001483 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001484 case LCD_TYPE_KS0074:
1485 /* serial mode, ks0074 */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001486 lcd.proto = LCD_PROTO_SERIAL;
1487 lcd.charset = LCD_CHARSET_KS0074;
1488 lcd.pins.bl = PIN_AUTOLF;
1489 lcd.pins.cl = PIN_STROBE;
1490 lcd.pins.da = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001491
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001492 lcd.width = 16;
1493 lcd.bwidth = 40;
1494 lcd.hwidth = 16;
1495 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001496 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001497 case LCD_TYPE_NEXCOM:
1498 /* parallel mode, 8 bits, generic */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001499 lcd.proto = LCD_PROTO_PARALLEL;
1500 lcd.charset = LCD_CHARSET_NORMAL;
1501 lcd.pins.e = PIN_AUTOLF;
1502 lcd.pins.rs = PIN_SELECP;
1503 lcd.pins.rw = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001504
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001505 lcd.width = 16;
1506 lcd.bwidth = 40;
1507 lcd.hwidth = 64;
1508 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001509 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001510 case LCD_TYPE_CUSTOM:
1511 /* customer-defined */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001512 lcd.proto = DEFAULT_LCD_PROTO;
1513 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001514 /* default geometry will be set later */
1515 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001516 case LCD_TYPE_HANTRONIX:
1517 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001518 default:
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001519 lcd.proto = LCD_PROTO_PARALLEL;
1520 lcd.charset = LCD_CHARSET_NORMAL;
1521 lcd.pins.e = PIN_STROBE;
1522 lcd.pins.rs = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001523
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001524 lcd.width = 16;
1525 lcd.bwidth = 40;
1526 lcd.hwidth = 64;
1527 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001528 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001529 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001530
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001531 /* Overwrite with module params set on loading */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001532 if (lcd_height != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001533 lcd.height = lcd_height;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001534 if (lcd_width != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001535 lcd.width = lcd_width;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001536 if (lcd_bwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001537 lcd.bwidth = lcd_bwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001538 if (lcd_hwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001539 lcd.hwidth = lcd_hwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001540 if (lcd_charset != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001541 lcd.charset = lcd_charset;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001542 if (lcd_proto != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001543 lcd.proto = lcd_proto;
1544 if (lcd_e_pin != PIN_NOT_SET)
1545 lcd.pins.e = lcd_e_pin;
1546 if (lcd_rs_pin != PIN_NOT_SET)
1547 lcd.pins.rs = lcd_rs_pin;
1548 if (lcd_rw_pin != PIN_NOT_SET)
1549 lcd.pins.rw = lcd_rw_pin;
1550 if (lcd_cl_pin != PIN_NOT_SET)
1551 lcd.pins.cl = lcd_cl_pin;
1552 if (lcd_da_pin != PIN_NOT_SET)
1553 lcd.pins.da = lcd_da_pin;
1554 if (lcd_bl_pin != PIN_NOT_SET)
1555 lcd.pins.bl = lcd_bl_pin;
Willy Tarreau7005b582008-11-13 17:18:59 -08001556
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001557 /* this is used to catch wrong and default values */
1558 if (lcd.width <= 0)
1559 lcd.width = DEFAULT_LCD_WIDTH;
1560 if (lcd.bwidth <= 0)
1561 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1562 if (lcd.hwidth <= 0)
1563 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1564 if (lcd.height <= 0)
1565 lcd.height = DEFAULT_LCD_HEIGHT;
1566
1567 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001568 lcd_write_cmd = lcd_write_cmd_s;
1569 lcd_write_data = lcd_write_data_s;
1570 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001571
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001572 if (lcd.pins.cl == PIN_NOT_SET)
1573 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1574 if (lcd.pins.da == PIN_NOT_SET)
1575 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001576
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001577 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001578 lcd_write_cmd = lcd_write_cmd_p8;
1579 lcd_write_data = lcd_write_data_p8;
1580 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001581
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001582 if (lcd.pins.e == PIN_NOT_SET)
1583 lcd.pins.e = DEFAULT_LCD_PIN_E;
1584 if (lcd.pins.rs == PIN_NOT_SET)
1585 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1586 if (lcd.pins.rw == PIN_NOT_SET)
1587 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001588 } else {
1589 lcd_write_cmd = lcd_write_cmd_tilcd;
1590 lcd_write_data = lcd_write_data_tilcd;
1591 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001592 }
1593
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001594 if (lcd.pins.bl == PIN_NOT_SET)
1595 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
Willy Tarreau698b1512008-11-22 11:29:33 +01001596
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001597 if (lcd.pins.e == PIN_NOT_SET)
1598 lcd.pins.e = PIN_NONE;
1599 if (lcd.pins.rs == PIN_NOT_SET)
1600 lcd.pins.rs = PIN_NONE;
1601 if (lcd.pins.rw == PIN_NOT_SET)
1602 lcd.pins.rw = PIN_NONE;
1603 if (lcd.pins.bl == PIN_NOT_SET)
1604 lcd.pins.bl = PIN_NONE;
1605 if (lcd.pins.cl == PIN_NOT_SET)
1606 lcd.pins.cl = PIN_NONE;
1607 if (lcd.pins.da == PIN_NOT_SET)
1608 lcd.pins.da = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001609
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001610 if (lcd.charset == NOT_SET)
1611 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001612
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001613 if (lcd.charset == LCD_CHARSET_KS0074)
Willy Tarreau698b1512008-11-22 11:29:33 +01001614 lcd_char_conv = lcd_char_conv_ks0074;
1615 else
1616 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001617
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001618 if (lcd.pins.bl != PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +01001619 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001620
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001621 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
Willy Tarreau698b1512008-11-22 11:29:33 +01001622 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001623 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
Willy Tarreau698b1512008-11-22 11:29:33 +01001624 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001625 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
Willy Tarreau698b1512008-11-22 11:29:33 +01001626 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001627 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001628 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001629 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001630 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001631 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
Willy Tarreau698b1512008-11-22 11:29:33 +01001632 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001633
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301634 /*
1635 * before this line, we must NOT send anything to the display.
Willy Tarreau698b1512008-11-22 11:29:33 +01001636 * Since lcd_init_display() needs to write data, we have to
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301637 * enable mark the LCD initialized just before.
1638 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001639 lcd.initialized = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001640 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001641
Willy Tarreau698b1512008-11-22 11:29:33 +01001642 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001643#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1644#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001645 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001646#endif
1647#else
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01001648 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001649#endif
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001650 /* clear the display on the next device opening */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001651 lcd.must_clear = true;
Geert Uytterhoeven204a4f62017-02-06 15:38:09 +01001652 lcd_home();
Willy Tarreau7005b582008-11-13 17:18:59 -08001653}
1654
Willy Tarreau7005b582008-11-13 17:18:59 -08001655/*
1656 * These are the file operation function for user access to /dev/keypad
1657 */
1658
Willy Tarreau698b1512008-11-22 11:29:33 +01001659static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001660 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001661{
Willy Tarreau698b1512008-11-22 11:29:33 +01001662 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001663 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001664
Willy Tarreau698b1512008-11-22 11:29:33 +01001665 if (keypad_buflen == 0) {
1666 if (file->f_flags & O_NONBLOCK)
1667 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001668
Arnd Bergmann310df692014-01-02 13:07:35 +01001669 if (wait_event_interruptible(keypad_read_wait,
1670 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001671 return -EINTR;
1672 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001673
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001674 for (; count-- > 0 && (keypad_buflen > 0);
1675 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001676 put_user(keypad_buffer[keypad_start], tmp);
1677 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1678 }
1679 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001680
Willy Tarreau698b1512008-11-22 11:29:33 +01001681 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001682}
1683
Willy Tarreau698b1512008-11-22 11:29:33 +01001684static int keypad_open(struct inode *inode, struct file *file)
1685{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001686 if (!atomic_dec_and_test(&keypad_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001687 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001688
Willy Tarreau698b1512008-11-22 11:29:33 +01001689 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1690 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001691
Willy Tarreau698b1512008-11-22 11:29:33 +01001692 keypad_buflen = 0; /* flush the buffer on opening */
Willy Tarreau698b1512008-11-22 11:29:33 +01001693 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001694}
1695
Willy Tarreau698b1512008-11-22 11:29:33 +01001696static int keypad_release(struct inode *inode, struct file *file)
1697{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001698 atomic_inc(&keypad_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001699 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001700}
1701
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001702static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001703 .read = keypad_read, /* read */
1704 .open = keypad_open, /* open */
1705 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001706 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001707};
1708
1709static struct miscdevice keypad_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001710 .minor = KEYPAD_MINOR,
1711 .name = "keypad",
1712 .fops = &keypad_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001713};
1714
Peter Huewe36d20412013-02-15 13:47:05 +01001715static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001716{
Willy Tarreau698b1512008-11-22 11:29:33 +01001717 /* send the key to the device only if a process is attached to it. */
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001718 if (!atomic_read(&keypad_available)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001719 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1720 keypad_buffer[(keypad_start + keypad_buflen++) %
1721 KEYPAD_BUFFER] = *string++;
1722 }
1723 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001724 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001725}
1726
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001727/* this function scans all the bits involving at least one logical signal,
1728 * and puts the results in the bitfield "phys_read" (one bit per established
1729 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001730 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001731 * Note: to debounce input signals, we will only consider as switched a signal
1732 * which is stable across 2 measures. Signals which are different between two
1733 * reads will be kept as they previously were in their logical form (phys_prev).
1734 * A signal which has just switched will have a 1 in
1735 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001736 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001737static void phys_scan_contacts(void)
1738{
1739 int bit, bitval;
1740 char oldval;
1741 char bitmask;
1742 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001743
Willy Tarreau698b1512008-11-22 11:29:33 +01001744 phys_prev = phys_curr;
1745 phys_read_prev = phys_read;
1746 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001747
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001748 /* keep track of old value, with all outputs disabled */
1749 oldval = r_dtr(pprt) | scan_mask_o;
1750 /* activate all keyboard outputs (active low) */
1751 w_dtr(pprt, oldval & ~scan_mask_o);
1752
1753 /* will have a 1 for each bit set to gnd */
1754 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1755 /* disable all matrix signals */
1756 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001757
Willy Tarreau698b1512008-11-22 11:29:33 +01001758 /* now that all outputs are cleared, the only active input bits are
1759 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001760 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001761
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001762 /* 1 for each grounded input */
1763 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1764
1765 /* grounded inputs are signals 40-44 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001766 phys_read |= (__u64)gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001767
Willy Tarreau698b1512008-11-22 11:29:33 +01001768 if (bitmask != gndmask) {
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301769 /*
1770 * since clearing the outputs changed some inputs, we know
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001771 * that some input signals are currently tied to some outputs.
1772 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001773 */
1774 for (bit = 0; bit < 8; bit++) {
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05301775 bitval = BIT(bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001776
1777 if (!(scan_mask_o & bitval)) /* skip unused bits */
1778 continue;
1779
1780 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1781 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001782 phys_read |= (__u64)bitmask << (5 * bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001783 }
1784 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001785 }
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301786 /*
1787 * this is easy: use old bits when they are flapping,
1788 * use new ones when stable
1789 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001790 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1791 (phys_read & ~(phys_read ^ phys_read_prev));
1792}
1793
1794static inline int input_state_high(struct logical_input *input)
1795{
1796#if 0
1797 /* FIXME:
1798 * this is an invalid test. It tries to catch
1799 * transitions from single-key to multiple-key, but
1800 * doesn't take into account the contacts polarity.
1801 * The only solution to the problem is to parse keys
1802 * from the most complex to the simplest combinations,
1803 * and mark them as 'caught' once a combination
1804 * matches, then unmatch it for all other ones.
1805 */
1806
1807 /* try to catch dangerous transitions cases :
1808 * someone adds a bit, so this signal was a false
1809 * positive resulting from a transition. We should
1810 * invalidate the signal immediately and not call the
1811 * release function.
1812 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1813 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001814 if (((phys_prev & input->mask) == input->value) &&
1815 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001816 input->state = INPUT_ST_LOW; /* invalidate */
1817 return 1;
1818 }
1819#endif
1820
1821 if ((phys_curr & input->mask) == input->value) {
1822 if ((input->type == INPUT_TYPE_STD) &&
1823 (input->high_timer == 0)) {
1824 input->high_timer++;
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001825 if (input->u.std.press_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001826 input->u.std.press_fct(input->u.std.press_data);
1827 } else if (input->type == INPUT_TYPE_KBD) {
1828 /* will turn on the light */
1829 keypressed = 1;
1830
1831 if (input->high_timer == 0) {
1832 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001833
Jake Champline6626de2013-05-04 11:21:17 -04001834 if (press_str[0]) {
1835 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001836
Jake Champline6626de2013-05-04 11:21:17 -04001837 keypad_send_key(press_str, s);
1838 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001839 }
1840
1841 if (input->u.kbd.repeat_str[0]) {
1842 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001843
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001844 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001845 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001846
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001847 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001848 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001849 }
1850 /* we will need to come back here soon */
1851 inputs_stable = 0;
1852 }
1853
1854 if (input->high_timer < 255)
1855 input->high_timer++;
1856 }
1857 return 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001858 }
Vincent Heuken083b3632014-07-10 03:34:28 -07001859
1860 /* else signal falling down. Let's fall through. */
1861 input->state = INPUT_ST_FALLING;
1862 input->fall_timer = 0;
1863
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001864 return 0;
1865}
1866
1867static inline void input_state_falling(struct logical_input *input)
1868{
1869#if 0
1870 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001871 if (((phys_prev & input->mask) == input->value) &&
1872 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001873 input->state = INPUT_ST_LOW; /* invalidate */
1874 return;
1875 }
1876#endif
1877
1878 if ((phys_curr & input->mask) == input->value) {
1879 if (input->type == INPUT_TYPE_KBD) {
1880 /* will turn on the light */
1881 keypressed = 1;
1882
1883 if (input->u.kbd.repeat_str[0]) {
1884 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001885
Jake Champline6626de2013-05-04 11:21:17 -04001886 if (input->high_timer >= KEYPAD_REP_START) {
1887 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001888
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001889 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001890 keypad_send_key(repeat_str, s);
1891 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001892 /* we will need to come back here soon */
1893 inputs_stable = 0;
1894 }
1895
1896 if (input->high_timer < 255)
1897 input->high_timer++;
1898 }
1899 input->state = INPUT_ST_HIGH;
1900 } else if (input->fall_timer >= input->fall_time) {
1901 /* call release event */
1902 if (input->type == INPUT_TYPE_STD) {
1903 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001904
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001905 if (release_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001906 release_fct(input->u.std.release_data);
1907 } else if (input->type == INPUT_TYPE_KBD) {
1908 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001909
Jake Champline6626de2013-05-04 11:21:17 -04001910 if (release_str[0]) {
1911 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001912
Jake Champline6626de2013-05-04 11:21:17 -04001913 keypad_send_key(release_str, s);
1914 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001915 }
1916
1917 input->state = INPUT_ST_LOW;
1918 } else {
1919 input->fall_timer++;
1920 inputs_stable = 0;
1921 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001922}
1923
Willy Tarreau698b1512008-11-22 11:29:33 +01001924static void panel_process_inputs(void)
1925{
1926 struct list_head *item;
1927 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001928
Willy Tarreau698b1512008-11-22 11:29:33 +01001929 keypressed = 0;
1930 inputs_stable = 1;
1931 list_for_each(item, &logical_inputs) {
1932 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001933
Willy Tarreau698b1512008-11-22 11:29:33 +01001934 switch (input->state) {
1935 case INPUT_ST_LOW:
1936 if ((phys_curr & input->mask) != input->value)
1937 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001938 /* if all needed ones were already set previously,
1939 * this means that this logical signal has been
1940 * activated by the releasing of another combined
1941 * signal, so we don't want to match.
1942 * eg: AB -(release B)-> A -(release A)-> 0 :
1943 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001944 */
1945 if ((phys_prev & input->mask) == input->value)
1946 break;
1947 input->rise_timer = 0;
1948 input->state = INPUT_ST_RISING;
1949 /* no break here, fall through */
1950 case INPUT_ST_RISING:
1951 if ((phys_curr & input->mask) != input->value) {
1952 input->state = INPUT_ST_LOW;
1953 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001954 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001955 if (input->rise_timer < input->rise_time) {
1956 inputs_stable = 0;
1957 input->rise_timer++;
1958 break;
1959 }
1960 input->high_timer = 0;
1961 input->state = INPUT_ST_HIGH;
1962 /* no break here, fall through */
1963 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001964 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001965 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001966 /* no break here, fall through */
1967 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001968 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001969 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001970 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001971}
1972
Willy Tarreau698b1512008-11-22 11:29:33 +01001973static void panel_scan_timer(void)
1974{
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001975 if (keypad.enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001976 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001977 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001978
1979 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001980 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001981 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001982
Willy Tarreau698b1512008-11-22 11:29:33 +01001983 if (!inputs_stable || phys_curr != phys_prev)
1984 panel_process_inputs();
1985 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001986
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001987 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001988 if (keypressed) {
Sirnam Swetha832bf282015-10-27 14:56:13 +05301989 if (lcd.light_tempo == 0 &&
1990 ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001991 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001992 lcd.light_tempo = FLASH_LIGHT_TEMPO;
1993 } else if (lcd.light_tempo > 0) {
1994 lcd.light_tempo--;
Sirnam Swetha832bf282015-10-27 14:56:13 +05301995 if (lcd.light_tempo == 0 &&
1996 ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001997 lcd_backlight(0);
1998 }
1999 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002000
Willy Tarreau698b1512008-11-22 11:29:33 +01002001 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08002002}
2003
Willy Tarreau698b1512008-11-22 11:29:33 +01002004static void init_scan_timer(void)
2005{
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002006 if (scan_timer.function)
Willy Tarreau698b1512008-11-22 11:29:33 +01002007 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08002008
Aya Mahfouz8f6e36c2015-02-19 07:58:29 +02002009 setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
Willy Tarreau698b1512008-11-22 11:29:33 +01002010 scan_timer.expires = jiffies + INPUT_POLL_TIME;
Willy Tarreau698b1512008-11-22 11:29:33 +01002011 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002012}
2013
2014/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002015 * if <omask> or <imask> are non-null, they will be or'ed with the bits
2016 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08002017 * returns 1 if ok, 0 if error (in which case, nothing is written).
2018 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01002019static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01002020 u8 *imask, u8 *omask)
Willy Tarreau698b1512008-11-22 11:29:33 +01002021{
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002022 const char sigtab[] = "EeSsPpAaBb";
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01002023 u8 im, om;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01002024 __u64 m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08002025
Ksenija Stanojevicd12f27e2016-01-03 20:42:26 +01002026 om = 0;
2027 im = 0;
Dominique van den Broeck2d534262014-05-24 01:35:24 +02002028 m = 0ULL;
2029 v = 0ULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002030 while (*name) {
2031 int in, out, bit, neg;
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002032 const char *idx;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002033
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002034 idx = strchr(sigtab, *name);
2035 if (!idx)
Willy Tarreau698b1512008-11-22 11:29:33 +01002036 return 0; /* input name not found */
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002037
2038 in = idx - sigtab;
Willy Tarreau698b1512008-11-22 11:29:33 +01002039 neg = (in & 1); /* odd (lower) names are negated */
2040 in >>= 1;
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05302041 im |= BIT(in);
Willy Tarreau7005b582008-11-13 17:18:59 -08002042
Willy Tarreau698b1512008-11-22 11:29:33 +01002043 name++;
Ksenija Stanojevic52ebf932016-01-03 20:43:27 +01002044 if (*name >= '0' && *name <= '7') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002045 out = *name - '0';
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05302046 om |= BIT(out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002047 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002048 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002049 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01002050 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002051 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002052
2053 bit = (out * 5) + in;
2054
2055 m |= 1ULL << bit;
2056 if (!neg)
2057 v |= 1ULL << bit;
2058 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08002059 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002060 *mask = m;
2061 *value = v;
2062 if (imask)
2063 *imask |= im;
2064 if (omask)
2065 *omask |= om;
2066 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002067}
2068
2069/* tries to bind a key to the signal name <name>. The key will send the
2070 * strings <press>, <repeat>, <release> for these respective events.
2071 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2072 */
Peter Huewe36d20412013-02-15 13:47:05 +01002073static struct logical_input *panel_bind_key(const char *name, const char *press,
2074 const char *repeat,
2075 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01002076{
2077 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002078
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002079 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002080 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01002081 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002082
Willy Tarreau698b1512008-11-22 11:29:33 +01002083 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002084 &scan_mask_o)) {
2085 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01002086 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002087 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002088
2089 key->type = INPUT_TYPE_KBD;
2090 key->state = INPUT_ST_LOW;
2091 key->rise_time = 1;
2092 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002093
Willy Tarreau698b1512008-11-22 11:29:33 +01002094 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2095 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2096 strncpy(key->u.kbd.release_str, release,
2097 sizeof(key->u.kbd.release_str));
2098 list_add(&key->list, &logical_inputs);
2099 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002100}
2101
Willy Tarreau630231772008-11-22 12:52:18 +01002102#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002103/* tries to bind a callback function to the signal name <name>. The function
2104 * <press_fct> will be called with the <press_data> arg when the signal is
2105 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002106 * Returns the pointer to the new signal if ok, NULL if the signal could not
2107 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002108 */
2109static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302110 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002111 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302112 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002113 int release_data)
2114{
2115 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002116
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002117 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002118 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002119 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002120
Willy Tarreau698b1512008-11-22 11:29:33 +01002121 memset(callback, 0, sizeof(struct logical_input));
2122 if (!input_name2mask(name, &callback->mask, &callback->value,
2123 &scan_mask_i, &scan_mask_o))
2124 return NULL;
2125
2126 callback->type = INPUT_TYPE_STD;
2127 callback->state = INPUT_ST_LOW;
2128 callback->rise_time = 1;
2129 callback->fall_time = 1;
2130 callback->u.std.press_fct = press_fct;
2131 callback->u.std.press_data = press_data;
2132 callback->u.std.release_fct = release_fct;
2133 callback->u.std.release_data = release_data;
2134 list_add(&callback->list, &logical_inputs);
2135 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002136}
Willy Tarreau630231772008-11-22 12:52:18 +01002137#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002138
Willy Tarreau698b1512008-11-22 11:29:33 +01002139static void keypad_init(void)
2140{
2141 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002142
Willy Tarreau698b1512008-11-22 11:29:33 +01002143 init_waitqueue_head(&keypad_read_wait);
2144 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002145
Willy Tarreau698b1512008-11-22 11:29:33 +01002146 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002147
Willy Tarreau698b1512008-11-22 11:29:33 +01002148 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2149 panel_bind_key(keypad_profile[keynum][0],
2150 keypad_profile[keynum][1],
2151 keypad_profile[keynum][2],
2152 keypad_profile[keynum][3]);
2153 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002154
Willy Tarreau698b1512008-11-22 11:29:33 +01002155 init_scan_timer();
2156 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002157}
2158
Willy Tarreau7005b582008-11-13 17:18:59 -08002159/**************************************************/
2160/* device initialization */
2161/**************************************************/
2162
Willy Tarreau698b1512008-11-22 11:29:33 +01002163static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2164 void *unused)
2165{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002166 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002167 switch (code) {
2168 case SYS_DOWN:
2169 panel_lcd_print
2170 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2171 break;
2172 case SYS_HALT:
2173 panel_lcd_print
2174 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2175 break;
2176 case SYS_POWER_OFF:
2177 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2178 break;
2179 default:
2180 break;
2181 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002182 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002183 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002184}
2185
2186static struct notifier_block panel_notifier = {
2187 panel_notify_sys,
2188 NULL,
2189 0
2190};
2191
Willy Tarreau698b1512008-11-22 11:29:33 +01002192static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002193{
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302194 struct pardev_cb panel_cb;
2195
Willy Tarreau698b1512008-11-22 11:29:33 +01002196 if (port->number != parport)
2197 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002198
Willy Tarreau698b1512008-11-22 11:29:33 +01002199 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002200 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2201 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002202 return;
2203 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002204
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302205 memset(&panel_cb, 0, sizeof(panel_cb));
2206 panel_cb.private = &pprt;
2207 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
2208
2209 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002210 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002211 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2212 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002213 return;
2214 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002215
Willy Tarreau698b1512008-11-22 11:29:33 +01002216 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002217 pr_err("could not claim access to parport%d. Aborting.\n",
2218 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002219 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002220 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002221
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002222 /* must init LCD first, just in case an IRQ from the keypad is
2223 * generated at keypad init
2224 */
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002225 if (lcd.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002226 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002227 if (misc_register(&lcd_dev))
2228 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002229 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002230
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002231 if (keypad.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002232 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002233 if (misc_register(&keypad_dev))
2234 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002235 }
Sudip Mukherjeebb046fe2015-03-09 20:08:23 +05302236 register_reboot_notifier(&panel_notifier);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002237 return;
2238
2239err_lcd_unreg:
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002240 if (lcd.enabled)
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002241 misc_deregister(&lcd_dev);
2242err_unreg_device:
2243 parport_unregister_device(pprt);
2244 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002245}
2246
Willy Tarreau698b1512008-11-22 11:29:33 +01002247static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002248{
Willy Tarreau698b1512008-11-22 11:29:33 +01002249 if (port->number != parport)
2250 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002251
Willy Tarreau698b1512008-11-22 11:29:33 +01002252 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002253 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2254 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002255 return;
2256 }
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002257 if (scan_timer.function)
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302258 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002259
Geert Uytterhoeven3f77b432017-02-06 15:38:08 +01002260 if (keypad.enabled) {
2261 misc_deregister(&keypad_dev);
2262 keypad_initialized = 0;
Peter Huewe0b0595b2009-09-29 01:22:40 +02002263 }
Geert Uytterhoeven3f77b432017-02-06 15:38:08 +01002264
2265 if (lcd.enabled) {
2266 panel_lcd_print("\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2267 misc_deregister(&lcd_dev);
2268 lcd.initialized = false;
2269 }
2270
2271 /* TODO: free all input signals */
2272 parport_release(pprt);
2273 parport_unregister_device(pprt);
2274 pprt = NULL;
2275 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002276}
2277
2278static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002279 .name = "panel",
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302280 .match_port = panel_attach,
Willy Tarreau698b1512008-11-22 11:29:33 +01002281 .detach = panel_detach,
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302282 .devmodel = true,
Willy Tarreau7005b582008-11-13 17:18:59 -08002283};
2284
2285/* init function */
Mariusz Gorskid9114762014-11-27 22:36:46 +01002286static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002287{
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302288 int selected_keypad_type = NOT_SET, err;
Willy Tarreau7005b582008-11-13 17:18:59 -08002289
Willy Tarreau698b1512008-11-22 11:29:33 +01002290 /* take care of an eventual profile */
2291 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002292 case PANEL_PROFILE_CUSTOM:
2293 /* custom profile */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002294 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2295 selected_lcd_type = DEFAULT_LCD_TYPE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002296 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002297 case PANEL_PROFILE_OLD:
2298 /* 8 bits, 2*16, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002299 selected_keypad_type = KEYPAD_TYPE_OLD;
2300 selected_lcd_type = LCD_TYPE_OLD;
2301
2302 /* TODO: This two are a little hacky, sort it out later */
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002303 if (lcd_width == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002304 lcd_width = 16;
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002305 if (lcd_hwidth == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002306 lcd_hwidth = 16;
2307 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002308 case PANEL_PROFILE_NEW:
2309 /* serial, 2*16, new keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002310 selected_keypad_type = KEYPAD_TYPE_NEW;
2311 selected_lcd_type = LCD_TYPE_KS0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01002312 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002313 case PANEL_PROFILE_HANTRONIX:
2314 /* 8 bits, 2*16 hantronix-like, no keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002315 selected_keypad_type = KEYPAD_TYPE_NONE;
2316 selected_lcd_type = LCD_TYPE_HANTRONIX;
Willy Tarreau698b1512008-11-22 11:29:33 +01002317 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002318 case PANEL_PROFILE_NEXCOM:
2319 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002320 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2321 selected_lcd_type = LCD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002322 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002323 case PANEL_PROFILE_LARGE:
2324 /* 8 bits, 2*40, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002325 selected_keypad_type = KEYPAD_TYPE_OLD;
2326 selected_lcd_type = LCD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002327 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002328 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002329
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01002330 /*
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002331 * Overwrite selection with module param values (both keypad and lcd),
2332 * where the deprecated params have lower prio.
2333 */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002334 if (keypad_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002335 selected_keypad_type = keypad_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002336 if (keypad_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002337 selected_keypad_type = keypad_type;
Willy Tarreau7005b582008-11-13 17:18:59 -08002338
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002339 keypad.enabled = (selected_keypad_type > 0);
2340
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002341 if (lcd_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002342 selected_lcd_type = lcd_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002343 if (lcd_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002344 selected_lcd_type = lcd_type;
2345
2346 lcd.enabled = (selected_lcd_type > 0);
2347
Sudip Mukherjee733345e2015-02-11 16:57:08 +05302348 if (lcd.enabled) {
2349 /*
2350 * Init lcd struct with load-time values to preserve exact
2351 * current functionality (at least for now).
2352 */
2353 lcd.height = lcd_height;
2354 lcd.width = lcd_width;
2355 lcd.bwidth = lcd_bwidth;
2356 lcd.hwidth = lcd_hwidth;
2357 lcd.charset = lcd_charset;
2358 lcd.proto = lcd_proto;
2359 lcd.pins.e = lcd_e_pin;
2360 lcd.pins.rs = lcd_rs_pin;
2361 lcd.pins.rw = lcd_rw_pin;
2362 lcd.pins.cl = lcd_cl_pin;
2363 lcd.pins.da = lcd_da_pin;
2364 lcd.pins.bl = lcd_bl_pin;
2365
2366 /* Leave it for now, just in case */
2367 lcd.esc_seq.len = -1;
2368 }
2369
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002370 switch (selected_keypad_type) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002371 case KEYPAD_TYPE_OLD:
2372 keypad_profile = old_keypad_profile;
2373 break;
2374 case KEYPAD_TYPE_NEW:
2375 keypad_profile = new_keypad_profile;
2376 break;
2377 case KEYPAD_TYPE_NEXCOM:
2378 keypad_profile = nexcom_keypad_profile;
2379 break;
2380 default:
2381 keypad_profile = NULL;
2382 break;
2383 }
2384
Sudip Mukherjeef43de772015-02-10 17:26:02 +05302385 if (!lcd.enabled && !keypad.enabled) {
2386 /* no device enabled, let's exit */
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01002387 pr_err("panel driver disabled.\n");
Sudip Mukherjeef43de772015-02-10 17:26:02 +05302388 return -ENODEV;
2389 }
2390
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302391 err = parport_register_driver(&panel_driver);
2392 if (err) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002393 pr_err("could not register with parport. Aborting.\n");
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302394 return err;
Willy Tarreau698b1512008-11-22 11:29:33 +01002395 }
2396
Willy Tarreau698b1512008-11-22 11:29:33 +01002397 if (pprt)
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01002398 pr_info("panel driver registered on parport%d (io=0x%lx).\n",
2399 parport, pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002400 else
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01002401 pr_info("panel driver not yet registered\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002402 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002403}
2404
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002405static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002406{
Willy Tarreau698b1512008-11-22 11:29:33 +01002407 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002408}
Willy Tarreau7005b582008-11-13 17:18:59 -08002409
Willy Tarreau7005b582008-11-13 17:18:59 -08002410module_init(panel_init_module);
2411module_exit(panel_cleanup_module);
2412MODULE_AUTHOR("Willy Tarreau");
2413MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002414
2415/*
2416 * Local variables:
2417 * c-indent-level: 4
2418 * tab-width: 8
2419 * End:
2420 */