1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64#include <linux/delay.h>
65#include <linux/slab.h>
66#include <linux/module.h>
67#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
75MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
76MODULE_DESCRIPTION(DRIVER_DESC);
77MODULE_LICENSE("GPL");
78
79
80
81
82
83
84
85
86
87static int bell_volume = 100;
88module_param(bell_volume, int, 0);
89MODULE_PARM_DESC(bell_volume, "Bell volume (in %). default is 100%");
90
91static int keyclick_volume = 100;
92module_param(keyclick_volume, int, 0);
93MODULE_PARM_DESC(keyclick_volume, "Keyclick volume (in %), default is 100%");
94
95static int ctrlclick_volume = 100;
96module_param(ctrlclick_volume, int, 0);
97MODULE_PARM_DESC(ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
98
99static int lk201_compose_is_alt;
100module_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");
103
104
105
106#undef LKKBD_DEBUG
107#ifdef LKKBD_DEBUG
108#define DBG(x...) printk(x)
109#else
110#define DBG(x...) do {} while (0)
111#endif
112
113
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
122#define LK_MODE_DOWN 0x80
123#define LK_MODE_AUTODOWN 0x82
124#define LK_MODE_UPDOWN 0x86
125#define LK_CMD_SET_MODE(mode, div) ((mode) | ((div) << 3))
126
127
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
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
155
156static unsigned short lkkbd_keycode[LK_NUM_KEYCODES] = {
157 [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,
208 [0xae] = KEY_LEFTSHIFT,
209 [0xaf] = KEY_LEFTCTRL,
210 [0xb0] = KEY_CAPSLOCK,
211 [0xb1] = KEY_COMPOSE,
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
267#define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
268 if (test_bit(LED, (LK)->dev->led)) \
269 VAR_ON |= BITS; \
270 else \
271 VAR_OFF |= BITS; \
272 } while (0)
273
274
275
276
277struct lkkbd {
278 unsigned short keycode[LK_NUM_KEYCODES];
279 int ignore_bytes;
280 unsigned char id[LK_NUM_IGNORE_BYTES];
281 struct input_dev *dev;
282 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
292#ifdef LKKBD_DEBUG
293
294
295
296static struct {
297 unsigned char value;
298 unsigned char *name;
299} lk_response[] = {
300#define RESPONSE(x) { .value = (x), .name = #x, }
301 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),
312#undef RESPONSE
313};
314
315static unsigned char *response_name(unsigned char value)
316{
317 int i;
318
319 for (i = 0; i < ARRAY_SIZE(lk_response); i++)
320 if (lk_response[i].value == value)
321 return lk_response[i].name;
322
323 return "<unknown>";
324}
325#endif
326
327
328
329
330static unsigned char volume_to_hw(int volume_percent)
331{
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)
342 ret = 6;
343 if (volume_percent >= 25)
344 ret = 5;
345 if (volume_percent >= 38)
346 ret = 4;
347 if (volume_percent >= 50)
348 ret = 3;
349 if (volume_percent >= 63)
350 ret = 2;
351 if (volume_percent >= 75)
352 ret = 1;
353 if (volume_percent >= 88)
354 ret = 0;
355
356 ret |= 0x80;
357
358 return ret;
359}
360
361static void lkkbd_detection_done(struct lkkbd *lk)
362{
363 int i;
364
365
366
367
368 lk->keycode[0xb1] = KEY_COMPOSE;
369
370
371
372
373 switch (lk->id[4]) {
374 case 1:
375 strlcpy(lk->name, "DEC LK201 keyboard", sizeof(lk->name));
376
377 if (lk201_compose_is_alt)
378 lk->keycode[0xb1] = KEY_LEFTALT;
379 break;
380
381 case 2:
382 strlcpy(lk->name, "DEC LK401 keyboard", sizeof(lk->name));
383 break;
384
385 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;
395 }
396
397 printk(KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
398 lk->phys, lk->name);
399
400
401
402
403 switch (lk->id[2]) {
404 case 0x00:
405
406 break;
407
408 case LK_STUCK_KEY:
409 printk(KERN_ERR "lkkbd: Stuck key on keyboard at %s\n",
410 lk->phys);
411 break;
412
413 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;
418
419 default:
420 printk(KERN_ERR
421 "lkkbd: Unknown error %02x on keyboard at %s\n",
422 lk->id[2], lk->phys);
423 break;
424 }
425
426
427
428
429 if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
430 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]]);
433}
434
435
436
437
438
439static irqreturn_t lkkbd_interrupt(struct serio *serio,
440 unsigned char data, unsigned int flags)
441{
442 struct lkkbd *lk = serio_get_drvdata(serio);
443 struct input_dev *input_dev = lk->dev;
444 unsigned int keycode;
445 int i;
446
447 DBG(KERN_INFO "Got byte 0x%02x\n", data);
448
449 if (lk->ignore_bytes > 0) {
450 DBG(KERN_INFO "Ignoring a byte on %s\n", lk->name);
451 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
452
453 if (lk->ignore_bytes == 0)
454 lkkbd_detection_done(lk);
455
456 return IRQ_HANDLED;
457 }
458
459 switch (data) {
460 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;
465
466 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;
472
473 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;
484
485 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 }
496 }
497
498 return IRQ_HANDLED;
499}
500
501static void lkkbd_toggle_leds(struct lkkbd *lk)
502{
503 struct serio *serio = lk->serio;
504 unsigned char leds_on = 0;
505 unsigned char leds_off = 0;
506
507 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
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
547 switch (type) {
548 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);
556 return 0;
557
558 case SND_BELL:
559 if (value != 0)
560 serio_write(lk->serio, LK_CMD_SOUND_BELL);
561
562 return 0;
563 }
564
565 break;
566
567 default:
568 printk(KERN_ERR "%s(): Got unknown type %d, code %d, value %d\n",
569 __func__, type, code, value);
570 }
571
572 return -1;
573}
574
575
576
577
578
579static void lkkbd_reinit(struct work_struct *work)
580{
581 struct lkkbd *lk = container_of(work, struct lkkbd, tq);
582 int division;
583
584
585 serio_write(lk->serio, LK_CMD_REQUEST_ID);
586
587
588 serio_write(lk->serio, LK_CMD_SET_DEFAULTS);
589
590
591 lkkbd_toggle_leds(lk);
592
593
594
595
596
597
598 serio_write(lk->serio, LK_CMD_ENABLE_LK401);
599
600
601 for (division = 1; division <= 14; division++)
602 serio_write(lk->serio,
603 LK_CMD_SET_MODE(LK_MODE_UPDOWN, division));
604
605
606 serio_write(lk->serio, LK_CMD_ENABLE_BELL);
607 serio_write(lk->serio, volume_to_hw(lk->bell_volume));
608
609
610 lkkbd_toggle_keyclick(lk, test_bit(SND_CLICK, lk->dev->snd));
611
612
613 if (test_bit(SND_BELL, lk->dev->snd))
614 serio_write(lk->serio, LK_CMD_SOUND_BELL);
615}
616
617
618
619
620static int lkkbd_connect(struct serio *serio, struct serio_driver *drv)
621{
622 struct lkkbd *lk;
623 struct input_dev *input_dev;
624 int i;
625 int err;
626
627 lk = kzalloc(sizeof(struct lkkbd), GFP_KERNEL);
628 input_dev = input_allocate_device();
629 if (!lk || !input_dev) {
630 err = -ENOMEM;
631 goto fail1;
632 }
633
634 lk->serio = serio;
635 lk->dev = input_dev;
636 INIT_WORK(&lk->tq, lkkbd_reinit);
637 lk->bell_volume = bell_volume;
638 lk->keyclick_volume = keyclick_volume;
639 lk->ctrlclick_volume = ctrlclick_volume;
640 memcpy(lk->keycode, lkkbd_keycode, sizeof(lk->keycode));
641
642 strlcpy(lk->name, "DEC LK keyboard", sizeof(lk->name));
643 snprintf(lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
644
645 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;
651 input_dev->dev.parent = &serio->dev;
652 input_dev->event = lkkbd_event;
653
654 input_set_drvdata(input_dev, lk);
655
656 __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);
666
667 input_dev->keycode = lk->keycode;
668 input_dev->keycodesize = sizeof(lk->keycode[0]);
669 input_dev->keycodemax = ARRAY_SIZE(lk->keycode);
670
671 for (i = 0; i < LK_NUM_KEYCODES; i++)
672 __set_bit(lk->keycode[i], input_dev->keybit);
673 __clear_bit(KEY_RESERVED, input_dev->keybit);
674
675 serio_set_drvdata(serio, lk);
676
677 err = serio_open(serio, drv);
678 if (err)
679 goto fail2;
680
681 err = input_register_device(lk->dev);
682 if (err)
683 goto fail3;
684
685 serio_write(lk->serio, LK_CMD_POWERCYCLE_RESET);
686
687 return 0;
688
689 fail3: serio_close(serio);
690 fail2: serio_set_drvdata(serio, NULL);
691 fail1: input_free_device(input_dev);
692 kfree(lk);
693 return err;
694}
695
696
697
698
699static void lkkbd_disconnect(struct serio *serio)
700{
701 struct lkkbd *lk = serio_get_drvdata(serio);
702
703 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);
709}
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
734module_serio_driver(lkkbd_drv);
735