Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  i8042 keyboard and mouse controller driver for Linux
0004  *
0005  *  Copyright (c) 1999-2004 Vojtech Pavlik
0006  */
0007 
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <linux/types.h>
0012 #include <linux/delay.h>
0013 #include <linux/module.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/ioport.h>
0016 #include <linux/init.h>
0017 #include <linux/serio.h>
0018 #include <linux/err.h>
0019 #include <linux/rcupdate.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/i8042.h>
0022 #include <linux/slab.h>
0023 #include <linux/suspend.h>
0024 #include <linux/property.h>
0025 
0026 #include <asm/io.h>
0027 
0028 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
0029 MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
0030 MODULE_LICENSE("GPL");
0031 
0032 static bool i8042_nokbd;
0033 module_param_named(nokbd, i8042_nokbd, bool, 0);
0034 MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
0035 
0036 static bool i8042_noaux;
0037 module_param_named(noaux, i8042_noaux, bool, 0);
0038 MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
0039 
0040 static bool i8042_nomux;
0041 module_param_named(nomux, i8042_nomux, bool, 0);
0042 MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
0043 
0044 static bool i8042_unlock;
0045 module_param_named(unlock, i8042_unlock, bool, 0);
0046 MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
0047 
0048 static bool i8042_probe_defer;
0049 module_param_named(probe_defer, i8042_probe_defer, bool, 0);
0050 MODULE_PARM_DESC(probe_defer, "Allow deferred probing.");
0051 
0052 enum i8042_controller_reset_mode {
0053     I8042_RESET_NEVER,
0054     I8042_RESET_ALWAYS,
0055     I8042_RESET_ON_S2RAM,
0056 #define I8042_RESET_DEFAULT I8042_RESET_ON_S2RAM
0057 };
0058 static enum i8042_controller_reset_mode i8042_reset = I8042_RESET_DEFAULT;
0059 static int i8042_set_reset(const char *val, const struct kernel_param *kp)
0060 {
0061     enum i8042_controller_reset_mode *arg = kp->arg;
0062     int error;
0063     bool reset;
0064 
0065     if (val) {
0066         error = kstrtobool(val, &reset);
0067         if (error)
0068             return error;
0069     } else {
0070         reset = true;
0071     }
0072 
0073     *arg = reset ? I8042_RESET_ALWAYS : I8042_RESET_NEVER;
0074     return 0;
0075 }
0076 
0077 static const struct kernel_param_ops param_ops_reset_param = {
0078     .flags = KERNEL_PARAM_OPS_FL_NOARG,
0079     .set = i8042_set_reset,
0080 };
0081 #define param_check_reset_param(name, p)    \
0082     __param_check(name, p, enum i8042_controller_reset_mode)
0083 module_param_named(reset, i8042_reset, reset_param, 0);
0084 MODULE_PARM_DESC(reset, "Reset controller on resume, cleanup or both");
0085 
0086 static bool i8042_direct;
0087 module_param_named(direct, i8042_direct, bool, 0);
0088 MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
0089 
0090 static bool i8042_dumbkbd;
0091 module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
0092 MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
0093 
0094 static bool i8042_noloop;
0095 module_param_named(noloop, i8042_noloop, bool, 0);
0096 MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
0097 
0098 static bool i8042_notimeout;
0099 module_param_named(notimeout, i8042_notimeout, bool, 0);
0100 MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
0101 
0102 static bool i8042_kbdreset;
0103 module_param_named(kbdreset, i8042_kbdreset, bool, 0);
0104 MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
0105 
0106 #ifdef CONFIG_X86
0107 static bool i8042_dritek;
0108 module_param_named(dritek, i8042_dritek, bool, 0);
0109 MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
0110 #endif
0111 
0112 #ifdef CONFIG_PNP
0113 static bool i8042_nopnp;
0114 module_param_named(nopnp, i8042_nopnp, bool, 0);
0115 MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
0116 #endif
0117 
0118 #define DEBUG
0119 #ifdef DEBUG
0120 static bool i8042_debug;
0121 module_param_named(debug, i8042_debug, bool, 0600);
0122 MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
0123 
0124 static bool i8042_unmask_kbd_data;
0125 module_param_named(unmask_kbd_data, i8042_unmask_kbd_data, bool, 0600);
0126 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]");
0127 #endif
0128 
0129 static bool i8042_present;
0130 static bool i8042_bypass_aux_irq_test;
0131 static char i8042_kbd_firmware_id[128];
0132 static char i8042_aux_firmware_id[128];
0133 static struct fwnode_handle *i8042_kbd_fwnode;
0134 
0135 #include "i8042.h"
0136 
0137 /*
0138  * i8042_lock protects serialization between i8042_command and
0139  * the interrupt handler.
0140  */
0141 static DEFINE_SPINLOCK(i8042_lock);
0142 
0143 /*
0144  * Writers to AUX and KBD ports as well as users issuing i8042_command
0145  * directly should acquire i8042_mutex (by means of calling
0146  * i8042_lock_chip() and i8042_unlock_chip() helpers) to ensure that
0147  * they do not disturb each other (unfortunately in many i8042
0148  * implementations write to one of the ports will immediately abort
0149  * command that is being processed by another port).
0150  */
0151 static DEFINE_MUTEX(i8042_mutex);
0152 
0153 struct i8042_port {
0154     struct serio *serio;
0155     int irq;
0156     bool exists;
0157     bool driver_bound;
0158     signed char mux;
0159 };
0160 
0161 #define I8042_KBD_PORT_NO   0
0162 #define I8042_AUX_PORT_NO   1
0163 #define I8042_MUX_PORT_NO   2
0164 #define I8042_NUM_PORTS     (I8042_NUM_MUX_PORTS + 2)
0165 
0166 static struct i8042_port i8042_ports[I8042_NUM_PORTS];
0167 
0168 static unsigned char i8042_initial_ctr;
0169 static unsigned char i8042_ctr;
0170 static bool i8042_mux_present;
0171 static bool i8042_kbd_irq_registered;
0172 static bool i8042_aux_irq_registered;
0173 static unsigned char i8042_suppress_kbd_ack;
0174 static struct platform_device *i8042_platform_device;
0175 static struct notifier_block i8042_kbd_bind_notifier_block;
0176 
0177 static irqreturn_t i8042_interrupt(int irq, void *dev_id);
0178 static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
0179                      struct serio *serio);
0180 
0181 void i8042_lock_chip(void)
0182 {
0183     mutex_lock(&i8042_mutex);
0184 }
0185 EXPORT_SYMBOL(i8042_lock_chip);
0186 
0187 void i8042_unlock_chip(void)
0188 {
0189     mutex_unlock(&i8042_mutex);
0190 }
0191 EXPORT_SYMBOL(i8042_unlock_chip);
0192 
0193 int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
0194                     struct serio *serio))
0195 {
0196     unsigned long flags;
0197     int ret = 0;
0198 
0199     spin_lock_irqsave(&i8042_lock, flags);
0200 
0201     if (i8042_platform_filter) {
0202         ret = -EBUSY;
0203         goto out;
0204     }
0205 
0206     i8042_platform_filter = filter;
0207 
0208 out:
0209     spin_unlock_irqrestore(&i8042_lock, flags);
0210     return ret;
0211 }
0212 EXPORT_SYMBOL(i8042_install_filter);
0213 
0214 int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
0215                        struct serio *port))
0216 {
0217     unsigned long flags;
0218     int ret = 0;
0219 
0220     spin_lock_irqsave(&i8042_lock, flags);
0221 
0222     if (i8042_platform_filter != filter) {
0223         ret = -EINVAL;
0224         goto out;
0225     }
0226 
0227     i8042_platform_filter = NULL;
0228 
0229 out:
0230     spin_unlock_irqrestore(&i8042_lock, flags);
0231     return ret;
0232 }
0233 EXPORT_SYMBOL(i8042_remove_filter);
0234 
0235 /*
0236  * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
0237  * be ready for reading values from it / writing values to it.
0238  * Called always with i8042_lock held.
0239  */
0240 
0241 static int i8042_wait_read(void)
0242 {
0243     int i = 0;
0244 
0245     while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
0246         udelay(50);
0247         i++;
0248     }
0249     return -(i == I8042_CTL_TIMEOUT);
0250 }
0251 
0252 static int i8042_wait_write(void)
0253 {
0254     int i = 0;
0255 
0256     while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
0257         udelay(50);
0258         i++;
0259     }
0260     return -(i == I8042_CTL_TIMEOUT);
0261 }
0262 
0263 /*
0264  * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
0265  * of the i8042 down the toilet.
0266  */
0267 
0268 static int i8042_flush(void)
0269 {
0270     unsigned long flags;
0271     unsigned char data, str;
0272     int count = 0;
0273     int retval = 0;
0274 
0275     spin_lock_irqsave(&i8042_lock, flags);
0276 
0277     while ((str = i8042_read_status()) & I8042_STR_OBF) {
0278         if (count++ < I8042_BUFFER_SIZE) {
0279             udelay(50);
0280             data = i8042_read_data();
0281             dbg("%02x <- i8042 (flush, %s)\n",
0282                 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
0283         } else {
0284             retval = -EIO;
0285             break;
0286         }
0287     }
0288 
0289     spin_unlock_irqrestore(&i8042_lock, flags);
0290 
0291     return retval;
0292 }
0293 
0294 /*
0295  * i8042_command() executes a command on the i8042. It also sends the input
0296  * parameter(s) of the commands to it, and receives the output value(s). The
0297  * parameters are to be stored in the param array, and the output is placed
0298  * into the same array. The number of the parameters and output values is
0299  * encoded in bits 8-11 of the command number.
0300  */
0301 
0302 static int __i8042_command(unsigned char *param, int command)
0303 {
0304     int i, error;
0305 
0306     if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
0307         return -1;
0308 
0309     error = i8042_wait_write();
0310     if (error)
0311         return error;
0312 
0313     dbg("%02x -> i8042 (command)\n", command & 0xff);
0314     i8042_write_command(command & 0xff);
0315 
0316     for (i = 0; i < ((command >> 12) & 0xf); i++) {
0317         error = i8042_wait_write();
0318         if (error) {
0319             dbg("     -- i8042 (wait write timeout)\n");
0320             return error;
0321         }
0322         dbg("%02x -> i8042 (parameter)\n", param[i]);
0323         i8042_write_data(param[i]);
0324     }
0325 
0326     for (i = 0; i < ((command >> 8) & 0xf); i++) {
0327         error = i8042_wait_read();
0328         if (error) {
0329             dbg("     -- i8042 (wait read timeout)\n");
0330             return error;
0331         }
0332 
0333         if (command == I8042_CMD_AUX_LOOP &&
0334             !(i8042_read_status() & I8042_STR_AUXDATA)) {
0335             dbg("     -- i8042 (auxerr)\n");
0336             return -1;
0337         }
0338 
0339         param[i] = i8042_read_data();
0340         dbg("%02x <- i8042 (return)\n", param[i]);
0341     }
0342 
0343     return 0;
0344 }
0345 
0346 int i8042_command(unsigned char *param, int command)
0347 {
0348     unsigned long flags;
0349     int retval;
0350 
0351     if (!i8042_present)
0352         return -1;
0353 
0354     spin_lock_irqsave(&i8042_lock, flags);
0355     retval = __i8042_command(param, command);
0356     spin_unlock_irqrestore(&i8042_lock, flags);
0357 
0358     return retval;
0359 }
0360 EXPORT_SYMBOL(i8042_command);
0361 
0362 /*
0363  * i8042_kbd_write() sends a byte out through the keyboard interface.
0364  */
0365 
0366 static int i8042_kbd_write(struct serio *port, unsigned char c)
0367 {
0368     unsigned long flags;
0369     int retval = 0;
0370 
0371     spin_lock_irqsave(&i8042_lock, flags);
0372 
0373     if (!(retval = i8042_wait_write())) {
0374         dbg("%02x -> i8042 (kbd-data)\n", c);
0375         i8042_write_data(c);
0376     }
0377 
0378     spin_unlock_irqrestore(&i8042_lock, flags);
0379 
0380     return retval;
0381 }
0382 
0383 /*
0384  * i8042_aux_write() sends a byte out through the aux interface.
0385  */
0386 
0387 static int i8042_aux_write(struct serio *serio, unsigned char c)
0388 {
0389     struct i8042_port *port = serio->port_data;
0390 
0391     return i8042_command(&c, port->mux == -1 ?
0392                     I8042_CMD_AUX_SEND :
0393                     I8042_CMD_MUX_SEND + port->mux);
0394 }
0395 
0396 
0397 /*
0398  * i8042_port_close attempts to clear AUX or KBD port state by disabling
0399  * and then re-enabling it.
0400  */
0401 
0402 static void i8042_port_close(struct serio *serio)
0403 {
0404     int irq_bit;
0405     int disable_bit;
0406     const char *port_name;
0407 
0408     if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
0409         irq_bit = I8042_CTR_AUXINT;
0410         disable_bit = I8042_CTR_AUXDIS;
0411         port_name = "AUX";
0412     } else {
0413         irq_bit = I8042_CTR_KBDINT;
0414         disable_bit = I8042_CTR_KBDDIS;
0415         port_name = "KBD";
0416     }
0417 
0418     i8042_ctr &= ~irq_bit;
0419     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
0420         pr_warn("Can't write CTR while closing %s port\n", port_name);
0421 
0422     udelay(50);
0423 
0424     i8042_ctr &= ~disable_bit;
0425     i8042_ctr |= irq_bit;
0426     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
0427         pr_err("Can't reactivate %s port\n", port_name);
0428 
0429     /*
0430      * See if there is any data appeared while we were messing with
0431      * port state.
0432      */
0433     i8042_interrupt(0, NULL);
0434 }
0435 
0436 /*
0437  * i8042_start() is called by serio core when port is about to finish
0438  * registering. It will mark port as existing so i8042_interrupt can
0439  * start sending data through it.
0440  */
0441 static int i8042_start(struct serio *serio)
0442 {
0443     struct i8042_port *port = serio->port_data;
0444 
0445     device_set_wakeup_capable(&serio->dev, true);
0446 
0447     /*
0448      * On platforms using suspend-to-idle, allow the keyboard to
0449      * wake up the system from sleep by enabling keyboard wakeups
0450      * by default.  This is consistent with keyboard wakeup
0451      * behavior on many platforms using suspend-to-RAM (ACPI S3)
0452      * by default.
0453      */
0454     if (pm_suspend_default_s2idle() &&
0455         serio == i8042_ports[I8042_KBD_PORT_NO].serio) {
0456         device_set_wakeup_enable(&serio->dev, true);
0457     }
0458 
0459     spin_lock_irq(&i8042_lock);
0460     port->exists = true;
0461     spin_unlock_irq(&i8042_lock);
0462 
0463     return 0;
0464 }
0465 
0466 /*
0467  * i8042_stop() marks serio port as non-existing so i8042_interrupt
0468  * will not try to send data to the port that is about to go away.
0469  * The function is called by serio core as part of unregister procedure.
0470  */
0471 static void i8042_stop(struct serio *serio)
0472 {
0473     struct i8042_port *port = serio->port_data;
0474 
0475     spin_lock_irq(&i8042_lock);
0476     port->exists = false;
0477     port->serio = NULL;
0478     spin_unlock_irq(&i8042_lock);
0479 
0480     /*
0481      * We need to make sure that interrupt handler finishes using
0482      * our serio port before we return from this function.
0483      * We synchronize with both AUX and KBD IRQs because there is
0484      * a (very unlikely) chance that AUX IRQ is raised for KBD port
0485      * and vice versa.
0486      */
0487     synchronize_irq(I8042_AUX_IRQ);
0488     synchronize_irq(I8042_KBD_IRQ);
0489 }
0490 
0491 /*
0492  * i8042_filter() filters out unwanted bytes from the input data stream.
0493  * It is called from i8042_interrupt and thus is running with interrupts
0494  * off and i8042_lock held.
0495  */
0496 static bool i8042_filter(unsigned char data, unsigned char str,
0497              struct serio *serio)
0498 {
0499     if (unlikely(i8042_suppress_kbd_ack)) {
0500         if ((~str & I8042_STR_AUXDATA) &&
0501             (data == 0xfa || data == 0xfe)) {
0502             i8042_suppress_kbd_ack--;
0503             dbg("Extra keyboard ACK - filtered out\n");
0504             return true;
0505         }
0506     }
0507 
0508     if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
0509         dbg("Filtered out by platform filter\n");
0510         return true;
0511     }
0512 
0513     return false;
0514 }
0515 
0516 /*
0517  * i8042_interrupt() is the most important function in this driver -
0518  * it handles the interrupts from the i8042, and sends incoming bytes
0519  * to the upper layers.
0520  */
0521 
0522 static irqreturn_t i8042_interrupt(int irq, void *dev_id)
0523 {
0524     struct i8042_port *port;
0525     struct serio *serio;
0526     unsigned long flags;
0527     unsigned char str, data;
0528     unsigned int dfl;
0529     unsigned int port_no;
0530     bool filtered;
0531     int ret = 1;
0532 
0533     spin_lock_irqsave(&i8042_lock, flags);
0534 
0535     str = i8042_read_status();
0536     if (unlikely(~str & I8042_STR_OBF)) {
0537         spin_unlock_irqrestore(&i8042_lock, flags);
0538         if (irq)
0539             dbg("Interrupt %d, without any data\n", irq);
0540         ret = 0;
0541         goto out;
0542     }
0543 
0544     data = i8042_read_data();
0545 
0546     if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
0547         static unsigned long last_transmit;
0548         static unsigned char last_str;
0549 
0550         dfl = 0;
0551         if (str & I8042_STR_MUXERR) {
0552             dbg("MUX error, status is %02x, data is %02x\n",
0553                 str, data);
0554 /*
0555  * When MUXERR condition is signalled the data register can only contain
0556  * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
0557  * it is not always the case. Some KBCs also report 0xfc when there is
0558  * nothing connected to the port while others sometimes get confused which
0559  * port the data came from and signal error leaving the data intact. They
0560  * _do not_ revert to legacy mode (actually I've never seen KBC reverting
0561  * to legacy mode yet, when we see one we'll add proper handling).
0562  * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
0563  * rest assume that the data came from the same serio last byte
0564  * was transmitted (if transmission happened not too long ago).
0565  */
0566 
0567             switch (data) {
0568                 default:
0569                     if (time_before(jiffies, last_transmit + HZ/10)) {
0570                         str = last_str;
0571                         break;
0572                     }
0573                     fallthrough;    /* report timeout */
0574                 case 0xfc:
0575                 case 0xfd:
0576                 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
0577                 case 0xff: dfl = SERIO_PARITY;  data = 0xfe; break;
0578             }
0579         }
0580 
0581         port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
0582         last_str = str;
0583         last_transmit = jiffies;
0584     } else {
0585 
0586         dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
0587               ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
0588 
0589         port_no = (str & I8042_STR_AUXDATA) ?
0590                 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
0591     }
0592 
0593     port = &i8042_ports[port_no];
0594     serio = port->exists ? port->serio : NULL;
0595 
0596     filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
0597            port_no, irq,
0598            dfl & SERIO_PARITY ? ", bad parity" : "",
0599            dfl & SERIO_TIMEOUT ? ", timeout" : "");
0600 
0601     filtered = i8042_filter(data, str, serio);
0602 
0603     spin_unlock_irqrestore(&i8042_lock, flags);
0604 
0605     if (likely(serio && !filtered))
0606         serio_interrupt(serio, data, dfl);
0607 
0608  out:
0609     return IRQ_RETVAL(ret);
0610 }
0611 
0612 /*
0613  * i8042_enable_kbd_port enables keyboard port on chip
0614  */
0615 
0616 static int i8042_enable_kbd_port(void)
0617 {
0618     i8042_ctr &= ~I8042_CTR_KBDDIS;
0619     i8042_ctr |= I8042_CTR_KBDINT;
0620 
0621     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
0622         i8042_ctr &= ~I8042_CTR_KBDINT;
0623         i8042_ctr |= I8042_CTR_KBDDIS;
0624         pr_err("Failed to enable KBD port\n");
0625         return -EIO;
0626     }
0627 
0628     return 0;
0629 }
0630 
0631 /*
0632  * i8042_enable_aux_port enables AUX (mouse) port on chip
0633  */
0634 
0635 static int i8042_enable_aux_port(void)
0636 {
0637     i8042_ctr &= ~I8042_CTR_AUXDIS;
0638     i8042_ctr |= I8042_CTR_AUXINT;
0639 
0640     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
0641         i8042_ctr &= ~I8042_CTR_AUXINT;
0642         i8042_ctr |= I8042_CTR_AUXDIS;
0643         pr_err("Failed to enable AUX port\n");
0644         return -EIO;
0645     }
0646 
0647     return 0;
0648 }
0649 
0650 /*
0651  * i8042_enable_mux_ports enables 4 individual AUX ports after
0652  * the controller has been switched into Multiplexed mode
0653  */
0654 
0655 static int i8042_enable_mux_ports(void)
0656 {
0657     unsigned char param;
0658     int i;
0659 
0660     for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
0661         i8042_command(&param, I8042_CMD_MUX_PFX + i);
0662         i8042_command(&param, I8042_CMD_AUX_ENABLE);
0663     }
0664 
0665     return i8042_enable_aux_port();
0666 }
0667 
0668 /*
0669  * i8042_set_mux_mode checks whether the controller has an
0670  * active multiplexor and puts the chip into Multiplexed (true)
0671  * or Legacy (false) mode.
0672  */
0673 
0674 static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
0675 {
0676 
0677     unsigned char param, val;
0678 /*
0679  * Get rid of bytes in the queue.
0680  */
0681 
0682     i8042_flush();
0683 
0684 /*
0685  * Internal loopback test - send three bytes, they should come back from the
0686  * mouse interface, the last should be version.
0687  */
0688 
0689     param = val = 0xf0;
0690     if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
0691         return -1;
0692     param = val = multiplex ? 0x56 : 0xf6;
0693     if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
0694         return -1;
0695     param = val = multiplex ? 0xa4 : 0xa5;
0696     if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
0697         return -1;
0698 
0699 /*
0700  * Workaround for interference with USB Legacy emulation
0701  * that causes a v10.12 MUX to be found.
0702  */
0703     if (param == 0xac)
0704         return -1;
0705 
0706     if (mux_version)
0707         *mux_version = param;
0708 
0709     return 0;
0710 }
0711 
0712 /*
0713  * i8042_check_mux() checks whether the controller supports the PS/2 Active
0714  * Multiplexing specification by Synaptics, Phoenix, Insyde and
0715  * LCS/Telegraphics.
0716  */
0717 
0718 static int i8042_check_mux(void)
0719 {
0720     unsigned char mux_version;
0721 
0722     if (i8042_set_mux_mode(true, &mux_version))
0723         return -1;
0724 
0725     pr_info("Detected active multiplexing controller, rev %d.%d\n",
0726         (mux_version >> 4) & 0xf, mux_version & 0xf);
0727 
0728 /*
0729  * Disable all muxed ports by disabling AUX.
0730  */
0731     i8042_ctr |= I8042_CTR_AUXDIS;
0732     i8042_ctr &= ~I8042_CTR_AUXINT;
0733 
0734     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
0735         pr_err("Failed to disable AUX port, can't use MUX\n");
0736         return -EIO;
0737     }
0738 
0739     i8042_mux_present = true;
0740 
0741     return 0;
0742 }
0743 
0744 /*
0745  * The following is used to test AUX IRQ delivery.
0746  */
0747 static struct completion i8042_aux_irq_delivered;
0748 static bool i8042_irq_being_tested;
0749 
0750 static irqreturn_t i8042_aux_test_irq(int irq, void *dev_id)
0751 {
0752     unsigned long flags;
0753     unsigned char str, data;
0754     int ret = 0;
0755 
0756     spin_lock_irqsave(&i8042_lock, flags);
0757     str = i8042_read_status();
0758     if (str & I8042_STR_OBF) {
0759         data = i8042_read_data();
0760         dbg("%02x <- i8042 (aux_test_irq, %s)\n",
0761             data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
0762         if (i8042_irq_being_tested &&
0763             data == 0xa5 && (str & I8042_STR_AUXDATA))
0764             complete(&i8042_aux_irq_delivered);
0765         ret = 1;
0766     }
0767     spin_unlock_irqrestore(&i8042_lock, flags);
0768 
0769     return IRQ_RETVAL(ret);
0770 }
0771 
0772 /*
0773  * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
0774  * verifies success by readinng CTR. Used when testing for presence of AUX
0775  * port.
0776  */
0777 static int i8042_toggle_aux(bool on)
0778 {
0779     unsigned char param;
0780     int i;
0781 
0782     if (i8042_command(&param,
0783             on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
0784         return -1;
0785 
0786     /* some chips need some time to set the I8042_CTR_AUXDIS bit */
0787     for (i = 0; i < 100; i++) {
0788         udelay(50);
0789 
0790         if (i8042_command(&param, I8042_CMD_CTL_RCTR))
0791             return -1;
0792 
0793         if (!(param & I8042_CTR_AUXDIS) == on)
0794             return 0;
0795     }
0796 
0797     return -1;
0798 }
0799 
0800 /*
0801  * i8042_check_aux() applies as much paranoia as it can at detecting
0802  * the presence of an AUX interface.
0803  */
0804 
0805 static int i8042_check_aux(void)
0806 {
0807     int retval = -1;
0808     bool irq_registered = false;
0809     bool aux_loop_broken = false;
0810     unsigned long flags;
0811     unsigned char param;
0812 
0813 /*
0814  * Get rid of bytes in the queue.
0815  */
0816 
0817     i8042_flush();
0818 
0819 /*
0820  * Internal loopback test - filters out AT-type i8042's. Unfortunately
0821  * SiS screwed up and their 5597 doesn't support the LOOP command even
0822  * though it has an AUX port.
0823  */
0824 
0825     param = 0x5a;
0826     retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
0827     if (retval || param != 0x5a) {
0828 
0829 /*
0830  * External connection test - filters out AT-soldered PS/2 i8042's
0831  * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
0832  * 0xfa - no error on some notebooks which ignore the spec
0833  * Because it's common for chipsets to return error on perfectly functioning
0834  * AUX ports, we test for this only when the LOOP command failed.
0835  */
0836 
0837         if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
0838             (param && param != 0xfa && param != 0xff))
0839             return -1;
0840 
0841 /*
0842  * If AUX_LOOP completed without error but returned unexpected data
0843  * mark it as broken
0844  */
0845         if (!retval)
0846             aux_loop_broken = true;
0847     }
0848 
0849 /*
0850  * Bit assignment test - filters out PS/2 i8042's in AT mode
0851  */
0852 
0853     if (i8042_toggle_aux(false)) {
0854         pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
0855         pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
0856     }
0857 
0858     if (i8042_toggle_aux(true))
0859         return -1;
0860 
0861 /*
0862  * Reset keyboard (needed on some laptops to successfully detect
0863  * touchpad, e.g., some Gigabyte laptop models with Elantech
0864  * touchpads).
0865  */
0866     if (i8042_kbdreset) {
0867         pr_warn("Attempting to reset device connected to KBD port\n");
0868         i8042_kbd_write(NULL, (unsigned char) 0xff);
0869     }
0870 
0871 /*
0872  * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
0873  * used it for a PCI card or somethig else.
0874  */
0875 
0876     if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
0877 /*
0878  * Without LOOP command we can't test AUX IRQ delivery. Assume the port
0879  * is working and hope we are right.
0880  */
0881         retval = 0;
0882         goto out;
0883     }
0884 
0885     if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
0886             "i8042", i8042_platform_device))
0887         goto out;
0888 
0889     irq_registered = true;
0890 
0891     if (i8042_enable_aux_port())
0892         goto out;
0893 
0894     spin_lock_irqsave(&i8042_lock, flags);
0895 
0896     init_completion(&i8042_aux_irq_delivered);
0897     i8042_irq_being_tested = true;
0898 
0899     param = 0xa5;
0900     retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
0901 
0902     spin_unlock_irqrestore(&i8042_lock, flags);
0903 
0904     if (retval)
0905         goto out;
0906 
0907     if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
0908                     msecs_to_jiffies(250)) == 0) {
0909 /*
0910  * AUX IRQ was never delivered so we need to flush the controller to
0911  * get rid of the byte we put there; otherwise keyboard may not work.
0912  */
0913         dbg("     -- i8042 (aux irq test timeout)\n");
0914         i8042_flush();
0915         retval = -1;
0916     }
0917 
0918  out:
0919 
0920 /*
0921  * Disable the interface.
0922  */
0923 
0924     i8042_ctr |= I8042_CTR_AUXDIS;
0925     i8042_ctr &= ~I8042_CTR_AUXINT;
0926 
0927     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
0928         retval = -1;
0929 
0930     if (irq_registered)
0931         free_irq(I8042_AUX_IRQ, i8042_platform_device);
0932 
0933     return retval;
0934 }
0935 
0936 static int i8042_controller_check(void)
0937 {
0938     if (i8042_flush()) {
0939         pr_info("No controller found\n");
0940         return -ENODEV;
0941     }
0942 
0943     return 0;
0944 }
0945 
0946 static int i8042_controller_selftest(void)
0947 {
0948     unsigned char param;
0949     int i = 0;
0950 
0951     /*
0952      * We try this 5 times; on some really fragile systems this does not
0953      * take the first time...
0954      */
0955     do {
0956 
0957         if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
0958             pr_err("i8042 controller selftest timeout\n");
0959             return -ENODEV;
0960         }
0961 
0962         if (param == I8042_RET_CTL_TEST)
0963             return 0;
0964 
0965         dbg("i8042 controller selftest: %#x != %#x\n",
0966             param, I8042_RET_CTL_TEST);
0967         msleep(50);
0968     } while (i++ < 5);
0969 
0970 #ifdef CONFIG_X86
0971     /*
0972      * On x86, we don't fail entire i8042 initialization if controller
0973      * reset fails in hopes that keyboard port will still be functional
0974      * and user will still get a working keyboard. This is especially
0975      * important on netbooks. On other arches we trust hardware more.
0976      */
0977     pr_info("giving up on controller selftest, continuing anyway...\n");
0978     return 0;
0979 #else
0980     pr_err("i8042 controller selftest failed\n");
0981     return -EIO;
0982 #endif
0983 }
0984 
0985 /*
0986  * i8042_controller_init initializes the i8042 controller, and,
0987  * most importantly, sets it into non-xlated mode if that's
0988  * desired.
0989  */
0990 
0991 static int i8042_controller_init(void)
0992 {
0993     unsigned long flags;
0994     int n = 0;
0995     unsigned char ctr[2];
0996 
0997 /*
0998  * Save the CTR for restore on unload / reboot.
0999  */
1000 
1001     do {
1002         if (n >= 10) {
1003             pr_err("Unable to get stable CTR read\n");
1004             return -EIO;
1005         }
1006 
1007         if (n != 0)
1008             udelay(50);
1009 
1010         if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
1011             pr_err("Can't read CTR while initializing i8042\n");
1012             return i8042_probe_defer ? -EPROBE_DEFER : -EIO;
1013         }
1014 
1015     } while (n < 2 || ctr[0] != ctr[1]);
1016 
1017     i8042_initial_ctr = i8042_ctr = ctr[0];
1018 
1019 /*
1020  * Disable the keyboard interface and interrupt.
1021  */
1022 
1023     i8042_ctr |= I8042_CTR_KBDDIS;
1024     i8042_ctr &= ~I8042_CTR_KBDINT;
1025 
1026 /*
1027  * Handle keylock.
1028  */
1029 
1030     spin_lock_irqsave(&i8042_lock, flags);
1031     if (~i8042_read_status() & I8042_STR_KEYLOCK) {
1032         if (i8042_unlock)
1033             i8042_ctr |= I8042_CTR_IGNKEYLOCK;
1034         else
1035             pr_warn("Warning: Keylock active\n");
1036     }
1037     spin_unlock_irqrestore(&i8042_lock, flags);
1038 
1039 /*
1040  * If the chip is configured into nontranslated mode by the BIOS, don't
1041  * bother enabling translating and be happy.
1042  */
1043 
1044     if (~i8042_ctr & I8042_CTR_XLATE)
1045         i8042_direct = true;
1046 
1047 /*
1048  * Set nontranslated mode for the kbd interface if requested by an option.
1049  * After this the kbd interface becomes a simple serial in/out, like the aux
1050  * interface is. We don't do this by default, since it can confuse notebook
1051  * BIOSes.
1052  */
1053 
1054     if (i8042_direct)
1055         i8042_ctr &= ~I8042_CTR_XLATE;
1056 
1057 /*
1058  * Write CTR back.
1059  */
1060 
1061     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1062         pr_err("Can't write CTR while initializing i8042\n");
1063         return -EIO;
1064     }
1065 
1066 /*
1067  * Flush whatever accumulated while we were disabling keyboard port.
1068  */
1069 
1070     i8042_flush();
1071 
1072     return 0;
1073 }
1074 
1075 
1076 /*
1077  * Reset the controller and reset CRT to the original value set by BIOS.
1078  */
1079 
1080 static void i8042_controller_reset(bool s2r_wants_reset)
1081 {
1082     i8042_flush();
1083 
1084 /*
1085  * Disable both KBD and AUX interfaces so they don't get in the way
1086  */
1087 
1088     i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1089     i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1090 
1091     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
1092         pr_warn("Can't write CTR while resetting\n");
1093 
1094 /*
1095  * Disable MUX mode if present.
1096  */
1097 
1098     if (i8042_mux_present)
1099         i8042_set_mux_mode(false, NULL);
1100 
1101 /*
1102  * Reset the controller if requested.
1103  */
1104 
1105     if (i8042_reset == I8042_RESET_ALWAYS ||
1106         (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1107         i8042_controller_selftest();
1108     }
1109 
1110 /*
1111  * Restore the original control register setting.
1112  */
1113 
1114     if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
1115         pr_warn("Can't restore CTR\n");
1116 }
1117 
1118 
1119 /*
1120  * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1121  * when kernel panics. Flashing LEDs is useful for users running X who may
1122  * not see the console and will help distinguishing panics from "real"
1123  * lockups.
1124  *
1125  * Note that DELAY has a limit of 10ms so we will not get stuck here
1126  * waiting for KBC to free up even if KBD interrupt is off
1127  */
1128 
1129 #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1130 
1131 static long i8042_panic_blink(int state)
1132 {
1133     long delay = 0;
1134     char led;
1135 
1136     led = (state) ? 0x01 | 0x04 : 0;
1137     while (i8042_read_status() & I8042_STR_IBF)
1138         DELAY;
1139     dbg("%02x -> i8042 (panic blink)\n", 0xed);
1140     i8042_suppress_kbd_ack = 2;
1141     i8042_write_data(0xed); /* set leds */
1142     DELAY;
1143     while (i8042_read_status() & I8042_STR_IBF)
1144         DELAY;
1145     DELAY;
1146     dbg("%02x -> i8042 (panic blink)\n", led);
1147     i8042_write_data(led);
1148     DELAY;
1149     return delay;
1150 }
1151 
1152 #undef DELAY
1153 
1154 #ifdef CONFIG_X86
1155 static void i8042_dritek_enable(void)
1156 {
1157     unsigned char param = 0x90;
1158     int error;
1159 
1160     error = i8042_command(&param, 0x1059);
1161     if (error)
1162         pr_warn("Failed to enable DRITEK extension: %d\n", error);
1163 }
1164 #endif
1165 
1166 #ifdef CONFIG_PM
1167 
1168 /*
1169  * Here we try to reset everything back to a state we had
1170  * before suspending.
1171  */
1172 
1173 static int i8042_controller_resume(bool s2r_wants_reset)
1174 {
1175     int error;
1176 
1177     error = i8042_controller_check();
1178     if (error)
1179         return error;
1180 
1181     if (i8042_reset == I8042_RESET_ALWAYS ||
1182         (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
1183         error = i8042_controller_selftest();
1184         if (error)
1185             return error;
1186     }
1187 
1188 /*
1189  * Restore original CTR value and disable all ports
1190  */
1191 
1192     i8042_ctr = i8042_initial_ctr;
1193     if (i8042_direct)
1194         i8042_ctr &= ~I8042_CTR_XLATE;
1195     i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1196     i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
1197     if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1198         pr_warn("Can't write CTR to resume, retrying...\n");
1199         msleep(50);
1200         if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
1201             pr_err("CTR write retry failed\n");
1202             return -EIO;
1203         }
1204     }
1205 
1206 
1207 #ifdef CONFIG_X86
1208     if (i8042_dritek)
1209         i8042_dritek_enable();
1210 #endif
1211 
1212     if (i8042_mux_present) {
1213         if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
1214             pr_warn("failed to resume active multiplexor, mouse won't work\n");
1215     } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1216         i8042_enable_aux_port();
1217 
1218     if (i8042_ports[I8042_KBD_PORT_NO].serio)
1219         i8042_enable_kbd_port();
1220 
1221     i8042_interrupt(0, NULL);
1222 
1223     return 0;
1224 }
1225 
1226 /*
1227  * Here we try to restore the original BIOS settings to avoid
1228  * upsetting it.
1229  */
1230 
1231 static int i8042_pm_suspend(struct device *dev)
1232 {
1233     int i;
1234 
1235     if (pm_suspend_via_firmware())
1236         i8042_controller_reset(true);
1237 
1238     /* Set up serio interrupts for system wakeup. */
1239     for (i = 0; i < I8042_NUM_PORTS; i++) {
1240         struct serio *serio = i8042_ports[i].serio;
1241 
1242         if (serio && device_may_wakeup(&serio->dev))
1243             enable_irq_wake(i8042_ports[i].irq);
1244     }
1245 
1246     return 0;
1247 }
1248 
1249 static int i8042_pm_resume_noirq(struct device *dev)
1250 {
1251     if (!pm_resume_via_firmware())
1252         i8042_interrupt(0, NULL);
1253 
1254     return 0;
1255 }
1256 
1257 static int i8042_pm_resume(struct device *dev)
1258 {
1259     bool want_reset;
1260     int i;
1261 
1262     for (i = 0; i < I8042_NUM_PORTS; i++) {
1263         struct serio *serio = i8042_ports[i].serio;
1264 
1265         if (serio && device_may_wakeup(&serio->dev))
1266             disable_irq_wake(i8042_ports[i].irq);
1267     }
1268 
1269     /*
1270      * If platform firmware was not going to be involved in suspend, we did
1271      * not restore the controller state to whatever it had been at boot
1272      * time, so we do not need to do anything.
1273      */
1274     if (!pm_suspend_via_firmware())
1275         return 0;
1276 
1277     /*
1278      * We only need to reset the controller if we are resuming after handing
1279      * off control to the platform firmware, otherwise we can simply restore
1280      * the mode.
1281      */
1282     want_reset = pm_resume_via_firmware();
1283 
1284     return i8042_controller_resume(want_reset);
1285 }
1286 
1287 static int i8042_pm_thaw(struct device *dev)
1288 {
1289     i8042_interrupt(0, NULL);
1290 
1291     return 0;
1292 }
1293 
1294 static int i8042_pm_reset(struct device *dev)
1295 {
1296     i8042_controller_reset(false);
1297 
1298     return 0;
1299 }
1300 
1301 static int i8042_pm_restore(struct device *dev)
1302 {
1303     return i8042_controller_resume(false);
1304 }
1305 
1306 static const struct dev_pm_ops i8042_pm_ops = {
1307     .suspend    = i8042_pm_suspend,
1308     .resume_noirq   = i8042_pm_resume_noirq,
1309     .resume     = i8042_pm_resume,
1310     .thaw       = i8042_pm_thaw,
1311     .poweroff   = i8042_pm_reset,
1312     .restore    = i8042_pm_restore,
1313 };
1314 
1315 #endif /* CONFIG_PM */
1316 
1317 /*
1318  * We need to reset the 8042 back to original mode on system shutdown,
1319  * because otherwise BIOSes will be confused.
1320  */
1321 
1322 static void i8042_shutdown(struct platform_device *dev)
1323 {
1324     i8042_controller_reset(false);
1325 }
1326 
1327 static int i8042_create_kbd_port(void)
1328 {
1329     struct serio *serio;
1330     struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1331 
1332     serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1333     if (!serio)
1334         return -ENOMEM;
1335 
1336     serio->id.type      = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1337     serio->write        = i8042_dumbkbd ? NULL : i8042_kbd_write;
1338     serio->start        = i8042_start;
1339     serio->stop     = i8042_stop;
1340     serio->close        = i8042_port_close;
1341     serio->ps2_cmd_mutex    = &i8042_mutex;
1342     serio->port_data    = port;
1343     serio->dev.parent   = &i8042_platform_device->dev;
1344     strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
1345     strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1346     strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1347         sizeof(serio->firmware_id));
1348     set_primary_fwnode(&serio->dev, i8042_kbd_fwnode);
1349 
1350     port->serio = serio;
1351     port->irq = I8042_KBD_IRQ;
1352 
1353     return 0;
1354 }
1355 
1356 static int i8042_create_aux_port(int idx)
1357 {
1358     struct serio *serio;
1359     int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1360     struct i8042_port *port = &i8042_ports[port_no];
1361 
1362     serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
1363     if (!serio)
1364         return -ENOMEM;
1365 
1366     serio->id.type      = SERIO_8042;
1367     serio->write        = i8042_aux_write;
1368     serio->start        = i8042_start;
1369     serio->stop     = i8042_stop;
1370     serio->ps2_cmd_mutex    = &i8042_mutex;
1371     serio->port_data    = port;
1372     serio->dev.parent   = &i8042_platform_device->dev;
1373     if (idx < 0) {
1374         strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1375         strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1376         strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1377             sizeof(serio->firmware_id));
1378         serio->close = i8042_port_close;
1379     } else {
1380         snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1381         snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1382         strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1383             sizeof(serio->firmware_id));
1384     }
1385 
1386     port->serio = serio;
1387     port->mux = idx;
1388     port->irq = I8042_AUX_IRQ;
1389 
1390     return 0;
1391 }
1392 
1393 static void i8042_free_kbd_port(void)
1394 {
1395     kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1396     i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1397 }
1398 
1399 static void i8042_free_aux_ports(void)
1400 {
1401     int i;
1402 
1403     for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1404         kfree(i8042_ports[i].serio);
1405         i8042_ports[i].serio = NULL;
1406     }
1407 }
1408 
1409 static void i8042_register_ports(void)
1410 {
1411     int i;
1412 
1413     for (i = 0; i < I8042_NUM_PORTS; i++) {
1414         struct serio *serio = i8042_ports[i].serio;
1415 
1416         if (!serio)
1417             continue;
1418 
1419         printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1420             serio->name,
1421             (unsigned long) I8042_DATA_REG,
1422             (unsigned long) I8042_COMMAND_REG,
1423             i8042_ports[i].irq);
1424         serio_register_port(serio);
1425     }
1426 }
1427 
1428 static void i8042_unregister_ports(void)
1429 {
1430     int i;
1431 
1432     for (i = 0; i < I8042_NUM_PORTS; i++) {
1433         if (i8042_ports[i].serio) {
1434             serio_unregister_port(i8042_ports[i].serio);
1435             i8042_ports[i].serio = NULL;
1436         }
1437     }
1438 }
1439 
1440 static void i8042_free_irqs(void)
1441 {
1442     if (i8042_aux_irq_registered)
1443         free_irq(I8042_AUX_IRQ, i8042_platform_device);
1444     if (i8042_kbd_irq_registered)
1445         free_irq(I8042_KBD_IRQ, i8042_platform_device);
1446 
1447     i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
1448 }
1449 
1450 static int i8042_setup_aux(void)
1451 {
1452     int (*aux_enable)(void);
1453     int error;
1454     int i;
1455 
1456     if (i8042_check_aux())
1457         return -ENODEV;
1458 
1459     if (i8042_nomux || i8042_check_mux()) {
1460         error = i8042_create_aux_port(-1);
1461         if (error)
1462             goto err_free_ports;
1463         aux_enable = i8042_enable_aux_port;
1464     } else {
1465         for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1466             error = i8042_create_aux_port(i);
1467             if (error)
1468                 goto err_free_ports;
1469         }
1470         aux_enable = i8042_enable_mux_ports;
1471     }
1472 
1473     error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1474                 "i8042", i8042_platform_device);
1475     if (error)
1476         goto err_free_ports;
1477 
1478     error = aux_enable();
1479     if (error)
1480         goto err_free_irq;
1481 
1482     i8042_aux_irq_registered = true;
1483     return 0;
1484 
1485  err_free_irq:
1486     free_irq(I8042_AUX_IRQ, i8042_platform_device);
1487  err_free_ports:
1488     i8042_free_aux_ports();
1489     return error;
1490 }
1491 
1492 static int i8042_setup_kbd(void)
1493 {
1494     int error;
1495 
1496     error = i8042_create_kbd_port();
1497     if (error)
1498         return error;
1499 
1500     error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1501                 "i8042", i8042_platform_device);
1502     if (error)
1503         goto err_free_port;
1504 
1505     error = i8042_enable_kbd_port();
1506     if (error)
1507         goto err_free_irq;
1508 
1509     i8042_kbd_irq_registered = true;
1510     return 0;
1511 
1512  err_free_irq:
1513     free_irq(I8042_KBD_IRQ, i8042_platform_device);
1514  err_free_port:
1515     i8042_free_kbd_port();
1516     return error;
1517 }
1518 
1519 static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1520                    unsigned long action, void *data)
1521 {
1522     struct device *dev = data;
1523     struct serio *serio = to_serio_port(dev);
1524     struct i8042_port *port = serio->port_data;
1525 
1526     if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1527         return 0;
1528 
1529     switch (action) {
1530     case BUS_NOTIFY_BOUND_DRIVER:
1531         port->driver_bound = true;
1532         break;
1533 
1534     case BUS_NOTIFY_UNBIND_DRIVER:
1535         port->driver_bound = false;
1536         break;
1537     }
1538 
1539     return 0;
1540 }
1541 
1542 static int i8042_probe(struct platform_device *dev)
1543 {
1544     int error;
1545 
1546     i8042_platform_device = dev;
1547 
1548     if (i8042_reset == I8042_RESET_ALWAYS) {
1549         error = i8042_controller_selftest();
1550         if (error)
1551             return error;
1552     }
1553 
1554     error = i8042_controller_init();
1555     if (error)
1556         return error;
1557 
1558 #ifdef CONFIG_X86
1559     if (i8042_dritek)
1560         i8042_dritek_enable();
1561 #endif
1562 
1563     if (!i8042_noaux) {
1564         error = i8042_setup_aux();
1565         if (error && error != -ENODEV && error != -EBUSY)
1566             goto out_fail;
1567     }
1568 
1569     if (!i8042_nokbd) {
1570         error = i8042_setup_kbd();
1571         if (error)
1572             goto out_fail;
1573     }
1574 /*
1575  * Ok, everything is ready, let's register all serio ports
1576  */
1577     i8042_register_ports();
1578 
1579     return 0;
1580 
1581  out_fail:
1582     i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1583     i8042_free_irqs();
1584     i8042_controller_reset(false);
1585     i8042_platform_device = NULL;
1586 
1587     return error;
1588 }
1589 
1590 static int i8042_remove(struct platform_device *dev)
1591 {
1592     i8042_unregister_ports();
1593     i8042_free_irqs();
1594     i8042_controller_reset(false);
1595     i8042_platform_device = NULL;
1596 
1597     return 0;
1598 }
1599 
1600 static struct platform_driver i8042_driver = {
1601     .driver     = {
1602         .name   = "i8042",
1603 #ifdef CONFIG_PM
1604         .pm = &i8042_pm_ops,
1605 #endif
1606     },
1607     .probe      = i8042_probe,
1608     .remove     = i8042_remove,
1609     .shutdown   = i8042_shutdown,
1610 };
1611 
1612 static struct notifier_block i8042_kbd_bind_notifier_block = {
1613     .notifier_call = i8042_kbd_bind_notifier,
1614 };
1615 
1616 static int __init i8042_init(void)
1617 {
1618     int err;
1619 
1620     dbg_init();
1621 
1622     err = i8042_platform_init();
1623     if (err)
1624         return (err == -ENODEV) ? 0 : err;
1625 
1626     err = i8042_controller_check();
1627     if (err)
1628         goto err_platform_exit;
1629 
1630     /* Set this before creating the dev to allow i8042_command to work right away */
1631     i8042_present = true;
1632 
1633     err = platform_driver_register(&i8042_driver);
1634     if (err)
1635         goto err_platform_exit;
1636 
1637     i8042_platform_device = platform_device_alloc("i8042", -1);
1638     if (!i8042_platform_device) {
1639         err = -ENOMEM;
1640         goto err_unregister_driver;
1641     }
1642 
1643     err = platform_device_add(i8042_platform_device);
1644     if (err)
1645         goto err_free_device;
1646 
1647     bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1648     panic_blink = i8042_panic_blink;
1649 
1650     return 0;
1651 
1652 err_free_device:
1653     platform_device_put(i8042_platform_device);
1654 err_unregister_driver:
1655     platform_driver_unregister(&i8042_driver);
1656  err_platform_exit:
1657     i8042_platform_exit();
1658     return err;
1659 }
1660 
1661 static void __exit i8042_exit(void)
1662 {
1663     if (!i8042_present)
1664         return;
1665 
1666     platform_device_unregister(i8042_platform_device);
1667     platform_driver_unregister(&i8042_driver);
1668     i8042_platform_exit();
1669 
1670     bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
1671     panic_blink = NULL;
1672 }
1673 
1674 module_init(i8042_init);
1675 module_exit(i8042_exit);