blob: fc0a63c2f2785a8b529ad3e0afc43debc284d6c7 [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>
68#include <linux/init.h>
69#include <linux/input.h>
70#include <linux/serio.h>
71#include <linux/workqueue.h>
72
73#define DRIVER_DESC "LK keyboard driver"
74
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070075MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
76MODULE_DESCRIPTION(DRIVER_DESC);
77MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * Known parameters:
81 * bell_volume
82 * keyclick_volume
83 * ctrlclick_volume
84 *
85 * Please notice that there's not yet an API to set these at runtime.
86 */
87static int bell_volume = 100; /* % */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070088module_param(bell_volume, int, 0);
89MODULE_PARM_DESC(bell_volume, "Bell volume (in %). default is 100%");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static int keyclick_volume = 100; /* % */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070092module_param(keyclick_volume, int, 0);
93MODULE_PARM_DESC(keyclick_volume, "Keyclick volume (in %), default is 100%");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static int ctrlclick_volume = 100; /* % */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -070096module_param(ctrlclick_volume, int, 0);
97MODULE_PARM_DESC(ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -050099static int lk201_compose_is_alt;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700100module_param(lk201_compose_is_alt, int, 0);
101MODULE_PARM_DESC(lk201_compose_is_alt,
102 "If set non-zero, LK201' Compose key will act as an Alt key");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104
105
106#undef LKKBD_DEBUG
107#ifdef LKKBD_DEBUG
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700108#define DBG(x...) printk(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#else
110#define DBG(x...) do {} while (0)
111#endif
112
113/* LED control */
114#define LK_LED_WAIT 0x81
115#define LK_LED_COMPOSE 0x82
116#define LK_LED_SHIFTLOCK 0x84
117#define LK_LED_SCROLLLOCK 0x88
118#define LK_CMD_LED_ON 0x13
119#define LK_CMD_LED_OFF 0x11
120
121/* Mode control */
122#define LK_MODE_DOWN 0x80
123#define LK_MODE_AUTODOWN 0x82
124#define LK_MODE_UPDOWN 0x86
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700125#define LK_CMD_SET_MODE(mode, div) ((mode) | ((div) << 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* Misc commands */
128#define LK_CMD_ENABLE_KEYCLICK 0x1b
129#define LK_CMD_DISABLE_KEYCLICK 0x99
130#define LK_CMD_DISABLE_BELL 0xa1
131#define LK_CMD_SOUND_BELL 0xa7
132#define LK_CMD_ENABLE_BELL 0x23
133#define LK_CMD_DISABLE_CTRCLICK 0xb9
134#define LK_CMD_ENABLE_CTRCLICK 0xbb
135#define LK_CMD_SET_DEFAULTS 0xd3
136#define LK_CMD_POWERCYCLE_RESET 0xfd
137#define LK_CMD_ENABLE_LK401 0xe9
138#define LK_CMD_REQUEST_ID 0xab
139
140/* Misc responses from keyboard */
141#define LK_STUCK_KEY 0x3d
142#define LK_SELFTEST_FAILED 0x3e
143#define LK_ALL_KEYS_UP 0xb3
144#define LK_METRONOME 0xb4
145#define LK_OUTPUT_ERROR 0xb5
146#define LK_INPUT_ERROR 0xb6
147#define LK_KBD_LOCKED 0xb7
148#define LK_KBD_TEST_MODE_ACK 0xb8
149#define LK_PREFIX_KEY_DOWN 0xb9
150#define LK_MODE_CHANGE_ACK 0xba
151#define LK_RESPONSE_RESERVED 0xbb
152
153#define LK_NUM_KEYCODES 256
154#define LK_NUM_IGNORE_BYTES 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700156static unsigned short lkkbd_keycode[LK_NUM_KEYCODES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 [0x56] = KEY_F1,
158 [0x57] = KEY_F2,
159 [0x58] = KEY_F3,
160 [0x59] = KEY_F4,
161 [0x5a] = KEY_F5,
162 [0x64] = KEY_F6,
163 [0x65] = KEY_F7,
164 [0x66] = KEY_F8,
165 [0x67] = KEY_F9,
166 [0x68] = KEY_F10,
167 [0x71] = KEY_F11,
168 [0x72] = KEY_F12,
169 [0x73] = KEY_F13,
170 [0x74] = KEY_F14,
171 [0x7c] = KEY_F15,
172 [0x7d] = KEY_F16,
173 [0x80] = KEY_F17,
174 [0x81] = KEY_F18,
175 [0x82] = KEY_F19,
176 [0x83] = KEY_F20,
177 [0x8a] = KEY_FIND,
178 [0x8b] = KEY_INSERT,
179 [0x8c] = KEY_DELETE,
180 [0x8d] = KEY_SELECT,
181 [0x8e] = KEY_PAGEUP,
182 [0x8f] = KEY_PAGEDOWN,
183 [0x92] = KEY_KP0,
184 [0x94] = KEY_KPDOT,
185 [0x95] = KEY_KPENTER,
186 [0x96] = KEY_KP1,
187 [0x97] = KEY_KP2,
188 [0x98] = KEY_KP3,
189 [0x99] = KEY_KP4,
190 [0x9a] = KEY_KP5,
191 [0x9b] = KEY_KP6,
192 [0x9c] = KEY_KPCOMMA,
193 [0x9d] = KEY_KP7,
194 [0x9e] = KEY_KP8,
195 [0x9f] = KEY_KP9,
196 [0xa0] = KEY_KPMINUS,
197 [0xa1] = KEY_PROG1,
198 [0xa2] = KEY_PROG2,
199 [0xa3] = KEY_PROG3,
200 [0xa4] = KEY_PROG4,
201 [0xa7] = KEY_LEFT,
202 [0xa8] = KEY_RIGHT,
203 [0xa9] = KEY_DOWN,
204 [0xaa] = KEY_UP,
205 [0xab] = KEY_RIGHTSHIFT,
206 [0xac] = KEY_LEFTALT,
207 [0xad] = KEY_COMPOSE, /* Right Compose, that is. */
208 [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */
209 [0xaf] = KEY_LEFTCTRL,
210 [0xb0] = KEY_CAPSLOCK,
211 [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */
212 [0xb2] = KEY_RIGHTALT,
213 [0xbc] = KEY_BACKSPACE,
214 [0xbd] = KEY_ENTER,
215 [0xbe] = KEY_TAB,
216 [0xbf] = KEY_ESC,
217 [0xc0] = KEY_1,
218 [0xc1] = KEY_Q,
219 [0xc2] = KEY_A,
220 [0xc3] = KEY_Z,
221 [0xc5] = KEY_2,
222 [0xc6] = KEY_W,
223 [0xc7] = KEY_S,
224 [0xc8] = KEY_X,
225 [0xc9] = KEY_102ND,
226 [0xcb] = KEY_3,
227 [0xcc] = KEY_E,
228 [0xcd] = KEY_D,
229 [0xce] = KEY_C,
230 [0xd0] = KEY_4,
231 [0xd1] = KEY_R,
232 [0xd2] = KEY_F,
233 [0xd3] = KEY_V,
234 [0xd4] = KEY_SPACE,
235 [0xd6] = KEY_5,
236 [0xd7] = KEY_T,
237 [0xd8] = KEY_G,
238 [0xd9] = KEY_B,
239 [0xdb] = KEY_6,
240 [0xdc] = KEY_Y,
241 [0xdd] = KEY_H,
242 [0xde] = KEY_N,
243 [0xe0] = KEY_7,
244 [0xe1] = KEY_U,
245 [0xe2] = KEY_J,
246 [0xe3] = KEY_M,
247 [0xe5] = KEY_8,
248 [0xe6] = KEY_I,
249 [0xe7] = KEY_K,
250 [0xe8] = KEY_COMMA,
251 [0xea] = KEY_9,
252 [0xeb] = KEY_O,
253 [0xec] = KEY_L,
254 [0xed] = KEY_DOT,
255 [0xef] = KEY_0,
256 [0xf0] = KEY_P,
257 [0xf2] = KEY_SEMICOLON,
258 [0xf3] = KEY_SLASH,
259 [0xf5] = KEY_EQUAL,
260 [0xf6] = KEY_RIGHTBRACE,
261 [0xf7] = KEY_BACKSLASH,
262 [0xf9] = KEY_MINUS,
263 [0xfa] = KEY_LEFTBRACE,
264 [0xfb] = KEY_APOSTROPHE,
265};
266
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500267#define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700268 if (test_bit(LED, (LK)->dev->led)) \
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500269 VAR_ON |= BITS; \
270 else \
271 VAR_OFF |= BITS; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 } while (0)
273
274/*
275 * Per-keyboard data
276 */
277struct lkkbd {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700278 unsigned short keycode[LK_NUM_KEYCODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int ignore_bytes;
280 unsigned char id[LK_NUM_IGNORE_BYTES];
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500281 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct serio *serio;
283 struct work_struct tq;
284 char name[64];
285 char phys[32];
286 char type;
287 int bell_volume;
288 int keyclick_volume;
289 int ctrlclick_volume;
290};
291
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500292#ifdef LKKBD_DEBUG
293/*
294 * Responses from the keyboard and mapping back to their names.
295 */
296static struct {
297 unsigned char value;
298 unsigned char *name;
299} lk_response[] = {
300#define RESPONSE(x) { .value = (x), .name = #x, }
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700301 RESPONSE(LK_STUCK_KEY),
302 RESPONSE(LK_SELFTEST_FAILED),
303 RESPONSE(LK_ALL_KEYS_UP),
304 RESPONSE(LK_METRONOME),
305 RESPONSE(LK_OUTPUT_ERROR),
306 RESPONSE(LK_INPUT_ERROR),
307 RESPONSE(LK_KBD_LOCKED),
308 RESPONSE(LK_KBD_TEST_MODE_ACK),
309 RESPONSE(LK_PREFIX_KEY_DOWN),
310 RESPONSE(LK_MODE_CHANGE_ACK),
311 RESPONSE(LK_RESPONSE_RESERVED),
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500312#undef RESPONSE
313};
314
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700315static unsigned char *response_name(unsigned char value)
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500316{
317 int i;
318
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700319 for (i = 0; i < ARRAY_SIZE(lk_response); i++)
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500320 if (lk_response[i].value == value)
321 return lk_response[i].name;
322
323 return "<unknown>";
324}
325#endif /* LKKBD_DEBUG */
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * Calculate volume parameter byte for a given volume.
329 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700330static unsigned char volume_to_hw(int volume_percent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 unsigned char ret = 0;
333
334 if (volume_percent < 0)
335 volume_percent = 0;
336 if (volume_percent > 100)
337 volume_percent = 100;
338
339 if (volume_percent >= 0)
340 ret = 7;
341 if (volume_percent >= 13) /* 12.5 */
342 ret = 6;
343 if (volume_percent >= 25)
344 ret = 5;
345 if (volume_percent >= 38) /* 37.5 */
346 ret = 4;
347 if (volume_percent >= 50)
348 ret = 3;
349 if (volume_percent >= 63) /* 62.5 */
350 ret = 2; /* This is the default volume */
351 if (volume_percent >= 75)
352 ret = 1;
353 if (volume_percent >= 88) /* 87.5 */
354 ret = 0;
355
356 ret |= 0x80;
357
358 return ret;
359}
360
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700361static void lkkbd_detection_done(struct lkkbd *lk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int i;
364
365 /*
366 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
367 */
368 lk->keycode[0xb1] = KEY_COMPOSE;
369
370 /*
371 * Print keyboard name and modify Compose=Alt on user's request.
372 */
373 switch (lk->id[4]) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700374 case 1:
375 strlcpy(lk->name, "DEC LK201 keyboard", sizeof(lk->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700377 if (lk201_compose_is_alt)
378 lk->keycode[0xb1] = KEY_LEFTALT;
379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700381 case 2:
382 strlcpy(lk->name, "DEC LK401 keyboard", sizeof(lk->name));
383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700385 default:
386 strlcpy(lk->name, "Unknown DEC keyboard", sizeof(lk->name));
387 printk(KERN_ERR
388 "lkkbd: keyboard on %s is unknown, please report to "
389 "Jan-Benedict Glaw <jbglaw@lug-owl.de>\n", lk->phys);
390 printk(KERN_ERR "lkkbd: keyboard ID'ed as:");
391 for (i = 0; i < LK_NUM_IGNORE_BYTES; i++)
392 printk(" 0x%02x", lk->id[i]);
393 printk("\n");
394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700396
397 printk(KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
398 lk->phys, lk->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
401 * Report errors during keyboard boot-up.
402 */
403 switch (lk->id[2]) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700404 case 0x00:
405 /* All okay */
406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700408 case LK_STUCK_KEY:
409 printk(KERN_ERR "lkkbd: Stuck key on keyboard at %s\n",
410 lk->phys);
411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700413 case LK_SELFTEST_FAILED:
414 printk(KERN_ERR
415 "lkkbd: Selftest failed on keyboard at %s, "
416 "keyboard may not work properly\n", lk->phys);
417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700419 default:
420 printk(KERN_ERR
421 "lkkbd: Unknown error %02x on keyboard at %s\n",
422 lk->id[2], lk->phys);
423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 /*
427 * Try to hint user if there's a stuck key.
428 */
429 if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700430 printk(KERN_ERR
431 "Scancode of stuck key is 0x%02x, keycode is 0x%04x\n",
432 lk->id[3], lk->keycode[lk->id[3]]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/*
436 * lkkbd_interrupt() is called by the low level driver when a character
437 * is received.
438 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700439static irqreturn_t lkkbd_interrupt(struct serio *serio,
440 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700442 struct lkkbd *lk = serio_get_drvdata(serio);
443 struct input_dev *input_dev = lk->dev;
444 unsigned int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int i;
446
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700447 DBG(KERN_INFO "Got byte 0x%02x\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (lk->ignore_bytes > 0) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700450 DBG(KERN_INFO "Ignoring a byte on %s\n", lk->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
452
453 if (lk->ignore_bytes == 0)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700454 lkkbd_detection_done(lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return IRQ_HANDLED;
457 }
458
459 switch (data) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700460 case LK_ALL_KEYS_UP:
461 for (i = 0; i < ARRAY_SIZE(lkkbd_keycode); i++)
462 input_report_key(input_dev, lk->keycode[i], 0);
463 input_sync(input_dev);
464 break;
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500465
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700466 case 0x01:
467 DBG(KERN_INFO "Got 0x01, scheduling re-initialization\n");
468 lk->ignore_bytes = LK_NUM_IGNORE_BYTES;
469 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
470 schedule_work(&lk->tq);
471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700473 case LK_METRONOME:
474 case LK_OUTPUT_ERROR:
475 case LK_INPUT_ERROR:
476 case LK_KBD_LOCKED:
477 case LK_KBD_TEST_MODE_ACK:
478 case LK_PREFIX_KEY_DOWN:
479 case LK_MODE_CHANGE_ACK:
480 case LK_RESPONSE_RESERVED:
481 DBG(KERN_INFO "Got %s and don't know how to handle...\n",
482 response_name(data));
483 break;
Jan-Benedict Glaw0aeafa72005-11-02 22:53:11 -0500484
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700485 default:
486 keycode = lk->keycode[data];
487 if (keycode != KEY_RESERVED) {
488 input_report_key(input_dev, keycode,
489 !test_bit(keycode, input_dev->key));
490 input_sync(input_dev);
491 } else {
492 printk(KERN_WARNING
493 "%s: Unknown key with scancode 0x%02x on %s.\n",
494 __FILE__, data, lk->name);
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
498 return IRQ_HANDLED;
499}
500
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700501static void lkkbd_toggle_leds(struct lkkbd *lk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700503 struct serio *serio = lk->serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned char leds_on = 0;
505 unsigned char leds_off = 0;
506
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700507 CHECK_LED(lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK);
508 CHECK_LED(lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE);
509 CHECK_LED(lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK);
510 CHECK_LED(lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT);
511 if (leds_on != 0) {
512 serio_write(serio, LK_CMD_LED_ON);
513 serio_write(serio, leds_on);
514 }
515 if (leds_off != 0) {
516 serio_write(serio, LK_CMD_LED_OFF);
517 serio_write(serio, leds_off);
518 }
519}
520
521static void lkkbd_toggle_keyclick(struct lkkbd *lk, bool on)
522{
523 struct serio *serio = lk->serio;
524
525 if (on) {
526 DBG("%s: Activating key clicks\n", __func__);
527 serio_write(serio, LK_CMD_ENABLE_KEYCLICK);
528 serio_write(serio, volume_to_hw(lk->keyclick_volume));
529 serio_write(serio, LK_CMD_ENABLE_CTRCLICK);
530 serio_write(serio, volume_to_hw(lk->ctrlclick_volume));
531 } else {
532 DBG("%s: Deactivating key clicks\n", __func__);
533 serio_write(serio, LK_CMD_DISABLE_KEYCLICK);
534 serio_write(serio, LK_CMD_DISABLE_CTRCLICK);
535 }
536
537}
538
539/*
540 * lkkbd_event() handles events from the input module.
541 */
542static int lkkbd_event(struct input_dev *dev,
543 unsigned int type, unsigned int code, int value)
544{
545 struct lkkbd *lk = input_get_drvdata(dev);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 switch (type) {
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700548 case EV_LED:
549 lkkbd_toggle_leds(lk);
550 return 0;
551
552 case EV_SND:
553 switch (code) {
554 case SND_CLICK:
555 lkkbd_toggle_keyclick(lk, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0;
557
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700558 case SND_BELL:
559 if (value != 0)
560 serio_write(lk->serio, LK_CMD_SOUND_BELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700562 return 0;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700567 default:
568 printk(KERN_ERR "%s(): Got unknown type %d, code %d, value %d\n",
569 __func__, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
572 return -1;
573}
574
575/*
576 * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
577 * were in.
578 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700579static void lkkbd_reinit(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
David Howellsc4028952006-11-22 14:57:56 +0000581 struct lkkbd *lk = container_of(work, struct lkkbd, tq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int division;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* Ask for ID */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700585 serio_write(lk->serio, LK_CMD_REQUEST_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* Reset parameters */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700588 serio_write(lk->serio, LK_CMD_SET_DEFAULTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* Set LEDs */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700591 lkkbd_toggle_leds(lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /*
594 * Try to activate extended LK401 mode. This command will
595 * only work with a LK401 keyboard and grants access to
596 * LAlt, RAlt, RCompose and RShift.
597 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700598 serio_write(lk->serio, LK_CMD_ENABLE_LK401);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Set all keys to UPDOWN mode */
601 for (division = 1; division <= 14; division++)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700602 serio_write(lk->serio,
603 LK_CMD_SET_MODE(LK_MODE_UPDOWN, division));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* Enable bell and set volume */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700606 serio_write(lk->serio, LK_CMD_ENABLE_BELL);
607 serio_write(lk->serio, volume_to_hw(lk->bell_volume));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Enable/disable keyclick (and possibly set volume) */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700610 lkkbd_toggle_keyclick(lk, test_bit(SND_CLICK, lk->dev->snd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* Sound the bell if needed */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700613 if (test_bit(SND_BELL, lk->dev->snd))
614 serio_write(lk->serio, LK_CMD_SOUND_BELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
618 * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
619 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700620static int lkkbd_connect(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct lkkbd *lk;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500623 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int i;
625 int err;
626
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700627 lk = kzalloc(sizeof(struct lkkbd), GFP_KERNEL);
628 input_dev = input_allocate_device();
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500629 if (!lk || !input_dev) {
630 err = -ENOMEM;
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500631 goto fail1;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 lk->serio = serio;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500635 lk->dev = input_dev;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700636 INIT_WORK(&lk->tq, lkkbd_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 lk->bell_volume = bell_volume;
638 lk->keyclick_volume = keyclick_volume;
639 lk->ctrlclick_volume = ctrlclick_volume;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700640 memcpy(lk->keycode, lkkbd_keycode, sizeof(lk->keycode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700642 strlcpy(lk->name, "DEC LK keyboard", sizeof(lk->name));
643 snprintf(lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500645 input_dev->name = lk->name;
646 input_dev->phys = lk->phys;
647 input_dev->id.bustype = BUS_RS232;
648 input_dev->id.vendor = SERIO_LKKBD;
649 input_dev->id.product = 0;
650 input_dev->id.version = 0x0100;
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400651 input_dev->dev.parent = &serio->dev;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500652 input_dev->event = lkkbd_event;
Dmitry Torokhovb3568722007-04-12 01:34:20 -0400653
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700654 input_set_drvdata(input_dev, lk);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500655
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700656 __set_bit(EV_KEY, input_dev->evbit);
657 __set_bit(EV_LED, input_dev->evbit);
658 __set_bit(EV_SND, input_dev->evbit);
659 __set_bit(EV_REP, input_dev->evbit);
660 __set_bit(LED_CAPSL, input_dev->ledbit);
661 __set_bit(LED_SLEEP, input_dev->ledbit);
662 __set_bit(LED_COMPOSE, input_dev->ledbit);
663 __set_bit(LED_SCROLLL, input_dev->ledbit);
664 __set_bit(SND_BELL, input_dev->sndbit);
665 __set_bit(SND_CLICK, input_dev->sndbit);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500666
667 input_dev->keycode = lk->keycode;
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700668 input_dev->keycodesize = sizeof(lk->keycode[0]);
669 input_dev->keycodemax = ARRAY_SIZE(lk->keycode);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700670
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500671 for (i = 0; i < LK_NUM_KEYCODES; i++)
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700672 __set_bit(lk->keycode[i], input_dev->keybit);
Dmitry Torokhovdd0d5442009-08-05 00:30:26 -0700673 __clear_bit(KEY_RESERVED, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700675 serio_set_drvdata(serio, lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700677 err = serio_open(serio, drv);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500678 if (err)
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500679 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700681 err = input_register_device(lk->dev);
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500682 if (err)
683 goto fail3;
684
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700685 serio_write(lk->serio, LK_CMD_POWERCYCLE_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 return 0;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500688
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700689 fail3: serio_close(serio);
690 fail2: serio_set_drvdata(serio, NULL);
691 fail1: input_free_device(input_dev);
692 kfree(lk);
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*
697 * lkkbd_disconnect() unregisters and closes behind us.
698 */
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700699static void lkkbd_disconnect(struct serio *serio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700701 struct lkkbd *lk = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Dmitry Torokhov7d3fadd2009-10-13 23:36:00 -0700703 input_get_device(lk->dev);
704 input_unregister_device(lk->dev);
705 serio_close(serio);
706 serio_set_drvdata(serio, NULL);
707 input_put_device(lk->dev);
708 kfree(lk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static struct serio_device_id lkkbd_serio_ids[] = {
712 {
713 .type = SERIO_RS232,
714 .proto = SERIO_LKKBD,
715 .id = SERIO_ANY,
716 .extra = SERIO_ANY,
717 },
718 { 0 }
719};
720
721MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
722
723static struct serio_driver lkkbd_drv = {
724 .driver = {
725 .name = "lkkbd",
726 },
727 .description = DRIVER_DESC,
728 .id_table = lkkbd_serio_ids,
729 .connect = lkkbd_connect,
730 .disconnect = lkkbd_disconnect,
731 .interrupt = lkkbd_interrupt,
732};
733
Axel Lin65ac9f72012-04-03 23:50:17 -0700734module_serio_driver(lkkbd_drv);