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
49 #include <linux/delay.h>
50 #include <linux/slab.h>
51 #include <linux/module.h>
52 #include <linux/interrupt.h>
53 #include <linux/input.h>
54 #include <linux/serio.h>
55 #include <linux/workqueue.h>
57 #define DRIVER_DESC "LK keyboard driver"
59 MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
60 MODULE_DESCRIPTION(DRIVER_DESC
);
61 MODULE_LICENSE("GPL");
69 * Please notice that there's not yet an API to set these at runtime.
71 static int bell_volume
= 100; /* % */
72 module_param(bell_volume
, int, 0);
73 MODULE_PARM_DESC(bell_volume
, "Bell volume (in %). default is 100%");
75 static int keyclick_volume
= 100; /* % */
76 module_param(keyclick_volume
, int, 0);
77 MODULE_PARM_DESC(keyclick_volume
, "Keyclick volume (in %), default is 100%");
79 static int ctrlclick_volume
= 100; /* % */
80 module_param(ctrlclick_volume
, int, 0);
81 MODULE_PARM_DESC(ctrlclick_volume
, "Ctrlclick volume (in %), default is 100%");
83 static int lk201_compose_is_alt
;
84 module_param(lk201_compose_is_alt
, int, 0);
85 MODULE_PARM_DESC(lk201_compose_is_alt
,
86 "If set non-zero, LK201' Compose key will act as an Alt key");
92 #define DBG(x...) printk(x)
94 #define DBG(x...) do {} while (0)
98 #define LK_LED_WAIT 0x81
99 #define LK_LED_COMPOSE 0x82
100 #define LK_LED_SHIFTLOCK 0x84
101 #define LK_LED_SCROLLLOCK 0x88
102 #define LK_CMD_LED_ON 0x13
103 #define LK_CMD_LED_OFF 0x11
106 #define LK_MODE_DOWN 0x80
107 #define LK_MODE_AUTODOWN 0x82
108 #define LK_MODE_UPDOWN 0x86
109 #define LK_CMD_SET_MODE(mode, div) ((mode) | ((div) << 3))
112 #define LK_CMD_ENABLE_KEYCLICK 0x1b
113 #define LK_CMD_DISABLE_KEYCLICK 0x99
114 #define LK_CMD_DISABLE_BELL 0xa1
115 #define LK_CMD_SOUND_BELL 0xa7
116 #define LK_CMD_ENABLE_BELL 0x23
117 #define LK_CMD_DISABLE_CTRCLICK 0xb9
118 #define LK_CMD_ENABLE_CTRCLICK 0xbb
119 #define LK_CMD_SET_DEFAULTS 0xd3
120 #define LK_CMD_POWERCYCLE_RESET 0xfd
121 #define LK_CMD_ENABLE_LK401 0xe9
122 #define LK_CMD_REQUEST_ID 0xab
124 /* Misc responses from keyboard */
125 #define LK_STUCK_KEY 0x3d
126 #define LK_SELFTEST_FAILED 0x3e
127 #define LK_ALL_KEYS_UP 0xb3
128 #define LK_METRONOME 0xb4
129 #define LK_OUTPUT_ERROR 0xb5
130 #define LK_INPUT_ERROR 0xb6
131 #define LK_KBD_LOCKED 0xb7
132 #define LK_KBD_TEST_MODE_ACK 0xb8
133 #define LK_PREFIX_KEY_DOWN 0xb9
134 #define LK_MODE_CHANGE_ACK 0xba
135 #define LK_RESPONSE_RESERVED 0xbb
137 #define LK_NUM_KEYCODES 256
138 #define LK_NUM_IGNORE_BYTES 6
140 static unsigned short lkkbd_keycode
[LK_NUM_KEYCODES
] = {
166 [0x8f] = KEY_PAGEDOWN
,
169 [0x95] = KEY_KPENTER
,
176 [0x9c] = KEY_KPCOMMA
,
180 [0xa0] = KEY_KPMINUS
,
189 [0xab] = KEY_RIGHTSHIFT
,
190 [0xac] = KEY_LEFTALT
,
191 [0xad] = KEY_COMPOSE
, /* Right Compose, that is. */
192 [0xae] = KEY_LEFTSHIFT
, /* Same as KEY_RIGHTSHIFT on LK201 */
193 [0xaf] = KEY_LEFTCTRL
,
194 [0xb0] = KEY_CAPSLOCK
,
195 [0xb1] = KEY_COMPOSE
, /* Left Compose, that is. */
196 [0xb2] = KEY_RIGHTALT
,
197 [0xbc] = KEY_BACKSPACE
,
241 [0xf2] = KEY_SEMICOLON
,
244 [0xf6] = KEY_RIGHTBRACE
,
245 [0xf7] = KEY_BACKSLASH
,
247 [0xfa] = KEY_LEFTBRACE
,
248 [0xfb] = KEY_APOSTROPHE
,
251 #define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
252 if (test_bit(LED, (LK)->dev->led)) \
262 unsigned short keycode
[LK_NUM_KEYCODES
];
264 unsigned char id
[LK_NUM_IGNORE_BYTES
];
265 struct input_dev
*dev
;
267 struct work_struct tq
;
273 int ctrlclick_volume
;
278 * Responses from the keyboard and mapping back to their names.
284 #define RESPONSE(x) { .value = (x), .name = #x, }
285 RESPONSE(LK_STUCK_KEY
),
286 RESPONSE(LK_SELFTEST_FAILED
),
287 RESPONSE(LK_ALL_KEYS_UP
),
288 RESPONSE(LK_METRONOME
),
289 RESPONSE(LK_OUTPUT_ERROR
),
290 RESPONSE(LK_INPUT_ERROR
),
291 RESPONSE(LK_KBD_LOCKED
),
292 RESPONSE(LK_KBD_TEST_MODE_ACK
),
293 RESPONSE(LK_PREFIX_KEY_DOWN
),
294 RESPONSE(LK_MODE_CHANGE_ACK
),
295 RESPONSE(LK_RESPONSE_RESERVED
),
299 static unsigned char *response_name(unsigned char value
)
303 for (i
= 0; i
< ARRAY_SIZE(lk_response
); i
++)
304 if (lk_response
[i
].value
== value
)
305 return lk_response
[i
].name
;
309 #endif /* LKKBD_DEBUG */
312 * Calculate volume parameter byte for a given volume.
314 static unsigned char volume_to_hw(int volume_percent
)
316 unsigned char ret
= 0;
318 if (volume_percent
< 0)
320 if (volume_percent
> 100)
321 volume_percent
= 100;
323 if (volume_percent
>= 0)
325 if (volume_percent
>= 13) /* 12.5 */
327 if (volume_percent
>= 25)
329 if (volume_percent
>= 38) /* 37.5 */
331 if (volume_percent
>= 50)
333 if (volume_percent
>= 63) /* 62.5 */
334 ret
= 2; /* This is the default volume */
335 if (volume_percent
>= 75)
337 if (volume_percent
>= 88) /* 87.5 */
345 static void lkkbd_detection_done(struct lkkbd
*lk
)
350 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
352 lk
->keycode
[0xb1] = KEY_COMPOSE
;
355 * Print keyboard name and modify Compose=Alt on user's request.
359 strscpy(lk
->name
, "DEC LK201 keyboard", sizeof(lk
->name
));
361 if (lk201_compose_is_alt
)
362 lk
->keycode
[0xb1] = KEY_LEFTALT
;
366 strscpy(lk
->name
, "DEC LK401 keyboard", sizeof(lk
->name
));
370 strscpy(lk
->name
, "Unknown DEC keyboard", sizeof(lk
->name
));
372 "lkkbd: keyboard on %s is unknown, please report to "
373 "Jan-Benedict Glaw <jbglaw@lug-owl.de>\n", lk
->phys
);
374 printk(KERN_ERR
"lkkbd: keyboard ID'ed as:");
375 for (i
= 0; i
< LK_NUM_IGNORE_BYTES
; i
++)
376 printk(" 0x%02x", lk
->id
[i
]);
381 printk(KERN_INFO
"lkkbd: keyboard on %s identified as: %s\n",
385 * Report errors during keyboard boot-up.
393 printk(KERN_ERR
"lkkbd: Stuck key on keyboard at %s\n",
397 case LK_SELFTEST_FAILED
:
399 "lkkbd: Selftest failed on keyboard at %s, "
400 "keyboard may not work properly\n", lk
->phys
);
405 "lkkbd: Unknown error %02x on keyboard at %s\n",
406 lk
->id
[2], lk
->phys
);
411 * Try to hint user if there's a stuck key.
413 if (lk
->id
[2] == LK_STUCK_KEY
&& lk
->id
[3] != 0)
415 "Scancode of stuck key is 0x%02x, keycode is 0x%04x\n",
416 lk
->id
[3], lk
->keycode
[lk
->id
[3]]);
420 * lkkbd_interrupt() is called by the low level driver when a character
423 static irqreturn_t
lkkbd_interrupt(struct serio
*serio
,
424 unsigned char data
, unsigned int flags
)
426 struct lkkbd
*lk
= serio_get_drvdata(serio
);
427 struct input_dev
*input_dev
= lk
->dev
;
428 unsigned int keycode
;
431 DBG(KERN_INFO
"Got byte 0x%02x\n", data
);
433 if (lk
->ignore_bytes
> 0) {
434 DBG(KERN_INFO
"Ignoring a byte on %s\n", lk
->name
);
435 lk
->id
[LK_NUM_IGNORE_BYTES
- lk
->ignore_bytes
--] = data
;
437 if (lk
->ignore_bytes
== 0)
438 lkkbd_detection_done(lk
);
445 for (i
= 0; i
< ARRAY_SIZE(lkkbd_keycode
); i
++)
446 input_report_key(input_dev
, lk
->keycode
[i
], 0);
447 input_sync(input_dev
);
451 DBG(KERN_INFO
"Got 0x01, scheduling re-initialization\n");
452 lk
->ignore_bytes
= LK_NUM_IGNORE_BYTES
;
453 lk
->id
[LK_NUM_IGNORE_BYTES
- lk
->ignore_bytes
--] = data
;
454 schedule_work(&lk
->tq
);
458 case LK_OUTPUT_ERROR
:
461 case LK_KBD_TEST_MODE_ACK
:
462 case LK_PREFIX_KEY_DOWN
:
463 case LK_MODE_CHANGE_ACK
:
464 case LK_RESPONSE_RESERVED
:
465 DBG(KERN_INFO
"Got %s and don't know how to handle...\n",
466 response_name(data
));
470 keycode
= lk
->keycode
[data
];
471 if (keycode
!= KEY_RESERVED
) {
472 input_report_key(input_dev
, keycode
,
473 !test_bit(keycode
, input_dev
->key
));
474 input_sync(input_dev
);
477 "%s: Unknown key with scancode 0x%02x on %s.\n",
478 __FILE__
, data
, lk
->name
);
485 static void lkkbd_toggle_leds(struct lkkbd
*lk
)
487 struct serio
*serio
= lk
->serio
;
488 unsigned char leds_on
= 0;
489 unsigned char leds_off
= 0;
491 CHECK_LED(lk
, leds_on
, leds_off
, LED_CAPSL
, LK_LED_SHIFTLOCK
);
492 CHECK_LED(lk
, leds_on
, leds_off
, LED_COMPOSE
, LK_LED_COMPOSE
);
493 CHECK_LED(lk
, leds_on
, leds_off
, LED_SCROLLL
, LK_LED_SCROLLLOCK
);
494 CHECK_LED(lk
, leds_on
, leds_off
, LED_SLEEP
, LK_LED_WAIT
);
496 serio_write(serio
, LK_CMD_LED_ON
);
497 serio_write(serio
, leds_on
);
500 serio_write(serio
, LK_CMD_LED_OFF
);
501 serio_write(serio
, leds_off
);
505 static void lkkbd_toggle_keyclick(struct lkkbd
*lk
, bool on
)
507 struct serio
*serio
= lk
->serio
;
510 DBG("%s: Activating key clicks\n", __func__
);
511 serio_write(serio
, LK_CMD_ENABLE_KEYCLICK
);
512 serio_write(serio
, volume_to_hw(lk
->keyclick_volume
));
513 serio_write(serio
, LK_CMD_ENABLE_CTRCLICK
);
514 serio_write(serio
, volume_to_hw(lk
->ctrlclick_volume
));
516 DBG("%s: Deactivating key clicks\n", __func__
);
517 serio_write(serio
, LK_CMD_DISABLE_KEYCLICK
);
518 serio_write(serio
, LK_CMD_DISABLE_CTRCLICK
);
524 * lkkbd_event() handles events from the input module.
526 static int lkkbd_event(struct input_dev
*dev
,
527 unsigned int type
, unsigned int code
, int value
)
529 struct lkkbd
*lk
= input_get_drvdata(dev
);
533 lkkbd_toggle_leds(lk
);
539 lkkbd_toggle_keyclick(lk
, value
);
544 serio_write(lk
->serio
, LK_CMD_SOUND_BELL
);
552 printk(KERN_ERR
"%s(): Got unknown type %d, code %d, value %d\n",
553 __func__
, type
, code
, value
);
560 * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
563 static void lkkbd_reinit(struct work_struct
*work
)
565 struct lkkbd
*lk
= container_of(work
, struct lkkbd
, tq
);
569 serio_write(lk
->serio
, LK_CMD_REQUEST_ID
);
571 /* Reset parameters */
572 serio_write(lk
->serio
, LK_CMD_SET_DEFAULTS
);
575 lkkbd_toggle_leds(lk
);
578 * Try to activate extended LK401 mode. This command will
579 * only work with a LK401 keyboard and grants access to
580 * LAlt, RAlt, RCompose and RShift.
582 serio_write(lk
->serio
, LK_CMD_ENABLE_LK401
);
584 /* Set all keys to UPDOWN mode */
585 for (division
= 1; division
<= 14; division
++)
586 serio_write(lk
->serio
,
587 LK_CMD_SET_MODE(LK_MODE_UPDOWN
, division
));
589 /* Enable bell and set volume */
590 serio_write(lk
->serio
, LK_CMD_ENABLE_BELL
);
591 serio_write(lk
->serio
, volume_to_hw(lk
->bell_volume
));
593 /* Enable/disable keyclick (and possibly set volume) */
594 lkkbd_toggle_keyclick(lk
, test_bit(SND_CLICK
, lk
->dev
->snd
));
596 /* Sound the bell if needed */
597 if (test_bit(SND_BELL
, lk
->dev
->snd
))
598 serio_write(lk
->serio
, LK_CMD_SOUND_BELL
);
602 * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
604 static int lkkbd_connect(struct serio
*serio
, struct serio_driver
*drv
)
607 struct input_dev
*input_dev
;
611 lk
= kzalloc(sizeof(*lk
), GFP_KERNEL
);
612 input_dev
= input_allocate_device();
613 if (!lk
|| !input_dev
) {
620 INIT_WORK(&lk
->tq
, lkkbd_reinit
);
621 lk
->bell_volume
= bell_volume
;
622 lk
->keyclick_volume
= keyclick_volume
;
623 lk
->ctrlclick_volume
= ctrlclick_volume
;
624 memcpy(lk
->keycode
, lkkbd_keycode
, sizeof(lk
->keycode
));
626 strscpy(lk
->name
, "DEC LK keyboard", sizeof(lk
->name
));
627 snprintf(lk
->phys
, sizeof(lk
->phys
), "%s/input0", serio
->phys
);
629 input_dev
->name
= lk
->name
;
630 input_dev
->phys
= lk
->phys
;
631 input_dev
->id
.bustype
= BUS_RS232
;
632 input_dev
->id
.vendor
= SERIO_LKKBD
;
633 input_dev
->id
.product
= 0;
634 input_dev
->id
.version
= 0x0100;
635 input_dev
->dev
.parent
= &serio
->dev
;
636 input_dev
->event
= lkkbd_event
;
638 input_set_drvdata(input_dev
, lk
);
640 __set_bit(EV_KEY
, input_dev
->evbit
);
641 __set_bit(EV_LED
, input_dev
->evbit
);
642 __set_bit(EV_SND
, input_dev
->evbit
);
643 __set_bit(EV_REP
, input_dev
->evbit
);
644 __set_bit(LED_CAPSL
, input_dev
->ledbit
);
645 __set_bit(LED_SLEEP
, input_dev
->ledbit
);
646 __set_bit(LED_COMPOSE
, input_dev
->ledbit
);
647 __set_bit(LED_SCROLLL
, input_dev
->ledbit
);
648 __set_bit(SND_BELL
, input_dev
->sndbit
);
649 __set_bit(SND_CLICK
, input_dev
->sndbit
);
651 input_dev
->keycode
= lk
->keycode
;
652 input_dev
->keycodesize
= sizeof(lk
->keycode
[0]);
653 input_dev
->keycodemax
= ARRAY_SIZE(lk
->keycode
);
655 for (i
= 0; i
< LK_NUM_KEYCODES
; i
++)
656 __set_bit(lk
->keycode
[i
], input_dev
->keybit
);
657 __clear_bit(KEY_RESERVED
, input_dev
->keybit
);
659 serio_set_drvdata(serio
, lk
);
661 err
= serio_open(serio
, drv
);
665 err
= input_register_device(lk
->dev
);
669 serio_write(lk
->serio
, LK_CMD_POWERCYCLE_RESET
);
673 fail3
: serio_close(serio
);
674 fail2
: serio_set_drvdata(serio
, NULL
);
675 fail1
: input_free_device(input_dev
);
681 * lkkbd_disconnect() unregisters and closes behind us.
683 static void lkkbd_disconnect(struct serio
*serio
)
685 struct lkkbd
*lk
= serio_get_drvdata(serio
);
687 input_get_device(lk
->dev
);
688 input_unregister_device(lk
->dev
);
690 serio_set_drvdata(serio
, NULL
);
691 input_put_device(lk
->dev
);
695 static const struct serio_device_id lkkbd_serio_ids
[] = {
698 .proto
= SERIO_LKKBD
,
705 MODULE_DEVICE_TABLE(serio
, lkkbd_serio_ids
);
707 static struct serio_driver lkkbd_drv
= {
711 .description
= DRIVER_DESC
,
712 .id_table
= lkkbd_serio_ids
,
713 .connect
= lkkbd_connect
,
714 .disconnect
= lkkbd_disconnect
,
715 .interrupt
= lkkbd_interrupt
,
718 module_serio_driver(lkkbd_drv
);