blob: db91de539ee30bfc14612f0056f3979b884ff4aa [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/io.h>
29
30MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
31MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
32MODULE_LICENSE("GPL");
33
Dmitry Torokhov386b3842009-09-09 19:08:16 -070034static bool i8042_nokbd;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -050035module_param_named(nokbd, i8042_nokbd, bool, 0);
36MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
37
Dmitry Torokhov386b3842009-09-09 19:08:16 -070038static bool i8042_noaux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039module_param_named(noaux, i8042_noaux, bool, 0);
40MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
41
Dmitry Torokhove55a3362014-10-31 09:35:53 -070042static bool i8042_nomux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043module_param_named(nomux, i8042_nomux, bool, 0);
Dominik Brodowski2c860a12010-04-05 22:29:09 -070044MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Dmitry Torokhov386b3842009-09-09 19:08:16 -070046static bool i8042_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047module_param_named(unlock, i8042_unlock, bool, 0);
48MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
49
Dmitry Torokhov386b3842009-09-09 19:08:16 -070050static bool i8042_reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051module_param_named(reset, i8042_reset, bool, 0);
52MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
53
Dmitry Torokhov386b3842009-09-09 19:08:16 -070054static bool i8042_direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055module_param_named(direct, i8042_direct, bool, 0);
56MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
57
Dmitry Torokhov386b3842009-09-09 19:08:16 -070058static bool i8042_dumbkbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
60MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
61
Dmitry Torokhov386b3842009-09-09 19:08:16 -070062static bool i8042_noloop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063module_param_named(noloop, i8042_noloop, bool, 0);
64MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
65
Jiri Kosinaf8313ef2011-01-08 01:37:26 -080066static bool i8042_notimeout;
67module_param_named(notimeout, i8042_notimeout, bool, 0);
68MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
69
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -080070static bool i8042_kbdreset;
71module_param_named(kbdreset, i8042_kbdreset, bool, 0);
72MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
73
Carlos Corbacho8987fec2008-01-21 01:04:40 -050074#ifdef CONFIG_X86
Dmitry Torokhov386b3842009-09-09 19:08:16 -070075static bool i8042_dritek;
Carlos Corbacho8987fec2008-01-21 01:04:40 -050076module_param_named(dritek, i8042_dritek, bool, 0);
77MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
78#endif
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifdef CONFIG_PNP
Dmitry Torokhov386b3842009-09-09 19:08:16 -070081static bool i8042_nopnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082module_param_named(nopnp, i8042_nopnp, bool, 0);
83MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
84#endif
85
86#define DEBUG
87#ifdef DEBUG
Dmitry Torokhov386b3842009-09-09 19:08:16 -070088static bool i8042_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089module_param_named(debug, i8042_debug, bool, 0600);
90MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
Stephen Chandler Paule1443d22015-07-15 10:20:17 -070091
92static bool i8042_unmask_kbd_data;
93module_param_named(unmask_kbd_data, i8042_unmask_kbd_data, bool, 0600);
94MODULE_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 -070095#endif
96
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -070097static bool i8042_bypass_aux_irq_test;
Hans de Goedea7c58682014-04-19 20:47:35 -070098static char i8042_kbd_firmware_id[128];
99static char i8042_aux_firmware_id[128];
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include "i8042.h"
102
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700103/*
104 * i8042_lock protects serialization between i8042_command and
105 * the interrupt handler.
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static DEFINE_SPINLOCK(i8042_lock);
108
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700109/*
110 * Writers to AUX and KBD ports as well as users issuing i8042_command
111 * directly should acquire i8042_mutex (by means of calling
112 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
113 * they do not disturb each other (unfortunately in many i8042
114 * implementations write to one of the ports will immediately abort
115 * command that is being processed by another port).
116 */
117static DEFINE_MUTEX(i8042_mutex);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119struct i8042_port {
120 struct serio *serio;
121 int irq;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700122 bool exists;
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700123 bool driver_bound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 signed char mux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127#define I8042_KBD_PORT_NO 0
128#define I8042_AUX_PORT_NO 1
129#define I8042_MUX_PORT_NO 2
130#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400131
132static struct i8042_port i8042_ports[I8042_NUM_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134static unsigned char i8042_initial_ctr;
135static unsigned char i8042_ctr;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700136static bool i8042_mux_present;
137static bool i8042_kbd_irq_registered;
138static bool i8042_aux_irq_registered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400139static unsigned char i8042_suppress_kbd_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static struct platform_device *i8042_platform_device;
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700141static struct notifier_block i8042_kbd_bind_notifier_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
David Howells7d12e782006-10-05 14:55:46 +0100143static irqreturn_t i8042_interrupt(int irq, void *dev_id);
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800144static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
145 struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700147void i8042_lock_chip(void)
148{
149 mutex_lock(&i8042_mutex);
150}
151EXPORT_SYMBOL(i8042_lock_chip);
152
153void i8042_unlock_chip(void)
154{
155 mutex_unlock(&i8042_mutex);
156}
157EXPORT_SYMBOL(i8042_unlock_chip);
158
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800159int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
160 struct serio *serio))
161{
162 unsigned long flags;
163 int ret = 0;
164
165 spin_lock_irqsave(&i8042_lock, flags);
166
167 if (i8042_platform_filter) {
168 ret = -EBUSY;
169 goto out;
170 }
171
172 i8042_platform_filter = filter;
173
174out:
175 spin_unlock_irqrestore(&i8042_lock, flags);
176 return ret;
177}
178EXPORT_SYMBOL(i8042_install_filter);
179
180int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
181 struct serio *port))
182{
183 unsigned long flags;
184 int ret = 0;
185
186 spin_lock_irqsave(&i8042_lock, flags);
187
188 if (i8042_platform_filter != filter) {
189 ret = -EINVAL;
190 goto out;
191 }
192
193 i8042_platform_filter = NULL;
194
195out:
196 spin_unlock_irqrestore(&i8042_lock, flags);
197 return ret;
198}
199EXPORT_SYMBOL(i8042_remove_filter);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
203 * be ready for reading values from it / writing values to it.
204 * Called always with i8042_lock held.
205 */
206
207static int i8042_wait_read(void)
208{
209 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
212 udelay(50);
213 i++;
214 }
215 return -(i == I8042_CTL_TIMEOUT);
216}
217
218static int i8042_wait_write(void)
219{
220 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
223 udelay(50);
224 i++;
225 }
226 return -(i == I8042_CTL_TIMEOUT);
227}
228
229/*
230 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
231 * of the i8042 down the toilet.
232 */
233
234static int i8042_flush(void)
235{
236 unsigned long flags;
237 unsigned char data, str;
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700238 int count = 0;
239 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 spin_lock_irqsave(&i8042_lock, flags);
242
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700243 while ((str = i8042_read_status()) & I8042_STR_OBF) {
244 if (count++ < I8042_BUFFER_SIZE) {
245 udelay(50);
246 data = i8042_read_data();
247 dbg("%02x <- i8042 (flush, %s)\n",
248 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
249 } else {
250 retval = -EIO;
251 break;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 spin_unlock_irqrestore(&i8042_lock, flags);
256
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700257 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
261 * i8042_command() executes a command on the i8042. It also sends the input
262 * parameter(s) of the commands to it, and receives the output value(s). The
263 * parameters are to be stored in the param array, and the output is placed
264 * into the same array. The number of the parameters and output values is
265 * encoded in bits 8-11 of the command number.
266 */
267
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400268static int __i8042_command(unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400270 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
273 return -1;
274
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400275 error = i8042_wait_write();
276 if (error)
277 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500278
Joe Perches4eb3c302010-11-29 23:33:07 -0800279 dbg("%02x -> i8042 (command)\n", command & 0xff);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500280 i8042_write_command(command & 0xff);
281
282 for (i = 0; i < ((command >> 12) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400283 error = i8042_wait_write();
284 if (error)
285 return error;
Joe Perches4eb3c302010-11-29 23:33:07 -0800286 dbg("%02x -> i8042 (parameter)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500287 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500290 for (i = 0; i < ((command >> 8) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400291 error = i8042_wait_read();
292 if (error) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800293 dbg(" -- i8042 (timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400294 return error;
295 }
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500296
297 if (command == I8042_CMD_AUX_LOOP &&
298 !(i8042_read_status() & I8042_STR_AUXDATA)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800299 dbg(" -- i8042 (auxerr)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400300 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500303 param[i] = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800304 dbg("%02x <- i8042 (return)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400307 return 0;
308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Márton Németh553a05b2007-10-22 00:56:52 -0400310int i8042_command(unsigned char *param, int command)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400311{
312 unsigned long flags;
313 int retval;
314
315 spin_lock_irqsave(&i8042_lock, flags);
316 retval = __i8042_command(param, command);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500317 spin_unlock_irqrestore(&i8042_lock, flags);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return retval;
320}
Márton Németh553a05b2007-10-22 00:56:52 -0400321EXPORT_SYMBOL(i8042_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/*
324 * i8042_kbd_write() sends a byte out through the keyboard interface.
325 */
326
327static int i8042_kbd_write(struct serio *port, unsigned char c)
328{
329 unsigned long flags;
330 int retval = 0;
331
332 spin_lock_irqsave(&i8042_lock, flags);
333
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400334 if (!(retval = i8042_wait_write())) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800335 dbg("%02x -> i8042 (kbd-data)\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 i8042_write_data(c);
337 }
338
339 spin_unlock_irqrestore(&i8042_lock, flags);
340
341 return retval;
342}
343
344/*
345 * i8042_aux_write() sends a byte out through the aux interface.
346 */
347
348static int i8042_aux_write(struct serio *serio, unsigned char c)
349{
350 struct i8042_port *port = serio->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dmitry Torokhovf4e3c712006-11-02 23:27:49 -0500352 return i8042_command(&c, port->mux == -1 ?
353 I8042_CMD_AUX_SEND :
354 I8042_CMD_MUX_SEND + port->mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700357
358/*
359 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
360 * and then re-enabling it.
361 */
362
363static void i8042_port_close(struct serio *serio)
364{
365 int irq_bit;
366 int disable_bit;
367 const char *port_name;
368
369 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
370 irq_bit = I8042_CTR_AUXINT;
371 disable_bit = I8042_CTR_AUXDIS;
372 port_name = "AUX";
373 } else {
374 irq_bit = I8042_CTR_KBDINT;
375 disable_bit = I8042_CTR_KBDDIS;
376 port_name = "KBD";
377 }
378
379 i8042_ctr &= ~irq_bit;
380 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800381 pr_warn("Can't write CTR while closing %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700382
383 udelay(50);
384
385 i8042_ctr &= ~disable_bit;
386 i8042_ctr |= irq_bit;
387 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800388 pr_err("Can't reactivate %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700389
390 /*
391 * See if there is any data appeared while we were messing with
392 * port state.
393 */
394 i8042_interrupt(0, NULL);
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * i8042_start() is called by serio core when port is about to finish
399 * registering. It will mark port as existing so i8042_interrupt can
400 * start sending data through it.
401 */
402static int i8042_start(struct serio *serio)
403{
404 struct i8042_port *port = serio->port_data;
405
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700406 port->exists = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 mb();
408 return 0;
409}
410
411/*
412 * i8042_stop() marks serio port as non-existing so i8042_interrupt
413 * will not try to send data to the port that is about to go away.
414 * The function is called by serio core as part of unregister procedure.
415 */
416static void i8042_stop(struct serio *serio)
417{
418 struct i8042_port *port = serio->port_data;
419
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700420 port->exists = false;
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400421
422 /*
423 * We synchronize with both AUX and KBD IRQs because there is
424 * a (very unlikely) chance that AUX IRQ is raised for KBD port
425 * and vice versa.
426 */
427 synchronize_irq(I8042_AUX_IRQ);
428 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 port->serio = NULL;
430}
431
432/*
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800433 * i8042_filter() filters out unwanted bytes from the input data stream.
434 * It is called from i8042_interrupt and thus is running with interrupts
435 * off and i8042_lock held.
436 */
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800437static bool i8042_filter(unsigned char data, unsigned char str,
438 struct serio *serio)
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800439{
440 if (unlikely(i8042_suppress_kbd_ack)) {
441 if ((~str & I8042_STR_AUXDATA) &&
442 (data == 0xfa || data == 0xfe)) {
443 i8042_suppress_kbd_ack--;
444 dbg("Extra keyboard ACK - filtered out\n");
445 return true;
446 }
447 }
448
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800449 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
Stefan Weil0747e3b2010-01-07 00:44:08 +0100450 dbg("Filtered out by platform filter\n");
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800451 return true;
452 }
453
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800454 return false;
455}
456
457/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * i8042_interrupt() is the most important function in this driver -
459 * it handles the interrupts from the i8042, and sends incoming bytes
460 * to the upper layers.
461 */
462
David Howells7d12e782006-10-05 14:55:46 +0100463static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct i8042_port *port;
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800466 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 unsigned long flags;
468 unsigned char str, data;
469 unsigned int dfl;
470 unsigned int port_no;
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800471 bool filtered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400472 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 spin_lock_irqsave(&i8042_lock, flags);
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 str = i8042_read_status();
477 if (unlikely(~str & I8042_STR_OBF)) {
478 spin_unlock_irqrestore(&i8042_lock, flags);
Joe Perches4eb3c302010-11-29 23:33:07 -0800479 if (irq)
480 dbg("Interrupt %d, without any data\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ret = 0;
482 goto out;
483 }
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 data = i8042_read_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
488 static unsigned long last_transmit;
489 static unsigned char last_str;
490
491 dfl = 0;
492 if (str & I8042_STR_MUXERR) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800493 dbg("MUX error, status is %02x, data is %02x\n",
494 str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*
496 * When MUXERR condition is signalled the data register can only contain
497 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500498 * it is not always the case. Some KBCs also report 0xfc when there is
499 * nothing connected to the port while others sometimes get confused which
500 * port the data came from and signal error leaving the data intact. They
501 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
502 * to legacy mode yet, when we see one we'll add proper handling).
503 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
504 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * was transmitted (if transmission happened not too long ago).
506 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500507
508 switch (data) {
509 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (time_before(jiffies, last_transmit + HZ/10)) {
511 str = last_str;
512 break;
513 }
514 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500515 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 case 0xfd:
517 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
518 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
519 }
520 }
521
522 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
523 last_str = str;
524 last_transmit = jiffies;
525 } else {
526
527 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
Jiri Kosinaf8313ef2011-01-08 01:37:26 -0800528 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 port_no = (str & I8042_STR_AUXDATA) ?
531 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
532 }
533
534 port = &i8042_ports[port_no];
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800535 serio = port->exists ? port->serio : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700537 filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
538 port_no, irq,
539 dfl & SERIO_PARITY ? ", bad parity" : "",
540 dfl & SERIO_TIMEOUT ? ", timeout" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800542 filtered = i8042_filter(data, str, serio);
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400543
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800544 spin_unlock_irqrestore(&i8042_lock, flags);
545
546 if (likely(port->exists && !filtered))
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800547 serio_interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500549 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return IRQ_RETVAL(ret);
551}
552
553/*
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700554 * i8042_enable_kbd_port enables keyboard port on chip
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400555 */
556
557static int i8042_enable_kbd_port(void)
558{
559 i8042_ctr &= ~I8042_CTR_KBDDIS;
560 i8042_ctr |= I8042_CTR_KBDINT;
561
562 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400563 i8042_ctr &= ~I8042_CTR_KBDINT;
564 i8042_ctr |= I8042_CTR_KBDDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800565 pr_err("Failed to enable KBD port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400566 return -EIO;
567 }
568
569 return 0;
570}
571
572/*
573 * i8042_enable_aux_port enables AUX (mouse) port on chip
574 */
575
576static int i8042_enable_aux_port(void)
577{
578 i8042_ctr &= ~I8042_CTR_AUXDIS;
579 i8042_ctr |= I8042_CTR_AUXINT;
580
581 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400582 i8042_ctr &= ~I8042_CTR_AUXINT;
583 i8042_ctr |= I8042_CTR_AUXDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800584 pr_err("Failed to enable AUX port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400585 return -EIO;
586 }
587
588 return 0;
589}
590
591/*
592 * i8042_enable_mux_ports enables 4 individual AUX ports after
593 * the controller has been switched into Multiplexed mode
594 */
595
596static int i8042_enable_mux_ports(void)
597{
598 unsigned char param;
599 int i;
600
601 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
602 i8042_command(&param, I8042_CMD_MUX_PFX + i);
603 i8042_command(&param, I8042_CMD_AUX_ENABLE);
604 }
605
606 return i8042_enable_aux_port();
607}
608
609/*
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700610 * i8042_set_mux_mode checks whether the controller has an
611 * active multiplexor and puts the chip into Multiplexed (true)
612 * or Legacy (false) mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
614
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700615static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700618 unsigned char param, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * Get rid of bytes in the queue.
621 */
622
623 i8042_flush();
624
625/*
626 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400627 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700630 param = val = 0xf0;
631 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700633 param = val = multiplex ? 0x56 : 0xf6;
634 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700636 param = val = multiplex ? 0xa4 : 0xa5;
637 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
638 return -1;
639
640/*
641 * Workaround for interference with USB Legacy emulation
642 * that causes a v10.12 MUX to be found.
643 */
644 if (param == 0xac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -1;
646
647 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500648 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return 0;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*
654 * i8042_check_mux() checks whether the controller supports the PS/2 Active
655 * Multiplexing specification by Synaptics, Phoenix, Insyde and
656 * LCS/Telegraphics.
657 */
658
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700659static int __init i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 unsigned char mux_version;
662
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700663 if (i8042_set_mux_mode(true, &mux_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return -1;
665
Joe Perches4eb3c302010-11-29 23:33:07 -0800666 pr_info("Detected active multiplexing controller, rev %d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 (mux_version >> 4) & 0xf, mux_version & 0xf);
668
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400669/*
670 * Disable all muxed ports by disabling AUX.
671 */
672 i8042_ctr |= I8042_CTR_AUXDIS;
673 i8042_ctr &= ~I8042_CTR_AUXINT;
674
675 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800676 pr_err("Failed to disable AUX port, can't use MUX\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400677 return -EIO;
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700680 i8042_mux_present = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683}
684
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400685/*
686 * The following is used to test AUX IRQ delivery.
687 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700688static struct completion i8042_aux_irq_delivered __initdata;
689static bool i8042_irq_being_tested __initdata;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400690
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700691static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400692{
693 unsigned long flags;
694 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400695 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400696
697 spin_lock_irqsave(&i8042_lock, flags);
698 str = i8042_read_status();
699 if (str & I8042_STR_OBF) {
700 data = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800701 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
702 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400703 if (i8042_irq_being_tested &&
704 data == 0xa5 && (str & I8042_STR_AUXDATA))
705 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400706 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400707 }
708 spin_unlock_irqrestore(&i8042_lock, flags);
709
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400710 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400711}
712
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400713/*
714 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
715 * verifies success by readinng CTR. Used when testing for presence of AUX
716 * port.
717 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700718static int __init i8042_toggle_aux(bool on)
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400719{
720 unsigned char param;
721 int i;
722
723 if (i8042_command(&param,
724 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
725 return -1;
726
727 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
728 for (i = 0; i < 100; i++) {
729 udelay(50);
730
731 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
732 return -1;
733
734 if (!(param & I8042_CTR_AUXDIS) == on)
735 return 0;
736 }
737
738 return -1;
739}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741/*
742 * i8042_check_aux() applies as much paranoia as it can at detecting
743 * the presence of an AUX interface.
744 */
745
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700746static int __init i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400748 int retval = -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700749 bool irq_registered = false;
750 bool aux_loop_broken = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400751 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754/*
755 * Get rid of bytes in the queue.
756 */
757
758 i8042_flush();
759
760/*
761 * Internal loopback test - filters out AT-type i8042's. Unfortunately
762 * SiS screwed up and their 5597 doesn't support the LOOP command even
763 * though it has an AUX port.
764 */
765
766 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500767 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
768 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770/*
771 * External connection test - filters out AT-soldered PS/2 i8042's
772 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
773 * 0xfa - no error on some notebooks which ignore the spec
774 * Because it's common for chipsets to return error on perfectly functioning
775 * AUX ports, we test for this only when the LOOP command failed.
776 */
777
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400778 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
779 (param && param != 0xfa && param != 0xff))
780 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500781
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500782/*
783 * If AUX_LOOP completed without error but returned unexpected data
784 * mark it as broken
785 */
786 if (!retval)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700787 aux_loop_broken = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790/*
791 * Bit assignment test - filters out PS/2 i8042's in AT mode
792 */
793
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700794 if (i8042_toggle_aux(false)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800795 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
796 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700799 if (i8042_toggle_aux(true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -1;
801
802/*
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -0800803 * Reset keyboard (needed on some laptops to successfully detect
804 * touchpad, e.g., some Gigabyte laptop models with Elantech
805 * touchpads).
806 */
807 if (i8042_kbdreset) {
808 pr_warn("Attempting to reset device connected to KBD port\n");
809 i8042_kbd_write(NULL, (unsigned char) 0xff);
810 }
811
812/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400813 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
814 * used it for a PCI card or somethig else.
815 */
816
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700817 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400818/*
819 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
820 * is working and hope we are right.
821 */
822 retval = 0;
823 goto out;
824 }
825
826 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
827 "i8042", i8042_platform_device))
828 goto out;
829
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700830 irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400831
832 if (i8042_enable_aux_port())
833 goto out;
834
835 spin_lock_irqsave(&i8042_lock, flags);
836
837 init_completion(&i8042_aux_irq_delivered);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700838 i8042_irq_being_tested = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400839
840 param = 0xa5;
841 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
842
843 spin_unlock_irqrestore(&i8042_lock, flags);
844
845 if (retval)
846 goto out;
847
848 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
849 msecs_to_jiffies(250)) == 0) {
850/*
851 * AUX IRQ was never delivered so we need to flush the controller to
852 * get rid of the byte we put there; otherwise keyboard may not work.
853 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800854 dbg(" -- i8042 (aux irq test timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400855 i8042_flush();
856 retval = -1;
857 }
858
859 out:
860
861/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * Disable the interface.
863 */
864
865 i8042_ctr |= I8042_CTR_AUXDIS;
866 i8042_ctr &= ~I8042_CTR_AUXINT;
867
868 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400869 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400871 if (irq_registered)
872 free_irq(I8042_AUX_IRQ, i8042_platform_device);
873
874 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400877static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700879 if (i8042_flush()) {
Takashi Iwaif5d75342015-09-05 10:29:09 -0700880 pr_info("No controller found\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400881 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 0;
885}
886
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400887static int i8042_controller_selftest(void)
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500888{
889 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700890 int i = 0;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500891
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700892 /*
893 * We try this 5 times; on some really fragile systems this does not
894 * take the first time...
895 */
896 do {
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500897
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700898 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
Paul Bollea2a94e72011-03-31 00:11:48 -0700899 pr_err("i8042 controller selftest timeout\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700900 return -ENODEV;
901 }
902
903 if (param == I8042_RET_CTL_TEST)
904 return 0;
905
Paul Bollea2a94e72011-03-31 00:11:48 -0700906 dbg("i8042 controller selftest: %#x != %#x\n",
907 param, I8042_RET_CTL_TEST);
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700908 msleep(50);
909 } while (i++ < 5);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500910
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700911#ifdef CONFIG_X86
912 /*
913 * On x86, we don't fail entire i8042 initialization if controller
914 * reset fails in hopes that keyboard port will still be functional
915 * and user will still get a working keyboard. This is especially
916 * important on netbooks. On other arches we trust hardware more.
917 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800918 pr_info("giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500919 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700920#else
Paul Bollea2a94e72011-03-31 00:11:48 -0700921 pr_err("i8042 controller selftest failed\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700922 return -EIO;
923#endif
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500924}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926/*
927 * i8042_controller init initializes the i8042 controller, and,
928 * most importantly, sets it into non-xlated mode if that's
929 * desired.
930 */
931
932static int i8042_controller_init(void)
933{
934 unsigned long flags;
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800935 int n = 0;
936 unsigned char ctr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938/*
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800939 * Save the CTR for restore on unload / reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
941
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800942 do {
943 if (n >= 10) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800944 pr_err("Unable to get stable CTR read\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800945 return -EIO;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800948 if (n != 0)
949 udelay(50);
950
951 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800952 pr_err("Can't read CTR while initializing i8042\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800953 return -EIO;
954 }
955
956 } while (n < 2 || ctr[0] != ctr[1]);
957
958 i8042_initial_ctr = i8042_ctr = ctr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960/*
961 * Disable the keyboard interface and interrupt.
962 */
963
964 i8042_ctr |= I8042_CTR_KBDDIS;
965 i8042_ctr &= ~I8042_CTR_KBDINT;
966
967/*
968 * Handle keylock.
969 */
970
971 spin_lock_irqsave(&i8042_lock, flags);
972 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
973 if (i8042_unlock)
974 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500975 else
Joe Perches4eb3c302010-11-29 23:33:07 -0800976 pr_warn("Warning: Keylock active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 spin_unlock_irqrestore(&i8042_lock, flags);
979
980/*
981 * If the chip is configured into nontranslated mode by the BIOS, don't
982 * bother enabling translating and be happy.
983 */
984
985 if (~i8042_ctr & I8042_CTR_XLATE)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700986 i8042_direct = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988/*
989 * Set nontranslated mode for the kbd interface if requested by an option.
990 * After this the kbd interface becomes a simple serial in/out, like the aux
991 * interface is. We don't do this by default, since it can confuse notebook
992 * BIOSes.
993 */
994
995 if (i8042_direct)
996 i8042_ctr &= ~I8042_CTR_XLATE;
997
998/*
999 * Write CTR back.
1000 */
1001
1002 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001003 pr_err("Can't write CTR while initializing i8042\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001004 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001007/*
1008 * Flush whatever accumulated while we were disabling keyboard port.
1009 */
1010
1011 i8042_flush();
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014}
1015
1016
1017/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001018 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
1020
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001021static void i8042_controller_reset(bool force_reset)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001022{
1023 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001026 * Disable both KBD and AUX interfaces so they don't get in the way
1027 */
1028
1029 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1030 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1031
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001032 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001033 pr_warn("Can't write CTR while resetting\n");
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001034
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001035/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * Disable MUX mode if present.
1037 */
1038
1039 if (i8042_mux_present)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001040 i8042_set_mux_mode(false, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001043 * Reset the controller if requested.
1044 */
1045
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001046 if (i8042_reset || force_reset)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001047 i8042_controller_selftest();
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001048
1049/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 * Restore the original control register setting.
1051 */
1052
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001053 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001054 pr_warn("Can't restore CTR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057
1058/*
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001059 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1060 * when kernel panics. Flashing LEDs is useful for users running X who may
Michael Opdenackeraa5e5dc2013-09-18 06:00:43 +02001061 * not see the console and will help distinguishing panics from "real"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * lockups.
1063 *
1064 * Note that DELAY has a limit of 10ms so we will not get stuck here
1065 * waiting for KBC to free up even if KBD interrupt is off
1066 */
1067
1068#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1069
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001070static long i8042_panic_blink(int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 long delay = 0;
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001073 char led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001075 led = (state) ? 0x01 | 0x04 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 while (i8042_read_status() & I8042_STR_IBF)
1077 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001078 dbg("%02x -> i8042 (panic blink)\n", 0xed);
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -05001079 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 i8042_write_data(0xed); /* set leds */
1081 DELAY;
1082 while (i8042_read_status() & I8042_STR_IBF)
1083 DELAY;
1084 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001085 dbg("%02x -> i8042 (panic blink)\n", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 i8042_write_data(led);
1087 DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return delay;
1089}
1090
1091#undef DELAY
1092
Bruno Prémontd35895d2008-05-27 01:36:04 -04001093#ifdef CONFIG_X86
1094static void i8042_dritek_enable(void)
1095{
Christoph Fritz594d6362010-09-29 18:04:21 -07001096 unsigned char param = 0x90;
Bruno Prémontd35895d2008-05-27 01:36:04 -04001097 int error;
1098
1099 error = i8042_command(&param, 0x1059);
1100 if (error)
Joe Perches4eb3c302010-11-29 23:33:07 -08001101 pr_warn("Failed to enable DRITEK extension: %d\n", error);
Bruno Prémontd35895d2008-05-27 01:36:04 -04001102}
1103#endif
1104
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001105#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001108 * Here we try to reset everything back to a state we had
1109 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
1111
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001112static int i8042_controller_resume(bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001114 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001116 error = i8042_controller_check();
1117 if (error)
1118 return error;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001119
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001120 if (i8042_reset || force_reset) {
1121 error = i8042_controller_selftest();
1122 if (error)
1123 return error;
1124 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001125
1126/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001127 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001128 */
1129
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001130 i8042_ctr = i8042_initial_ctr;
1131 if (i8042_direct)
1132 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001133 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1134 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001135 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001136 pr_warn("Can't write CTR to resume, retrying...\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001137 msleep(50);
1138 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001139 pr_err("CTR write retry failed\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001140 return -EIO;
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Bruno Prémontd35895d2008-05-27 01:36:04 -04001144
1145#ifdef CONFIG_X86
1146 if (i8042_dritek)
1147 i8042_dritek_enable();
1148#endif
1149
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001150 if (i8042_mux_present) {
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001151 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
Joe Perches4eb3c302010-11-29 23:33:07 -08001152 pr_warn("failed to resume active multiplexor, mouse won't work\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001153 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1154 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001156 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1157 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
David Howells7d12e782006-10-05 14:55:46 +01001159 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001163
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001164/*
1165 * Here we try to restore the original BIOS settings to avoid
1166 * upsetting it.
1167 */
1168
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001169static int i8042_pm_suspend(struct device *dev)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001170{
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001171 int i;
1172
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001173 i8042_controller_reset(true);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001174
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001175 /* Set up serio interrupts for system wakeup. */
1176 for (i = 0; i < I8042_NUM_PORTS; i++) {
1177 struct serio *serio = i8042_ports[i].serio;
1178
1179 if (serio && device_may_wakeup(&serio->dev))
1180 enable_irq_wake(i8042_ports[i].irq);
1181 }
1182
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001183 return 0;
1184}
1185
1186static int i8042_pm_resume(struct device *dev)
1187{
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001188 int i;
1189
1190 for (i = 0; i < I8042_NUM_PORTS; i++) {
1191 struct serio *serio = i8042_ports[i].serio;
1192
1193 if (serio && device_may_wakeup(&serio->dev))
1194 disable_irq_wake(i8042_ports[i].irq);
1195 }
1196
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001197 /*
1198 * On resume from S2R we always try to reset the controller
1199 * to bring it in a sane state. (In case of S2D we expect
1200 * BIOS to reset the controller for us.)
1201 */
1202 return i8042_controller_resume(true);
1203}
1204
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001205static int i8042_pm_thaw(struct device *dev)
1206{
1207 i8042_interrupt(0, NULL);
1208
1209 return 0;
1210}
1211
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001212static int i8042_pm_reset(struct device *dev)
1213{
1214 i8042_controller_reset(false);
1215
1216 return 0;
1217}
1218
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001219static int i8042_pm_restore(struct device *dev)
1220{
1221 return i8042_controller_resume(false);
1222}
1223
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001224static const struct dev_pm_ops i8042_pm_ops = {
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001225 .suspend = i8042_pm_suspend,
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001226 .resume = i8042_pm_resume,
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001227 .thaw = i8042_pm_thaw,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001228 .poweroff = i8042_pm_reset,
1229 .restore = i8042_pm_restore,
1230};
1231
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001232#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234/*
1235 * We need to reset the 8042 back to original mode on system shutdown,
1236 * because otherwise BIOSes will be confused.
1237 */
1238
Russell King3ae5eae2005-11-09 22:32:44 +00001239static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001241 i8042_controller_reset(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001244static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct serio *serio;
1247 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1248
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001249 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001250 if (!serio)
1251 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001253 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1254 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001255 serio->start = i8042_start;
1256 serio->stop = i8042_stop;
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001257 serio->close = i8042_port_close;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001258 serio->port_data = port;
1259 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001260 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001261 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001262 strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1263 sizeof(serio->firmware_id));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001264
1265 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001266 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001267
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001271static int __init i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001274 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1275 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001277 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001278 if (!serio)
1279 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001281 serio->id.type = SERIO_8042;
1282 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001283 serio->start = i8042_start;
1284 serio->stop = i8042_stop;
1285 serio->port_data = port;
1286 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001287 if (idx < 0) {
1288 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1289 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001290 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1291 sizeof(serio->firmware_id));
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001292 serio->close = i8042_port_close;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001293 } else {
1294 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1295 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
Hans de Goede266e43c2014-09-11 10:13:13 -07001296 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1297 sizeof(serio->firmware_id));
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001298 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001299
1300 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001301 port->mux = idx;
1302 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001303
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001307static void __init i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001309 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1310 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1311}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001313static void __init i8042_free_aux_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001314{
1315 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001317 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1318 kfree(i8042_ports[i].serio);
1319 i8042_ports[i].serio = NULL;
1320 }
1321}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001322
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001323static void __init i8042_register_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001324{
1325 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001326
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001327 for (i = 0; i < I8042_NUM_PORTS; i++) {
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001328 struct serio *serio = i8042_ports[i].serio;
1329
1330 if (serio) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001331 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001332 serio->name,
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001333 (unsigned long) I8042_DATA_REG,
1334 (unsigned long) I8042_COMMAND_REG,
1335 i8042_ports[i].irq);
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001336 serio_register_port(serio);
1337 device_set_wakeup_capable(&serio->dev, true);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001338 }
1339 }
1340}
1341
Bill Pembertone2619cf2012-11-23 21:50:47 -08001342static void i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001343{
1344 int i;
1345
1346 for (i = 0; i < I8042_NUM_PORTS; i++) {
1347 if (i8042_ports[i].serio) {
1348 serio_unregister_port(i8042_ports[i].serio);
1349 i8042_ports[i].serio = NULL;
1350 }
1351 }
1352}
1353
Dmitry Torokhov181d6832009-09-16 01:06:43 -07001354/*
1355 * Checks whether port belongs to i8042 controller.
1356 */
1357bool i8042_check_port_owner(const struct serio *port)
1358{
1359 int i;
1360
1361 for (i = 0; i < I8042_NUM_PORTS; i++)
1362 if (i8042_ports[i].serio == port)
1363 return true;
1364
1365 return false;
1366}
1367EXPORT_SYMBOL(i8042_check_port_owner);
1368
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001369static void i8042_free_irqs(void)
1370{
1371 if (i8042_aux_irq_registered)
1372 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1373 if (i8042_kbd_irq_registered)
1374 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1375
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001376 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001377}
1378
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001379static int __init i8042_setup_aux(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001380{
1381 int (*aux_enable)(void);
1382 int error;
1383 int i;
1384
1385 if (i8042_check_aux())
1386 return -ENODEV;
1387
1388 if (i8042_nomux || i8042_check_mux()) {
1389 error = i8042_create_aux_port(-1);
1390 if (error)
1391 goto err_free_ports;
1392 aux_enable = i8042_enable_aux_port;
1393 } else {
1394 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1395 error = i8042_create_aux_port(i);
1396 if (error)
1397 goto err_free_ports;
1398 }
1399 aux_enable = i8042_enable_mux_ports;
1400 }
1401
1402 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1403 "i8042", i8042_platform_device);
1404 if (error)
1405 goto err_free_ports;
1406
1407 if (aux_enable())
1408 goto err_free_irq;
1409
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001410 i8042_aux_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001411 return 0;
1412
1413 err_free_irq:
1414 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1415 err_free_ports:
1416 i8042_free_aux_ports();
1417 return error;
1418}
1419
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001420static int __init i8042_setup_kbd(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001421{
1422 int error;
1423
1424 error = i8042_create_kbd_port();
1425 if (error)
1426 return error;
1427
1428 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1429 "i8042", i8042_platform_device);
1430 if (error)
1431 goto err_free_port;
1432
1433 error = i8042_enable_kbd_port();
1434 if (error)
1435 goto err_free_irq;
1436
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001437 i8042_kbd_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001438 return 0;
1439
1440 err_free_irq:
1441 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1442 err_free_port:
1443 i8042_free_kbd_port();
1444 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001447static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1448 unsigned long action, void *data)
1449{
1450 struct device *dev = data;
1451 struct serio *serio = to_serio_port(dev);
1452 struct i8042_port *port = serio->port_data;
1453
1454 if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1455 return 0;
1456
1457 switch (action) {
1458 case BUS_NOTIFY_BOUND_DRIVER:
1459 port->driver_bound = true;
1460 break;
1461
1462 case BUS_NOTIFY_UNBIND_DRIVER:
1463 port->driver_bound = false;
1464 break;
1465 }
1466
1467 return 0;
1468}
1469
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001470static int __init i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001472 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001474 i8042_platform_device = dev;
1475
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001476 if (i8042_reset) {
1477 error = i8042_controller_selftest();
1478 if (error)
1479 return error;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001482 error = i8042_controller_init();
1483 if (error)
1484 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Bruno Prémontd35895d2008-05-27 01:36:04 -04001486#ifdef CONFIG_X86
1487 if (i8042_dritek)
1488 i8042_dritek_enable();
1489#endif
1490
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001491 if (!i8042_noaux) {
1492 error = i8042_setup_aux();
1493 if (error && error != -ENODEV && error != -EBUSY)
1494 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001497 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001498 error = i8042_setup_kbd();
1499 if (error)
1500 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001501 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001502/*
1503 * Ok, everything is ready, let's register all serio ports
1504 */
1505 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001508
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001509 out_fail:
1510 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1511 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001512 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001513 i8042_platform_device = NULL;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001514
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001515 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Bill Pembertone2619cf2012-11-23 21:50:47 -08001518static int i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001520 i8042_unregister_ports();
1521 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001522 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001523 i8042_platform_device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001525 return 0;
1526}
1527
1528static struct platform_driver i8042_driver = {
1529 .driver = {
1530 .name = "i8042",
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001531#ifdef CONFIG_PM
1532 .pm = &i8042_pm_ops,
1533#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001534 },
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001535 .remove = i8042_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001536 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001537};
1538
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001539static struct notifier_block i8042_kbd_bind_notifier_block = {
1540 .notifier_call = i8042_kbd_bind_notifier,
1541};
1542
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001543static int __init i8042_init(void)
1544{
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001545 struct platform_device *pdev;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001546 int err;
1547
1548 dbg_init();
1549
1550 err = i8042_platform_init();
1551 if (err)
1552 return err;
1553
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001554 err = i8042_controller_check();
1555 if (err)
1556 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001557
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001558 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1559 if (IS_ERR(pdev)) {
1560 err = PTR_ERR(pdev);
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001561 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001562 }
1563
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001564 bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001565 panic_blink = i8042_panic_blink;
1566
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001567 return 0;
1568
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001569 err_platform_exit:
1570 i8042_platform_exit();
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001571 return err;
1572}
1573
1574static void __exit i8042_exit(void)
1575{
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001576 platform_device_unregister(i8042_platform_device);
Dmitry Torokhovaf045b82010-08-31 17:27:02 -07001577 platform_driver_unregister(&i8042_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 i8042_platform_exit();
1579
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001580 bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 panic_blink = NULL;
1582}
1583
1584module_init(i8042_init);
1585module_exit(i8042_exit);