Subversion Repositories shark

Rev

Rev 518 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
495 giacomo 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 <linuxcomp.h>
21
 
22
#include <linux/delay.h>
23
#include <linux/module.h>
24
#include <linux/slab.h>
25
#include <linux/interrupt.h>
26
#include <linux/init.h>
27
#include <linux/input.h>
28
#include <linux/serio.h>
29
#include <linux/workqueue.h>
30
#include <linux/timer.h>
31
 
32
//#define ATKBD_DEBUG
33
 
34
MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35
MODULE_DESCRIPTION("AT and PS/2 keyboard driver");
36
MODULE_PARM(atkbd_set, "1i");
37
MODULE_PARM(atkbd_reset, "1i");
38
MODULE_PARM(atkbd_softrepeat, "1i");
39
MODULE_LICENSE("GPL");
40
 
41
static int atkbd_set = 2;
42
#if defined(__i386__) || defined (__x86_64__)
43
static int atkbd_reset;
44
#else
45
static int atkbd_reset = 1;
46
#endif
47
static int atkbd_softrepeat;
48
 
49
/*
50
 * Scancode to keycode tables. These are just the default setting, and
51
 * are loadable via an userland utility.
52
 */
53
 
54
static unsigned char atkbd_set2_keycode[512] = {
55
          0, 67, 65, 63, 61, 59, 60, 88,  0, 68, 66, 64, 62, 15, 41, 85,
56
          0, 56, 42,182, 29, 16,  2, 89,  0,  0, 44, 31, 30, 17,  3, 90,
57
          0, 46, 45, 32, 18,  5,  4, 91, 90, 57, 47, 33, 20, 19,  6,  0,
58
         91, 49, 48, 35, 34, 21,  7,  0,  0,  0, 50, 36, 22,  8,  9,  0,
59
          0, 51, 37, 23, 24, 11, 10,  0,  0, 52, 53, 38, 39, 25, 12,  0,
60
        122, 89, 40,120, 26, 13,  0,  0, 58, 54, 28, 27,  0, 43,  0,  0,
61
         85, 86, 90, 91, 92, 93, 14, 94, 95, 79,183, 75, 71,121,  0,123,
62
         82, 83, 80, 76, 77, 72,  1, 69, 87, 78, 81, 74, 55, 73, 70, 99,
63
          0,  0,  0, 65, 99,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
64
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
65
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
66
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
67
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
68
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
69
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
70
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,255,
71
          0,  0, 92, 90, 85,  0,137,  0,  0,  0,  0, 91, 89,144,115,  0,
72
        217,100,255,  0, 97,165,164,  0,156,  0,  0,140,115,  0,  0,125,
73
        173,114,  0,113,152,163,151,126,128,166,  0,140,  0,147,  0,127,
74
        159,167,115,160,164,  0,  0,116,158,  0,150,166,  0,  0,  0,142,
75
        157,  0,114,166,168,  0,  0,213,155,  0, 98,113,  0,163,  0,138,
76
        226,  0,  0,  0,  0,  0,153,140,  0,255, 96,  0,  0,  0,143,  0,
77
        133,  0,116,  0,143,  0,174,133,  0,107,  0,105,102,  0,  0,112,
78
        110,111,108,112,106,103,  0,119,  0,118,109,  0, 99,104,119
79
};
80
 
81
static unsigned char atkbd_set3_keycode[512] = {
82
          0,  0,  0,  0,  0,  0,  0, 59,  1,138,128,129,130, 15, 41, 60,
83
        131, 29, 42, 86, 58, 16,  2, 61,133, 56, 44, 31, 30, 17,  3, 62,
84
        134, 46, 45, 32, 18,  5,  4, 63,135, 57, 47, 33, 20, 19,  6, 64,
85
        136, 49, 48, 35, 34, 21,  7, 65,137,100, 50, 36, 22,  8,  9, 66,
86
        125, 51, 37, 23, 24, 11, 10, 67,126, 52, 53, 38, 39, 25, 12, 68,
87
        113,114, 40, 84, 26, 13, 87, 99, 97, 54, 28, 27, 43, 84, 88, 70,
88
        108,105,119,103,111,107, 14,110,  0, 79,106, 75, 71,109,102,104,
89
         82, 83, 80, 76, 77, 72, 69, 98,  0, 96, 81,  0, 78, 73, 55, 85,
90
         89, 90, 91, 92, 74,185,184,182,  0,  0,  0,125,126,127,112,  0,
91
          0,139,150,163,165,115,152,150,166,140,160,154,113,114,167,168,
92
        148,149,147,140,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
93
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
94
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
95
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
96
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
97
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,255
98
};
99
 
100
static unsigned char atkbd_unxlate_table[128] = {
101
          0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13,
102
         21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
103
         35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
104
         50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88,  5,  6,  4, 12,  3,
105
         11,  2, 10,  1,  9,119,126,108,117,125,123,107,115,116,121,105,
106
        114,122,112,113,127, 96, 97,120,  7, 15, 23, 31, 39, 47, 55, 63,
107
         71, 79, 86, 94,  8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111,
108
         19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110
109
};
110
 
111
#define ATKBD_CMD_SETLEDS       0x10ed
112
#define ATKBD_CMD_GSCANSET      0x11f0
113
#define ATKBD_CMD_SSCANSET      0x10f0
114
#define ATKBD_CMD_GETID         0x02f2
115
#define ATKBD_CMD_SETREP        0x10f3
116
#define ATKBD_CMD_ENABLE        0x00f4
117
#define ATKBD_CMD_RESET_DIS     0x00f5
118
#define ATKBD_CMD_SETALL_MBR    0x00fa
119
#define ATKBD_CMD_RESET_BAT     0x02ff
120
#define ATKBD_CMD_RESEND        0x00fe
121
#define ATKBD_CMD_EX_ENABLE     0x10ea
122
#define ATKBD_CMD_EX_SETLEDS    0x20eb
123
#define ATKBD_CMD_OK_GETID      0x02e8
124
 
125
#define ATKBD_RET_ACK           0xfa
126
#define ATKBD_RET_NAK           0xfe
127
#define ATKBD_RET_BAT           0xaa
128
#define ATKBD_RET_EMUL0         0xe0
129
#define ATKBD_RET_EMULX         0x80
130
#define ATKBD_RET_EMUL1         0xe1
131
#define ATKBD_RET_RELEASE       0xf0
132
 
133
#define ATKBD_KEY_UNKNOWN         0
134
#define ATKBD_KEY_NULL          255
135
 
136
/*
137
 * The atkbd control structure
138
 */
139
 
140
struct atkbd {
141
        unsigned char keycode[512];
142
        struct input_dev dev;
143
        struct serio *serio;
144
        struct timer_list timer;
145
        char name[64];
146
        char phys[32];
147
        unsigned char cmdbuf[4];
148
        unsigned char cmdcnt;
149
        unsigned char set;
150
        unsigned char release;
151
        int lastkey;
152
        volatile signed char ack;
153
        unsigned char emul;
154
        unsigned short id;
155
        unsigned char write;
156
        unsigned char translated;
157
        unsigned char resend;
158
        unsigned char bat_xl;
159
        unsigned int last;
160
        unsigned long time;
161
};
162
 
163
/*
164
 * atkbd_interrupt(). Here takes place processing of data received from
165
 * the keyboard into events.
166
 */
167
 
168
static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
169
                        unsigned int flags, struct pt_regs *regs)
170
{
171
        struct atkbd *atkbd = serio->private;
172
        unsigned int code = data;
173
        int value;
174
 
175
#ifdef ATKBD_DEBUG
176
        printk(KERN_DEBUG "atkbd.c: Received %02x flags %02x\n", data, flags);
177
#endif
178
 
179
#if !defined(__i386__) && !defined (__x86_64__)
180
        if ((flags & (SERIO_FRAME | SERIO_PARITY)) && (~flags & SERIO_TIMEOUT) && !atkbd->resend && atkbd->write) {
181
                printk("atkbd.c: frame/parity error: %02x\n", flags);
182
                serio_write(serio, ATKBD_CMD_RESEND);
183
                atkbd->resend = 1;
184
                goto out;
185
        }
186
 
187
        if (!flags && data == ATKBD_RET_ACK)
188
                atkbd->resend = 0;
189
#endif
190
 
191
        switch (code) {
192
                case ATKBD_RET_ACK:
193
                        atkbd->ack = 1;
194
                        goto out;
195
                case ATKBD_RET_NAK:
196
                        atkbd->ack = -1;
197
                        goto out;
198
        }
199
 
200
        if (atkbd->translated) do {
201
 
202
                if (atkbd->emul != 1) {
203
                        if (code == ATKBD_RET_EMUL0 || code == ATKBD_RET_EMUL1)
204
                                break;
205
                        if (code == ATKBD_RET_BAT) {
206
                                if (!atkbd->bat_xl)
207
                                        break;
208
                                atkbd->bat_xl = 0;
209
                        }
210
                        if (code == (ATKBD_RET_BAT & 0x7f))
211
                                atkbd->bat_xl = 1;
212
                }
213
 
214
                if (code < 0x80) {
215
                        code = atkbd_unxlate_table[code];
216
                        break;
217
                }
218
 
219
                if (atkbd->cmdcnt)
220
                        break;
221
 
222
                code = atkbd_unxlate_table[code & 0x7f];
223
                atkbd->release = 1;
224
 
225
        } while (0);
226
 
227
        if (atkbd->cmdcnt) {
228
                atkbd->cmdbuf[--atkbd->cmdcnt] = code;
229
                goto out;
230
        }
231
 
232
        switch (code) {
233
                case ATKBD_RET_BAT:
234
                        serio_rescan(atkbd->serio);
235
                        goto out;
236
                case ATKBD_RET_EMUL0:
237
                        atkbd->emul = 1;
238
                        goto out;
239
                case ATKBD_RET_EMUL1:
240
                        atkbd->emul = 2;
241
                        goto out;
242
                case ATKBD_RET_RELEASE:
243
                        atkbd->release = 1;
244
                        goto out;
245
        }
246
 
247
        if (atkbd->emul) {
248
                if (--atkbd->emul)
249
                        goto out;
250
                code |= 0x100;
251
        }
252
 
253
        switch (atkbd->keycode[code]) {
254
                case ATKBD_KEY_NULL:
255
                        break;
256
                case ATKBD_KEY_UNKNOWN:
257
                        printk(KERN_WARNING "atkbd.c: Unknown key %s (%s set %d, code %#x, data %#x, on %s).\n",
258
                                atkbd->release ? "released" : "pressed",
259
                                atkbd->translated ? "translated" : "raw",
260
                                atkbd->set, code, data, serio->phys);
261
                        break;
262
                default:
263
                        value = atkbd->release ? 0 :
264
                                (1 + (!atkbd_softrepeat && test_bit(atkbd->keycode[code], atkbd->dev.key)));
265
 
266
                        switch (value) {        /* Workaround Toshiba laptop multiple keypress */
267
                                case 0:
268
                                        atkbd->last = 0;
269
                                        break;
270
                                case 1:
271
                                        atkbd->last = code;
272
                                        atkbd->time = jiffies26 + (atkbd->dev.rep[REP_DELAY] * HZ + 500) / 1000 / 2;
273
                                        break;
274
                                case 2:
275
                                        if (!time_after(jiffies26, atkbd->time) && atkbd->last == code)
276
                                                value = 1;
277
                                        break;
278
                        }
279
 
280
                        input_regs(&atkbd->dev, regs);
281
                        input_event(&atkbd->dev, EV_KEY, atkbd->keycode[code], value);
282
                        input_sync(&atkbd->dev);
283
        }
284
 
285
        atkbd->release = 0;
286
out:
287
        return IRQ_HANDLED;
288
}
289
 
290
/*
291
 * atkbd_sendbyte() sends a byte to the keyboard, and waits for
292
 * acknowledge. It doesn't handle resends according to the keyboard
293
 * protocol specs, because if these are needed, the keyboard needs
294
 * replacement anyway, and they only make a mess in the protocol.
295
 */
296
 
297
static int atkbd_sendbyte(struct atkbd *atkbd, unsigned char byte)
298
{
299
        int timeout = 20000; /* 200 msec */
300
        atkbd->ack = 0;
301
 
302
#ifdef ATKBD_DEBUG
303
        printk(KERN_DEBUG "atkbd.c: Sent: %02x\n", byte);
304
#endif
305
        if (serio_write(atkbd->serio, byte))
306
                return -1;
307
 
308
        while (!atkbd->ack && timeout--) udelay(10);
309
 
310
        return -(atkbd->ack <= 0);
311
}
312
 
313
/*
314
 * atkbd_command() sends a command, and its parameters to the keyboard,
315
 * then waits for the response and puts it in the param array.
316
 */
317
 
318
static int atkbd_command(struct atkbd *atkbd, unsigned char *param, int command)
319
{
320
        int timeout = 500000; /* 500 msec */
321
        int send = (command >> 12) & 0xf;
322
        int receive = (command >> 8) & 0xf;
323
        int i;
324
 
325
        atkbd->cmdcnt = receive;
326
 
327
        if (command == ATKBD_CMD_RESET_BAT)
328
                timeout = 2000000; /* 2 sec */
329
 
330
        if (command & 0xff)
331
                if (atkbd_sendbyte(atkbd, command & 0xff))
332
                        return (atkbd->cmdcnt = 0) - 1;
333
 
334
        for (i = 0; i < send; i++)
335
                if (atkbd_sendbyte(atkbd, param[i]))
336
                        return (atkbd->cmdcnt = 0) - 1;
337
 
338
        while (atkbd->cmdcnt && timeout--) {
339
 
340
                if (atkbd->cmdcnt == 1 &&
341
                    command == ATKBD_CMD_RESET_BAT && timeout > 100000)
342
                        timeout = 100000;
343
 
344
                if (atkbd->cmdcnt == 1 && command == ATKBD_CMD_GETID &&
345
                    atkbd->cmdbuf[1] != 0xab && atkbd->cmdbuf[1] != 0xac) {
346
                        atkbd->cmdcnt = 0;
347
                        break;
348
                }
349
 
350
                udelay(1);
351
        }
352
 
353
        if (param)
354
                for (i = 0; i < receive; i++)
355
                        param[i] = atkbd->cmdbuf[(receive - 1) - i];
356
 
357
        if (command == ATKBD_CMD_RESET_BAT && atkbd->cmdcnt == 1)
358
                atkbd->cmdcnt = 0;
359
 
360
        if (atkbd->cmdcnt) {
361
                atkbd->cmdcnt = 0;
362
                return -1;
363
        }
364
 
365
        return 0;
366
}
367
 
368
/*
369
 * Event callback from the input module. Events that change the state of
370
 * the hardware are processed here.
371
 */
372
 
373
static int atkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
374
{
375
        struct atkbd *atkbd = dev->private;
376
        struct { int p; u8 v; } period[] =     
377
                { {30, 0x00}, {25, 0x02}, {20, 0x04}, {15, 0x08}, {10, 0x0c}, {7, 0x10}, {5, 0x14}, {0, 0x14} };
378
        struct { int d; u8 v; } delay[] =
379
                { {1000, 0x60}, {750, 0x40}, {500, 0x20}, {250, 0x00}, {0, 0x00} };
380
        char param[2];
381
        int i, j;
382
 
383
        if (!atkbd->write)
384
                return -1;
385
 
386
        switch (type) {
387
 
388
                case EV_LED:
389
 
390
                        *param = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0)
391
                               | (test_bit(LED_NUML,    dev->led) ? 2 : 0)
392
                               | (test_bit(LED_CAPSL,   dev->led) ? 4 : 0);
393
                        atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS);
394
 
395
                        if (atkbd->set == 4) {
396
                                param[0] = 0;
397
                                param[1] = (test_bit(LED_COMPOSE, dev->led) ? 0x01 : 0)
398
                                         | (test_bit(LED_SLEEP,   dev->led) ? 0x02 : 0)
399
                                         | (test_bit(LED_SUSPEND, dev->led) ? 0x04 : 0)
400
                                         | (test_bit(LED_MISC,    dev->led) ? 0x10 : 0)
401
                                         | (test_bit(LED_MUTE,    dev->led) ? 0x20 : 0);
402
                                atkbd_command(atkbd, param, ATKBD_CMD_EX_SETLEDS);
403
                        }
404
 
405
                        return 0;
406
 
407
 
408
                case EV_REP:
409
 
410
                        if (atkbd_softrepeat) return 0;
411
 
412
                        i = j = 0;
413
                        while (period[i].p > dev->rep[REP_PERIOD]) i++;
414
                        while (delay[j].d > dev->rep[REP_DELAY]) j++;
415
                        dev->rep[REP_PERIOD] = period[i].p;
416
                        dev->rep[REP_DELAY] = delay[j].d;
417
                        param[0] = period[i].v | delay[j].v;
418
                        atkbd_command(atkbd, param, ATKBD_CMD_SETREP);
419
 
420
                        return 0;
421
        }
422
 
423
        return -1;
424
}
425
 
426
/*
427
 * atkbd_probe() probes for an AT keyboard on a serio port.
428
 */
429
 
430
static int atkbd_probe(struct atkbd *atkbd)
431
{
432
        unsigned char param[2];
433
 
434
/*
435
 * Some systems, where the bit-twiddling when testing the io-lines of the
436
 * controller may confuse the keyboard need a full reset of the keyboard. On
437
 * these systems the BIOS also usually doesn't do it for us.
438
 */
439
 
440
        if (atkbd_reset)
441
                if (atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT))
442
                        printk(KERN_WARNING "atkbd.c: keyboard reset failed on %s\n", atkbd->serio->phys);
443
 
444
/*
445
 * Then we check the keyboard ID. We should get 0xab83 under normal conditions.
446
 * Some keyboards report different values, but the first byte is always 0xab or
447
 * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this
448
 * should make sure we don't try to set the LEDs on it.
449
 */
450
 
451
        if (atkbd_command(atkbd, param, ATKBD_CMD_GETID)) {
452
 
453
/*
454
 * If the get ID command failed, we check if we can at least set the LEDs on
455
 * the keyboard. This should work on every keyboard out there. It also turns
456
 * the LEDs off, which we want anyway.
457
 */
458
                param[0] = 0;
459
                if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
460
                        return -1;
461
                atkbd->id = 0xabba;
462
                return 0;
463
        }
464
 
465
        if (param[0] != 0xab && param[0] != 0xac)
466
                return -1;
467
        atkbd->id = (param[0] << 8) | param[1];
468
 
469
 
470
        return 0;
471
}
472
 
473
/*
474
 * atkbd_set_3 checks if a keyboard has a working Set 3 support, and
475
 * sets it into that. Unfortunately there are keyboards that can be switched
476
 * to Set 3, but don't work well in that (BTC Multimedia ...)
477
 */
478
 
479
static int atkbd_set_3(struct atkbd *atkbd)
480
{
481
        unsigned char param[2];
482
 
483
/*
484
 * For known special keyboards we can go ahead and set the correct set.
485
 * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and
486
 * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards.
487
 */
488
 
489
        if (atkbd->translated)
490
                return 2;
491
 
492
        if (atkbd->id == 0xaca1) {
493
                param[0] = 3;
494
                atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET);
495
                return 3;
496
        }
497
 
498
        if (atkbd_set != 2)
499
                if (!atkbd_command(atkbd, param, ATKBD_CMD_OK_GETID)) {
500
                        atkbd->id = param[0] << 8 | param[1];
501
                        return 2;
502
                }
503
 
504
        if (atkbd_set == 4) {
505
                param[0] = 0x71;
506
                if (!atkbd_command(atkbd, param, ATKBD_CMD_EX_ENABLE))
507
                        return 4;
508
        }
509
 
510
        if (atkbd_set != 3)
511
                return 2;
512
 
513
        param[0] = 3;
514
        if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
515
                return 2;
516
 
517
        param[0] = 0;
518
        if (atkbd_command(atkbd, param, ATKBD_CMD_GSCANSET))
519
                return 2;
520
 
521
        if (param[0] != 3) {
522
                param[0] = 2;
523
                if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))
524
                return 2;
525
        }
526
 
527
        atkbd_command(atkbd, param, ATKBD_CMD_SETALL_MBR);
528
 
529
        return 3;
530
}
531
 
532
static int atkbd_enable(struct atkbd *atkbd)
533
{
534
        unsigned char param[1];
535
 
536
/*
537
 * Set the LEDs to a defined state.
538
 */
539
 
540
        param[0] = 0;
541
        if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))
542
                return -1;
543
 
544
/*
545
 * Set autorepeat to fastest possible.
546
 */
547
 
548
        param[0] = 0;
549
        if (atkbd_command(atkbd, param, ATKBD_CMD_SETREP))
550
                return -1;
551
 
552
/*
553
 * Enable the keyboard to receive keystrokes.
554
 */
555
 
556
        if (atkbd_command(atkbd, NULL, ATKBD_CMD_ENABLE)) {
557
                printk(KERN_ERR "atkbd.c: Failed to enable keyboard on %s\n",
558
                        atkbd->serio->phys);
559
                return -1;
560
        }
561
 
562
        return 0;
563
}
564
 
565
/*
566
 * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a
567
 * reboot.
568
 */
569
 
570
static void atkbd_cleanup(struct serio *serio)
571
{
572
        struct atkbd *atkbd = serio->private;
573
        atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT);
574
}
575
 
576
/*
577
 * atkbd_disconnect() closes and frees.
578
 */
579
 
580
static void atkbd_disconnect(struct serio *serio)
581
{
582
        struct atkbd *atkbd = serio->private;
583
        input_unregister_device(&atkbd->dev);
584
        serio_close(serio);
585
        kfree(atkbd);
586
}
587
 
588
/*
589
 * atkbd_connect() is called when the serio module finds and interface
590
 * that isn't handled yet by an appropriate device driver. We check if
591
 * there is an AT keyboard out there and if yes, we register ourselves
592
 * to the input module.
593
 */
594
 
595
static void atkbd_connect(struct serio *serio, struct serio_dev *dev)
596
{
597
        struct atkbd *atkbd;
598
        int i;
599
 
600
        if (!(atkbd = kmalloc(sizeof(struct atkbd), GFP_KERNEL)))
601
                return;
602
        memset(atkbd, 0, sizeof(struct atkbd));
603
 
604
        switch (serio->type & SERIO_TYPE) {
605
 
606
                case SERIO_8042_XL:
607
                        atkbd->translated = 1;
608
                case SERIO_8042:
609
                        if (serio->write)
610
                                atkbd->write = 1;
611
                        break;
612
                case SERIO_RS232:
613
                        if ((serio->type & SERIO_PROTO) == SERIO_PS2SER)
614
                                break;
615
                default:
616
                        kfree(atkbd);
617
                        return;
618
        }
619
 
620
        if (atkbd->write) {
621
                atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);
622
                atkbd->dev.ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);
623
        } else  atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
624
 
625
        if (!atkbd_softrepeat) {
626
                atkbd->dev.rep[REP_DELAY] = 250;
627
                atkbd->dev.rep[REP_PERIOD] = 33;
628
        }
629
 
630
        atkbd->serio = serio;
631
 
632
        init_input_dev(&atkbd->dev);
633
 
634
        atkbd->dev.keycode = atkbd->keycode;
635
        atkbd->dev.keycodesize = sizeof(unsigned char);
636
        atkbd->dev.keycodemax = ARRAY_SIZE(atkbd_set2_keycode);
637
        atkbd->dev.event = atkbd_event;
638
        atkbd->dev.private = atkbd;
639
 
640
        serio->private = atkbd;
641
 
642
        if (serio_open(serio, dev)) {
643
                kfree(atkbd);
644
                return;
645
        }
646
 
647
        if (atkbd->write) {
648
 
649
                if (atkbd_probe(atkbd)) {
650
                        serio_close(serio);
651
                        kfree(atkbd);
652
                        return;
653
                }
654
 
655
                atkbd->set = atkbd_set_3(atkbd);
656
                atkbd_enable(atkbd);
657
 
658
        } else {
659
                atkbd->set = 2;
660
                atkbd->id = 0xab00;
661
        }
662
 
663
        if (atkbd->set == 4) {
664
                atkbd->dev.ledbit[0] |= BIT(LED_COMPOSE) | BIT(LED_SUSPEND) | BIT(LED_SLEEP) | BIT(LED_MUTE) | BIT(LED_MISC);
665
                sprintf26(atkbd->name, "AT Set 2 Extended keyboard");
666
        } else
667
                sprintf26(atkbd->name, "AT %s Set %d keyboard",
668
                        atkbd->translated ? "Translated" : "Raw", atkbd->set);
669
 
670
        sprintf26(atkbd->phys, "%s/input0", serio->phys);
671
 
672
        if (atkbd->set == 3)
673
                memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode));
674
        else
675
                memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));
676
 
677
        atkbd->dev.name = atkbd->name;
678
        atkbd->dev.phys = atkbd->phys;
679
        atkbd->dev.id.bustype = BUS_I8042;
680
        atkbd->dev.id.vendor = 0x0001;
681
        atkbd->dev.id.product = atkbd->set;
682
        atkbd->dev.id.version = atkbd->id;
683
 
684
        for (i = 0; i < 512; i++)
685
                if (atkbd->keycode[i] && atkbd->keycode[i] < 255)
686
                        set_bit(atkbd->keycode[i], atkbd->dev.keybit);
687
 
688
        input_register_device(&atkbd->dev);
689
 
690
        printk(KERN_INFO "input: %s on %s\n", atkbd->name, serio->phys);
691
}
692
 
693
 
694
static struct serio_dev atkbd_dev = {
695
        .interrupt =    atkbd_interrupt,
696
        .connect =      atkbd_connect,
697
        .disconnect =   atkbd_disconnect,
698
        .cleanup =      atkbd_cleanup,
699
};
700
 
701
#ifndef MODULE
702
static int __init atkbd_setup_set(char *str)
703
{
704
        int ints[4];
705
        str = get_options(str, ARRAY_SIZE(ints), ints);
706
        if (ints[0] > 0) atkbd_set = ints[1];
707
        return 1;
708
}
709
static int __init atkbd_setup_reset(char *str)
710
{
711
        int ints[4];
712
        str = get_options(str, ARRAY_SIZE(ints), ints);
713
        if (ints[0] > 0) atkbd_reset = ints[1];
714
        return 1;
715
}
716
 
717
__setup("atkbd_set=", atkbd_setup_set);
718
__setup("atkbd_reset", atkbd_setup_reset);
719
#endif
720
 
721
int __init atkbd_init(void)
722
{
723
        serio_register_device(&atkbd_dev);
724
        return 0;
725
}
726
 
727
void __exit atkbd_exit(void)
728
{
729
        serio_unregister_device(&atkbd_dev);
730
}
731
 
732
module_init(atkbd_init);
733
module_exit(atkbd_exit);