blob: ea54fb4ec837ea737653fc77fff786d2d38e7d2a [file] [log] [blame]
Willy Tarreau7005b582008-11-13 17:18:59 -08001/*
Willy Tarreau698b1512008-11-22 11:29:33 +01002 * Front panel driver for Linux
3 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
Willy Tarreau7005b582008-11-13 17:18:59 -08004 *
Willy Tarreau698b1512008-11-22 11:29:33 +01005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
Willy Tarreau7005b582008-11-13 17:18:59 -08009 *
Willy Tarreau698b1512008-11-22 11:29:33 +010010 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
11 * connected to a parallel printer port.
Willy Tarreau7005b582008-11-13 17:18:59 -080012 *
Willy Tarreau698b1512008-11-22 11:29:33 +010013 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
14 * serial module compatible with Samsung's KS0074. The pins may be connected in
15 * any combination, everything is programmable.
Willy Tarreau7005b582008-11-13 17:18:59 -080016 *
Willy Tarreau698b1512008-11-22 11:29:33 +010017 * The keypad consists in a matrix of push buttons connecting input pins to
18 * data output pins or to the ground. The combinations have to be hard-coded
19 * in the driver, though several profiles exist and adding new ones is easy.
Willy Tarreau7005b582008-11-13 17:18:59 -080020 *
Willy Tarreau698b1512008-11-22 11:29:33 +010021 * Several profiles are provided for commonly found LCD+keypad modules on the
22 * market, such as those found in Nexcom's appliances.
Willy Tarreau7005b582008-11-13 17:18:59 -080023 *
24 * FIXME:
25 * - the initialization/deinitialization process is very dirty and should
26 * be rewritten. It may even be buggy.
27 *
28 * TODO:
29 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
30 * - make the LCD a part of a virtual screen of Vx*Vy
31 * - make the inputs list smp-safe
32 * - change the keyboard to a double mapping : signals -> key_id -> values
33 * so that applications can change values without knowing signals
34 *
35 */
36
Toshiaki Yamane493aa892012-07-12 22:01:00 +090037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Willy Tarreau7005b582008-11-13 17:18:59 -080039#include <linux/module.h>
40
41#include <linux/types.h>
42#include <linux/errno.h>
43#include <linux/signal.h>
44#include <linux/sched.h>
45#include <linux/spinlock.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080046#include <linux/interrupt.h>
47#include <linux/miscdevice.h>
Willy Tarreau698b1512008-11-22 11:29:33 +010048#include <linux/slab.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080049#include <linux/ioport.h>
50#include <linux/fcntl.h>
51#include <linux/init.h>
52#include <linux/delay.h>
Andy Shevchenkod85170e2010-07-22 19:57:08 +030053#include <linux/kernel.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080054#include <linux/ctype.h>
55#include <linux/parport.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080056#include <linux/list.h>
57#include <linux/notifier.h>
58#include <linux/reboot.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020059#include <generated/utsrelease.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080060
Willy Tarreau698b1512008-11-22 11:29:33 +010061#include <linux/io.h>
Takanori Suzuki48f658b2010-05-08 22:56:24 +090062#include <linux/uaccess.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080063
Willy Tarreau7005b582008-11-13 17:18:59 -080064#define LCD_MINOR 156
65#define KEYPAD_MINOR 185
Willy Tarreau7005b582008-11-13 17:18:59 -080066
67#define PANEL_VERSION "0.9.5"
68
69#define LCD_MAXBYTES 256 /* max burst write */
70
Willy Tarreau7005b582008-11-13 17:18:59 -080071#define KEYPAD_BUFFER 64
Willy Tarreau7005b582008-11-13 17:18:59 -080072
Henri Häkkinen429ccf02010-06-12 00:27:36 +030073/* poll the keyboard this every second */
74#define INPUT_POLL_TIME (HZ/50)
75/* a key starts to repeat after this times INPUT_POLL_TIME */
76#define KEYPAD_REP_START (10)
77/* a key repeats this times INPUT_POLL_TIME */
78#define KEYPAD_REP_DELAY (2)
79
80/* keep the light on this times INPUT_POLL_TIME for each flash */
81#define FLASH_LIGHT_TEMPO (200)
Willy Tarreau7005b582008-11-13 17:18:59 -080082
83/* converts an r_str() input to an active high, bits string : 000BAOSE */
84#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
85
Willy Tarreau698b1512008-11-22 11:29:33 +010086#define PNL_PBUSY 0x80 /* inverted input, active low */
87#define PNL_PACK 0x40 /* direct input, active low */
88#define PNL_POUTPA 0x20 /* direct input, active high */
89#define PNL_PSELECD 0x10 /* direct input, active high */
90#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080091
Willy Tarreau698b1512008-11-22 11:29:33 +010092#define PNL_PBIDIR 0x20 /* bi-directional ports */
Henri Häkkinen429ccf02010-06-12 00:27:36 +030093/* high to read data in or-ed with data out */
94#define PNL_PINTEN 0x10
Willy Tarreau698b1512008-11-22 11:29:33 +010095#define PNL_PSELECP 0x08 /* inverted output, active low */
96#define PNL_PINITP 0x04 /* direct output, active low */
97#define PNL_PAUTOLF 0x02 /* inverted output, active low */
98#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080099
100#define PNL_PD0 0x01
101#define PNL_PD1 0x02
102#define PNL_PD2 0x04
103#define PNL_PD3 0x08
104#define PNL_PD4 0x10
105#define PNL_PD5 0x20
106#define PNL_PD6 0x40
107#define PNL_PD7 0x80
108
109#define PIN_NONE 0
110#define PIN_STROBE 1
111#define PIN_D0 2
112#define PIN_D1 3
113#define PIN_D2 4
114#define PIN_D3 5
115#define PIN_D4 6
116#define PIN_D5 7
117#define PIN_D6 8
118#define PIN_D7 9
119#define PIN_AUTOLF 14
120#define PIN_INITP 16
121#define PIN_SELECP 17
122#define PIN_NOT_SET 127
123
Willy Tarreau7005b582008-11-13 17:18:59 -0800124#define LCD_FLAG_S 0x0001
125#define LCD_FLAG_ID 0x0002
126#define LCD_FLAG_B 0x0004 /* blink on */
127#define LCD_FLAG_C 0x0008 /* cursor on */
128#define LCD_FLAG_D 0x0010 /* display on */
129#define LCD_FLAG_F 0x0020 /* large font mode */
130#define LCD_FLAG_N 0x0040 /* 2-rows mode */
131#define LCD_FLAG_L 0x0080 /* backlight enabled */
132
Mariusz Gorski21149242014-12-05 22:28:19 +0100133/* LCD commands */
134#define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
135
136#define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
137#define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
138
139#define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
140#define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
141#define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
142#define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
143
144#define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
145#define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
146#define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
147
148#define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
149#define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
150#define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
151#define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
152
153#define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
154
155#define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
156
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300157#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
Willy Tarreau7005b582008-11-13 17:18:59 -0800158#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
159
Mariusz Gorski36277d42014-11-27 22:36:47 +0100160#define NOT_SET -1
161
Willy Tarreau7005b582008-11-13 17:18:59 -0800162/* macros to simplify use of the parallel port */
163#define r_ctr(x) (parport_read_control((x)->port))
164#define r_dtr(x) (parport_read_data((x)->port))
165#define r_str(x) (parport_read_status((x)->port))
Toshiaki Yamane6ebb56d2012-07-25 12:18:12 +0900166#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
167#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
Willy Tarreau7005b582008-11-13 17:18:59 -0800168
169/* this defines which bits are to be used and which ones to be ignored */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300170/* logical or of the output bits involved in the scan matrix */
171static __u8 scan_mask_o;
172/* logical or of the input bits involved in the scan matrix */
173static __u8 scan_mask_i;
Willy Tarreau7005b582008-11-13 17:18:59 -0800174
Willy Tarreau698b1512008-11-22 11:29:33 +0100175typedef __u64 pmask_t;
Willy Tarreau7005b582008-11-13 17:18:59 -0800176
177enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100178 INPUT_TYPE_STD,
179 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800180};
181
182enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100183 INPUT_ST_LOW,
184 INPUT_ST_RISING,
185 INPUT_ST_HIGH,
186 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800187};
188
189struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100190 struct list_head list;
191 pmask_t mask;
192 pmask_t value;
193 enum input_type type;
194 enum input_state state;
195 __u8 rise_time, fall_time;
196 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800197
Willy Tarreau698b1512008-11-22 11:29:33 +0100198 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300199 struct { /* valid when type == INPUT_TYPE_STD */
Monam Agarwal68d386b2014-02-25 19:40:49 +0530200 void (*press_fct)(int);
201 void (*release_fct)(int);
Willy Tarreau698b1512008-11-22 11:29:33 +0100202 int press_data;
203 int release_data;
204 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300205 struct { /* valid when type == INPUT_TYPE_KBD */
206 /* strings can be non null-terminated */
Willy Tarreau698b1512008-11-22 11:29:33 +0100207 char press_str[sizeof(void *) + sizeof(int)];
208 char repeat_str[sizeof(void *) + sizeof(int)];
209 char release_str[sizeof(void *) + sizeof(int)];
210 } kbd;
211 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800212};
213
Peter Huewe36d20412013-02-15 13:47:05 +0100214static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800215
216/* physical contacts history
217 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
218 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
219 * corresponds to the ground.
220 * Within each group, bits are stored in the same order as read on the port :
221 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
222 * So, each __u64 (or pmask_t) is represented like this :
223 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
224 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
225 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300226
227/* what has just been read from the I/O ports */
228static pmask_t phys_read;
229/* previous phys_read */
230static pmask_t phys_read_prev;
231/* stabilized phys_read (phys_read|phys_read_prev) */
232static pmask_t phys_curr;
233/* previous phys_curr */
234static pmask_t phys_prev;
235/* 0 means that at least one logical signal needs be computed */
236static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800237
Willy Tarreau7005b582008-11-13 17:18:59 -0800238/* these variables are specific to the keypad */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100239static struct {
240 bool enabled;
241} keypad;
242
Willy Tarreau7005b582008-11-13 17:18:59 -0800243static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100244static int keypad_buflen;
245static int keypad_start;
246static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800247static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800248
249/* lcd-specific variables */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100250static struct {
251 bool enabled;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100252 bool initialized;
253 bool must_clear;
254
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100255 int height;
256 int width;
257 int bwidth;
258 int hwidth;
259 int charset;
260 int proto;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100261 int light_tempo;
262
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100263 /* TODO: use union here? */
264 struct {
265 int e;
266 int rs;
267 int rw;
268 int cl;
269 int da;
270 int bl;
271 } pins;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100272
273 /* contains the LCD config state */
274 unsigned long int flags;
275
276 /* Contains the LCD X and Y offset */
277 struct {
278 unsigned long int x;
279 unsigned long int y;
280 } addr;
281
282 /* Current escape sequence and it's length or -1 if outside */
283 struct {
284 char buf[LCD_ESCAPE_LEN + 1];
285 int len;
286 } esc_seq;
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100287} lcd;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300288
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +0100289/* Needed only for init */
290static int selected_lcd_type = NOT_SET;
291
Willy Tarreau7005b582008-11-13 17:18:59 -0800292/*
293 * Bit masks to convert LCD signals to parallel port outputs.
294 * _d_ are values for data port, _c_ are for control port.
295 * [0] = signal OFF, [1] = signal ON, [2] = mask
296 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100297#define BIT_CLR 0
298#define BIT_SET 1
299#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800300#define BIT_STATES 3
301/*
302 * one entry for each bit on the LCD
303 */
304#define LCD_BIT_E 0
305#define LCD_BIT_RS 1
306#define LCD_BIT_RW 2
307#define LCD_BIT_BL 3
308#define LCD_BIT_CL 4
309#define LCD_BIT_DA 5
310#define LCD_BITS 6
311
312/*
313 * each bit can be either connected to a DATA or CTRL port
314 */
315#define LCD_PORT_C 0
316#define LCD_PORT_D 1
317#define LCD_PORTS 2
318
319static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
320
321/*
322 * LCD protocols
323 */
324#define LCD_PROTO_PARALLEL 0
325#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400326#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800327
328/*
329 * LCD character sets
330 */
331#define LCD_CHARSET_NORMAL 0
332#define LCD_CHARSET_KS0074 1
333
334/*
335 * LCD types
336 */
337#define LCD_TYPE_NONE 0
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530338#define LCD_TYPE_CUSTOM 1
339#define LCD_TYPE_OLD 2
340#define LCD_TYPE_KS0074 3
341#define LCD_TYPE_HANTRONIX 4
342#define LCD_TYPE_NEXCOM 5
Willy Tarreau7005b582008-11-13 17:18:59 -0800343
344/*
345 * keypad types
346 */
347#define KEYPAD_TYPE_NONE 0
348#define KEYPAD_TYPE_OLD 1
349#define KEYPAD_TYPE_NEW 2
350#define KEYPAD_TYPE_NEXCOM 3
351
352/*
353 * panel profiles
354 */
355#define PANEL_PROFILE_CUSTOM 0
356#define PANEL_PROFILE_OLD 1
357#define PANEL_PROFILE_NEW 2
358#define PANEL_PROFILE_HANTRONIX 3
359#define PANEL_PROFILE_NEXCOM 4
360#define PANEL_PROFILE_LARGE 5
361
362/*
363 * Construct custom config from the kernel's configuration
364 */
Willy Tarreau7005b582008-11-13 17:18:59 -0800365#define DEFAULT_PARPORT 0
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100366#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100367#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
368#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100369#define DEFAULT_LCD_HEIGHT 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800370#define DEFAULT_LCD_WIDTH 40
371#define DEFAULT_LCD_BWIDTH 40
372#define DEFAULT_LCD_HWIDTH 64
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100373#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
Willy Tarreau7005b582008-11-13 17:18:59 -0800374#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
375
376#define DEFAULT_LCD_PIN_E PIN_AUTOLF
377#define DEFAULT_LCD_PIN_RS PIN_SELECP
378#define DEFAULT_LCD_PIN_RW PIN_INITP
379#define DEFAULT_LCD_PIN_SCL PIN_STROBE
380#define DEFAULT_LCD_PIN_SDA PIN_D0
381#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
Willy Tarreau7005b582008-11-13 17:18:59 -0800382
Willy Tarreau7005b582008-11-13 17:18:59 -0800383#ifdef CONFIG_PANEL_PARPORT
384#undef DEFAULT_PARPORT
385#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
386#endif
387
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100388#ifdef CONFIG_PANEL_PROFILE
389#undef DEFAULT_PROFILE
390#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
391#endif
392
Willy Tarreau698b1512008-11-22 11:29:33 +0100393#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800394#ifdef CONFIG_PANEL_KEYPAD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100395#undef DEFAULT_KEYPAD_TYPE
396#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
Willy Tarreau7005b582008-11-13 17:18:59 -0800397#endif
398
Willy Tarreau7005b582008-11-13 17:18:59 -0800399#ifdef CONFIG_PANEL_LCD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100400#undef DEFAULT_LCD_TYPE
401#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
Willy Tarreau7005b582008-11-13 17:18:59 -0800402#endif
403
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100404#ifdef CONFIG_PANEL_LCD_HEIGHT
405#undef DEFAULT_LCD_HEIGHT
406#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
407#endif
408
Willy Tarreau7005b582008-11-13 17:18:59 -0800409#ifdef CONFIG_PANEL_LCD_WIDTH
410#undef DEFAULT_LCD_WIDTH
411#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
412#endif
413
414#ifdef CONFIG_PANEL_LCD_BWIDTH
415#undef DEFAULT_LCD_BWIDTH
416#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
417#endif
418
419#ifdef CONFIG_PANEL_LCD_HWIDTH
420#undef DEFAULT_LCD_HWIDTH
421#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
422#endif
423
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100424#ifdef CONFIG_PANEL_LCD_CHARSET
425#undef DEFAULT_LCD_CHARSET
426#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800427#endif
428
429#ifdef CONFIG_PANEL_LCD_PROTO
430#undef DEFAULT_LCD_PROTO
431#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
432#endif
433
434#ifdef CONFIG_PANEL_LCD_PIN_E
435#undef DEFAULT_LCD_PIN_E
436#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
437#endif
438
439#ifdef CONFIG_PANEL_LCD_PIN_RS
440#undef DEFAULT_LCD_PIN_RS
441#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
442#endif
443
444#ifdef CONFIG_PANEL_LCD_PIN_RW
445#undef DEFAULT_LCD_PIN_RW
446#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
447#endif
448
449#ifdef CONFIG_PANEL_LCD_PIN_SCL
450#undef DEFAULT_LCD_PIN_SCL
451#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
452#endif
453
454#ifdef CONFIG_PANEL_LCD_PIN_SDA
455#undef DEFAULT_LCD_PIN_SDA
456#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
457#endif
458
459#ifdef CONFIG_PANEL_LCD_PIN_BL
460#undef DEFAULT_LCD_PIN_BL
461#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
462#endif
463
Willy Tarreau7005b582008-11-13 17:18:59 -0800464#endif /* DEFAULT_PROFILE == 0 */
465
466/* global variables */
Mariusz Gorskif4757af2014-11-04 22:47:19 +0100467
468/* Device single-open policy control */
469static atomic_t lcd_available = ATOMIC_INIT(1);
470static atomic_t keypad_available = ATOMIC_INIT(1);
471
Willy Tarreau698b1512008-11-22 11:29:33 +0100472static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800473
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100474static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800475
Monam Agarwal68d386b2014-02-25 19:40:49 +0530476static void (*lcd_write_cmd)(int);
477static void (*lcd_write_data)(int);
478static void (*lcd_clear_fast)(void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800479
Willy Tarreau698b1512008-11-22 11:29:33 +0100480static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800481static struct timer_list scan_timer;
482
Willy Tarreau630231772008-11-22 12:52:18 +0100483MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100484
Mariusz Gorski59a66a22014-11-27 22:36:45 +0100485static int parport = DEFAULT_PARPORT;
Willy Tarreau698b1512008-11-22 11:29:33 +0100486module_param(parport, int, 0000);
487MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100488
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100489static int profile = DEFAULT_PROFILE;
490module_param(profile, int, 0000);
491MODULE_PARM_DESC(profile,
492 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
493 "4=16x2 nexcom; default=40x2, old kp");
494
Mariusz Gorski36277d42014-11-27 22:36:47 +0100495static int keypad_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100496module_param(keypad_type, int, 0000);
497MODULE_PARM_DESC(keypad_type,
498 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
499
Mariusz Gorski36277d42014-11-27 22:36:47 +0100500static int lcd_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100501module_param(lcd_type, int, 0000);
502MODULE_PARM_DESC(lcd_type,
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530503 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100504
Mariusz Gorski36277d42014-11-27 22:36:47 +0100505static int lcd_height = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100506module_param(lcd_height, int, 0000);
507MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100508
Mariusz Gorski36277d42014-11-27 22:36:47 +0100509static int lcd_width = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100510module_param(lcd_width, int, 0000);
511MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100512
Mariusz Gorski36277d42014-11-27 22:36:47 +0100513static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100514module_param(lcd_bwidth, int, 0000);
515MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100516
Mariusz Gorski36277d42014-11-27 22:36:47 +0100517static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100518module_param(lcd_hwidth, int, 0000);
519MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100520
Mariusz Gorski36277d42014-11-27 22:36:47 +0100521static int lcd_charset = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100522module_param(lcd_charset, int, 0000);
523MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100524
Mariusz Gorski36277d42014-11-27 22:36:47 +0100525static int lcd_proto = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100526module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300527MODULE_PARM_DESC(lcd_proto,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +0200528 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100529
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100530/*
531 * These are the parallel port pins the LCD control signals are connected to.
532 * Set this to 0 if the signal is not used. Set it to its opposite value
533 * (negative) if the signal is negated. -MAXINT is used to indicate that the
534 * pin has not been explicitly specified.
535 *
Willy Tarreau630231772008-11-22 12:52:18 +0100536 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100537 */
538
539static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100540module_param(lcd_e_pin, int, 0000);
541MODULE_PARM_DESC(lcd_e_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530542 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100543
544static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100545module_param(lcd_rs_pin, int, 0000);
546MODULE_PARM_DESC(lcd_rs_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530547 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100548
549static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100550module_param(lcd_rw_pin, int, 0000);
551MODULE_PARM_DESC(lcd_rw_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530552 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100553
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100554static int lcd_cl_pin = PIN_NOT_SET;
555module_param(lcd_cl_pin, int, 0000);
556MODULE_PARM_DESC(lcd_cl_pin,
557 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100558
559static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100560module_param(lcd_da_pin, int, 0000);
561MODULE_PARM_DESC(lcd_da_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530562 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100563
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100564static int lcd_bl_pin = PIN_NOT_SET;
565module_param(lcd_bl_pin, int, 0000);
566MODULE_PARM_DESC(lcd_bl_pin,
567 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
568
569/* Deprecated module parameters - consider not using them anymore */
570
Mariusz Gorski36277d42014-11-27 22:36:47 +0100571static int lcd_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100572module_param(lcd_enabled, int, 0000);
573MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
574
Mariusz Gorski36277d42014-11-27 22:36:47 +0100575static int keypad_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100576module_param(keypad_enabled, int, 0000);
577MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
578
Willy Tarreau7005b582008-11-13 17:18:59 -0800579
Peter Huewe36d20412013-02-15 13:47:05 +0100580static const unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800581
582/* for some LCD drivers (ks0074) we need a charset conversion table. */
Peter Huewe36d20412013-02-15 13:47:05 +0100583static const unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100584 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
585 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
586 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
587 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
588 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
589 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
590 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
591 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
592 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
593 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
594 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
595 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
596 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
597 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
598 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
599 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
600 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
601 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
602 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
603 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
604 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
605 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
606 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
607 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
608 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
609 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
610 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
611 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
612 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
613 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
614 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
615 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
616 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800617};
618
Peter Huewe36d20412013-02-15 13:47:05 +0100619static const char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100620 {"S0", "Left\n", "Left\n", ""},
621 {"S1", "Down\n", "Down\n", ""},
622 {"S2", "Up\n", "Up\n", ""},
623 {"S3", "Right\n", "Right\n", ""},
624 {"S4", "Esc\n", "Esc\n", ""},
625 {"S5", "Ret\n", "Ret\n", ""},
626 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800627};
628
629/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100630static const char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100631 {"S0", "Left\n", "Left\n", ""},
632 {"S1", "Down\n", "Down\n", ""},
633 {"S2", "Up\n", "Up\n", ""},
634 {"S3", "Right\n", "Right\n", ""},
635 {"S4s5", "", "Esc\n", "Esc\n"},
636 {"s4S5", "", "Ret\n", "Ret\n"},
637 {"S4S5", "Help\n", "", ""},
638 /* add new signals above this line */
639 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800640};
641
642/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100643static const char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100644 {"a-p-e-", "Down\n", "Down\n", ""},
645 {"a-p-E-", "Ret\n", "Ret\n", ""},
646 {"a-P-E-", "Esc\n", "Esc\n", ""},
647 {"a-P-e-", "Up\n", "Up\n", ""},
648 /* add new signals above this line */
649 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800650};
651
Peter Huewe36d20412013-02-15 13:47:05 +0100652static const char (*keypad_profile)[4][9] = old_keypad_profile;
Willy Tarreau7005b582008-11-13 17:18:59 -0800653
654/* FIXME: this should be converted to a bit array containing signals states */
655static struct {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300656 unsigned char e; /* parallel LCD E (data latch on falling edge) */
657 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
658 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
659 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
660 unsigned char cl; /* serial LCD clock (latch on rising edge) */
661 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800662} bits;
663
664static void init_scan_timer(void);
665
666/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100667static int set_data_bits(void)
668{
669 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800670
Willy Tarreau698b1512008-11-22 11:29:33 +0100671 val = r_dtr(pprt);
672 for (bit = 0; bit < LCD_BITS; bit++)
673 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800674
Willy Tarreau698b1512008-11-22 11:29:33 +0100675 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
676 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
677 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
678 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
679 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
680 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800681
Willy Tarreau698b1512008-11-22 11:29:33 +0100682 w_dtr(pprt, val);
683 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800684}
685
686/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100687static int set_ctrl_bits(void)
688{
689 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800690
Willy Tarreau698b1512008-11-22 11:29:33 +0100691 val = r_ctr(pprt);
692 for (bit = 0; bit < LCD_BITS; bit++)
693 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800694
Willy Tarreau698b1512008-11-22 11:29:33 +0100695 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
696 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
697 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
698 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
699 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
700 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800701
Willy Tarreau698b1512008-11-22 11:29:33 +0100702 w_ctr(pprt, val);
703 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800704}
705
706/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530707static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100708{
709 set_data_bits();
710 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800711}
712
713/*
714 * Converts a parallel port pin (from -25 to 25) to data and control ports
715 * masks, and data and control port bits. The signal will be considered
716 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
717 *
718 * Result will be used this way :
719 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
720 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
721 */
Peter Huewe36d20412013-02-15 13:47:05 +0100722static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
Willy Tarreau698b1512008-11-22 11:29:33 +0100723{
724 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800725
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200726 d_val[0] = 0;
727 c_val[0] = 0;
728 d_val[1] = 0;
729 c_val[1] = 0;
730 d_val[2] = 0xFF;
731 c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800732
Willy Tarreau698b1512008-11-22 11:29:33 +0100733 if (pin == 0)
734 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800735
Willy Tarreau698b1512008-11-22 11:29:33 +0100736 inv = (pin < 0);
737 if (inv)
738 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800739
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200740 d_bit = 0;
741 c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800742
Willy Tarreau698b1512008-11-22 11:29:33 +0100743 switch (pin) {
744 case PIN_STROBE: /* strobe, inverted */
745 c_bit = PNL_PSTROBE;
746 inv = !inv;
747 break;
748 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
749 d_bit = 1 << (pin - 2);
750 break;
751 case PIN_AUTOLF: /* autofeed, inverted */
752 c_bit = PNL_PAUTOLF;
753 inv = !inv;
754 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300755 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100756 c_bit = PNL_PINITP;
757 break;
758 case PIN_SELECP: /* select_in, inverted */
759 c_bit = PNL_PSELECP;
760 inv = !inv;
761 break;
762 default: /* unknown pin, ignore */
763 break;
764 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800765
Willy Tarreau698b1512008-11-22 11:29:33 +0100766 if (c_bit) {
767 c_val[2] &= ~c_bit;
768 c_val[!inv] = c_bit;
769 } else if (d_bit) {
770 d_val[2] &= ~d_bit;
771 d_val[!inv] = d_bit;
772 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800773}
774
775/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100776static void long_sleep(int ms)
777{
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200778 if (in_interrupt()) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100779 mdelay(ms);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200780 } else {
Davidlohr Bueso2be90fe2015-01-26 02:15:02 -0800781 __set_current_state(TASK_INTERRUPTIBLE);
Willy Tarreau698b1512008-11-22 11:29:33 +0100782 schedule_timeout((ms * HZ + 999) / 1000);
783 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800784}
785
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300786/* send a serial byte to the LCD panel. The caller is responsible for locking
787 if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100788static void lcd_send_serial(int byte)
789{
790 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800791
Willy Tarreau698b1512008-11-22 11:29:33 +0100792 /* the data bit is set on D0, and the clock on STROBE.
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300793 * LCD reads D0 on STROBE's rising edge. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100794 for (bit = 0; bit < 8; bit++) {
795 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530796 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100797 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530798 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300799 udelay(2); /* maintain the data during 2 us before CLK up */
Willy Tarreau698b1512008-11-22 11:29:33 +0100800 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530801 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300802 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100803 byte >>= 1;
804 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800805}
806
807/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100808static void lcd_backlight(int on)
809{
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100810 if (lcd.pins.bl == PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +0100811 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800812
Justin P. Mattock6975e182012-03-19 08:17:52 -0700813 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800814 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100815 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530816 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800817 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800818}
819
820/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100821static void lcd_write_cmd_s(int cmd)
822{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800823 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100824 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
825 lcd_send_serial(cmd & 0x0F);
826 lcd_send_serial((cmd >> 4) & 0x0F);
827 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800828 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800829}
830
831/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100832static void lcd_write_data_s(int data)
833{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800834 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100835 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
836 lcd_send_serial(data & 0x0F);
837 lcd_send_serial((data >> 4) & 0x0F);
838 udelay(40); /* the shortest data takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800839 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800840}
841
842/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100843static void lcd_write_cmd_p8(int cmd)
844{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800845 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800846 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100847 w_dtr(pprt, cmd);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300848 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800849
Willy Tarreau698b1512008-11-22 11:29:33 +0100850 bits.e = BIT_SET;
851 bits.rs = BIT_CLR;
852 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800853 set_ctrl_bits();
854
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300855 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800856
857 bits.e = BIT_CLR;
858 set_ctrl_bits();
859
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300860 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800861 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100862}
Willy Tarreau7005b582008-11-13 17:18:59 -0800863
Willy Tarreau698b1512008-11-22 11:29:33 +0100864/* send data to the LCD panel in 8 bits parallel mode */
865static void lcd_write_data_p8(int data)
866{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800867 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100868 /* present the data to the data port */
869 w_dtr(pprt, data);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300870 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100871
872 bits.e = BIT_SET;
873 bits.rs = BIT_SET;
874 bits.rw = BIT_CLR;
875 set_ctrl_bits();
876
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300877 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100878
879 bits.e = BIT_CLR;
880 set_ctrl_bits();
881
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300882 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800883 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100884}
885
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400886/* send a command to the TI LCD panel */
887static void lcd_write_cmd_tilcd(int cmd)
888{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800889 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400890 /* present the data to the control port */
891 w_ctr(pprt, cmd);
892 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800893 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400894}
895
896/* send data to the TI LCD panel */
897static void lcd_write_data_tilcd(int data)
898{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800899 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400900 /* present the data to the data port */
901 w_dtr(pprt, data);
902 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800903 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400904}
905
Willy Tarreau698b1512008-11-22 11:29:33 +0100906static void lcd_gotoxy(void)
907{
Mariusz Gorski21149242014-12-05 22:28:19 +0100908 lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100909 | (lcd.addr.y ? lcd.hwidth : 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300910 /* we force the cursor to stay at the end of the
911 line if it wants to go farther */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100912 | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100913 (lcd.hwidth - 1) : lcd.bwidth - 1));
Willy Tarreau698b1512008-11-22 11:29:33 +0100914}
915
916static void lcd_print(char c)
917{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100918 if (lcd.addr.x < lcd.bwidth) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100919 if (lcd_char_conv != NULL)
920 c = lcd_char_conv[(unsigned char)c];
921 lcd_write_data(c);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100922 lcd.addr.x++;
Willy Tarreau698b1512008-11-22 11:29:33 +0100923 }
924 /* prevents the cursor from wrapping onto the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100925 if (lcd.addr.x == lcd.bwidth)
Willy Tarreau698b1512008-11-22 11:29:33 +0100926 lcd_gotoxy();
927}
928
929/* fills the display with spaces and resets X/Y */
930static void lcd_clear_fast_s(void)
931{
932 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200933
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100934 lcd.addr.x = 0;
935 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100936 lcd_gotoxy();
937
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800938 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100939 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100940 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
941 lcd_send_serial(' ' & 0x0F);
942 lcd_send_serial((' ' >> 4) & 0x0F);
943 udelay(40); /* the shortest data takes at least 40 us */
944 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800945 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100946
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100947 lcd.addr.x = 0;
948 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100949 lcd_gotoxy();
950}
951
952/* fills the display with spaces and resets X/Y */
953static void lcd_clear_fast_p8(void)
954{
955 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200956
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100957 lcd.addr.x = 0;
958 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100959 lcd_gotoxy();
960
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800961 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100962 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100963 /* present the data to the data port */
964 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300965
966 /* maintain the data during 20 us before the strobe */
967 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100968
969 bits.e = BIT_SET;
970 bits.rs = BIT_SET;
971 bits.rw = BIT_CLR;
972 set_ctrl_bits();
973
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300974 /* maintain the strobe during 40 us */
975 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100976
977 bits.e = BIT_CLR;
978 set_ctrl_bits();
979
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300980 /* the shortest data takes at least 45 us */
981 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100982 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800983 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100984
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100985 lcd.addr.x = 0;
986 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100987 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800988}
989
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400990/* fills the display with spaces and resets X/Y */
991static void lcd_clear_fast_tilcd(void)
992{
993 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200994
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100995 lcd.addr.x = 0;
996 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400997 lcd_gotoxy();
998
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800999 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001000 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001001 /* present the data to the data port */
1002 w_dtr(pprt, ' ');
1003 udelay(60);
1004 }
1005
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001006 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001007
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001008 lcd.addr.x = 0;
1009 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001010 lcd_gotoxy();
1011}
1012
Willy Tarreau7005b582008-11-13 17:18:59 -08001013/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +01001014static void lcd_clear_display(void)
1015{
Mariusz Gorski21149242014-12-05 22:28:19 +01001016 lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001017 lcd.addr.x = 0;
1018 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001019 /* we must wait a few milliseconds (15) */
1020 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -08001021}
1022
Willy Tarreau698b1512008-11-22 11:29:33 +01001023static void lcd_init_display(void)
1024{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001025 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001026 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -08001027
Willy Tarreau698b1512008-11-22 11:29:33 +01001028 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -08001029
Mariusz Gorski21149242014-12-05 22:28:19 +01001030 /* 8bits, 1 line, small fonts; let's do it 3 times */
1031 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001032 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001033 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001034 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001035 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001036 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001037
Mariusz Gorski21149242014-12-05 22:28:19 +01001038 /* set font height and lines number */
1039 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
1040 | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
1041 | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001042 );
1043 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001044
Mariusz Gorski21149242014-12-05 22:28:19 +01001045 /* display off, cursor off, blink off */
1046 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
Willy Tarreau698b1512008-11-22 11:29:33 +01001047 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001048
Mariusz Gorski21149242014-12-05 22:28:19 +01001049 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
1050 | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
1051 | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
1052 | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001053 );
Willy Tarreau7005b582008-11-13 17:18:59 -08001054
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001055 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08001056
Willy Tarreau698b1512008-11-22 11:29:33 +01001057 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001058
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001059 /* entry mode set : increment, cursor shifting */
Mariusz Gorski21149242014-12-05 22:28:19 +01001060 lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
Willy Tarreau7005b582008-11-13 17:18:59 -08001061
Willy Tarreau698b1512008-11-22 11:29:33 +01001062 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001063}
1064
1065/*
1066 * These are the file operation function for user access to /dev/lcd
1067 * This function can also be called from inside the kernel, by
1068 * setting file and ppos to NULL.
1069 *
1070 */
1071
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001072static inline int handle_lcd_special_code(void)
1073{
1074 /* LCD special codes */
1075
1076 int processed = 0;
1077
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001078 char *esc = lcd.esc_seq.buf + 2;
1079 int oldflags = lcd.flags;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001080
1081 /* check for display mode flags */
1082 switch (*esc) {
1083 case 'D': /* Display ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001084 lcd.flags |= LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001085 processed = 1;
1086 break;
1087 case 'd': /* Display OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001088 lcd.flags &= ~LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001089 processed = 1;
1090 break;
1091 case 'C': /* Cursor ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001092 lcd.flags |= LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001093 processed = 1;
1094 break;
1095 case 'c': /* Cursor OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001096 lcd.flags &= ~LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001097 processed = 1;
1098 break;
1099 case 'B': /* Blink ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001100 lcd.flags |= LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001101 processed = 1;
1102 break;
1103 case 'b': /* Blink OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001104 lcd.flags &= ~LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001105 processed = 1;
1106 break;
1107 case '+': /* Back light ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001108 lcd.flags |= LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001109 processed = 1;
1110 break;
1111 case '-': /* Back light OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001112 lcd.flags &= ~LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001113 processed = 1;
1114 break;
1115 case '*':
1116 /* flash back light using the keypad timer */
1117 if (scan_timer.function != NULL) {
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001118 if (lcd.light_tempo == 0
1119 && ((lcd.flags & LCD_FLAG_L) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001120 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001121 lcd.light_tempo = FLASH_LIGHT_TEMPO;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001122 }
1123 processed = 1;
1124 break;
1125 case 'f': /* Small Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001126 lcd.flags &= ~LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001127 processed = 1;
1128 break;
1129 case 'F': /* Large Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001130 lcd.flags |= LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001131 processed = 1;
1132 break;
1133 case 'n': /* One Line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001134 lcd.flags &= ~LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001135 processed = 1;
1136 break;
1137 case 'N': /* Two Lines */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001138 lcd.flags |= LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001139 break;
1140 case 'l': /* Shift Cursor Left */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001141 if (lcd.addr.x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001142 /* back one char if not at end of line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001143 if (lcd.addr.x < lcd.bwidth)
Mariusz Gorski21149242014-12-05 22:28:19 +01001144 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001145 lcd.addr.x--;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001146 }
1147 processed = 1;
1148 break;
1149 case 'r': /* shift cursor right */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001150 if (lcd.addr.x < lcd.width) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001151 /* allow the cursor to pass the end of the line */
Mariusz Gorski21149242014-12-05 22:28:19 +01001152 if (lcd.addr.x < (lcd.bwidth - 1))
1153 lcd_write_cmd(LCD_CMD_SHIFT |
1154 LCD_CMD_SHIFT_RIGHT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001155 lcd.addr.x++;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001156 }
1157 processed = 1;
1158 break;
1159 case 'L': /* shift display left */
Mariusz Gorski21149242014-12-05 22:28:19 +01001160 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001161 processed = 1;
1162 break;
1163 case 'R': /* shift display right */
Mariusz Gorski21149242014-12-05 22:28:19 +01001164 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
1165 LCD_CMD_SHIFT_RIGHT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001166 processed = 1;
1167 break;
1168 case 'k': { /* kill end of line */
1169 int x;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001170
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001171 for (x = lcd.addr.x; x < lcd.bwidth; x++)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001172 lcd_write_data(' ');
1173
1174 /* restore cursor position */
1175 lcd_gotoxy();
1176 processed = 1;
1177 break;
1178 }
1179 case 'I': /* reinitialize display */
1180 lcd_init_display();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001181 processed = 1;
1182 break;
1183 case 'G': {
1184 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1185 * and '7', representing the numerical ASCII code of the
1186 * redefined character, and <xx...xx> a sequence of 16
1187 * hex digits representing 8 bytes for each character.
1188 * Most LCDs will only use 5 lower bits of the 7 first
1189 * bytes.
1190 */
1191
1192 unsigned char cgbytes[8];
1193 unsigned char cgaddr;
1194 int cgoffset;
1195 int shift;
1196 char value;
1197 int addr;
1198
1199 if (strchr(esc, ';') == NULL)
1200 break;
1201
1202 esc++;
1203
1204 cgaddr = *(esc++) - '0';
1205 if (cgaddr > 7) {
1206 processed = 1;
1207 break;
1208 }
1209
1210 cgoffset = 0;
1211 shift = 0;
1212 value = 0;
1213 while (*esc && cgoffset < 8) {
1214 shift ^= 4;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001215 if (*esc >= '0' && *esc <= '9') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001216 value |= (*esc - '0') << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001217 } else if (*esc >= 'A' && *esc <= 'Z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001218 value |= (*esc - 'A' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001219 } else if (*esc >= 'a' && *esc <= 'z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001220 value |= (*esc - 'a' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001221 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001222 esc++;
1223 continue;
1224 }
1225
1226 if (shift == 0) {
1227 cgbytes[cgoffset++] = value;
1228 value = 0;
1229 }
1230
1231 esc++;
1232 }
1233
Mariusz Gorski21149242014-12-05 22:28:19 +01001234 lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001235 for (addr = 0; addr < cgoffset; addr++)
1236 lcd_write_data(cgbytes[addr]);
1237
1238 /* ensures that we stop writing to CGRAM */
1239 lcd_gotoxy();
1240 processed = 1;
1241 break;
1242 }
1243 case 'x': /* gotoxy : LxXXX[yYYY]; */
1244 case 'y': /* gotoxy : LyYYY[xXXX]; */
1245 if (strchr(esc, ';') == NULL)
1246 break;
1247
1248 while (*esc) {
1249 if (*esc == 'x') {
1250 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001251 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001252 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001253 } else if (*esc == 'y') {
1254 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001255 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001256 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001257 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001258 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001259 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001260 }
1261
1262 lcd_gotoxy();
1263 processed = 1;
1264 break;
1265 }
1266
Mariusz Gorski21149242014-12-05 22:28:19 +01001267 /* TODO: This indent party here got ugly, clean it! */
Adam Buchbinderf2635892012-09-19 21:51:07 -04001268 /* Check whether one flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001269 if (oldflags != lcd.flags) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001270 /* check whether one of B,C,D flags were changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001271 if ((oldflags ^ lcd.flags) &
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001272 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1273 /* set display mode */
Mariusz Gorski21149242014-12-05 22:28:19 +01001274 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
1275 | ((lcd.flags & LCD_FLAG_D)
1276 ? LCD_CMD_DISPLAY_ON : 0)
1277 | ((lcd.flags & LCD_FLAG_C)
1278 ? LCD_CMD_CURSOR_ON : 0)
1279 | ((lcd.flags & LCD_FLAG_B)
1280 ? LCD_CMD_BLINK_ON : 0));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001281 /* check whether one of F,N flags was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001282 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
Mariusz Gorski21149242014-12-05 22:28:19 +01001283 lcd_write_cmd(LCD_CMD_FUNCTION_SET
1284 | LCD_CMD_DATA_LEN_8BITS
1285 | ((lcd.flags & LCD_FLAG_F)
1286 ? LCD_CMD_TWO_LINES : 0)
1287 | ((lcd.flags & LCD_FLAG_N)
1288 ? LCD_CMD_FONT_5X10_DOTS
1289 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001290 /* check whether L flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001291 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1292 if (lcd.flags & (LCD_FLAG_L))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001293 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001294 else if (lcd.light_tempo == 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001295 /* switch off the light only when the tempo
1296 lighting is gone */
1297 lcd_backlight(0);
1298 }
1299 }
1300
1301 return processed;
1302}
1303
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001304static void lcd_write_char(char c)
Willy Tarreau698b1512008-11-22 11:29:33 +01001305{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001306 /* first, we'll test if we're in escape mode */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001307 if ((c != '\n') && lcd.esc_seq.len >= 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001308 /* yes, let's add this char to the buffer */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001309 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1310 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001311 } else {
1312 /* aborts any previous escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001313 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001314
1315 switch (c) {
1316 case LCD_ESCAPE_CHAR:
1317 /* start of an escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001318 lcd.esc_seq.len = 0;
1319 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001320 break;
1321 case '\b':
1322 /* go back one char and clear it */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001323 if (lcd.addr.x > 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001324 /* check if we're not at the
1325 end of the line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001326 if (lcd.addr.x < lcd.bwidth)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001327 /* back one char */
Mariusz Gorski21149242014-12-05 22:28:19 +01001328 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001329 lcd.addr.x--;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001330 }
1331 /* replace with a space */
1332 lcd_write_data(' ');
1333 /* back one char again */
Mariusz Gorski21149242014-12-05 22:28:19 +01001334 lcd_write_cmd(LCD_CMD_SHIFT);
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001335 break;
1336 case '\014':
1337 /* quickly clear the display */
1338 lcd_clear_fast();
1339 break;
1340 case '\n':
1341 /* flush the remainder of the current line and
1342 go to the beginning of the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001343 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001344 lcd_write_data(' ');
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001345 lcd.addr.x = 0;
1346 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001347 lcd_gotoxy();
1348 break;
1349 case '\r':
1350 /* go to the beginning of the same line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001351 lcd.addr.x = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001352 lcd_gotoxy();
1353 break;
1354 case '\t':
1355 /* print a space instead of the tab */
1356 lcd_print(' ');
1357 break;
1358 default:
1359 /* simply print this char */
1360 lcd_print(c);
1361 break;
1362 }
1363 }
1364
1365 /* now we'll see if we're in an escape mode and if the current
1366 escape sequence can be understood. */
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 */
1389 /* flush the escape sequence if it's been processed
1390 or if it is getting too long. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001391 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1392 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001393 } /* escape codes */
1394}
1395
1396static ssize_t lcd_write(struct file *file,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001397 const char __user *buf, size_t count, loff_t *ppos)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001398{
1399 const char __user *tmp = buf;
Willy Tarreau698b1512008-11-22 11:29:33 +01001400 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001401
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001402 for (; count-- > 0; (*ppos)++, tmp++) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001403 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001404 /* let's be a little nice with other processes
1405 that need some CPU */
1406 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001407
Bastien Armand6a4193a2014-04-23 19:42:11 +02001408 if (get_user(c, tmp))
Willy Tarreau698b1512008-11-22 11:29:33 +01001409 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001410
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001411 lcd_write_char(c);
Willy Tarreau7005b582008-11-13 17:18:59 -08001412 }
1413
Willy Tarreau698b1512008-11-22 11:29:33 +01001414 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001415}
1416
Willy Tarreau698b1512008-11-22 11:29:33 +01001417static int lcd_open(struct inode *inode, struct file *file)
1418{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001419 if (!atomic_dec_and_test(&lcd_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001420 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001421
Willy Tarreau698b1512008-11-22 11:29:33 +01001422 if (file->f_mode & FMODE_READ) /* device is write-only */
1423 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001424
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001425 if (lcd.must_clear) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001426 lcd_clear_display();
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001427 lcd.must_clear = false;
Willy Tarreau698b1512008-11-22 11:29:33 +01001428 }
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001429 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001430}
1431
Willy Tarreau698b1512008-11-22 11:29:33 +01001432static int lcd_release(struct inode *inode, struct file *file)
1433{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001434 atomic_inc(&lcd_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001435 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001436}
1437
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001438static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001439 .write = lcd_write,
1440 .open = lcd_open,
1441 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001442 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001443};
1444
1445static struct miscdevice lcd_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001446 .minor = LCD_MINOR,
1447 .name = "lcd",
1448 .fops = &lcd_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001449};
1450
Willy Tarreau7005b582008-11-13 17:18:59 -08001451/* public function usable from the kernel for any purpose */
Peter Huewe36d20412013-02-15 13:47:05 +01001452static void panel_lcd_print(const char *s)
Willy Tarreau698b1512008-11-22 11:29:33 +01001453{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001454 const char *tmp = s;
1455 int count = strlen(s);
1456
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001457 if (lcd.enabled && lcd.initialized) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001458 for (; count-- > 0; tmp++) {
1459 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
1460 /* let's be a little nice with other processes
1461 that need some CPU */
1462 schedule();
1463
1464 lcd_write_char(*tmp);
1465 }
1466 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001467}
1468
Willy Tarreau7005b582008-11-13 17:18:59 -08001469/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +01001470static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001471{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001472 switch (selected_lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001473 case LCD_TYPE_OLD:
1474 /* parallel mode, 8 bits */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001475 lcd.proto = LCD_PROTO_PARALLEL;
1476 lcd.charset = LCD_CHARSET_NORMAL;
1477 lcd.pins.e = PIN_STROBE;
1478 lcd.pins.rs = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001479
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001480 lcd.width = 40;
1481 lcd.bwidth = 40;
1482 lcd.hwidth = 64;
1483 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001484 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001485 case LCD_TYPE_KS0074:
1486 /* serial mode, ks0074 */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001487 lcd.proto = LCD_PROTO_SERIAL;
1488 lcd.charset = LCD_CHARSET_KS0074;
1489 lcd.pins.bl = PIN_AUTOLF;
1490 lcd.pins.cl = PIN_STROBE;
1491 lcd.pins.da = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001492
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001493 lcd.width = 16;
1494 lcd.bwidth = 40;
1495 lcd.hwidth = 16;
1496 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001497 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001498 case LCD_TYPE_NEXCOM:
1499 /* parallel mode, 8 bits, generic */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001500 lcd.proto = LCD_PROTO_PARALLEL;
1501 lcd.charset = LCD_CHARSET_NORMAL;
1502 lcd.pins.e = PIN_AUTOLF;
1503 lcd.pins.rs = PIN_SELECP;
1504 lcd.pins.rw = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001505
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001506 lcd.width = 16;
1507 lcd.bwidth = 40;
1508 lcd.hwidth = 64;
1509 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001510 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001511 case LCD_TYPE_CUSTOM:
1512 /* customer-defined */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001513 lcd.proto = DEFAULT_LCD_PROTO;
1514 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001515 /* default geometry will be set later */
1516 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001517 case LCD_TYPE_HANTRONIX:
1518 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001519 default:
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001520 lcd.proto = LCD_PROTO_PARALLEL;
1521 lcd.charset = LCD_CHARSET_NORMAL;
1522 lcd.pins.e = PIN_STROBE;
1523 lcd.pins.rs = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001524
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001525 lcd.width = 16;
1526 lcd.bwidth = 40;
1527 lcd.hwidth = 64;
1528 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001529 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001530 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001531
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001532 /* Overwrite with module params set on loading */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001533 if (lcd_height != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001534 lcd.height = lcd_height;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001535 if (lcd_width != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001536 lcd.width = lcd_width;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001537 if (lcd_bwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001538 lcd.bwidth = lcd_bwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001539 if (lcd_hwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001540 lcd.hwidth = lcd_hwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001541 if (lcd_charset != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001542 lcd.charset = lcd_charset;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001543 if (lcd_proto != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001544 lcd.proto = lcd_proto;
1545 if (lcd_e_pin != PIN_NOT_SET)
1546 lcd.pins.e = lcd_e_pin;
1547 if (lcd_rs_pin != PIN_NOT_SET)
1548 lcd.pins.rs = lcd_rs_pin;
1549 if (lcd_rw_pin != PIN_NOT_SET)
1550 lcd.pins.rw = lcd_rw_pin;
1551 if (lcd_cl_pin != PIN_NOT_SET)
1552 lcd.pins.cl = lcd_cl_pin;
1553 if (lcd_da_pin != PIN_NOT_SET)
1554 lcd.pins.da = lcd_da_pin;
1555 if (lcd_bl_pin != PIN_NOT_SET)
1556 lcd.pins.bl = lcd_bl_pin;
Willy Tarreau7005b582008-11-13 17:18:59 -08001557
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001558 /* this is used to catch wrong and default values */
1559 if (lcd.width <= 0)
1560 lcd.width = DEFAULT_LCD_WIDTH;
1561 if (lcd.bwidth <= 0)
1562 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1563 if (lcd.hwidth <= 0)
1564 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1565 if (lcd.height <= 0)
1566 lcd.height = DEFAULT_LCD_HEIGHT;
1567
1568 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001569 lcd_write_cmd = lcd_write_cmd_s;
1570 lcd_write_data = lcd_write_data_s;
1571 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001572
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001573 if (lcd.pins.cl == PIN_NOT_SET)
1574 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1575 if (lcd.pins.da == PIN_NOT_SET)
1576 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001577
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001578 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001579 lcd_write_cmd = lcd_write_cmd_p8;
1580 lcd_write_data = lcd_write_data_p8;
1581 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001582
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001583 if (lcd.pins.e == PIN_NOT_SET)
1584 lcd.pins.e = DEFAULT_LCD_PIN_E;
1585 if (lcd.pins.rs == PIN_NOT_SET)
1586 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1587 if (lcd.pins.rw == PIN_NOT_SET)
1588 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001589 } else {
1590 lcd_write_cmd = lcd_write_cmd_tilcd;
1591 lcd_write_data = lcd_write_data_tilcd;
1592 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001593 }
1594
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001595 if (lcd.pins.bl == PIN_NOT_SET)
1596 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
Willy Tarreau698b1512008-11-22 11:29:33 +01001597
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001598 if (lcd.pins.e == PIN_NOT_SET)
1599 lcd.pins.e = PIN_NONE;
1600 if (lcd.pins.rs == PIN_NOT_SET)
1601 lcd.pins.rs = PIN_NONE;
1602 if (lcd.pins.rw == PIN_NOT_SET)
1603 lcd.pins.rw = PIN_NONE;
1604 if (lcd.pins.bl == PIN_NOT_SET)
1605 lcd.pins.bl = PIN_NONE;
1606 if (lcd.pins.cl == PIN_NOT_SET)
1607 lcd.pins.cl = PIN_NONE;
1608 if (lcd.pins.da == PIN_NOT_SET)
1609 lcd.pins.da = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001610
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001611 if (lcd.charset == NOT_SET)
1612 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001613
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001614 if (lcd.charset == LCD_CHARSET_KS0074)
Willy Tarreau698b1512008-11-22 11:29:33 +01001615 lcd_char_conv = lcd_char_conv_ks0074;
1616 else
1617 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001618
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001619 if (lcd.pins.bl != PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +01001620 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001621
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001622 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
Willy Tarreau698b1512008-11-22 11:29:33 +01001623 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001624 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
Willy Tarreau698b1512008-11-22 11:29:33 +01001625 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001626 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
Willy Tarreau698b1512008-11-22 11:29:33 +01001627 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001628 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001629 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001630 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001631 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001632 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
Willy Tarreau698b1512008-11-22 11:29:33 +01001633 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001634
Willy Tarreau698b1512008-11-22 11:29:33 +01001635 /* before this line, we must NOT send anything to the display.
1636 * Since lcd_init_display() needs to write data, we have to
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001637 * enable mark the LCD initialized just before. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001638 lcd.initialized = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001639 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001640
Willy Tarreau698b1512008-11-22 11:29:33 +01001641 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001642#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1643#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001644 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001645#endif
1646#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001647 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1648 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001649#endif
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001650 lcd.addr.x = 0;
1651 lcd.addr.y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001652 /* clear the display on the next device opening */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001653 lcd.must_clear = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001654 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001655}
1656
Willy Tarreau7005b582008-11-13 17:18:59 -08001657/*
1658 * These are the file operation function for user access to /dev/keypad
1659 */
1660
Willy Tarreau698b1512008-11-22 11:29:33 +01001661static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001662 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001663{
Willy Tarreau698b1512008-11-22 11:29:33 +01001664 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001665 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001666
Willy Tarreau698b1512008-11-22 11:29:33 +01001667 if (keypad_buflen == 0) {
1668 if (file->f_flags & O_NONBLOCK)
1669 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001670
Arnd Bergmann310df692014-01-02 13:07:35 +01001671 if (wait_event_interruptible(keypad_read_wait,
1672 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001673 return -EINTR;
1674 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001675
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001676 for (; count-- > 0 && (keypad_buflen > 0);
1677 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001678 put_user(keypad_buffer[keypad_start], tmp);
1679 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1680 }
1681 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001682
Willy Tarreau698b1512008-11-22 11:29:33 +01001683 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001684}
1685
Willy Tarreau698b1512008-11-22 11:29:33 +01001686static int keypad_open(struct inode *inode, struct file *file)
1687{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001688 if (!atomic_dec_and_test(&keypad_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001689 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001690
Willy Tarreau698b1512008-11-22 11:29:33 +01001691 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1692 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001693
Willy Tarreau698b1512008-11-22 11:29:33 +01001694 keypad_buflen = 0; /* flush the buffer on opening */
Willy Tarreau698b1512008-11-22 11:29:33 +01001695 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001696}
1697
Willy Tarreau698b1512008-11-22 11:29:33 +01001698static int keypad_release(struct inode *inode, struct file *file)
1699{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001700 atomic_inc(&keypad_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001701 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001702}
1703
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001704static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001705 .read = keypad_read, /* read */
1706 .open = keypad_open, /* open */
1707 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001708 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001709};
1710
1711static struct miscdevice keypad_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001712 .minor = KEYPAD_MINOR,
1713 .name = "keypad",
1714 .fops = &keypad_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001715};
1716
Peter Huewe36d20412013-02-15 13:47:05 +01001717static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001718{
Willy Tarreau698b1512008-11-22 11:29:33 +01001719 /* send the key to the device only if a process is attached to it. */
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001720 if (!atomic_read(&keypad_available)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001721 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1722 keypad_buffer[(keypad_start + keypad_buflen++) %
1723 KEYPAD_BUFFER] = *string++;
1724 }
1725 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001726 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001727}
1728
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001729/* this function scans all the bits involving at least one logical signal,
1730 * and puts the results in the bitfield "phys_read" (one bit per established
1731 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001732 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001733 * Note: to debounce input signals, we will only consider as switched a signal
1734 * which is stable across 2 measures. Signals which are different between two
1735 * reads will be kept as they previously were in their logical form (phys_prev).
1736 * A signal which has just switched will have a 1 in
1737 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001738 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001739static void phys_scan_contacts(void)
1740{
1741 int bit, bitval;
1742 char oldval;
1743 char bitmask;
1744 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001745
Willy Tarreau698b1512008-11-22 11:29:33 +01001746 phys_prev = phys_curr;
1747 phys_read_prev = phys_read;
1748 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001749
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001750 /* keep track of old value, with all outputs disabled */
1751 oldval = r_dtr(pprt) | scan_mask_o;
1752 /* activate all keyboard outputs (active low) */
1753 w_dtr(pprt, oldval & ~scan_mask_o);
1754
1755 /* will have a 1 for each bit set to gnd */
1756 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1757 /* disable all matrix signals */
1758 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001759
Willy Tarreau698b1512008-11-22 11:29:33 +01001760 /* now that all outputs are cleared, the only active input bits are
1761 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001762 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001763
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001764 /* 1 for each grounded input */
1765 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1766
1767 /* grounded inputs are signals 40-44 */
1768 phys_read |= (pmask_t) gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001769
Willy Tarreau698b1512008-11-22 11:29:33 +01001770 if (bitmask != gndmask) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001771 /* since clearing the outputs changed some inputs, we know
1772 * that some input signals are currently tied to some outputs.
1773 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001774 */
1775 for (bit = 0; bit < 8; bit++) {
1776 bitval = 1 << bit;
1777
1778 if (!(scan_mask_o & bitval)) /* skip unused bits */
1779 continue;
1780
1781 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1782 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1783 phys_read |= (pmask_t) bitmask << (5 * bit);
1784 }
1785 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001786 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001787 /* this is easy: use old bits when they are flapping,
1788 * use new ones when stable */
1789 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1790 (phys_read & ~(phys_read ^ phys_read_prev));
1791}
1792
1793static inline int input_state_high(struct logical_input *input)
1794{
1795#if 0
1796 /* FIXME:
1797 * this is an invalid test. It tries to catch
1798 * transitions from single-key to multiple-key, but
1799 * doesn't take into account the contacts polarity.
1800 * The only solution to the problem is to parse keys
1801 * from the most complex to the simplest combinations,
1802 * and mark them as 'caught' once a combination
1803 * matches, then unmatch it for all other ones.
1804 */
1805
1806 /* try to catch dangerous transitions cases :
1807 * someone adds a bit, so this signal was a false
1808 * positive resulting from a transition. We should
1809 * invalidate the signal immediately and not call the
1810 * release function.
1811 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1812 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001813 if (((phys_prev & input->mask) == input->value) &&
1814 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001815 input->state = INPUT_ST_LOW; /* invalidate */
1816 return 1;
1817 }
1818#endif
1819
1820 if ((phys_curr & input->mask) == input->value) {
1821 if ((input->type == INPUT_TYPE_STD) &&
1822 (input->high_timer == 0)) {
1823 input->high_timer++;
1824 if (input->u.std.press_fct != NULL)
1825 input->u.std.press_fct(input->u.std.press_data);
1826 } else if (input->type == INPUT_TYPE_KBD) {
1827 /* will turn on the light */
1828 keypressed = 1;
1829
1830 if (input->high_timer == 0) {
1831 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001832
Jake Champline6626de2013-05-04 11:21:17 -04001833 if (press_str[0]) {
1834 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001835
Jake Champline6626de2013-05-04 11:21:17 -04001836 keypad_send_key(press_str, s);
1837 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001838 }
1839
1840 if (input->u.kbd.repeat_str[0]) {
1841 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001842
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001843 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001844 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001845
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001846 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001847 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001848 }
1849 /* we will need to come back here soon */
1850 inputs_stable = 0;
1851 }
1852
1853 if (input->high_timer < 255)
1854 input->high_timer++;
1855 }
1856 return 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001857 }
Vincent Heuken083b3632014-07-10 03:34:28 -07001858
1859 /* else signal falling down. Let's fall through. */
1860 input->state = INPUT_ST_FALLING;
1861 input->fall_timer = 0;
1862
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001863 return 0;
1864}
1865
1866static inline void input_state_falling(struct logical_input *input)
1867{
1868#if 0
1869 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001870 if (((phys_prev & input->mask) == input->value) &&
1871 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001872 input->state = INPUT_ST_LOW; /* invalidate */
1873 return;
1874 }
1875#endif
1876
1877 if ((phys_curr & input->mask) == input->value) {
1878 if (input->type == INPUT_TYPE_KBD) {
1879 /* will turn on the light */
1880 keypressed = 1;
1881
1882 if (input->u.kbd.repeat_str[0]) {
1883 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001884
Jake Champline6626de2013-05-04 11:21:17 -04001885 if (input->high_timer >= KEYPAD_REP_START) {
1886 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001887
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001888 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001889 keypad_send_key(repeat_str, s);
1890 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001891 /* we will need to come back here soon */
1892 inputs_stable = 0;
1893 }
1894
1895 if (input->high_timer < 255)
1896 input->high_timer++;
1897 }
1898 input->state = INPUT_ST_HIGH;
1899 } else if (input->fall_timer >= input->fall_time) {
1900 /* call release event */
1901 if (input->type == INPUT_TYPE_STD) {
1902 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001903
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001904 if (release_fct != NULL)
1905 release_fct(input->u.std.release_data);
1906 } else if (input->type == INPUT_TYPE_KBD) {
1907 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001908
Jake Champline6626de2013-05-04 11:21:17 -04001909 if (release_str[0]) {
1910 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001911
Jake Champline6626de2013-05-04 11:21:17 -04001912 keypad_send_key(release_str, s);
1913 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001914 }
1915
1916 input->state = INPUT_ST_LOW;
1917 } else {
1918 input->fall_timer++;
1919 inputs_stable = 0;
1920 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001921}
1922
Willy Tarreau698b1512008-11-22 11:29:33 +01001923static void panel_process_inputs(void)
1924{
1925 struct list_head *item;
1926 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001927
Willy Tarreau698b1512008-11-22 11:29:33 +01001928 keypressed = 0;
1929 inputs_stable = 1;
1930 list_for_each(item, &logical_inputs) {
1931 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001932
Willy Tarreau698b1512008-11-22 11:29:33 +01001933 switch (input->state) {
1934 case INPUT_ST_LOW:
1935 if ((phys_curr & input->mask) != input->value)
1936 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001937 /* if all needed ones were already set previously,
1938 * this means that this logical signal has been
1939 * activated by the releasing of another combined
1940 * signal, so we don't want to match.
1941 * eg: AB -(release B)-> A -(release A)-> 0 :
1942 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001943 */
1944 if ((phys_prev & input->mask) == input->value)
1945 break;
1946 input->rise_timer = 0;
1947 input->state = INPUT_ST_RISING;
1948 /* no break here, fall through */
1949 case INPUT_ST_RISING:
1950 if ((phys_curr & input->mask) != input->value) {
1951 input->state = INPUT_ST_LOW;
1952 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001953 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001954 if (input->rise_timer < input->rise_time) {
1955 inputs_stable = 0;
1956 input->rise_timer++;
1957 break;
1958 }
1959 input->high_timer = 0;
1960 input->state = INPUT_ST_HIGH;
1961 /* no break here, fall through */
1962 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001963 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001964 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001965 /* no break here, fall through */
1966 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001967 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001968 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001969 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001970}
1971
Willy Tarreau698b1512008-11-22 11:29:33 +01001972static void panel_scan_timer(void)
1973{
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001974 if (keypad.enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001975 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001976 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001977
1978 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001979 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001980 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001981
Willy Tarreau698b1512008-11-22 11:29:33 +01001982 if (!inputs_stable || phys_curr != phys_prev)
1983 panel_process_inputs();
1984 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001985
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001986 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001987 if (keypressed) {
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001988 if (lcd.light_tempo == 0
1989 && ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001990 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001991 lcd.light_tempo = FLASH_LIGHT_TEMPO;
1992 } else if (lcd.light_tempo > 0) {
1993 lcd.light_tempo--;
1994 if (lcd.light_tempo == 0
1995 && ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001996 lcd_backlight(0);
1997 }
1998 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001999
Willy Tarreau698b1512008-11-22 11:29:33 +01002000 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08002001}
2002
Willy Tarreau698b1512008-11-22 11:29:33 +01002003static void init_scan_timer(void)
2004{
2005 if (scan_timer.function != NULL)
2006 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08002007
Aya Mahfouz8f6e36c2015-02-19 07:58:29 +02002008 setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
Willy Tarreau698b1512008-11-22 11:29:33 +01002009 scan_timer.expires = jiffies + INPUT_POLL_TIME;
Willy Tarreau698b1512008-11-22 11:29:33 +01002010 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002011}
2012
2013/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002014 * if <omask> or <imask> are non-null, they will be or'ed with the bits
2015 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08002016 * returns 1 if ok, 0 if error (in which case, nothing is written).
2017 */
Peter Huewe36d20412013-02-15 13:47:05 +01002018static int input_name2mask(const char *name, pmask_t *mask, pmask_t *value,
Willy Tarreau698b1512008-11-22 11:29:33 +01002019 char *imask, char *omask)
2020{
2021 static char sigtab[10] = "EeSsPpAaBb";
2022 char im, om;
2023 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08002024
Dominique van den Broeck2d534262014-05-24 01:35:24 +02002025 om = 0ULL;
2026 im = 0ULL;
2027 m = 0ULL;
2028 v = 0ULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002029 while (*name) {
2030 int in, out, bit, neg;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002031
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002032 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name);
2033 in++)
Willy Tarreau698b1512008-11-22 11:29:33 +01002034 ;
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002035
Willy Tarreau698b1512008-11-22 11:29:33 +01002036 if (in >= sizeof(sigtab))
2037 return 0; /* input name not found */
2038 neg = (in & 1); /* odd (lower) names are negated */
2039 in >>= 1;
2040 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08002041
Willy Tarreau698b1512008-11-22 11:29:33 +01002042 name++;
2043 if (isdigit(*name)) {
2044 out = *name - '0';
2045 om |= (1 << out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002046 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002047 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002048 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01002049 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002050 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002051
2052 bit = (out * 5) + in;
2053
2054 m |= 1ULL << bit;
2055 if (!neg)
2056 v |= 1ULL << bit;
2057 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08002058 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002059 *mask = m;
2060 *value = v;
2061 if (imask)
2062 *imask |= im;
2063 if (omask)
2064 *omask |= om;
2065 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002066}
2067
2068/* tries to bind a key to the signal name <name>. The key will send the
2069 * strings <press>, <repeat>, <release> for these respective events.
2070 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2071 */
Peter Huewe36d20412013-02-15 13:47:05 +01002072static struct logical_input *panel_bind_key(const char *name, const char *press,
2073 const char *repeat,
2074 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01002075{
2076 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002077
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002078 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002079 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01002080 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002081
Willy Tarreau698b1512008-11-22 11:29:33 +01002082 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002083 &scan_mask_o)) {
2084 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01002085 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002086 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002087
2088 key->type = INPUT_TYPE_KBD;
2089 key->state = INPUT_ST_LOW;
2090 key->rise_time = 1;
2091 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002092
Willy Tarreau698b1512008-11-22 11:29:33 +01002093 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2094 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2095 strncpy(key->u.kbd.release_str, release,
2096 sizeof(key->u.kbd.release_str));
2097 list_add(&key->list, &logical_inputs);
2098 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002099}
2100
Willy Tarreau630231772008-11-22 12:52:18 +01002101#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002102/* tries to bind a callback function to the signal name <name>. The function
2103 * <press_fct> will be called with the <press_data> arg when the signal is
2104 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002105 * Returns the pointer to the new signal if ok, NULL if the signal could not
2106 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002107 */
2108static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302109 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002110 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302111 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002112 int release_data)
2113{
2114 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002115
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002116 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002117 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002118 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002119
Willy Tarreau698b1512008-11-22 11:29:33 +01002120 memset(callback, 0, sizeof(struct logical_input));
2121 if (!input_name2mask(name, &callback->mask, &callback->value,
2122 &scan_mask_i, &scan_mask_o))
2123 return NULL;
2124
2125 callback->type = INPUT_TYPE_STD;
2126 callback->state = INPUT_ST_LOW;
2127 callback->rise_time = 1;
2128 callback->fall_time = 1;
2129 callback->u.std.press_fct = press_fct;
2130 callback->u.std.press_data = press_data;
2131 callback->u.std.release_fct = release_fct;
2132 callback->u.std.release_data = release_data;
2133 list_add(&callback->list, &logical_inputs);
2134 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002135}
Willy Tarreau630231772008-11-22 12:52:18 +01002136#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002137
Willy Tarreau698b1512008-11-22 11:29:33 +01002138static void keypad_init(void)
2139{
2140 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002141
Willy Tarreau698b1512008-11-22 11:29:33 +01002142 init_waitqueue_head(&keypad_read_wait);
2143 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002144
Willy Tarreau698b1512008-11-22 11:29:33 +01002145 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002146
Willy Tarreau698b1512008-11-22 11:29:33 +01002147 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2148 panel_bind_key(keypad_profile[keynum][0],
2149 keypad_profile[keynum][1],
2150 keypad_profile[keynum][2],
2151 keypad_profile[keynum][3]);
2152 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002153
Willy Tarreau698b1512008-11-22 11:29:33 +01002154 init_scan_timer();
2155 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002156}
2157
Willy Tarreau7005b582008-11-13 17:18:59 -08002158/**************************************************/
2159/* device initialization */
2160/**************************************************/
2161
Willy Tarreau698b1512008-11-22 11:29:33 +01002162static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2163 void *unused)
2164{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002165 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002166 switch (code) {
2167 case SYS_DOWN:
2168 panel_lcd_print
2169 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2170 break;
2171 case SYS_HALT:
2172 panel_lcd_print
2173 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2174 break;
2175 case SYS_POWER_OFF:
2176 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2177 break;
2178 default:
2179 break;
2180 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002181 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002182 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002183}
2184
2185static struct notifier_block panel_notifier = {
2186 panel_notify_sys,
2187 NULL,
2188 0
2189};
2190
Willy Tarreau698b1512008-11-22 11:29:33 +01002191static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002192{
Willy Tarreau698b1512008-11-22 11:29:33 +01002193 if (port->number != parport)
2194 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002195
Willy Tarreau698b1512008-11-22 11:29:33 +01002196 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002197 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2198 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002199 return;
2200 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002201
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002202 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
Willy Tarreau698b1512008-11-22 11:29:33 +01002203 NULL,
2204 /*PARPORT_DEV_EXCL */
2205 0, (void *)&pprt);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002206 if (pprt == NULL) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002207 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2208 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002209 return;
2210 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002211
Willy Tarreau698b1512008-11-22 11:29:33 +01002212 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002213 pr_err("could not claim access to parport%d. Aborting.\n",
2214 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002215 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002216 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002217
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002218 /* must init LCD first, just in case an IRQ from the keypad is
2219 * generated at keypad init
2220 */
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002221 if (lcd.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002222 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002223 if (misc_register(&lcd_dev))
2224 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002225 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002226
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002227 if (keypad.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002228 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002229 if (misc_register(&keypad_dev))
2230 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002231 }
Sudip Mukherjeebb046fe2015-03-09 20:08:23 +05302232 register_reboot_notifier(&panel_notifier);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002233 return;
2234
2235err_lcd_unreg:
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002236 if (lcd.enabled)
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002237 misc_deregister(&lcd_dev);
2238err_unreg_device:
2239 parport_unregister_device(pprt);
2240 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002241}
2242
Willy Tarreau698b1512008-11-22 11:29:33 +01002243static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002244{
Willy Tarreau698b1512008-11-22 11:29:33 +01002245 if (port->number != parport)
2246 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002247
Willy Tarreau698b1512008-11-22 11:29:33 +01002248 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002249 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2250 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002251 return;
2252 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002253
Sudip Mukherjeebb046fe2015-03-09 20:08:23 +05302254 unregister_reboot_notifier(&panel_notifier);
2255
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002256 if (keypad.enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002257 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002258 keypad_initialized = 0;
2259 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002260
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002261 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002262 misc_deregister(&lcd_dev);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002263 lcd.initialized = false;
Peter Huewe0b0595b2009-09-29 01:22:40 +02002264 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002265
Willy Tarreau698b1512008-11-22 11:29:33 +01002266 parport_release(pprt);
2267 parport_unregister_device(pprt);
2268 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002269}
2270
2271static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002272 .name = "panel",
2273 .attach = panel_attach,
2274 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002275};
2276
2277/* init function */
Mariusz Gorskid9114762014-11-27 22:36:46 +01002278static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002279{
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302280 int selected_keypad_type = NOT_SET, err;
Willy Tarreau7005b582008-11-13 17:18:59 -08002281
Willy Tarreau698b1512008-11-22 11:29:33 +01002282 /* take care of an eventual profile */
2283 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002284 case PANEL_PROFILE_CUSTOM:
2285 /* custom profile */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002286 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2287 selected_lcd_type = DEFAULT_LCD_TYPE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002288 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002289 case PANEL_PROFILE_OLD:
2290 /* 8 bits, 2*16, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002291 selected_keypad_type = KEYPAD_TYPE_OLD;
2292 selected_lcd_type = LCD_TYPE_OLD;
2293
2294 /* TODO: This two are a little hacky, sort it out later */
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002295 if (lcd_width == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002296 lcd_width = 16;
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002297 if (lcd_hwidth == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002298 lcd_hwidth = 16;
2299 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002300 case PANEL_PROFILE_NEW:
2301 /* serial, 2*16, new keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002302 selected_keypad_type = KEYPAD_TYPE_NEW;
2303 selected_lcd_type = LCD_TYPE_KS0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01002304 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002305 case PANEL_PROFILE_HANTRONIX:
2306 /* 8 bits, 2*16 hantronix-like, no keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002307 selected_keypad_type = KEYPAD_TYPE_NONE;
2308 selected_lcd_type = LCD_TYPE_HANTRONIX;
Willy Tarreau698b1512008-11-22 11:29:33 +01002309 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002310 case PANEL_PROFILE_NEXCOM:
2311 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002312 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2313 selected_lcd_type = LCD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002314 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002315 case PANEL_PROFILE_LARGE:
2316 /* 8 bits, 2*40, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002317 selected_keypad_type = KEYPAD_TYPE_OLD;
2318 selected_lcd_type = LCD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002319 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002320 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002321
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01002322 /*
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002323 * Overwrite selection with module param values (both keypad and lcd),
2324 * where the deprecated params have lower prio.
2325 */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002326 if (keypad_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002327 selected_keypad_type = keypad_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002328 if (keypad_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002329 selected_keypad_type = keypad_type;
Willy Tarreau7005b582008-11-13 17:18:59 -08002330
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002331 keypad.enabled = (selected_keypad_type > 0);
2332
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002333 if (lcd_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002334 selected_lcd_type = lcd_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002335 if (lcd_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002336 selected_lcd_type = lcd_type;
2337
2338 lcd.enabled = (selected_lcd_type > 0);
2339
Sudip Mukherjee733345e2015-02-11 16:57:08 +05302340 if (lcd.enabled) {
2341 /*
2342 * Init lcd struct with load-time values to preserve exact
2343 * current functionality (at least for now).
2344 */
2345 lcd.height = lcd_height;
2346 lcd.width = lcd_width;
2347 lcd.bwidth = lcd_bwidth;
2348 lcd.hwidth = lcd_hwidth;
2349 lcd.charset = lcd_charset;
2350 lcd.proto = lcd_proto;
2351 lcd.pins.e = lcd_e_pin;
2352 lcd.pins.rs = lcd_rs_pin;
2353 lcd.pins.rw = lcd_rw_pin;
2354 lcd.pins.cl = lcd_cl_pin;
2355 lcd.pins.da = lcd_da_pin;
2356 lcd.pins.bl = lcd_bl_pin;
2357
2358 /* Leave it for now, just in case */
2359 lcd.esc_seq.len = -1;
2360 }
2361
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002362 switch (selected_keypad_type) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002363 case KEYPAD_TYPE_OLD:
2364 keypad_profile = old_keypad_profile;
2365 break;
2366 case KEYPAD_TYPE_NEW:
2367 keypad_profile = new_keypad_profile;
2368 break;
2369 case KEYPAD_TYPE_NEXCOM:
2370 keypad_profile = nexcom_keypad_profile;
2371 break;
2372 default:
2373 keypad_profile = NULL;
2374 break;
2375 }
2376
Sudip Mukherjeef43de772015-02-10 17:26:02 +05302377 if (!lcd.enabled && !keypad.enabled) {
2378 /* no device enabled, let's exit */
2379 pr_err("driver version " PANEL_VERSION " disabled.\n");
2380 return -ENODEV;
2381 }
2382
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302383 err = parport_register_driver(&panel_driver);
2384 if (err) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002385 pr_err("could not register with parport. Aborting.\n");
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302386 return err;
Willy Tarreau698b1512008-11-22 11:29:33 +01002387 }
2388
Willy Tarreau698b1512008-11-22 11:29:33 +01002389 if (pprt)
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002390 pr_info("driver version " PANEL_VERSION
2391 " registered on parport%d (io=0x%lx).\n", parport,
2392 pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002393 else
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002394 pr_info("driver version " PANEL_VERSION
2395 " not yet registered\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002396 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002397}
2398
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002399static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002400{
Willy Tarreau7005b582008-11-13 17:18:59 -08002401
Willy Tarreau698b1512008-11-22 11:29:33 +01002402 if (scan_timer.function != NULL)
Julia Lawalle112f892014-03-26 22:33:41 +01002403 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002404
Costantino Leandro57898132009-02-17 11:10:48 -05002405 if (pprt != NULL) {
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002406 if (keypad.enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002407 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002408 keypad_initialized = 0;
2409 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002410
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002411 if (lcd.enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002412 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2413 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2414 misc_deregister(&lcd_dev);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002415 lcd.initialized = false;
Costantino Leandro57898132009-02-17 11:10:48 -05002416 }
2417
2418 /* TODO: free all input signals */
2419 parport_release(pprt);
2420 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002421 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002422 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002423 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002424}
Willy Tarreau7005b582008-11-13 17:18:59 -08002425
Willy Tarreau7005b582008-11-13 17:18:59 -08002426module_init(panel_init_module);
2427module_exit(panel_cleanup_module);
2428MODULE_AUTHOR("Willy Tarreau");
2429MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002430
2431/*
2432 * Local variables:
2433 * c-indent-level: 4
2434 * tab-width: 8
2435 * End:
2436 */