blob: 8bd7182195b399c82300d5f8e52afe8e42088fc4 [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
37#include <linux/module.h>
38
39#include <linux/types.h>
40#include <linux/errno.h>
41#include <linux/signal.h>
42#include <linux/sched.h>
43#include <linux/spinlock.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080044#include <linux/interrupt.h>
45#include <linux/miscdevice.h>
Willy Tarreau698b1512008-11-22 11:29:33 +010046#include <linux/slab.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080047#include <linux/ioport.h>
48#include <linux/fcntl.h>
49#include <linux/init.h>
50#include <linux/delay.h>
51#include <linux/ctype.h>
52#include <linux/parport.h>
53#include <linux/version.h>
54#include <linux/list.h>
55#include <linux/notifier.h>
56#include <linux/reboot.h>
Sam Ravnborg273b2812009-10-18 00:52:28 +020057#include <generated/utsrelease.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080058
Willy Tarreau698b1512008-11-22 11:29:33 +010059#include <linux/io.h>
Takanori Suzuki48f658b2010-05-08 22:56:24 +090060#include <linux/uaccess.h>
Willy Tarreau7005b582008-11-13 17:18:59 -080061#include <asm/system.h>
62
Willy Tarreau7005b582008-11-13 17:18:59 -080063#define LCD_MINOR 156
64#define KEYPAD_MINOR 185
Willy Tarreau7005b582008-11-13 17:18:59 -080065
66#define PANEL_VERSION "0.9.5"
67
68#define LCD_MAXBYTES 256 /* max burst write */
69
Willy Tarreau7005b582008-11-13 17:18:59 -080070#define KEYPAD_BUFFER 64
Willy Tarreau7005b582008-11-13 17:18:59 -080071
Henri Häkkinen429ccf02010-06-12 00:27:36 +030072/* poll the keyboard this every second */
73#define INPUT_POLL_TIME (HZ/50)
74/* a key starts to repeat after this times INPUT_POLL_TIME */
75#define KEYPAD_REP_START (10)
76/* a key repeats this times INPUT_POLL_TIME */
77#define KEYPAD_REP_DELAY (2)
78
79/* keep the light on this times INPUT_POLL_TIME for each flash */
80#define FLASH_LIGHT_TEMPO (200)
Willy Tarreau7005b582008-11-13 17:18:59 -080081
82/* converts an r_str() input to an active high, bits string : 000BAOSE */
83#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
84
Willy Tarreau698b1512008-11-22 11:29:33 +010085#define PNL_PBUSY 0x80 /* inverted input, active low */
86#define PNL_PACK 0x40 /* direct input, active low */
87#define PNL_POUTPA 0x20 /* direct input, active high */
88#define PNL_PSELECD 0x10 /* direct input, active high */
89#define PNL_PERRORP 0x08 /* direct input, active low */
Willy Tarreau7005b582008-11-13 17:18:59 -080090
Willy Tarreau698b1512008-11-22 11:29:33 +010091#define PNL_PBIDIR 0x20 /* bi-directional ports */
Henri Häkkinen429ccf02010-06-12 00:27:36 +030092/* high to read data in or-ed with data out */
93#define PNL_PINTEN 0x10
Willy Tarreau698b1512008-11-22 11:29:33 +010094#define PNL_PSELECP 0x08 /* inverted output, active low */
95#define PNL_PINITP 0x04 /* direct output, active low */
96#define PNL_PAUTOLF 0x02 /* inverted output, active low */
97#define PNL_PSTROBE 0x01 /* inverted output */
Willy Tarreau7005b582008-11-13 17:18:59 -080098
99#define PNL_PD0 0x01
100#define PNL_PD1 0x02
101#define PNL_PD2 0x04
102#define PNL_PD3 0x08
103#define PNL_PD4 0x10
104#define PNL_PD5 0x20
105#define PNL_PD6 0x40
106#define PNL_PD7 0x80
107
108#define PIN_NONE 0
109#define PIN_STROBE 1
110#define PIN_D0 2
111#define PIN_D1 3
112#define PIN_D2 4
113#define PIN_D3 5
114#define PIN_D4 6
115#define PIN_D5 7
116#define PIN_D6 8
117#define PIN_D7 9
118#define PIN_AUTOLF 14
119#define PIN_INITP 16
120#define PIN_SELECP 17
121#define PIN_NOT_SET 127
122
Willy Tarreau7005b582008-11-13 17:18:59 -0800123#define LCD_FLAG_S 0x0001
124#define LCD_FLAG_ID 0x0002
125#define LCD_FLAG_B 0x0004 /* blink on */
126#define LCD_FLAG_C 0x0008 /* cursor on */
127#define LCD_FLAG_D 0x0010 /* display on */
128#define LCD_FLAG_F 0x0020 /* large font mode */
129#define LCD_FLAG_N 0x0040 /* 2-rows mode */
130#define LCD_FLAG_L 0x0080 /* backlight enabled */
131
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300132#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
Willy Tarreau7005b582008-11-13 17:18:59 -0800133#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
134
135/* macros to simplify use of the parallel port */
136#define r_ctr(x) (parport_read_control((x)->port))
137#define r_dtr(x) (parport_read_data((x)->port))
138#define r_str(x) (parport_read_status((x)->port))
Willy Tarreau698b1512008-11-22 11:29:33 +0100139#define w_ctr(x, y) do { parport_write_control((x)->port, (y)); } while (0)
140#define w_dtr(x, y) do { parport_write_data((x)->port, (y)); } while (0)
Willy Tarreau7005b582008-11-13 17:18:59 -0800141
142/* this defines which bits are to be used and which ones to be ignored */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300143/* logical or of the output bits involved in the scan matrix */
144static __u8 scan_mask_o;
145/* logical or of the input bits involved in the scan matrix */
146static __u8 scan_mask_i;
Willy Tarreau7005b582008-11-13 17:18:59 -0800147
Willy Tarreau698b1512008-11-22 11:29:33 +0100148typedef __u64 pmask_t;
Willy Tarreau7005b582008-11-13 17:18:59 -0800149
150enum input_type {
Willy Tarreau698b1512008-11-22 11:29:33 +0100151 INPUT_TYPE_STD,
152 INPUT_TYPE_KBD,
Willy Tarreau7005b582008-11-13 17:18:59 -0800153};
154
155enum input_state {
Willy Tarreau698b1512008-11-22 11:29:33 +0100156 INPUT_ST_LOW,
157 INPUT_ST_RISING,
158 INPUT_ST_HIGH,
159 INPUT_ST_FALLING,
Willy Tarreau7005b582008-11-13 17:18:59 -0800160};
161
162struct logical_input {
Willy Tarreau698b1512008-11-22 11:29:33 +0100163 struct list_head list;
164 pmask_t mask;
165 pmask_t value;
166 enum input_type type;
167 enum input_state state;
168 __u8 rise_time, fall_time;
169 __u8 rise_timer, fall_timer, high_timer;
Willy Tarreau7005b582008-11-13 17:18:59 -0800170
Willy Tarreau698b1512008-11-22 11:29:33 +0100171 union {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300172 struct { /* valid when type == INPUT_TYPE_STD */
Willy Tarreau698b1512008-11-22 11:29:33 +0100173 void (*press_fct) (int);
174 void (*release_fct) (int);
175 int press_data;
176 int release_data;
177 } std;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300178 struct { /* valid when type == INPUT_TYPE_KBD */
179 /* strings can be non null-terminated */
Willy Tarreau698b1512008-11-22 11:29:33 +0100180 char press_str[sizeof(void *) + sizeof(int)];
181 char repeat_str[sizeof(void *) + sizeof(int)];
182 char release_str[sizeof(void *) + sizeof(int)];
183 } kbd;
184 } u;
Willy Tarreau7005b582008-11-13 17:18:59 -0800185};
186
Willy Tarreau698b1512008-11-22 11:29:33 +0100187LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
Willy Tarreau7005b582008-11-13 17:18:59 -0800188
189/* physical contacts history
190 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
191 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
192 * corresponds to the ground.
193 * Within each group, bits are stored in the same order as read on the port :
194 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
195 * So, each __u64 (or pmask_t) is represented like this :
196 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
197 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
198 */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300199
200/* what has just been read from the I/O ports */
201static pmask_t phys_read;
202/* previous phys_read */
203static pmask_t phys_read_prev;
204/* stabilized phys_read (phys_read|phys_read_prev) */
205static pmask_t phys_curr;
206/* previous phys_curr */
207static pmask_t phys_prev;
208/* 0 means that at least one logical signal needs be computed */
209static char inputs_stable;
Willy Tarreau7005b582008-11-13 17:18:59 -0800210
Willy Tarreau7005b582008-11-13 17:18:59 -0800211/* these variables are specific to the keypad */
212static char keypad_buffer[KEYPAD_BUFFER];
Willy Tarreau698b1512008-11-22 11:29:33 +0100213static int keypad_buflen;
214static int keypad_start;
215static char keypressed;
Willy Tarreau7005b582008-11-13 17:18:59 -0800216static wait_queue_head_t keypad_read_wait;
Willy Tarreau7005b582008-11-13 17:18:59 -0800217
218/* lcd-specific variables */
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300219
220/* contains the LCD config state */
221static unsigned long int lcd_flags;
222/* contains the LCD X offset */
223static unsigned long int lcd_addr_x;
224/* contains the LCD Y offset */
225static unsigned long int lcd_addr_y;
226/* current escape sequence, 0 terminated */
227static char lcd_escape[LCD_ESCAPE_LEN + 1];
228/* not in escape state. >=0 = escape cmd len */
229static int lcd_escape_len = -1;
Willy Tarreau7005b582008-11-13 17:18:59 -0800230
Willy Tarreau7005b582008-11-13 17:18:59 -0800231/*
232 * Bit masks to convert LCD signals to parallel port outputs.
233 * _d_ are values for data port, _c_ are for control port.
234 * [0] = signal OFF, [1] = signal ON, [2] = mask
235 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100236#define BIT_CLR 0
237#define BIT_SET 1
238#define BIT_MSK 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800239#define BIT_STATES 3
240/*
241 * one entry for each bit on the LCD
242 */
243#define LCD_BIT_E 0
244#define LCD_BIT_RS 1
245#define LCD_BIT_RW 2
246#define LCD_BIT_BL 3
247#define LCD_BIT_CL 4
248#define LCD_BIT_DA 5
249#define LCD_BITS 6
250
251/*
252 * each bit can be either connected to a DATA or CTRL port
253 */
254#define LCD_PORT_C 0
255#define LCD_PORT_D 1
256#define LCD_PORTS 2
257
258static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
259
260/*
261 * LCD protocols
262 */
263#define LCD_PROTO_PARALLEL 0
264#define LCD_PROTO_SERIAL 1
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400265#define LCD_PROTO_TI_DA8XX_LCD 2
Willy Tarreau7005b582008-11-13 17:18:59 -0800266
267/*
268 * LCD character sets
269 */
270#define LCD_CHARSET_NORMAL 0
271#define LCD_CHARSET_KS0074 1
272
273/*
274 * LCD types
275 */
276#define LCD_TYPE_NONE 0
277#define LCD_TYPE_OLD 1
278#define LCD_TYPE_KS0074 2
279#define LCD_TYPE_HANTRONIX 3
280#define LCD_TYPE_NEXCOM 4
281#define LCD_TYPE_CUSTOM 5
282
283/*
284 * keypad types
285 */
286#define KEYPAD_TYPE_NONE 0
287#define KEYPAD_TYPE_OLD 1
288#define KEYPAD_TYPE_NEW 2
289#define KEYPAD_TYPE_NEXCOM 3
290
291/*
292 * panel profiles
293 */
294#define PANEL_PROFILE_CUSTOM 0
295#define PANEL_PROFILE_OLD 1
296#define PANEL_PROFILE_NEW 2
297#define PANEL_PROFILE_HANTRONIX 3
298#define PANEL_PROFILE_NEXCOM 4
299#define PANEL_PROFILE_LARGE 5
300
301/*
302 * Construct custom config from the kernel's configuration
303 */
304#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
305#define DEFAULT_PARPORT 0
306#define DEFAULT_LCD LCD_TYPE_OLD
307#define DEFAULT_KEYPAD KEYPAD_TYPE_OLD
Willy Tarreau7005b582008-11-13 17:18:59 -0800308#define DEFAULT_LCD_WIDTH 40
309#define DEFAULT_LCD_BWIDTH 40
310#define DEFAULT_LCD_HWIDTH 64
311#define DEFAULT_LCD_HEIGHT 2
312#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
313
314#define DEFAULT_LCD_PIN_E PIN_AUTOLF
315#define DEFAULT_LCD_PIN_RS PIN_SELECP
316#define DEFAULT_LCD_PIN_RW PIN_INITP
317#define DEFAULT_LCD_PIN_SCL PIN_STROBE
318#define DEFAULT_LCD_PIN_SDA PIN_D0
319#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
320#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
321
322#ifdef CONFIG_PANEL_PROFILE
323#undef DEFAULT_PROFILE
324#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
325#endif
326
327#ifdef CONFIG_PANEL_PARPORT
328#undef DEFAULT_PARPORT
329#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
330#endif
331
Willy Tarreau698b1512008-11-22 11:29:33 +0100332#if DEFAULT_PROFILE == 0 /* custom */
Willy Tarreau7005b582008-11-13 17:18:59 -0800333#ifdef CONFIG_PANEL_KEYPAD
334#undef DEFAULT_KEYPAD
335#define DEFAULT_KEYPAD CONFIG_PANEL_KEYPAD
336#endif
337
Willy Tarreau7005b582008-11-13 17:18:59 -0800338#ifdef CONFIG_PANEL_LCD
339#undef DEFAULT_LCD
340#define DEFAULT_LCD CONFIG_PANEL_LCD
341#endif
342
343#ifdef CONFIG_PANEL_LCD_WIDTH
344#undef DEFAULT_LCD_WIDTH
345#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
346#endif
347
348#ifdef CONFIG_PANEL_LCD_BWIDTH
349#undef DEFAULT_LCD_BWIDTH
350#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
351#endif
352
353#ifdef CONFIG_PANEL_LCD_HWIDTH
354#undef DEFAULT_LCD_HWIDTH
355#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
356#endif
357
358#ifdef CONFIG_PANEL_LCD_HEIGHT
359#undef DEFAULT_LCD_HEIGHT
360#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
361#endif
362
363#ifdef CONFIG_PANEL_LCD_PROTO
364#undef DEFAULT_LCD_PROTO
365#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
366#endif
367
368#ifdef CONFIG_PANEL_LCD_PIN_E
369#undef DEFAULT_LCD_PIN_E
370#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
371#endif
372
373#ifdef CONFIG_PANEL_LCD_PIN_RS
374#undef DEFAULT_LCD_PIN_RS
375#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
376#endif
377
378#ifdef CONFIG_PANEL_LCD_PIN_RW
379#undef DEFAULT_LCD_PIN_RW
380#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
381#endif
382
383#ifdef CONFIG_PANEL_LCD_PIN_SCL
384#undef DEFAULT_LCD_PIN_SCL
385#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
386#endif
387
388#ifdef CONFIG_PANEL_LCD_PIN_SDA
389#undef DEFAULT_LCD_PIN_SDA
390#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
391#endif
392
393#ifdef CONFIG_PANEL_LCD_PIN_BL
394#undef DEFAULT_LCD_PIN_BL
395#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
396#endif
397
398#ifdef CONFIG_PANEL_LCD_CHARSET
399#undef DEFAULT_LCD_CHARSET
Peter Huewea6a6c902009-12-15 06:21:45 +0100400#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
Willy Tarreau7005b582008-11-13 17:18:59 -0800401#endif
402
403#endif /* DEFAULT_PROFILE == 0 */
404
405/* global variables */
Willy Tarreau698b1512008-11-22 11:29:33 +0100406static int keypad_open_cnt; /* #times opened */
407static int lcd_open_cnt; /* #times opened */
Willy Tarreau698b1512008-11-22 11:29:33 +0100408static struct pardevice *pprt;
Willy Tarreau7005b582008-11-13 17:18:59 -0800409
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100410static int lcd_initialized;
411static int keypad_initialized;
Willy Tarreau7005b582008-11-13 17:18:59 -0800412
Willy Tarreau698b1512008-11-22 11:29:33 +0100413static int light_tempo;
Willy Tarreau7005b582008-11-13 17:18:59 -0800414
Willy Tarreau698b1512008-11-22 11:29:33 +0100415static char lcd_must_clear;
416static char lcd_left_shift;
417static char init_in_progress;
Willy Tarreau7005b582008-11-13 17:18:59 -0800418
Willy Tarreau698b1512008-11-22 11:29:33 +0100419static void (*lcd_write_cmd) (int);
420static void (*lcd_write_data) (int);
421static void (*lcd_clear_fast) (void);
Willy Tarreau7005b582008-11-13 17:18:59 -0800422
Willy Tarreau698b1512008-11-22 11:29:33 +0100423static DEFINE_SPINLOCK(pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800424static struct timer_list scan_timer;
425
Willy Tarreau630231772008-11-22 12:52:18 +0100426MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100427
428static int parport = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100429module_param(parport, int, 0000);
430MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100431
432static int lcd_height = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100433module_param(lcd_height, int, 0000);
434MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100435
436static int lcd_width = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100437module_param(lcd_width, int, 0000);
438MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100439
440static int lcd_bwidth = -1; /* internal buffer width (usually 40) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100441module_param(lcd_bwidth, int, 0000);
442MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100443
444static int lcd_hwidth = -1; /* hardware buffer width (usually 64) */
Willy Tarreau698b1512008-11-22 11:29:33 +0100445module_param(lcd_hwidth, int, 0000);
446MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100447
448static int lcd_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100449module_param(lcd_enabled, int, 0000);
450MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100451
452static int keypad_enabled = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100453module_param(keypad_enabled, int, 0000);
454MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100455
456static int lcd_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100457module_param(lcd_type, int, 0000);
458MODULE_PARM_DESC(lcd_type,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300459 "LCD type: 0=none, 1=old //, 2=serial ks0074, "
460 "3=hantronix //, 4=nexcom //, 5=compiled-in");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100461
462static int lcd_proto = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100463module_param(lcd_proto, int, 0000);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300464MODULE_PARM_DESC(lcd_proto,
465 "LCD communication: 0=parallel (//), 1=serial,"
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400466 "2=TI LCD Interface");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100467
468static int lcd_charset = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100469module_param(lcd_charset, int, 0000);
470MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100471
472static int keypad_type = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +0100473module_param(keypad_type, int, 0000);
474MODULE_PARM_DESC(keypad_type,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300475 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, "
476 "3=nexcom 4 keys");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100477
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100478static int profile = DEFAULT_PROFILE;
Willy Tarreau698b1512008-11-22 11:29:33 +0100479module_param(profile, int, 0000);
480MODULE_PARM_DESC(profile,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300481 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
482 "4=16x2 nexcom; default=40x2, old kp");
Willy Tarreau7005b582008-11-13 17:18:59 -0800483
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100484/*
485 * These are the parallel port pins the LCD control signals are connected to.
486 * Set this to 0 if the signal is not used. Set it to its opposite value
487 * (negative) if the signal is negated. -MAXINT is used to indicate that the
488 * pin has not been explicitly specified.
489 *
Willy Tarreau630231772008-11-22 12:52:18 +0100490 * WARNING! no check will be performed about collisions with keypad !
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100491 */
492
493static int lcd_e_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100494module_param(lcd_e_pin, int, 0000);
495MODULE_PARM_DESC(lcd_e_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300496 "# of the // port pin connected to LCD 'E' signal, "
497 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100498
499static int lcd_rs_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100500module_param(lcd_rs_pin, int, 0000);
501MODULE_PARM_DESC(lcd_rs_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300502 "# of the // port pin connected to LCD 'RS' signal, "
503 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100504
505static int lcd_rw_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100506module_param(lcd_rw_pin, int, 0000);
507MODULE_PARM_DESC(lcd_rw_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300508 "# of the // port pin connected to LCD 'RW' signal, "
509 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100510
511static int lcd_bl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100512module_param(lcd_bl_pin, int, 0000);
513MODULE_PARM_DESC(lcd_bl_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300514 "# of the // port pin connected to LCD backlight, "
515 "with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100516
517static int lcd_da_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100518module_param(lcd_da_pin, int, 0000);
519MODULE_PARM_DESC(lcd_da_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300520 "# of the // port pin connected to serial LCD 'SDA' "
521 "signal, with polarity (-17..17)");
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +0100522
523static int lcd_cl_pin = PIN_NOT_SET;
Willy Tarreau698b1512008-11-22 11:29:33 +0100524module_param(lcd_cl_pin, int, 0000);
525MODULE_PARM_DESC(lcd_cl_pin,
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300526 "# of the // port pin connected to serial LCD 'SCL' "
527 "signal, with polarity (-17..17)");
Willy Tarreau7005b582008-11-13 17:18:59 -0800528
Willy Tarreau698b1512008-11-22 11:29:33 +0100529static unsigned char *lcd_char_conv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800530
531/* for some LCD drivers (ks0074) we need a charset conversion table. */
532static unsigned char lcd_char_conv_ks0074[256] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100533 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
534 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
535 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
536 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
537 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
538 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
539 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
540 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
541 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
542 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
543 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
544 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
545 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
546 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
547 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
548 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
549 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
550 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
551 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
552 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
553 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
554 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
555 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
556 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
557 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
558 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
559 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
560 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
561 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
562 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
563 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
564 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
565 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
Willy Tarreau7005b582008-11-13 17:18:59 -0800566};
567
568char old_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100569 {"S0", "Left\n", "Left\n", ""},
570 {"S1", "Down\n", "Down\n", ""},
571 {"S2", "Up\n", "Up\n", ""},
572 {"S3", "Right\n", "Right\n", ""},
573 {"S4", "Esc\n", "Esc\n", ""},
574 {"S5", "Ret\n", "Ret\n", ""},
575 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800576};
577
578/* signals, press, repeat, release */
579char new_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100580 {"S0", "Left\n", "Left\n", ""},
581 {"S1", "Down\n", "Down\n", ""},
582 {"S2", "Up\n", "Up\n", ""},
583 {"S3", "Right\n", "Right\n", ""},
584 {"S4s5", "", "Esc\n", "Esc\n"},
585 {"s4S5", "", "Ret\n", "Ret\n"},
586 {"S4S5", "Help\n", "", ""},
587 /* add new signals above this line */
588 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800589};
590
591/* signals, press, repeat, release */
592char nexcom_keypad_profile[][4][9] = {
Willy Tarreau698b1512008-11-22 11:29:33 +0100593 {"a-p-e-", "Down\n", "Down\n", ""},
594 {"a-p-E-", "Ret\n", "Ret\n", ""},
595 {"a-P-E-", "Esc\n", "Esc\n", ""},
596 {"a-P-e-", "Up\n", "Up\n", ""},
597 /* add new signals above this line */
598 {"", "", "", ""}
Willy Tarreau7005b582008-11-13 17:18:59 -0800599};
600
601static char (*keypad_profile)[4][9] = old_keypad_profile;
602
603/* FIXME: this should be converted to a bit array containing signals states */
604static struct {
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300605 unsigned char e; /* parallel LCD E (data latch on falling edge) */
606 unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */
607 unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */
608 unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */
609 unsigned char cl; /* serial LCD clock (latch on rising edge) */
610 unsigned char da; /* serial LCD data */
Willy Tarreau7005b582008-11-13 17:18:59 -0800611} bits;
612
613static void init_scan_timer(void);
614
615/* sets data port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100616static int set_data_bits(void)
617{
618 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800619
Willy Tarreau698b1512008-11-22 11:29:33 +0100620 val = r_dtr(pprt);
621 for (bit = 0; bit < LCD_BITS; bit++)
622 val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800623
Willy Tarreau698b1512008-11-22 11:29:33 +0100624 val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e]
625 | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs]
626 | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw]
627 | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl]
628 | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl]
629 | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800630
Willy Tarreau698b1512008-11-22 11:29:33 +0100631 w_dtr(pprt, val);
632 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800633}
634
635/* sets ctrl port bits according to current signals values */
Willy Tarreau698b1512008-11-22 11:29:33 +0100636static int set_ctrl_bits(void)
637{
638 int val, bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800639
Willy Tarreau698b1512008-11-22 11:29:33 +0100640 val = r_ctr(pprt);
641 for (bit = 0; bit < LCD_BITS; bit++)
642 val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK];
Willy Tarreau7005b582008-11-13 17:18:59 -0800643
Willy Tarreau698b1512008-11-22 11:29:33 +0100644 val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e]
645 | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs]
646 | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw]
647 | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl]
648 | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl]
649 | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da];
Willy Tarreau7005b582008-11-13 17:18:59 -0800650
Willy Tarreau698b1512008-11-22 11:29:33 +0100651 w_ctr(pprt, val);
652 return val;
Willy Tarreau7005b582008-11-13 17:18:59 -0800653}
654
655/* sets ctrl & data port bits according to current signals values */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530656static void panel_set_bits(void)
Willy Tarreau698b1512008-11-22 11:29:33 +0100657{
658 set_data_bits();
659 set_ctrl_bits();
Willy Tarreau7005b582008-11-13 17:18:59 -0800660}
661
662/*
663 * Converts a parallel port pin (from -25 to 25) to data and control ports
664 * masks, and data and control port bits. The signal will be considered
665 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
666 *
667 * Result will be used this way :
668 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
669 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
670 */
Willy Tarreau698b1512008-11-22 11:29:33 +0100671void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
672{
673 int d_bit, c_bit, inv;
Willy Tarreau7005b582008-11-13 17:18:59 -0800674
Willy Tarreau698b1512008-11-22 11:29:33 +0100675 d_val[0] = c_val[0] = d_val[1] = c_val[1] = 0;
676 d_val[2] = c_val[2] = 0xFF;
Willy Tarreau7005b582008-11-13 17:18:59 -0800677
Willy Tarreau698b1512008-11-22 11:29:33 +0100678 if (pin == 0)
679 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800680
Willy Tarreau698b1512008-11-22 11:29:33 +0100681 inv = (pin < 0);
682 if (inv)
683 pin = -pin;
Willy Tarreau7005b582008-11-13 17:18:59 -0800684
Willy Tarreau698b1512008-11-22 11:29:33 +0100685 d_bit = c_bit = 0;
Willy Tarreau7005b582008-11-13 17:18:59 -0800686
Willy Tarreau698b1512008-11-22 11:29:33 +0100687 switch (pin) {
688 case PIN_STROBE: /* strobe, inverted */
689 c_bit = PNL_PSTROBE;
690 inv = !inv;
691 break;
692 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
693 d_bit = 1 << (pin - 2);
694 break;
695 case PIN_AUTOLF: /* autofeed, inverted */
696 c_bit = PNL_PAUTOLF;
697 inv = !inv;
698 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300699 case PIN_INITP: /* init, direct */
Willy Tarreau698b1512008-11-22 11:29:33 +0100700 c_bit = PNL_PINITP;
701 break;
702 case PIN_SELECP: /* select_in, inverted */
703 c_bit = PNL_PSELECP;
704 inv = !inv;
705 break;
706 default: /* unknown pin, ignore */
707 break;
708 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800709
Willy Tarreau698b1512008-11-22 11:29:33 +0100710 if (c_bit) {
711 c_val[2] &= ~c_bit;
712 c_val[!inv] = c_bit;
713 } else if (d_bit) {
714 d_val[2] &= ~d_bit;
715 d_val[!inv] = d_bit;
716 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800717}
718
719/* sleeps that many milliseconds with a reschedule */
Willy Tarreau698b1512008-11-22 11:29:33 +0100720static void long_sleep(int ms)
721{
Willy Tarreau7005b582008-11-13 17:18:59 -0800722
Willy Tarreau698b1512008-11-22 11:29:33 +0100723 if (in_interrupt())
724 mdelay(ms);
725 else {
726 current->state = TASK_INTERRUPTIBLE;
727 schedule_timeout((ms * HZ + 999) / 1000);
728 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800729}
730
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300731/* send a serial byte to the LCD panel. The caller is responsible for locking
732 if needed. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100733static void lcd_send_serial(int byte)
734{
735 int bit;
Willy Tarreau7005b582008-11-13 17:18:59 -0800736
Willy Tarreau698b1512008-11-22 11:29:33 +0100737 /* the data bit is set on D0, and the clock on STROBE.
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300738 * LCD reads D0 on STROBE's rising edge. */
Willy Tarreau698b1512008-11-22 11:29:33 +0100739 for (bit = 0; bit < 8; bit++) {
740 bits.cl = BIT_CLR; /* CLK low */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530741 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100742 bits.da = byte & 1;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530743 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300744 udelay(2); /* maintain the data during 2 us before CLK up */
Willy Tarreau698b1512008-11-22 11:29:33 +0100745 bits.cl = BIT_SET; /* CLK high */
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530746 panel_set_bits();
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300747 udelay(1); /* maintain the strobe during 1 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100748 byte >>= 1;
749 }
Willy Tarreau7005b582008-11-13 17:18:59 -0800750}
751
752/* turn the backlight on or off */
Willy Tarreau698b1512008-11-22 11:29:33 +0100753static void lcd_backlight(int on)
754{
755 if (lcd_bl_pin == PIN_NONE)
756 return;
Willy Tarreau7005b582008-11-13 17:18:59 -0800757
Willy Tarreau698b1512008-11-22 11:29:33 +0100758 /* The backlight is activated by seting the AUTOFEED line to +5V */
759 spin_lock(&pprt_lock);
760 bits.bl = on;
Sachin P. Sant6136ac82009-02-03 21:10:58 +0530761 panel_set_bits();
Willy Tarreau698b1512008-11-22 11:29:33 +0100762 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800763}
764
765/* send a command to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100766static void lcd_write_cmd_s(int cmd)
767{
768 spin_lock(&pprt_lock);
769 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
770 lcd_send_serial(cmd & 0x0F);
771 lcd_send_serial((cmd >> 4) & 0x0F);
772 udelay(40); /* the shortest command takes at least 40 us */
773 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800774}
775
776/* send data to the LCD panel in serial mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100777static void lcd_write_data_s(int data)
778{
779 spin_lock(&pprt_lock);
780 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
781 lcd_send_serial(data & 0x0F);
782 lcd_send_serial((data >> 4) & 0x0F);
783 udelay(40); /* the shortest data takes at least 40 us */
784 spin_unlock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800785}
786
787/* send a command to the LCD panel in 8 bits parallel mode */
Willy Tarreau698b1512008-11-22 11:29:33 +0100788static void lcd_write_cmd_p8(int cmd)
789{
790 spin_lock(&pprt_lock);
Willy Tarreau7005b582008-11-13 17:18:59 -0800791 /* present the data to the data port */
Willy Tarreau698b1512008-11-22 11:29:33 +0100792 w_dtr(pprt, cmd);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300793 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau7005b582008-11-13 17:18:59 -0800794
Willy Tarreau698b1512008-11-22 11:29:33 +0100795 bits.e = BIT_SET;
796 bits.rs = BIT_CLR;
797 bits.rw = BIT_CLR;
Willy Tarreau7005b582008-11-13 17:18:59 -0800798 set_ctrl_bits();
799
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300800 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau7005b582008-11-13 17:18:59 -0800801
802 bits.e = BIT_CLR;
803 set_ctrl_bits();
804
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300805 udelay(120); /* the shortest command takes at least 120 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100806 spin_unlock(&pprt_lock);
807}
Willy Tarreau7005b582008-11-13 17:18:59 -0800808
Willy Tarreau698b1512008-11-22 11:29:33 +0100809/* send data to the LCD panel in 8 bits parallel mode */
810static void lcd_write_data_p8(int data)
811{
812 spin_lock(&pprt_lock);
813 /* present the data to the data port */
814 w_dtr(pprt, data);
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300815 udelay(20); /* maintain the data during 20 us before the strobe */
Willy Tarreau698b1512008-11-22 11:29:33 +0100816
817 bits.e = BIT_SET;
818 bits.rs = BIT_SET;
819 bits.rw = BIT_CLR;
820 set_ctrl_bits();
821
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300822 udelay(40); /* maintain the strobe during 40 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100823
824 bits.e = BIT_CLR;
825 set_ctrl_bits();
826
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300827 udelay(45); /* the shortest data takes at least 45 us */
Willy Tarreau698b1512008-11-22 11:29:33 +0100828 spin_unlock(&pprt_lock);
829}
830
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400831/* send a command to the TI LCD panel */
832static void lcd_write_cmd_tilcd(int cmd)
833{
834 spin_lock(&pprt_lock);
835 /* present the data to the control port */
836 w_ctr(pprt, cmd);
837 udelay(60);
838 spin_unlock(&pprt_lock);
839}
840
841/* send data to the TI LCD panel */
842static void lcd_write_data_tilcd(int data)
843{
844 spin_lock(&pprt_lock);
845 /* present the data to the data port */
846 w_dtr(pprt, data);
847 udelay(60);
848 spin_unlock(&pprt_lock);
849}
850
Willy Tarreau698b1512008-11-22 11:29:33 +0100851static void lcd_gotoxy(void)
852{
853 lcd_write_cmd(0x80 /* set DDRAM address */
854 | (lcd_addr_y ? lcd_hwidth : 0)
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300855 /* we force the cursor to stay at the end of the
856 line if it wants to go farther */
Willy Tarreau698b1512008-11-22 11:29:33 +0100857 | ((lcd_addr_x < lcd_bwidth) ? lcd_addr_x &
858 (lcd_hwidth - 1) : lcd_bwidth - 1));
859}
860
861static void lcd_print(char c)
862{
863 if (lcd_addr_x < lcd_bwidth) {
864 if (lcd_char_conv != NULL)
865 c = lcd_char_conv[(unsigned char)c];
866 lcd_write_data(c);
867 lcd_addr_x++;
868 }
869 /* prevents the cursor from wrapping onto the next line */
870 if (lcd_addr_x == lcd_bwidth)
871 lcd_gotoxy();
872}
873
874/* fills the display with spaces and resets X/Y */
875static void lcd_clear_fast_s(void)
876{
877 int pos;
878 lcd_addr_x = lcd_addr_y = 0;
879 lcd_gotoxy();
880
881 spin_lock(&pprt_lock);
882 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
883 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
884 lcd_send_serial(' ' & 0x0F);
885 lcd_send_serial((' ' >> 4) & 0x0F);
886 udelay(40); /* the shortest data takes at least 40 us */
887 }
888 spin_unlock(&pprt_lock);
889
890 lcd_addr_x = lcd_addr_y = 0;
891 lcd_gotoxy();
892}
893
894/* fills the display with spaces and resets X/Y */
895static void lcd_clear_fast_p8(void)
896{
897 int pos;
898 lcd_addr_x = lcd_addr_y = 0;
899 lcd_gotoxy();
900
901 spin_lock(&pprt_lock);
902 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
903 /* present the data to the data port */
904 w_dtr(pprt, ' ');
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300905
906 /* maintain the data during 20 us before the strobe */
907 udelay(20);
Willy Tarreau698b1512008-11-22 11:29:33 +0100908
909 bits.e = BIT_SET;
910 bits.rs = BIT_SET;
911 bits.rw = BIT_CLR;
912 set_ctrl_bits();
913
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300914 /* maintain the strobe during 40 us */
915 udelay(40);
Willy Tarreau698b1512008-11-22 11:29:33 +0100916
917 bits.e = BIT_CLR;
918 set_ctrl_bits();
919
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300920 /* the shortest data takes at least 45 us */
921 udelay(45);
Willy Tarreau698b1512008-11-22 11:29:33 +0100922 }
923 spin_unlock(&pprt_lock);
924
925 lcd_addr_x = lcd_addr_y = 0;
926 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -0800927}
928
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -0400929/* fills the display with spaces and resets X/Y */
930static void lcd_clear_fast_tilcd(void)
931{
932 int pos;
933 lcd_addr_x = lcd_addr_y = 0;
934 lcd_gotoxy();
935
936 spin_lock(&pprt_lock);
937 for (pos = 0; pos < lcd_height * lcd_hwidth; pos++) {
938 /* present the data to the data port */
939 w_dtr(pprt, ' ');
940 udelay(60);
941 }
942
943 spin_unlock(&pprt_lock);
944
945 lcd_addr_x = lcd_addr_y = 0;
946 lcd_gotoxy();
947}
948
Willy Tarreau7005b582008-11-13 17:18:59 -0800949/* clears the display and resets X/Y */
Willy Tarreau698b1512008-11-22 11:29:33 +0100950static void lcd_clear_display(void)
951{
952 lcd_write_cmd(0x01); /* clear display */
953 lcd_addr_x = lcd_addr_y = 0;
954 /* we must wait a few milliseconds (15) */
955 long_sleep(15);
Willy Tarreau7005b582008-11-13 17:18:59 -0800956}
957
Willy Tarreau698b1512008-11-22 11:29:33 +0100958static void lcd_init_display(void)
959{
Willy Tarreau7005b582008-11-13 17:18:59 -0800960
Willy Tarreau698b1512008-11-22 11:29:33 +0100961 lcd_flags = ((lcd_height > 1) ? LCD_FLAG_N : 0)
962 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
Willy Tarreau7005b582008-11-13 17:18:59 -0800963
Willy Tarreau698b1512008-11-22 11:29:33 +0100964 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
Willy Tarreau7005b582008-11-13 17:18:59 -0800965
Willy Tarreau698b1512008-11-22 11:29:33 +0100966 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
967 long_sleep(10);
968 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
969 long_sleep(10);
970 lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
971 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800972
Willy Tarreau698b1512008-11-22 11:29:33 +0100973 lcd_write_cmd(0x30 /* set font height and lines number */
974 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
975 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0)
976 );
977 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800978
Willy Tarreau698b1512008-11-22 11:29:33 +0100979 lcd_write_cmd(0x08); /* display off, cursor off, blink off */
980 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800981
Willy Tarreau698b1512008-11-22 11:29:33 +0100982 lcd_write_cmd(0x08 /* set display mode */
983 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
984 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
985 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0)
986 );
Willy Tarreau7005b582008-11-13 17:18:59 -0800987
Willy Tarreau698b1512008-11-22 11:29:33 +0100988 lcd_backlight((lcd_flags & LCD_FLAG_L) ? 1 : 0);
Willy Tarreau7005b582008-11-13 17:18:59 -0800989
Willy Tarreau698b1512008-11-22 11:29:33 +0100990 long_sleep(10);
Willy Tarreau7005b582008-11-13 17:18:59 -0800991
Henri Häkkinen429ccf02010-06-12 00:27:36 +0300992 /* entry mode set : increment, cursor shifting */
993 lcd_write_cmd(0x06);
Willy Tarreau7005b582008-11-13 17:18:59 -0800994
Willy Tarreau698b1512008-11-22 11:29:33 +0100995 lcd_clear_display();
Willy Tarreau7005b582008-11-13 17:18:59 -0800996}
997
998/*
999 * These are the file operation function for user access to /dev/lcd
1000 * This function can also be called from inside the kernel, by
1001 * setting file and ppos to NULL.
1002 *
1003 */
1004
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001005static inline int handle_lcd_special_code(void)
1006{
1007 /* LCD special codes */
1008
1009 int processed = 0;
1010
1011 char *esc = lcd_escape + 2;
1012 int oldflags = lcd_flags;
1013
1014 /* check for display mode flags */
1015 switch (*esc) {
1016 case 'D': /* Display ON */
1017 lcd_flags |= LCD_FLAG_D;
1018 processed = 1;
1019 break;
1020 case 'd': /* Display OFF */
1021 lcd_flags &= ~LCD_FLAG_D;
1022 processed = 1;
1023 break;
1024 case 'C': /* Cursor ON */
1025 lcd_flags |= LCD_FLAG_C;
1026 processed = 1;
1027 break;
1028 case 'c': /* Cursor OFF */
1029 lcd_flags &= ~LCD_FLAG_C;
1030 processed = 1;
1031 break;
1032 case 'B': /* Blink ON */
1033 lcd_flags |= LCD_FLAG_B;
1034 processed = 1;
1035 break;
1036 case 'b': /* Blink OFF */
1037 lcd_flags &= ~LCD_FLAG_B;
1038 processed = 1;
1039 break;
1040 case '+': /* Back light ON */
1041 lcd_flags |= LCD_FLAG_L;
1042 processed = 1;
1043 break;
1044 case '-': /* Back light OFF */
1045 lcd_flags &= ~LCD_FLAG_L;
1046 processed = 1;
1047 break;
1048 case '*':
1049 /* flash back light using the keypad timer */
1050 if (scan_timer.function != NULL) {
1051 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1052 lcd_backlight(1);
1053 light_tempo = FLASH_LIGHT_TEMPO;
1054 }
1055 processed = 1;
1056 break;
1057 case 'f': /* Small Font */
1058 lcd_flags &= ~LCD_FLAG_F;
1059 processed = 1;
1060 break;
1061 case 'F': /* Large Font */
1062 lcd_flags |= LCD_FLAG_F;
1063 processed = 1;
1064 break;
1065 case 'n': /* One Line */
1066 lcd_flags &= ~LCD_FLAG_N;
1067 processed = 1;
1068 break;
1069 case 'N': /* Two Lines */
1070 lcd_flags |= LCD_FLAG_N;
1071 break;
1072 case 'l': /* Shift Cursor Left */
1073 if (lcd_addr_x > 0) {
1074 /* back one char if not at end of line */
1075 if (lcd_addr_x < lcd_bwidth)
1076 lcd_write_cmd(0x10);
1077 lcd_addr_x--;
1078 }
1079 processed = 1;
1080 break;
1081 case 'r': /* shift cursor right */
1082 if (lcd_addr_x < lcd_width) {
1083 /* allow the cursor to pass the end of the line */
1084 if (lcd_addr_x <
1085 (lcd_bwidth - 1))
1086 lcd_write_cmd(0x14);
1087 lcd_addr_x++;
1088 }
1089 processed = 1;
1090 break;
1091 case 'L': /* shift display left */
1092 lcd_left_shift++;
1093 lcd_write_cmd(0x18);
1094 processed = 1;
1095 break;
1096 case 'R': /* shift display right */
1097 lcd_left_shift--;
1098 lcd_write_cmd(0x1C);
1099 processed = 1;
1100 break;
1101 case 'k': { /* kill end of line */
1102 int x;
1103 for (x = lcd_addr_x; x < lcd_bwidth; x++)
1104 lcd_write_data(' ');
1105
1106 /* restore cursor position */
1107 lcd_gotoxy();
1108 processed = 1;
1109 break;
1110 }
1111 case 'I': /* reinitialize display */
1112 lcd_init_display();
1113 lcd_left_shift = 0;
1114 processed = 1;
1115 break;
1116 case 'G': {
1117 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1118 * and '7', representing the numerical ASCII code of the
1119 * redefined character, and <xx...xx> a sequence of 16
1120 * hex digits representing 8 bytes for each character.
1121 * Most LCDs will only use 5 lower bits of the 7 first
1122 * bytes.
1123 */
1124
1125 unsigned char cgbytes[8];
1126 unsigned char cgaddr;
1127 int cgoffset;
1128 int shift;
1129 char value;
1130 int addr;
1131
1132 if (strchr(esc, ';') == NULL)
1133 break;
1134
1135 esc++;
1136
1137 cgaddr = *(esc++) - '0';
1138 if (cgaddr > 7) {
1139 processed = 1;
1140 break;
1141 }
1142
1143 cgoffset = 0;
1144 shift = 0;
1145 value = 0;
1146 while (*esc && cgoffset < 8) {
1147 shift ^= 4;
1148 if (*esc >= '0' && *esc <= '9')
1149 value |= (*esc - '0') << shift;
1150 else if (*esc >= 'A' && *esc <= 'Z')
1151 value |= (*esc - 'A' + 10) << shift;
1152 else if (*esc >= 'a' && *esc <= 'z')
1153 value |= (*esc - 'a' + 10) << shift;
1154 else {
1155 esc++;
1156 continue;
1157 }
1158
1159 if (shift == 0) {
1160 cgbytes[cgoffset++] = value;
1161 value = 0;
1162 }
1163
1164 esc++;
1165 }
1166
1167 lcd_write_cmd(0x40 | (cgaddr * 8));
1168 for (addr = 0; addr < cgoffset; addr++)
1169 lcd_write_data(cgbytes[addr]);
1170
1171 /* ensures that we stop writing to CGRAM */
1172 lcd_gotoxy();
1173 processed = 1;
1174 break;
1175 }
1176 case 'x': /* gotoxy : LxXXX[yYYY]; */
1177 case 'y': /* gotoxy : LyYYY[xXXX]; */
1178 if (strchr(esc, ';') == NULL)
1179 break;
1180
1181 while (*esc) {
1182 if (*esc == 'x') {
1183 esc++;
1184 lcd_addr_x = 0;
1185 while (isdigit(*esc)) {
1186 lcd_addr_x = lcd_addr_x * 10 +
1187 (*esc - '0');
1188 esc++;
1189 }
1190 } else if (*esc == 'y') {
1191 esc++;
1192 lcd_addr_y = 0;
1193 while (isdigit(*esc)) {
1194 lcd_addr_y = lcd_addr_y * 10 +
1195 (*esc - '0');
1196 esc++;
1197 }
1198 } else
1199 break;
1200 }
1201
1202 lcd_gotoxy();
1203 processed = 1;
1204 break;
1205 }
1206
1207 /* Check wether one flag was changed */
1208 if (oldflags != lcd_flags) {
1209 /* check whether one of B,C,D flags were changed */
1210 if ((oldflags ^ lcd_flags) &
1211 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1212 /* set display mode */
1213 lcd_write_cmd(0x08
1214 | ((lcd_flags & LCD_FLAG_D) ? 4 : 0)
1215 | ((lcd_flags & LCD_FLAG_C) ? 2 : 0)
1216 | ((lcd_flags & LCD_FLAG_B) ? 1 : 0));
1217 /* check whether one of F,N flags was changed */
1218 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_F | LCD_FLAG_N))
1219 lcd_write_cmd(0x30
1220 | ((lcd_flags & LCD_FLAG_F) ? 4 : 0)
1221 | ((lcd_flags & LCD_FLAG_N) ? 8 : 0));
1222 /* check wether L flag was changed */
1223 else if ((oldflags ^ lcd_flags) & (LCD_FLAG_L)) {
1224 if (lcd_flags & (LCD_FLAG_L))
1225 lcd_backlight(1);
1226 else if (light_tempo == 0)
1227 /* switch off the light only when the tempo
1228 lighting is gone */
1229 lcd_backlight(0);
1230 }
1231 }
1232
1233 return processed;
1234}
1235
Willy Tarreau698b1512008-11-22 11:29:33 +01001236static ssize_t lcd_write(struct file *file,
1237 const char *buf, size_t count, loff_t *ppos)
1238{
Willy Tarreau698b1512008-11-22 11:29:33 +01001239 const char *tmp = buf;
1240 char c;
Willy Tarreau7005b582008-11-13 17:18:59 -08001241
Willy Tarreau698b1512008-11-22 11:29:33 +01001242 for (; count-- > 0; (ppos ? (*ppos)++ : 0), ++tmp) {
1243 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001244 /* let's be a little nice with other processes
1245 that need some CPU */
1246 schedule();
Willy Tarreau7005b582008-11-13 17:18:59 -08001247
Willy Tarreau698b1512008-11-22 11:29:33 +01001248 if (ppos == NULL && file == NULL)
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001249 /* let's not use get_user() from the kernel ! */
1250 c = *tmp;
Willy Tarreau698b1512008-11-22 11:29:33 +01001251 else if (get_user(c, tmp))
1252 return -EFAULT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001253
Willy Tarreau698b1512008-11-22 11:29:33 +01001254 /* first, we'll test if we're in escape mode */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001255 if ((c != '\n') && lcd_escape_len >= 0) {
1256 /* yes, let's add this char to the buffer */
Willy Tarreau698b1512008-11-22 11:29:33 +01001257 lcd_escape[lcd_escape_len++] = c;
1258 lcd_escape[lcd_escape_len] = 0;
1259 } else {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001260 /* aborts any previous escape sequence */
1261 lcd_escape_len = -1;
Willy Tarreau698b1512008-11-22 11:29:33 +01001262
1263 switch (c) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001264 case LCD_ESCAPE_CHAR:
1265 /* start of an escape sequence */
Willy Tarreau698b1512008-11-22 11:29:33 +01001266 lcd_escape_len = 0;
1267 lcd_escape[lcd_escape_len] = 0;
1268 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001269 case '\b':
1270 /* go back one char and clear it */
Willy Tarreau698b1512008-11-22 11:29:33 +01001271 if (lcd_addr_x > 0) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001272 /* check if we're not at the
1273 end of the line */
1274 if (lcd_addr_x < lcd_bwidth)
1275 /* back one char */
1276 lcd_write_cmd(0x10);
Willy Tarreau698b1512008-11-22 11:29:33 +01001277 lcd_addr_x--;
1278 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001279 /* replace with a space */
1280 lcd_write_data(' ');
1281 /* back one char again */
1282 lcd_write_cmd(0x10);
Willy Tarreau698b1512008-11-22 11:29:33 +01001283 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001284 case '\014':
1285 /* quickly clear the display */
Willy Tarreau698b1512008-11-22 11:29:33 +01001286 lcd_clear_fast();
1287 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001288 case '\n':
1289 /* flush the remainder of the current line and
1290 go to the beginning of the next line */
Willy Tarreau698b1512008-11-22 11:29:33 +01001291 for (; lcd_addr_x < lcd_bwidth; lcd_addr_x++)
1292 lcd_write_data(' ');
1293 lcd_addr_x = 0;
1294 lcd_addr_y = (lcd_addr_y + 1) % lcd_height;
1295 lcd_gotoxy();
1296 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001297 case '\r':
1298 /* go to the beginning of the same line */
Willy Tarreau698b1512008-11-22 11:29:33 +01001299 lcd_addr_x = 0;
1300 lcd_gotoxy();
1301 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001302 case '\t':
1303 /* print a space instead of the tab */
Willy Tarreau698b1512008-11-22 11:29:33 +01001304 lcd_print(' ');
1305 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001306 default:
1307 /* simply print this char */
Willy Tarreau698b1512008-11-22 11:29:33 +01001308 lcd_print(c);
1309 break;
1310 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001311 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001312
1313 /* now we'll see if we're in an escape mode and if the current
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001314 escape sequence can be understood. */
1315 if (lcd_escape_len >= 2) {
1316 int processed = 0;
Willy Tarreau698b1512008-11-22 11:29:33 +01001317
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001318 if (!strcmp(lcd_escape, "[2J")) {
1319 /* clear the display */
1320 lcd_clear_fast();
Willy Tarreau698b1512008-11-22 11:29:33 +01001321 processed = 1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001322 } else if (!strcmp(lcd_escape, "[H")) {
1323 /* cursor to home */
Willy Tarreau698b1512008-11-22 11:29:33 +01001324 lcd_addr_x = lcd_addr_y = 0;
1325 lcd_gotoxy();
1326 processed = 1;
1327 }
1328 /* codes starting with ^[[L */
1329 else if ((lcd_escape_len >= 3) &&
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001330 (lcd_escape[0] == '[') &&
1331 (lcd_escape[1] == 'L')) {
1332 processed = handle_lcd_special_code();
Willy Tarreau698b1512008-11-22 11:29:33 +01001333 }
1334
1335 /* LCD special escape codes */
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001336 /* flush the escape sequence if it's been processed
1337 or if it is getting too long. */
Willy Tarreau698b1512008-11-22 11:29:33 +01001338 if (processed || (lcd_escape_len >= LCD_ESCAPE_LEN))
1339 lcd_escape_len = -1;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001340 } /* escape codes */
Willy Tarreau7005b582008-11-13 17:18:59 -08001341 }
1342
Willy Tarreau698b1512008-11-22 11:29:33 +01001343 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001344}
1345
Willy Tarreau698b1512008-11-22 11:29:33 +01001346static int lcd_open(struct inode *inode, struct file *file)
1347{
1348 if (lcd_open_cnt)
1349 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001350
Willy Tarreau698b1512008-11-22 11:29:33 +01001351 if (file->f_mode & FMODE_READ) /* device is write-only */
1352 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001353
Willy Tarreau698b1512008-11-22 11:29:33 +01001354 if (lcd_must_clear) {
1355 lcd_clear_display();
1356 lcd_must_clear = 0;
1357 }
1358 lcd_open_cnt++;
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001359 return nonseekable_open(inode, file);
Willy Tarreau7005b582008-11-13 17:18:59 -08001360}
1361
Willy Tarreau698b1512008-11-22 11:29:33 +01001362static int lcd_release(struct inode *inode, struct file *file)
1363{
1364 lcd_open_cnt--;
1365 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001366}
1367
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001368static const struct file_operations lcd_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001369 .write = lcd_write,
1370 .open = lcd_open,
1371 .release = lcd_release,
Arnd Bergmann3ff81012010-07-07 23:40:09 +02001372 .llseek = no_llseek,
Willy Tarreau7005b582008-11-13 17:18:59 -08001373};
1374
1375static struct miscdevice lcd_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001376 LCD_MINOR,
1377 "lcd",
1378 &lcd_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001379};
1380
Willy Tarreau7005b582008-11-13 17:18:59 -08001381/* public function usable from the kernel for any purpose */
Willy Tarreau698b1512008-11-22 11:29:33 +01001382void panel_lcd_print(char *s)
1383{
1384 if (lcd_enabled && lcd_initialized)
1385 lcd_write(NULL, s, strlen(s), NULL);
Willy Tarreau7005b582008-11-13 17:18:59 -08001386}
1387
Willy Tarreau7005b582008-11-13 17:18:59 -08001388/* initialize the LCD driver */
Willy Tarreau698b1512008-11-22 11:29:33 +01001389void lcd_init(void)
1390{
1391 switch (lcd_type) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001392 case LCD_TYPE_OLD:
1393 /* parallel mode, 8 bits */
Willy Tarreau698b1512008-11-22 11:29:33 +01001394 if (lcd_proto < 0)
1395 lcd_proto = LCD_PROTO_PARALLEL;
1396 if (lcd_charset < 0)
1397 lcd_charset = LCD_CHARSET_NORMAL;
1398 if (lcd_e_pin == PIN_NOT_SET)
1399 lcd_e_pin = PIN_STROBE;
1400 if (lcd_rs_pin == PIN_NOT_SET)
1401 lcd_rs_pin = PIN_AUTOLF;
Willy Tarreau7005b582008-11-13 17:18:59 -08001402
Willy Tarreau698b1512008-11-22 11:29:33 +01001403 if (lcd_width < 0)
1404 lcd_width = 40;
1405 if (lcd_bwidth < 0)
1406 lcd_bwidth = 40;
1407 if (lcd_hwidth < 0)
1408 lcd_hwidth = 64;
1409 if (lcd_height < 0)
1410 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001411 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001412 case LCD_TYPE_KS0074:
1413 /* serial mode, ks0074 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001414 if (lcd_proto < 0)
1415 lcd_proto = LCD_PROTO_SERIAL;
1416 if (lcd_charset < 0)
1417 lcd_charset = LCD_CHARSET_KS0074;
1418 if (lcd_bl_pin == PIN_NOT_SET)
1419 lcd_bl_pin = PIN_AUTOLF;
1420 if (lcd_cl_pin == PIN_NOT_SET)
1421 lcd_cl_pin = PIN_STROBE;
1422 if (lcd_da_pin == PIN_NOT_SET)
1423 lcd_da_pin = PIN_D0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001424
Willy Tarreau698b1512008-11-22 11:29:33 +01001425 if (lcd_width < 0)
1426 lcd_width = 16;
1427 if (lcd_bwidth < 0)
1428 lcd_bwidth = 40;
1429 if (lcd_hwidth < 0)
1430 lcd_hwidth = 16;
1431 if (lcd_height < 0)
1432 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001433 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001434 case LCD_TYPE_NEXCOM:
1435 /* parallel mode, 8 bits, generic */
Willy Tarreau698b1512008-11-22 11:29:33 +01001436 if (lcd_proto < 0)
1437 lcd_proto = LCD_PROTO_PARALLEL;
1438 if (lcd_charset < 0)
1439 lcd_charset = LCD_CHARSET_NORMAL;
1440 if (lcd_e_pin == PIN_NOT_SET)
1441 lcd_e_pin = PIN_AUTOLF;
1442 if (lcd_rs_pin == PIN_NOT_SET)
1443 lcd_rs_pin = PIN_SELECP;
1444 if (lcd_rw_pin == PIN_NOT_SET)
1445 lcd_rw_pin = PIN_INITP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001446
Willy Tarreau698b1512008-11-22 11:29:33 +01001447 if (lcd_width < 0)
1448 lcd_width = 16;
1449 if (lcd_bwidth < 0)
1450 lcd_bwidth = 40;
1451 if (lcd_hwidth < 0)
1452 lcd_hwidth = 64;
1453 if (lcd_height < 0)
1454 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001455 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001456 case LCD_TYPE_CUSTOM:
1457 /* customer-defined */
Willy Tarreau698b1512008-11-22 11:29:33 +01001458 if (lcd_proto < 0)
1459 lcd_proto = DEFAULT_LCD_PROTO;
1460 if (lcd_charset < 0)
1461 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001462 /* default geometry will be set later */
1463 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001464 case LCD_TYPE_HANTRONIX:
1465 /* parallel mode, 8 bits, hantronix-like */
Willy Tarreau698b1512008-11-22 11:29:33 +01001466 default:
1467 if (lcd_proto < 0)
1468 lcd_proto = LCD_PROTO_PARALLEL;
1469 if (lcd_charset < 0)
1470 lcd_charset = LCD_CHARSET_NORMAL;
1471 if (lcd_e_pin == PIN_NOT_SET)
1472 lcd_e_pin = PIN_STROBE;
1473 if (lcd_rs_pin == PIN_NOT_SET)
1474 lcd_rs_pin = PIN_SELECP;
Willy Tarreau7005b582008-11-13 17:18:59 -08001475
Willy Tarreau698b1512008-11-22 11:29:33 +01001476 if (lcd_width < 0)
1477 lcd_width = 16;
1478 if (lcd_bwidth < 0)
1479 lcd_bwidth = 40;
1480 if (lcd_hwidth < 0)
1481 lcd_hwidth = 64;
1482 if (lcd_height < 0)
1483 lcd_height = 2;
Willy Tarreau7005b582008-11-13 17:18:59 -08001484 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001485 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001486
Willy Tarreau698b1512008-11-22 11:29:33 +01001487 /* this is used to catch wrong and default values */
1488 if (lcd_width <= 0)
1489 lcd_width = DEFAULT_LCD_WIDTH;
1490 if (lcd_bwidth <= 0)
1491 lcd_bwidth = DEFAULT_LCD_BWIDTH;
1492 if (lcd_hwidth <= 0)
1493 lcd_hwidth = DEFAULT_LCD_HWIDTH;
1494 if (lcd_height <= 0)
1495 lcd_height = DEFAULT_LCD_HEIGHT;
Willy Tarreau7005b582008-11-13 17:18:59 -08001496
Willy Tarreau698b1512008-11-22 11:29:33 +01001497 if (lcd_proto == LCD_PROTO_SERIAL) { /* SERIAL */
1498 lcd_write_cmd = lcd_write_cmd_s;
1499 lcd_write_data = lcd_write_data_s;
1500 lcd_clear_fast = lcd_clear_fast_s;
Willy Tarreau7005b582008-11-13 17:18:59 -08001501
Willy Tarreau698b1512008-11-22 11:29:33 +01001502 if (lcd_cl_pin == PIN_NOT_SET)
1503 lcd_cl_pin = DEFAULT_LCD_PIN_SCL;
1504 if (lcd_da_pin == PIN_NOT_SET)
1505 lcd_da_pin = DEFAULT_LCD_PIN_SDA;
Willy Tarreau7005b582008-11-13 17:18:59 -08001506
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001507 } else if (lcd_proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
Willy Tarreau698b1512008-11-22 11:29:33 +01001508 lcd_write_cmd = lcd_write_cmd_p8;
1509 lcd_write_data = lcd_write_data_p8;
1510 lcd_clear_fast = lcd_clear_fast_p8;
Willy Tarreau7005b582008-11-13 17:18:59 -08001511
Willy Tarreau698b1512008-11-22 11:29:33 +01001512 if (lcd_e_pin == PIN_NOT_SET)
1513 lcd_e_pin = DEFAULT_LCD_PIN_E;
1514 if (lcd_rs_pin == PIN_NOT_SET)
1515 lcd_rs_pin = DEFAULT_LCD_PIN_RS;
1516 if (lcd_rw_pin == PIN_NOT_SET)
1517 lcd_rw_pin = DEFAULT_LCD_PIN_RW;
Sudhakar Rajashekhara77943d32009-08-20 18:13:18 -04001518 } else {
1519 lcd_write_cmd = lcd_write_cmd_tilcd;
1520 lcd_write_data = lcd_write_data_tilcd;
1521 lcd_clear_fast = lcd_clear_fast_tilcd;
Willy Tarreau698b1512008-11-22 11:29:33 +01001522 }
1523
1524 if (lcd_bl_pin == PIN_NOT_SET)
1525 lcd_bl_pin = DEFAULT_LCD_PIN_BL;
1526
1527 if (lcd_e_pin == PIN_NOT_SET)
1528 lcd_e_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001529 if (lcd_rs_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001530 lcd_rs_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001531 if (lcd_rw_pin == PIN_NOT_SET)
Willy Tarreau698b1512008-11-22 11:29:33 +01001532 lcd_rw_pin = PIN_NONE;
1533 if (lcd_bl_pin == PIN_NOT_SET)
1534 lcd_bl_pin = PIN_NONE;
1535 if (lcd_cl_pin == PIN_NOT_SET)
1536 lcd_cl_pin = PIN_NONE;
1537 if (lcd_da_pin == PIN_NOT_SET)
1538 lcd_da_pin = PIN_NONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08001539
Willy Tarreau698b1512008-11-22 11:29:33 +01001540 if (lcd_charset < 0)
1541 lcd_charset = DEFAULT_LCD_CHARSET;
Willy Tarreau7005b582008-11-13 17:18:59 -08001542
Willy Tarreau698b1512008-11-22 11:29:33 +01001543 if (lcd_charset == LCD_CHARSET_KS0074)
1544 lcd_char_conv = lcd_char_conv_ks0074;
1545 else
1546 lcd_char_conv = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08001547
Willy Tarreau698b1512008-11-22 11:29:33 +01001548 if (lcd_bl_pin != PIN_NONE)
1549 init_scan_timer();
Willy Tarreau7005b582008-11-13 17:18:59 -08001550
Willy Tarreau698b1512008-11-22 11:29:33 +01001551 pin_to_bits(lcd_e_pin, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1552 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1553 pin_to_bits(lcd_rs_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1554 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1555 pin_to_bits(lcd_rw_pin, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1556 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1557 pin_to_bits(lcd_bl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1558 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1559 pin_to_bits(lcd_cl_pin, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1560 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1561 pin_to_bits(lcd_da_pin, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1562 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
Willy Tarreau7005b582008-11-13 17:18:59 -08001563
Willy Tarreau698b1512008-11-22 11:29:33 +01001564 /* before this line, we must NOT send anything to the display.
1565 * Since lcd_init_display() needs to write data, we have to
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001566 * enable mark the LCD initialized just before. */
Willy Tarreau698b1512008-11-22 11:29:33 +01001567 lcd_initialized = 1;
1568 lcd_init_display();
Willy Tarreau7005b582008-11-13 17:18:59 -08001569
Willy Tarreau698b1512008-11-22 11:29:33 +01001570 /* display a short message */
Willy Tarreau7005b582008-11-13 17:18:59 -08001571#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1572#ifdef CONFIG_PANEL_BOOT_MESSAGE
Willy Tarreau698b1512008-11-22 11:29:33 +01001573 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
Willy Tarreau7005b582008-11-13 17:18:59 -08001574#endif
1575#else
Willy Tarreau698b1512008-11-22 11:29:33 +01001576 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-"
1577 PANEL_VERSION);
Willy Tarreau7005b582008-11-13 17:18:59 -08001578#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01001579 lcd_addr_x = lcd_addr_y = 0;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001580 /* clear the display on the next device opening */
1581 lcd_must_clear = 1;
Willy Tarreau698b1512008-11-22 11:29:33 +01001582 lcd_gotoxy();
Willy Tarreau7005b582008-11-13 17:18:59 -08001583}
1584
Willy Tarreau7005b582008-11-13 17:18:59 -08001585/*
1586 * These are the file operation function for user access to /dev/keypad
1587 */
1588
Willy Tarreau698b1512008-11-22 11:29:33 +01001589static ssize_t keypad_read(struct file *file,
1590 char *buf, size_t count, loff_t *ppos)
1591{
Willy Tarreau7005b582008-11-13 17:18:59 -08001592
Willy Tarreau698b1512008-11-22 11:29:33 +01001593 unsigned i = *ppos;
1594 char *tmp = buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001595
Willy Tarreau698b1512008-11-22 11:29:33 +01001596 if (keypad_buflen == 0) {
1597 if (file->f_flags & O_NONBLOCK)
1598 return -EAGAIN;
Willy Tarreau7005b582008-11-13 17:18:59 -08001599
Willy Tarreau698b1512008-11-22 11:29:33 +01001600 interruptible_sleep_on(&keypad_read_wait);
1601 if (signal_pending(current))
1602 return -EINTR;
1603 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001604
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001605 for (; count-- > 0 && (keypad_buflen > 0);
1606 ++i, ++tmp, --keypad_buflen) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001607 put_user(keypad_buffer[keypad_start], tmp);
1608 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1609 }
1610 *ppos = i;
Willy Tarreau7005b582008-11-13 17:18:59 -08001611
Willy Tarreau698b1512008-11-22 11:29:33 +01001612 return tmp - buf;
Willy Tarreau7005b582008-11-13 17:18:59 -08001613}
1614
Willy Tarreau698b1512008-11-22 11:29:33 +01001615static int keypad_open(struct inode *inode, struct file *file)
1616{
Willy Tarreau7005b582008-11-13 17:18:59 -08001617
Willy Tarreau698b1512008-11-22 11:29:33 +01001618 if (keypad_open_cnt)
1619 return -EBUSY; /* open only once at a time */
Willy Tarreau7005b582008-11-13 17:18:59 -08001620
Willy Tarreau698b1512008-11-22 11:29:33 +01001621 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1622 return -EPERM;
Willy Tarreau7005b582008-11-13 17:18:59 -08001623
Willy Tarreau698b1512008-11-22 11:29:33 +01001624 keypad_buflen = 0; /* flush the buffer on opening */
1625 keypad_open_cnt++;
1626 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001627}
1628
Willy Tarreau698b1512008-11-22 11:29:33 +01001629static int keypad_release(struct inode *inode, struct file *file)
1630{
1631 keypad_open_cnt--;
1632 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08001633}
1634
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001635static const struct file_operations keypad_fops = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001636 .read = keypad_read, /* read */
1637 .open = keypad_open, /* open */
1638 .release = keypad_release, /* close */
Willy Tarreau7005b582008-11-13 17:18:59 -08001639};
1640
1641static struct miscdevice keypad_dev = {
Willy Tarreau698b1512008-11-22 11:29:33 +01001642 KEYPAD_MINOR,
1643 "keypad",
1644 &keypad_fops
Willy Tarreau7005b582008-11-13 17:18:59 -08001645};
1646
Willy Tarreau698b1512008-11-22 11:29:33 +01001647static void keypad_send_key(char *string, int max_len)
1648{
1649 if (init_in_progress)
1650 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08001651
Willy Tarreau698b1512008-11-22 11:29:33 +01001652 /* send the key to the device only if a process is attached to it. */
1653 if (keypad_open_cnt > 0) {
1654 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1655 keypad_buffer[(keypad_start + keypad_buflen++) %
1656 KEYPAD_BUFFER] = *string++;
1657 }
1658 wake_up_interruptible(&keypad_read_wait);
Willy Tarreau7005b582008-11-13 17:18:59 -08001659 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001660}
1661
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001662/* this function scans all the bits involving at least one logical signal,
1663 * and puts the results in the bitfield "phys_read" (one bit per established
1664 * contact), and sets "phys_read_prev" to "phys_read".
Willy Tarreau7005b582008-11-13 17:18:59 -08001665 *
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001666 * Note: to debounce input signals, we will only consider as switched a signal
1667 * which is stable across 2 measures. Signals which are different between two
1668 * reads will be kept as they previously were in their logical form (phys_prev).
1669 * A signal which has just switched will have a 1 in
1670 * (phys_read ^ phys_read_prev).
Willy Tarreau7005b582008-11-13 17:18:59 -08001671 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001672static void phys_scan_contacts(void)
1673{
1674 int bit, bitval;
1675 char oldval;
1676 char bitmask;
1677 char gndmask;
Willy Tarreau7005b582008-11-13 17:18:59 -08001678
Willy Tarreau698b1512008-11-22 11:29:33 +01001679 phys_prev = phys_curr;
1680 phys_read_prev = phys_read;
1681 phys_read = 0; /* flush all signals */
Willy Tarreau7005b582008-11-13 17:18:59 -08001682
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001683 /* keep track of old value, with all outputs disabled */
1684 oldval = r_dtr(pprt) | scan_mask_o;
1685 /* activate all keyboard outputs (active low) */
1686 w_dtr(pprt, oldval & ~scan_mask_o);
1687
1688 /* will have a 1 for each bit set to gnd */
1689 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1690 /* disable all matrix signals */
1691 w_dtr(pprt, oldval);
Willy Tarreau7005b582008-11-13 17:18:59 -08001692
Willy Tarreau698b1512008-11-22 11:29:33 +01001693 /* now that all outputs are cleared, the only active input bits are
1694 * directly connected to the ground
Willy Tarreau7005b582008-11-13 17:18:59 -08001695 */
Willy Tarreau7005b582008-11-13 17:18:59 -08001696
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001697 /* 1 for each grounded input */
1698 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1699
1700 /* grounded inputs are signals 40-44 */
1701 phys_read |= (pmask_t) gndmask << 40;
Willy Tarreau7005b582008-11-13 17:18:59 -08001702
Willy Tarreau698b1512008-11-22 11:29:33 +01001703 if (bitmask != gndmask) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001704 /* since clearing the outputs changed some inputs, we know
1705 * that some input signals are currently tied to some outputs.
1706 * So we'll scan them.
Willy Tarreau698b1512008-11-22 11:29:33 +01001707 */
1708 for (bit = 0; bit < 8; bit++) {
1709 bitval = 1 << bit;
1710
1711 if (!(scan_mask_o & bitval)) /* skip unused bits */
1712 continue;
1713
1714 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1715 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1716 phys_read |= (pmask_t) bitmask << (5 * bit);
1717 }
1718 w_dtr(pprt, oldval); /* disable all outputs */
Willy Tarreau7005b582008-11-13 17:18:59 -08001719 }
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001720 /* this is easy: use old bits when they are flapping,
1721 * use new ones when stable */
1722 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1723 (phys_read & ~(phys_read ^ phys_read_prev));
1724}
1725
1726static inline int input_state_high(struct logical_input *input)
1727{
1728#if 0
1729 /* FIXME:
1730 * this is an invalid test. It tries to catch
1731 * transitions from single-key to multiple-key, but
1732 * doesn't take into account the contacts polarity.
1733 * The only solution to the problem is to parse keys
1734 * from the most complex to the simplest combinations,
1735 * and mark them as 'caught' once a combination
1736 * matches, then unmatch it for all other ones.
1737 */
1738
1739 /* try to catch dangerous transitions cases :
1740 * someone adds a bit, so this signal was a false
1741 * positive resulting from a transition. We should
1742 * invalidate the signal immediately and not call the
1743 * release function.
1744 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1745 */
1746 if (((phys_prev & input->mask) == input->value)
1747 && ((phys_curr & input->mask) > input->value)) {
1748 input->state = INPUT_ST_LOW; /* invalidate */
1749 return 1;
1750 }
1751#endif
1752
1753 if ((phys_curr & input->mask) == input->value) {
1754 if ((input->type == INPUT_TYPE_STD) &&
1755 (input->high_timer == 0)) {
1756 input->high_timer++;
1757 if (input->u.std.press_fct != NULL)
1758 input->u.std.press_fct(input->u.std.press_data);
1759 } else if (input->type == INPUT_TYPE_KBD) {
1760 /* will turn on the light */
1761 keypressed = 1;
1762
1763 if (input->high_timer == 0) {
1764 char *press_str = input->u.kbd.press_str;
1765 if (press_str[0])
1766 keypad_send_key(press_str,
1767 sizeof(press_str));
1768 }
1769
1770 if (input->u.kbd.repeat_str[0]) {
1771 char *repeat_str = input->u.kbd.repeat_str;
1772 if (input->high_timer >= KEYPAD_REP_START) {
1773 input->high_timer -= KEYPAD_REP_DELAY;
1774 keypad_send_key(repeat_str,
1775 sizeof(repeat_str));
1776 }
1777 /* we will need to come back here soon */
1778 inputs_stable = 0;
1779 }
1780
1781 if (input->high_timer < 255)
1782 input->high_timer++;
1783 }
1784 return 1;
1785 } else {
1786 /* else signal falling down. Let's fall through. */
1787 input->state = INPUT_ST_FALLING;
1788 input->fall_timer = 0;
1789 }
1790 return 0;
1791}
1792
1793static inline void input_state_falling(struct logical_input *input)
1794{
1795#if 0
1796 /* FIXME !!! same comment as in input_state_high */
1797 if (((phys_prev & input->mask) == input->value)
1798 && ((phys_curr & input->mask) > input->value)) {
1799 input->state = INPUT_ST_LOW; /* invalidate */
1800 return;
1801 }
1802#endif
1803
1804 if ((phys_curr & input->mask) == input->value) {
1805 if (input->type == INPUT_TYPE_KBD) {
1806 /* will turn on the light */
1807 keypressed = 1;
1808
1809 if (input->u.kbd.repeat_str[0]) {
1810 char *repeat_str = input->u.kbd.repeat_str;
1811 if (input->high_timer >= KEYPAD_REP_START)
1812 input->high_timer -= KEYPAD_REP_DELAY;
1813 keypad_send_key(repeat_str,
1814 sizeof(repeat_str));
1815 /* we will need to come back here soon */
1816 inputs_stable = 0;
1817 }
1818
1819 if (input->high_timer < 255)
1820 input->high_timer++;
1821 }
1822 input->state = INPUT_ST_HIGH;
1823 } else if (input->fall_timer >= input->fall_time) {
1824 /* call release event */
1825 if (input->type == INPUT_TYPE_STD) {
1826 void (*release_fct)(int) = input->u.std.release_fct;
1827 if (release_fct != NULL)
1828 release_fct(input->u.std.release_data);
1829 } else if (input->type == INPUT_TYPE_KBD) {
1830 char *release_str = input->u.kbd.release_str;
1831 if (release_str[0])
1832 keypad_send_key(release_str,
1833 sizeof(release_str));
1834 }
1835
1836 input->state = INPUT_ST_LOW;
1837 } else {
1838 input->fall_timer++;
1839 inputs_stable = 0;
1840 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001841}
1842
Willy Tarreau698b1512008-11-22 11:29:33 +01001843static void panel_process_inputs(void)
1844{
1845 struct list_head *item;
1846 struct logical_input *input;
Willy Tarreau7005b582008-11-13 17:18:59 -08001847
1848#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01001849 printk(KERN_DEBUG
1850 "entering panel_process_inputs with pp=%016Lx & pc=%016Lx\n",
1851 phys_prev, phys_curr);
Willy Tarreau7005b582008-11-13 17:18:59 -08001852#endif
1853
Willy Tarreau698b1512008-11-22 11:29:33 +01001854 keypressed = 0;
1855 inputs_stable = 1;
1856 list_for_each(item, &logical_inputs) {
1857 input = list_entry(item, struct logical_input, list);
Willy Tarreau7005b582008-11-13 17:18:59 -08001858
Willy Tarreau698b1512008-11-22 11:29:33 +01001859 switch (input->state) {
1860 case INPUT_ST_LOW:
1861 if ((phys_curr & input->mask) != input->value)
1862 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001863 /* if all needed ones were already set previously,
1864 * this means that this logical signal has been
1865 * activated by the releasing of another combined
1866 * signal, so we don't want to match.
1867 * eg: AB -(release B)-> A -(release A)-> 0 :
1868 * don't match A.
Willy Tarreau698b1512008-11-22 11:29:33 +01001869 */
1870 if ((phys_prev & input->mask) == input->value)
1871 break;
1872 input->rise_timer = 0;
1873 input->state = INPUT_ST_RISING;
1874 /* no break here, fall through */
1875 case INPUT_ST_RISING:
1876 if ((phys_curr & input->mask) != input->value) {
1877 input->state = INPUT_ST_LOW;
1878 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08001879 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001880 if (input->rise_timer < input->rise_time) {
1881 inputs_stable = 0;
1882 input->rise_timer++;
1883 break;
1884 }
1885 input->high_timer = 0;
1886 input->state = INPUT_ST_HIGH;
1887 /* no break here, fall through */
1888 case INPUT_ST_HIGH:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001889 if (input_state_high(input))
Willy Tarreau698b1512008-11-22 11:29:33 +01001890 break;
Willy Tarreau698b1512008-11-22 11:29:33 +01001891 /* no break here, fall through */
1892 case INPUT_ST_FALLING:
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001893 input_state_falling(input);
Willy Tarreau698b1512008-11-22 11:29:33 +01001894 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001895 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001896}
1897
Willy Tarreau698b1512008-11-22 11:29:33 +01001898static void panel_scan_timer(void)
1899{
Willy Tarreau630231772008-11-22 12:52:18 +01001900 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01001901 if (spin_trylock(&pprt_lock)) {
1902 phys_scan_contacts();
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001903
1904 /* no need for the parport anymore */
1905 spin_unlock(&pprt_lock);
Willy Tarreau698b1512008-11-22 11:29:33 +01001906 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001907
Willy Tarreau698b1512008-11-22 11:29:33 +01001908 if (!inputs_stable || phys_curr != phys_prev)
1909 panel_process_inputs();
1910 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001911
Willy Tarreau698b1512008-11-22 11:29:33 +01001912 if (lcd_enabled && lcd_initialized) {
1913 if (keypressed) {
1914 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1915 lcd_backlight(1);
1916 light_tempo = FLASH_LIGHT_TEMPO;
1917 } else if (light_tempo > 0) {
1918 light_tempo--;
1919 if (light_tempo == 0 && ((lcd_flags & LCD_FLAG_L) == 0))
1920 lcd_backlight(0);
1921 }
1922 }
Willy Tarreau7005b582008-11-13 17:18:59 -08001923
Willy Tarreau698b1512008-11-22 11:29:33 +01001924 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
Willy Tarreau7005b582008-11-13 17:18:59 -08001925}
1926
Willy Tarreau698b1512008-11-22 11:29:33 +01001927static void init_scan_timer(void)
1928{
1929 if (scan_timer.function != NULL)
1930 return; /* already started */
Willy Tarreau7005b582008-11-13 17:18:59 -08001931
Willy Tarreau698b1512008-11-22 11:29:33 +01001932 init_timer(&scan_timer);
1933 scan_timer.expires = jiffies + INPUT_POLL_TIME;
1934 scan_timer.data = 0;
1935 scan_timer.function = (void *)&panel_scan_timer;
1936 add_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08001937}
1938
1939/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001940 * if <omask> or <imask> are non-null, they will be or'ed with the bits
1941 * corresponding to out and in bits respectively.
Willy Tarreau7005b582008-11-13 17:18:59 -08001942 * returns 1 if ok, 0 if error (in which case, nothing is written).
1943 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001944static int input_name2mask(char *name, pmask_t *mask, pmask_t *value,
1945 char *imask, char *omask)
1946{
1947 static char sigtab[10] = "EeSsPpAaBb";
1948 char im, om;
1949 pmask_t m, v;
Willy Tarreau7005b582008-11-13 17:18:59 -08001950
Willy Tarreau698b1512008-11-22 11:29:33 +01001951 om = im = m = v = 0ULL;
1952 while (*name) {
1953 int in, out, bit, neg;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03001954 for (in = 0; (in < sizeof(sigtab)) &&
1955 (sigtab[in] != *name); in++)
Willy Tarreau698b1512008-11-22 11:29:33 +01001956 ;
1957 if (in >= sizeof(sigtab))
1958 return 0; /* input name not found */
1959 neg = (in & 1); /* odd (lower) names are negated */
1960 in >>= 1;
1961 im |= (1 << in);
Willy Tarreau7005b582008-11-13 17:18:59 -08001962
Willy Tarreau698b1512008-11-22 11:29:33 +01001963 name++;
1964 if (isdigit(*name)) {
1965 out = *name - '0';
1966 om |= (1 << out);
1967 } else if (*name == '-')
1968 out = 8;
1969 else
1970 return 0; /* unknown bit name */
1971
1972 bit = (out * 5) + in;
1973
1974 m |= 1ULL << bit;
1975 if (!neg)
1976 v |= 1ULL << bit;
1977 name++;
Willy Tarreau7005b582008-11-13 17:18:59 -08001978 }
Willy Tarreau698b1512008-11-22 11:29:33 +01001979 *mask = m;
1980 *value = v;
1981 if (imask)
1982 *imask |= im;
1983 if (omask)
1984 *omask |= om;
1985 return 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08001986}
1987
1988/* tries to bind a key to the signal name <name>. The key will send the
1989 * strings <press>, <repeat>, <release> for these respective events.
1990 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
1991 */
Willy Tarreau698b1512008-11-22 11:29:33 +01001992static struct logical_input *panel_bind_key(char *name, char *press,
1993 char *repeat, char *release)
1994{
1995 struct logical_input *key;
Willy Tarreau7005b582008-11-13 17:18:59 -08001996
Julia Lawall7a6cb0d2010-05-13 22:00:05 +02001997 key = kzalloc(sizeof(struct logical_input), GFP_KERNEL);
Willy Tarreau698b1512008-11-22 11:29:33 +01001998 if (!key) {
1999 printk(KERN_ERR "panel: not enough memory\n");
2000 return NULL;
2001 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002002 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
2003 &scan_mask_o))
2004 return NULL;
2005
2006 key->type = INPUT_TYPE_KBD;
2007 key->state = INPUT_ST_LOW;
2008 key->rise_time = 1;
2009 key->fall_time = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002010
2011#if 0
Willy Tarreau698b1512008-11-22 11:29:33 +01002012 printk(KERN_DEBUG "bind: <%s> : m=%016Lx v=%016Lx\n", name, key->mask,
2013 key->value);
Willy Tarreau7005b582008-11-13 17:18:59 -08002014#endif
Willy Tarreau698b1512008-11-22 11:29:33 +01002015 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2016 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2017 strncpy(key->u.kbd.release_str, release,
2018 sizeof(key->u.kbd.release_str));
2019 list_add(&key->list, &logical_inputs);
2020 return key;
Willy Tarreau7005b582008-11-13 17:18:59 -08002021}
2022
Willy Tarreau630231772008-11-22 12:52:18 +01002023#if 0
Willy Tarreau7005b582008-11-13 17:18:59 -08002024/* tries to bind a callback function to the signal name <name>. The function
2025 * <press_fct> will be called with the <press_data> arg when the signal is
2026 * activated, and so on for <release_fct>/<release_data>
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002027 * Returns the pointer to the new signal if ok, NULL if the signal could not
2028 * be bound.
Willy Tarreau7005b582008-11-13 17:18:59 -08002029 */
2030static struct logical_input *panel_bind_callback(char *name,
Willy Tarreau698b1512008-11-22 11:29:33 +01002031 void (*press_fct) (int),
2032 int press_data,
2033 void (*release_fct) (int),
2034 int release_data)
2035{
2036 struct logical_input *callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002037
Willy Tarreau698b1512008-11-22 11:29:33 +01002038 callback = kmalloc(sizeof(struct logical_input), GFP_KERNEL);
2039 if (!callback) {
2040 printk(KERN_ERR "panel: not enough memory\n");
2041 return NULL;
2042 }
2043 memset(callback, 0, sizeof(struct logical_input));
2044 if (!input_name2mask(name, &callback->mask, &callback->value,
2045 &scan_mask_i, &scan_mask_o))
2046 return NULL;
2047
2048 callback->type = INPUT_TYPE_STD;
2049 callback->state = INPUT_ST_LOW;
2050 callback->rise_time = 1;
2051 callback->fall_time = 1;
2052 callback->u.std.press_fct = press_fct;
2053 callback->u.std.press_data = press_data;
2054 callback->u.std.release_fct = release_fct;
2055 callback->u.std.release_data = release_data;
2056 list_add(&callback->list, &logical_inputs);
2057 return callback;
Willy Tarreau7005b582008-11-13 17:18:59 -08002058}
Willy Tarreau630231772008-11-22 12:52:18 +01002059#endif
Willy Tarreau7005b582008-11-13 17:18:59 -08002060
Willy Tarreau698b1512008-11-22 11:29:33 +01002061static void keypad_init(void)
2062{
2063 int keynum;
2064 init_waitqueue_head(&keypad_read_wait);
2065 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
Willy Tarreau7005b582008-11-13 17:18:59 -08002066
Willy Tarreau698b1512008-11-22 11:29:33 +01002067 /* Let's create all known keys */
Willy Tarreau7005b582008-11-13 17:18:59 -08002068
Willy Tarreau698b1512008-11-22 11:29:33 +01002069 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2070 panel_bind_key(keypad_profile[keynum][0],
2071 keypad_profile[keynum][1],
2072 keypad_profile[keynum][2],
2073 keypad_profile[keynum][3]);
2074 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002075
Willy Tarreau698b1512008-11-22 11:29:33 +01002076 init_scan_timer();
2077 keypad_initialized = 1;
Willy Tarreau7005b582008-11-13 17:18:59 -08002078}
2079
Willy Tarreau7005b582008-11-13 17:18:59 -08002080/**************************************************/
2081/* device initialization */
2082/**************************************************/
2083
Willy Tarreau698b1512008-11-22 11:29:33 +01002084static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2085 void *unused)
2086{
2087 if (lcd_enabled && lcd_initialized) {
2088 switch (code) {
2089 case SYS_DOWN:
2090 panel_lcd_print
2091 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2092 break;
2093 case SYS_HALT:
2094 panel_lcd_print
2095 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2096 break;
2097 case SYS_POWER_OFF:
2098 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2099 break;
2100 default:
2101 break;
2102 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002103 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002104 return NOTIFY_DONE;
Willy Tarreau7005b582008-11-13 17:18:59 -08002105}
2106
2107static struct notifier_block panel_notifier = {
2108 panel_notify_sys,
2109 NULL,
2110 0
2111};
2112
Willy Tarreau698b1512008-11-22 11:29:33 +01002113static void panel_attach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002114{
Willy Tarreau698b1512008-11-22 11:29:33 +01002115 if (port->number != parport)
2116 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002117
Willy Tarreau698b1512008-11-22 11:29:33 +01002118 if (pprt) {
2119 printk(KERN_ERR
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002120 "panel_attach(): port->number=%d parport=%d, "
2121 "already registered !\n",
Willy Tarreau698b1512008-11-22 11:29:33 +01002122 port->number, parport);
2123 return;
2124 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002125
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002126 pprt = parport_register_device(port, "panel", NULL, NULL, /* pf, kf */
Willy Tarreau698b1512008-11-22 11:29:33 +01002127 NULL,
2128 /*PARPORT_DEV_EXCL */
2129 0, (void *)&pprt);
Willy Tarreau7005b582008-11-13 17:18:59 -08002130
Willy Tarreau698b1512008-11-22 11:29:33 +01002131 if (parport_claim(pprt)) {
2132 printk(KERN_ERR
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002133 "Panel: could not claim access to parport%d. "
2134 "Aborting.\n", parport);
Willy Tarreau698b1512008-11-22 11:29:33 +01002135 return;
2136 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002137
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002138 /* must init LCD first, just in case an IRQ from the keypad is
2139 * generated at keypad init
2140 */
Willy Tarreau698b1512008-11-22 11:29:33 +01002141 if (lcd_enabled) {
2142 lcd_init();
2143 misc_register(&lcd_dev);
2144 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002145
Willy Tarreau698b1512008-11-22 11:29:33 +01002146 if (keypad_enabled) {
2147 keypad_init();
2148 misc_register(&keypad_dev);
2149 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002150}
2151
Willy Tarreau698b1512008-11-22 11:29:33 +01002152static void panel_detach(struct parport *port)
Willy Tarreau7005b582008-11-13 17:18:59 -08002153{
Willy Tarreau698b1512008-11-22 11:29:33 +01002154 if (port->number != parport)
2155 return;
Willy Tarreau7005b582008-11-13 17:18:59 -08002156
Willy Tarreau698b1512008-11-22 11:29:33 +01002157 if (!pprt) {
2158 printk(KERN_ERR
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002159 "panel_detach(): port->number=%d parport=%d, "
2160 "nothing to unregister.\n",
Willy Tarreau698b1512008-11-22 11:29:33 +01002161 port->number, parport);
2162 return;
2163 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002164
Peter Huewe0b0595b2009-09-29 01:22:40 +02002165 if (keypad_enabled && keypad_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002166 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002167 keypad_initialized = 0;
2168 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002169
Peter Huewe0b0595b2009-09-29 01:22:40 +02002170 if (lcd_enabled && lcd_initialized) {
Willy Tarreau698b1512008-11-22 11:29:33 +01002171 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002172 lcd_initialized = 0;
2173 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002174
Willy Tarreau698b1512008-11-22 11:29:33 +01002175 parport_release(pprt);
2176 parport_unregister_device(pprt);
2177 pprt = NULL;
Willy Tarreau7005b582008-11-13 17:18:59 -08002178}
2179
2180static struct parport_driver panel_driver = {
Willy Tarreau698b1512008-11-22 11:29:33 +01002181 .name = "panel",
2182 .attach = panel_attach,
2183 .detach = panel_detach,
Willy Tarreau7005b582008-11-13 17:18:59 -08002184};
2185
2186/* init function */
Willy Tarreau698b1512008-11-22 11:29:33 +01002187int panel_init(void)
2188{
2189 /* for backwards compatibility */
2190 if (keypad_type < 0)
2191 keypad_type = keypad_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002192
Willy Tarreau698b1512008-11-22 11:29:33 +01002193 if (lcd_type < 0)
2194 lcd_type = lcd_enabled;
Willy Tarreau7005b582008-11-13 17:18:59 -08002195
Willy Tarreau698b1512008-11-22 11:29:33 +01002196 if (parport < 0)
2197 parport = DEFAULT_PARPORT;
Willy Tarreau7005b582008-11-13 17:18:59 -08002198
Willy Tarreau698b1512008-11-22 11:29:33 +01002199 /* take care of an eventual profile */
2200 switch (profile) {
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002201 case PANEL_PROFILE_CUSTOM:
2202 /* custom profile */
Willy Tarreau698b1512008-11-22 11:29:33 +01002203 if (keypad_type < 0)
2204 keypad_type = DEFAULT_KEYPAD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002205 if (lcd_type < 0)
2206 lcd_type = DEFAULT_LCD;
2207 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002208 case PANEL_PROFILE_OLD:
2209 /* 8 bits, 2*16, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002210 if (keypad_type < 0)
2211 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002212 if (lcd_type < 0)
2213 lcd_type = LCD_TYPE_OLD;
2214 if (lcd_width < 0)
2215 lcd_width = 16;
2216 if (lcd_hwidth < 0)
2217 lcd_hwidth = 16;
2218 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002219 case PANEL_PROFILE_NEW:
2220 /* serial, 2*16, new keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002221 if (keypad_type < 0)
2222 keypad_type = KEYPAD_TYPE_NEW;
Willy Tarreau698b1512008-11-22 11:29:33 +01002223 if (lcd_type < 0)
2224 lcd_type = LCD_TYPE_KS0074;
2225 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002226 case PANEL_PROFILE_HANTRONIX:
2227 /* 8 bits, 2*16 hantronix-like, no keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002228 if (keypad_type < 0)
2229 keypad_type = KEYPAD_TYPE_NONE;
Willy Tarreau698b1512008-11-22 11:29:33 +01002230 if (lcd_type < 0)
2231 lcd_type = LCD_TYPE_HANTRONIX;
2232 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002233 case PANEL_PROFILE_NEXCOM:
2234 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
Willy Tarreau698b1512008-11-22 11:29:33 +01002235 if (keypad_type < 0)
2236 keypad_type = KEYPAD_TYPE_NEXCOM;
Willy Tarreau698b1512008-11-22 11:29:33 +01002237 if (lcd_type < 0)
2238 lcd_type = LCD_TYPE_NEXCOM;
2239 break;
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002240 case PANEL_PROFILE_LARGE:
2241 /* 8 bits, 2*40, old keypad */
Willy Tarreau698b1512008-11-22 11:29:33 +01002242 if (keypad_type < 0)
2243 keypad_type = KEYPAD_TYPE_OLD;
Willy Tarreau698b1512008-11-22 11:29:33 +01002244 if (lcd_type < 0)
2245 lcd_type = LCD_TYPE_OLD;
2246 break;
Willy Tarreau7005b582008-11-13 17:18:59 -08002247 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002248
Willy Tarreau698b1512008-11-22 11:29:33 +01002249 lcd_enabled = (lcd_type > 0);
2250 keypad_enabled = (keypad_type > 0);
Willy Tarreau7005b582008-11-13 17:18:59 -08002251
Willy Tarreau698b1512008-11-22 11:29:33 +01002252 switch (keypad_type) {
2253 case KEYPAD_TYPE_OLD:
2254 keypad_profile = old_keypad_profile;
2255 break;
2256 case KEYPAD_TYPE_NEW:
2257 keypad_profile = new_keypad_profile;
2258 break;
2259 case KEYPAD_TYPE_NEXCOM:
2260 keypad_profile = nexcom_keypad_profile;
2261 break;
2262 default:
2263 keypad_profile = NULL;
2264 break;
2265 }
2266
2267 /* tells various subsystems about the fact that we are initializing */
2268 init_in_progress = 1;
2269
2270 if (parport_register_driver(&panel_driver)) {
2271 printk(KERN_ERR
2272 "Panel: could not register with parport. Aborting.\n");
2273 return -EIO;
2274 }
2275
Willy Tarreau630231772008-11-22 12:52:18 +01002276 if (!lcd_enabled && !keypad_enabled) {
2277 /* no device enabled, let's release the parport */
Willy Tarreau698b1512008-11-22 11:29:33 +01002278 if (pprt) {
2279 parport_release(pprt);
2280 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002281 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002282 }
2283 parport_unregister_driver(&panel_driver);
2284 printk(KERN_ERR "Panel driver version " PANEL_VERSION
2285 " disabled.\n");
2286 return -ENODEV;
2287 }
2288
2289 register_reboot_notifier(&panel_notifier);
2290
2291 if (pprt)
2292 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2293 " registered on parport%d (io=0x%lx).\n", parport,
2294 pprt->port->base);
2295 else
2296 printk(KERN_INFO "Panel driver version " PANEL_VERSION
2297 " not yet registered\n");
Henri Häkkinen429ccf02010-06-12 00:27:36 +03002298 /* tells various subsystems about the fact that initialization
2299 is finished */
Willy Tarreau698b1512008-11-22 11:29:33 +01002300 init_in_progress = 0;
2301 return 0;
Willy Tarreau7005b582008-11-13 17:18:59 -08002302}
2303
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002304static int __init panel_init_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002305{
2306 return panel_init();
Willy Tarreau7005b582008-11-13 17:18:59 -08002307}
2308
Willy Tarreauf6d1fcf2008-11-22 12:04:19 +01002309static void __exit panel_cleanup_module(void)
Willy Tarreau698b1512008-11-22 11:29:33 +01002310{
2311 unregister_reboot_notifier(&panel_notifier);
Willy Tarreau7005b582008-11-13 17:18:59 -08002312
Willy Tarreau698b1512008-11-22 11:29:33 +01002313 if (scan_timer.function != NULL)
2314 del_timer(&scan_timer);
Willy Tarreau7005b582008-11-13 17:18:59 -08002315
Costantino Leandro57898132009-02-17 11:10:48 -05002316 if (pprt != NULL) {
Peter Huewe0b0595b2009-09-29 01:22:40 +02002317 if (keypad_enabled) {
Costantino Leandro57898132009-02-17 11:10:48 -05002318 misc_deregister(&keypad_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002319 keypad_initialized = 0;
2320 }
Willy Tarreau7005b582008-11-13 17:18:59 -08002321
Costantino Leandro57898132009-02-17 11:10:48 -05002322 if (lcd_enabled) {
2323 panel_lcd_print("\x0cLCD driver " PANEL_VERSION
2324 "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2325 misc_deregister(&lcd_dev);
Peter Huewe0b0595b2009-09-29 01:22:40 +02002326 lcd_initialized = 0;
Costantino Leandro57898132009-02-17 11:10:48 -05002327 }
2328
2329 /* TODO: free all input signals */
2330 parport_release(pprt);
2331 parport_unregister_device(pprt);
Peter Huewe060132a2010-07-07 04:52:16 +02002332 pprt = NULL;
Willy Tarreau698b1512008-11-22 11:29:33 +01002333 }
Willy Tarreau698b1512008-11-22 11:29:33 +01002334 parport_unregister_driver(&panel_driver);
Willy Tarreau7005b582008-11-13 17:18:59 -08002335}
Willy Tarreau7005b582008-11-13 17:18:59 -08002336
Willy Tarreau7005b582008-11-13 17:18:59 -08002337module_init(panel_init_module);
2338module_exit(panel_cleanup_module);
2339MODULE_AUTHOR("Willy Tarreau");
2340MODULE_LICENSE("GPL");
Willy Tarreau7005b582008-11-13 17:18:59 -08002341
2342/*
2343 * Local variables:
2344 * c-indent-level: 4
2345 * tab-width: 8
2346 * End:
2347 */