xref: /openbmc/linux/drivers/input/keyboard/lkkbd.c (revision 791d3ef2)
1 /*
2  *  Copyright (C) 2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
3  */
4 
5 /*
6  * LK keyboard driver for Linux, based on sunkbd.c (C) by Vojtech Pavlik
7  */
8 
9 /*
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
12  * adaptor).
13  *
14  * DISCLAIMER: This works for _me_. If you break anything by using the
15  * information given below, I will _not_ be liable!
16  *
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!!!
22  *
23  * Pin numbers for DE9 and DB25 are noted on the plug (quite small:). For
24  * RJ10, it's like this:
25  *
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:)
30  *    |    ||
31  *    |____|/
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).
37  *
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
46  */
47 
48 /*
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.
53  *
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.
58  *
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
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/input.h>
69 #include <linux/serio.h>
70 #include <linux/workqueue.h>
71 
72 #define DRIVER_DESC	"LK keyboard driver"
73 
74 MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
75 MODULE_DESCRIPTION(DRIVER_DESC);
76 MODULE_LICENSE("GPL");
77 
78 /*
79  * Known parameters:
80  *	bell_volume
81  *	keyclick_volume
82  *	ctrlclick_volume
83  *
84  * Please notice that there's not yet an API to set these at runtime.
85  */
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%");
89 
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%");
93 
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%");
97 
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");
102 
103 
104 
105 #undef LKKBD_DEBUG
106 #ifdef LKKBD_DEBUG
107 #define DBG(x...) printk(x)
108 #else
109 #define DBG(x...) do {} while (0)
110 #endif
111 
112 /* LED control */
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
119 
120 /* Mode control */
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))
125 
126 /* Misc commands */
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
138 
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
151 
152 #define LK_NUM_KEYCODES		256
153 #define LK_NUM_IGNORE_BYTES	6
154 
155 static unsigned short lkkbd_keycode[LK_NUM_KEYCODES] = {
156 	[0x56] = KEY_F1,
157 	[0x57] = KEY_F2,
158 	[0x58] = KEY_F3,
159 	[0x59] = KEY_F4,
160 	[0x5a] = KEY_F5,
161 	[0x64] = KEY_F6,
162 	[0x65] = KEY_F7,
163 	[0x66] = KEY_F8,
164 	[0x67] = KEY_F9,
165 	[0x68] = KEY_F10,
166 	[0x71] = KEY_F11,
167 	[0x72] = KEY_F12,
168 	[0x73] = KEY_F13,
169 	[0x74] = KEY_F14,
170 	[0x7c] = KEY_F15,
171 	[0x7d] = KEY_F16,
172 	[0x80] = KEY_F17,
173 	[0x81] = KEY_F18,
174 	[0x82] = KEY_F19,
175 	[0x83] = KEY_F20,
176 	[0x8a] = KEY_FIND,
177 	[0x8b] = KEY_INSERT,
178 	[0x8c] = KEY_DELETE,
179 	[0x8d] = KEY_SELECT,
180 	[0x8e] = KEY_PAGEUP,
181 	[0x8f] = KEY_PAGEDOWN,
182 	[0x92] = KEY_KP0,
183 	[0x94] = KEY_KPDOT,
184 	[0x95] = KEY_KPENTER,
185 	[0x96] = KEY_KP1,
186 	[0x97] = KEY_KP2,
187 	[0x98] = KEY_KP3,
188 	[0x99] = KEY_KP4,
189 	[0x9a] = KEY_KP5,
190 	[0x9b] = KEY_KP6,
191 	[0x9c] = KEY_KPCOMMA,
192 	[0x9d] = KEY_KP7,
193 	[0x9e] = KEY_KP8,
194 	[0x9f] = KEY_KP9,
195 	[0xa0] = KEY_KPMINUS,
196 	[0xa1] = KEY_PROG1,
197 	[0xa2] = KEY_PROG2,
198 	[0xa3] = KEY_PROG3,
199 	[0xa4] = KEY_PROG4,
200 	[0xa7] = KEY_LEFT,
201 	[0xa8] = KEY_RIGHT,
202 	[0xa9] = KEY_DOWN,
203 	[0xaa] = KEY_UP,
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,
213 	[0xbd] = KEY_ENTER,
214 	[0xbe] = KEY_TAB,
215 	[0xbf] = KEY_ESC,
216 	[0xc0] = KEY_1,
217 	[0xc1] = KEY_Q,
218 	[0xc2] = KEY_A,
219 	[0xc3] = KEY_Z,
220 	[0xc5] = KEY_2,
221 	[0xc6] = KEY_W,
222 	[0xc7] = KEY_S,
223 	[0xc8] = KEY_X,
224 	[0xc9] = KEY_102ND,
225 	[0xcb] = KEY_3,
226 	[0xcc] = KEY_E,
227 	[0xcd] = KEY_D,
228 	[0xce] = KEY_C,
229 	[0xd0] = KEY_4,
230 	[0xd1] = KEY_R,
231 	[0xd2] = KEY_F,
232 	[0xd3] = KEY_V,
233 	[0xd4] = KEY_SPACE,
234 	[0xd6] = KEY_5,
235 	[0xd7] = KEY_T,
236 	[0xd8] = KEY_G,
237 	[0xd9] = KEY_B,
238 	[0xdb] = KEY_6,
239 	[0xdc] = KEY_Y,
240 	[0xdd] = KEY_H,
241 	[0xde] = KEY_N,
242 	[0xe0] = KEY_7,
243 	[0xe1] = KEY_U,
244 	[0xe2] = KEY_J,
245 	[0xe3] = KEY_M,
246 	[0xe5] = KEY_8,
247 	[0xe6] = KEY_I,
248 	[0xe7] = KEY_K,
249 	[0xe8] = KEY_COMMA,
250 	[0xea] = KEY_9,
251 	[0xeb] = KEY_O,
252 	[0xec] = KEY_L,
253 	[0xed] = KEY_DOT,
254 	[0xef] = KEY_0,
255 	[0xf0] = KEY_P,
256 	[0xf2] = KEY_SEMICOLON,
257 	[0xf3] = KEY_SLASH,
258 	[0xf5] = KEY_EQUAL,
259 	[0xf6] = KEY_RIGHTBRACE,
260 	[0xf7] = KEY_BACKSLASH,
261 	[0xf9] = KEY_MINUS,
262 	[0xfa] = KEY_LEFTBRACE,
263 	[0xfb] = KEY_APOSTROPHE,
264 };
265 
266 #define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do {		\
267 	if (test_bit(LED, (LK)->dev->led))			\
268 		VAR_ON |= BITS;					\
269 	else							\
270 		VAR_OFF |= BITS;				\
271 	} while (0)
272 
273 /*
274  * Per-keyboard data
275  */
276 struct lkkbd {
277 	unsigned short keycode[LK_NUM_KEYCODES];
278 	int ignore_bytes;
279 	unsigned char id[LK_NUM_IGNORE_BYTES];
280 	struct input_dev *dev;
281 	struct serio *serio;
282 	struct work_struct tq;
283 	char name[64];
284 	char phys[32];
285 	char type;
286 	int bell_volume;
287 	int keyclick_volume;
288 	int ctrlclick_volume;
289 };
290 
291 #ifdef LKKBD_DEBUG
292 /*
293  * Responses from the keyboard and mapping back to their names.
294  */
295 static struct {
296 	unsigned char value;
297 	unsigned char *name;
298 } lk_response[] = {
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),
311 #undef RESPONSE
312 };
313 
314 static unsigned char *response_name(unsigned char value)
315 {
316 	int i;
317 
318 	for (i = 0; i < ARRAY_SIZE(lk_response); i++)
319 		if (lk_response[i].value == value)
320 			return lk_response[i].name;
321 
322 	return "<unknown>";
323 }
324 #endif /* LKKBD_DEBUG */
325 
326 /*
327  * Calculate volume parameter byte for a given volume.
328  */
329 static unsigned char volume_to_hw(int volume_percent)
330 {
331 	unsigned char ret = 0;
332 
333 	if (volume_percent < 0)
334 		volume_percent = 0;
335 	if (volume_percent > 100)
336 		volume_percent = 100;
337 
338 	if (volume_percent >= 0)
339 		ret = 7;
340 	if (volume_percent >= 13)	/* 12.5 */
341 		ret = 6;
342 	if (volume_percent >= 25)
343 		ret = 5;
344 	if (volume_percent >= 38)	/* 37.5 */
345 		ret = 4;
346 	if (volume_percent >= 50)
347 		ret = 3;
348 	if (volume_percent >= 63)	/* 62.5 */
349 		ret = 2;		/* This is the default volume */
350 	if (volume_percent >= 75)
351 		ret = 1;
352 	if (volume_percent >= 88)	/* 87.5 */
353 		ret = 0;
354 
355 	ret |= 0x80;
356 
357 	return ret;
358 }
359 
360 static void lkkbd_detection_done(struct lkkbd *lk)
361 {
362 	int i;
363 
364 	/*
365 	 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
366 	 */
367 	lk->keycode[0xb1] = KEY_COMPOSE;
368 
369 	/*
370 	 * Print keyboard name and modify Compose=Alt on user's request.
371 	 */
372 	switch (lk->id[4]) {
373 	case 1:
374 		strlcpy(lk->name, "DEC LK201 keyboard", sizeof(lk->name));
375 
376 		if (lk201_compose_is_alt)
377 			lk->keycode[0xb1] = KEY_LEFTALT;
378 		break;
379 
380 	case 2:
381 		strlcpy(lk->name, "DEC LK401 keyboard", sizeof(lk->name));
382 		break;
383 
384 	default:
385 		strlcpy(lk->name, "Unknown DEC keyboard", sizeof(lk->name));
386 		printk(KERN_ERR
387 			"lkkbd: keyboard on %s is unknown, please report to "
388 			"Jan-Benedict Glaw <jbglaw@lug-owl.de>\n", lk->phys);
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]);
392 		printk("\n");
393 		break;
394 	}
395 
396 	printk(KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
397 		lk->phys, lk->name);
398 
399 	/*
400 	 * Report errors during keyboard boot-up.
401 	 */
402 	switch (lk->id[2]) {
403 	case 0x00:
404 		/* All okay */
405 		break;
406 
407 	case LK_STUCK_KEY:
408 		printk(KERN_ERR "lkkbd: Stuck key on keyboard at %s\n",
409 			lk->phys);
410 		break;
411 
412 	case LK_SELFTEST_FAILED:
413 		printk(KERN_ERR
414 			"lkkbd: Selftest failed on keyboard at %s, "
415 			"keyboard may not work properly\n", lk->phys);
416 		break;
417 
418 	default:
419 		printk(KERN_ERR
420 			"lkkbd: Unknown error %02x on keyboard at %s\n",
421 			lk->id[2], lk->phys);
422 		break;
423 	}
424 
425 	/*
426 	 * Try to hint user if there's a stuck key.
427 	 */
428 	if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
429 		printk(KERN_ERR
430 			"Scancode of stuck key is 0x%02x, keycode is 0x%04x\n",
431 			lk->id[3], lk->keycode[lk->id[3]]);
432 }
433 
434 /*
435  * lkkbd_interrupt() is called by the low level driver when a character
436  * is received.
437  */
438 static irqreturn_t lkkbd_interrupt(struct serio *serio,
439 				   unsigned char data, unsigned int flags)
440 {
441 	struct lkkbd *lk = serio_get_drvdata(serio);
442 	struct input_dev *input_dev = lk->dev;
443 	unsigned int keycode;
444 	int i;
445 
446 	DBG(KERN_INFO "Got byte 0x%02x\n", data);
447 
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;
451 
452 		if (lk->ignore_bytes == 0)
453 			lkkbd_detection_done(lk);
454 
455 		return IRQ_HANDLED;
456 	}
457 
458 	switch (data) {
459 	case LK_ALL_KEYS_UP:
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);
463 		break;
464 
465 	case 0x01:
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);
470 		break;
471 
472 	case LK_METRONOME:
473 	case LK_OUTPUT_ERROR:
474 	case LK_INPUT_ERROR:
475 	case LK_KBD_LOCKED:
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));
482 		break;
483 
484 	default:
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);
490 		} else {
491 			printk(KERN_WARNING
492 				"%s: Unknown key with scancode 0x%02x on %s.\n",
493 				__FILE__, data, lk->name);
494 		}
495 	}
496 
497 	return IRQ_HANDLED;
498 }
499 
500 static void lkkbd_toggle_leds(struct lkkbd *lk)
501 {
502 	struct serio *serio = lk->serio;
503 	unsigned char leds_on = 0;
504 	unsigned char leds_off = 0;
505 
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);
510 	if (leds_on != 0) {
511 		serio_write(serio, LK_CMD_LED_ON);
512 		serio_write(serio, leds_on);
513 	}
514 	if (leds_off != 0) {
515 		serio_write(serio, LK_CMD_LED_OFF);
516 		serio_write(serio, leds_off);
517 	}
518 }
519 
520 static void lkkbd_toggle_keyclick(struct lkkbd *lk, bool on)
521 {
522 	struct serio *serio = lk->serio;
523 
524 	if (on) {
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));
530 	} else {
531 		DBG("%s: Deactivating key clicks\n", __func__);
532 		serio_write(serio, LK_CMD_DISABLE_KEYCLICK);
533 		serio_write(serio, LK_CMD_DISABLE_CTRCLICK);
534 	}
535 
536 }
537 
538 /*
539  * lkkbd_event() handles events from the input module.
540  */
541 static int lkkbd_event(struct input_dev *dev,
542 			unsigned int type, unsigned int code, int value)
543 {
544 	struct lkkbd *lk = input_get_drvdata(dev);
545 
546 	switch (type) {
547 	case EV_LED:
548 		lkkbd_toggle_leds(lk);
549 		return 0;
550 
551 	case EV_SND:
552 		switch (code) {
553 		case SND_CLICK:
554 			lkkbd_toggle_keyclick(lk, value);
555 			return 0;
556 
557 		case SND_BELL:
558 			if (value != 0)
559 				serio_write(lk->serio, LK_CMD_SOUND_BELL);
560 
561 			return 0;
562 		}
563 
564 		break;
565 
566 	default:
567 		printk(KERN_ERR "%s(): Got unknown type %d, code %d, value %d\n",
568 			__func__, type, code, value);
569 	}
570 
571 	return -1;
572 }
573 
574 /*
575  * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
576  * were in.
577  */
578 static void lkkbd_reinit(struct work_struct *work)
579 {
580 	struct lkkbd *lk = container_of(work, struct lkkbd, tq);
581 	int division;
582 
583 	/* Ask for ID */
584 	serio_write(lk->serio, LK_CMD_REQUEST_ID);
585 
586 	/* Reset parameters */
587 	serio_write(lk->serio, LK_CMD_SET_DEFAULTS);
588 
589 	/* Set LEDs */
590 	lkkbd_toggle_leds(lk);
591 
592 	/*
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.
596 	 */
597 	serio_write(lk->serio, LK_CMD_ENABLE_LK401);
598 
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));
603 
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));
607 
608 	/* Enable/disable keyclick (and possibly set volume) */
609 	lkkbd_toggle_keyclick(lk, test_bit(SND_CLICK, lk->dev->snd));
610 
611 	/* Sound the bell if needed */
612 	if (test_bit(SND_BELL, lk->dev->snd))
613 		serio_write(lk->serio, LK_CMD_SOUND_BELL);
614 }
615 
616 /*
617  * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
618  */
619 static int lkkbd_connect(struct serio *serio, struct serio_driver *drv)
620 {
621 	struct lkkbd *lk;
622 	struct input_dev *input_dev;
623 	int i;
624 	int err;
625 
626 	lk = kzalloc(sizeof(struct lkkbd), GFP_KERNEL);
627 	input_dev = input_allocate_device();
628 	if (!lk || !input_dev) {
629 		err = -ENOMEM;
630 		goto fail1;
631 	}
632 
633 	lk->serio = serio;
634 	lk->dev = 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));
640 
641 	strlcpy(lk->name, "DEC LK keyboard", sizeof(lk->name));
642 	snprintf(lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
643 
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;
652 
653 	input_set_drvdata(input_dev, lk);
654 
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);
665 
666 	input_dev->keycode = lk->keycode;
667 	input_dev->keycodesize = sizeof(lk->keycode[0]);
668 	input_dev->keycodemax = ARRAY_SIZE(lk->keycode);
669 
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);
673 
674 	serio_set_drvdata(serio, lk);
675 
676 	err = serio_open(serio, drv);
677 	if (err)
678 		goto fail2;
679 
680 	err = input_register_device(lk->dev);
681 	if (err)
682 		goto fail3;
683 
684 	serio_write(lk->serio, LK_CMD_POWERCYCLE_RESET);
685 
686 	return 0;
687 
688  fail3:	serio_close(serio);
689  fail2:	serio_set_drvdata(serio, NULL);
690  fail1:	input_free_device(input_dev);
691 	kfree(lk);
692 	return err;
693 }
694 
695 /*
696  * lkkbd_disconnect() unregisters and closes behind us.
697  */
698 static void lkkbd_disconnect(struct serio *serio)
699 {
700 	struct lkkbd *lk = serio_get_drvdata(serio);
701 
702 	input_get_device(lk->dev);
703 	input_unregister_device(lk->dev);
704 	serio_close(serio);
705 	serio_set_drvdata(serio, NULL);
706 	input_put_device(lk->dev);
707 	kfree(lk);
708 }
709 
710 static const struct serio_device_id lkkbd_serio_ids[] = {
711 	{
712 		.type	= SERIO_RS232,
713 		.proto	= SERIO_LKKBD,
714 		.id	= SERIO_ANY,
715 		.extra	= SERIO_ANY,
716 	},
717 	{ 0 }
718 };
719 
720 MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
721 
722 static struct serio_driver lkkbd_drv = {
723 	.driver		= {
724 		.name	= "lkkbd",
725 	},
726 	.description	= DRIVER_DESC,
727 	.id_table	= lkkbd_serio_ids,
728 	.connect	= lkkbd_connect,
729 	.disconnect	= lkkbd_disconnect,
730 	.interrupt	= lkkbd_interrupt,
731 };
732 
733 module_serio_driver(lkkbd_drv);
734