6 * LK keyboard driver for Linux, based on sunkbd.c (C) by Vojtech Pavlik
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
14 * DISCLAIMER: This works for _me_. If you break anything by using the
15 * information given below, I will _not_ be liable!
17 * RJ10 pinout: To DE9: Or DB25:
18 * 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!!!
23 * Pin numbers for DE9 and DB25 are noted on the plug (quite small:). For
24 * RJ10, it's like this:
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:)
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).
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
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.
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.
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
64 #include <linux/delay.h>
65 #include <linux/slab.h>
66 #include <linux/module.h>
67 #include <linux/interrupt.h>
68 #include <linux/input.h>
69 #include <linux/serio.h>
70 #include <linux/workqueue.h>
72 #define DRIVER_DESC "LK keyboard driver"
75 MODULE_DESCRIPTION(DRIVER_DESC);
76 MODULE_LICENSE("GPL");
84 * Please notice that there's not yet an API to set these at runtime.
86 static int bell_volume = 100; /* % */
87 module_param(bell_volume, int, 0);
88 MODULE_PARM_DESC(bell_volume, "Bell volume (in %). default is 100%");
90 static int keyclick_volume = 100; /* % */
91 module_param(keyclick_volume, int, 0);
92 MODULE_PARM_DESC(keyclick_volume, "Keyclick volume (in %), default is 100%");
94 static int ctrlclick_volume = 100; /* % */
95 module_param(ctrlclick_volume, int, 0);
96 MODULE_PARM_DESC(ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
98 static int lk201_compose_is_alt;
99 module_param(lk201_compose_is_alt, int, 0);
100 MODULE_PARM_DESC(lk201_compose_is_alt,
101 "If set non-zero, LK201' Compose key will act as an Alt key");
107 #define DBG(x...) printk(x)
109 #define DBG(x...) do {} while (0)
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
121 #define LK_MODE_DOWN 0x80
122 #define LK_MODE_AUTODOWN 0x82
123 #define LK_MODE_UPDOWN 0x86
124 #define LK_CMD_SET_MODE(mode, div) ((mode) | ((div) << 3))
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
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
152 #define LK_NUM_KEYCODES 256
153 #define LK_NUM_IGNORE_BYTES 6
155 static unsigned short lkkbd_keycode[LK_NUM_KEYCODES] = {
181 [0x8f] = KEY_PAGEDOWN,
184 [0x95] = KEY_KPENTER,
191 [0x9c] = KEY_KPCOMMA,
195 [0xa0] = KEY_KPMINUS,
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,
256 [0xf2] = KEY_SEMICOLON,
259 [0xf6] = KEY_RIGHTBRACE,
260 [0xf7] = KEY_BACKSLASH,
262 [0xfa] = KEY_LEFTBRACE,
263 [0xfb] = KEY_APOSTROPHE,
266 #define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
267 if (test_bit(LED, (LK)->dev->led)) \
277 unsigned short keycode[LK_NUM_KEYCODES];
279 unsigned char id[LK_NUM_IGNORE_BYTES];
280 struct input_dev *dev;
282 struct work_struct tq;
288 int ctrlclick_volume;
293 * Responses from the keyboard and mapping back to their names.
299 #define RESPONSE(x) { .value = (x), .name = #x, }
300 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),
314 static unsigned char *response_name(unsigned char value)
318 for (i = 0; i < ARRAY_SIZE(lk_response); i++)
319 if (lk_response[i].value == value)
320 return lk_response[i].name;
324 #endif /* LKKBD_DEBUG */
327 * Calculate volume parameter byte for a given volume.
329 static unsigned char volume_to_hw(int volume_percent)
331 unsigned char ret = 0;
333 if (volume_percent < 0)
335 if (volume_percent > 100)
336 volume_percent = 100;
338 if (volume_percent >= 0)
340 if (volume_percent >= 13) /* 12.5 */
342 if (volume_percent >= 25)
344 if (volume_percent >= 38) /* 37.5 */
346 if (volume_percent >= 50)
348 if (volume_percent >= 63) /* 62.5 */
349 ret = 2; /* This is the default volume */
350 if (volume_percent >= 75)
352 if (volume_percent >= 88) /* 87.5 */
360 static void lkkbd_detection_done(struct lkkbd *lk)
365 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
367 lk->keycode[0xb1] = KEY_COMPOSE;
370 * Print keyboard name and modify Compose=Alt on user's request.
374 strlcpy(lk->name, "DEC LK201 keyboard", sizeof(lk->name));
376 if (lk201_compose_is_alt)
377 lk->keycode[0xb1] = KEY_LEFTALT;
381 strlcpy(lk->name, "DEC LK401 keyboard", sizeof(lk->name));
385 strlcpy(lk->name, "Unknown DEC keyboard", sizeof(lk->name));
387 "lkkbd: keyboard on %s is unknown, please report to "
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]);
396 printk(KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
400 * Report errors during keyboard boot-up.
408 printk(KERN_ERR "lkkbd: Stuck key on keyboard at %s\n",
412 case LK_SELFTEST_FAILED:
414 "lkkbd: Selftest failed on keyboard at %s, "
415 "keyboard may not work properly\n", lk->phys);
420 "lkkbd: Unknown error %02x on keyboard at %s\n",
421 lk->id[2], lk->phys);
426 * Try to hint user if there's a stuck key.
428 if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
430 "Scancode of stuck key is 0x%02x, keycode is 0x%04x\n",
431 lk->id[3], lk->keycode[lk->id[3]]);
435 * lkkbd_interrupt() is called by the low level driver when a character
438 static irqreturn_t lkkbd_interrupt(struct serio *serio,
439 unsigned char data, unsigned int flags)
441 struct lkkbd *lk = serio_get_drvdata(serio);
442 struct input_dev *input_dev = lk->dev;
443 unsigned int keycode;
446 DBG(KERN_INFO "Got byte 0x%02x\n", data);
448 if (lk->ignore_bytes > 0) {
449 DBG(KERN_INFO "Ignoring a byte on %s\n", lk->name);
450 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
452 if (lk->ignore_bytes == 0)
453 lkkbd_detection_done(lk);
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);
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);
473 case LK_OUTPUT_ERROR:
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));
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);
492 "%s: Unknown key with scancode 0x%02x on %s.\n",
493 __FILE__, data, lk->name);
500 static void lkkbd_toggle_leds(struct lkkbd *lk)
502 struct serio *serio = lk->serio;
503 unsigned char leds_on = 0;
504 unsigned char leds_off = 0;
506 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);
511 serio_write(serio, LK_CMD_LED_ON);
512 serio_write(serio, leds_on);
515 serio_write(serio, LK_CMD_LED_OFF);
516 serio_write(serio, leds_off);
520 static void lkkbd_toggle_keyclick(struct lkkbd *lk, bool on)
522 struct serio *serio = lk->serio;
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));
531 DBG("%s: Deactivating key clicks\n", __func__);
532 serio_write(serio, LK_CMD_DISABLE_KEYCLICK);
533 serio_write(serio, LK_CMD_DISABLE_CTRCLICK);
539 * lkkbd_event() handles events from the input module.
541 static int lkkbd_event(struct input_dev *dev,
542 unsigned int type, unsigned int code, int value)
544 struct lkkbd *lk = input_get_drvdata(dev);
548 lkkbd_toggle_leds(lk);
554 lkkbd_toggle_keyclick(lk, value);
559 serio_write(lk->serio, LK_CMD_SOUND_BELL);
567 printk(KERN_ERR "%s(): Got unknown type %d, code %d, value %d\n",
568 __func__, type, code, value);
575 * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
578 static void lkkbd_reinit(struct work_struct *work)
580 struct lkkbd *lk = container_of(work, struct lkkbd, tq);
584 serio_write(lk->serio, LK_CMD_REQUEST_ID);
586 /* Reset parameters */
587 serio_write(lk->serio, LK_CMD_SET_DEFAULTS);
590 lkkbd_toggle_leds(lk);
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.
597 serio_write(lk->serio, LK_CMD_ENABLE_LK401);
599 /* Set all keys to UPDOWN mode */
600 for (division = 1; division <= 14; division++)
601 serio_write(lk->serio,
602 LK_CMD_SET_MODE(LK_MODE_UPDOWN, division));
604 /* Enable bell and set volume */
605 serio_write(lk->serio, LK_CMD_ENABLE_BELL);
606 serio_write(lk->serio, volume_to_hw(lk->bell_volume));
608 /* Enable/disable keyclick (and possibly set volume) */
609 lkkbd_toggle_keyclick(lk, test_bit(SND_CLICK, lk->dev->snd));
611 /* Sound the bell if needed */
612 if (test_bit(SND_BELL, lk->dev->snd))
613 serio_write(lk->serio, LK_CMD_SOUND_BELL);
617 * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
619 static int lkkbd_connect(struct serio *serio, struct serio_driver *drv)
622 struct input_dev *input_dev;
626 lk = kzalloc(sizeof(struct lkkbd), GFP_KERNEL);
627 input_dev = input_allocate_device();
628 if (!lk || !input_dev) {
635 INIT_WORK(&lk->tq, lkkbd_reinit);
636 lk->bell_volume = bell_volume;
637 lk->keyclick_volume = keyclick_volume;
638 lk->ctrlclick_volume = ctrlclick_volume;
639 memcpy(lk->keycode, lkkbd_keycode, sizeof(lk->keycode));
641 strlcpy(lk->name, "DEC LK keyboard", sizeof(lk->name));
642 snprintf(lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
644 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;
650 input_dev->dev.parent = &serio->dev;
651 input_dev->event = lkkbd_event;
653 input_set_drvdata(input_dev, lk);
655 __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);
666 input_dev->keycode = lk->keycode;
667 input_dev->keycodesize = sizeof(lk->keycode[0]);
668 input_dev->keycodemax = ARRAY_SIZE(lk->keycode);
670 for (i = 0; i < LK_NUM_KEYCODES; i++)
671 __set_bit(lk->keycode[i], input_dev->keybit);
672 __clear_bit(KEY_RESERVED, input_dev->keybit);
674 serio_set_drvdata(serio, lk);
676 err = serio_open(serio, drv);
680 err = input_register_device(lk->dev);
684 serio_write(lk->serio, LK_CMD_POWERCYCLE_RESET);
688 fail3: serio_close(serio);
689 fail2: serio_set_drvdata(serio, NULL);
690 fail1: input_free_device(input_dev);
696 * lkkbd_disconnect() unregisters and closes behind us.
698 static void lkkbd_disconnect(struct serio *serio)
700 struct lkkbd *lk = serio_get_drvdata(serio);
702 input_get_device(lk->dev);
703 input_unregister_device(lk->dev);
705 serio_set_drvdata(serio, NULL);
706 input_put_device(lk->dev);
710 static struct serio_device_id lkkbd_serio_ids[] = {
713 .proto = SERIO_LKKBD,
720 MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
722 static struct serio_driver lkkbd_drv = {
726 .description = DRIVER_DESC,
727 .id_table = lkkbd_serio_ids,
728 .connect = lkkbd_connect,
729 .disconnect = lkkbd_disconnect,
730 .interrupt = lkkbd_interrupt,
733 module_serio_driver(lkkbd_drv);