GNU Linux-libre 5.4.200-gnu1
[releases.git] / drivers / input / serio / i8042.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  i8042 keyboard and mouse controller driver for Linux
4  *
5  *  Copyright (c) 1999-2004 Vojtech Pavlik
6  */
7
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/types.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/serio.h>
18 #include <linux/err.h>
19 #include <linux/rcupdate.h>
20 #include <linux/platform_device.h>
21 #include <linux/i8042.h>
22 #include <linux/slab.h>
23 #include <linux/suspend.h>
24
25 #include <asm/io.h>
26
27 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
29 MODULE_LICENSE("GPL");
30
31 static bool i8042_nokbd;
32 module_param_named(nokbd, i8042_nokbd, bool, 0);
33 MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
34
35 static bool i8042_noaux;
36 module_param_named(noaux, i8042_noaux, bool, 0);
37 MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
38
39 static bool i8042_nomux;
40 module_param_named(nomux, i8042_nomux, bool, 0);
41 MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
42
43 static bool i8042_unlock;
44 module_param_named(unlock, i8042_unlock, bool, 0);
45 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
46
47 static bool i8042_probe_defer;
48 module_param_named(probe_defer, i8042_probe_defer, bool, 0);
49 MODULE_PARM_DESC(probe_defer, "Allow deferred probing.");
50
51 enum i8042_controller_reset_mode {
52         I8042_RESET_NEVER,
53         I8042_RESET_ALWAYS,
54         I8042_RESET_ON_S2RAM,
55 #define I8042_RESET_DEFAULT     I8042_RESET_ON_S2RAM
56 };
57 static enum i8042_controller_reset_mode i8042_reset = I8042_RESET_DEFAULT;
58 static int i8042_set_reset(const char *val, const struct kernel_param *kp)
59 {
60         enum i8042_controller_reset_mode *arg = kp->arg;
61         int error;
62         bool reset;
63
64         if (val) {
65                 error = kstrtobool(val, &reset);
66                 if (error)
67                         return error;
68         } else {
69                 reset = true;
70         }
71
72         *arg = reset ? I8042_RESET_ALWAYS : I8042_RESET_NEVER;
73         return 0;
74 }
75
76 static const struct kernel_param_ops param_ops_reset_param = {
77         .flags = KERNEL_PARAM_OPS_FL_NOARG,
78         .set = i8042_set_reset,
79 };
80 #define param_check_reset_param(name, p)        \
81         __param_check(name, p, enum i8042_controller_reset_mode)
82 module_param_named(reset, i8042_reset, reset_param, 0);
83 MODULE_PARM_DESC(reset, "Reset controller on resume, cleanup or both");
84
85 static bool i8042_direct;
86 module_param_named(direct, i8042_direct, bool, 0);
87 MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
88
89 static bool i8042_dumbkbd;
90 module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
91 MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
92
93 static bool i8042_noloop;
94 module_param_named(noloop, i8042_noloop, bool, 0);
95 MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
96
97 static bool i8042_notimeout;
98 module_param_named(notimeout, i8042_notimeout, bool, 0);
99 MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
100
101 static bool i8042_kbdreset;
102 module_param_named(kbdreset, i8042_kbdreset, bool, 0);
103 MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
104
105 #ifdef CONFIG_X86
106 static bool i8042_dritek;
107 module_param_named(dritek, i8042_dritek, bool, 0);
108 MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
109 #endif
110
111 #ifdef CONFIG_PNP
112 static bool i8042_nopnp;
113 module_param_named(nopnp, i8042_nopnp, bool, 0);
114 MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
115 #endif
116
117 #define DEBUG
118 #ifdef DEBUG
119 static bool i8042_debug;
120 module_param_named(debug, i8042_debug, bool, 0600);
121 MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
122
123 static bool i8042_unmask_kbd_data;
124 module_param_named(unmask_kbd_data, i8042_unmask_kbd_data, bool, 0600);
125 MODULE_PARM_DESC(unmask_kbd_data, "Unconditional enable (may reveal sensitive data) of normally sanitize-filtered kbd data traffic debug log [pre-condition: i8042.debug=1 enabled]");
126 #endif
127
128 static bool i8042_present;
129 static bool i8042_bypass_aux_irq_test;
130 static char i8042_kbd_firmware_id[128];
131 static char i8042_aux_firmware_id[128];
132
133 #include "i8042.h"
134
135 /*
136  * i8042_lock protects serialization between i8042_command and
137  * the interrupt handler.
138  */
139 static DEFINE_SPINLOCK(i8042_lock);
140
141 /*
142  * Writers to AUX and KBD ports as well as users issuing i8042_command
143  * directly should acquire i8042_mutex (by means of calling
144  * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
145  * they do not disturb each other (unfortunately in many i8042
146  * implementations write to one of the ports will immediately abort
147  * command that is being processed by another port).
148  */
149 static DEFINE_MUTEX(i8042_mutex);
150
151 struct i8042_port {
152         struct serio *serio;
153         int irq;
154         bool exists;
155         bool driver_bound;
156         signed char mux;
157 };
158
159 #define I8042_KBD_PORT_NO       0
160 #define I8042_AUX_PORT_NO       1
161 #define I8042_MUX_PORT_NO       2
162 #define I8042_NUM_PORTS         (I8042_NUM_MUX_PORTS + 2)
163
164 static struct i8042_port i8042_ports[I8042_NUM_PORTS];
165
166 static unsigned char i8042_initial_ctr;
167 static unsigned char i8042_ctr;
168 static bool i8042_mux_present;
169 static bool i8042_kbd_irq_registered;
170 static bool i8042_aux_irq_registered;
171 static unsigned char i8042_suppress_kbd_ack;
172 static struct platform_device *i8042_platform_device;
173 static struct notifier_block i8042_kbd_bind_notifier_block;
174
175 static irqreturn_t i8042_interrupt(int irq, void *dev_id);
176 static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
177                                      struct serio *serio);
178
179 void i8042_lock_chip(void)
180 {
181         mutex_lock(&i8042_mutex);
182 }
183 EXPORT_SYMBOL(i8042_lock_chip);
184
185 void i8042_unlock_chip(void)
186 {
187         mutex_unlock(&i8042_mutex);
188 }
189 EXPORT_SYMBOL(i8042_unlock_chip);
190
191 int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
192                                         struct serio *serio))
193 {
194         unsigned long flags;
195         int ret = 0;
196
197         spin_lock_irqsave(&i8042_lock, flags);
198
199         if (i8042_platform_filter) {
200                 ret = -EBUSY;
201                 goto out;
202         }
203
204         i8042_platform_filter = filter;
205
206 out:
207         spin_unlock_irqrestore(&i8042_lock, flags);
208         return ret;
209 }
210 EXPORT_SYMBOL(i8042_install_filter);
211
212 int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
213                                        struct serio *port))
214 {
215         unsigned long flags;
216         int ret = 0;
217
218         spin_lock_irqsave(&i8042_lock, flags);
219
220         if (i8042_platform_filter != filter) {
221                 ret = -EINVAL;
222                 goto out;
223         }
224
225         i8042_platform_filter = NULL;
226
227 out:
228         spin_unlock_irqrestore(&i8042_lock, flags);
229         return ret;
230 }
231 EXPORT_SYMBOL(i8042_remove_filter);
232
233 /*
234  * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
235  * be ready for reading values from it / writing values to it.
236  * Called always with i8042_lock held.
237  */
238
239 static int i8042_wait_read(void)
240 {
241         int i = 0;
242
243         while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
244                 udelay(50);
245                 i++;
246         }
247         return -(i == I8042_CTL_TIMEOUT);
248 }
249
250 static int i8042_wait_write(void)
251 {
252         int i = 0;
253
254         while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
255                 udelay(50);
256                 i++;
257         }
258         return -(i == I8042_CTL_TIMEOUT);
259 }
260
261 /*
262  * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
263  * of the i8042 down the toilet.
264  */
265
266 static int i8042_flush(void)
267 {
268         unsigned long flags;
269         unsigned char data, str;
270         int count = 0;
271         int retval = 0;
272
273         spin_lock_irqsave(&i8042_lock, flags);
274
275         while ((str = i8042_read_status()) & I8042_STR_OBF) {
276                 if (count++ < I8042_BUFFER_SIZE) {
277                         udelay(50);
278                         data = i8042_read_data();
279                         dbg("%02x <- i8042 (flush, %s)\n",
280                             data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
281                 } else {
282                         retval = -EIO;
283                         break;
284                 }
285         }
286
287         spin_unlock_irqrestore(&i8042_lock, flags);
288
289         return retval;
290 }
291
292 /*
293  * i8042_command() executes a command on the i8042. It also sends the input
294  * parameter(s) of the commands to it, and receives the output value(s). The
295  * parameters are to be stored in the param array, and the output is placed
296  * into the same array. The number of the parameters and output values is
297  * encoded in bits 8-11 of the command number.
298  */
299
300 static int __i8042_command(unsigned char *param, int command)
301 {
302         int i, error;
303
304         if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
305                 return -1;
306
307         error = i8042_wait_write();
308         if (error)
309                 return error;
310
311         dbg("%02x -> i8042 (command)\n", command & 0xff);
312         i8042_write_command(command & 0xff);
313
314         for (i = 0; i < ((command >> 12) & 0xf); i++) {
315                 error = i8042_wait_write();
316                 if (error) {
317                         dbg("     -- i8042 (wait write timeout)\n");
318                         return error;
319                 }
320                 dbg("%02x -> i8042 (parameter)\n", param[i]);
321                 i8042_write_data(param[i]);
322         }
323
324         for (i = 0; i < ((command >> 8) & 0xf); i++) {
325                 error = i8042_wait_read();
326                 if (error) {
327                         dbg("     -- i8042 (wait read timeout)\n");
328                         return error;
329                 }
330
331                 if (command == I8042_CMD_AUX_LOOP &&
332                     !(i8042_read_status() & I8042_STR_AUXDATA)) {
333                         dbg("     -- i8042 (auxerr)\n");
334                         return -1;
335                 }
336
337                 param[i] = i8042_read_data();
338                 dbg("%02x <- i8042 (return)\n", param[i]);
339         }
340
341         return 0;
342 }
343
344 int i8042_command(unsigned char *param, int command)
345 {
346         unsigned long flags;
347         int retval;
348
349         if (!i8042_present)
350                 return -1;
351
352         spin_lock_irqsave(&i8042_lock, flags);
353         retval = __i8042_command(param, command);
354         spin_unlock_irqrestore(&i8042_lock, flags);
355
356         return retval;
357 }
358 EXPORT_SYMBOL(i8042_command);
359
360 /*
361  * i8042_kbd_write() sends a byte out through the keyboard interface.
362  */
363
364 static int i8042_kbd_write(struct serio *port, unsigned char c)
365 {
366         unsigned long flags;
367         int retval = 0;
368
369         spin_lock_irqsave(&i8042_lock, flags);
370
371         if (!(retval = i8042_wait_write())) {
372                 dbg("%02x -> i8042 (kbd-data)\n", c);
373                 i8042_write_data(c);
374         }
375
376         spin_unlock_irqrestore(&i8042_lock, flags);
377
378         return retval;
379 }
380
381 /*
382  * i8042_aux_write() sends a byte out through the aux interface.
383  */
384
385 static int i8042_aux_write(struct serio *serio, unsigned char c)
386 {
387         struct i8042_port *port = serio->port_data;
388
389         return i8042_command(&c, port->mux == -1 ?
390                                         I8042_CMD_AUX_SEND :
391                                         I8042_CMD_MUX_SEND + port->mux);
392 }
393
394
395 /*
396  * i8042_port_close attempts to clear AUX or KBD port state by disabling
397  * and then re-enabling it.
398  */
399
400 static void i8042_port_close(struct serio *serio)
401 {
402         int irq_bit;
403         int disable_bit;
404         const char *port_name;
405
406         if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
407                 irq_bit = I8042_CTR_AUXINT;
408                 disable_bit = I8042_CTR_AUXDIS;
409                 port_name = "AUX";
410         } else {
411                 irq_bit = I8042_CTR_KBDINT;
412                 disable_bit = I8042_CTR_KBDDIS;
413                 port_name = "KBD";
414         }
415
416         i8042_ctr &= ~irq_bit;
417         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
418                 pr_warn("Can't write CTR while closing %s port\n", port_name);
419
420         udelay(50);
421
422         i8042_ctr &= ~disable_bit;
423         i8042_ctr |= irq_bit;
424         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
425                 pr_err("Can't reactivate %s port\n", port_name);
426
427         /*
428          * See if there is any data appeared while we were messing with
429          * port state.
430          */
431         i8042_interrupt(0, NULL);
432 }
433
434 /*
435  * i8042_start() is called by serio core when port is about to finish
436  * registering. It will mark port as existing so i8042_interrupt can
437  * start sending data through it.
438  */
439 static int i8042_start(struct serio *serio)
440 {
441         struct i8042_port *port = serio->port_data;
442
443         device_set_wakeup_capable(&serio->dev, true);
444
445         /*
446          * On platforms using suspend-to-idle, allow the keyboard to
447          * wake up the system from sleep by enabling keyboard wakeups
448          * by default.  This is consistent with keyboard wakeup
449          * behavior on many platforms using suspend-to-RAM (ACPI S3)
450          * by default.
451          */
452         if (pm_suspend_default_s2idle() &&
453             serio == i8042_ports[I8042_KBD_PORT_NO].serio) {
454                 device_set_wakeup_enable(&serio->dev, true);
455         }
456
457         spin_lock_irq(&i8042_lock);
458         port->exists = true;
459         spin_unlock_irq(&i8042_lock);
460
461         return 0;
462 }
463
464 /*
465  * i8042_stop() marks serio port as non-existing so i8042_interrupt
466  * will not try to send data to the port that is about to go away.
467  * The function is called by serio core as part of unregister procedure.
468  */
469 static void i8042_stop(struct serio *serio)
470 {
471         struct i8042_port *port = serio->port_data;
472
473         spin_lock_irq(&i8042_lock);
474         port->exists = false;
475         port->serio = NULL;
476         spin_unlock_irq(&i8042_lock);
477
478         /*
479          * We need to make sure that interrupt handler finishes using
480          * our serio port before we return from this function.
481          * We synchronize with both AUX and KBD IRQs because there is
482          * a (very unlikely) chance that AUX IRQ is raised for KBD port
483          * and vice versa.
484          */
485         synchronize_irq(I8042_AUX_IRQ);
486         synchronize_irq(I8042_KBD_IRQ);
487 }
488
489 /*
490  * i8042_filter() filters out unwanted bytes from the input data stream.
491  * It is called from i8042_interrupt and thus is running with interrupts
492  * off and i8042_lock held.
493  */
494 static bool i8042_filter(unsigned char data, unsigned char str,
495                          struct serio *serio)
496 {
497         if (unlikely(i8042_suppress_kbd_ack)) {
498                 if ((~str & I8042_STR_AUXDATA) &&
499                     (data == 0xfa || data == 0xfe)) {
500                         i8042_suppress_kbd_ack--;
501                         dbg("Extra keyboard ACK - filtered out\n");
502                         return true;
503                 }
504         }
505
506         if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
507                 dbg("Filtered out by platform filter\n");
508                 return true;
509         }
510
511         return false;
512 }
513
514 /*
515  * i8042_interrupt() is the most important function in this driver -
516  * it handles the interrupts from the i8042, and sends incoming bytes
517  * to the upper layers.
518  */
519
520 static irqreturn_t i8042_interrupt(int irq, void *dev_id)
521 {
522         struct i8042_port *port;
523         struct serio *serio;
524         unsigned long flags;
525         unsigned char str, data;
526         unsigned int dfl;
527         unsigned int port_no;
528         bool filtered;
529         int ret = 1;
530
531         spin_lock_irqsave(&i8042_lock, flags);
532
533         str = i8042_read_status();
534         if (unlikely(~str & I8042_STR_OBF)) {
535                 spin_unlock_irqrestore(&i8042_lock, flags);
536                 if (irq)
537                         dbg("Interrupt %d, without any data\n", irq);
538                 ret = 0;
539                 goto out;
540         }
541
542         data = i8042_read_data();
543
544         if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
545                 static unsigned long last_transmit;
546                 static unsigned char last_str;
547
548                 dfl = 0;
549                 if (str & I8042_STR_MUXERR) {
550                         dbg("MUX error, status is %02x, data is %02x\n",
551                             str, data);
552 /*
553  * When MUXERR condition is signalled the data register can only contain
554  * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
555  * it is not always the case. Some KBCs also report 0xfc when there is
556  * nothing connected to the port while others sometimes get confused which
557  * port the data came from and signal error leaving the data intact. They
558  * _do not_ revert to legacy mode (actually I've never seen KBC reverting
559  * to legacy mode yet, when we see one we'll add proper handling).
560  * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
561  * rest assume that the data came from the same serio last byte
562  * was transmitted (if transmission happened not too long ago).
563  */
564
565                         switch (data) {
566                                 default:
567                                         if (time_before(jiffies, last_transmit + HZ/10)) {
568                                                 str = last_str;
569                                                 break;
570                                         }
571                                         /* fall through - report timeout */
572                                 case 0xfc:
573                                 case 0xfd:
574                                 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
575                                 case 0xff: dfl = SERIO_PARITY;  data = 0xfe; break;
576                         }
577                 }
578
579                 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
580                 last_str = str;
581                 last_transmit = jiffies;
582         } else {
583
584                 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
585                       ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
586
587                 port_no = (str & I8042_STR_AUXDATA) ?
588                                 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
589         }
590
591         port = &i8042_ports[port_no];
592         serio = port->exists ? port->serio : NULL;
593
594         filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
595                    port_no, irq,
596                    dfl & SERIO_PARITY ? ", bad parity" : "",
597                    dfl & SERIO_TIMEOUT ? ", timeout" : "");
598
599         filtered = i8042_filter(data, str, serio);
600
601         spin_unlock_irqrestore(&i8042_lock, flags);
602
603         if (likely(serio && !filtered))
604                 serio_interrupt(serio, data, dfl);
605
606  out:
607         return IRQ_RETVAL(ret);
608 }
609
610 /*
611  * i8042_enable_kbd_port enables keyboard port on chip
612  */
613
614 static int i8042_enable_kbd_port(void)
615 {
616         i8042_ctr &= ~I8042_CTR_KBDDIS;
617         i8042_ctr |= I8042_CTR_KBDINT;
618
619         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
620                 i8042_ctr &= ~I8042_CTR_KBDINT;
621                 i8042_ctr |= I8042_CTR_KBDDIS;
622                 pr_err("Failed to enable KBD port\n");
623                 return -EIO;
624         }
625
626         return 0;
627 }
628
629 /*
630  * i8042_enable_aux_port enables AUX (mouse) port on chip
631  */
632
633 static int i8042_enable_aux_port(void)
634 {
635         i8042_ctr &= ~I8042_CTR_AUXDIS;
636         i8042_ctr |= I8042_CTR_AUXINT;
637
638         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
639                 i8042_ctr &= ~I8042_CTR_AUXINT;
640                 i8042_ctr |= I8042_CTR_AUXDIS;
641                 pr_err("Failed to enable AUX port\n");
642                 return -EIO;
643         }
644
645         return 0;
646 }
647
648 /*
649  * i8042_enable_mux_ports enables 4 individual AUX ports after
650  * the controller has been switched into Multiplexed mode
651  */
652
653 static int i8042_enable_mux_ports(void)
654 {
655         unsigned char param;
656         int i;
657
658         for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
659                 i8042_command(&param, I8042_CMD_MUX_PFX + i);
660                 i8042_command(&param, I8042_CMD_AUX_ENABLE);
661         }
662
663         return i8042_enable_aux_port();
664 }
665
666 /*
667  * i8042_set_mux_mode checks whether the controller has an
668  * active multiplexor and puts the chip into Multiplexed (true)
669  * or Legacy (false) mode.
670  */
671
672 static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
673 {
674
675         unsigned char param, val;
676 /*
677  * Get rid of bytes in the queue.
678  */
679
680         i8042_flush();
681
682 /*
683  * Internal loopback test - send three bytes, they should come back from the
684  * mouse interface, the last should be version.
685  */
686
687         param = val = 0xf0;
688         if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
689                 return -1;
690         param = val = multiplex ? 0x56 : 0xf6;
691         if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
692                 return -1;
693         param = val = multiplex ? 0xa4 : 0xa5;
694         if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
695                 return -1;
696
697 /*
698  * Workaround for interference with USB Legacy emulation
699  * that causes a v10.12 MUX to be found.
700  */
701         if (param == 0xac)
702                 return -1;
703
704         if (mux_version)
705                 *mux_version = param;
706
707         return 0;
708 }
709
710 /*
711  * i8042_check_mux() checks whether the controller supports the PS/2 Active
712  * Multiplexing specification by Synaptics, Phoenix, Insyde and
713  * LCS/Telegraphics.
714  */
715
716 static int i8042_check_mux(void)
717 {
718         unsigned char mux_version;
719
720         if (i8042_set_mux_mode(true, &mux_version))
721                 return -1;
722
723         pr_info("Detected active multiplexing controller, rev %d.%d\n",
724                 (mux_version >> 4) & 0xf, mux_version & 0xf);
725
726 /*
727  * Disable all muxed ports by disabling AUX.
728  */
729         i8042_ctr |= I8042_CTR_AUXDIS;
730         i8042_ctr &= ~I8042_CTR_AUXINT;
731
732         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
733                 pr_err("Failed to disable AUX port, can't use MUX\n");
734                 return -EIO;
735         }
736
737         i8042_mux_present = true;
738
739         return 0;
740 }
741
742 /*
743  * The following is used to test AUX IRQ delivery.
744  */
745 static struct completion i8042_aux_irq_delivered;
746 static bool i8042_irq_being_tested;
747
748 static irqreturn_t i8042_aux_test_irq(int irq, void *dev_id)
749 {
750         unsigned long flags;
751         unsigned char str, data;
752         int ret = 0;
753
754         spin_lock_irqsave(&i8042_lock, flags);
755         str = i8042_read_status();
756         if (str & I8042_STR_OBF) {
757                 data = i8042_read_data();
758                 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
759                     data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
760                 if (i8042_irq_being_tested &&
761                     data == 0xa5 && (str & I8042_STR_AUXDATA))
762                         complete(&i8042_aux_irq_delivered);
763                 ret = 1;
764         }
765         spin_unlock_irqrestore(&i8042_lock, flags);
766
767         return IRQ_RETVAL(ret);
768 }
769
770 /*
771  * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
772  * verifies success by readinng CTR. Used when testing for presence of AUX
773  * port.
774  */
775 static int i8042_toggle_aux(bool on)
776 {
777         unsigned char param;
778         int i;
779
780         if (i8042_command(&param,
781                         on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
782                 return -1;
783
784         /* some chips need some time to set the I8042_CTR_AUXDIS bit */
785         for (i = 0; i < 100; i++) {
786                 udelay(50);
787
788                 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
789                         return -1;
790
791                 if (!(param & I8042_CTR_AUXDIS) == on)
792                         return 0;
793         }
794
795         return -1;
796 }
797
798 /*
799  * i8042_check_aux() applies as much paranoia as it can at detecting
800  * the presence of an AUX interface.
801  */
802
803 static int i8042_check_aux(void)
804 {
805         int retval = -1;
806         bool irq_registered = false;
807         bool aux_loop_broken = false;
808         unsigned long flags;
809         unsigned char param;
810
811 /*
812  * Get rid of bytes in the queue.
813  */
814
815         i8042_flush();
816
817 /*
818  * Internal loopback test - filters out AT-type i8042's. Unfortunately
819  * SiS screwed up and their 5597 doesn't support the LOOP command even
820  * though it has an AUX port.
821  */
822
823         param = 0x5a;
824         retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
825         if (retval || param != 0x5a) {
826
827 /*
828  * External connection test - filters out AT-soldered PS/2 i8042's
829  * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
830  * 0xfa - no error on some notebooks which ignore the spec
831  * Because it's common for chipsets to return error on perfectly functioning
832  * AUX ports, we test for this only when the LOOP command failed.
833  */
834
835                 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
836                     (param && param != 0xfa && param != 0xff))
837                         return -1;
838
839 /*
840  * If AUX_LOOP completed without error but returned unexpected data
841  * mark it as broken
842  */
843                 if (!retval)
844                         aux_loop_broken = true;
845         }
846
847 /*
848  * Bit assignment test - filters out PS/2 i8042's in AT mode
849  */
850
851         if (i8042_toggle_aux(false)) {
852                 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
853                 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
854         }
855
856         if (i8042_toggle_aux(true))
857                 return -1;
858
859 /*
860  * Reset keyboard (needed on some laptops to successfully detect
861  * touchpad, e.g., some Gigabyte laptop models with Elantech
862  * touchpads).
863  */
864         if (i8042_kbdreset) {
865                 pr_warn("Attempting to reset device connected to KBD port\n");
866                 i8042_kbd_write(NULL, (unsigned char) 0xff);
867         }
868
869 /*
870  * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
871  * used it for a PCI card or somethig else.
872  */
873
874         if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
875 /*
876  * Without LOOP command we can't test AUX IRQ delivery. Assume the port
877  * is working and hope we are right.
878  */
879                 retval = 0;
880                 goto out;
881         }
882
883         if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
884                         "i8042", i8042_platform_device))
885                 goto out;
886
887         irq_registered = true;
888
889         if (i8042_enable_aux_port())
890                 goto out;
891
892         spin_lock_irqsave(&i8042_lock, flags);
893
894         init_completion(&i8042_aux_irq_delivered);
895         i8042_irq_being_tested = true;
896
897         param = 0xa5;
898         retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
899
900         spin_unlock_irqrestore(&i8042_lock, flags);
901
902         if (retval)
903                 goto out;
904
905         if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
906                                         msecs_to_jiffies(250)) == 0) {
907 /*
908  * AUX IRQ was never delivered so we need to flush the controller to
909  * get rid of the byte we put there; otherwise keyboard may not work.
910  */
911                 dbg("     -- i8042 (aux irq test timeout)\n");
912                 i8042_flush();
913                 retval = -1;
914         }
915
916  out:
917
918 /*
919  * Disable the interface.
920  */
921
922         i8042_ctr |= I8042_CTR_AUXDIS;
923         i8042_ctr &= ~I8042_CTR_AUXINT;
924
925         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
926                 retval = -1;
927
928         if (irq_registered)
929                 free_irq(I8042_AUX_IRQ, i8042_platform_device);
930
931         return retval;
932 }
933
934 static int i8042_controller_check(void)
935 {
936         if (i8042_flush()) {
937                 pr_info("No controller found\n");
938                 return -ENODEV;
939         }
940
941         return 0;
942 }
943
944 static int i8042_controller_selftest(void)
945 {
946         unsigned char param;
947         int i = 0;
948
949         /*
950          * We try this 5 times; on some really fragile systems this does not
951          * take the first time...
952          */
953         do {
954
955                 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
956                         pr_err("i8042 controller selftest timeout\n");
957                         return -ENODEV;
958                 }
959
960                 if (param == I8042_RET_CTL_TEST)
961                         return 0;
962
963                 dbg("i8042 controller selftest: %#x != %#x\n",
964                     param, I8042_RET_CTL_TEST);
965                 msleep(50);
966         } while (i++ < 5);
967
968 #ifdef CONFIG_X86
969         /*
970          * On x86, we don't fail entire i8042 initialization if controller
971          * reset fails in hopes that keyboard port will still be functional
972          * and user will still get a working keyboard. This is especially
973          * important on netbooks. On other arches we trust hardware more.
974          */
975         pr_info("giving up on controller selftest, continuing anyway...\n");
976         return 0;
977 #else
978         pr_err("i8042 controller selftest failed\n");
979         return -EIO;
980 #endif
981 }
982
983 /*
984  * i8042_controller init initializes the i8042 controller, and,
985  * most importantly, sets it into non-xlated mode if that's
986  * desired.
987  */
988
989 static int i8042_controller_init(void)
990 {
991         unsigned long flags;
992         int n = 0;
993         unsigned char ctr[2];
994
995 /*
996  * Save the CTR for restore on unload / reboot.
997  */
998
999         do {
1000                 if (n >= 10) {
1001                         pr_err("Unable to get stable CTR read\n");
1002                         return -EIO;
1003                 }
1004
1005                 if (n != 0)
1006                         udelay(50);
1007
1008                 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
1009                         pr_err("Can't read CTR while initializing i8042\n");
1010                         return i8042_probe_defer ? -EPROBE_DEFER : -EIO;
1011                 }
1012
1013         } while (n < 2 || ctr[0] != ctr[1]);
1014
1015         i8042_initial_ctr = i8042_ctr = ctr[0];
1016
1017 /*
1018  * Disable the keyboard interface and interrupt.
1019  */
1020
1021         i8042_ctr |= I8042_CTR_KBDDIS;
1022         i8042_ctr &= ~I8042_CTR_KBDINT;
1023
1024 /*
1025  * Handle keylock.
1026  */
1027
1028         spin_lock_irqsave(&i8042_lock, flags);
1029         if (~i8042_read_status() & I8042_STR_KEYLOCK) {
1030                 if (i8042_unlock)
1031                         i8042_ctr |= I8042_CTR_IGNKEYLOCK;
1032                 else
1033                         pr_warn("Warning: Keylock active\n");
1034         }
1035         spin_unlock_irqrestore(&i8042_lock, flags);
1036
1037 /*
1038  * If the chip is configured into nontranslated mode by the BIOS, don't
1039  * bother enabling translating and be happy.
1040  */
1041
1042         if (~i8042_ctr & I8042_CTR_XLATE)
1043                 i8042_direct = true;
1044
1045 /*
1046  * Set nontranslated mode for the kbd interface if requested by an option.
1047  * After this the kbd interface becomes a simple serial in/out, like the aux
1048  * interface is. We don't do this by default, since it can confuse notebook
1049  * BIOSes.
1050  */
1051
1052         if (i8042_direct)
1053                 i8042_ctr &= ~I8042_CTR_XLATE;
1054
1055 /*
1056  * Write CTR back.
1057  */
1058
1059         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1060                 pr_err("Can't write CTR while initializing i8042\n");
1061                 return -EIO;
1062         }
1063
1064 /*
1065  * Flush whatever accumulated while we were disabling keyboard port.
1066  */
1067
1068         i8042_flush();
1069
1070         return 0;
1071 }
1072
1073
1074 /*
1075  * Reset the controller and reset CRT to the original value set by BIOS.
1076  */
1077
1078 static void i8042_controller_reset(bool s2r_wants_reset)
1079 {
1080         i8042_flush();
1081
1082 /*
1083  * Disable both KBD and AUX interfaces so they don't get in the way
1084  */
1085
1086         i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1087         i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1088
1089         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
1090                 pr_warn("Can't write CTR while resetting\n");
1091
1092 /*
1093  * Disable MUX mode if present.
1094  */
1095
1096         if (i8042_mux_present)
1097                 i8042_set_mux_mode(false, NULL);
1098
1099 /*
1100  * Reset the controller if requested.
1101  */
1102
1103         if (i8042_reset == I8042_RESET_ALWAYS ||
1104             (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1105                 i8042_controller_selftest();
1106         }
1107
1108 /*
1109  * Restore the original control register setting.
1110  */
1111
1112         if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
1113                 pr_warn("Can't restore CTR\n");
1114 }
1115
1116
1117 /*
1118  * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1119  * when kernel panics. Flashing LEDs is useful for users running X who may
1120  * not see the console and will help distinguishing panics from "real"
1121  * lockups.
1122  *
1123  * Note that DELAY has a limit of 10ms so we will not get stuck here
1124  * waiting for KBC to free up even if KBD interrupt is off
1125  */
1126
1127 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1128
1129 static long i8042_panic_blink(int state)
1130 {
1131         long delay = 0;
1132         char led;
1133
1134         led = (state) ? 0x01 | 0x04 : 0;
1135         while (i8042_read_status() & I8042_STR_IBF)
1136                 DELAY;
1137         dbg("%02x -> i8042 (panic blink)\n", 0xed);
1138         i8042_suppress_kbd_ack = 2;
1139         i8042_write_data(0xed); /* set leds */
1140         DELAY;
1141         while (i8042_read_status() & I8042_STR_IBF)
1142                 DELAY;
1143         DELAY;
1144         dbg("%02x -> i8042 (panic blink)\n", led);
1145         i8042_write_data(led);
1146         DELAY;
1147         return delay;
1148 }
1149
1150 #undef DELAY
1151
1152 #ifdef CONFIG_X86
1153 static void i8042_dritek_enable(void)
1154 {
1155         unsigned char param = 0x90;
1156         int error;
1157
1158         error = i8042_command(&param, 0x1059);
1159         if (error)
1160                 pr_warn("Failed to enable DRITEK extension: %d\n", error);
1161 }
1162 #endif
1163
1164 #ifdef CONFIG_PM
1165
1166 /*
1167  * Here we try to reset everything back to a state we had
1168  * before suspending.
1169  */
1170
1171 static int i8042_controller_resume(bool s2r_wants_reset)
1172 {
1173         int error;
1174
1175         error = i8042_controller_check();
1176         if (error)
1177                 return error;
1178
1179         if (i8042_reset == I8042_RESET_ALWAYS ||
1180             (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1181                 error = i8042_controller_selftest();
1182                 if (error)
1183                         return error;
1184         }
1185
1186 /*
1187  * Restore original CTR value and disable all ports
1188  */
1189
1190         i8042_ctr = i8042_initial_ctr;
1191         if (i8042_direct)
1192                 i8042_ctr &= ~I8042_CTR_XLATE;
1193         i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1194         i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
1195         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1196                 pr_warn("Can't write CTR to resume, retrying...\n");
1197                 msleep(50);
1198                 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1199                         pr_err("CTR write retry failed\n");
1200                         return -EIO;
1201                 }
1202         }
1203
1204
1205 #ifdef CONFIG_X86
1206         if (i8042_dritek)
1207                 i8042_dritek_enable();
1208 #endif
1209
1210         if (i8042_mux_present) {
1211                 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
1212                         pr_warn("failed to resume active multiplexor, mouse won't work\n");
1213         } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1214                 i8042_enable_aux_port();
1215
1216         if (i8042_ports[I8042_KBD_PORT_NO].serio)
1217                 i8042_enable_kbd_port();
1218
1219         i8042_interrupt(0, NULL);
1220
1221         return 0;
1222 }
1223
1224 /*
1225  * Here we try to restore the original BIOS settings to avoid
1226  * upsetting it.
1227  */
1228
1229 static int i8042_pm_suspend(struct device *dev)
1230 {
1231         int i;
1232
1233         if (pm_suspend_via_firmware())
1234                 i8042_controller_reset(true);
1235
1236         /* Set up serio interrupts for system wakeup. */
1237         for (i = 0; i < I8042_NUM_PORTS; i++) {
1238                 struct serio *serio = i8042_ports[i].serio;
1239
1240                 if (serio && device_may_wakeup(&serio->dev))
1241                         enable_irq_wake(i8042_ports[i].irq);
1242         }
1243
1244         return 0;
1245 }
1246
1247 static int i8042_pm_resume_noirq(struct device *dev)
1248 {
1249         if (!pm_resume_via_firmware())
1250                 i8042_interrupt(0, NULL);
1251
1252         return 0;
1253 }
1254
1255 static int i8042_pm_resume(struct device *dev)
1256 {
1257         bool want_reset;
1258         int i;
1259
1260         for (i = 0; i < I8042_NUM_PORTS; i++) {
1261                 struct serio *serio = i8042_ports[i].serio;
1262
1263                 if (serio && device_may_wakeup(&serio->dev))
1264                         disable_irq_wake(i8042_ports[i].irq);
1265         }
1266
1267         /*
1268          * If platform firmware was not going to be involved in suspend, we did
1269          * not restore the controller state to whatever it had been at boot
1270          * time, so we do not need to do anything.
1271          */
1272         if (!pm_suspend_via_firmware())
1273                 return 0;
1274
1275         /*
1276          * We only need to reset the controller if we are resuming after handing
1277          * off control to the platform firmware, otherwise we can simply restore
1278          * the mode.
1279          */
1280         want_reset = pm_resume_via_firmware();
1281
1282         return i8042_controller_resume(want_reset);
1283 }
1284
1285 static int i8042_pm_thaw(struct device *dev)
1286 {
1287         i8042_interrupt(0, NULL);
1288
1289         return 0;
1290 }
1291
1292 static int i8042_pm_reset(struct device *dev)
1293 {
1294         i8042_controller_reset(false);
1295
1296         return 0;
1297 }
1298
1299 static int i8042_pm_restore(struct device *dev)
1300 {
1301         return i8042_controller_resume(false);
1302 }
1303
1304 static const struct dev_pm_ops i8042_pm_ops = {
1305         .suspend        = i8042_pm_suspend,
1306         .resume_noirq   = i8042_pm_resume_noirq,
1307         .resume         = i8042_pm_resume,
1308         .thaw           = i8042_pm_thaw,
1309         .poweroff       = i8042_pm_reset,
1310         .restore        = i8042_pm_restore,
1311 };
1312
1313 #endif /* CONFIG_PM */
1314
1315 /*
1316  * We need to reset the 8042 back to original mode on system shutdown,
1317  * because otherwise BIOSes will be confused.
1318  */
1319
1320 static void i8042_shutdown(struct platform_device *dev)
1321 {
1322         i8042_controller_reset(false);
1323 }
1324
1325 static int i8042_create_kbd_port(void)
1326 {
1327         struct serio *serio;
1328         struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1329
1330         serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1331         if (!serio)
1332                 return -ENOMEM;
1333
1334         serio->id.type          = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1335         serio->write            = i8042_dumbkbd ? NULL : i8042_kbd_write;
1336         serio->start            = i8042_start;
1337         serio->stop             = i8042_stop;
1338         serio->close            = i8042_port_close;
1339         serio->ps2_cmd_mutex    = &i8042_mutex;
1340         serio->port_data        = port;
1341         serio->dev.parent       = &i8042_platform_device->dev;
1342         strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
1343         strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1344         strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1345                 sizeof(serio->firmware_id));
1346
1347         port->serio = serio;
1348         port->irq = I8042_KBD_IRQ;
1349
1350         return 0;
1351 }
1352
1353 static int i8042_create_aux_port(int idx)
1354 {
1355         struct serio *serio;
1356         int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1357         struct i8042_port *port = &i8042_ports[port_no];
1358
1359         serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1360         if (!serio)
1361                 return -ENOMEM;
1362
1363         serio->id.type          = SERIO_8042;
1364         serio->write            = i8042_aux_write;
1365         serio->start            = i8042_start;
1366         serio->stop             = i8042_stop;
1367         serio->ps2_cmd_mutex    = &i8042_mutex;
1368         serio->port_data        = port;
1369         serio->dev.parent       = &i8042_platform_device->dev;
1370         if (idx < 0) {
1371                 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1372                 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1373                 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1374                         sizeof(serio->firmware_id));
1375                 serio->close = i8042_port_close;
1376         } else {
1377                 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1378                 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1379                 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1380                         sizeof(serio->firmware_id));
1381         }
1382
1383         port->serio = serio;
1384         port->mux = idx;
1385         port->irq = I8042_AUX_IRQ;
1386
1387         return 0;
1388 }
1389
1390 static void i8042_free_kbd_port(void)
1391 {
1392         kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1393         i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1394 }
1395
1396 static void i8042_free_aux_ports(void)
1397 {
1398         int i;
1399
1400         for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1401                 kfree(i8042_ports[i].serio);
1402                 i8042_ports[i].serio = NULL;
1403         }
1404 }
1405
1406 static void i8042_register_ports(void)
1407 {
1408         int i;
1409
1410         for (i = 0; i < I8042_NUM_PORTS; i++) {
1411                 struct serio *serio = i8042_ports[i].serio;
1412
1413                 if (!serio)
1414                         continue;
1415
1416                 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1417                         serio->name,
1418                         (unsigned long) I8042_DATA_REG,
1419                         (unsigned long) I8042_COMMAND_REG,
1420                         i8042_ports[i].irq);
1421                 serio_register_port(serio);
1422         }
1423 }
1424
1425 static void i8042_unregister_ports(void)
1426 {
1427         int i;
1428
1429         for (i = 0; i < I8042_NUM_PORTS; i++) {
1430                 if (i8042_ports[i].serio) {
1431                         serio_unregister_port(i8042_ports[i].serio);
1432                         i8042_ports[i].serio = NULL;
1433                 }
1434         }
1435 }
1436
1437 static void i8042_free_irqs(void)
1438 {
1439         if (i8042_aux_irq_registered)
1440                 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1441         if (i8042_kbd_irq_registered)
1442                 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1443
1444         i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
1445 }
1446
1447 static int i8042_setup_aux(void)
1448 {
1449         int (*aux_enable)(void);
1450         int error;
1451         int i;
1452
1453         if (i8042_check_aux())
1454                 return -ENODEV;
1455
1456         if (i8042_nomux || i8042_check_mux()) {
1457                 error = i8042_create_aux_port(-1);
1458                 if (error)
1459                         goto err_free_ports;
1460                 aux_enable = i8042_enable_aux_port;
1461         } else {
1462                 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1463                         error = i8042_create_aux_port(i);
1464                         if (error)
1465                                 goto err_free_ports;
1466                 }
1467                 aux_enable = i8042_enable_mux_ports;
1468         }
1469
1470         error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1471                             "i8042", i8042_platform_device);
1472         if (error)
1473                 goto err_free_ports;
1474
1475         error = aux_enable();
1476         if (error)
1477                 goto err_free_irq;
1478
1479         i8042_aux_irq_registered = true;
1480         return 0;
1481
1482  err_free_irq:
1483         free_irq(I8042_AUX_IRQ, i8042_platform_device);
1484  err_free_ports:
1485         i8042_free_aux_ports();
1486         return error;
1487 }
1488
1489 static int i8042_setup_kbd(void)
1490 {
1491         int error;
1492
1493         error = i8042_create_kbd_port();
1494         if (error)
1495                 return error;
1496
1497         error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1498                             "i8042", i8042_platform_device);
1499         if (error)
1500                 goto err_free_port;
1501
1502         error = i8042_enable_kbd_port();
1503         if (error)
1504                 goto err_free_irq;
1505
1506         i8042_kbd_irq_registered = true;
1507         return 0;
1508
1509  err_free_irq:
1510         free_irq(I8042_KBD_IRQ, i8042_platform_device);
1511  err_free_port:
1512         i8042_free_kbd_port();
1513         return error;
1514 }
1515
1516 static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1517                                    unsigned long action, void *data)
1518 {
1519         struct device *dev = data;
1520         struct serio *serio = to_serio_port(dev);
1521         struct i8042_port *port = serio->port_data;
1522
1523         if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1524                 return 0;
1525
1526         switch (action) {
1527         case BUS_NOTIFY_BOUND_DRIVER:
1528                 port->driver_bound = true;
1529                 break;
1530
1531         case BUS_NOTIFY_UNBIND_DRIVER:
1532                 port->driver_bound = false;
1533                 break;
1534         }
1535
1536         return 0;
1537 }
1538
1539 static int i8042_probe(struct platform_device *dev)
1540 {
1541         int error;
1542
1543         i8042_platform_device = dev;
1544
1545         if (i8042_reset == I8042_RESET_ALWAYS) {
1546                 error = i8042_controller_selftest();
1547                 if (error)
1548                         return error;
1549         }
1550
1551         error = i8042_controller_init();
1552         if (error)
1553                 return error;
1554
1555 #ifdef CONFIG_X86
1556         if (i8042_dritek)
1557                 i8042_dritek_enable();
1558 #endif
1559
1560         if (!i8042_noaux) {
1561                 error = i8042_setup_aux();
1562                 if (error && error != -ENODEV && error != -EBUSY)
1563                         goto out_fail;
1564         }
1565
1566         if (!i8042_nokbd) {
1567                 error = i8042_setup_kbd();
1568                 if (error)
1569                         goto out_fail;
1570         }
1571 /*
1572  * Ok, everything is ready, let's register all serio ports
1573  */
1574         i8042_register_ports();
1575
1576         return 0;
1577
1578  out_fail:
1579         i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1580         i8042_free_irqs();
1581         i8042_controller_reset(false);
1582         i8042_platform_device = NULL;
1583
1584         return error;
1585 }
1586
1587 static int i8042_remove(struct platform_device *dev)
1588 {
1589         i8042_unregister_ports();
1590         i8042_free_irqs();
1591         i8042_controller_reset(false);
1592         i8042_platform_device = NULL;
1593
1594         return 0;
1595 }
1596
1597 static struct platform_driver i8042_driver = {
1598         .driver         = {
1599                 .name   = "i8042",
1600 #ifdef CONFIG_PM
1601                 .pm     = &i8042_pm_ops,
1602 #endif
1603         },
1604         .probe          = i8042_probe,
1605         .remove         = i8042_remove,
1606         .shutdown       = i8042_shutdown,
1607 };
1608
1609 static struct notifier_block i8042_kbd_bind_notifier_block = {
1610         .notifier_call = i8042_kbd_bind_notifier,
1611 };
1612
1613 static int __init i8042_init(void)
1614 {
1615         int err;
1616
1617         dbg_init();
1618
1619         err = i8042_platform_init();
1620         if (err)
1621                 return (err == -ENODEV) ? 0 : err;
1622
1623         err = i8042_controller_check();
1624         if (err)
1625                 goto err_platform_exit;
1626
1627         /* Set this before creating the dev to allow i8042_command to work right away */
1628         i8042_present = true;
1629
1630         err = platform_driver_register(&i8042_driver);
1631         if (err)
1632                 goto err_platform_exit;
1633
1634         i8042_platform_device = platform_device_alloc("i8042", -1);
1635         if (!i8042_platform_device) {
1636                 err = -ENOMEM;
1637                 goto err_unregister_driver;
1638         }
1639
1640         err = platform_device_add(i8042_platform_device);
1641         if (err)
1642                 goto err_free_device;
1643
1644         bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1645         panic_blink = i8042_panic_blink;
1646
1647         return 0;
1648
1649 err_free_device:
1650         platform_device_put(i8042_platform_device);
1651 err_unregister_driver:
1652         platform_driver_unregister(&i8042_driver);
1653  err_platform_exit:
1654         i8042_platform_exit();
1655         return err;
1656 }
1657
1658 static void __exit i8042_exit(void)
1659 {
1660         if (!i8042_present)
1661                 return;
1662
1663         platform_device_unregister(i8042_platform_device);
1664         platform_driver_unregister(&i8042_driver);
1665         i8042_platform_exit();
1666
1667         bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1668         panic_blink = NULL;
1669 }
1670
1671 module_init(i8042_init);
1672 module_exit(i8042_exit);