MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / input / serio / i8042.c
blob151c29ddb635bb1cbcbf2767a20e786c14f5fab8
1 /*
2 * i8042 keyboard and mouse controller driver for Linux
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 */
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.
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>
26 #include <asm/io.h>
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
30 MODULE_LICENSE("GPL");
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.");
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.");
40 static unsigned int i8042_unlock;
41 module_param_named(unlock, i8042_unlock, bool, 0);
42 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
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.");
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.");
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");
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");
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");
67 #undef DEBUG
68 #include "i8042.h"
70 spinlock_t i8042_lock = SPIN_LOCK_UNLOCKED;
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];
81 static struct i8042_values i8042_kbd_values = {
82 .disable = I8042_CTR_KBDDIS,
83 .irqen = I8042_CTR_KBDINT,
84 .mux = -1,
85 .name = "KBD",
88 static struct i8042_values i8042_aux_values = {
89 .disable = I8042_CTR_AUXDIS,
90 .irqen = I8042_CTR_AUXINT,
91 .mux = -1,
92 .name = "AUX",
95 static struct i8042_values i8042_mux_values[I8042_NUM_MUX_PORTS];
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;
109 * Shared IRQ's require a device pointer, but this driver doesn't support
110 * multiple devices
112 #define i8042_request_irq_cookie (&i8042_timer)
114 static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs);
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.
122 static int i8042_wait_read(void)
124 int i = 0;
125 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
126 udelay(50);
127 i++;
129 return -(i == I8042_CTL_TIMEOUT);
132 static int i8042_wait_write(void)
134 int i = 0;
135 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
136 udelay(50);
137 i++;
139 return -(i == I8042_CTL_TIMEOUT);
143 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
144 * of the i8042 down the toilet.
147 static int i8042_flush(void)
149 unsigned long flags;
150 unsigned char data;
151 int i = 0;
153 spin_lock_irqsave(&i8042_lock, flags);
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");
162 spin_unlock_irqrestore(&i8042_lock, flags);
164 return i;
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.
175 static int i8042_command(unsigned char *param, int command)
177 unsigned long flags;
178 int retval = 0, i = 0;
180 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
181 return -1;
183 spin_lock_irqsave(&i8042_lock, flags);
185 retval = i8042_wait_write();
186 if (!retval) {
187 dbg("%02x -> i8042 (command)", command & 0xff);
188 i8042_write_command(command & 0xff);
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]);
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]);
208 spin_unlock_irqrestore(&i8042_lock, flags);
210 if (retval)
211 dbg(" -- i8042 (timeout)");
213 return retval;
217 * i8042_kbd_write() sends a byte out through the keyboard interface.
220 static int i8042_kbd_write(struct serio *port, unsigned char c)
222 unsigned long flags;
223 int retval = 0;
225 spin_lock_irqsave(&i8042_lock, flags);
227 if(!(retval = i8042_wait_write())) {
228 dbg("%02x -> i8042 (kbd-data)", c);
229 i8042_write_data(c);
232 spin_unlock_irqrestore(&i8042_lock, flags);
234 return retval;
238 * i8042_aux_write() sends a byte out through the aux interface.
241 static int i8042_aux_write(struct serio *port, unsigned char c)
243 struct i8042_values *values = port->port_data;
244 int retval;
247 * Send the byte out.
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);
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.
261 i8042_interrupt(0, NULL, NULL);
262 return retval;
266 * i8042_activate_port() enables port on a chip.
269 static int i8042_activate_port(struct serio *port)
271 struct i8042_values *values = port->port_data;
273 i8042_flush();
276 * Enable port again here because it is disabled if we are
277 * resuming (normally it is enabled already).
279 i8042_ctr &= ~values->disable;
281 i8042_ctr |= values->irqen;
283 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
284 i8042_ctr &= ~values->irqen;
285 return -1;
288 return 0;
293 * i8042_open() is called when a port is open by the higher layer.
294 * It allocates the interrupt and calls i8042_enable_port.
297 static int i8042_open(struct serio *port)
299 struct i8042_values *values = port->port_data;
301 if (values->mux != -1)
302 if (i8042_mux_open++)
303 return 0;
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;
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;
316 i8042_interrupt(0, NULL, NULL);
318 return 0;
320 activate_fail:
321 free_irq(values->irq, i8042_request_irq_cookie);
323 irq_fail:
324 values->exists = 0;
325 serio_unregister_port_delayed(port);
327 return -1;
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.
336 static void i8042_close(struct serio *port)
338 struct i8042_values *values = port->port_data;
340 if (values->mux != -1)
341 if (--i8042_mux_open)
342 return;
344 i8042_ctr &= ~values->irqen;
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;
351 free_irq(values->irq, i8042_request_irq_cookie);
353 i8042_flush();
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.
362 static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs)
364 unsigned long flags;
365 unsigned char str, data = 0;
366 unsigned int dfl;
367 unsigned int aux_idx;
368 int ret;
370 mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
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);
378 if (~str & I8042_STR_OBF) {
379 if (irq) dbg("Interrupt %d, without any data", irq);
380 ret = 0;
381 goto out;
384 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
385 static unsigned long last_transmit;
386 static unsigned char last_str;
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:
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).
403 if (time_before(jiffies, last_transmit + HZ/10)) {
404 str = last_str;
405 break;
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;
414 aux_idx = (str >> 6) & 3;
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" : "");
421 if (likely(i8042_mux_values[aux_idx].exists))
422 serio_interrupt(i8042_mux_port[aux_idx], data, dfl, regs);
424 last_str = str;
425 last_transmit = jiffies;
426 goto irq_ret;
429 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
430 ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
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" : "");
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);
446 irq_ret:
447 ret = 1;
448 out:
449 return IRQ_RETVAL(ret);
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.
458 static int i8042_enable_mux_mode(struct i8042_values *values, unsigned char *mux_version)
461 unsigned char param;
463 * Get rid of bytes in the queue.
466 i8042_flush();
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.
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;
484 if (mux_version)
485 *mux_version = ~param;
487 return 0;
492 * i8042_enable_mux_ports enables 4 individual AUX ports after
493 * the controller has been switched into Multiplexed mode
496 static int i8042_enable_mux_ports(struct i8042_values *values)
498 unsigned char param;
499 int i;
501 * Disable all muxed ports by disabling AUX.
504 i8042_ctr |= I8042_CTR_AUXDIS;
505 i8042_ctr &= ~I8042_CTR_AUXINT;
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;
513 * Enable all muxed ports.
516 for (i = 0; i < 4; i++) {
517 i8042_command(&param, I8042_CMD_MUX_PFX + i);
518 i8042_command(&param, I8042_CMD_AUX_ENABLE);
521 return 0;
526 * i8042_check_mux() checks whether the controller supports the PS/2 Active
527 * Multiplexing specification by Synaptics, Phoenix, Insyde and
528 * LCS/Telegraphics.
531 static int __init i8042_check_mux(struct i8042_values *values)
533 unsigned char mux_version;
535 if (i8042_enable_mux_mode(values, &mux_version))
536 return -1;
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;
543 printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
544 (mux_version >> 4) & 0xf, mux_version & 0xf);
546 if (i8042_enable_mux_ports(values))
547 return -1;
549 i8042_mux_present = 1;
550 return 0;
555 * i8042_check_aux() applies as much paranoia as it can at detecting
556 * the presence of an AUX interface.
559 static int __init i8042_check_aux(struct i8042_values *values)
561 unsigned char param;
562 static int i8042_check_aux_cookie;
565 * Check if AUX irq is available. If it isn't, then there is no point
566 * in trying to detect AUX presence.
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);
575 * Get rid of bytes in the queue.
578 i8042_flush();
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.
586 param = 0x5a;
587 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xa5) {
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.
597 if (i8042_command(&param, I8042_CMD_AUX_TEST)
598 || (param && param != 0xfa && param != 0xff))
599 return -1;
603 * Bit assignment test - filters out PS/2 i8042's in AT mode
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");
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;
619 * Disable the interface.
622 i8042_ctr |= I8042_CTR_AUXDIS;
623 i8042_ctr &= ~I8042_CTR_AUXINT;
625 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
626 return -1;
628 return 0;
633 * i8042_port_register() marks the device as existing,
634 * registers it, and reports to the user.
637 static int __init i8042_port_register(struct serio *port)
639 struct i8042_values *values = port->port_data;
641 values->exists = 1;
643 i8042_ctr &= ~values->disable;
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;
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);
657 serio_register_port(port);
659 return 0;
663 static void i8042_timer_func(unsigned long data)
665 i8042_interrupt(0, NULL, NULL);
670 * i8042_controller init initializes the i8042 controller, and,
671 * most importantly, sets it into non-xlated mode if that's
672 * desired.
675 static int i8042_controller_init(void)
677 unsigned long flags;
680 * Test the i8042. We need to know if it thinks it's working correctly
681 * before doing anything else.
684 i8042_flush();
686 if (i8042_reset) {
688 unsigned char param;
690 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
691 printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
692 return -1;
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;
703 * Save the CTR for restoral on unload / reboot.
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;
711 i8042_initial_ctr = i8042_ctr;
714 * Disable the keyboard interface and interrupt.
717 i8042_ctr |= I8042_CTR_KBDDIS;
718 i8042_ctr &= ~I8042_CTR_KBDINT;
721 * Handle keylock.
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");
731 spin_unlock_irqrestore(&i8042_lock, flags);
734 * If the chip is configured into nontranslated mode by the BIOS, don't
735 * bother enabling translating and be happy.
738 if (~i8042_ctr & I8042_CTR_XLATE)
739 i8042_direct = 1;
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.
748 if (i8042_direct)
749 i8042_ctr &= ~I8042_CTR_XLATE;
752 * Write CTR back.
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;
760 return 0;
765 * Reset the controller.
767 void i8042_controller_reset(void)
769 if (i8042_reset) {
770 unsigned char param;
772 if (i8042_command(&param, I8042_CMD_CTL_TEST))
773 printk(KERN_ERR "i8042.c: i8042 controller reset timeout.\n");
777 * Restore the original control register setting.
780 i8042_ctr = i8042_initial_ctr;
782 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
783 printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
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.
792 void i8042_controller_cleanup(void)
794 int i;
796 i8042_flush();
799 * Reset anything that is connected to the ports.
802 if (i8042_kbd_values.exists)
803 serio_cleanup(i8042_kbd_port);
805 if (i8042_aux_values.exists)
806 serio_cleanup(i8042_aux_port);
808 for (i = 0; i < I8042_NUM_MUX_PORTS; i++)
809 if (i8042_mux_values[i].exists)
810 serio_cleanup(i8042_mux_port[i]);
812 i8042_controller_reset();
817 * Here we try to restore the original BIOS settings
820 static int i8042_controller_suspend(void)
822 del_timer_sync(&i8042_timer);
823 i8042_controller_reset();
825 return 0;
830 * Here we try to reset everything back to a state in which suspended
833 static int i8042_controller_resume(void)
835 int i;
837 if (i8042_controller_init()) {
838 printk(KERN_ERR "i8042: resume failed\n");
839 return -1;
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");
849 * Reconnect anything that was connected to the ports.
852 if (i8042_kbd_values.exists && i8042_activate_port(i8042_kbd_port) == 0)
853 serio_reconnect(i8042_kbd_port);
855 if (i8042_aux_values.exists && i8042_activate_port(i8042_aux_port) == 0)
856 serio_reconnect(i8042_aux_port);
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]);
862 * Restart timer (for polling "stuck" data)
864 mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
866 return 0;
871 * We need to reset the 8042 back to original mode on system shutdown,
872 * because otherwise BIOSes will be confused.
875 static int i8042_notify_sys(struct notifier_block *this, unsigned long code,
876 void *unused)
878 if (code == SYS_DOWN || code == SYS_HALT)
879 i8042_controller_cleanup();
880 return NOTIFY_DONE;
883 static struct notifier_block i8042_notifier =
885 i8042_notify_sys,
886 NULL,
891 * Suspend/resume handlers for the new PM scheme (driver model)
893 static int i8042_suspend(struct device *dev, u32 state, u32 level)
895 return level == SUSPEND_DISABLE ? i8042_controller_suspend() : 0;
898 static int i8042_resume(struct device *dev, u32 level)
900 return level == RESUME_ENABLE ? i8042_controller_resume() : 0;
903 static void i8042_shutdown(struct device *dev)
905 i8042_controller_cleanup();
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,
917 * Suspend/resume handler for the old PM scheme (APM)
919 static int i8042_pm_callback(struct pm_dev *dev, pm_request_t request, void *dummy)
921 switch (request) {
922 case PM_SUSPEND:
923 return i8042_controller_suspend();
925 case PM_RESUME:
926 return i8042_controller_resume();
929 return 0;
932 static struct serio * __init i8042_allocate_kbd_port(void)
934 struct serio *serio;
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));
949 return serio;
952 static struct serio * __init i8042_allocate_aux_port(void)
954 struct serio *serio;
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));
969 return serio;
972 static struct serio * __init i8042_allocate_mux_port(int index)
974 struct serio *serio;
975 struct i8042_values *values = &i8042_mux_values[index];
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;
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);
994 return serio;
997 int __init i8042_init(void)
999 int i;
1000 int err;
1002 dbg_init();
1004 init_timer(&i8042_timer);
1005 i8042_timer.function = i8042_timer_func;
1007 if (i8042_platform_init())
1008 return -EBUSY;
1010 i8042_aux_values.irq = I8042_AUX_IRQ;
1011 i8042_kbd_values.irq = I8042_KBD_IRQ;
1013 if (i8042_controller_init())
1014 return -ENODEV;
1016 err = driver_register(&i8042_driver);
1017 if (err)
1018 return err;
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);
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]);
1033 else {
1034 i8042_aux_port = i8042_allocate_aux_port();
1035 if (i8042_aux_port)
1036 i8042_port_register(i8042_aux_port);
1040 i8042_kbd_port = i8042_allocate_kbd_port();
1041 if (i8042_kbd_port)
1042 i8042_port_register(i8042_kbd_port);
1044 mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
1046 i8042_pm_dev = pm_register(PM_SYS_DEV, PM_SYS_UNKNOWN, i8042_pm_callback);
1048 register_reboot_notifier(&i8042_notifier);
1050 return 0;
1053 void __exit i8042_exit(void)
1055 int i;
1057 unregister_reboot_notifier(&i8042_notifier);
1059 if (i8042_pm_dev)
1060 pm_unregister(i8042_pm_dev);
1062 i8042_controller_cleanup();
1064 if (i8042_kbd_values.exists)
1065 serio_unregister_port(i8042_kbd_port);
1067 if (i8042_aux_values.exists)
1068 serio_unregister_port(i8042_aux_port);
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]);
1074 del_timer_sync(&i8042_timer);
1076 platform_device_unregister(i8042_platform_device);
1077 driver_unregister(&i8042_driver);
1079 i8042_platform_exit();
1082 module_init(i8042_init);
1083 module_exit(i8042_exit);