This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / input / serio / i8042.c
1 /*
2  *  i8042 keyboard and mouse controller driver for Linux
3  *
4  *  Copyright (c) 1999-2004 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 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/config.h>
19 #include <linux/reboot.h>
20 #include <linux/init.h>
21 #include <linux/sysdev.h>
22 #include <linux/pm.h>
23 #include <linux/serio.h>
24 #include <linux/pci.h>
25 #include <linux/err.h>
26
27 #include <asm/io.h>
28
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
30 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
31 MODULE_LICENSE("GPL");
32
33 static unsigned int i8042_noaux;
34 module_param_named(noaux, i8042_noaux, bool, 0);
35 MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
36
37 static unsigned int i8042_nomux;
38 module_param_named(nomux, i8042_nomux, bool, 0);
39 MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing conrtoller is present.");
40
41 static unsigned int i8042_unlock;
42 module_param_named(unlock, i8042_unlock, bool, 0);
43 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
44
45 static unsigned int i8042_reset;
46 module_param_named(reset, i8042_reset, bool, 0);
47 MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
48
49 static unsigned int i8042_direct;
50 module_param_named(direct, i8042_direct, bool, 0);
51 MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
52
53 static unsigned int i8042_dumbkbd;
54 module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
55 MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
56
57 static unsigned int i8042_noloop;
58 module_param_named(noloop, i8042_noloop, bool, 0);
59 MODULE_PARM_DESC(dumbkbd, "Disable the AUX Loopback command while probing for the AUX port");
60
61 __obsolete_setup("i8042_noaux");
62 __obsolete_setup("i8042_nomux");
63 __obsolete_setup("i8042_unlock");
64 __obsolete_setup("i8042_reset");
65 __obsolete_setup("i8042_direct");
66 __obsolete_setup("i8042_dumbkbd");
67
68 #undef DEBUG
69 #include "i8042.h"
70
71 spinlock_t i8042_lock = SPIN_LOCK_UNLOCKED;
72
73 struct i8042_values {
74         int irq;
75         unsigned char disable;
76         unsigned char irqen;
77         unsigned char exists;
78         signed char mux;
79         char name[8];
80 };
81
82 static struct i8042_values i8042_kbd_values = {
83         .disable        = I8042_CTR_KBDDIS,
84         .irqen          = I8042_CTR_KBDINT,
85         .mux            = -1,
86         .name           = "KBD",
87 };
88
89 static struct i8042_values i8042_aux_values = {
90         .disable        = I8042_CTR_AUXDIS,
91         .irqen          = I8042_CTR_AUXINT,
92         .mux            = -1,
93         .name           = "AUX",
94 };
95
96 static struct i8042_values i8042_mux_values[I8042_NUM_MUX_PORTS];
97
98 static struct serio *i8042_kbd_port;
99 static struct serio *i8042_aux_port;
100 static struct serio *i8042_mux_port[I8042_NUM_MUX_PORTS];
101 static unsigned char i8042_initial_ctr;
102 static unsigned char i8042_ctr;
103 static unsigned char i8042_mux_open;
104 static unsigned char i8042_mux_present;
105 static struct pm_dev *i8042_pm_dev;
106 static struct timer_list i8042_timer;
107 static struct platform_device *i8042_platform_device;
108
109 /*
110  * Shared IRQ's require a device pointer, but this driver doesn't support
111  * multiple devices
112  */
113 #define i8042_request_irq_cookie (&i8042_timer)
114
115 static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs);
116
117 /*
118  * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
119  * be ready for reading values from it / writing values to it.
120  * Called always with i8042_lock held.
121  */
122
123 static int i8042_wait_read(void)
124 {
125         int i = 0;
126         while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
127                 udelay(50);
128                 i++;
129         }
130         return -(i == I8042_CTL_TIMEOUT);
131 }
132
133 static int i8042_wait_write(void)
134 {
135         int i = 0;
136         while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
137                 udelay(50);
138                 i++;
139         }
140         return -(i == I8042_CTL_TIMEOUT);
141 }
142
143 /*
144  * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
145  * of the i8042 down the toilet.
146  */
147
148 static int i8042_flush(void)
149 {
150         unsigned long flags;
151         unsigned char data;
152         int i = 0;
153
154         spin_lock_irqsave(&i8042_lock, flags);
155
156         while ((i8042_read_status() & I8042_STR_OBF) && (i++ < I8042_BUFFER_SIZE)) {
157                 udelay(50);
158                 data = i8042_read_data();
159                 dbg("%02x <- i8042 (flush, %s)", data,
160                         i8042_read_status() & I8042_STR_AUXDATA ? "aux" : "kbd");
161         }
162
163         spin_unlock_irqrestore(&i8042_lock, flags);
164
165         return i;
166 }
167
168 /*
169  * i8042_command() executes a command on the i8042. It also sends the input
170  * parameter(s) of the commands to it, and receives the output value(s). The
171  * parameters are to be stored in the param array, and the output is placed
172  * into the same array. The number of the parameters and output values is
173  * encoded in bits 8-11 of the command number.
174  */
175
176 static int i8042_command(unsigned char *param, int command)
177 {
178         unsigned long flags;
179         int retval = 0, i = 0;
180
181         if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
182                 return -1;
183
184         spin_lock_irqsave(&i8042_lock, flags);
185
186         retval = i8042_wait_write();
187         if (!retval) {
188                 dbg("%02x -> i8042 (command)", command & 0xff);
189                 i8042_write_command(command & 0xff);
190         }
191
192         if (!retval)
193                 for (i = 0; i < ((command >> 12) & 0xf); i++) {
194                         if ((retval = i8042_wait_write())) break;
195                         dbg("%02x -> i8042 (parameter)", param[i]);
196                         i8042_write_data(param[i]);
197                 }
198
199         if (!retval)
200                 for (i = 0; i < ((command >> 8) & 0xf); i++) {
201                         if ((retval = i8042_wait_read())) break;
202                         if (i8042_read_status() & I8042_STR_AUXDATA)
203                                 param[i] = ~i8042_read_data();
204                         else
205                                 param[i] = i8042_read_data();
206                         dbg("%02x <- i8042 (return)", param[i]);
207                 }
208
209         spin_unlock_irqrestore(&i8042_lock, flags);
210
211         if (retval)
212                 dbg("     -- i8042 (timeout)");
213
214         return retval;
215 }
216
217 /*
218  * i8042_kbd_write() sends a byte out through the keyboard interface.
219  */
220
221 static int i8042_kbd_write(struct serio *port, unsigned char c)
222 {
223         unsigned long flags;
224         int retval = 0;
225
226         spin_lock_irqsave(&i8042_lock, flags);
227
228         if(!(retval = i8042_wait_write())) {
229                 dbg("%02x -> i8042 (kbd-data)", c);
230                 i8042_write_data(c);
231         }
232
233         spin_unlock_irqrestore(&i8042_lock, flags);
234
235         return retval;
236 }
237
238 /*
239  * i8042_aux_write() sends a byte out through the aux interface.
240  */
241
242 static int i8042_aux_write(struct serio *port, unsigned char c)
243 {
244         struct i8042_values *values = port->port_data;
245         int retval;
246
247 /*
248  * Send the byte out.
249  */
250
251         if (values->mux == -1)
252                 retval = i8042_command(&c, I8042_CMD_AUX_SEND);
253         else
254                 retval = i8042_command(&c, I8042_CMD_MUX_SEND + values->mux);
255
256 /*
257  * Make sure the interrupt happens and the character is received even
258  * in the case the IRQ isn't wired, so that we can receive further
259  * characters later.
260  */
261
262         i8042_interrupt(0, NULL, NULL);
263         return retval;
264 }
265
266 /*
267  * i8042_activate_port() enables port on a chip.
268  */
269
270 static int i8042_activate_port(struct serio *port)
271 {
272         struct i8042_values *values = port->port_data;
273
274         i8042_flush();
275
276         /*
277          * Enable port again here because it is disabled if we are
278          * resuming (normally it is enabled already).
279          */
280         i8042_ctr &= ~values->disable;
281
282         i8042_ctr |= values->irqen;
283
284         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
285                 i8042_ctr &= ~values->irqen;
286                 return -1;
287         }
288
289         return 0;
290 }
291
292
293 /*
294  * i8042_open() is called when a port is open by the higher layer.
295  * It allocates the interrupt and calls i8042_enable_port.
296  */
297
298 static int i8042_open(struct serio *port)
299 {
300         struct i8042_values *values = port->port_data;
301
302         if (values->mux != -1)
303                 if (i8042_mux_open++)
304                         return 0;
305
306         if (request_irq(values->irq, i8042_interrupt,
307                         SA_SHIRQ, "i8042", i8042_request_irq_cookie)) {
308                 printk(KERN_ERR "i8042.c: Can't get irq %d for %s, unregistering the port.\n", values->irq, values->name);
309                 goto irq_fail;
310         }
311
312         if (i8042_activate_port(port)) {
313                 printk(KERN_ERR "i8042.c: Can't activate %s, unregistering the port\n", values->name);
314                 goto activate_fail;
315         }
316
317         i8042_interrupt(0, NULL, NULL);
318
319         return 0;
320
321 activate_fail:
322         free_irq(values->irq, i8042_request_irq_cookie);
323
324 irq_fail:
325         values->exists = 0;
326         serio_unregister_port_delayed(port);
327
328         return -1;
329 }
330
331 /*
332  * i8042_close() frees the interrupt, so that it can possibly be used
333  * by another driver. We never know - if the user doesn't have a mouse,
334  * the BIOS could have used the AUX interrupt for PCI.
335  */
336
337 static void i8042_close(struct serio *port)
338 {
339         struct i8042_values *values = port->port_data;
340
341         if (values->mux != -1)
342                 if (--i8042_mux_open)
343                         return;
344
345         i8042_ctr &= ~values->irqen;
346
347         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
348                 printk(KERN_ERR "i8042.c: Can't write CTR while closing %s.\n", values->name);
349                 return;
350         }
351
352         free_irq(values->irq, i8042_request_irq_cookie);
353
354         i8042_flush();
355 }
356
357 /*
358  * i8042_interrupt() is the most important function in this driver -
359  * it handles the interrupts from the i8042, and sends incoming bytes
360  * to the upper layers.
361  */
362
363 static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs)
364 {
365         unsigned long flags;
366         unsigned char str, data = 0;
367         unsigned int dfl;
368         unsigned int aux_idx;
369         int ret;
370
371         mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
372
373         spin_lock_irqsave(&i8042_lock, flags);
374         str = i8042_read_status();
375         if (str & I8042_STR_OBF)
376                 data = i8042_read_data();
377         spin_unlock_irqrestore(&i8042_lock, flags);
378
379         if (~str & I8042_STR_OBF) {
380                 if (irq) dbg("Interrupt %d, without any data", irq);
381                 ret = 0;
382                 goto out;
383         }
384
385         if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
386                 static unsigned long last_transmit;
387                 static unsigned char last_str;
388
389                 dfl = 0;
390                 if (str & I8042_STR_MUXERR) {
391                         dbg("MUX error, status is %02x, data is %02x", str, data);
392                         switch (data) {
393                                 default:
394 /*
395  * When MUXERR condition is signalled the data register can only contain
396  * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
397  * it is not always the case. Some KBC just get confused which port the
398  * data came from and signal error leaving the data intact. They _do not_
399  * revert to legacy mode (actually I've never seen KBC reverting to legacy
400  * mode yet, when we see one we'll add proper handling).
401  * Anyway, we will assume that the data came from the same serio last byte
402  * was transmitted (if transmission happened not too long ago).
403  */
404                                         if (time_before(jiffies, last_transmit + HZ/10)) {
405                                                 str = last_str;
406                                                 break;
407                                         }
408                                         /* fall through - report timeout */
409                                 case 0xfd:
410                                 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
411                                 case 0xff: dfl = SERIO_PARITY;  data = 0xfe; break;
412                         }
413                 }
414
415                 aux_idx = (str >> 6) & 3;
416
417                 dbg("%02x <- i8042 (interrupt, aux%d, %d%s%s)",
418                         data, aux_idx, irq,
419                         dfl & SERIO_PARITY ? ", bad parity" : "",
420                         dfl & SERIO_TIMEOUT ? ", timeout" : "");
421
422                 if (likely(i8042_mux_values[aux_idx].exists))
423                         serio_interrupt(i8042_mux_port[aux_idx], data, dfl, regs);
424
425                 last_str = str;
426                 last_transmit = jiffies;
427                 goto irq_ret;
428         }
429
430         dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
431               ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
432
433         dbg("%02x <- i8042 (interrupt, %s, %d%s%s)",
434                 data, (str & I8042_STR_AUXDATA) ? "aux" : "kbd", irq,
435                 dfl & SERIO_PARITY ? ", bad parity" : "",
436                 dfl & SERIO_TIMEOUT ? ", timeout" : "");
437
438
439         if (str & I8042_STR_AUXDATA) {
440                 if (likely(i8042_aux_values.exists))
441                         serio_interrupt(i8042_aux_port, data, dfl, regs);
442         } else {
443                 if (likely(i8042_kbd_values.exists))
444                         serio_interrupt(i8042_kbd_port, data, dfl, regs);
445         }
446
447 irq_ret:
448         ret = 1;
449 out:
450         return IRQ_RETVAL(ret);
451 }
452
453 /*
454  * i8042_enable_mux_mode checks whether the controller has an active
455  * multiplexor and puts the chip into Multiplexed (as opposed to
456  * Legacy) mode.
457  */
458
459 static int i8042_enable_mux_mode(struct i8042_values *values, unsigned char *mux_version)
460 {
461
462         unsigned char param;
463 /*
464  * Get rid of bytes in the queue.
465  */
466
467         i8042_flush();
468
469 /*
470  * Internal loopback test - send three bytes, they should come back from the
471  * mouse interface, the last should be version. Note that we negate mouseport
472  * command responses for the i8042_check_aux() routine.
473  */
474
475         param = 0xf0;
476         if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0x0f)
477                 return -1;
478         param = 0x56;
479         if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xa9)
480                 return -1;
481         param = 0xa4;
482         if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == 0x5b)
483                 return -1;
484
485         if (mux_version)
486                 *mux_version = ~param;
487
488         return 0;
489 }
490
491
492 /*
493  * i8042_enable_mux_ports enables 4 individual AUX ports after
494  * the controller has been switched into Multiplexed mode
495  */
496
497 static int i8042_enable_mux_ports(struct i8042_values *values)
498 {
499         unsigned char param;
500         int i;
501 /*
502  * Disable all muxed ports by disabling AUX.
503  */
504
505         i8042_ctr |= I8042_CTR_AUXDIS;
506         i8042_ctr &= ~I8042_CTR_AUXINT;
507
508         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
509                 printk(KERN_ERR "i8042.c: Failed to disable AUX port, can't use MUX.\n");
510                 return -1;
511         }
512
513 /*
514  * Enable all muxed ports.
515  */
516
517         for (i = 0; i < 4; i++) {
518                 i8042_command(&param, I8042_CMD_MUX_PFX + i);
519                 i8042_command(&param, I8042_CMD_AUX_ENABLE);
520         }
521
522         return 0;
523 }
524
525
526 /*
527  * i8042_check_mux() checks whether the controller supports the PS/2 Active
528  * Multiplexing specification by Synaptics, Phoenix, Insyde and
529  * LCS/Telegraphics.
530  */
531
532 static int __init i8042_check_mux(struct i8042_values *values)
533 {
534         unsigned char mux_version;
535
536         if (i8042_enable_mux_mode(values, &mux_version))
537                 return -1;
538
539         /* Workaround for broken chips which seem to support MUX, but in reality don't. */
540         /* They all report version 10.12 */
541         if (mux_version == 0xAC)
542                 return -1;
543
544         printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
545                 (mux_version >> 4) & 0xf, mux_version & 0xf);
546
547         if (i8042_enable_mux_ports(values))
548                 return -1;
549
550         i8042_mux_present = 1;
551         return 0;
552 }
553
554
555 /*
556  * i8042_check_aux() applies as much paranoia as it can at detecting
557  * the presence of an AUX interface.
558  */
559
560 static int __init i8042_check_aux(struct i8042_values *values)
561 {
562         unsigned char param;
563         static int i8042_check_aux_cookie;
564
565 /*
566  * Check if AUX irq is available. If it isn't, then there is no point
567  * in trying to detect AUX presence.
568  */
569
570         if (request_irq(values->irq, i8042_interrupt, SA_SHIRQ,
571                                 "i8042", &i8042_check_aux_cookie))
572                 return -1;
573         free_irq(values->irq, &i8042_check_aux_cookie);
574
575 /*
576  * Get rid of bytes in the queue.
577  */
578
579         i8042_flush();
580
581 /*
582  * Internal loopback test - filters out AT-type i8042's. Unfortunately
583  * SiS screwed up and their 5597 doesn't support the LOOP command even
584  * though it has an AUX port.
585  */
586
587         param = 0x5a;
588         if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xa5) {
589
590 /*
591  * External connection test - filters out AT-soldered PS/2 i8042's
592  * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
593  * 0xfa - no error on some notebooks which ignore the spec
594  * Because it's common for chipsets to return error on perfectly functioning
595  * AUX ports, we test for this only when the LOOP command failed.
596  */
597
598                 if (i8042_command(&param, I8042_CMD_AUX_TEST)
599                         || (param && param != 0xfa && param != 0xff))
600                                 return -1;
601         }
602
603 /*
604  * Bit assignment test - filters out PS/2 i8042's in AT mode
605  */
606
607         if (i8042_command(&param, I8042_CMD_AUX_DISABLE))
608                 return -1;
609         if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (~param & I8042_CTR_AUXDIS)) {
610                 printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
611                 printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
612         }
613
614         if (i8042_command(&param, I8042_CMD_AUX_ENABLE))
615                 return -1;
616         if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (param & I8042_CTR_AUXDIS))
617                 return -1;
618
619 /*
620  * Disable the interface.
621  */
622
623         i8042_ctr |= I8042_CTR_AUXDIS;
624         i8042_ctr &= ~I8042_CTR_AUXINT;
625
626         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
627                 return -1;
628
629         return 0;
630 }
631
632
633 /*
634  * i8042_port_register() marks the device as existing,
635  * registers it, and reports to the user.
636  */
637
638 static int __init i8042_port_register(struct serio *port)
639 {
640         struct i8042_values *values = port->port_data;
641
642         values->exists = 1;
643
644         i8042_ctr &= ~values->disable;
645
646         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
647                 printk(KERN_WARNING "i8042.c: Can't write CTR while registering.\n");
648                 values->exists = 0;
649                 return -1;
650         }
651
652         printk(KERN_INFO "serio: i8042 %s port at %#lx,%#lx irq %d\n",
653                values->name,
654                (unsigned long) I8042_DATA_REG,
655                (unsigned long) I8042_COMMAND_REG,
656                values->irq);
657
658         serio_register_port(port);
659
660         return 0;
661 }
662
663
664 static void i8042_timer_func(unsigned long data)
665 {
666         i8042_interrupt(0, NULL, NULL);
667 }
668
669
670 static int i8042_spank_usb(void)
671 {
672         struct pci_dev *usb = NULL;
673         int found = 0;
674         u16 word;
675         unsigned long addr;
676         unsigned long len;
677         int i;
678         
679         while((usb = pci_find_class((PCI_CLASS_SERIAL_USB << 8), usb)) != NULL)
680         {
681                 /* UHCI controller not in legacy ? */
682                 
683                 pci_read_config_word(usb, 0xC0, &word);
684                 if(word & 0x2000)
685                         continue;
686                         
687                 /* Check it is enabled. If the port is active in legacy mode
688                    then this will be mapped already */
689                    
690                 for(i = 0; i < PCI_ROM_RESOURCE; i++)
691                 {
692                         if (!(pci_resource_flags (usb, i) & IORESOURCE_IO))
693                                 continue;
694                 }
695                 if(i == PCI_ROM_RESOURCE)
696                         continue;
697                 
698                 /*
699                  *      Retrieve the bits
700                  */
701                     
702                 addr = pci_resource_start(usb, i);
703                 len = pci_resource_len (usb, i);
704                 
705                 /*
706                  *      Check its configured and not in use
707                  */
708                 if(addr == 0)
709                         continue;
710                 if (request_region(addr, len, "usb whackamole"))
711                         continue;
712                                 
713                 /*
714                  *      Kick the problem controller out of legacy mode
715                  *      so things like the E750x don't break
716                  */
717                 
718                 outw(0, addr + 4);              /* IRQ Mask */
719                 outw(4, addr);                  /* Reset */
720                 msleep(20);
721                 outw(0, addr);
722                 
723                 msleep(20);
724                 /* Now take if off the BIOS */
725                 pci_write_config_word(usb, 0xC0, 0x2000);
726                 release_region(addr, len);
727
728                 found = 1;
729         }
730         return found;
731 }
732
733 /*
734  * i8042_controller init initializes the i8042 controller, and,
735  * most importantly, sets it into non-xlated mode if that's
736  * desired.
737  */
738
739 static int i8042_controller_init(void)
740 {
741         int tries = 0;
742         unsigned long flags;
743
744 /*
745  * Test the i8042. We need to know if it thinks it's working correctly
746  * before doing anything else.
747  */
748
749         i8042_flush();
750
751         if (i8042_reset) {
752
753                 unsigned char param;
754
755                 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
756                         printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
757                         return -1;
758                 }
759
760                 if (param != I8042_RET_CTL_TEST) {
761                         printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
762                                  param, I8042_RET_CTL_TEST);
763                         return -1;
764                 }
765         }
766
767 /*
768  * Save the CTR for restoral on unload / reboot.
769  */
770
771         while(i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
772                 if(tries > 3 || !i8042_spank_usb())
773                 {
774                         printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
775                         return -1;
776                 }
777                 printk(KERN_WARNING "i8042.c: Can't read CTR, disabling USB legacy and retrying.\n");
778                 i8042_flush();
779                 tries++;
780         }
781
782         i8042_initial_ctr = i8042_ctr;
783
784 /*
785  * Disable the keyboard interface and interrupt.
786  */
787
788         i8042_ctr |= I8042_CTR_KBDDIS;
789         i8042_ctr &= ~I8042_CTR_KBDINT;
790
791 /*
792  * Handle keylock.
793  */
794
795         spin_lock_irqsave(&i8042_lock, flags);
796         if (~i8042_read_status() & I8042_STR_KEYLOCK) {
797                 if (i8042_unlock)
798                         i8042_ctr |= I8042_CTR_IGNKEYLOCK;
799                  else
800                         printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
801         }
802         spin_unlock_irqrestore(&i8042_lock, flags);
803
804 /*
805  * If the chip is configured into nontranslated mode by the BIOS, don't
806  * bother enabling translating and be happy.
807  */
808
809         if (~i8042_ctr & I8042_CTR_XLATE)
810                 i8042_direct = 1;
811
812 /*
813  * Set nontranslated mode for the kbd interface if requested by an option.
814  * After this the kbd interface becomes a simple serial in/out, like the aux
815  * interface is. We don't do this by default, since it can confuse notebook
816  * BIOSes.
817  */
818
819         if (i8042_direct)
820                 i8042_ctr &= ~I8042_CTR_XLATE;
821
822 /*
823  * Write CTR back.
824  */
825
826         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
827                 printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
828                 return -1;
829         }
830
831         return 0;
832 }
833
834
835 /*
836  * Reset the controller.
837  */
838 void i8042_controller_reset(void)
839 {
840         if (i8042_reset) {
841                 unsigned char param;
842
843                 if (i8042_command(&param, I8042_CMD_CTL_TEST))
844                         printk(KERN_ERR "i8042.c: i8042 controller reset timeout.\n");
845         }
846
847 /*
848  * Restore the original control register setting.
849  */
850
851         i8042_ctr = i8042_initial_ctr;
852
853         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
854                 printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
855 }
856
857
858 /*
859  * Here we try to reset everything back to a state in which the BIOS will be
860  * able to talk to the hardware when rebooting.
861  */
862
863 void i8042_controller_cleanup(void)
864 {
865         int i;
866
867         i8042_flush();
868
869 /*
870  * Reset anything that is connected to the ports.
871  */
872
873         if (i8042_kbd_values.exists)
874                 serio_cleanup(i8042_kbd_port);
875
876         if (i8042_aux_values.exists)
877                 serio_cleanup(i8042_aux_port);
878
879         for (i = 0; i < I8042_NUM_MUX_PORTS; i++)
880                 if (i8042_mux_values[i].exists)
881                         serio_cleanup(i8042_mux_port[i]);
882
883         i8042_controller_reset();
884 }
885
886
887 /*
888  * Here we try to restore the original BIOS settings
889  */
890
891 static int i8042_controller_suspend(void)
892 {
893         del_timer_sync(&i8042_timer);
894         i8042_controller_reset();
895
896         return 0;
897 }
898
899
900 /*
901  * Here we try to reset everything back to a state in which suspended
902  */
903
904 static int i8042_controller_resume(void)
905 {
906         int i;
907
908         if (i8042_controller_init()) {
909                 printk(KERN_ERR "i8042: resume failed\n");
910                 return -1;
911         }
912
913         if (i8042_mux_present)
914                 if (i8042_enable_mux_mode(&i8042_aux_values, NULL) ||
915                     i8042_enable_mux_ports(&i8042_aux_values)) {
916                         printk(KERN_WARNING "i8042: failed to resume active multiplexor, mouse won't work.\n");
917                 }
918
919 /*
920  * Reconnect anything that was connected to the ports.
921  */
922
923         if (i8042_kbd_values.exists && i8042_activate_port(i8042_kbd_port) == 0)
924                 serio_reconnect(i8042_kbd_port);
925
926         if (i8042_aux_values.exists && i8042_activate_port(i8042_aux_port) == 0)
927                 serio_reconnect(i8042_aux_port);
928
929         for (i = 0; i < I8042_NUM_MUX_PORTS; i++)
930                 if (i8042_mux_values[i].exists && i8042_activate_port(i8042_mux_port[i]) == 0)
931                         serio_reconnect(i8042_mux_port[i]);
932 /*
933  * Restart timer (for polling "stuck" data)
934  */
935         mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
936
937         return 0;
938 }
939
940
941 /*
942  * We need to reset the 8042 back to original mode on system shutdown,
943  * because otherwise BIOSes will be confused.
944  */
945
946 static int i8042_notify_sys(struct notifier_block *this, unsigned long code,
947                             void *unused)
948 {
949         if (code == SYS_DOWN || code == SYS_HALT)
950                 i8042_controller_cleanup();
951         return NOTIFY_DONE;
952 }
953
954 static struct notifier_block i8042_notifier =
955 {
956         i8042_notify_sys,
957         NULL,
958         0
959 };
960
961 static int blink_frequency = 500;
962 module_param_named(panicblink, blink_frequency, int, 0600);
963
964 #define DELAY mdelay(1), delay++
965
966 /* Tell the user who may be running in X and not see the console that we have 
967    panic'ed. This is to distingush panics from "real" lockups.  */
968 static long i8042_panic_blink(long count)
969
970         long delay = 0;
971         static long last_blink;
972         static char led;
973         /* Roughly 1/2s frequency. KDB uses about 1s. Make sure it is 
974            different. */
975         if (!blink_frequency) 
976                 return 0;
977         if (count - last_blink < blink_frequency)
978                 return 0;
979         led ^= 0x01 | 0x04;
980         while (i8042_read_status() & I8042_STR_IBF)
981                 DELAY;
982         i8042_write_data(0xed); /* set leds */
983         DELAY;
984         while (i8042_read_status() & I8042_STR_IBF)
985                 DELAY;
986         DELAY;
987         i8042_write_data(led);
988         DELAY;
989         last_blink = count;
990         return delay;
991 }  
992
993 #undef DELAY
994
995 /*
996  * Suspend/resume handlers for the new PM scheme (driver model)
997  */
998 static int i8042_suspend(struct device *dev, u32 state, u32 level)
999 {
1000         return level == SUSPEND_DISABLE ? i8042_controller_suspend() : 0;
1001 }
1002
1003 static int i8042_resume(struct device *dev, u32 level)
1004 {
1005         return level == RESUME_ENABLE ? i8042_controller_resume() : 0;
1006 }
1007
1008 static void i8042_shutdown(struct device *dev)
1009 {
1010         i8042_controller_cleanup();
1011 }
1012
1013 static struct device_driver i8042_driver = {
1014         .name           = "i8042",
1015         .bus            = &platform_bus_type,
1016         .suspend        = i8042_suspend,
1017         .resume         = i8042_resume,
1018         .shutdown       = i8042_shutdown,
1019 };
1020
1021 /*
1022  * Suspend/resume handler for the old PM scheme (APM)
1023  */
1024 static int i8042_pm_callback(struct pm_dev *dev, pm_request_t request, void *dummy)
1025 {
1026         switch (request) {
1027                 case PM_SUSPEND:
1028                         return i8042_controller_suspend();
1029
1030                 case PM_RESUME:
1031                         return i8042_controller_resume();
1032         }
1033
1034         return 0;
1035 }
1036
1037 static struct serio * __init i8042_allocate_kbd_port(void)
1038 {
1039         struct serio *serio;
1040
1041         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1042         if (serio) {
1043                 memset(serio, 0, sizeof(struct serio));
1044                 serio->type             = i8042_direct ? SERIO_8042 : SERIO_8042_XL,
1045                 serio->write            = i8042_dumbkbd ? NULL : i8042_kbd_write,
1046                 serio->open             = i8042_open,
1047                 serio->close            = i8042_close,
1048                 serio->port_data        = &i8042_kbd_values,
1049                 serio->dev.parent       = &i8042_platform_device->dev;
1050                 strlcpy(serio->name, "i8042 Kbd Port", sizeof(serio->name));
1051                 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1052         }
1053
1054         return serio;
1055 }
1056
1057 static struct serio * __init i8042_allocate_aux_port(void)
1058 {
1059         struct serio *serio;
1060
1061         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1062         if (serio) {
1063                 memset(serio, 0, sizeof(struct serio));
1064                 serio->type             = SERIO_8042;
1065                 serio->write            = i8042_aux_write;
1066                 serio->open             = i8042_open;
1067                 serio->close            = i8042_close;
1068                 serio->port_data        = &i8042_aux_values,
1069                 serio->dev.parent       = &i8042_platform_device->dev;
1070                 strlcpy(serio->name, "i8042 Aux Port", sizeof(serio->name));
1071                 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1072         }
1073
1074         return serio;
1075 }
1076
1077 static struct serio * __init i8042_allocate_mux_port(int index)
1078 {
1079         struct serio *serio;
1080         struct i8042_values *values = &i8042_mux_values[index];
1081
1082         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1083         if (serio) {
1084                 *values = i8042_aux_values;
1085                 snprintf(values->name, sizeof(values->name), "AUX%d", index);
1086                 values->mux = index;
1087
1088                 memset(serio, 0, sizeof(struct serio));
1089                 serio->type             = SERIO_8042;
1090                 serio->write            = i8042_aux_write;
1091                 serio->open             = i8042_open;
1092                 serio->close            = i8042_close;
1093                 serio->port_data        = values;
1094                 serio->dev.parent       = &i8042_platform_device->dev;
1095                 snprintf(serio->name, sizeof(serio->name), "i8042 Aux-%d Port", index);
1096                 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, index + 1);
1097         }
1098
1099         return serio;
1100 }
1101
1102 int __init i8042_init(void)
1103 {
1104         int i;
1105         int err;
1106
1107         dbg_init();
1108
1109         init_timer(&i8042_timer);
1110         i8042_timer.function = i8042_timer_func;
1111
1112         if (i8042_platform_init())
1113                 return -EBUSY;
1114
1115         i8042_aux_values.irq = I8042_AUX_IRQ;
1116         i8042_kbd_values.irq = I8042_KBD_IRQ;
1117
1118         if (i8042_controller_init())
1119                 return -ENODEV;
1120
1121         err = driver_register(&i8042_driver);
1122         if (err)
1123                 return err;
1124
1125         i8042_platform_device = platform_device_register_simple("i8042", -1, NULL, 0);
1126         if (IS_ERR(i8042_platform_device)) {
1127                 driver_unregister(&i8042_driver);
1128                 return PTR_ERR(i8042_platform_device);
1129         }
1130
1131         if (!i8042_noaux && !i8042_check_aux(&i8042_aux_values)) {
1132                 if (!i8042_nomux && !i8042_check_mux(&i8042_aux_values))
1133                         for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1134                                 i8042_mux_port[i] = i8042_allocate_mux_port(i);
1135                                 if (i8042_mux_port[i])
1136                                         i8042_port_register(i8042_mux_port[i]);
1137                         }
1138                 else {
1139                         i8042_aux_port = i8042_allocate_aux_port();
1140                         if (i8042_aux_port)
1141                                 i8042_port_register(i8042_aux_port);
1142                 }
1143         }
1144
1145         i8042_kbd_port = i8042_allocate_kbd_port();
1146         if (i8042_kbd_port)
1147                 i8042_port_register(i8042_kbd_port);
1148
1149         mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
1150
1151         i8042_pm_dev = pm_register(PM_SYS_DEV, PM_SYS_UNKNOWN, i8042_pm_callback);
1152
1153         register_reboot_notifier(&i8042_notifier);
1154
1155         panic_blink = i8042_panic_blink;
1156
1157         return 0;
1158 }
1159
1160 void __exit i8042_exit(void)
1161 {
1162         int i;
1163
1164         unregister_reboot_notifier(&i8042_notifier);
1165
1166         if (i8042_pm_dev)
1167                 pm_unregister(i8042_pm_dev);
1168
1169         i8042_controller_cleanup();
1170
1171         if (i8042_kbd_values.exists)
1172                 serio_unregister_port(i8042_kbd_port);
1173
1174         if (i8042_aux_values.exists)
1175                 serio_unregister_port(i8042_aux_port);
1176
1177         for (i = 0; i < I8042_NUM_MUX_PORTS; i++)
1178                 if (i8042_mux_values[i].exists)
1179                         serio_unregister_port(i8042_mux_port[i]);
1180
1181         del_timer_sync(&i8042_timer);
1182
1183         platform_device_unregister(i8042_platform_device);
1184         driver_unregister(&i8042_driver);
1185
1186         i8042_platform_exit();
1187
1188         panic_blink = NULL;
1189 }
1190
1191 module_init(i8042_init);
1192 module_exit(i8042_exit);