blob: 3339633e88d36829999bec89382ba34381046733 [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
338#define LCD_TYPE_OLD 1
339#define LCD_TYPE_KS0074 2
340#define LCD_TYPE_HANTRONIX 3
341#define LCD_TYPE_NEXCOM 4
342#define LCD_TYPE_CUSTOM 5
343
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
Willy Tarreau698b1512008-11-22 11:29:33 +0100476static char init_in_progress;
Willy Tarreau7005b582008-11-13 17:18:59 -0800477
Monam Agarwal68d386b2014-02-25 19:40:49 +0530478static void (*lcd_write_cmd)(int);
479static void (*lcd_write_data)(int);
480static void (*lcd_clear_fast)(void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800481
Willy Tarreau698b1512008-11-22 11:29:33 +0100482static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800483static struct timer_list scan_timer;
484
Willy Tarreau630231772008-11-22 12:52:18 +0100485MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100486
Mariusz Gorski59a66a22014-11-27 22:36:45 +0100487static int parport = DEFAULT_PARPORT;
Willy Tarreau698b1512008-11-22 11:29:33 +0100488module_param(parport, int, 0000);
489MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100490
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100491static int profile = DEFAULT_PROFILE;
492module_param(profile, int, 0000);
493MODULE_PARM_DESC(profile,
494 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
495 "4=16x2 nexcom; default=40x2, old kp");
496
Mariusz Gorski36277d42014-11-27 22:36:47 +0100497static int keypad_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100498module_param(keypad_type, int, 0000);
499MODULE_PARM_DESC(keypad_type,
500 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
501
Mariusz Gorski36277d42014-11-27 22:36:47 +0100502static int lcd_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100503module_param(lcd_type, int, 0000);
504MODULE_PARM_DESC(lcd_type,
505 "LCD type: 0=none, 1=old //, 2=serial ks0074, 3=hantronix //, 4=nexcom //, 5=compiled-in");
506
Mariusz Gorski36277d42014-11-27 22:36:47 +0100507static int lcd_height = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100508module_param(lcd_height, int, 0000);
509MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100510
Mariusz Gorski36277d42014-11-27 22:36:47 +0100511static int lcd_width = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100512module_param(lcd_width, int, 0000);
513MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100514
Mariusz Gorski36277d42014-11-27 22:36:47 +0100515static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100516module_param(lcd_bwidth, int, 0000);
517MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100518
Mariusz Gorski36277d42014-11-27 22:36:47 +0100519static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100520module_param(lcd_hwidth, int, 0000);
521MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100522
Mariusz Gorski36277d42014-11-27 22:36:47 +0100523static int lcd_charset = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100524module_param(lcd_charset, int, 0000);
525MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100526
Mariusz Gorski36277d42014-11-27 22:36:47 +0100527static int lcd_proto = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100528module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300529MODULE_PARM_DESC(lcd_proto,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +0200530 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100531
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100532/*
533 * These are the parallel port pins the LCD control signals are connected to.
534 * Set this to 0 if the signal is not used. Set it to its opposite value
535 * (negative) if the signal is negated. -MAXINT is used to indicate that the
536 * pin has not been explicitly specified.
537 *
Willy Tarreau630231772008-11-22 12:52:18 +0100538 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100539 */
540
541static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100542module_param(lcd_e_pin, int, 0000);
543MODULE_PARM_DESC(lcd_e_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530544 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100545
546static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100547module_param(lcd_rs_pin, int, 0000);
548MODULE_PARM_DESC(lcd_rs_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530549 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100550
551static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100552module_param(lcd_rw_pin, int, 0000);
553MODULE_PARM_DESC(lcd_rw_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530554 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100555
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100556static int lcd_cl_pin = PIN_NOT_SET;
557module_param(lcd_cl_pin, int, 0000);
558MODULE_PARM_DESC(lcd_cl_pin,
559 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100560
561static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100562module_param(lcd_da_pin, int, 0000);
563MODULE_PARM_DESC(lcd_da_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530564 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100565
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100566static int lcd_bl_pin = PIN_NOT_SET;
567module_param(lcd_bl_pin, int, 0000);
568MODULE_PARM_DESC(lcd_bl_pin,
569 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
570
571/* Deprecated module parameters - consider not using them anymore */
572
Mariusz Gorski36277d42014-11-27 22:36:47 +0100573static int lcd_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100574module_param(lcd_enabled, int, 0000);
575MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
576
Mariusz Gorski36277d42014-11-27 22:36:47 +0100577static int keypad_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100578module_param(keypad_enabled, int, 0000);
579MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
580
Willy Tarreau7005b582008-11-13 17:18:59 -0800581
Peter Huewe36d20412013-02-15 13:47:05 +0100582static const unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800583
584/* for some LCD drivers (ks0074) we need a charset conversion table. */
Peter Huewe36d20412013-02-15 13:47:05 +0100585static const unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100586 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
587 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
588 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
589 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
590 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
591 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
592 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
593 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
594 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
595 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
596 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
597 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
598 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
599 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
600 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
601 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
602 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
603 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
604 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
605 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
606 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
607 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
608 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
609 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
610 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
611 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
612 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
613 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
614 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
615 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
616 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
617 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
618 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800619};
620
Peter Huewe36d20412013-02-15 13:47:05 +0100621static const char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100622 {"S0", "Left\n", "Left\n", ""},
623 {"S1", "Down\n", "Down\n", ""},
624 {"S2", "Up\n", "Up\n", ""},
625 {"S3", "Right\n", "Right\n", ""},
626 {"S4", "Esc\n", "Esc\n", ""},
627 {"S5", "Ret\n", "Ret\n", ""},
628 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800629};
630
631/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100632static const char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100633 {"S0", "Left\n", "Left\n", ""},
634 {"S1", "Down\n", "Down\n", ""},
635 {"S2", "Up\n", "Up\n", ""},
636 {"S3", "Right\n", "Right\n", ""},
637 {"S4s5", "", "Esc\n", "Esc\n"},
638 {"s4S5", "", "Ret\n", "Ret\n"},
639 {"S4S5", "Help\n", "", ""},
640 /* add new signals above this line */
641 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800642};
643
644/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100645static const char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100646 {"a-p-e-", "Down\n", "Down\n", ""},
647 {"a-p-E-", "Ret\n", "Ret\n", ""},
648 {"a-P-E-", "Esc\n", "Esc\n", ""},
649 {"a-P-e-", "Up\n", "Up\n", ""},
650 /* add new signals above this line */
651 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800652};
653
Peter Huewe36d20412013-02-15 13:47:05 +0100654static const char (*keypad_profile)[4][9] = old_keypad_profile;
Willy Tarreau7005b582008-11-13 17:18:59 -0800655
656/* FIXME: this should be converted to a bit array containing signals states */
657static struct {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300658 unsigned char e; /* parallel LCD E (data latch on falling edge) */
659 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
660 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
661 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
662 unsigned char cl; /* serial LCD clock (latch on rising edge) */
663 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800664} bits;
665
666static void init_scan_timer(void);
667
668/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100669static int set_data_bits(void)
670{
671 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800672
Willy Tarreau698b1512008-11-22 11:29:33 +0100673 val = r_dtr(pprt);
674 for (bit = 0; bit < LCD_BITS; bit++)
675 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800676
Willy Tarreau698b1512008-11-22 11:29:33 +0100677 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
678 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
679 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
680 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
681 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
682 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800683
Willy Tarreau698b1512008-11-22 11:29:33 +0100684 w_dtr(pprt, val);
685 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800686}
687
688/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100689static int set_ctrl_bits(void)
690{
691 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800692
Willy Tarreau698b1512008-11-22 11:29:33 +0100693 val = r_ctr(pprt);
694 for (bit = 0; bit < LCD_BITS; bit++)
695 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800696
Willy Tarreau698b1512008-11-22 11:29:33 +0100697 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
698 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
699 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
700 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
701 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
702 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800703
Willy Tarreau698b1512008-11-22 11:29:33 +0100704 w_ctr(pprt, val);
705 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800706}
707
708/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530709static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100710{
711 set_data_bits();
712 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800713}
714
715/*
716 * Converts a parallel port pin (from -25 to 25) to data and control ports
717 * masks, and data and control port bits. The signal will be considered
718 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
719 *
720 * Result will be used this way :
721 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
722 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
723 */
Peter Huewe36d20412013-02-15 13:47:05 +0100724static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
Willy Tarreau698b1512008-11-22 11:29:33 +0100725{
726 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800727
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200728 d_val[0] = 0;
729 c_val[0] = 0;
730 d_val[1] = 0;
731 c_val[1] = 0;
732 d_val[2] = 0xFF;
733 c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800734
Willy Tarreau698b1512008-11-22 11:29:33 +0100735 if (pin == 0)
736 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800737
Willy Tarreau698b1512008-11-22 11:29:33 +0100738 inv = (pin < 0);
739 if (inv)
740 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800741
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200742 d_bit = 0;
743 c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800744
Willy Tarreau698b1512008-11-22 11:29:33 +0100745 switch (pin) {
746 case PIN_STROBE: /* strobe, inverted */
747 c_bit = PNL_PSTROBE;
748 inv = !inv;
749 break;
750 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
751 d_bit = 1 << (pin - 2);
752 break;
753 case PIN_AUTOLF: /* autofeed, inverted */
754 c_bit = PNL_PAUTOLF;
755 inv = !inv;
756 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300757 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100758 c_bit = PNL_PINITP;
759 break;
760 case PIN_SELECP: /* select_in, inverted */
761 c_bit = PNL_PSELECP;
762 inv = !inv;
763 break;
764 default: /* unknown pin, ignore */
765 break;
766 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800767
Willy Tarreau698b1512008-11-22 11:29:33 +0100768 if (c_bit) {
769 c_val[2] &= ~c_bit;
770 c_val[!inv] = c_bit;
771 } else if (d_bit) {
772 d_val[2] &= ~d_bit;
773 d_val[!inv] = d_bit;
774 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800775}
776
777/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100778static void long_sleep(int ms)
779{
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200780 if (in_interrupt()) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100781 mdelay(ms);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200782 } else {
Davidlohr Bueso2be90fe2015-01-26 02:15:02 -0800783 __set_current_state(TASK_INTERRUPTIBLE);
Willy Tarreau698b1512008-11-22 11:29:33 +0100784 schedule_timeout((ms * HZ + 999) / 1000);
785 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800786}
787
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300788/* send a serial byte to the LCD panel. The caller is responsible for locking
789 if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100790static void lcd_send_serial(int byte)
791{
792 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800793
Willy Tarreau698b1512008-11-22 11:29:33 +0100794 /* the data bit is set on D0, and the clock on STROBE.
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300795 * LCD reads D0 on STROBE's rising edge. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100796 for (bit = 0; bit < 8; bit++) {
797 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530798 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100799 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530800 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300801 udelay(2); /* maintain the data during 2 us before CLK up */
Willy Tarreau698b1512008-11-22 11:29:33 +0100802 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530803 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300804 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100805 byte >>= 1;
806 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800807}
808
809/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100810static void lcd_backlight(int on)
811{
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100812 if (lcd.pins.bl == PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +0100813 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800814
Justin P. Mattock6975e182012-03-19 08:17:52 -0700815 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800816 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100817 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530818 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800819 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800820}
821
822/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100823static void lcd_write_cmd_s(int cmd)
824{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800825 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100826 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
827 lcd_send_serial(cmd & 0x0F);
828 lcd_send_serial((cmd >> 4) & 0x0F);
829 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800830 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800831}
832
833/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100834static void lcd_write_data_s(int data)
835{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800836 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100837 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
838 lcd_send_serial(data & 0x0F);
839 lcd_send_serial((data >> 4) & 0x0F);
840 udelay(40); /* the shortest data takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800841 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800842}
843
844/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100845static void lcd_write_cmd_p8(int cmd)
846{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800847 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800848 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100849 w_dtr(pprt, cmd);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300850 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800851
Willy Tarreau698b1512008-11-22 11:29:33 +0100852 bits.e = BIT_SET;
853 bits.rs = BIT_CLR;
854 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800855 set_ctrl_bits();
856
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300857 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800858
859 bits.e = BIT_CLR;
860 set_ctrl_bits();
861
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300862 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800863 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100864}
Willy Tarreau7005b582008-11-13 17:18:59 -0800865
Willy Tarreau698b1512008-11-22 11:29:33 +0100866/* send data to the LCD panel in 8 bits parallel mode */
867static void lcd_write_data_p8(int data)
868{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800869 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100870 /* present the data to the data port */
871 w_dtr(pprt, data);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300872 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100873
874 bits.e = BIT_SET;
875 bits.rs = BIT_SET;
876 bits.rw = BIT_CLR;
877 set_ctrl_bits();
878
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300879 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100880
881 bits.e = BIT_CLR;
882 set_ctrl_bits();
883
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300884 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800885 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100886}
887
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400888/* send a command to the TI LCD panel */
889static void lcd_write_cmd_tilcd(int cmd)
890{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800891 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400892 /* present the data to the control port */
893 w_ctr(pprt, cmd);
894 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800895 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400896}
897
898/* send data to the TI LCD panel */
899static void lcd_write_data_tilcd(int data)
900{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800901 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400902 /* present the data to the data port */
903 w_dtr(pprt, data);
904 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800905 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400906}
907
Willy Tarreau698b1512008-11-22 11:29:33 +0100908static void lcd_gotoxy(void)
909{
Mariusz Gorski21149242014-12-05 22:28:19 +0100910 lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100911 | (lcd.addr.y ? lcd.hwidth : 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300912 /* we force the cursor to stay at the end of the
913 line if it wants to go farther */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100914 | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100915 (lcd.hwidth - 1) : lcd.bwidth - 1));
Willy Tarreau698b1512008-11-22 11:29:33 +0100916}
917
918static void lcd_print(char c)
919{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100920 if (lcd.addr.x < lcd.bwidth) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100921 if (lcd_char_conv != NULL)
922 c = lcd_char_conv[(unsigned char)c];
923 lcd_write_data(c);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100924 lcd.addr.x++;
Willy Tarreau698b1512008-11-22 11:29:33 +0100925 }
926 /* prevents the cursor from wrapping onto the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100927 if (lcd.addr.x == lcd.bwidth)
Willy Tarreau698b1512008-11-22 11:29:33 +0100928 lcd_gotoxy();
929}
930
931/* fills the display with spaces and resets X/Y */
932static void lcd_clear_fast_s(void)
933{
934 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200935
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100936 lcd.addr.x = 0;
937 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100938 lcd_gotoxy();
939
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800940 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100941 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100942 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
943 lcd_send_serial(' ' & 0x0F);
944 lcd_send_serial((' ' >> 4) & 0x0F);
945 udelay(40); /* the shortest data takes at least 40 us */
946 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800947 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100948
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100949 lcd.addr.x = 0;
950 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100951 lcd_gotoxy();
952}
953
954/* fills the display with spaces and resets X/Y */
955static void lcd_clear_fast_p8(void)
956{
957 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200958
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100959 lcd.addr.x = 0;
960 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100961 lcd_gotoxy();
962
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800963 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100964 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100965 /* present the data to the data port */
966 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300967
968 /* maintain the data during 20 us before the strobe */
969 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100970
971 bits.e = BIT_SET;
972 bits.rs = BIT_SET;
973 bits.rw = BIT_CLR;
974 set_ctrl_bits();
975
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300976 /* maintain the strobe during 40 us */
977 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100978
979 bits.e = BIT_CLR;
980 set_ctrl_bits();
981
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300982 /* the shortest data takes at least 45 us */
983 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100984 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800985 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100986
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100987 lcd.addr.x = 0;
988 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100989 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800990}
991
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400992/* fills the display with spaces and resets X/Y */
993static void lcd_clear_fast_tilcd(void)
994{
995 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200996
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100997 lcd.addr.x = 0;
998 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400999 lcd_gotoxy();
1000
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001001 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001002 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001003 /* present the data to the data port */
1004 w_dtr(pprt, ' ');
1005 udelay(60);
1006 }
1007
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001008 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001009
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001010 lcd.addr.x = 0;
1011 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001012 lcd_gotoxy();
1013}
1014
Willy Tarreau7005b582008-11-13 17:18:59 -08001015/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +01001016static void lcd_clear_display(void)
1017{
Mariusz Gorski21149242014-12-05 22:28:19 +01001018 lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001019 lcd.addr.x = 0;
1020 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001021 /* we must wait a few milliseconds (15) */
1022 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -08001023}
1024
Willy Tarreau698b1512008-11-22 11:29:33 +01001025static void lcd_init_display(void)
1026{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001027 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001028 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -08001029
Willy Tarreau698b1512008-11-22 11:29:33 +01001030 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -08001031
Mariusz Gorski21149242014-12-05 22:28:19 +01001032 /* 8bits, 1 line, small fonts; let's do it 3 times */
1033 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);
Mariusz Gorski21149242014-12-05 22:28:19 +01001037 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001038 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001039
Mariusz Gorski21149242014-12-05 22:28:19 +01001040 /* set font height and lines number */
1041 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
1042 | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
1043 | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001044 );
1045 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001046
Mariusz Gorski21149242014-12-05 22:28:19 +01001047 /* display off, cursor off, blink off */
1048 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
Willy Tarreau698b1512008-11-22 11:29:33 +01001049 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001050
Mariusz Gorski21149242014-12-05 22:28:19 +01001051 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
1052 | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
1053 | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
1054 | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001055 );
Willy Tarreau7005b582008-11-13 17:18:59 -08001056
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001057 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08001058
Willy Tarreau698b1512008-11-22 11:29:33 +01001059 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001060
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001061 /* entry mode set : increment, cursor shifting */
Mariusz Gorski21149242014-12-05 22:28:19 +01001062 lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
Willy Tarreau7005b582008-11-13 17:18:59 -08001063
Willy Tarreau698b1512008-11-22 11:29:33 +01001064 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001065}
1066
1067/*
1068 * These are the file operation function for user access to /dev/lcd
1069 * This function can also be called from inside the kernel, by
1070 * setting file and ppos to NULL.
1071 *
1072 */
1073
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001074static inline int handle_lcd_special_code(void)
1075{
1076 /* LCD special codes */
1077
1078 int processed = 0;
1079
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001080 char *esc = lcd.esc_seq.buf + 2;
1081 int oldflags = lcd.flags;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001082
1083 /* check for display mode flags */
1084 switch (*esc) {
1085 case 'D': /* Display ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001086 lcd.flags |= LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001087 processed = 1;
1088 break;
1089 case 'd': /* Display OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001090 lcd.flags &= ~LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001091 processed = 1;
1092 break;
1093 case 'C': /* Cursor ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001094 lcd.flags |= LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001095 processed = 1;
1096 break;
1097 case 'c': /* Cursor OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001098 lcd.flags &= ~LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001099 processed = 1;
1100 break;
1101 case 'B': /* Blink ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001102 lcd.flags |= LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001103 processed = 1;
1104 break;
1105 case 'b': /* Blink OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001106 lcd.flags &= ~LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001107 processed = 1;
1108 break;
1109 case '+': /* Back light ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001110 lcd.flags |= LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001111 processed = 1;
1112 break;
1113 case '-': /* Back light OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001114 lcd.flags &= ~LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001115 processed = 1;
1116 break;
1117 case '*':
1118 /* flash back light using the keypad timer */
1119 if (scan_timer.function != NULL) {
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001120 if (lcd.light_tempo == 0
1121 && ((lcd.flags & LCD_FLAG_L) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001122 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001123 lcd.light_tempo = FLASH_LIGHT_TEMPO;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001124 }
1125 processed = 1;
1126 break;
1127 case 'f': /* Small Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001128 lcd.flags &= ~LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001129 processed = 1;
1130 break;
1131 case 'F': /* Large Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001132 lcd.flags |= LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001133 processed = 1;
1134 break;
1135 case 'n': /* One Line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001136 lcd.flags &= ~LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001137 processed = 1;
1138 break;
1139 case 'N': /* Two Lines */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001140 lcd.flags |= LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001141 break;
1142 case 'l': /* Shift Cursor Left */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001143 if (lcd.addr.x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001144 /* back one char if not at end of line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001145 if (lcd.addr.x < lcd.bwidth)
Mariusz Gorski21149242014-12-05 22:28:19 +01001146 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001147 lcd.addr.x--;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001148 }
1149 processed = 1;
1150 break;
1151 case 'r': /* shift cursor right */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001152 if (lcd.addr.x < lcd.width) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001153 /* allow the cursor to pass the end of the line */
Mariusz Gorski21149242014-12-05 22:28:19 +01001154 if (lcd.addr.x < (lcd.bwidth - 1))
1155 lcd_write_cmd(LCD_CMD_SHIFT |
1156 LCD_CMD_SHIFT_RIGHT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001157 lcd.addr.x++;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001158 }
1159 processed = 1;
1160 break;
1161 case 'L': /* shift display left */
Mariusz Gorski21149242014-12-05 22:28:19 +01001162 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001163 processed = 1;
1164 break;
1165 case 'R': /* shift display right */
Mariusz Gorski21149242014-12-05 22:28:19 +01001166 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
1167 LCD_CMD_SHIFT_RIGHT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001168 processed = 1;
1169 break;
1170 case 'k': { /* kill end of line */
1171 int x;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001172
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001173 for (x = lcd.addr.x; x < lcd.bwidth; x++)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001174 lcd_write_data(' ');
1175
1176 /* restore cursor position */
1177 lcd_gotoxy();
1178 processed = 1;
1179 break;
1180 }
1181 case 'I': /* reinitialize display */
1182 lcd_init_display();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001183 processed = 1;
1184 break;
1185 case 'G': {
1186 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1187 * and '7', representing the numerical ASCII code of the
1188 * redefined character, and <xx...xx> a sequence of 16
1189 * hex digits representing 8 bytes for each character.
1190 * Most LCDs will only use 5 lower bits of the 7 first
1191 * bytes.
1192 */
1193
1194 unsigned char cgbytes[8];
1195 unsigned char cgaddr;
1196 int cgoffset;
1197 int shift;
1198 char value;
1199 int addr;
1200
1201 if (strchr(esc, ';') == NULL)
1202 break;
1203
1204 esc++;
1205
1206 cgaddr = *(esc++) - '0';
1207 if (cgaddr > 7) {
1208 processed = 1;
1209 break;
1210 }
1211
1212 cgoffset = 0;
1213 shift = 0;
1214 value = 0;
1215 while (*esc && cgoffset < 8) {
1216 shift ^= 4;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001217 if (*esc >= '0' && *esc <= '9') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001218 value |= (*esc - '0') << 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 if (*esc >= 'a' && *esc <= 'z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001222 value |= (*esc - 'a' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001223 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001224 esc++;
1225 continue;
1226 }
1227
1228 if (shift == 0) {
1229 cgbytes[cgoffset++] = value;
1230 value = 0;
1231 }
1232
1233 esc++;
1234 }
1235
Mariusz Gorski21149242014-12-05 22:28:19 +01001236 lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001237 for (addr = 0; addr < cgoffset; addr++)
1238 lcd_write_data(cgbytes[addr]);
1239
1240 /* ensures that we stop writing to CGRAM */
1241 lcd_gotoxy();
1242 processed = 1;
1243 break;
1244 }
1245 case 'x': /* gotoxy : LxXXX[yYYY]; */
1246 case 'y': /* gotoxy : LyYYY[xXXX]; */
1247 if (strchr(esc, ';') == NULL)
1248 break;
1249
1250 while (*esc) {
1251 if (*esc == 'x') {
1252 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001253 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001254 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001255 } else if (*esc == 'y') {
1256 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001257 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001258 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001259 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001260 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001261 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001262 }
1263
1264 lcd_gotoxy();
1265 processed = 1;
1266 break;
1267 }
1268
Mariusz Gorski21149242014-12-05 22:28:19 +01001269 /* TODO: This indent party here got ugly, clean it! */
Adam Buchbinderf2635892012-09-19 21:51:07 -04001270 /* Check whether one flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001271 if (oldflags != lcd.flags) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001272 /* check whether one of B,C,D flags were changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001273 if ((oldflags ^ lcd.flags) &
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001274 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1275 /* set display mode */
Mariusz Gorski21149242014-12-05 22:28:19 +01001276 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
1277 | ((lcd.flags & LCD_FLAG_D)
1278 ? LCD_CMD_DISPLAY_ON : 0)
1279 | ((lcd.flags & LCD_FLAG_C)
1280 ? LCD_CMD_CURSOR_ON : 0)
1281 | ((lcd.flags & LCD_FLAG_B)
1282 ? LCD_CMD_BLINK_ON : 0));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001283 /* check whether one of F,N flags was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001284 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
Mariusz Gorski21149242014-12-05 22:28:19 +01001285 lcd_write_cmd(LCD_CMD_FUNCTION_SET
1286 | LCD_CMD_DATA_LEN_8BITS
1287 | ((lcd.flags & LCD_FLAG_F)
1288 ? LCD_CMD_TWO_LINES : 0)
1289 | ((lcd.flags & LCD_FLAG_N)
1290 ? LCD_CMD_FONT_5X10_DOTS
1291 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001292 /* check whether L flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001293 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1294 if (lcd.flags & (LCD_FLAG_L))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001295 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001296 else if (lcd.light_tempo == 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001297 /* switch off the light only when the tempo
1298 lighting is gone */
1299 lcd_backlight(0);
1300 }
1301 }
1302
1303 return processed;
1304}
1305
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001306static void lcd_write_char(char c)
Willy Tarreau698b1512008-11-22 11:29:33 +01001307{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001308 /* first, we'll test if we're in escape mode */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001309 if ((c != '\n') && lcd.esc_seq.len >= 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001310 /* yes, let's add this char to the buffer */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001311 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1312 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001313 } else {
1314 /* aborts any previous escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001315 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001316
1317 switch (c) {
1318 case LCD_ESCAPE_CHAR:
1319 /* start of an escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001320 lcd.esc_seq.len = 0;
1321 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001322 break;
1323 case '\b':
1324 /* go back one char and clear it */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001325 if (lcd.addr.x > 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001326 /* check if we're not at the
1327 end of the line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001328 if (lcd.addr.x < lcd.bwidth)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001329 /* back one char */
Mariusz Gorski21149242014-12-05 22:28:19 +01001330 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001331 lcd.addr.x--;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001332 }
1333 /* replace with a space */
1334 lcd_write_data(' ');
1335 /* back one char again */
Mariusz Gorski21149242014-12-05 22:28:19 +01001336 lcd_write_cmd(LCD_CMD_SHIFT);
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001337 break;
1338 case '\014':
1339 /* quickly clear the display */
1340 lcd_clear_fast();
1341 break;
1342 case '\n':
1343 /* flush the remainder of the current line and
1344 go to the beginning of the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001345 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001346 lcd_write_data(' ');
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001347 lcd.addr.x = 0;
1348 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001349 lcd_gotoxy();
1350 break;
1351 case '\r':
1352 /* go to the beginning of the same line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001353 lcd.addr.x = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001354 lcd_gotoxy();
1355 break;
1356 case '\t':
1357 /* print a space instead of the tab */
1358 lcd_print(' ');
1359 break;
1360 default:
1361 /* simply print this char */
1362 lcd_print(c);
1363 break;
1364 }
1365 }
1366
1367 /* now we'll see if we're in an escape mode and if the current
1368 escape sequence can be understood. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001369 if (lcd.esc_seq.len >= 2) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001370 int processed = 0;
1371
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001372 if (!strcmp(lcd.esc_seq.buf, "[2J")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001373 /* clear the display */
1374 lcd_clear_fast();
1375 processed = 1;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001376 } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001377 /* cursor to home */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001378 lcd.addr.x = 0;
1379 lcd.addr.y = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001380 lcd_gotoxy();
1381 processed = 1;
1382 }
1383 /* codes starting with ^[[L */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001384 else if ((lcd.esc_seq.len >= 3) &&
1385 (lcd.esc_seq.buf[0] == '[') &&
1386 (lcd.esc_seq.buf[1] == 'L')) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001387 processed = handle_lcd_special_code();
1388 }
1389
1390 /* LCD special escape codes */
1391 /* flush the escape sequence if it's been processed
1392 or if it is getting too long. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001393 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1394 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001395 } /* escape codes */
1396}
1397
1398static ssize_t lcd_write(struct file *file,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001399 const char __user *buf, size_t count, loff_t *ppos)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001400{
1401 const char __user *tmp = buf;
Willy Tarreau698b1512008-11-22 11:29:33 +01001402 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001403
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001404 for (; count-- > 0; (*ppos)++, tmp++) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001405 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001406 /* let's be a little nice with other processes
1407 that need some CPU */
1408 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001409
Bastien Armand6a4193a2014-04-23 19:42:11 +02001410 if (get_user(c, tmp))
Willy Tarreau698b1512008-11-22 11:29:33 +01001411 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001412
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001413 lcd_write_char(c);
Willy Tarreau7005b582008-11-13 17:18:59 -08001414 }
1415
Willy Tarreau698b1512008-11-22 11:29:33 +01001416 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001417}
1418
Willy Tarreau698b1512008-11-22 11:29:33 +01001419static int lcd_open(struct inode *inode, struct file *file)
1420{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001421 if (!atomic_dec_and_test(&lcd_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001422 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001423
Willy Tarreau698b1512008-11-22 11:29:33 +01001424 if (file->f_mode & FMODE_READ) /* device is write-only */
1425 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001426
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001427 if (lcd.must_clear) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001428 lcd_clear_display();
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001429 lcd.must_clear = false;
Willy Tarreau698b1512008-11-22 11:29:33 +01001430 }
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001431 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001432}
1433
Willy Tarreau698b1512008-11-22 11:29:33 +01001434static int lcd_release(struct inode *inode, struct file *file)
1435{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001436 atomic_inc(&lcd_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001437 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001438}
1439
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001440static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001441 .write = lcd_write,
1442 .open = lcd_open,
1443 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001444 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001445};
1446
1447static struct miscdevice lcd_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001448 .minor = LCD_MINOR,
1449 .name = "lcd",
1450 .fops = &lcd_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001451};
1452
Willy Tarreau7005b582008-11-13 17:18:59 -08001453/* public function usable from the kernel for any purpose */
Peter Huewe36d20412013-02-15 13:47:05 +01001454static void panel_lcd_print(const char *s)
Willy Tarreau698b1512008-11-22 11:29:33 +01001455{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001456 const char *tmp = s;
1457 int count = strlen(s);
1458
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001459 if (lcd.enabled && lcd.initialized) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001460 for (; count-- > 0; tmp++) {
1461 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
1462 /* let's be a little nice with other processes
1463 that need some CPU */
1464 schedule();
1465
1466 lcd_write_char(*tmp);
1467 }
1468 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001469}
1470
Willy Tarreau7005b582008-11-13 17:18:59 -08001471/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +01001472static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001473{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001474 switch (selected_lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001475 case LCD_TYPE_OLD:
1476 /* parallel mode, 8 bits */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001477 lcd.proto = LCD_PROTO_PARALLEL;
1478 lcd.charset = LCD_CHARSET_NORMAL;
1479 lcd.pins.e = PIN_STROBE;
1480 lcd.pins.rs = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001481
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001482 lcd.width = 40;
1483 lcd.bwidth = 40;
1484 lcd.hwidth = 64;
1485 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001486 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001487 case LCD_TYPE_KS0074:
1488 /* serial mode, ks0074 */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001489 lcd.proto = LCD_PROTO_SERIAL;
1490 lcd.charset = LCD_CHARSET_KS0074;
1491 lcd.pins.bl = PIN_AUTOLF;
1492 lcd.pins.cl = PIN_STROBE;
1493 lcd.pins.da = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001494
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001495 lcd.width = 16;
1496 lcd.bwidth = 40;
1497 lcd.hwidth = 16;
1498 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001499 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001500 case LCD_TYPE_NEXCOM:
1501 /* parallel mode, 8 bits, generic */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001502 lcd.proto = LCD_PROTO_PARALLEL;
1503 lcd.charset = LCD_CHARSET_NORMAL;
1504 lcd.pins.e = PIN_AUTOLF;
1505 lcd.pins.rs = PIN_SELECP;
1506 lcd.pins.rw = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001507
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001508 lcd.width = 16;
1509 lcd.bwidth = 40;
1510 lcd.hwidth = 64;
1511 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001512 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001513 case LCD_TYPE_CUSTOM:
1514 /* customer-defined */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001515 lcd.proto = DEFAULT_LCD_PROTO;
1516 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001517 /* default geometry will be set later */
1518 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001519 case LCD_TYPE_HANTRONIX:
1520 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001521 default:
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001522 lcd.proto = LCD_PROTO_PARALLEL;
1523 lcd.charset = LCD_CHARSET_NORMAL;
1524 lcd.pins.e = PIN_STROBE;
1525 lcd.pins.rs = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001526
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001527 lcd.width = 16;
1528 lcd.bwidth = 40;
1529 lcd.hwidth = 64;
1530 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001531 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001532 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001533
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001534 /* Overwrite with module params set on loading */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001535 if (lcd_height != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001536 lcd.height = lcd_height;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001537 if (lcd_width != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001538 lcd.width = lcd_width;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001539 if (lcd_bwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001540 lcd.bwidth = lcd_bwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001541 if (lcd_hwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001542 lcd.hwidth = lcd_hwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001543 if (lcd_charset != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001544 lcd.charset = lcd_charset;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001545 if (lcd_proto != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001546 lcd.proto = lcd_proto;
1547 if (lcd_e_pin != PIN_NOT_SET)
1548 lcd.pins.e = lcd_e_pin;
1549 if (lcd_rs_pin != PIN_NOT_SET)
1550 lcd.pins.rs = lcd_rs_pin;
1551 if (lcd_rw_pin != PIN_NOT_SET)
1552 lcd.pins.rw = lcd_rw_pin;
1553 if (lcd_cl_pin != PIN_NOT_SET)
1554 lcd.pins.cl = lcd_cl_pin;
1555 if (lcd_da_pin != PIN_NOT_SET)
1556 lcd.pins.da = lcd_da_pin;
1557 if (lcd_bl_pin != PIN_NOT_SET)
1558 lcd.pins.bl = lcd_bl_pin;
Willy Tarreau7005b582008-11-13 17:18:59 -08001559
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001560 /* this is used to catch wrong and default values */
1561 if (lcd.width <= 0)
1562 lcd.width = DEFAULT_LCD_WIDTH;
1563 if (lcd.bwidth <= 0)
1564 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1565 if (lcd.hwidth <= 0)
1566 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1567 if (lcd.height <= 0)
1568 lcd.height = DEFAULT_LCD_HEIGHT;
1569
1570 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001571 lcd_write_cmd = lcd_write_cmd_s;
1572 lcd_write_data = lcd_write_data_s;
1573 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001574
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001575 if (lcd.pins.cl == PIN_NOT_SET)
1576 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1577 if (lcd.pins.da == PIN_NOT_SET)
1578 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001579
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001580 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001581 lcd_write_cmd = lcd_write_cmd_p8;
1582 lcd_write_data = lcd_write_data_p8;
1583 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001584
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001585 if (lcd.pins.e == PIN_NOT_SET)
1586 lcd.pins.e = DEFAULT_LCD_PIN_E;
1587 if (lcd.pins.rs == PIN_NOT_SET)
1588 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1589 if (lcd.pins.rw == PIN_NOT_SET)
1590 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001591 } else {
1592 lcd_write_cmd = lcd_write_cmd_tilcd;
1593 lcd_write_data = lcd_write_data_tilcd;
1594 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001595 }
1596
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001597 if (lcd.pins.bl == PIN_NOT_SET)
1598 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
Willy Tarreau698b1512008-11-22 11:29:33 +01001599
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001600 if (lcd.pins.e == PIN_NOT_SET)
1601 lcd.pins.e = PIN_NONE;
1602 if (lcd.pins.rs == PIN_NOT_SET)
1603 lcd.pins.rs = PIN_NONE;
1604 if (lcd.pins.rw == PIN_NOT_SET)
1605 lcd.pins.rw = PIN_NONE;
1606 if (lcd.pins.bl == PIN_NOT_SET)
1607 lcd.pins.bl = PIN_NONE;
1608 if (lcd.pins.cl == PIN_NOT_SET)
1609 lcd.pins.cl = PIN_NONE;
1610 if (lcd.pins.da == PIN_NOT_SET)
1611 lcd.pins.da = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001612
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001613 if (lcd.charset == NOT_SET)
1614 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001615
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001616 if (lcd.charset == LCD_CHARSET_KS0074)
Willy Tarreau698b1512008-11-22 11:29:33 +01001617 lcd_char_conv = lcd_char_conv_ks0074;
1618 else
1619 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001620
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001621 if (lcd.pins.bl != PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +01001622 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001623
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001624 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
Willy Tarreau698b1512008-11-22 11:29:33 +01001625 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001626 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
Willy Tarreau698b1512008-11-22 11:29:33 +01001627 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001628 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
Willy Tarreau698b1512008-11-22 11:29:33 +01001629 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001630 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001631 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001632 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001633 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001634 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
Willy Tarreau698b1512008-11-22 11:29:33 +01001635 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001636
Willy Tarreau698b1512008-11-22 11:29:33 +01001637 /* before this line, we must NOT send anything to the display.
1638 * Since lcd_init_display() needs to write data, we have to
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001639 * enable mark the LCD initialized just before. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001640 lcd.initialized = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001641 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001642
Willy Tarreau698b1512008-11-22 11:29:33 +01001643 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001644#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1645#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001646 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001647#endif
1648#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001649 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1650 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001651#endif
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001652 lcd.addr.x = 0;
1653 lcd.addr.y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001654 /* clear the display on the next device opening */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001655 lcd.must_clear = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001656 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001657}
1658
Willy Tarreau7005b582008-11-13 17:18:59 -08001659/*
1660 * These are the file operation function for user access to /dev/keypad
1661 */
1662
Willy Tarreau698b1512008-11-22 11:29:33 +01001663static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001664 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001665{
Willy Tarreau698b1512008-11-22 11:29:33 +01001666 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001667 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001668
Willy Tarreau698b1512008-11-22 11:29:33 +01001669 if (keypad_buflen == 0) {
1670 if (file->f_flags & O_NONBLOCK)
1671 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001672
Arnd Bergmann310df692014-01-02 13:07:35 +01001673 if (wait_event_interruptible(keypad_read_wait,
1674 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001675 return -EINTR;
1676 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001677
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001678 for (; count-- > 0 && (keypad_buflen > 0);
1679 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001680 put_user(keypad_buffer[keypad_start], tmp);
1681 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1682 }
1683 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001684
Willy Tarreau698b1512008-11-22 11:29:33 +01001685 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001686}
1687
Willy Tarreau698b1512008-11-22 11:29:33 +01001688static int keypad_open(struct inode *inode, struct file *file)
1689{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001690 if (!atomic_dec_and_test(&keypad_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001691 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001692
Willy Tarreau698b1512008-11-22 11:29:33 +01001693 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1694 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001695
Willy Tarreau698b1512008-11-22 11:29:33 +01001696 keypad_buflen = 0; /* flush the buffer on opening */
Willy Tarreau698b1512008-11-22 11:29:33 +01001697 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001698}
1699
Willy Tarreau698b1512008-11-22 11:29:33 +01001700static int keypad_release(struct inode *inode, struct file *file)
1701{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001702 atomic_inc(&keypad_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001703 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001704}
1705
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001706static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001707 .read = keypad_read, /* read */
1708 .open = keypad_open, /* open */
1709 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001710 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001711};
1712
1713static struct miscdevice keypad_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001714 .minor = KEYPAD_MINOR,
1715 .name = "keypad",
1716 .fops = &keypad_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001717};
1718
Peter Huewe36d20412013-02-15 13:47:05 +01001719static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001720{
1721 if (init_in_progress)
1722 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001723
Willy Tarreau698b1512008-11-22 11:29:33 +01001724 /* send the key to the device only if a process is attached to it. */
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001725 if (!atomic_read(&keypad_available)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001726 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1727 keypad_buffer[(keypad_start + keypad_buflen++) %
1728 KEYPAD_BUFFER] = *string++;
1729 }
1730 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001731 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001732}
1733
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001734/* this function scans all the bits involving at least one logical signal,
1735 * and puts the results in the bitfield "phys_read" (one bit per established
1736 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001737 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001738 * Note: to debounce input signals, we will only consider as switched a signal
1739 * which is stable across 2 measures. Signals which are different between two
1740 * reads will be kept as they previously were in their logical form (phys_prev).
1741 * A signal which has just switched will have a 1 in
1742 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001743 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001744static void phys_scan_contacts(void)
1745{
1746 int bit, bitval;
1747 char oldval;
1748 char bitmask;
1749 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001750
Willy Tarreau698b1512008-11-22 11:29:33 +01001751 phys_prev = phys_curr;
1752 phys_read_prev = phys_read;
1753 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001754
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001755 /* keep track of old value, with all outputs disabled */
1756 oldval = r_dtr(pprt) | scan_mask_o;
1757 /* activate all keyboard outputs (active low) */
1758 w_dtr(pprt, oldval & ~scan_mask_o);
1759
1760 /* will have a 1 for each bit set to gnd */
1761 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1762 /* disable all matrix signals */
1763 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001764
Willy Tarreau698b1512008-11-22 11:29:33 +01001765 /* now that all outputs are cleared, the only active input bits are
1766 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001767 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001768
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001769 /* 1 for each grounded input */
1770 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1771
1772 /* grounded inputs are signals 40-44 */
1773 phys_read |= (pmask_t) gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001774
Willy Tarreau698b1512008-11-22 11:29:33 +01001775 if (bitmask != gndmask) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001776 /* since clearing the outputs changed some inputs, we know
1777 * that some input signals are currently tied to some outputs.
1778 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001779 */
1780 for (bit = 0; bit < 8; bit++) {
1781 bitval = 1 << bit;
1782
1783 if (!(scan_mask_o & bitval)) /* skip unused bits */
1784 continue;
1785
1786 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1787 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1788 phys_read |= (pmask_t) bitmask << (5 * bit);
1789 }
1790 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001791 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001792 /* this is easy: use old bits when they are flapping,
1793 * use new ones when stable */
1794 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1795 (phys_read & ~(phys_read ^ phys_read_prev));
1796}
1797
1798static inline int input_state_high(struct logical_input *input)
1799{
1800#if 0
1801 /* FIXME:
1802 * this is an invalid test. It tries to catch
1803 * transitions from single-key to multiple-key, but
1804 * doesn't take into account the contacts polarity.
1805 * The only solution to the problem is to parse keys
1806 * from the most complex to the simplest combinations,
1807 * and mark them as 'caught' once a combination
1808 * matches, then unmatch it for all other ones.
1809 */
1810
1811 /* try to catch dangerous transitions cases :
1812 * someone adds a bit, so this signal was a false
1813 * positive resulting from a transition. We should
1814 * invalidate the signal immediately and not call the
1815 * release function.
1816 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1817 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001818 if (((phys_prev & input->mask) == input->value) &&
1819 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001820 input->state = INPUT_ST_LOW; /* invalidate */
1821 return 1;
1822 }
1823#endif
1824
1825 if ((phys_curr & input->mask) == input->value) {
1826 if ((input->type == INPUT_TYPE_STD) &&
1827 (input->high_timer == 0)) {
1828 input->high_timer++;
1829 if (input->u.std.press_fct != NULL)
1830 input->u.std.press_fct(input->u.std.press_data);
1831 } else if (input->type == INPUT_TYPE_KBD) {
1832 /* will turn on the light */
1833 keypressed = 1;
1834
1835 if (input->high_timer == 0) {
1836 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001837
Jake Champline6626de2013-05-04 11:21:17 -04001838 if (press_str[0]) {
1839 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001840
Jake Champline6626de2013-05-04 11:21:17 -04001841 keypad_send_key(press_str, s);
1842 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001843 }
1844
1845 if (input->u.kbd.repeat_str[0]) {
1846 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001847
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001848 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001849 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001850
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001851 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001852 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001853 }
1854 /* we will need to come back here soon */
1855 inputs_stable = 0;
1856 }
1857
1858 if (input->high_timer < 255)
1859 input->high_timer++;
1860 }
1861 return 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001862 }
Vincent Heuken083b3632014-07-10 03:34:28 -07001863
1864 /* else signal falling down. Let's fall through. */
1865 input->state = INPUT_ST_FALLING;
1866 input->fall_timer = 0;
1867
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001868 return 0;
1869}
1870
1871static inline void input_state_falling(struct logical_input *input)
1872{
1873#if 0
1874 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001875 if (((phys_prev & input->mask) == input->value) &&
1876 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001877 input->state = INPUT_ST_LOW; /* invalidate */
1878 return;
1879 }
1880#endif
1881
1882 if ((phys_curr & input->mask) == input->value) {
1883 if (input->type == INPUT_TYPE_KBD) {
1884 /* will turn on the light */
1885 keypressed = 1;
1886
1887 if (input->u.kbd.repeat_str[0]) {
1888 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001889
Jake Champline6626de2013-05-04 11:21:17 -04001890 if (input->high_timer >= KEYPAD_REP_START) {
1891 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001892
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001893 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001894 keypad_send_key(repeat_str, s);
1895 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001896 /* we will need to come back here soon */
1897 inputs_stable = 0;
1898 }
1899
1900 if (input->high_timer < 255)
1901 input->high_timer++;
1902 }
1903 input->state = INPUT_ST_HIGH;
1904 } else if (input->fall_timer >= input->fall_time) {
1905 /* call release event */
1906 if (input->type == INPUT_TYPE_STD) {
1907 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001908
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001909 if (release_fct != NULL)
1910 release_fct(input->u.std.release_data);
1911 } else if (input->type == INPUT_TYPE_KBD) {
1912 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001913
Jake Champline6626de2013-05-04 11:21:17 -04001914 if (release_str[0]) {
1915 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001916
Jake Champline6626de2013-05-04 11:21:17 -04001917 keypad_send_key(release_str, s);
1918 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001919 }
1920
1921 input->state = INPUT_ST_LOW;
1922 } else {
1923 input->fall_timer++;
1924 inputs_stable = 0;
1925 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001926}
1927
Willy Tarreau698b1512008-11-22 11:29:33 +01001928static void panel_process_inputs(void)
1929{
1930 struct list_head *item;
1931 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001932
Willy Tarreau698b1512008-11-22 11:29:33 +01001933 keypressed = 0;
1934 inputs_stable = 1;
1935 list_for_each(item, &logical_inputs) {
1936 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001937
Willy Tarreau698b1512008-11-22 11:29:33 +01001938 switch (input->state) {
1939 case INPUT_ST_LOW:
1940 if ((phys_curr & input->mask) != input->value)
1941 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001942 /* if all needed ones were already set previously,
1943 * this means that this logical signal has been
1944 * activated by the releasing of another combined
1945 * signal, so we don't want to match.
1946 * eg: AB -(release B)-> A -(release A)-> 0 :
1947 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001948 */
1949 if ((phys_prev & input->mask) == input->value)
1950 break;
1951 input->rise_timer = 0;
1952 input->state = INPUT_ST_RISING;
1953 /* no break here, fall through */
1954 case INPUT_ST_RISING:
1955 if ((phys_curr & input->mask) != input->value) {
1956 input->state = INPUT_ST_LOW;
1957 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001958 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001959 if (input->rise_timer < input->rise_time) {
1960 inputs_stable = 0;
1961 input->rise_timer++;
1962 break;
1963 }
1964 input->high_timer = 0;
1965 input->state = INPUT_ST_HIGH;
1966 /* no break here, fall through */
1967 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001968 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001969 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001970 /* no break here, fall through */
1971 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001972 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001973 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001974 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001975}
1976
Willy Tarreau698b1512008-11-22 11:29:33 +01001977static void panel_scan_timer(void)
1978{
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001979 if (keypad.enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001980 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001981 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001982
1983 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001984 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001985 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001986
Willy Tarreau698b1512008-11-22 11:29:33 +01001987 if (!inputs_stable || phys_curr != phys_prev)
1988 panel_process_inputs();
1989 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001990
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001991 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001992 if (keypressed) {
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001993 if (lcd.light_tempo == 0
1994 && ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001995 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001996 lcd.light_tempo = FLASH_LIGHT_TEMPO;
1997 } else if (lcd.light_tempo > 0) {
1998 lcd.light_tempo--;
1999 if (lcd.light_tempo == 0
2000 && ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01002001 lcd_backlight(0);
2002 }
2003 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002004
Willy Tarreau698b1512008-11-22 11:29:33 +01002005 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08002006}
2007
Willy Tarreau698b1512008-11-22 11:29:33 +01002008static void init_scan_timer(void)
2009{
2010 if (scan_timer.function != NULL)
2011 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08002012
Aya Mahfouz8f6e36c2015-02-19 07:58:29 +02002013 setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
Willy Tarreau698b1512008-11-22 11:29:33 +01002014 scan_timer.expires = jiffies + INPUT_POLL_TIME;
Willy Tarreau698b1512008-11-22 11:29:33 +01002015 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002016}
2017
2018/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002019 * if <omask> or <imask> are non-null, they will be or'ed with the bits
2020 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08002021 * returns 1 if ok, 0 if error (in which case, nothing is written).
2022 */
Peter Huewe36d20412013-02-15 13:47:05 +01002023static int input_name2mask(const char *name, pmask_t *mask, pmask_t *value,
Willy Tarreau698b1512008-11-22 11:29:33 +01002024 char *imask, char *omask)
2025{
2026 static char sigtab[10] = "EeSsPpAaBb";
2027 char im, om;
2028 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08002029
Dominique van den Broeck2d534262014-05-24 01:35:24 +02002030 om = 0ULL;
2031 im = 0ULL;
2032 m = 0ULL;
2033 v = 0ULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002034 while (*name) {
2035 int in, out, bit, neg;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002036
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002037 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name);
2038 in++)
Willy Tarreau698b1512008-11-22 11:29:33 +01002039 ;
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002040
Willy Tarreau698b1512008-11-22 11:29:33 +01002041 if (in >= sizeof(sigtab))
2042 return 0; /* input name not found */
2043 neg = (in & 1); /* odd (lower) names are negated */
2044 in >>= 1;
2045 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08002046
Willy Tarreau698b1512008-11-22 11:29:33 +01002047 name++;
2048 if (isdigit(*name)) {
2049 out = *name - '0';
2050 om |= (1 << out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002051 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002052 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002053 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01002054 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002055 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002056
2057 bit = (out * 5) + in;
2058
2059 m |= 1ULL << bit;
2060 if (!neg)
2061 v |= 1ULL << bit;
2062 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08002063 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002064 *mask = m;
2065 *value = v;
2066 if (imask)
2067 *imask |= im;
2068 if (omask)
2069 *omask |= om;
2070 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002071}
2072
2073/* tries to bind a key to the signal name <name>. The key will send the
2074 * strings <press>, <repeat>, <release> for these respective events.
2075 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2076 */
Peter Huewe36d20412013-02-15 13:47:05 +01002077static struct logical_input *panel_bind_key(const char *name, const char *press,
2078 const char *repeat,
2079 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01002080{
2081 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002082
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002083 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002084 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01002085 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002086
Willy Tarreau698b1512008-11-22 11:29:33 +01002087 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002088 &scan_mask_o)) {
2089 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01002090 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002091 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002092
2093 key->type = INPUT_TYPE_KBD;
2094 key->state = INPUT_ST_LOW;
2095 key->rise_time = 1;
2096 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002097
Willy Tarreau698b1512008-11-22 11:29:33 +01002098 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2099 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2100 strncpy(key->u.kbd.release_str, release,
2101 sizeof(key->u.kbd.release_str));
2102 list_add(&key->list, &logical_inputs);
2103 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002104}
2105
Willy Tarreau630231772008-11-22 12:52:18 +01002106#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002107/* tries to bind a callback function to the signal name <name>. The function
2108 * <press_fct> will be called with the <press_data> arg when the signal is
2109 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002110 * Returns the pointer to the new signal if ok, NULL if the signal could not
2111 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002112 */
2113static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302114 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002115 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302116 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002117 int release_data)
2118{
2119 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002120
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002121 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002122 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002123 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002124
Willy Tarreau698b1512008-11-22 11:29:33 +01002125 memset(callback, 0, sizeof(struct logical_input));
2126 if (!input_name2mask(name, &callback->mask, &callback->value,
2127 &scan_mask_i, &scan_mask_o))
2128 return NULL;
2129
2130 callback->type = INPUT_TYPE_STD;
2131 callback->state = INPUT_ST_LOW;
2132 callback->rise_time = 1;
2133 callback->fall_time = 1;
2134 callback->u.std.press_fct = press_fct;
2135 callback->u.std.press_data = press_data;
2136 callback->u.std.release_fct = release_fct;
2137 callback->u.std.release_data = release_data;
2138 list_add(&callback->list, &logical_inputs);
2139 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002140}
Willy Tarreau630231772008-11-22 12:52:18 +01002141#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002142
Willy Tarreau698b1512008-11-22 11:29:33 +01002143static void keypad_init(void)
2144{
2145 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002146
Willy Tarreau698b1512008-11-22 11:29:33 +01002147 init_waitqueue_head(&keypad_read_wait);
2148 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002149
Willy Tarreau698b1512008-11-22 11:29:33 +01002150 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002151
Willy Tarreau698b1512008-11-22 11:29:33 +01002152 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2153 panel_bind_key(keypad_profile[keynum][0],
2154 keypad_profile[keynum][1],
2155 keypad_profile[keynum][2],
2156 keypad_profile[keynum][3]);
2157 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002158
Willy Tarreau698b1512008-11-22 11:29:33 +01002159 init_scan_timer();
2160 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002161}
2162
Willy Tarreau7005b582008-11-13 17:18:59 -08002163/**************************************************/
2164/* device initialization */
2165/**************************************************/
2166
Willy Tarreau698b1512008-11-22 11:29:33 +01002167static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2168 void *unused)
2169{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002170 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002171 switch (code) {
2172 case SYS_DOWN:
2173 panel_lcd_print
2174 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2175 break;
2176 case SYS_HALT:
2177 panel_lcd_print
2178 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2179 break;
2180 case SYS_POWER_OFF:
2181 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2182 break;
2183 default:
2184 break;
2185 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002186 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002187 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002188}
2189
2190static struct notifier_block panel_notifier = {
2191 panel_notify_sys,
2192 NULL,
2193 0
2194};
2195
Willy Tarreau698b1512008-11-22 11:29:33 +01002196static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002197{
Willy Tarreau698b1512008-11-22 11:29:33 +01002198 if (port->number != parport)
2199 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002200
Willy Tarreau698b1512008-11-22 11:29:33 +01002201 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002202 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2203 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002204 return;
2205 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002206
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002207 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
Willy Tarreau698b1512008-11-22 11:29:33 +01002208 NULL,
2209 /*PARPORT_DEV_EXCL */
2210 0, (void *)&pprt);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002211 if (pprt == NULL) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002212 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2213 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002214 return;
2215 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002216
Willy Tarreau698b1512008-11-22 11:29:33 +01002217 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002218 pr_err("could not claim access to parport%d. Aborting.\n",
2219 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002220 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002221 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002222
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002223 /* must init LCD first, just in case an IRQ from the keypad is
2224 * generated at keypad init
2225 */
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002226 if (lcd.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002227 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002228 if (misc_register(&lcd_dev))
2229 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002230 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002231
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002232 if (keypad.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002233 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002234 if (misc_register(&keypad_dev))
2235 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002236 }
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002237 return;
2238
2239err_lcd_unreg:
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002240 if (lcd.enabled)
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002241 misc_deregister(&lcd_dev);
2242err_unreg_device:
2243 parport_unregister_device(pprt);
2244 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002245}
2246
Willy Tarreau698b1512008-11-22 11:29:33 +01002247static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002248{
Willy Tarreau698b1512008-11-22 11:29:33 +01002249 if (port->number != parport)
2250 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002251
Willy Tarreau698b1512008-11-22 11:29:33 +01002252 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002253 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2254 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002255 return;
2256 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002257
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002258 if (keypad.enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002259 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002260 keypad_initialized = 0;
2261 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002262
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002263 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002264 misc_deregister(&lcd_dev);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002265 lcd.initialized = false;
Peter Huewe0b0595b2009-09-29 01:22:40 +02002266 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002267
Willy Tarreau698b1512008-11-22 11:29:33 +01002268 parport_release(pprt);
2269 parport_unregister_device(pprt);
2270 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002271}
2272
2273static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002274 .name = "panel",
2275 .attach = panel_attach,
2276 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002277};
2278
2279/* init function */
Mariusz Gorskid9114762014-11-27 22:36:46 +01002280static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002281{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002282 int selected_keypad_type = NOT_SET;
Willy Tarreau7005b582008-11-13 17:18:59 -08002283
Willy Tarreau698b1512008-11-22 11:29:33 +01002284 /* take care of an eventual profile */
2285 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002286 case PANEL_PROFILE_CUSTOM:
2287 /* custom profile */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002288 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2289 selected_lcd_type = DEFAULT_LCD_TYPE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002290 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002291 case PANEL_PROFILE_OLD:
2292 /* 8 bits, 2*16, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002293 selected_keypad_type = KEYPAD_TYPE_OLD;
2294 selected_lcd_type = LCD_TYPE_OLD;
2295
2296 /* TODO: This two are a little hacky, sort it out later */
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002297 if (lcd_width == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002298 lcd_width = 16;
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002299 if (lcd_hwidth == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002300 lcd_hwidth = 16;
2301 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002302 case PANEL_PROFILE_NEW:
2303 /* serial, 2*16, new keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002304 selected_keypad_type = KEYPAD_TYPE_NEW;
2305 selected_lcd_type = LCD_TYPE_KS0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01002306 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002307 case PANEL_PROFILE_HANTRONIX:
2308 /* 8 bits, 2*16 hantronix-like, no keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002309 selected_keypad_type = KEYPAD_TYPE_NONE;
2310 selected_lcd_type = LCD_TYPE_HANTRONIX;
Willy Tarreau698b1512008-11-22 11:29:33 +01002311 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002312 case PANEL_PROFILE_NEXCOM:
2313 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002314 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2315 selected_lcd_type = LCD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002316 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002317 case PANEL_PROFILE_LARGE:
2318 /* 8 bits, 2*40, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002319 selected_keypad_type = KEYPAD_TYPE_OLD;
2320 selected_lcd_type = LCD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002321 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002322 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002323
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002324
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01002325 /*
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002326 * Overwrite selection with module param values (both keypad and lcd),
2327 * where the deprecated params have lower prio.
2328 */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002329 if (keypad_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002330 selected_keypad_type = keypad_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002331 if (keypad_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002332 selected_keypad_type = keypad_type;
Willy Tarreau7005b582008-11-13 17:18:59 -08002333
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002334 keypad.enabled = (selected_keypad_type > 0);
2335
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002336 if (lcd_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002337 selected_lcd_type = lcd_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002338 if (lcd_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002339 selected_lcd_type = lcd_type;
2340
2341 lcd.enabled = (selected_lcd_type > 0);
2342
Sudip Mukherjee733345e2015-02-11 16:57:08 +05302343 if (lcd.enabled) {
2344 /*
2345 * Init lcd struct with load-time values to preserve exact
2346 * current functionality (at least for now).
2347 */
2348 lcd.height = lcd_height;
2349 lcd.width = lcd_width;
2350 lcd.bwidth = lcd_bwidth;
2351 lcd.hwidth = lcd_hwidth;
2352 lcd.charset = lcd_charset;
2353 lcd.proto = lcd_proto;
2354 lcd.pins.e = lcd_e_pin;
2355 lcd.pins.rs = lcd_rs_pin;
2356 lcd.pins.rw = lcd_rw_pin;
2357 lcd.pins.cl = lcd_cl_pin;
2358 lcd.pins.da = lcd_da_pin;
2359 lcd.pins.bl = lcd_bl_pin;
2360
2361 /* Leave it for now, just in case */
2362 lcd.esc_seq.len = -1;
2363 }
2364
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002365 switch (selected_keypad_type) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002366 case KEYPAD_TYPE_OLD:
2367 keypad_profile = old_keypad_profile;
2368 break;
2369 case KEYPAD_TYPE_NEW:
2370 keypad_profile = new_keypad_profile;
2371 break;
2372 case KEYPAD_TYPE_NEXCOM:
2373 keypad_profile = nexcom_keypad_profile;
2374 break;
2375 default:
2376 keypad_profile = NULL;
2377 break;
2378 }
2379
2380 /* tells various subsystems about the fact that we are initializing */
2381 init_in_progress = 1;
2382
Sudip Mukherjeef43de772015-02-10 17:26:02 +05302383 if (!lcd.enabled && !keypad.enabled) {
2384 /* no device enabled, let's exit */
2385 pr_err("driver version " PANEL_VERSION " disabled.\n");
2386 return -ENODEV;
2387 }
2388
Willy Tarreau698b1512008-11-22 11:29:33 +01002389 if (parport_register_driver(&panel_driver)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002390 pr_err("could not register with parport. Aborting.\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002391 return -EIO;
2392 }
2393
Willy Tarreau698b1512008-11-22 11:29:33 +01002394 register_reboot_notifier(&panel_notifier);
2395
2396 if (pprt)
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002397 pr_info("driver version " PANEL_VERSION
2398 " registered on parport%d (io=0x%lx).\n", parport,
2399 pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002400 else
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002401 pr_info("driver version " PANEL_VERSION
2402 " not yet registered\n");
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002403 /* tells various subsystems about the fact that initialization
2404 is finished */
Willy Tarreau698b1512008-11-22 11:29:33 +01002405 init_in_progress = 0;
2406 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002407}
2408
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002409static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002410{
2411 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002412
Willy Tarreau698b1512008-11-22 11:29:33 +01002413 if (scan_timer.function != NULL)
Julia Lawalle112f892014-03-26 22:33:41 +01002414 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002415
Costantino Leandro57898132009-02-17 11:10:48 -05002416 if (pprt != NULL) {
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002417 if (keypad.enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002418 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002419 keypad_initialized = 0;
2420 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002421
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002422 if (lcd.enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002423 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2424 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2425 misc_deregister(&lcd_dev);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002426 lcd.initialized = false;
Costantino Leandro57898132009-02-17 11:10:48 -05002427 }
2428
2429 /* TODO: free all input signals */
2430 parport_release(pprt);
2431 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002432 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002433 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002434 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002435}
Willy Tarreau7005b582008-11-13 17:18:59 -08002436
Willy Tarreau7005b582008-11-13 17:18:59 -08002437module_init(panel_init_module);
2438module_exit(panel_cleanup_module);
2439MODULE_AUTHOR("Willy Tarreau");
2440MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002441
2442/*
2443 * Local variables:
2444 * c-indent-level: 4
2445 * tab-width: 8
2446 * End:
2447 */