blob: 8af500ecaaaf6634d4397e8632ac675a1f85f5fe [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
906static void lcd_print(char c)
907{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100908 if (lcd.addr.x < lcd.bwidth) {
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +0300909 if (lcd_char_conv)
Willy Tarreau698b1512008-11-22 11:29:33 +0100910 c = lcd_char_conv[(unsigned char)c];
911 lcd_write_data(c);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100912 lcd.addr.x++;
Willy Tarreau698b1512008-11-22 11:29:33 +0100913 }
914 /* prevents the cursor from wrapping onto the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100915 if (lcd.addr.x == lcd.bwidth)
Willy Tarreau698b1512008-11-22 11:29:33 +0100916 lcd_gotoxy();
917}
918
919/* fills the display with spaces and resets X/Y */
920static void lcd_clear_fast_s(void)
921{
922 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200923
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100924 lcd.addr.x = 0;
925 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100926 lcd_gotoxy();
927
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800928 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100929 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100930 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
931 lcd_send_serial(' ' & 0x0F);
932 lcd_send_serial((' ' >> 4) & 0x0F);
Ricardo Ruedasdf44f152015-12-08 22:30:40 +0100933 /* the shortest data takes at least 40 us */
Greg Kroah-Hartman4cff7ad2016-02-01 12:50:26 -0800934 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100935 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800936 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100937
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100938 lcd.addr.x = 0;
939 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100940 lcd_gotoxy();
941}
942
943/* fills the display with spaces and resets X/Y */
944static void lcd_clear_fast_p8(void)
945{
946 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200947
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100948 lcd.addr.x = 0;
949 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100950 lcd_gotoxy();
951
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800952 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100953 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100954 /* present the data to the data port */
955 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300956
957 /* maintain the data during 20 us before the strobe */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530958 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100959
Daniel Chromikbea74332016-02-11 13:29:23 +0100960 set_bit(LCD_BIT_E, bits);
961 set_bit(LCD_BIT_RS, bits);
962 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100963 set_ctrl_bits();
964
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300965 /* maintain the strobe during 40 us */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530966 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100967
Daniel Chromikbea74332016-02-11 13:29:23 +0100968 clear_bit(LCD_BIT_E, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100969 set_ctrl_bits();
970
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300971 /* the shortest data takes at least 45 us */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530972 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100973 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800974 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100975
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100976 lcd.addr.x = 0;
977 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100978 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800979}
980
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400981/* fills the display with spaces and resets X/Y */
982static void lcd_clear_fast_tilcd(void)
983{
984 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200985
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100986 lcd.addr.x = 0;
987 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400988 lcd_gotoxy();
989
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800990 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100991 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400992 /* present the data to the data port */
993 w_dtr(pprt, ' ');
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530994 udelay(60);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400995 }
996
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800997 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400998
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100999 lcd.addr.x = 0;
1000 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001001 lcd_gotoxy();
1002}
1003
Willy Tarreau7005b582008-11-13 17:18:59 -08001004/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +01001005static void lcd_clear_display(void)
1006{
Mariusz Gorski21149242014-12-05 22:28:19 +01001007 lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001008 lcd.addr.x = 0;
1009 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001010 /* we must wait a few milliseconds (15) */
1011 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -08001012}
1013
Willy Tarreau698b1512008-11-22 11:29:33 +01001014static void lcd_init_display(void)
1015{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001016 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001017 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -08001018
Willy Tarreau698b1512008-11-22 11:29:33 +01001019 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -08001020
Mariusz Gorski21149242014-12-05 22:28:19 +01001021 /* 8bits, 1 line, small fonts; let's do it 3 times */
1022 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001023 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001024 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001025 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001026 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001027 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001028
Mariusz Gorski21149242014-12-05 22:28:19 +01001029 /* set font height and lines number */
1030 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
1031 | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
1032 | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001033 );
1034 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001035
Mariusz Gorski21149242014-12-05 22:28:19 +01001036 /* display off, cursor off, blink off */
1037 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
Willy Tarreau698b1512008-11-22 11:29:33 +01001038 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001039
Mariusz Gorski21149242014-12-05 22:28:19 +01001040 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
1041 | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
1042 | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
1043 | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001044 );
Willy Tarreau7005b582008-11-13 17:18:59 -08001045
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001046 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08001047
Willy Tarreau698b1512008-11-22 11:29:33 +01001048 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001049
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001050 /* entry mode set : increment, cursor shifting */
Mariusz Gorski21149242014-12-05 22:28:19 +01001051 lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
Willy Tarreau7005b582008-11-13 17:18:59 -08001052
Willy Tarreau698b1512008-11-22 11:29:33 +01001053 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001054}
1055
1056/*
1057 * These are the file operation function for user access to /dev/lcd
1058 * This function can also be called from inside the kernel, by
1059 * setting file and ppos to NULL.
1060 *
1061 */
1062
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001063static inline int handle_lcd_special_code(void)
1064{
1065 /* LCD special codes */
1066
1067 int processed = 0;
1068
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001069 char *esc = lcd.esc_seq.buf + 2;
1070 int oldflags = lcd.flags;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001071
1072 /* check for display mode flags */
1073 switch (*esc) {
1074 case 'D': /* Display ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001075 lcd.flags |= LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001076 processed = 1;
1077 break;
1078 case 'd': /* Display OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001079 lcd.flags &= ~LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001080 processed = 1;
1081 break;
1082 case 'C': /* Cursor ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001083 lcd.flags |= LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001084 processed = 1;
1085 break;
1086 case 'c': /* Cursor OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001087 lcd.flags &= ~LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001088 processed = 1;
1089 break;
1090 case 'B': /* Blink ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001091 lcd.flags |= LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001092 processed = 1;
1093 break;
1094 case 'b': /* Blink OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001095 lcd.flags &= ~LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001096 processed = 1;
1097 break;
1098 case '+': /* Back light ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001099 lcd.flags |= LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001100 processed = 1;
1101 break;
1102 case '-': /* Back light OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001103 lcd.flags &= ~LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001104 processed = 1;
1105 break;
1106 case '*':
1107 /* flash back light using the keypad timer */
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001108 if (scan_timer.function) {
Sirnam Swetha832bf282015-10-27 14:56:13 +05301109 if (lcd.light_tempo == 0 &&
1110 ((lcd.flags & LCD_FLAG_L) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001111 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001112 lcd.light_tempo = FLASH_LIGHT_TEMPO;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001113 }
1114 processed = 1;
1115 break;
1116 case 'f': /* Small Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001117 lcd.flags &= ~LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001118 processed = 1;
1119 break;
1120 case 'F': /* Large Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001121 lcd.flags |= LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001122 processed = 1;
1123 break;
1124 case 'n': /* One Line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001125 lcd.flags &= ~LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001126 processed = 1;
1127 break;
1128 case 'N': /* Two Lines */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001129 lcd.flags |= LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001130 break;
1131 case 'l': /* Shift Cursor Left */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001132 if (lcd.addr.x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001133 /* back one char if not at end of line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001134 if (lcd.addr.x < lcd.bwidth)
Mariusz Gorski21149242014-12-05 22:28:19 +01001135 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001136 lcd.addr.x--;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001137 }
1138 processed = 1;
1139 break;
1140 case 'r': /* shift cursor right */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001141 if (lcd.addr.x < lcd.width) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001142 /* allow the cursor to pass the end of the line */
Mariusz Gorski21149242014-12-05 22:28:19 +01001143 if (lcd.addr.x < (lcd.bwidth - 1))
1144 lcd_write_cmd(LCD_CMD_SHIFT |
1145 LCD_CMD_SHIFT_RIGHT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001146 lcd.addr.x++;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001147 }
1148 processed = 1;
1149 break;
1150 case 'L': /* shift display left */
Mariusz Gorski21149242014-12-05 22:28:19 +01001151 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001152 processed = 1;
1153 break;
1154 case 'R': /* shift display right */
Mariusz Gorski21149242014-12-05 22:28:19 +01001155 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
1156 LCD_CMD_SHIFT_RIGHT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001157 processed = 1;
1158 break;
1159 case 'k': { /* kill end of line */
1160 int x;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001161
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001162 for (x = lcd.addr.x; x < lcd.bwidth; x++)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001163 lcd_write_data(' ');
1164
1165 /* restore cursor position */
1166 lcd_gotoxy();
1167 processed = 1;
1168 break;
1169 }
1170 case 'I': /* reinitialize display */
1171 lcd_init_display();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001172 processed = 1;
1173 break;
1174 case 'G': {
1175 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1176 * and '7', representing the numerical ASCII code of the
1177 * redefined character, and <xx...xx> a sequence of 16
1178 * hex digits representing 8 bytes for each character.
1179 * Most LCDs will only use 5 lower bits of the 7 first
1180 * bytes.
1181 */
1182
1183 unsigned char cgbytes[8];
1184 unsigned char cgaddr;
1185 int cgoffset;
1186 int shift;
1187 char value;
1188 int addr;
1189
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001190 if (!strchr(esc, ';'))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001191 break;
1192
1193 esc++;
1194
1195 cgaddr = *(esc++) - '0';
1196 if (cgaddr > 7) {
1197 processed = 1;
1198 break;
1199 }
1200
1201 cgoffset = 0;
1202 shift = 0;
1203 value = 0;
1204 while (*esc && cgoffset < 8) {
1205 shift ^= 4;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001206 if (*esc >= '0' && *esc <= '9') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001207 value |= (*esc - '0') << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001208 } else if (*esc >= 'A' && *esc <= 'Z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001209 value |= (*esc - 'A' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001210 } else if (*esc >= 'a' && *esc <= 'z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001211 value |= (*esc - 'a' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001212 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001213 esc++;
1214 continue;
1215 }
1216
1217 if (shift == 0) {
1218 cgbytes[cgoffset++] = value;
1219 value = 0;
1220 }
1221
1222 esc++;
1223 }
1224
Mariusz Gorski21149242014-12-05 22:28:19 +01001225 lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001226 for (addr = 0; addr < cgoffset; addr++)
1227 lcd_write_data(cgbytes[addr]);
1228
1229 /* ensures that we stop writing to CGRAM */
1230 lcd_gotoxy();
1231 processed = 1;
1232 break;
1233 }
1234 case 'x': /* gotoxy : LxXXX[yYYY]; */
1235 case 'y': /* gotoxy : LyYYY[xXXX]; */
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001236 if (!strchr(esc, ';'))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001237 break;
1238
1239 while (*esc) {
1240 if (*esc == 'x') {
1241 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001242 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001243 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001244 } else if (*esc == 'y') {
1245 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001246 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001247 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001248 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001249 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001250 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001251 }
1252
1253 lcd_gotoxy();
1254 processed = 1;
1255 break;
1256 }
1257
Mariusz Gorski21149242014-12-05 22:28:19 +01001258 /* TODO: This indent party here got ugly, clean it! */
Adam Buchbinderf2635892012-09-19 21:51:07 -04001259 /* Check whether one flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001260 if (oldflags != lcd.flags) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001261 /* check whether one of B,C,D flags were changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001262 if ((oldflags ^ lcd.flags) &
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001263 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1264 /* set display mode */
Mariusz Gorski21149242014-12-05 22:28:19 +01001265 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
1266 | ((lcd.flags & LCD_FLAG_D)
1267 ? LCD_CMD_DISPLAY_ON : 0)
1268 | ((lcd.flags & LCD_FLAG_C)
1269 ? LCD_CMD_CURSOR_ON : 0)
1270 | ((lcd.flags & LCD_FLAG_B)
1271 ? LCD_CMD_BLINK_ON : 0));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001272 /* check whether one of F,N flags was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001273 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
Mariusz Gorski21149242014-12-05 22:28:19 +01001274 lcd_write_cmd(LCD_CMD_FUNCTION_SET
1275 | LCD_CMD_DATA_LEN_8BITS
1276 | ((lcd.flags & LCD_FLAG_F)
Mariusz Gorski21149242014-12-05 22:28:19 +01001277 ? LCD_CMD_FONT_5X10_DOTS
Geert Uytterhoeven84a1ed02017-02-06 15:38:03 +01001278 : 0)
1279 | ((lcd.flags & LCD_FLAG_N)
1280 ? LCD_CMD_TWO_LINES
Mariusz Gorski21149242014-12-05 22:28:19 +01001281 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001282 /* check whether L flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001283 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1284 if (lcd.flags & (LCD_FLAG_L))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001285 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001286 else if (lcd.light_tempo == 0)
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301287 /*
1288 * switch off the light only when the tempo
1289 * lighting is gone
1290 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001291 lcd_backlight(0);
1292 }
1293 }
1294
1295 return processed;
1296}
1297
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001298static void lcd_write_char(char c)
Willy Tarreau698b1512008-11-22 11:29:33 +01001299{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001300 /* first, we'll test if we're in escape mode */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001301 if ((c != '\n') && lcd.esc_seq.len >= 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001302 /* yes, let's add this char to the buffer */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001303 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1304 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001305 } else {
1306 /* aborts any previous escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001307 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001308
1309 switch (c) {
1310 case LCD_ESCAPE_CHAR:
1311 /* start of an escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001312 lcd.esc_seq.len = 0;
1313 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001314 break;
1315 case '\b':
1316 /* go back one char and clear it */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001317 if (lcd.addr.x > 0) {
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301318 /*
1319 * check if we're not at the
1320 * end of the line
1321 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001322 if (lcd.addr.x < lcd.bwidth)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001323 /* back one char */
Mariusz Gorski21149242014-12-05 22:28:19 +01001324 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001325 lcd.addr.x--;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001326 }
1327 /* replace with a space */
1328 lcd_write_data(' ');
1329 /* back one char again */
Mariusz Gorski21149242014-12-05 22:28:19 +01001330 lcd_write_cmd(LCD_CMD_SHIFT);
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001331 break;
1332 case '\014':
1333 /* quickly clear the display */
1334 lcd_clear_fast();
1335 break;
1336 case '\n':
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301337 /*
1338 * flush the remainder of the current line and
1339 * go to the beginning of the next line
1340 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001341 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001342 lcd_write_data(' ');
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001343 lcd.addr.x = 0;
1344 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001345 lcd_gotoxy();
1346 break;
1347 case '\r':
1348 /* go to the beginning of the same line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001349 lcd.addr.x = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001350 lcd_gotoxy();
1351 break;
1352 case '\t':
1353 /* print a space instead of the tab */
1354 lcd_print(' ');
1355 break;
1356 default:
1357 /* simply print this char */
1358 lcd_print(c);
1359 break;
1360 }
1361 }
1362
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301363 /*
1364 * now we'll see if we're in an escape mode and if the current
1365 * escape sequence can be understood.
1366 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001367 if (lcd.esc_seq.len >= 2) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001368 int processed = 0;
1369
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001370 if (!strcmp(lcd.esc_seq.buf, "[2J")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001371 /* clear the display */
1372 lcd_clear_fast();
1373 processed = 1;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001374 } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001375 /* cursor to home */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001376 lcd.addr.x = 0;
1377 lcd.addr.y = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001378 lcd_gotoxy();
1379 processed = 1;
1380 }
1381 /* codes starting with ^[[L */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001382 else if ((lcd.esc_seq.len >= 3) &&
1383 (lcd.esc_seq.buf[0] == '[') &&
1384 (lcd.esc_seq.buf[1] == 'L')) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001385 processed = handle_lcd_special_code();
1386 }
1387
1388 /* LCD special escape codes */
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301389 /*
1390 * flush the escape sequence if it's been processed
1391 * or if it is getting too long.
1392 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001393 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1394 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001395 } /* escape codes */
1396}
1397
1398static ssize_t lcd_write(struct file *file,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001399 const char __user *buf, size_t count, loff_t *ppos)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001400{
1401 const char __user *tmp = buf;
Willy Tarreau698b1512008-11-22 11:29:33 +01001402 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001403
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001404 for (; count-- > 0; (*ppos)++, tmp++) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001405 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301406 /*
1407 * let's be a little nice with other processes
1408 * that need some CPU
1409 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001410 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001411
Bastien Armand6a4193a2014-04-23 19:42:11 +02001412 if (get_user(c, tmp))
Willy Tarreau698b1512008-11-22 11:29:33 +01001413 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001414
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001415 lcd_write_char(c);
Willy Tarreau7005b582008-11-13 17:18:59 -08001416 }
1417
Willy Tarreau698b1512008-11-22 11:29:33 +01001418 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001419}
1420
Willy Tarreau698b1512008-11-22 11:29:33 +01001421static int lcd_open(struct inode *inode, struct file *file)
1422{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001423 if (!atomic_dec_and_test(&lcd_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001424 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001425
Willy Tarreau698b1512008-11-22 11:29:33 +01001426 if (file->f_mode & FMODE_READ) /* device is write-only */
1427 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001428
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001429 if (lcd.must_clear) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001430 lcd_clear_display();
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001431 lcd.must_clear = false;
Willy Tarreau698b1512008-11-22 11:29:33 +01001432 }
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001433 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001434}
1435
Willy Tarreau698b1512008-11-22 11:29:33 +01001436static int lcd_release(struct inode *inode, struct file *file)
1437{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001438 atomic_inc(&lcd_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001439 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001440}
1441
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001442static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001443 .write = lcd_write,
1444 .open = lcd_open,
1445 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001446 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001447};
1448
1449static struct miscdevice lcd_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001450 .minor = LCD_MINOR,
1451 .name = "lcd",
1452 .fops = &lcd_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001453};
1454
Willy Tarreau7005b582008-11-13 17:18:59 -08001455/* public function usable from the kernel for any purpose */
Peter Huewe36d20412013-02-15 13:47:05 +01001456static void panel_lcd_print(const char *s)
Willy Tarreau698b1512008-11-22 11:29:33 +01001457{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001458 const char *tmp = s;
1459 int count = strlen(s);
1460
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001461 if (lcd.enabled && lcd.initialized) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001462 for (; count-- > 0; tmp++) {
1463 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301464 /*
1465 * let's be a little nice with other processes
1466 * that need some CPU
1467 */
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001468 schedule();
1469
1470 lcd_write_char(*tmp);
1471 }
1472 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001473}
1474
Willy Tarreau7005b582008-11-13 17:18:59 -08001475/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +01001476static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001477{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001478 switch (selected_lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001479 case LCD_TYPE_OLD:
1480 /* parallel mode, 8 bits */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001481 lcd.proto = LCD_PROTO_PARALLEL;
1482 lcd.charset = LCD_CHARSET_NORMAL;
1483 lcd.pins.e = PIN_STROBE;
1484 lcd.pins.rs = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001485
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001486 lcd.width = 40;
1487 lcd.bwidth = 40;
1488 lcd.hwidth = 64;
1489 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001490 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001491 case LCD_TYPE_KS0074:
1492 /* serial mode, ks0074 */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001493 lcd.proto = LCD_PROTO_SERIAL;
1494 lcd.charset = LCD_CHARSET_KS0074;
1495 lcd.pins.bl = PIN_AUTOLF;
1496 lcd.pins.cl = PIN_STROBE;
1497 lcd.pins.da = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001498
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001499 lcd.width = 16;
1500 lcd.bwidth = 40;
1501 lcd.hwidth = 16;
1502 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001503 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001504 case LCD_TYPE_NEXCOM:
1505 /* parallel mode, 8 bits, generic */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001506 lcd.proto = LCD_PROTO_PARALLEL;
1507 lcd.charset = LCD_CHARSET_NORMAL;
1508 lcd.pins.e = PIN_AUTOLF;
1509 lcd.pins.rs = PIN_SELECP;
1510 lcd.pins.rw = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001511
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001512 lcd.width = 16;
1513 lcd.bwidth = 40;
1514 lcd.hwidth = 64;
1515 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001516 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001517 case LCD_TYPE_CUSTOM:
1518 /* customer-defined */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001519 lcd.proto = DEFAULT_LCD_PROTO;
1520 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001521 /* default geometry will be set later */
1522 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001523 case LCD_TYPE_HANTRONIX:
1524 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001525 default:
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001526 lcd.proto = LCD_PROTO_PARALLEL;
1527 lcd.charset = LCD_CHARSET_NORMAL;
1528 lcd.pins.e = PIN_STROBE;
1529 lcd.pins.rs = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001530
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001531 lcd.width = 16;
1532 lcd.bwidth = 40;
1533 lcd.hwidth = 64;
1534 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001535 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001536 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001537
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001538 /* Overwrite with module params set on loading */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001539 if (lcd_height != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001540 lcd.height = lcd_height;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001541 if (lcd_width != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001542 lcd.width = lcd_width;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001543 if (lcd_bwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001544 lcd.bwidth = lcd_bwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001545 if (lcd_hwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001546 lcd.hwidth = lcd_hwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001547 if (lcd_charset != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001548 lcd.charset = lcd_charset;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001549 if (lcd_proto != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001550 lcd.proto = lcd_proto;
1551 if (lcd_e_pin != PIN_NOT_SET)
1552 lcd.pins.e = lcd_e_pin;
1553 if (lcd_rs_pin != PIN_NOT_SET)
1554 lcd.pins.rs = lcd_rs_pin;
1555 if (lcd_rw_pin != PIN_NOT_SET)
1556 lcd.pins.rw = lcd_rw_pin;
1557 if (lcd_cl_pin != PIN_NOT_SET)
1558 lcd.pins.cl = lcd_cl_pin;
1559 if (lcd_da_pin != PIN_NOT_SET)
1560 lcd.pins.da = lcd_da_pin;
1561 if (lcd_bl_pin != PIN_NOT_SET)
1562 lcd.pins.bl = lcd_bl_pin;
Willy Tarreau7005b582008-11-13 17:18:59 -08001563
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001564 /* this is used to catch wrong and default values */
1565 if (lcd.width <= 0)
1566 lcd.width = DEFAULT_LCD_WIDTH;
1567 if (lcd.bwidth <= 0)
1568 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1569 if (lcd.hwidth <= 0)
1570 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1571 if (lcd.height <= 0)
1572 lcd.height = DEFAULT_LCD_HEIGHT;
1573
1574 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001575 lcd_write_cmd = lcd_write_cmd_s;
1576 lcd_write_data = lcd_write_data_s;
1577 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001578
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001579 if (lcd.pins.cl == PIN_NOT_SET)
1580 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1581 if (lcd.pins.da == PIN_NOT_SET)
1582 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001583
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001584 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001585 lcd_write_cmd = lcd_write_cmd_p8;
1586 lcd_write_data = lcd_write_data_p8;
1587 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001588
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001589 if (lcd.pins.e == PIN_NOT_SET)
1590 lcd.pins.e = DEFAULT_LCD_PIN_E;
1591 if (lcd.pins.rs == PIN_NOT_SET)
1592 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1593 if (lcd.pins.rw == PIN_NOT_SET)
1594 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001595 } else {
1596 lcd_write_cmd = lcd_write_cmd_tilcd;
1597 lcd_write_data = lcd_write_data_tilcd;
1598 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001599 }
1600
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001601 if (lcd.pins.bl == PIN_NOT_SET)
1602 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
Willy Tarreau698b1512008-11-22 11:29:33 +01001603
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001604 if (lcd.pins.e == PIN_NOT_SET)
1605 lcd.pins.e = PIN_NONE;
1606 if (lcd.pins.rs == PIN_NOT_SET)
1607 lcd.pins.rs = PIN_NONE;
1608 if (lcd.pins.rw == PIN_NOT_SET)
1609 lcd.pins.rw = PIN_NONE;
1610 if (lcd.pins.bl == PIN_NOT_SET)
1611 lcd.pins.bl = PIN_NONE;
1612 if (lcd.pins.cl == PIN_NOT_SET)
1613 lcd.pins.cl = PIN_NONE;
1614 if (lcd.pins.da == PIN_NOT_SET)
1615 lcd.pins.da = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001616
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001617 if (lcd.charset == NOT_SET)
1618 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001619
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001620 if (lcd.charset == LCD_CHARSET_KS0074)
Willy Tarreau698b1512008-11-22 11:29:33 +01001621 lcd_char_conv = lcd_char_conv_ks0074;
1622 else
1623 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001624
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001625 if (lcd.pins.bl != PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +01001626 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001627
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001628 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
Willy Tarreau698b1512008-11-22 11:29:33 +01001629 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001630 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
Willy Tarreau698b1512008-11-22 11:29:33 +01001631 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001632 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
Willy Tarreau698b1512008-11-22 11:29:33 +01001633 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001634 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001635 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001636 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001637 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001638 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
Willy Tarreau698b1512008-11-22 11:29:33 +01001639 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001640
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301641 /*
1642 * before this line, we must NOT send anything to the display.
Willy Tarreau698b1512008-11-22 11:29:33 +01001643 * Since lcd_init_display() needs to write data, we have to
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301644 * enable mark the LCD initialized just before.
1645 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001646 lcd.initialized = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001647 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001648
Willy Tarreau698b1512008-11-22 11:29:33 +01001649 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001650#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1651#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001652 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001653#endif
1654#else
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01001655 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001656#endif
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001657 lcd.addr.x = 0;
1658 lcd.addr.y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001659 /* clear the display on the next device opening */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001660 lcd.must_clear = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001661 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001662}
1663
Willy Tarreau7005b582008-11-13 17:18:59 -08001664/*
1665 * These are the file operation function for user access to /dev/keypad
1666 */
1667
Willy Tarreau698b1512008-11-22 11:29:33 +01001668static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001669 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001670{
Willy Tarreau698b1512008-11-22 11:29:33 +01001671 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001672 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001673
Willy Tarreau698b1512008-11-22 11:29:33 +01001674 if (keypad_buflen == 0) {
1675 if (file->f_flags & O_NONBLOCK)
1676 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001677
Arnd Bergmann310df692014-01-02 13:07:35 +01001678 if (wait_event_interruptible(keypad_read_wait,
1679 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001680 return -EINTR;
1681 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001682
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001683 for (; count-- > 0 && (keypad_buflen > 0);
1684 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001685 put_user(keypad_buffer[keypad_start], tmp);
1686 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1687 }
1688 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001689
Willy Tarreau698b1512008-11-22 11:29:33 +01001690 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001691}
1692
Willy Tarreau698b1512008-11-22 11:29:33 +01001693static int keypad_open(struct inode *inode, struct file *file)
1694{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001695 if (!atomic_dec_and_test(&keypad_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001696 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001697
Willy Tarreau698b1512008-11-22 11:29:33 +01001698 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1699 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001700
Willy Tarreau698b1512008-11-22 11:29:33 +01001701 keypad_buflen = 0; /* flush the buffer on opening */
Willy Tarreau698b1512008-11-22 11:29:33 +01001702 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001703}
1704
Willy Tarreau698b1512008-11-22 11:29:33 +01001705static int keypad_release(struct inode *inode, struct file *file)
1706{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001707 atomic_inc(&keypad_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001708 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001709}
1710
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001711static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001712 .read = keypad_read, /* read */
1713 .open = keypad_open, /* open */
1714 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001715 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001716};
1717
1718static struct miscdevice keypad_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001719 .minor = KEYPAD_MINOR,
1720 .name = "keypad",
1721 .fops = &keypad_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001722};
1723
Peter Huewe36d20412013-02-15 13:47:05 +01001724static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001725{
Willy Tarreau698b1512008-11-22 11:29:33 +01001726 /* send the key to the device only if a process is attached to it. */
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001727 if (!atomic_read(&keypad_available)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001728 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1729 keypad_buffer[(keypad_start + keypad_buflen++) %
1730 KEYPAD_BUFFER] = *string++;
1731 }
1732 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001733 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001734}
1735
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001736/* this function scans all the bits involving at least one logical signal,
1737 * and puts the results in the bitfield "phys_read" (one bit per established
1738 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001739 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001740 * Note: to debounce input signals, we will only consider as switched a signal
1741 * which is stable across 2 measures. Signals which are different between two
1742 * reads will be kept as they previously were in their logical form (phys_prev).
1743 * A signal which has just switched will have a 1 in
1744 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001745 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001746static void phys_scan_contacts(void)
1747{
1748 int bit, bitval;
1749 char oldval;
1750 char bitmask;
1751 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001752
Willy Tarreau698b1512008-11-22 11:29:33 +01001753 phys_prev = phys_curr;
1754 phys_read_prev = phys_read;
1755 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001756
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001757 /* keep track of old value, with all outputs disabled */
1758 oldval = r_dtr(pprt) | scan_mask_o;
1759 /* activate all keyboard outputs (active low) */
1760 w_dtr(pprt, oldval & ~scan_mask_o);
1761
1762 /* will have a 1 for each bit set to gnd */
1763 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1764 /* disable all matrix signals */
1765 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001766
Willy Tarreau698b1512008-11-22 11:29:33 +01001767 /* now that all outputs are cleared, the only active input bits are
1768 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001769 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001770
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001771 /* 1 for each grounded input */
1772 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1773
1774 /* grounded inputs are signals 40-44 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001775 phys_read |= (__u64)gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001776
Willy Tarreau698b1512008-11-22 11:29:33 +01001777 if (bitmask != gndmask) {
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301778 /*
1779 * since clearing the outputs changed some inputs, we know
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001780 * that some input signals are currently tied to some outputs.
1781 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001782 */
1783 for (bit = 0; bit < 8; bit++) {
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05301784 bitval = BIT(bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001785
1786 if (!(scan_mask_o & bitval)) /* skip unused bits */
1787 continue;
1788
1789 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1790 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001791 phys_read |= (__u64)bitmask << (5 * bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001792 }
1793 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001794 }
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301795 /*
1796 * this is easy: use old bits when they are flapping,
1797 * use new ones when stable
1798 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001799 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1800 (phys_read & ~(phys_read ^ phys_read_prev));
1801}
1802
1803static inline int input_state_high(struct logical_input *input)
1804{
1805#if 0
1806 /* FIXME:
1807 * this is an invalid test. It tries to catch
1808 * transitions from single-key to multiple-key, but
1809 * doesn't take into account the contacts polarity.
1810 * The only solution to the problem is to parse keys
1811 * from the most complex to the simplest combinations,
1812 * and mark them as 'caught' once a combination
1813 * matches, then unmatch it for all other ones.
1814 */
1815
1816 /* try to catch dangerous transitions cases :
1817 * someone adds a bit, so this signal was a false
1818 * positive resulting from a transition. We should
1819 * invalidate the signal immediately and not call the
1820 * release function.
1821 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1822 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001823 if (((phys_prev & input->mask) == input->value) &&
1824 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001825 input->state = INPUT_ST_LOW; /* invalidate */
1826 return 1;
1827 }
1828#endif
1829
1830 if ((phys_curr & input->mask) == input->value) {
1831 if ((input->type == INPUT_TYPE_STD) &&
1832 (input->high_timer == 0)) {
1833 input->high_timer++;
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001834 if (input->u.std.press_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001835 input->u.std.press_fct(input->u.std.press_data);
1836 } else if (input->type == INPUT_TYPE_KBD) {
1837 /* will turn on the light */
1838 keypressed = 1;
1839
1840 if (input->high_timer == 0) {
1841 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001842
Jake Champline6626de2013-05-04 11:21:17 -04001843 if (press_str[0]) {
1844 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001845
Jake Champline6626de2013-05-04 11:21:17 -04001846 keypad_send_key(press_str, s);
1847 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001848 }
1849
1850 if (input->u.kbd.repeat_str[0]) {
1851 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001852
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001853 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001854 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001855
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001856 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001857 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001858 }
1859 /* we will need to come back here soon */
1860 inputs_stable = 0;
1861 }
1862
1863 if (input->high_timer < 255)
1864 input->high_timer++;
1865 }
1866 return 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001867 }
Vincent Heuken083b3632014-07-10 03:34:28 -07001868
1869 /* else signal falling down. Let's fall through. */
1870 input->state = INPUT_ST_FALLING;
1871 input->fall_timer = 0;
1872
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001873 return 0;
1874}
1875
1876static inline void input_state_falling(struct logical_input *input)
1877{
1878#if 0
1879 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001880 if (((phys_prev & input->mask) == input->value) &&
1881 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001882 input->state = INPUT_ST_LOW; /* invalidate */
1883 return;
1884 }
1885#endif
1886
1887 if ((phys_curr & input->mask) == input->value) {
1888 if (input->type == INPUT_TYPE_KBD) {
1889 /* will turn on the light */
1890 keypressed = 1;
1891
1892 if (input->u.kbd.repeat_str[0]) {
1893 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001894
Jake Champline6626de2013-05-04 11:21:17 -04001895 if (input->high_timer >= KEYPAD_REP_START) {
1896 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001897
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001898 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001899 keypad_send_key(repeat_str, s);
1900 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001901 /* we will need to come back here soon */
1902 inputs_stable = 0;
1903 }
1904
1905 if (input->high_timer < 255)
1906 input->high_timer++;
1907 }
1908 input->state = INPUT_ST_HIGH;
1909 } else if (input->fall_timer >= input->fall_time) {
1910 /* call release event */
1911 if (input->type == INPUT_TYPE_STD) {
1912 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001913
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001914 if (release_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001915 release_fct(input->u.std.release_data);
1916 } else if (input->type == INPUT_TYPE_KBD) {
1917 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001918
Jake Champline6626de2013-05-04 11:21:17 -04001919 if (release_str[0]) {
1920 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001921
Jake Champline6626de2013-05-04 11:21:17 -04001922 keypad_send_key(release_str, s);
1923 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001924 }
1925
1926 input->state = INPUT_ST_LOW;
1927 } else {
1928 input->fall_timer++;
1929 inputs_stable = 0;
1930 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001931}
1932
Willy Tarreau698b1512008-11-22 11:29:33 +01001933static void panel_process_inputs(void)
1934{
1935 struct list_head *item;
1936 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001937
Willy Tarreau698b1512008-11-22 11:29:33 +01001938 keypressed = 0;
1939 inputs_stable = 1;
1940 list_for_each(item, &logical_inputs) {
1941 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001942
Willy Tarreau698b1512008-11-22 11:29:33 +01001943 switch (input->state) {
1944 case INPUT_ST_LOW:
1945 if ((phys_curr & input->mask) != input->value)
1946 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001947 /* if all needed ones were already set previously,
1948 * this means that this logical signal has been
1949 * activated by the releasing of another combined
1950 * signal, so we don't want to match.
1951 * eg: AB -(release B)-> A -(release A)-> 0 :
1952 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001953 */
1954 if ((phys_prev & input->mask) == input->value)
1955 break;
1956 input->rise_timer = 0;
1957 input->state = INPUT_ST_RISING;
1958 /* no break here, fall through */
1959 case INPUT_ST_RISING:
1960 if ((phys_curr & input->mask) != input->value) {
1961 input->state = INPUT_ST_LOW;
1962 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001963 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001964 if (input->rise_timer < input->rise_time) {
1965 inputs_stable = 0;
1966 input->rise_timer++;
1967 break;
1968 }
1969 input->high_timer = 0;
1970 input->state = INPUT_ST_HIGH;
1971 /* no break here, fall through */
1972 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001973 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001974 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001975 /* no break here, fall through */
1976 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001977 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001978 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001979 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001980}
1981
Willy Tarreau698b1512008-11-22 11:29:33 +01001982static void panel_scan_timer(void)
1983{
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001984 if (keypad.enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001985 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001986 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001987
1988 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001989 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001990 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001991
Willy Tarreau698b1512008-11-22 11:29:33 +01001992 if (!inputs_stable || phys_curr != phys_prev)
1993 panel_process_inputs();
1994 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001995
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001996 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001997 if (keypressed) {
Sirnam Swetha832bf282015-10-27 14:56:13 +05301998 if (lcd.light_tempo == 0 &&
1999 ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01002000 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002001 lcd.light_tempo = FLASH_LIGHT_TEMPO;
2002 } else if (lcd.light_tempo > 0) {
2003 lcd.light_tempo--;
Sirnam Swetha832bf282015-10-27 14:56:13 +05302004 if (lcd.light_tempo == 0 &&
2005 ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01002006 lcd_backlight(0);
2007 }
2008 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002009
Willy Tarreau698b1512008-11-22 11:29:33 +01002010 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08002011}
2012
Willy Tarreau698b1512008-11-22 11:29:33 +01002013static void init_scan_timer(void)
2014{
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002015 if (scan_timer.function)
Willy Tarreau698b1512008-11-22 11:29:33 +01002016 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08002017
Aya Mahfouz8f6e36c2015-02-19 07:58:29 +02002018 setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
Willy Tarreau698b1512008-11-22 11:29:33 +01002019 scan_timer.expires = jiffies + INPUT_POLL_TIME;
Willy Tarreau698b1512008-11-22 11:29:33 +01002020 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002021}
2022
2023/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002024 * if <omask> or <imask> are non-null, they will be or'ed with the bits
2025 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08002026 * returns 1 if ok, 0 if error (in which case, nothing is written).
2027 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01002028static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01002029 u8 *imask, u8 *omask)
Willy Tarreau698b1512008-11-22 11:29:33 +01002030{
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002031 const char sigtab[] = "EeSsPpAaBb";
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01002032 u8 im, om;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01002033 __u64 m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08002034
Ksenija Stanojevicd12f27e2016-01-03 20:42:26 +01002035 om = 0;
2036 im = 0;
Dominique van den Broeck2d534262014-05-24 01:35:24 +02002037 m = 0ULL;
2038 v = 0ULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002039 while (*name) {
2040 int in, out, bit, neg;
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002041 const char *idx;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002042
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002043 idx = strchr(sigtab, *name);
2044 if (!idx)
Willy Tarreau698b1512008-11-22 11:29:33 +01002045 return 0; /* input name not found */
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002046
2047 in = idx - sigtab;
Willy Tarreau698b1512008-11-22 11:29:33 +01002048 neg = (in & 1); /* odd (lower) names are negated */
2049 in >>= 1;
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05302050 im |= BIT(in);
Willy Tarreau7005b582008-11-13 17:18:59 -08002051
Willy Tarreau698b1512008-11-22 11:29:33 +01002052 name++;
Ksenija Stanojevic52ebf932016-01-03 20:43:27 +01002053 if (*name >= '0' && *name <= '7') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002054 out = *name - '0';
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05302055 om |= BIT(out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002056 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002057 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002058 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01002059 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002060 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002061
2062 bit = (out * 5) + in;
2063
2064 m |= 1ULL << bit;
2065 if (!neg)
2066 v |= 1ULL << bit;
2067 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08002068 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002069 *mask = m;
2070 *value = v;
2071 if (imask)
2072 *imask |= im;
2073 if (omask)
2074 *omask |= om;
2075 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002076}
2077
2078/* tries to bind a key to the signal name <name>. The key will send the
2079 * strings <press>, <repeat>, <release> for these respective events.
2080 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2081 */
Peter Huewe36d20412013-02-15 13:47:05 +01002082static struct logical_input *panel_bind_key(const char *name, const char *press,
2083 const char *repeat,
2084 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01002085{
2086 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002087
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002088 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002089 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01002090 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002091
Willy Tarreau698b1512008-11-22 11:29:33 +01002092 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002093 &scan_mask_o)) {
2094 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01002095 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002096 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002097
2098 key->type = INPUT_TYPE_KBD;
2099 key->state = INPUT_ST_LOW;
2100 key->rise_time = 1;
2101 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002102
Willy Tarreau698b1512008-11-22 11:29:33 +01002103 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2104 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2105 strncpy(key->u.kbd.release_str, release,
2106 sizeof(key->u.kbd.release_str));
2107 list_add(&key->list, &logical_inputs);
2108 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002109}
2110
Willy Tarreau630231772008-11-22 12:52:18 +01002111#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002112/* tries to bind a callback function to the signal name <name>. The function
2113 * <press_fct> will be called with the <press_data> arg when the signal is
2114 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002115 * Returns the pointer to the new signal if ok, NULL if the signal could not
2116 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002117 */
2118static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302119 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002120 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302121 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002122 int release_data)
2123{
2124 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002125
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002126 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002127 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002128 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002129
Willy Tarreau698b1512008-11-22 11:29:33 +01002130 memset(callback, 0, sizeof(struct logical_input));
2131 if (!input_name2mask(name, &callback->mask, &callback->value,
2132 &scan_mask_i, &scan_mask_o))
2133 return NULL;
2134
2135 callback->type = INPUT_TYPE_STD;
2136 callback->state = INPUT_ST_LOW;
2137 callback->rise_time = 1;
2138 callback->fall_time = 1;
2139 callback->u.std.press_fct = press_fct;
2140 callback->u.std.press_data = press_data;
2141 callback->u.std.release_fct = release_fct;
2142 callback->u.std.release_data = release_data;
2143 list_add(&callback->list, &logical_inputs);
2144 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002145}
Willy Tarreau630231772008-11-22 12:52:18 +01002146#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002147
Willy Tarreau698b1512008-11-22 11:29:33 +01002148static void keypad_init(void)
2149{
2150 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002151
Willy Tarreau698b1512008-11-22 11:29:33 +01002152 init_waitqueue_head(&keypad_read_wait);
2153 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002154
Willy Tarreau698b1512008-11-22 11:29:33 +01002155 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002156
Willy Tarreau698b1512008-11-22 11:29:33 +01002157 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2158 panel_bind_key(keypad_profile[keynum][0],
2159 keypad_profile[keynum][1],
2160 keypad_profile[keynum][2],
2161 keypad_profile[keynum][3]);
2162 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002163
Willy Tarreau698b1512008-11-22 11:29:33 +01002164 init_scan_timer();
2165 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002166}
2167
Willy Tarreau7005b582008-11-13 17:18:59 -08002168/**************************************************/
2169/* device initialization */
2170/**************************************************/
2171
Willy Tarreau698b1512008-11-22 11:29:33 +01002172static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2173 void *unused)
2174{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002175 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002176 switch (code) {
2177 case SYS_DOWN:
2178 panel_lcd_print
2179 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2180 break;
2181 case SYS_HALT:
2182 panel_lcd_print
2183 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2184 break;
2185 case SYS_POWER_OFF:
2186 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2187 break;
2188 default:
2189 break;
2190 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002191 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002192 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002193}
2194
2195static struct notifier_block panel_notifier = {
2196 panel_notify_sys,
2197 NULL,
2198 0
2199};
2200
Willy Tarreau698b1512008-11-22 11:29:33 +01002201static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002202{
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302203 struct pardev_cb panel_cb;
2204
Willy Tarreau698b1512008-11-22 11:29:33 +01002205 if (port->number != parport)
2206 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002207
Willy Tarreau698b1512008-11-22 11:29:33 +01002208 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002209 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2210 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002211 return;
2212 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002213
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302214 memset(&panel_cb, 0, sizeof(panel_cb));
2215 panel_cb.private = &pprt;
2216 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
2217
2218 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002219 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002220 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2221 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002222 return;
2223 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002224
Willy Tarreau698b1512008-11-22 11:29:33 +01002225 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002226 pr_err("could not claim access to parport%d. Aborting.\n",
2227 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002228 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002229 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002230
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002231 /* must init LCD first, just in case an IRQ from the keypad is
2232 * generated at keypad init
2233 */
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002234 if (lcd.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002235 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002236 if (misc_register(&lcd_dev))
2237 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002238 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002239
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002240 if (keypad.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002241 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002242 if (misc_register(&keypad_dev))
2243 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002244 }
Sudip Mukherjeebb046fe2015-03-09 20:08:23 +05302245 register_reboot_notifier(&panel_notifier);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002246 return;
2247
2248err_lcd_unreg:
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002249 if (lcd.enabled)
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002250 misc_deregister(&lcd_dev);
2251err_unreg_device:
2252 parport_unregister_device(pprt);
2253 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002254}
2255
Willy Tarreau698b1512008-11-22 11:29:33 +01002256static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002257{
Willy Tarreau698b1512008-11-22 11:29:33 +01002258 if (port->number != parport)
2259 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002260
Willy Tarreau698b1512008-11-22 11:29:33 +01002261 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002262 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2263 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002264 return;
2265 }
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002266 if (scan_timer.function)
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302267 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002268
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002269 if (pprt) {
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302270 if (keypad.enabled) {
2271 misc_deregister(&keypad_dev);
2272 keypad_initialized = 0;
2273 }
Sudip Mukherjeebb046fe2015-03-09 20:08:23 +05302274
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302275 if (lcd.enabled) {
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01002276 panel_lcd_print("\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302277 misc_deregister(&lcd_dev);
2278 lcd.initialized = false;
2279 }
2280
2281 /* TODO: free all input signals */
2282 parport_release(pprt);
2283 parport_unregister_device(pprt);
2284 pprt = NULL;
2285 unregister_reboot_notifier(&panel_notifier);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002286 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002287}
2288
2289static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002290 .name = "panel",
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302291 .match_port = panel_attach,
Willy Tarreau698b1512008-11-22 11:29:33 +01002292 .detach = panel_detach,
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302293 .devmodel = true,
Willy Tarreau7005b582008-11-13 17:18:59 -08002294};
2295
2296/* init function */
Mariusz Gorskid9114762014-11-27 22:36:46 +01002297static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002298{
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302299 int selected_keypad_type = NOT_SET, err;
Willy Tarreau7005b582008-11-13 17:18:59 -08002300
Willy Tarreau698b1512008-11-22 11:29:33 +01002301 /* take care of an eventual profile */
2302 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002303 case PANEL_PROFILE_CUSTOM:
2304 /* custom profile */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002305 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2306 selected_lcd_type = DEFAULT_LCD_TYPE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002307 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002308 case PANEL_PROFILE_OLD:
2309 /* 8 bits, 2*16, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002310 selected_keypad_type = KEYPAD_TYPE_OLD;
2311 selected_lcd_type = LCD_TYPE_OLD;
2312
2313 /* TODO: This two are a little hacky, sort it out later */
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002314 if (lcd_width == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002315 lcd_width = 16;
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002316 if (lcd_hwidth == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002317 lcd_hwidth = 16;
2318 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002319 case PANEL_PROFILE_NEW:
2320 /* serial, 2*16, new keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002321 selected_keypad_type = KEYPAD_TYPE_NEW;
2322 selected_lcd_type = LCD_TYPE_KS0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01002323 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002324 case PANEL_PROFILE_HANTRONIX:
2325 /* 8 bits, 2*16 hantronix-like, no keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002326 selected_keypad_type = KEYPAD_TYPE_NONE;
2327 selected_lcd_type = LCD_TYPE_HANTRONIX;
Willy Tarreau698b1512008-11-22 11:29:33 +01002328 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002329 case PANEL_PROFILE_NEXCOM:
2330 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002331 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2332 selected_lcd_type = LCD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002333 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002334 case PANEL_PROFILE_LARGE:
2335 /* 8 bits, 2*40, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002336 selected_keypad_type = KEYPAD_TYPE_OLD;
2337 selected_lcd_type = LCD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002338 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002339 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002340
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01002341 /*
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002342 * Overwrite selection with module param values (both keypad and lcd),
2343 * where the deprecated params have lower prio.
2344 */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002345 if (keypad_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002346 selected_keypad_type = keypad_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002347 if (keypad_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002348 selected_keypad_type = keypad_type;
Willy Tarreau7005b582008-11-13 17:18:59 -08002349
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002350 keypad.enabled = (selected_keypad_type > 0);
2351
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002352 if (lcd_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002353 selected_lcd_type = lcd_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002354 if (lcd_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002355 selected_lcd_type = lcd_type;
2356
2357 lcd.enabled = (selected_lcd_type > 0);
2358
Sudip Mukherjee733345e2015-02-11 16:57:08 +05302359 if (lcd.enabled) {
2360 /*
2361 * Init lcd struct with load-time values to preserve exact
2362 * current functionality (at least for now).
2363 */
2364 lcd.height = lcd_height;
2365 lcd.width = lcd_width;
2366 lcd.bwidth = lcd_bwidth;
2367 lcd.hwidth = lcd_hwidth;
2368 lcd.charset = lcd_charset;
2369 lcd.proto = lcd_proto;
2370 lcd.pins.e = lcd_e_pin;
2371 lcd.pins.rs = lcd_rs_pin;
2372 lcd.pins.rw = lcd_rw_pin;
2373 lcd.pins.cl = lcd_cl_pin;
2374 lcd.pins.da = lcd_da_pin;
2375 lcd.pins.bl = lcd_bl_pin;
2376
2377 /* Leave it for now, just in case */
2378 lcd.esc_seq.len = -1;
2379 }
2380
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002381 switch (selected_keypad_type) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002382 case KEYPAD_TYPE_OLD:
2383 keypad_profile = old_keypad_profile;
2384 break;
2385 case KEYPAD_TYPE_NEW:
2386 keypad_profile = new_keypad_profile;
2387 break;
2388 case KEYPAD_TYPE_NEXCOM:
2389 keypad_profile = nexcom_keypad_profile;
2390 break;
2391 default:
2392 keypad_profile = NULL;
2393 break;
2394 }
2395
Sudip Mukherjeef43de772015-02-10 17:26:02 +05302396 if (!lcd.enabled && !keypad.enabled) {
2397 /* no device enabled, let's exit */
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01002398 pr_err("panel driver disabled.\n");
Sudip Mukherjeef43de772015-02-10 17:26:02 +05302399 return -ENODEV;
2400 }
2401
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302402 err = parport_register_driver(&panel_driver);
2403 if (err) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002404 pr_err("could not register with parport. Aborting.\n");
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302405 return err;
Willy Tarreau698b1512008-11-22 11:29:33 +01002406 }
2407
Willy Tarreau698b1512008-11-22 11:29:33 +01002408 if (pprt)
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01002409 pr_info("panel driver registered on parport%d (io=0x%lx).\n",
2410 parport, pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002411 else
Geert Uytterhoeven30f468b2017-02-06 15:38:04 +01002412 pr_info("panel driver not yet registered\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002413 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002414}
2415
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002416static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002417{
Willy Tarreau698b1512008-11-22 11:29:33 +01002418 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002419}
Willy Tarreau7005b582008-11-13 17:18:59 -08002420
Willy Tarreau7005b582008-11-13 17:18:59 -08002421module_init(panel_init_module);
2422module_exit(panel_cleanup_module);
2423MODULE_AUTHOR("Willy Tarreau");
2424MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002425
2426/*
2427 * Local variables:
2428 * c-indent-level: 4
2429 * tab-width: 8
2430 * End:
2431 */