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