blob: 98325b7b4462ab9d5daa747707ca3ed30e019259 [file] [log] [blame]
Willy Tarreau7005b582008-11-13 17:18:59 -08001/*
Willy Tarreau698b1512008-11-22 11:29:33 +01002 * Front panel driver for Linux
3 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
Willy Tarreau7005b582008-11-13 17:18:59 -08004 *
Willy Tarreau698b1512008-11-22 11:29:33 +01005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
Willy Tarreau7005b582008-11-13 17:18:59 -08009 *
Willy Tarreau698b1512008-11-22 11:29:33 +010010 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
11 * connected to a parallel printer port.
Willy Tarreau7005b582008-11-13 17:18:59 -080012 *
Willy Tarreau698b1512008-11-22 11:29:33 +010013 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
14 * serial module compatible with Samsung's KS0074. The pins may be connected in
15 * any combination, everything is programmable.
Willy Tarreau7005b582008-11-13 17:18:59 -080016 *
Willy Tarreau698b1512008-11-22 11:29:33 +010017 * The keypad consists in a matrix of push buttons connecting input pins to
18 * data output pins or to the ground. The combinations have to be hard-coded
19 * in the driver, though several profiles exist and adding new ones is easy.
Willy Tarreau7005b582008-11-13 17:18:59 -080020 *
Willy Tarreau698b1512008-11-22 11:29:33 +010021 * Several profiles are provided for commonly found LCD+keypad modules on the
22 * market, such as those found in Nexcom's appliances.
Willy Tarreau7005b582008-11-13 17:18:59 -080023 *
24 * FIXME:
25 * - the initialization/deinitialization process is very dirty and should
26 * be rewritten. It may even be buggy.
27 *
28 * TODO:
29 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
30 * - make the LCD a part of a virtual screen of Vx*Vy
31 * - make the inputs list smp-safe
32 * - change the keyboard to a double mapping : signals -> key_id -> values
33 * so that applications can change values without knowing signals
34 *
35 */
36
Toshiaki Yamane493aa892012-07-12 22:01:00 +090037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Willy Tarreau7005b582008-11-13 17:18:59 -080039#include <linux/module.h>
40
41#include <linux/types.h>
42#include <linux/errno.h>
43#include <linux/signal.h>
44#include <linux/sched.h>
45#include <linux/spinlock.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080046#include <linux/interrupt.h>
47#include <linux/miscdevice.h>
Willy Tarreau698b1512008-11-22 11:29:33 +010048#include <linux/slab.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080049#include <linux/ioport.h>
50#include <linux/fcntl.h>
51#include <linux/init.h>
52#include <linux/delay.h>
Andy Shevchenkod85170e2010-07-22 19:57:08 +030053#include <linux/kernel.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080054#include <linux/ctype.h>
55#include <linux/parport.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080056#include <linux/list.h>
57#include <linux/notifier.h>
58#include <linux/reboot.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020059#include <generated/utsrelease.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080060
Willy Tarreau698b1512008-11-22 11:29:33 +010061#include <linux/io.h>
Takanori Suzuki48f658b2010-05-08 22:56:24 +090062#include <linux/uaccess.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080063
Willy Tarreau7005b582008-11-13 17:18:59 -080064#define LCD_MINOR 156
65#define KEYPAD_MINOR 185
Willy Tarreau7005b582008-11-13 17:18:59 -080066
67#define PANEL_VERSION "0.9.5"
68
69#define LCD_MAXBYTES 256 /* max burst write */
70
Willy Tarreau7005b582008-11-13 17:18:59 -080071#define KEYPAD_BUFFER 64
Willy Tarreau7005b582008-11-13 17:18:59 -080072
Henri Häkkinen429ccf02010-06-12 00:27:36 +030073/* poll the keyboard this every second */
74#define INPUT_POLL_TIME (HZ/50)
75/* a key starts to repeat after this times INPUT_POLL_TIME */
76#define KEYPAD_REP_START (10)
77/* a key repeats this times INPUT_POLL_TIME */
78#define KEYPAD_REP_DELAY (2)
79
80/* keep the light on this times INPUT_POLL_TIME for each flash */
81#define FLASH_LIGHT_TEMPO (200)
Willy Tarreau7005b582008-11-13 17:18:59 -080082
83/* converts an r_str() input to an active high, bits string : 000BAOSE */
84#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
85
Willy Tarreau698b1512008-11-22 11:29:33 +010086#define PNL_PBUSY 0x80 /* inverted input, active low */
87#define PNL_PACK 0x40 /* direct input, active low */
88#define PNL_POUTPA 0x20 /* direct input, active high */
89#define PNL_PSELECD 0x10 /* direct input, active high */
90#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080091
Willy Tarreau698b1512008-11-22 11:29:33 +010092#define PNL_PBIDIR 0x20 /* bi-directional ports */
Henri Häkkinen429ccf02010-06-12 00:27:36 +030093/* high to read data in or-ed with data out */
94#define PNL_PINTEN 0x10
Willy Tarreau698b1512008-11-22 11:29:33 +010095#define PNL_PSELECP 0x08 /* inverted output, active low */
96#define PNL_PINITP 0x04 /* direct output, active low */
97#define PNL_PAUTOLF 0x02 /* inverted output, active low */
98#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080099
100#define PNL_PD0 0x01
101#define PNL_PD1 0x02
102#define PNL_PD2 0x04
103#define PNL_PD3 0x08
104#define PNL_PD4 0x10
105#define PNL_PD5 0x20
106#define PNL_PD6 0x40
107#define PNL_PD7 0x80
108
109#define PIN_NONE 0
110#define PIN_STROBE 1
111#define PIN_D0 2
112#define PIN_D1 3
113#define PIN_D2 4
114#define PIN_D3 5
115#define PIN_D4 6
116#define PIN_D5 7
117#define PIN_D6 8
118#define PIN_D7 9
119#define PIN_AUTOLF 14
120#define PIN_INITP 16
121#define PIN_SELECP 17
122#define PIN_NOT_SET 127
123
Willy Tarreau7005b582008-11-13 17:18:59 -0800124#define LCD_FLAG_S 0x0001
125#define LCD_FLAG_ID 0x0002
126#define LCD_FLAG_B 0x0004 /* blink on */
127#define LCD_FLAG_C 0x0008 /* cursor on */
128#define LCD_FLAG_D 0x0010 /* display on */
129#define LCD_FLAG_F 0x0020 /* large font mode */
130#define LCD_FLAG_N 0x0040 /* 2-rows mode */
131#define LCD_FLAG_L 0x0080 /* backlight enabled */
132
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300133#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
Willy Tarreau7005b582008-11-13 17:18:59 -0800134#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
135
Mariusz Gorski36277d42014-11-27 22:36:47 +0100136#define NOT_SET -1
137
Willy Tarreau7005b582008-11-13 17:18:59 -0800138/* macros to simplify use of the parallel port */
139#define r_ctr(x) (parport_read_control((x)->port))
140#define r_dtr(x) (parport_read_data((x)->port))
141#define r_str(x) (parport_read_status((x)->port))
Toshiaki Yamane6ebb56d2012-07-25 12:18:12 +0900142#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
143#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
Willy Tarreau7005b582008-11-13 17:18:59 -0800144
145/* this defines which bits are to be used and which ones to be ignored */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300146/* logical or of the output bits involved in the scan matrix */
147static __u8 scan_mask_o;
148/* logical or of the input bits involved in the scan matrix */
149static __u8 scan_mask_i;
Willy Tarreau7005b582008-11-13 17:18:59 -0800150
Willy Tarreau698b1512008-11-22 11:29:33 +0100151typedef __u64 pmask_t;
Willy Tarreau7005b582008-11-13 17:18:59 -0800152
153enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100154 INPUT_TYPE_STD,
155 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800156};
157
158enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100159 INPUT_ST_LOW,
160 INPUT_ST_RISING,
161 INPUT_ST_HIGH,
162 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800163};
164
165struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100166 struct list_head list;
167 pmask_t mask;
168 pmask_t value;
169 enum input_type type;
170 enum input_state state;
171 __u8 rise_time, fall_time;
172 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800173
Willy Tarreau698b1512008-11-22 11:29:33 +0100174 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300175 struct { /* valid when type == INPUT_TYPE_STD */
Monam Agarwal68d386b2014-02-25 19:40:49 +0530176 void (*press_fct)(int);
177 void (*release_fct)(int);
Willy Tarreau698b1512008-11-22 11:29:33 +0100178 int press_data;
179 int release_data;
180 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300181 struct { /* valid when type == INPUT_TYPE_KBD */
182 /* strings can be non null-terminated */
Willy Tarreau698b1512008-11-22 11:29:33 +0100183 char press_str[sizeof(void *) + sizeof(int)];
184 char repeat_str[sizeof(void *) + sizeof(int)];
185 char release_str[sizeof(void *) + sizeof(int)];
186 } kbd;
187 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800188};
189
Peter Huewe36d20412013-02-15 13:47:05 +0100190static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800191
192/* physical contacts history
193 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
194 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
195 * corresponds to the ground.
196 * Within each group, bits are stored in the same order as read on the port :
197 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
198 * So, each __u64 (or pmask_t) is represented like this :
199 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
200 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
201 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300202
203/* what has just been read from the I/O ports */
204static pmask_t phys_read;
205/* previous phys_read */
206static pmask_t phys_read_prev;
207/* stabilized phys_read (phys_read|phys_read_prev) */
208static pmask_t phys_curr;
209/* previous phys_curr */
210static pmask_t phys_prev;
211/* 0 means that at least one logical signal needs be computed */
212static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800213
Willy Tarreau7005b582008-11-13 17:18:59 -0800214/* these variables are specific to the keypad */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100215static struct {
216 bool enabled;
217} keypad;
218
Willy Tarreau7005b582008-11-13 17:18:59 -0800219static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100220static int keypad_buflen;
221static int keypad_start;
222static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800223static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800224
225/* lcd-specific variables */
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100226static struct {
227 bool enabled;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100228 bool initialized;
229 bool must_clear;
230
231 /* TODO: use bool here? */
232 char left_shift;
233
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100234 int height;
235 int width;
236 int bwidth;
237 int hwidth;
238 int charset;
239 int proto;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100240 int light_tempo;
241
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100242 /* TODO: use union here? */
243 struct {
244 int e;
245 int rs;
246 int rw;
247 int cl;
248 int da;
249 int bl;
250 } pins;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100251
252 /* contains the LCD config state */
253 unsigned long int flags;
254
255 /* Contains the LCD X and Y offset */
256 struct {
257 unsigned long int x;
258 unsigned long int y;
259 } addr;
260
261 /* Current escape sequence and it's length or -1 if outside */
262 struct {
263 char buf[LCD_ESCAPE_LEN + 1];
264 int len;
265 } esc_seq;
Mariusz Gorskia8b25802014-11-27 22:36:49 +0100266} lcd;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300267
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +0100268/* Needed only for init */
269static int selected_lcd_type = NOT_SET;
270
Willy Tarreau7005b582008-11-13 17:18:59 -0800271/*
272 * Bit masks to convert LCD signals to parallel port outputs.
273 * _d_ are values for data port, _c_ are for control port.
274 * [0] = signal OFF, [1] = signal ON, [2] = mask
275 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100276#define BIT_CLR 0
277#define BIT_SET 1
278#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800279#define BIT_STATES 3
280/*
281 * one entry for each bit on the LCD
282 */
283#define LCD_BIT_E 0
284#define LCD_BIT_RS 1
285#define LCD_BIT_RW 2
286#define LCD_BIT_BL 3
287#define LCD_BIT_CL 4
288#define LCD_BIT_DA 5
289#define LCD_BITS 6
290
291/*
292 * each bit can be either connected to a DATA or CTRL port
293 */
294#define LCD_PORT_C 0
295#define LCD_PORT_D 1
296#define LCD_PORTS 2
297
298static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
299
300/*
301 * LCD protocols
302 */
303#define LCD_PROTO_PARALLEL 0
304#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400305#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800306
307/*
308 * LCD character sets
309 */
310#define LCD_CHARSET_NORMAL 0
311#define LCD_CHARSET_KS0074 1
312
313/*
314 * LCD types
315 */
316#define LCD_TYPE_NONE 0
317#define LCD_TYPE_OLD 1
318#define LCD_TYPE_KS0074 2
319#define LCD_TYPE_HANTRONIX 3
320#define LCD_TYPE_NEXCOM 4
321#define LCD_TYPE_CUSTOM 5
322
323/*
324 * keypad types
325 */
326#define KEYPAD_TYPE_NONE 0
327#define KEYPAD_TYPE_OLD 1
328#define KEYPAD_TYPE_NEW 2
329#define KEYPAD_TYPE_NEXCOM 3
330
331/*
332 * panel profiles
333 */
334#define PANEL_PROFILE_CUSTOM 0
335#define PANEL_PROFILE_OLD 1
336#define PANEL_PROFILE_NEW 2
337#define PANEL_PROFILE_HANTRONIX 3
338#define PANEL_PROFILE_NEXCOM 4
339#define PANEL_PROFILE_LARGE 5
340
341/*
342 * Construct custom config from the kernel's configuration
343 */
Willy Tarreau7005b582008-11-13 17:18:59 -0800344#define DEFAULT_PARPORT 0
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100345#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100346#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
347#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100348#define DEFAULT_LCD_HEIGHT 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800349#define DEFAULT_LCD_WIDTH 40
350#define DEFAULT_LCD_BWIDTH 40
351#define DEFAULT_LCD_HWIDTH 64
Mariusz Gorskife4d7e22014-11-12 02:08:06 +0100352#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
Willy Tarreau7005b582008-11-13 17:18:59 -0800353#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
354
355#define DEFAULT_LCD_PIN_E PIN_AUTOLF
356#define DEFAULT_LCD_PIN_RS PIN_SELECP
357#define DEFAULT_LCD_PIN_RW PIN_INITP
358#define DEFAULT_LCD_PIN_SCL PIN_STROBE
359#define DEFAULT_LCD_PIN_SDA PIN_D0
360#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
Willy Tarreau7005b582008-11-13 17:18:59 -0800361
Willy Tarreau7005b582008-11-13 17:18:59 -0800362#ifdef CONFIG_PANEL_PARPORT
363#undef DEFAULT_PARPORT
364#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
365#endif
366
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100367#ifdef CONFIG_PANEL_PROFILE
368#undef DEFAULT_PROFILE
369#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
370#endif
371
Willy Tarreau698b1512008-11-22 11:29:33 +0100372#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800373#ifdef CONFIG_PANEL_KEYPAD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100374#undef DEFAULT_KEYPAD_TYPE
375#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
Willy Tarreau7005b582008-11-13 17:18:59 -0800376#endif
377
Willy Tarreau7005b582008-11-13 17:18:59 -0800378#ifdef CONFIG_PANEL_LCD
Mariusz Gorski98fac3d2014-11-12 02:08:09 +0100379#undef DEFAULT_LCD_TYPE
380#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
Willy Tarreau7005b582008-11-13 17:18:59 -0800381#endif
382
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100383#ifdef CONFIG_PANEL_LCD_HEIGHT
384#undef DEFAULT_LCD_HEIGHT
385#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
386#endif
387
Willy Tarreau7005b582008-11-13 17:18:59 -0800388#ifdef CONFIG_PANEL_LCD_WIDTH
389#undef DEFAULT_LCD_WIDTH
390#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
391#endif
392
393#ifdef CONFIG_PANEL_LCD_BWIDTH
394#undef DEFAULT_LCD_BWIDTH
395#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
396#endif
397
398#ifdef CONFIG_PANEL_LCD_HWIDTH
399#undef DEFAULT_LCD_HWIDTH
400#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
401#endif
402
Mariusz Gorski1e13e8a2014-11-12 02:08:07 +0100403#ifdef CONFIG_PANEL_LCD_CHARSET
404#undef DEFAULT_LCD_CHARSET
405#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800406#endif
407
408#ifdef CONFIG_PANEL_LCD_PROTO
409#undef DEFAULT_LCD_PROTO
410#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
411#endif
412
413#ifdef CONFIG_PANEL_LCD_PIN_E
414#undef DEFAULT_LCD_PIN_E
415#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
416#endif
417
418#ifdef CONFIG_PANEL_LCD_PIN_RS
419#undef DEFAULT_LCD_PIN_RS
420#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
421#endif
422
423#ifdef CONFIG_PANEL_LCD_PIN_RW
424#undef DEFAULT_LCD_PIN_RW
425#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
426#endif
427
428#ifdef CONFIG_PANEL_LCD_PIN_SCL
429#undef DEFAULT_LCD_PIN_SCL
430#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
431#endif
432
433#ifdef CONFIG_PANEL_LCD_PIN_SDA
434#undef DEFAULT_LCD_PIN_SDA
435#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
436#endif
437
438#ifdef CONFIG_PANEL_LCD_PIN_BL
439#undef DEFAULT_LCD_PIN_BL
440#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
441#endif
442
Willy Tarreau7005b582008-11-13 17:18:59 -0800443#endif /* DEFAULT_PROFILE == 0 */
444
445/* global variables */
Mariusz Gorskif4757af2014-11-04 22:47:19 +0100446
447/* Device single-open policy control */
448static atomic_t lcd_available = ATOMIC_INIT(1);
449static atomic_t keypad_available = ATOMIC_INIT(1);
450
Willy Tarreau698b1512008-11-22 11:29:33 +0100451static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800452
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100453static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800454
Willy Tarreau698b1512008-11-22 11:29:33 +0100455static char init_in_progress;
Willy Tarreau7005b582008-11-13 17:18:59 -0800456
Monam Agarwal68d386b2014-02-25 19:40:49 +0530457static void (*lcd_write_cmd)(int);
458static void (*lcd_write_data)(int);
459static void (*lcd_clear_fast)(void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800460
Willy Tarreau698b1512008-11-22 11:29:33 +0100461static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800462static struct timer_list scan_timer;
463
Willy Tarreau630231772008-11-22 12:52:18 +0100464MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100465
Mariusz Gorski59a66a22014-11-27 22:36:45 +0100466static int parport = DEFAULT_PARPORT;
Willy Tarreau698b1512008-11-22 11:29:33 +0100467module_param(parport, int, 0000);
468MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100469
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100470static int profile = DEFAULT_PROFILE;
471module_param(profile, int, 0000);
472MODULE_PARM_DESC(profile,
473 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
474 "4=16x2 nexcom; default=40x2, old kp");
475
Mariusz Gorski36277d42014-11-27 22:36:47 +0100476static int keypad_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100477module_param(keypad_type, int, 0000);
478MODULE_PARM_DESC(keypad_type,
479 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
480
Mariusz Gorski36277d42014-11-27 22:36:47 +0100481static int lcd_type = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100482module_param(lcd_type, int, 0000);
483MODULE_PARM_DESC(lcd_type,
484 "LCD type: 0=none, 1=old //, 2=serial ks0074, 3=hantronix //, 4=nexcom //, 5=compiled-in");
485
Mariusz Gorski36277d42014-11-27 22:36:47 +0100486static int lcd_height = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100487module_param(lcd_height, int, 0000);
488MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100489
Mariusz Gorski36277d42014-11-27 22:36:47 +0100490static int lcd_width = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100491module_param(lcd_width, int, 0000);
492MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100493
Mariusz Gorski36277d42014-11-27 22:36:47 +0100494static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100495module_param(lcd_bwidth, int, 0000);
496MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100497
Mariusz Gorski36277d42014-11-27 22:36:47 +0100498static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100499module_param(lcd_hwidth, int, 0000);
500MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100501
Mariusz Gorski36277d42014-11-27 22:36:47 +0100502static int lcd_charset = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100503module_param(lcd_charset, int, 0000);
504MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100505
Mariusz Gorski36277d42014-11-27 22:36:47 +0100506static int lcd_proto = NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100507module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300508MODULE_PARM_DESC(lcd_proto,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +0200509 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100510
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100511/*
512 * These are the parallel port pins the LCD control signals are connected to.
513 * Set this to 0 if the signal is not used. Set it to its opposite value
514 * (negative) if the signal is negated. -MAXINT is used to indicate that the
515 * pin has not been explicitly specified.
516 *
Willy Tarreau630231772008-11-22 12:52:18 +0100517 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100518 */
519
520static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100521module_param(lcd_e_pin, int, 0000);
522MODULE_PARM_DESC(lcd_e_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530523 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100524
525static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100526module_param(lcd_rs_pin, int, 0000);
527MODULE_PARM_DESC(lcd_rs_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530528 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100529
530static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100531module_param(lcd_rw_pin, int, 0000);
532MODULE_PARM_DESC(lcd_rw_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530533 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100534
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100535static int lcd_cl_pin = PIN_NOT_SET;
536module_param(lcd_cl_pin, int, 0000);
537MODULE_PARM_DESC(lcd_cl_pin,
538 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100539
540static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100541module_param(lcd_da_pin, int, 0000);
542MODULE_PARM_DESC(lcd_da_pin,
Monam Agarwalfe5d2e02014-02-25 19:42:46 +0530543 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100544
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100545static int lcd_bl_pin = PIN_NOT_SET;
546module_param(lcd_bl_pin, int, 0000);
547MODULE_PARM_DESC(lcd_bl_pin,
548 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
549
550/* Deprecated module parameters - consider not using them anymore */
551
Mariusz Gorski36277d42014-11-27 22:36:47 +0100552static int lcd_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100553module_param(lcd_enabled, int, 0000);
554MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
555
Mariusz Gorski36277d42014-11-27 22:36:47 +0100556static int keypad_enabled = NOT_SET;
Mariusz Gorski98e0e762014-11-12 02:08:08 +0100557module_param(keypad_enabled, int, 0000);
558MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
559
Willy Tarreau7005b582008-11-13 17:18:59 -0800560
Peter Huewe36d20412013-02-15 13:47:05 +0100561static const unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800562
563/* for some LCD drivers (ks0074) we need a charset conversion table. */
Peter Huewe36d20412013-02-15 13:47:05 +0100564static const unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100565 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
566 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
567 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
568 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
569 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
570 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
571 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
572 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
573 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
574 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
575 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
576 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
577 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
578 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
579 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
580 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
581 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
582 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
583 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
584 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
585 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
586 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
587 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
588 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
589 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
590 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
591 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
592 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
593 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
594 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
595 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
596 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
597 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800598};
599
Peter Huewe36d20412013-02-15 13:47:05 +0100600static const char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100601 {"S0", "Left\n", "Left\n", ""},
602 {"S1", "Down\n", "Down\n", ""},
603 {"S2", "Up\n", "Up\n", ""},
604 {"S3", "Right\n", "Right\n", ""},
605 {"S4", "Esc\n", "Esc\n", ""},
606 {"S5", "Ret\n", "Ret\n", ""},
607 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800608};
609
610/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100611static const char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100612 {"S0", "Left\n", "Left\n", ""},
613 {"S1", "Down\n", "Down\n", ""},
614 {"S2", "Up\n", "Up\n", ""},
615 {"S3", "Right\n", "Right\n", ""},
616 {"S4s5", "", "Esc\n", "Esc\n"},
617 {"s4S5", "", "Ret\n", "Ret\n"},
618 {"S4S5", "Help\n", "", ""},
619 /* add new signals above this line */
620 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800621};
622
623/* signals, press, repeat, release */
Peter Huewe36d20412013-02-15 13:47:05 +0100624static const char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100625 {"a-p-e-", "Down\n", "Down\n", ""},
626 {"a-p-E-", "Ret\n", "Ret\n", ""},
627 {"a-P-E-", "Esc\n", "Esc\n", ""},
628 {"a-P-e-", "Up\n", "Up\n", ""},
629 /* add new signals above this line */
630 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800631};
632
Peter Huewe36d20412013-02-15 13:47:05 +0100633static const char (*keypad_profile)[4][9] = old_keypad_profile;
Willy Tarreau7005b582008-11-13 17:18:59 -0800634
635/* FIXME: this should be converted to a bit array containing signals states */
636static struct {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300637 unsigned char e; /* parallel LCD E (data latch on falling edge) */
638 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
639 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
640 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
641 unsigned char cl; /* serial LCD clock (latch on rising edge) */
642 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800643} bits;
644
645static void init_scan_timer(void);
646
647/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100648static int set_data_bits(void)
649{
650 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800651
Willy Tarreau698b1512008-11-22 11:29:33 +0100652 val = r_dtr(pprt);
653 for (bit = 0; bit < LCD_BITS; bit++)
654 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800655
Willy Tarreau698b1512008-11-22 11:29:33 +0100656 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
657 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
658 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
659 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
660 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
661 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800662
Willy Tarreau698b1512008-11-22 11:29:33 +0100663 w_dtr(pprt, val);
664 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800665}
666
667/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100668static int set_ctrl_bits(void)
669{
670 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800671
Willy Tarreau698b1512008-11-22 11:29:33 +0100672 val = r_ctr(pprt);
673 for (bit = 0; bit < LCD_BITS; bit++)
674 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800675
Willy Tarreau698b1512008-11-22 11:29:33 +0100676 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
677 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
678 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
679 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
680 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
681 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800682
Willy Tarreau698b1512008-11-22 11:29:33 +0100683 w_ctr(pprt, val);
684 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800685}
686
687/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530688static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100689{
690 set_data_bits();
691 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800692}
693
694/*
695 * Converts a parallel port pin (from -25 to 25) to data and control ports
696 * masks, and data and control port bits. The signal will be considered
697 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
698 *
699 * Result will be used this way :
700 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
701 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
702 */
Peter Huewe36d20412013-02-15 13:47:05 +0100703static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
Willy Tarreau698b1512008-11-22 11:29:33 +0100704{
705 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800706
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200707 d_val[0] = 0;
708 c_val[0] = 0;
709 d_val[1] = 0;
710 c_val[1] = 0;
711 d_val[2] = 0xFF;
712 c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800713
Willy Tarreau698b1512008-11-22 11:29:33 +0100714 if (pin == 0)
715 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800716
Willy Tarreau698b1512008-11-22 11:29:33 +0100717 inv = (pin < 0);
718 if (inv)
719 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800720
Dominique van den Broeck2d534262014-05-24 01:35:24 +0200721 d_bit = 0;
722 c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800723
Willy Tarreau698b1512008-11-22 11:29:33 +0100724 switch (pin) {
725 case PIN_STROBE: /* strobe, inverted */
726 c_bit = PNL_PSTROBE;
727 inv = !inv;
728 break;
729 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
730 d_bit = 1 << (pin - 2);
731 break;
732 case PIN_AUTOLF: /* autofeed, inverted */
733 c_bit = PNL_PAUTOLF;
734 inv = !inv;
735 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300736 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100737 c_bit = PNL_PINITP;
738 break;
739 case PIN_SELECP: /* select_in, inverted */
740 c_bit = PNL_PSELECP;
741 inv = !inv;
742 break;
743 default: /* unknown pin, ignore */
744 break;
745 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800746
Willy Tarreau698b1512008-11-22 11:29:33 +0100747 if (c_bit) {
748 c_val[2] &= ~c_bit;
749 c_val[!inv] = c_bit;
750 } else if (d_bit) {
751 d_val[2] &= ~d_bit;
752 d_val[!inv] = d_bit;
753 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800754}
755
756/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100757static void long_sleep(int ms)
758{
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200759 if (in_interrupt()) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100760 mdelay(ms);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +0200761 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +0100762 current->state = TASK_INTERRUPTIBLE;
763 schedule_timeout((ms * HZ + 999) / 1000);
764 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800765}
766
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300767/* send a serial byte to the LCD panel. The caller is responsible for locking
768 if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100769static void lcd_send_serial(int byte)
770{
771 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800772
Willy Tarreau698b1512008-11-22 11:29:33 +0100773 /* the data bit is set on D0, and the clock on STROBE.
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300774 * LCD reads D0 on STROBE's rising edge. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100775 for (bit = 0; bit < 8; bit++) {
776 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530777 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100778 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530779 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300780 udelay(2); /* maintain the data during 2 us before CLK up */
Willy Tarreau698b1512008-11-22 11:29:33 +0100781 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530782 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300783 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100784 byte >>= 1;
785 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800786}
787
788/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100789static void lcd_backlight(int on)
790{
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100791 if (lcd.pins.bl == PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +0100792 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800793
Justin P. Mattock6975e182012-03-19 08:17:52 -0700794 /* The backlight is activated by setting the AUTOFEED line to +5V */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800795 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100796 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530797 panel_set_bits();
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800798 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800799}
800
801/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100802static void lcd_write_cmd_s(int cmd)
803{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800804 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100805 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
806 lcd_send_serial(cmd & 0x0F);
807 lcd_send_serial((cmd >> 4) & 0x0F);
808 udelay(40); /* the shortest command takes at least 40 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800809 spin_unlock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800810}
811
812/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100813static void lcd_write_data_s(int data)
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(0x5F); /* R/W=W, RS=1 */
817 lcd_send_serial(data & 0x0F);
818 lcd_send_serial((data >> 4) & 0x0F);
819 udelay(40); /* the shortest data 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 a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100824static void lcd_write_cmd_p8(int cmd)
825{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800826 spin_lock_irq(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800827 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100828 w_dtr(pprt, cmd);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300829 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800830
Willy Tarreau698b1512008-11-22 11:29:33 +0100831 bits.e = BIT_SET;
832 bits.rs = BIT_CLR;
833 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800834 set_ctrl_bits();
835
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300836 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800837
838 bits.e = BIT_CLR;
839 set_ctrl_bits();
840
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300841 udelay(120); /* the shortest command takes at least 120 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800842 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100843}
Willy Tarreau7005b582008-11-13 17:18:59 -0800844
Willy Tarreau698b1512008-11-22 11:29:33 +0100845/* send data to the LCD panel in 8 bits parallel mode */
846static void lcd_write_data_p8(int data)
847{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800848 spin_lock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100849 /* present the data to the data port */
850 w_dtr(pprt, data);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300851 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100852
853 bits.e = BIT_SET;
854 bits.rs = BIT_SET;
855 bits.rw = BIT_CLR;
856 set_ctrl_bits();
857
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300858 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100859
860 bits.e = BIT_CLR;
861 set_ctrl_bits();
862
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300863 udelay(45); /* the shortest data takes at least 45 us */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800864 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100865}
866
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400867/* send a command to the TI LCD panel */
868static void lcd_write_cmd_tilcd(int cmd)
869{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800870 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400871 /* present the data to the control port */
872 w_ctr(pprt, cmd);
873 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800874 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400875}
876
877/* send data to the TI LCD panel */
878static void lcd_write_data_tilcd(int data)
879{
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800880 spin_lock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400881 /* present the data to the data port */
882 w_dtr(pprt, data);
883 udelay(60);
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800884 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400885}
886
Willy Tarreau698b1512008-11-22 11:29:33 +0100887static void lcd_gotoxy(void)
888{
889 lcd_write_cmd(0x80 /* set DDRAM address */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100890 | (lcd.addr.y ? lcd.hwidth : 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300891 /* we force the cursor to stay at the end of the
892 line if it wants to go farther */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100893 | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100894 (lcd.hwidth - 1) : lcd.bwidth - 1));
Willy Tarreau698b1512008-11-22 11:29:33 +0100895}
896
897static void lcd_print(char c)
898{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100899 if (lcd.addr.x < lcd.bwidth) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100900 if (lcd_char_conv != NULL)
901 c = lcd_char_conv[(unsigned char)c];
902 lcd_write_data(c);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100903 lcd.addr.x++;
Willy Tarreau698b1512008-11-22 11:29:33 +0100904 }
905 /* prevents the cursor from wrapping onto the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100906 if (lcd.addr.x == lcd.bwidth)
Willy Tarreau698b1512008-11-22 11:29:33 +0100907 lcd_gotoxy();
908}
909
910/* fills the display with spaces and resets X/Y */
911static void lcd_clear_fast_s(void)
912{
913 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200914
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100915 lcd.addr.x = 0;
916 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100917 lcd_gotoxy();
918
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800919 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100920 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100921 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
922 lcd_send_serial(' ' & 0x0F);
923 lcd_send_serial((' ' >> 4) & 0x0F);
924 udelay(40); /* the shortest data takes at least 40 us */
925 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800926 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100927
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}
932
933/* fills the display with spaces and resets X/Y */
934static void lcd_clear_fast_p8(void)
935{
936 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200937
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100938 lcd.addr.x = 0;
939 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100940 lcd_gotoxy();
941
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800942 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100943 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Willy Tarreau698b1512008-11-22 11:29:33 +0100944 /* present the data to the data port */
945 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300946
947 /* maintain the data during 20 us before the strobe */
948 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100949
950 bits.e = BIT_SET;
951 bits.rs = BIT_SET;
952 bits.rw = BIT_CLR;
953 set_ctrl_bits();
954
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300955 /* maintain the strobe during 40 us */
956 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100957
958 bits.e = BIT_CLR;
959 set_ctrl_bits();
960
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300961 /* the shortest data takes at least 45 us */
962 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100963 }
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800964 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +0100965
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100966 lcd.addr.x = 0;
967 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +0100968 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800969}
970
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400971/* fills the display with spaces and resets X/Y */
972static void lcd_clear_fast_tilcd(void)
973{
974 int pos;
Bastien Armandc3ed0af2014-04-04 21:45:07 +0200975
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100976 lcd.addr.x = 0;
977 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400978 lcd_gotoxy();
979
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800980 spin_lock_irq(&pprt_lock);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +0100981 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400982 /* present the data to the data port */
983 w_dtr(pprt, ' ');
984 udelay(60);
985 }
986
Fengguang Wud4d2dbc2012-09-09 16:23:46 +0800987 spin_unlock_irq(&pprt_lock);
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400988
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100989 lcd.addr.x = 0;
990 lcd.addr.y = 0;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400991 lcd_gotoxy();
992}
993
Willy Tarreau7005b582008-11-13 17:18:59 -0800994/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +0100995static void lcd_clear_display(void)
996{
997 lcd_write_cmd(0x01); /* clear display */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +0100998 lcd.addr.x = 0;
999 lcd.addr.y = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001000 /* we must wait a few milliseconds (15) */
1001 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -08001002}
1003
Willy Tarreau698b1512008-11-22 11:29:33 +01001004static void lcd_init_display(void)
1005{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001006 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001007 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -08001008
Willy Tarreau698b1512008-11-22 11:29:33 +01001009 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -08001010
Willy Tarreau698b1512008-11-22 11:29:33 +01001011 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
1012 long_sleep(10);
1013 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
1014 long_sleep(10);
1015 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
1016 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001017
Willy Tarreau698b1512008-11-22 11:29:33 +01001018 lcd_write_cmd(0x30 /* set font height and lines number */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001019 | ((lcd.flags & LCD_FLAG_F) ? 4 : 0)
1020 | ((lcd.flags & LCD_FLAG_N) ? 8 : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001021 );
1022 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001023
Willy Tarreau698b1512008-11-22 11:29:33 +01001024 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
1025 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001026
Willy Tarreau698b1512008-11-22 11:29:33 +01001027 lcd_write_cmd(0x08 /* set display mode */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001028 | ((lcd.flags & LCD_FLAG_D) ? 4 : 0)
1029 | ((lcd.flags & LCD_FLAG_C) ? 2 : 0)
1030 | ((lcd.flags & LCD_FLAG_B) ? 1 : 0)
Willy Tarreau698b1512008-11-22 11:29:33 +01001031 );
Willy Tarreau7005b582008-11-13 17:18:59 -08001032
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001033 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08001034
Willy Tarreau698b1512008-11-22 11:29:33 +01001035 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -08001036
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001037 /* entry mode set : increment, cursor shifting */
1038 lcd_write_cmd(0x06);
Willy Tarreau7005b582008-11-13 17:18:59 -08001039
Willy Tarreau698b1512008-11-22 11:29:33 +01001040 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001041}
1042
1043/*
1044 * These are the file operation function for user access to /dev/lcd
1045 * This function can also be called from inside the kernel, by
1046 * setting file and ppos to NULL.
1047 *
1048 */
1049
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001050static inline int handle_lcd_special_code(void)
1051{
1052 /* LCD special codes */
1053
1054 int processed = 0;
1055
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001056 char *esc = lcd.esc_seq.buf + 2;
1057 int oldflags = lcd.flags;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001058
1059 /* check for display mode flags */
1060 switch (*esc) {
1061 case 'D': /* Display ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001062 lcd.flags |= LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001063 processed = 1;
1064 break;
1065 case 'd': /* Display OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001066 lcd.flags &= ~LCD_FLAG_D;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001067 processed = 1;
1068 break;
1069 case 'C': /* Cursor ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001070 lcd.flags |= LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001071 processed = 1;
1072 break;
1073 case 'c': /* Cursor OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001074 lcd.flags &= ~LCD_FLAG_C;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001075 processed = 1;
1076 break;
1077 case 'B': /* Blink ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001078 lcd.flags |= LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001079 processed = 1;
1080 break;
1081 case 'b': /* Blink OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001082 lcd.flags &= ~LCD_FLAG_B;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001083 processed = 1;
1084 break;
1085 case '+': /* Back light ON */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001086 lcd.flags |= LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001087 processed = 1;
1088 break;
1089 case '-': /* Back light OFF */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001090 lcd.flags &= ~LCD_FLAG_L;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001091 processed = 1;
1092 break;
1093 case '*':
1094 /* flash back light using the keypad timer */
1095 if (scan_timer.function != NULL) {
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001096 if (lcd.light_tempo == 0
1097 && ((lcd.flags & LCD_FLAG_L) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001098 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001099 lcd.light_tempo = FLASH_LIGHT_TEMPO;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001100 }
1101 processed = 1;
1102 break;
1103 case 'f': /* Small Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001104 lcd.flags &= ~LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001105 processed = 1;
1106 break;
1107 case 'F': /* Large Font */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001108 lcd.flags |= LCD_FLAG_F;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001109 processed = 1;
1110 break;
1111 case 'n': /* One Line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001112 lcd.flags &= ~LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001113 processed = 1;
1114 break;
1115 case 'N': /* Two Lines */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001116 lcd.flags |= LCD_FLAG_N;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001117 break;
1118 case 'l': /* Shift Cursor Left */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001119 if (lcd.addr.x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001120 /* back one char if not at end of line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001121 if (lcd.addr.x < lcd.bwidth)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001122 lcd_write_cmd(0x10);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001123 lcd.addr.x--;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001124 }
1125 processed = 1;
1126 break;
1127 case 'r': /* shift cursor right */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001128 if (lcd.addr.x < lcd.width) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001129 /* allow the cursor to pass the end of the line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001130 if (lcd.addr.x <
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001131 (lcd.bwidth - 1))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001132 lcd_write_cmd(0x14);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001133 lcd.addr.x++;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001134 }
1135 processed = 1;
1136 break;
1137 case 'L': /* shift display left */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001138 lcd.left_shift++;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001139 lcd_write_cmd(0x18);
1140 processed = 1;
1141 break;
1142 case 'R': /* shift display right */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001143 lcd.left_shift--;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001144 lcd_write_cmd(0x1C);
1145 processed = 1;
1146 break;
1147 case 'k': { /* kill end of line */
1148 int x;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001149
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001150 for (x = lcd.addr.x; x < lcd.bwidth; x++)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001151 lcd_write_data(' ');
1152
1153 /* restore cursor position */
1154 lcd_gotoxy();
1155 processed = 1;
1156 break;
1157 }
1158 case 'I': /* reinitialize display */
1159 lcd_init_display();
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001160 lcd.left_shift = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001161 processed = 1;
1162 break;
1163 case 'G': {
1164 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1165 * and '7', representing the numerical ASCII code of the
1166 * redefined character, and <xx...xx> a sequence of 16
1167 * hex digits representing 8 bytes for each character.
1168 * Most LCDs will only use 5 lower bits of the 7 first
1169 * bytes.
1170 */
1171
1172 unsigned char cgbytes[8];
1173 unsigned char cgaddr;
1174 int cgoffset;
1175 int shift;
1176 char value;
1177 int addr;
1178
1179 if (strchr(esc, ';') == NULL)
1180 break;
1181
1182 esc++;
1183
1184 cgaddr = *(esc++) - '0';
1185 if (cgaddr > 7) {
1186 processed = 1;
1187 break;
1188 }
1189
1190 cgoffset = 0;
1191 shift = 0;
1192 value = 0;
1193 while (*esc && cgoffset < 8) {
1194 shift ^= 4;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001195 if (*esc >= '0' && *esc <= '9') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001196 value |= (*esc - '0') << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001197 } else if (*esc >= 'A' && *esc <= 'Z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001198 value |= (*esc - 'A' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001199 } else if (*esc >= 'a' && *esc <= 'z') {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001200 value |= (*esc - 'a' + 10) << shift;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001201 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001202 esc++;
1203 continue;
1204 }
1205
1206 if (shift == 0) {
1207 cgbytes[cgoffset++] = value;
1208 value = 0;
1209 }
1210
1211 esc++;
1212 }
1213
1214 lcd_write_cmd(0x40 | (cgaddr * 8));
1215 for (addr = 0; addr < cgoffset; addr++)
1216 lcd_write_data(cgbytes[addr]);
1217
1218 /* ensures that we stop writing to CGRAM */
1219 lcd_gotoxy();
1220 processed = 1;
1221 break;
1222 }
1223 case 'x': /* gotoxy : LxXXX[yYYY]; */
1224 case 'y': /* gotoxy : LyYYY[xXXX]; */
1225 if (strchr(esc, ';') == NULL)
1226 break;
1227
1228 while (*esc) {
1229 if (*esc == 'x') {
1230 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001231 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001232 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001233 } else if (*esc == 'y') {
1234 esc++;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001235 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
Pelle Windestam12995702011-08-30 20:29:24 +02001236 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001237 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001238 break;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02001239 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001240 }
1241
1242 lcd_gotoxy();
1243 processed = 1;
1244 break;
1245 }
1246
Adam Buchbinderf2635892012-09-19 21:51:07 -04001247 /* Check whether one flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001248 if (oldflags != lcd.flags) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001249 /* check whether one of B,C,D flags were changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001250 if ((oldflags ^ lcd.flags) &
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001251 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1252 /* set display mode */
1253 lcd_write_cmd(0x08
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001254 | ((lcd.flags & LCD_FLAG_D) ? 4 : 0)
1255 | ((lcd.flags & LCD_FLAG_C) ? 2 : 0)
1256 | ((lcd.flags & LCD_FLAG_B) ? 1 : 0));
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001257 /* check whether one of F,N flags was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001258 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001259 lcd_write_cmd(0x30
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001260 | ((lcd.flags & LCD_FLAG_F) ? 4 : 0)
1261 | ((lcd.flags & LCD_FLAG_N) ? 8 : 0));
Adam Buchbinderf2635892012-09-19 21:51:07 -04001262 /* check whether L flag was changed */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001263 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1264 if (lcd.flags & (LCD_FLAG_L))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001265 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001266 else if (lcd.light_tempo == 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001267 /* switch off the light only when the tempo
1268 lighting is gone */
1269 lcd_backlight(0);
1270 }
1271 }
1272
1273 return processed;
1274}
1275
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001276static void lcd_write_char(char c)
Willy Tarreau698b1512008-11-22 11:29:33 +01001277{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001278 /* first, we'll test if we're in escape mode */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001279 if ((c != '\n') && lcd.esc_seq.len >= 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001280 /* yes, let's add this char to the buffer */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001281 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1282 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001283 } else {
1284 /* aborts any previous escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001285 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001286
1287 switch (c) {
1288 case LCD_ESCAPE_CHAR:
1289 /* start of an escape sequence */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001290 lcd.esc_seq.len = 0;
1291 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001292 break;
1293 case '\b':
1294 /* go back one char and clear it */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001295 if (lcd.addr.x > 0) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001296 /* check if we're not at the
1297 end of the line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001298 if (lcd.addr.x < lcd.bwidth)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001299 /* back one char */
1300 lcd_write_cmd(0x10);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001301 lcd.addr.x--;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001302 }
1303 /* replace with a space */
1304 lcd_write_data(' ');
1305 /* back one char again */
1306 lcd_write_cmd(0x10);
1307 break;
1308 case '\014':
1309 /* quickly clear the display */
1310 lcd_clear_fast();
1311 break;
1312 case '\n':
1313 /* flush the remainder of the current line and
1314 go to the beginning of the next line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001315 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001316 lcd_write_data(' ');
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001317 lcd.addr.x = 0;
1318 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001319 lcd_gotoxy();
1320 break;
1321 case '\r':
1322 /* go to the beginning of the same line */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001323 lcd.addr.x = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001324 lcd_gotoxy();
1325 break;
1326 case '\t':
1327 /* print a space instead of the tab */
1328 lcd_print(' ');
1329 break;
1330 default:
1331 /* simply print this char */
1332 lcd_print(c);
1333 break;
1334 }
1335 }
1336
1337 /* now we'll see if we're in an escape mode and if the current
1338 escape sequence can be understood. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001339 if (lcd.esc_seq.len >= 2) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001340 int processed = 0;
1341
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001342 if (!strcmp(lcd.esc_seq.buf, "[2J")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001343 /* clear the display */
1344 lcd_clear_fast();
1345 processed = 1;
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001346 } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001347 /* cursor to home */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001348 lcd.addr.x = 0;
1349 lcd.addr.y = 0;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001350 lcd_gotoxy();
1351 processed = 1;
1352 }
1353 /* codes starting with ^[[L */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001354 else if ((lcd.esc_seq.len >= 3) &&
1355 (lcd.esc_seq.buf[0] == '[') &&
1356 (lcd.esc_seq.buf[1] == 'L')) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001357 processed = handle_lcd_special_code();
1358 }
1359
1360 /* LCD special escape codes */
1361 /* flush the escape sequence if it's been processed
1362 or if it is getting too long. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001363 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1364 lcd.esc_seq.len = -1;
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001365 } /* escape codes */
1366}
1367
1368static ssize_t lcd_write(struct file *file,
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001369 const char __user *buf, size_t count, loff_t *ppos)
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001370{
1371 const char __user *tmp = buf;
Willy Tarreau698b1512008-11-22 11:29:33 +01001372 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001373
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001374 for (; count-- > 0; (*ppos)++, tmp++) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001375 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001376 /* let's be a little nice with other processes
1377 that need some CPU */
1378 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001379
Bastien Armand6a4193a2014-04-23 19:42:11 +02001380 if (get_user(c, tmp))
Willy Tarreau698b1512008-11-22 11:29:33 +01001381 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001382
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001383 lcd_write_char(c);
Willy Tarreau7005b582008-11-13 17:18:59 -08001384 }
1385
Willy Tarreau698b1512008-11-22 11:29:33 +01001386 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001387}
1388
Willy Tarreau698b1512008-11-22 11:29:33 +01001389static int lcd_open(struct inode *inode, struct file *file)
1390{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001391 if (!atomic_dec_and_test(&lcd_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001392 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001393
Willy Tarreau698b1512008-11-22 11:29:33 +01001394 if (file->f_mode & FMODE_READ) /* device is write-only */
1395 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001396
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001397 if (lcd.must_clear) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001398 lcd_clear_display();
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001399 lcd.must_clear = false;
Willy Tarreau698b1512008-11-22 11:29:33 +01001400 }
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001401 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001402}
1403
Willy Tarreau698b1512008-11-22 11:29:33 +01001404static int lcd_release(struct inode *inode, struct file *file)
1405{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001406 atomic_inc(&lcd_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001407 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001408}
1409
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001410static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001411 .write = lcd_write,
1412 .open = lcd_open,
1413 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001414 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001415};
1416
1417static struct miscdevice lcd_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001418 .minor = LCD_MINOR,
1419 .name = "lcd",
1420 .fops = &lcd_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001421};
1422
Willy Tarreau7005b582008-11-13 17:18:59 -08001423/* public function usable from the kernel for any purpose */
Peter Huewe36d20412013-02-15 13:47:05 +01001424static void panel_lcd_print(const char *s)
Willy Tarreau698b1512008-11-22 11:29:33 +01001425{
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001426 const char *tmp = s;
1427 int count = strlen(s);
1428
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001429 if (lcd.enabled && lcd.initialized) {
Bastien Armand70a8c3e2014-04-18 18:10:57 +02001430 for (; count-- > 0; tmp++) {
1431 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
1432 /* let's be a little nice with other processes
1433 that need some CPU */
1434 schedule();
1435
1436 lcd_write_char(*tmp);
1437 }
1438 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001439}
1440
Willy Tarreau7005b582008-11-13 17:18:59 -08001441/* initialize the LCD driver */
Peter Huewe36d20412013-02-15 13:47:05 +01001442static void lcd_init(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01001443{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01001444 switch (selected_lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001445 case LCD_TYPE_OLD:
1446 /* parallel mode, 8 bits */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001447 lcd.proto = LCD_PROTO_PARALLEL;
1448 lcd.charset = LCD_CHARSET_NORMAL;
1449 lcd.pins.e = PIN_STROBE;
1450 lcd.pins.rs = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001451
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001452 lcd.width = 40;
1453 lcd.bwidth = 40;
1454 lcd.hwidth = 64;
1455 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001456 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001457 case LCD_TYPE_KS0074:
1458 /* serial mode, ks0074 */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001459 lcd.proto = LCD_PROTO_SERIAL;
1460 lcd.charset = LCD_CHARSET_KS0074;
1461 lcd.pins.bl = PIN_AUTOLF;
1462 lcd.pins.cl = PIN_STROBE;
1463 lcd.pins.da = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001464
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001465 lcd.width = 16;
1466 lcd.bwidth = 40;
1467 lcd.hwidth = 16;
1468 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001469 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001470 case LCD_TYPE_NEXCOM:
1471 /* parallel mode, 8 bits, generic */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001472 lcd.proto = LCD_PROTO_PARALLEL;
1473 lcd.charset = LCD_CHARSET_NORMAL;
1474 lcd.pins.e = PIN_AUTOLF;
1475 lcd.pins.rs = PIN_SELECP;
1476 lcd.pins.rw = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001477
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001478 lcd.width = 16;
1479 lcd.bwidth = 40;
1480 lcd.hwidth = 64;
1481 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001482 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001483 case LCD_TYPE_CUSTOM:
1484 /* customer-defined */
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001485 lcd.proto = DEFAULT_LCD_PROTO;
1486 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001487 /* default geometry will be set later */
1488 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001489 case LCD_TYPE_HANTRONIX:
1490 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001491 default:
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_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001496
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001497 lcd.width = 16;
1498 lcd.bwidth = 40;
1499 lcd.hwidth = 64;
1500 lcd.height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001501 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001502 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001503
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001504 /* Overwrite with module params set on loading */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001505 if (lcd_height != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001506 lcd.height = lcd_height;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001507 if (lcd_width != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001508 lcd.width = lcd_width;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001509 if (lcd_bwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001510 lcd.bwidth = lcd_bwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001511 if (lcd_hwidth != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001512 lcd.hwidth = lcd_hwidth;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001513 if (lcd_charset != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001514 lcd.charset = lcd_charset;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01001515 if (lcd_proto != NOT_SET)
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001516 lcd.proto = lcd_proto;
1517 if (lcd_e_pin != PIN_NOT_SET)
1518 lcd.pins.e = lcd_e_pin;
1519 if (lcd_rs_pin != PIN_NOT_SET)
1520 lcd.pins.rs = lcd_rs_pin;
1521 if (lcd_rw_pin != PIN_NOT_SET)
1522 lcd.pins.rw = lcd_rw_pin;
1523 if (lcd_cl_pin != PIN_NOT_SET)
1524 lcd.pins.cl = lcd_cl_pin;
1525 if (lcd_da_pin != PIN_NOT_SET)
1526 lcd.pins.da = lcd_da_pin;
1527 if (lcd_bl_pin != PIN_NOT_SET)
1528 lcd.pins.bl = lcd_bl_pin;
Willy Tarreau7005b582008-11-13 17:18:59 -08001529
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001530 /* this is used to catch wrong and default values */
1531 if (lcd.width <= 0)
1532 lcd.width = DEFAULT_LCD_WIDTH;
1533 if (lcd.bwidth <= 0)
1534 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1535 if (lcd.hwidth <= 0)
1536 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1537 if (lcd.height <= 0)
1538 lcd.height = DEFAULT_LCD_HEIGHT;
1539
1540 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001541 lcd_write_cmd = lcd_write_cmd_s;
1542 lcd_write_data = lcd_write_data_s;
1543 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001544
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001545 if (lcd.pins.cl == PIN_NOT_SET)
1546 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1547 if (lcd.pins.da == PIN_NOT_SET)
1548 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001549
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001550 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001551 lcd_write_cmd = lcd_write_cmd_p8;
1552 lcd_write_data = lcd_write_data_p8;
1553 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001554
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001555 if (lcd.pins.e == PIN_NOT_SET)
1556 lcd.pins.e = DEFAULT_LCD_PIN_E;
1557 if (lcd.pins.rs == PIN_NOT_SET)
1558 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1559 if (lcd.pins.rw == PIN_NOT_SET)
1560 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001561 } else {
1562 lcd_write_cmd = lcd_write_cmd_tilcd;
1563 lcd_write_data = lcd_write_data_tilcd;
1564 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001565 }
1566
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001567 if (lcd.pins.bl == PIN_NOT_SET)
1568 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
Willy Tarreau698b1512008-11-22 11:29:33 +01001569
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001570 if (lcd.pins.e == PIN_NOT_SET)
1571 lcd.pins.e = PIN_NONE;
1572 if (lcd.pins.rs == PIN_NOT_SET)
1573 lcd.pins.rs = PIN_NONE;
1574 if (lcd.pins.rw == PIN_NOT_SET)
1575 lcd.pins.rw = PIN_NONE;
1576 if (lcd.pins.bl == PIN_NOT_SET)
1577 lcd.pins.bl = PIN_NONE;
1578 if (lcd.pins.cl == PIN_NOT_SET)
1579 lcd.pins.cl = PIN_NONE;
1580 if (lcd.pins.da == PIN_NOT_SET)
1581 lcd.pins.da = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001582
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001583 if (lcd.charset == NOT_SET)
1584 lcd.charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001585
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001586 if (lcd.charset == LCD_CHARSET_KS0074)
Willy Tarreau698b1512008-11-22 11:29:33 +01001587 lcd_char_conv = lcd_char_conv_ks0074;
1588 else
1589 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001590
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001591 if (lcd.pins.bl != PIN_NONE)
Willy Tarreau698b1512008-11-22 11:29:33 +01001592 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001593
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001594 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
Willy Tarreau698b1512008-11-22 11:29:33 +01001595 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001596 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
Willy Tarreau698b1512008-11-22 11:29:33 +01001597 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001598 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
Willy Tarreau698b1512008-11-22 11:29:33 +01001599 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001600 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001601 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001602 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
Willy Tarreau698b1512008-11-22 11:29:33 +01001603 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01001604 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
Willy Tarreau698b1512008-11-22 11:29:33 +01001605 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001606
Willy Tarreau698b1512008-11-22 11:29:33 +01001607 /* before this line, we must NOT send anything to the display.
1608 * Since lcd_init_display() needs to write data, we have to
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001609 * enable mark the LCD initialized just before. */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001610 lcd.initialized = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001611 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001612
Willy Tarreau698b1512008-11-22 11:29:33 +01001613 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001614#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1615#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001616 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001617#endif
1618#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001619 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1620 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001621#endif
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001622 lcd.addr.x = 0;
1623 lcd.addr.y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001624 /* clear the display on the next device opening */
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001625 lcd.must_clear = true;
Willy Tarreau698b1512008-11-22 11:29:33 +01001626 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001627}
1628
Willy Tarreau7005b582008-11-13 17:18:59 -08001629/*
1630 * These are the file operation function for user access to /dev/keypad
1631 */
1632
Willy Tarreau698b1512008-11-22 11:29:33 +01001633static ssize_t keypad_read(struct file *file,
Bastien Armandcce75f42014-04-18 18:10:08 +02001634 char __user *buf, size_t count, loff_t *ppos)
Willy Tarreau698b1512008-11-22 11:29:33 +01001635{
Willy Tarreau698b1512008-11-22 11:29:33 +01001636 unsigned i = *ppos;
Bastien Armandcce75f42014-04-18 18:10:08 +02001637 char __user *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001638
Willy Tarreau698b1512008-11-22 11:29:33 +01001639 if (keypad_buflen == 0) {
1640 if (file->f_flags & O_NONBLOCK)
1641 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001642
Arnd Bergmann310df692014-01-02 13:07:35 +01001643 if (wait_event_interruptible(keypad_read_wait,
1644 keypad_buflen != 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001645 return -EINTR;
1646 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001647
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001648 for (; count-- > 0 && (keypad_buflen > 0);
1649 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001650 put_user(keypad_buffer[keypad_start], tmp);
1651 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1652 }
1653 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001654
Willy Tarreau698b1512008-11-22 11:29:33 +01001655 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001656}
1657
Willy Tarreau698b1512008-11-22 11:29:33 +01001658static int keypad_open(struct inode *inode, struct file *file)
1659{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001660 if (!atomic_dec_and_test(&keypad_available))
Willy Tarreau698b1512008-11-22 11:29:33 +01001661 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001662
Willy Tarreau698b1512008-11-22 11:29:33 +01001663 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1664 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001665
Willy Tarreau698b1512008-11-22 11:29:33 +01001666 keypad_buflen = 0; /* flush the buffer on opening */
Willy Tarreau698b1512008-11-22 11:29:33 +01001667 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001668}
1669
Willy Tarreau698b1512008-11-22 11:29:33 +01001670static int keypad_release(struct inode *inode, struct file *file)
1671{
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001672 atomic_inc(&keypad_available);
Willy Tarreau698b1512008-11-22 11:29:33 +01001673 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001674}
1675
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001676static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001677 .read = keypad_read, /* read */
1678 .open = keypad_open, /* open */
1679 .release = keypad_release, /* close */
Arnd Bergmann6038f372010-08-15 18:52:59 +02001680 .llseek = default_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001681};
1682
1683static struct miscdevice keypad_dev = {
Mariusz Gorski6c3773de2014-10-29 23:32:30 +01001684 .minor = KEYPAD_MINOR,
1685 .name = "keypad",
1686 .fops = &keypad_fops,
Willy Tarreau7005b582008-11-13 17:18:59 -08001687};
1688
Peter Huewe36d20412013-02-15 13:47:05 +01001689static void keypad_send_key(const char *string, int max_len)
Willy Tarreau698b1512008-11-22 11:29:33 +01001690{
1691 if (init_in_progress)
1692 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001693
Willy Tarreau698b1512008-11-22 11:29:33 +01001694 /* send the key to the device only if a process is attached to it. */
Mariusz Gorskif4757af2014-11-04 22:47:19 +01001695 if (!atomic_read(&keypad_available)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001696 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1697 keypad_buffer[(keypad_start + keypad_buflen++) %
1698 KEYPAD_BUFFER] = *string++;
1699 }
1700 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001701 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001702}
1703
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001704/* this function scans all the bits involving at least one logical signal,
1705 * and puts the results in the bitfield "phys_read" (one bit per established
1706 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001707 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001708 * Note: to debounce input signals, we will only consider as switched a signal
1709 * which is stable across 2 measures. Signals which are different between two
1710 * reads will be kept as they previously were in their logical form (phys_prev).
1711 * A signal which has just switched will have a 1 in
1712 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001713 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001714static void phys_scan_contacts(void)
1715{
1716 int bit, bitval;
1717 char oldval;
1718 char bitmask;
1719 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001720
Willy Tarreau698b1512008-11-22 11:29:33 +01001721 phys_prev = phys_curr;
1722 phys_read_prev = phys_read;
1723 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001724
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001725 /* keep track of old value, with all outputs disabled */
1726 oldval = r_dtr(pprt) | scan_mask_o;
1727 /* activate all keyboard outputs (active low) */
1728 w_dtr(pprt, oldval & ~scan_mask_o);
1729
1730 /* will have a 1 for each bit set to gnd */
1731 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1732 /* disable all matrix signals */
1733 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001734
Willy Tarreau698b1512008-11-22 11:29:33 +01001735 /* now that all outputs are cleared, the only active input bits are
1736 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001737 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001738
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001739 /* 1 for each grounded input */
1740 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1741
1742 /* grounded inputs are signals 40-44 */
1743 phys_read |= (pmask_t) gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001744
Willy Tarreau698b1512008-11-22 11:29:33 +01001745 if (bitmask != gndmask) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001746 /* since clearing the outputs changed some inputs, we know
1747 * that some input signals are currently tied to some outputs.
1748 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001749 */
1750 for (bit = 0; bit < 8; bit++) {
1751 bitval = 1 << bit;
1752
1753 if (!(scan_mask_o & bitval)) /* skip unused bits */
1754 continue;
1755
1756 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1757 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1758 phys_read |= (pmask_t) bitmask << (5 * bit);
1759 }
1760 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001761 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001762 /* this is easy: use old bits when they are flapping,
1763 * use new ones when stable */
1764 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1765 (phys_read & ~(phys_read ^ phys_read_prev));
1766}
1767
1768static inline int input_state_high(struct logical_input *input)
1769{
1770#if 0
1771 /* FIXME:
1772 * this is an invalid test. It tries to catch
1773 * transitions from single-key to multiple-key, but
1774 * doesn't take into account the contacts polarity.
1775 * The only solution to the problem is to parse keys
1776 * from the most complex to the simplest combinations,
1777 * and mark them as 'caught' once a combination
1778 * matches, then unmatch it for all other ones.
1779 */
1780
1781 /* try to catch dangerous transitions cases :
1782 * someone adds a bit, so this signal was a false
1783 * positive resulting from a transition. We should
1784 * invalidate the signal immediately and not call the
1785 * release function.
1786 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1787 */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001788 if (((phys_prev & input->mask) == input->value) &&
1789 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001790 input->state = INPUT_ST_LOW; /* invalidate */
1791 return 1;
1792 }
1793#endif
1794
1795 if ((phys_curr & input->mask) == input->value) {
1796 if ((input->type == INPUT_TYPE_STD) &&
1797 (input->high_timer == 0)) {
1798 input->high_timer++;
1799 if (input->u.std.press_fct != NULL)
1800 input->u.std.press_fct(input->u.std.press_data);
1801 } else if (input->type == INPUT_TYPE_KBD) {
1802 /* will turn on the light */
1803 keypressed = 1;
1804
1805 if (input->high_timer == 0) {
1806 char *press_str = input->u.kbd.press_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001807
Jake Champline6626de2013-05-04 11:21:17 -04001808 if (press_str[0]) {
1809 int s = sizeof(input->u.kbd.press_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001810
Jake Champline6626de2013-05-04 11:21:17 -04001811 keypad_send_key(press_str, s);
1812 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001813 }
1814
1815 if (input->u.kbd.repeat_str[0]) {
1816 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001817
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001818 if (input->high_timer >= KEYPAD_REP_START) {
Jake Champline6626de2013-05-04 11:21:17 -04001819 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001820
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001821 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001822 keypad_send_key(repeat_str, s);
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001823 }
1824 /* we will need to come back here soon */
1825 inputs_stable = 0;
1826 }
1827
1828 if (input->high_timer < 255)
1829 input->high_timer++;
1830 }
1831 return 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001832 }
Vincent Heuken083b3632014-07-10 03:34:28 -07001833
1834 /* else signal falling down. Let's fall through. */
1835 input->state = INPUT_ST_FALLING;
1836 input->fall_timer = 0;
1837
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001838 return 0;
1839}
1840
1841static inline void input_state_falling(struct logical_input *input)
1842{
1843#if 0
1844 /* FIXME !!! same comment as in input_state_high */
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02001845 if (((phys_prev & input->mask) == input->value) &&
1846 ((phys_curr & input->mask) > input->value)) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001847 input->state = INPUT_ST_LOW; /* invalidate */
1848 return;
1849 }
1850#endif
1851
1852 if ((phys_curr & input->mask) == input->value) {
1853 if (input->type == INPUT_TYPE_KBD) {
1854 /* will turn on the light */
1855 keypressed = 1;
1856
1857 if (input->u.kbd.repeat_str[0]) {
1858 char *repeat_str = input->u.kbd.repeat_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001859
Jake Champline6626de2013-05-04 11:21:17 -04001860 if (input->high_timer >= KEYPAD_REP_START) {
1861 int s = sizeof(input->u.kbd.repeat_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001862
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001863 input->high_timer -= KEYPAD_REP_DELAY;
Jake Champline6626de2013-05-04 11:21:17 -04001864 keypad_send_key(repeat_str, s);
1865 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001866 /* we will need to come back here soon */
1867 inputs_stable = 0;
1868 }
1869
1870 if (input->high_timer < 255)
1871 input->high_timer++;
1872 }
1873 input->state = INPUT_ST_HIGH;
1874 } else if (input->fall_timer >= input->fall_time) {
1875 /* call release event */
1876 if (input->type == INPUT_TYPE_STD) {
1877 void (*release_fct)(int) = input->u.std.release_fct;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001878
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001879 if (release_fct != NULL)
1880 release_fct(input->u.std.release_data);
1881 } else if (input->type == INPUT_TYPE_KBD) {
1882 char *release_str = input->u.kbd.release_str;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001883
Jake Champline6626de2013-05-04 11:21:17 -04001884 if (release_str[0]) {
1885 int s = sizeof(input->u.kbd.release_str);
Bastien Armandc3ed0af2014-04-04 21:45:07 +02001886
Jake Champline6626de2013-05-04 11:21:17 -04001887 keypad_send_key(release_str, s);
1888 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001889 }
1890
1891 input->state = INPUT_ST_LOW;
1892 } else {
1893 input->fall_timer++;
1894 inputs_stable = 0;
1895 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001896}
1897
Willy Tarreau698b1512008-11-22 11:29:33 +01001898static void panel_process_inputs(void)
1899{
1900 struct list_head *item;
1901 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001902
Willy Tarreau698b1512008-11-22 11:29:33 +01001903 keypressed = 0;
1904 inputs_stable = 1;
1905 list_for_each(item, &logical_inputs) {
1906 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001907
Willy Tarreau698b1512008-11-22 11:29:33 +01001908 switch (input->state) {
1909 case INPUT_ST_LOW:
1910 if ((phys_curr & input->mask) != input->value)
1911 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001912 /* if all needed ones were already set previously,
1913 * this means that this logical signal has been
1914 * activated by the releasing of another combined
1915 * signal, so we don't want to match.
1916 * eg: AB -(release B)-> A -(release A)-> 0 :
1917 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001918 */
1919 if ((phys_prev & input->mask) == input->value)
1920 break;
1921 input->rise_timer = 0;
1922 input->state = INPUT_ST_RISING;
1923 /* no break here, fall through */
1924 case INPUT_ST_RISING:
1925 if ((phys_curr & input->mask) != input->value) {
1926 input->state = INPUT_ST_LOW;
1927 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001928 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001929 if (input->rise_timer < input->rise_time) {
1930 inputs_stable = 0;
1931 input->rise_timer++;
1932 break;
1933 }
1934 input->high_timer = 0;
1935 input->state = INPUT_ST_HIGH;
1936 /* no break here, fall through */
1937 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001938 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001939 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001940 /* no break here, fall through */
1941 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001942 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001943 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001944 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001945}
1946
Willy Tarreau698b1512008-11-22 11:29:33 +01001947static void panel_scan_timer(void)
1948{
Mariusz Gorskia8b25802014-11-27 22:36:49 +01001949 if (keypad.enabled && keypad_initialized) {
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001950 if (spin_trylock_irq(&pprt_lock)) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001951 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001952
1953 /* no need for the parport anymore */
Fengguang Wud4d2dbc2012-09-09 16:23:46 +08001954 spin_unlock_irq(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001955 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001956
Willy Tarreau698b1512008-11-22 11:29:33 +01001957 if (!inputs_stable || phys_curr != phys_prev)
1958 panel_process_inputs();
1959 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001960
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001961 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001962 if (keypressed) {
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001963 if (lcd.light_tempo == 0
1964 && ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001965 lcd_backlight(1);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01001966 lcd.light_tempo = FLASH_LIGHT_TEMPO;
1967 } else if (lcd.light_tempo > 0) {
1968 lcd.light_tempo--;
1969 if (lcd.light_tempo == 0
1970 && ((lcd.flags & LCD_FLAG_L) == 0))
Willy Tarreau698b1512008-11-22 11:29:33 +01001971 lcd_backlight(0);
1972 }
1973 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001974
Willy Tarreau698b1512008-11-22 11:29:33 +01001975 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001976}
1977
Willy Tarreau698b1512008-11-22 11:29:33 +01001978static void init_scan_timer(void)
1979{
1980 if (scan_timer.function != NULL)
1981 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001982
Willy Tarreau698b1512008-11-22 11:29:33 +01001983 init_timer(&scan_timer);
1984 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1985 scan_timer.data = 0;
1986 scan_timer.function = (void *)&panel_scan_timer;
1987 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001988}
1989
1990/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001991 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1992 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08001993 * returns 1 if ok, 0 if error (in which case, nothing is written).
1994 */
Peter Huewe36d20412013-02-15 13:47:05 +01001995static int input_name2mask(const char *name, pmask_t *mask, pmask_t *value,
Willy Tarreau698b1512008-11-22 11:29:33 +01001996 char *imask, char *omask)
1997{
1998 static char sigtab[10] = "EeSsPpAaBb";
1999 char im, om;
2000 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08002001
Dominique van den Broeck2d534262014-05-24 01:35:24 +02002002 om = 0ULL;
2003 im = 0ULL;
2004 m = 0ULL;
2005 v = 0ULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002006 while (*name) {
2007 int in, out, bit, neg;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002008
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002009 for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name);
2010 in++)
Willy Tarreau698b1512008-11-22 11:29:33 +01002011 ;
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002012
Willy Tarreau698b1512008-11-22 11:29:33 +01002013 if (in >= sizeof(sigtab))
2014 return 0; /* input name not found */
2015 neg = (in & 1); /* odd (lower) names are negated */
2016 in >>= 1;
2017 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08002018
Willy Tarreau698b1512008-11-22 11:29:33 +01002019 name++;
2020 if (isdigit(*name)) {
2021 out = *name - '0';
2022 om |= (1 << out);
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002023 } else if (*name == '-') {
Willy Tarreau698b1512008-11-22 11:29:33 +01002024 out = 8;
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002025 } else {
Willy Tarreau698b1512008-11-22 11:29:33 +01002026 return 0; /* unknown bit name */
Dominique van den Broeck3ac76902014-05-21 14:09:59 +02002027 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002028
2029 bit = (out * 5) + in;
2030
2031 m |= 1ULL << bit;
2032 if (!neg)
2033 v |= 1ULL << bit;
2034 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08002035 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002036 *mask = m;
2037 *value = v;
2038 if (imask)
2039 *imask |= im;
2040 if (omask)
2041 *omask |= om;
2042 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002043}
2044
2045/* tries to bind a key to the signal name <name>. The key will send the
2046 * strings <press>, <repeat>, <release> for these respective events.
2047 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2048 */
Peter Huewe36d20412013-02-15 13:47:05 +01002049static struct logical_input *panel_bind_key(const char *name, const char *press,
2050 const char *repeat,
2051 const char *release)
Willy Tarreau698b1512008-11-22 11:29:33 +01002052{
2053 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002054
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002055 key = kzalloc(sizeof(*key), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002056 if (!key)
Willy Tarreau698b1512008-11-22 11:29:33 +01002057 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002058
Willy Tarreau698b1512008-11-22 11:29:33 +01002059 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002060 &scan_mask_o)) {
2061 kfree(key);
Willy Tarreau698b1512008-11-22 11:29:33 +01002062 return NULL;
Kulikov Vasiliycb46f472010-07-12 18:48:24 +04002063 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002064
2065 key->type = INPUT_TYPE_KBD;
2066 key->state = INPUT_ST_LOW;
2067 key->rise_time = 1;
2068 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002069
Willy Tarreau698b1512008-11-22 11:29:33 +01002070 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2071 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2072 strncpy(key->u.kbd.release_str, release,
2073 sizeof(key->u.kbd.release_str));
2074 list_add(&key->list, &logical_inputs);
2075 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002076}
2077
Willy Tarreau630231772008-11-22 12:52:18 +01002078#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002079/* tries to bind a callback function to the signal name <name>. The function
2080 * <press_fct> will be called with the <press_data> arg when the signal is
2081 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002082 * Returns the pointer to the new signal if ok, NULL if the signal could not
2083 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002084 */
2085static struct logical_input *panel_bind_callback(char *name,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302086 void (*press_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002087 int press_data,
Monam Agarwal68d386b2014-02-25 19:40:49 +05302088 void (*release_fct)(int),
Willy Tarreau698b1512008-11-22 11:29:33 +01002089 int release_data)
2090{
2091 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002092
Dominique van den Broeckfdf4a4942014-05-21 14:10:00 +02002093 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002094 if (!callback)
Willy Tarreau698b1512008-11-22 11:29:33 +01002095 return NULL;
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002096
Willy Tarreau698b1512008-11-22 11:29:33 +01002097 memset(callback, 0, sizeof(struct logical_input));
2098 if (!input_name2mask(name, &callback->mask, &callback->value,
2099 &scan_mask_i, &scan_mask_o))
2100 return NULL;
2101
2102 callback->type = INPUT_TYPE_STD;
2103 callback->state = INPUT_ST_LOW;
2104 callback->rise_time = 1;
2105 callback->fall_time = 1;
2106 callback->u.std.press_fct = press_fct;
2107 callback->u.std.press_data = press_data;
2108 callback->u.std.release_fct = release_fct;
2109 callback->u.std.release_data = release_data;
2110 list_add(&callback->list, &logical_inputs);
2111 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002112}
Willy Tarreau630231772008-11-22 12:52:18 +01002113#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002114
Willy Tarreau698b1512008-11-22 11:29:33 +01002115static void keypad_init(void)
2116{
2117 int keynum;
Bastien Armandc3ed0af2014-04-04 21:45:07 +02002118
Willy Tarreau698b1512008-11-22 11:29:33 +01002119 init_waitqueue_head(&keypad_read_wait);
2120 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002121
Willy Tarreau698b1512008-11-22 11:29:33 +01002122 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002123
Willy Tarreau698b1512008-11-22 11:29:33 +01002124 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2125 panel_bind_key(keypad_profile[keynum][0],
2126 keypad_profile[keynum][1],
2127 keypad_profile[keynum][2],
2128 keypad_profile[keynum][3]);
2129 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002130
Willy Tarreau698b1512008-11-22 11:29:33 +01002131 init_scan_timer();
2132 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002133}
2134
Willy Tarreau7005b582008-11-13 17:18:59 -08002135/**************************************************/
2136/* device initialization */
2137/**************************************************/
2138
Willy Tarreau698b1512008-11-22 11:29:33 +01002139static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2140 void *unused)
2141{
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002142 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002143 switch (code) {
2144 case SYS_DOWN:
2145 panel_lcd_print
2146 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2147 break;
2148 case SYS_HALT:
2149 panel_lcd_print
2150 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2151 break;
2152 case SYS_POWER_OFF:
2153 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2154 break;
2155 default:
2156 break;
2157 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002158 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002159 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002160}
2161
2162static struct notifier_block panel_notifier = {
2163 panel_notify_sys,
2164 NULL,
2165 0
2166};
2167
Willy Tarreau698b1512008-11-22 11:29:33 +01002168static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002169{
Willy Tarreau698b1512008-11-22 11:29:33 +01002170 if (port->number != parport)
2171 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002172
Willy Tarreau698b1512008-11-22 11:29:33 +01002173 if (pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002174 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2175 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002176 return;
2177 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002178
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002179 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
Willy Tarreau698b1512008-11-22 11:29:33 +01002180 NULL,
2181 /*PARPORT_DEV_EXCL */
2182 0, (void *)&pprt);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002183 if (pprt == NULL) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002184 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2185 __func__, port->number, parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002186 return;
2187 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002188
Willy Tarreau698b1512008-11-22 11:29:33 +01002189 if (parport_claim(pprt)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002190 pr_err("could not claim access to parport%d. Aborting.\n",
2191 parport);
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002192 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002193 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002194
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002195 /* must init LCD first, just in case an IRQ from the keypad is
2196 * generated at keypad init
2197 */
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002198 if (lcd.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002199 lcd_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002200 if (misc_register(&lcd_dev))
2201 goto err_unreg_device;
Willy Tarreau698b1512008-11-22 11:29:33 +01002202 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002203
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002204 if (keypad.enabled) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002205 keypad_init();
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002206 if (misc_register(&keypad_dev))
2207 goto err_lcd_unreg;
Willy Tarreau698b1512008-11-22 11:29:33 +01002208 }
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002209 return;
2210
2211err_lcd_unreg:
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002212 if (lcd.enabled)
Kulikov Vasiliy10f3f5b2010-07-30 15:08:20 +04002213 misc_deregister(&lcd_dev);
2214err_unreg_device:
2215 parport_unregister_device(pprt);
2216 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002217}
2218
Willy Tarreau698b1512008-11-22 11:29:33 +01002219static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002220{
Willy Tarreau698b1512008-11-22 11:29:33 +01002221 if (port->number != parport)
2222 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002223
Willy Tarreau698b1512008-11-22 11:29:33 +01002224 if (!pprt) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002225 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2226 __func__, port->number, parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002227 return;
2228 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002229
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002230 if (keypad.enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002231 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002232 keypad_initialized = 0;
2233 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002234
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002235 if (lcd.enabled && lcd.initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002236 misc_deregister(&lcd_dev);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002237 lcd.initialized = false;
Peter Huewe0b0595b2009-09-29 01:22:40 +02002238 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002239
Willy Tarreau698b1512008-11-22 11:29:33 +01002240 parport_release(pprt);
2241 parport_unregister_device(pprt);
2242 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002243}
2244
2245static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002246 .name = "panel",
2247 .attach = panel_attach,
2248 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002249};
2250
2251/* init function */
Mariusz Gorskid9114762014-11-27 22:36:46 +01002252static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002253{
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002254 int selected_keypad_type = NOT_SET;
Willy Tarreau7005b582008-11-13 17:18:59 -08002255
Willy Tarreau698b1512008-11-22 11:29:33 +01002256 /* take care of an eventual profile */
2257 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002258 case PANEL_PROFILE_CUSTOM:
2259 /* custom profile */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002260 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2261 selected_lcd_type = DEFAULT_LCD_TYPE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002262 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002263 case PANEL_PROFILE_OLD:
2264 /* 8 bits, 2*16, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002265 selected_keypad_type = KEYPAD_TYPE_OLD;
2266 selected_lcd_type = LCD_TYPE_OLD;
2267
2268 /* TODO: This two are a little hacky, sort it out later */
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002269 if (lcd_width == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002270 lcd_width = 16;
Mariusz Gorski2d35bcf2014-11-27 22:36:48 +01002271 if (lcd_hwidth == NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01002272 lcd_hwidth = 16;
2273 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002274 case PANEL_PROFILE_NEW:
2275 /* serial, 2*16, new keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002276 selected_keypad_type = KEYPAD_TYPE_NEW;
2277 selected_lcd_type = LCD_TYPE_KS0074;
Willy Tarreau698b1512008-11-22 11:29:33 +01002278 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002279 case PANEL_PROFILE_HANTRONIX:
2280 /* 8 bits, 2*16 hantronix-like, no keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002281 selected_keypad_type = KEYPAD_TYPE_NONE;
2282 selected_lcd_type = LCD_TYPE_HANTRONIX;
Willy Tarreau698b1512008-11-22 11:29:33 +01002283 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002284 case PANEL_PROFILE_NEXCOM:
2285 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002286 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2287 selected_lcd_type = LCD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002288 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002289 case PANEL_PROFILE_LARGE:
2290 /* 8 bits, 2*40, old keypad */
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002291 selected_keypad_type = KEYPAD_TYPE_OLD;
2292 selected_lcd_type = LCD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002293 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002294 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002295
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002296 /*
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01002297 * Init lcd struct with load-time values to preserve exact current
2298 * functionality (at least for now).
2299 */
2300 lcd.height = lcd_height;
2301 lcd.width = lcd_width;
2302 lcd.bwidth = lcd_bwidth;
2303 lcd.hwidth = lcd_hwidth;
2304 lcd.charset = lcd_charset;
2305 lcd.proto = lcd_proto;
2306 lcd.pins.e = lcd_e_pin;
2307 lcd.pins.rs = lcd_rs_pin;
2308 lcd.pins.rw = lcd_rw_pin;
2309 lcd.pins.cl = lcd_cl_pin;
2310 lcd.pins.da = lcd_da_pin;
2311 lcd.pins.bl = lcd_bl_pin;
2312
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002313 /* Leave it for now, just in case */
2314 lcd.esc_seq.len = -1;
2315
Mariusz Gorski8037e2a2014-11-27 22:36:51 +01002316 /*
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002317 * Overwrite selection with module param values (both keypad and lcd),
2318 * where the deprecated params have lower prio.
2319 */
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002320 if (keypad_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002321 selected_keypad_type = keypad_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002322 if (keypad_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002323 selected_keypad_type = keypad_type;
Willy Tarreau7005b582008-11-13 17:18:59 -08002324
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002325 keypad.enabled = (selected_keypad_type > 0);
2326
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002327 if (lcd_enabled != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002328 selected_lcd_type = lcd_enabled;
Mariusz Gorski1a4b2e32014-11-27 22:36:52 +01002329 if (lcd_type != NOT_SET)
Mariusz Gorski87b8e0c2014-11-27 22:36:50 +01002330 selected_lcd_type = lcd_type;
2331
2332 lcd.enabled = (selected_lcd_type > 0);
2333
2334 switch (selected_keypad_type) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002335 case KEYPAD_TYPE_OLD:
2336 keypad_profile = old_keypad_profile;
2337 break;
2338 case KEYPAD_TYPE_NEW:
2339 keypad_profile = new_keypad_profile;
2340 break;
2341 case KEYPAD_TYPE_NEXCOM:
2342 keypad_profile = nexcom_keypad_profile;
2343 break;
2344 default:
2345 keypad_profile = NULL;
2346 break;
2347 }
2348
2349 /* tells various subsystems about the fact that we are initializing */
2350 init_in_progress = 1;
2351
2352 if (parport_register_driver(&panel_driver)) {
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002353 pr_err("could not register with parport. Aborting.\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002354 return -EIO;
2355 }
2356
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002357 if (!lcd.enabled && !keypad.enabled) {
Willy Tarreau630231772008-11-22 12:52:18 +01002358 /* no device enabled, let's release the parport */
Willy Tarreau698b1512008-11-22 11:29:33 +01002359 if (pprt) {
2360 parport_release(pprt);
2361 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002362 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002363 }
2364 parport_unregister_driver(&panel_driver);
Toshiaki Yamaneeb073a92012-07-12 22:01:17 +09002365 pr_err("driver version " PANEL_VERSION " disabled.\n");
Willy Tarreau698b1512008-11-22 11:29:33 +01002366 return -ENODEV;
2367 }
2368
2369 register_reboot_notifier(&panel_notifier);
2370
2371 if (pprt)
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002372 pr_info("driver version " PANEL_VERSION
2373 " registered on parport%d (io=0x%lx).\n", parport,
2374 pprt->port->base);
Willy Tarreau698b1512008-11-22 11:29:33 +01002375 else
Toshiaki Yamane493aa892012-07-12 22:01:00 +09002376 pr_info("driver version " PANEL_VERSION
2377 " not yet registered\n");
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002378 /* tells various subsystems about the fact that initialization
2379 is finished */
Willy Tarreau698b1512008-11-22 11:29:33 +01002380 init_in_progress = 0;
2381 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002382}
2383
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002384static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002385{
2386 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002387
Willy Tarreau698b1512008-11-22 11:29:33 +01002388 if (scan_timer.function != NULL)
Julia Lawalle112f892014-03-26 22:33:41 +01002389 del_timer_sync(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002390
Costantino Leandro57898132009-02-17 11:10:48 -05002391 if (pprt != NULL) {
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002392 if (keypad.enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002393 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002394 keypad_initialized = 0;
2395 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002396
Mariusz Gorskia8b25802014-11-27 22:36:49 +01002397 if (lcd.enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002398 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2399 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2400 misc_deregister(&lcd_dev);
Mariusz Gorski6d8b5882014-11-27 22:36:53 +01002401 lcd.initialized = false;
Costantino Leandro57898132009-02-17 11:10:48 -05002402 }
2403
2404 /* TODO: free all input signals */
2405 parport_release(pprt);
2406 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002407 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002408 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002409 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002410}
Willy Tarreau7005b582008-11-13 17:18:59 -08002411
Willy Tarreau7005b582008-11-13 17:18:59 -08002412module_init(panel_init_module);
2413module_exit(panel_cleanup_module);
2414MODULE_AUTHOR("Willy Tarreau");
2415MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002416
2417/*
2418 * Local variables:
2419 * c-indent-level: 4
2420 * tab-width: 8
2421 * End:
2422 */