blob: 62685a76891363b93d1d6a1d4db613fad814686b [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 Souza930e19242016-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/*
Marcos Paulo de Souza0e2b4452016-11-22 18:03:35 -0800390 * i8042_port_close attempts to clear AUX or KBD port state by disabling
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700391 * 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
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700437 port->exists = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 mb();
439 return 0;
440}
441
442/*
443 * i8042_stop() marks serio port as non-existing so i8042_interrupt
444 * will not try to send data to the port that is about to go away.
445 * The function is called by serio core as part of unregister procedure.
446 */
447static void i8042_stop(struct serio *serio)
448{
449 struct i8042_port *port = serio->port_data;
450
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700451 port->exists = false;
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400452
453 /*
454 * We synchronize with both AUX and KBD IRQs because there is
455 * a (very unlikely) chance that AUX IRQ is raised for KBD port
456 * and vice versa.
457 */
458 synchronize_irq(I8042_AUX_IRQ);
459 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 port->serio = NULL;
461}
462
463/*
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800464 * i8042_filter() filters out unwanted bytes from the input data stream.
465 * It is called from i8042_interrupt and thus is running with interrupts
466 * off and i8042_lock held.
467 */
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800468static bool i8042_filter(unsigned char data, unsigned char str,
469 struct serio *serio)
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800470{
471 if (unlikely(i8042_suppress_kbd_ack)) {
472 if ((~str & I8042_STR_AUXDATA) &&
473 (data == 0xfa || data == 0xfe)) {
474 i8042_suppress_kbd_ack--;
475 dbg("Extra keyboard ACK - filtered out\n");
476 return true;
477 }
478 }
479
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800480 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
Stefan Weil0747e3b2010-01-07 00:44:08 +0100481 dbg("Filtered out by platform filter\n");
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800482 return true;
483 }
484
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800485 return false;
486}
487
488/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * i8042_interrupt() is the most important function in this driver -
490 * it handles the interrupts from the i8042, and sends incoming bytes
491 * to the upper layers.
492 */
493
David Howells7d12e782006-10-05 14:55:46 +0100494static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct i8042_port *port;
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800497 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 unsigned long flags;
499 unsigned char str, data;
500 unsigned int dfl;
501 unsigned int port_no;
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800502 bool filtered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400503 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 spin_lock_irqsave(&i8042_lock, flags);
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 str = i8042_read_status();
508 if (unlikely(~str & I8042_STR_OBF)) {
509 spin_unlock_irqrestore(&i8042_lock, flags);
Joe Perches4eb3c302010-11-29 23:33:07 -0800510 if (irq)
511 dbg("Interrupt %d, without any data\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 ret = 0;
513 goto out;
514 }
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 data = i8042_read_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
519 static unsigned long last_transmit;
520 static unsigned char last_str;
521
522 dfl = 0;
523 if (str & I8042_STR_MUXERR) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800524 dbg("MUX error, status is %02x, data is %02x\n",
525 str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/*
527 * When MUXERR condition is signalled the data register can only contain
528 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500529 * it is not always the case. Some KBCs also report 0xfc when there is
530 * nothing connected to the port while others sometimes get confused which
531 * port the data came from and signal error leaving the data intact. They
532 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
533 * to legacy mode yet, when we see one we'll add proper handling).
534 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
535 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * was transmitted (if transmission happened not too long ago).
537 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500538
539 switch (data) {
540 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (time_before(jiffies, last_transmit + HZ/10)) {
542 str = last_str;
543 break;
544 }
545 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500546 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 case 0xfd:
548 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
549 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
550 }
551 }
552
553 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
554 last_str = str;
555 last_transmit = jiffies;
556 } else {
557
558 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
Jiri Kosinaf8313ef2011-01-08 01:37:26 -0800559 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 port_no = (str & I8042_STR_AUXDATA) ?
562 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
563 }
564
565 port = &i8042_ports[port_no];
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800566 serio = port->exists ? port->serio : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700568 filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
569 port_no, irq,
570 dfl & SERIO_PARITY ? ", bad parity" : "",
571 dfl & SERIO_TIMEOUT ? ", timeout" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800573 filtered = i8042_filter(data, str, serio);
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400574
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800575 spin_unlock_irqrestore(&i8042_lock, flags);
576
577 if (likely(port->exists && !filtered))
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800578 serio_interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500580 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return IRQ_RETVAL(ret);
582}
583
584/*
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700585 * i8042_enable_kbd_port enables keyboard port on chip
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400586 */
587
588static int i8042_enable_kbd_port(void)
589{
590 i8042_ctr &= ~I8042_CTR_KBDDIS;
591 i8042_ctr |= I8042_CTR_KBDINT;
592
593 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400594 i8042_ctr &= ~I8042_CTR_KBDINT;
595 i8042_ctr |= I8042_CTR_KBDDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800596 pr_err("Failed to enable KBD port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400597 return -EIO;
598 }
599
600 return 0;
601}
602
603/*
604 * i8042_enable_aux_port enables AUX (mouse) port on chip
605 */
606
607static int i8042_enable_aux_port(void)
608{
609 i8042_ctr &= ~I8042_CTR_AUXDIS;
610 i8042_ctr |= I8042_CTR_AUXINT;
611
612 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400613 i8042_ctr &= ~I8042_CTR_AUXINT;
614 i8042_ctr |= I8042_CTR_AUXDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800615 pr_err("Failed to enable AUX port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400616 return -EIO;
617 }
618
619 return 0;
620}
621
622/*
623 * i8042_enable_mux_ports enables 4 individual AUX ports after
624 * the controller has been switched into Multiplexed mode
625 */
626
627static int i8042_enable_mux_ports(void)
628{
629 unsigned char param;
630 int i;
631
632 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
633 i8042_command(&param, I8042_CMD_MUX_PFX + i);
634 i8042_command(&param, I8042_CMD_AUX_ENABLE);
635 }
636
637 return i8042_enable_aux_port();
638}
639
640/*
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700641 * i8042_set_mux_mode checks whether the controller has an
642 * active multiplexor and puts the chip into Multiplexed (true)
643 * or Legacy (false) mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
645
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700646static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700649 unsigned char param, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
651 * Get rid of bytes in the queue.
652 */
653
654 i8042_flush();
655
656/*
657 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400658 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
660
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700661 param = val = 0xf0;
662 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700664 param = val = multiplex ? 0x56 : 0xf6;
665 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700667 param = val = multiplex ? 0xa4 : 0xa5;
668 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
669 return -1;
670
671/*
672 * Workaround for interference with USB Legacy emulation
673 * that causes a v10.12 MUX to be found.
674 */
675 if (param == 0xac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -1;
677
678 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500679 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 return 0;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/*
685 * i8042_check_mux() checks whether the controller supports the PS/2 Active
686 * Multiplexing specification by Synaptics, Phoenix, Insyde and
687 * LCS/Telegraphics.
688 */
689
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700690static int __init i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 unsigned char mux_version;
693
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700694 if (i8042_set_mux_mode(true, &mux_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -1;
696
Joe Perches4eb3c302010-11-29 23:33:07 -0800697 pr_info("Detected active multiplexing controller, rev %d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 (mux_version >> 4) & 0xf, mux_version & 0xf);
699
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400700/*
701 * Disable all muxed ports by disabling AUX.
702 */
703 i8042_ctr |= I8042_CTR_AUXDIS;
704 i8042_ctr &= ~I8042_CTR_AUXINT;
705
706 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800707 pr_err("Failed to disable AUX port, can't use MUX\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400708 return -EIO;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700711 i8042_mux_present = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714}
715
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400716/*
717 * The following is used to test AUX IRQ delivery.
718 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700719static struct completion i8042_aux_irq_delivered __initdata;
720static bool i8042_irq_being_tested __initdata;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400721
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700722static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400723{
724 unsigned long flags;
725 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400726 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400727
728 spin_lock_irqsave(&i8042_lock, flags);
729 str = i8042_read_status();
730 if (str & I8042_STR_OBF) {
731 data = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800732 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
733 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400734 if (i8042_irq_being_tested &&
735 data == 0xa5 && (str & I8042_STR_AUXDATA))
736 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400737 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400738 }
739 spin_unlock_irqrestore(&i8042_lock, flags);
740
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400741 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400742}
743
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400744/*
745 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
746 * verifies success by readinng CTR. Used when testing for presence of AUX
747 * port.
748 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700749static int __init i8042_toggle_aux(bool on)
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400750{
751 unsigned char param;
752 int i;
753
754 if (i8042_command(&param,
755 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
756 return -1;
757
758 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
759 for (i = 0; i < 100; i++) {
760 udelay(50);
761
762 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
763 return -1;
764
765 if (!(param & I8042_CTR_AUXDIS) == on)
766 return 0;
767 }
768
769 return -1;
770}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772/*
773 * i8042_check_aux() applies as much paranoia as it can at detecting
774 * the presence of an AUX interface.
775 */
776
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700777static int __init i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400779 int retval = -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700780 bool irq_registered = false;
781 bool aux_loop_broken = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400782 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785/*
786 * Get rid of bytes in the queue.
787 */
788
789 i8042_flush();
790
791/*
792 * Internal loopback test - filters out AT-type i8042's. Unfortunately
793 * SiS screwed up and their 5597 doesn't support the LOOP command even
794 * though it has an AUX port.
795 */
796
797 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500798 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
799 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801/*
802 * External connection test - filters out AT-soldered PS/2 i8042's
803 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
804 * 0xfa - no error on some notebooks which ignore the spec
805 * Because it's common for chipsets to return error on perfectly functioning
806 * AUX ports, we test for this only when the LOOP command failed.
807 */
808
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400809 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
810 (param && param != 0xfa && param != 0xff))
811 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500812
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500813/*
814 * If AUX_LOOP completed without error but returned unexpected data
815 * mark it as broken
816 */
817 if (!retval)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700818 aux_loop_broken = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821/*
822 * Bit assignment test - filters out PS/2 i8042's in AT mode
823 */
824
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700825 if (i8042_toggle_aux(false)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800826 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
827 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700830 if (i8042_toggle_aux(true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -1;
832
833/*
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -0800834 * Reset keyboard (needed on some laptops to successfully detect
835 * touchpad, e.g., some Gigabyte laptop models with Elantech
836 * touchpads).
837 */
838 if (i8042_kbdreset) {
839 pr_warn("Attempting to reset device connected to KBD port\n");
840 i8042_kbd_write(NULL, (unsigned char) 0xff);
841 }
842
843/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400844 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
845 * used it for a PCI card or somethig else.
846 */
847
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700848 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400849/*
850 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
851 * is working and hope we are right.
852 */
853 retval = 0;
854 goto out;
855 }
856
857 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
858 "i8042", i8042_platform_device))
859 goto out;
860
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700861 irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400862
863 if (i8042_enable_aux_port())
864 goto out;
865
866 spin_lock_irqsave(&i8042_lock, flags);
867
868 init_completion(&i8042_aux_irq_delivered);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700869 i8042_irq_being_tested = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400870
871 param = 0xa5;
872 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
873
874 spin_unlock_irqrestore(&i8042_lock, flags);
875
876 if (retval)
877 goto out;
878
879 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
880 msecs_to_jiffies(250)) == 0) {
881/*
882 * AUX IRQ was never delivered so we need to flush the controller to
883 * get rid of the byte we put there; otherwise keyboard may not work.
884 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800885 dbg(" -- i8042 (aux irq test timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400886 i8042_flush();
887 retval = -1;
888 }
889
890 out:
891
892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * Disable the interface.
894 */
895
896 i8042_ctr |= I8042_CTR_AUXDIS;
897 i8042_ctr &= ~I8042_CTR_AUXINT;
898
899 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400900 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400902 if (irq_registered)
903 free_irq(I8042_AUX_IRQ, i8042_platform_device);
904
905 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400908static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700910 if (i8042_flush()) {
Takashi Iwaif5d75342015-09-05 10:29:09 -0700911 pr_info("No controller found\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400912 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400918static int i8042_controller_selftest(void)
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500919{
920 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700921 int i = 0;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500922
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700923 /*
924 * We try this 5 times; on some really fragile systems this does not
925 * take the first time...
926 */
927 do {
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500928
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700929 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
Paul Bollea2a94e72011-03-31 00:11:48 -0700930 pr_err("i8042 controller selftest timeout\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700931 return -ENODEV;
932 }
933
934 if (param == I8042_RET_CTL_TEST)
935 return 0;
936
Paul Bollea2a94e72011-03-31 00:11:48 -0700937 dbg("i8042 controller selftest: %#x != %#x\n",
938 param, I8042_RET_CTL_TEST);
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700939 msleep(50);
940 } while (i++ < 5);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500941
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700942#ifdef CONFIG_X86
943 /*
944 * On x86, we don't fail entire i8042 initialization if controller
945 * reset fails in hopes that keyboard port will still be functional
946 * and user will still get a working keyboard. This is especially
947 * important on netbooks. On other arches we trust hardware more.
948 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800949 pr_info("giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500950 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700951#else
Paul Bollea2a94e72011-03-31 00:11:48 -0700952 pr_err("i8042 controller selftest failed\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700953 return -EIO;
954#endif
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500955}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957/*
958 * i8042_controller init initializes the i8042 controller, and,
959 * most importantly, sets it into non-xlated mode if that's
960 * desired.
961 */
962
963static int i8042_controller_init(void)
964{
965 unsigned long flags;
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800966 int n = 0;
967 unsigned char ctr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969/*
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800970 * Save the CTR for restore on unload / reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
972
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800973 do {
974 if (n >= 10) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800975 pr_err("Unable to get stable CTR read\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800976 return -EIO;
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800979 if (n != 0)
980 udelay(50);
981
982 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800983 pr_err("Can't read CTR while initializing i8042\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800984 return -EIO;
985 }
986
987 } while (n < 2 || ctr[0] != ctr[1]);
988
989 i8042_initial_ctr = i8042_ctr = ctr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991/*
992 * Disable the keyboard interface and interrupt.
993 */
994
995 i8042_ctr |= I8042_CTR_KBDDIS;
996 i8042_ctr &= ~I8042_CTR_KBDINT;
997
998/*
999 * Handle keylock.
1000 */
1001
1002 spin_lock_irqsave(&i8042_lock, flags);
1003 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
1004 if (i8042_unlock)
1005 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001006 else
Joe Perches4eb3c302010-11-29 23:33:07 -08001007 pr_warn("Warning: Keylock active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 spin_unlock_irqrestore(&i8042_lock, flags);
1010
1011/*
1012 * If the chip is configured into nontranslated mode by the BIOS, don't
1013 * bother enabling translating and be happy.
1014 */
1015
1016 if (~i8042_ctr & I8042_CTR_XLATE)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001017 i8042_direct = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019/*
1020 * Set nontranslated mode for the kbd interface if requested by an option.
1021 * After this the kbd interface becomes a simple serial in/out, like the aux
1022 * interface is. We don't do this by default, since it can confuse notebook
1023 * BIOSes.
1024 */
1025
1026 if (i8042_direct)
1027 i8042_ctr &= ~I8042_CTR_XLATE;
1028
1029/*
1030 * Write CTR back.
1031 */
1032
1033 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001034 pr_err("Can't write CTR while initializing i8042\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001035 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001038/*
1039 * Flush whatever accumulated while we were disabling keyboard port.
1040 */
1041
1042 i8042_flush();
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
1045}
1046
1047
1048/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001049 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
1051
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001052static void i8042_controller_reset(bool s2r_wants_reset)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001053{
1054 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001057 * Disable both KBD and AUX interfaces so they don't get in the way
1058 */
1059
1060 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1061 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1062
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001063 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001064 pr_warn("Can't write CTR while resetting\n");
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001065
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001066/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * Disable MUX mode if present.
1068 */
1069
1070 if (i8042_mux_present)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001071 i8042_set_mux_mode(false, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001074 * Reset the controller if requested.
1075 */
1076
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001077 if (i8042_reset == I8042_RESET_ALWAYS ||
1078 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001079 i8042_controller_selftest();
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001080 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001081
1082/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * Restore the original control register setting.
1084 */
1085
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001086 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001087 pr_warn("Can't restore CTR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090
1091/*
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001092 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1093 * when kernel panics. Flashing LEDs is useful for users running X who may
Michael Opdenackeraa5e5dc2013-09-18 06:00:43 +02001094 * not see the console and will help distinguishing panics from "real"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * lockups.
1096 *
1097 * Note that DELAY has a limit of 10ms so we will not get stuck here
1098 * waiting for KBC to free up even if KBD interrupt is off
1099 */
1100
1101#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1102
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001103static long i8042_panic_blink(int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 long delay = 0;
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001106 char led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001108 led = (state) ? 0x01 | 0x04 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 while (i8042_read_status() & I8042_STR_IBF)
1110 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001111 dbg("%02x -> i8042 (panic blink)\n", 0xed);
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -05001112 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 i8042_write_data(0xed); /* set leds */
1114 DELAY;
1115 while (i8042_read_status() & I8042_STR_IBF)
1116 DELAY;
1117 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001118 dbg("%02x -> i8042 (panic blink)\n", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 i8042_write_data(led);
1120 DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return delay;
1122}
1123
1124#undef DELAY
1125
Bruno Prémontd35895d2008-05-27 01:36:04 -04001126#ifdef CONFIG_X86
1127static void i8042_dritek_enable(void)
1128{
Christoph Fritz594d6362010-09-29 18:04:21 -07001129 unsigned char param = 0x90;
Bruno Prémontd35895d2008-05-27 01:36:04 -04001130 int error;
1131
1132 error = i8042_command(&param, 0x1059);
1133 if (error)
Joe Perches4eb3c302010-11-29 23:33:07 -08001134 pr_warn("Failed to enable DRITEK extension: %d\n", error);
Bruno Prémontd35895d2008-05-27 01:36:04 -04001135}
1136#endif
1137
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001138#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001141 * Here we try to reset everything back to a state we had
1142 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
1144
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001145static int i8042_controller_resume(bool s2r_wants_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001147 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001149 error = i8042_controller_check();
1150 if (error)
1151 return error;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001152
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001153 if (i8042_reset == I8042_RESET_ALWAYS ||
1154 (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001155 error = i8042_controller_selftest();
1156 if (error)
1157 return error;
1158 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001159
1160/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001161 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001162 */
1163
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001164 i8042_ctr = i8042_initial_ctr;
1165 if (i8042_direct)
1166 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001167 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1168 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001169 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001170 pr_warn("Can't write CTR to resume, retrying...\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001171 msleep(50);
1172 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001173 pr_err("CTR write retry failed\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001174 return -EIO;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
Bruno Prémontd35895d2008-05-27 01:36:04 -04001178
1179#ifdef CONFIG_X86
1180 if (i8042_dritek)
1181 i8042_dritek_enable();
1182#endif
1183
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001184 if (i8042_mux_present) {
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001185 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
Joe Perches4eb3c302010-11-29 23:33:07 -08001186 pr_warn("failed to resume active multiplexor, mouse won't work\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001187 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1188 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001190 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1191 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
David Howells7d12e782006-10-05 14:55:46 +01001193 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001197
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001198/*
1199 * Here we try to restore the original BIOS settings to avoid
1200 * upsetting it.
1201 */
1202
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001203static int i8042_pm_suspend(struct device *dev)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001204{
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001205 int i;
1206
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001207 if (pm_suspend_via_firmware())
1208 i8042_controller_reset(true);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001209
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001210 /* Set up serio interrupts for system wakeup. */
1211 for (i = 0; i < I8042_NUM_PORTS; i++) {
1212 struct serio *serio = i8042_ports[i].serio;
1213
1214 if (serio && device_may_wakeup(&serio->dev))
1215 enable_irq_wake(i8042_ports[i].irq);
1216 }
1217
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001218 return 0;
1219}
1220
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001221static int i8042_pm_resume_noirq(struct device *dev)
1222{
1223 if (!pm_resume_via_firmware())
1224 i8042_interrupt(0, NULL);
1225
1226 return 0;
1227}
1228
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001229static int i8042_pm_resume(struct device *dev)
1230{
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001231 bool want_reset;
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001232 int i;
1233
1234 for (i = 0; i < I8042_NUM_PORTS; i++) {
1235 struct serio *serio = i8042_ports[i].serio;
1236
1237 if (serio && device_may_wakeup(&serio->dev))
1238 disable_irq_wake(i8042_ports[i].irq);
1239 }
1240
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001241 /*
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001242 * If platform firmware was not going to be involved in suspend, we did
1243 * not restore the controller state to whatever it had been at boot
1244 * time, so we do not need to do anything.
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001245 */
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001246 if (!pm_suspend_via_firmware())
1247 return 0;
1248
1249 /*
1250 * We only need to reset the controller if we are resuming after handing
1251 * off control to the platform firmware, otherwise we can simply restore
1252 * the mode.
1253 */
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001254 want_reset = pm_resume_via_firmware();
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001255
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001256 return i8042_controller_resume(want_reset);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001257}
1258
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001259static int i8042_pm_thaw(struct device *dev)
1260{
1261 i8042_interrupt(0, NULL);
1262
1263 return 0;
1264}
1265
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001266static int i8042_pm_reset(struct device *dev)
1267{
1268 i8042_controller_reset(false);
1269
1270 return 0;
1271}
1272
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001273static int i8042_pm_restore(struct device *dev)
1274{
1275 return i8042_controller_resume(false);
1276}
1277
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001278static const struct dev_pm_ops i8042_pm_ops = {
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001279 .suspend = i8042_pm_suspend,
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001280 .resume_noirq = i8042_pm_resume_noirq,
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001281 .resume = i8042_pm_resume,
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001282 .thaw = i8042_pm_thaw,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001283 .poweroff = i8042_pm_reset,
1284 .restore = i8042_pm_restore,
1285};
1286
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001287#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/*
1290 * We need to reset the 8042 back to original mode on system shutdown,
1291 * because otherwise BIOSes will be confused.
1292 */
1293
Russell King3ae5eae2005-11-09 22:32:44 +00001294static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001296 i8042_controller_reset(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001299static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct serio *serio;
1302 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1303
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001304 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001305 if (!serio)
1306 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001308 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1309 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001310 serio->start = i8042_start;
1311 serio->stop = i8042_stop;
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001312 serio->close = i8042_port_close;
Dmitry Torokhov40974612016-07-25 11:36:54 -07001313 serio->ps2_cmd_mutex = &i8042_mutex;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001314 serio->port_data = port;
1315 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001316 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001317 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001318 strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1319 sizeof(serio->firmware_id));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001320
1321 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001322 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001323
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001327static int __init i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001330 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1331 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001333 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001334 if (!serio)
1335 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001337 serio->id.type = SERIO_8042;
1338 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001339 serio->start = i8042_start;
1340 serio->stop = i8042_stop;
Dmitry Torokhov47af45d2016-08-16 17:38:54 -07001341 serio->ps2_cmd_mutex = &i8042_mutex;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001342 serio->port_data = port;
1343 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001344 if (idx < 0) {
1345 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1346 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001347 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1348 sizeof(serio->firmware_id));
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001349 serio->close = i8042_port_close;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001350 } else {
1351 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1352 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
Hans de Goede266e43c2014-09-11 10:13:13 -07001353 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1354 sizeof(serio->firmware_id));
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001355 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001356
1357 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001358 port->mux = idx;
1359 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001360
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001364static void __init i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001366 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1367 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001370static void __init i8042_free_aux_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001371{
1372 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001374 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1375 kfree(i8042_ports[i].serio);
1376 i8042_ports[i].serio = NULL;
1377 }
1378}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001379
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001380static void __init i8042_register_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001381{
1382 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001383
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001384 for (i = 0; i < I8042_NUM_PORTS; i++) {
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001385 struct serio *serio = i8042_ports[i].serio;
1386
1387 if (serio) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001388 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001389 serio->name,
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001390 (unsigned long) I8042_DATA_REG,
1391 (unsigned long) I8042_COMMAND_REG,
1392 i8042_ports[i].irq);
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001393 serio_register_port(serio);
1394 device_set_wakeup_capable(&serio->dev, true);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001395 }
1396 }
1397}
1398
Bill Pembertone2619cf2012-11-23 21:50:47 -08001399static void i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001400{
1401 int i;
1402
1403 for (i = 0; i < I8042_NUM_PORTS; i++) {
1404 if (i8042_ports[i].serio) {
1405 serio_unregister_port(i8042_ports[i].serio);
1406 i8042_ports[i].serio = NULL;
1407 }
1408 }
1409}
1410
1411static void i8042_free_irqs(void)
1412{
1413 if (i8042_aux_irq_registered)
1414 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1415 if (i8042_kbd_irq_registered)
1416 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1417
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001418 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001419}
1420
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001421static int __init i8042_setup_aux(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001422{
1423 int (*aux_enable)(void);
1424 int error;
1425 int i;
1426
1427 if (i8042_check_aux())
1428 return -ENODEV;
1429
1430 if (i8042_nomux || i8042_check_mux()) {
1431 error = i8042_create_aux_port(-1);
1432 if (error)
1433 goto err_free_ports;
1434 aux_enable = i8042_enable_aux_port;
1435 } else {
1436 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1437 error = i8042_create_aux_port(i);
1438 if (error)
1439 goto err_free_ports;
1440 }
1441 aux_enable = i8042_enable_mux_ports;
1442 }
1443
1444 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1445 "i8042", i8042_platform_device);
1446 if (error)
1447 goto err_free_ports;
1448
1449 if (aux_enable())
1450 goto err_free_irq;
1451
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001452 i8042_aux_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001453 return 0;
1454
1455 err_free_irq:
1456 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1457 err_free_ports:
1458 i8042_free_aux_ports();
1459 return error;
1460}
1461
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001462static int __init i8042_setup_kbd(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001463{
1464 int error;
1465
1466 error = i8042_create_kbd_port();
1467 if (error)
1468 return error;
1469
1470 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1471 "i8042", i8042_platform_device);
1472 if (error)
1473 goto err_free_port;
1474
1475 error = i8042_enable_kbd_port();
1476 if (error)
1477 goto err_free_irq;
1478
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001479 i8042_kbd_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001480 return 0;
1481
1482 err_free_irq:
1483 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1484 err_free_port:
1485 i8042_free_kbd_port();
1486 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001489static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1490 unsigned long action, void *data)
1491{
1492 struct device *dev = data;
1493 struct serio *serio = to_serio_port(dev);
1494 struct i8042_port *port = serio->port_data;
1495
1496 if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1497 return 0;
1498
1499 switch (action) {
1500 case BUS_NOTIFY_BOUND_DRIVER:
1501 port->driver_bound = true;
1502 break;
1503
1504 case BUS_NOTIFY_UNBIND_DRIVER:
1505 port->driver_bound = false;
1506 break;
1507 }
1508
1509 return 0;
1510}
1511
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001512static int __init i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001514 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001516 i8042_platform_device = dev;
1517
Marcos Paulo de Souza930e19242016-10-01 12:07:35 -07001518 if (i8042_reset == I8042_RESET_ALWAYS) {
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001519 error = i8042_controller_selftest();
1520 if (error)
1521 return error;
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001524 error = i8042_controller_init();
1525 if (error)
1526 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Bruno Prémontd35895d2008-05-27 01:36:04 -04001528#ifdef CONFIG_X86
1529 if (i8042_dritek)
1530 i8042_dritek_enable();
1531#endif
1532
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001533 if (!i8042_noaux) {
1534 error = i8042_setup_aux();
1535 if (error && error != -ENODEV && error != -EBUSY)
1536 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001539 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001540 error = i8042_setup_kbd();
1541 if (error)
1542 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001543 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001544/*
1545 * Ok, everything is ready, let's register all serio ports
1546 */
1547 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001550
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001551 out_fail:
1552 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1553 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001554 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001555 i8042_platform_device = NULL;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001556
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001557 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Bill Pembertone2619cf2012-11-23 21:50:47 -08001560static int i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001562 i8042_unregister_ports();
1563 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001564 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001565 i8042_platform_device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001567 return 0;
1568}
1569
1570static struct platform_driver i8042_driver = {
1571 .driver = {
1572 .name = "i8042",
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001573#ifdef CONFIG_PM
1574 .pm = &i8042_pm_ops,
1575#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001576 },
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001577 .remove = i8042_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001578 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001579};
1580
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001581static struct notifier_block i8042_kbd_bind_notifier_block = {
1582 .notifier_call = i8042_kbd_bind_notifier,
1583};
1584
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001585static int __init i8042_init(void)
1586{
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001587 struct platform_device *pdev;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001588 int err;
1589
1590 dbg_init();
1591
1592 err = i8042_platform_init();
1593 if (err)
1594 return err;
1595
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001596 err = i8042_controller_check();
1597 if (err)
1598 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001599
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001600 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1601 if (IS_ERR(pdev)) {
1602 err = PTR_ERR(pdev);
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001603 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001604 }
1605
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001606 bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001607 panic_blink = i8042_panic_blink;
1608
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001609 return 0;
1610
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001611 err_platform_exit:
1612 i8042_platform_exit();
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001613 return err;
1614}
1615
1616static void __exit i8042_exit(void)
1617{
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001618 platform_device_unregister(i8042_platform_device);
Dmitry Torokhovaf045b82010-08-31 17:27:02 -07001619 platform_driver_unregister(&i8042_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 i8042_platform_exit();
1621
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001622 bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 panic_blink = NULL;
1624}
1625
1626module_init(i8042_init);
1627module_exit(i8042_exit);