blob: 9fcd9f1d5dc8edc2547c5d303df87e5b666b8059 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
3 */
4
5/*
6 * LK keyboard driver for Linux, based on sunkbd.c (C) by Vojtech Pavlik
7 */
8
9/*
10 * DEC LK201 and LK401 keyboard driver for Linux (primary for DECstations
11 * and VAXstations, but can also be used on any standard RS232 with an
12 * adaptor).
13 *
14 * DISCLAIMER: This works for _me_. If you break anything by using the
15 * information given below, I will _not_ be liable!
16 *
17 * RJ10 pinout: To DE9: Or DB25:
Dmitry Torokhovd083e902005-05-29 02:28:42 -050018 * 1 - RxD <----> Pin 3 (TxD) <-> Pin 2 (TxD)
19 * 2 - GND <----> Pin 5 (GND) <-> Pin 7 (GND)
20 * 4 - TxD <----> Pin 2 (RxD) <-> Pin 3 (RxD)
21 * 3 - +12V (from HDD drive connector), DON'T connect to DE9 or DB25!!!
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
23 * Pin numbers for DE9 and DB25 are noted on the plug (quite small:). For
24 * RJ10, it's like this:
25 *
26 * __=__ Hold the plug in front of you, cable downwards,
27 * /___/| nose is hidden behind the plug. Now, pin 1 is at
28 * |1234|| the left side, pin 4 at the right and 2 and 3 are
29 * |IIII|| in between, of course:)
30 * | ||
31 * |____|/
32 * || So the adaptor consists of three connected cables
33 * || for data transmission (RxD and TxD) and signal ground.
34 * Additionally, you have to get +12V from somewhere.
35 * Most easily, you'll get that from a floppy or HDD power connector.
36 * It's the yellow cable there (black is ground and red is +5V).
37 *
38 * The keyboard and all the commands it understands are documented in
39 * "VCB02 Video Subsystem - Technical Manual", EK-104AA-TM-001. This
40 * document is LK201 specific, but LK401 is mostly compatible. It comes
41 * up in LK201 mode and doesn't report any of the additional keys it
42 * has. These need to be switched on with the LK_CMD_ENABLE_LK401
43 * command. You'll find this document (scanned .pdf file) on MANX,
44 * a search engine specific to DEC documentation. Try
45 * http://www.vt100.net/manx/details?pn=EK-104AA-TM-001;id=21;cp=1
46 */
47
48/*
49 * This program is free software; you can redistribute it and/or modify
50 * it under the terms of the GNU General Public License as published by
51 * the Free Software Foundation; either version 2 of the License, or
52 * (at your option) any later version.
53 *
54 * This program is distributed in the hope that it will be useful,
55 * but WITHOUT ANY WARRANTY; without even the implied warranty of
56 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 * GNU General Public License for more details.
58 *
59 * You should have received a copy of the GNU General Public License
60 * along with this program; if not, write to the Free Software
61 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
64#include <linux/delay.h>
65#include <linux/slab.h>
66#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/input.h>
69#include <linux/serio.h>
70#include <linux/workqueue.h>
71
72#define DRIVER_DESC "LK keyboard driver"
73
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070074MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
75MODULE_DESCRIPTION(DRIVER_DESC);
76MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Known parameters:
80 * bell_volume
81 * keyclick_volume
82 * ctrlclick_volume
83 *
84 * Please notice that there's not yet an API to set these at runtime.
85 */
86static int bell_volume = 100; /* % */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070087module_param(bell_volume, int, 0);
88MODULE_PARM_DESC(bell_volume, "Bell volume (in %). default is 100%");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static int keyclick_volume = 100; /* % */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070091module_param(keyclick_volume, int, 0);
92MODULE_PARM_DESC(keyclick_volume, "Keyclick volume (in %), default is 100%");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94static int ctrlclick_volume = 100; /* % */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070095module_param(ctrlclick_volume, int, 0);
96MODULE_PARM_DESC(ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -050098static int lk201_compose_is_alt;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070099module_param(lk201_compose_is_alt, int, 0);
100MODULE_PARM_DESC(lk201_compose_is_alt,
101 "If set non-zero, LK201' Compose key will act as an Alt key");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103
104
105#undef LKKBD_DEBUG
106#ifdef LKKBD_DEBUG
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700107#define DBG(x...) printk(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#else
109#define DBG(x...) do {} while (0)
110#endif
111
112/* LED control */
113#define LK_LED_WAIT 0x81
114#define LK_LED_COMPOSE 0x82
115#define LK_LED_SHIFTLOCK 0x84
116#define LK_LED_SCROLLLOCK 0x88
117#define LK_CMD_LED_ON 0x13
118#define LK_CMD_LED_OFF 0x11
119
120/* Mode control */
121#define LK_MODE_DOWN 0x80
122#define LK_MODE_AUTODOWN 0x82
123#define LK_MODE_UPDOWN 0x86
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700124#define LK_CMD_SET_MODE(mode, div) ((mode) | ((div) << 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* Misc commands */
127#define LK_CMD_ENABLE_KEYCLICK 0x1b
128#define LK_CMD_DISABLE_KEYCLICK 0x99
129#define LK_CMD_DISABLE_BELL 0xa1
130#define LK_CMD_SOUND_BELL 0xa7
131#define LK_CMD_ENABLE_BELL 0x23
132#define LK_CMD_DISABLE_CTRCLICK 0xb9
133#define LK_CMD_ENABLE_CTRCLICK 0xbb
134#define LK_CMD_SET_DEFAULTS 0xd3
135#define LK_CMD_POWERCYCLE_RESET 0xfd
136#define LK_CMD_ENABLE_LK401 0xe9
137#define LK_CMD_REQUEST_ID 0xab
138
139/* Misc responses from keyboard */
140#define LK_STUCK_KEY 0x3d
141#define LK_SELFTEST_FAILED 0x3e
142#define LK_ALL_KEYS_UP 0xb3
143#define LK_METRONOME 0xb4
144#define LK_OUTPUT_ERROR 0xb5
145#define LK_INPUT_ERROR 0xb6
146#define LK_KBD_LOCKED 0xb7
147#define LK_KBD_TEST_MODE_ACK 0xb8
148#define LK_PREFIX_KEY_DOWN 0xb9
149#define LK_MODE_CHANGE_ACK 0xba
150#define LK_RESPONSE_RESERVED 0xbb
151
152#define LK_NUM_KEYCODES 256
153#define LK_NUM_IGNORE_BYTES 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700155static unsigned short lkkbd_keycode[LK_NUM_KEYCODES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 [0x56] = KEY_F1,
157 [0x57] = KEY_F2,
158 [0x58] = KEY_F3,
159 [0x59] = KEY_F4,
160 [0x5a] = KEY_F5,
161 [0x64] = KEY_F6,
162 [0x65] = KEY_F7,
163 [0x66] = KEY_F8,
164 [0x67] = KEY_F9,
165 [0x68] = KEY_F10,
166 [0x71] = KEY_F11,
167 [0x72] = KEY_F12,
168 [0x73] = KEY_F13,
169 [0x74] = KEY_F14,
170 [0x7c] = KEY_F15,
171 [0x7d] = KEY_F16,
172 [0x80] = KEY_F17,
173 [0x81] = KEY_F18,
174 [0x82] = KEY_F19,
175 [0x83] = KEY_F20,
176 [0x8a] = KEY_FIND,
177 [0x8b] = KEY_INSERT,
178 [0x8c] = KEY_DELETE,
179 [0x8d] = KEY_SELECT,
180 [0x8e] = KEY_PAGEUP,
181 [0x8f] = KEY_PAGEDOWN,
182 [0x92] = KEY_KP0,
183 [0x94] = KEY_KPDOT,
184 [0x95] = KEY_KPENTER,
185 [0x96] = KEY_KP1,
186 [0x97] = KEY_KP2,
187 [0x98] = KEY_KP3,
188 [0x99] = KEY_KP4,
189 [0x9a] = KEY_KP5,
190 [0x9b] = KEY_KP6,
191 [0x9c] = KEY_KPCOMMA,
192 [0x9d] = KEY_KP7,
193 [0x9e] = KEY_KP8,
194 [0x9f] = KEY_KP9,
195 [0xa0] = KEY_KPMINUS,
196 [0xa1] = KEY_PROG1,
197 [0xa2] = KEY_PROG2,
198 [0xa3] = KEY_PROG3,
199 [0xa4] = KEY_PROG4,
200 [0xa7] = KEY_LEFT,
201 [0xa8] = KEY_RIGHT,
202 [0xa9] = KEY_DOWN,
203 [0xaa] = KEY_UP,
204 [0xab] = KEY_RIGHTSHIFT,
205 [0xac] = KEY_LEFTALT,
206 [0xad] = KEY_COMPOSE, /* Right Compose, that is. */
207 [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */
208 [0xaf] = KEY_LEFTCTRL,
209 [0xb0] = KEY_CAPSLOCK,
210 [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */
211 [0xb2] = KEY_RIGHTALT,
212 [0xbc] = KEY_BACKSPACE,
213 [0xbd] = KEY_ENTER,
214 [0xbe] = KEY_TAB,
215 [0xbf] = KEY_ESC,
216 [0xc0] = KEY_1,
217 [0xc1] = KEY_Q,
218 [0xc2] = KEY_A,
219 [0xc3] = KEY_Z,
220 [0xc5] = KEY_2,
221 [0xc6] = KEY_W,
222 [0xc7] = KEY_S,
223 [0xc8] = KEY_X,
224 [0xc9] = KEY_102ND,
225 [0xcb] = KEY_3,
226 [0xcc] = KEY_E,
227 [0xcd] = KEY_D,
228 [0xce] = KEY_C,
229 [0xd0] = KEY_4,
230 [0xd1] = KEY_R,
231 [0xd2] = KEY_F,
232 [0xd3] = KEY_V,
233 [0xd4] = KEY_SPACE,
234 [0xd6] = KEY_5,
235 [0xd7] = KEY_T,
236 [0xd8] = KEY_G,
237 [0xd9] = KEY_B,
238 [0xdb] = KEY_6,
239 [0xdc] = KEY_Y,
240 [0xdd] = KEY_H,
241 [0xde] = KEY_N,
242 [0xe0] = KEY_7,
243 [0xe1] = KEY_U,
244 [0xe2] = KEY_J,
245 [0xe3] = KEY_M,
246 [0xe5] = KEY_8,
247 [0xe6] = KEY_I,
248 [0xe7] = KEY_K,
249 [0xe8] = KEY_COMMA,
250 [0xea] = KEY_9,
251 [0xeb] = KEY_O,
252 [0xec] = KEY_L,
253 [0xed] = KEY_DOT,
254 [0xef] = KEY_0,
255 [0xf0] = KEY_P,
256 [0xf2] = KEY_SEMICOLON,
257 [0xf3] = KEY_SLASH,
258 [0xf5] = KEY_EQUAL,
259 [0xf6] = KEY_RIGHTBRACE,
260 [0xf7] = KEY_BACKSLASH,
261 [0xf9] = KEY_MINUS,
262 [0xfa] = KEY_LEFTBRACE,
263 [0xfb] = KEY_APOSTROPHE,
264};
265
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500266#define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700267 if (test_bit(LED, (LK)->dev->led)) \
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500268 VAR_ON |= BITS; \
269 else \
270 VAR_OFF |= BITS; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } while (0)
272
273/*
274 * Per-keyboard data
275 */
276struct lkkbd {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700277 unsigned short keycode[LK_NUM_KEYCODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int ignore_bytes;
279 unsigned char id[LK_NUM_IGNORE_BYTES];
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500280 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct serio *serio;
282 struct work_struct tq;
283 char name[64];
284 char phys[32];
285 char type;
286 int bell_volume;
287 int keyclick_volume;
288 int ctrlclick_volume;
289};
290
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500291#ifdef LKKBD_DEBUG
292/*
293 * Responses from the keyboard and mapping back to their names.
294 */
295static struct {
296 unsigned char value;
297 unsigned char *name;
298} lk_response[] = {
299#define RESPONSE(x) { .value = (x), .name = #x, }
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700300 RESPONSE(LK_STUCK_KEY),
301 RESPONSE(LK_SELFTEST_FAILED),
302 RESPONSE(LK_ALL_KEYS_UP),
303 RESPONSE(LK_METRONOME),
304 RESPONSE(LK_OUTPUT_ERROR),
305 RESPONSE(LK_INPUT_ERROR),
306 RESPONSE(LK_KBD_LOCKED),
307 RESPONSE(LK_KBD_TEST_MODE_ACK),
308 RESPONSE(LK_PREFIX_KEY_DOWN),
309 RESPONSE(LK_MODE_CHANGE_ACK),
310 RESPONSE(LK_RESPONSE_RESERVED),
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500311#undef RESPONSE
312};
313
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700314static unsigned char *response_name(unsigned char value)
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500315{
316 int i;
317
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700318 for (i = 0; i < ARRAY_SIZE(lk_response); i++)
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500319 if (lk_response[i].value == value)
320 return lk_response[i].name;
321
322 return "<unknown>";
323}
324#endif /* LKKBD_DEBUG */
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*
327 * Calculate volume parameter byte for a given volume.
328 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700329static unsigned char volume_to_hw(int volume_percent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 unsigned char ret = 0;
332
333 if (volume_percent < 0)
334 volume_percent = 0;
335 if (volume_percent > 100)
336 volume_percent = 100;
337
338 if (volume_percent >= 0)
339 ret = 7;
340 if (volume_percent >= 13) /* 12.5 */
341 ret = 6;
342 if (volume_percent >= 25)
343 ret = 5;
344 if (volume_percent >= 38) /* 37.5 */
345 ret = 4;
346 if (volume_percent >= 50)
347 ret = 3;
348 if (volume_percent >= 63) /* 62.5 */
349 ret = 2; /* This is the default volume */
350 if (volume_percent >= 75)
351 ret = 1;
352 if (volume_percent >= 88) /* 87.5 */
353 ret = 0;
354
355 ret |= 0x80;
356
357 return ret;
358}
359
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700360static void lkkbd_detection_done(struct lkkbd *lk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 int i;
363
364 /*
365 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
366 */
367 lk->keycode[0xb1] = KEY_COMPOSE;
368
369 /*
370 * Print keyboard name and modify Compose=Alt on user's request.
371 */
372 switch (lk->id[4]) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700373 case 1:
374 strlcpy(lk->name, "DEC LK201 keyboard", sizeof(lk->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700376 if (lk201_compose_is_alt)
377 lk->keycode[0xb1] = KEY_LEFTALT;
378 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700380 case 2:
381 strlcpy(lk->name, "DEC LK401 keyboard", sizeof(lk->name));
382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700384 default:
385 strlcpy(lk->name, "Unknown DEC keyboard", sizeof(lk->name));
386 printk(KERN_ERR
387 "lkkbd: keyboard on %s is unknown, please report to "
388 "Jan-Benedict Glaw <jbglaw@lug-owl.de>\n", lk->phys);
389 printk(KERN_ERR "lkkbd: keyboard ID'ed as:");
390 for (i = 0; i < LK_NUM_IGNORE_BYTES; i++)
391 printk(" 0x%02x", lk->id[i]);
392 printk("\n");
393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700395
396 printk(KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
397 lk->phys, lk->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
400 * Report errors during keyboard boot-up.
401 */
402 switch (lk->id[2]) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700403 case 0x00:
404 /* All okay */
405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700407 case LK_STUCK_KEY:
408 printk(KERN_ERR "lkkbd: Stuck key on keyboard at %s\n",
409 lk->phys);
410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700412 case LK_SELFTEST_FAILED:
413 printk(KERN_ERR
414 "lkkbd: Selftest failed on keyboard at %s, "
415 "keyboard may not work properly\n", lk->phys);
416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700418 default:
419 printk(KERN_ERR
420 "lkkbd: Unknown error %02x on keyboard at %s\n",
421 lk->id[2], lk->phys);
422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 /*
426 * Try to hint user if there's a stuck key.
427 */
428 if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700429 printk(KERN_ERR
430 "Scancode of stuck key is 0x%02x, keycode is 0x%04x\n",
431 lk->id[3], lk->keycode[lk->id[3]]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * lkkbd_interrupt() is called by the low level driver when a character
436 * is received.
437 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700438static irqreturn_t lkkbd_interrupt(struct serio *serio,
439 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700441 struct lkkbd *lk = serio_get_drvdata(serio);
442 struct input_dev *input_dev = lk->dev;
443 unsigned int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int i;
445
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700446 DBG(KERN_INFO "Got byte 0x%02x\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (lk->ignore_bytes > 0) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700449 DBG(KERN_INFO "Ignoring a byte on %s\n", lk->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
451
452 if (lk->ignore_bytes == 0)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700453 lkkbd_detection_done(lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 return IRQ_HANDLED;
456 }
457
458 switch (data) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700459 case LK_ALL_KEYS_UP:
460 for (i = 0; i < ARRAY_SIZE(lkkbd_keycode); i++)
461 input_report_key(input_dev, lk->keycode[i], 0);
462 input_sync(input_dev);
463 break;
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500464
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700465 case 0x01:
466 DBG(KERN_INFO "Got 0x01, scheduling re-initialization\n");
467 lk->ignore_bytes = LK_NUM_IGNORE_BYTES;
468 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
469 schedule_work(&lk->tq);
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700472 case LK_METRONOME:
473 case LK_OUTPUT_ERROR:
474 case LK_INPUT_ERROR:
475 case LK_KBD_LOCKED:
476 case LK_KBD_TEST_MODE_ACK:
477 case LK_PREFIX_KEY_DOWN:
478 case LK_MODE_CHANGE_ACK:
479 case LK_RESPONSE_RESERVED:
480 DBG(KERN_INFO "Got %s and don't know how to handle...\n",
481 response_name(data));
482 break;
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500483
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700484 default:
485 keycode = lk->keycode[data];
486 if (keycode != KEY_RESERVED) {
487 input_report_key(input_dev, keycode,
488 !test_bit(keycode, input_dev->key));
489 input_sync(input_dev);
490 } else {
491 printk(KERN_WARNING
492 "%s: Unknown key with scancode 0x%02x on %s.\n",
493 __FILE__, data, lk->name);
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
497 return IRQ_HANDLED;
498}
499
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700500static void lkkbd_toggle_leds(struct lkkbd *lk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700502 struct serio *serio = lk->serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 unsigned char leds_on = 0;
504 unsigned char leds_off = 0;
505
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700506 CHECK_LED(lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK);
507 CHECK_LED(lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE);
508 CHECK_LED(lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK);
509 CHECK_LED(lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT);
510 if (leds_on != 0) {
511 serio_write(serio, LK_CMD_LED_ON);
512 serio_write(serio, leds_on);
513 }
514 if (leds_off != 0) {
515 serio_write(serio, LK_CMD_LED_OFF);
516 serio_write(serio, leds_off);
517 }
518}
519
520static void lkkbd_toggle_keyclick(struct lkkbd *lk, bool on)
521{
522 struct serio *serio = lk->serio;
523
524 if (on) {
525 DBG("%s: Activating key clicks\n", __func__);
526 serio_write(serio, LK_CMD_ENABLE_KEYCLICK);
527 serio_write(serio, volume_to_hw(lk->keyclick_volume));
528 serio_write(serio, LK_CMD_ENABLE_CTRCLICK);
529 serio_write(serio, volume_to_hw(lk->ctrlclick_volume));
530 } else {
531 DBG("%s: Deactivating key clicks\n", __func__);
532 serio_write(serio, LK_CMD_DISABLE_KEYCLICK);
533 serio_write(serio, LK_CMD_DISABLE_CTRCLICK);
534 }
535
536}
537
538/*
539 * lkkbd_event() handles events from the input module.
540 */
541static int lkkbd_event(struct input_dev *dev,
542 unsigned int type, unsigned int code, int value)
543{
544 struct lkkbd *lk = input_get_drvdata(dev);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 switch (type) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700547 case EV_LED:
548 lkkbd_toggle_leds(lk);
549 return 0;
550
551 case EV_SND:
552 switch (code) {
553 case SND_CLICK:
554 lkkbd_toggle_keyclick(lk, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
556
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700557 case SND_BELL:
558 if (value != 0)
559 serio_write(lk->serio, LK_CMD_SOUND_BELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700561 return 0;
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700566 default:
567 printk(KERN_ERR "%s(): Got unknown type %d, code %d, value %d\n",
568 __func__, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
571 return -1;
572}
573
574/*
575 * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
576 * were in.
577 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700578static void lkkbd_reinit(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
David Howellsc4028952006-11-22 14:57:56 +0000580 struct lkkbd *lk = container_of(work, struct lkkbd, tq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int division;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* Ask for ID */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700584 serio_write(lk->serio, LK_CMD_REQUEST_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* Reset parameters */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700587 serio_write(lk->serio, LK_CMD_SET_DEFAULTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* Set LEDs */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700590 lkkbd_toggle_leds(lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /*
593 * Try to activate extended LK401 mode. This command will
594 * only work with a LK401 keyboard and grants access to
595 * LAlt, RAlt, RCompose and RShift.
596 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700597 serio_write(lk->serio, LK_CMD_ENABLE_LK401);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* Set all keys to UPDOWN mode */
600 for (division = 1; division <= 14; division++)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700601 serio_write(lk->serio,
602 LK_CMD_SET_MODE(LK_MODE_UPDOWN, division));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* Enable bell and set volume */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700605 serio_write(lk->serio, LK_CMD_ENABLE_BELL);
606 serio_write(lk->serio, volume_to_hw(lk->bell_volume));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* Enable/disable keyclick (and possibly set volume) */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700609 lkkbd_toggle_keyclick(lk, test_bit(SND_CLICK, lk->dev->snd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* Sound the bell if needed */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700612 if (test_bit(SND_BELL, lk->dev->snd))
613 serio_write(lk->serio, LK_CMD_SOUND_BELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617 * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
618 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700619static int lkkbd_connect(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct lkkbd *lk;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500622 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int i;
624 int err;
625
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700626 lk = kzalloc(sizeof(struct lkkbd), GFP_KERNEL);
627 input_dev = input_allocate_device();
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500628 if (!lk || !input_dev) {
629 err = -ENOMEM;
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500630 goto fail1;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 lk->serio = serio;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500634 lk->dev = input_dev;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700635 INIT_WORK(&lk->tq, lkkbd_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 lk->bell_volume = bell_volume;
637 lk->keyclick_volume = keyclick_volume;
638 lk->ctrlclick_volume = ctrlclick_volume;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700639 memcpy(lk->keycode, lkkbd_keycode, sizeof(lk->keycode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700641 strlcpy(lk->name, "DEC LK keyboard", sizeof(lk->name));
642 snprintf(lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500644 input_dev->name = lk->name;
645 input_dev->phys = lk->phys;
646 input_dev->id.bustype = BUS_RS232;
647 input_dev->id.vendor = SERIO_LKKBD;
648 input_dev->id.product = 0;
649 input_dev->id.version = 0x0100;
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400650 input_dev->dev.parent = &serio->dev;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500651 input_dev->event = lkkbd_event;
Dmitry Torokhovb3568722007-04-12 01:34:20 -0400652
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700653 input_set_drvdata(input_dev, lk);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500654
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700655 __set_bit(EV_KEY, input_dev->evbit);
656 __set_bit(EV_LED, input_dev->evbit);
657 __set_bit(EV_SND, input_dev->evbit);
658 __set_bit(EV_REP, input_dev->evbit);
659 __set_bit(LED_CAPSL, input_dev->ledbit);
660 __set_bit(LED_SLEEP, input_dev->ledbit);
661 __set_bit(LED_COMPOSE, input_dev->ledbit);
662 __set_bit(LED_SCROLLL, input_dev->ledbit);
663 __set_bit(SND_BELL, input_dev->sndbit);
664 __set_bit(SND_CLICK, input_dev->sndbit);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500665
666 input_dev->keycode = lk->keycode;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700667 input_dev->keycodesize = sizeof(lk->keycode[0]);
668 input_dev->keycodemax = ARRAY_SIZE(lk->keycode);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700669
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500670 for (i = 0; i < LK_NUM_KEYCODES; i++)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700671 __set_bit(lk->keycode[i], input_dev->keybit);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700672 __clear_bit(KEY_RESERVED, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700674 serio_set_drvdata(serio, lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700676 err = serio_open(serio, drv);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500677 if (err)
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500678 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700680 err = input_register_device(lk->dev);
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500681 if (err)
682 goto fail3;
683
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700684 serio_write(lk->serio, LK_CMD_POWERCYCLE_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 return 0;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500687
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700688 fail3: serio_close(serio);
689 fail2: serio_set_drvdata(serio, NULL);
690 fail1: input_free_device(input_dev);
691 kfree(lk);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695/*
696 * lkkbd_disconnect() unregisters and closes behind us.
697 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700698static void lkkbd_disconnect(struct serio *serio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700700 struct lkkbd *lk = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700702 input_get_device(lk->dev);
703 input_unregister_device(lk->dev);
704 serio_close(serio);
705 serio_set_drvdata(serio, NULL);
706 input_put_device(lk->dev);
707 kfree(lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static struct serio_device_id lkkbd_serio_ids[] = {
711 {
712 .type = SERIO_RS232,
713 .proto = SERIO_LKKBD,
714 .id = SERIO_ANY,
715 .extra = SERIO_ANY,
716 },
717 { 0 }
718};
719
720MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
721
722static struct serio_driver lkkbd_drv = {
723 .driver = {
724 .name = "lkkbd",
725 },
726 .description = DRIVER_DESC,
727 .id_table = lkkbd_serio_ids,
728 .connect = lkkbd_connect,
729 .disconnect = lkkbd_disconnect,
730 .interrupt = lkkbd_interrupt,
731};
732
Axel Lin65ac9f72012-04-03 23:50:17 -0700733module_serio_driver(lkkbd_drv);