panel.c 46.7 KB
Newer Older
W
Willy Tarreau 已提交
1
/*
2 3
 * Front panel driver for Linux
 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
4
 * Copyright (C) 2016-2017 Glider bvba
W
Willy Tarreau 已提交
5
 *
6 7 8 9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
W
Willy Tarreau 已提交
10
 *
11 12
 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
 * connected to a parallel printer port.
W
Willy Tarreau 已提交
13
 *
14 15 16
 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
 * serial module compatible with Samsung's KS0074. The pins may be connected in
 * any combination, everything is programmable.
W
Willy Tarreau 已提交
17
 *
18 19 20
 * The keypad consists in a matrix of push buttons connecting input pins to
 * data output pins or to the ground. The combinations have to be hard-coded
 * in the driver, though several profiles exist and adding new ones is easy.
W
Willy Tarreau 已提交
21
 *
22 23
 * Several profiles are provided for commonly found LCD+keypad modules on the
 * market, such as those found in Nexcom's appliances.
W
Willy Tarreau 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * FIXME:
 *      - the initialization/deinitialization process is very dirty and should
 *        be rewritten. It may even be buggy.
 *
 * TODO:
 *	- document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
 *      - make the LCD a part of a virtual screen of Vx*Vy
 *	- make the inputs list smp-safe
 *      - change the keyboard to a double mapping : signals -> key_id -> values
 *        so that applications can change values without knowing signals
 *
 */

38 39
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

W
Willy Tarreau 已提交
40 41 42 43 44 45 46 47 48
#include <linux/module.h>

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/miscdevice.h>
49
#include <linux/slab.h>
W
Willy Tarreau 已提交
50 51 52 53
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/delay.h>
54
#include <linux/kernel.h>
W
Willy Tarreau 已提交
55 56 57 58
#include <linux/ctype.h>
#include <linux/parport.h>
#include <linux/list.h>

59
#include <linux/io.h>
60
#include <linux/uaccess.h>
W
Willy Tarreau 已提交
61

62 63
#include <misc/charlcd.h>

W
Willy Tarreau 已提交
64 65 66 67 68 69
#define KEYPAD_MINOR		185

#define LCD_MAXBYTES		256	/* max burst write */

#define KEYPAD_BUFFER		64

70
/* poll the keyboard this every second */
71
#define INPUT_POLL_TIME		(HZ / 50)
72 73 74 75 76
/* a key starts to repeat after this times INPUT_POLL_TIME */
#define KEYPAD_REP_START	(10)
/* a key repeats this times INPUT_POLL_TIME */
#define KEYPAD_REP_DELAY	(2)

W
Willy Tarreau 已提交
77 78 79
/* converts an r_str() input to an active high, bits string : 000BAOSE */
#define PNL_PINPUT(a)		((((unsigned char)(a)) ^ 0x7F) >> 3)

80 81 82 83 84
#define PNL_PBUSY		0x80	/* inverted input, active low */
#define PNL_PACK		0x40	/* direct input, active low */
#define PNL_POUTPA		0x20	/* direct input, active high */
#define PNL_PSELECD		0x10	/* direct input, active high */
#define PNL_PERRORP		0x08	/* direct input, active low */
W
Willy Tarreau 已提交
85

86
#define PNL_PBIDIR		0x20	/* bi-directional ports */
87 88
/* high to read data in or-ed with data out */
#define PNL_PINTEN		0x10
89 90 91 92
#define PNL_PSELECP		0x08	/* inverted output, active low */
#define PNL_PINITP		0x04	/* direct output, active low */
#define PNL_PAUTOLF		0x02	/* inverted output, active low */
#define PNL_PSTROBE		0x01	/* inverted output */
W
Willy Tarreau 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

#define PNL_PD0			0x01
#define PNL_PD1			0x02
#define PNL_PD2			0x04
#define PNL_PD3			0x08
#define PNL_PD4			0x10
#define PNL_PD5			0x20
#define PNL_PD6			0x40
#define PNL_PD7			0x80

#define PIN_NONE		0
#define PIN_STROBE		1
#define PIN_D0			2
#define PIN_D1			3
#define PIN_D2			4
#define PIN_D3			5
#define PIN_D4			6
#define PIN_D5			7
#define PIN_D6			8
#define PIN_D7			9
#define PIN_AUTOLF		14
#define PIN_INITP		16
#define PIN_SELECP		17
#define PIN_NOT_SET		127

118 119
#define NOT_SET			-1

W
Willy Tarreau 已提交
120 121 122 123
/* macros to simplify use of the parallel port */
#define r_ctr(x)        (parport_read_control((x)->port))
#define r_dtr(x)        (parport_read_data((x)->port))
#define r_str(x)        (parport_read_status((x)->port))
124 125
#define w_ctr(x, y)     (parport_write_control((x)->port, (y)))
#define w_dtr(x, y)     (parport_write_data((x)->port, (y)))
W
Willy Tarreau 已提交
126 127

/* this defines which bits are to be used and which ones to be ignored */
128 129 130 131
/* logical or of the output bits involved in the scan matrix */
static __u8 scan_mask_o;
/* logical or of the input bits involved in the scan matrix */
static __u8 scan_mask_i;
W
Willy Tarreau 已提交
132 133

enum input_type {
134 135
	INPUT_TYPE_STD,
	INPUT_TYPE_KBD,
W
Willy Tarreau 已提交
136 137 138
};

enum input_state {
139 140 141 142
	INPUT_ST_LOW,
	INPUT_ST_RISING,
	INPUT_ST_HIGH,
	INPUT_ST_FALLING,
W
Willy Tarreau 已提交
143 144 145
};

struct logical_input {
146
	struct list_head list;
147 148
	__u64 mask;
	__u64 value;
149 150 151 152 153 154
	enum input_type type;
	enum input_state state;
	__u8 rise_time, fall_time;
	__u8 rise_timer, fall_timer, high_timer;

	union {
155
		struct {	/* valid when type == INPUT_TYPE_STD */
156 157
			void (*press_fct)(int);
			void (*release_fct)(int);
158 159 160
			int press_data;
			int release_data;
		} std;
161 162
		struct {	/* valid when type == INPUT_TYPE_KBD */
			/* strings can be non null-terminated */
163 164 165 166 167
			char press_str[sizeof(void *) + sizeof(int)];
			char repeat_str[sizeof(void *) + sizeof(int)];
			char release_str[sizeof(void *) + sizeof(int)];
		} kbd;
	} u;
W
Willy Tarreau 已提交
168 169
};

170
static LIST_HEAD(logical_inputs);	/* list of all defined logical inputs */
W
Willy Tarreau 已提交
171 172 173 174 175 176 177

/* physical contacts history
 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
 * corresponds to the ground.
 * Within each group, bits are stored in the same order as read on the port :
 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
178
 * So, each __u64 is represented like this :
W
Willy Tarreau 已提交
179 180 181
 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
 */
182 183

/* what has just been read from the I/O ports */
184
static __u64 phys_read;
185
/* previous phys_read */
186
static __u64 phys_read_prev;
187
/* stabilized phys_read (phys_read|phys_read_prev) */
188
static __u64 phys_curr;
189
/* previous phys_curr */
190
static __u64 phys_prev;
191 192
/* 0 means that at least one logical signal needs be computed */
static char inputs_stable;
W
Willy Tarreau 已提交
193 194

/* these variables are specific to the keypad */
195 196 197 198
static struct {
	bool enabled;
} keypad;

W
Willy Tarreau 已提交
199
static char keypad_buffer[KEYPAD_BUFFER];
200 201 202
static int keypad_buflen;
static int keypad_start;
static char keypressed;
W
Willy Tarreau 已提交
203 204 205
static wait_queue_head_t keypad_read_wait;

/* lcd-specific variables */
206 207
static struct {
	bool enabled;
208 209
	bool initialized;

210 211
	int charset;
	int proto;
212

213 214 215 216 217 218 219 220 221
	/* TODO: use union here? */
	struct {
		int e;
		int rs;
		int rw;
		int cl;
		int da;
		int bl;
	} pins;
222

223
	struct charlcd *charlcd;
224
} lcd;
225

226 227 228
/* Needed only for init */
static int selected_lcd_type = NOT_SET;

W
Willy Tarreau 已提交
229 230 231 232 233
/*
 * Bit masks to convert LCD signals to parallel port outputs.
 * _d_ are values for data port, _c_ are for control port.
 * [0] = signal OFF, [1] = signal ON, [2] = mask
 */
234 235 236
#define BIT_CLR		0
#define BIT_SET		1
#define BIT_MSK		2
W
Willy Tarreau 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
#define BIT_STATES	3
/*
 * one entry for each bit on the LCD
 */
#define LCD_BIT_E	0
#define LCD_BIT_RS	1
#define LCD_BIT_RW	2
#define LCD_BIT_BL	3
#define LCD_BIT_CL	4
#define LCD_BIT_DA	5
#define LCD_BITS	6

/*
 * each bit can be either connected to a DATA or CTRL port
 */
#define LCD_PORT_C	0
#define LCD_PORT_D	1
#define LCD_PORTS	2

static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];

/*
 * LCD protocols
 */
#define LCD_PROTO_PARALLEL      0
#define LCD_PROTO_SERIAL        1
263
#define LCD_PROTO_TI_DA8XX_LCD	2
W
Willy Tarreau 已提交
264 265 266 267 268 269 270 271 272 273 274

/*
 * LCD character sets
 */
#define LCD_CHARSET_NORMAL      0
#define LCD_CHARSET_KS0074      1

/*
 * LCD types
 */
#define LCD_TYPE_NONE		0
S
Sudip Mukherjee 已提交
275 276 277 278 279
#define LCD_TYPE_CUSTOM		1
#define LCD_TYPE_OLD		2
#define LCD_TYPE_KS0074		3
#define LCD_TYPE_HANTRONIX	4
#define LCD_TYPE_NEXCOM		5
W
Willy Tarreau 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

/*
 * keypad types
 */
#define KEYPAD_TYPE_NONE	0
#define KEYPAD_TYPE_OLD		1
#define KEYPAD_TYPE_NEW		2
#define KEYPAD_TYPE_NEXCOM	3

/*
 * panel profiles
 */
#define PANEL_PROFILE_CUSTOM	0
#define PANEL_PROFILE_OLD	1
#define PANEL_PROFILE_NEW	2
#define PANEL_PROFILE_HANTRONIX	3
#define PANEL_PROFILE_NEXCOM	4
#define PANEL_PROFILE_LARGE	5

/*
 * Construct custom config from the kernel's configuration
 */
#define DEFAULT_PARPORT         0
303
#define DEFAULT_PROFILE         PANEL_PROFILE_LARGE
304 305
#define DEFAULT_KEYPAD_TYPE     KEYPAD_TYPE_OLD
#define DEFAULT_LCD_TYPE        LCD_TYPE_OLD
306
#define DEFAULT_LCD_HEIGHT      2
W
Willy Tarreau 已提交
307 308 309
#define DEFAULT_LCD_WIDTH       40
#define DEFAULT_LCD_BWIDTH      40
#define DEFAULT_LCD_HWIDTH      64
310
#define DEFAULT_LCD_CHARSET     LCD_CHARSET_NORMAL
W
Willy Tarreau 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324
#define DEFAULT_LCD_PROTO       LCD_PROTO_PARALLEL

#define DEFAULT_LCD_PIN_E       PIN_AUTOLF
#define DEFAULT_LCD_PIN_RS      PIN_SELECP
#define DEFAULT_LCD_PIN_RW      PIN_INITP
#define DEFAULT_LCD_PIN_SCL     PIN_STROBE
#define DEFAULT_LCD_PIN_SDA     PIN_D0
#define DEFAULT_LCD_PIN_BL      PIN_NOT_SET

#ifdef CONFIG_PANEL_PARPORT
#undef DEFAULT_PARPORT
#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
#endif

325 326 327 328 329
#ifdef CONFIG_PANEL_PROFILE
#undef DEFAULT_PROFILE
#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
#endif

330
#if DEFAULT_PROFILE == 0	/* custom */
W
Willy Tarreau 已提交
331
#ifdef CONFIG_PANEL_KEYPAD
332 333
#undef DEFAULT_KEYPAD_TYPE
#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
W
Willy Tarreau 已提交
334 335 336
#endif

#ifdef CONFIG_PANEL_LCD
337 338
#undef DEFAULT_LCD_TYPE
#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
W
Willy Tarreau 已提交
339 340
#endif

341 342 343 344 345
#ifdef CONFIG_PANEL_LCD_HEIGHT
#undef DEFAULT_LCD_HEIGHT
#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
#endif

W
Willy Tarreau 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
#ifdef CONFIG_PANEL_LCD_WIDTH
#undef DEFAULT_LCD_WIDTH
#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
#endif

#ifdef CONFIG_PANEL_LCD_BWIDTH
#undef DEFAULT_LCD_BWIDTH
#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
#endif

#ifdef CONFIG_PANEL_LCD_HWIDTH
#undef DEFAULT_LCD_HWIDTH
#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
#endif

361 362 363
#ifdef CONFIG_PANEL_LCD_CHARSET
#undef DEFAULT_LCD_CHARSET
#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
W
Willy Tarreau 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#endif

#ifdef CONFIG_PANEL_LCD_PROTO
#undef DEFAULT_LCD_PROTO
#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
#endif

#ifdef CONFIG_PANEL_LCD_PIN_E
#undef DEFAULT_LCD_PIN_E
#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
#endif

#ifdef CONFIG_PANEL_LCD_PIN_RS
#undef DEFAULT_LCD_PIN_RS
#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
#endif

#ifdef CONFIG_PANEL_LCD_PIN_RW
#undef DEFAULT_LCD_PIN_RW
#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
#endif

#ifdef CONFIG_PANEL_LCD_PIN_SCL
#undef DEFAULT_LCD_PIN_SCL
#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
#endif

#ifdef CONFIG_PANEL_LCD_PIN_SDA
#undef DEFAULT_LCD_PIN_SDA
#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
#endif

#ifdef CONFIG_PANEL_LCD_PIN_BL
#undef DEFAULT_LCD_PIN_BL
#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
#endif

#endif /* DEFAULT_PROFILE == 0 */

/* global variables */
404 405 406 407

/* Device single-open policy control */
static atomic_t keypad_available = ATOMIC_INIT(1);

408
static struct pardevice *pprt;
W
Willy Tarreau 已提交
409

410
static int keypad_initialized;
W
Willy Tarreau 已提交
411

412
static DEFINE_SPINLOCK(pprt_lock);
W
Willy Tarreau 已提交
413 414
static struct timer_list scan_timer;

415
MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
416

417
static int parport = DEFAULT_PARPORT;
418 419
module_param(parport, int, 0000);
MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
420

421 422 423 424 425 426
static int profile = DEFAULT_PROFILE;
module_param(profile, int, 0000);
MODULE_PARM_DESC(profile,
		 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
		 "4=16x2 nexcom; default=40x2, old kp");

427
static int keypad_type = NOT_SET;
428 429 430 431
module_param(keypad_type, int, 0000);
MODULE_PARM_DESC(keypad_type,
		 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");

432
static int lcd_type = NOT_SET;
433 434
module_param(lcd_type, int, 0000);
MODULE_PARM_DESC(lcd_type,
S
Sudip Mukherjee 已提交
435
		 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
436

437
static int lcd_height = NOT_SET;
438 439
module_param(lcd_height, int, 0000);
MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
440

441
static int lcd_width = NOT_SET;
442 443
module_param(lcd_width, int, 0000);
MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
444

445
static int lcd_bwidth = NOT_SET;	/* internal buffer width (usually 40) */
446 447
module_param(lcd_bwidth, int, 0000);
MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
448

449
static int lcd_hwidth = NOT_SET;	/* hardware buffer width (usually 64) */
450 451
module_param(lcd_hwidth, int, 0000);
MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
452

453
static int lcd_charset = NOT_SET;
454 455
module_param(lcd_charset, int, 0000);
MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
456

457
static int lcd_proto = NOT_SET;
458
module_param(lcd_proto, int, 0000);
459
MODULE_PARM_DESC(lcd_proto,
460
		 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
461 462 463 464 465 466 467

/*
 * These are the parallel port pins the LCD control signals are connected to.
 * Set this to 0 if the signal is not used. Set it to its opposite value
 * (negative) if the signal is negated. -MAXINT is used to indicate that the
 * pin has not been explicitly specified.
 *
468
 * WARNING! no check will be performed about collisions with keypad !
469 470 471
 */

static int lcd_e_pin  = PIN_NOT_SET;
472 473
module_param(lcd_e_pin, int, 0000);
MODULE_PARM_DESC(lcd_e_pin,
474
		 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
475 476

static int lcd_rs_pin = PIN_NOT_SET;
477 478
module_param(lcd_rs_pin, int, 0000);
MODULE_PARM_DESC(lcd_rs_pin,
479
		 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
480 481

static int lcd_rw_pin = PIN_NOT_SET;
482 483
module_param(lcd_rw_pin, int, 0000);
MODULE_PARM_DESC(lcd_rw_pin,
484
		 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
485

486 487 488 489
static int lcd_cl_pin = PIN_NOT_SET;
module_param(lcd_cl_pin, int, 0000);
MODULE_PARM_DESC(lcd_cl_pin,
		 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
490 491

static int lcd_da_pin = PIN_NOT_SET;
492 493
module_param(lcd_da_pin, int, 0000);
MODULE_PARM_DESC(lcd_da_pin,
494
		 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
495

496 497 498 499 500 501 502
static int lcd_bl_pin = PIN_NOT_SET;
module_param(lcd_bl_pin, int, 0000);
MODULE_PARM_DESC(lcd_bl_pin,
		 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");

/* Deprecated module parameters - consider not using them anymore */

503
static int lcd_enabled = NOT_SET;
504 505 506
module_param(lcd_enabled, int, 0000);
MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");

507
static int keypad_enabled = NOT_SET;
508 509 510
module_param(keypad_enabled, int, 0000);
MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");

W
Willy Tarreau 已提交
511
/* for some LCD drivers (ks0074) we need a charset conversion table. */
512
static const unsigned char lcd_char_conv_ks0074[256] = {
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	/*          0|8   1|9   2|A   3|B   4|C   5|D   6|E   7|F */
	/* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	/* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
	/* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
	/* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
	/* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
	/* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
	/* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
	/* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
	/* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
	/* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
	/* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
	/* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
	/* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
	/* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
	/* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
	/* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
	/* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
	/* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
	/* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
	/* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
	/* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
	/* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
	/* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
	/* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
	/* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
	/* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
	/* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
	/* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
	/* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
	/* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
	/* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
	/* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
W
Willy Tarreau 已提交
546 547
};

548
static const char old_keypad_profile[][4][9] = {
549 550 551 552 553 554 555
	{"S0", "Left\n", "Left\n", ""},
	{"S1", "Down\n", "Down\n", ""},
	{"S2", "Up\n", "Up\n", ""},
	{"S3", "Right\n", "Right\n", ""},
	{"S4", "Esc\n", "Esc\n", ""},
	{"S5", "Ret\n", "Ret\n", ""},
	{"", "", "", ""}
W
Willy Tarreau 已提交
556 557 558
};

/* signals, press, repeat, release */
559
static const char new_keypad_profile[][4][9] = {
560 561 562 563 564 565 566 567 568
	{"S0", "Left\n", "Left\n", ""},
	{"S1", "Down\n", "Down\n", ""},
	{"S2", "Up\n", "Up\n", ""},
	{"S3", "Right\n", "Right\n", ""},
	{"S4s5", "", "Esc\n", "Esc\n"},
	{"s4S5", "", "Ret\n", "Ret\n"},
	{"S4S5", "Help\n", "", ""},
	/* add new signals above this line */
	{"", "", "", ""}
W
Willy Tarreau 已提交
569 570 571
};

/* signals, press, repeat, release */
572
static const char nexcom_keypad_profile[][4][9] = {
573 574 575 576 577 578
	{"a-p-e-", "Down\n", "Down\n", ""},
	{"a-p-E-", "Ret\n", "Ret\n", ""},
	{"a-P-E-", "Esc\n", "Esc\n", ""},
	{"a-P-e-", "Up\n", "Up\n", ""},
	/* add new signals above this line */
	{"", "", "", ""}
W
Willy Tarreau 已提交
579 580
};

581
static const char (*keypad_profile)[4][9] = old_keypad_profile;
W
Willy Tarreau 已提交
582

583 584 585 586 587 588 589 590 591 592 593 594
static DECLARE_BITMAP(bits, LCD_BITS);

static void lcd_get_bits(unsigned int port, int *val)
{
	unsigned int bit, state;

	for (bit = 0; bit < LCD_BITS; bit++) {
		state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
		*val &= lcd_bits[port][bit][BIT_MSK];
		*val |= lcd_bits[port][bit][state];
	}
}
W
Willy Tarreau 已提交
595 596

/* sets data port bits according to current signals values */
597 598
static int set_data_bits(void)
{
599
	int val;
600 601

	val = r_dtr(pprt);
602
	lcd_get_bits(LCD_PORT_D, &val);
603 604
	w_dtr(pprt, val);
	return val;
W
Willy Tarreau 已提交
605 606 607
}

/* sets ctrl port bits according to current signals values */
608 609
static int set_ctrl_bits(void)
{
610
	int val;
611 612

	val = r_ctr(pprt);
613
	lcd_get_bits(LCD_PORT_C, &val);
614 615
	w_ctr(pprt, val);
	return val;
W
Willy Tarreau 已提交
616 617 618
}

/* sets ctrl & data port bits according to current signals values */
619
static void panel_set_bits(void)
620 621 622
{
	set_data_bits();
	set_ctrl_bits();
W
Willy Tarreau 已提交
623 624 625 626 627 628 629 630 631 632 633
}

/*
 * Converts a parallel port pin (from -25 to 25) to data and control ports
 * masks, and data and control port bits. The signal will be considered
 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
 *
 * Result will be used this way :
 *   out(dport, in(dport) & d_val[2] | d_val[signal_state])
 *   out(cport, in(cport) & c_val[2] | c_val[signal_state])
 */
634
static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
635 636 637
{
	int d_bit, c_bit, inv;

638 639 640 641 642 643
	d_val[0] = 0;
	c_val[0] = 0;
	d_val[1] = 0;
	c_val[1] = 0;
	d_val[2] = 0xFF;
	c_val[2] = 0xFF;
644 645 646 647 648 649 650 651

	if (pin == 0)
		return;

	inv = (pin < 0);
	if (inv)
		pin = -pin;

652 653
	d_bit = 0;
	c_bit = 0;
654 655 656 657 658 659 660 661 662 663 664 665 666

	switch (pin) {
	case PIN_STROBE:	/* strobe, inverted */
		c_bit = PNL_PSTROBE;
		inv = !inv;
		break;
	case PIN_D0...PIN_D7:	/* D0 - D7 = 2 - 9 */
		d_bit = 1 << (pin - 2);
		break;
	case PIN_AUTOLF:	/* autofeed, inverted */
		c_bit = PNL_PAUTOLF;
		inv = !inv;
		break;
667
	case PIN_INITP:		/* init, direct */
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
		c_bit = PNL_PINITP;
		break;
	case PIN_SELECP:	/* select_in, inverted */
		c_bit = PNL_PSELECP;
		inv = !inv;
		break;
	default:		/* unknown pin, ignore */
		break;
	}

	if (c_bit) {
		c_val[2] &= ~c_bit;
		c_val[!inv] = c_bit;
	} else if (d_bit) {
		d_val[2] &= ~d_bit;
		d_val[!inv] = d_bit;
	}
W
Willy Tarreau 已提交
685 686
}

687 688 689 690
/*
 * send a serial byte to the LCD panel. The caller is responsible for locking
 * if needed.
 */
691 692 693 694
static void lcd_send_serial(int byte)
{
	int bit;

695 696 697 698
	/*
	 * the data bit is set on D0, and the clock on STROBE.
	 * LCD reads D0 on STROBE's rising edge.
	 */
699
	for (bit = 0; bit < 8; bit++) {
700
		clear_bit(LCD_BIT_CL, bits);	/* CLK low */
701
		panel_set_bits();
702 703 704 705 706 707
		if (byte & 1) {
			set_bit(LCD_BIT_DA, bits);
		} else {
			clear_bit(LCD_BIT_DA, bits);
		}

708
		panel_set_bits();
709
		udelay(2);  /* maintain the data during 2 us before CLK up */
710
		set_bit(LCD_BIT_CL, bits);	/* CLK high */
711
		panel_set_bits();
712
		udelay(1);  /* maintain the strobe during 1 us */
713 714
		byte >>= 1;
	}
W
Willy Tarreau 已提交
715 716 717
}

/* turn the backlight on or off */
718
static void lcd_backlight(struct charlcd *charlcd, int on)
719
{
720 721 722
	if (lcd.pins.bl == PIN_NONE)
		return;

723
	/* The backlight is activated by setting the AUTOFEED line to +5V  */
724
	spin_lock_irq(&pprt_lock);
725 726 727 728
	if (on)
		set_bit(LCD_BIT_BL, bits);
	else
		clear_bit(LCD_BIT_BL, bits);
729
	panel_set_bits();
730
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
731 732 733
}

/* send a command to the LCD panel in serial mode */
734
static void lcd_write_cmd_s(struct charlcd *charlcd, int cmd)
735
{
736
	spin_lock_irq(&pprt_lock);
737 738 739
	lcd_send_serial(0x1F);	/* R/W=W, RS=0 */
	lcd_send_serial(cmd & 0x0F);
	lcd_send_serial((cmd >> 4) & 0x0F);
740
	udelay(40);		/* the shortest command takes at least 40 us */
741
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
742 743 744
}

/* send data to the LCD panel in serial mode */
745
static void lcd_write_data_s(struct charlcd *charlcd, int data)
746
{
747
	spin_lock_irq(&pprt_lock);
748 749 750
	lcd_send_serial(0x5F);	/* R/W=W, RS=1 */
	lcd_send_serial(data & 0x0F);
	lcd_send_serial((data >> 4) & 0x0F);
751
	udelay(40);		/* the shortest data takes at least 40 us */
752
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
753 754 755
}

/* send a command to the LCD panel in 8 bits parallel mode */
756
static void lcd_write_cmd_p8(struct charlcd *charlcd, int cmd)
757
{
758
	spin_lock_irq(&pprt_lock);
759 760
	/* present the data to the data port */
	w_dtr(pprt, cmd);
761
	udelay(20);	/* maintain the data during 20 us before the strobe */
W
Willy Tarreau 已提交
762

763 764 765
	set_bit(LCD_BIT_E, bits);
	clear_bit(LCD_BIT_RS, bits);
	clear_bit(LCD_BIT_RW, bits);
766
	set_ctrl_bits();
W
Willy Tarreau 已提交
767

768
	udelay(40);	/* maintain the strobe during 40 us */
W
Willy Tarreau 已提交
769

770
	clear_bit(LCD_BIT_E, bits);
771
	set_ctrl_bits();
W
Willy Tarreau 已提交
772

773
	udelay(120);	/* the shortest command takes at least 120 us */
774
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
775 776 777
}

/* send data to the LCD panel in 8 bits parallel mode */
778
static void lcd_write_data_p8(struct charlcd *charlcd, int data)
779
{
780
	spin_lock_irq(&pprt_lock);
781 782
	/* present the data to the data port */
	w_dtr(pprt, data);
783
	udelay(20);	/* maintain the data during 20 us before the strobe */
W
Willy Tarreau 已提交
784

785 786 787
	set_bit(LCD_BIT_E, bits);
	set_bit(LCD_BIT_RS, bits);
	clear_bit(LCD_BIT_RW, bits);
788
	set_ctrl_bits();
W
Willy Tarreau 已提交
789

790
	udelay(40);	/* maintain the strobe during 40 us */
W
Willy Tarreau 已提交
791

792
	clear_bit(LCD_BIT_E, bits);
793
	set_ctrl_bits();
W
Willy Tarreau 已提交
794

795
	udelay(45);	/* the shortest data takes at least 45 us */
796
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
797 798
}

799
/* send a command to the TI LCD panel */
800
static void lcd_write_cmd_tilcd(struct charlcd *charlcd, int cmd)
801
{
802
	spin_lock_irq(&pprt_lock);
803 804
	/* present the data to the control port */
	w_ctr(pprt, cmd);
805
	udelay(60);
806
	spin_unlock_irq(&pprt_lock);
807 808 809
}

/* send data to the TI LCD panel */
810
static void lcd_write_data_tilcd(struct charlcd *charlcd, int data)
811
{
812
	spin_lock_irq(&pprt_lock);
813 814
	/* present the data to the data port */
	w_dtr(pprt, data);
815
	udelay(60);
816
	spin_unlock_irq(&pprt_lock);
817 818
}

W
Willy Tarreau 已提交
819
/* fills the display with spaces and resets X/Y */
820
static void lcd_clear_fast_s(struct charlcd *charlcd)
821 822
{
	int pos;
823

824
	spin_lock_irq(&pprt_lock);
825
	for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
826 827 828
		lcd_send_serial(0x5F);	/* R/W=W, RS=1 */
		lcd_send_serial(' ' & 0x0F);
		lcd_send_serial((' ' >> 4) & 0x0F);
829
		/* the shortest data takes at least 40 us */
830
		udelay(40);
831
	}
832
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
833 834 835
}

/* fills the display with spaces and resets X/Y */
836
static void lcd_clear_fast_p8(struct charlcd *charlcd)
837 838
{
	int pos;
839

840
	spin_lock_irq(&pprt_lock);
841
	for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
842 843
		/* present the data to the data port */
		w_dtr(pprt, ' ');
844 845

		/* maintain the data during 20 us before the strobe */
846
		udelay(20);
W
Willy Tarreau 已提交
847

848 849 850
		set_bit(LCD_BIT_E, bits);
		set_bit(LCD_BIT_RS, bits);
		clear_bit(LCD_BIT_RW, bits);
851
		set_ctrl_bits();
W
Willy Tarreau 已提交
852

853
		/* maintain the strobe during 40 us */
854
		udelay(40);
W
Willy Tarreau 已提交
855

856
		clear_bit(LCD_BIT_E, bits);
857
		set_ctrl_bits();
W
Willy Tarreau 已提交
858

859
		/* the shortest data takes at least 45 us */
860
		udelay(45);
861
	}
862
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
863 864
}

865
/* fills the display with spaces and resets X/Y */
866
static void lcd_clear_fast_tilcd(struct charlcd *charlcd)
867 868
{
	int pos;
869

870
	spin_lock_irq(&pprt_lock);
871
	for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
872 873
		/* present the data to the data port */
		w_dtr(pprt, ' ');
874
		udelay(60);
875 876
	}

877
	spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
878 879
}

880 881 882 883 884 885
static struct charlcd_ops charlcd_serial_ops = {
	.write_cmd	= lcd_write_cmd_s,
	.write_data	= lcd_write_data_s,
	.clear_fast	= lcd_clear_fast_s,
	.backlight	= lcd_backlight,
};
W
Willy Tarreau 已提交
886

887 888 889 890 891 892
static struct charlcd_ops charlcd_parallel_ops = {
	.write_cmd	= lcd_write_cmd_p8,
	.write_data	= lcd_write_data_p8,
	.clear_fast	= lcd_clear_fast_p8,
	.backlight	= lcd_backlight,
};
W
Willy Tarreau 已提交
893

894 895 896 897 898 899
static struct charlcd_ops charlcd_tilcd_ops = {
	.write_cmd	= lcd_write_cmd_tilcd,
	.write_data	= lcd_write_data_tilcd,
	.clear_fast	= lcd_clear_fast_tilcd,
	.backlight	= lcd_backlight,
};
W
Willy Tarreau 已提交
900

901 902
/* initialize the LCD driver */
static void lcd_init(void)
903
{
904
	struct charlcd *charlcd;
905

906 907 908
	charlcd = charlcd_alloc(0);
	if (!charlcd)
		return;
909

910
	/*
911 912
	 * Init lcd struct with load-time values to preserve exact
	 * current functionality (at least for now).
913
	 */
914 915 916 917
	charlcd->height = lcd_height;
	charlcd->width = lcd_width;
	charlcd->bwidth = lcd_bwidth;
	charlcd->hwidth = lcd_hwidth;
918

919
	switch (selected_lcd_type) {
920 921
	case LCD_TYPE_OLD:
		/* parallel mode, 8 bits */
922 923 924 925 926
		lcd.proto = LCD_PROTO_PARALLEL;
		lcd.charset = LCD_CHARSET_NORMAL;
		lcd.pins.e = PIN_STROBE;
		lcd.pins.rs = PIN_AUTOLF;

927 928 929 930
		charlcd->width = 40;
		charlcd->bwidth = 40;
		charlcd->hwidth = 64;
		charlcd->height = 2;
W
Willy Tarreau 已提交
931
		break;
932 933
	case LCD_TYPE_KS0074:
		/* serial mode, ks0074 */
934 935 936 937 938 939
		lcd.proto = LCD_PROTO_SERIAL;
		lcd.charset = LCD_CHARSET_KS0074;
		lcd.pins.bl = PIN_AUTOLF;
		lcd.pins.cl = PIN_STROBE;
		lcd.pins.da = PIN_D0;

940 941 942 943
		charlcd->width = 16;
		charlcd->bwidth = 40;
		charlcd->hwidth = 16;
		charlcd->height = 2;
W
Willy Tarreau 已提交
944
		break;
945 946
	case LCD_TYPE_NEXCOM:
		/* parallel mode, 8 bits, generic */
947 948 949 950 951 952
		lcd.proto = LCD_PROTO_PARALLEL;
		lcd.charset = LCD_CHARSET_NORMAL;
		lcd.pins.e = PIN_AUTOLF;
		lcd.pins.rs = PIN_SELECP;
		lcd.pins.rw = PIN_INITP;

953 954 955 956
		charlcd->width = 16;
		charlcd->bwidth = 40;
		charlcd->hwidth = 64;
		charlcd->height = 2;
W
Willy Tarreau 已提交
957
		break;
958 959
	case LCD_TYPE_CUSTOM:
		/* customer-defined */
960 961
		lcd.proto = DEFAULT_LCD_PROTO;
		lcd.charset = DEFAULT_LCD_CHARSET;
W
Willy Tarreau 已提交
962 963
		/* default geometry will be set later */
		break;
964 965
	case LCD_TYPE_HANTRONIX:
		/* parallel mode, 8 bits, hantronix-like */
966
	default:
967 968 969 970 971
		lcd.proto = LCD_PROTO_PARALLEL;
		lcd.charset = LCD_CHARSET_NORMAL;
		lcd.pins.e = PIN_STROBE;
		lcd.pins.rs = PIN_SELECP;

972 973 974 975
		charlcd->width = 16;
		charlcd->bwidth = 40;
		charlcd->hwidth = 64;
		charlcd->height = 2;
W
Willy Tarreau 已提交
976
		break;
977
	}
W
Willy Tarreau 已提交
978

979
	/* Overwrite with module params set on loading */
980
	if (lcd_height != NOT_SET)
981
		charlcd->height = lcd_height;
982
	if (lcd_width != NOT_SET)
983
		charlcd->width = lcd_width;
984
	if (lcd_bwidth != NOT_SET)
985
		charlcd->bwidth = lcd_bwidth;
986
	if (lcd_hwidth != NOT_SET)
987
		charlcd->hwidth = lcd_hwidth;
988
	if (lcd_charset != NOT_SET)
989
		lcd.charset = lcd_charset;
990
	if (lcd_proto != NOT_SET)
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
		lcd.proto = lcd_proto;
	if (lcd_e_pin != PIN_NOT_SET)
		lcd.pins.e = lcd_e_pin;
	if (lcd_rs_pin != PIN_NOT_SET)
		lcd.pins.rs = lcd_rs_pin;
	if (lcd_rw_pin != PIN_NOT_SET)
		lcd.pins.rw = lcd_rw_pin;
	if (lcd_cl_pin != PIN_NOT_SET)
		lcd.pins.cl = lcd_cl_pin;
	if (lcd_da_pin != PIN_NOT_SET)
		lcd.pins.da = lcd_da_pin;
	if (lcd_bl_pin != PIN_NOT_SET)
		lcd.pins.bl = lcd_bl_pin;

1005
	/* this is used to catch wrong and default values */
1006 1007 1008 1009 1010 1011 1012 1013
	if (charlcd->width <= 0)
		charlcd->width = DEFAULT_LCD_WIDTH;
	if (charlcd->bwidth <= 0)
		charlcd->bwidth = DEFAULT_LCD_BWIDTH;
	if (charlcd->hwidth <= 0)
		charlcd->hwidth = DEFAULT_LCD_HWIDTH;
	if (charlcd->height <= 0)
		charlcd->height = DEFAULT_LCD_HEIGHT;
1014 1015

	if (lcd.proto == LCD_PROTO_SERIAL) {	/* SERIAL */
1016
		charlcd->ops = &charlcd_serial_ops;
1017

1018 1019 1020 1021
		if (lcd.pins.cl == PIN_NOT_SET)
			lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
		if (lcd.pins.da == PIN_NOT_SET)
			lcd.pins.da = DEFAULT_LCD_PIN_SDA;
1022

1023
	} else if (lcd.proto == LCD_PROTO_PARALLEL) {	/* PARALLEL */
1024
		charlcd->ops = &charlcd_parallel_ops;
1025

1026 1027 1028 1029 1030 1031
		if (lcd.pins.e == PIN_NOT_SET)
			lcd.pins.e = DEFAULT_LCD_PIN_E;
		if (lcd.pins.rs == PIN_NOT_SET)
			lcd.pins.rs = DEFAULT_LCD_PIN_RS;
		if (lcd.pins.rw == PIN_NOT_SET)
			lcd.pins.rw = DEFAULT_LCD_PIN_RW;
1032
	} else {
1033
		charlcd->ops = &charlcd_tilcd_ops;
1034
	}
W
Willy Tarreau 已提交
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	if (lcd.pins.bl == PIN_NOT_SET)
		lcd.pins.bl = DEFAULT_LCD_PIN_BL;

	if (lcd.pins.e == PIN_NOT_SET)
		lcd.pins.e = PIN_NONE;
	if (lcd.pins.rs == PIN_NOT_SET)
		lcd.pins.rs = PIN_NONE;
	if (lcd.pins.rw == PIN_NOT_SET)
		lcd.pins.rw = PIN_NONE;
	if (lcd.pins.bl == PIN_NOT_SET)
		lcd.pins.bl = PIN_NONE;
	if (lcd.pins.cl == PIN_NOT_SET)
		lcd.pins.cl = PIN_NONE;
	if (lcd.pins.da == PIN_NOT_SET)
		lcd.pins.da = PIN_NONE;

	if (lcd.charset == NOT_SET)
		lcd.charset = DEFAULT_LCD_CHARSET;

	if (lcd.charset == LCD_CHARSET_KS0074)
1056
		charlcd->char_conv = lcd_char_conv_ks0074;
1057
	else
1058
		charlcd->char_conv = NULL;
1059

1060
	pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
1061
		    lcd_bits[LCD_PORT_C][LCD_BIT_E]);
1062
	pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
1063
		    lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
1064
	pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
1065
		    lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
1066
	pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
1067
		    lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
1068
	pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
1069
		    lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
1070
	pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
1071 1072
		    lcd_bits[LCD_PORT_C][LCD_BIT_DA]);

1073
	lcd.charlcd = charlcd;
1074
	lcd.initialized = true;
W
Willy Tarreau 已提交
1075 1076 1077 1078 1079 1080
}

/*
 * These are the file operation function for user access to /dev/keypad
 */

1081
static ssize_t keypad_read(struct file *file,
1082
			   char __user *buf, size_t count, loff_t *ppos)
1083 1084
{
	unsigned i = *ppos;
1085
	char __user *tmp = buf;
W
Willy Tarreau 已提交
1086

1087 1088 1089
	if (keypad_buflen == 0) {
		if (file->f_flags & O_NONBLOCK)
			return -EAGAIN;
W
Willy Tarreau 已提交
1090

1091 1092
		if (wait_event_interruptible(keypad_read_wait,
					     keypad_buflen != 0))
1093 1094
			return -EINTR;
	}
W
Willy Tarreau 已提交
1095

1096 1097
	for (; count-- > 0 && (keypad_buflen > 0);
	     ++i, ++tmp, --keypad_buflen) {
1098 1099 1100 1101
		put_user(keypad_buffer[keypad_start], tmp);
		keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
	}
	*ppos = i;
W
Willy Tarreau 已提交
1102

1103
	return tmp - buf;
W
Willy Tarreau 已提交
1104 1105
}

1106 1107
static int keypad_open(struct inode *inode, struct file *file)
{
1108
	if (!atomic_dec_and_test(&keypad_available))
1109
		return -EBUSY;	/* open only once at a time */
W
Willy Tarreau 已提交
1110

1111 1112
	if (file->f_mode & FMODE_WRITE)	/* device is read-only */
		return -EPERM;
W
Willy Tarreau 已提交
1113

1114 1115
	keypad_buflen = 0;	/* flush the buffer on opening */
	return 0;
W
Willy Tarreau 已提交
1116 1117
}

1118 1119
static int keypad_release(struct inode *inode, struct file *file)
{
1120
	atomic_inc(&keypad_available);
1121
	return 0;
W
Willy Tarreau 已提交
1122 1123
}

1124
static const struct file_operations keypad_fops = {
1125 1126 1127
	.read    = keypad_read,		/* read */
	.open    = keypad_open,		/* open */
	.release = keypad_release,	/* close */
1128
	.llseek  = default_llseek,
W
Willy Tarreau 已提交
1129 1130 1131
};

static struct miscdevice keypad_dev = {
1132 1133 1134
	.minor	= KEYPAD_MINOR,
	.name	= "keypad",
	.fops	= &keypad_fops,
W
Willy Tarreau 已提交
1135 1136
};

1137
static void keypad_send_key(const char *string, int max_len)
1138 1139
{
	/* send the key to the device only if a process is attached to it. */
1140
	if (!atomic_read(&keypad_available)) {
1141 1142 1143 1144 1145
		while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
			keypad_buffer[(keypad_start + keypad_buflen++) %
				      KEYPAD_BUFFER] = *string++;
		}
		wake_up_interruptible(&keypad_read_wait);
W
Willy Tarreau 已提交
1146 1147 1148
	}
}

1149 1150 1151
/* this function scans all the bits involving at least one logical signal,
 * and puts the results in the bitfield "phys_read" (one bit per established
 * contact), and sets "phys_read_prev" to "phys_read".
W
Willy Tarreau 已提交
1152
 *
1153 1154 1155 1156 1157
 * Note: to debounce input signals, we will only consider as switched a signal
 * which is stable across 2 measures. Signals which are different between two
 * reads will be kept as they previously were in their logical form (phys_prev).
 * A signal which has just switched will have a 1 in
 * (phys_read ^ phys_read_prev).
W
Willy Tarreau 已提交
1158
 */
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
static void phys_scan_contacts(void)
{
	int bit, bitval;
	char oldval;
	char bitmask;
	char gndmask;

	phys_prev = phys_curr;
	phys_read_prev = phys_read;
	phys_read = 0;		/* flush all signals */

1170 1171 1172 1173 1174 1175 1176 1177 1178
	/* keep track of old value, with all outputs disabled */
	oldval = r_dtr(pprt) | scan_mask_o;
	/* activate all keyboard outputs (active low) */
	w_dtr(pprt, oldval & ~scan_mask_o);

	/* will have a 1 for each bit set to gnd */
	bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
	/* disable all matrix signals */
	w_dtr(pprt, oldval);
1179 1180 1181

	/* now that all outputs are cleared, the only active input bits are
	 * directly connected to the ground
W
Willy Tarreau 已提交
1182
	 */
1183

1184 1185 1186 1187
	/* 1 for each grounded input */
	gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;

	/* grounded inputs are signals 40-44 */
1188
	phys_read |= (__u64)gndmask << 40;
W
Willy Tarreau 已提交
1189

1190
	if (bitmask != gndmask) {
1191 1192
		/*
		 * since clearing the outputs changed some inputs, we know
1193 1194
		 * that some input signals are currently tied to some outputs.
		 * So we'll scan them.
1195 1196
		 */
		for (bit = 0; bit < 8; bit++) {
1197
			bitval = BIT(bit);
W
Willy Tarreau 已提交
1198

1199 1200 1201 1202 1203
			if (!(scan_mask_o & bitval))	/* skip unused bits */
				continue;

			w_dtr(pprt, oldval & ~bitval);	/* enable this output */
			bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
1204
			phys_read |= (__u64)bitmask << (5 * bit);
1205 1206
		}
		w_dtr(pprt, oldval);	/* disable all outputs */
W
Willy Tarreau 已提交
1207
	}
1208 1209 1210 1211
	/*
	 * this is easy: use old bits when they are flapping,
	 * use new ones when stable
	 */
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
		    (phys_read & ~(phys_read ^ phys_read_prev));
}

static inline int input_state_high(struct logical_input *input)
{
#if 0
	/* FIXME:
	 * this is an invalid test. It tries to catch
	 * transitions from single-key to multiple-key, but
	 * doesn't take into account the contacts polarity.
	 * The only solution to the problem is to parse keys
	 * from the most complex to the simplest combinations,
	 * and mark them as 'caught' once a combination
	 * matches, then unmatch it for all other ones.
	 */

	/* try to catch dangerous transitions cases :
	 * someone adds a bit, so this signal was a false
	 * positive resulting from a transition. We should
	 * invalidate the signal immediately and not call the
	 * release function.
	 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
	 */
1236 1237
	if (((phys_prev & input->mask) == input->value) &&
	    ((phys_curr & input->mask) >  input->value)) {
1238 1239 1240 1241 1242 1243 1244 1245 1246
		input->state = INPUT_ST_LOW; /* invalidate */
		return 1;
	}
#endif

	if ((phys_curr & input->mask) == input->value) {
		if ((input->type == INPUT_TYPE_STD) &&
		    (input->high_timer == 0)) {
			input->high_timer++;
1247
			if (input->u.std.press_fct)
1248 1249 1250 1251 1252 1253 1254
				input->u.std.press_fct(input->u.std.press_data);
		} else if (input->type == INPUT_TYPE_KBD) {
			/* will turn on the light */
			keypressed = 1;

			if (input->high_timer == 0) {
				char *press_str = input->u.kbd.press_str;
1255

1256 1257
				if (press_str[0]) {
					int s = sizeof(input->u.kbd.press_str);
1258

1259 1260
					keypad_send_key(press_str, s);
				}
1261 1262 1263 1264
			}

			if (input->u.kbd.repeat_str[0]) {
				char *repeat_str = input->u.kbd.repeat_str;
1265

1266
				if (input->high_timer >= KEYPAD_REP_START) {
1267
					int s = sizeof(input->u.kbd.repeat_str);
1268

1269
					input->high_timer -= KEYPAD_REP_DELAY;
1270
					keypad_send_key(repeat_str, s);
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
				}
				/* we will need to come back here soon */
				inputs_stable = 0;
			}

			if (input->high_timer < 255)
				input->high_timer++;
		}
		return 1;
	}
1281 1282 1283 1284 1285

	/* else signal falling down. Let's fall through. */
	input->state = INPUT_ST_FALLING;
	input->fall_timer = 0;

1286 1287 1288 1289 1290 1291 1292
	return 0;
}

static inline void input_state_falling(struct logical_input *input)
{
#if 0
	/* FIXME !!! same comment as in input_state_high */
1293 1294
	if (((phys_prev & input->mask) == input->value) &&
	    ((phys_curr & input->mask) >  input->value)) {
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
		input->state = INPUT_ST_LOW;	/* invalidate */
		return;
	}
#endif

	if ((phys_curr & input->mask) == input->value) {
		if (input->type == INPUT_TYPE_KBD) {
			/* will turn on the light */
			keypressed = 1;

			if (input->u.kbd.repeat_str[0]) {
				char *repeat_str = input->u.kbd.repeat_str;
1307

1308 1309
				if (input->high_timer >= KEYPAD_REP_START) {
					int s = sizeof(input->u.kbd.repeat_str);
1310

1311
					input->high_timer -= KEYPAD_REP_DELAY;
1312 1313
					keypad_send_key(repeat_str, s);
				}
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
				/* we will need to come back here soon */
				inputs_stable = 0;
			}

			if (input->high_timer < 255)
				input->high_timer++;
		}
		input->state = INPUT_ST_HIGH;
	} else if (input->fall_timer >= input->fall_time) {
		/* call release event */
		if (input->type == INPUT_TYPE_STD) {
			void (*release_fct)(int) = input->u.std.release_fct;
1326

1327
			if (release_fct)
1328 1329 1330
				release_fct(input->u.std.release_data);
		} else if (input->type == INPUT_TYPE_KBD) {
			char *release_str = input->u.kbd.release_str;
1331

1332 1333
			if (release_str[0]) {
				int s = sizeof(input->u.kbd.release_str);
1334

1335 1336
				keypad_send_key(release_str, s);
			}
1337 1338 1339 1340 1341 1342 1343
		}

		input->state = INPUT_ST_LOW;
	} else {
		input->fall_timer++;
		inputs_stable = 0;
	}
W
Willy Tarreau 已提交
1344 1345
}

1346 1347 1348 1349
static void panel_process_inputs(void)
{
	struct list_head *item;
	struct logical_input *input;
W
Willy Tarreau 已提交
1350

1351 1352 1353 1354 1355 1356 1357 1358 1359
	keypressed = 0;
	inputs_stable = 1;
	list_for_each(item, &logical_inputs) {
		input = list_entry(item, struct logical_input, list);

		switch (input->state) {
		case INPUT_ST_LOW:
			if ((phys_curr & input->mask) != input->value)
				break;
1360 1361 1362 1363 1364 1365
			/* if all needed ones were already set previously,
			 * this means that this logical signal has been
			 * activated by the releasing of another combined
			 * signal, so we don't want to match.
			 * eg: AB -(release B)-> A -(release A)-> 0 :
			 *     don't match A.
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
			 */
			if ((phys_prev & input->mask) == input->value)
				break;
			input->rise_timer = 0;
			input->state = INPUT_ST_RISING;
			/* no break here, fall through */
		case INPUT_ST_RISING:
			if ((phys_curr & input->mask) != input->value) {
				input->state = INPUT_ST_LOW;
				break;
			}
			if (input->rise_timer < input->rise_time) {
				inputs_stable = 0;
				input->rise_timer++;
				break;
			}
			input->high_timer = 0;
			input->state = INPUT_ST_HIGH;
			/* no break here, fall through */
		case INPUT_ST_HIGH:
1386
			if (input_state_high(input))
1387 1388 1389
				break;
			/* no break here, fall through */
		case INPUT_ST_FALLING:
1390
			input_state_falling(input);
1391 1392 1393
		}
	}
}
W
Willy Tarreau 已提交
1394

1395 1396
static void panel_scan_timer(void)
{
1397
	if (keypad.enabled && keypad_initialized) {
1398
		if (spin_trylock_irq(&pprt_lock)) {
1399
			phys_scan_contacts();
1400 1401

			/* no need for the parport anymore */
1402
			spin_unlock_irq(&pprt_lock);
W
Willy Tarreau 已提交
1403 1404
		}

1405 1406
		if (!inputs_stable || phys_curr != phys_prev)
			panel_process_inputs();
W
Willy Tarreau 已提交
1407 1408
	}

1409
	if (keypressed && lcd.enabled && lcd.initialized)
1410
		charlcd_poke(lcd.charlcd);
1411 1412

	mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
W
Willy Tarreau 已提交
1413 1414
}

1415 1416
static void init_scan_timer(void)
{
1417
	if (scan_timer.function)
1418 1419
		return;		/* already started */

1420
	setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
1421 1422
	scan_timer.expires = jiffies + INPUT_POLL_TIME;
	add_timer(&scan_timer);
W
Willy Tarreau 已提交
1423 1424 1425
}

/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
1426 1427
 * if <omask> or <imask> are non-null, they will be or'ed with the bits
 * corresponding to out and in bits respectively.
W
Willy Tarreau 已提交
1428 1429
 * returns 1 if ok, 0 if error (in which case, nothing is written).
 */
1430
static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
K
Ksenija Stanojevic 已提交
1431
			  u8 *imask, u8 *omask)
1432
{
1433
	const char sigtab[] = "EeSsPpAaBb";
K
Ksenija Stanojevic 已提交
1434
	u8 im, om;
1435
	__u64 m, v;
1436

K
Ksenija Stanojevic 已提交
1437 1438
	om = 0;
	im = 0;
1439 1440
	m = 0ULL;
	v = 0ULL;
1441 1442
	while (*name) {
		int in, out, bit, neg;
1443
		const char *idx;
1444

1445 1446
		idx = strchr(sigtab, *name);
		if (!idx)
1447
			return 0;	/* input name not found */
1448 1449

		in = idx - sigtab;
1450 1451
		neg = (in & 1);	/* odd (lower) names are negated */
		in >>= 1;
1452
		im |= BIT(in);
1453 1454

		name++;
1455
		if (*name >= '0' && *name <= '7') {
1456
			out = *name - '0';
1457
			om |= BIT(out);
1458
		} else if (*name == '-') {
1459
			out = 8;
1460
		} else {
1461
			return 0;	/* unknown bit name */
1462
		}
1463 1464 1465 1466 1467 1468 1469

		bit = (out * 5) + in;

		m |= 1ULL << bit;
		if (!neg)
			v |= 1ULL << bit;
		name++;
W
Willy Tarreau 已提交
1470
	}
1471 1472 1473 1474 1475 1476 1477
	*mask = m;
	*value = v;
	if (imask)
		*imask |= im;
	if (omask)
		*omask |= om;
	return 1;
W
Willy Tarreau 已提交
1478 1479 1480 1481 1482 1483
}

/* tries to bind a key to the signal name <name>. The key will send the
 * strings <press>, <repeat>, <release> for these respective events.
 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
 */
1484 1485 1486
static struct logical_input *panel_bind_key(const char *name, const char *press,
					    const char *repeat,
					    const char *release)
1487 1488 1489
{
	struct logical_input *key;

1490
	key = kzalloc(sizeof(*key), GFP_KERNEL);
1491
	if (!key)
1492
		return NULL;
1493

1494
	if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
1495 1496
			     &scan_mask_o)) {
		kfree(key);
1497
		return NULL;
1498
	}
1499 1500 1501 1502 1503

	key->type = INPUT_TYPE_KBD;
	key->state = INPUT_ST_LOW;
	key->rise_time = 1;
	key->fall_time = 1;
W
Willy Tarreau 已提交
1504

1505 1506 1507 1508 1509 1510
	strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
	strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
	strncpy(key->u.kbd.release_str, release,
		sizeof(key->u.kbd.release_str));
	list_add(&key->list, &logical_inputs);
	return key;
W
Willy Tarreau 已提交
1511 1512
}

1513
#if 0
W
Willy Tarreau 已提交
1514 1515 1516
/* tries to bind a callback function to the signal name <name>. The function
 * <press_fct> will be called with the <press_data> arg when the signal is
 * activated, and so on for <release_fct>/<release_data>
1517 1518
 * Returns the pointer to the new signal if ok, NULL if the signal could not
 * be bound.
W
Willy Tarreau 已提交
1519 1520
 */
static struct logical_input *panel_bind_callback(char *name,
1521
						 void (*press_fct)(int),
1522
						 int press_data,
1523
						 void (*release_fct)(int),
1524 1525 1526 1527
						 int release_data)
{
	struct logical_input *callback;

1528
	callback = kmalloc(sizeof(*callback), GFP_KERNEL);
1529
	if (!callback)
1530
		return NULL;
1531

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	memset(callback, 0, sizeof(struct logical_input));
	if (!input_name2mask(name, &callback->mask, &callback->value,
			     &scan_mask_i, &scan_mask_o))
		return NULL;

	callback->type = INPUT_TYPE_STD;
	callback->state = INPUT_ST_LOW;
	callback->rise_time = 1;
	callback->fall_time = 1;
	callback->u.std.press_fct = press_fct;
	callback->u.std.press_data = press_data;
	callback->u.std.release_fct = release_fct;
	callback->u.std.release_data = release_data;
	list_add(&callback->list, &logical_inputs);
	return callback;
W
Willy Tarreau 已提交
1547
}
1548
#endif
W
Willy Tarreau 已提交
1549

1550 1551 1552
static void keypad_init(void)
{
	int keynum;
1553

1554 1555
	init_waitqueue_head(&keypad_read_wait);
	keypad_buflen = 0;	/* flushes any eventual noisy keystroke */
W
Willy Tarreau 已提交
1556

1557
	/* Let's create all known keys */
W
Willy Tarreau 已提交
1558

1559 1560 1561 1562 1563 1564
	for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
		panel_bind_key(keypad_profile[keynum][0],
			       keypad_profile[keynum][1],
			       keypad_profile[keynum][2],
			       keypad_profile[keynum][3]);
	}
W
Willy Tarreau 已提交
1565

1566 1567
	init_scan_timer();
	keypad_initialized = 1;
W
Willy Tarreau 已提交
1568 1569 1570 1571 1572 1573
}

/**************************************************/
/* device initialization                          */
/**************************************************/

1574
static void panel_attach(struct parport *port)
W
Willy Tarreau 已提交
1575
{
1576 1577
	struct pardev_cb panel_cb;

1578 1579 1580 1581
	if (port->number != parport)
		return;

	if (pprt) {
1582 1583
		pr_err("%s: port->number=%d parport=%d, already registered!\n",
		       __func__, port->number, parport);
1584 1585 1586
		return;
	}

1587 1588 1589 1590 1591
	memset(&panel_cb, 0, sizeof(panel_cb));
	panel_cb.private = &pprt;
	/* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */

	pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
1592
	if (!pprt) {
1593 1594
		pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
		       __func__, port->number, parport);
K
Kulikov Vasiliy 已提交
1595 1596
		return;
	}
1597 1598

	if (parport_claim(pprt)) {
1599 1600
		pr_err("could not claim access to parport%d. Aborting.\n",
		       parport);
K
Kulikov Vasiliy 已提交
1601
		goto err_unreg_device;
1602 1603
	}

1604 1605 1606
	/* must init LCD first, just in case an IRQ from the keypad is
	 * generated at keypad init
	 */
1607
	if (lcd.enabled) {
1608
		lcd_init();
1609
		if (!lcd.charlcd || charlcd_register(lcd.charlcd))
K
Kulikov Vasiliy 已提交
1610
			goto err_unreg_device;
1611 1612
	}

1613
	if (keypad.enabled) {
1614
		keypad_init();
K
Kulikov Vasiliy 已提交
1615 1616
		if (misc_register(&keypad_dev))
			goto err_lcd_unreg;
1617
	}
K
Kulikov Vasiliy 已提交
1618 1619 1620
	return;

err_lcd_unreg:
1621
	if (lcd.enabled)
1622
		charlcd_unregister(lcd.charlcd);
K
Kulikov Vasiliy 已提交
1623
err_unreg_device:
1624 1625
	kfree(lcd.charlcd);
	lcd.charlcd = NULL;
K
Kulikov Vasiliy 已提交
1626 1627
	parport_unregister_device(pprt);
	pprt = NULL;
W
Willy Tarreau 已提交
1628 1629
}

1630
static void panel_detach(struct parport *port)
W
Willy Tarreau 已提交
1631
{
1632 1633 1634 1635
	if (port->number != parport)
		return;

	if (!pprt) {
1636 1637
		pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
		       __func__, port->number, parport);
1638 1639
		return;
	}
1640
	if (scan_timer.function)
S
Sudip Mukherjee 已提交
1641
		del_timer_sync(&scan_timer);
1642

1643 1644 1645 1646
	if (keypad.enabled) {
		misc_deregister(&keypad_dev);
		keypad_initialized = 0;
	}
1647

1648
	if (lcd.enabled) {
1649
		charlcd_unregister(lcd.charlcd);
1650
		lcd.initialized = false;
1651 1652
		kfree(lcd.charlcd);
		lcd.charlcd = NULL;
1653
	}
1654 1655 1656 1657 1658

	/* TODO: free all input signals */
	parport_release(pprt);
	parport_unregister_device(pprt);
	pprt = NULL;
W
Willy Tarreau 已提交
1659 1660 1661
}

static struct parport_driver panel_driver = {
1662
	.name = "panel",
1663
	.match_port = panel_attach,
1664
	.detach = panel_detach,
1665
	.devmodel = true,
W
Willy Tarreau 已提交
1666 1667 1668
};

/* init function */
1669
static int __init panel_init_module(void)
1670
{
1671
	int selected_keypad_type = NOT_SET, err;
1672 1673 1674

	/* take care of an eventual profile */
	switch (profile) {
1675 1676
	case PANEL_PROFILE_CUSTOM:
		/* custom profile */
1677 1678
		selected_keypad_type = DEFAULT_KEYPAD_TYPE;
		selected_lcd_type = DEFAULT_LCD_TYPE;
1679
		break;
1680 1681
	case PANEL_PROFILE_OLD:
		/* 8 bits, 2*16, old keypad */
1682 1683 1684 1685
		selected_keypad_type = KEYPAD_TYPE_OLD;
		selected_lcd_type = LCD_TYPE_OLD;

		/* TODO: This two are a little hacky, sort it out later */
1686
		if (lcd_width == NOT_SET)
1687
			lcd_width = 16;
1688
		if (lcd_hwidth == NOT_SET)
1689 1690
			lcd_hwidth = 16;
		break;
1691 1692
	case PANEL_PROFILE_NEW:
		/* serial, 2*16, new keypad */
1693 1694
		selected_keypad_type = KEYPAD_TYPE_NEW;
		selected_lcd_type = LCD_TYPE_KS0074;
1695
		break;
1696 1697
	case PANEL_PROFILE_HANTRONIX:
		/* 8 bits, 2*16 hantronix-like, no keypad */
1698 1699
		selected_keypad_type = KEYPAD_TYPE_NONE;
		selected_lcd_type = LCD_TYPE_HANTRONIX;
1700
		break;
1701 1702
	case PANEL_PROFILE_NEXCOM:
		/* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
1703 1704
		selected_keypad_type = KEYPAD_TYPE_NEXCOM;
		selected_lcd_type = LCD_TYPE_NEXCOM;
1705
		break;
1706 1707
	case PANEL_PROFILE_LARGE:
		/* 8 bits, 2*40, old keypad */
1708 1709
		selected_keypad_type = KEYPAD_TYPE_OLD;
		selected_lcd_type = LCD_TYPE_OLD;
1710 1711 1712
		break;
	}

1713 1714 1715 1716
	/*
	 * Overwrite selection with module param values (both keypad and lcd),
	 * where the deprecated params have lower prio.
	 */
1717
	if (keypad_enabled != NOT_SET)
1718
		selected_keypad_type = keypad_enabled;
1719
	if (keypad_type != NOT_SET)
1720 1721 1722 1723
		selected_keypad_type = keypad_type;

	keypad.enabled = (selected_keypad_type > 0);

1724
	if (lcd_enabled != NOT_SET)
1725
		selected_lcd_type = lcd_enabled;
1726
	if (lcd_type != NOT_SET)
1727 1728 1729
		selected_lcd_type = lcd_type;

	lcd.enabled = (selected_lcd_type > 0);
1730

1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
	if (lcd.enabled) {
		/*
		 * Init lcd struct with load-time values to preserve exact
		 * current functionality (at least for now).
		 */
		lcd.charset = lcd_charset;
		lcd.proto = lcd_proto;
		lcd.pins.e = lcd_e_pin;
		lcd.pins.rs = lcd_rs_pin;
		lcd.pins.rw = lcd_rw_pin;
		lcd.pins.cl = lcd_cl_pin;
		lcd.pins.da = lcd_da_pin;
		lcd.pins.bl = lcd_bl_pin;
	}

1746
	switch (selected_keypad_type) {
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	case KEYPAD_TYPE_OLD:
		keypad_profile = old_keypad_profile;
		break;
	case KEYPAD_TYPE_NEW:
		keypad_profile = new_keypad_profile;
		break;
	case KEYPAD_TYPE_NEXCOM:
		keypad_profile = nexcom_keypad_profile;
		break;
	default:
		keypad_profile = NULL;
		break;
	}

1761
	if (!lcd.enabled && !keypad.enabled) {
1762
		/* no device enabled, let's exit */
1763
		pr_err("panel driver disabled.\n");
1764
		return -ENODEV;
W
Willy Tarreau 已提交
1765 1766
	}

1767 1768
	err = parport_register_driver(&panel_driver);
	if (err) {
1769
		pr_err("could not register with parport. Aborting.\n");
1770
		return err;
1771 1772
	}

1773
	if (pprt)
1774 1775
		pr_info("panel driver registered on parport%d (io=0x%lx).\n",
			parport, pprt->port->base);
1776
	else
1777
		pr_info("panel driver not yet registered\n");
1778 1779
	return 0;
}
W
Willy Tarreau 已提交
1780

1781
static void __exit panel_cleanup_module(void)
1782 1783
{
	parport_unregister_driver(&panel_driver);
W
Willy Tarreau 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
}

module_init(panel_init_module);
module_exit(panel_cleanup_module);
MODULE_AUTHOR("Willy Tarreau");
MODULE_LICENSE("GPL");

/*
 * Local variables:
 *  c-indent-level: 4
 *  tab-width: 8
 * End:
 */