upgrade to linux 2.6.10-1.12_FC2
[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_get_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                 pci_dev_put(usb);
729
730                 found = 1;
731         }
732         return found;
733 }
734
735 /*
736  * i8042_controller init initializes the i8042 controller, and,
737  * most importantly, sets it into non-xlated mode if that's
738  * desired.
739  */
740
741 static int i8042_controller_init(void)
742 {
743         int tries = 0;
744         unsigned long flags;
745
746 /*
747  * Test the i8042. We need to know if it thinks it's working correctly
748  * before doing anything else.
749  */
750
751         i8042_flush();
752
753         if (i8042_reset) {
754
755                 unsigned char param;
756
757                 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
758                         printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
759                         return -1;
760                 }
761
762                 if (param != I8042_RET_CTL_TEST) {
763                         printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
764                                  param, I8042_RET_CTL_TEST);
765                         return -1;
766                 }
767         }
768
769 /*
770  * Save the CTR for restoral on unload / reboot.
771  */
772
773         while(i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
774                 if(tries > 3 || !i8042_spank_usb())
775                 {
776                         printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
777                         return -1;
778                 }
779                 printk(KERN_WARNING "i8042.c: Can't read CTR, disabling USB legacy and retrying.\n");
780                 i8042_flush();
781                 tries++;
782         }
783
784         i8042_initial_ctr = i8042_ctr;
785
786 /*
787  * Disable the keyboard interface and interrupt.
788  */
789
790         i8042_ctr |= I8042_CTR_KBDDIS;
791         i8042_ctr &= ~I8042_CTR_KBDINT;
792
793 /*
794  * Handle keylock.
795  */
796
797         spin_lock_irqsave(&i8042_lock, flags);
798         if (~i8042_read_status() & I8042_STR_KEYLOCK) {
799                 if (i8042_unlock)
800                         i8042_ctr |= I8042_CTR_IGNKEYLOCK;
801                  else
802                         printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
803         }
804         spin_unlock_irqrestore(&i8042_lock, flags);
805
806 /*
807  * If the chip is configured into nontranslated mode by the BIOS, don't
808  * bother enabling translating and be happy.
809  */
810
811         if (~i8042_ctr & I8042_CTR_XLATE)
812                 i8042_direct = 1;
813
814 /*
815  * Set nontranslated mode for the kbd interface if requested by an option.
816  * After this the kbd interface becomes a simple serial in/out, like the aux
817  * interface is. We don't do this by default, since it can confuse notebook
818  * BIOSes.
819  */
820
821         if (i8042_direct)
822                 i8042_ctr &= ~I8042_CTR_XLATE;
823
824 /*
825  * Write CTR back.
826  */
827
828         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
829                 printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
830                 return -1;
831         }
832
833         return 0;
834 }
835
836
837 /*
838  * Reset the controller.
839  */
840 void i8042_controller_reset(void)
841 {
842         if (i8042_reset) {
843                 unsigned char param;
844
845                 if (i8042_command(&param, I8042_CMD_CTL_TEST))
846                         printk(KERN_ERR "i8042.c: i8042 controller reset timeout.\n");
847         }
848
849 /*
850  * Restore the original control register setting.
851  */
852
853         i8042_ctr = i8042_initial_ctr;
854
855         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
856                 printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
857 }
858
859
860 /*
861  * Here we try to reset everything back to a state in which the BIOS will be
862  * able to talk to the hardware when rebooting.
863  */
864
865 void i8042_controller_cleanup(void)
866 {
867         int i;
868
869         i8042_flush();
870
871 /*
872  * Reset anything that is connected to the ports.
873  */
874
875         if (i8042_kbd_values.exists)
876                 serio_cleanup(i8042_kbd_port);
877
878         if (i8042_aux_values.exists)
879                 serio_cleanup(i8042_aux_port);
880
881         for (i = 0; i < I8042_NUM_MUX_PORTS; i++)
882                 if (i8042_mux_values[i].exists)
883                         serio_cleanup(i8042_mux_port[i]);
884
885         i8042_controller_reset();
886 }
887
888
889 static int blink_frequency = 500;
890 module_param_named(panicblink, blink_frequency, int, 0600);
891
892 /* Catch the case when the kbd interrupt is off */
893 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
894
895 /* Tell the user who may be running in X and not see the console that we have
896    panic'ed. This is to distingush panics from "real" lockups.  */
897 static long i8042_panic_blink(long count)
898 {
899         long delay = 0;
900         static long last_blink;
901         static char led;
902         /* Roughly 1/2s frequency. KDB uses about 1s. Make sure it is
903            different. */
904         if (!blink_frequency)
905                 return 0;
906         if (count - last_blink < blink_frequency)
907                 return 0;
908         led ^= 0x01 | 0x04;
909         while (i8042_read_status() & I8042_STR_IBF)
910                 DELAY;
911         i8042_write_data(0xed); /* set leds */
912         DELAY;
913         while (i8042_read_status() & I8042_STR_IBF)
914                 DELAY;
915         DELAY;
916         i8042_write_data(led);
917         DELAY;
918         last_blink = count;
919         return delay;
920 }
921
922 #undef DELAY
923
924 /*
925  * Here we try to restore the original BIOS settings
926  */
927
928 static int i8042_controller_suspend(void)
929 {
930         del_timer_sync(&i8042_timer);
931         i8042_controller_reset();
932
933         return 0;
934 }
935
936
937 /*
938  * Here we try to reset everything back to a state in which suspended
939  */
940
941 static int i8042_controller_resume(void)
942 {
943         int i;
944
945         if (i8042_controller_init()) {
946                 printk(KERN_ERR "i8042: resume failed\n");
947                 return -1;
948         }
949
950         if (i8042_mux_present)
951                 if (i8042_enable_mux_mode(&i8042_aux_values, NULL) ||
952                     i8042_enable_mux_ports(&i8042_aux_values)) {
953                         printk(KERN_WARNING "i8042: failed to resume active multiplexor, mouse won't work.\n");
954                 }
955
956 /*
957  * Reconnect anything that was connected to the ports.
958  */
959
960         if (i8042_kbd_values.exists && i8042_activate_port(i8042_kbd_port) == 0)
961                 serio_reconnect(i8042_kbd_port);
962
963         if (i8042_aux_values.exists && i8042_activate_port(i8042_aux_port) == 0)
964                 serio_reconnect(i8042_aux_port);
965
966         for (i = 0; i < I8042_NUM_MUX_PORTS; i++)
967                 if (i8042_mux_values[i].exists && i8042_activate_port(i8042_mux_port[i]) == 0)
968                         serio_reconnect(i8042_mux_port[i]);
969 /*
970  * Restart timer (for polling "stuck" data)
971  */
972         mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
973
974         panic_blink = i8042_panic_blink;
975
976         return 0;
977 }
978
979
980 /*
981  * We need to reset the 8042 back to original mode on system shutdown,
982  * because otherwise BIOSes will be confused.
983  */
984
985 static int i8042_notify_sys(struct notifier_block *this, unsigned long code,
986                             void *unused)
987 {
988         if (code == SYS_DOWN || code == SYS_HALT)
989                 i8042_controller_cleanup();
990         return NOTIFY_DONE;
991 }
992
993 static struct notifier_block i8042_notifier =
994 {
995         i8042_notify_sys,
996         NULL,
997         0
998 };
999
1000 /*
1001  * Suspend/resume handlers for the new PM scheme (driver model)
1002  */
1003 static int i8042_suspend(struct device *dev, u32 state, u32 level)
1004 {
1005         return level == SUSPEND_DISABLE ? i8042_controller_suspend() : 0;
1006 }
1007
1008 static int i8042_resume(struct device *dev, u32 level)
1009 {
1010         return level == RESUME_ENABLE ? i8042_controller_resume() : 0;
1011 }
1012
1013 static void i8042_shutdown(struct device *dev)
1014 {
1015         i8042_controller_cleanup();
1016 }
1017
1018 static struct device_driver i8042_driver = {
1019         .name           = "i8042",
1020         .bus            = &platform_bus_type,
1021         .suspend        = i8042_suspend,
1022         .resume         = i8042_resume,
1023         .shutdown       = i8042_shutdown,
1024 };
1025
1026 /*
1027  * Suspend/resume handler for the old PM scheme (APM)
1028  */
1029 static int i8042_pm_callback(struct pm_dev *dev, pm_request_t request, void *dummy)
1030 {
1031         switch (request) {
1032                 case PM_SUSPEND:
1033                         return i8042_controller_suspend();
1034
1035                 case PM_RESUME:
1036                         return i8042_controller_resume();
1037         }
1038
1039         return 0;
1040 }
1041
1042 static struct serio * __init i8042_allocate_kbd_port(void)
1043 {
1044         struct serio *serio;
1045
1046         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1047         if (serio) {
1048                 memset(serio, 0, sizeof(struct serio));
1049                 serio->type             = i8042_direct ? SERIO_8042 : SERIO_8042_XL,
1050                 serio->write            = i8042_dumbkbd ? NULL : i8042_kbd_write,
1051                 serio->open             = i8042_open,
1052                 serio->close            = i8042_close,
1053                 serio->port_data        = &i8042_kbd_values,
1054                 serio->dev.parent       = &i8042_platform_device->dev;
1055                 strlcpy(serio->name, "i8042 Kbd Port", sizeof(serio->name));
1056                 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1057         }
1058
1059         return serio;
1060 }
1061
1062 static struct serio * __init i8042_allocate_aux_port(void)
1063 {
1064         struct serio *serio;
1065
1066         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1067         if (serio) {
1068                 memset(serio, 0, sizeof(struct serio));
1069                 serio->type             = SERIO_8042;
1070                 serio->write            = i8042_aux_write;
1071                 serio->open             = i8042_open;
1072                 serio->close            = i8042_close;
1073                 serio->port_data        = &i8042_aux_values,
1074                 serio->dev.parent       = &i8042_platform_device->dev;
1075                 strlcpy(serio->name, "i8042 Aux Port", sizeof(serio->name));
1076                 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1077         }
1078
1079         return serio;
1080 }
1081
1082 static struct serio * __init i8042_allocate_mux_port(int index)
1083 {
1084         struct serio *serio;
1085         struct i8042_values *values = &i8042_mux_values[index];
1086
1087         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1088         if (serio) {
1089                 *values = i8042_aux_values;
1090                 snprintf(values->name, sizeof(values->name), "AUX%d", index);
1091                 values->mux = index;
1092
1093                 memset(serio, 0, sizeof(struct serio));
1094                 serio->type             = SERIO_8042;
1095                 serio->write            = i8042_aux_write;
1096                 serio->open             = i8042_open;
1097                 serio->close            = i8042_close;
1098                 serio->port_data        = values;
1099                 serio->dev.parent       = &i8042_platform_device->dev;
1100                 snprintf(serio->name, sizeof(serio->name), "i8042 Aux-%d Port", index);
1101                 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, index + 1);
1102         }
1103
1104         return serio;
1105 }
1106
1107 int __init i8042_init(void)
1108 {
1109         int i;
1110         int err;
1111
1112         dbg_init();
1113
1114         init_timer(&i8042_timer);
1115         i8042_timer.function = i8042_timer_func;
1116
1117         if (i8042_platform_init())
1118                 return -EBUSY;
1119
1120         i8042_aux_values.irq = I8042_AUX_IRQ;
1121         i8042_kbd_values.irq = I8042_KBD_IRQ;
1122
1123         if (i8042_controller_init())
1124                 return -ENODEV;
1125
1126         err = driver_register(&i8042_driver);
1127         if (err)
1128                 return err;
1129
1130         i8042_platform_device = platform_device_register_simple("i8042", -1, NULL, 0);
1131         if (IS_ERR(i8042_platform_device)) {
1132                 driver_unregister(&i8042_driver);
1133                 return PTR_ERR(i8042_platform_device);
1134         }
1135
1136         if (!i8042_noaux && !i8042_check_aux(&i8042_aux_values)) {
1137                 if (!i8042_nomux && !i8042_check_mux(&i8042_aux_values))
1138                         for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1139                                 i8042_mux_port[i] = i8042_allocate_mux_port(i);
1140                                 if (i8042_mux_port[i])
1141                                         i8042_port_register(i8042_mux_port[i]);
1142                         }
1143                 else {
1144                         i8042_aux_port = i8042_allocate_aux_port();
1145                         if (i8042_aux_port)
1146                                 i8042_port_register(i8042_aux_port);
1147                 }
1148         }
1149
1150         i8042_kbd_port = i8042_allocate_kbd_port();
1151         if (i8042_kbd_port)
1152                 i8042_port_register(i8042_kbd_port);
1153
1154         mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
1155
1156         i8042_pm_dev = pm_register(PM_SYS_DEV, PM_SYS_UNKNOWN, i8042_pm_callback);
1157
1158         register_reboot_notifier(&i8042_notifier);
1159
1160         return 0;
1161 }
1162
1163 void __exit i8042_exit(void)
1164 {
1165         int i;
1166
1167         unregister_reboot_notifier(&i8042_notifier);
1168
1169         if (i8042_pm_dev)
1170                 pm_unregister(i8042_pm_dev);
1171
1172         i8042_controller_cleanup();
1173
1174         if (i8042_kbd_values.exists)
1175                 serio_unregister_port(i8042_kbd_port);
1176
1177         if (i8042_aux_values.exists)
1178                 serio_unregister_port(i8042_aux_port);
1179
1180         for (i = 0; i < I8042_NUM_MUX_PORTS; i++)
1181                 if (i8042_mux_values[i].exists)
1182                         serio_unregister_port(i8042_mux_port[i]);
1183
1184         del_timer_sync(&i8042_timer);
1185
1186         platform_device_unregister(i8042_platform_device);
1187         driver_unregister(&i8042_driver);
1188
1189         i8042_platform_exit();
1190
1191         panic_blink = NULL;
1192 }
1193
1194 module_init(i8042_init);
1195 module_exit(i8042_exit);