blob: c84c685056b997ec325900dca1524828fa03b0b5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Joe Perches4eb3c302010-11-29 23:33:07 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070015#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/delay.h>
17#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
19#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/serio.h>
22#include <linux/err.h>
23#include <linux/rcupdate.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
Márton Németh553a05b2007-10-22 00:56:52 -040025#include <linux/i8042.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +020027#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/io.h>
30
31MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
32MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
33MODULE_LICENSE("GPL");
34
Dmitry Torokhov386b3842009-09-09 19:08:16 -070035static bool i8042_nokbd;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -050036module_param_named(nokbd, i8042_nokbd, bool, 0);
37MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
38
Dmitry Torokhov386b3842009-09-09 19:08:16 -070039static bool i8042_noaux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040module_param_named(noaux, i8042_noaux, bool, 0);
41MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
42
Dmitry Torokhove55a3362014-10-31 09:35:53 -070043static bool i8042_nomux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044module_param_named(nomux, i8042_nomux, bool, 0);
Dominik Brodowski2c860a12010-04-05 22:29:09 -070045MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Dmitry Torokhov386b3842009-09-09 19:08:16 -070047static bool i8042_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048module_param_named(unlock, i8042_unlock, bool, 0);
49MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
50
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -070051enum i8042_controller_reset_mode {
52 I8042_RESET_NEVER,
53 I8042_RESET_ALWAYS,
54 I8042_RESET_ON_S2RAM,
55#define I8042_RESET_DEFAULT I8042_RESET_ON_S2RAM
56};
57static enum i8042_controller_reset_mode i8042_reset = I8042_RESET_DEFAULT;
58static int i8042_set_reset(const char *val, const struct kernel_param *kp)
59{
60 enum i8042_controller_reset_mode *arg = kp->arg;
61 int error;
62 bool reset;
63
64 if (val) {
65 error = kstrtobool(val, &reset);
66 if (error)
67 return error;
68 } else {
69 reset = true;
70 }
71
72 *arg = reset ? I8042_RESET_ALWAYS : I8042_RESET_NEVER;
73 return 0;
74}
75
76static const struct kernel_param_ops param_ops_reset_param = {
77 .flags = KERNEL_PARAM_OPS_FL_NOARG,
78 .set = i8042_set_reset,
79};
80#define param_check_reset_param(name, p) \
81 __param_check(name, p, enum i8042_controller_reset_mode)
82module_param_named(reset, i8042_reset, reset_param, 0);
83MODULE_PARM_DESC(reset, "Reset controller on resume, cleanup or both");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dmitry Torokhov386b3842009-09-09 19:08:16 -070085static bool i8042_direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086module_param_named(direct, i8042_direct, bool, 0);
87MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
88
Dmitry Torokhov386b3842009-09-09 19:08:16 -070089static bool i8042_dumbkbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
91MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
92
Dmitry Torokhov386b3842009-09-09 19:08:16 -070093static bool i8042_noloop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094module_param_named(noloop, i8042_noloop, bool, 0);
95MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
96
Jiri Kosinaf8313ef2011-01-08 01:37:26 -080097static bool i8042_notimeout;
98module_param_named(notimeout, i8042_notimeout, bool, 0);
99MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
100
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -0800101static bool i8042_kbdreset;
102module_param_named(kbdreset, i8042_kbdreset, bool, 0);
103MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
104
Carlos Corbacho8987fec2008-01-21 01:04:40 -0500105#ifdef CONFIG_X86
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700106static bool i8042_dritek;
Carlos Corbacho8987fec2008-01-21 01:04:40 -0500107module_param_named(dritek, i8042_dritek, bool, 0);
108MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
109#endif
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#ifdef CONFIG_PNP
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700112static bool i8042_nopnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param_named(nopnp, i8042_nopnp, bool, 0);
114MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
115#endif
116
117#define DEBUG
118#ifdef DEBUG
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700119static bool i8042_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param_named(debug, i8042_debug, bool, 0600);
121MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700122
123static bool i8042_unmask_kbd_data;
124module_param_named(unmask_kbd_data, i8042_unmask_kbd_data, bool, 0600);
125MODULE_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]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif
127
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700128static bool i8042_bypass_aux_irq_test;
Hans de Goedea7c58682014-04-19 20:47:35 -0700129static char i8042_kbd_firmware_id[128];
130static char i8042_aux_firmware_id[128];
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#include "i8042.h"
133
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700134/*
135 * i8042_lock protects serialization between i8042_command and
136 * the interrupt handler.
137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static DEFINE_SPINLOCK(i8042_lock);
139
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700140/*
141 * Writers to AUX and KBD ports as well as users issuing i8042_command
142 * directly should acquire i8042_mutex (by means of calling
143 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
144 * they do not disturb each other (unfortunately in many i8042
145 * implementations write to one of the ports will immediately abort
146 * command that is being processed by another port).
147 */
148static DEFINE_MUTEX(i8042_mutex);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150struct i8042_port {
151 struct serio *serio;
152 int irq;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700153 bool exists;
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700154 bool driver_bound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 signed char mux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158#define I8042_KBD_PORT_NO 0
159#define I8042_AUX_PORT_NO 1
160#define I8042_MUX_PORT_NO 2
161#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400162
163static struct i8042_port i8042_ports[I8042_NUM_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165static unsigned char i8042_initial_ctr;
166static unsigned char i8042_ctr;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700167static bool i8042_mux_present;
168static bool i8042_kbd_irq_registered;
169static bool i8042_aux_irq_registered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400170static unsigned char i8042_suppress_kbd_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static struct platform_device *i8042_platform_device;
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700172static struct notifier_block i8042_kbd_bind_notifier_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David Howells7d12e782006-10-05 14:55:46 +0100174static irqreturn_t i8042_interrupt(int irq, void *dev_id);
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800175static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
176 struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700178void i8042_lock_chip(void)
179{
180 mutex_lock(&i8042_mutex);
181}
182EXPORT_SYMBOL(i8042_lock_chip);
183
184void i8042_unlock_chip(void)
185{
186 mutex_unlock(&i8042_mutex);
187}
188EXPORT_SYMBOL(i8042_unlock_chip);
189
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800190int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
191 struct serio *serio))
192{
193 unsigned long flags;
194 int ret = 0;
195
196 spin_lock_irqsave(&i8042_lock, flags);
197
198 if (i8042_platform_filter) {
199 ret = -EBUSY;
200 goto out;
201 }
202
203 i8042_platform_filter = filter;
204
205out:
206 spin_unlock_irqrestore(&i8042_lock, flags);
207 return ret;
208}
209EXPORT_SYMBOL(i8042_install_filter);
210
211int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
212 struct serio *port))
213{
214 unsigned long flags;
215 int ret = 0;
216
217 spin_lock_irqsave(&i8042_lock, flags);
218
219 if (i8042_platform_filter != filter) {
220 ret = -EINVAL;
221 goto out;
222 }
223
224 i8042_platform_filter = NULL;
225
226out:
227 spin_unlock_irqrestore(&i8042_lock, flags);
228 return ret;
229}
230EXPORT_SYMBOL(i8042_remove_filter);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
234 * be ready for reading values from it / writing values to it.
235 * Called always with i8042_lock held.
236 */
237
238static int i8042_wait_read(void)
239{
240 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
243 udelay(50);
244 i++;
245 }
246 return -(i == I8042_CTL_TIMEOUT);
247}
248
249static int i8042_wait_write(void)
250{
251 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
254 udelay(50);
255 i++;
256 }
257 return -(i == I8042_CTL_TIMEOUT);
258}
259
260/*
261 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
262 * of the i8042 down the toilet.
263 */
264
265static int i8042_flush(void)
266{
267 unsigned long flags;
268 unsigned char data, str;
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700269 int count = 0;
270 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 spin_lock_irqsave(&i8042_lock, flags);
273
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700274 while ((str = i8042_read_status()) & I8042_STR_OBF) {
275 if (count++ < I8042_BUFFER_SIZE) {
276 udelay(50);
277 data = i8042_read_data();
278 dbg("%02x <- i8042 (flush, %s)\n",
279 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
280 } else {
281 retval = -EIO;
282 break;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 spin_unlock_irqrestore(&i8042_lock, flags);
287
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700288 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
292 * i8042_command() executes a command on the i8042. It also sends the input
293 * parameter(s) of the commands to it, and receives the output value(s). The
294 * parameters are to be stored in the param array, and the output is placed
295 * into the same array. The number of the parameters and output values is
296 * encoded in bits 8-11 of the command number.
297 */
298
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400299static int __i8042_command(unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400301 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
304 return -1;
305
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400306 error = i8042_wait_write();
307 if (error)
308 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500309
Joe Perches4eb3c302010-11-29 23:33:07 -0800310 dbg("%02x -> i8042 (command)\n", command & 0xff);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500311 i8042_write_command(command & 0xff);
312
313 for (i = 0; i < ((command >> 12) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400314 error = i8042_wait_write();
315 if (error)
316 return error;
Joe Perches4eb3c302010-11-29 23:33:07 -0800317 dbg("%02x -> i8042 (parameter)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500318 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500321 for (i = 0; i < ((command >> 8) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400322 error = i8042_wait_read();
323 if (error) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800324 dbg(" -- i8042 (timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400325 return error;
326 }
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500327
328 if (command == I8042_CMD_AUX_LOOP &&
329 !(i8042_read_status() & I8042_STR_AUXDATA)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800330 dbg(" -- i8042 (auxerr)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400331 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500334 param[i] = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800335 dbg("%02x <- i8042 (return)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400338 return 0;
339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Márton Németh553a05b2007-10-22 00:56:52 -0400341int i8042_command(unsigned char *param, int command)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400342{
343 unsigned long flags;
344 int retval;
345
346 spin_lock_irqsave(&i8042_lock, flags);
347 retval = __i8042_command(param, command);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500348 spin_unlock_irqrestore(&i8042_lock, flags);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return retval;
351}
Márton Németh553a05b2007-10-22 00:56:52 -0400352EXPORT_SYMBOL(i8042_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354/*
355 * i8042_kbd_write() sends a byte out through the keyboard interface.
356 */
357
358static int i8042_kbd_write(struct serio *port, unsigned char c)
359{
360 unsigned long flags;
361 int retval = 0;
362
363 spin_lock_irqsave(&i8042_lock, flags);
364
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400365 if (!(retval = i8042_wait_write())) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800366 dbg("%02x -> i8042 (kbd-data)\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 i8042_write_data(c);
368 }
369
370 spin_unlock_irqrestore(&i8042_lock, flags);
371
372 return retval;
373}
374
375/*
376 * i8042_aux_write() sends a byte out through the aux interface.
377 */
378
379static int i8042_aux_write(struct serio *serio, unsigned char c)
380{
381 struct i8042_port *port = serio->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dmitry Torokhovf4e3c712006-11-02 23:27:49 -0500383 return i8042_command(&c, port->mux == -1 ?
384 I8042_CMD_AUX_SEND :
385 I8042_CMD_MUX_SEND + port->mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700388
389/*
390 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
391 * and then re-enabling it.
392 */
393
394static void i8042_port_close(struct serio *serio)
395{
396 int irq_bit;
397 int disable_bit;
398 const char *port_name;
399
400 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
401 irq_bit = I8042_CTR_AUXINT;
402 disable_bit = I8042_CTR_AUXDIS;
403 port_name = "AUX";
404 } else {
405 irq_bit = I8042_CTR_KBDINT;
406 disable_bit = I8042_CTR_KBDDIS;
407 port_name = "KBD";
408 }
409
410 i8042_ctr &= ~irq_bit;
411 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800412 pr_warn("Can't write CTR while closing %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700413
414 udelay(50);
415
416 i8042_ctr &= ~disable_bit;
417 i8042_ctr |= irq_bit;
418 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800419 pr_err("Can't reactivate %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700420
421 /*
422 * See if there is any data appeared while we were messing with
423 * port state.
424 */
425 i8042_interrupt(0, NULL);
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * i8042_start() is called by serio core when port is about to finish
430 * registering. It will mark port as existing so i8042_interrupt can
431 * start sending data through it.
432 */
433static int i8042_start(struct serio *serio)
434{
435 struct i8042_port *port = serio->port_data;
436
Chen Hong5b50e0e2017-07-02 15:11:10 -0700437 spin_lock_irq(&i8042_lock);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700438 port->exists = true;
Chen Hong5b50e0e2017-07-02 15:11:10 -0700439 spin_unlock_irq(&i8042_lock);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442}
443
444/*
445 * i8042_stop() marks serio port as non-existing so i8042_interrupt
446 * will not try to send data to the port that is about to go away.
447 * The function is called by serio core as part of unregister procedure.
448 */
449static void i8042_stop(struct serio *serio)
450{
451 struct i8042_port *port = serio->port_data;
452
Chen Hong5b50e0e2017-07-02 15:11:10 -0700453 spin_lock_irq(&i8042_lock);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700454 port->exists = false;
Chen Hong5b50e0e2017-07-02 15:11:10 -0700455 port->serio = NULL;
456 spin_unlock_irq(&i8042_lock);
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400457
458 /*
Chen Hong5b50e0e2017-07-02 15:11:10 -0700459 * We need to make sure that interrupt handler finishes using
460 * our serio port before we return from this function.
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400461 * We synchronize with both AUX and KBD IRQs because there is
462 * a (very unlikely) chance that AUX IRQ is raised for KBD port
463 * and vice versa.
464 */
465 synchronize_irq(I8042_AUX_IRQ);
466 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800470 * i8042_filter() filters out unwanted bytes from the input data stream.
471 * It is called from i8042_interrupt and thus is running with interrupts
472 * off and i8042_lock held.
473 */
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800474static bool i8042_filter(unsigned char data, unsigned char str,
475 struct serio *serio)
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800476{
477 if (unlikely(i8042_suppress_kbd_ack)) {
478 if ((~str & I8042_STR_AUXDATA) &&
479 (data == 0xfa || data == 0xfe)) {
480 i8042_suppress_kbd_ack--;
481 dbg("Extra keyboard ACK - filtered out\n");
482 return true;
483 }
484 }
485
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800486 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
Stefan Weil0747e3b2010-01-07 00:44:08 +0100487 dbg("Filtered out by platform filter\n");
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800488 return true;
489 }
490
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800491 return false;
492}
493
494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * i8042_interrupt() is the most important function in this driver -
496 * it handles the interrupts from the i8042, and sends incoming bytes
497 * to the upper layers.
498 */
499
David Howells7d12e782006-10-05 14:55:46 +0100500static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct i8042_port *port;
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800503 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned long flags;
505 unsigned char str, data;
506 unsigned int dfl;
507 unsigned int port_no;
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800508 bool filtered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400509 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 spin_lock_irqsave(&i8042_lock, flags);
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 str = i8042_read_status();
514 if (unlikely(~str & I8042_STR_OBF)) {
515 spin_unlock_irqrestore(&i8042_lock, flags);
Joe Perches4eb3c302010-11-29 23:33:07 -0800516 if (irq)
517 dbg("Interrupt %d, without any data\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ret = 0;
519 goto out;
520 }
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 data = i8042_read_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
525 static unsigned long last_transmit;
526 static unsigned char last_str;
527
528 dfl = 0;
529 if (str & I8042_STR_MUXERR) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800530 dbg("MUX error, status is %02x, data is %02x\n",
531 str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
533 * When MUXERR condition is signalled the data register can only contain
534 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500535 * it is not always the case. Some KBCs also report 0xfc when there is
536 * nothing connected to the port while others sometimes get confused which
537 * port the data came from and signal error leaving the data intact. They
538 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
539 * to legacy mode yet, when we see one we'll add proper handling).
540 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
541 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * was transmitted (if transmission happened not too long ago).
543 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500544
545 switch (data) {
546 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (time_before(jiffies, last_transmit + HZ/10)) {
548 str = last_str;
549 break;
550 }
551 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500552 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 case 0xfd:
554 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
555 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
556 }
557 }
558
559 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
560 last_str = str;
561 last_transmit = jiffies;
562 } else {
563
564 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
Jiri Kosinaf8313ef2011-01-08 01:37:26 -0800565 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 port_no = (str & I8042_STR_AUXDATA) ?
568 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
569 }
570
571 port = &i8042_ports[port_no];
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800572 serio = port->exists ? port->serio : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700574 filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
575 port_no, irq,
576 dfl & SERIO_PARITY ? ", bad parity" : "",
577 dfl & SERIO_TIMEOUT ? ", timeout" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800579 filtered = i8042_filter(data, str, serio);
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400580
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800581 spin_unlock_irqrestore(&i8042_lock, flags);
582
Chen Hong5b50e0e2017-07-02 15:11:10 -0700583 if (likely(serio && !filtered))
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800584 serio_interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500586 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return IRQ_RETVAL(ret);
588}
589
590/*
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700591 * i8042_enable_kbd_port enables keyboard port on chip
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400592 */
593
594static int i8042_enable_kbd_port(void)
595{
596 i8042_ctr &= ~I8042_CTR_KBDDIS;
597 i8042_ctr |= I8042_CTR_KBDINT;
598
599 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400600 i8042_ctr &= ~I8042_CTR_KBDINT;
601 i8042_ctr |= I8042_CTR_KBDDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800602 pr_err("Failed to enable KBD port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400603 return -EIO;
604 }
605
606 return 0;
607}
608
609/*
610 * i8042_enable_aux_port enables AUX (mouse) port on chip
611 */
612
613static int i8042_enable_aux_port(void)
614{
615 i8042_ctr &= ~I8042_CTR_AUXDIS;
616 i8042_ctr |= I8042_CTR_AUXINT;
617
618 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400619 i8042_ctr &= ~I8042_CTR_AUXINT;
620 i8042_ctr |= I8042_CTR_AUXDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800621 pr_err("Failed to enable AUX port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400622 return -EIO;
623 }
624
625 return 0;
626}
627
628/*
629 * i8042_enable_mux_ports enables 4 individual AUX ports after
630 * the controller has been switched into Multiplexed mode
631 */
632
633static int i8042_enable_mux_ports(void)
634{
635 unsigned char param;
636 int i;
637
638 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
639 i8042_command(&param, I8042_CMD_MUX_PFX + i);
640 i8042_command(&param, I8042_CMD_AUX_ENABLE);
641 }
642
643 return i8042_enable_aux_port();
644}
645
646/*
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700647 * i8042_set_mux_mode checks whether the controller has an
648 * active multiplexor and puts the chip into Multiplexed (true)
649 * or Legacy (false) mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
651
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700652static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700655 unsigned char param, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
657 * Get rid of bytes in the queue.
658 */
659
660 i8042_flush();
661
662/*
663 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400664 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
666
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700667 param = val = 0xf0;
668 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700670 param = val = multiplex ? 0x56 : 0xf6;
671 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700673 param = val = multiplex ? 0xa4 : 0xa5;
674 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
675 return -1;
676
677/*
678 * Workaround for interference with USB Legacy emulation
679 * that causes a v10.12 MUX to be found.
680 */
681 if (param == 0xac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -1;
683
684 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500685 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 return 0;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
691 * i8042_check_mux() checks whether the controller supports the PS/2 Active
692 * Multiplexing specification by Synaptics, Phoenix, Insyde and
693 * LCS/Telegraphics.
694 */
695
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700696static int __init i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 unsigned char mux_version;
699
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700700 if (i8042_set_mux_mode(true, &mux_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -1;
702
Joe Perches4eb3c302010-11-29 23:33:07 -0800703 pr_info("Detected active multiplexing controller, rev %d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 (mux_version >> 4) & 0xf, mux_version & 0xf);
705
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400706/*
707 * Disable all muxed ports by disabling AUX.
708 */
709 i8042_ctr |= I8042_CTR_AUXDIS;
710 i8042_ctr &= ~I8042_CTR_AUXINT;
711
712 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800713 pr_err("Failed to disable AUX port, can't use MUX\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400714 return -EIO;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700717 i8042_mux_present = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720}
721
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400722/*
723 * The following is used to test AUX IRQ delivery.
724 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700725static struct completion i8042_aux_irq_delivered __initdata;
726static bool i8042_irq_being_tested __initdata;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400727
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700728static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400729{
730 unsigned long flags;
731 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400732 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400733
734 spin_lock_irqsave(&i8042_lock, flags);
735 str = i8042_read_status();
736 if (str & I8042_STR_OBF) {
737 data = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800738 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
739 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400740 if (i8042_irq_being_tested &&
741 data == 0xa5 && (str & I8042_STR_AUXDATA))
742 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400743 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400744 }
745 spin_unlock_irqrestore(&i8042_lock, flags);
746
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400747 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400748}
749
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400750/*
751 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
752 * verifies success by readinng CTR. Used when testing for presence of AUX
753 * port.
754 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700755static int __init i8042_toggle_aux(bool on)
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400756{
757 unsigned char param;
758 int i;
759
760 if (i8042_command(&param,
761 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
762 return -1;
763
764 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
765 for (i = 0; i < 100; i++) {
766 udelay(50);
767
768 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
769 return -1;
770
771 if (!(param & I8042_CTR_AUXDIS) == on)
772 return 0;
773 }
774
775 return -1;
776}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778/*
779 * i8042_check_aux() applies as much paranoia as it can at detecting
780 * the presence of an AUX interface.
781 */
782
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700783static int __init i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400785 int retval = -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700786 bool irq_registered = false;
787 bool aux_loop_broken = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400788 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791/*
792 * Get rid of bytes in the queue.
793 */
794
795 i8042_flush();
796
797/*
798 * Internal loopback test - filters out AT-type i8042's. Unfortunately
799 * SiS screwed up and their 5597 doesn't support the LOOP command even
800 * though it has an AUX port.
801 */
802
803 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500804 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
805 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807/*
808 * External connection test - filters out AT-soldered PS/2 i8042's
809 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
810 * 0xfa - no error on some notebooks which ignore the spec
811 * Because it's common for chipsets to return error on perfectly functioning
812 * AUX ports, we test for this only when the LOOP command failed.
813 */
814
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400815 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
816 (param && param != 0xfa && param != 0xff))
817 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500818
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500819/*
820 * If AUX_LOOP completed without error but returned unexpected data
821 * mark it as broken
822 */
823 if (!retval)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700824 aux_loop_broken = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
827/*
828 * Bit assignment test - filters out PS/2 i8042's in AT mode
829 */
830
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700831 if (i8042_toggle_aux(false)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800832 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
833 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700836 if (i8042_toggle_aux(true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -1;
838
839/*
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -0800840 * Reset keyboard (needed on some laptops to successfully detect
841 * touchpad, e.g., some Gigabyte laptop models with Elantech
842 * touchpads).
843 */
844 if (i8042_kbdreset) {
845 pr_warn("Attempting to reset device connected to KBD port\n");
846 i8042_kbd_write(NULL, (unsigned char) 0xff);
847 }
848
849/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400850 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
851 * used it for a PCI card or somethig else.
852 */
853
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700854 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400855/*
856 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
857 * is working and hope we are right.
858 */
859 retval = 0;
860 goto out;
861 }
862
863 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
864 "i8042", i8042_platform_device))
865 goto out;
866
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700867 irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400868
869 if (i8042_enable_aux_port())
870 goto out;
871
872 spin_lock_irqsave(&i8042_lock, flags);
873
874 init_completion(&i8042_aux_irq_delivered);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700875 i8042_irq_being_tested = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400876
877 param = 0xa5;
878 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
879
880 spin_unlock_irqrestore(&i8042_lock, flags);
881
882 if (retval)
883 goto out;
884
885 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
886 msecs_to_jiffies(250)) == 0) {
887/*
888 * AUX IRQ was never delivered so we need to flush the controller to
889 * get rid of the byte we put there; otherwise keyboard may not work.
890 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800891 dbg(" -- i8042 (aux irq test timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400892 i8042_flush();
893 retval = -1;
894 }
895
896 out:
897
898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * Disable the interface.
900 */
901
902 i8042_ctr |= I8042_CTR_AUXDIS;
903 i8042_ctr &= ~I8042_CTR_AUXINT;
904
905 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400906 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400908 if (irq_registered)
909 free_irq(I8042_AUX_IRQ, i8042_platform_device);
910
911 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400914static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700916 if (i8042_flush()) {
Takashi Iwaif5d75342015-09-05 10:29:09 -0700917 pr_info("No controller found\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400918 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400924static int i8042_controller_selftest(void)
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500925{
926 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700927 int i = 0;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500928
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700929 /*
930 * We try this 5 times; on some really fragile systems this does not
931 * take the first time...
932 */
933 do {
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500934
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700935 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
Paul Bollea2a94e72011-03-31 00:11:48 -0700936 pr_err("i8042 controller selftest timeout\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700937 return -ENODEV;
938 }
939
940 if (param == I8042_RET_CTL_TEST)
941 return 0;
942
Paul Bollea2a94e72011-03-31 00:11:48 -0700943 dbg("i8042 controller selftest: %#x != %#x\n",
944 param, I8042_RET_CTL_TEST);
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700945 msleep(50);
946 } while (i++ < 5);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500947
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700948#ifdef CONFIG_X86
949 /*
950 * On x86, we don't fail entire i8042 initialization if controller
951 * reset fails in hopes that keyboard port will still be functional
952 * and user will still get a working keyboard. This is especially
953 * important on netbooks. On other arches we trust hardware more.
954 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800955 pr_info("giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500956 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700957#else
Paul Bollea2a94e72011-03-31 00:11:48 -0700958 pr_err("i8042 controller selftest failed\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700959 return -EIO;
960#endif
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500961}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963/*
964 * i8042_controller init initializes the i8042 controller, and,
965 * most importantly, sets it into non-xlated mode if that's
966 * desired.
967 */
968
969static int i8042_controller_init(void)
970{
971 unsigned long flags;
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800972 int n = 0;
973 unsigned char ctr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975/*
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800976 * Save the CTR for restore on unload / reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
978
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800979 do {
980 if (n >= 10) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800981 pr_err("Unable to get stable CTR read\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800982 return -EIO;
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800985 if (n != 0)
986 udelay(50);
987
988 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800989 pr_err("Can't read CTR while initializing i8042\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800990 return -EIO;
991 }
992
993 } while (n < 2 || ctr[0] != ctr[1]);
994
995 i8042_initial_ctr = i8042_ctr = ctr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997/*
998 * Disable the keyboard interface and interrupt.
999 */
1000
1001 i8042_ctr |= I8042_CTR_KBDDIS;
1002 i8042_ctr &= ~I8042_CTR_KBDINT;
1003
1004/*
1005 * Handle keylock.
1006 */
1007
1008 spin_lock_irqsave(&i8042_lock, flags);
1009 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
1010 if (i8042_unlock)
1011 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001012 else
Joe Perches4eb3c302010-11-29 23:33:07 -08001013 pr_warn("Warning: Keylock active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 spin_unlock_irqrestore(&i8042_lock, flags);
1016
1017/*
1018 * If the chip is configured into nontranslated mode by the BIOS, don't
1019 * bother enabling translating and be happy.
1020 */
1021
1022 if (~i8042_ctr & I8042_CTR_XLATE)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001023 i8042_direct = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025/*
1026 * Set nontranslated mode for the kbd interface if requested by an option.
1027 * After this the kbd interface becomes a simple serial in/out, like the aux
1028 * interface is. We don't do this by default, since it can confuse notebook
1029 * BIOSes.
1030 */
1031
1032 if (i8042_direct)
1033 i8042_ctr &= ~I8042_CTR_XLATE;
1034
1035/*
1036 * Write CTR back.
1037 */
1038
1039 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001040 pr_err("Can't write CTR while initializing i8042\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001041 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001044/*
1045 * Flush whatever accumulated while we were disabling keyboard port.
1046 */
1047
1048 i8042_flush();
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return 0;
1051}
1052
1053
1054/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001055 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
1057
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001058static void i8042_controller_reset(bool s2r_wants_reset)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001059{
1060 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001063 * Disable both KBD and AUX interfaces so they don't get in the way
1064 */
1065
1066 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1067 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1068
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001069 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001070 pr_warn("Can't write CTR while resetting\n");
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001071
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001072/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * Disable MUX mode if present.
1074 */
1075
1076 if (i8042_mux_present)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001077 i8042_set_mux_mode(false, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001080 * Reset the controller if requested.
1081 */
1082
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001083 if (i8042_reset == I8042_RESET_ALWAYS ||
1084 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001085 i8042_controller_selftest();
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001086 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001087
1088/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * Restore the original control register setting.
1090 */
1091
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001092 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001093 pr_warn("Can't restore CTR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096
1097/*
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001098 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1099 * when kernel panics. Flashing LEDs is useful for users running X who may
Michael Opdenackeraa5e5dc2013-09-18 06:00:43 +02001100 * not see the console and will help distinguishing panics from "real"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 * lockups.
1102 *
1103 * Note that DELAY has a limit of 10ms so we will not get stuck here
1104 * waiting for KBC to free up even if KBD interrupt is off
1105 */
1106
1107#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1108
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001109static long i8042_panic_blink(int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 long delay = 0;
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001112 char led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001114 led = (state) ? 0x01 | 0x04 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 while (i8042_read_status() & I8042_STR_IBF)
1116 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001117 dbg("%02x -> i8042 (panic blink)\n", 0xed);
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -05001118 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 i8042_write_data(0xed); /* set leds */
1120 DELAY;
1121 while (i8042_read_status() & I8042_STR_IBF)
1122 DELAY;
1123 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001124 dbg("%02x -> i8042 (panic blink)\n", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 i8042_write_data(led);
1126 DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return delay;
1128}
1129
1130#undef DELAY
1131
Bruno Prémontd35895d2008-05-27 01:36:04 -04001132#ifdef CONFIG_X86
1133static void i8042_dritek_enable(void)
1134{
Christoph Fritz594d6362010-09-29 18:04:21 -07001135 unsigned char param = 0x90;
Bruno Prémontd35895d2008-05-27 01:36:04 -04001136 int error;
1137
1138 error = i8042_command(&param, 0x1059);
1139 if (error)
Joe Perches4eb3c302010-11-29 23:33:07 -08001140 pr_warn("Failed to enable DRITEK extension: %d\n", error);
Bruno Prémontd35895d2008-05-27 01:36:04 -04001141}
1142#endif
1143
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001144#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001147 * Here we try to reset everything back to a state we had
1148 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
1150
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001151static int i8042_controller_resume(bool s2r_wants_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001153 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001155 error = i8042_controller_check();
1156 if (error)
1157 return error;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001158
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001159 if (i8042_reset == I8042_RESET_ALWAYS ||
1160 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001161 error = i8042_controller_selftest();
1162 if (error)
1163 return error;
1164 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001165
1166/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001167 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001168 */
1169
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001170 i8042_ctr = i8042_initial_ctr;
1171 if (i8042_direct)
1172 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001173 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1174 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001175 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001176 pr_warn("Can't write CTR to resume, retrying...\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001177 msleep(50);
1178 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001179 pr_err("CTR write retry failed\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001180 return -EIO;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
Bruno Prémontd35895d2008-05-27 01:36:04 -04001184
1185#ifdef CONFIG_X86
1186 if (i8042_dritek)
1187 i8042_dritek_enable();
1188#endif
1189
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001190 if (i8042_mux_present) {
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001191 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
Joe Perches4eb3c302010-11-29 23:33:07 -08001192 pr_warn("failed to resume active multiplexor, mouse won't work\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001193 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1194 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001196 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1197 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
David Howells7d12e782006-10-05 14:55:46 +01001199 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001203
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001204/*
1205 * Here we try to restore the original BIOS settings to avoid
1206 * upsetting it.
1207 */
1208
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001209static int i8042_pm_suspend(struct device *dev)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001210{
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001211 int i;
1212
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001213 if (pm_suspend_via_firmware())
1214 i8042_controller_reset(true);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001215
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001216 /* Set up serio interrupts for system wakeup. */
1217 for (i = 0; i < I8042_NUM_PORTS; i++) {
1218 struct serio *serio = i8042_ports[i].serio;
1219
1220 if (serio && device_may_wakeup(&serio->dev))
1221 enable_irq_wake(i8042_ports[i].irq);
1222 }
1223
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001224 return 0;
1225}
1226
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001227static int i8042_pm_resume_noirq(struct device *dev)
1228{
1229 if (!pm_resume_via_firmware())
1230 i8042_interrupt(0, NULL);
1231
1232 return 0;
1233}
1234
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001235static int i8042_pm_resume(struct device *dev)
1236{
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001237 bool want_reset;
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001238 int i;
1239
1240 for (i = 0; i < I8042_NUM_PORTS; i++) {
1241 struct serio *serio = i8042_ports[i].serio;
1242
1243 if (serio && device_may_wakeup(&serio->dev))
1244 disable_irq_wake(i8042_ports[i].irq);
1245 }
1246
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001247 /*
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001248 * If platform firmware was not going to be involved in suspend, we did
1249 * not restore the controller state to whatever it had been at boot
1250 * time, so we do not need to do anything.
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001251 */
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001252 if (!pm_suspend_via_firmware())
1253 return 0;
1254
1255 /*
1256 * We only need to reset the controller if we are resuming after handing
1257 * off control to the platform firmware, otherwise we can simply restore
1258 * the mode.
1259 */
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001260 want_reset = pm_resume_via_firmware();
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001261
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001262 return i8042_controller_resume(want_reset);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001263}
1264
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001265static int i8042_pm_thaw(struct device *dev)
1266{
1267 i8042_interrupt(0, NULL);
1268
1269 return 0;
1270}
1271
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001272static int i8042_pm_reset(struct device *dev)
1273{
1274 i8042_controller_reset(false);
1275
1276 return 0;
1277}
1278
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001279static int i8042_pm_restore(struct device *dev)
1280{
1281 return i8042_controller_resume(false);
1282}
1283
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001284static const struct dev_pm_ops i8042_pm_ops = {
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001285 .suspend = i8042_pm_suspend,
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001286 .resume_noirq = i8042_pm_resume_noirq,
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001287 .resume = i8042_pm_resume,
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001288 .thaw = i8042_pm_thaw,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001289 .poweroff = i8042_pm_reset,
1290 .restore = i8042_pm_restore,
1291};
1292
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001293#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295/*
1296 * We need to reset the 8042 back to original mode on system shutdown,
1297 * because otherwise BIOSes will be confused.
1298 */
1299
Russell King3ae5eae2005-11-09 22:32:44 +00001300static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001302 i8042_controller_reset(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001305static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 struct serio *serio;
1308 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1309
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001310 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001311 if (!serio)
1312 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001314 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1315 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001316 serio->start = i8042_start;
1317 serio->stop = i8042_stop;
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001318 serio->close = i8042_port_close;
Dmitry Torokhov40974612016-07-25 11:36:54 -07001319 serio->ps2_cmd_mutex = &i8042_mutex;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001320 serio->port_data = port;
1321 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001322 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001323 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001324 strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1325 sizeof(serio->firmware_id));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001326
1327 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001328 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001329
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001333static int __init i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001336 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1337 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001339 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001340 if (!serio)
1341 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001343 serio->id.type = SERIO_8042;
1344 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001345 serio->start = i8042_start;
1346 serio->stop = i8042_stop;
Dmitry Torokhov47af45d2016-08-16 17:38:54 -07001347 serio->ps2_cmd_mutex = &i8042_mutex;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001348 serio->port_data = port;
1349 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001350 if (idx < 0) {
1351 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1352 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001353 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1354 sizeof(serio->firmware_id));
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001355 serio->close = i8042_port_close;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001356 } else {
1357 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1358 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
Hans de Goede266e43c2014-09-11 10:13:13 -07001359 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1360 sizeof(serio->firmware_id));
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001361 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001362
1363 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001364 port->mux = idx;
1365 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001366
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001370static void __init i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001372 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1373 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001376static void __init i8042_free_aux_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001377{
1378 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001380 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1381 kfree(i8042_ports[i].serio);
1382 i8042_ports[i].serio = NULL;
1383 }
1384}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001385
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001386static void __init i8042_register_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001387{
1388 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001389
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001390 for (i = 0; i < I8042_NUM_PORTS; i++) {
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001391 struct serio *serio = i8042_ports[i].serio;
1392
1393 if (serio) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001394 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001395 serio->name,
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001396 (unsigned long) I8042_DATA_REG,
1397 (unsigned long) I8042_COMMAND_REG,
1398 i8042_ports[i].irq);
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001399 serio_register_port(serio);
1400 device_set_wakeup_capable(&serio->dev, true);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001401 }
1402 }
1403}
1404
Bill Pembertone2619cf2012-11-23 21:50:47 -08001405static void i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001406{
1407 int i;
1408
1409 for (i = 0; i < I8042_NUM_PORTS; i++) {
1410 if (i8042_ports[i].serio) {
1411 serio_unregister_port(i8042_ports[i].serio);
1412 i8042_ports[i].serio = NULL;
1413 }
1414 }
1415}
1416
1417static void i8042_free_irqs(void)
1418{
1419 if (i8042_aux_irq_registered)
1420 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1421 if (i8042_kbd_irq_registered)
1422 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1423
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001424 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001425}
1426
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001427static int __init i8042_setup_aux(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001428{
1429 int (*aux_enable)(void);
1430 int error;
1431 int i;
1432
1433 if (i8042_check_aux())
1434 return -ENODEV;
1435
1436 if (i8042_nomux || i8042_check_mux()) {
1437 error = i8042_create_aux_port(-1);
1438 if (error)
1439 goto err_free_ports;
1440 aux_enable = i8042_enable_aux_port;
1441 } else {
1442 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1443 error = i8042_create_aux_port(i);
1444 if (error)
1445 goto err_free_ports;
1446 }
1447 aux_enable = i8042_enable_mux_ports;
1448 }
1449
1450 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1451 "i8042", i8042_platform_device);
1452 if (error)
1453 goto err_free_ports;
1454
1455 if (aux_enable())
1456 goto err_free_irq;
1457
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001458 i8042_aux_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001459 return 0;
1460
1461 err_free_irq:
1462 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1463 err_free_ports:
1464 i8042_free_aux_ports();
1465 return error;
1466}
1467
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001468static int __init i8042_setup_kbd(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001469{
1470 int error;
1471
1472 error = i8042_create_kbd_port();
1473 if (error)
1474 return error;
1475
1476 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1477 "i8042", i8042_platform_device);
1478 if (error)
1479 goto err_free_port;
1480
1481 error = i8042_enable_kbd_port();
1482 if (error)
1483 goto err_free_irq;
1484
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001485 i8042_kbd_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001486 return 0;
1487
1488 err_free_irq:
1489 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1490 err_free_port:
1491 i8042_free_kbd_port();
1492 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001495static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1496 unsigned long action, void *data)
1497{
1498 struct device *dev = data;
1499 struct serio *serio = to_serio_port(dev);
1500 struct i8042_port *port = serio->port_data;
1501
1502 if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1503 return 0;
1504
1505 switch (action) {
1506 case BUS_NOTIFY_BOUND_DRIVER:
1507 port->driver_bound = true;
1508 break;
1509
1510 case BUS_NOTIFY_UNBIND_DRIVER:
1511 port->driver_bound = false;
1512 break;
1513 }
1514
1515 return 0;
1516}
1517
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001518static int __init i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001520 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001522 i8042_platform_device = dev;
1523
Marcos Paulo de Souza930e1922016-10-01 12:07:35 -07001524 if (i8042_reset == I8042_RESET_ALWAYS) {
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001525 error = i8042_controller_selftest();
1526 if (error)
1527 return error;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001530 error = i8042_controller_init();
1531 if (error)
1532 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Bruno Prémontd35895d2008-05-27 01:36:04 -04001534#ifdef CONFIG_X86
1535 if (i8042_dritek)
1536 i8042_dritek_enable();
1537#endif
1538
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001539 if (!i8042_noaux) {
1540 error = i8042_setup_aux();
1541 if (error && error != -ENODEV && error != -EBUSY)
1542 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001545 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001546 error = i8042_setup_kbd();
1547 if (error)
1548 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001549 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001550/*
1551 * Ok, everything is ready, let's register all serio ports
1552 */
1553 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001556
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001557 out_fail:
1558 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1559 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001560 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001561 i8042_platform_device = NULL;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001562
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001563 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Bill Pembertone2619cf2012-11-23 21:50:47 -08001566static int i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001568 i8042_unregister_ports();
1569 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001570 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001571 i8042_platform_device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001573 return 0;
1574}
1575
1576static struct platform_driver i8042_driver = {
1577 .driver = {
1578 .name = "i8042",
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001579#ifdef CONFIG_PM
1580 .pm = &i8042_pm_ops,
1581#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001582 },
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001583 .remove = i8042_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001584 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001585};
1586
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001587static struct notifier_block i8042_kbd_bind_notifier_block = {
1588 .notifier_call = i8042_kbd_bind_notifier,
1589};
1590
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001591static int __init i8042_init(void)
1592{
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001593 struct platform_device *pdev;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001594 int err;
1595
1596 dbg_init();
1597
1598 err = i8042_platform_init();
1599 if (err)
1600 return err;
1601
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001602 err = i8042_controller_check();
1603 if (err)
1604 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001605
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001606 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1607 if (IS_ERR(pdev)) {
1608 err = PTR_ERR(pdev);
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001609 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001610 }
1611
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001612 bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001613 panic_blink = i8042_panic_blink;
1614
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001615 return 0;
1616
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001617 err_platform_exit:
1618 i8042_platform_exit();
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001619 return err;
1620}
1621
1622static void __exit i8042_exit(void)
1623{
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001624 platform_device_unregister(i8042_platform_device);
Dmitry Torokhovaf045b82010-08-31 17:27:02 -07001625 platform_driver_unregister(&i8042_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 i8042_platform_exit();
1627
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001628 bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 panic_blink = NULL;
1630}
1631
1632module_init(i8042_init);
1633module_exit(i8042_exit);