upgrade to linux 2.6.9-1.11_FC2
[linux-2.6.git] / drivers / input / keyboard / atkbd.c
1 /*
2  * AT and PS/2 keyboard driver
3  *
4  * Copyright (c) 1999-2002 Vojtech Pavlik
5  */
6
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.
11  */
12
13 /*
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.
18  */
19
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>
29
30 #define DRIVER_DESC     "AT and PS/2 keyboard driver"
31
32 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
33 MODULE_DESCRIPTION(DRIVER_DESC);
34 MODULE_LICENSE("GPL");
35
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)");
39
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");
47
48 static int atkbd_softrepeat;
49 module_param_named(softrepeat, atkbd_softrepeat, bool, 0);
50 MODULE_PARM_DESC(softrepeat, "Use software keyboard repeat");
51
52 static int atkbd_softraw = 1;
53 module_param_named(softraw, atkbd_softraw, bool, 0);
54 MODULE_PARM_DESC(softraw, "Use software generated rawmode");
55
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");
59
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");
63
64 __obsolete_setup("atkbd_set=");
65 __obsolete_setup("atkbd_reset");
66 __obsolete_setup("atkbd_softrepeat=");
67
68 /*
69  * Scancode to keycode tables. These are just the default setting, and
70  * are loadable via an userland utility.
71  */
72
73 #if defined(__hppa__)
74 #include "hpps2atkbd.h"
75 #else
76
77 static unsigned char atkbd_set2_keycode[512] = {
78
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,
87
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,
96
97           0,  0,  0, 65, 99,
98 };
99
100 #endif
101
102 static unsigned char atkbd_set3_keycode[512] = {
103
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,
112
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
116 };
117
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
127 };
128
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
142
143
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
153
154 #define ATKBD_KEY_UNKNOWN         0
155 #define ATKBD_KEY_NULL          255
156
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
162
163 #define ATKBD_SPECIAL           250
164
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 },
171 };
172
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 */
177
178 /*
179  * The atkbd control structure
180  */
181
182 struct atkbd {
183
184         /* Written only during init */
185         char name[64];
186         char phys[32];
187         struct serio *serio;
188         struct input_dev dev;
189
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;
196
197         /* Protected by FLAG_ACK */
198         unsigned char nak;
199
200         /* Protected by FLAG_CMD */
201         unsigned char cmdbuf[4];
202         unsigned char cmdcnt;
203
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;
211
212         /* Ensures that only one command is executing at a time */
213         struct semaphore cmd_sem;
214
215         /* Used to signal completion from interrupt handler */
216         wait_queue_head_t wait;
217
218         /* Flags */
219         unsigned long flags;
220 };
221
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];
228 };
229
230
231 static void atkbd_report_key(struct input_dev *dev, struct pt_regs *regs, int code, int value)
232 {
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);
240 }
241
242 /*
243  * atkbd_interrupt(). Here takes place processing of data received from
244  * the keyboard into events.
245  */
246
247 static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
248                         unsigned int flags, struct pt_regs *regs)
249 {
250         struct atkbd *atkbd = serio->private;
251         unsigned int code = data;
252         int scroll = 0, click = -1;
253         int value;
254
255 #ifdef ATKBD_DEBUG
256         printk(KERN_DEBUG "atkbd.c: Received %02x flags %02x\n", data, flags);
257 #endif
258
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;
265         }
266
267         if (!flags && data == ATKBD_RET_ACK)
268                 atkbd->resend = 0;
269 #endif
270
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);
278                                 }
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;
287                 }
288                 goto out;
289         }
290
291         if (test_bit(ATKBD_FLAG_CMD, &atkbd->flags)) {
292
293                 if (atkbd->cmdcnt)
294                         atkbd->cmdbuf[--atkbd->cmdcnt] = code;
295
296                 if (test_and_clear_bit(ATKBD_FLAG_CMD1, &atkbd->flags) && atkbd->cmdcnt)
297                         wake_up_interruptible(&atkbd->wait);
298
299                 if (!atkbd->cmdcnt) {
300                         clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
301                         wake_up_interruptible(&atkbd->wait);
302                 }
303                 goto out;
304         }
305
306         if (!test_bit(ATKBD_FLAG_ENABLED, &atkbd->flags))
307                 goto out;
308
309         input_event(&atkbd->dev, EV_MSC, MSC_RAW, code);
310
311         if (atkbd->translated) {
312
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;
320                 }
321
322                 if (!atkbd->emul &&
323                      (code & 0x7f) == (ATKBD_RET_BAT & 0x7f))
324                         atkbd->bat_xl = !atkbd->release;
325         }
326
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;
350         }
351
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;
358         }
359
360         if (atkbd->keycode[code] != ATKBD_KEY_NULL)
361                 input_event(&atkbd->dev, EV_MSC, MSC_SCAN, code);
362
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 #if 0
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);
374 #endif                                 
375                         } else {
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);
384                         }
385                         break;
386                 case ATKBD_SCR_1:
387                         scroll = 1 - atkbd->release * 2;
388                         break;
389                 case ATKBD_SCR_2:
390                         scroll = 2 - atkbd->release * 4;
391                         break;
392                 case ATKBD_SCR_4:
393                         scroll = 4 - atkbd->release * 8;
394                         break;
395                 case ATKBD_SCR_8:
396                         scroll = 8 - atkbd->release * 16;
397                         break;
398                 case ATKBD_SCR_CLICK:
399                         click = !atkbd->release;
400                         break;
401                 default:
402                         value = atkbd->release ? 0 :
403                                 (1 + (!atkbd_softrepeat && test_bit(atkbd->keycode[code], atkbd->dev.key)));
404
405                         switch (value) {        /* Workaround Toshiba laptop multiple keypress */
406                                 case 0:
407                                         atkbd->last = 0;
408                                         break;
409                                 case 1:
410                                         atkbd->last = code;
411                                         atkbd->time = jiffies + (atkbd->dev.rep[REP_DELAY] * HZ + 500) / 1000 / 2;
412                                         break;
413                                 case 2:
414                                         if (!time_after(jiffies, atkbd->time) && atkbd->last == code)
415                                                 value = 1;
416                                         break;
417                         }
418
419                         atkbd_report_key(&atkbd->dev, regs, atkbd->keycode[code], value);
420         }
421
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);
427         }
428
429         atkbd->release = 0;
430 out:
431         return IRQ_HANDLED;
432 }
433
434 /*
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.
439  *
440  * atkbd_sendbyte() can only be called from a process context
441  */
442
443 static int atkbd_sendbyte(struct atkbd *atkbd, unsigned char byte)
444 {
445 #ifdef ATKBD_DEBUG
446         printk(KERN_DEBUG "atkbd.c: Sent: %02x\n", byte);
447 #endif
448         atkbd->nak = 1;
449         set_bit(ATKBD_FLAG_ACK, &atkbd->flags);
450
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));
455
456         clear_bit(ATKBD_FLAG_ACK, &atkbd->flags);
457         return -atkbd->nak;
458 }
459
460 /*
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.
463  *
464  * atkbd_command() can only be called from a process context
465  */
466
467 static int atkbd_command(struct atkbd *atkbd, unsigned char *param, int command)
468 {
469         int timeout;
470         int send = (command >> 12) & 0xf;
471         int receive = (command >> 8) & 0xf;
472         int rc = -1;
473         int i;
474
475         timeout = msecs_to_jiffies(command == ATKBD_CMD_RESET_BAT ? 4000 : 500);
476
477         down(&atkbd->cmd_sem);
478         clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
479
480         if (receive && param)
481                 for (i = 0; i < receive; i++)
482                         atkbd->cmdbuf[(receive - 1) - i] = param[i];
483
484         atkbd->cmdcnt = receive;
485
486         if (command & 0xff)
487                 if (atkbd_sendbyte(atkbd, command & 0xff))
488                         goto out;
489
490         for (i = 0; i < send; i++)
491                 if (atkbd_sendbyte(atkbd, param[i]))
492                         goto out;
493
494         timeout = wait_event_interruptible_timeout(atkbd->wait,
495                                 !test_bit(ATKBD_FLAG_CMD1, &atkbd->flags), timeout);
496
497         if (atkbd->cmdcnt && timeout > 0) {
498                 if (command == ATKBD_CMD_RESET_BAT && jiffies_to_msecs(timeout) > 100)
499                         timeout = msecs_to_jiffies(100);
500
501                 if (command == ATKBD_CMD_GETID &&
502                     atkbd->cmdbuf[receive - 1] != 0xab && atkbd->cmdbuf[receive - 1] != 0xac) {
503                         /*
504                          * Device behind the port is not a keyboard
505                          * so we don't need to wait for the 2nd byte
506                          * of ID response.
507                          */
508                         clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
509                         atkbd->cmdcnt = 0;
510                 }
511
512                 wait_event_interruptible_timeout(atkbd->wait,
513                                 !test_bit(ATKBD_FLAG_CMD, &atkbd->flags), timeout);
514         }
515
516         if (param)
517                 for (i = 0; i < receive; i++)
518                         param[i] = atkbd->cmdbuf[(receive - 1) - i];
519
520         if (atkbd->cmdcnt && (command != ATKBD_CMD_RESET_BAT || atkbd->cmdcnt != 1))
521                 goto out;
522
523         rc = 0;
524
525 out:
526         clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);
527         clear_bit(ATKBD_FLAG_CMD1, &atkbd->flags);
528         up(&atkbd->cmd_sem);
529
530         return rc;
531 }
532
533 /*
534  * atkbd_execute_scheduled_command() sends a command, previously scheduled by
535  * atkbd_schedule_command(), to the keyboard.
536  */
537
538 static void atkbd_execute_scheduled_command(void *data)
539 {
540         struct atkbd_work *atkbd_work = data;
541
542         atkbd_command(atkbd_work->atkbd, atkbd_work->param, atkbd_work->command);
543
544         kfree(atkbd_work);
545 }
546
547 /*
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
550  * context.
551  */
552
553 static int atkbd_schedule_command(struct atkbd *atkbd, unsigned char *param, int command)
554 {
555         struct atkbd_work *atkbd_work;
556         int send = (command >> 12) & 0xf;
557         int receive = (command >> 8) & 0xf;
558
559         if (!test_bit(ATKBD_FLAG_ENABLED, &atkbd->flags))
560                 return -1;
561
562         if (!(atkbd_work = kmalloc(sizeof(struct atkbd_work) + max(send, receive), GFP_ATOMIC)))
563                 return -1;
564
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);
570
571         if (!schedule_work(&atkbd_work->work)) {
572                 kfree(atkbd_work);
573                 return -1;
574         }
575
576         return 0;
577 }
578
579
580 /*
581  * Event callback from the input module. Events that change the state of
582  * the hardware are processed here.
583  */
584
585 static int atkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
586 {
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];
594         int i, j;
595
596         if (!atkbd->write)
597                 return -1;
598
599         switch (type) {
600
601                 case EV_LED:
602
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);
607
608                         if (atkbd->extra) {
609                                 param[0] = 0;
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);
616                         }
617
618                         return 0;
619
620
621                 case EV_REP:
622
623                         if (atkbd_softrepeat) return 0;
624
625                         i = j = 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);
632
633                         return 0;
634         }
635
636         return -1;
637 }
638
639 /*
640  * atkbd_probe() probes for an AT keyboard on a serio port.
641  */
642
643 static int atkbd_probe(struct atkbd *atkbd)
644 {
645         unsigned char param[2];
646
647 /*
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.
651  */
652
653         if (atkbd_reset)
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);
656
657 /*
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.
662  */
663
664         param[0] = param[1] = 0xa5;     /* initialize with invalid values */
665         if (atkbd_command(atkbd, param, ATKBD_CMD_GETID)) {
666
667 /*
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.
671  */
672                 param[0] = 0;
673                 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
674                         return -1;
675                 atkbd->id = 0xabba;
676                 return 0;
677         }
678
679         if (param[0] != 0xab && param[0] != 0xac)
680                 return -1;
681         atkbd->id = (param[0] << 8) | param[1];
682
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");
686                 return -1;
687         }
688
689         return 0;
690 }
691
692 /*
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 ...)
696  */
697
698 static int atkbd_set_3(struct atkbd *atkbd)
699 {
700         unsigned char param[2];
701
702 /*
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.
706  */
707
708         if (atkbd->translated)
709                 return 2;
710
711         if (atkbd->id == 0xaca1) {
712                 param[0] = 3;
713                 atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET);
714                 return 3;
715         }
716
717         if (atkbd_extra) {
718                 param[0] = 0x71;
719                 if (!atkbd_command(atkbd, param, ATKBD_CMD_EX_ENABLE)) {
720                         atkbd->extra = 1;
721                         return 2;
722                 }
723         }
724
725         if (atkbd_set != 3)
726                 return 2;
727
728         if (!atkbd_command(atkbd, param, ATKBD_CMD_OK_GETID)) {
729                 atkbd->id = param[0] << 8 | param[1];
730                 return 2;
731         }
732
733         param[0] = 3;
734         if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
735                 return 2;
736
737         param[0] = 0;
738         if (atkbd_command(atkbd, param, ATKBD_CMD_GSCANSET))
739                 return 2;
740
741         if (param[0] != 3) {
742                 param[0] = 2;
743                 if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
744                 return 2;
745         }
746
747         atkbd_command(atkbd, param, ATKBD_CMD_SETALL_MBR);
748
749         return 3;
750 }
751
752 static int atkbd_enable(struct atkbd *atkbd)
753 {
754         unsigned char param[1];
755
756 /*
757  * Set the LEDs to a defined state.
758  */
759
760         param[0] = 0;
761         if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
762                 return -1;
763
764 /*
765  * Set autorepeat to fastest possible.
766  */
767
768         param[0] = 0;
769         if (atkbd_command(atkbd, param, ATKBD_CMD_SETREP))
770                 return -1;
771
772 /*
773  * Enable the keyboard to receive keystrokes.
774  */
775
776         if (atkbd_command(atkbd, NULL, ATKBD_CMD_ENABLE)) {
777                 printk(KERN_ERR "atkbd.c: Failed to enable keyboard on %s\n",
778                         atkbd->serio->phys);
779                 return -1;
780         }
781
782         return 0;
783 }
784
785 /*
786  * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a
787  * reboot.
788  */
789
790 static void atkbd_cleanup(struct serio *serio)
791 {
792         struct atkbd *atkbd = serio->private;
793         atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT);
794 }
795
796 /*
797  * atkbd_disconnect() closes and frees.
798  */
799
800 static void atkbd_disconnect(struct serio *serio)
801 {
802         struct atkbd *atkbd = serio->private;
803
804         clear_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
805         synchronize_kernel();
806         flush_scheduled_work();
807
808         input_unregister_device(&atkbd->dev);
809         serio_close(serio);
810         kfree(atkbd);
811 }
812
813 /*
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.
818  */
819
820 static void atkbd_connect(struct serio *serio, struct serio_driver *drv)
821 {
822         struct atkbd *atkbd;
823         int i;
824
825         if (!(atkbd = kmalloc(sizeof(struct atkbd), GFP_KERNEL)))
826                 return;
827         memset(atkbd, 0, sizeof(struct atkbd));
828
829         init_MUTEX(&atkbd->cmd_sem);
830         init_waitqueue_head(&atkbd->wait);
831
832         switch (serio->type & SERIO_TYPE) {
833
834                 case SERIO_8042_XL:
835                         atkbd->translated = 1;
836                 case SERIO_8042:
837                         if (serio->write)
838                                 atkbd->write = 1;
839                         break;
840                 case SERIO_RS232:
841                         if ((serio->type & SERIO_PROTO) == SERIO_PS2SER)
842                                 break;
843                 default:
844                         kfree(atkbd);
845                         return;
846         }
847
848         if (!atkbd->write)
849                 atkbd_softrepeat = 1;
850         if (atkbd_softrepeat)
851                 atkbd_softraw = 1;
852
853         if (atkbd->write) {
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);
858
859         if (!atkbd_softrepeat) {
860                 atkbd->dev.rep[REP_DELAY] = 250;
861                 atkbd->dev.rep[REP_PERIOD] = 33;
862         } else atkbd_softraw = 1;
863
864         atkbd->serio = serio;
865
866         init_input_dev(&atkbd->dev);
867
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;
873
874         serio->private = atkbd;
875
876         if (serio_open(serio, drv)) {
877                 kfree(atkbd);
878                 return;
879         }
880
881         if (atkbd->write) {
882
883                 if (atkbd_probe(atkbd)) {
884                         serio_close(serio);
885                         serio->private = NULL;
886                         kfree(atkbd);
887                         return;
888                 }
889
890                 atkbd->set = atkbd_set_3(atkbd);
891                 atkbd_enable(atkbd);
892
893         } else {
894                 atkbd->set = 2;
895                 atkbd->id = 0xab00;
896         }
897
898         if (atkbd->extra) {
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");
901         } else
902                 sprintf(atkbd->name, "AT %s Set %d keyboard",
903                         atkbd->translated ? "Translated" : "Raw", atkbd->set);
904
905         sprintf(atkbd->phys, "%s/input0", serio->phys);
906
907         if (atkbd_scroll) {
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);
913         }
914
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];
919                 }
920         } else if (atkbd->set == 3) {
921                 memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode));
922         } else {
923                 memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));
924         }
925
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;
932
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);
936
937         input_register_device(&atkbd->dev);
938
939         set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
940
941         printk(KERN_INFO "input: %s on %s\n", atkbd->name, serio->phys);
942 }
943
944 /*
945  * atkbd_reconnect() tries to restore keyboard into a sane state and is
946  * most likely called on resume.
947  */
948
949 static int atkbd_reconnect(struct serio *serio)
950 {
951         struct atkbd *atkbd = serio->private;
952         struct serio_driver *drv = serio->drv;
953         unsigned char param[1];
954
955         if (!drv) {
956                 printk(KERN_DEBUG "atkbd: reconnect request, but serio is disconnected, ignoring...\n");
957                 return -1;
958         }
959
960         if (atkbd->write) {
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);
964
965                 if (atkbd_probe(atkbd))
966                         return -1;
967                 if (atkbd->set != atkbd_set_3(atkbd))
968                         return -1;
969
970                 atkbd_enable(atkbd);
971
972                 if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
973                         return -1;
974         }
975
976         set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);
977
978         return 0;
979 }
980
981 static struct serio_driver atkbd_drv = {
982         .driver         = {
983                 .name   = "atkbd",
984         },
985         .description    = DRIVER_DESC,
986         .interrupt      = atkbd_interrupt,
987         .connect        = atkbd_connect,
988         .reconnect      = atkbd_reconnect,
989         .disconnect     = atkbd_disconnect,
990         .cleanup        = atkbd_cleanup,
991 };
992
993 int __init atkbd_init(void)
994 {
995         serio_register_driver(&atkbd_drv);
996         return 0;
997 }
998
999 void __exit atkbd_exit(void)
1000 {
1001         serio_unregister_driver(&atkbd_drv);
1002 }
1003
1004 module_init(atkbd_init);
1005 module_exit(atkbd_exit);