Commit | Line | Data |
---|---|---|
7005b584 | 1 | /* |
698b1515 WT |
2 | * Front panel driver for Linux |
3 | * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu> | |
7005b584 | 4 | * |
698b1515 WT |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; either version | |
8 | * 2 of the License, or (at your option) any later version. | |
7005b584 | 9 | * |
698b1515 WT |
10 | * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad) |
11 | * connected to a parallel printer port. | |
7005b584 | 12 | * |
698b1515 WT |
13 | * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit |
14 | * serial module compatible with Samsung's KS0074. The pins may be connected in | |
15 | * any combination, everything is programmable. | |
7005b584 | 16 | * |
698b1515 WT |
17 | * The keypad consists in a matrix of push buttons connecting input pins to |
18 | * data output pins or to the ground. The combinations have to be hard-coded | |
19 | * in the driver, though several profiles exist and adding new ones is easy. | |
7005b584 | 20 | * |
698b1515 WT |
21 | * Several profiles are provided for commonly found LCD+keypad modules on the |
22 | * market, such as those found in Nexcom's appliances. | |
7005b584 WT |
23 | * |
24 | * FIXME: | |
25 | * - the initialization/deinitialization process is very dirty and should | |
26 | * be rewritten. It may even be buggy. | |
27 | * | |
28 | * TODO: | |
29 | * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs) | |
30 | * - make the LCD a part of a virtual screen of Vx*Vy | |
31 | * - make the inputs list smp-safe | |
32 | * - change the keyboard to a double mapping : signals -> key_id -> values | |
33 | * so that applications can change values without knowing signals | |
34 | * | |
35 | */ | |
36 | ||
493aa896 TY |
37 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
38 | ||
7005b584 WT |
39 | #include <linux/module.h> |
40 | ||
41 | #include <linux/types.h> | |
42 | #include <linux/errno.h> | |
43 | #include <linux/signal.h> | |
44 | #include <linux/sched.h> | |
45 | #include <linux/spinlock.h> | |
7005b584 WT |
46 | #include <linux/interrupt.h> |
47 | #include <linux/miscdevice.h> | |
698b1515 | 48 | #include <linux/slab.h> |
7005b584 WT |
49 | #include <linux/ioport.h> |
50 | #include <linux/fcntl.h> | |
51 | #include <linux/init.h> | |
52 | #include <linux/delay.h> | |
d85170ed | 53 | #include <linux/kernel.h> |
7005b584 WT |
54 | #include <linux/ctype.h> |
55 | #include <linux/parport.h> | |
7005b584 WT |
56 | #include <linux/list.h> |
57 | #include <linux/notifier.h> | |
58 | #include <linux/reboot.h> | |
273b281f | 59 | #include <generated/utsrelease.h> |
7005b584 | 60 | |
698b1515 | 61 | #include <linux/io.h> |
48f658bb | 62 | #include <linux/uaccess.h> |
7005b584 | 63 | |
7005b584 WT |
64 | #define LCD_MINOR 156 |
65 | #define KEYPAD_MINOR 185 | |
7005b584 WT |
66 | |
67 | #define PANEL_VERSION "0.9.5" | |
68 | ||
69 | #define LCD_MAXBYTES 256 /* max burst write */ | |
70 | ||
7005b584 | 71 | #define KEYPAD_BUFFER 64 |
7005b584 | 72 | |
429ccf05 HH |
73 | /* poll the keyboard this every second */ |
74 | #define INPUT_POLL_TIME (HZ/50) | |
75 | /* a key starts to repeat after this times INPUT_POLL_TIME */ | |
76 | #define KEYPAD_REP_START (10) | |
77 | /* a key repeats this times INPUT_POLL_TIME */ | |
78 | #define KEYPAD_REP_DELAY (2) | |
79 | ||
80 | /* keep the light on this times INPUT_POLL_TIME for each flash */ | |
81 | #define FLASH_LIGHT_TEMPO (200) | |
7005b584 WT |
82 | |
83 | /* converts an r_str() input to an active high, bits string : 000BAOSE */ | |
84 | #define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3) | |
85 | ||
698b1515 WT |
86 | #define PNL_PBUSY 0x80 /* inverted input, active low */ |
87 | #define PNL_PACK 0x40 /* direct input, active low */ | |
88 | #define PNL_POUTPA 0x20 /* direct input, active high */ | |
89 | #define PNL_PSELECD 0x10 /* direct input, active high */ | |
90 | #define PNL_PERRORP 0x08 /* direct input, active low */ | |
7005b584 | 91 | |
698b1515 | 92 | #define PNL_PBIDIR 0x20 /* bi-directional ports */ |
429ccf05 HH |
93 | /* high to read data in or-ed with data out */ |
94 | #define PNL_PINTEN 0x10 | |
698b1515 WT |
95 | #define PNL_PSELECP 0x08 /* inverted output, active low */ |
96 | #define PNL_PINITP 0x04 /* direct output, active low */ | |
97 | #define PNL_PAUTOLF 0x02 /* inverted output, active low */ | |
98 | #define PNL_PSTROBE 0x01 /* inverted output */ | |
7005b584 WT |
99 | |
100 | #define PNL_PD0 0x01 | |
101 | #define PNL_PD1 0x02 | |
102 | #define PNL_PD2 0x04 | |
103 | #define PNL_PD3 0x08 | |
104 | #define PNL_PD4 0x10 | |
105 | #define PNL_PD5 0x20 | |
106 | #define PNL_PD6 0x40 | |
107 | #define PNL_PD7 0x80 | |
108 | ||
109 | #define PIN_NONE 0 | |
110 | #define PIN_STROBE 1 | |
111 | #define PIN_D0 2 | |
112 | #define PIN_D1 3 | |
113 | #define PIN_D2 4 | |
114 | #define PIN_D3 5 | |
115 | #define PIN_D4 6 | |
116 | #define PIN_D5 7 | |
117 | #define PIN_D6 8 | |
118 | #define PIN_D7 9 | |
119 | #define PIN_AUTOLF 14 | |
120 | #define PIN_INITP 16 | |
121 | #define PIN_SELECP 17 | |
122 | #define PIN_NOT_SET 127 | |
123 | ||
7005b584 WT |
124 | #define LCD_FLAG_S 0x0001 |
125 | #define LCD_FLAG_ID 0x0002 | |
126 | #define LCD_FLAG_B 0x0004 /* blink on */ | |
127 | #define LCD_FLAG_C 0x0008 /* cursor on */ | |
128 | #define LCD_FLAG_D 0x0010 /* display on */ | |
129 | #define LCD_FLAG_F 0x0020 /* large font mode */ | |
130 | #define LCD_FLAG_N 0x0040 /* 2-rows mode */ | |
131 | #define LCD_FLAG_L 0x0080 /* backlight enabled */ | |
132 | ||
2114924a MG |
133 | /* LCD commands */ |
134 | #define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */ | |
135 | ||
136 | #define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */ | |
137 | #define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */ | |
138 | ||
139 | #define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */ | |
140 | #define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */ | |
141 | #define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */ | |
142 | #define LCD_CMD_BLINK_ON 0x01 /* Set blink on */ | |
143 | ||
144 | #define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */ | |
145 | #define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */ | |
146 | #define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */ | |
147 | ||
148 | #define LCD_CMD_FUNCTION_SET 0x20 /* Set function */ | |
149 | #define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */ | |
150 | #define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */ | |
151 | #define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */ | |
152 | ||
153 | #define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */ | |
154 | ||
155 | #define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */ | |
156 | ||
429ccf05 | 157 | #define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */ |
7005b584 WT |
158 | #define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */ |
159 | ||
36277d4a MG |
160 | #define NOT_SET -1 |
161 | ||
7005b584 WT |
162 | /* macros to simplify use of the parallel port */ |
163 | #define r_ctr(x) (parport_read_control((x)->port)) | |
164 | #define r_dtr(x) (parport_read_data((x)->port)) | |
165 | #define r_str(x) (parport_read_status((x)->port)) | |
6ebb56d9 TY |
166 | #define w_ctr(x, y) (parport_write_control((x)->port, (y))) |
167 | #define w_dtr(x, y) (parport_write_data((x)->port, (y))) | |
7005b584 WT |
168 | |
169 | /* this defines which bits are to be used and which ones to be ignored */ | |
429ccf05 HH |
170 | /* logical or of the output bits involved in the scan matrix */ |
171 | static __u8 scan_mask_o; | |
172 | /* logical or of the input bits involved in the scan matrix */ | |
173 | static __u8 scan_mask_i; | |
7005b584 | 174 | |
698b1515 | 175 | typedef __u64 pmask_t; |
7005b584 WT |
176 | |
177 | enum input_type { | |
698b1515 WT |
178 | INPUT_TYPE_STD, |
179 | INPUT_TYPE_KBD, | |
7005b584 WT |
180 | }; |
181 | ||
182 | enum input_state { | |
698b1515 WT |
183 | INPUT_ST_LOW, |
184 | INPUT_ST_RISING, | |
185 | INPUT_ST_HIGH, | |
186 | INPUT_ST_FALLING, | |
7005b584 WT |
187 | }; |
188 | ||
189 | struct logical_input { | |
698b1515 WT |
190 | struct list_head list; |
191 | pmask_t mask; | |
192 | pmask_t value; | |
193 | enum input_type type; | |
194 | enum input_state state; | |
195 | __u8 rise_time, fall_time; | |
196 | __u8 rise_timer, fall_timer, high_timer; | |
197 | ||
198 | union { | |
429ccf05 | 199 | struct { /* valid when type == INPUT_TYPE_STD */ |
68d386bf MA |
200 | void (*press_fct)(int); |
201 | void (*release_fct)(int); | |
698b1515 WT |
202 | int press_data; |
203 | int release_data; | |
204 | } std; | |
429ccf05 HH |
205 | struct { /* valid when type == INPUT_TYPE_KBD */ |
206 | /* strings can be non null-terminated */ | |
698b1515 WT |
207 | char press_str[sizeof(void *) + sizeof(int)]; |
208 | char repeat_str[sizeof(void *) + sizeof(int)]; | |
209 | char release_str[sizeof(void *) + sizeof(int)]; | |
210 | } kbd; | |
211 | } u; | |
7005b584 WT |
212 | }; |
213 | ||
36d2041a | 214 | static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */ |
7005b584 WT |
215 | |
216 | /* physical contacts history | |
217 | * Physical contacts are a 45 bits string of 9 groups of 5 bits each. | |
218 | * The 8 lower groups correspond to output bits 0 to 7, and the 9th group | |
219 | * corresponds to the ground. | |
220 | * Within each group, bits are stored in the same order as read on the port : | |
221 | * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0). | |
222 | * So, each __u64 (or pmask_t) is represented like this : | |
223 | * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE | |
224 | * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00> | |
225 | */ | |
429ccf05 HH |
226 | |
227 | /* what has just been read from the I/O ports */ | |
228 | static pmask_t phys_read; | |
229 | /* previous phys_read */ | |
230 | static pmask_t phys_read_prev; | |
231 | /* stabilized phys_read (phys_read|phys_read_prev) */ | |
232 | static pmask_t phys_curr; | |
233 | /* previous phys_curr */ | |
234 | static pmask_t phys_prev; | |
235 | /* 0 means that at least one logical signal needs be computed */ | |
236 | static char inputs_stable; | |
7005b584 | 237 | |
7005b584 | 238 | /* these variables are specific to the keypad */ |
a8b2580b MG |
239 | static struct { |
240 | bool enabled; | |
241 | } keypad; | |
242 | ||
7005b584 | 243 | static char keypad_buffer[KEYPAD_BUFFER]; |
698b1515 WT |
244 | static int keypad_buflen; |
245 | static int keypad_start; | |
246 | static char keypressed; | |
7005b584 | 247 | static wait_queue_head_t keypad_read_wait; |
7005b584 WT |
248 | |
249 | /* lcd-specific variables */ | |
a8b2580b MG |
250 | static struct { |
251 | bool enabled; | |
6d8b588c MG |
252 | bool initialized; |
253 | bool must_clear; | |
254 | ||
8037e2a3 MG |
255 | int height; |
256 | int width; | |
257 | int bwidth; | |
258 | int hwidth; | |
259 | int charset; | |
260 | int proto; | |
6d8b588c MG |
261 | int light_tempo; |
262 | ||
8037e2a3 MG |
263 | /* TODO: use union here? */ |
264 | struct { | |
265 | int e; | |
266 | int rs; | |
267 | int rw; | |
268 | int cl; | |
269 | int da; | |
270 | int bl; | |
271 | } pins; | |
6d8b588c MG |
272 | |
273 | /* contains the LCD config state */ | |
274 | unsigned long int flags; | |
275 | ||
276 | /* Contains the LCD X and Y offset */ | |
277 | struct { | |
278 | unsigned long int x; | |
279 | unsigned long int y; | |
280 | } addr; | |
281 | ||
282 | /* Current escape sequence and it's length or -1 if outside */ | |
283 | struct { | |
284 | char buf[LCD_ESCAPE_LEN + 1]; | |
285 | int len; | |
286 | } esc_seq; | |
a8b2580b | 287 | } lcd; |
429ccf05 | 288 | |
87b8e0c8 MG |
289 | /* Needed only for init */ |
290 | static int selected_lcd_type = NOT_SET; | |
291 | ||
7005b584 WT |
292 | /* |
293 | * Bit masks to convert LCD signals to parallel port outputs. | |
294 | * _d_ are values for data port, _c_ are for control port. | |
295 | * [0] = signal OFF, [1] = signal ON, [2] = mask | |
296 | */ | |
698b1515 WT |
297 | #define BIT_CLR 0 |
298 | #define BIT_SET 1 | |
299 | #define BIT_MSK 2 | |
7005b584 WT |
300 | #define BIT_STATES 3 |
301 | /* | |
302 | * one entry for each bit on the LCD | |
303 | */ | |
304 | #define LCD_BIT_E 0 | |
305 | #define LCD_BIT_RS 1 | |
306 | #define LCD_BIT_RW 2 | |
307 | #define LCD_BIT_BL 3 | |
308 | #define LCD_BIT_CL 4 | |
309 | #define LCD_BIT_DA 5 | |
310 | #define LCD_BITS 6 | |
311 | ||
312 | /* | |
313 | * each bit can be either connected to a DATA or CTRL port | |
314 | */ | |
315 | #define LCD_PORT_C 0 | |
316 | #define LCD_PORT_D 1 | |
317 | #define LCD_PORTS 2 | |
318 | ||
319 | static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES]; | |
320 | ||
321 | /* | |
322 | * LCD protocols | |
323 | */ | |
324 | #define LCD_PROTO_PARALLEL 0 | |
325 | #define LCD_PROTO_SERIAL 1 | |
77943d31 | 326 | #define LCD_PROTO_TI_DA8XX_LCD 2 |
7005b584 WT |
327 | |
328 | /* | |
329 | * LCD character sets | |
330 | */ | |
331 | #define LCD_CHARSET_NORMAL 0 | |
332 | #define LCD_CHARSET_KS0074 1 | |
333 | ||
334 | /* | |
335 | * LCD types | |
336 | */ | |
337 | #define LCD_TYPE_NONE 0 | |
2c20d92d SM |
338 | #define LCD_TYPE_CUSTOM 1 |
339 | #define LCD_TYPE_OLD 2 | |
340 | #define LCD_TYPE_KS0074 3 | |
341 | #define LCD_TYPE_HANTRONIX 4 | |
342 | #define LCD_TYPE_NEXCOM 5 | |
7005b584 WT |
343 | |
344 | /* | |
345 | * keypad types | |
346 | */ | |
347 | #define KEYPAD_TYPE_NONE 0 | |
348 | #define KEYPAD_TYPE_OLD 1 | |
349 | #define KEYPAD_TYPE_NEW 2 | |
350 | #define KEYPAD_TYPE_NEXCOM 3 | |
351 | ||
352 | /* | |
353 | * panel profiles | |
354 | */ | |
355 | #define PANEL_PROFILE_CUSTOM 0 | |
356 | #define PANEL_PROFILE_OLD 1 | |
357 | #define PANEL_PROFILE_NEW 2 | |
358 | #define PANEL_PROFILE_HANTRONIX 3 | |
359 | #define PANEL_PROFILE_NEXCOM 4 | |
360 | #define PANEL_PROFILE_LARGE 5 | |
361 | ||
362 | /* | |
363 | * Construct custom config from the kernel's configuration | |
364 | */ | |
7005b584 | 365 | #define DEFAULT_PARPORT 0 |
fe4d7e2c | 366 | #define DEFAULT_PROFILE PANEL_PROFILE_LARGE |
98fac3d3 MG |
367 | #define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD |
368 | #define DEFAULT_LCD_TYPE LCD_TYPE_OLD | |
fe4d7e2c | 369 | #define DEFAULT_LCD_HEIGHT 2 |
7005b584 WT |
370 | #define DEFAULT_LCD_WIDTH 40 |
371 | #define DEFAULT_LCD_BWIDTH 40 | |
372 | #define DEFAULT_LCD_HWIDTH 64 | |
fe4d7e2c | 373 | #define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL |
7005b584 WT |
374 | #define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL |
375 | ||
376 | #define DEFAULT_LCD_PIN_E PIN_AUTOLF | |
377 | #define DEFAULT_LCD_PIN_RS PIN_SELECP | |
378 | #define DEFAULT_LCD_PIN_RW PIN_INITP | |
379 | #define DEFAULT_LCD_PIN_SCL PIN_STROBE | |
380 | #define DEFAULT_LCD_PIN_SDA PIN_D0 | |
381 | #define DEFAULT_LCD_PIN_BL PIN_NOT_SET | |
7005b584 | 382 | |
7005b584 WT |
383 | #ifdef CONFIG_PANEL_PARPORT |
384 | #undef DEFAULT_PARPORT | |
385 | #define DEFAULT_PARPORT CONFIG_PANEL_PARPORT | |
386 | #endif | |
387 | ||
1e13e8aa MG |
388 | #ifdef CONFIG_PANEL_PROFILE |
389 | #undef DEFAULT_PROFILE | |
390 | #define DEFAULT_PROFILE CONFIG_PANEL_PROFILE | |
391 | #endif | |
392 | ||
698b1515 | 393 | #if DEFAULT_PROFILE == 0 /* custom */ |
7005b584 | 394 | #ifdef CONFIG_PANEL_KEYPAD |
98fac3d3 MG |
395 | #undef DEFAULT_KEYPAD_TYPE |
396 | #define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD | |
7005b584 WT |
397 | #endif |
398 | ||
7005b584 | 399 | #ifdef CONFIG_PANEL_LCD |
98fac3d3 MG |
400 | #undef DEFAULT_LCD_TYPE |
401 | #define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD | |
7005b584 WT |
402 | #endif |
403 | ||
1e13e8aa MG |
404 | #ifdef CONFIG_PANEL_LCD_HEIGHT |
405 | #undef DEFAULT_LCD_HEIGHT | |
406 | #define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT | |
407 | #endif | |
408 | ||
7005b584 WT |
409 | #ifdef CONFIG_PANEL_LCD_WIDTH |
410 | #undef DEFAULT_LCD_WIDTH | |
411 | #define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH | |
412 | #endif | |
413 | ||
414 | #ifdef CONFIG_PANEL_LCD_BWIDTH | |
415 | #undef DEFAULT_LCD_BWIDTH | |
416 | #define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH | |
417 | #endif | |
418 | ||
419 | #ifdef CONFIG_PANEL_LCD_HWIDTH | |
420 | #undef DEFAULT_LCD_HWIDTH | |
421 | #define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH | |
422 | #endif | |
423 | ||
1e13e8aa MG |
424 | #ifdef CONFIG_PANEL_LCD_CHARSET |
425 | #undef DEFAULT_LCD_CHARSET | |
426 | #define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET | |
7005b584 WT |
427 | #endif |
428 | ||
429 | #ifdef CONFIG_PANEL_LCD_PROTO | |
430 | #undef DEFAULT_LCD_PROTO | |
431 | #define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO | |
432 | #endif | |
433 | ||
434 | #ifdef CONFIG_PANEL_LCD_PIN_E | |
435 | #undef DEFAULT_LCD_PIN_E | |
436 | #define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E | |
437 | #endif | |
438 | ||
439 | #ifdef CONFIG_PANEL_LCD_PIN_RS | |
440 | #undef DEFAULT_LCD_PIN_RS | |
441 | #define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS | |
442 | #endif | |
443 | ||
444 | #ifdef CONFIG_PANEL_LCD_PIN_RW | |
445 | #undef DEFAULT_LCD_PIN_RW | |
446 | #define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW | |
447 | #endif | |
448 | ||
449 | #ifdef CONFIG_PANEL_LCD_PIN_SCL | |
450 | #undef DEFAULT_LCD_PIN_SCL | |
451 | #define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL | |
452 | #endif | |
453 | ||
454 | #ifdef CONFIG_PANEL_LCD_PIN_SDA | |
455 | #undef DEFAULT_LCD_PIN_SDA | |
456 | #define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA | |
457 | #endif | |
458 | ||
459 | #ifdef CONFIG_PANEL_LCD_PIN_BL | |
460 | #undef DEFAULT_LCD_PIN_BL | |
461 | #define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL | |
462 | #endif | |
463 | ||
7005b584 WT |
464 | #endif /* DEFAULT_PROFILE == 0 */ |
465 | ||
466 | /* global variables */ | |
f4757af8 MG |
467 | |
468 | /* Device single-open policy control */ | |
469 | static atomic_t lcd_available = ATOMIC_INIT(1); | |
470 | static atomic_t keypad_available = ATOMIC_INIT(1); | |
471 | ||
698b1515 | 472 | static struct pardevice *pprt; |
7005b584 | 473 | |
f6d1fcfe | 474 | static int keypad_initialized; |
7005b584 | 475 | |
68d386bf MA |
476 | static void (*lcd_write_cmd)(int); |
477 | static void (*lcd_write_data)(int); | |
478 | static void (*lcd_clear_fast)(void); | |
7005b584 | 479 | |
698b1515 | 480 | static DEFINE_SPINLOCK(pprt_lock); |
7005b584 WT |
481 | static struct timer_list scan_timer; |
482 | ||
63023177 | 483 | MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver"); |
f6d1fcfe | 484 | |
59a66a24 | 485 | static int parport = DEFAULT_PARPORT; |
698b1515 WT |
486 | module_param(parport, int, 0000); |
487 | MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)"); | |
f6d1fcfe | 488 | |
98e0e762 MG |
489 | static int profile = DEFAULT_PROFILE; |
490 | module_param(profile, int, 0000); | |
491 | MODULE_PARM_DESC(profile, | |
492 | "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; " | |
493 | "4=16x2 nexcom; default=40x2, old kp"); | |
494 | ||
36277d4a | 495 | static int keypad_type = NOT_SET; |
98e0e762 MG |
496 | module_param(keypad_type, int, 0000); |
497 | MODULE_PARM_DESC(keypad_type, | |
498 | "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys"); | |
499 | ||
36277d4a | 500 | static int lcd_type = NOT_SET; |
98e0e762 MG |
501 | module_param(lcd_type, int, 0000); |
502 | MODULE_PARM_DESC(lcd_type, | |
2c20d92d | 503 | "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom"); |
98e0e762 | 504 | |
36277d4a | 505 | static int lcd_height = NOT_SET; |
698b1515 WT |
506 | module_param(lcd_height, int, 0000); |
507 | MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD"); | |
f6d1fcfe | 508 | |
36277d4a | 509 | static int lcd_width = NOT_SET; |
698b1515 WT |
510 | module_param(lcd_width, int, 0000); |
511 | MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD"); | |
f6d1fcfe | 512 | |
36277d4a | 513 | static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */ |
698b1515 WT |
514 | module_param(lcd_bwidth, int, 0000); |
515 | MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)"); | |
f6d1fcfe | 516 | |
36277d4a | 517 | static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */ |
698b1515 WT |
518 | module_param(lcd_hwidth, int, 0000); |
519 | MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)"); | |
f6d1fcfe | 520 | |
36277d4a | 521 | static int lcd_charset = NOT_SET; |
98e0e762 MG |
522 | module_param(lcd_charset, int, 0000); |
523 | MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074"); | |
f6d1fcfe | 524 | |
36277d4a | 525 | static int lcd_proto = NOT_SET; |
698b1515 | 526 | module_param(lcd_proto, int, 0000); |
429ccf05 | 527 | MODULE_PARM_DESC(lcd_proto, |
fdf4a494 | 528 | "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface"); |
f6d1fcfe | 529 | |
f6d1fcfe WT |
530 | /* |
531 | * These are the parallel port pins the LCD control signals are connected to. | |
532 | * Set this to 0 if the signal is not used. Set it to its opposite value | |
533 | * (negative) if the signal is negated. -MAXINT is used to indicate that the | |
534 | * pin has not been explicitly specified. | |
535 | * | |
63023177 | 536 | * WARNING! no check will be performed about collisions with keypad ! |
f6d1fcfe WT |
537 | */ |
538 | ||
539 | static int lcd_e_pin = PIN_NOT_SET; | |
698b1515 WT |
540 | module_param(lcd_e_pin, int, 0000); |
541 | MODULE_PARM_DESC(lcd_e_pin, | |
fe5d2e01 | 542 | "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)"); |
f6d1fcfe WT |
543 | |
544 | static int lcd_rs_pin = PIN_NOT_SET; | |
698b1515 WT |
545 | module_param(lcd_rs_pin, int, 0000); |
546 | MODULE_PARM_DESC(lcd_rs_pin, | |
fe5d2e01 | 547 | "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)"); |
f6d1fcfe WT |
548 | |
549 | static int lcd_rw_pin = PIN_NOT_SET; | |
698b1515 WT |
550 | module_param(lcd_rw_pin, int, 0000); |
551 | MODULE_PARM_DESC(lcd_rw_pin, | |
fe5d2e01 | 552 | "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)"); |
f6d1fcfe | 553 | |
98e0e762 MG |
554 | static int lcd_cl_pin = PIN_NOT_SET; |
555 | module_param(lcd_cl_pin, int, 0000); | |
556 | MODULE_PARM_DESC(lcd_cl_pin, | |
557 | "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)"); | |
f6d1fcfe WT |
558 | |
559 | static int lcd_da_pin = PIN_NOT_SET; | |
698b1515 WT |
560 | module_param(lcd_da_pin, int, 0000); |
561 | MODULE_PARM_DESC(lcd_da_pin, | |
fe5d2e01 | 562 | "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)"); |
f6d1fcfe | 563 | |
98e0e762 MG |
564 | static int lcd_bl_pin = PIN_NOT_SET; |
565 | module_param(lcd_bl_pin, int, 0000); | |
566 | MODULE_PARM_DESC(lcd_bl_pin, | |
567 | "# of the // port pin connected to LCD backlight, with polarity (-17..17)"); | |
568 | ||
569 | /* Deprecated module parameters - consider not using them anymore */ | |
570 | ||
36277d4a | 571 | static int lcd_enabled = NOT_SET; |
98e0e762 MG |
572 | module_param(lcd_enabled, int, 0000); |
573 | MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead"); | |
574 | ||
36277d4a | 575 | static int keypad_enabled = NOT_SET; |
98e0e762 MG |
576 | module_param(keypad_enabled, int, 0000); |
577 | MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead"); | |
578 | ||
7005b584 | 579 | |
36d2041a | 580 | static const unsigned char *lcd_char_conv; |
7005b584 WT |
581 | |
582 | /* for some LCD drivers (ks0074) we need a charset conversion table. */ | |
36d2041a | 583 | static const unsigned char lcd_char_conv_ks0074[256] = { |
698b1515 WT |
584 | /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */ |
585 | /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
586 | /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | |
587 | /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, | |
588 | /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, | |
589 | /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27, | |
590 | /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, | |
591 | /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, | |
592 | /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, | |
593 | /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, | |
594 | /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, | |
595 | /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, | |
596 | /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4, | |
597 | /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, | |
598 | /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, | |
599 | /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, | |
600 | /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20, | |
601 | /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, | |
602 | /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, | |
603 | /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, | |
604 | /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, | |
605 | /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f, | |
606 | /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96, | |
607 | /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd, | |
608 | /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60, | |
609 | /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9, | |
610 | /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3, | |
611 | /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78, | |
612 | /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe, | |
613 | /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8, | |
614 | /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69, | |
615 | /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25, | |
616 | /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79, | |
7005b584 WT |
617 | }; |
618 | ||
36d2041a | 619 | static const char old_keypad_profile[][4][9] = { |
698b1515 WT |
620 | {"S0", "Left\n", "Left\n", ""}, |
621 | {"S1", "Down\n", "Down\n", ""}, | |
622 | {"S2", "Up\n", "Up\n", ""}, | |
623 | {"S3", "Right\n", "Right\n", ""}, | |
624 | {"S4", "Esc\n", "Esc\n", ""}, | |
625 | {"S5", "Ret\n", "Ret\n", ""}, | |
626 | {"", "", "", ""} | |
7005b584 WT |
627 | }; |
628 | ||
629 | /* signals, press, repeat, release */ | |
36d2041a | 630 | static const char new_keypad_profile[][4][9] = { |
698b1515 WT |
631 | {"S0", "Left\n", "Left\n", ""}, |
632 | {"S1", "Down\n", "Down\n", ""}, | |
633 | {"S2", "Up\n", "Up\n", ""}, | |
634 | {"S3", "Right\n", "Right\n", ""}, | |
635 | {"S4s5", "", "Esc\n", "Esc\n"}, | |
636 | {"s4S5", "", "Ret\n", "Ret\n"}, | |
637 | {"S4S5", "Help\n", "", ""}, | |
638 | /* add new signals above this line */ | |
639 | {"", "", "", ""} | |
7005b584 WT |
640 | }; |
641 | ||
642 | /* signals, press, repeat, release */ | |
36d2041a | 643 | static const char nexcom_keypad_profile[][4][9] = { |
698b1515 WT |
644 | {"a-p-e-", "Down\n", "Down\n", ""}, |
645 | {"a-p-E-", "Ret\n", "Ret\n", ""}, | |
646 | {"a-P-E-", "Esc\n", "Esc\n", ""}, | |
647 | {"a-P-e-", "Up\n", "Up\n", ""}, | |
648 | /* add new signals above this line */ | |
649 | {"", "", "", ""} | |
7005b584 WT |
650 | }; |
651 | ||
36d2041a | 652 | static const char (*keypad_profile)[4][9] = old_keypad_profile; |
7005b584 WT |
653 | |
654 | /* FIXME: this should be converted to a bit array containing signals states */ | |
655 | static struct { | |
429ccf05 HH |
656 | unsigned char e; /* parallel LCD E (data latch on falling edge) */ |
657 | unsigned char rs; /* parallel LCD RS (0 = cmd, 1 = data) */ | |
658 | unsigned char rw; /* parallel LCD R/W (0 = W, 1 = R) */ | |
659 | unsigned char bl; /* parallel LCD backlight (0 = off, 1 = on) */ | |
660 | unsigned char cl; /* serial LCD clock (latch on rising edge) */ | |
661 | unsigned char da; /* serial LCD data */ | |
7005b584 WT |
662 | } bits; |
663 | ||
664 | static void init_scan_timer(void); | |
665 | ||
666 | /* sets data port bits according to current signals values */ | |
698b1515 WT |
667 | static int set_data_bits(void) |
668 | { | |
669 | int val, bit; | |
670 | ||
671 | val = r_dtr(pprt); | |
672 | for (bit = 0; bit < LCD_BITS; bit++) | |
673 | val &= lcd_bits[LCD_PORT_D][bit][BIT_MSK]; | |
674 | ||
675 | val |= lcd_bits[LCD_PORT_D][LCD_BIT_E][bits.e] | |
676 | | lcd_bits[LCD_PORT_D][LCD_BIT_RS][bits.rs] | |
677 | | lcd_bits[LCD_PORT_D][LCD_BIT_RW][bits.rw] | |
678 | | lcd_bits[LCD_PORT_D][LCD_BIT_BL][bits.bl] | |
679 | | lcd_bits[LCD_PORT_D][LCD_BIT_CL][bits.cl] | |
680 | | lcd_bits[LCD_PORT_D][LCD_BIT_DA][bits.da]; | |
681 | ||
682 | w_dtr(pprt, val); | |
683 | return val; | |
7005b584 WT |
684 | } |
685 | ||
686 | /* sets ctrl port bits according to current signals values */ | |
698b1515 WT |
687 | static int set_ctrl_bits(void) |
688 | { | |
689 | int val, bit; | |
690 | ||
691 | val = r_ctr(pprt); | |
692 | for (bit = 0; bit < LCD_BITS; bit++) | |
693 | val &= lcd_bits[LCD_PORT_C][bit][BIT_MSK]; | |
694 | ||
695 | val |= lcd_bits[LCD_PORT_C][LCD_BIT_E][bits.e] | |
696 | | lcd_bits[LCD_PORT_C][LCD_BIT_RS][bits.rs] | |
697 | | lcd_bits[LCD_PORT_C][LCD_BIT_RW][bits.rw] | |
698 | | lcd_bits[LCD_PORT_C][LCD_BIT_BL][bits.bl] | |
699 | | lcd_bits[LCD_PORT_C][LCD_BIT_CL][bits.cl] | |
700 | | lcd_bits[LCD_PORT_C][LCD_BIT_DA][bits.da]; | |
701 | ||
702 | w_ctr(pprt, val); | |
703 | return val; | |
7005b584 WT |
704 | } |
705 | ||
706 | /* sets ctrl & data port bits according to current signals values */ | |
6136ac86 | 707 | static void panel_set_bits(void) |
698b1515 WT |
708 | { |
709 | set_data_bits(); | |
710 | set_ctrl_bits(); | |
7005b584 WT |
711 | } |
712 | ||
713 | /* | |
714 | * Converts a parallel port pin (from -25 to 25) to data and control ports | |
715 | * masks, and data and control port bits. The signal will be considered | |
716 | * unconnected if it's on pin 0 or an invalid pin (<-25 or >25). | |
717 | * | |
718 | * Result will be used this way : | |
719 | * out(dport, in(dport) & d_val[2] | d_val[signal_state]) | |
720 | * out(cport, in(cport) & c_val[2] | c_val[signal_state]) | |
721 | */ | |
36d2041a | 722 | static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val) |
698b1515 WT |
723 | { |
724 | int d_bit, c_bit, inv; | |
725 | ||
2d53426b DB |
726 | d_val[0] = 0; |
727 | c_val[0] = 0; | |
728 | d_val[1] = 0; | |
729 | c_val[1] = 0; | |
730 | d_val[2] = 0xFF; | |
731 | c_val[2] = 0xFF; | |
698b1515 WT |
732 | |
733 | if (pin == 0) | |
734 | return; | |
735 | ||
736 | inv = (pin < 0); | |
737 | if (inv) | |
738 | pin = -pin; | |
739 | ||
2d53426b DB |
740 | d_bit = 0; |
741 | c_bit = 0; | |
698b1515 WT |
742 | |
743 | switch (pin) { | |
744 | case PIN_STROBE: /* strobe, inverted */ | |
745 | c_bit = PNL_PSTROBE; | |
746 | inv = !inv; | |
747 | break; | |
748 | case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */ | |
749 | d_bit = 1 << (pin - 2); | |
750 | break; | |
751 | case PIN_AUTOLF: /* autofeed, inverted */ | |
752 | c_bit = PNL_PAUTOLF; | |
753 | inv = !inv; | |
754 | break; | |
429ccf05 | 755 | case PIN_INITP: /* init, direct */ |
698b1515 WT |
756 | c_bit = PNL_PINITP; |
757 | break; | |
758 | case PIN_SELECP: /* select_in, inverted */ | |
759 | c_bit = PNL_PSELECP; | |
760 | inv = !inv; | |
761 | break; | |
762 | default: /* unknown pin, ignore */ | |
763 | break; | |
764 | } | |
765 | ||
766 | if (c_bit) { | |
767 | c_val[2] &= ~c_bit; | |
768 | c_val[!inv] = c_bit; | |
769 | } else if (d_bit) { | |
770 | d_val[2] &= ~d_bit; | |
771 | d_val[!inv] = d_bit; | |
772 | } | |
7005b584 WT |
773 | } |
774 | ||
775 | /* sleeps that many milliseconds with a reschedule */ | |
698b1515 WT |
776 | static void long_sleep(int ms) |
777 | { | |
895875a3 | 778 | if (in_interrupt()) |
698b1515 | 779 | mdelay(ms); |
895875a3 NMG |
780 | else |
781 | schedule_timeout_interruptible(msecs_to_jiffies(ms)); | |
698b1515 | 782 | } |
7005b584 | 783 | |
881bf281 AW |
784 | /* |
785 | * send a serial byte to the LCD panel. The caller is responsible for locking | |
786 | * if needed. | |
787 | */ | |
698b1515 WT |
788 | static void lcd_send_serial(int byte) |
789 | { | |
790 | int bit; | |
791 | ||
881bf281 AW |
792 | /* |
793 | * the data bit is set on D0, and the clock on STROBE. | |
794 | * LCD reads D0 on STROBE's rising edge. | |
795 | */ | |
698b1515 WT |
796 | for (bit = 0; bit < 8; bit++) { |
797 | bits.cl = BIT_CLR; /* CLK low */ | |
6136ac86 | 798 | panel_set_bits(); |
698b1515 | 799 | bits.da = byte & 1; |
6136ac86 | 800 | panel_set_bits(); |
429ccf05 | 801 | udelay(2); /* maintain the data during 2 us before CLK up */ |
698b1515 | 802 | bits.cl = BIT_SET; /* CLK high */ |
6136ac86 | 803 | panel_set_bits(); |
429ccf05 | 804 | udelay(1); /* maintain the strobe during 1 us */ |
698b1515 WT |
805 | byte >>= 1; |
806 | } | |
7005b584 WT |
807 | } |
808 | ||
809 | /* turn the backlight on or off */ | |
698b1515 WT |
810 | static void lcd_backlight(int on) |
811 | { | |
8037e2a3 | 812 | if (lcd.pins.bl == PIN_NONE) |
698b1515 WT |
813 | return; |
814 | ||
6975e183 | 815 | /* The backlight is activated by setting the AUTOFEED line to +5V */ |
d4d2dbca | 816 | spin_lock_irq(&pprt_lock); |
698b1515 | 817 | bits.bl = on; |
6136ac86 | 818 | panel_set_bits(); |
d4d2dbca | 819 | spin_unlock_irq(&pprt_lock); |
7005b584 WT |
820 | } |
821 | ||
822 | /* send a command to the LCD panel in serial mode */ | |
698b1515 WT |
823 | static void lcd_write_cmd_s(int cmd) |
824 | { | |
d4d2dbca | 825 | spin_lock_irq(&pprt_lock); |
698b1515 WT |
826 | lcd_send_serial(0x1F); /* R/W=W, RS=0 */ |
827 | lcd_send_serial(cmd & 0x0F); | |
828 | lcd_send_serial((cmd >> 4) & 0x0F); | |
829 | udelay(40); /* the shortest command takes at least 40 us */ | |
d4d2dbca | 830 | spin_unlock_irq(&pprt_lock); |
7005b584 WT |
831 | } |
832 | ||
833 | /* send data to the LCD panel in serial mode */ | |
698b1515 WT |
834 | static void lcd_write_data_s(int data) |
835 | { | |
d4d2dbca | 836 | spin_lock_irq(&pprt_lock); |
698b1515 WT |
837 | lcd_send_serial(0x5F); /* R/W=W, RS=1 */ |
838 | lcd_send_serial(data & 0x0F); | |
839 | lcd_send_serial((data >> 4) & 0x0F); | |
840 | udelay(40); /* the shortest data takes at least 40 us */ | |
d4d2dbca | 841 | spin_unlock_irq(&pprt_lock); |
7005b584 WT |
842 | } |
843 | ||
844 | /* send a command to the LCD panel in 8 bits parallel mode */ | |
698b1515 WT |
845 | static void lcd_write_cmd_p8(int cmd) |
846 | { | |
d4d2dbca | 847 | spin_lock_irq(&pprt_lock); |
698b1515 WT |
848 | /* present the data to the data port */ |
849 | w_dtr(pprt, cmd); | |
429ccf05 | 850 | udelay(20); /* maintain the data during 20 us before the strobe */ |
7005b584 | 851 | |
698b1515 WT |
852 | bits.e = BIT_SET; |
853 | bits.rs = BIT_CLR; | |
854 | bits.rw = BIT_CLR; | |
855 | set_ctrl_bits(); | |
7005b584 | 856 | |
429ccf05 | 857 | udelay(40); /* maintain the strobe during 40 us */ |
7005b584 | 858 | |
698b1515 WT |
859 | bits.e = BIT_CLR; |
860 | set_ctrl_bits(); | |
7005b584 | 861 | |
429ccf05 | 862 | udelay(120); /* the shortest command takes at least 120 us */ |
d4d2dbca | 863 | spin_unlock_irq(&pprt_lock); |
7005b584 WT |
864 | } |
865 | ||
866 | /* send data to the LCD panel in 8 bits parallel mode */ | |
698b1515 WT |
867 | static void lcd_write_data_p8(int data) |
868 | { | |
d4d2dbca | 869 | spin_lock_irq(&pprt_lock); |
698b1515 WT |
870 | /* present the data to the data port */ |
871 | w_dtr(pprt, data); | |
429ccf05 | 872 | udelay(20); /* maintain the data during 20 us before the strobe */ |
7005b584 | 873 | |
698b1515 WT |
874 | bits.e = BIT_SET; |
875 | bits.rs = BIT_SET; | |
876 | bits.rw = BIT_CLR; | |
877 | set_ctrl_bits(); | |
7005b584 | 878 | |
429ccf05 | 879 | udelay(40); /* maintain the strobe during 40 us */ |
7005b584 | 880 | |
698b1515 WT |
881 | bits.e = BIT_CLR; |
882 | set_ctrl_bits(); | |
7005b584 | 883 | |
429ccf05 | 884 | udelay(45); /* the shortest data takes at least 45 us */ |
d4d2dbca | 885 | spin_unlock_irq(&pprt_lock); |
7005b584 WT |
886 | } |
887 | ||
77943d31 SR |
888 | /* send a command to the TI LCD panel */ |
889 | static void lcd_write_cmd_tilcd(int cmd) | |
890 | { | |
d4d2dbca | 891 | spin_lock_irq(&pprt_lock); |
77943d31 SR |
892 | /* present the data to the control port */ |
893 | w_ctr(pprt, cmd); | |
894 | udelay(60); | |
d4d2dbca | 895 | spin_unlock_irq(&pprt_lock); |
77943d31 SR |
896 | } |
897 | ||
898 | /* send data to the TI LCD panel */ | |
899 | static void lcd_write_data_tilcd(int data) | |
900 | { | |
d4d2dbca | 901 | spin_lock_irq(&pprt_lock); |
77943d31 SR |
902 | /* present the data to the data port */ |
903 | w_dtr(pprt, data); | |
904 | udelay(60); | |
d4d2dbca | 905 | spin_unlock_irq(&pprt_lock); |
77943d31 SR |
906 | } |
907 | ||
698b1515 WT |
908 | static void lcd_gotoxy(void) |
909 | { | |
2114924a | 910 | lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR |
6d8b588c | 911 | | (lcd.addr.y ? lcd.hwidth : 0) |
8c17893c NB |
912 | /* |
913 | * we force the cursor to stay at the end of the | |
914 | * line if it wants to go farther | |
915 | */ | |
6d8b588c | 916 | | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x & |
8037e2a3 | 917 | (lcd.hwidth - 1) : lcd.bwidth - 1)); |
7005b584 WT |
918 | } |
919 | ||
698b1515 WT |
920 | static void lcd_print(char c) |
921 | { | |
6d8b588c | 922 | if (lcd.addr.x < lcd.bwidth) { |
b565b3fb | 923 | if (lcd_char_conv) |
698b1515 WT |
924 | c = lcd_char_conv[(unsigned char)c]; |
925 | lcd_write_data(c); | |
6d8b588c | 926 | lcd.addr.x++; |
698b1515 WT |
927 | } |
928 | /* prevents the cursor from wrapping onto the next line */ | |
6d8b588c | 929 | if (lcd.addr.x == lcd.bwidth) |
698b1515 | 930 | lcd_gotoxy(); |
7005b584 WT |
931 | } |
932 | ||
933 | /* fills the display with spaces and resets X/Y */ | |
698b1515 WT |
934 | static void lcd_clear_fast_s(void) |
935 | { | |
936 | int pos; | |
c3ed0afc | 937 | |
6d8b588c MG |
938 | lcd.addr.x = 0; |
939 | lcd.addr.y = 0; | |
698b1515 WT |
940 | lcd_gotoxy(); |
941 | ||
d4d2dbca | 942 | spin_lock_irq(&pprt_lock); |
8037e2a3 | 943 | for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) { |
698b1515 WT |
944 | lcd_send_serial(0x5F); /* R/W=W, RS=1 */ |
945 | lcd_send_serial(' ' & 0x0F); | |
946 | lcd_send_serial((' ' >> 4) & 0x0F); | |
947 | udelay(40); /* the shortest data takes at least 40 us */ | |
948 | } | |
d4d2dbca | 949 | spin_unlock_irq(&pprt_lock); |
698b1515 | 950 | |
6d8b588c MG |
951 | lcd.addr.x = 0; |
952 | lcd.addr.y = 0; | |
698b1515 | 953 | lcd_gotoxy(); |
7005b584 WT |
954 | } |
955 | ||
956 | /* fills the display with spaces and resets X/Y */ | |
698b1515 WT |
957 | static void lcd_clear_fast_p8(void) |
958 | { | |
959 | int pos; | |
c3ed0afc | 960 | |
6d8b588c MG |
961 | lcd.addr.x = 0; |
962 | lcd.addr.y = 0; | |
698b1515 | 963 | lcd_gotoxy(); |
7005b584 | 964 | |
d4d2dbca | 965 | spin_lock_irq(&pprt_lock); |
8037e2a3 | 966 | for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) { |
698b1515 WT |
967 | /* present the data to the data port */ |
968 | w_dtr(pprt, ' '); | |
429ccf05 HH |
969 | |
970 | /* maintain the data during 20 us before the strobe */ | |
971 | udelay(20); | |
7005b584 | 972 | |
698b1515 WT |
973 | bits.e = BIT_SET; |
974 | bits.rs = BIT_SET; | |
975 | bits.rw = BIT_CLR; | |
976 | set_ctrl_bits(); | |
7005b584 | 977 | |
429ccf05 HH |
978 | /* maintain the strobe during 40 us */ |
979 | udelay(40); | |
7005b584 | 980 | |
698b1515 WT |
981 | bits.e = BIT_CLR; |
982 | set_ctrl_bits(); | |
7005b584 | 983 | |
429ccf05 HH |
984 | /* the shortest data takes at least 45 us */ |
985 | udelay(45); | |
698b1515 | 986 | } |
d4d2dbca | 987 | spin_unlock_irq(&pprt_lock); |
7005b584 | 988 | |
6d8b588c MG |
989 | lcd.addr.x = 0; |
990 | lcd.addr.y = 0; | |
698b1515 | 991 | lcd_gotoxy(); |
7005b584 WT |
992 | } |
993 | ||
77943d31 SR |
994 | /* fills the display with spaces and resets X/Y */ |
995 | static void lcd_clear_fast_tilcd(void) | |
996 | { | |
997 | int pos; | |
c3ed0afc | 998 | |
6d8b588c MG |
999 | lcd.addr.x = 0; |
1000 | lcd.addr.y = 0; | |
77943d31 SR |
1001 | lcd_gotoxy(); |
1002 | ||
d4d2dbca | 1003 | spin_lock_irq(&pprt_lock); |
8037e2a3 | 1004 | for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) { |
77943d31 SR |
1005 | /* present the data to the data port */ |
1006 | w_dtr(pprt, ' '); | |
1007 | udelay(60); | |
1008 | } | |
1009 | ||
d4d2dbca | 1010 | spin_unlock_irq(&pprt_lock); |
77943d31 | 1011 | |
6d8b588c MG |
1012 | lcd.addr.x = 0; |
1013 | lcd.addr.y = 0; | |
77943d31 SR |
1014 | lcd_gotoxy(); |
1015 | } | |
1016 | ||
7005b584 | 1017 | /* clears the display and resets X/Y */ |
698b1515 WT |
1018 | static void lcd_clear_display(void) |
1019 | { | |
2114924a | 1020 | lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR); |
6d8b588c MG |
1021 | lcd.addr.x = 0; |
1022 | lcd.addr.y = 0; | |
698b1515 WT |
1023 | /* we must wait a few milliseconds (15) */ |
1024 | long_sleep(15); | |
7005b584 WT |
1025 | } |
1026 | ||
698b1515 WT |
1027 | static void lcd_init_display(void) |
1028 | { | |
6d8b588c | 1029 | lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0) |
698b1515 | 1030 | | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B; |
7005b584 | 1031 | |
698b1515 | 1032 | long_sleep(20); /* wait 20 ms after power-up for the paranoid */ |
7005b584 | 1033 | |
2114924a MG |
1034 | /* 8bits, 1 line, small fonts; let's do it 3 times */ |
1035 | lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS); | |
698b1515 | 1036 | long_sleep(10); |
2114924a | 1037 | lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS); |
698b1515 | 1038 | long_sleep(10); |
2114924a | 1039 | lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS); |
698b1515 | 1040 | long_sleep(10); |
7005b584 | 1041 | |
2114924a MG |
1042 | /* set font height and lines number */ |
1043 | lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS | |
1044 | | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) | |
1045 | | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0) | |
698b1515 WT |
1046 | ); |
1047 | long_sleep(10); | |
7005b584 | 1048 | |
2114924a MG |
1049 | /* display off, cursor off, blink off */ |
1050 | lcd_write_cmd(LCD_CMD_DISPLAY_CTRL); | |
698b1515 | 1051 | long_sleep(10); |
7005b584 | 1052 | |
2114924a MG |
1053 | lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */ |
1054 | | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0) | |
1055 | | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0) | |
1056 | | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0) | |
698b1515 | 1057 | ); |
7005b584 | 1058 | |
6d8b588c | 1059 | lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0); |
7005b584 | 1060 | |
698b1515 | 1061 | long_sleep(10); |
7005b584 | 1062 | |
429ccf05 | 1063 | /* entry mode set : increment, cursor shifting */ |
2114924a | 1064 | lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC); |
7005b584 | 1065 | |
698b1515 | 1066 | lcd_clear_display(); |
7005b584 WT |
1067 | } |
1068 | ||
1069 | /* | |
1070 | * These are the file operation function for user access to /dev/lcd | |
1071 | * This function can also be called from inside the kernel, by | |
1072 | * setting file and ppos to NULL. | |
1073 | * | |
1074 | */ | |
1075 | ||
429ccf05 HH |
1076 | static inline int handle_lcd_special_code(void) |
1077 | { | |
1078 | /* LCD special codes */ | |
1079 | ||
1080 | int processed = 0; | |
1081 | ||
6d8b588c MG |
1082 | char *esc = lcd.esc_seq.buf + 2; |
1083 | int oldflags = lcd.flags; | |
429ccf05 HH |
1084 | |
1085 | /* check for display mode flags */ | |
1086 | switch (*esc) { | |
1087 | case 'D': /* Display ON */ | |
6d8b588c | 1088 | lcd.flags |= LCD_FLAG_D; |
429ccf05 HH |
1089 | processed = 1; |
1090 | break; | |
1091 | case 'd': /* Display OFF */ | |
6d8b588c | 1092 | lcd.flags &= ~LCD_FLAG_D; |
429ccf05 HH |
1093 | processed = 1; |
1094 | break; | |
1095 | case 'C': /* Cursor ON */ | |
6d8b588c | 1096 | lcd.flags |= LCD_FLAG_C; |
429ccf05 HH |
1097 | processed = 1; |
1098 | break; | |
1099 | case 'c': /* Cursor OFF */ | |
6d8b588c | 1100 | lcd.flags &= ~LCD_FLAG_C; |
429ccf05 HH |
1101 | processed = 1; |
1102 | break; | |
1103 | case 'B': /* Blink ON */ | |
6d8b588c | 1104 | lcd.flags |= LCD_FLAG_B; |
429ccf05 HH |
1105 | processed = 1; |
1106 | break; | |
1107 | case 'b': /* Blink OFF */ | |
6d8b588c | 1108 | lcd.flags &= ~LCD_FLAG_B; |
429ccf05 HH |
1109 | processed = 1; |
1110 | break; | |
1111 | case '+': /* Back light ON */ | |
6d8b588c | 1112 | lcd.flags |= LCD_FLAG_L; |
429ccf05 HH |
1113 | processed = 1; |
1114 | break; | |
1115 | case '-': /* Back light OFF */ | |
6d8b588c | 1116 | lcd.flags &= ~LCD_FLAG_L; |
429ccf05 HH |
1117 | processed = 1; |
1118 | break; | |
1119 | case '*': | |
1120 | /* flash back light using the keypad timer */ | |
b565b3fb | 1121 | if (scan_timer.function) { |
6d8b588c MG |
1122 | if (lcd.light_tempo == 0 |
1123 | && ((lcd.flags & LCD_FLAG_L) == 0)) | |
429ccf05 | 1124 | lcd_backlight(1); |
6d8b588c | 1125 | lcd.light_tempo = FLASH_LIGHT_TEMPO; |
429ccf05 HH |
1126 | } |
1127 | processed = 1; | |
1128 | break; | |
1129 | case 'f': /* Small Font */ | |
6d8b588c | 1130 | lcd.flags &= ~LCD_FLAG_F; |
429ccf05 HH |
1131 | processed = 1; |
1132 | break; | |
1133 | case 'F': /* Large Font */ | |
6d8b588c | 1134 | lcd.flags |= LCD_FLAG_F; |
429ccf05 HH |
1135 | processed = 1; |
1136 | break; | |
1137 | case 'n': /* One Line */ | |
6d8b588c | 1138 | lcd.flags &= ~LCD_FLAG_N; |
429ccf05 HH |
1139 | processed = 1; |
1140 | break; | |
1141 | case 'N': /* Two Lines */ | |
6d8b588c | 1142 | lcd.flags |= LCD_FLAG_N; |
429ccf05 HH |
1143 | break; |
1144 | case 'l': /* Shift Cursor Left */ | |
6d8b588c | 1145 | if (lcd.addr.x > 0) { |
429ccf05 | 1146 | /* back one char if not at end of line */ |
6d8b588c | 1147 | if (lcd.addr.x < lcd.bwidth) |
2114924a | 1148 | lcd_write_cmd(LCD_CMD_SHIFT); |
6d8b588c | 1149 | lcd.addr.x--; |
429ccf05 HH |
1150 | } |
1151 | processed = 1; | |
1152 | break; | |
1153 | case 'r': /* shift cursor right */ | |
6d8b588c | 1154 | if (lcd.addr.x < lcd.width) { |
429ccf05 | 1155 | /* allow the cursor to pass the end of the line */ |
2114924a MG |
1156 | if (lcd.addr.x < (lcd.bwidth - 1)) |
1157 | lcd_write_cmd(LCD_CMD_SHIFT | | |
1158 | LCD_CMD_SHIFT_RIGHT); | |
6d8b588c | 1159 | lcd.addr.x++; |
429ccf05 HH |
1160 | } |
1161 | processed = 1; | |
1162 | break; | |
1163 | case 'L': /* shift display left */ | |
2114924a | 1164 | lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT); |
429ccf05 HH |
1165 | processed = 1; |
1166 | break; | |
1167 | case 'R': /* shift display right */ | |
2114924a MG |
1168 | lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT | |
1169 | LCD_CMD_SHIFT_RIGHT); | |
429ccf05 HH |
1170 | processed = 1; |
1171 | break; | |
1172 | case 'k': { /* kill end of line */ | |
1173 | int x; | |
c3ed0afc | 1174 | |
6d8b588c | 1175 | for (x = lcd.addr.x; x < lcd.bwidth; x++) |
429ccf05 HH |
1176 | lcd_write_data(' '); |
1177 | ||
1178 | /* restore cursor position */ | |
1179 | lcd_gotoxy(); | |
1180 | processed = 1; | |
1181 | break; | |
1182 | } | |
1183 | case 'I': /* reinitialize display */ | |
1184 | lcd_init_display(); | |
429ccf05 HH |
1185 | processed = 1; |
1186 | break; | |
1187 | case 'G': { | |
1188 | /* Generator : LGcxxxxx...xx; must have <c> between '0' | |
1189 | * and '7', representing the numerical ASCII code of the | |
1190 | * redefined character, and <xx...xx> a sequence of 16 | |
1191 | * hex digits representing 8 bytes for each character. | |
1192 | * Most LCDs will only use 5 lower bits of the 7 first | |
1193 | * bytes. | |
1194 | */ | |
1195 | ||
1196 | unsigned char cgbytes[8]; | |
1197 | unsigned char cgaddr; | |
1198 | int cgoffset; | |
1199 | int shift; | |
1200 | char value; | |
1201 | int addr; | |
1202 | ||
b565b3fb | 1203 | if (!strchr(esc, ';')) |
429ccf05 HH |
1204 | break; |
1205 | ||
1206 | esc++; | |
1207 | ||
1208 | cgaddr = *(esc++) - '0'; | |
1209 | if (cgaddr > 7) { | |
1210 | processed = 1; | |
1211 | break; | |
1212 | } | |
1213 | ||
1214 | cgoffset = 0; | |
1215 | shift = 0; | |
1216 | value = 0; | |
1217 | while (*esc && cgoffset < 8) { | |
1218 | shift ^= 4; | |
3ac76904 | 1219 | if (*esc >= '0' && *esc <= '9') { |
429ccf05 | 1220 | value |= (*esc - '0') << shift; |
3ac76904 | 1221 | } else if (*esc >= 'A' && *esc <= 'Z') { |
429ccf05 | 1222 | value |= (*esc - 'A' + 10) << shift; |
3ac76904 | 1223 | } else if (*esc >= 'a' && *esc <= 'z') { |
429ccf05 | 1224 | value |= (*esc - 'a' + 10) << shift; |
3ac76904 | 1225 | } else { |
429ccf05 HH |
1226 | esc++; |
1227 | continue; | |
1228 | } | |
1229 | ||
1230 | if (shift == 0) { | |
1231 | cgbytes[cgoffset++] = value; | |
1232 | value = 0; | |
1233 | } | |
1234 | ||
1235 | esc++; | |
1236 | } | |
1237 | ||
2114924a | 1238 | lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8)); |
429ccf05 HH |
1239 | for (addr = 0; addr < cgoffset; addr++) |
1240 | lcd_write_data(cgbytes[addr]); | |
1241 | ||
1242 | /* ensures that we stop writing to CGRAM */ | |
1243 | lcd_gotoxy(); | |
1244 | processed = 1; | |
1245 | break; | |
1246 | } | |
1247 | case 'x': /* gotoxy : LxXXX[yYYY]; */ | |
1248 | case 'y': /* gotoxy : LyYYY[xXXX]; */ | |
b565b3fb | 1249 | if (!strchr(esc, ';')) |
429ccf05 HH |
1250 | break; |
1251 | ||
1252 | while (*esc) { | |
1253 | if (*esc == 'x') { | |
1254 | esc++; | |
6d8b588c | 1255 | if (kstrtoul(esc, 10, &lcd.addr.x) < 0) |
12995706 | 1256 | break; |
429ccf05 HH |
1257 | } else if (*esc == 'y') { |
1258 | esc++; | |
6d8b588c | 1259 | if (kstrtoul(esc, 10, &lcd.addr.y) < 0) |
12995706 | 1260 | break; |
3ac76904 | 1261 | } else { |
429ccf05 | 1262 | break; |
3ac76904 | 1263 | } |
429ccf05 HH |
1264 | } |
1265 | ||
1266 | lcd_gotoxy(); | |
1267 | processed = 1; | |
1268 | break; | |
1269 | } | |
1270 | ||
2114924a | 1271 | /* TODO: This indent party here got ugly, clean it! */ |
f2635894 | 1272 | /* Check whether one flag was changed */ |
6d8b588c | 1273 | if (oldflags != lcd.flags) { |
429ccf05 | 1274 | /* check whether one of B,C,D flags were changed */ |
6d8b588c | 1275 | if ((oldflags ^ lcd.flags) & |
429ccf05 HH |
1276 | (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D)) |
1277 | /* set display mode */ | |
2114924a MG |
1278 | lcd_write_cmd(LCD_CMD_DISPLAY_CTRL |
1279 | | ((lcd.flags & LCD_FLAG_D) | |
1280 | ? LCD_CMD_DISPLAY_ON : 0) | |
1281 | | ((lcd.flags & LCD_FLAG_C) | |
1282 | ? LCD_CMD_CURSOR_ON : 0) | |
1283 | | ((lcd.flags & LCD_FLAG_B) | |
1284 | ? LCD_CMD_BLINK_ON : 0)); | |
429ccf05 | 1285 | /* check whether one of F,N flags was changed */ |
6d8b588c | 1286 | else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N)) |
2114924a MG |
1287 | lcd_write_cmd(LCD_CMD_FUNCTION_SET |
1288 | | LCD_CMD_DATA_LEN_8BITS | |
1289 | | ((lcd.flags & LCD_FLAG_F) | |
1290 | ? LCD_CMD_TWO_LINES : 0) | |
1291 | | ((lcd.flags & LCD_FLAG_N) | |
1292 | ? LCD_CMD_FONT_5X10_DOTS | |
1293 | : 0)); | |
f2635894 | 1294 | /* check whether L flag was changed */ |
6d8b588c MG |
1295 | else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) { |
1296 | if (lcd.flags & (LCD_FLAG_L)) | |
429ccf05 | 1297 | lcd_backlight(1); |
6d8b588c | 1298 | else if (lcd.light_tempo == 0) |
8c17893c NB |
1299 | /* |
1300 | * switch off the light only when the tempo | |
1301 | * lighting is gone | |
1302 | */ | |
429ccf05 HH |
1303 | lcd_backlight(0); |
1304 | } | |
1305 | } | |
1306 | ||
1307 | return processed; | |
1308 | } | |
1309 | ||
70a8c3eb BA |
1310 | static void lcd_write_char(char c) |
1311 | { | |
1312 | /* first, we'll test if we're in escape mode */ | |
6d8b588c | 1313 | if ((c != '\n') && lcd.esc_seq.len >= 0) { |
70a8c3eb | 1314 | /* yes, let's add this char to the buffer */ |
6d8b588c MG |
1315 | lcd.esc_seq.buf[lcd.esc_seq.len++] = c; |
1316 | lcd.esc_seq.buf[lcd.esc_seq.len] = 0; | |
70a8c3eb BA |
1317 | } else { |
1318 | /* aborts any previous escape sequence */ | |
6d8b588c | 1319 | lcd.esc_seq.len = -1; |
70a8c3eb BA |
1320 | |
1321 | switch (c) { | |
1322 | case LCD_ESCAPE_CHAR: | |
1323 | /* start of an escape sequence */ | |
6d8b588c MG |
1324 | lcd.esc_seq.len = 0; |
1325 | lcd.esc_seq.buf[lcd.esc_seq.len] = 0; | |
70a8c3eb BA |
1326 | break; |
1327 | case '\b': | |
1328 | /* go back one char and clear it */ | |
6d8b588c | 1329 | if (lcd.addr.x > 0) { |
8c17893c NB |
1330 | /* |
1331 | * check if we're not at the | |
1332 | * end of the line | |
1333 | */ | |
6d8b588c | 1334 | if (lcd.addr.x < lcd.bwidth) |
70a8c3eb | 1335 | /* back one char */ |
2114924a | 1336 | lcd_write_cmd(LCD_CMD_SHIFT); |
6d8b588c | 1337 | lcd.addr.x--; |
70a8c3eb BA |
1338 | } |
1339 | /* replace with a space */ | |
1340 | lcd_write_data(' '); | |
1341 | /* back one char again */ | |
2114924a | 1342 | lcd_write_cmd(LCD_CMD_SHIFT); |
70a8c3eb BA |
1343 | break; |
1344 | case '\014': | |
1345 | /* quickly clear the display */ | |
1346 | lcd_clear_fast(); | |
1347 | break; | |
1348 | case '\n': | |
8c17893c NB |
1349 | /* |
1350 | * flush the remainder of the current line and | |
1351 | * go to the beginning of the next line | |
1352 | */ | |
6d8b588c | 1353 | for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++) |
70a8c3eb | 1354 | lcd_write_data(' '); |
6d8b588c MG |
1355 | lcd.addr.x = 0; |
1356 | lcd.addr.y = (lcd.addr.y + 1) % lcd.height; | |
70a8c3eb BA |
1357 | lcd_gotoxy(); |
1358 | break; | |
1359 | case '\r': | |
1360 | /* go to the beginning of the same line */ | |
6d8b588c | 1361 | lcd.addr.x = 0; |
70a8c3eb BA |
1362 | lcd_gotoxy(); |
1363 | break; | |
1364 | case '\t': | |
1365 | /* print a space instead of the tab */ | |
1366 | lcd_print(' '); | |
1367 | break; | |
1368 | default: | |
1369 | /* simply print this char */ | |
1370 | lcd_print(c); | |
1371 | break; | |
1372 | } | |
1373 | } | |
1374 | ||
8c17893c NB |
1375 | /* |
1376 | * now we'll see if we're in an escape mode and if the current | |
1377 | * escape sequence can be understood. | |
1378 | */ | |
6d8b588c | 1379 | if (lcd.esc_seq.len >= 2) { |
70a8c3eb BA |
1380 | int processed = 0; |
1381 | ||
6d8b588c | 1382 | if (!strcmp(lcd.esc_seq.buf, "[2J")) { |
70a8c3eb BA |
1383 | /* clear the display */ |
1384 | lcd_clear_fast(); | |
1385 | processed = 1; | |
6d8b588c | 1386 | } else if (!strcmp(lcd.esc_seq.buf, "[H")) { |
70a8c3eb | 1387 | /* cursor to home */ |
6d8b588c MG |
1388 | lcd.addr.x = 0; |
1389 | lcd.addr.y = 0; | |
70a8c3eb BA |
1390 | lcd_gotoxy(); |
1391 | processed = 1; | |
1392 | } | |
1393 | /* codes starting with ^[[L */ | |
6d8b588c MG |
1394 | else if ((lcd.esc_seq.len >= 3) && |
1395 | (lcd.esc_seq.buf[0] == '[') && | |
1396 | (lcd.esc_seq.buf[1] == 'L')) { | |
70a8c3eb BA |
1397 | processed = handle_lcd_special_code(); |
1398 | } | |
1399 | ||
1400 | /* LCD special escape codes */ | |
8c17893c NB |
1401 | /* |
1402 | * flush the escape sequence if it's been processed | |
1403 | * or if it is getting too long. | |
1404 | */ | |
6d8b588c MG |
1405 | if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN)) |
1406 | lcd.esc_seq.len = -1; | |
70a8c3eb BA |
1407 | } /* escape codes */ |
1408 | } | |
1409 | ||
698b1515 | 1410 | static ssize_t lcd_write(struct file *file, |
fdf4a494 | 1411 | const char __user *buf, size_t count, loff_t *ppos) |
698b1515 | 1412 | { |
70a8c3eb | 1413 | const char __user *tmp = buf; |
698b1515 WT |
1414 | char c; |
1415 | ||
70a8c3eb | 1416 | for (; count-- > 0; (*ppos)++, tmp++) { |
698b1515 | 1417 | if (!in_interrupt() && (((count + 1) & 0x1f) == 0)) |
8c17893c NB |
1418 | /* |
1419 | * let's be a little nice with other processes | |
1420 | * that need some CPU | |
1421 | */ | |
429ccf05 | 1422 | schedule(); |
698b1515 | 1423 | |
6a4193a2 | 1424 | if (get_user(c, tmp)) |
698b1515 WT |
1425 | return -EFAULT; |
1426 | ||
70a8c3eb | 1427 | lcd_write_char(c); |
698b1515 | 1428 | } |
7005b584 | 1429 | |
698b1515 | 1430 | return tmp - buf; |
7005b584 WT |
1431 | } |
1432 | ||
698b1515 WT |
1433 | static int lcd_open(struct inode *inode, struct file *file) |
1434 | { | |
f4757af8 | 1435 | if (!atomic_dec_and_test(&lcd_available)) |
698b1515 | 1436 | return -EBUSY; /* open only once at a time */ |
7005b584 | 1437 | |
698b1515 WT |
1438 | if (file->f_mode & FMODE_READ) /* device is write-only */ |
1439 | return -EPERM; | |
7005b584 | 1440 | |
6d8b588c | 1441 | if (lcd.must_clear) { |
698b1515 | 1442 | lcd_clear_display(); |
6d8b588c | 1443 | lcd.must_clear = false; |
698b1515 | 1444 | } |
3ff81013 | 1445 | return nonseekable_open(inode, file); |
7005b584 WT |
1446 | } |
1447 | ||
698b1515 WT |
1448 | static int lcd_release(struct inode *inode, struct file *file) |
1449 | { | |
f4757af8 | 1450 | atomic_inc(&lcd_available); |
698b1515 | 1451 | return 0; |
7005b584 WT |
1452 | } |
1453 | ||
429ccf05 | 1454 | static const struct file_operations lcd_fops = { |
698b1515 WT |
1455 | .write = lcd_write, |
1456 | .open = lcd_open, | |
1457 | .release = lcd_release, | |
3ff81013 | 1458 | .llseek = no_llseek, |
7005b584 WT |
1459 | }; |
1460 | ||
1461 | static struct miscdevice lcd_dev = { | |
6c3773de MG |
1462 | .minor = LCD_MINOR, |
1463 | .name = "lcd", | |
1464 | .fops = &lcd_fops, | |
7005b584 WT |
1465 | }; |
1466 | ||
7005b584 | 1467 | /* public function usable from the kernel for any purpose */ |
36d2041a | 1468 | static void panel_lcd_print(const char *s) |
698b1515 | 1469 | { |
70a8c3eb BA |
1470 | const char *tmp = s; |
1471 | int count = strlen(s); | |
1472 | ||
6d8b588c | 1473 | if (lcd.enabled && lcd.initialized) { |
70a8c3eb BA |
1474 | for (; count-- > 0; tmp++) { |
1475 | if (!in_interrupt() && (((count + 1) & 0x1f) == 0)) | |
8c17893c NB |
1476 | /* |
1477 | * let's be a little nice with other processes | |
1478 | * that need some CPU | |
1479 | */ | |
70a8c3eb BA |
1480 | schedule(); |
1481 | ||
1482 | lcd_write_char(*tmp); | |
1483 | } | |
1484 | } | |
7005b584 WT |
1485 | } |
1486 | ||
7005b584 | 1487 | /* initialize the LCD driver */ |
36d2041a | 1488 | static void lcd_init(void) |
698b1515 | 1489 | { |
87b8e0c8 | 1490 | switch (selected_lcd_type) { |
429ccf05 HH |
1491 | case LCD_TYPE_OLD: |
1492 | /* parallel mode, 8 bits */ | |
8037e2a3 MG |
1493 | lcd.proto = LCD_PROTO_PARALLEL; |
1494 | lcd.charset = LCD_CHARSET_NORMAL; | |
1495 | lcd.pins.e = PIN_STROBE; | |
1496 | lcd.pins.rs = PIN_AUTOLF; | |
1497 | ||
1498 | lcd.width = 40; | |
1499 | lcd.bwidth = 40; | |
1500 | lcd.hwidth = 64; | |
1501 | lcd.height = 2; | |
7005b584 | 1502 | break; |
429ccf05 HH |
1503 | case LCD_TYPE_KS0074: |
1504 | /* serial mode, ks0074 */ | |
8037e2a3 MG |
1505 | lcd.proto = LCD_PROTO_SERIAL; |
1506 | lcd.charset = LCD_CHARSET_KS0074; | |
1507 | lcd.pins.bl = PIN_AUTOLF; | |
1508 | lcd.pins.cl = PIN_STROBE; | |
1509 | lcd.pins.da = PIN_D0; | |
1510 | ||
1511 | lcd.width = 16; | |
1512 | lcd.bwidth = 40; | |
1513 | lcd.hwidth = 16; | |
1514 | lcd.height = 2; | |
7005b584 | 1515 | break; |
429ccf05 HH |
1516 | case LCD_TYPE_NEXCOM: |
1517 | /* parallel mode, 8 bits, generic */ | |
8037e2a3 MG |
1518 | lcd.proto = LCD_PROTO_PARALLEL; |
1519 | lcd.charset = LCD_CHARSET_NORMAL; | |
1520 | lcd.pins.e = PIN_AUTOLF; | |
1521 | lcd.pins.rs = PIN_SELECP; | |
1522 | lcd.pins.rw = PIN_INITP; | |
1523 | ||
1524 | lcd.width = 16; | |
1525 | lcd.bwidth = 40; | |
1526 | lcd.hwidth = 64; | |
1527 | lcd.height = 2; | |
7005b584 | 1528 | break; |
429ccf05 HH |
1529 | case LCD_TYPE_CUSTOM: |
1530 | /* customer-defined */ | |
8037e2a3 MG |
1531 | lcd.proto = DEFAULT_LCD_PROTO; |
1532 | lcd.charset = DEFAULT_LCD_CHARSET; | |
7005b584 WT |
1533 | /* default geometry will be set later */ |
1534 | break; | |
429ccf05 HH |
1535 | case LCD_TYPE_HANTRONIX: |
1536 | /* parallel mode, 8 bits, hantronix-like */ | |
698b1515 | 1537 | default: |
8037e2a3 MG |
1538 | lcd.proto = LCD_PROTO_PARALLEL; |
1539 | lcd.charset = LCD_CHARSET_NORMAL; | |
1540 | lcd.pins.e = PIN_STROBE; | |
1541 | lcd.pins.rs = PIN_SELECP; | |
1542 | ||
1543 | lcd.width = 16; | |
1544 | lcd.bwidth = 40; | |
1545 | lcd.hwidth = 64; | |
1546 | lcd.height = 2; | |
7005b584 | 1547 | break; |
698b1515 | 1548 | } |
7005b584 | 1549 | |
8037e2a3 | 1550 | /* Overwrite with module params set on loading */ |
1a4b2e3e | 1551 | if (lcd_height != NOT_SET) |
8037e2a3 | 1552 | lcd.height = lcd_height; |
1a4b2e3e | 1553 | if (lcd_width != NOT_SET) |
8037e2a3 | 1554 | lcd.width = lcd_width; |
1a4b2e3e | 1555 | if (lcd_bwidth != NOT_SET) |
8037e2a3 | 1556 | lcd.bwidth = lcd_bwidth; |
1a4b2e3e | 1557 | if (lcd_hwidth != NOT_SET) |
8037e2a3 | 1558 | lcd.hwidth = lcd_hwidth; |
1a4b2e3e | 1559 | if (lcd_charset != NOT_SET) |
8037e2a3 | 1560 | lcd.charset = lcd_charset; |
1a4b2e3e | 1561 | if (lcd_proto != NOT_SET) |
8037e2a3 MG |
1562 | lcd.proto = lcd_proto; |
1563 | if (lcd_e_pin != PIN_NOT_SET) | |
1564 | lcd.pins.e = lcd_e_pin; | |
1565 | if (lcd_rs_pin != PIN_NOT_SET) | |
1566 | lcd.pins.rs = lcd_rs_pin; | |
1567 | if (lcd_rw_pin != PIN_NOT_SET) | |
1568 | lcd.pins.rw = lcd_rw_pin; | |
1569 | if (lcd_cl_pin != PIN_NOT_SET) | |
1570 | lcd.pins.cl = lcd_cl_pin; | |
1571 | if (lcd_da_pin != PIN_NOT_SET) | |
1572 | lcd.pins.da = lcd_da_pin; | |
1573 | if (lcd_bl_pin != PIN_NOT_SET) | |
1574 | lcd.pins.bl = lcd_bl_pin; | |
1575 | ||
698b1515 | 1576 | /* this is used to catch wrong and default values */ |
8037e2a3 MG |
1577 | if (lcd.width <= 0) |
1578 | lcd.width = DEFAULT_LCD_WIDTH; | |
1579 | if (lcd.bwidth <= 0) | |
1580 | lcd.bwidth = DEFAULT_LCD_BWIDTH; | |
1581 | if (lcd.hwidth <= 0) | |
1582 | lcd.hwidth = DEFAULT_LCD_HWIDTH; | |
1583 | if (lcd.height <= 0) | |
1584 | lcd.height = DEFAULT_LCD_HEIGHT; | |
1585 | ||
1586 | if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */ | |
698b1515 WT |
1587 | lcd_write_cmd = lcd_write_cmd_s; |
1588 | lcd_write_data = lcd_write_data_s; | |
1589 | lcd_clear_fast = lcd_clear_fast_s; | |
1590 | ||
8037e2a3 MG |
1591 | if (lcd.pins.cl == PIN_NOT_SET) |
1592 | lcd.pins.cl = DEFAULT_LCD_PIN_SCL; | |
1593 | if (lcd.pins.da == PIN_NOT_SET) | |
1594 | lcd.pins.da = DEFAULT_LCD_PIN_SDA; | |
698b1515 | 1595 | |
8037e2a3 | 1596 | } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */ |
698b1515 WT |
1597 | lcd_write_cmd = lcd_write_cmd_p8; |
1598 | lcd_write_data = lcd_write_data_p8; | |
1599 | lcd_clear_fast = lcd_clear_fast_p8; | |
1600 | ||
8037e2a3 MG |
1601 | if (lcd.pins.e == PIN_NOT_SET) |
1602 | lcd.pins.e = DEFAULT_LCD_PIN_E; | |
1603 | if (lcd.pins.rs == PIN_NOT_SET) | |
1604 | lcd.pins.rs = DEFAULT_LCD_PIN_RS; | |
1605 | if (lcd.pins.rw == PIN_NOT_SET) | |
1606 | lcd.pins.rw = DEFAULT_LCD_PIN_RW; | |
77943d31 SR |
1607 | } else { |
1608 | lcd_write_cmd = lcd_write_cmd_tilcd; | |
1609 | lcd_write_data = lcd_write_data_tilcd; | |
1610 | lcd_clear_fast = lcd_clear_fast_tilcd; | |
698b1515 | 1611 | } |
7005b584 | 1612 | |
8037e2a3 MG |
1613 | if (lcd.pins.bl == PIN_NOT_SET) |
1614 | lcd.pins.bl = DEFAULT_LCD_PIN_BL; | |
1615 | ||
1616 | if (lcd.pins.e == PIN_NOT_SET) | |
1617 | lcd.pins.e = PIN_NONE; | |
1618 | if (lcd.pins.rs == PIN_NOT_SET) | |
1619 | lcd.pins.rs = PIN_NONE; | |
1620 | if (lcd.pins.rw == PIN_NOT_SET) | |
1621 | lcd.pins.rw = PIN_NONE; | |
1622 | if (lcd.pins.bl == PIN_NOT_SET) | |
1623 | lcd.pins.bl = PIN_NONE; | |
1624 | if (lcd.pins.cl == PIN_NOT_SET) | |
1625 | lcd.pins.cl = PIN_NONE; | |
1626 | if (lcd.pins.da == PIN_NOT_SET) | |
1627 | lcd.pins.da = PIN_NONE; | |
1628 | ||
1629 | if (lcd.charset == NOT_SET) | |
1630 | lcd.charset = DEFAULT_LCD_CHARSET; | |
1631 | ||
1632 | if (lcd.charset == LCD_CHARSET_KS0074) | |
698b1515 WT |
1633 | lcd_char_conv = lcd_char_conv_ks0074; |
1634 | else | |
1635 | lcd_char_conv = NULL; | |
1636 | ||
8037e2a3 | 1637 | if (lcd.pins.bl != PIN_NONE) |
698b1515 WT |
1638 | init_scan_timer(); |
1639 | ||
8037e2a3 | 1640 | pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E], |
698b1515 | 1641 | lcd_bits[LCD_PORT_C][LCD_BIT_E]); |
8037e2a3 | 1642 | pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS], |
698b1515 | 1643 | lcd_bits[LCD_PORT_C][LCD_BIT_RS]); |
8037e2a3 | 1644 | pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW], |
698b1515 | 1645 | lcd_bits[LCD_PORT_C][LCD_BIT_RW]); |
8037e2a3 | 1646 | pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL], |
698b1515 | 1647 | lcd_bits[LCD_PORT_C][LCD_BIT_BL]); |
8037e2a3 | 1648 | pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL], |
698b1515 | 1649 | lcd_bits[LCD_PORT_C][LCD_BIT_CL]); |
8037e2a3 | 1650 | pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA], |
698b1515 WT |
1651 | lcd_bits[LCD_PORT_C][LCD_BIT_DA]); |
1652 | ||
8c17893c NB |
1653 | /* |
1654 | * before this line, we must NOT send anything to the display. | |
698b1515 | 1655 | * Since lcd_init_display() needs to write data, we have to |
8c17893c NB |
1656 | * enable mark the LCD initialized just before. |
1657 | */ | |
6d8b588c | 1658 | lcd.initialized = true; |
698b1515 | 1659 | lcd_init_display(); |
7005b584 | 1660 | |
698b1515 | 1661 | /* display a short message */ |
7005b584 WT |
1662 | #ifdef CONFIG_PANEL_CHANGE_MESSAGE |
1663 | #ifdef CONFIG_PANEL_BOOT_MESSAGE | |
698b1515 | 1664 | panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE); |
7005b584 WT |
1665 | #endif |
1666 | #else | |
698b1515 WT |
1667 | panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE "\nPanel-" |
1668 | PANEL_VERSION); | |
7005b584 | 1669 | #endif |
6d8b588c MG |
1670 | lcd.addr.x = 0; |
1671 | lcd.addr.y = 0; | |
429ccf05 | 1672 | /* clear the display on the next device opening */ |
6d8b588c | 1673 | lcd.must_clear = true; |
698b1515 | 1674 | lcd_gotoxy(); |
7005b584 WT |
1675 | } |
1676 | ||
7005b584 WT |
1677 | /* |
1678 | * These are the file operation function for user access to /dev/keypad | |
1679 | */ | |
1680 | ||
698b1515 | 1681 | static ssize_t keypad_read(struct file *file, |
cce75f41 | 1682 | char __user *buf, size_t count, loff_t *ppos) |
698b1515 | 1683 | { |
698b1515 | 1684 | unsigned i = *ppos; |
cce75f41 | 1685 | char __user *tmp = buf; |
7005b584 | 1686 | |
698b1515 WT |
1687 | if (keypad_buflen == 0) { |
1688 | if (file->f_flags & O_NONBLOCK) | |
1689 | return -EAGAIN; | |
7005b584 | 1690 | |
310df69c AB |
1691 | if (wait_event_interruptible(keypad_read_wait, |
1692 | keypad_buflen != 0)) | |
698b1515 WT |
1693 | return -EINTR; |
1694 | } | |
7005b584 | 1695 | |
429ccf05 HH |
1696 | for (; count-- > 0 && (keypad_buflen > 0); |
1697 | ++i, ++tmp, --keypad_buflen) { | |
698b1515 WT |
1698 | put_user(keypad_buffer[keypad_start], tmp); |
1699 | keypad_start = (keypad_start + 1) % KEYPAD_BUFFER; | |
1700 | } | |
1701 | *ppos = i; | |
7005b584 | 1702 | |
698b1515 | 1703 | return tmp - buf; |
7005b584 WT |
1704 | } |
1705 | ||
698b1515 WT |
1706 | static int keypad_open(struct inode *inode, struct file *file) |
1707 | { | |
f4757af8 | 1708 | if (!atomic_dec_and_test(&keypad_available)) |
698b1515 | 1709 | return -EBUSY; /* open only once at a time */ |
7005b584 | 1710 | |
698b1515 WT |
1711 | if (file->f_mode & FMODE_WRITE) /* device is read-only */ |
1712 | return -EPERM; | |
7005b584 | 1713 | |
698b1515 | 1714 | keypad_buflen = 0; /* flush the buffer on opening */ |
698b1515 | 1715 | return 0; |
7005b584 WT |
1716 | } |
1717 | ||
698b1515 WT |
1718 | static int keypad_release(struct inode *inode, struct file *file) |
1719 | { | |
f4757af8 | 1720 | atomic_inc(&keypad_available); |
698b1515 | 1721 | return 0; |
7005b584 WT |
1722 | } |
1723 | ||
429ccf05 | 1724 | static const struct file_operations keypad_fops = { |
698b1515 WT |
1725 | .read = keypad_read, /* read */ |
1726 | .open = keypad_open, /* open */ | |
1727 | .release = keypad_release, /* close */ | |
6038f373 | 1728 | .llseek = default_llseek, |
7005b584 WT |
1729 | }; |
1730 | ||
1731 | static struct miscdevice keypad_dev = { | |
6c3773de MG |
1732 | .minor = KEYPAD_MINOR, |
1733 | .name = "keypad", | |
1734 | .fops = &keypad_fops, | |
7005b584 WT |
1735 | }; |
1736 | ||
36d2041a | 1737 | static void keypad_send_key(const char *string, int max_len) |
698b1515 | 1738 | { |
698b1515 | 1739 | /* send the key to the device only if a process is attached to it. */ |
f4757af8 | 1740 | if (!atomic_read(&keypad_available)) { |
698b1515 WT |
1741 | while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) { |
1742 | keypad_buffer[(keypad_start + keypad_buflen++) % | |
1743 | KEYPAD_BUFFER] = *string++; | |
1744 | } | |
1745 | wake_up_interruptible(&keypad_read_wait); | |
7005b584 | 1746 | } |
7005b584 WT |
1747 | } |
1748 | ||
429ccf05 HH |
1749 | /* this function scans all the bits involving at least one logical signal, |
1750 | * and puts the results in the bitfield "phys_read" (one bit per established | |
1751 | * contact), and sets "phys_read_prev" to "phys_read". | |
7005b584 | 1752 | * |
429ccf05 HH |
1753 | * Note: to debounce input signals, we will only consider as switched a signal |
1754 | * which is stable across 2 measures. Signals which are different between two | |
1755 | * reads will be kept as they previously were in their logical form (phys_prev). | |
1756 | * A signal which has just switched will have a 1 in | |
1757 | * (phys_read ^ phys_read_prev). | |
7005b584 | 1758 | */ |
698b1515 WT |
1759 | static void phys_scan_contacts(void) |
1760 | { | |
1761 | int bit, bitval; | |
1762 | char oldval; | |
1763 | char bitmask; | |
1764 | char gndmask; | |
1765 | ||
1766 | phys_prev = phys_curr; | |
1767 | phys_read_prev = phys_read; | |
1768 | phys_read = 0; /* flush all signals */ | |
1769 | ||
429ccf05 HH |
1770 | /* keep track of old value, with all outputs disabled */ |
1771 | oldval = r_dtr(pprt) | scan_mask_o; | |
1772 | /* activate all keyboard outputs (active low) */ | |
1773 | w_dtr(pprt, oldval & ~scan_mask_o); | |
1774 | ||
1775 | /* will have a 1 for each bit set to gnd */ | |
1776 | bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; | |
1777 | /* disable all matrix signals */ | |
1778 | w_dtr(pprt, oldval); | |
698b1515 WT |
1779 | |
1780 | /* now that all outputs are cleared, the only active input bits are | |
1781 | * directly connected to the ground | |
7005b584 | 1782 | */ |
698b1515 | 1783 | |
429ccf05 HH |
1784 | /* 1 for each grounded input */ |
1785 | gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i; | |
1786 | ||
1787 | /* grounded inputs are signals 40-44 */ | |
1788 | phys_read |= (pmask_t) gndmask << 40; | |
7005b584 | 1789 | |
698b1515 | 1790 | if (bitmask != gndmask) { |
8c17893c NB |
1791 | /* |
1792 | * since clearing the outputs changed some inputs, we know | |
429ccf05 HH |
1793 | * that some input signals are currently tied to some outputs. |
1794 | * So we'll scan them. | |
698b1515 WT |
1795 | */ |
1796 | for (bit = 0; bit < 8; bit++) { | |
79f2af62 | 1797 | bitval = BIT(bit); |
7005b584 | 1798 | |
698b1515 WT |
1799 | if (!(scan_mask_o & bitval)) /* skip unused bits */ |
1800 | continue; | |
1801 | ||
1802 | w_dtr(pprt, oldval & ~bitval); /* enable this output */ | |
1803 | bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask; | |
1804 | phys_read |= (pmask_t) bitmask << (5 * bit); | |
1805 | } | |
1806 | w_dtr(pprt, oldval); /* disable all outputs */ | |
7005b584 | 1807 | } |
8c17893c NB |
1808 | /* |
1809 | * this is easy: use old bits when they are flapping, | |
1810 | * use new ones when stable | |
1811 | */ | |
429ccf05 HH |
1812 | phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) | |
1813 | (phys_read & ~(phys_read ^ phys_read_prev)); | |
1814 | } | |
1815 | ||
1816 | static inline int input_state_high(struct logical_input *input) | |
1817 | { | |
1818 | #if 0 | |
1819 | /* FIXME: | |
1820 | * this is an invalid test. It tries to catch | |
1821 | * transitions from single-key to multiple-key, but | |
1822 | * doesn't take into account the contacts polarity. | |
1823 | * The only solution to the problem is to parse keys | |
1824 | * from the most complex to the simplest combinations, | |
1825 | * and mark them as 'caught' once a combination | |
1826 | * matches, then unmatch it for all other ones. | |
1827 | */ | |
1828 | ||
1829 | /* try to catch dangerous transitions cases : | |
1830 | * someone adds a bit, so this signal was a false | |
1831 | * positive resulting from a transition. We should | |
1832 | * invalidate the signal immediately and not call the | |
1833 | * release function. | |
1834 | * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release. | |
1835 | */ | |
fdf4a494 DB |
1836 | if (((phys_prev & input->mask) == input->value) && |
1837 | ((phys_curr & input->mask) > input->value)) { | |
429ccf05 HH |
1838 | input->state = INPUT_ST_LOW; /* invalidate */ |
1839 | return 1; | |
1840 | } | |
1841 | #endif | |
1842 | ||
1843 | if ((phys_curr & input->mask) == input->value) { | |
1844 | if ((input->type == INPUT_TYPE_STD) && | |
1845 | (input->high_timer == 0)) { | |
1846 | input->high_timer++; | |
b565b3fb | 1847 | if (input->u.std.press_fct) |
429ccf05 HH |
1848 | input->u.std.press_fct(input->u.std.press_data); |
1849 | } else if (input->type == INPUT_TYPE_KBD) { | |
1850 | /* will turn on the light */ | |
1851 | keypressed = 1; | |
1852 | ||
1853 | if (input->high_timer == 0) { | |
1854 | char *press_str = input->u.kbd.press_str; | |
c3ed0afc | 1855 | |
e6626de5 JC |
1856 | if (press_str[0]) { |
1857 | int s = sizeof(input->u.kbd.press_str); | |
c3ed0afc | 1858 | |
e6626de5 JC |
1859 | keypad_send_key(press_str, s); |
1860 | } | |
429ccf05 HH |
1861 | } |
1862 | ||
1863 | if (input->u.kbd.repeat_str[0]) { | |
1864 | char *repeat_str = input->u.kbd.repeat_str; | |
c3ed0afc | 1865 | |
429ccf05 | 1866 | if (input->high_timer >= KEYPAD_REP_START) { |
e6626de5 | 1867 | int s = sizeof(input->u.kbd.repeat_str); |
c3ed0afc | 1868 | |
429ccf05 | 1869 | input->high_timer -= KEYPAD_REP_DELAY; |
e6626de5 | 1870 | keypad_send_key(repeat_str, s); |
429ccf05 HH |
1871 | } |
1872 | /* we will need to come back here soon */ | |
1873 | inputs_stable = 0; | |
1874 | } | |
1875 | ||
1876 | if (input->high_timer < 255) | |
1877 | input->high_timer++; | |
1878 | } | |
1879 | return 1; | |
429ccf05 | 1880 | } |
083b3638 VH |
1881 | |
1882 | /* else signal falling down. Let's fall through. */ | |
1883 | input->state = INPUT_ST_FALLING; | |
1884 | input->fall_timer = 0; | |
1885 | ||
429ccf05 HH |
1886 | return 0; |
1887 | } | |
1888 | ||
1889 | static inline void input_state_falling(struct logical_input *input) | |
1890 | { | |
1891 | #if 0 | |
1892 | /* FIXME !!! same comment as in input_state_high */ | |
fdf4a494 DB |
1893 | if (((phys_prev & input->mask) == input->value) && |
1894 | ((phys_curr & input->mask) > input->value)) { | |
429ccf05 HH |
1895 | input->state = INPUT_ST_LOW; /* invalidate */ |
1896 | return; | |
1897 | } | |
1898 | #endif | |
1899 | ||
1900 | if ((phys_curr & input->mask) == input->value) { | |
1901 | if (input->type == INPUT_TYPE_KBD) { | |
1902 | /* will turn on the light */ | |
1903 | keypressed = 1; | |
1904 | ||
1905 | if (input->u.kbd.repeat_str[0]) { | |
1906 | char *repeat_str = input->u.kbd.repeat_str; | |
c3ed0afc | 1907 | |
e6626de5 JC |
1908 | if (input->high_timer >= KEYPAD_REP_START) { |
1909 | int s = sizeof(input->u.kbd.repeat_str); | |
c3ed0afc | 1910 | |
429ccf05 | 1911 | input->high_timer -= KEYPAD_REP_DELAY; |
e6626de5 JC |
1912 | keypad_send_key(repeat_str, s); |
1913 | } | |
429ccf05 HH |
1914 | /* we will need to come back here soon */ |
1915 | inputs_stable = 0; | |
1916 | } | |
1917 | ||
1918 | if (input->high_timer < 255) | |
1919 | input->high_timer++; | |
1920 | } | |
1921 | input->state = INPUT_ST_HIGH; | |
1922 | } else if (input->fall_timer >= input->fall_time) { | |
1923 | /* call release event */ | |
1924 | if (input->type == INPUT_TYPE_STD) { | |
1925 | void (*release_fct)(int) = input->u.std.release_fct; | |
c3ed0afc | 1926 | |
b565b3fb | 1927 | if (release_fct) |
429ccf05 HH |
1928 | release_fct(input->u.std.release_data); |
1929 | } else if (input->type == INPUT_TYPE_KBD) { | |
1930 | char *release_str = input->u.kbd.release_str; | |
c3ed0afc | 1931 | |
e6626de5 JC |
1932 | if (release_str[0]) { |
1933 | int s = sizeof(input->u.kbd.release_str); | |
c3ed0afc | 1934 | |
e6626de5 JC |
1935 | keypad_send_key(release_str, s); |
1936 | } | |
429ccf05 HH |
1937 | } |
1938 | ||
1939 | input->state = INPUT_ST_LOW; | |
1940 | } else { | |
1941 | input->fall_timer++; | |
1942 | inputs_stable = 0; | |
1943 | } | |
7005b584 WT |
1944 | } |
1945 | ||
698b1515 WT |
1946 | static void panel_process_inputs(void) |
1947 | { | |
1948 | struct list_head *item; | |
1949 | struct logical_input *input; | |
7005b584 | 1950 | |
698b1515 WT |
1951 | keypressed = 0; |
1952 | inputs_stable = 1; | |
1953 | list_for_each(item, &logical_inputs) { | |
1954 | input = list_entry(item, struct logical_input, list); | |
1955 | ||
1956 | switch (input->state) { | |
1957 | case INPUT_ST_LOW: | |
1958 | if ((phys_curr & input->mask) != input->value) | |
1959 | break; | |
429ccf05 HH |
1960 | /* if all needed ones were already set previously, |
1961 | * this means that this logical signal has been | |
1962 | * activated by the releasing of another combined | |
1963 | * signal, so we don't want to match. | |
1964 | * eg: AB -(release B)-> A -(release A)-> 0 : | |
1965 | * don't match A. | |
698b1515 WT |
1966 | */ |
1967 | if ((phys_prev & input->mask) == input->value) | |
1968 | break; | |
1969 | input->rise_timer = 0; | |
1970 | input->state = INPUT_ST_RISING; | |
1971 | /* no break here, fall through */ | |
1972 | case INPUT_ST_RISING: | |
1973 | if ((phys_curr & input->mask) != input->value) { | |
1974 | input->state = INPUT_ST_LOW; | |
1975 | break; | |
1976 | } | |
1977 | if (input->rise_timer < input->rise_time) { | |
1978 | inputs_stable = 0; | |
1979 | input->rise_timer++; | |
1980 | break; | |
1981 | } | |
1982 | input->high_timer = 0; | |
1983 | input->state = INPUT_ST_HIGH; | |
1984 | /* no break here, fall through */ | |
1985 | case INPUT_ST_HIGH: | |
429ccf05 | 1986 | if (input_state_high(input)) |
698b1515 | 1987 | break; |
698b1515 WT |
1988 | /* no break here, fall through */ |
1989 | case INPUT_ST_FALLING: | |
429ccf05 | 1990 | input_state_falling(input); |
698b1515 WT |
1991 | } |
1992 | } | |
1993 | } | |
7005b584 | 1994 | |
698b1515 WT |
1995 | static void panel_scan_timer(void) |
1996 | { | |
a8b2580b | 1997 | if (keypad.enabled && keypad_initialized) { |
d4d2dbca | 1998 | if (spin_trylock_irq(&pprt_lock)) { |
698b1515 | 1999 | phys_scan_contacts(); |
429ccf05 HH |
2000 | |
2001 | /* no need for the parport anymore */ | |
d4d2dbca | 2002 | spin_unlock_irq(&pprt_lock); |
7005b584 WT |
2003 | } |
2004 | ||
698b1515 WT |
2005 | if (!inputs_stable || phys_curr != phys_prev) |
2006 | panel_process_inputs(); | |
7005b584 | 2007 | } |
7005b584 | 2008 | |
6d8b588c | 2009 | if (lcd.enabled && lcd.initialized) { |
698b1515 | 2010 | if (keypressed) { |
6d8b588c MG |
2011 | if (lcd.light_tempo == 0 |
2012 | && ((lcd.flags & LCD_FLAG_L) == 0)) | |
698b1515 | 2013 | lcd_backlight(1); |
6d8b588c MG |
2014 | lcd.light_tempo = FLASH_LIGHT_TEMPO; |
2015 | } else if (lcd.light_tempo > 0) { | |
2016 | lcd.light_tempo--; | |
2017 | if (lcd.light_tempo == 0 | |
2018 | && ((lcd.flags & LCD_FLAG_L) == 0)) | |
698b1515 WT |
2019 | lcd_backlight(0); |
2020 | } | |
2021 | } | |
2022 | ||
2023 | mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME); | |
7005b584 WT |
2024 | } |
2025 | ||
698b1515 WT |
2026 | static void init_scan_timer(void) |
2027 | { | |
b565b3fb | 2028 | if (scan_timer.function) |
698b1515 WT |
2029 | return; /* already started */ |
2030 | ||
8f6e36c5 | 2031 | setup_timer(&scan_timer, (void *)&panel_scan_timer, 0); |
698b1515 | 2032 | scan_timer.expires = jiffies + INPUT_POLL_TIME; |
698b1515 | 2033 | add_timer(&scan_timer); |
7005b584 WT |
2034 | } |
2035 | ||
2036 | /* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits. | |
429ccf05 HH |
2037 | * if <omask> or <imask> are non-null, they will be or'ed with the bits |
2038 | * corresponding to out and in bits respectively. | |
7005b584 WT |
2039 | * returns 1 if ok, 0 if error (in which case, nothing is written). |
2040 | */ | |
36d2041a | 2041 | static int input_name2mask(const char *name, pmask_t *mask, pmask_t *value, |
698b1515 WT |
2042 | char *imask, char *omask) |
2043 | { | |
2044 | static char sigtab[10] = "EeSsPpAaBb"; | |
2045 | char im, om; | |
2046 | pmask_t m, v; | |
2047 | ||
2d53426b DB |
2048 | om = 0ULL; |
2049 | im = 0ULL; | |
2050 | m = 0ULL; | |
2051 | v = 0ULL; | |
698b1515 WT |
2052 | while (*name) { |
2053 | int in, out, bit, neg; | |
c3ed0afc | 2054 | |
fdf4a494 DB |
2055 | for (in = 0; (in < sizeof(sigtab)) && (sigtab[in] != *name); |
2056 | in++) | |
698b1515 | 2057 | ; |
fdf4a494 | 2058 | |
698b1515 WT |
2059 | if (in >= sizeof(sigtab)) |
2060 | return 0; /* input name not found */ | |
2061 | neg = (in & 1); /* odd (lower) names are negated */ | |
2062 | in >>= 1; | |
79f2af62 | 2063 | im |= BIT(in); |
698b1515 WT |
2064 | |
2065 | name++; | |
2066 | if (isdigit(*name)) { | |
2067 | out = *name - '0'; | |
79f2af62 | 2068 | om |= BIT(out); |
3ac76904 | 2069 | } else if (*name == '-') { |
698b1515 | 2070 | out = 8; |
3ac76904 | 2071 | } else { |
698b1515 | 2072 | return 0; /* unknown bit name */ |
3ac76904 | 2073 | } |
698b1515 WT |
2074 | |
2075 | bit = (out * 5) + in; | |
2076 | ||
2077 | m |= 1ULL << bit; | |
2078 | if (!neg) | |
2079 | v |= 1ULL << bit; | |
2080 | name++; | |
7005b584 | 2081 | } |
698b1515 WT |
2082 | *mask = m; |
2083 | *value = v; | |
2084 | if (imask) | |
2085 | *imask |= im; | |
2086 | if (omask) | |
2087 | *omask |= om; | |
2088 | return 1; | |
7005b584 WT |
2089 | } |
2090 | ||
2091 | /* tries to bind a key to the signal name <name>. The key will send the | |
2092 | * strings <press>, <repeat>, <release> for these respective events. | |
2093 | * Returns the pointer to the new key if ok, NULL if the key could not be bound. | |
2094 | */ | |
36d2041a PH |
2095 | static struct logical_input *panel_bind_key(const char *name, const char *press, |
2096 | const char *repeat, | |
2097 | const char *release) | |
698b1515 WT |
2098 | { |
2099 | struct logical_input *key; | |
2100 | ||
fdf4a494 | 2101 | key = kzalloc(sizeof(*key), GFP_KERNEL); |
eb073a9b | 2102 | if (!key) |
698b1515 | 2103 | return NULL; |
eb073a9b | 2104 | |
698b1515 | 2105 | if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i, |
cb46f472 KV |
2106 | &scan_mask_o)) { |
2107 | kfree(key); | |
698b1515 | 2108 | return NULL; |
cb46f472 | 2109 | } |
698b1515 WT |
2110 | |
2111 | key->type = INPUT_TYPE_KBD; | |
2112 | key->state = INPUT_ST_LOW; | |
2113 | key->rise_time = 1; | |
2114 | key->fall_time = 1; | |
7005b584 | 2115 | |
698b1515 WT |
2116 | strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str)); |
2117 | strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str)); | |
2118 | strncpy(key->u.kbd.release_str, release, | |
2119 | sizeof(key->u.kbd.release_str)); | |
2120 | list_add(&key->list, &logical_inputs); | |
2121 | return key; | |
7005b584 WT |
2122 | } |
2123 | ||
63023177 | 2124 | #if 0 |
7005b584 WT |
2125 | /* tries to bind a callback function to the signal name <name>. The function |
2126 | * <press_fct> will be called with the <press_data> arg when the signal is | |
2127 | * activated, and so on for <release_fct>/<release_data> | |
429ccf05 HH |
2128 | * Returns the pointer to the new signal if ok, NULL if the signal could not |
2129 | * be bound. | |
7005b584 WT |
2130 | */ |
2131 | static struct logical_input *panel_bind_callback(char *name, | |
68d386bf | 2132 | void (*press_fct)(int), |
698b1515 | 2133 | int press_data, |
68d386bf | 2134 | void (*release_fct)(int), |
698b1515 WT |
2135 | int release_data) |
2136 | { | |
2137 | struct logical_input *callback; | |
2138 | ||
fdf4a494 | 2139 | callback = kmalloc(sizeof(*callback), GFP_KERNEL); |
eb073a9b | 2140 | if (!callback) |
698b1515 | 2141 | return NULL; |
eb073a9b | 2142 | |
698b1515 WT |
2143 | memset(callback, 0, sizeof(struct logical_input)); |
2144 | if (!input_name2mask(name, &callback->mask, &callback->value, | |
2145 | &scan_mask_i, &scan_mask_o)) | |
2146 | return NULL; | |
2147 | ||
2148 | callback->type = INPUT_TYPE_STD; | |
2149 | callback->state = INPUT_ST_LOW; | |
2150 | callback->rise_time = 1; | |
2151 | callback->fall_time = 1; | |
2152 | callback->u.std.press_fct = press_fct; | |
2153 | callback->u.std.press_data = press_data; | |
2154 | callback->u.std.release_fct = release_fct; | |
2155 | callback->u.std.release_data = release_data; | |
2156 | list_add(&callback->list, &logical_inputs); | |
2157 | return callback; | |
7005b584 | 2158 | } |
63023177 | 2159 | #endif |
7005b584 | 2160 | |
698b1515 WT |
2161 | static void keypad_init(void) |
2162 | { | |
2163 | int keynum; | |
c3ed0afc | 2164 | |
698b1515 WT |
2165 | init_waitqueue_head(&keypad_read_wait); |
2166 | keypad_buflen = 0; /* flushes any eventual noisy keystroke */ | |
7005b584 | 2167 | |
698b1515 | 2168 | /* Let's create all known keys */ |
7005b584 | 2169 | |
698b1515 WT |
2170 | for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) { |
2171 | panel_bind_key(keypad_profile[keynum][0], | |
2172 | keypad_profile[keynum][1], | |
2173 | keypad_profile[keynum][2], | |
2174 | keypad_profile[keynum][3]); | |
2175 | } | |
7005b584 | 2176 | |
698b1515 WT |
2177 | init_scan_timer(); |
2178 | keypad_initialized = 1; | |
7005b584 WT |
2179 | } |
2180 | ||
7005b584 WT |
2181 | /**************************************************/ |
2182 | /* device initialization */ | |
2183 | /**************************************************/ | |
2184 | ||
698b1515 WT |
2185 | static int panel_notify_sys(struct notifier_block *this, unsigned long code, |
2186 | void *unused) | |
2187 | { | |
6d8b588c | 2188 | if (lcd.enabled && lcd.initialized) { |
698b1515 WT |
2189 | switch (code) { |
2190 | case SYS_DOWN: | |
2191 | panel_lcd_print | |
2192 | ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+"); | |
2193 | break; | |
2194 | case SYS_HALT: | |
2195 | panel_lcd_print | |
2196 | ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+"); | |
2197 | break; | |
2198 | case SYS_POWER_OFF: | |
2199 | panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+"); | |
2200 | break; | |
2201 | default: | |
2202 | break; | |
2203 | } | |
7005b584 | 2204 | } |
698b1515 | 2205 | return NOTIFY_DONE; |
7005b584 WT |
2206 | } |
2207 | ||
2208 | static struct notifier_block panel_notifier = { | |
2209 | panel_notify_sys, | |
2210 | NULL, | |
2211 | 0 | |
2212 | }; | |
2213 | ||
698b1515 | 2214 | static void panel_attach(struct parport *port) |
7005b584 | 2215 | { |
9be83c0a SM |
2216 | struct pardev_cb panel_cb; |
2217 | ||
698b1515 WT |
2218 | if (port->number != parport) |
2219 | return; | |
2220 | ||
2221 | if (pprt) { | |
eb073a9b TY |
2222 | pr_err("%s: port->number=%d parport=%d, already registered!\n", |
2223 | __func__, port->number, parport); | |
698b1515 WT |
2224 | return; |
2225 | } | |
2226 | ||
9be83c0a SM |
2227 | memset(&panel_cb, 0, sizeof(panel_cb)); |
2228 | panel_cb.private = &pprt; | |
2229 | /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */ | |
2230 | ||
2231 | pprt = parport_register_dev_model(port, "panel", &panel_cb, 0); | |
b565b3fb | 2232 | if (!pprt) { |
eb073a9b TY |
2233 | pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n", |
2234 | __func__, port->number, parport); | |
10f3f5b7 KV |
2235 | return; |
2236 | } | |
698b1515 WT |
2237 | |
2238 | if (parport_claim(pprt)) { | |
eb073a9b TY |
2239 | pr_err("could not claim access to parport%d. Aborting.\n", |
2240 | parport); | |
10f3f5b7 | 2241 | goto err_unreg_device; |
698b1515 WT |
2242 | } |
2243 | ||
429ccf05 HH |
2244 | /* must init LCD first, just in case an IRQ from the keypad is |
2245 | * generated at keypad init | |
2246 | */ | |
a8b2580b | 2247 | if (lcd.enabled) { |
698b1515 | 2248 | lcd_init(); |
10f3f5b7 KV |
2249 | if (misc_register(&lcd_dev)) |
2250 | goto err_unreg_device; | |
698b1515 WT |
2251 | } |
2252 | ||
a8b2580b | 2253 | if (keypad.enabled) { |
698b1515 | 2254 | keypad_init(); |
10f3f5b7 KV |
2255 | if (misc_register(&keypad_dev)) |
2256 | goto err_lcd_unreg; | |
698b1515 | 2257 | } |
bb046fef | 2258 | register_reboot_notifier(&panel_notifier); |
10f3f5b7 KV |
2259 | return; |
2260 | ||
2261 | err_lcd_unreg: | |
a8b2580b | 2262 | if (lcd.enabled) |
10f3f5b7 KV |
2263 | misc_deregister(&lcd_dev); |
2264 | err_unreg_device: | |
2265 | parport_unregister_device(pprt); | |
2266 | pprt = NULL; | |
7005b584 WT |
2267 | } |
2268 | ||
698b1515 | 2269 | static void panel_detach(struct parport *port) |
7005b584 | 2270 | { |
698b1515 WT |
2271 | if (port->number != parport) |
2272 | return; | |
2273 | ||
2274 | if (!pprt) { | |
eb073a9b TY |
2275 | pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n", |
2276 | __func__, port->number, parport); | |
698b1515 WT |
2277 | return; |
2278 | } | |
b565b3fb | 2279 | if (scan_timer.function) |
7d98c63e | 2280 | del_timer_sync(&scan_timer); |
698b1515 | 2281 | |
b565b3fb | 2282 | if (pprt) { |
7d98c63e SM |
2283 | if (keypad.enabled) { |
2284 | misc_deregister(&keypad_dev); | |
2285 | keypad_initialized = 0; | |
2286 | } | |
bb046fef | 2287 | |
7d98c63e SM |
2288 | if (lcd.enabled) { |
2289 | panel_lcd_print("\x0cLCD driver " PANEL_VERSION | |
2290 | "\nunloaded.\x1b[Lc\x1b[Lb\x1b[L-"); | |
2291 | misc_deregister(&lcd_dev); | |
2292 | lcd.initialized = false; | |
2293 | } | |
698b1515 | 2294 | |
7d98c63e SM |
2295 | /* TODO: free all input signals */ |
2296 | parport_release(pprt); | |
2297 | parport_unregister_device(pprt); | |
2298 | pprt = NULL; | |
2299 | unregister_reboot_notifier(&panel_notifier); | |
0b0595bf | 2300 | } |
7005b584 WT |
2301 | } |
2302 | ||
2303 | static struct parport_driver panel_driver = { | |
698b1515 | 2304 | .name = "panel", |
9be83c0a | 2305 | .match_port = panel_attach, |
698b1515 | 2306 | .detach = panel_detach, |
9be83c0a | 2307 | .devmodel = true, |
7005b584 WT |
2308 | }; |
2309 | ||
2310 | /* init function */ | |
d9114767 | 2311 | static int __init panel_init_module(void) |
698b1515 | 2312 | { |
e134201b | 2313 | int selected_keypad_type = NOT_SET, err; |
698b1515 | 2314 | |
698b1515 WT |
2315 | /* take care of an eventual profile */ |
2316 | switch (profile) { | |
429ccf05 HH |
2317 | case PANEL_PROFILE_CUSTOM: |
2318 | /* custom profile */ | |
87b8e0c8 MG |
2319 | selected_keypad_type = DEFAULT_KEYPAD_TYPE; |
2320 | selected_lcd_type = DEFAULT_LCD_TYPE; | |
698b1515 | 2321 | break; |
429ccf05 HH |
2322 | case PANEL_PROFILE_OLD: |
2323 | /* 8 bits, 2*16, old keypad */ | |
87b8e0c8 MG |
2324 | selected_keypad_type = KEYPAD_TYPE_OLD; |
2325 | selected_lcd_type = LCD_TYPE_OLD; | |
2326 | ||
2327 | /* TODO: This two are a little hacky, sort it out later */ | |
2d35bcf6 | 2328 | if (lcd_width == NOT_SET) |
698b1515 | 2329 | lcd_width = 16; |
2d35bcf6 | 2330 | if (lcd_hwidth == NOT_SET) |
698b1515 WT |
2331 | lcd_hwidth = 16; |
2332 | break; | |
429ccf05 HH |
2333 | case PANEL_PROFILE_NEW: |
2334 | /* serial, 2*16, new keypad */ | |
87b8e0c8 MG |
2335 | selected_keypad_type = KEYPAD_TYPE_NEW; |
2336 | selected_lcd_type = LCD_TYPE_KS0074; | |
698b1515 | 2337 | break; |
429ccf05 HH |
2338 | case PANEL_PROFILE_HANTRONIX: |
2339 | /* 8 bits, 2*16 hantronix-like, no keypad */ | |
87b8e0c8 MG |
2340 | selected_keypad_type = KEYPAD_TYPE_NONE; |
2341 | selected_lcd_type = LCD_TYPE_HANTRONIX; | |
698b1515 | 2342 | break; |
429ccf05 HH |
2343 | case PANEL_PROFILE_NEXCOM: |
2344 | /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */ | |
87b8e0c8 MG |
2345 | selected_keypad_type = KEYPAD_TYPE_NEXCOM; |
2346 | selected_lcd_type = LCD_TYPE_NEXCOM; | |
698b1515 | 2347 | break; |
429ccf05 HH |
2348 | case PANEL_PROFILE_LARGE: |
2349 | /* 8 bits, 2*40, old keypad */ | |
87b8e0c8 MG |
2350 | selected_keypad_type = KEYPAD_TYPE_OLD; |
2351 | selected_lcd_type = LCD_TYPE_OLD; | |
698b1515 WT |
2352 | break; |
2353 | } | |
2354 | ||
87b8e0c8 MG |
2355 | /* |
2356 | * Overwrite selection with module param values (both keypad and lcd), | |
2357 | * where the deprecated params have lower prio. | |
2358 | */ | |
1a4b2e3e | 2359 | if (keypad_enabled != NOT_SET) |
87b8e0c8 | 2360 | selected_keypad_type = keypad_enabled; |
1a4b2e3e | 2361 | if (keypad_type != NOT_SET) |
87b8e0c8 MG |
2362 | selected_keypad_type = keypad_type; |
2363 | ||
2364 | keypad.enabled = (selected_keypad_type > 0); | |
2365 | ||
1a4b2e3e | 2366 | if (lcd_enabled != NOT_SET) |
87b8e0c8 | 2367 | selected_lcd_type = lcd_enabled; |
1a4b2e3e | 2368 | if (lcd_type != NOT_SET) |
87b8e0c8 MG |
2369 | selected_lcd_type = lcd_type; |
2370 | ||
2371 | lcd.enabled = (selected_lcd_type > 0); | |
698b1515 | 2372 | |
733345ec SM |
2373 | if (lcd.enabled) { |
2374 | /* | |
2375 | * Init lcd struct with load-time values to preserve exact | |
2376 | * current functionality (at least for now). | |
2377 | */ | |
2378 | lcd.height = lcd_height; | |
2379 | lcd.width = lcd_width; | |
2380 | lcd.bwidth = lcd_bwidth; | |
2381 | lcd.hwidth = lcd_hwidth; | |
2382 | lcd.charset = lcd_charset; | |
2383 | lcd.proto = lcd_proto; | |
2384 | lcd.pins.e = lcd_e_pin; | |
2385 | lcd.pins.rs = lcd_rs_pin; | |
2386 | lcd.pins.rw = lcd_rw_pin; | |
2387 | lcd.pins.cl = lcd_cl_pin; | |
2388 | lcd.pins.da = lcd_da_pin; | |
2389 | lcd.pins.bl = lcd_bl_pin; | |
2390 | ||
2391 | /* Leave it for now, just in case */ | |
2392 | lcd.esc_seq.len = -1; | |
2393 | } | |
2394 | ||
87b8e0c8 | 2395 | switch (selected_keypad_type) { |
698b1515 WT |
2396 | case KEYPAD_TYPE_OLD: |
2397 | keypad_profile = old_keypad_profile; | |
2398 | break; | |
2399 | case KEYPAD_TYPE_NEW: | |
2400 | keypad_profile = new_keypad_profile; | |
2401 | break; | |
2402 | case KEYPAD_TYPE_NEXCOM: | |
2403 | keypad_profile = nexcom_keypad_profile; | |
2404 | break; | |
2405 | default: | |
2406 | keypad_profile = NULL; | |
2407 | break; | |
2408 | } | |
2409 | ||
a8b2580b | 2410 | if (!lcd.enabled && !keypad.enabled) { |
f43de77c | 2411 | /* no device enabled, let's exit */ |
eb073a9b | 2412 | pr_err("driver version " PANEL_VERSION " disabled.\n"); |
698b1515 | 2413 | return -ENODEV; |
7005b584 | 2414 | } |
7005b584 | 2415 | |
e134201b SM |
2416 | err = parport_register_driver(&panel_driver); |
2417 | if (err) { | |
f43de77c | 2418 | pr_err("could not register with parport. Aborting.\n"); |
e134201b | 2419 | return err; |
f43de77c SM |
2420 | } |
2421 | ||
698b1515 | 2422 | if (pprt) |
493aa896 TY |
2423 | pr_info("driver version " PANEL_VERSION |
2424 | " registered on parport%d (io=0x%lx).\n", parport, | |
2425 | pprt->port->base); | |
698b1515 | 2426 | else |
493aa896 TY |
2427 | pr_info("driver version " PANEL_VERSION |
2428 | " not yet registered\n"); | |
698b1515 WT |
2429 | return 0; |
2430 | } | |
7005b584 | 2431 | |
f6d1fcfe | 2432 | static void __exit panel_cleanup_module(void) |
698b1515 | 2433 | { |
698b1515 | 2434 | parport_unregister_driver(&panel_driver); |
7005b584 | 2435 | } |
7005b584 | 2436 | |
7005b584 WT |
2437 | module_init(panel_init_module); |
2438 | module_exit(panel_cleanup_module); | |
2439 | MODULE_AUTHOR("Willy Tarreau"); | |
2440 | MODULE_LICENSE("GPL"); | |
7005b584 WT |
2441 | |
2442 | /* | |
2443 | * Local variables: | |
2444 | * c-indent-level: 4 | |
2445 | * tab-width: 8 | |
2446 | * End: | |
2447 | */ |