2 * AT and PS/2 keyboard driver
4 * Copyright (c) 1999-2002 Vojtech Pavlik
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
14 * This driver can handle standard AT keyboards and PS/2 keyboards in
15 * Translated and Raw Set 2 and Set 3, as well as AT keyboards on dumb
16 * input-only controllers and AT keyboards connected over a one way RS232
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/input.h>
27 #include <linux/serio.h>
28 #include <linux/workqueue.h>
30 #define DRIVER_DESC "AT and PS/2 keyboard driver"
32 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
33 MODULE_DESCRIPTION(DRIVER_DESC);
34 MODULE_LICENSE("GPL");
36 static int atkbd_set = 2;
37 module_param_named(set, atkbd_set, int, 0);
38 MODULE_PARM_DESC(set, "Select keyboard code set (2 = default, 3 = PS/2 native)");
40 #if defined(__i386__) || defined(__x86_64__) || defined(__hppa__)
41 static int atkbd_reset;
43 static int atkbd_reset = 1;
45 module_param_named(reset, atkbd_reset, bool, 0);
46 MODULE_PARM_DESC(reset, "Reset keyboard during initialization");
48 static int atkbd_softrepeat;
49 module_param_named(softrepeat, atkbd_softrepeat, bool, 0);
50 MODULE_PARM_DESC(softrepeat, "Use software keyboard repeat");
52 static int atkbd_softraw = 1;
53 module_param_named(softraw, atkbd_softraw, bool, 0);
54 MODULE_PARM_DESC(softraw, "Use software generated rawmode");
56 static int atkbd_scroll;
57 module_param_named(scroll, atkbd_scroll, bool, 0);
58 MODULE_PARM_DESC(scroll, "Enable scroll-wheel on MS Office and similar keyboards");
60 static int atkbd_extra;
61 module_param_named(extra, atkbd_extra, bool, 0);
62 MODULE_PARM_DESC(extra, "Enable extra LEDs and keys on IBM RapidAcces, EzKey and similar keyboards");
64 __obsolete_setup("atkbd_set=");
65 __obsolete_setup("atkbd_reset");
66 __obsolete_setup("atkbd_softrepeat=");
69 * Scancode to keycode tables. These are just the default setting, and
70 * are loadable via an userland utility.
74 #include "hpps2atkbd.h"
77 static unsigned char atkbd_set2_keycode[512] = {
79 0, 67, 65, 63, 61, 59, 60, 88, 0, 68, 66, 64, 62, 15, 41,117,
80 0, 56, 42, 93, 29, 16, 2, 0, 0, 0, 44, 31, 30, 17, 3, 0,
81 0, 46, 45, 32, 18, 5, 4, 95, 0, 57, 47, 33, 20, 19, 6,183,
82 0, 49, 48, 35, 34, 21, 7,184, 0, 0, 50, 36, 22, 8, 9,185,
83 0, 51, 37, 23, 24, 11, 10, 0, 0, 52, 53, 38, 39, 25, 12, 0,
84 0, 89, 40, 0, 26, 13, 0, 0, 58, 54, 28, 27, 0, 43, 0, 85,
85 0, 86, 91, 90, 92, 0, 14, 94, 0, 79,124, 75, 71,121, 0, 0,
86 82, 83, 80, 76, 77, 72, 1, 69, 87, 78, 81, 74, 55, 73, 70, 99,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 217,100,255, 0, 97,165, 0, 0,156, 0, 0, 0, 0, 0, 0,125,
90 173,114, 0,113, 0, 0, 0,126,128, 0, 0,140, 0, 0, 0,127,
91 159, 0,115, 0,164, 0, 0,116,158, 0,150,166, 0, 0, 0,142,
92 157, 0, 0, 0, 0, 0, 0, 0,155, 0, 98, 0, 0,163, 0, 0,
93 226, 0, 0, 0, 0, 0, 0, 0, 0,255, 96, 0, 0, 0,143, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0,107, 0,105,102, 0, 0,112,
95 110,111,108,112,106,103, 0,119, 0,118,109, 0, 99,104,119, 0,
102 static unsigned char atkbd_set3_keycode[512] = {
104 0, 0, 0, 0, 0, 0, 0, 59, 1,138,128,129,130, 15, 41, 60,
105 131, 29, 42, 86, 58, 16, 2, 61,133, 56, 44, 31, 30, 17, 3, 62,
106 134, 46, 45, 32, 18, 5, 4, 63,135, 57, 47, 33, 20, 19, 6, 64,
107 136, 49, 48, 35, 34, 21, 7, 65,137,100, 50, 36, 22, 8, 9, 66,
108 125, 51, 37, 23, 24, 11, 10, 67,126, 52, 53, 38, 39, 25, 12, 68,
109 113,114, 40, 43, 26, 13, 87, 99, 97, 54, 28, 27, 43, 43, 88, 70,
110 108,105,119,103,111,107, 14,110, 0, 79,106, 75, 71,109,102,104,
111 82, 83, 80, 76, 77, 72, 69, 98, 0, 96, 81, 0, 78, 73, 55,183,
113 184,185,186,187, 74, 94, 92, 93, 0, 0, 0,125,126,127,112, 0,
114 0,139,150,163,165,115,152,150,166,140,160,154,113,114,167,168,
118 static unsigned char atkbd_unxlate_table[128] = {
119 0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13,
120 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
121 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
122 50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88, 5, 6, 4, 12, 3,
123 11, 2, 10, 1, 9,119,126,108,117,125,123,107,115,116,121,105,
124 114,122,112,113,127, 96, 97,120, 7, 15, 23, 31, 39, 47, 55, 63,
125 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111,
126 19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110
129 #define ATKBD_CMD_SETLEDS 0x10ed
130 #define ATKBD_CMD_GSCANSET 0x11f0
131 #define ATKBD_CMD_SSCANSET 0x10f0
132 #define ATKBD_CMD_GETID 0x02f2
133 #define ATKBD_CMD_SETREP 0x10f3
134 #define ATKBD_CMD_ENABLE 0x00f4
135 #define ATKBD_CMD_RESET_DIS 0x00f5
136 #define ATKBD_CMD_SETALL_MBR 0x00fa
137 #define ATKBD_CMD_RESET_BAT 0x02ff
138 #define ATKBD_CMD_RESEND 0x00fe
139 #define ATKBD_CMD_EX_ENABLE 0x10ea
140 #define ATKBD_CMD_EX_SETLEDS 0x20eb
141 #define ATKBD_CMD_OK_GETID 0x02e8
144 #define ATKBD_RET_ACK 0xfa
145 #define ATKBD_RET_NAK 0xfe
146 #define ATKBD_RET_BAT 0xaa
147 #define ATKBD_RET_EMUL0 0xe0
148 #define ATKBD_RET_EMUL1 0xe1
149 #define ATKBD_RET_RELEASE 0xf0
150 #define ATKBD_RET_HANGUEL 0xf1
151 #define ATKBD_RET_HANJA 0xf2
152 #define ATKBD_RET_ERR 0xff
154 #define ATKBD_KEY_UNKNOWN 0
155 #define ATKBD_KEY_NULL 255
157 #define ATKBD_SCR_1 254
158 #define ATKBD_SCR_2 253
159 #define ATKBD_SCR_4 252
160 #define ATKBD_SCR_8 251
161 #define ATKBD_SCR_CLICK 250
163 #define ATKBD_SPECIAL 250
165 static unsigned char atkbd_scroll_keys[5][2] = {
166 { ATKBD_SCR_1, 0x45 },
167 { ATKBD_SCR_2, 0x29 },
168 { ATKBD_SCR_4, 0x36 },
169 { ATKBD_SCR_8, 0x27 },
170 { ATKBD_SCR_CLICK, 0x60 },
173 #define ATKBD_FLAG_ACK 0 /* Waiting for ACK/NAK */
174 #define ATKBD_FLAG_CMD 1 /* Waiting for command to finish */
175 #define ATKBD_FLAG_CMD1 2 /* First byte of command response */
176 #define ATKBD_FLAG_ENABLED 3 /* Waining for init to finish */
179 * The atkbd control structure
184 /* Written only during init */
188 struct input_dev dev;
192 unsigned char keycode[512];
193 unsigned char translated;
197 /* Protected by FLAG_ACK */
200 /* Protected by FLAG_CMD */
201 unsigned char cmdbuf[4];
202 unsigned char cmdcnt;
204 /* Accessed only from interrupt */
206 unsigned char resend;
207 unsigned char release;
208 unsigned char bat_xl;
212 /* Ensures that only one command is executing at a time */
213 struct semaphore cmd_sem;
215 /* Used to signal completion from interrupt handler */
216 wait_queue_head_t wait;
222 /* Work structure to schedule execution of a command */
224 struct work_struct work;
227 unsigned char param[0];
231 static void atkbd_report_key(struct input_dev *dev, struct pt_regs *regs, int code, int value)
233 input_regs(dev, regs);
235 input_report_key(dev, code, 1);
236 input_report_key(dev, code, 0);
238 input_event(dev, EV_KEY, code, value);
243 * atkbd_interrupt(). Here takes place processing of data received from
244 * the keyboard into events.
247 static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
248 unsigned int flags, struct pt_regs *regs)
250 struct atkbd *atkbd = serio->private;
251 unsigned int code = data;
252 int scroll = 0, click = -1;
256 printk(KERN_DEBUG "atkbd.c: Received %02x flags %02x\n", data, flags);
259 #if !defined(__i386__) && !defined (__x86_64__)
260 if ((flags & (SERIO_FRAME | SERIO_PARITY)) && (~flags & SERIO_TIMEOUT) && !atkbd->resend && atkbd->write) {
261 printk(KERN_WARNING "atkbd.c: frame/parity error: %02x\n", flags);
262 serio_write(serio, ATKBD_CMD_RESEND);
267 if (!flags && data == ATKBD_RET_ACK)
271 if (test_bit(ATKBD_FLAG_ACK, &atkbd->flags)) {
276 set_bit(ATKBD_FLAG_CMD, &atkbd->flags);
277 set_bit(ATKBD_FLAG_CMD1, &atkbd->flags);
279 clear_bit(ATKBD_FLAG_ACK, &atkbd->flags);
280 wake_up_interruptible(&atkbd->wait);
284 clear_bit(ATKBD_FLAG_ACK, &atkbd->flags);
285 wake_up_interruptible(&atkbd->wait);
291 if (test_bit(ATKBD_FLAG_CMD, &atkbd->flags)) {
294 atkbd->cmdbuf[--atkbd->cmdcnt] = code;
296 if (test_and_clear_bit(ATKBD_FLAG_CMD1, &atkbd->flags) && atkbd->cmdcnt)
297 wake_up_interruptible(&atkbd->wait);
299 if (!atkbd->cmdcnt) {
300 clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
301 wake_up_interruptible(&atkbd->wait);
306 if (!test_bit(ATKBD_FLAG_ENABLED, &atkbd->flags))
309 input_event(&atkbd->dev, EV_MSC, MSC_RAW, code);
311 if (atkbd->translated) {
314 !(code == ATKBD_RET_EMUL0 || code == ATKBD_RET_EMUL1 ||
315 code == ATKBD_RET_HANGUEL || code == ATKBD_RET_HANJA ||
316 code == ATKBD_RET_ERR ||
317 (code == ATKBD_RET_BAT && !atkbd->bat_xl))) {
318 atkbd->release = code >> 7;
323 (code & 0x7f) == (ATKBD_RET_BAT & 0x7f))
324 atkbd->bat_xl = !atkbd->release;
329 clear_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
330 serio_rescan(atkbd->serio);
332 case ATKBD_RET_EMUL0:
335 case ATKBD_RET_EMUL1:
338 case ATKBD_RET_RELEASE:
341 case ATKBD_RET_HANGUEL:
342 atkbd_report_key(&atkbd->dev, regs, KEY_HANGUEL, 3);
344 case ATKBD_RET_HANJA:
345 atkbd_report_key(&atkbd->dev, regs, KEY_HANJA, 3);
348 printk(KERN_DEBUG "atkbd.c: Keyboard on %s reports too many keys pressed.\n", serio->phys);
353 code = (code & 0x7f) | ((code & 0x80) << 1);
357 code |= (atkbd->set != 3) ? 0x80 : 0x100;
360 if (atkbd->keycode[code] != ATKBD_KEY_NULL)
361 input_event(&atkbd->dev, EV_MSC, MSC_SCAN, code);
363 switch (atkbd->keycode[code]) {
366 case ATKBD_KEY_UNKNOWN:
367 if (data == ATKBD_RET_ACK || data == ATKBD_RET_NAK) {
369 /* Quite a few key switchers and other tools trigger this and it confuses
370 people who can do nothing about it */
371 printk(KERN_WARNING "atkbd.c: Spurious %s on %s. Some program, "
372 "like XFree86, might be trying access hardware directly.\n",
373 data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys);
376 printk(KERN_WARNING "atkbd.c: Unknown key %s "
377 "(%s set %d, code %#x on %s).\n",
378 atkbd->release ? "released" : "pressed",
379 atkbd->translated ? "translated" : "raw",
380 atkbd->set, code, serio->phys);
381 printk(KERN_WARNING "atkbd.c: Use 'setkeycodes %s%02x <keycode>' "
382 "to make it known.\n",
383 code & 0x80 ? "e0" : "", code & 0x7f);
387 scroll = 1 - atkbd->release * 2;
390 scroll = 2 - atkbd->release * 4;
393 scroll = 4 - atkbd->release * 8;
396 scroll = 8 - atkbd->release * 16;
398 case ATKBD_SCR_CLICK:
399 click = !atkbd->release;
402 value = atkbd->release ? 0 :
403 (1 + (!atkbd_softrepeat && test_bit(atkbd->keycode[code], atkbd->dev.key)));
405 switch (value) { /* Workaround Toshiba laptop multiple keypress */
411 atkbd->time = jiffies + (atkbd->dev.rep[REP_DELAY] * HZ + 500) / 1000 / 2;
414 if (!time_after(jiffies, atkbd->time) && atkbd->last == code)
419 atkbd_report_key(&atkbd->dev, regs, atkbd->keycode[code], value);
422 if (scroll || click != -1) {
423 input_regs(&atkbd->dev, regs);
424 input_report_key(&atkbd->dev, BTN_MIDDLE, click);
425 input_report_rel(&atkbd->dev, REL_WHEEL, scroll);
426 input_sync(&atkbd->dev);
435 * atkbd_sendbyte() sends a byte to the keyboard, and waits for
436 * acknowledge. It doesn't handle resends according to the keyboard
437 * protocol specs, because if these are needed, the keyboard needs
438 * replacement anyway, and they only make a mess in the protocol.
440 * atkbd_sendbyte() can only be called from a process context
443 static int atkbd_sendbyte(struct atkbd *atkbd, unsigned char byte)
446 printk(KERN_DEBUG "atkbd.c: Sent: %02x\n", byte);
449 set_bit(ATKBD_FLAG_ACK, &atkbd->flags);
451 if (serio_write(atkbd->serio, byte) == 0)
452 wait_event_interruptible_timeout(atkbd->wait,
453 !test_bit(ATKBD_FLAG_ACK, &atkbd->flags),
454 msecs_to_jiffies(200));
456 clear_bit(ATKBD_FLAG_ACK, &atkbd->flags);
461 * atkbd_command() sends a command, and its parameters to the keyboard,
462 * then waits for the response and puts it in the param array.
464 * atkbd_command() can only be called from a process context
467 static int atkbd_command(struct atkbd *atkbd, unsigned char *param, int command)
470 int send = (command >> 12) & 0xf;
471 int receive = (command >> 8) & 0xf;
475 timeout = msecs_to_jiffies(command == ATKBD_CMD_RESET_BAT ? 4000 : 500);
477 down(&atkbd->cmd_sem);
478 clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
480 if (receive && param)
481 for (i = 0; i < receive; i++)
482 atkbd->cmdbuf[(receive - 1) - i] = param[i];
484 atkbd->cmdcnt = receive;
487 if (atkbd_sendbyte(atkbd, command & 0xff))
490 for (i = 0; i < send; i++)
491 if (atkbd_sendbyte(atkbd, param[i]))
494 timeout = wait_event_interruptible_timeout(atkbd->wait,
495 !test_bit(ATKBD_FLAG_CMD1, &atkbd->flags), timeout);
497 if (atkbd->cmdcnt && timeout > 0) {
498 if (command == ATKBD_CMD_RESET_BAT && jiffies_to_msecs(timeout) > 100)
499 timeout = msecs_to_jiffies(100);
501 if (command == ATKBD_CMD_GETID &&
502 atkbd->cmdbuf[receive - 1] != 0xab && atkbd->cmdbuf[receive - 1] != 0xac) {
504 * Device behind the port is not a keyboard
505 * so we don't need to wait for the 2nd byte
508 clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
512 wait_event_interruptible_timeout(atkbd->wait,
513 !test_bit(ATKBD_FLAG_CMD, &atkbd->flags), timeout);
517 for (i = 0; i < receive; i++)
518 param[i] = atkbd->cmdbuf[(receive - 1) - i];
520 if (atkbd->cmdcnt && (command != ATKBD_CMD_RESET_BAT || atkbd->cmdcnt != 1))
526 clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
527 clear_bit(ATKBD_FLAG_CMD1, &atkbd->flags);
534 * atkbd_execute_scheduled_command() sends a command, previously scheduled by
535 * atkbd_schedule_command(), to the keyboard.
538 static void atkbd_execute_scheduled_command(void *data)
540 struct atkbd_work *atkbd_work = data;
542 atkbd_command(atkbd_work->atkbd, atkbd_work->param, atkbd_work->command);
548 * atkbd_schedule_command() allows to schedule delayed execution of a keyboard
549 * command and can be used to issue a command from an interrupt or softirq
553 static int atkbd_schedule_command(struct atkbd *atkbd, unsigned char *param, int command)
555 struct atkbd_work *atkbd_work;
556 int send = (command >> 12) & 0xf;
557 int receive = (command >> 8) & 0xf;
559 if (!test_bit(ATKBD_FLAG_ENABLED, &atkbd->flags))
562 if (!(atkbd_work = kmalloc(sizeof(struct atkbd_work) + max(send, receive), GFP_ATOMIC)))
565 memset(atkbd_work, 0, sizeof(struct atkbd_work));
566 atkbd_work->atkbd = atkbd;
567 atkbd_work->command = command;
568 memcpy(atkbd_work->param, param, send);
569 INIT_WORK(&atkbd_work->work, atkbd_execute_scheduled_command, atkbd_work);
571 if (!schedule_work(&atkbd_work->work)) {
581 * Event callback from the input module. Events that change the state of
582 * the hardware are processed here.
585 static int atkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
587 struct atkbd *atkbd = dev->private;
588 const short period[32] =
589 { 33, 37, 42, 46, 50, 54, 58, 63, 67, 75, 83, 92, 100, 109, 116, 125,
590 133, 149, 167, 182, 200, 217, 232, 250, 270, 303, 333, 370, 400, 435, 470, 500 };
591 const short delay[4] =
592 { 250, 500, 750, 1000 };
593 unsigned char param[2];
603 param[0] = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0)
604 | (test_bit(LED_NUML, dev->led) ? 2 : 0)
605 | (test_bit(LED_CAPSL, dev->led) ? 4 : 0);
606 atkbd_schedule_command(atkbd, param, ATKBD_CMD_SETLEDS);
610 param[1] = (test_bit(LED_COMPOSE, dev->led) ? 0x01 : 0)
611 | (test_bit(LED_SLEEP, dev->led) ? 0x02 : 0)
612 | (test_bit(LED_SUSPEND, dev->led) ? 0x04 : 0)
613 | (test_bit(LED_MISC, dev->led) ? 0x10 : 0)
614 | (test_bit(LED_MUTE, dev->led) ? 0x20 : 0);
615 atkbd_schedule_command(atkbd, param, ATKBD_CMD_EX_SETLEDS);
623 if (atkbd_softrepeat) return 0;
626 while (i < 32 && period[i] < dev->rep[REP_PERIOD]) i++;
627 while (j < 4 && delay[j] < dev->rep[REP_DELAY]) j++;
628 dev->rep[REP_PERIOD] = period[i];
629 dev->rep[REP_DELAY] = delay[j];
630 param[0] = i | (j << 5);
631 atkbd_schedule_command(atkbd, param, ATKBD_CMD_SETREP);
640 * atkbd_probe() probes for an AT keyboard on a serio port.
643 static int atkbd_probe(struct atkbd *atkbd)
645 unsigned char param[2];
648 * Some systems, where the bit-twiddling when testing the io-lines of the
649 * controller may confuse the keyboard need a full reset of the keyboard. On
650 * these systems the BIOS also usually doesn't do it for us.
654 if (atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT))
655 printk(KERN_WARNING "atkbd.c: keyboard reset failed on %s\n", atkbd->serio->phys);
658 * Then we check the keyboard ID. We should get 0xab83 under normal conditions.
659 * Some keyboards report different values, but the first byte is always 0xab or
660 * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this
661 * should make sure we don't try to set the LEDs on it.
664 param[0] = param[1] = 0xa5; /* initialize with invalid values */
665 if (atkbd_command(atkbd, param, ATKBD_CMD_GETID)) {
668 * If the get ID command failed, we check if we can at least set the LEDs on
669 * the keyboard. This should work on every keyboard out there. It also turns
670 * the LEDs off, which we want anyway.
673 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
679 if (param[0] != 0xab && param[0] != 0xac)
681 atkbd->id = (param[0] << 8) | param[1];
683 if (atkbd->id == 0xaca1 && atkbd->translated) {
684 printk(KERN_ERR "atkbd.c: NCD terminal keyboards are only supported on non-translating\n");
685 printk(KERN_ERR "atkbd.c: controllers. Use i8042.direct=1 to disable translation.\n");
693 * atkbd_set_3 checks if a keyboard has a working Set 3 support, and
694 * sets it into that. Unfortunately there are keyboards that can be switched
695 * to Set 3, but don't work well in that (BTC Multimedia ...)
698 static int atkbd_set_3(struct atkbd *atkbd)
700 unsigned char param[2];
703 * For known special keyboards we can go ahead and set the correct set.
704 * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and
705 * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards.
708 if (atkbd->translated)
711 if (atkbd->id == 0xaca1) {
713 atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET);
719 if (!atkbd_command(atkbd, param, ATKBD_CMD_EX_ENABLE)) {
728 if (!atkbd_command(atkbd, param, ATKBD_CMD_OK_GETID)) {
729 atkbd->id = param[0] << 8 | param[1];
734 if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
738 if (atkbd_command(atkbd, param, ATKBD_CMD_GSCANSET))
743 if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
747 atkbd_command(atkbd, param, ATKBD_CMD_SETALL_MBR);
752 static int atkbd_enable(struct atkbd *atkbd)
754 unsigned char param[1];
757 * Set the LEDs to a defined state.
761 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
765 * Set autorepeat to fastest possible.
769 if (atkbd_command(atkbd, param, ATKBD_CMD_SETREP))
773 * Enable the keyboard to receive keystrokes.
776 if (atkbd_command(atkbd, NULL, ATKBD_CMD_ENABLE)) {
777 printk(KERN_ERR "atkbd.c: Failed to enable keyboard on %s\n",
786 * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a
790 static void atkbd_cleanup(struct serio *serio)
792 struct atkbd *atkbd = serio->private;
793 atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT);
797 * atkbd_disconnect() closes and frees.
800 static void atkbd_disconnect(struct serio *serio)
802 struct atkbd *atkbd = serio->private;
804 clear_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
805 synchronize_kernel();
806 flush_scheduled_work();
808 input_unregister_device(&atkbd->dev);
814 * atkbd_connect() is called when the serio module finds and interface
815 * that isn't handled yet by an appropriate device driver. We check if
816 * there is an AT keyboard out there and if yes, we register ourselves
817 * to the input module.
820 static void atkbd_connect(struct serio *serio, struct serio_driver *drv)
825 if (!(atkbd = kmalloc(sizeof(struct atkbd), GFP_KERNEL)))
827 memset(atkbd, 0, sizeof(struct atkbd));
829 init_MUTEX(&atkbd->cmd_sem);
830 init_waitqueue_head(&atkbd->wait);
832 switch (serio->type & SERIO_TYPE) {
835 atkbd->translated = 1;
841 if ((serio->type & SERIO_PROTO) == SERIO_PS2SER)
849 atkbd_softrepeat = 1;
850 if (atkbd_softrepeat)
854 atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP) | BIT(EV_MSC);
855 atkbd->dev.ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);
856 } else atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_MSC);
857 atkbd->dev.mscbit[0] = atkbd_softraw ? BIT(MSC_SCAN) : BIT(MSC_RAW) | BIT(MSC_SCAN);
859 if (!atkbd_softrepeat) {
860 atkbd->dev.rep[REP_DELAY] = 250;
861 atkbd->dev.rep[REP_PERIOD] = 33;
862 } else atkbd_softraw = 1;
864 atkbd->serio = serio;
866 init_input_dev(&atkbd->dev);
868 atkbd->dev.keycode = atkbd->keycode;
869 atkbd->dev.keycodesize = sizeof(unsigned char);
870 atkbd->dev.keycodemax = ARRAY_SIZE(atkbd_set2_keycode);
871 atkbd->dev.event = atkbd_event;
872 atkbd->dev.private = atkbd;
874 serio->private = atkbd;
876 if (serio_open(serio, drv)) {
883 if (atkbd_probe(atkbd)) {
885 serio->private = NULL;
890 atkbd->set = atkbd_set_3(atkbd);
899 atkbd->dev.ledbit[0] |= BIT(LED_COMPOSE) | BIT(LED_SUSPEND) | BIT(LED_SLEEP) | BIT(LED_MUTE) | BIT(LED_MISC);
900 sprintf(atkbd->name, "AT Set 2 Extra keyboard");
902 sprintf(atkbd->name, "AT %s Set %d keyboard",
903 atkbd->translated ? "Translated" : "Raw", atkbd->set);
905 sprintf(atkbd->phys, "%s/input0", serio->phys);
908 for (i = 0; i < 5; i++)
909 atkbd_set2_keycode[atkbd_scroll_keys[i][1]] = atkbd_scroll_keys[i][0];
910 atkbd->dev.evbit[0] |= BIT(EV_REL);
911 atkbd->dev.relbit[0] = BIT(REL_WHEEL);
912 set_bit(BTN_MIDDLE, atkbd->dev.keybit);
915 if (atkbd->translated) {
916 for (i = 0; i < 128; i++) {
917 atkbd->keycode[i] = atkbd_set2_keycode[atkbd_unxlate_table[i]];
918 atkbd->keycode[i | 0x80] = atkbd_set2_keycode[atkbd_unxlate_table[i] | 0x80];
920 } else if (atkbd->set == 3) {
921 memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode));
923 memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));
926 atkbd->dev.name = atkbd->name;
927 atkbd->dev.phys = atkbd->phys;
928 atkbd->dev.id.bustype = BUS_I8042;
929 atkbd->dev.id.vendor = 0x0001;
930 atkbd->dev.id.product = atkbd->translated ? 1 : atkbd->set;
931 atkbd->dev.id.version = atkbd->id;
933 for (i = 0; i < 512; i++)
934 if (atkbd->keycode[i] && atkbd->keycode[i] < ATKBD_SPECIAL)
935 set_bit(atkbd->keycode[i], atkbd->dev.keybit);
937 input_register_device(&atkbd->dev);
939 set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
941 printk(KERN_INFO "input: %s on %s\n", atkbd->name, serio->phys);
945 * atkbd_reconnect() tries to restore keyboard into a sane state and is
946 * most likely called on resume.
949 static int atkbd_reconnect(struct serio *serio)
951 struct atkbd *atkbd = serio->private;
952 struct serio_driver *drv = serio->drv;
953 unsigned char param[1];
956 printk(KERN_DEBUG "atkbd: reconnect request, but serio is disconnected, ignoring...\n");
961 param[0] = (test_bit(LED_SCROLLL, atkbd->dev.led) ? 1 : 0)
962 | (test_bit(LED_NUML, atkbd->dev.led) ? 2 : 0)
963 | (test_bit(LED_CAPSL, atkbd->dev.led) ? 4 : 0);
965 if (atkbd_probe(atkbd))
967 if (atkbd->set != atkbd_set_3(atkbd))
972 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
976 set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
981 static struct serio_driver atkbd_drv = {
985 .description = DRIVER_DESC,
986 .interrupt = atkbd_interrupt,
987 .connect = atkbd_connect,
988 .reconnect = atkbd_reconnect,
989 .disconnect = atkbd_disconnect,
990 .cleanup = atkbd_cleanup,
993 int __init atkbd_init(void)
995 serio_register_driver(&atkbd_drv);
999 void __exit atkbd_exit(void)
1001 serio_unregister_driver(&atkbd_drv);
1004 module_init(atkbd_init);
1005 module_exit(atkbd_exit);