initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / input / keyboard / atkbd.c
blobec97ae7d505255d5d8082aaf04e3e79910bdcd85
1 /*
2 * AT and PS/2 keyboard driver
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
7 /*
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
17 * converter.
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;
42 #else
43 static int atkbd_reset = 1;
44 #endif
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.
73 #if defined(__hppa__)
74 #include "hpps2atkbd.h"
75 #else
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,
97 0, 0, 0, 65, 99,
100 #endif
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,
115 148,149,147,140
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
182 struct atkbd {
184 /* Written only during init */
185 char name[64];
186 char phys[32];
187 struct serio *serio;
188 struct input_dev dev;
190 unsigned char set;
191 unsigned short id;
192 unsigned char keycode[512];
193 unsigned char translated;
194 unsigned char extra;
195 unsigned char write;
197 /* Protected by FLAG_ACK */
198 unsigned char nak;
200 /* Protected by FLAG_CMD */
201 unsigned char cmdbuf[4];
202 unsigned char cmdcnt;
204 /* Accessed only from interrupt */
205 unsigned char emul;
206 unsigned char resend;
207 unsigned char release;
208 unsigned char bat_xl;
209 unsigned int last;
210 unsigned long time;
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;
218 /* Flags */
219 unsigned long flags;
222 /* Work structure to schedule execution of a command */
223 struct atkbd_work {
224 struct work_struct work;
225 struct atkbd *atkbd;
226 int command;
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);
234 if (value == 3) {
235 input_report_key(dev, code, 1);
236 input_report_key(dev, code, 0);
237 } else
238 input_event(dev, EV_KEY, code, value);
239 input_sync(dev);
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;
253 int value;
255 #ifdef ATKBD_DEBUG
256 printk(KERN_DEBUG "atkbd.c: Received %02x flags %02x\n", data, flags);
257 #endif
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);
263 atkbd->resend = 1;
264 goto out;
267 if (!flags && data == ATKBD_RET_ACK)
268 atkbd->resend = 0;
269 #endif
271 if (test_bit(ATKBD_FLAG_ACK, &atkbd->flags)) {
272 switch (code) {
273 case ATKBD_RET_ACK:
274 atkbd->nak = 0;
275 if (atkbd->cmdcnt) {
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);
281 break;
282 case ATKBD_RET_NAK:
283 atkbd->nak = 1;
284 clear_bit(ATKBD_FLAG_ACK, &atkbd->flags);
285 wake_up_interruptible(&atkbd->wait);
286 break;
288 goto out;
291 if (test_bit(ATKBD_FLAG_CMD, &atkbd->flags)) {
293 if (atkbd->cmdcnt)
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);
303 goto out;
306 if (!test_bit(ATKBD_FLAG_ENABLED, &atkbd->flags))
307 goto out;
309 input_event(&atkbd->dev, EV_MSC, MSC_RAW, code);
311 if (atkbd->translated) {
313 if (atkbd->emul ||
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;
319 code &= 0x7f;
322 if (!atkbd->emul &&
323 (code & 0x7f) == (ATKBD_RET_BAT & 0x7f))
324 atkbd->bat_xl = !atkbd->release;
327 switch (code) {
328 case ATKBD_RET_BAT:
329 clear_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
330 serio_rescan(atkbd->serio);
331 goto out;
332 case ATKBD_RET_EMUL0:
333 atkbd->emul = 1;
334 goto out;
335 case ATKBD_RET_EMUL1:
336 atkbd->emul = 2;
337 goto out;
338 case ATKBD_RET_RELEASE:
339 atkbd->release = 1;
340 goto out;
341 case ATKBD_RET_HANGUEL:
342 atkbd_report_key(&atkbd->dev, regs, KEY_HANGUEL, 3);
343 goto out;
344 case ATKBD_RET_HANJA:
345 atkbd_report_key(&atkbd->dev, regs, KEY_HANJA, 3);
346 goto out;
347 case ATKBD_RET_ERR:
348 printk(KERN_DEBUG "atkbd.c: Keyboard on %s reports too many keys pressed.\n", serio->phys);
349 goto out;
352 if (atkbd->set != 3)
353 code = (code & 0x7f) | ((code & 0x80) << 1);
354 if (atkbd->emul) {
355 if (--atkbd->emul)
356 goto out;
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]) {
364 case ATKBD_KEY_NULL:
365 break;
366 case ATKBD_KEY_UNKNOWN:
367 if (data == ATKBD_RET_ACK || data == ATKBD_RET_NAK) {
368 printk(KERN_WARNING "atkbd.c: Spurious %s on %s. Some program, "
369 "like XFree86, might be trying access hardware directly.\n",
370 data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys);
371 } else {
372 printk(KERN_WARNING "atkbd.c: Unknown key %s "
373 "(%s set %d, code %#x on %s).\n",
374 atkbd->release ? "released" : "pressed",
375 atkbd->translated ? "translated" : "raw",
376 atkbd->set, code, serio->phys);
377 printk(KERN_WARNING "atkbd.c: Use 'setkeycodes %s%02x <keycode>' "
378 "to make it known.\n",
379 code & 0x80 ? "e0" : "", code & 0x7f);
381 break;
382 case ATKBD_SCR_1:
383 scroll = 1 - atkbd->release * 2;
384 break;
385 case ATKBD_SCR_2:
386 scroll = 2 - atkbd->release * 4;
387 break;
388 case ATKBD_SCR_4:
389 scroll = 4 - atkbd->release * 8;
390 break;
391 case ATKBD_SCR_8:
392 scroll = 8 - atkbd->release * 16;
393 break;
394 case ATKBD_SCR_CLICK:
395 click = !atkbd->release;
396 break;
397 default:
398 value = atkbd->release ? 0 :
399 (1 + (!atkbd_softrepeat && test_bit(atkbd->keycode[code], atkbd->dev.key)));
401 switch (value) { /* Workaround Toshiba laptop multiple keypress */
402 case 0:
403 atkbd->last = 0;
404 break;
405 case 1:
406 atkbd->last = code;
407 atkbd->time = jiffies + (atkbd->dev.rep[REP_DELAY] * HZ + 500) / 1000 / 2;
408 break;
409 case 2:
410 if (!time_after(jiffies, atkbd->time) && atkbd->last == code)
411 value = 1;
412 break;
415 atkbd_report_key(&atkbd->dev, regs, atkbd->keycode[code], value);
418 if (scroll || click != -1) {
419 input_regs(&atkbd->dev, regs);
420 input_report_key(&atkbd->dev, BTN_MIDDLE, click);
421 input_report_rel(&atkbd->dev, REL_WHEEL, scroll);
422 input_sync(&atkbd->dev);
425 atkbd->release = 0;
426 out:
427 return IRQ_HANDLED;
431 * atkbd_sendbyte() sends a byte to the keyboard, and waits for
432 * acknowledge. It doesn't handle resends according to the keyboard
433 * protocol specs, because if these are needed, the keyboard needs
434 * replacement anyway, and they only make a mess in the protocol.
436 * atkbd_sendbyte() can only be called from a process context
439 static int atkbd_sendbyte(struct atkbd *atkbd, unsigned char byte)
441 #ifdef ATKBD_DEBUG
442 printk(KERN_DEBUG "atkbd.c: Sent: %02x\n", byte);
443 #endif
444 atkbd->nak = 1;
445 set_bit(ATKBD_FLAG_ACK, &atkbd->flags);
447 if (serio_write(atkbd->serio, byte) == 0)
448 wait_event_interruptible_timeout(atkbd->wait,
449 !test_bit(ATKBD_FLAG_ACK, &atkbd->flags),
450 msecs_to_jiffies(200));
452 clear_bit(ATKBD_FLAG_ACK, &atkbd->flags);
453 return -atkbd->nak;
457 * atkbd_command() sends a command, and its parameters to the keyboard,
458 * then waits for the response and puts it in the param array.
460 * atkbd_command() can only be called from a process context
463 static int atkbd_command(struct atkbd *atkbd, unsigned char *param, int command)
465 int timeout;
466 int send = (command >> 12) & 0xf;
467 int receive = (command >> 8) & 0xf;
468 int rc = -1;
469 int i;
471 timeout = msecs_to_jiffies(command == ATKBD_CMD_RESET_BAT ? 4000 : 500);
473 down(&atkbd->cmd_sem);
474 clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
476 if (receive && param)
477 for (i = 0; i < receive; i++)
478 atkbd->cmdbuf[(receive - 1) - i] = param[i];
480 atkbd->cmdcnt = receive;
482 if (command & 0xff)
483 if (atkbd_sendbyte(atkbd, command & 0xff))
484 goto out;
486 for (i = 0; i < send; i++)
487 if (atkbd_sendbyte(atkbd, param[i]))
488 goto out;
490 timeout = wait_event_interruptible_timeout(atkbd->wait,
491 !test_bit(ATKBD_FLAG_CMD1, &atkbd->flags), timeout);
493 if (atkbd->cmdcnt && timeout > 0) {
494 if (command == ATKBD_CMD_RESET_BAT && jiffies_to_msecs(timeout) > 100)
495 timeout = msecs_to_jiffies(100);
497 if (command == ATKBD_CMD_GETID &&
498 atkbd->cmdbuf[receive - 1] != 0xab && atkbd->cmdbuf[receive - 1] != 0xac) {
500 * Device behind the port is not a keyboard
501 * so we don't need to wait for the 2nd byte
502 * of ID response.
504 clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
505 atkbd->cmdcnt = 0;
508 wait_event_interruptible_timeout(atkbd->wait,
509 !test_bit(ATKBD_FLAG_CMD, &atkbd->flags), timeout);
512 if (param)
513 for (i = 0; i < receive; i++)
514 param[i] = atkbd->cmdbuf[(receive - 1) - i];
516 if (atkbd->cmdcnt && (command != ATKBD_CMD_RESET_BAT || atkbd->cmdcnt != 1))
517 goto out;
519 rc = 0;
521 out:
522 clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
523 clear_bit(ATKBD_FLAG_CMD1, &atkbd->flags);
524 up(&atkbd->cmd_sem);
526 return rc;
530 * atkbd_execute_scheduled_command() sends a command, previously scheduled by
531 * atkbd_schedule_command(), to the keyboard.
534 static void atkbd_execute_scheduled_command(void *data)
536 struct atkbd_work *atkbd_work = data;
538 atkbd_command(atkbd_work->atkbd, atkbd_work->param, atkbd_work->command);
540 kfree(atkbd_work);
544 * atkbd_schedule_command() allows to schedule delayed execution of a keyboard
545 * command and can be used to issue a command from an interrupt or softirq
546 * context.
549 static int atkbd_schedule_command(struct atkbd *atkbd, unsigned char *param, int command)
551 struct atkbd_work *atkbd_work;
552 int send = (command >> 12) & 0xf;
553 int receive = (command >> 8) & 0xf;
555 if (!test_bit(ATKBD_FLAG_ENABLED, &atkbd->flags))
556 return -1;
558 if (!(atkbd_work = kmalloc(sizeof(struct atkbd_work) + max(send, receive), GFP_ATOMIC)))
559 return -1;
561 memset(atkbd_work, 0, sizeof(struct atkbd_work));
562 atkbd_work->atkbd = atkbd;
563 atkbd_work->command = command;
564 memcpy(atkbd_work->param, param, send);
565 INIT_WORK(&atkbd_work->work, atkbd_execute_scheduled_command, atkbd_work);
567 if (!schedule_work(&atkbd_work->work)) {
568 kfree(atkbd_work);
569 return -1;
572 return 0;
577 * Event callback from the input module. Events that change the state of
578 * the hardware are processed here.
581 static int atkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
583 struct atkbd *atkbd = dev->private;
584 const short period[32] =
585 { 33, 37, 42, 46, 50, 54, 58, 63, 67, 75, 83, 92, 100, 109, 116, 125,
586 133, 149, 167, 182, 200, 217, 232, 250, 270, 303, 333, 370, 400, 435, 470, 500 };
587 const short delay[4] =
588 { 250, 500, 750, 1000 };
589 unsigned char param[2];
590 int i, j;
592 if (!atkbd->write)
593 return -1;
595 switch (type) {
597 case EV_LED:
599 param[0] = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0)
600 | (test_bit(LED_NUML, dev->led) ? 2 : 0)
601 | (test_bit(LED_CAPSL, dev->led) ? 4 : 0);
602 atkbd_schedule_command(atkbd, param, ATKBD_CMD_SETLEDS);
604 if (atkbd->extra) {
605 param[0] = 0;
606 param[1] = (test_bit(LED_COMPOSE, dev->led) ? 0x01 : 0)
607 | (test_bit(LED_SLEEP, dev->led) ? 0x02 : 0)
608 | (test_bit(LED_SUSPEND, dev->led) ? 0x04 : 0)
609 | (test_bit(LED_MISC, dev->led) ? 0x10 : 0)
610 | (test_bit(LED_MUTE, dev->led) ? 0x20 : 0);
611 atkbd_schedule_command(atkbd, param, ATKBD_CMD_EX_SETLEDS);
614 return 0;
617 case EV_REP:
619 if (atkbd_softrepeat) return 0;
621 i = j = 0;
622 while (i < 32 && period[i] < dev->rep[REP_PERIOD]) i++;
623 while (j < 4 && delay[j] < dev->rep[REP_DELAY]) j++;
624 dev->rep[REP_PERIOD] = period[i];
625 dev->rep[REP_DELAY] = delay[j];
626 param[0] = i | (j << 5);
627 atkbd_schedule_command(atkbd, param, ATKBD_CMD_SETREP);
629 return 0;
632 return -1;
636 * atkbd_probe() probes for an AT keyboard on a serio port.
639 static int atkbd_probe(struct atkbd *atkbd)
641 unsigned char param[2];
644 * Some systems, where the bit-twiddling when testing the io-lines of the
645 * controller may confuse the keyboard need a full reset of the keyboard. On
646 * these systems the BIOS also usually doesn't do it for us.
649 if (atkbd_reset)
650 if (atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT))
651 printk(KERN_WARNING "atkbd.c: keyboard reset failed on %s\n", atkbd->serio->phys);
654 * Then we check the keyboard ID. We should get 0xab83 under normal conditions.
655 * Some keyboards report different values, but the first byte is always 0xab or
656 * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this
657 * should make sure we don't try to set the LEDs on it.
660 param[0] = param[1] = 0xa5; /* initialize with invalid values */
661 if (atkbd_command(atkbd, param, ATKBD_CMD_GETID)) {
664 * If the get ID command failed, we check if we can at least set the LEDs on
665 * the keyboard. This should work on every keyboard out there. It also turns
666 * the LEDs off, which we want anyway.
668 param[0] = 0;
669 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
670 return -1;
671 atkbd->id = 0xabba;
672 return 0;
675 if (param[0] != 0xab && param[0] != 0xac)
676 return -1;
677 atkbd->id = (param[0] << 8) | param[1];
679 if (atkbd->id == 0xaca1 && atkbd->translated) {
680 printk(KERN_ERR "atkbd.c: NCD terminal keyboards are only supported on non-translating\n");
681 printk(KERN_ERR "atkbd.c: controllers. Use i8042.direct=1 to disable translation.\n");
682 return -1;
685 return 0;
689 * atkbd_set_3 checks if a keyboard has a working Set 3 support, and
690 * sets it into that. Unfortunately there are keyboards that can be switched
691 * to Set 3, but don't work well in that (BTC Multimedia ...)
694 static int atkbd_set_3(struct atkbd *atkbd)
696 unsigned char param[2];
699 * For known special keyboards we can go ahead and set the correct set.
700 * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and
701 * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards.
704 if (atkbd->translated)
705 return 2;
707 if (atkbd->id == 0xaca1) {
708 param[0] = 3;
709 atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET);
710 return 3;
713 if (atkbd_extra) {
714 param[0] = 0x71;
715 if (!atkbd_command(atkbd, param, ATKBD_CMD_EX_ENABLE)) {
716 atkbd->extra = 1;
717 return 2;
721 if (atkbd_set != 3)
722 return 2;
724 if (!atkbd_command(atkbd, param, ATKBD_CMD_OK_GETID)) {
725 atkbd->id = param[0] << 8 | param[1];
726 return 2;
729 param[0] = 3;
730 if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
731 return 2;
733 param[0] = 0;
734 if (atkbd_command(atkbd, param, ATKBD_CMD_GSCANSET))
735 return 2;
737 if (param[0] != 3) {
738 param[0] = 2;
739 if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
740 return 2;
743 atkbd_command(atkbd, param, ATKBD_CMD_SETALL_MBR);
745 return 3;
748 static int atkbd_enable(struct atkbd *atkbd)
750 unsigned char param[1];
753 * Set the LEDs to a defined state.
756 param[0] = 0;
757 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
758 return -1;
761 * Set autorepeat to fastest possible.
764 param[0] = 0;
765 if (atkbd_command(atkbd, param, ATKBD_CMD_SETREP))
766 return -1;
769 * Enable the keyboard to receive keystrokes.
772 if (atkbd_command(atkbd, NULL, ATKBD_CMD_ENABLE)) {
773 printk(KERN_ERR "atkbd.c: Failed to enable keyboard on %s\n",
774 atkbd->serio->phys);
775 return -1;
778 return 0;
782 * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a
783 * reboot.
786 static void atkbd_cleanup(struct serio *serio)
788 struct atkbd *atkbd = serio->private;
789 atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT);
793 * atkbd_disconnect() closes and frees.
796 static void atkbd_disconnect(struct serio *serio)
798 struct atkbd *atkbd = serio->private;
800 clear_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
801 synchronize_kernel();
802 flush_scheduled_work();
804 input_unregister_device(&atkbd->dev);
805 serio_close(serio);
806 kfree(atkbd);
810 * atkbd_connect() is called when the serio module finds and interface
811 * that isn't handled yet by an appropriate device driver. We check if
812 * there is an AT keyboard out there and if yes, we register ourselves
813 * to the input module.
816 static void atkbd_connect(struct serio *serio, struct serio_driver *drv)
818 struct atkbd *atkbd;
819 int i;
821 if (!(atkbd = kmalloc(sizeof(struct atkbd), GFP_KERNEL)))
822 return;
823 memset(atkbd, 0, sizeof(struct atkbd));
825 init_MUTEX(&atkbd->cmd_sem);
826 init_waitqueue_head(&atkbd->wait);
828 switch (serio->type & SERIO_TYPE) {
830 case SERIO_8042_XL:
831 atkbd->translated = 1;
832 case SERIO_8042:
833 if (serio->write)
834 atkbd->write = 1;
835 break;
836 case SERIO_RS232:
837 if ((serio->type & SERIO_PROTO) == SERIO_PS2SER)
838 break;
839 default:
840 kfree(atkbd);
841 return;
844 if (!atkbd->write)
845 atkbd_softrepeat = 1;
846 if (atkbd_softrepeat)
847 atkbd_softraw = 1;
849 if (atkbd->write) {
850 atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP) | BIT(EV_MSC);
851 atkbd->dev.ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);
852 } else atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_MSC);
853 atkbd->dev.mscbit[0] = atkbd_softraw ? BIT(MSC_SCAN) : BIT(MSC_RAW) | BIT(MSC_SCAN);
855 if (!atkbd_softrepeat) {
856 atkbd->dev.rep[REP_DELAY] = 250;
857 atkbd->dev.rep[REP_PERIOD] = 33;
858 } else atkbd_softraw = 1;
860 atkbd->serio = serio;
862 init_input_dev(&atkbd->dev);
864 atkbd->dev.keycode = atkbd->keycode;
865 atkbd->dev.keycodesize = sizeof(unsigned char);
866 atkbd->dev.keycodemax = ARRAY_SIZE(atkbd_set2_keycode);
867 atkbd->dev.event = atkbd_event;
868 atkbd->dev.private = atkbd;
870 serio->private = atkbd;
872 if (serio_open(serio, drv)) {
873 kfree(atkbd);
874 return;
877 if (atkbd->write) {
879 if (atkbd_probe(atkbd)) {
880 serio_close(serio);
881 serio->private = NULL;
882 kfree(atkbd);
883 return;
886 atkbd->set = atkbd_set_3(atkbd);
887 atkbd_enable(atkbd);
889 } else {
890 atkbd->set = 2;
891 atkbd->id = 0xab00;
894 if (atkbd->extra) {
895 atkbd->dev.ledbit[0] |= BIT(LED_COMPOSE) | BIT(LED_SUSPEND) | BIT(LED_SLEEP) | BIT(LED_MUTE) | BIT(LED_MISC);
896 sprintf(atkbd->name, "AT Set 2 Extra keyboard");
897 } else
898 sprintf(atkbd->name, "AT %s Set %d keyboard",
899 atkbd->translated ? "Translated" : "Raw", atkbd->set);
901 sprintf(atkbd->phys, "%s/input0", serio->phys);
903 if (atkbd_scroll) {
904 for (i = 0; i < 5; i++)
905 atkbd_set2_keycode[atkbd_scroll_keys[i][1]] = atkbd_scroll_keys[i][0];
906 atkbd->dev.evbit[0] |= BIT(EV_REL);
907 atkbd->dev.relbit[0] = BIT(REL_WHEEL);
908 set_bit(BTN_MIDDLE, atkbd->dev.keybit);
911 if (atkbd->translated) {
912 for (i = 0; i < 128; i++) {
913 atkbd->keycode[i] = atkbd_set2_keycode[atkbd_unxlate_table[i]];
914 atkbd->keycode[i | 0x80] = atkbd_set2_keycode[atkbd_unxlate_table[i] | 0x80];
916 } else if (atkbd->set == 3) {
917 memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode));
918 } else {
919 memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));
922 atkbd->dev.name = atkbd->name;
923 atkbd->dev.phys = atkbd->phys;
924 atkbd->dev.id.bustype = BUS_I8042;
925 atkbd->dev.id.vendor = 0x0001;
926 atkbd->dev.id.product = atkbd->translated ? 1 : atkbd->set;
927 atkbd->dev.id.version = atkbd->id;
929 for (i = 0; i < 512; i++)
930 if (atkbd->keycode[i] && atkbd->keycode[i] < ATKBD_SPECIAL)
931 set_bit(atkbd->keycode[i], atkbd->dev.keybit);
933 input_register_device(&atkbd->dev);
935 set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
937 printk(KERN_INFO "input: %s on %s\n", atkbd->name, serio->phys);
941 * atkbd_reconnect() tries to restore keyboard into a sane state and is
942 * most likely called on resume.
945 static int atkbd_reconnect(struct serio *serio)
947 struct atkbd *atkbd = serio->private;
948 struct serio_driver *drv = serio->drv;
949 unsigned char param[1];
951 if (!drv) {
952 printk(KERN_DEBUG "atkbd: reconnect request, but serio is disconnected, ignoring...\n");
953 return -1;
956 if (atkbd->write) {
957 param[0] = (test_bit(LED_SCROLLL, atkbd->dev.led) ? 1 : 0)
958 | (test_bit(LED_NUML, atkbd->dev.led) ? 2 : 0)
959 | (test_bit(LED_CAPSL, atkbd->dev.led) ? 4 : 0);
961 if (atkbd_probe(atkbd))
962 return -1;
963 if (atkbd->set != atkbd_set_3(atkbd))
964 return -1;
966 atkbd_enable(atkbd);
968 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
969 return -1;
972 set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
974 return 0;
977 static struct serio_driver atkbd_drv = {
978 .driver = {
979 .name = "atkbd",
981 .description = DRIVER_DESC,
982 .interrupt = atkbd_interrupt,
983 .connect = atkbd_connect,
984 .reconnect = atkbd_reconnect,
985 .disconnect = atkbd_disconnect,
986 .cleanup = atkbd_cleanup,
989 int __init atkbd_init(void)
991 serio_register_driver(&atkbd_drv);
992 return 0;
995 void __exit atkbd_exit(void)
997 serio_unregister_driver(&atkbd_drv);
1000 module_init(atkbd_init);
1001 module_exit(atkbd_exit);