blob: 405252a884dd41e233da4399ab109f213becb85a [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
Dmitry Torokhov386b3842009-09-09 19:08:16 -070051static bool i8042_reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052module_param_named(reset, i8042_reset, bool, 0);
53MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
54
Dmitry Torokhov386b3842009-09-09 19:08:16 -070055static bool i8042_direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056module_param_named(direct, i8042_direct, bool, 0);
57MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
58
Dmitry Torokhov386b3842009-09-09 19:08:16 -070059static bool i8042_dumbkbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
61MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
62
Dmitry Torokhov386b3842009-09-09 19:08:16 -070063static bool i8042_noloop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064module_param_named(noloop, i8042_noloop, bool, 0);
65MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
66
Jiri Kosinaf8313ef2011-01-08 01:37:26 -080067static bool i8042_notimeout;
68module_param_named(notimeout, i8042_notimeout, bool, 0);
69MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
70
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -080071static bool i8042_kbdreset;
72module_param_named(kbdreset, i8042_kbdreset, bool, 0);
73MODULE_PARM_DESC(kbdreset, "Reset device connected to KBD port");
74
Carlos Corbacho8987fec2008-01-21 01:04:40 -050075#ifdef CONFIG_X86
Dmitry Torokhov386b3842009-09-09 19:08:16 -070076static bool i8042_dritek;
Carlos Corbacho8987fec2008-01-21 01:04:40 -050077module_param_named(dritek, i8042_dritek, bool, 0);
78MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
79#endif
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#ifdef CONFIG_PNP
Dmitry Torokhov386b3842009-09-09 19:08:16 -070082static bool i8042_nopnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083module_param_named(nopnp, i8042_nopnp, bool, 0);
84MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
85#endif
86
87#define DEBUG
88#ifdef DEBUG
Dmitry Torokhov386b3842009-09-09 19:08:16 -070089static bool i8042_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090module_param_named(debug, i8042_debug, bool, 0600);
91MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
Stephen Chandler Paule1443d22015-07-15 10:20:17 -070092
93static bool i8042_unmask_kbd_data;
94module_param_named(unmask_kbd_data, i8042_unmask_kbd_data, bool, 0600);
95MODULE_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 -070096#endif
97
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -070098static bool i8042_bypass_aux_irq_test;
Hans de Goedea7c58682014-04-19 20:47:35 -070099static char i8042_kbd_firmware_id[128];
100static char i8042_aux_firmware_id[128];
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include "i8042.h"
103
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700104/*
105 * i8042_lock protects serialization between i8042_command and
106 * the interrupt handler.
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static DEFINE_SPINLOCK(i8042_lock);
109
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700110/*
111 * Writers to AUX and KBD ports as well as users issuing i8042_command
112 * directly should acquire i8042_mutex (by means of calling
113 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
114 * they do not disturb each other (unfortunately in many i8042
115 * implementations write to one of the ports will immediately abort
116 * command that is being processed by another port).
117 */
118static DEFINE_MUTEX(i8042_mutex);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120struct i8042_port {
121 struct serio *serio;
122 int irq;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700123 bool exists;
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700124 bool driver_bound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 signed char mux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128#define I8042_KBD_PORT_NO 0
129#define I8042_AUX_PORT_NO 1
130#define I8042_MUX_PORT_NO 2
131#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400132
133static struct i8042_port i8042_ports[I8042_NUM_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static unsigned char i8042_initial_ctr;
136static unsigned char i8042_ctr;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700137static bool i8042_mux_present;
138static bool i8042_kbd_irq_registered;
139static bool i8042_aux_irq_registered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400140static unsigned char i8042_suppress_kbd_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct platform_device *i8042_platform_device;
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700142static struct notifier_block i8042_kbd_bind_notifier_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
David Howells7d12e782006-10-05 14:55:46 +0100144static irqreturn_t i8042_interrupt(int irq, void *dev_id);
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800145static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
146 struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700148void i8042_lock_chip(void)
149{
150 mutex_lock(&i8042_mutex);
151}
152EXPORT_SYMBOL(i8042_lock_chip);
153
154void i8042_unlock_chip(void)
155{
156 mutex_unlock(&i8042_mutex);
157}
158EXPORT_SYMBOL(i8042_unlock_chip);
159
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800160int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
161 struct serio *serio))
162{
163 unsigned long flags;
164 int ret = 0;
165
166 spin_lock_irqsave(&i8042_lock, flags);
167
168 if (i8042_platform_filter) {
169 ret = -EBUSY;
170 goto out;
171 }
172
173 i8042_platform_filter = filter;
174
175out:
176 spin_unlock_irqrestore(&i8042_lock, flags);
177 return ret;
178}
179EXPORT_SYMBOL(i8042_install_filter);
180
181int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
182 struct serio *port))
183{
184 unsigned long flags;
185 int ret = 0;
186
187 spin_lock_irqsave(&i8042_lock, flags);
188
189 if (i8042_platform_filter != filter) {
190 ret = -EINVAL;
191 goto out;
192 }
193
194 i8042_platform_filter = NULL;
195
196out:
197 spin_unlock_irqrestore(&i8042_lock, flags);
198 return ret;
199}
200EXPORT_SYMBOL(i8042_remove_filter);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
203 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
204 * be ready for reading values from it / writing values to it.
205 * Called always with i8042_lock held.
206 */
207
208static int i8042_wait_read(void)
209{
210 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
213 udelay(50);
214 i++;
215 }
216 return -(i == I8042_CTL_TIMEOUT);
217}
218
219static int i8042_wait_write(void)
220{
221 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
224 udelay(50);
225 i++;
226 }
227 return -(i == I8042_CTL_TIMEOUT);
228}
229
230/*
231 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
232 * of the i8042 down the toilet.
233 */
234
235static int i8042_flush(void)
236{
237 unsigned long flags;
238 unsigned char data, str;
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700239 int count = 0;
240 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 spin_lock_irqsave(&i8042_lock, flags);
243
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700244 while ((str = i8042_read_status()) & I8042_STR_OBF) {
245 if (count++ < I8042_BUFFER_SIZE) {
246 udelay(50);
247 data = i8042_read_data();
248 dbg("%02x <- i8042 (flush, %s)\n",
249 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
250 } else {
251 retval = -EIO;
252 break;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
256 spin_unlock_irqrestore(&i8042_lock, flags);
257
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700258 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/*
262 * i8042_command() executes a command on the i8042. It also sends the input
263 * parameter(s) of the commands to it, and receives the output value(s). The
264 * parameters are to be stored in the param array, and the output is placed
265 * into the same array. The number of the parameters and output values is
266 * encoded in bits 8-11 of the command number.
267 */
268
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400269static int __i8042_command(unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400271 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
274 return -1;
275
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400276 error = i8042_wait_write();
277 if (error)
278 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500279
Joe Perches4eb3c302010-11-29 23:33:07 -0800280 dbg("%02x -> i8042 (command)\n", command & 0xff);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500281 i8042_write_command(command & 0xff);
282
283 for (i = 0; i < ((command >> 12) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400284 error = i8042_wait_write();
285 if (error)
286 return error;
Joe Perches4eb3c302010-11-29 23:33:07 -0800287 dbg("%02x -> i8042 (parameter)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500288 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500291 for (i = 0; i < ((command >> 8) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400292 error = i8042_wait_read();
293 if (error) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800294 dbg(" -- i8042 (timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400295 return error;
296 }
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500297
298 if (command == I8042_CMD_AUX_LOOP &&
299 !(i8042_read_status() & I8042_STR_AUXDATA)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800300 dbg(" -- i8042 (auxerr)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400301 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500304 param[i] = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800305 dbg("%02x <- i8042 (return)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400308 return 0;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Márton Németh553a05b2007-10-22 00:56:52 -0400311int i8042_command(unsigned char *param, int command)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400312{
313 unsigned long flags;
314 int retval;
315
316 spin_lock_irqsave(&i8042_lock, flags);
317 retval = __i8042_command(param, command);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500318 spin_unlock_irqrestore(&i8042_lock, flags);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return retval;
321}
Márton Németh553a05b2007-10-22 00:56:52 -0400322EXPORT_SYMBOL(i8042_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324/*
325 * i8042_kbd_write() sends a byte out through the keyboard interface.
326 */
327
328static int i8042_kbd_write(struct serio *port, unsigned char c)
329{
330 unsigned long flags;
331 int retval = 0;
332
333 spin_lock_irqsave(&i8042_lock, flags);
334
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400335 if (!(retval = i8042_wait_write())) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800336 dbg("%02x -> i8042 (kbd-data)\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 i8042_write_data(c);
338 }
339
340 spin_unlock_irqrestore(&i8042_lock, flags);
341
342 return retval;
343}
344
345/*
346 * i8042_aux_write() sends a byte out through the aux interface.
347 */
348
349static int i8042_aux_write(struct serio *serio, unsigned char c)
350{
351 struct i8042_port *port = serio->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Dmitry Torokhovf4e3c712006-11-02 23:27:49 -0500353 return i8042_command(&c, port->mux == -1 ?
354 I8042_CMD_AUX_SEND :
355 I8042_CMD_MUX_SEND + port->mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700358
359/*
360 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
361 * and then re-enabling it.
362 */
363
364static void i8042_port_close(struct serio *serio)
365{
366 int irq_bit;
367 int disable_bit;
368 const char *port_name;
369
370 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
371 irq_bit = I8042_CTR_AUXINT;
372 disable_bit = I8042_CTR_AUXDIS;
373 port_name = "AUX";
374 } else {
375 irq_bit = I8042_CTR_KBDINT;
376 disable_bit = I8042_CTR_KBDDIS;
377 port_name = "KBD";
378 }
379
380 i8042_ctr &= ~irq_bit;
381 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800382 pr_warn("Can't write CTR while closing %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700383
384 udelay(50);
385
386 i8042_ctr &= ~disable_bit;
387 i8042_ctr |= irq_bit;
388 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800389 pr_err("Can't reactivate %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700390
391 /*
392 * See if there is any data appeared while we were messing with
393 * port state.
394 */
395 i8042_interrupt(0, NULL);
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * i8042_start() is called by serio core when port is about to finish
400 * registering. It will mark port as existing so i8042_interrupt can
401 * start sending data through it.
402 */
403static int i8042_start(struct serio *serio)
404{
405 struct i8042_port *port = serio->port_data;
406
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700407 port->exists = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 mb();
409 return 0;
410}
411
412/*
413 * i8042_stop() marks serio port as non-existing so i8042_interrupt
414 * will not try to send data to the port that is about to go away.
415 * The function is called by serio core as part of unregister procedure.
416 */
417static void i8042_stop(struct serio *serio)
418{
419 struct i8042_port *port = serio->port_data;
420
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700421 port->exists = false;
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400422
423 /*
424 * We synchronize with both AUX and KBD IRQs because there is
425 * a (very unlikely) chance that AUX IRQ is raised for KBD port
426 * and vice versa.
427 */
428 synchronize_irq(I8042_AUX_IRQ);
429 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 port->serio = NULL;
431}
432
433/*
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800434 * i8042_filter() filters out unwanted bytes from the input data stream.
435 * It is called from i8042_interrupt and thus is running with interrupts
436 * off and i8042_lock held.
437 */
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800438static bool i8042_filter(unsigned char data, unsigned char str,
439 struct serio *serio)
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800440{
441 if (unlikely(i8042_suppress_kbd_ack)) {
442 if ((~str & I8042_STR_AUXDATA) &&
443 (data == 0xfa || data == 0xfe)) {
444 i8042_suppress_kbd_ack--;
445 dbg("Extra keyboard ACK - filtered out\n");
446 return true;
447 }
448 }
449
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800450 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
Stefan Weil0747e3b2010-01-07 00:44:08 +0100451 dbg("Filtered out by platform filter\n");
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800452 return true;
453 }
454
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800455 return false;
456}
457
458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * i8042_interrupt() is the most important function in this driver -
460 * it handles the interrupts from the i8042, and sends incoming bytes
461 * to the upper layers.
462 */
463
David Howells7d12e782006-10-05 14:55:46 +0100464static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct i8042_port *port;
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800467 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 unsigned long flags;
469 unsigned char str, data;
470 unsigned int dfl;
471 unsigned int port_no;
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800472 bool filtered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400473 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 spin_lock_irqsave(&i8042_lock, flags);
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 str = i8042_read_status();
478 if (unlikely(~str & I8042_STR_OBF)) {
479 spin_unlock_irqrestore(&i8042_lock, flags);
Joe Perches4eb3c302010-11-29 23:33:07 -0800480 if (irq)
481 dbg("Interrupt %d, without any data\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ret = 0;
483 goto out;
484 }
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 data = i8042_read_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
489 static unsigned long last_transmit;
490 static unsigned char last_str;
491
492 dfl = 0;
493 if (str & I8042_STR_MUXERR) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800494 dbg("MUX error, status is %02x, data is %02x\n",
495 str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
497 * When MUXERR condition is signalled the data register can only contain
498 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500499 * it is not always the case. Some KBCs also report 0xfc when there is
500 * nothing connected to the port while others sometimes get confused which
501 * port the data came from and signal error leaving the data intact. They
502 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
503 * to legacy mode yet, when we see one we'll add proper handling).
504 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
505 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * was transmitted (if transmission happened not too long ago).
507 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500508
509 switch (data) {
510 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (time_before(jiffies, last_transmit + HZ/10)) {
512 str = last_str;
513 break;
514 }
515 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500516 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 case 0xfd:
518 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
519 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
520 }
521 }
522
523 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
524 last_str = str;
525 last_transmit = jiffies;
526 } else {
527
528 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
Jiri Kosinaf8313ef2011-01-08 01:37:26 -0800529 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 port_no = (str & I8042_STR_AUXDATA) ?
532 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
533 }
534
535 port = &i8042_ports[port_no];
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800536 serio = port->exists ? port->serio : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Stephen Chandler Paule1443d22015-07-15 10:20:17 -0700538 filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
539 port_no, irq,
540 dfl & SERIO_PARITY ? ", bad parity" : "",
541 dfl & SERIO_TIMEOUT ? ", timeout" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800543 filtered = i8042_filter(data, str, serio);
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400544
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800545 spin_unlock_irqrestore(&i8042_lock, flags);
546
547 if (likely(port->exists && !filtered))
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800548 serio_interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500550 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return IRQ_RETVAL(ret);
552}
553
554/*
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700555 * i8042_enable_kbd_port enables keyboard port on chip
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400556 */
557
558static int i8042_enable_kbd_port(void)
559{
560 i8042_ctr &= ~I8042_CTR_KBDDIS;
561 i8042_ctr |= I8042_CTR_KBDINT;
562
563 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400564 i8042_ctr &= ~I8042_CTR_KBDINT;
565 i8042_ctr |= I8042_CTR_KBDDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800566 pr_err("Failed to enable KBD port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400567 return -EIO;
568 }
569
570 return 0;
571}
572
573/*
574 * i8042_enable_aux_port enables AUX (mouse) port on chip
575 */
576
577static int i8042_enable_aux_port(void)
578{
579 i8042_ctr &= ~I8042_CTR_AUXDIS;
580 i8042_ctr |= I8042_CTR_AUXINT;
581
582 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400583 i8042_ctr &= ~I8042_CTR_AUXINT;
584 i8042_ctr |= I8042_CTR_AUXDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800585 pr_err("Failed to enable AUX port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400586 return -EIO;
587 }
588
589 return 0;
590}
591
592/*
593 * i8042_enable_mux_ports enables 4 individual AUX ports after
594 * the controller has been switched into Multiplexed mode
595 */
596
597static int i8042_enable_mux_ports(void)
598{
599 unsigned char param;
600 int i;
601
602 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
603 i8042_command(&param, I8042_CMD_MUX_PFX + i);
604 i8042_command(&param, I8042_CMD_AUX_ENABLE);
605 }
606
607 return i8042_enable_aux_port();
608}
609
610/*
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700611 * i8042_set_mux_mode checks whether the controller has an
612 * active multiplexor and puts the chip into Multiplexed (true)
613 * or Legacy (false) mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
615
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700616static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700619 unsigned char param, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * Get rid of bytes in the queue.
622 */
623
624 i8042_flush();
625
626/*
627 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400628 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
630
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700631 param = val = 0xf0;
632 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700634 param = val = multiplex ? 0x56 : 0xf6;
635 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700637 param = val = multiplex ? 0xa4 : 0xa5;
638 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
639 return -1;
640
641/*
642 * Workaround for interference with USB Legacy emulation
643 * that causes a v10.12 MUX to be found.
644 */
645 if (param == 0xac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -1;
647
648 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500649 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 return 0;
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/*
655 * i8042_check_mux() checks whether the controller supports the PS/2 Active
656 * Multiplexing specification by Synaptics, Phoenix, Insyde and
657 * LCS/Telegraphics.
658 */
659
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700660static int __init i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 unsigned char mux_version;
663
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700664 if (i8042_set_mux_mode(true, &mux_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -1;
666
Joe Perches4eb3c302010-11-29 23:33:07 -0800667 pr_info("Detected active multiplexing controller, rev %d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 (mux_version >> 4) & 0xf, mux_version & 0xf);
669
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400670/*
671 * Disable all muxed ports by disabling AUX.
672 */
673 i8042_ctr |= I8042_CTR_AUXDIS;
674 i8042_ctr &= ~I8042_CTR_AUXINT;
675
676 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800677 pr_err("Failed to disable AUX port, can't use MUX\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400678 return -EIO;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700681 i8042_mux_present = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400686/*
687 * The following is used to test AUX IRQ delivery.
688 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700689static struct completion i8042_aux_irq_delivered __initdata;
690static bool i8042_irq_being_tested __initdata;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400691
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700692static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400693{
694 unsigned long flags;
695 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400696 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400697
698 spin_lock_irqsave(&i8042_lock, flags);
699 str = i8042_read_status();
700 if (str & I8042_STR_OBF) {
701 data = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800702 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
703 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400704 if (i8042_irq_being_tested &&
705 data == 0xa5 && (str & I8042_STR_AUXDATA))
706 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400707 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400708 }
709 spin_unlock_irqrestore(&i8042_lock, flags);
710
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400711 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400712}
713
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400714/*
715 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
716 * verifies success by readinng CTR. Used when testing for presence of AUX
717 * port.
718 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700719static int __init i8042_toggle_aux(bool on)
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400720{
721 unsigned char param;
722 int i;
723
724 if (i8042_command(&param,
725 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
726 return -1;
727
728 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
729 for (i = 0; i < 100; i++) {
730 udelay(50);
731
732 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
733 return -1;
734
735 if (!(param & I8042_CTR_AUXDIS) == on)
736 return 0;
737 }
738
739 return -1;
740}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742/*
743 * i8042_check_aux() applies as much paranoia as it can at detecting
744 * the presence of an AUX interface.
745 */
746
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700747static int __init i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400749 int retval = -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700750 bool irq_registered = false;
751 bool aux_loop_broken = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400752 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755/*
756 * Get rid of bytes in the queue.
757 */
758
759 i8042_flush();
760
761/*
762 * Internal loopback test - filters out AT-type i8042's. Unfortunately
763 * SiS screwed up and their 5597 doesn't support the LOOP command even
764 * though it has an AUX port.
765 */
766
767 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500768 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
769 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771/*
772 * External connection test - filters out AT-soldered PS/2 i8042's
773 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
774 * 0xfa - no error on some notebooks which ignore the spec
775 * Because it's common for chipsets to return error on perfectly functioning
776 * AUX ports, we test for this only when the LOOP command failed.
777 */
778
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400779 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
780 (param && param != 0xfa && param != 0xff))
781 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500782
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500783/*
784 * If AUX_LOOP completed without error but returned unexpected data
785 * mark it as broken
786 */
787 if (!retval)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700788 aux_loop_broken = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
791/*
792 * Bit assignment test - filters out PS/2 i8042's in AT mode
793 */
794
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700795 if (i8042_toggle_aux(false)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800796 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
797 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700800 if (i8042_toggle_aux(true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -1;
802
803/*
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -0800804 * Reset keyboard (needed on some laptops to successfully detect
805 * touchpad, e.g., some Gigabyte laptop models with Elantech
806 * touchpads).
807 */
808 if (i8042_kbdreset) {
809 pr_warn("Attempting to reset device connected to KBD port\n");
810 i8042_kbd_write(NULL, (unsigned char) 0xff);
811 }
812
813/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400814 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
815 * used it for a PCI card or somethig else.
816 */
817
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700818 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400819/*
820 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
821 * is working and hope we are right.
822 */
823 retval = 0;
824 goto out;
825 }
826
827 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
828 "i8042", i8042_platform_device))
829 goto out;
830
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700831 irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400832
833 if (i8042_enable_aux_port())
834 goto out;
835
836 spin_lock_irqsave(&i8042_lock, flags);
837
838 init_completion(&i8042_aux_irq_delivered);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700839 i8042_irq_being_tested = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400840
841 param = 0xa5;
842 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
843
844 spin_unlock_irqrestore(&i8042_lock, flags);
845
846 if (retval)
847 goto out;
848
849 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
850 msecs_to_jiffies(250)) == 0) {
851/*
852 * AUX IRQ was never delivered so we need to flush the controller to
853 * get rid of the byte we put there; otherwise keyboard may not work.
854 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800855 dbg(" -- i8042 (aux irq test timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400856 i8042_flush();
857 retval = -1;
858 }
859
860 out:
861
862/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * Disable the interface.
864 */
865
866 i8042_ctr |= I8042_CTR_AUXDIS;
867 i8042_ctr &= ~I8042_CTR_AUXINT;
868
869 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400870 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400872 if (irq_registered)
873 free_irq(I8042_AUX_IRQ, i8042_platform_device);
874
875 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400878static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700880 if (i8042_flush()) {
Takashi Iwaif5d75342015-09-05 10:29:09 -0700881 pr_info("No controller found\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400882 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
886}
887
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400888static int i8042_controller_selftest(void)
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500889{
890 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700891 int i = 0;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500892
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700893 /*
894 * We try this 5 times; on some really fragile systems this does not
895 * take the first time...
896 */
897 do {
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500898
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700899 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
Paul Bollea2a94e72011-03-31 00:11:48 -0700900 pr_err("i8042 controller selftest timeout\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700901 return -ENODEV;
902 }
903
904 if (param == I8042_RET_CTL_TEST)
905 return 0;
906
Paul Bollea2a94e72011-03-31 00:11:48 -0700907 dbg("i8042 controller selftest: %#x != %#x\n",
908 param, I8042_RET_CTL_TEST);
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700909 msleep(50);
910 } while (i++ < 5);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500911
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700912#ifdef CONFIG_X86
913 /*
914 * On x86, we don't fail entire i8042 initialization if controller
915 * reset fails in hopes that keyboard port will still be functional
916 * and user will still get a working keyboard. This is especially
917 * important on netbooks. On other arches we trust hardware more.
918 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800919 pr_info("giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500920 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700921#else
Paul Bollea2a94e72011-03-31 00:11:48 -0700922 pr_err("i8042 controller selftest failed\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700923 return -EIO;
924#endif
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500925}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927/*
928 * i8042_controller init initializes the i8042 controller, and,
929 * most importantly, sets it into non-xlated mode if that's
930 * desired.
931 */
932
933static int i8042_controller_init(void)
934{
935 unsigned long flags;
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800936 int n = 0;
937 unsigned char ctr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939/*
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800940 * Save the CTR for restore on unload / reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
942
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800943 do {
944 if (n >= 10) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800945 pr_err("Unable to get stable CTR read\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800946 return -EIO;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800949 if (n != 0)
950 udelay(50);
951
952 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800953 pr_err("Can't read CTR while initializing i8042\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800954 return -EIO;
955 }
956
957 } while (n < 2 || ctr[0] != ctr[1]);
958
959 i8042_initial_ctr = i8042_ctr = ctr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961/*
962 * Disable the keyboard interface and interrupt.
963 */
964
965 i8042_ctr |= I8042_CTR_KBDDIS;
966 i8042_ctr &= ~I8042_CTR_KBDINT;
967
968/*
969 * Handle keylock.
970 */
971
972 spin_lock_irqsave(&i8042_lock, flags);
973 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
974 if (i8042_unlock)
975 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500976 else
Joe Perches4eb3c302010-11-29 23:33:07 -0800977 pr_warn("Warning: Keylock active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 spin_unlock_irqrestore(&i8042_lock, flags);
980
981/*
982 * If the chip is configured into nontranslated mode by the BIOS, don't
983 * bother enabling translating and be happy.
984 */
985
986 if (~i8042_ctr & I8042_CTR_XLATE)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700987 i8042_direct = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989/*
990 * Set nontranslated mode for the kbd interface if requested by an option.
991 * After this the kbd interface becomes a simple serial in/out, like the aux
992 * interface is. We don't do this by default, since it can confuse notebook
993 * BIOSes.
994 */
995
996 if (i8042_direct)
997 i8042_ctr &= ~I8042_CTR_XLATE;
998
999/*
1000 * Write CTR back.
1001 */
1002
1003 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001004 pr_err("Can't write CTR while initializing i8042\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001005 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001008/*
1009 * Flush whatever accumulated while we were disabling keyboard port.
1010 */
1011
1012 i8042_flush();
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return 0;
1015}
1016
1017
1018/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001019 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
1021
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001022static void i8042_controller_reset(bool force_reset)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001023{
1024 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001027 * Disable both KBD and AUX interfaces so they don't get in the way
1028 */
1029
1030 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1031 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1032
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001033 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001034 pr_warn("Can't write CTR while resetting\n");
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001035
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001036/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * Disable MUX mode if present.
1038 */
1039
1040 if (i8042_mux_present)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001041 i8042_set_mux_mode(false, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001044 * Reset the controller if requested.
1045 */
1046
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001047 if (i8042_reset || force_reset)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001048 i8042_controller_selftest();
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001049
1050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * Restore the original control register setting.
1052 */
1053
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001054 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001055 pr_warn("Can't restore CTR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058
1059/*
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001060 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1061 * when kernel panics. Flashing LEDs is useful for users running X who may
Michael Opdenackeraa5e5dc2013-09-18 06:00:43 +02001062 * not see the console and will help distinguishing panics from "real"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * lockups.
1064 *
1065 * Note that DELAY has a limit of 10ms so we will not get stuck here
1066 * waiting for KBC to free up even if KBD interrupt is off
1067 */
1068
1069#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1070
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001071static long i8042_panic_blink(int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 long delay = 0;
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001074 char led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001076 led = (state) ? 0x01 | 0x04 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 while (i8042_read_status() & I8042_STR_IBF)
1078 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001079 dbg("%02x -> i8042 (panic blink)\n", 0xed);
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -05001080 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 i8042_write_data(0xed); /* set leds */
1082 DELAY;
1083 while (i8042_read_status() & I8042_STR_IBF)
1084 DELAY;
1085 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001086 dbg("%02x -> i8042 (panic blink)\n", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 i8042_write_data(led);
1088 DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return delay;
1090}
1091
1092#undef DELAY
1093
Bruno Prémontd35895d2008-05-27 01:36:04 -04001094#ifdef CONFIG_X86
1095static void i8042_dritek_enable(void)
1096{
Christoph Fritz594d6362010-09-29 18:04:21 -07001097 unsigned char param = 0x90;
Bruno Prémontd35895d2008-05-27 01:36:04 -04001098 int error;
1099
1100 error = i8042_command(&param, 0x1059);
1101 if (error)
Joe Perches4eb3c302010-11-29 23:33:07 -08001102 pr_warn("Failed to enable DRITEK extension: %d\n", error);
Bruno Prémontd35895d2008-05-27 01:36:04 -04001103}
1104#endif
1105
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001106#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -07001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001109 * Here we try to reset everything back to a state we had
1110 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
1112
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001113static int i8042_controller_resume(bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001115 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001117 error = i8042_controller_check();
1118 if (error)
1119 return error;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001120
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001121 if (i8042_reset || force_reset) {
1122 error = i8042_controller_selftest();
1123 if (error)
1124 return error;
1125 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001126
1127/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001128 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001129 */
1130
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001131 i8042_ctr = i8042_initial_ctr;
1132 if (i8042_direct)
1133 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001134 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1135 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001136 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001137 pr_warn("Can't write CTR to resume, retrying...\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001138 msleep(50);
1139 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001140 pr_err("CTR write retry failed\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001141 return -EIO;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Bruno Prémontd35895d2008-05-27 01:36:04 -04001145
1146#ifdef CONFIG_X86
1147 if (i8042_dritek)
1148 i8042_dritek_enable();
1149#endif
1150
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001151 if (i8042_mux_present) {
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001152 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
Joe Perches4eb3c302010-11-29 23:33:07 -08001153 pr_warn("failed to resume active multiplexor, mouse won't work\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001154 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1155 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001157 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1158 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
David Howells7d12e782006-10-05 14:55:46 +01001160 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001164
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001165/*
1166 * Here we try to restore the original BIOS settings to avoid
1167 * upsetting it.
1168 */
1169
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001170static int i8042_pm_suspend(struct device *dev)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001171{
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001172 int i;
1173
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001174 if (pm_suspend_via_firmware())
1175 i8042_controller_reset(true);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001176
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001177 /* Set up serio interrupts for system wakeup. */
1178 for (i = 0; i < I8042_NUM_PORTS; i++) {
1179 struct serio *serio = i8042_ports[i].serio;
1180
1181 if (serio && device_may_wakeup(&serio->dev))
1182 enable_irq_wake(i8042_ports[i].irq);
1183 }
1184
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001185 return 0;
1186}
1187
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001188static int i8042_pm_resume_noirq(struct device *dev)
1189{
1190 if (!pm_resume_via_firmware())
1191 i8042_interrupt(0, NULL);
1192
1193 return 0;
1194}
1195
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001196static int i8042_pm_resume(struct device *dev)
1197{
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001198 bool force_reset;
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001199 int i;
1200
1201 for (i = 0; i < I8042_NUM_PORTS; i++) {
1202 struct serio *serio = i8042_ports[i].serio;
1203
1204 if (serio && device_may_wakeup(&serio->dev))
1205 disable_irq_wake(i8042_ports[i].irq);
1206 }
1207
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001208 /*
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001209 * If platform firmware was not going to be involved in suspend, we did
1210 * not restore the controller state to whatever it had been at boot
1211 * time, so we do not need to do anything.
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001212 */
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001213 if (!pm_suspend_via_firmware())
1214 return 0;
1215
1216 /*
1217 * We only need to reset the controller if we are resuming after handing
1218 * off control to the platform firmware, otherwise we can simply restore
1219 * the mode.
1220 */
1221 force_reset = pm_resume_via_firmware();
1222
1223 return i8042_controller_resume(force_reset);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001224}
1225
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001226static int i8042_pm_thaw(struct device *dev)
1227{
1228 i8042_interrupt(0, NULL);
1229
1230 return 0;
1231}
1232
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001233static int i8042_pm_reset(struct device *dev)
1234{
1235 i8042_controller_reset(false);
1236
1237 return 0;
1238}
1239
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001240static int i8042_pm_restore(struct device *dev)
1241{
1242 return i8042_controller_resume(false);
1243}
1244
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001245static const struct dev_pm_ops i8042_pm_ops = {
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001246 .suspend = i8042_pm_suspend,
Rafael J. Wysocki1c5dd132015-10-07 03:03:57 +02001247 .resume_noirq = i8042_pm_resume_noirq,
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001248 .resume = i8042_pm_resume,
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001249 .thaw = i8042_pm_thaw,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001250 .poweroff = i8042_pm_reset,
1251 .restore = i8042_pm_restore,
1252};
1253
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001254#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256/*
1257 * We need to reset the 8042 back to original mode on system shutdown,
1258 * because otherwise BIOSes will be confused.
1259 */
1260
Russell King3ae5eae2005-11-09 22:32:44 +00001261static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001263 i8042_controller_reset(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001266static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct serio *serio;
1269 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1270
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001271 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001272 if (!serio)
1273 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001275 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1276 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001277 serio->start = i8042_start;
1278 serio->stop = i8042_stop;
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001279 serio->close = i8042_port_close;
Dmitry Torokhov40974612016-07-25 11:36:54 -07001280 serio->ps2_cmd_mutex = &i8042_mutex;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001281 serio->port_data = port;
1282 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001283 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001284 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001285 strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1286 sizeof(serio->firmware_id));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001287
1288 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001289 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001290
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001294static int __init i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001297 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1298 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001300 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001301 if (!serio)
1302 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001304 serio->id.type = SERIO_8042;
1305 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001306 serio->start = i8042_start;
1307 serio->stop = i8042_stop;
Dmitry Torokhov47af45d2016-08-16 17:38:54 -07001308 serio->ps2_cmd_mutex = &i8042_mutex;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001309 serio->port_data = port;
1310 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001311 if (idx < 0) {
1312 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1313 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001314 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1315 sizeof(serio->firmware_id));
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001316 serio->close = i8042_port_close;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001317 } else {
1318 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1319 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
Hans de Goede266e43c2014-09-11 10:13:13 -07001320 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1321 sizeof(serio->firmware_id));
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001322 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001323
1324 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001325 port->mux = idx;
1326 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001327
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001331static void __init i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001333 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1334 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1335}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001337static void __init i8042_free_aux_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001338{
1339 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001341 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1342 kfree(i8042_ports[i].serio);
1343 i8042_ports[i].serio = NULL;
1344 }
1345}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001346
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001347static void __init i8042_register_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001348{
1349 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001350
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001351 for (i = 0; i < I8042_NUM_PORTS; i++) {
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001352 struct serio *serio = i8042_ports[i].serio;
1353
1354 if (serio) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001355 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001356 serio->name,
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001357 (unsigned long) I8042_DATA_REG,
1358 (unsigned long) I8042_COMMAND_REG,
1359 i8042_ports[i].irq);
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001360 serio_register_port(serio);
1361 device_set_wakeup_capable(&serio->dev, true);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001362 }
1363 }
1364}
1365
Bill Pembertone2619cf2012-11-23 21:50:47 -08001366static void i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001367{
1368 int i;
1369
1370 for (i = 0; i < I8042_NUM_PORTS; i++) {
1371 if (i8042_ports[i].serio) {
1372 serio_unregister_port(i8042_ports[i].serio);
1373 i8042_ports[i].serio = NULL;
1374 }
1375 }
1376}
1377
1378static void i8042_free_irqs(void)
1379{
1380 if (i8042_aux_irq_registered)
1381 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1382 if (i8042_kbd_irq_registered)
1383 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1384
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001385 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001386}
1387
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001388static int __init i8042_setup_aux(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001389{
1390 int (*aux_enable)(void);
1391 int error;
1392 int i;
1393
1394 if (i8042_check_aux())
1395 return -ENODEV;
1396
1397 if (i8042_nomux || i8042_check_mux()) {
1398 error = i8042_create_aux_port(-1);
1399 if (error)
1400 goto err_free_ports;
1401 aux_enable = i8042_enable_aux_port;
1402 } else {
1403 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1404 error = i8042_create_aux_port(i);
1405 if (error)
1406 goto err_free_ports;
1407 }
1408 aux_enable = i8042_enable_mux_ports;
1409 }
1410
1411 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1412 "i8042", i8042_platform_device);
1413 if (error)
1414 goto err_free_ports;
1415
1416 if (aux_enable())
1417 goto err_free_irq;
1418
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001419 i8042_aux_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001420 return 0;
1421
1422 err_free_irq:
1423 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1424 err_free_ports:
1425 i8042_free_aux_ports();
1426 return error;
1427}
1428
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001429static int __init i8042_setup_kbd(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001430{
1431 int error;
1432
1433 error = i8042_create_kbd_port();
1434 if (error)
1435 return error;
1436
1437 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1438 "i8042", i8042_platform_device);
1439 if (error)
1440 goto err_free_port;
1441
1442 error = i8042_enable_kbd_port();
1443 if (error)
1444 goto err_free_irq;
1445
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001446 i8042_kbd_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001447 return 0;
1448
1449 err_free_irq:
1450 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1451 err_free_port:
1452 i8042_free_kbd_port();
1453 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001456static int i8042_kbd_bind_notifier(struct notifier_block *nb,
1457 unsigned long action, void *data)
1458{
1459 struct device *dev = data;
1460 struct serio *serio = to_serio_port(dev);
1461 struct i8042_port *port = serio->port_data;
1462
1463 if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
1464 return 0;
1465
1466 switch (action) {
1467 case BUS_NOTIFY_BOUND_DRIVER:
1468 port->driver_bound = true;
1469 break;
1470
1471 case BUS_NOTIFY_UNBIND_DRIVER:
1472 port->driver_bound = false;
1473 break;
1474 }
1475
1476 return 0;
1477}
1478
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001479static int __init i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001481 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001483 i8042_platform_device = dev;
1484
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001485 if (i8042_reset) {
1486 error = i8042_controller_selftest();
1487 if (error)
1488 return error;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001491 error = i8042_controller_init();
1492 if (error)
1493 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Bruno Prémontd35895d2008-05-27 01:36:04 -04001495#ifdef CONFIG_X86
1496 if (i8042_dritek)
1497 i8042_dritek_enable();
1498#endif
1499
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001500 if (!i8042_noaux) {
1501 error = i8042_setup_aux();
1502 if (error && error != -ENODEV && error != -EBUSY)
1503 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001506 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001507 error = i8042_setup_kbd();
1508 if (error)
1509 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001510 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001511/*
1512 * Ok, everything is ready, let's register all serio ports
1513 */
1514 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001517
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001518 out_fail:
1519 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1520 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001521 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001522 i8042_platform_device = NULL;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001523
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001524 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
Bill Pembertone2619cf2012-11-23 21:50:47 -08001527static int i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001529 i8042_unregister_ports();
1530 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001531 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001532 i8042_platform_device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001534 return 0;
1535}
1536
1537static struct platform_driver i8042_driver = {
1538 .driver = {
1539 .name = "i8042",
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001540#ifdef CONFIG_PM
1541 .pm = &i8042_pm_ops,
1542#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001543 },
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001544 .remove = i8042_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001545 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001546};
1547
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001548static struct notifier_block i8042_kbd_bind_notifier_block = {
1549 .notifier_call = i8042_kbd_bind_notifier,
1550};
1551
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001552static int __init i8042_init(void)
1553{
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001554 struct platform_device *pdev;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001555 int err;
1556
1557 dbg_init();
1558
1559 err = i8042_platform_init();
1560 if (err)
1561 return err;
1562
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001563 err = i8042_controller_check();
1564 if (err)
1565 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001566
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001567 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1568 if (IS_ERR(pdev)) {
1569 err = PTR_ERR(pdev);
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001570 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001571 }
1572
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001573 bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001574 panic_blink = i8042_panic_blink;
1575
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001576 return 0;
1577
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001578 err_platform_exit:
1579 i8042_platform_exit();
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001580 return err;
1581}
1582
1583static void __exit i8042_exit(void)
1584{
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001585 platform_device_unregister(i8042_platform_device);
Dmitry Torokhovaf045b82010-08-31 17:27:02 -07001586 platform_driver_unregister(&i8042_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 i8042_platform_exit();
1588
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001589 bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 panic_blink = NULL;
1591}
1592
1593module_init(i8042_init);
1594module_exit(i8042_exit);