2 * Copyright (C) 2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
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
63 * Should you need to contact me, the author, you can do so either by
64 * email or by paper mail:
65 * Jan-Benedict Glaw, Lilienstraße 16, 33790 Hörste (near Halle/Westf.),
69 #include <linux/delay.h>
70 #include <linux/slab.h>
71 #include <linux/module.h>
72 #include <linux/moduleparam.h>
73 #include <linux/interrupt.h>
74 #include <linux/init.h>
75 #include <linux/input.h>
76 #include <linux/serio.h>
77 #include <linux/workqueue.h>
79 #define DRIVER_DESC "LK keyboard driver"
81 MODULE_AUTHOR ("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
82 MODULE_DESCRIPTION (DRIVER_DESC);
83 MODULE_LICENSE ("GPL");
91 * Please notice that there's not yet an API to set these at runtime.
93 static int bell_volume = 100; /* % */
94 module_param (bell_volume, int, 0);
95 MODULE_PARM_DESC (bell_volume, "Bell volume (in %). default is 100%");
97 static int keyclick_volume = 100; /* % */
98 module_param (keyclick_volume, int, 0);
99 MODULE_PARM_DESC (keyclick_volume, "Keyclick volume (in %), default is 100%");
101 static int ctrlclick_volume = 100; /* % */
102 module_param (ctrlclick_volume, int, 0);
103 MODULE_PARM_DESC (ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
105 static int lk201_compose_is_alt;
106 module_param (lk201_compose_is_alt, int, 0);
107 MODULE_PARM_DESC (lk201_compose_is_alt, "If set non-zero, LK201' Compose key "
108 "will act as an Alt key");
114 #define DBG(x...) printk (x)
116 #define DBG(x...) do {} while (0)
120 #define LK_LED_WAIT 0x81
121 #define LK_LED_COMPOSE 0x82
122 #define LK_LED_SHIFTLOCK 0x84
123 #define LK_LED_SCROLLLOCK 0x88
124 #define LK_CMD_LED_ON 0x13
125 #define LK_CMD_LED_OFF 0x11
128 #define LK_MODE_DOWN 0x80
129 #define LK_MODE_AUTODOWN 0x82
130 #define LK_MODE_UPDOWN 0x86
131 #define LK_CMD_SET_MODE(mode,div) ((mode) | ((div) << 3))
134 #define LK_CMD_ENABLE_KEYCLICK 0x1b
135 #define LK_CMD_DISABLE_KEYCLICK 0x99
136 #define LK_CMD_DISABLE_BELL 0xa1
137 #define LK_CMD_SOUND_BELL 0xa7
138 #define LK_CMD_ENABLE_BELL 0x23
139 #define LK_CMD_DISABLE_CTRCLICK 0xb9
140 #define LK_CMD_ENABLE_CTRCLICK 0xbb
141 #define LK_CMD_SET_DEFAULTS 0xd3
142 #define LK_CMD_POWERCYCLE_RESET 0xfd
143 #define LK_CMD_ENABLE_LK401 0xe9
144 #define LK_CMD_REQUEST_ID 0xab
146 /* Misc responses from keyboard */
147 #define LK_STUCK_KEY 0x3d
148 #define LK_SELFTEST_FAILED 0x3e
149 #define LK_ALL_KEYS_UP 0xb3
150 #define LK_METRONOME 0xb4
151 #define LK_OUTPUT_ERROR 0xb5
152 #define LK_INPUT_ERROR 0xb6
153 #define LK_KBD_LOCKED 0xb7
154 #define LK_KBD_TEST_MODE_ACK 0xb8
155 #define LK_PREFIX_KEY_DOWN 0xb9
156 #define LK_MODE_CHANGE_ACK 0xba
157 #define LK_RESPONSE_RESERVED 0xbb
159 #define LK_NUM_KEYCODES 256
160 #define LK_NUM_IGNORE_BYTES 6
161 typedef u_int16_t lk_keycode_t;
165 static lk_keycode_t lkkbd_keycode[LK_NUM_KEYCODES] = {
191 [0x8f] = KEY_PAGEDOWN,
194 [0x95] = KEY_KPENTER,
201 [0x9c] = KEY_KPCOMMA,
205 [0xa0] = KEY_KPMINUS,
214 [0xab] = KEY_RIGHTSHIFT,
215 [0xac] = KEY_LEFTALT,
216 [0xad] = KEY_COMPOSE, /* Right Compose, that is. */
217 [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */
218 [0xaf] = KEY_LEFTCTRL,
219 [0xb0] = KEY_CAPSLOCK,
220 [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */
221 [0xb2] = KEY_RIGHTALT,
222 [0xbc] = KEY_BACKSPACE,
266 [0xf2] = KEY_SEMICOLON,
269 [0xf6] = KEY_RIGHTBRACE,
270 [0xf7] = KEY_BACKSLASH,
272 [0xfa] = KEY_LEFTBRACE,
273 [0xfb] = KEY_APOSTROPHE,
276 #define CHECK_LED(LED, BITS) do { \
277 if (test_bit (LED, lk->dev->led)) \
287 lk_keycode_t keycode[LK_NUM_KEYCODES];
289 unsigned char id[LK_NUM_IGNORE_BYTES];
290 struct input_dev *dev;
292 struct work_struct tq;
298 int ctrlclick_volume;
302 * Calculate volume parameter byte for a given volume.
305 volume_to_hw (int volume_percent)
307 unsigned char ret = 0;
309 if (volume_percent < 0)
311 if (volume_percent > 100)
312 volume_percent = 100;
314 if (volume_percent >= 0)
316 if (volume_percent >= 13) /* 12.5 */
318 if (volume_percent >= 25)
320 if (volume_percent >= 38) /* 37.5 */
322 if (volume_percent >= 50)
324 if (volume_percent >= 63) /* 62.5 */
325 ret = 2; /* This is the default volume */
326 if (volume_percent >= 75)
328 if (volume_percent >= 88) /* 87.5 */
337 lkkbd_detection_done (struct lkkbd *lk)
342 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
344 lk->keycode[0xb1] = KEY_COMPOSE;
347 * Print keyboard name and modify Compose=Alt on user's request.
351 sprintf (lk->name, "DEC LK201 keyboard");
353 if (lk201_compose_is_alt)
354 lk->keycode[0xb1] = KEY_LEFTALT;
358 sprintf (lk->name, "DEC LK401 keyboard");
362 sprintf (lk->name, "Unknown DEC keyboard");
363 printk (KERN_ERR "lkkbd: keyboard on %s is unknown, "
364 "please report to Jan-Benedict Glaw "
365 "<jbglaw@lug-owl.de>\n", lk->phys);
366 printk (KERN_ERR "lkkbd: keyboard ID'ed as:");
367 for (i = 0; i < LK_NUM_IGNORE_BYTES; i++)
368 printk (" 0x%02x", lk->id[i]);
372 printk (KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
376 * Report errors during keyboard boot-up.
384 printk (KERN_ERR "lkkbd: Stuck key on keyboard at "
388 case LK_SELFTEST_FAILED:
389 printk (KERN_ERR "lkkbd: Selftest failed on keyboard "
390 "at %s, keyboard may not work "
391 "properly\n", lk->phys);
395 printk (KERN_ERR "lkkbd: Unknown error %02x on "
396 "keyboard at %s\n", lk->id[2],
402 * Try to hint user if there's a stuck key.
404 if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
405 printk (KERN_ERR "Scancode of stuck key is 0x%02x, keycode "
406 "is 0x%04x\n", lk->id[3],
407 lk->keycode[lk->id[3]]);
413 * lkkbd_interrupt() is called by the low level driver when a character
417 lkkbd_interrupt (struct serio *serio, unsigned char data, unsigned int flags,
418 struct pt_regs *regs)
420 struct lkkbd *lk = serio_get_drvdata (serio);
423 DBG (KERN_INFO "Got byte 0x%02x\n", data);
425 if (lk->ignore_bytes > 0) {
426 DBG (KERN_INFO "Ignoring a byte on %s\n", lk->name);
427 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
429 if (lk->ignore_bytes == 0)
430 lkkbd_detection_done (lk);
437 input_regs (lk->dev, regs);
438 for (i = 0; i < ARRAY_SIZE (lkkbd_keycode); i++)
439 if (lk->keycode[i] != KEY_RESERVED)
440 input_report_key (lk->dev, lk->keycode[i], 0);
441 input_sync (lk->dev);
444 DBG (KERN_INFO "Got LK_METRONOME and don't "
445 "know how to handle...\n");
447 case LK_OUTPUT_ERROR:
448 DBG (KERN_INFO "Got LK_OUTPUT_ERROR and don't "
449 "know how to handle...\n");
452 DBG (KERN_INFO "Got LK_INPUT_ERROR and don't "
453 "know how to handle...\n");
456 DBG (KERN_INFO "Got LK_KBD_LOCKED and don't "
457 "know how to handle...\n");
459 case LK_KBD_TEST_MODE_ACK:
460 DBG (KERN_INFO "Got LK_KBD_TEST_MODE_ACK and don't "
461 "know how to handle...\n");
463 case LK_PREFIX_KEY_DOWN:
464 DBG (KERN_INFO "Got LK_PREFIX_KEY_DOWN and don't "
465 "know how to handle...\n");
467 case LK_MODE_CHANGE_ACK:
468 DBG (KERN_INFO "Got LK_MODE_CHANGE_ACK and ignored "
471 case LK_RESPONSE_RESERVED:
472 DBG (KERN_INFO "Got LK_RESPONSE_RESERVED and don't "
473 "know how to handle...\n");
476 DBG (KERN_INFO "Got 0x01, scheduling re-initialization\n");
477 lk->ignore_bytes = LK_NUM_IGNORE_BYTES;
478 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
479 schedule_work (&lk->tq);
483 if (lk->keycode[data] != KEY_RESERVED) {
484 input_regs (lk->dev, regs);
485 if (!test_bit (lk->keycode[data], lk->dev->key))
486 input_report_key (lk->dev, lk->keycode[data], 1);
488 input_report_key (lk->dev, lk->keycode[data], 0);
489 input_sync (lk->dev);
491 printk (KERN_WARNING "%s: Unknown key with "
492 "scancode 0x%02x on %s.\n",
493 __FILE__, data, lk->name);
500 * lkkbd_event() handles events from the input module.
503 lkkbd_event (struct input_dev *dev, unsigned int type, unsigned int code,
506 struct lkkbd *lk = dev->private;
507 unsigned char leds_on = 0;
508 unsigned char leds_off = 0;
512 CHECK_LED (LED_CAPSL, LK_LED_SHIFTLOCK);
513 CHECK_LED (LED_COMPOSE, LK_LED_COMPOSE);
514 CHECK_LED (LED_SCROLLL, LK_LED_SCROLLLOCK);
515 CHECK_LED (LED_SLEEP, LK_LED_WAIT);
517 lk->serio->write (lk->serio, LK_CMD_LED_ON);
518 lk->serio->write (lk->serio, leds_on);
521 lk->serio->write (lk->serio, LK_CMD_LED_OFF);
522 lk->serio->write (lk->serio, leds_off);
530 DBG ("%s: Deactivating key clicks\n", __FUNCTION__);
531 lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK);
532 lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK);
534 DBG ("%s: Activating key clicks\n", __FUNCTION__);
535 lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK);
536 lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume));
537 lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK);
538 lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume));
544 lk->serio->write (lk->serio, LK_CMD_SOUND_BELL);
551 printk (KERN_ERR "%s (): Got unknown type %d, code %d, value %d\n",
552 __FUNCTION__, type, code, value);
559 * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
563 lkkbd_reinit (void *data)
565 struct lkkbd *lk = data;
567 unsigned char leds_on = 0;
568 unsigned char leds_off = 0;
571 lk->serio->write (lk->serio, LK_CMD_REQUEST_ID);
573 /* Reset parameters */
574 lk->serio->write (lk->serio, LK_CMD_SET_DEFAULTS);
577 CHECK_LED (LED_CAPSL, LK_LED_SHIFTLOCK);
578 CHECK_LED (LED_COMPOSE, LK_LED_COMPOSE);
579 CHECK_LED (LED_SCROLLL, LK_LED_SCROLLLOCK);
580 CHECK_LED (LED_SLEEP, LK_LED_WAIT);
582 lk->serio->write (lk->serio, LK_CMD_LED_ON);
583 lk->serio->write (lk->serio, leds_on);
586 lk->serio->write (lk->serio, LK_CMD_LED_OFF);
587 lk->serio->write (lk->serio, leds_off);
591 * Try to activate extended LK401 mode. This command will
592 * only work with a LK401 keyboard and grants access to
593 * LAlt, RAlt, RCompose and RShift.
595 lk->serio->write (lk->serio, LK_CMD_ENABLE_LK401);
597 /* Set all keys to UPDOWN mode */
598 for (division = 1; division <= 14; division++)
599 lk->serio->write (lk->serio, LK_CMD_SET_MODE (LK_MODE_UPDOWN,
602 /* Enable bell and set volume */
603 lk->serio->write (lk->serio, LK_CMD_ENABLE_BELL);
604 lk->serio->write (lk->serio, volume_to_hw (lk->bell_volume));
606 /* Enable/disable keyclick (and possibly set volume) */
607 if (test_bit (SND_CLICK, lk->dev->snd)) {
608 lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK);
609 lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume));
610 lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK);
611 lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume));
613 lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK);
614 lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK);
617 /* Sound the bell if needed */
618 if (test_bit (SND_BELL, lk->dev->snd))
619 lk->serio->write (lk->serio, LK_CMD_SOUND_BELL);
623 * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
626 lkkbd_connect (struct serio *serio, struct serio_driver *drv)
629 struct input_dev *input_dev;
633 lk = kzalloc (sizeof (struct lkkbd), GFP_KERNEL);
634 input_dev = input_allocate_device ();
635 if (!lk || !input_dev) {
642 INIT_WORK (&lk->tq, lkkbd_reinit, lk);
643 lk->bell_volume = bell_volume;
644 lk->keyclick_volume = keyclick_volume;
645 lk->ctrlclick_volume = ctrlclick_volume;
646 memcpy (lk->keycode, lkkbd_keycode, sizeof (lk_keycode_t) * LK_NUM_KEYCODES);
648 strlcpy (lk->name, "DEC LK keyboard", sizeof(lk->name));
649 snprintf (lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
651 input_dev->name = lk->name;
652 input_dev->phys = lk->phys;
653 input_dev->id.bustype = BUS_RS232;
654 input_dev->id.vendor = SERIO_LKKBD;
655 input_dev->id.product = 0;
656 input_dev->id.version = 0x0100;
657 input_dev->cdev.dev = &serio->dev;
658 input_dev->event = lkkbd_event;
659 input_dev->private = lk;
661 set_bit (EV_KEY, input_dev->evbit);
662 set_bit (EV_LED, input_dev->evbit);
663 set_bit (EV_SND, input_dev->evbit);
664 set_bit (EV_REP, input_dev->evbit);
665 set_bit (LED_CAPSL, input_dev->ledbit);
666 set_bit (LED_SLEEP, input_dev->ledbit);
667 set_bit (LED_COMPOSE, input_dev->ledbit);
668 set_bit (LED_SCROLLL, input_dev->ledbit);
669 set_bit (SND_BELL, input_dev->sndbit);
670 set_bit (SND_CLICK, input_dev->sndbit);
672 input_dev->keycode = lk->keycode;
673 input_dev->keycodesize = sizeof (lk_keycode_t);
674 input_dev->keycodemax = LK_NUM_KEYCODES;
675 for (i = 0; i < LK_NUM_KEYCODES; i++)
676 set_bit (lk->keycode[i], input_dev->keybit);
678 serio_set_drvdata (serio, lk);
680 err = serio_open (serio, drv);
684 input_register_device (lk->dev);
685 lk->serio->write (lk->serio, LK_CMD_POWERCYCLE_RESET);
689 fail: serio_set_drvdata (serio, NULL);
690 input_free_device (input_dev);
696 * lkkbd_disconnect() unregisters and closes behind us.
699 lkkbd_disconnect (struct serio *serio)
701 struct lkkbd *lk = serio_get_drvdata (serio);
703 input_get_device (lk->dev);
704 input_unregister_device (lk->dev);
706 serio_set_drvdata (serio, NULL);
707 input_put_device (lk->dev);
711 static struct serio_device_id lkkbd_serio_ids[] = {
714 .proto = SERIO_LKKBD,
721 MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
723 static struct serio_driver lkkbd_drv = {
727 .description = DRIVER_DESC,
728 .id_table = lkkbd_serio_ids,
729 .connect = lkkbd_connect,
730 .disconnect = lkkbd_disconnect,
731 .interrupt = lkkbd_interrupt,
735 * The functions for insering/removing us as a module.
740 serio_register_driver(&lkkbd_drv);
747 serio_unregister_driver(&lkkbd_drv);
750 module_init (lkkbd_init);
751 module_exit (lkkbd_exit);