blob: a9fa4c0ac22064aef9351898a979f76054569fc0 [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 */
Sirnam Swetha2b3c9eb2015-10-27 14:54:51 +053074#define INPUT_POLL_TIME (HZ / 50)
Henri Häkkinen429ccf02010-06-12 00:27:36 +030075/* 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 Tarreau7005b582008-11-13 17:18:59 -0800175enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100176 INPUT_TYPE_STD,
177 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800178};
179
180enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100181 INPUT_ST_LOW,
182 INPUT_ST_RISING,
183 INPUT_ST_HIGH,
184 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800185};
186
187struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100188 struct list_head list;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100189 __u64 mask;
190 __u64 value;
Willy Tarreau698b1512008-11-22 11:29:33 +0100191 enum input_type type;
192 enum input_state state;
193 __u8 rise_time, fall_time;
194 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800195
Willy Tarreau698b1512008-11-22 11:29:33 +0100196 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300197 struct { /* valid when type == INPUT_TYPE_STD */
Monam Agarwal68d386b2014-02-25 19:40:49 +0530198 void (*press_fct)(int);
199 void (*release_fct)(int);
Willy Tarreau698b1512008-11-22 11:29:33 +0100200 int press_data;
201 int release_data;
202 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300203 struct { /* valid when type == INPUT_TYPE_KBD */
204 /* strings can be non null-terminated */
Willy Tarreau698b1512008-11-22 11:29:33 +0100205 char press_str[sizeof(void *) + sizeof(int)];
206 char repeat_str[sizeof(void *) + sizeof(int)];
207 char release_str[sizeof(void *) + sizeof(int)];
208 } kbd;
209 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800210};
211
Peter Huewe36d20412013-02-15 13:47:05 +0100212static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800213
214/* physical contacts history
215 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
216 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
217 * corresponds to the ground.
218 * Within each group, bits are stored in the same order as read on the port :
219 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100220 * So, each __u64 is represented like this :
Willy Tarreau7005b582008-11-13 17:18:59 -0800221 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
222 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
223 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300224
225/* what has just been read from the I/O ports */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100226static __u64 phys_read;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300227/* previous phys_read */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100228static __u64 phys_read_prev;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300229/* stabilized phys_read (phys_read|phys_read_prev) */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100230static __u64 phys_curr;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300231/* previous phys_curr */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +0100232static __u64 phys_prev;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300233/* 0 means that at least one logical signal needs be computed */
234static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800235
Willy Tarreau7005b582008-11-13 17:18:59 -0800236/* these variables are specific to the keypad */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100237static struct {
238 bool enabled;
239} keypad;
240
Willy Tarreau7005b582008-11-13 17:18:59 -0800241static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100242static int keypad_buflen;
243static int keypad_start;
244static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800245static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800246
247/* lcd-specific variables */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100248static struct {
249 bool enabled;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100250 bool initialized;
251 bool must_clear;
252
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100253 int height;
254 int width;
255 int bwidth;
256 int hwidth;
257 int charset;
258 int proto;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100259 int light_tempo;
260
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100261 /* TODO: use union here? */
262 struct {
263 int e;
264 int rs;
265 int rw;
266 int cl;
267 int da;
268 int bl;
269 } pins;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100270
271 /* contains the LCD config state */
272 unsigned long int flags;
273
274 /* Contains the LCD X and Y offset */
275 struct {
276 unsigned long int x;
277 unsigned long int y;
278 } addr;
279
280 /* Current escape sequence and it's length or -1 if outside */
281 struct {
282 char buf[LCD_ESCAPE_LEN + 1];
283 int len;
284 } esc_seq;
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100285} lcd;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300286
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +0100287/* Needed only for init */
288static int selected_lcd_type = NOT_SET;
289
Willy Tarreau7005b582008-11-13 17:18:59 -0800290/*
291 * Bit masks to convert LCD signals to parallel port outputs.
292 * _d_ are values for data port, _c_ are for control port.
293 * [0] = signal OFF, [1] = signal ON, [2] = mask
294 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100295#define BIT_CLR 0
296#define BIT_SET 1
297#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800298#define BIT_STATES 3
299/*
300 * one entry for each bit on the LCD
301 */
302#define LCD_BIT_E 0
303#define LCD_BIT_RS 1
304#define LCD_BIT_RW 2
305#define LCD_BIT_BL 3
306#define LCD_BIT_CL 4
307#define LCD_BIT_DA 5
308#define LCD_BITS 6
309
310/*
311 * each bit can be either connected to a DATA or CTRL port
312 */
313#define LCD_PORT_C 0
314#define LCD_PORT_D 1
315#define LCD_PORTS 2
316
317static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
318
319/*
320 * LCD protocols
321 */
322#define LCD_PROTO_PARALLEL 0
323#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400324#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800325
326/*
327 * LCD character sets
328 */
329#define LCD_CHARSET_NORMAL 0
330#define LCD_CHARSET_KS0074 1
331
332/*
333 * LCD types
334 */
335#define LCD_TYPE_NONE 0
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530336#define LCD_TYPE_CUSTOM 1
337#define LCD_TYPE_OLD 2
338#define LCD_TYPE_KS0074 3
339#define LCD_TYPE_HANTRONIX 4
340#define LCD_TYPE_NEXCOM 5
Willy Tarreau7005b582008-11-13 17:18:59 -0800341
342/*
343 * keypad types
344 */
345#define KEYPAD_TYPE_NONE 0
346#define KEYPAD_TYPE_OLD 1
347#define KEYPAD_TYPE_NEW 2
348#define KEYPAD_TYPE_NEXCOM 3
349
350/*
351 * panel profiles
352 */
353#define PANEL_PROFILE_CUSTOM 0
354#define PANEL_PROFILE_OLD 1
355#define PANEL_PROFILE_NEW 2
356#define PANEL_PROFILE_HANTRONIX 3
357#define PANEL_PROFILE_NEXCOM 4
358#define PANEL_PROFILE_LARGE 5
359
360/*
361 * Construct custom config from the kernel's configuration
362 */
Willy Tarreau7005b582008-11-13 17:18:59 -0800363#define DEFAULT_PARPORT 0
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100364#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100365#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
366#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100367#define DEFAULT_LCD_HEIGHT 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800368#define DEFAULT_LCD_WIDTH 40
369#define DEFAULT_LCD_BWIDTH 40
370#define DEFAULT_LCD_HWIDTH 64
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100371#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
Willy Tarreau7005b582008-11-13 17:18:59 -0800372#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
373
374#define DEFAULT_LCD_PIN_E PIN_AUTOLF
375#define DEFAULT_LCD_PIN_RS PIN_SELECP
376#define DEFAULT_LCD_PIN_RW PIN_INITP
377#define DEFAULT_LCD_PIN_SCL PIN_STROBE
378#define DEFAULT_LCD_PIN_SDA PIN_D0
379#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
Willy Tarreau7005b582008-11-13 17:18:59 -0800380
Willy Tarreau7005b582008-11-13 17:18:59 -0800381#ifdef CONFIG_PANEL_PARPORT
382#undef DEFAULT_PARPORT
383#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
384#endif
385
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100386#ifdef CONFIG_PANEL_PROFILE
387#undef DEFAULT_PROFILE
388#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
389#endif
390
Willy Tarreau698b1512008-11-22 11:29:33 +0100391#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800392#ifdef CONFIG_PANEL_KEYPAD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100393#undef DEFAULT_KEYPAD_TYPE
394#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
Willy Tarreau7005b582008-11-13 17:18:59 -0800395#endif
396
Willy Tarreau7005b582008-11-13 17:18:59 -0800397#ifdef CONFIG_PANEL_LCD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100398#undef DEFAULT_LCD_TYPE
399#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
Willy Tarreau7005b582008-11-13 17:18:59 -0800400#endif
401
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100402#ifdef CONFIG_PANEL_LCD_HEIGHT
403#undef DEFAULT_LCD_HEIGHT
404#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
405#endif
406
Willy Tarreau7005b582008-11-13 17:18:59 -0800407#ifdef CONFIG_PANEL_LCD_WIDTH
408#undef DEFAULT_LCD_WIDTH
409#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
410#endif
411
412#ifdef CONFIG_PANEL_LCD_BWIDTH
413#undef DEFAULT_LCD_BWIDTH
414#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
415#endif
416
417#ifdef CONFIG_PANEL_LCD_HWIDTH
418#undef DEFAULT_LCD_HWIDTH
419#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
420#endif
421
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100422#ifdef CONFIG_PANEL_LCD_CHARSET
423#undef DEFAULT_LCD_CHARSET
424#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800425#endif
426
427#ifdef CONFIG_PANEL_LCD_PROTO
428#undef DEFAULT_LCD_PROTO
429#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
430#endif
431
432#ifdef CONFIG_PANEL_LCD_PIN_E
433#undef DEFAULT_LCD_PIN_E
434#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
435#endif
436
437#ifdef CONFIG_PANEL_LCD_PIN_RS
438#undef DEFAULT_LCD_PIN_RS
439#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
440#endif
441
442#ifdef CONFIG_PANEL_LCD_PIN_RW
443#undef DEFAULT_LCD_PIN_RW
444#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
445#endif
446
447#ifdef CONFIG_PANEL_LCD_PIN_SCL
448#undef DEFAULT_LCD_PIN_SCL
449#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
450#endif
451
452#ifdef CONFIG_PANEL_LCD_PIN_SDA
453#undef DEFAULT_LCD_PIN_SDA
454#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
455#endif
456
457#ifdef CONFIG_PANEL_LCD_PIN_BL
458#undef DEFAULT_LCD_PIN_BL
459#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
460#endif
461
Willy Tarreau7005b582008-11-13 17:18:59 -0800462#endif /* DEFAULT_PROFILE == 0 */
463
464/* global variables */
Mariusz Gorskif4757af2014-11-04 22:47:19 +0100465
466/* Device single-open policy control */
467static atomic_t lcd_available = ATOMIC_INIT(1);
468static atomic_t keypad_available = ATOMIC_INIT(1);
469
Willy Tarreau698b1512008-11-22 11:29:33 +0100470static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800471
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100472static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800473
Monam Agarwal68d386b2014-02-25 19:40:49 +0530474static void (*lcd_write_cmd)(int);
475static void (*lcd_write_data)(int);
476static void (*lcd_clear_fast)(void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800477
Willy Tarreau698b1512008-11-22 11:29:33 +0100478static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800479static struct timer_list scan_timer;
480
Willy Tarreau630231772008-11-22 12:52:18 +0100481MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100482
Mariusz Gorski59a66a22014-11-27 22:36:45 +0100483static int parport = DEFAULT_PARPORT;
Willy Tarreau698b1512008-11-22 11:29:33 +0100484module_param(parport, int, 0000);
485MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100486
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100487static int profile = DEFAULT_PROFILE;
488module_param(profile, int, 0000);
489MODULE_PARM_DESC(profile,
490 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
491 "4=16x2 nexcom; default=40x2, old kp");
492
Mariusz Gorski36277d42014-11-27 22:36:47 +0100493static int keypad_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100494module_param(keypad_type, int, 0000);
495MODULE_PARM_DESC(keypad_type,
496 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
497
Mariusz Gorski36277d42014-11-27 22:36:47 +0100498static int lcd_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100499module_param(lcd_type, int, 0000);
500MODULE_PARM_DESC(lcd_type,
Sudip Mukherjee2c20d922015-03-24 16:29:32 +0530501 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100502
Mariusz Gorski36277d42014-11-27 22:36:47 +0100503static int lcd_height = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100504module_param(lcd_height, int, 0000);
505MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100506
Mariusz Gorski36277d42014-11-27 22:36:47 +0100507static int lcd_width = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100508module_param(lcd_width, int, 0000);
509MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100510
Mariusz Gorski36277d42014-11-27 22:36:47 +0100511static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100512module_param(lcd_bwidth, int, 0000);
513MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100514
Mariusz Gorski36277d42014-11-27 22:36:47 +0100515static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100516module_param(lcd_hwidth, int, 0000);
517MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100518
Mariusz Gorski36277d42014-11-27 22:36:47 +0100519static int lcd_charset = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100520module_param(lcd_charset, int, 0000);
521MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100522
Mariusz Gorski36277d42014-11-27 22:36:47 +0100523static int lcd_proto = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100524module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300525MODULE_PARM_DESC(lcd_proto,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +0200526 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100527
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100528/*
529 * These are the parallel port pins the LCD control signals are connected to.
530 * Set this to 0 if the signal is not used. Set it to its opposite value
531 * (negative) if the signal is negated. -MAXINT is used to indicate that the
532 * pin has not been explicitly specified.
533 *
Willy Tarreau630231772008-11-22 12:52:18 +0100534 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100535 */
536
537static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100538module_param(lcd_e_pin, int, 0000);
539MODULE_PARM_DESC(lcd_e_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530540 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100541
542static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100543module_param(lcd_rs_pin, int, 0000);
544MODULE_PARM_DESC(lcd_rs_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530545 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100546
547static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100548module_param(lcd_rw_pin, int, 0000);
549MODULE_PARM_DESC(lcd_rw_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530550 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100551
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100552static int lcd_cl_pin = PIN_NOT_SET;
553module_param(lcd_cl_pin, int, 0000);
554MODULE_PARM_DESC(lcd_cl_pin,
555 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100556
557static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100558module_param(lcd_da_pin, int, 0000);
559MODULE_PARM_DESC(lcd_da_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530560 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100561
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100562static int lcd_bl_pin = PIN_NOT_SET;
563module_param(lcd_bl_pin, int, 0000);
564MODULE_PARM_DESC(lcd_bl_pin,
565 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
566
567/* Deprecated module parameters - consider not using them anymore */
568
Mariusz Gorski36277d42014-11-27 22:36:47 +0100569static int lcd_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100570module_param(lcd_enabled, int, 0000);
571MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
572
Mariusz Gorski36277d42014-11-27 22:36:47 +0100573static int keypad_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100574module_param(keypad_enabled, int, 0000);
575MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
576
Peter Huewe36d20412013-02-15 13:47:05 +0100577static const unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800578
579/* for some LCD drivers (ks0074) we need a charset conversion table. */
Peter Huewe36d20412013-02-15 13:47:05 +0100580static const unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100581 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
582 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
583 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
584 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
585 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
586 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
587 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
588 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
589 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
590 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
591 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
592 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
593 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
594 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
595 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
596 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
597 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
598 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
599 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
600 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
601 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
602 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
603 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
604 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
605 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
606 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
607 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
608 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
609 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
610 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
611 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
612 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
613 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800614};
615
Peter Huewe36d20412013-02-15 13:47:05 +0100616static const char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100617 {"S0", "Left\n", "Left\n", ""},
618 {"S1", "Down\n", "Down\n", ""},
619 {"S2", "Up\n", "Up\n", ""},
620 {"S3", "Right\n", "Right\n", ""},
621 {"S4", "Esc\n", "Esc\n", ""},
622 {"S5", "Ret\n", "Ret\n", ""},
623 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800624};
625
626/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100627static const char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100628 {"S0", "Left\n", "Left\n", ""},
629 {"S1", "Down\n", "Down\n", ""},
630 {"S2", "Up\n", "Up\n", ""},
631 {"S3", "Right\n", "Right\n", ""},
632 {"S4s5", "", "Esc\n", "Esc\n"},
633 {"s4S5", "", "Ret\n", "Ret\n"},
634 {"S4S5", "Help\n", "", ""},
635 /* add new signals above this line */
636 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800637};
638
639/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100640static const char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100641 {"a-p-e-", "Down\n", "Down\n", ""},
642 {"a-p-E-", "Ret\n", "Ret\n", ""},
643 {"a-P-E-", "Esc\n", "Esc\n", ""},
644 {"a-P-e-", "Up\n", "Up\n", ""},
645 /* add new signals above this line */
646 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800647};
648
Peter Huewe36d20412013-02-15 13:47:05 +0100649static const char (*keypad_profile)[4][9] = old_keypad_profile;
Willy Tarreau7005b582008-11-13 17:18:59 -0800650
Daniel Chromikbea74332016-02-11 13:29:23 +0100651static DECLARE_BITMAP(bits, LCD_BITS);
652
653static void lcd_get_bits(unsigned int port, int *val)
654{
655 unsigned int bit, state;
656
657 for (bit = 0; bit < LCD_BITS; bit++) {
658 state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
659 *val &= lcd_bits[port][bit][BIT_MSK];
660 *val |= lcd_bits[port][bit][state];
661 }
662}
Willy Tarreau7005b582008-11-13 17:18:59 -0800663
664static void init_scan_timer(void);
665
666/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100667static int set_data_bits(void)
668{
Daniel Chromikbea74332016-02-11 13:29:23 +0100669 int val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800670
Willy Tarreau698b1512008-11-22 11:29:33 +0100671 val = r_dtr(pprt);
Daniel Chromikbea74332016-02-11 13:29:23 +0100672 lcd_get_bits(LCD_PORT_D, &val);
Willy Tarreau698b1512008-11-22 11:29:33 +0100673 w_dtr(pprt, val);
674 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800675}
676
677/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100678static int set_ctrl_bits(void)
679{
Daniel Chromikbea74332016-02-11 13:29:23 +0100680 int val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800681
Willy Tarreau698b1512008-11-22 11:29:33 +0100682 val = r_ctr(pprt);
Daniel Chromikbea74332016-02-11 13:29:23 +0100683 lcd_get_bits(LCD_PORT_C, &val);
Willy Tarreau698b1512008-11-22 11:29:33 +0100684 w_ctr(pprt, val);
685 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800686}
687
688/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530689static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100690{
691 set_data_bits();
692 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800693}
694
695/*
696 * Converts a parallel port pin (from -25 to 25) to data and control ports
697 * masks, and data and control port bits. The signal will be considered
698 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
699 *
700 * Result will be used this way :
701 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
702 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
703 */
Peter Huewe36d20412013-02-15 13:47:05 +0100704static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
Willy Tarreau698b1512008-11-22 11:29:33 +0100705{
706 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800707
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200708 d_val[0] = 0;
709 c_val[0] = 0;
710 d_val[1] = 0;
711 c_val[1] = 0;
712 d_val[2] = 0xFF;
713 c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800714
Willy Tarreau698b1512008-11-22 11:29:33 +0100715 if (pin == 0)
716 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800717
Willy Tarreau698b1512008-11-22 11:29:33 +0100718 inv = (pin < 0);
719 if (inv)
720 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800721
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200722 d_bit = 0;
723 c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800724
Willy Tarreau698b1512008-11-22 11:29:33 +0100725 switch (pin) {
726 case PIN_STROBE: /* strobe, inverted */
727 c_bit = PNL_PSTROBE;
728 inv = !inv;
729 break;
730 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
731 d_bit = 1 << (pin - 2);
732 break;
733 case PIN_AUTOLF: /* autofeed, inverted */
734 c_bit = PNL_PAUTOLF;
735 inv = !inv;
736 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300737 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100738 c_bit = PNL_PINITP;
739 break;
740 case PIN_SELECP: /* select_in, inverted */
741 c_bit = PNL_PSELECP;
742 inv = !inv;
743 break;
744 default: /* unknown pin, ignore */
745 break;
746 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800747
Willy Tarreau698b1512008-11-22 11:29:33 +0100748 if (c_bit) {
749 c_val[2] &= ~c_bit;
750 c_val[!inv] = c_bit;
751 } else if (d_bit) {
752 d_val[2] &= ~d_bit;
753 d_val[!inv] = d_bit;
754 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800755}
756
757/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100758static void long_sleep(int ms)
759{
Nicholas Mc Guire895875a2015-05-29 19:02:32 +0200760 if (in_interrupt())
Willy Tarreau698b1512008-11-22 11:29:33 +0100761 mdelay(ms);
Nicholas Mc Guire895875a2015-05-29 19:02:32 +0200762 else
763 schedule_timeout_interruptible(msecs_to_jiffies(ms));
Willy Tarreau7005b582008-11-13 17:18:59 -0800764}
765
Alex Wilson881bf282015-07-31 11:08:29 -0600766/*
767 * send a serial byte to the LCD panel. The caller is responsible for locking
768 * if needed.
769 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100770static void lcd_send_serial(int byte)
771{
772 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800773
Alex Wilson881bf282015-07-31 11:08:29 -0600774 /*
775 * the data bit is set on D0, and the clock on STROBE.
776 * LCD reads D0 on STROBE's rising edge.
777 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100778 for (bit = 0; bit < 8; bit++) {
Daniel Chromikbea74332016-02-11 13:29:23 +0100779 clear_bit(LCD_BIT_CL, bits); /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530780 panel_set_bits();
Daniel Chromikbea74332016-02-11 13:29:23 +0100781 if (byte & 1) {
782 set_bit(LCD_BIT_DA, bits);
783 } else {
784 clear_bit(LCD_BIT_DA, bits);
785 }
786
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530787 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300788 udelay(2); /* maintain the data during 2 us before CLK up */
Daniel Chromikbea74332016-02-11 13:29:23 +0100789 set_bit(LCD_BIT_CL, bits); /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530790 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300791 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100792 byte >>= 1;
793 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800794}
795
796/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100797static void lcd_backlight(int on)
798{
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100799 if (lcd.pins.bl == PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +0100800 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800801
Justin P. Mattock6975e182012-03-19 08:17:52 -0700802 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800803 spin_lock_irq(&pprt_lock);
Daniel Chromikbea74332016-02-11 13:29:23 +0100804 if (on)
805 set_bit(LCD_BIT_BL, bits);
806 else
807 clear_bit(LCD_BIT_BL, bits);
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530808 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800809 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800810}
811
812/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100813static void lcd_write_cmd_s(int cmd)
814{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800815 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100816 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
817 lcd_send_serial(cmd & 0x0F);
818 lcd_send_serial((cmd >> 4) & 0x0F);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530819 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800820 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800821}
822
823/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100824static void lcd_write_data_s(int data)
825{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800826 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100827 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
828 lcd_send_serial(data & 0x0F);
829 lcd_send_serial((data >> 4) & 0x0F);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530830 udelay(40); /* the shortest data takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800831 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800832}
833
834/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100835static void lcd_write_cmd_p8(int cmd)
836{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800837 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800838 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100839 w_dtr(pprt, cmd);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530840 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800841
Daniel Chromikbea74332016-02-11 13:29:23 +0100842 set_bit(LCD_BIT_E, bits);
843 clear_bit(LCD_BIT_RS, bits);
844 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau7005b582008-11-13 17:18:59 -0800845 set_ctrl_bits();
846
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530847 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800848
Daniel Chromikbea74332016-02-11 13:29:23 +0100849 clear_bit(LCD_BIT_E, bits);
Willy Tarreau7005b582008-11-13 17:18:59 -0800850 set_ctrl_bits();
851
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530852 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800853 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100854}
Willy Tarreau7005b582008-11-13 17:18:59 -0800855
Willy Tarreau698b1512008-11-22 11:29:33 +0100856/* send data to the LCD panel in 8 bits parallel mode */
857static void lcd_write_data_p8(int data)
858{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800859 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100860 /* present the data to the data port */
861 w_dtr(pprt, data);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530862 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100863
Daniel Chromikbea74332016-02-11 13:29:23 +0100864 set_bit(LCD_BIT_E, bits);
865 set_bit(LCD_BIT_RS, bits);
866 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100867 set_ctrl_bits();
868
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530869 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100870
Daniel Chromikbea74332016-02-11 13:29:23 +0100871 clear_bit(LCD_BIT_E, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100872 set_ctrl_bits();
873
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530874 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800875 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100876}
877
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400878/* send a command to the TI LCD panel */
879static void lcd_write_cmd_tilcd(int cmd)
880{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800881 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400882 /* present the data to the control port */
883 w_ctr(pprt, cmd);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530884 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800885 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400886}
887
888/* send data to the TI LCD panel */
889static void lcd_write_data_tilcd(int data)
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 data port */
893 w_dtr(pprt, data);
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530894 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800895 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400896}
897
Willy Tarreau698b1512008-11-22 11:29:33 +0100898static void lcd_gotoxy(void)
899{
Mariusz Gorski21149242014-12-05 22:28:19 +0100900 lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100901 | (lcd.addr.y ? lcd.hwidth : 0)
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +0530902 /*
903 * we force the cursor to stay at the end of the
904 * line if it wants to go farther
905 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100906 | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100907 (lcd.hwidth - 1) : lcd.bwidth - 1));
Willy Tarreau698b1512008-11-22 11:29:33 +0100908}
909
910static void lcd_print(char c)
911{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100912 if (lcd.addr.x < lcd.bwidth) {
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +0300913 if (lcd_char_conv)
Willy Tarreau698b1512008-11-22 11:29:33 +0100914 c = lcd_char_conv[(unsigned char)c];
915 lcd_write_data(c);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100916 lcd.addr.x++;
Willy Tarreau698b1512008-11-22 11:29:33 +0100917 }
918 /* prevents the cursor from wrapping onto the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100919 if (lcd.addr.x == lcd.bwidth)
Willy Tarreau698b1512008-11-22 11:29:33 +0100920 lcd_gotoxy();
921}
922
923/* fills the display with spaces and resets X/Y */
924static void lcd_clear_fast_s(void)
925{
926 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200927
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100928 lcd.addr.x = 0;
929 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100930 lcd_gotoxy();
931
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800932 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100933 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100934 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
935 lcd_send_serial(' ' & 0x0F);
936 lcd_send_serial((' ' >> 4) & 0x0F);
Ricardo Ruedasdf44f152015-12-08 22:30:40 +0100937 /* the shortest data takes at least 40 us */
Greg Kroah-Hartman4cff7ad2016-02-01 12:50:26 -0800938 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100939 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800940 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100941
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100942 lcd.addr.x = 0;
943 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100944 lcd_gotoxy();
945}
946
947/* fills the display with spaces and resets X/Y */
948static void lcd_clear_fast_p8(void)
949{
950 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200951
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100952 lcd.addr.x = 0;
953 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100954 lcd_gotoxy();
955
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800956 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100957 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100958 /* present the data to the data port */
959 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300960
961 /* maintain the data during 20 us before the strobe */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530962 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100963
Daniel Chromikbea74332016-02-11 13:29:23 +0100964 set_bit(LCD_BIT_E, bits);
965 set_bit(LCD_BIT_RS, bits);
966 clear_bit(LCD_BIT_RW, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100967 set_ctrl_bits();
968
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300969 /* maintain the strobe during 40 us */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530970 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100971
Daniel Chromikbea74332016-02-11 13:29:23 +0100972 clear_bit(LCD_BIT_E, bits);
Willy Tarreau698b1512008-11-22 11:29:33 +0100973 set_ctrl_bits();
974
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300975 /* the shortest data takes at least 45 us */
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530976 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100977 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800978 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100979
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100980 lcd.addr.x = 0;
981 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100982 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800983}
984
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400985/* fills the display with spaces and resets X/Y */
986static void lcd_clear_fast_tilcd(void)
987{
988 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200989
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100990 lcd.addr.x = 0;
991 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400992 lcd_gotoxy();
993
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800994 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100995 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400996 /* present the data to the data port */
997 w_dtr(pprt, ' ');
Sudip Mukherjeeb64a1cb2016-01-23 14:49:20 +0530998 udelay(60);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400999 }
1000
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001001 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001002
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001003 lcd.addr.x = 0;
1004 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001005 lcd_gotoxy();
1006}
1007
Willy Tarreau7005b582008-11-13 17:18:59 -08001008/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +01001009static void lcd_clear_display(void)
1010{
Mariusz Gorski21149242014-12-05 22:28:19 +01001011 lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001012 lcd.addr.x = 0;
1013 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001014 /* we must wait a few milliseconds (15) */
1015 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -08001016}
1017
Willy Tarreau698b1512008-11-22 11:29:33 +01001018static void lcd_init_display(void)
1019{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001020 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001021 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -08001022
Willy Tarreau698b1512008-11-22 11:29:33 +01001023 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -08001024
Mariusz Gorski21149242014-12-05 22:28:19 +01001025 /* 8bits, 1 line, small fonts; let's do it 3 times */
1026 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001027 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001028 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001029 long_sleep(10);
Mariusz Gorski21149242014-12-05 22:28:19 +01001030 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
Willy Tarreau698b1512008-11-22 11:29:33 +01001031 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001032
Mariusz Gorski21149242014-12-05 22:28:19 +01001033 /* set font height and lines number */
1034 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
1035 | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
1036 | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001037 );
1038 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001039
Mariusz Gorski21149242014-12-05 22:28:19 +01001040 /* display off, cursor off, blink off */
1041 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
Willy Tarreau698b1512008-11-22 11:29:33 +01001042 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001043
Mariusz Gorski21149242014-12-05 22:28:19 +01001044 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
1045 | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
1046 | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
1047 | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001048 );
Willy Tarreau7005b582008-11-13 17:18:59 -08001049
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001050 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08001051
Willy Tarreau698b1512008-11-22 11:29:33 +01001052 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001053
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001054 /* entry mode set : increment, cursor shifting */
Mariusz Gorski21149242014-12-05 22:28:19 +01001055 lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
Willy Tarreau7005b582008-11-13 17:18:59 -08001056
Willy Tarreau698b1512008-11-22 11:29:33 +01001057 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001058}
1059
1060/*
1061 * These are the file operation function for user access to /dev/lcd
1062 * This function can also be called from inside the kernel, by
1063 * setting file and ppos to NULL.
1064 *
1065 */
1066
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001067static inline int handle_lcd_special_code(void)
1068{
1069 /* LCD special codes */
1070
1071 int processed = 0;
1072
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001073 char *esc = lcd.esc_seq.buf + 2;
1074 int oldflags = lcd.flags;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001075
1076 /* check for display mode flags */
1077 switch (*esc) {
1078 case 'D': /* Display ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001079 lcd.flags |= LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001080 processed = 1;
1081 break;
1082 case 'd': /* Display OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001083 lcd.flags &= ~LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001084 processed = 1;
1085 break;
1086 case 'C': /* Cursor ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001087 lcd.flags |= LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001088 processed = 1;
1089 break;
1090 case 'c': /* Cursor OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001091 lcd.flags &= ~LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001092 processed = 1;
1093 break;
1094 case 'B': /* Blink ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001095 lcd.flags |= LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001096 processed = 1;
1097 break;
1098 case 'b': /* Blink OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001099 lcd.flags &= ~LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001100 processed = 1;
1101 break;
1102 case '+': /* Back light ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001103 lcd.flags |= LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001104 processed = 1;
1105 break;
1106 case '-': /* Back light OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001107 lcd.flags &= ~LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001108 processed = 1;
1109 break;
1110 case '*':
1111 /* flash back light using the keypad timer */
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001112 if (scan_timer.function) {
Sirnam Swetha832bf282015-10-27 14:56:13 +05301113 if (lcd.light_tempo == 0 &&
1114 ((lcd.flags & LCD_FLAG_L) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001115 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001116 lcd.light_tempo = FLASH_LIGHT_TEMPO;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001117 }
1118 processed = 1;
1119 break;
1120 case 'f': /* Small Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001121 lcd.flags &= ~LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001122 processed = 1;
1123 break;
1124 case 'F': /* Large Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001125 lcd.flags |= LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001126 processed = 1;
1127 break;
1128 case 'n': /* One Line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001129 lcd.flags &= ~LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001130 processed = 1;
1131 break;
1132 case 'N': /* Two Lines */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001133 lcd.flags |= LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001134 break;
1135 case 'l': /* Shift Cursor Left */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001136 if (lcd.addr.x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001137 /* back one char if not at end of line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001138 if (lcd.addr.x < lcd.bwidth)
Mariusz Gorski21149242014-12-05 22:28:19 +01001139 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001140 lcd.addr.x--;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001141 }
1142 processed = 1;
1143 break;
1144 case 'r': /* shift cursor right */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001145 if (lcd.addr.x < lcd.width) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001146 /* allow the cursor to pass the end of the line */
Mariusz Gorski21149242014-12-05 22:28:19 +01001147 if (lcd.addr.x < (lcd.bwidth - 1))
1148 lcd_write_cmd(LCD_CMD_SHIFT |
1149 LCD_CMD_SHIFT_RIGHT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001150 lcd.addr.x++;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001151 }
1152 processed = 1;
1153 break;
1154 case 'L': /* shift display left */
Mariusz Gorski21149242014-12-05 22:28:19 +01001155 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001156 processed = 1;
1157 break;
1158 case 'R': /* shift display right */
Mariusz Gorski21149242014-12-05 22:28:19 +01001159 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
1160 LCD_CMD_SHIFT_RIGHT);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001161 processed = 1;
1162 break;
1163 case 'k': { /* kill end of line */
1164 int x;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001165
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001166 for (x = lcd.addr.x; x < lcd.bwidth; x++)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001167 lcd_write_data(' ');
1168
1169 /* restore cursor position */
1170 lcd_gotoxy();
1171 processed = 1;
1172 break;
1173 }
1174 case 'I': /* reinitialize display */
1175 lcd_init_display();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001176 processed = 1;
1177 break;
1178 case 'G': {
1179 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1180 * and '7', representing the numerical ASCII code of the
1181 * redefined character, and <xx...xx> a sequence of 16
1182 * hex digits representing 8 bytes for each character.
1183 * Most LCDs will only use 5 lower bits of the 7 first
1184 * bytes.
1185 */
1186
1187 unsigned char cgbytes[8];
1188 unsigned char cgaddr;
1189 int cgoffset;
1190 int shift;
1191 char value;
1192 int addr;
1193
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001194 if (!strchr(esc, ';'))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001195 break;
1196
1197 esc++;
1198
1199 cgaddr = *(esc++) - '0';
1200 if (cgaddr > 7) {
1201 processed = 1;
1202 break;
1203 }
1204
1205 cgoffset = 0;
1206 shift = 0;
1207 value = 0;
1208 while (*esc && cgoffset < 8) {
1209 shift ^= 4;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001210 if (*esc >= '0' && *esc <= '9') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001211 value |= (*esc - '0') << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001212 } else if (*esc >= 'A' && *esc <= 'Z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001213 value |= (*esc - 'A' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001214 } else if (*esc >= 'a' && *esc <= 'z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001215 value |= (*esc - 'a' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001216 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001217 esc++;
1218 continue;
1219 }
1220
1221 if (shift == 0) {
1222 cgbytes[cgoffset++] = value;
1223 value = 0;
1224 }
1225
1226 esc++;
1227 }
1228
Mariusz Gorski21149242014-12-05 22:28:19 +01001229 lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001230 for (addr = 0; addr < cgoffset; addr++)
1231 lcd_write_data(cgbytes[addr]);
1232
1233 /* ensures that we stop writing to CGRAM */
1234 lcd_gotoxy();
1235 processed = 1;
1236 break;
1237 }
1238 case 'x': /* gotoxy : LxXXX[yYYY]; */
1239 case 'y': /* gotoxy : LyYYY[xXXX]; */
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001240 if (!strchr(esc, ';'))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001241 break;
1242
1243 while (*esc) {
1244 if (*esc == 'x') {
1245 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001246 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001247 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001248 } else if (*esc == 'y') {
1249 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001250 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001251 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001252 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001253 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001254 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001255 }
1256
1257 lcd_gotoxy();
1258 processed = 1;
1259 break;
1260 }
1261
Mariusz Gorski21149242014-12-05 22:28:19 +01001262 /* TODO: This indent party here got ugly, clean it! */
Adam Buchbinderf2635892012-09-19 21:51:07 -04001263 /* Check whether one flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001264 if (oldflags != lcd.flags) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001265 /* check whether one of B,C,D flags were changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001266 if ((oldflags ^ lcd.flags) &
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001267 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1268 /* set display mode */
Mariusz Gorski21149242014-12-05 22:28:19 +01001269 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
1270 | ((lcd.flags & LCD_FLAG_D)
1271 ? LCD_CMD_DISPLAY_ON : 0)
1272 | ((lcd.flags & LCD_FLAG_C)
1273 ? LCD_CMD_CURSOR_ON : 0)
1274 | ((lcd.flags & LCD_FLAG_B)
1275 ? LCD_CMD_BLINK_ON : 0));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001276 /* check whether one of F,N flags was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001277 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
Mariusz Gorski21149242014-12-05 22:28:19 +01001278 lcd_write_cmd(LCD_CMD_FUNCTION_SET
1279 | LCD_CMD_DATA_LEN_8BITS
1280 | ((lcd.flags & LCD_FLAG_F)
1281 ? LCD_CMD_TWO_LINES : 0)
1282 | ((lcd.flags & LCD_FLAG_N)
1283 ? LCD_CMD_FONT_5X10_DOTS
1284 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001285 /* check whether L flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001286 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1287 if (lcd.flags & (LCD_FLAG_L))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001288 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001289 else if (lcd.light_tempo == 0)
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301290 /*
1291 * switch off the light only when the tempo
1292 * lighting is gone
1293 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001294 lcd_backlight(0);
1295 }
1296 }
1297
1298 return processed;
1299}
1300
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001301static void lcd_write_char(char c)
Willy Tarreau698b1512008-11-22 11:29:33 +01001302{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001303 /* first, we'll test if we're in escape mode */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001304 if ((c != '\n') && lcd.esc_seq.len >= 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001305 /* yes, let's add this char to the buffer */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001306 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1307 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001308 } else {
1309 /* aborts any previous escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001310 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001311
1312 switch (c) {
1313 case LCD_ESCAPE_CHAR:
1314 /* start of an escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001315 lcd.esc_seq.len = 0;
1316 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001317 break;
1318 case '\b':
1319 /* go back one char and clear it */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001320 if (lcd.addr.x > 0) {
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301321 /*
1322 * check if we're not at the
1323 * end of the line
1324 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001325 if (lcd.addr.x < lcd.bwidth)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001326 /* back one char */
Mariusz Gorski21149242014-12-05 22:28:19 +01001327 lcd_write_cmd(LCD_CMD_SHIFT);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001328 lcd.addr.x--;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001329 }
1330 /* replace with a space */
1331 lcd_write_data(' ');
1332 /* back one char again */
Mariusz Gorski21149242014-12-05 22:28:19 +01001333 lcd_write_cmd(LCD_CMD_SHIFT);
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001334 break;
1335 case '\014':
1336 /* quickly clear the display */
1337 lcd_clear_fast();
1338 break;
1339 case '\n':
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301340 /*
1341 * flush the remainder of the current line and
1342 * go to the beginning of the next line
1343 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001344 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001345 lcd_write_data(' ');
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001346 lcd.addr.x = 0;
1347 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001348 lcd_gotoxy();
1349 break;
1350 case '\r':
1351 /* go to the beginning of the same line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001352 lcd.addr.x = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001353 lcd_gotoxy();
1354 break;
1355 case '\t':
1356 /* print a space instead of the tab */
1357 lcd_print(' ');
1358 break;
1359 default:
1360 /* simply print this char */
1361 lcd_print(c);
1362 break;
1363 }
1364 }
1365
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301366 /*
1367 * now we'll see if we're in an escape mode and if the current
1368 * escape sequence can be understood.
1369 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001370 if (lcd.esc_seq.len >= 2) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001371 int processed = 0;
1372
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001373 if (!strcmp(lcd.esc_seq.buf, "[2J")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001374 /* clear the display */
1375 lcd_clear_fast();
1376 processed = 1;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001377 } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001378 /* cursor to home */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001379 lcd.addr.x = 0;
1380 lcd.addr.y = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001381 lcd_gotoxy();
1382 processed = 1;
1383 }
1384 /* codes starting with ^[[L */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001385 else if ((lcd.esc_seq.len >= 3) &&
1386 (lcd.esc_seq.buf[0] == '[') &&
1387 (lcd.esc_seq.buf[1] == 'L')) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001388 processed = handle_lcd_special_code();
1389 }
1390
1391 /* LCD special escape codes */
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301392 /*
1393 * flush the escape sequence if it's been processed
1394 * or if it is getting too long.
1395 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001396 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1397 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001398 } /* escape codes */
1399}
1400
1401static ssize_t lcd_write(struct file *file,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001402 const char __user *buf, size_t count, loff_t *ppos)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001403{
1404 const char __user *tmp = buf;
Willy Tarreau698b1512008-11-22 11:29:33 +01001405 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001406
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001407 for (; count-- > 0; (*ppos)++, tmp++) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001408 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301409 /*
1410 * let's be a little nice with other processes
1411 * that need some CPU
1412 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001413 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001414
Bastien Armand6a4193a2014-04-23 19:42:11 +02001415 if (get_user(c, tmp))
Willy Tarreau698b1512008-11-22 11:29:33 +01001416 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001417
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001418 lcd_write_char(c);
Willy Tarreau7005b582008-11-13 17:18:59 -08001419 }
1420
Willy Tarreau698b1512008-11-22 11:29:33 +01001421 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001422}
1423
Willy Tarreau698b1512008-11-22 11:29:33 +01001424static int lcd_open(struct inode *inode, struct file *file)
1425{
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001426 int ret;
Willy Tarreau7005b582008-11-13 17:18:59 -08001427
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001428 ret = -EBUSY;
1429 if (!atomic_dec_and_test(&lcd_available))
1430 goto fail; /* open only once at a time */
1431
1432 ret = -EPERM;
Willy Tarreau698b1512008-11-22 11:29:33 +01001433 if (file->f_mode & FMODE_READ) /* device is write-only */
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001434 goto fail;
Willy Tarreau7005b582008-11-13 17:18:59 -08001435
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001436 if (lcd.must_clear) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001437 lcd_clear_display();
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001438 lcd.must_clear = false;
Willy Tarreau698b1512008-11-22 11:29:33 +01001439 }
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001440 return nonseekable_open(inode, file);
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001441
1442 fail:
1443 atomic_inc(&lcd_available);
1444 return ret;
Willy Tarreau7005b582008-11-13 17:18:59 -08001445}
1446
Willy Tarreau698b1512008-11-22 11:29:33 +01001447static int lcd_release(struct inode *inode, struct file *file)
1448{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001449 atomic_inc(&lcd_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001450 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001451}
1452
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001453static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001454 .write = lcd_write,
1455 .open = lcd_open,
1456 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001457 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001458};
1459
1460static struct miscdevice lcd_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001461 .minor = LCD_MINOR,
1462 .name = "lcd",
1463 .fops = &lcd_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001464};
1465
Willy Tarreau7005b582008-11-13 17:18:59 -08001466/* public function usable from the kernel for any purpose */
Peter Huewe36d20412013-02-15 13:47:05 +01001467static void panel_lcd_print(const char *s)
Willy Tarreau698b1512008-11-22 11:29:33 +01001468{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001469 const char *tmp = s;
1470 int count = strlen(s);
1471
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001472 if (lcd.enabled && lcd.initialized) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001473 for (; count-- > 0; tmp++) {
1474 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301475 /*
1476 * let's be a little nice with other processes
1477 * that need some CPU
1478 */
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001479 schedule();
1480
1481 lcd_write_char(*tmp);
1482 }
1483 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001484}
1485
Willy Tarreau7005b582008-11-13 17:18:59 -08001486/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +01001487static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001488{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001489 switch (selected_lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001490 case LCD_TYPE_OLD:
1491 /* parallel mode, 8 bits */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001492 lcd.proto = LCD_PROTO_PARALLEL;
1493 lcd.charset = LCD_CHARSET_NORMAL;
1494 lcd.pins.e = PIN_STROBE;
1495 lcd.pins.rs = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001496
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001497 lcd.width = 40;
1498 lcd.bwidth = 40;
1499 lcd.hwidth = 64;
1500 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001501 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001502 case LCD_TYPE_KS0074:
1503 /* serial mode, ks0074 */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001504 lcd.proto = LCD_PROTO_SERIAL;
1505 lcd.charset = LCD_CHARSET_KS0074;
1506 lcd.pins.bl = PIN_AUTOLF;
1507 lcd.pins.cl = PIN_STROBE;
1508 lcd.pins.da = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001509
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001510 lcd.width = 16;
1511 lcd.bwidth = 40;
1512 lcd.hwidth = 16;
1513 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001514 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001515 case LCD_TYPE_NEXCOM:
1516 /* parallel mode, 8 bits, generic */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001517 lcd.proto = LCD_PROTO_PARALLEL;
1518 lcd.charset = LCD_CHARSET_NORMAL;
1519 lcd.pins.e = PIN_AUTOLF;
1520 lcd.pins.rs = PIN_SELECP;
1521 lcd.pins.rw = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001522
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001523 lcd.width = 16;
1524 lcd.bwidth = 40;
1525 lcd.hwidth = 64;
1526 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001527 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001528 case LCD_TYPE_CUSTOM:
1529 /* customer-defined */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001530 lcd.proto = DEFAULT_LCD_PROTO;
1531 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001532 /* default geometry will be set later */
1533 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001534 case LCD_TYPE_HANTRONIX:
1535 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001536 default:
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001537 lcd.proto = LCD_PROTO_PARALLEL;
1538 lcd.charset = LCD_CHARSET_NORMAL;
1539 lcd.pins.e = PIN_STROBE;
1540 lcd.pins.rs = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001541
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001542 lcd.width = 16;
1543 lcd.bwidth = 40;
1544 lcd.hwidth = 64;
1545 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001546 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001547 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001548
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001549 /* Overwrite with module params set on loading */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001550 if (lcd_height != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001551 lcd.height = lcd_height;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001552 if (lcd_width != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001553 lcd.width = lcd_width;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001554 if (lcd_bwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001555 lcd.bwidth = lcd_bwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001556 if (lcd_hwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001557 lcd.hwidth = lcd_hwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001558 if (lcd_charset != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001559 lcd.charset = lcd_charset;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001560 if (lcd_proto != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001561 lcd.proto = lcd_proto;
1562 if (lcd_e_pin != PIN_NOT_SET)
1563 lcd.pins.e = lcd_e_pin;
1564 if (lcd_rs_pin != PIN_NOT_SET)
1565 lcd.pins.rs = lcd_rs_pin;
1566 if (lcd_rw_pin != PIN_NOT_SET)
1567 lcd.pins.rw = lcd_rw_pin;
1568 if (lcd_cl_pin != PIN_NOT_SET)
1569 lcd.pins.cl = lcd_cl_pin;
1570 if (lcd_da_pin != PIN_NOT_SET)
1571 lcd.pins.da = lcd_da_pin;
1572 if (lcd_bl_pin != PIN_NOT_SET)
1573 lcd.pins.bl = lcd_bl_pin;
Willy Tarreau7005b582008-11-13 17:18:59 -08001574
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001575 /* this is used to catch wrong and default values */
1576 if (lcd.width <= 0)
1577 lcd.width = DEFAULT_LCD_WIDTH;
1578 if (lcd.bwidth <= 0)
1579 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1580 if (lcd.hwidth <= 0)
1581 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1582 if (lcd.height <= 0)
1583 lcd.height = DEFAULT_LCD_HEIGHT;
1584
1585 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001586 lcd_write_cmd = lcd_write_cmd_s;
1587 lcd_write_data = lcd_write_data_s;
1588 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001589
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001590 if (lcd.pins.cl == PIN_NOT_SET)
1591 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1592 if (lcd.pins.da == PIN_NOT_SET)
1593 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001594
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001595 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001596 lcd_write_cmd = lcd_write_cmd_p8;
1597 lcd_write_data = lcd_write_data_p8;
1598 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001599
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001600 if (lcd.pins.e == PIN_NOT_SET)
1601 lcd.pins.e = DEFAULT_LCD_PIN_E;
1602 if (lcd.pins.rs == PIN_NOT_SET)
1603 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1604 if (lcd.pins.rw == PIN_NOT_SET)
1605 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001606 } else {
1607 lcd_write_cmd = lcd_write_cmd_tilcd;
1608 lcd_write_data = lcd_write_data_tilcd;
1609 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001610 }
1611
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001612 if (lcd.pins.bl == PIN_NOT_SET)
1613 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
Willy Tarreau698b1512008-11-22 11:29:33 +01001614
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001615 if (lcd.pins.e == PIN_NOT_SET)
1616 lcd.pins.e = PIN_NONE;
1617 if (lcd.pins.rs == PIN_NOT_SET)
1618 lcd.pins.rs = PIN_NONE;
1619 if (lcd.pins.rw == PIN_NOT_SET)
1620 lcd.pins.rw = PIN_NONE;
1621 if (lcd.pins.bl == PIN_NOT_SET)
1622 lcd.pins.bl = PIN_NONE;
1623 if (lcd.pins.cl == PIN_NOT_SET)
1624 lcd.pins.cl = PIN_NONE;
1625 if (lcd.pins.da == PIN_NOT_SET)
1626 lcd.pins.da = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001627
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001628 if (lcd.charset == NOT_SET)
1629 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001630
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001631 if (lcd.charset == LCD_CHARSET_KS0074)
Willy Tarreau698b1512008-11-22 11:29:33 +01001632 lcd_char_conv = lcd_char_conv_ks0074;
1633 else
1634 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001635
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001636 if (lcd.pins.bl != PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +01001637 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001638
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001639 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
Willy Tarreau698b1512008-11-22 11:29:33 +01001640 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001641 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
Willy Tarreau698b1512008-11-22 11:29:33 +01001642 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001643 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
Willy Tarreau698b1512008-11-22 11:29:33 +01001644 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001645 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001646 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001647 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001648 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001649 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
Willy Tarreau698b1512008-11-22 11:29:33 +01001650 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001651
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301652 /*
1653 * before this line, we must NOT send anything to the display.
Willy Tarreau698b1512008-11-22 11:29:33 +01001654 * Since lcd_init_display() needs to write data, we have to
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301655 * enable mark the LCD initialized just before.
1656 */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001657 lcd.initialized = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001658 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001659
Willy Tarreau698b1512008-11-22 11:29:33 +01001660 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001661#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1662#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001663 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001664#endif
1665#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001666 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1667 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001668#endif
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001669 lcd.addr.x = 0;
1670 lcd.addr.y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001671 /* clear the display on the next device opening */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001672 lcd.must_clear = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001673 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001674}
1675
Willy Tarreau7005b582008-11-13 17:18:59 -08001676/*
1677 * These are the file operation function for user access to /dev/keypad
1678 */
1679
Willy Tarreau698b1512008-11-22 11:29:33 +01001680static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001681 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001682{
Willy Tarreau698b1512008-11-22 11:29:33 +01001683 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001684 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001685
Willy Tarreau698b1512008-11-22 11:29:33 +01001686 if (keypad_buflen == 0) {
1687 if (file->f_flags & O_NONBLOCK)
1688 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001689
Arnd Bergmann310df692014-01-02 13:07:35 +01001690 if (wait_event_interruptible(keypad_read_wait,
1691 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001692 return -EINTR;
1693 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001694
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001695 for (; count-- > 0 && (keypad_buflen > 0);
1696 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001697 put_user(keypad_buffer[keypad_start], tmp);
1698 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1699 }
1700 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001701
Willy Tarreau698b1512008-11-22 11:29:33 +01001702 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001703}
1704
Willy Tarreau698b1512008-11-22 11:29:33 +01001705static int keypad_open(struct inode *inode, struct file *file)
1706{
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001707 int ret;
Willy Tarreau7005b582008-11-13 17:18:59 -08001708
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001709 ret = -EBUSY;
1710 if (!atomic_dec_and_test(&keypad_available))
1711 goto fail; /* open only once at a time */
1712
1713 ret = -EPERM;
Willy Tarreau698b1512008-11-22 11:29:33 +01001714 if (file->f_mode & FMODE_WRITE) /* device is read-only */
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001715 goto fail;
Willy Tarreau7005b582008-11-13 17:18:59 -08001716
Willy Tarreau698b1512008-11-22 11:29:33 +01001717 keypad_buflen = 0; /* flush the buffer on opening */
Willy Tarreau698b1512008-11-22 11:29:33 +01001718 return 0;
Willy Tarreaue81b96c2017-09-07 11:56:40 +02001719 fail:
1720 atomic_inc(&keypad_available);
1721 return ret;
Willy Tarreau7005b582008-11-13 17:18:59 -08001722}
1723
Willy Tarreau698b1512008-11-22 11:29:33 +01001724static int keypad_release(struct inode *inode, struct file *file)
1725{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001726 atomic_inc(&keypad_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001727 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001728}
1729
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001730static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001731 .read = keypad_read, /* read */
1732 .open = keypad_open, /* open */
1733 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001734 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001735};
1736
1737static struct miscdevice keypad_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001738 .minor = KEYPAD_MINOR,
1739 .name = "keypad",
1740 .fops = &keypad_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001741};
1742
Peter Huewe36d20412013-02-15 13:47:05 +01001743static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001744{
Willy Tarreau698b1512008-11-22 11:29:33 +01001745 /* send the key to the device only if a process is attached to it. */
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001746 if (!atomic_read(&keypad_available)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001747 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1748 keypad_buffer[(keypad_start + keypad_buflen++) %
1749 KEYPAD_BUFFER] = *string++;
1750 }
1751 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001752 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001753}
1754
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001755/* this function scans all the bits involving at least one logical signal,
1756 * and puts the results in the bitfield "phys_read" (one bit per established
1757 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001758 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001759 * Note: to debounce input signals, we will only consider as switched a signal
1760 * which is stable across 2 measures. Signals which are different between two
1761 * reads will be kept as they previously were in their logical form (phys_prev).
1762 * A signal which has just switched will have a 1 in
1763 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001764 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001765static void phys_scan_contacts(void)
1766{
1767 int bit, bitval;
1768 char oldval;
1769 char bitmask;
1770 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001771
Willy Tarreau698b1512008-11-22 11:29:33 +01001772 phys_prev = phys_curr;
1773 phys_read_prev = phys_read;
1774 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001775
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001776 /* keep track of old value, with all outputs disabled */
1777 oldval = r_dtr(pprt) | scan_mask_o;
1778 /* activate all keyboard outputs (active low) */
1779 w_dtr(pprt, oldval & ~scan_mask_o);
1780
1781 /* will have a 1 for each bit set to gnd */
1782 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1783 /* disable all matrix signals */
1784 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001785
Willy Tarreau698b1512008-11-22 11:29:33 +01001786 /* now that all outputs are cleared, the only active input bits are
1787 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001788 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001789
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001790 /* 1 for each grounded input */
1791 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1792
1793 /* grounded inputs are signals 40-44 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001794 phys_read |= (__u64)gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001795
Willy Tarreau698b1512008-11-22 11:29:33 +01001796 if (bitmask != gndmask) {
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301797 /*
1798 * since clearing the outputs changed some inputs, we know
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001799 * that some input signals are currently tied to some outputs.
1800 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001801 */
1802 for (bit = 0; bit < 8; bit++) {
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05301803 bitval = BIT(bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001804
1805 if (!(scan_mask_o & bitval)) /* skip unused bits */
1806 continue;
1807
1808 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1809 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01001810 phys_read |= (__u64)bitmask << (5 * bit);
Willy Tarreau698b1512008-11-22 11:29:33 +01001811 }
1812 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001813 }
Nayeemahmed Badebade8c178932015-08-27 21:02:46 +05301814 /*
1815 * this is easy: use old bits when they are flapping,
1816 * use new ones when stable
1817 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001818 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1819 (phys_read & ~(phys_read ^ phys_read_prev));
1820}
1821
1822static inline int input_state_high(struct logical_input *input)
1823{
1824#if 0
1825 /* FIXME:
1826 * this is an invalid test. It tries to catch
1827 * transitions from single-key to multiple-key, but
1828 * doesn't take into account the contacts polarity.
1829 * The only solution to the problem is to parse keys
1830 * from the most complex to the simplest combinations,
1831 * and mark them as 'caught' once a combination
1832 * matches, then unmatch it for all other ones.
1833 */
1834
1835 /* try to catch dangerous transitions cases :
1836 * someone adds a bit, so this signal was a false
1837 * positive resulting from a transition. We should
1838 * invalidate the signal immediately and not call the
1839 * release function.
1840 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1841 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001842 if (((phys_prev & input->mask) == input->value) &&
1843 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001844 input->state = INPUT_ST_LOW; /* invalidate */
1845 return 1;
1846 }
1847#endif
1848
1849 if ((phys_curr & input->mask) == input->value) {
1850 if ((input->type == INPUT_TYPE_STD) &&
1851 (input->high_timer == 0)) {
1852 input->high_timer++;
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001853 if (input->u.std.press_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001854 input->u.std.press_fct(input->u.std.press_data);
1855 } else if (input->type == INPUT_TYPE_KBD) {
1856 /* will turn on the light */
1857 keypressed = 1;
1858
1859 if (input->high_timer == 0) {
1860 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001861
Jake Champline6626de2013-05-04 11:21:17 -04001862 if (press_str[0]) {
1863 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001864
Jake Champline6626de2013-05-04 11:21:17 -04001865 keypad_send_key(press_str, s);
1866 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001867 }
1868
1869 if (input->u.kbd.repeat_str[0]) {
1870 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001871
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001872 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001873 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001874
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001875 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001876 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001877 }
1878 /* we will need to come back here soon */
1879 inputs_stable = 0;
1880 }
1881
1882 if (input->high_timer < 255)
1883 input->high_timer++;
1884 }
1885 return 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001886 }
Vincent Heuken083b3632014-07-10 03:34:28 -07001887
1888 /* else signal falling down. Let's fall through. */
1889 input->state = INPUT_ST_FALLING;
1890 input->fall_timer = 0;
1891
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001892 return 0;
1893}
1894
1895static inline void input_state_falling(struct logical_input *input)
1896{
1897#if 0
1898 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001899 if (((phys_prev & input->mask) == input->value) &&
1900 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001901 input->state = INPUT_ST_LOW; /* invalidate */
1902 return;
1903 }
1904#endif
1905
1906 if ((phys_curr & input->mask) == input->value) {
1907 if (input->type == INPUT_TYPE_KBD) {
1908 /* will turn on the light */
1909 keypressed = 1;
1910
1911 if (input->u.kbd.repeat_str[0]) {
1912 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001913
Jake Champline6626de2013-05-04 11:21:17 -04001914 if (input->high_timer >= KEYPAD_REP_START) {
1915 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001916
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001917 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001918 keypad_send_key(repeat_str, s);
1919 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001920 /* we will need to come back here soon */
1921 inputs_stable = 0;
1922 }
1923
1924 if (input->high_timer < 255)
1925 input->high_timer++;
1926 }
1927 input->state = INPUT_ST_HIGH;
1928 } else if (input->fall_timer >= input->fall_time) {
1929 /* call release event */
1930 if (input->type == INPUT_TYPE_STD) {
1931 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001932
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03001933 if (release_fct)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001934 release_fct(input->u.std.release_data);
1935 } else if (input->type == INPUT_TYPE_KBD) {
1936 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001937
Jake Champline6626de2013-05-04 11:21:17 -04001938 if (release_str[0]) {
1939 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001940
Jake Champline6626de2013-05-04 11:21:17 -04001941 keypad_send_key(release_str, s);
1942 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001943 }
1944
1945 input->state = INPUT_ST_LOW;
1946 } else {
1947 input->fall_timer++;
1948 inputs_stable = 0;
1949 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001950}
1951
Willy Tarreau698b1512008-11-22 11:29:33 +01001952static void panel_process_inputs(void)
1953{
1954 struct list_head *item;
1955 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001956
Willy Tarreau698b1512008-11-22 11:29:33 +01001957 keypressed = 0;
1958 inputs_stable = 1;
1959 list_for_each(item, &logical_inputs) {
1960 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001961
Willy Tarreau698b1512008-11-22 11:29:33 +01001962 switch (input->state) {
1963 case INPUT_ST_LOW:
1964 if ((phys_curr & input->mask) != input->value)
1965 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001966 /* if all needed ones were already set previously,
1967 * this means that this logical signal has been
1968 * activated by the releasing of another combined
1969 * signal, so we don't want to match.
1970 * eg: AB -(release B)-> A -(release A)-> 0 :
1971 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001972 */
1973 if ((phys_prev & input->mask) == input->value)
1974 break;
1975 input->rise_timer = 0;
1976 input->state = INPUT_ST_RISING;
1977 /* no break here, fall through */
1978 case INPUT_ST_RISING:
1979 if ((phys_curr & input->mask) != input->value) {
1980 input->state = INPUT_ST_LOW;
1981 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001982 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001983 if (input->rise_timer < input->rise_time) {
1984 inputs_stable = 0;
1985 input->rise_timer++;
1986 break;
1987 }
1988 input->high_timer = 0;
1989 input->state = INPUT_ST_HIGH;
1990 /* no break here, fall through */
1991 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001992 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001993 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001994 /* no break here, fall through */
1995 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001996 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001997 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001998 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001999}
2000
Willy Tarreau698b1512008-11-22 11:29:33 +01002001static void panel_scan_timer(void)
2002{
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002003 if (keypad.enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08002004 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002005 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002006
2007 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08002008 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01002009 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002010
Willy Tarreau698b1512008-11-22 11:29:33 +01002011 if (!inputs_stable || phys_curr != phys_prev)
2012 panel_process_inputs();
2013 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002014
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002015 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002016 if (keypressed) {
Sirnam Swetha832bf282015-10-27 14:56:13 +05302017 if (lcd.light_tempo == 0 &&
2018 ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01002019 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002020 lcd.light_tempo = FLASH_LIGHT_TEMPO;
2021 } else if (lcd.light_tempo > 0) {
2022 lcd.light_tempo--;
Sirnam Swetha832bf282015-10-27 14:56:13 +05302023 if (lcd.light_tempo == 0 &&
2024 ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01002025 lcd_backlight(0);
2026 }
2027 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002028
Willy Tarreau698b1512008-11-22 11:29:33 +01002029 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08002030}
2031
Willy Tarreau698b1512008-11-22 11:29:33 +01002032static void init_scan_timer(void)
2033{
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002034 if (scan_timer.function)
Willy Tarreau698b1512008-11-22 11:29:33 +01002035 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08002036
Aya Mahfouz8f6e36c2015-02-19 07:58:29 +02002037 setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
Willy Tarreau698b1512008-11-22 11:29:33 +01002038 scan_timer.expires = jiffies + INPUT_POLL_TIME;
Willy Tarreau698b1512008-11-22 11:29:33 +01002039 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002040}
2041
2042/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002043 * if <omask> or <imask> are non-null, they will be or'ed with the bits
2044 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08002045 * returns 1 if ok, 0 if error (in which case, nothing is written).
2046 */
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01002047static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01002048 u8 *imask, u8 *omask)
Willy Tarreau698b1512008-11-22 11:29:33 +01002049{
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002050 const char sigtab[] = "EeSsPpAaBb";
Ksenija Stanojevicd938e1e2016-01-03 20:39:49 +01002051 u8 im, om;
Ksenija Stanojevic35fe0872016-01-03 20:40:58 +01002052 __u64 m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08002053
Ksenija Stanojevicd12f27e2016-01-03 20:42:26 +01002054 om = 0;
2055 im = 0;
Dominique van den Broeck2d534262014-05-24 01:35:24 +02002056 m = 0ULL;
2057 v = 0ULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002058 while (*name) {
2059 int in, out, bit, neg;
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002060 const char *idx;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002061
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002062 idx = strchr(sigtab, *name);
2063 if (!idx)
Willy Tarreau698b1512008-11-22 11:29:33 +01002064 return 0; /* input name not found */
Ksenija Stanojevic8aa73072016-01-03 20:44:44 +01002065
2066 in = idx - sigtab;
Willy Tarreau698b1512008-11-22 11:29:33 +01002067 neg = (in & 1); /* odd (lower) names are negated */
2068 in >>= 1;
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05302069 im |= BIT(in);
Willy Tarreau7005b582008-11-13 17:18:59 -08002070
Willy Tarreau698b1512008-11-22 11:29:33 +01002071 name++;
Ksenija Stanojevic52ebf932016-01-03 20:43:27 +01002072 if (*name >= '0' && *name <= '7') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002073 out = *name - '0';
Amitoj Kaur Chawla79f2af62015-10-16 22:11:36 +05302074 om |= BIT(out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002075 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002076 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002077 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01002078 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002079 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002080
2081 bit = (out * 5) + in;
2082
2083 m |= 1ULL << bit;
2084 if (!neg)
2085 v |= 1ULL << bit;
2086 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08002087 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002088 *mask = m;
2089 *value = v;
2090 if (imask)
2091 *imask |= im;
2092 if (omask)
2093 *omask |= om;
2094 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002095}
2096
2097/* tries to bind a key to the signal name <name>. The key will send the
2098 * strings <press>, <repeat>, <release> for these respective events.
2099 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2100 */
Peter Huewe36d20412013-02-15 13:47:05 +01002101static struct logical_input *panel_bind_key(const char *name, const char *press,
2102 const char *repeat,
2103 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01002104{
2105 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002106
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002107 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002108 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01002109 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002110
Willy Tarreau698b1512008-11-22 11:29:33 +01002111 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002112 &scan_mask_o)) {
2113 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01002114 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002115 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002116
2117 key->type = INPUT_TYPE_KBD;
2118 key->state = INPUT_ST_LOW;
2119 key->rise_time = 1;
2120 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002121
Willy Tarreau698b1512008-11-22 11:29:33 +01002122 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2123 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2124 strncpy(key->u.kbd.release_str, release,
2125 sizeof(key->u.kbd.release_str));
2126 list_add(&key->list, &logical_inputs);
2127 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002128}
2129
Willy Tarreau630231772008-11-22 12:52:18 +01002130#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002131/* tries to bind a callback function to the signal name <name>. The function
2132 * <press_fct> will be called with the <press_data> arg when the signal is
2133 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002134 * Returns the pointer to the new signal if ok, NULL if the signal could not
2135 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002136 */
2137static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302138 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002139 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302140 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002141 int release_data)
2142{
2143 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002144
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002145 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002146 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002147 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002148
Willy Tarreau698b1512008-11-22 11:29:33 +01002149 memset(callback, 0, sizeof(struct logical_input));
2150 if (!input_name2mask(name, &callback->mask, &callback->value,
2151 &scan_mask_i, &scan_mask_o))
2152 return NULL;
2153
2154 callback->type = INPUT_TYPE_STD;
2155 callback->state = INPUT_ST_LOW;
2156 callback->rise_time = 1;
2157 callback->fall_time = 1;
2158 callback->u.std.press_fct = press_fct;
2159 callback->u.std.press_data = press_data;
2160 callback->u.std.release_fct = release_fct;
2161 callback->u.std.release_data = release_data;
2162 list_add(&callback->list, &logical_inputs);
2163 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002164}
Willy Tarreau630231772008-11-22 12:52:18 +01002165#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002166
Willy Tarreau698b1512008-11-22 11:29:33 +01002167static void keypad_init(void)
2168{
2169 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002170
Willy Tarreau698b1512008-11-22 11:29:33 +01002171 init_waitqueue_head(&keypad_read_wait);
2172 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002173
Willy Tarreau698b1512008-11-22 11:29:33 +01002174 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002175
Willy Tarreau698b1512008-11-22 11:29:33 +01002176 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2177 panel_bind_key(keypad_profile[keynum][0],
2178 keypad_profile[keynum][1],
2179 keypad_profile[keynum][2],
2180 keypad_profile[keynum][3]);
2181 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002182
Willy Tarreau698b1512008-11-22 11:29:33 +01002183 init_scan_timer();
2184 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002185}
2186
Willy Tarreau7005b582008-11-13 17:18:59 -08002187/**************************************************/
2188/* device initialization */
2189/**************************************************/
2190
Willy Tarreau698b1512008-11-22 11:29:33 +01002191static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2192 void *unused)
2193{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002194 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002195 switch (code) {
2196 case SYS_DOWN:
2197 panel_lcd_print
2198 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2199 break;
2200 case SYS_HALT:
2201 panel_lcd_print
2202 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2203 break;
2204 case SYS_POWER_OFF:
2205 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2206 break;
2207 default:
2208 break;
2209 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002210 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002211 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002212}
2213
2214static struct notifier_block panel_notifier = {
2215 panel_notify_sys,
2216 NULL,
2217 0
2218};
2219
Willy Tarreau698b1512008-11-22 11:29:33 +01002220static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002221{
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302222 struct pardev_cb panel_cb;
2223
Willy Tarreau698b1512008-11-22 11:29:33 +01002224 if (port->number != parport)
2225 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002226
Willy Tarreau698b1512008-11-22 11:29:33 +01002227 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002228 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2229 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002230 return;
2231 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002232
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302233 memset(&panel_cb, 0, sizeof(panel_cb));
2234 panel_cb.private = &pprt;
2235 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
2236
2237 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002238 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002239 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2240 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002241 return;
2242 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002243
Willy Tarreau698b1512008-11-22 11:29:33 +01002244 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002245 pr_err("could not claim access to parport%d. Aborting.\n",
2246 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002247 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002248 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002249
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002250 /* must init LCD first, just in case an IRQ from the keypad is
2251 * generated at keypad init
2252 */
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002253 if (lcd.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002254 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002255 if (misc_register(&lcd_dev))
2256 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002257 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002258
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002259 if (keypad.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002260 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002261 if (misc_register(&keypad_dev))
2262 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002263 }
Sudip Mukherjeebb046fe2015-03-09 20:08:23 +05302264 register_reboot_notifier(&panel_notifier);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002265 return;
2266
2267err_lcd_unreg:
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002268 if (lcd.enabled)
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002269 misc_deregister(&lcd_dev);
2270err_unreg_device:
2271 parport_unregister_device(pprt);
2272 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002273}
2274
Willy Tarreau698b1512008-11-22 11:29:33 +01002275static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002276{
Willy Tarreau698b1512008-11-22 11:29:33 +01002277 if (port->number != parport)
2278 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002279
Willy Tarreau698b1512008-11-22 11:29:33 +01002280 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002281 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2282 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002283 return;
2284 }
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002285 if (scan_timer.function)
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302286 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002287
Aybuke Ozdemirb565b3f2015-10-03 14:52:09 +03002288 if (pprt) {
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302289 if (keypad.enabled) {
2290 misc_deregister(&keypad_dev);
2291 keypad_initialized = 0;
2292 }
Sudip Mukherjeebb046fe2015-03-09 20:08:23 +05302293
Sudip Mukherjee7d98c632015-05-12 17:36:08 +05302294 if (lcd.enabled) {
2295 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2296 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2297 misc_deregister(&lcd_dev);
2298 lcd.initialized = false;
2299 }
2300
2301 /* TODO: free all input signals */
2302 parport_release(pprt);
2303 parport_unregister_device(pprt);
2304 pprt = NULL;
2305 unregister_reboot_notifier(&panel_notifier);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002306 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002307}
2308
2309static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002310 .name = "panel",
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302311 .match_port = panel_attach,
Willy Tarreau698b1512008-11-22 11:29:33 +01002312 .detach = panel_detach,
Sudip Mukherjee9be83c02015-05-20 20:56:58 +05302313 .devmodel = true,
Willy Tarreau7005b582008-11-13 17:18:59 -08002314};
2315
2316/* init function */
Mariusz Gorskid9114762014-11-27 22:36:46 +01002317static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002318{
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302319 int selected_keypad_type = NOT_SET, err;
Willy Tarreau7005b582008-11-13 17:18:59 -08002320
Willy Tarreau698b1512008-11-22 11:29:33 +01002321 /* take care of an eventual profile */
2322 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002323 case PANEL_PROFILE_CUSTOM:
2324 /* custom profile */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002325 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2326 selected_lcd_type = DEFAULT_LCD_TYPE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002327 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002328 case PANEL_PROFILE_OLD:
2329 /* 8 bits, 2*16, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002330 selected_keypad_type = KEYPAD_TYPE_OLD;
2331 selected_lcd_type = LCD_TYPE_OLD;
2332
2333 /* TODO: This two are a little hacky, sort it out later */
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002334 if (lcd_width == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002335 lcd_width = 16;
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002336 if (lcd_hwidth == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002337 lcd_hwidth = 16;
2338 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002339 case PANEL_PROFILE_NEW:
2340 /* serial, 2*16, new keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002341 selected_keypad_type = KEYPAD_TYPE_NEW;
2342 selected_lcd_type = LCD_TYPE_KS0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01002343 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002344 case PANEL_PROFILE_HANTRONIX:
2345 /* 8 bits, 2*16 hantronix-like, no keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002346 selected_keypad_type = KEYPAD_TYPE_NONE;
2347 selected_lcd_type = LCD_TYPE_HANTRONIX;
Willy Tarreau698b1512008-11-22 11:29:33 +01002348 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002349 case PANEL_PROFILE_NEXCOM:
2350 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002351 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2352 selected_lcd_type = LCD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002353 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002354 case PANEL_PROFILE_LARGE:
2355 /* 8 bits, 2*40, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002356 selected_keypad_type = KEYPAD_TYPE_OLD;
2357 selected_lcd_type = LCD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002358 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002359 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002360
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01002361 /*
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002362 * Overwrite selection with module param values (both keypad and lcd),
2363 * where the deprecated params have lower prio.
2364 */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002365 if (keypad_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002366 selected_keypad_type = keypad_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002367 if (keypad_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002368 selected_keypad_type = keypad_type;
Willy Tarreau7005b582008-11-13 17:18:59 -08002369
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002370 keypad.enabled = (selected_keypad_type > 0);
2371
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002372 if (lcd_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002373 selected_lcd_type = lcd_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002374 if (lcd_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002375 selected_lcd_type = lcd_type;
2376
2377 lcd.enabled = (selected_lcd_type > 0);
2378
Sudip Mukherjee733345e2015-02-11 16:57:08 +05302379 if (lcd.enabled) {
2380 /*
2381 * Init lcd struct with load-time values to preserve exact
2382 * current functionality (at least for now).
2383 */
2384 lcd.height = lcd_height;
2385 lcd.width = lcd_width;
2386 lcd.bwidth = lcd_bwidth;
2387 lcd.hwidth = lcd_hwidth;
2388 lcd.charset = lcd_charset;
2389 lcd.proto = lcd_proto;
2390 lcd.pins.e = lcd_e_pin;
2391 lcd.pins.rs = lcd_rs_pin;
2392 lcd.pins.rw = lcd_rw_pin;
2393 lcd.pins.cl = lcd_cl_pin;
2394 lcd.pins.da = lcd_da_pin;
2395 lcd.pins.bl = lcd_bl_pin;
2396
2397 /* Leave it for now, just in case */
2398 lcd.esc_seq.len = -1;
2399 }
2400
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002401 switch (selected_keypad_type) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002402 case KEYPAD_TYPE_OLD:
2403 keypad_profile = old_keypad_profile;
2404 break;
2405 case KEYPAD_TYPE_NEW:
2406 keypad_profile = new_keypad_profile;
2407 break;
2408 case KEYPAD_TYPE_NEXCOM:
2409 keypad_profile = nexcom_keypad_profile;
2410 break;
2411 default:
2412 keypad_profile = NULL;
2413 break;
2414 }
2415
Sudip Mukherjeef43de772015-02-10 17:26:02 +05302416 if (!lcd.enabled && !keypad.enabled) {
2417 /* no device enabled, let's exit */
2418 pr_err("driver version " PANEL_VERSION " disabled.\n");
2419 return -ENODEV;
2420 }
2421
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302422 err = parport_register_driver(&panel_driver);
2423 if (err) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002424 pr_err("could not register with parport. Aborting.\n");
Sudip Mukherjeee1342012015-03-09 20:08:24 +05302425 return err;
Willy Tarreau698b1512008-11-22 11:29:33 +01002426 }
2427
Willy Tarreau698b1512008-11-22 11:29:33 +01002428 if (pprt)
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002429 pr_info("driver version " PANEL_VERSION
2430 " registered on parport%d (io=0x%lx).\n", parport,
2431 pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002432 else
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002433 pr_info("driver version " PANEL_VERSION
2434 " not yet registered\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002435 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002436}
2437
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002438static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002439{
Willy Tarreau698b1512008-11-22 11:29:33 +01002440 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002441}
Willy Tarreau7005b582008-11-13 17:18:59 -08002442
Willy Tarreau7005b582008-11-13 17:18:59 -08002443module_init(panel_init_module);
2444module_exit(panel_cleanup_module);
2445MODULE_AUTHOR("Willy Tarreau");
2446MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002447
2448/*
2449 * Local variables:
2450 * c-indent-level: 4
2451 * tab-width: 8
2452 * End:
2453 */