1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
7 * LK keyboard driver for Linux, based on sunkbd.c (C) by Vojtech Pavlik
11 * DEC LK201 and LK401 keyboard driver for Linux (primary for DECstations
12 * and VAXstations, but can also be used on any standard RS232 with an
15 * DISCLAIMER: This works for _me_. If you break anything by using the
16 * information given below, I will _not_ be liable!
18 * RJ10 pinout: To DE9: Or DB25:
19 * 1 - RxD <----> Pin 3 (TxD) <-> Pin 2 (TxD)
20 * 2 - GND <----> Pin 5 (GND) <-> Pin 7 (GND)
21 * 4 - TxD <----> Pin 2 (RxD) <-> Pin 3 (RxD)
22 * 3 - +12V (from HDD drive connector), DON'T connect to DE9 or DB25!!!
24 * Pin numbers for DE9 and DB25 are noted on the plug (quite small:). For
25 * RJ10, it's like this:
27 * __=__ Hold the plug in front of you, cable downwards,
28 * /___/| nose is hidden behind the plug. Now, pin 1 is at
29 * |1234|| the left side, pin 4 at the right and 2 and 3 are
30 * |IIII|| in between, of course:)
33 * || So the adaptor consists of three connected cables
34 * || for data transmission (RxD and TxD) and signal ground.
35 * Additionally, you have to get +12V from somewhere.
36 * Most easily, you'll get that from a floppy or HDD power connector.
37 * It's the yellow cable there (black is ground and red is +5V).
39 * The keyboard and all the commands it understands are documented in
40 * "VCB02 Video Subsystem - Technical Manual", EK-104AA-TM-001. This
41 * document is LK201 specific, but LK401 is mostly compatible. It comes
42 * up in LK201 mode and doesn't report any of the additional keys it
43 * has. These need to be switched on with the LK_CMD_ENABLE_LK401
44 * command. You'll find this document (scanned .pdf file) on MANX,
45 * a search engine specific to DEC documentation. Try
46 * http://www.vt100.net/manx/details?pn=EK-104AA-TM-001;id=21;cp=1
52 #include <linux/delay.h>
53 #include <linux/slab.h>
54 #include <linux/module.h>
55 #include <linux/interrupt.h>
56 #include <linux/input.h>
57 #include <linux/serio.h>
58 #include <linux/workqueue.h>
60 #define DRIVER_DESC "LK keyboard driver"
62 MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
63 MODULE_DESCRIPTION(DRIVER_DESC);
64 MODULE_LICENSE("GPL");
72 * Please notice that there's not yet an API to set these at runtime.
74 static int bell_volume = 100; /* % */
75 module_param(bell_volume, int, 0);
76 MODULE_PARM_DESC(bell_volume, "Bell volume (in %). default is 100%");
78 static int keyclick_volume = 100; /* % */
79 module_param(keyclick_volume, int, 0);
80 MODULE_PARM_DESC(keyclick_volume, "Keyclick volume (in %), default is 100%");
82 static int ctrlclick_volume = 100; /* % */
83 module_param(ctrlclick_volume, int, 0);
84 MODULE_PARM_DESC(ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
86 static int lk201_compose_is_alt;
87 module_param(lk201_compose_is_alt, int, 0);
88 MODULE_PARM_DESC(lk201_compose_is_alt,
89 "If set non-zero, LK201' Compose key will act as an Alt key");
95 #define DBG(x...) printk(x)
97 #define DBG(x...) do {} while (0)
101 #define LK_LED_WAIT 0x81
102 #define LK_LED_COMPOSE 0x82
103 #define LK_LED_SHIFTLOCK 0x84
104 #define LK_LED_SCROLLLOCK 0x88
105 #define LK_CMD_LED_ON 0x13
106 #define LK_CMD_LED_OFF 0x11
109 #define LK_MODE_DOWN 0x80
110 #define LK_MODE_AUTODOWN 0x82
111 #define LK_MODE_UPDOWN 0x86
112 #define LK_CMD_SET_MODE(mode, div) ((mode) | ((div) << 3))
115 #define LK_CMD_ENABLE_KEYCLICK 0x1b
116 #define LK_CMD_DISABLE_KEYCLICK 0x99
117 #define LK_CMD_DISABLE_BELL 0xa1
118 #define LK_CMD_SOUND_BELL 0xa7
119 #define LK_CMD_ENABLE_BELL 0x23
120 #define LK_CMD_DISABLE_CTRCLICK 0xb9
121 #define LK_CMD_ENABLE_CTRCLICK 0xbb
122 #define LK_CMD_SET_DEFAULTS 0xd3
123 #define LK_CMD_POWERCYCLE_RESET 0xfd
124 #define LK_CMD_ENABLE_LK401 0xe9
125 #define LK_CMD_REQUEST_ID 0xab
127 /* Misc responses from keyboard */
128 #define LK_STUCK_KEY 0x3d
129 #define LK_SELFTEST_FAILED 0x3e
130 #define LK_ALL_KEYS_UP 0xb3
131 #define LK_METRONOME 0xb4
132 #define LK_OUTPUT_ERROR 0xb5
133 #define LK_INPUT_ERROR 0xb6
134 #define LK_KBD_LOCKED 0xb7
135 #define LK_KBD_TEST_MODE_ACK 0xb8
136 #define LK_PREFIX_KEY_DOWN 0xb9
137 #define LK_MODE_CHANGE_ACK 0xba
138 #define LK_RESPONSE_RESERVED 0xbb
140 #define LK_NUM_KEYCODES 256
141 #define LK_NUM_IGNORE_BYTES 6
143 static unsigned short lkkbd_keycode[LK_NUM_KEYCODES] = {
169 [0x8f] = KEY_PAGEDOWN,
172 [0x95] = KEY_KPENTER,
179 [0x9c] = KEY_KPCOMMA,
183 [0xa0] = KEY_KPMINUS,
192 [0xab] = KEY_RIGHTSHIFT,
193 [0xac] = KEY_LEFTALT,
194 [0xad] = KEY_COMPOSE, /* Right Compose, that is. */
195 [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */
196 [0xaf] = KEY_LEFTCTRL,
197 [0xb0] = KEY_CAPSLOCK,
198 [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */
199 [0xb2] = KEY_RIGHTALT,
200 [0xbc] = KEY_BACKSPACE,
244 [0xf2] = KEY_SEMICOLON,
247 [0xf6] = KEY_RIGHTBRACE,
248 [0xf7] = KEY_BACKSLASH,
250 [0xfa] = KEY_LEFTBRACE,
251 [0xfb] = KEY_APOSTROPHE,
254 #define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
255 if (test_bit(LED, (LK)->dev->led)) \
265 unsigned short keycode[LK_NUM_KEYCODES];
267 unsigned char id[LK_NUM_IGNORE_BYTES];
268 struct input_dev *dev;
270 struct work_struct tq;
276 int ctrlclick_volume;
281 * Responses from the keyboard and mapping back to their names.
287 #define RESPONSE(x) { .value = (x), .name = #x, }
288 RESPONSE(LK_STUCK_KEY),
289 RESPONSE(LK_SELFTEST_FAILED),
290 RESPONSE(LK_ALL_KEYS_UP),
291 RESPONSE(LK_METRONOME),
292 RESPONSE(LK_OUTPUT_ERROR),
293 RESPONSE(LK_INPUT_ERROR),
294 RESPONSE(LK_KBD_LOCKED),
295 RESPONSE(LK_KBD_TEST_MODE_ACK),
296 RESPONSE(LK_PREFIX_KEY_DOWN),
297 RESPONSE(LK_MODE_CHANGE_ACK),
298 RESPONSE(LK_RESPONSE_RESERVED),
302 static unsigned char *response_name(unsigned char value)
306 for (i = 0; i < ARRAY_SIZE(lk_response); i++)
307 if (lk_response[i].value == value)
308 return lk_response[i].name;
312 #endif /* LKKBD_DEBUG */
315 * Calculate volume parameter byte for a given volume.
317 static unsigned char volume_to_hw(int volume_percent)
319 unsigned char ret = 0;
321 if (volume_percent < 0)
323 if (volume_percent > 100)
324 volume_percent = 100;
326 if (volume_percent >= 0)
328 if (volume_percent >= 13) /* 12.5 */
330 if (volume_percent >= 25)
332 if (volume_percent >= 38) /* 37.5 */
334 if (volume_percent >= 50)
336 if (volume_percent >= 63) /* 62.5 */
337 ret = 2; /* This is the default volume */
338 if (volume_percent >= 75)
340 if (volume_percent >= 88) /* 87.5 */
348 static void lkkbd_detection_done(struct lkkbd *lk)
353 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
355 lk->keycode[0xb1] = KEY_COMPOSE;
358 * Print keyboard name and modify Compose=Alt on user's request.
362 strlcpy(lk->name, "DEC LK201 keyboard", sizeof(lk->name));
364 if (lk201_compose_is_alt)
365 lk->keycode[0xb1] = KEY_LEFTALT;
369 strlcpy(lk->name, "DEC LK401 keyboard", sizeof(lk->name));
373 strlcpy(lk->name, "Unknown DEC keyboard", sizeof(lk->name));
375 "lkkbd: keyboard on %s is unknown, please report to "
376 "Jan-Benedict Glaw <jbglaw@lug-owl.de>\n", lk->phys);
377 printk(KERN_ERR "lkkbd: keyboard ID'ed as:");
378 for (i = 0; i < LK_NUM_IGNORE_BYTES; i++)
379 printk(" 0x%02x", lk->id[i]);
384 printk(KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
388 * Report errors during keyboard boot-up.
396 printk(KERN_ERR "lkkbd: Stuck key on keyboard at %s\n",
400 case LK_SELFTEST_FAILED:
402 "lkkbd: Selftest failed on keyboard at %s, "
403 "keyboard may not work properly\n", lk->phys);
408 "lkkbd: Unknown error %02x on keyboard at %s\n",
409 lk->id[2], lk->phys);
414 * Try to hint user if there's a stuck key.
416 if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
418 "Scancode of stuck key is 0x%02x, keycode is 0x%04x\n",
419 lk->id[3], lk->keycode[lk->id[3]]);
423 * lkkbd_interrupt() is called by the low level driver when a character
426 static irqreturn_t lkkbd_interrupt(struct serio *serio,
427 unsigned char data, unsigned int flags)
429 struct lkkbd *lk = serio_get_drvdata(serio);
430 struct input_dev *input_dev = lk->dev;
431 unsigned int keycode;
434 DBG(KERN_INFO "Got byte 0x%02x\n", data);
436 if (lk->ignore_bytes > 0) {
437 DBG(KERN_INFO "Ignoring a byte on %s\n", lk->name);
438 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
440 if (lk->ignore_bytes == 0)
441 lkkbd_detection_done(lk);
448 for (i = 0; i < ARRAY_SIZE(lkkbd_keycode); i++)
449 input_report_key(input_dev, lk->keycode[i], 0);
450 input_sync(input_dev);
454 DBG(KERN_INFO "Got 0x01, scheduling re-initialization\n");
455 lk->ignore_bytes = LK_NUM_IGNORE_BYTES;
456 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
457 schedule_work(&lk->tq);
461 case LK_OUTPUT_ERROR:
464 case LK_KBD_TEST_MODE_ACK:
465 case LK_PREFIX_KEY_DOWN:
466 case LK_MODE_CHANGE_ACK:
467 case LK_RESPONSE_RESERVED:
468 DBG(KERN_INFO "Got %s and don't know how to handle...\n",
469 response_name(data));
473 keycode = lk->keycode[data];
474 if (keycode != KEY_RESERVED) {
475 input_report_key(input_dev, keycode,
476 !test_bit(keycode, input_dev->key));
477 input_sync(input_dev);
480 "%s: Unknown key with scancode 0x%02x on %s.\n",
481 __FILE__, data, lk->name);
488 static void lkkbd_toggle_leds(struct lkkbd *lk)
490 struct serio *serio = lk->serio;
491 unsigned char leds_on = 0;
492 unsigned char leds_off = 0;
494 CHECK_LED(lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK);
495 CHECK_LED(lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE);
496 CHECK_LED(lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK);
497 CHECK_LED(lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT);
499 serio_write(serio, LK_CMD_LED_ON);
500 serio_write(serio, leds_on);
503 serio_write(serio, LK_CMD_LED_OFF);
504 serio_write(serio, leds_off);
508 static void lkkbd_toggle_keyclick(struct lkkbd *lk, bool on)
510 struct serio *serio = lk->serio;
513 DBG("%s: Activating key clicks\n", __func__);
514 serio_write(serio, LK_CMD_ENABLE_KEYCLICK);
515 serio_write(serio, volume_to_hw(lk->keyclick_volume));
516 serio_write(serio, LK_CMD_ENABLE_CTRCLICK);
517 serio_write(serio, volume_to_hw(lk->ctrlclick_volume));
519 DBG("%s: Deactivating key clicks\n", __func__);
520 serio_write(serio, LK_CMD_DISABLE_KEYCLICK);
521 serio_write(serio, LK_CMD_DISABLE_CTRCLICK);
527 * lkkbd_event() handles events from the input module.
529 static int lkkbd_event(struct input_dev *dev,
530 unsigned int type, unsigned int code, int value)
532 struct lkkbd *lk = input_get_drvdata(dev);
536 lkkbd_toggle_leds(lk);
542 lkkbd_toggle_keyclick(lk, value);
547 serio_write(lk->serio, LK_CMD_SOUND_BELL);
555 printk(KERN_ERR "%s(): Got unknown type %d, code %d, value %d\n",
556 __func__, type, code, value);
563 * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
566 static void lkkbd_reinit(struct work_struct *work)
568 struct lkkbd *lk = container_of(work, struct lkkbd, tq);
572 serio_write(lk->serio, LK_CMD_REQUEST_ID);
574 /* Reset parameters */
575 serio_write(lk->serio, LK_CMD_SET_DEFAULTS);
578 lkkbd_toggle_leds(lk);
581 * Try to activate extended LK401 mode. This command will
582 * only work with a LK401 keyboard and grants access to
583 * LAlt, RAlt, RCompose and RShift.
585 serio_write(lk->serio, LK_CMD_ENABLE_LK401);
587 /* Set all keys to UPDOWN mode */
588 for (division = 1; division <= 14; division++)
589 serio_write(lk->serio,
590 LK_CMD_SET_MODE(LK_MODE_UPDOWN, division));
592 /* Enable bell and set volume */
593 serio_write(lk->serio, LK_CMD_ENABLE_BELL);
594 serio_write(lk->serio, volume_to_hw(lk->bell_volume));
596 /* Enable/disable keyclick (and possibly set volume) */
597 lkkbd_toggle_keyclick(lk, test_bit(SND_CLICK, lk->dev->snd));
599 /* Sound the bell if needed */
600 if (test_bit(SND_BELL, lk->dev->snd))
601 serio_write(lk->serio, LK_CMD_SOUND_BELL);
605 * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
607 static int lkkbd_connect(struct serio *serio, struct serio_driver *drv)
610 struct input_dev *input_dev;
614 lk = kzalloc(sizeof(struct lkkbd), GFP_KERNEL);
615 input_dev = input_allocate_device();
616 if (!lk || !input_dev) {
623 INIT_WORK(&lk->tq, lkkbd_reinit);
624 lk->bell_volume = bell_volume;
625 lk->keyclick_volume = keyclick_volume;
626 lk->ctrlclick_volume = ctrlclick_volume;
627 memcpy(lk->keycode, lkkbd_keycode, sizeof(lk->keycode));
629 strlcpy(lk->name, "DEC LK keyboard", sizeof(lk->name));
630 snprintf(lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
632 input_dev->name = lk->name;
633 input_dev->phys = lk->phys;
634 input_dev->id.bustype = BUS_RS232;
635 input_dev->id.vendor = SERIO_LKKBD;
636 input_dev->id.product = 0;
637 input_dev->id.version = 0x0100;
638 input_dev->dev.parent = &serio->dev;
639 input_dev->event = lkkbd_event;
641 input_set_drvdata(input_dev, lk);
643 __set_bit(EV_KEY, input_dev->evbit);
644 __set_bit(EV_LED, input_dev->evbit);
645 __set_bit(EV_SND, input_dev->evbit);
646 __set_bit(EV_REP, input_dev->evbit);
647 __set_bit(LED_CAPSL, input_dev->ledbit);
648 __set_bit(LED_SLEEP, input_dev->ledbit);
649 __set_bit(LED_COMPOSE, input_dev->ledbit);
650 __set_bit(LED_SCROLLL, input_dev->ledbit);
651 __set_bit(SND_BELL, input_dev->sndbit);
652 __set_bit(SND_CLICK, input_dev->sndbit);
654 input_dev->keycode = lk->keycode;
655 input_dev->keycodesize = sizeof(lk->keycode[0]);
656 input_dev->keycodemax = ARRAY_SIZE(lk->keycode);
658 for (i = 0; i < LK_NUM_KEYCODES; i++)
659 __set_bit(lk->keycode[i], input_dev->keybit);
660 __clear_bit(KEY_RESERVED, input_dev->keybit);
662 serio_set_drvdata(serio, lk);
664 err = serio_open(serio, drv);
668 err = input_register_device(lk->dev);
672 serio_write(lk->serio, LK_CMD_POWERCYCLE_RESET);
676 fail3: serio_close(serio);
677 fail2: serio_set_drvdata(serio, NULL);
678 fail1: input_free_device(input_dev);
684 * lkkbd_disconnect() unregisters and closes behind us.
686 static void lkkbd_disconnect(struct serio *serio)
688 struct lkkbd *lk = serio_get_drvdata(serio);
690 input_get_device(lk->dev);
691 input_unregister_device(lk->dev);
693 serio_set_drvdata(serio, NULL);
694 input_put_device(lk->dev);
698 static const struct serio_device_id lkkbd_serio_ids[] = {
701 .proto = SERIO_LKKBD,
708 MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
710 static struct serio_driver lkkbd_drv = {
714 .description = DRIVER_DESC,
715 .id_table = lkkbd_serio_ids,
716 .connect = lkkbd_connect,
717 .disconnect = lkkbd_disconnect,
718 .interrupt = lkkbd_interrupt,
721 module_serio_driver(lkkbd_drv);