1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Front panel driver for Linux 4 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu> 5 * Copyright (C) 2016-2017 Glider bvba 6 * 7 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad) 8 * connected to a parallel printer port. 9 * 10 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit 11 * serial module compatible with Samsung's KS0074. The pins may be connected in 12 * any combination, everything is programmable. 13 * 14 * The keypad consists in a matrix of push buttons connecting input pins to 15 * data output pins or to the ground. The combinations have to be hard-coded 16 * in the driver, though several profiles exist and adding new ones is easy. 17 * 18 * Several profiles are provided for commonly found LCD+keypad modules on the 19 * market, such as those found in Nexcom's appliances. 20 * 21 * FIXME: 22 * - the initialization/deinitialization process is very dirty and should 23 * be rewritten. It may even be buggy. 24 * 25 * TODO: 26 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs) 27 * - make the LCD a part of a virtual screen of Vx*Vy 28 * - make the inputs list smp-safe 29 * - change the keyboard to a double mapping : signals -> key_id -> values 30 * so that applications can change values without knowing signals 31 * 32 */ 33 34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 35 36 #include <linux/module.h> 37 38 #include <linux/types.h> 39 #include <linux/errno.h> 40 #include <linux/signal.h> 41 #include <linux/sched.h> 42 #include <linux/spinlock.h> 43 #include <linux/interrupt.h> 44 #include <linux/miscdevice.h> 45 #include <linux/slab.h> 46 #include <linux/ioport.h> 47 #include <linux/fcntl.h> 48 #include <linux/init.h> 49 #include <linux/delay.h> 50 #include <linux/kernel.h> 51 #include <linux/ctype.h> 52 #include <linux/parport.h> 53 #include <linux/list.h> 54 55 #include <linux/io.h> 56 #include <linux/uaccess.h> 57 58 #include "charlcd.h" 59 #include "hd44780_common.h" 60 61 #define LCD_MAXBYTES 256 /* max burst write */ 62 63 #define KEYPAD_BUFFER 64 64 65 /* poll the keyboard this every second */ 66 #define INPUT_POLL_TIME (HZ / 50) 67 /* a key starts to repeat after this times INPUT_POLL_TIME */ 68 #define KEYPAD_REP_START (10) 69 /* a key repeats this times INPUT_POLL_TIME */ 70 #define KEYPAD_REP_DELAY (2) 71 72 /* converts an r_str() input to an active high, bits string : 000BAOSE */ 73 #define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3) 74 75 #define PNL_PBUSY 0x80 /* inverted input, active low */ 76 #define PNL_PACK 0x40 /* direct input, active low */ 77 #define PNL_POUTPA 0x20 /* direct input, active high */ 78 #define PNL_PSELECD 0x10 /* direct input, active high */ 79 #define PNL_PERRORP 0x08 /* direct input, active low */ 80 81 #define PNL_PBIDIR 0x20 /* bi-directional ports */ 82 /* high to read data in or-ed with data out */ 83 #define PNL_PINTEN 0x10 84 #define PNL_PSELECP 0x08 /* inverted output, active low */ 85 #define PNL_PINITP 0x04 /* direct output, active low */ 86 #define PNL_PAUTOLF 0x02 /* inverted output, active low */ 87 #define PNL_PSTROBE 0x01 /* inverted output */ 88 89 #define PNL_PD0 0x01 90 #define PNL_PD1 0x02 91 #define PNL_PD2 0x04 92 #define PNL_PD3 0x08 93 #define PNL_PD4 0x10 94 #define PNL_PD5 0x20 95 #define PNL_PD6 0x40 96 #define PNL_PD7 0x80 97 98 #define PIN_NONE 0 99 #define PIN_STROBE 1 100 #define PIN_D0 2 101 #define PIN_D1 3 102 #define PIN_D2 4 103 #define PIN_D3 5 104 #define PIN_D4 6 105 #define PIN_D5 7 106 #define PIN_D6 8 107 #define PIN_D7 9 108 #define PIN_AUTOLF 14 109 #define PIN_INITP 16 110 #define PIN_SELECP 17 111 #define PIN_NOT_SET 127 112 113 #define NOT_SET -1 114 115 /* macros to simplify use of the parallel port */ 116 #define r_ctr(x) (parport_read_control((x)->port)) 117 #define r_dtr(x) (parport_read_data((x)->port)) 118 #define r_str(x) (parport_read_status((x)->port)) 119 #define w_ctr(x, y) (parport_write_control((x)->port, (y))) 120 #define w_dtr(x, y) (parport_write_data((x)->port, (y))) 121 122 /* this defines which bits are to be used and which ones to be ignored */ 123 /* logical or of the output bits involved in the scan matrix */ 124 static __u8 scan_mask_o; 125 /* logical or of the input bits involved in the scan matrix */ 126 static __u8 scan_mask_i; 127 128 enum input_type { 129 INPUT_TYPE_STD, 130 INPUT_TYPE_KBD, 131 }; 132 133 enum input_state { 134 INPUT_ST_LOW, 135 INPUT_ST_RISING, 136 INPUT_ST_HIGH, 137 INPUT_ST_FALLING, 138 }; 139 140 struct logical_input { 141 struct list_head list; 142 __u64 mask; 143 __u64 value; 144 enum input_type type; 145 enum input_state state; 146 __u8 rise_time, fall_time; 147 __u8 rise_timer, fall_timer, high_timer; 148 149 union { 150 struct { /* valid when type == INPUT_TYPE_STD */ 151 void (*press_fct)(int); 152 void (*release_fct)(int); 153 int press_data; 154 int release_data; 155 } std; 156 struct { /* valid when type == INPUT_TYPE_KBD */ 157 char press_str[sizeof(void *) + sizeof(int)] __nonstring; 158 char repeat_str[sizeof(void *) + sizeof(int)] __nonstring; 159 char release_str[sizeof(void *) + sizeof(int)] __nonstring; 160 } kbd; 161 } u; 162 }; 163 164 static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */ 165 166 /* physical contacts history 167 * Physical contacts are a 45 bits string of 9 groups of 5 bits each. 168 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group 169 * corresponds to the ground. 170 * Within each group, bits are stored in the same order as read on the port : 171 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0). 172 * So, each __u64 is represented like this : 173 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE 174 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00> 175 */ 176 177 /* what has just been read from the I/O ports */ 178 static __u64 phys_read; 179 /* previous phys_read */ 180 static __u64 phys_read_prev; 181 /* stabilized phys_read (phys_read|phys_read_prev) */ 182 static __u64 phys_curr; 183 /* previous phys_curr */ 184 static __u64 phys_prev; 185 /* 0 means that at least one logical signal needs be computed */ 186 static char inputs_stable; 187 188 /* these variables are specific to the keypad */ 189 static struct { 190 bool enabled; 191 } keypad; 192 193 static char keypad_buffer[KEYPAD_BUFFER]; 194 static int keypad_buflen; 195 static int keypad_start; 196 static char keypressed; 197 static wait_queue_head_t keypad_read_wait; 198 199 /* lcd-specific variables */ 200 static struct { 201 bool enabled; 202 bool initialized; 203 204 int charset; 205 int proto; 206 207 /* TODO: use union here? */ 208 struct { 209 int e; 210 int rs; 211 int rw; 212 int cl; 213 int da; 214 int bl; 215 } pins; 216 217 struct charlcd *charlcd; 218 } lcd; 219 220 /* Needed only for init */ 221 static int selected_lcd_type = NOT_SET; 222 223 /* 224 * Bit masks to convert LCD signals to parallel port outputs. 225 * _d_ are values for data port, _c_ are for control port. 226 * [0] = signal OFF, [1] = signal ON, [2] = mask 227 */ 228 #define BIT_CLR 0 229 #define BIT_SET 1 230 #define BIT_MSK 2 231 #define BIT_STATES 3 232 /* 233 * one entry for each bit on the LCD 234 */ 235 #define LCD_BIT_E 0 236 #define LCD_BIT_RS 1 237 #define LCD_BIT_RW 2 238 #define LCD_BIT_BL 3 239 #define LCD_BIT_CL 4 240 #define LCD_BIT_DA 5 241 #define LCD_BITS 6 242 243 /* 244 * each bit can be either connected to a DATA or CTRL port 245 */ 246 #define LCD_PORT_C 0 247 #define LCD_PORT_D 1 248 #define LCD_PORTS 2 249 250 static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES]; 251 252 /* 253 * LCD protocols 254 */ 255 #define LCD_PROTO_PARALLEL 0 256 #define LCD_PROTO_SERIAL 1 257 #define LCD_PROTO_TI_DA8XX_LCD 2 258 259 /* 260 * LCD character sets 261 */ 262 #define LCD_CHARSET_NORMAL 0 263 #define LCD_CHARSET_KS0074 1 264 265 /* 266 * LCD types 267 */ 268 #define LCD_TYPE_NONE 0 269 #define LCD_TYPE_CUSTOM 1 270 #define LCD_TYPE_OLD 2 271 #define LCD_TYPE_KS0074 3 272 #define LCD_TYPE_HANTRONIX 4 273 #define LCD_TYPE_NEXCOM 5 274 275 /* 276 * keypad types 277 */ 278 #define KEYPAD_TYPE_NONE 0 279 #define KEYPAD_TYPE_OLD 1 280 #define KEYPAD_TYPE_NEW 2 281 #define KEYPAD_TYPE_NEXCOM 3 282 283 /* 284 * panel profiles 285 */ 286 #define PANEL_PROFILE_CUSTOM 0 287 #define PANEL_PROFILE_OLD 1 288 #define PANEL_PROFILE_NEW 2 289 #define PANEL_PROFILE_HANTRONIX 3 290 #define PANEL_PROFILE_NEXCOM 4 291 #define PANEL_PROFILE_LARGE 5 292 293 /* 294 * Construct custom config from the kernel's configuration 295 */ 296 #define DEFAULT_PARPORT 0 297 #define DEFAULT_PROFILE PANEL_PROFILE_LARGE 298 #define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD 299 #define DEFAULT_LCD_TYPE LCD_TYPE_OLD 300 #define DEFAULT_LCD_HEIGHT 2 301 #define DEFAULT_LCD_WIDTH 40 302 #define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL 303 #define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL 304 305 #define DEFAULT_LCD_PIN_E PIN_AUTOLF 306 #define DEFAULT_LCD_PIN_RS PIN_SELECP 307 #define DEFAULT_LCD_PIN_RW PIN_INITP 308 #define DEFAULT_LCD_PIN_SCL PIN_STROBE 309 #define DEFAULT_LCD_PIN_SDA PIN_D0 310 #define DEFAULT_LCD_PIN_BL PIN_NOT_SET 311 312 #ifdef CONFIG_PANEL_PARPORT 313 #undef DEFAULT_PARPORT 314 #define DEFAULT_PARPORT CONFIG_PANEL_PARPORT 315 #endif 316 317 #ifdef CONFIG_PANEL_PROFILE 318 #undef DEFAULT_PROFILE 319 #define DEFAULT_PROFILE CONFIG_PANEL_PROFILE 320 #endif 321 322 #if DEFAULT_PROFILE == 0 /* custom */ 323 #ifdef CONFIG_PANEL_KEYPAD 324 #undef DEFAULT_KEYPAD_TYPE 325 #define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD 326 #endif 327 328 #ifdef CONFIG_PANEL_LCD 329 #undef DEFAULT_LCD_TYPE 330 #define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD 331 #endif 332 333 #ifdef CONFIG_PANEL_LCD_HEIGHT 334 #undef DEFAULT_LCD_HEIGHT 335 #define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT 336 #endif 337 338 #ifdef CONFIG_PANEL_LCD_WIDTH 339 #undef DEFAULT_LCD_WIDTH 340 #define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH 341 #endif 342 343 #ifdef CONFIG_PANEL_LCD_BWIDTH 344 #undef DEFAULT_LCD_BWIDTH 345 #define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH 346 #endif 347 348 #ifdef CONFIG_PANEL_LCD_HWIDTH 349 #undef DEFAULT_LCD_HWIDTH 350 #define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH 351 #endif 352 353 #ifdef CONFIG_PANEL_LCD_CHARSET 354 #undef DEFAULT_LCD_CHARSET 355 #define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET 356 #endif 357 358 #ifdef CONFIG_PANEL_LCD_PROTO 359 #undef DEFAULT_LCD_PROTO 360 #define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO 361 #endif 362 363 #ifdef CONFIG_PANEL_LCD_PIN_E 364 #undef DEFAULT_LCD_PIN_E 365 #define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E 366 #endif 367 368 #ifdef CONFIG_PANEL_LCD_PIN_RS 369 #undef DEFAULT_LCD_PIN_RS 370 #define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS 371 #endif 372 373 #ifdef CONFIG_PANEL_LCD_PIN_RW 374 #undef DEFAULT_LCD_PIN_RW 375 #define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW 376 #endif 377 378 #ifdef CONFIG_PANEL_LCD_PIN_SCL 379 #undef DEFAULT_LCD_PIN_SCL 380 #define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL 381 #endif 382 383 #ifdef CONFIG_PANEL_LCD_PIN_SDA 384 #undef DEFAULT_LCD_PIN_SDA 385 #define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA 386 #endif 387 388 #ifdef CONFIG_PANEL_LCD_PIN_BL 389 #undef DEFAULT_LCD_PIN_BL 390 #define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL 391 #endif 392 393 #endif /* DEFAULT_PROFILE == 0 */ 394 395 /* global variables */ 396 397 /* Device single-open policy control */ 398 static atomic_t keypad_available = ATOMIC_INIT(1); 399 400 static struct pardevice *pprt; 401 402 static int keypad_initialized; 403 404 static DEFINE_SPINLOCK(pprt_lock); 405 static struct timer_list scan_timer; 406 407 MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver"); 408 409 static int parport = DEFAULT_PARPORT; 410 module_param(parport, int, 0000); 411 MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)"); 412 413 static int profile = DEFAULT_PROFILE; 414 module_param(profile, int, 0000); 415 MODULE_PARM_DESC(profile, 416 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; " 417 "4=16x2 nexcom; default=40x2, old kp"); 418 419 static int keypad_type = NOT_SET; 420 module_param(keypad_type, int, 0000); 421 MODULE_PARM_DESC(keypad_type, 422 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys"); 423 424 static int lcd_type = NOT_SET; 425 module_param(lcd_type, int, 0000); 426 MODULE_PARM_DESC(lcd_type, 427 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom"); 428 429 static int lcd_height = NOT_SET; 430 module_param(lcd_height, int, 0000); 431 MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD"); 432 433 static int lcd_width = NOT_SET; 434 module_param(lcd_width, int, 0000); 435 MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD"); 436 437 static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */ 438 module_param(lcd_bwidth, int, 0000); 439 MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)"); 440 441 static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */ 442 module_param(lcd_hwidth, int, 0000); 443 MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)"); 444 445 static int lcd_charset = NOT_SET; 446 module_param(lcd_charset, int, 0000); 447 MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074"); 448 449 static int lcd_proto = NOT_SET; 450 module_param(lcd_proto, int, 0000); 451 MODULE_PARM_DESC(lcd_proto, 452 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface"); 453 454 /* 455 * These are the parallel port pins the LCD control signals are connected to. 456 * Set this to 0 if the signal is not used. Set it to its opposite value 457 * (negative) if the signal is negated. -MAXINT is used to indicate that the 458 * pin has not been explicitly specified. 459 * 460 * WARNING! no check will be performed about collisions with keypad ! 461 */ 462 463 static int lcd_e_pin = PIN_NOT_SET; 464 module_param(lcd_e_pin, int, 0000); 465 MODULE_PARM_DESC(lcd_e_pin, 466 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)"); 467 468 static int lcd_rs_pin = PIN_NOT_SET; 469 module_param(lcd_rs_pin, int, 0000); 470 MODULE_PARM_DESC(lcd_rs_pin, 471 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)"); 472 473 static int lcd_rw_pin = PIN_NOT_SET; 474 module_param(lcd_rw_pin, int, 0000); 475 MODULE_PARM_DESC(lcd_rw_pin, 476 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)"); 477 478 static int lcd_cl_pin = PIN_NOT_SET; 479 module_param(lcd_cl_pin, int, 0000); 480 MODULE_PARM_DESC(lcd_cl_pin, 481 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)"); 482 483 static int lcd_da_pin = PIN_NOT_SET; 484 module_param(lcd_da_pin, int, 0000); 485 MODULE_PARM_DESC(lcd_da_pin, 486 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)"); 487 488 static int lcd_bl_pin = PIN_NOT_SET; 489 module_param(lcd_bl_pin, int, 0000); 490 MODULE_PARM_DESC(lcd_bl_pin, 491 "# of the // port pin connected to LCD backlight, with polarity (-17..17)"); 492 493 /* Deprecated module parameters - consider not using them anymore */ 494 495 static int lcd_enabled = NOT_SET; 496 module_param(lcd_enabled, int, 0000); 497 MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead"); 498 499 static int keypad_enabled = NOT_SET; 500 module_param(keypad_enabled, int, 0000); 501 MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead"); 502 503 /* for some LCD drivers (ks0074) we need a charset conversion table. */ 504 static const unsigned char lcd_char_conv_ks0074[256] = { 505 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */ 506 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 507 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 508 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 509 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 510 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27, 511 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 512 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 513 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 514 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 515 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 516 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 517 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4, 518 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 519 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 520 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 521 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20, 522 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 523 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 524 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 525 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 526 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f, 527 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96, 528 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd, 529 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60, 530 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9, 531 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3, 532 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78, 533 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe, 534 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8, 535 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69, 536 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25, 537 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79, 538 }; 539 540 static const char old_keypad_profile[][4][9] = { 541 {"S0", "Left\n", "Left\n", ""}, 542 {"S1", "Down\n", "Down\n", ""}, 543 {"S2", "Up\n", "Up\n", ""}, 544 {"S3", "Right\n", "Right\n", ""}, 545 {"S4", "Esc\n", "Esc\n", ""}, 546 {"S5", "Ret\n", "Ret\n", ""}, 547 {"", "", "", ""} 548 }; 549 550 /* signals, press, repeat, release */ 551 static const char new_keypad_profile[][4][9] = { 552 {"S0", "Left\n", "Left\n", ""}, 553 {"S1", "Down\n", "Down\n", ""}, 554 {"S2", "Up\n", "Up\n", ""}, 555 {"S3", "Right\n", "Right\n", ""}, 556 {"S4s5", "", "Esc\n", "Esc\n"}, 557 {"s4S5", "", "Ret\n", "Ret\n"}, 558 {"S4S5", "Help\n", "", ""}, 559 /* add new signals above this line */ 560 {"", "", "", ""} 561 }; 562 563 /* signals, press, repeat, release */ 564 static const char nexcom_keypad_profile[][4][9] = { 565 {"a-p-e-", "Down\n", "Down\n", ""}, 566 {"a-p-E-", "Ret\n", "Ret\n", ""}, 567 {"a-P-E-", "Esc\n", "Esc\n", ""}, 568 {"a-P-e-", "Up\n", "Up\n", ""}, 569 /* add new signals above this line */ 570 {"", "", "", ""} 571 }; 572 573 static const char (*keypad_profile)[4][9] = old_keypad_profile; 574 575 static DECLARE_BITMAP(bits, LCD_BITS); 576 577 static void lcd_get_bits(unsigned int port, int *val) 578 { 579 unsigned int bit, state; 580 581 for (bit = 0; bit < LCD_BITS; bit++) { 582 state = test_bit(bit, bits) ? BIT_SET : BIT_CLR; 583 *val &= lcd_bits[port][bit][BIT_MSK]; 584 *val |= lcd_bits[port][bit][state]; 585 } 586 } 587 588 /* sets data port bits according to current signals values */ 589 static int set_data_bits(void) 590 { 591 int val; 592 593 val = r_dtr(pprt); 594 lcd_get_bits(LCD_PORT_D, &val); 595 w_dtr(pprt, val); 596 return val; 597 } 598 599 /* sets ctrl port bits according to current signals values */ 600 static int set_ctrl_bits(void) 601 { 602 int val; 603 604 val = r_ctr(pprt); 605 lcd_get_bits(LCD_PORT_C, &val); 606 w_ctr(pprt, val); 607 return val; 608 } 609 610 /* sets ctrl & data port bits according to current signals values */ 611 static void panel_set_bits(void) 612 { 613 set_data_bits(); 614 set_ctrl_bits(); 615 } 616 617 /* 618 * Converts a parallel port pin (from -25 to 25) to data and control ports 619 * masks, and data and control port bits. The signal will be considered 620 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25). 621 * 622 * Result will be used this way : 623 * out(dport, in(dport) & d_val[2] | d_val[signal_state]) 624 * out(cport, in(cport) & c_val[2] | c_val[signal_state]) 625 */ 626 static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val) 627 { 628 int d_bit, c_bit, inv; 629 630 d_val[0] = 0; 631 c_val[0] = 0; 632 d_val[1] = 0; 633 c_val[1] = 0; 634 d_val[2] = 0xFF; 635 c_val[2] = 0xFF; 636 637 if (pin == 0) 638 return; 639 640 inv = (pin < 0); 641 if (inv) 642 pin = -pin; 643 644 d_bit = 0; 645 c_bit = 0; 646 647 switch (pin) { 648 case PIN_STROBE: /* strobe, inverted */ 649 c_bit = PNL_PSTROBE; 650 inv = !inv; 651 break; 652 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */ 653 d_bit = 1 << (pin - 2); 654 break; 655 case PIN_AUTOLF: /* autofeed, inverted */ 656 c_bit = PNL_PAUTOLF; 657 inv = !inv; 658 break; 659 case PIN_INITP: /* init, direct */ 660 c_bit = PNL_PINITP; 661 break; 662 case PIN_SELECP: /* select_in, inverted */ 663 c_bit = PNL_PSELECP; 664 inv = !inv; 665 break; 666 default: /* unknown pin, ignore */ 667 break; 668 } 669 670 if (c_bit) { 671 c_val[2] &= ~c_bit; 672 c_val[!inv] = c_bit; 673 } else if (d_bit) { 674 d_val[2] &= ~d_bit; 675 d_val[!inv] = d_bit; 676 } 677 } 678 679 /* 680 * send a serial byte to the LCD panel. The caller is responsible for locking 681 * if needed. 682 */ 683 static void lcd_send_serial(int byte) 684 { 685 int bit; 686 687 /* 688 * the data bit is set on D0, and the clock on STROBE. 689 * LCD reads D0 on STROBE's rising edge. 690 */ 691 for (bit = 0; bit < 8; bit++) { 692 clear_bit(LCD_BIT_CL, bits); /* CLK low */ 693 panel_set_bits(); 694 if (byte & 1) { 695 set_bit(LCD_BIT_DA, bits); 696 } else { 697 clear_bit(LCD_BIT_DA, bits); 698 } 699 700 panel_set_bits(); 701 udelay(2); /* maintain the data during 2 us before CLK up */ 702 set_bit(LCD_BIT_CL, bits); /* CLK high */ 703 panel_set_bits(); 704 udelay(1); /* maintain the strobe during 1 us */ 705 byte >>= 1; 706 } 707 } 708 709 /* turn the backlight on or off */ 710 static void lcd_backlight(struct charlcd *charlcd, enum charlcd_onoff on) 711 { 712 if (lcd.pins.bl == PIN_NONE) 713 return; 714 715 /* The backlight is activated by setting the AUTOFEED line to +5V */ 716 spin_lock_irq(&pprt_lock); 717 assign_bit(LCD_BIT_BL, bits, on); 718 panel_set_bits(); 719 spin_unlock_irq(&pprt_lock); 720 } 721 722 /* send a command to the LCD panel in serial mode */ 723 static void lcd_write_cmd_s(struct hd44780_common *hdc, int cmd) 724 { 725 spin_lock_irq(&pprt_lock); 726 lcd_send_serial(0x1F); /* R/W=W, RS=0 */ 727 lcd_send_serial(cmd & 0x0F); 728 lcd_send_serial((cmd >> 4) & 0x0F); 729 udelay(40); /* the shortest command takes at least 40 us */ 730 spin_unlock_irq(&pprt_lock); 731 } 732 733 /* send data to the LCD panel in serial mode */ 734 static void lcd_write_data_s(struct hd44780_common *hdc, int data) 735 { 736 spin_lock_irq(&pprt_lock); 737 lcd_send_serial(0x5F); /* R/W=W, RS=1 */ 738 lcd_send_serial(data & 0x0F); 739 lcd_send_serial((data >> 4) & 0x0F); 740 udelay(40); /* the shortest data takes at least 40 us */ 741 spin_unlock_irq(&pprt_lock); 742 } 743 744 /* send a command to the LCD panel in 8 bits parallel mode */ 745 static void lcd_write_cmd_p8(struct hd44780_common *hdc, int cmd) 746 { 747 spin_lock_irq(&pprt_lock); 748 /* present the data to the data port */ 749 w_dtr(pprt, cmd); 750 udelay(20); /* maintain the data during 20 us before the strobe */ 751 752 set_bit(LCD_BIT_E, bits); 753 clear_bit(LCD_BIT_RS, bits); 754 clear_bit(LCD_BIT_RW, bits); 755 set_ctrl_bits(); 756 757 udelay(40); /* maintain the strobe during 40 us */ 758 759 clear_bit(LCD_BIT_E, bits); 760 set_ctrl_bits(); 761 762 udelay(120); /* the shortest command takes at least 120 us */ 763 spin_unlock_irq(&pprt_lock); 764 } 765 766 /* send data to the LCD panel in 8 bits parallel mode */ 767 static void lcd_write_data_p8(struct hd44780_common *hdc, int data) 768 { 769 spin_lock_irq(&pprt_lock); 770 /* present the data to the data port */ 771 w_dtr(pprt, data); 772 udelay(20); /* maintain the data during 20 us before the strobe */ 773 774 set_bit(LCD_BIT_E, bits); 775 set_bit(LCD_BIT_RS, bits); 776 clear_bit(LCD_BIT_RW, bits); 777 set_ctrl_bits(); 778 779 udelay(40); /* maintain the strobe during 40 us */ 780 781 clear_bit(LCD_BIT_E, bits); 782 set_ctrl_bits(); 783 784 udelay(45); /* the shortest data takes at least 45 us */ 785 spin_unlock_irq(&pprt_lock); 786 } 787 788 /* send a command to the TI LCD panel */ 789 static void lcd_write_cmd_tilcd(struct hd44780_common *hdc, int cmd) 790 { 791 spin_lock_irq(&pprt_lock); 792 /* present the data to the control port */ 793 w_ctr(pprt, cmd); 794 udelay(60); 795 spin_unlock_irq(&pprt_lock); 796 } 797 798 /* send data to the TI LCD panel */ 799 static void lcd_write_data_tilcd(struct hd44780_common *hdc, int data) 800 { 801 spin_lock_irq(&pprt_lock); 802 /* present the data to the data port */ 803 w_dtr(pprt, data); 804 udelay(60); 805 spin_unlock_irq(&pprt_lock); 806 } 807 808 static const struct charlcd_ops charlcd_ops = { 809 .backlight = lcd_backlight, 810 .print = hd44780_common_print, 811 .gotoxy = hd44780_common_gotoxy, 812 .home = hd44780_common_home, 813 .clear_display = hd44780_common_clear_display, 814 .init_display = hd44780_common_init_display, 815 .shift_cursor = hd44780_common_shift_cursor, 816 .shift_display = hd44780_common_shift_display, 817 .display = hd44780_common_display, 818 .cursor = hd44780_common_cursor, 819 .blink = hd44780_common_blink, 820 .fontsize = hd44780_common_fontsize, 821 .lines = hd44780_common_lines, 822 .redefine_char = hd44780_common_redefine_char, 823 }; 824 825 /* initialize the LCD driver */ 826 static void lcd_init(void) 827 { 828 struct charlcd *charlcd; 829 struct hd44780_common *hdc; 830 831 charlcd = hd44780_common_alloc(); 832 if (!charlcd) 833 return; 834 835 hdc = charlcd->drvdata; 836 hdc->hd44780 = &lcd; 837 838 /* 839 * Init lcd struct with load-time values to preserve exact 840 * current functionality (at least for now). 841 */ 842 charlcd->height = lcd_height; 843 charlcd->width = lcd_width; 844 hdc->bwidth = lcd_bwidth; 845 hdc->hwidth = lcd_hwidth; 846 847 switch (selected_lcd_type) { 848 case LCD_TYPE_OLD: 849 /* parallel mode, 8 bits */ 850 lcd.proto = LCD_PROTO_PARALLEL; 851 lcd.charset = LCD_CHARSET_NORMAL; 852 lcd.pins.e = PIN_STROBE; 853 lcd.pins.rs = PIN_AUTOLF; 854 855 charlcd->width = 40; 856 hdc->bwidth = 40; 857 hdc->hwidth = 64; 858 charlcd->height = 2; 859 break; 860 case LCD_TYPE_KS0074: 861 /* serial mode, ks0074 */ 862 lcd.proto = LCD_PROTO_SERIAL; 863 lcd.charset = LCD_CHARSET_KS0074; 864 lcd.pins.bl = PIN_AUTOLF; 865 lcd.pins.cl = PIN_STROBE; 866 lcd.pins.da = PIN_D0; 867 868 charlcd->width = 16; 869 hdc->bwidth = 40; 870 hdc->hwidth = 16; 871 charlcd->height = 2; 872 break; 873 case LCD_TYPE_NEXCOM: 874 /* parallel mode, 8 bits, generic */ 875 lcd.proto = LCD_PROTO_PARALLEL; 876 lcd.charset = LCD_CHARSET_NORMAL; 877 lcd.pins.e = PIN_AUTOLF; 878 lcd.pins.rs = PIN_SELECP; 879 lcd.pins.rw = PIN_INITP; 880 881 charlcd->width = 16; 882 hdc->bwidth = 40; 883 hdc->hwidth = 64; 884 charlcd->height = 2; 885 break; 886 case LCD_TYPE_CUSTOM: 887 /* customer-defined */ 888 lcd.proto = DEFAULT_LCD_PROTO; 889 lcd.charset = DEFAULT_LCD_CHARSET; 890 /* default geometry will be set later */ 891 break; 892 case LCD_TYPE_HANTRONIX: 893 /* parallel mode, 8 bits, hantronix-like */ 894 default: 895 lcd.proto = LCD_PROTO_PARALLEL; 896 lcd.charset = LCD_CHARSET_NORMAL; 897 lcd.pins.e = PIN_STROBE; 898 lcd.pins.rs = PIN_SELECP; 899 900 charlcd->width = 16; 901 hdc->bwidth = 40; 902 hdc->hwidth = 64; 903 charlcd->height = 2; 904 break; 905 } 906 907 /* Overwrite with module params set on loading */ 908 if (lcd_height != NOT_SET) 909 charlcd->height = lcd_height; 910 if (lcd_width != NOT_SET) 911 charlcd->width = lcd_width; 912 if (lcd_bwidth != NOT_SET) 913 hdc->bwidth = lcd_bwidth; 914 if (lcd_hwidth != NOT_SET) 915 hdc->hwidth = lcd_hwidth; 916 if (lcd_charset != NOT_SET) 917 lcd.charset = lcd_charset; 918 if (lcd_proto != NOT_SET) 919 lcd.proto = lcd_proto; 920 if (lcd_e_pin != PIN_NOT_SET) 921 lcd.pins.e = lcd_e_pin; 922 if (lcd_rs_pin != PIN_NOT_SET) 923 lcd.pins.rs = lcd_rs_pin; 924 if (lcd_rw_pin != PIN_NOT_SET) 925 lcd.pins.rw = lcd_rw_pin; 926 if (lcd_cl_pin != PIN_NOT_SET) 927 lcd.pins.cl = lcd_cl_pin; 928 if (lcd_da_pin != PIN_NOT_SET) 929 lcd.pins.da = lcd_da_pin; 930 if (lcd_bl_pin != PIN_NOT_SET) 931 lcd.pins.bl = lcd_bl_pin; 932 933 /* this is used to catch wrong and default values */ 934 if (charlcd->width <= 0) 935 charlcd->width = DEFAULT_LCD_WIDTH; 936 if (hdc->bwidth <= 0) 937 hdc->bwidth = DEFAULT_LCD_BWIDTH; 938 if (hdc->hwidth <= 0) 939 hdc->hwidth = DEFAULT_LCD_HWIDTH; 940 if (charlcd->height <= 0) 941 charlcd->height = DEFAULT_LCD_HEIGHT; 942 943 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */ 944 charlcd->ops = &charlcd_ops; 945 hdc->write_data = lcd_write_data_s; 946 hdc->write_cmd = lcd_write_cmd_s; 947 948 if (lcd.pins.cl == PIN_NOT_SET) 949 lcd.pins.cl = DEFAULT_LCD_PIN_SCL; 950 if (lcd.pins.da == PIN_NOT_SET) 951 lcd.pins.da = DEFAULT_LCD_PIN_SDA; 952 953 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */ 954 charlcd->ops = &charlcd_ops; 955 hdc->write_data = lcd_write_data_p8; 956 hdc->write_cmd = lcd_write_cmd_p8; 957 958 if (lcd.pins.e == PIN_NOT_SET) 959 lcd.pins.e = DEFAULT_LCD_PIN_E; 960 if (lcd.pins.rs == PIN_NOT_SET) 961 lcd.pins.rs = DEFAULT_LCD_PIN_RS; 962 if (lcd.pins.rw == PIN_NOT_SET) 963 lcd.pins.rw = DEFAULT_LCD_PIN_RW; 964 } else { 965 charlcd->ops = &charlcd_ops; 966 hdc->write_data = lcd_write_data_tilcd; 967 hdc->write_cmd = lcd_write_cmd_tilcd; 968 } 969 970 if (lcd.pins.bl == PIN_NOT_SET) 971 lcd.pins.bl = DEFAULT_LCD_PIN_BL; 972 973 if (lcd.pins.e == PIN_NOT_SET) 974 lcd.pins.e = PIN_NONE; 975 if (lcd.pins.rs == PIN_NOT_SET) 976 lcd.pins.rs = PIN_NONE; 977 if (lcd.pins.rw == PIN_NOT_SET) 978 lcd.pins.rw = PIN_NONE; 979 if (lcd.pins.bl == PIN_NOT_SET) 980 lcd.pins.bl = PIN_NONE; 981 if (lcd.pins.cl == PIN_NOT_SET) 982 lcd.pins.cl = PIN_NONE; 983 if (lcd.pins.da == PIN_NOT_SET) 984 lcd.pins.da = PIN_NONE; 985 986 if (lcd.charset == NOT_SET) 987 lcd.charset = DEFAULT_LCD_CHARSET; 988 989 if (lcd.charset == LCD_CHARSET_KS0074) 990 charlcd->char_conv = lcd_char_conv_ks0074; 991 else 992 charlcd->char_conv = NULL; 993 994 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E], 995 lcd_bits[LCD_PORT_C][LCD_BIT_E]); 996 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS], 997 lcd_bits[LCD_PORT_C][LCD_BIT_RS]); 998 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW], 999 lcd_bits[LCD_PORT_C][LCD_BIT_RW]); 1000 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL], 1001 lcd_bits[LCD_PORT_C][LCD_BIT_BL]); 1002 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL], 1003 lcd_bits[LCD_PORT_C][LCD_BIT_CL]); 1004 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA], 1005 lcd_bits[LCD_PORT_C][LCD_BIT_DA]); 1006 1007 lcd.charlcd = charlcd; 1008 lcd.initialized = true; 1009 } 1010 1011 /* 1012 * These are the file operation function for user access to /dev/keypad 1013 */ 1014 1015 static ssize_t keypad_read(struct file *file, 1016 char __user *buf, size_t count, loff_t *ppos) 1017 { 1018 unsigned i = *ppos; 1019 char __user *tmp = buf; 1020 1021 if (keypad_buflen == 0) { 1022 if (file->f_flags & O_NONBLOCK) 1023 return -EAGAIN; 1024 1025 if (wait_event_interruptible(keypad_read_wait, 1026 keypad_buflen != 0)) 1027 return -EINTR; 1028 } 1029 1030 for (; count-- > 0 && (keypad_buflen > 0); 1031 ++i, ++tmp, --keypad_buflen) { 1032 put_user(keypad_buffer[keypad_start], tmp); 1033 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER; 1034 } 1035 *ppos = i; 1036 1037 return tmp - buf; 1038 } 1039 1040 static int keypad_open(struct inode *inode, struct file *file) 1041 { 1042 int ret; 1043 1044 ret = -EBUSY; 1045 if (!atomic_dec_and_test(&keypad_available)) 1046 goto fail; /* open only once at a time */ 1047 1048 ret = -EPERM; 1049 if (file->f_mode & FMODE_WRITE) /* device is read-only */ 1050 goto fail; 1051 1052 keypad_buflen = 0; /* flush the buffer on opening */ 1053 return 0; 1054 fail: 1055 atomic_inc(&keypad_available); 1056 return ret; 1057 } 1058 1059 static int keypad_release(struct inode *inode, struct file *file) 1060 { 1061 atomic_inc(&keypad_available); 1062 return 0; 1063 } 1064 1065 static const struct file_operations keypad_fops = { 1066 .read = keypad_read, /* read */ 1067 .open = keypad_open, /* open */ 1068 .release = keypad_release, /* close */ 1069 .llseek = default_llseek, 1070 }; 1071 1072 static struct miscdevice keypad_dev = { 1073 .minor = KEYPAD_MINOR, 1074 .name = "keypad", 1075 .fops = &keypad_fops, 1076 }; 1077 1078 static void keypad_send_key(const char *string, int max_len) 1079 { 1080 /* send the key to the device only if a process is attached to it. */ 1081 if (!atomic_read(&keypad_available)) { 1082 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) { 1083 keypad_buffer[(keypad_start + keypad_buflen++) % 1084 KEYPAD_BUFFER] = *string++; 1085 } 1086 wake_up_interruptible(&keypad_read_wait); 1087 } 1088 } 1089 1090 /* this function scans all the bits involving at least one logical signal, 1091 * and puts the results in the bitfield "phys_read" (one bit per established 1092 * contact), and sets "phys_read_prev" to "phys_read". 1093 * 1094 * Note: to debounce input signals, we will only consider as switched a signal 1095 * which is stable across 2 measures. Signals which are different between two 1096 * reads will be kept as they previously were in their logical form (phys_prev). 1097 * A signal which has just switched will have a 1 in 1098 * (phys_read ^ phys_read_prev). 1099 */ 1100 static void phys_scan_contacts(void) 1101 { 1102 int bit, bitval; 1103 char oldval; 1104 char bitmask; 1105 char gndmask; 1106 1107 phys_prev = phys_curr; 1108 phys_read_prev = phys_read; 1109 phys_read = 0; /* flush all signals */ 1110 1111 /* keep track of old value, with all outputs disabled */ 1112 oldval = r_dtr(pprt) | scan_mask_o; 1113 /* activate all keyboard outputs (active low) */ 1114 w_dtr(pprt, oldval & ~scan_mask_o); 1115 1116 /* will have a 1 for each bit set to gnd */ 1117 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; 1118 /* disable all matrix signals */ 1119 w_dtr(pprt, oldval); 1120 1121 /* now that all outputs are cleared, the only active input bits are 1122 * directly connected to the ground 1123 */ 1124 1125 /* 1 for each grounded input */ 1126 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; 1127 1128 /* grounded inputs are signals 40-44 */ 1129 phys_read |= (__u64)gndmask << 40; 1130 1131 if (bitmask != gndmask) { 1132 /* 1133 * since clearing the outputs changed some inputs, we know 1134 * that some input signals are currently tied to some outputs. 1135 * So we'll scan them. 1136 */ 1137 for (bit = 0; bit < 8; bit++) { 1138 bitval = BIT(bit); 1139 1140 if (!(scan_mask_o & bitval)) /* skip unused bits */ 1141 continue; 1142 1143 w_dtr(pprt, oldval & ~bitval); /* enable this output */ 1144 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask; 1145 phys_read |= (__u64)bitmask << (5 * bit); 1146 } 1147 w_dtr(pprt, oldval); /* disable all outputs */ 1148 } 1149 /* 1150 * this is easy: use old bits when they are flapping, 1151 * use new ones when stable 1152 */ 1153 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) | 1154 (phys_read & ~(phys_read ^ phys_read_prev)); 1155 } 1156 1157 static inline int input_state_high(struct logical_input *input) 1158 { 1159 #if 0 1160 /* FIXME: 1161 * this is an invalid test. It tries to catch 1162 * transitions from single-key to multiple-key, but 1163 * doesn't take into account the contacts polarity. 1164 * The only solution to the problem is to parse keys 1165 * from the most complex to the simplest combinations, 1166 * and mark them as 'caught' once a combination 1167 * matches, then unmatch it for all other ones. 1168 */ 1169 1170 /* try to catch dangerous transitions cases : 1171 * someone adds a bit, so this signal was a false 1172 * positive resulting from a transition. We should 1173 * invalidate the signal immediately and not call the 1174 * release function. 1175 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release. 1176 */ 1177 if (((phys_prev & input->mask) == input->value) && 1178 ((phys_curr & input->mask) > input->value)) { 1179 input->state = INPUT_ST_LOW; /* invalidate */ 1180 return 1; 1181 } 1182 #endif 1183 1184 if ((phys_curr & input->mask) == input->value) { 1185 if ((input->type == INPUT_TYPE_STD) && 1186 (input->high_timer == 0)) { 1187 input->high_timer++; 1188 if (input->u.std.press_fct) 1189 input->u.std.press_fct(input->u.std.press_data); 1190 } else if (input->type == INPUT_TYPE_KBD) { 1191 /* will turn on the light */ 1192 keypressed = 1; 1193 1194 if (input->high_timer == 0) { 1195 char *press_str = input->u.kbd.press_str; 1196 1197 if (press_str[0]) { 1198 int s = sizeof(input->u.kbd.press_str); 1199 1200 keypad_send_key(press_str, s); 1201 } 1202 } 1203 1204 if (input->u.kbd.repeat_str[0]) { 1205 char *repeat_str = input->u.kbd.repeat_str; 1206 1207 if (input->high_timer >= KEYPAD_REP_START) { 1208 int s = sizeof(input->u.kbd.repeat_str); 1209 1210 input->high_timer -= KEYPAD_REP_DELAY; 1211 keypad_send_key(repeat_str, s); 1212 } 1213 /* we will need to come back here soon */ 1214 inputs_stable = 0; 1215 } 1216 1217 if (input->high_timer < 255) 1218 input->high_timer++; 1219 } 1220 return 1; 1221 } 1222 1223 /* else signal falling down. Let's fall through. */ 1224 input->state = INPUT_ST_FALLING; 1225 input->fall_timer = 0; 1226 1227 return 0; 1228 } 1229 1230 static inline void input_state_falling(struct logical_input *input) 1231 { 1232 #if 0 1233 /* FIXME !!! same comment as in input_state_high */ 1234 if (((phys_prev & input->mask) == input->value) && 1235 ((phys_curr & input->mask) > input->value)) { 1236 input->state = INPUT_ST_LOW; /* invalidate */ 1237 return; 1238 } 1239 #endif 1240 1241 if ((phys_curr & input->mask) == input->value) { 1242 if (input->type == INPUT_TYPE_KBD) { 1243 /* will turn on the light */ 1244 keypressed = 1; 1245 1246 if (input->u.kbd.repeat_str[0]) { 1247 char *repeat_str = input->u.kbd.repeat_str; 1248 1249 if (input->high_timer >= KEYPAD_REP_START) { 1250 int s = sizeof(input->u.kbd.repeat_str); 1251 1252 input->high_timer -= KEYPAD_REP_DELAY; 1253 keypad_send_key(repeat_str, s); 1254 } 1255 /* we will need to come back here soon */ 1256 inputs_stable = 0; 1257 } 1258 1259 if (input->high_timer < 255) 1260 input->high_timer++; 1261 } 1262 input->state = INPUT_ST_HIGH; 1263 } else if (input->fall_timer >= input->fall_time) { 1264 /* call release event */ 1265 if (input->type == INPUT_TYPE_STD) { 1266 void (*release_fct)(int) = input->u.std.release_fct; 1267 1268 if (release_fct) 1269 release_fct(input->u.std.release_data); 1270 } else if (input->type == INPUT_TYPE_KBD) { 1271 char *release_str = input->u.kbd.release_str; 1272 1273 if (release_str[0]) { 1274 int s = sizeof(input->u.kbd.release_str); 1275 1276 keypad_send_key(release_str, s); 1277 } 1278 } 1279 1280 input->state = INPUT_ST_LOW; 1281 } else { 1282 input->fall_timer++; 1283 inputs_stable = 0; 1284 } 1285 } 1286 1287 static void panel_process_inputs(void) 1288 { 1289 struct logical_input *input; 1290 1291 keypressed = 0; 1292 inputs_stable = 1; 1293 list_for_each_entry(input, &logical_inputs, list) { 1294 switch (input->state) { 1295 case INPUT_ST_LOW: 1296 if ((phys_curr & input->mask) != input->value) 1297 break; 1298 /* if all needed ones were already set previously, 1299 * this means that this logical signal has been 1300 * activated by the releasing of another combined 1301 * signal, so we don't want to match. 1302 * eg: AB -(release B)-> A -(release A)-> 0 : 1303 * don't match A. 1304 */ 1305 if ((phys_prev & input->mask) == input->value) 1306 break; 1307 input->rise_timer = 0; 1308 input->state = INPUT_ST_RISING; 1309 fallthrough; 1310 case INPUT_ST_RISING: 1311 if ((phys_curr & input->mask) != input->value) { 1312 input->state = INPUT_ST_LOW; 1313 break; 1314 } 1315 if (input->rise_timer < input->rise_time) { 1316 inputs_stable = 0; 1317 input->rise_timer++; 1318 break; 1319 } 1320 input->high_timer = 0; 1321 input->state = INPUT_ST_HIGH; 1322 fallthrough; 1323 case INPUT_ST_HIGH: 1324 if (input_state_high(input)) 1325 break; 1326 fallthrough; 1327 case INPUT_ST_FALLING: 1328 input_state_falling(input); 1329 } 1330 } 1331 } 1332 1333 static void panel_scan_timer(struct timer_list *unused) 1334 { 1335 if (keypad.enabled && keypad_initialized) { 1336 if (spin_trylock_irq(&pprt_lock)) { 1337 phys_scan_contacts(); 1338 1339 /* no need for the parport anymore */ 1340 spin_unlock_irq(&pprt_lock); 1341 } 1342 1343 if (!inputs_stable || phys_curr != phys_prev) 1344 panel_process_inputs(); 1345 } 1346 1347 if (keypressed && lcd.enabled && lcd.initialized) 1348 charlcd_poke(lcd.charlcd); 1349 1350 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME); 1351 } 1352 1353 static void init_scan_timer(void) 1354 { 1355 if (scan_timer.function) 1356 return; /* already started */ 1357 1358 timer_setup(&scan_timer, panel_scan_timer, 0); 1359 scan_timer.expires = jiffies + INPUT_POLL_TIME; 1360 add_timer(&scan_timer); 1361 } 1362 1363 /* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits. 1364 * if <omask> or <imask> are non-null, they will be or'ed with the bits 1365 * corresponding to out and in bits respectively. 1366 * returns 1 if ok, 0 if error (in which case, nothing is written). 1367 */ 1368 static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value, 1369 u8 *imask, u8 *omask) 1370 { 1371 const char sigtab[] = "EeSsPpAaBb"; 1372 u8 im, om; 1373 __u64 m, v; 1374 1375 om = 0; 1376 im = 0; 1377 m = 0ULL; 1378 v = 0ULL; 1379 while (*name) { 1380 int in, out, bit, neg; 1381 const char *idx; 1382 1383 idx = strchr(sigtab, *name); 1384 if (!idx) 1385 return 0; /* input name not found */ 1386 1387 in = idx - sigtab; 1388 neg = (in & 1); /* odd (lower) names are negated */ 1389 in >>= 1; 1390 im |= BIT(in); 1391 1392 name++; 1393 if (*name >= '0' && *name <= '7') { 1394 out = *name - '0'; 1395 om |= BIT(out); 1396 } else if (*name == '-') { 1397 out = 8; 1398 } else { 1399 return 0; /* unknown bit name */ 1400 } 1401 1402 bit = (out * 5) + in; 1403 1404 m |= 1ULL << bit; 1405 if (!neg) 1406 v |= 1ULL << bit; 1407 name++; 1408 } 1409 *mask = m; 1410 *value = v; 1411 if (imask) 1412 *imask |= im; 1413 if (omask) 1414 *omask |= om; 1415 return 1; 1416 } 1417 1418 /* tries to bind a key to the signal name <name>. The key will send the 1419 * strings <press>, <repeat>, <release> for these respective events. 1420 * Returns the pointer to the new key if ok, NULL if the key could not be bound. 1421 */ 1422 static struct logical_input *panel_bind_key(const char *name, const char *press, 1423 const char *repeat, 1424 const char *release) 1425 { 1426 struct logical_input *key; 1427 1428 key = kzalloc_obj(*key); 1429 if (!key) 1430 return NULL; 1431 1432 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i, 1433 &scan_mask_o)) { 1434 kfree(key); 1435 return NULL; 1436 } 1437 1438 key->type = INPUT_TYPE_KBD; 1439 key->state = INPUT_ST_LOW; 1440 key->rise_time = 1; 1441 key->fall_time = 1; 1442 1443 strtomem_pad(key->u.kbd.press_str, press, '\0'); 1444 strtomem_pad(key->u.kbd.repeat_str, repeat, '\0'); 1445 strtomem_pad(key->u.kbd.release_str, release, '\0'); 1446 list_add(&key->list, &logical_inputs); 1447 return key; 1448 } 1449 1450 static void keypad_init(void) 1451 { 1452 int keynum; 1453 1454 init_waitqueue_head(&keypad_read_wait); 1455 keypad_buflen = 0; /* flushes any eventual noisy keystroke */ 1456 1457 /* Let's create all known keys */ 1458 1459 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) { 1460 panel_bind_key(keypad_profile[keynum][0], 1461 keypad_profile[keynum][1], 1462 keypad_profile[keynum][2], 1463 keypad_profile[keynum][3]); 1464 } 1465 1466 init_scan_timer(); 1467 keypad_initialized = 1; 1468 } 1469 1470 /**************************************************/ 1471 /* device initialization */ 1472 /**************************************************/ 1473 1474 static void panel_attach(struct parport *port) 1475 { 1476 int selected_keypad_type = NOT_SET; 1477 struct pardev_cb panel_cb; 1478 1479 /* take care of an eventual profile */ 1480 switch (profile) { 1481 case PANEL_PROFILE_CUSTOM: 1482 /* custom profile */ 1483 selected_keypad_type = DEFAULT_KEYPAD_TYPE; 1484 selected_lcd_type = DEFAULT_LCD_TYPE; 1485 break; 1486 case PANEL_PROFILE_OLD: 1487 /* 8 bits, 2*16, old keypad */ 1488 selected_keypad_type = KEYPAD_TYPE_OLD; 1489 selected_lcd_type = LCD_TYPE_OLD; 1490 1491 /* TODO: This two are a little hacky, sort it out later */ 1492 if (lcd_width == NOT_SET) 1493 lcd_width = 16; 1494 if (lcd_hwidth == NOT_SET) 1495 lcd_hwidth = 16; 1496 break; 1497 case PANEL_PROFILE_NEW: 1498 /* serial, 2*16, new keypad */ 1499 selected_keypad_type = KEYPAD_TYPE_NEW; 1500 selected_lcd_type = LCD_TYPE_KS0074; 1501 break; 1502 case PANEL_PROFILE_HANTRONIX: 1503 /* 8 bits, 2*16 hantronix-like, no keypad */ 1504 selected_keypad_type = KEYPAD_TYPE_NONE; 1505 selected_lcd_type = LCD_TYPE_HANTRONIX; 1506 break; 1507 case PANEL_PROFILE_NEXCOM: 1508 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */ 1509 selected_keypad_type = KEYPAD_TYPE_NEXCOM; 1510 selected_lcd_type = LCD_TYPE_NEXCOM; 1511 break; 1512 case PANEL_PROFILE_LARGE: 1513 /* 8 bits, 2*40, old keypad */ 1514 selected_keypad_type = KEYPAD_TYPE_OLD; 1515 selected_lcd_type = LCD_TYPE_OLD; 1516 break; 1517 } 1518 1519 /* 1520 * Overwrite selection with module param values (both keypad and lcd), 1521 * where the deprecated params have lower prio. 1522 */ 1523 if (keypad_enabled != NOT_SET) 1524 selected_keypad_type = keypad_enabled; 1525 if (keypad_type != NOT_SET) 1526 selected_keypad_type = keypad_type; 1527 1528 keypad.enabled = (selected_keypad_type > 0); 1529 1530 if (lcd_enabled != NOT_SET) 1531 selected_lcd_type = lcd_enabled; 1532 if (lcd_type != NOT_SET) 1533 selected_lcd_type = lcd_type; 1534 1535 lcd.enabled = (selected_lcd_type > 0); 1536 1537 if (lcd.enabled) { 1538 /* 1539 * Init lcd struct with load-time values to preserve exact 1540 * current functionality (at least for now). 1541 */ 1542 lcd.charset = lcd_charset; 1543 lcd.proto = lcd_proto; 1544 lcd.pins.e = lcd_e_pin; 1545 lcd.pins.rs = lcd_rs_pin; 1546 lcd.pins.rw = lcd_rw_pin; 1547 lcd.pins.cl = lcd_cl_pin; 1548 lcd.pins.da = lcd_da_pin; 1549 lcd.pins.bl = lcd_bl_pin; 1550 } 1551 1552 switch (selected_keypad_type) { 1553 case KEYPAD_TYPE_OLD: 1554 keypad_profile = old_keypad_profile; 1555 break; 1556 case KEYPAD_TYPE_NEW: 1557 keypad_profile = new_keypad_profile; 1558 break; 1559 case KEYPAD_TYPE_NEXCOM: 1560 keypad_profile = nexcom_keypad_profile; 1561 break; 1562 default: 1563 keypad_profile = NULL; 1564 break; 1565 } 1566 1567 if (!lcd.enabled && !keypad.enabled) { 1568 /* no device enabled, let's exit */ 1569 pr_err("panel driver disabled.\n"); 1570 return; 1571 } 1572 1573 if (port->number != parport) 1574 return; 1575 1576 if (pprt) { 1577 pr_err("%s: port->number=%d parport=%d, already registered!\n", 1578 __func__, port->number, parport); 1579 return; 1580 } 1581 1582 memset(&panel_cb, 0, sizeof(panel_cb)); 1583 panel_cb.private = &pprt; 1584 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */ 1585 1586 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0); 1587 if (!pprt) { 1588 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n", 1589 __func__, port->number, parport); 1590 return; 1591 } 1592 1593 if (parport_claim(pprt)) { 1594 pr_err("could not claim access to parport%d. Aborting.\n", 1595 parport); 1596 goto err_unreg_device; 1597 } 1598 1599 /* must init LCD first, just in case an IRQ from the keypad is 1600 * generated at keypad init 1601 */ 1602 if (lcd.enabled) { 1603 lcd_init(); 1604 if (!lcd.charlcd || charlcd_register(lcd.charlcd)) 1605 goto err_unreg_device; 1606 } 1607 1608 if (keypad.enabled) { 1609 keypad_init(); 1610 if (misc_register(&keypad_dev)) 1611 goto err_lcd_unreg; 1612 } 1613 return; 1614 1615 err_lcd_unreg: 1616 if (scan_timer.function) 1617 timer_delete_sync(&scan_timer); 1618 if (lcd.enabled) 1619 charlcd_unregister(lcd.charlcd); 1620 err_unreg_device: 1621 hd44780_common_free(lcd.charlcd); 1622 lcd.charlcd = NULL; 1623 parport_unregister_device(pprt); 1624 pprt = NULL; 1625 } 1626 1627 static void panel_detach(struct parport *port) 1628 { 1629 if (port->number != parport) 1630 return; 1631 1632 if (!pprt) { 1633 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n", 1634 __func__, port->number, parport); 1635 return; 1636 } 1637 if (scan_timer.function) 1638 timer_delete_sync(&scan_timer); 1639 1640 if (keypad.enabled) { 1641 misc_deregister(&keypad_dev); 1642 keypad_initialized = 0; 1643 } 1644 1645 if (lcd.enabled) { 1646 charlcd_unregister(lcd.charlcd); 1647 lcd.initialized = false; 1648 hd44780_common_free(lcd.charlcd); 1649 lcd.charlcd = NULL; 1650 } 1651 1652 /* TODO: free all input signals */ 1653 parport_release(pprt); 1654 parport_unregister_device(pprt); 1655 pprt = NULL; 1656 } 1657 1658 static struct parport_driver panel_driver = { 1659 .name = "panel", 1660 .match_port = panel_attach, 1661 .detach = panel_detach, 1662 }; 1663 module_parport_driver(panel_driver); 1664 1665 MODULE_AUTHOR("Willy Tarreau"); 1666 MODULE_LICENSE("GPL"); 1667