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