blob: cb5ece77fd7d8c47f29db0400da5184de0939644 [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");
91#endif
92
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -070093static bool i8042_bypass_aux_irq_test;
Hans de Goedea7c58682014-04-19 20:47:35 -070094static char i8042_kbd_firmware_id[128];
95static char i8042_aux_firmware_id[128];
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include "i8042.h"
98
Dmitry Torokhov181d6832009-09-16 01:06:43 -070099/*
100 * i8042_lock protects serialization between i8042_command and
101 * the interrupt handler.
102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static DEFINE_SPINLOCK(i8042_lock);
104
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700105/*
106 * Writers to AUX and KBD ports as well as users issuing i8042_command
107 * directly should acquire i8042_mutex (by means of calling
108 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
109 * they do not disturb each other (unfortunately in many i8042
110 * implementations write to one of the ports will immediately abort
111 * command that is being processed by another port).
112 */
113static DEFINE_MUTEX(i8042_mutex);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct i8042_port {
116 struct serio *serio;
117 int irq;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700118 bool exists;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 signed char mux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122#define I8042_KBD_PORT_NO 0
123#define I8042_AUX_PORT_NO 1
124#define I8042_MUX_PORT_NO 2
125#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400126
127static struct i8042_port i8042_ports[I8042_NUM_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static unsigned char i8042_initial_ctr;
130static unsigned char i8042_ctr;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700131static bool i8042_mux_present;
132static bool i8042_kbd_irq_registered;
133static bool i8042_aux_irq_registered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400134static unsigned char i8042_suppress_kbd_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static struct platform_device *i8042_platform_device;
136
David Howells7d12e782006-10-05 14:55:46 +0100137static irqreturn_t i8042_interrupt(int irq, void *dev_id);
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800138static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
139 struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700141void i8042_lock_chip(void)
142{
143 mutex_lock(&i8042_mutex);
144}
145EXPORT_SYMBOL(i8042_lock_chip);
146
147void i8042_unlock_chip(void)
148{
149 mutex_unlock(&i8042_mutex);
150}
151EXPORT_SYMBOL(i8042_unlock_chip);
152
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800153int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
154 struct serio *serio))
155{
156 unsigned long flags;
157 int ret = 0;
158
159 spin_lock_irqsave(&i8042_lock, flags);
160
161 if (i8042_platform_filter) {
162 ret = -EBUSY;
163 goto out;
164 }
165
166 i8042_platform_filter = filter;
167
168out:
169 spin_unlock_irqrestore(&i8042_lock, flags);
170 return ret;
171}
172EXPORT_SYMBOL(i8042_install_filter);
173
174int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
175 struct serio *port))
176{
177 unsigned long flags;
178 int ret = 0;
179
180 spin_lock_irqsave(&i8042_lock, flags);
181
182 if (i8042_platform_filter != filter) {
183 ret = -EINVAL;
184 goto out;
185 }
186
187 i8042_platform_filter = NULL;
188
189out:
190 spin_unlock_irqrestore(&i8042_lock, flags);
191 return ret;
192}
193EXPORT_SYMBOL(i8042_remove_filter);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
196 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
197 * be ready for reading values from it / writing values to it.
198 * Called always with i8042_lock held.
199 */
200
201static int i8042_wait_read(void)
202{
203 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
206 udelay(50);
207 i++;
208 }
209 return -(i == I8042_CTL_TIMEOUT);
210}
211
212static int i8042_wait_write(void)
213{
214 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
217 udelay(50);
218 i++;
219 }
220 return -(i == I8042_CTL_TIMEOUT);
221}
222
223/*
224 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
225 * of the i8042 down the toilet.
226 */
227
228static int i8042_flush(void)
229{
230 unsigned long flags;
231 unsigned char data, str;
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700232 int count = 0;
233 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 spin_lock_irqsave(&i8042_lock, flags);
236
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700237 while ((str = i8042_read_status()) & I8042_STR_OBF) {
238 if (count++ < I8042_BUFFER_SIZE) {
239 udelay(50);
240 data = i8042_read_data();
241 dbg("%02x <- i8042 (flush, %s)\n",
242 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
243 } else {
244 retval = -EIO;
245 break;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 spin_unlock_irqrestore(&i8042_lock, flags);
250
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700251 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/*
255 * i8042_command() executes a command on the i8042. It also sends the input
256 * parameter(s) of the commands to it, and receives the output value(s). The
257 * parameters are to be stored in the param array, and the output is placed
258 * into the same array. The number of the parameters and output values is
259 * encoded in bits 8-11 of the command number.
260 */
261
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400262static int __i8042_command(unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400264 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
267 return -1;
268
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400269 error = i8042_wait_write();
270 if (error)
271 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500272
Joe Perches4eb3c302010-11-29 23:33:07 -0800273 dbg("%02x -> i8042 (command)\n", command & 0xff);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500274 i8042_write_command(command & 0xff);
275
276 for (i = 0; i < ((command >> 12) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400277 error = i8042_wait_write();
278 if (error)
279 return error;
Joe Perches4eb3c302010-11-29 23:33:07 -0800280 dbg("%02x -> i8042 (parameter)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500281 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500284 for (i = 0; i < ((command >> 8) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400285 error = i8042_wait_read();
286 if (error) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800287 dbg(" -- i8042 (timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400288 return error;
289 }
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500290
291 if (command == I8042_CMD_AUX_LOOP &&
292 !(i8042_read_status() & I8042_STR_AUXDATA)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800293 dbg(" -- i8042 (auxerr)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400294 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500297 param[i] = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800298 dbg("%02x <- i8042 (return)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400301 return 0;
302}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Márton Németh553a05b2007-10-22 00:56:52 -0400304int i8042_command(unsigned char *param, int command)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400305{
306 unsigned long flags;
307 int retval;
308
309 spin_lock_irqsave(&i8042_lock, flags);
310 retval = __i8042_command(param, command);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500311 spin_unlock_irqrestore(&i8042_lock, flags);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return retval;
314}
Márton Németh553a05b2007-10-22 00:56:52 -0400315EXPORT_SYMBOL(i8042_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/*
318 * i8042_kbd_write() sends a byte out through the keyboard interface.
319 */
320
321static int i8042_kbd_write(struct serio *port, unsigned char c)
322{
323 unsigned long flags;
324 int retval = 0;
325
326 spin_lock_irqsave(&i8042_lock, flags);
327
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400328 if (!(retval = i8042_wait_write())) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800329 dbg("%02x -> i8042 (kbd-data)\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 i8042_write_data(c);
331 }
332
333 spin_unlock_irqrestore(&i8042_lock, flags);
334
335 return retval;
336}
337
338/*
339 * i8042_aux_write() sends a byte out through the aux interface.
340 */
341
342static int i8042_aux_write(struct serio *serio, unsigned char c)
343{
344 struct i8042_port *port = serio->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dmitry Torokhovf4e3c712006-11-02 23:27:49 -0500346 return i8042_command(&c, port->mux == -1 ?
347 I8042_CMD_AUX_SEND :
348 I8042_CMD_MUX_SEND + port->mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700351
352/*
353 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
354 * and then re-enabling it.
355 */
356
357static void i8042_port_close(struct serio *serio)
358{
359 int irq_bit;
360 int disable_bit;
361 const char *port_name;
362
363 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
364 irq_bit = I8042_CTR_AUXINT;
365 disable_bit = I8042_CTR_AUXDIS;
366 port_name = "AUX";
367 } else {
368 irq_bit = I8042_CTR_KBDINT;
369 disable_bit = I8042_CTR_KBDDIS;
370 port_name = "KBD";
371 }
372
373 i8042_ctr &= ~irq_bit;
374 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800375 pr_warn("Can't write CTR while closing %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700376
377 udelay(50);
378
379 i8042_ctr &= ~disable_bit;
380 i8042_ctr |= irq_bit;
381 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800382 pr_err("Can't reactivate %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700383
384 /*
385 * See if there is any data appeared while we were messing with
386 * port state.
387 */
388 i8042_interrupt(0, NULL);
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * i8042_start() is called by serio core when port is about to finish
393 * registering. It will mark port as existing so i8042_interrupt can
394 * start sending data through it.
395 */
396static int i8042_start(struct serio *serio)
397{
398 struct i8042_port *port = serio->port_data;
399
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700400 port->exists = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 mb();
402 return 0;
403}
404
405/*
406 * i8042_stop() marks serio port as non-existing so i8042_interrupt
407 * will not try to send data to the port that is about to go away.
408 * The function is called by serio core as part of unregister procedure.
409 */
410static void i8042_stop(struct serio *serio)
411{
412 struct i8042_port *port = serio->port_data;
413
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700414 port->exists = false;
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400415
416 /*
417 * We synchronize with both AUX and KBD IRQs because there is
418 * a (very unlikely) chance that AUX IRQ is raised for KBD port
419 * and vice versa.
420 */
421 synchronize_irq(I8042_AUX_IRQ);
422 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 port->serio = NULL;
424}
425
426/*
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800427 * i8042_filter() filters out unwanted bytes from the input data stream.
428 * It is called from i8042_interrupt and thus is running with interrupts
429 * off and i8042_lock held.
430 */
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800431static bool i8042_filter(unsigned char data, unsigned char str,
432 struct serio *serio)
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800433{
434 if (unlikely(i8042_suppress_kbd_ack)) {
435 if ((~str & I8042_STR_AUXDATA) &&
436 (data == 0xfa || data == 0xfe)) {
437 i8042_suppress_kbd_ack--;
438 dbg("Extra keyboard ACK - filtered out\n");
439 return true;
440 }
441 }
442
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800443 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
Stefan Weil0747e3b2010-01-07 00:44:08 +0100444 dbg("Filtered out by platform filter\n");
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800445 return true;
446 }
447
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800448 return false;
449}
450
451/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * i8042_interrupt() is the most important function in this driver -
453 * it handles the interrupts from the i8042, and sends incoming bytes
454 * to the upper layers.
455 */
456
David Howells7d12e782006-10-05 14:55:46 +0100457static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct i8042_port *port;
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800460 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 unsigned long flags;
462 unsigned char str, data;
463 unsigned int dfl;
464 unsigned int port_no;
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800465 bool filtered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400466 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 spin_lock_irqsave(&i8042_lock, flags);
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 str = i8042_read_status();
471 if (unlikely(~str & I8042_STR_OBF)) {
472 spin_unlock_irqrestore(&i8042_lock, flags);
Joe Perches4eb3c302010-11-29 23:33:07 -0800473 if (irq)
474 dbg("Interrupt %d, without any data\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 ret = 0;
476 goto out;
477 }
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 data = i8042_read_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
482 static unsigned long last_transmit;
483 static unsigned char last_str;
484
485 dfl = 0;
486 if (str & I8042_STR_MUXERR) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800487 dbg("MUX error, status is %02x, data is %02x\n",
488 str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * When MUXERR condition is signalled the data register can only contain
491 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500492 * it is not always the case. Some KBCs also report 0xfc when there is
493 * nothing connected to the port while others sometimes get confused which
494 * port the data came from and signal error leaving the data intact. They
495 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
496 * to legacy mode yet, when we see one we'll add proper handling).
497 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
498 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * was transmitted (if transmission happened not too long ago).
500 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500501
502 switch (data) {
503 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (time_before(jiffies, last_transmit + HZ/10)) {
505 str = last_str;
506 break;
507 }
508 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500509 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 case 0xfd:
511 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
512 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
513 }
514 }
515
516 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
517 last_str = str;
518 last_transmit = jiffies;
519 } else {
520
521 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
Jiri Kosinaf8313ef2011-01-08 01:37:26 -0800522 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 port_no = (str & I8042_STR_AUXDATA) ?
525 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
526 }
527
528 port = &i8042_ports[port_no];
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800529 serio = port->exists ? port->serio : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Joe Perches4eb3c302010-11-29 23:33:07 -0800531 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)\n",
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400532 data, port_no, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 dfl & SERIO_PARITY ? ", bad parity" : "",
534 dfl & SERIO_TIMEOUT ? ", timeout" : "");
535
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800536 filtered = i8042_filter(data, str, serio);
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400537
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800538 spin_unlock_irqrestore(&i8042_lock, flags);
539
540 if (likely(port->exists && !filtered))
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800541 serio_interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500543 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return IRQ_RETVAL(ret);
545}
546
547/*
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700548 * i8042_enable_kbd_port enables keyboard port on chip
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400549 */
550
551static int i8042_enable_kbd_port(void)
552{
553 i8042_ctr &= ~I8042_CTR_KBDDIS;
554 i8042_ctr |= I8042_CTR_KBDINT;
555
556 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400557 i8042_ctr &= ~I8042_CTR_KBDINT;
558 i8042_ctr |= I8042_CTR_KBDDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800559 pr_err("Failed to enable KBD port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400560 return -EIO;
561 }
562
563 return 0;
564}
565
566/*
567 * i8042_enable_aux_port enables AUX (mouse) port on chip
568 */
569
570static int i8042_enable_aux_port(void)
571{
572 i8042_ctr &= ~I8042_CTR_AUXDIS;
573 i8042_ctr |= I8042_CTR_AUXINT;
574
575 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400576 i8042_ctr &= ~I8042_CTR_AUXINT;
577 i8042_ctr |= I8042_CTR_AUXDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800578 pr_err("Failed to enable AUX port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400579 return -EIO;
580 }
581
582 return 0;
583}
584
585/*
586 * i8042_enable_mux_ports enables 4 individual AUX ports after
587 * the controller has been switched into Multiplexed mode
588 */
589
590static int i8042_enable_mux_ports(void)
591{
592 unsigned char param;
593 int i;
594
595 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
596 i8042_command(&param, I8042_CMD_MUX_PFX + i);
597 i8042_command(&param, I8042_CMD_AUX_ENABLE);
598 }
599
600 return i8042_enable_aux_port();
601}
602
603/*
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700604 * i8042_set_mux_mode checks whether the controller has an
605 * active multiplexor and puts the chip into Multiplexed (true)
606 * or Legacy (false) mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700609static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700612 unsigned char param, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/*
614 * Get rid of bytes in the queue.
615 */
616
617 i8042_flush();
618
619/*
620 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400621 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700624 param = val = 0xf0;
625 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700627 param = val = multiplex ? 0x56 : 0xf6;
628 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700630 param = val = multiplex ? 0xa4 : 0xa5;
631 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
632 return -1;
633
634/*
635 * Workaround for interference with USB Legacy emulation
636 * that causes a v10.12 MUX to be found.
637 */
638 if (param == 0xac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -1;
640
641 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500642 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 return 0;
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
648 * i8042_check_mux() checks whether the controller supports the PS/2 Active
649 * Multiplexing specification by Synaptics, Phoenix, Insyde and
650 * LCS/Telegraphics.
651 */
652
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700653static int __init i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 unsigned char mux_version;
656
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700657 if (i8042_set_mux_mode(true, &mux_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -1;
659
Joe Perches4eb3c302010-11-29 23:33:07 -0800660 pr_info("Detected active multiplexing controller, rev %d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 (mux_version >> 4) & 0xf, mux_version & 0xf);
662
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400663/*
664 * Disable all muxed ports by disabling AUX.
665 */
666 i8042_ctr |= I8042_CTR_AUXDIS;
667 i8042_ctr &= ~I8042_CTR_AUXINT;
668
669 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800670 pr_err("Failed to disable AUX port, can't use MUX\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400671 return -EIO;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700674 i8042_mux_present = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
677}
678
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400679/*
680 * The following is used to test AUX IRQ delivery.
681 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700682static struct completion i8042_aux_irq_delivered __initdata;
683static bool i8042_irq_being_tested __initdata;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400684
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700685static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400686{
687 unsigned long flags;
688 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400689 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400690
691 spin_lock_irqsave(&i8042_lock, flags);
692 str = i8042_read_status();
693 if (str & I8042_STR_OBF) {
694 data = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800695 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
696 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400697 if (i8042_irq_being_tested &&
698 data == 0xa5 && (str & I8042_STR_AUXDATA))
699 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400700 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400701 }
702 spin_unlock_irqrestore(&i8042_lock, flags);
703
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400704 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400705}
706
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400707/*
708 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
709 * verifies success by readinng CTR. Used when testing for presence of AUX
710 * port.
711 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700712static int __init i8042_toggle_aux(bool on)
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400713{
714 unsigned char param;
715 int i;
716
717 if (i8042_command(&param,
718 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
719 return -1;
720
721 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
722 for (i = 0; i < 100; i++) {
723 udelay(50);
724
725 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
726 return -1;
727
728 if (!(param & I8042_CTR_AUXDIS) == on)
729 return 0;
730 }
731
732 return -1;
733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735/*
736 * i8042_check_aux() applies as much paranoia as it can at detecting
737 * the presence of an AUX interface.
738 */
739
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700740static int __init i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400742 int retval = -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700743 bool irq_registered = false;
744 bool aux_loop_broken = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400745 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748/*
749 * Get rid of bytes in the queue.
750 */
751
752 i8042_flush();
753
754/*
755 * Internal loopback test - filters out AT-type i8042's. Unfortunately
756 * SiS screwed up and their 5597 doesn't support the LOOP command even
757 * though it has an AUX port.
758 */
759
760 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500761 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
762 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764/*
765 * External connection test - filters out AT-soldered PS/2 i8042's
766 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
767 * 0xfa - no error on some notebooks which ignore the spec
768 * Because it's common for chipsets to return error on perfectly functioning
769 * AUX ports, we test for this only when the LOOP command failed.
770 */
771
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400772 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
773 (param && param != 0xfa && param != 0xff))
774 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500775
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500776/*
777 * If AUX_LOOP completed without error but returned unexpected data
778 * mark it as broken
779 */
780 if (!retval)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700781 aux_loop_broken = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
784/*
785 * Bit assignment test - filters out PS/2 i8042's in AT mode
786 */
787
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700788 if (i8042_toggle_aux(false)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800789 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
790 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700793 if (i8042_toggle_aux(true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -1;
795
796/*
Srihari Vijayaraghavan148e9a72015-01-07 16:25:53 -0800797 * Reset keyboard (needed on some laptops to successfully detect
798 * touchpad, e.g., some Gigabyte laptop models with Elantech
799 * touchpads).
800 */
801 if (i8042_kbdreset) {
802 pr_warn("Attempting to reset device connected to KBD port\n");
803 i8042_kbd_write(NULL, (unsigned char) 0xff);
804 }
805
806/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400807 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
808 * used it for a PCI card or somethig else.
809 */
810
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700811 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400812/*
813 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
814 * is working and hope we are right.
815 */
816 retval = 0;
817 goto out;
818 }
819
820 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
821 "i8042", i8042_platform_device))
822 goto out;
823
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700824 irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400825
826 if (i8042_enable_aux_port())
827 goto out;
828
829 spin_lock_irqsave(&i8042_lock, flags);
830
831 init_completion(&i8042_aux_irq_delivered);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700832 i8042_irq_being_tested = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400833
834 param = 0xa5;
835 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
836
837 spin_unlock_irqrestore(&i8042_lock, flags);
838
839 if (retval)
840 goto out;
841
842 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
843 msecs_to_jiffies(250)) == 0) {
844/*
845 * AUX IRQ was never delivered so we need to flush the controller to
846 * get rid of the byte we put there; otherwise keyboard may not work.
847 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800848 dbg(" -- i8042 (aux irq test timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400849 i8042_flush();
850 retval = -1;
851 }
852
853 out:
854
855/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * Disable the interface.
857 */
858
859 i8042_ctr |= I8042_CTR_AUXDIS;
860 i8042_ctr &= ~I8042_CTR_AUXINT;
861
862 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400863 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400865 if (irq_registered)
866 free_irq(I8042_AUX_IRQ, i8042_platform_device);
867
868 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400871static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Andrey Moiseev2f0d2602013-09-16 15:17:31 -0700873 if (i8042_flush()) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800874 pr_err("No controller found\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400875 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400881static int i8042_controller_selftest(void)
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500882{
883 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700884 int i = 0;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500885
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700886 /*
887 * We try this 5 times; on some really fragile systems this does not
888 * take the first time...
889 */
890 do {
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500891
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700892 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
Paul Bollea2a94e72011-03-31 00:11:48 -0700893 pr_err("i8042 controller selftest timeout\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700894 return -ENODEV;
895 }
896
897 if (param == I8042_RET_CTL_TEST)
898 return 0;
899
Paul Bollea2a94e72011-03-31 00:11:48 -0700900 dbg("i8042 controller selftest: %#x != %#x\n",
901 param, I8042_RET_CTL_TEST);
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700902 msleep(50);
903 } while (i++ < 5);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500904
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700905#ifdef CONFIG_X86
906 /*
907 * On x86, we don't fail entire i8042 initialization if controller
908 * reset fails in hopes that keyboard port will still be functional
909 * and user will still get a working keyboard. This is especially
910 * important on netbooks. On other arches we trust hardware more.
911 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800912 pr_info("giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500913 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700914#else
Paul Bollea2a94e72011-03-31 00:11:48 -0700915 pr_err("i8042 controller selftest failed\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700916 return -EIO;
917#endif
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500918}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920/*
921 * i8042_controller init initializes the i8042 controller, and,
922 * most importantly, sets it into non-xlated mode if that's
923 * desired.
924 */
925
926static int i8042_controller_init(void)
927{
928 unsigned long flags;
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800929 int n = 0;
930 unsigned char ctr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932/*
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800933 * Save the CTR for restore on unload / reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
935
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800936 do {
937 if (n >= 10) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800938 pr_err("Unable to get stable CTR read\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800939 return -EIO;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800942 if (n != 0)
943 udelay(50);
944
945 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800946 pr_err("Can't read CTR while initializing i8042\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800947 return -EIO;
948 }
949
950 } while (n < 2 || ctr[0] != ctr[1]);
951
952 i8042_initial_ctr = i8042_ctr = ctr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954/*
955 * Disable the keyboard interface and interrupt.
956 */
957
958 i8042_ctr |= I8042_CTR_KBDDIS;
959 i8042_ctr &= ~I8042_CTR_KBDINT;
960
961/*
962 * Handle keylock.
963 */
964
965 spin_lock_irqsave(&i8042_lock, flags);
966 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
967 if (i8042_unlock)
968 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500969 else
Joe Perches4eb3c302010-11-29 23:33:07 -0800970 pr_warn("Warning: Keylock active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 spin_unlock_irqrestore(&i8042_lock, flags);
973
974/*
975 * If the chip is configured into nontranslated mode by the BIOS, don't
976 * bother enabling translating and be happy.
977 */
978
979 if (~i8042_ctr & I8042_CTR_XLATE)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700980 i8042_direct = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982/*
983 * Set nontranslated mode for the kbd interface if requested by an option.
984 * After this the kbd interface becomes a simple serial in/out, like the aux
985 * interface is. We don't do this by default, since it can confuse notebook
986 * BIOSes.
987 */
988
989 if (i8042_direct)
990 i8042_ctr &= ~I8042_CTR_XLATE;
991
992/*
993 * Write CTR back.
994 */
995
996 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800997 pr_err("Can't write CTR while initializing i8042\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400998 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001001/*
1002 * Flush whatever accumulated while we were disabling keyboard port.
1003 */
1004
1005 i8042_flush();
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return 0;
1008}
1009
1010
1011/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001012 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 */
1014
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001015static void i8042_controller_reset(bool force_reset)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001016{
1017 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001020 * Disable both KBD and AUX interfaces so they don't get in the way
1021 */
1022
1023 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1024 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1025
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001026 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001027 pr_warn("Can't write CTR while resetting\n");
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001028
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * Disable MUX mode if present.
1031 */
1032
1033 if (i8042_mux_present)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001034 i8042_set_mux_mode(false, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001037 * Reset the controller if requested.
1038 */
1039
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001040 if (i8042_reset || force_reset)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001041 i8042_controller_selftest();
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001042
1043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * Restore the original control register setting.
1045 */
1046
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001047 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001048 pr_warn("Can't restore CTR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051
1052/*
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001053 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1054 * when kernel panics. Flashing LEDs is useful for users running X who may
Michael Opdenackeraa5e5dc2013-09-18 06:00:43 +02001055 * not see the console and will help distinguishing panics from "real"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 * lockups.
1057 *
1058 * Note that DELAY has a limit of 10ms so we will not get stuck here
1059 * waiting for KBC to free up even if KBD interrupt is off
1060 */
1061
1062#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1063
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001064static long i8042_panic_blink(int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 long delay = 0;
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001067 char led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001069 led = (state) ? 0x01 | 0x04 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 while (i8042_read_status() & I8042_STR_IBF)
1071 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001072 dbg("%02x -> i8042 (panic blink)\n", 0xed);
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -05001073 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 i8042_write_data(0xed); /* set leds */
1075 DELAY;
1076 while (i8042_read_status() & I8042_STR_IBF)
1077 DELAY;
1078 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001079 dbg("%02x -> i8042 (panic blink)\n", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 i8042_write_data(led);
1081 DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return delay;
1083}
1084
1085#undef DELAY
1086
Bruno Prémontd35895d2008-05-27 01:36:04 -04001087#ifdef CONFIG_X86
1088static void i8042_dritek_enable(void)
1089{
Christoph Fritz594d6362010-09-29 18:04:21 -07001090 unsigned char param = 0x90;
Bruno Prémontd35895d2008-05-27 01:36:04 -04001091 int error;
1092
1093 error = i8042_command(&param, 0x1059);
1094 if (error)
Joe Perches4eb3c302010-11-29 23:33:07 -08001095 pr_warn("Failed to enable DRITEK extension: %d\n", error);
Bruno Prémontd35895d2008-05-27 01:36:04 -04001096}
1097#endif
1098
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001099#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001102 * Here we try to reset everything back to a state we had
1103 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
1105
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001106static int i8042_controller_resume(bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001108 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001110 error = i8042_controller_check();
1111 if (error)
1112 return error;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001113
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001114 if (i8042_reset || force_reset) {
1115 error = i8042_controller_selftest();
1116 if (error)
1117 return error;
1118 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001119
1120/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001121 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001122 */
1123
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001124 i8042_ctr = i8042_initial_ctr;
1125 if (i8042_direct)
1126 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001127 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1128 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -05001129 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001130 pr_warn("Can't write CTR to resume, retrying...\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001131 msleep(50);
1132 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001133 pr_err("CTR write retry failed\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001134 return -EIO;
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Bruno Prémontd35895d2008-05-27 01:36:04 -04001138
1139#ifdef CONFIG_X86
1140 if (i8042_dritek)
1141 i8042_dritek_enable();
1142#endif
1143
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001144 if (i8042_mux_present) {
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001145 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
Joe Perches4eb3c302010-11-29 23:33:07 -08001146 pr_warn("failed to resume active multiplexor, mouse won't work\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001147 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1148 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001150 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1151 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
David Howells7d12e782006-10-05 14:55:46 +01001153 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001157
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001158/*
1159 * Here we try to restore the original BIOS settings to avoid
1160 * upsetting it.
1161 */
1162
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001163static int i8042_pm_suspend(struct device *dev)
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001164{
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001165 int i;
1166
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001167 i8042_controller_reset(true);
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001168
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001169 /* Set up serio interrupts for system wakeup. */
1170 for (i = 0; i < I8042_NUM_PORTS; i++) {
1171 struct serio *serio = i8042_ports[i].serio;
1172
1173 if (serio && device_may_wakeup(&serio->dev))
1174 enable_irq_wake(i8042_ports[i].irq);
1175 }
1176
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001177 return 0;
1178}
1179
1180static int i8042_pm_resume(struct device *dev)
1181{
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001182 int i;
1183
1184 for (i = 0; i < I8042_NUM_PORTS; i++) {
1185 struct serio *serio = i8042_ports[i].serio;
1186
1187 if (serio && device_may_wakeup(&serio->dev))
1188 disable_irq_wake(i8042_ports[i].irq);
1189 }
1190
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001191 /*
1192 * On resume from S2R we always try to reset the controller
1193 * to bring it in a sane state. (In case of S2D we expect
1194 * BIOS to reset the controller for us.)
1195 */
1196 return i8042_controller_resume(true);
1197}
1198
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001199static int i8042_pm_thaw(struct device *dev)
1200{
1201 i8042_interrupt(0, NULL);
1202
1203 return 0;
1204}
1205
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001206static int i8042_pm_reset(struct device *dev)
1207{
1208 i8042_controller_reset(false);
1209
1210 return 0;
1211}
1212
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001213static int i8042_pm_restore(struct device *dev)
1214{
1215 return i8042_controller_resume(false);
1216}
1217
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001218static const struct dev_pm_ops i8042_pm_ops = {
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001219 .suspend = i8042_pm_suspend,
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001220 .resume = i8042_pm_resume,
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001221 .thaw = i8042_pm_thaw,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001222 .poweroff = i8042_pm_reset,
1223 .restore = i8042_pm_restore,
1224};
1225
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001226#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228/*
1229 * We need to reset the 8042 back to original mode on system shutdown,
1230 * because otherwise BIOSes will be confused.
1231 */
1232
Russell King3ae5eae2005-11-09 22:32:44 +00001233static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001235 i8042_controller_reset(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001238static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct serio *serio;
1241 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1242
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001243 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001244 if (!serio)
1245 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001247 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1248 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001249 serio->start = i8042_start;
1250 serio->stop = i8042_stop;
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001251 serio->close = i8042_port_close;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001252 serio->port_data = port;
1253 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001254 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001255 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001256 strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
1257 sizeof(serio->firmware_id));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001258
1259 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001260 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001261
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001265static int __init i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001268 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1269 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
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 = SERIO_8042;
1276 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001277 serio->start = i8042_start;
1278 serio->stop = i8042_stop;
1279 serio->port_data = port;
1280 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001281 if (idx < 0) {
1282 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1283 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
Hans de Goedea7c58682014-04-19 20:47:35 -07001284 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1285 sizeof(serio->firmware_id));
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001286 serio->close = i8042_port_close;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001287 } else {
1288 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1289 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
Hans de Goede266e43c2014-09-11 10:13:13 -07001290 strlcpy(serio->firmware_id, i8042_aux_firmware_id,
1291 sizeof(serio->firmware_id));
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001292 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001293
1294 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001295 port->mux = idx;
1296 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001297
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001301static void __init i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001303 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1304 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001307static void __init i8042_free_aux_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001308{
1309 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001311 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1312 kfree(i8042_ports[i].serio);
1313 i8042_ports[i].serio = NULL;
1314 }
1315}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001316
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001317static void __init i8042_register_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001318{
1319 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001320
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001321 for (i = 0; i < I8042_NUM_PORTS; i++) {
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001322 struct serio *serio = i8042_ports[i].serio;
1323
1324 if (serio) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001325 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001326 serio->name,
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001327 (unsigned long) I8042_DATA_REG,
1328 (unsigned long) I8042_COMMAND_REG,
1329 i8042_ports[i].irq);
Rafael J. Wysockif13b2062015-03-09 17:03:07 -07001330 serio_register_port(serio);
1331 device_set_wakeup_capable(&serio->dev, true);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001332 }
1333 }
1334}
1335
Bill Pembertone2619cf2012-11-23 21:50:47 -08001336static void i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001337{
1338 int i;
1339
1340 for (i = 0; i < I8042_NUM_PORTS; i++) {
1341 if (i8042_ports[i].serio) {
1342 serio_unregister_port(i8042_ports[i].serio);
1343 i8042_ports[i].serio = NULL;
1344 }
1345 }
1346}
1347
Dmitry Torokhov181d6832009-09-16 01:06:43 -07001348/*
1349 * Checks whether port belongs to i8042 controller.
1350 */
1351bool i8042_check_port_owner(const struct serio *port)
1352{
1353 int i;
1354
1355 for (i = 0; i < I8042_NUM_PORTS; i++)
1356 if (i8042_ports[i].serio == port)
1357 return true;
1358
1359 return false;
1360}
1361EXPORT_SYMBOL(i8042_check_port_owner);
1362
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001363static void i8042_free_irqs(void)
1364{
1365 if (i8042_aux_irq_registered)
1366 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1367 if (i8042_kbd_irq_registered)
1368 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1369
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001370 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001371}
1372
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001373static int __init i8042_setup_aux(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001374{
1375 int (*aux_enable)(void);
1376 int error;
1377 int i;
1378
1379 if (i8042_check_aux())
1380 return -ENODEV;
1381
1382 if (i8042_nomux || i8042_check_mux()) {
1383 error = i8042_create_aux_port(-1);
1384 if (error)
1385 goto err_free_ports;
1386 aux_enable = i8042_enable_aux_port;
1387 } else {
1388 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1389 error = i8042_create_aux_port(i);
1390 if (error)
1391 goto err_free_ports;
1392 }
1393 aux_enable = i8042_enable_mux_ports;
1394 }
1395
1396 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1397 "i8042", i8042_platform_device);
1398 if (error)
1399 goto err_free_ports;
1400
1401 if (aux_enable())
1402 goto err_free_irq;
1403
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001404 i8042_aux_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001405 return 0;
1406
1407 err_free_irq:
1408 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1409 err_free_ports:
1410 i8042_free_aux_ports();
1411 return error;
1412}
1413
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001414static int __init i8042_setup_kbd(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001415{
1416 int error;
1417
1418 error = i8042_create_kbd_port();
1419 if (error)
1420 return error;
1421
1422 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1423 "i8042", i8042_platform_device);
1424 if (error)
1425 goto err_free_port;
1426
1427 error = i8042_enable_kbd_port();
1428 if (error)
1429 goto err_free_irq;
1430
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001431 i8042_kbd_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001432 return 0;
1433
1434 err_free_irq:
1435 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1436 err_free_port:
1437 i8042_free_kbd_port();
1438 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001441static int __init i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001443 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001445 i8042_platform_device = dev;
1446
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001447 if (i8042_reset) {
1448 error = i8042_controller_selftest();
1449 if (error)
1450 return error;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001453 error = i8042_controller_init();
1454 if (error)
1455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Bruno Prémontd35895d2008-05-27 01:36:04 -04001457#ifdef CONFIG_X86
1458 if (i8042_dritek)
1459 i8042_dritek_enable();
1460#endif
1461
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001462 if (!i8042_noaux) {
1463 error = i8042_setup_aux();
1464 if (error && error != -ENODEV && error != -EBUSY)
1465 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001468 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001469 error = i8042_setup_kbd();
1470 if (error)
1471 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001472 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001473/*
1474 * Ok, everything is ready, let's register all serio ports
1475 */
1476 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001479
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001480 out_fail:
1481 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1482 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001483 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001484 i8042_platform_device = NULL;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001485
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001486 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Bill Pembertone2619cf2012-11-23 21:50:47 -08001489static int i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001491 i8042_unregister_ports();
1492 i8042_free_irqs();
Dmitry Torokhov1729ad12011-10-29 12:37:06 -07001493 i8042_controller_reset(false);
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001494 i8042_platform_device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001496 return 0;
1497}
1498
1499static struct platform_driver i8042_driver = {
1500 .driver = {
1501 .name = "i8042",
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001502#ifdef CONFIG_PM
1503 .pm = &i8042_pm_ops,
1504#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001505 },
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001506 .remove = i8042_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001507 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001508};
1509
1510static int __init i8042_init(void)
1511{
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001512 struct platform_device *pdev;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001513 int err;
1514
1515 dbg_init();
1516
1517 err = i8042_platform_init();
1518 if (err)
1519 return err;
1520
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001521 err = i8042_controller_check();
1522 if (err)
1523 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001524
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001525 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1526 if (IS_ERR(pdev)) {
1527 err = PTR_ERR(pdev);
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001528 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001529 }
1530
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001531 panic_blink = i8042_panic_blink;
1532
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001533 return 0;
1534
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001535 err_platform_exit:
1536 i8042_platform_exit();
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001537 return err;
1538}
1539
1540static void __exit i8042_exit(void)
1541{
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001542 platform_device_unregister(i8042_platform_device);
Dmitry Torokhovaf045b82010-08-31 17:27:02 -07001543 platform_driver_unregister(&i8042_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 i8042_platform_exit();
1545
1546 panic_blink = NULL;
1547}
1548
1549module_init(i8042_init);
1550module_exit(i8042_exit);