Linux 2.6.21.1
[linux/fpc-iii.git] / drivers / input / keyboard / lkkbd.c
blob3d4d0a0ede28333bb3bc7b12ae24bcd7bd116e2e
1 /*
2 * Copyright (C) 2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
3 */
5 /*
6 * LK keyboard driver for Linux, based on sunkbd.c (C) by Vojtech Pavlik
7 */
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).
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:)
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).
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
64 #include <linux/delay.h>
65 #include <linux/slab.h>
66 #include <linux/module.h>
67 #include <linux/moduleparam.h>
68 #include <linux/interrupt.h>
69 #include <linux/init.h>
70 #include <linux/input.h>
71 #include <linux/serio.h>
72 #include <linux/workqueue.h>
74 #define DRIVER_DESC "LK keyboard driver"
76 MODULE_AUTHOR ("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
77 MODULE_DESCRIPTION (DRIVER_DESC);
78 MODULE_LICENSE ("GPL");
81 * Known parameters:
82 * bell_volume
83 * keyclick_volume
84 * ctrlclick_volume
86 * Please notice that there's not yet an API to set these at runtime.
88 static int bell_volume = 100; /* % */
89 module_param (bell_volume, int, 0);
90 MODULE_PARM_DESC (bell_volume, "Bell volume (in %). default is 100%");
92 static int keyclick_volume = 100; /* % */
93 module_param (keyclick_volume, int, 0);
94 MODULE_PARM_DESC (keyclick_volume, "Keyclick volume (in %), default is 100%");
96 static int ctrlclick_volume = 100; /* % */
97 module_param (ctrlclick_volume, int, 0);
98 MODULE_PARM_DESC (ctrlclick_volume, "Ctrlclick volume (in %), default is 100%");
100 static int lk201_compose_is_alt;
101 module_param (lk201_compose_is_alt, int, 0);
102 MODULE_PARM_DESC (lk201_compose_is_alt, "If set non-zero, LK201' Compose key "
103 "will act as an Alt key");
107 #undef LKKBD_DEBUG
108 #ifdef LKKBD_DEBUG
109 #define DBG(x...) printk (x)
110 #else
111 #define DBG(x...) do {} while (0)
112 #endif
114 /* LED control */
115 #define LK_LED_WAIT 0x81
116 #define LK_LED_COMPOSE 0x82
117 #define LK_LED_SHIFTLOCK 0x84
118 #define LK_LED_SCROLLLOCK 0x88
119 #define LK_CMD_LED_ON 0x13
120 #define LK_CMD_LED_OFF 0x11
122 /* Mode control */
123 #define LK_MODE_DOWN 0x80
124 #define LK_MODE_AUTODOWN 0x82
125 #define LK_MODE_UPDOWN 0x86
126 #define LK_CMD_SET_MODE(mode,div) ((mode) | ((div) << 3))
128 /* Misc commands */
129 #define LK_CMD_ENABLE_KEYCLICK 0x1b
130 #define LK_CMD_DISABLE_KEYCLICK 0x99
131 #define LK_CMD_DISABLE_BELL 0xa1
132 #define LK_CMD_SOUND_BELL 0xa7
133 #define LK_CMD_ENABLE_BELL 0x23
134 #define LK_CMD_DISABLE_CTRCLICK 0xb9
135 #define LK_CMD_ENABLE_CTRCLICK 0xbb
136 #define LK_CMD_SET_DEFAULTS 0xd3
137 #define LK_CMD_POWERCYCLE_RESET 0xfd
138 #define LK_CMD_ENABLE_LK401 0xe9
139 #define LK_CMD_REQUEST_ID 0xab
141 /* Misc responses from keyboard */
142 #define LK_STUCK_KEY 0x3d
143 #define LK_SELFTEST_FAILED 0x3e
144 #define LK_ALL_KEYS_UP 0xb3
145 #define LK_METRONOME 0xb4
146 #define LK_OUTPUT_ERROR 0xb5
147 #define LK_INPUT_ERROR 0xb6
148 #define LK_KBD_LOCKED 0xb7
149 #define LK_KBD_TEST_MODE_ACK 0xb8
150 #define LK_PREFIX_KEY_DOWN 0xb9
151 #define LK_MODE_CHANGE_ACK 0xba
152 #define LK_RESPONSE_RESERVED 0xbb
154 #define LK_NUM_KEYCODES 256
155 #define LK_NUM_IGNORE_BYTES 6
156 typedef u_int16_t lk_keycode_t;
160 static lk_keycode_t lkkbd_keycode[LK_NUM_KEYCODES] = {
161 [0x56] = KEY_F1,
162 [0x57] = KEY_F2,
163 [0x58] = KEY_F3,
164 [0x59] = KEY_F4,
165 [0x5a] = KEY_F5,
166 [0x64] = KEY_F6,
167 [0x65] = KEY_F7,
168 [0x66] = KEY_F8,
169 [0x67] = KEY_F9,
170 [0x68] = KEY_F10,
171 [0x71] = KEY_F11,
172 [0x72] = KEY_F12,
173 [0x73] = KEY_F13,
174 [0x74] = KEY_F14,
175 [0x7c] = KEY_F15,
176 [0x7d] = KEY_F16,
177 [0x80] = KEY_F17,
178 [0x81] = KEY_F18,
179 [0x82] = KEY_F19,
180 [0x83] = KEY_F20,
181 [0x8a] = KEY_FIND,
182 [0x8b] = KEY_INSERT,
183 [0x8c] = KEY_DELETE,
184 [0x8d] = KEY_SELECT,
185 [0x8e] = KEY_PAGEUP,
186 [0x8f] = KEY_PAGEDOWN,
187 [0x92] = KEY_KP0,
188 [0x94] = KEY_KPDOT,
189 [0x95] = KEY_KPENTER,
190 [0x96] = KEY_KP1,
191 [0x97] = KEY_KP2,
192 [0x98] = KEY_KP3,
193 [0x99] = KEY_KP4,
194 [0x9a] = KEY_KP5,
195 [0x9b] = KEY_KP6,
196 [0x9c] = KEY_KPCOMMA,
197 [0x9d] = KEY_KP7,
198 [0x9e] = KEY_KP8,
199 [0x9f] = KEY_KP9,
200 [0xa0] = KEY_KPMINUS,
201 [0xa1] = KEY_PROG1,
202 [0xa2] = KEY_PROG2,
203 [0xa3] = KEY_PROG3,
204 [0xa4] = KEY_PROG4,
205 [0xa7] = KEY_LEFT,
206 [0xa8] = KEY_RIGHT,
207 [0xa9] = KEY_DOWN,
208 [0xaa] = KEY_UP,
209 [0xab] = KEY_RIGHTSHIFT,
210 [0xac] = KEY_LEFTALT,
211 [0xad] = KEY_COMPOSE, /* Right Compose, that is. */
212 [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */
213 [0xaf] = KEY_LEFTCTRL,
214 [0xb0] = KEY_CAPSLOCK,
215 [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */
216 [0xb2] = KEY_RIGHTALT,
217 [0xbc] = KEY_BACKSPACE,
218 [0xbd] = KEY_ENTER,
219 [0xbe] = KEY_TAB,
220 [0xbf] = KEY_ESC,
221 [0xc0] = KEY_1,
222 [0xc1] = KEY_Q,
223 [0xc2] = KEY_A,
224 [0xc3] = KEY_Z,
225 [0xc5] = KEY_2,
226 [0xc6] = KEY_W,
227 [0xc7] = KEY_S,
228 [0xc8] = KEY_X,
229 [0xc9] = KEY_102ND,
230 [0xcb] = KEY_3,
231 [0xcc] = KEY_E,
232 [0xcd] = KEY_D,
233 [0xce] = KEY_C,
234 [0xd0] = KEY_4,
235 [0xd1] = KEY_R,
236 [0xd2] = KEY_F,
237 [0xd3] = KEY_V,
238 [0xd4] = KEY_SPACE,
239 [0xd6] = KEY_5,
240 [0xd7] = KEY_T,
241 [0xd8] = KEY_G,
242 [0xd9] = KEY_B,
243 [0xdb] = KEY_6,
244 [0xdc] = KEY_Y,
245 [0xdd] = KEY_H,
246 [0xde] = KEY_N,
247 [0xe0] = KEY_7,
248 [0xe1] = KEY_U,
249 [0xe2] = KEY_J,
250 [0xe3] = KEY_M,
251 [0xe5] = KEY_8,
252 [0xe6] = KEY_I,
253 [0xe7] = KEY_K,
254 [0xe8] = KEY_COMMA,
255 [0xea] = KEY_9,
256 [0xeb] = KEY_O,
257 [0xec] = KEY_L,
258 [0xed] = KEY_DOT,
259 [0xef] = KEY_0,
260 [0xf0] = KEY_P,
261 [0xf2] = KEY_SEMICOLON,
262 [0xf3] = KEY_SLASH,
263 [0xf5] = KEY_EQUAL,
264 [0xf6] = KEY_RIGHTBRACE,
265 [0xf7] = KEY_BACKSLASH,
266 [0xf9] = KEY_MINUS,
267 [0xfa] = KEY_LEFTBRACE,
268 [0xfb] = KEY_APOSTROPHE,
271 #define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \
272 if (test_bit (LED, (LK)->dev->led)) \
273 VAR_ON |= BITS; \
274 else \
275 VAR_OFF |= BITS; \
276 } while (0)
279 * Per-keyboard data
281 struct lkkbd {
282 lk_keycode_t keycode[LK_NUM_KEYCODES];
283 int ignore_bytes;
284 unsigned char id[LK_NUM_IGNORE_BYTES];
285 struct input_dev *dev;
286 struct serio *serio;
287 struct work_struct tq;
288 char name[64];
289 char phys[32];
290 char type;
291 int bell_volume;
292 int keyclick_volume;
293 int ctrlclick_volume;
296 #ifdef LKKBD_DEBUG
298 * Responses from the keyboard and mapping back to their names.
300 static struct {
301 unsigned char value;
302 unsigned char *name;
303 } lk_response[] = {
304 #define RESPONSE(x) { .value = (x), .name = #x, }
305 RESPONSE (LK_STUCK_KEY),
306 RESPONSE (LK_SELFTEST_FAILED),
307 RESPONSE (LK_ALL_KEYS_UP),
308 RESPONSE (LK_METRONOME),
309 RESPONSE (LK_OUTPUT_ERROR),
310 RESPONSE (LK_INPUT_ERROR),
311 RESPONSE (LK_KBD_LOCKED),
312 RESPONSE (LK_KBD_TEST_MODE_ACK),
313 RESPONSE (LK_PREFIX_KEY_DOWN),
314 RESPONSE (LK_MODE_CHANGE_ACK),
315 RESPONSE (LK_RESPONSE_RESERVED),
316 #undef RESPONSE
319 static unsigned char *
320 response_name (unsigned char value)
322 int i;
324 for (i = 0; i < ARRAY_SIZE (lk_response); i++)
325 if (lk_response[i].value == value)
326 return lk_response[i].name;
328 return "<unknown>";
330 #endif /* LKKBD_DEBUG */
333 * Calculate volume parameter byte for a given volume.
335 static unsigned char
336 volume_to_hw (int volume_percent)
338 unsigned char ret = 0;
340 if (volume_percent < 0)
341 volume_percent = 0;
342 if (volume_percent > 100)
343 volume_percent = 100;
345 if (volume_percent >= 0)
346 ret = 7;
347 if (volume_percent >= 13) /* 12.5 */
348 ret = 6;
349 if (volume_percent >= 25)
350 ret = 5;
351 if (volume_percent >= 38) /* 37.5 */
352 ret = 4;
353 if (volume_percent >= 50)
354 ret = 3;
355 if (volume_percent >= 63) /* 62.5 */
356 ret = 2; /* This is the default volume */
357 if (volume_percent >= 75)
358 ret = 1;
359 if (volume_percent >= 88) /* 87.5 */
360 ret = 0;
362 ret |= 0x80;
364 return ret;
367 static void
368 lkkbd_detection_done (struct lkkbd *lk)
370 int i;
373 * Reset setting for Compose key. Let Compose be KEY_COMPOSE.
375 lk->keycode[0xb1] = KEY_COMPOSE;
378 * Print keyboard name and modify Compose=Alt on user's request.
380 switch (lk->id[4]) {
381 case 1:
382 strlcpy (lk->name, "DEC LK201 keyboard",
383 sizeof (lk->name));
385 if (lk201_compose_is_alt)
386 lk->keycode[0xb1] = KEY_LEFTALT;
387 break;
389 case 2:
390 strlcpy (lk->name, "DEC LK401 keyboard",
391 sizeof (lk->name));
392 break;
394 default:
395 strlcpy (lk->name, "Unknown DEC keyboard",
396 sizeof (lk->name));
397 printk (KERN_ERR "lkkbd: keyboard on %s is unknown, "
398 "please report to Jan-Benedict Glaw "
399 "<jbglaw@lug-owl.de>\n", lk->phys);
400 printk (KERN_ERR "lkkbd: keyboard ID'ed as:");
401 for (i = 0; i < LK_NUM_IGNORE_BYTES; i++)
402 printk (" 0x%02x", lk->id[i]);
403 printk ("\n");
404 break;
406 printk (KERN_INFO "lkkbd: keyboard on %s identified as: %s\n",
407 lk->phys, lk->name);
410 * Report errors during keyboard boot-up.
412 switch (lk->id[2]) {
413 case 0x00:
414 /* All okay */
415 break;
417 case LK_STUCK_KEY:
418 printk (KERN_ERR "lkkbd: Stuck key on keyboard at "
419 "%s\n", lk->phys);
420 break;
422 case LK_SELFTEST_FAILED:
423 printk (KERN_ERR "lkkbd: Selftest failed on keyboard "
424 "at %s, keyboard may not work "
425 "properly\n", lk->phys);
426 break;
428 default:
429 printk (KERN_ERR "lkkbd: Unknown error %02x on "
430 "keyboard at %s\n", lk->id[2],
431 lk->phys);
432 break;
436 * Try to hint user if there's a stuck key.
438 if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0)
439 printk (KERN_ERR "Scancode of stuck key is 0x%02x, keycode "
440 "is 0x%04x\n", lk->id[3],
441 lk->keycode[lk->id[3]]);
443 return;
447 * lkkbd_interrupt() is called by the low level driver when a character
448 * is received.
450 static irqreturn_t
451 lkkbd_interrupt (struct serio *serio, unsigned char data, unsigned int flags)
453 struct lkkbd *lk = serio_get_drvdata (serio);
454 int i;
456 DBG (KERN_INFO "Got byte 0x%02x\n", data);
458 if (lk->ignore_bytes > 0) {
459 DBG (KERN_INFO "Ignoring a byte on %s\n", lk->name);
460 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
462 if (lk->ignore_bytes == 0)
463 lkkbd_detection_done (lk);
465 return IRQ_HANDLED;
468 switch (data) {
469 case LK_ALL_KEYS_UP:
470 for (i = 0; i < ARRAY_SIZE (lkkbd_keycode); i++)
471 if (lk->keycode[i] != KEY_RESERVED)
472 input_report_key (lk->dev, lk->keycode[i], 0);
473 input_sync (lk->dev);
474 break;
476 case 0x01:
477 DBG (KERN_INFO "Got 0x01, scheduling re-initialization\n");
478 lk->ignore_bytes = LK_NUM_IGNORE_BYTES;
479 lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data;
480 schedule_work (&lk->tq);
481 break;
483 case LK_METRONOME:
484 case LK_OUTPUT_ERROR:
485 case LK_INPUT_ERROR:
486 case LK_KBD_LOCKED:
487 case LK_KBD_TEST_MODE_ACK:
488 case LK_PREFIX_KEY_DOWN:
489 case LK_MODE_CHANGE_ACK:
490 case LK_RESPONSE_RESERVED:
491 DBG (KERN_INFO "Got %s and don't know how to handle...\n",
492 response_name (data));
493 break;
495 default:
496 if (lk->keycode[data] != KEY_RESERVED) {
497 if (!test_bit (lk->keycode[data], lk->dev->key))
498 input_report_key (lk->dev, lk->keycode[data], 1);
499 else
500 input_report_key (lk->dev, lk->keycode[data], 0);
501 input_sync (lk->dev);
502 } else
503 printk (KERN_WARNING "%s: Unknown key with "
504 "scancode 0x%02x on %s.\n",
505 __FILE__, data, lk->name);
508 return IRQ_HANDLED;
512 * lkkbd_event() handles events from the input module.
514 static int
515 lkkbd_event (struct input_dev *dev, unsigned int type, unsigned int code,
516 int value)
518 struct lkkbd *lk = dev->private;
519 unsigned char leds_on = 0;
520 unsigned char leds_off = 0;
522 switch (type) {
523 case EV_LED:
524 CHECK_LED (lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK);
525 CHECK_LED (lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE);
526 CHECK_LED (lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK);
527 CHECK_LED (lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT);
528 if (leds_on != 0) {
529 lk->serio->write (lk->serio, LK_CMD_LED_ON);
530 lk->serio->write (lk->serio, leds_on);
532 if (leds_off != 0) {
533 lk->serio->write (lk->serio, LK_CMD_LED_OFF);
534 lk->serio->write (lk->serio, leds_off);
536 return 0;
538 case EV_SND:
539 switch (code) {
540 case SND_CLICK:
541 if (value == 0) {
542 DBG ("%s: Deactivating key clicks\n", __FUNCTION__);
543 lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK);
544 lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK);
545 } else {
546 DBG ("%s: Activating key clicks\n", __FUNCTION__);
547 lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK);
548 lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume));
549 lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK);
550 lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume));
552 return 0;
554 case SND_BELL:
555 if (value != 0)
556 lk->serio->write (lk->serio, LK_CMD_SOUND_BELL);
558 return 0;
560 break;
562 default:
563 printk (KERN_ERR "%s (): Got unknown type %d, code %d, value %d\n",
564 __FUNCTION__, type, code, value);
567 return -1;
571 * lkkbd_reinit() sets leds and beeps to a state the computer remembers they
572 * were in.
574 static void
575 lkkbd_reinit (struct work_struct *work)
577 struct lkkbd *lk = container_of(work, struct lkkbd, tq);
578 int division;
579 unsigned char leds_on = 0;
580 unsigned char leds_off = 0;
582 /* Ask for ID */
583 lk->serio->write (lk->serio, LK_CMD_REQUEST_ID);
585 /* Reset parameters */
586 lk->serio->write (lk->serio, LK_CMD_SET_DEFAULTS);
588 /* Set LEDs */
589 CHECK_LED (lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK);
590 CHECK_LED (lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE);
591 CHECK_LED (lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK);
592 CHECK_LED (lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT);
593 if (leds_on != 0) {
594 lk->serio->write (lk->serio, LK_CMD_LED_ON);
595 lk->serio->write (lk->serio, leds_on);
597 if (leds_off != 0) {
598 lk->serio->write (lk->serio, LK_CMD_LED_OFF);
599 lk->serio->write (lk->serio, leds_off);
603 * Try to activate extended LK401 mode. This command will
604 * only work with a LK401 keyboard and grants access to
605 * LAlt, RAlt, RCompose and RShift.
607 lk->serio->write (lk->serio, LK_CMD_ENABLE_LK401);
609 /* Set all keys to UPDOWN mode */
610 for (division = 1; division <= 14; division++)
611 lk->serio->write (lk->serio, LK_CMD_SET_MODE (LK_MODE_UPDOWN,
612 division));
614 /* Enable bell and set volume */
615 lk->serio->write (lk->serio, LK_CMD_ENABLE_BELL);
616 lk->serio->write (lk->serio, volume_to_hw (lk->bell_volume));
618 /* Enable/disable keyclick (and possibly set volume) */
619 if (test_bit (SND_CLICK, lk->dev->snd)) {
620 lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK);
621 lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume));
622 lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK);
623 lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume));
624 } else {
625 lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK);
626 lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK);
629 /* Sound the bell if needed */
630 if (test_bit (SND_BELL, lk->dev->snd))
631 lk->serio->write (lk->serio, LK_CMD_SOUND_BELL);
635 * lkkbd_connect() probes for a LK keyboard and fills the necessary structures.
637 static int
638 lkkbd_connect (struct serio *serio, struct serio_driver *drv)
640 struct lkkbd *lk;
641 struct input_dev *input_dev;
642 int i;
643 int err;
645 lk = kzalloc (sizeof (struct lkkbd), GFP_KERNEL);
646 input_dev = input_allocate_device ();
647 if (!lk || !input_dev) {
648 err = -ENOMEM;
649 goto fail1;
652 lk->serio = serio;
653 lk->dev = input_dev;
654 INIT_WORK (&lk->tq, lkkbd_reinit);
655 lk->bell_volume = bell_volume;
656 lk->keyclick_volume = keyclick_volume;
657 lk->ctrlclick_volume = ctrlclick_volume;
658 memcpy (lk->keycode, lkkbd_keycode, sizeof (lk_keycode_t) * LK_NUM_KEYCODES);
660 strlcpy (lk->name, "DEC LK keyboard", sizeof(lk->name));
661 snprintf (lk->phys, sizeof(lk->phys), "%s/input0", serio->phys);
663 input_dev->name = lk->name;
664 input_dev->phys = lk->phys;
665 input_dev->id.bustype = BUS_RS232;
666 input_dev->id.vendor = SERIO_LKKBD;
667 input_dev->id.product = 0;
668 input_dev->id.version = 0x0100;
669 input_dev->cdev.dev = &serio->dev;
670 input_dev->event = lkkbd_event;
671 input_dev->private = lk;
673 set_bit (EV_KEY, input_dev->evbit);
674 set_bit (EV_LED, input_dev->evbit);
675 set_bit (EV_SND, input_dev->evbit);
676 set_bit (EV_REP, input_dev->evbit);
677 set_bit (LED_CAPSL, input_dev->ledbit);
678 set_bit (LED_SLEEP, input_dev->ledbit);
679 set_bit (LED_COMPOSE, input_dev->ledbit);
680 set_bit (LED_SCROLLL, input_dev->ledbit);
681 set_bit (SND_BELL, input_dev->sndbit);
682 set_bit (SND_CLICK, input_dev->sndbit);
684 input_dev->keycode = lk->keycode;
685 input_dev->keycodesize = sizeof (lk_keycode_t);
686 input_dev->keycodemax = LK_NUM_KEYCODES;
687 for (i = 0; i < LK_NUM_KEYCODES; i++)
688 set_bit (lk->keycode[i], input_dev->keybit);
690 serio_set_drvdata (serio, lk);
692 err = serio_open (serio, drv);
693 if (err)
694 goto fail2;
696 err = input_register_device (lk->dev);
697 if (err)
698 goto fail3;
700 lk->serio->write (lk->serio, LK_CMD_POWERCYCLE_RESET);
702 return 0;
704 fail3: serio_close (serio);
705 fail2: serio_set_drvdata (serio, NULL);
706 fail1: input_free_device (input_dev);
707 kfree (lk);
708 return err;
712 * lkkbd_disconnect() unregisters and closes behind us.
714 static void
715 lkkbd_disconnect (struct serio *serio)
717 struct lkkbd *lk = serio_get_drvdata (serio);
719 input_get_device (lk->dev);
720 input_unregister_device (lk->dev);
721 serio_close (serio);
722 serio_set_drvdata (serio, NULL);
723 input_put_device (lk->dev);
724 kfree (lk);
727 static struct serio_device_id lkkbd_serio_ids[] = {
729 .type = SERIO_RS232,
730 .proto = SERIO_LKKBD,
731 .id = SERIO_ANY,
732 .extra = SERIO_ANY,
734 { 0 }
737 MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids);
739 static struct serio_driver lkkbd_drv = {
740 .driver = {
741 .name = "lkkbd",
743 .description = DRIVER_DESC,
744 .id_table = lkkbd_serio_ids,
745 .connect = lkkbd_connect,
746 .disconnect = lkkbd_disconnect,
747 .interrupt = lkkbd_interrupt,
751 * The functions for insering/removing us as a module.
753 static int __init
754 lkkbd_init (void)
756 return serio_register_driver(&lkkbd_drv);
759 static void __exit
760 lkkbd_exit (void)
762 serio_unregister_driver(&lkkbd_drv);
765 module_init (lkkbd_init);
766 module_exit (lkkbd_exit);