blob: c04ff00a3663e7864e28ad3d69c12ff0d40e5538 [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 Torokhov386b3842009-09-09 19:08:16 -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
Carlos Corbacho8987fec2008-01-21 01:04:40 -050066#ifdef CONFIG_X86
Dmitry Torokhov386b3842009-09-09 19:08:16 -070067static bool i8042_dritek;
Carlos Corbacho8987fec2008-01-21 01:04:40 -050068module_param_named(dritek, i8042_dritek, bool, 0);
69MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
70#endif
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#ifdef CONFIG_PNP
Dmitry Torokhov386b3842009-09-09 19:08:16 -070073static bool i8042_nopnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074module_param_named(nopnp, i8042_nopnp, bool, 0);
75MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
76#endif
77
78#define DEBUG
79#ifdef DEBUG
Dmitry Torokhov386b3842009-09-09 19:08:16 -070080static bool i8042_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081module_param_named(debug, i8042_debug, bool, 0600);
82MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
83#endif
84
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -070085static bool i8042_bypass_aux_irq_test;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include "i8042.h"
88
Dmitry Torokhov181d6832009-09-16 01:06:43 -070089/*
90 * i8042_lock protects serialization between i8042_command and
91 * the interrupt handler.
92 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static DEFINE_SPINLOCK(i8042_lock);
94
Dmitry Torokhov181d6832009-09-16 01:06:43 -070095/*
96 * Writers to AUX and KBD ports as well as users issuing i8042_command
97 * directly should acquire i8042_mutex (by means of calling
98 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
99 * they do not disturb each other (unfortunately in many i8042
100 * implementations write to one of the ports will immediately abort
101 * command that is being processed by another port).
102 */
103static DEFINE_MUTEX(i8042_mutex);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105struct i8042_port {
106 struct serio *serio;
107 int irq;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700108 bool exists;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 signed char mux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112#define I8042_KBD_PORT_NO 0
113#define I8042_AUX_PORT_NO 1
114#define I8042_MUX_PORT_NO 2
115#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400116
117static struct i8042_port i8042_ports[I8042_NUM_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119static unsigned char i8042_initial_ctr;
120static unsigned char i8042_ctr;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700121static bool i8042_mux_present;
122static bool i8042_kbd_irq_registered;
123static bool i8042_aux_irq_registered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400124static unsigned char i8042_suppress_kbd_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static struct platform_device *i8042_platform_device;
126
David Howells7d12e782006-10-05 14:55:46 +0100127static irqreturn_t i8042_interrupt(int irq, void *dev_id);
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800128static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
129 struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700131void i8042_lock_chip(void)
132{
133 mutex_lock(&i8042_mutex);
134}
135EXPORT_SYMBOL(i8042_lock_chip);
136
137void i8042_unlock_chip(void)
138{
139 mutex_unlock(&i8042_mutex);
140}
141EXPORT_SYMBOL(i8042_unlock_chip);
142
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800143int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
144 struct serio *serio))
145{
146 unsigned long flags;
147 int ret = 0;
148
149 spin_lock_irqsave(&i8042_lock, flags);
150
151 if (i8042_platform_filter) {
152 ret = -EBUSY;
153 goto out;
154 }
155
156 i8042_platform_filter = filter;
157
158out:
159 spin_unlock_irqrestore(&i8042_lock, flags);
160 return ret;
161}
162EXPORT_SYMBOL(i8042_install_filter);
163
164int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
165 struct serio *port))
166{
167 unsigned long flags;
168 int ret = 0;
169
170 spin_lock_irqsave(&i8042_lock, flags);
171
172 if (i8042_platform_filter != filter) {
173 ret = -EINVAL;
174 goto out;
175 }
176
177 i8042_platform_filter = NULL;
178
179out:
180 spin_unlock_irqrestore(&i8042_lock, flags);
181 return ret;
182}
183EXPORT_SYMBOL(i8042_remove_filter);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
187 * be ready for reading values from it / writing values to it.
188 * Called always with i8042_lock held.
189 */
190
191static int i8042_wait_read(void)
192{
193 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
196 udelay(50);
197 i++;
198 }
199 return -(i == I8042_CTL_TIMEOUT);
200}
201
202static int i8042_wait_write(void)
203{
204 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
207 udelay(50);
208 i++;
209 }
210 return -(i == I8042_CTL_TIMEOUT);
211}
212
213/*
214 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
215 * of the i8042 down the toilet.
216 */
217
218static int i8042_flush(void)
219{
220 unsigned long flags;
221 unsigned char data, str;
222 int i = 0;
223
224 spin_lock_irqsave(&i8042_lock, flags);
225
226 while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
227 udelay(50);
228 data = i8042_read_data();
229 i++;
Joe Perches4eb3c302010-11-29 23:33:07 -0800230 dbg("%02x <- i8042 (flush, %s)\n",
231 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
234 spin_unlock_irqrestore(&i8042_lock, flags);
235
236 return i;
237}
238
239/*
240 * i8042_command() executes a command on the i8042. It also sends the input
241 * parameter(s) of the commands to it, and receives the output value(s). The
242 * parameters are to be stored in the param array, and the output is placed
243 * into the same array. The number of the parameters and output values is
244 * encoded in bits 8-11 of the command number.
245 */
246
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400247static int __i8042_command(unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400249 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
252 return -1;
253
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400254 error = i8042_wait_write();
255 if (error)
256 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500257
Joe Perches4eb3c302010-11-29 23:33:07 -0800258 dbg("%02x -> i8042 (command)\n", command & 0xff);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500259 i8042_write_command(command & 0xff);
260
261 for (i = 0; i < ((command >> 12) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400262 error = i8042_wait_write();
263 if (error)
264 return error;
Joe Perches4eb3c302010-11-29 23:33:07 -0800265 dbg("%02x -> i8042 (parameter)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500266 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500269 for (i = 0; i < ((command >> 8) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400270 error = i8042_wait_read();
271 if (error) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800272 dbg(" -- i8042 (timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400273 return error;
274 }
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500275
276 if (command == I8042_CMD_AUX_LOOP &&
277 !(i8042_read_status() & I8042_STR_AUXDATA)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800278 dbg(" -- i8042 (auxerr)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400279 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500282 param[i] = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800283 dbg("%02x <- i8042 (return)\n", param[i]);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400286 return 0;
287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Márton Németh553a05b2007-10-22 00:56:52 -0400289int i8042_command(unsigned char *param, int command)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400290{
291 unsigned long flags;
292 int retval;
293
294 spin_lock_irqsave(&i8042_lock, flags);
295 retval = __i8042_command(param, command);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500296 spin_unlock_irqrestore(&i8042_lock, flags);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return retval;
299}
Márton Németh553a05b2007-10-22 00:56:52 -0400300EXPORT_SYMBOL(i8042_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302/*
303 * i8042_kbd_write() sends a byte out through the keyboard interface.
304 */
305
306static int i8042_kbd_write(struct serio *port, unsigned char c)
307{
308 unsigned long flags;
309 int retval = 0;
310
311 spin_lock_irqsave(&i8042_lock, flags);
312
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400313 if (!(retval = i8042_wait_write())) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800314 dbg("%02x -> i8042 (kbd-data)\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 i8042_write_data(c);
316 }
317
318 spin_unlock_irqrestore(&i8042_lock, flags);
319
320 return retval;
321}
322
323/*
324 * i8042_aux_write() sends a byte out through the aux interface.
325 */
326
327static int i8042_aux_write(struct serio *serio, unsigned char c)
328{
329 struct i8042_port *port = serio->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dmitry Torokhovf4e3c712006-11-02 23:27:49 -0500331 return i8042_command(&c, port->mux == -1 ?
332 I8042_CMD_AUX_SEND :
333 I8042_CMD_MUX_SEND + port->mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700336
337/*
338 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
339 * and then re-enabling it.
340 */
341
342static void i8042_port_close(struct serio *serio)
343{
344 int irq_bit;
345 int disable_bit;
346 const char *port_name;
347
348 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
349 irq_bit = I8042_CTR_AUXINT;
350 disable_bit = I8042_CTR_AUXDIS;
351 port_name = "AUX";
352 } else {
353 irq_bit = I8042_CTR_KBDINT;
354 disable_bit = I8042_CTR_KBDDIS;
355 port_name = "KBD";
356 }
357
358 i8042_ctr &= ~irq_bit;
359 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800360 pr_warn("Can't write CTR while closing %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700361
362 udelay(50);
363
364 i8042_ctr &= ~disable_bit;
365 i8042_ctr |= irq_bit;
366 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -0800367 pr_err("Can't reactivate %s port\n", port_name);
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700368
369 /*
370 * See if there is any data appeared while we were messing with
371 * port state.
372 */
373 i8042_interrupt(0, NULL);
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * i8042_start() is called by serio core when port is about to finish
378 * registering. It will mark port as existing so i8042_interrupt can
379 * start sending data through it.
380 */
381static int i8042_start(struct serio *serio)
382{
383 struct i8042_port *port = serio->port_data;
384
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700385 port->exists = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 mb();
387 return 0;
388}
389
390/*
391 * i8042_stop() marks serio port as non-existing so i8042_interrupt
392 * will not try to send data to the port that is about to go away.
393 * The function is called by serio core as part of unregister procedure.
394 */
395static void i8042_stop(struct serio *serio)
396{
397 struct i8042_port *port = serio->port_data;
398
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700399 port->exists = false;
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400400
401 /*
402 * We synchronize with both AUX and KBD IRQs because there is
403 * a (very unlikely) chance that AUX IRQ is raised for KBD port
404 * and vice versa.
405 */
406 synchronize_irq(I8042_AUX_IRQ);
407 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 port->serio = NULL;
409}
410
411/*
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800412 * i8042_filter() filters out unwanted bytes from the input data stream.
413 * It is called from i8042_interrupt and thus is running with interrupts
414 * off and i8042_lock held.
415 */
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800416static bool i8042_filter(unsigned char data, unsigned char str,
417 struct serio *serio)
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800418{
419 if (unlikely(i8042_suppress_kbd_ack)) {
420 if ((~str & I8042_STR_AUXDATA) &&
421 (data == 0xfa || data == 0xfe)) {
422 i8042_suppress_kbd_ack--;
423 dbg("Extra keyboard ACK - filtered out\n");
424 return true;
425 }
426 }
427
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800428 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
Stefan Weil0747e3b2010-01-07 00:44:08 +0100429 dbg("Filtered out by platform filter\n");
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800430 return true;
431 }
432
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800433 return false;
434}
435
436/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * i8042_interrupt() is the most important function in this driver -
438 * it handles the interrupts from the i8042, and sends incoming bytes
439 * to the upper layers.
440 */
441
David Howells7d12e782006-10-05 14:55:46 +0100442static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct i8042_port *port;
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800445 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 unsigned long flags;
447 unsigned char str, data;
448 unsigned int dfl;
449 unsigned int port_no;
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800450 bool filtered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400451 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 spin_lock_irqsave(&i8042_lock, flags);
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 str = i8042_read_status();
456 if (unlikely(~str & I8042_STR_OBF)) {
457 spin_unlock_irqrestore(&i8042_lock, flags);
Joe Perches4eb3c302010-11-29 23:33:07 -0800458 if (irq)
459 dbg("Interrupt %d, without any data\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 ret = 0;
461 goto out;
462 }
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 data = i8042_read_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
467 static unsigned long last_transmit;
468 static unsigned char last_str;
469
470 dfl = 0;
471 if (str & I8042_STR_MUXERR) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800472 dbg("MUX error, status is %02x, data is %02x\n",
473 str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
475 * When MUXERR condition is signalled the data register can only contain
476 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500477 * it is not always the case. Some KBCs also report 0xfc when there is
478 * nothing connected to the port while others sometimes get confused which
479 * port the data came from and signal error leaving the data intact. They
480 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
481 * to legacy mode yet, when we see one we'll add proper handling).
482 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
483 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * was transmitted (if transmission happened not too long ago).
485 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500486
487 switch (data) {
488 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (time_before(jiffies, last_transmit + HZ/10)) {
490 str = last_str;
491 break;
492 }
493 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500494 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 case 0xfd:
496 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
497 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
498 }
499 }
500
501 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
502 last_str = str;
503 last_transmit = jiffies;
504 } else {
505
506 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
507 ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
508
509 port_no = (str & I8042_STR_AUXDATA) ?
510 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
511 }
512
513 port = &i8042_ports[port_no];
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800514 serio = port->exists ? port->serio : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Joe Perches4eb3c302010-11-29 23:33:07 -0800516 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)\n",
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400517 data, port_no, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 dfl & SERIO_PARITY ? ", bad parity" : "",
519 dfl & SERIO_TIMEOUT ? ", timeout" : "");
520
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800521 filtered = i8042_filter(data, str, serio);
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400522
Dmitry Torokhov4e8d3402009-12-11 22:00:57 -0800523 spin_unlock_irqrestore(&i8042_lock, flags);
524
525 if (likely(port->exists && !filtered))
Matthew Garrett967c9ef2009-12-11 22:00:57 -0800526 serio_interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500528 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return IRQ_RETVAL(ret);
530}
531
532/*
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -0700533 * i8042_enable_kbd_port enables keyboard port on chip
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400534 */
535
536static int i8042_enable_kbd_port(void)
537{
538 i8042_ctr &= ~I8042_CTR_KBDDIS;
539 i8042_ctr |= I8042_CTR_KBDINT;
540
541 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400542 i8042_ctr &= ~I8042_CTR_KBDINT;
543 i8042_ctr |= I8042_CTR_KBDDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800544 pr_err("Failed to enable KBD port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400545 return -EIO;
546 }
547
548 return 0;
549}
550
551/*
552 * i8042_enable_aux_port enables AUX (mouse) port on chip
553 */
554
555static int i8042_enable_aux_port(void)
556{
557 i8042_ctr &= ~I8042_CTR_AUXDIS;
558 i8042_ctr |= I8042_CTR_AUXINT;
559
560 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400561 i8042_ctr &= ~I8042_CTR_AUXINT;
562 i8042_ctr |= I8042_CTR_AUXDIS;
Joe Perches4eb3c302010-11-29 23:33:07 -0800563 pr_err("Failed to enable AUX port\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400564 return -EIO;
565 }
566
567 return 0;
568}
569
570/*
571 * i8042_enable_mux_ports enables 4 individual AUX ports after
572 * the controller has been switched into Multiplexed mode
573 */
574
575static int i8042_enable_mux_ports(void)
576{
577 unsigned char param;
578 int i;
579
580 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
581 i8042_command(&param, I8042_CMD_MUX_PFX + i);
582 i8042_command(&param, I8042_CMD_AUX_ENABLE);
583 }
584
585 return i8042_enable_aux_port();
586}
587
588/*
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700589 * i8042_set_mux_mode checks whether the controller has an
590 * active multiplexor and puts the chip into Multiplexed (true)
591 * or Legacy (false) mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
593
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700594static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700597 unsigned char param, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
599 * Get rid of bytes in the queue.
600 */
601
602 i8042_flush();
603
604/*
605 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400606 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700609 param = val = 0xf0;
610 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700612 param = val = multiplex ? 0x56 : 0xf6;
613 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700615 param = val = multiplex ? 0xa4 : 0xa5;
616 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
617 return -1;
618
619/*
620 * Workaround for interference with USB Legacy emulation
621 * that causes a v10.12 MUX to be found.
622 */
623 if (param == 0xac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -1;
625
626 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500627 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return 0;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * i8042_check_mux() checks whether the controller supports the PS/2 Active
634 * Multiplexing specification by Synaptics, Phoenix, Insyde and
635 * LCS/Telegraphics.
636 */
637
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700638static int __init i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 unsigned char mux_version;
641
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700642 if (i8042_set_mux_mode(true, &mux_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -1;
644
Joe Perches4eb3c302010-11-29 23:33:07 -0800645 pr_info("Detected active multiplexing controller, rev %d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 (mux_version >> 4) & 0xf, mux_version & 0xf);
647
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400648/*
649 * Disable all muxed ports by disabling AUX.
650 */
651 i8042_ctr |= I8042_CTR_AUXDIS;
652 i8042_ctr &= ~I8042_CTR_AUXINT;
653
654 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800655 pr_err("Failed to disable AUX port, can't use MUX\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400656 return -EIO;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700659 i8042_mux_present = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return 0;
662}
663
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400664/*
665 * The following is used to test AUX IRQ delivery.
666 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700667static struct completion i8042_aux_irq_delivered __initdata;
668static bool i8042_irq_being_tested __initdata;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400669
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700670static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400671{
672 unsigned long flags;
673 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400674 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400675
676 spin_lock_irqsave(&i8042_lock, flags);
677 str = i8042_read_status();
678 if (str & I8042_STR_OBF) {
679 data = i8042_read_data();
Joe Perches4eb3c302010-11-29 23:33:07 -0800680 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
681 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400682 if (i8042_irq_being_tested &&
683 data == 0xa5 && (str & I8042_STR_AUXDATA))
684 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400685 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400686 }
687 spin_unlock_irqrestore(&i8042_lock, flags);
688
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400689 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400690}
691
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400692/*
693 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
694 * verifies success by readinng CTR. Used when testing for presence of AUX
695 * port.
696 */
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700697static int __init i8042_toggle_aux(bool on)
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400698{
699 unsigned char param;
700 int i;
701
702 if (i8042_command(&param,
703 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
704 return -1;
705
706 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
707 for (i = 0; i < 100; i++) {
708 udelay(50);
709
710 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
711 return -1;
712
713 if (!(param & I8042_CTR_AUXDIS) == on)
714 return 0;
715 }
716
717 return -1;
718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720/*
721 * i8042_check_aux() applies as much paranoia as it can at detecting
722 * the presence of an AUX interface.
723 */
724
Dmitry Torokhovf8113412009-09-09 19:08:17 -0700725static int __init i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400727 int retval = -1;
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700728 bool irq_registered = false;
729 bool aux_loop_broken = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400730 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733/*
734 * Get rid of bytes in the queue.
735 */
736
737 i8042_flush();
738
739/*
740 * Internal loopback test - filters out AT-type i8042's. Unfortunately
741 * SiS screwed up and their 5597 doesn't support the LOOP command even
742 * though it has an AUX port.
743 */
744
745 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500746 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
747 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/*
750 * External connection test - filters out AT-soldered PS/2 i8042's
751 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
752 * 0xfa - no error on some notebooks which ignore the spec
753 * Because it's common for chipsets to return error on perfectly functioning
754 * AUX ports, we test for this only when the LOOP command failed.
755 */
756
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400757 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
758 (param && param != 0xfa && param != 0xff))
759 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500760
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500761/*
762 * If AUX_LOOP completed without error but returned unexpected data
763 * mark it as broken
764 */
765 if (!retval)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700766 aux_loop_broken = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769/*
770 * Bit assignment test - filters out PS/2 i8042's in AT mode
771 */
772
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700773 if (i8042_toggle_aux(false)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800774 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
775 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700778 if (i8042_toggle_aux(true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -1;
780
781/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400782 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
783 * used it for a PCI card or somethig else.
784 */
785
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700786 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400787/*
788 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
789 * is working and hope we are right.
790 */
791 retval = 0;
792 goto out;
793 }
794
795 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
796 "i8042", i8042_platform_device))
797 goto out;
798
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700799 irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400800
801 if (i8042_enable_aux_port())
802 goto out;
803
804 spin_lock_irqsave(&i8042_lock, flags);
805
806 init_completion(&i8042_aux_irq_delivered);
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700807 i8042_irq_being_tested = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400808
809 param = 0xa5;
810 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
811
812 spin_unlock_irqrestore(&i8042_lock, flags);
813
814 if (retval)
815 goto out;
816
817 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
818 msecs_to_jiffies(250)) == 0) {
819/*
820 * AUX IRQ was never delivered so we need to flush the controller to
821 * get rid of the byte we put there; otherwise keyboard may not work.
822 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800823 dbg(" -- i8042 (aux irq test timeout)\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400824 i8042_flush();
825 retval = -1;
826 }
827
828 out:
829
830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * Disable the interface.
832 */
833
834 i8042_ctr |= I8042_CTR_AUXDIS;
835 i8042_ctr &= ~I8042_CTR_AUXINT;
836
837 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400838 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400840 if (irq_registered)
841 free_irq(I8042_AUX_IRQ, i8042_platform_device);
842
843 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400846static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400848 if (i8042_flush() == I8042_BUFFER_SIZE) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800849 pr_err("No controller found\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400850 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854}
855
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400856static int i8042_controller_selftest(void)
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500857{
858 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700859 int i = 0;
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500860
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700861 /*
862 * We try this 5 times; on some really fragile systems this does not
863 * take the first time...
864 */
865 do {
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500866
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700867 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800868 pr_err("i8042 controller self test timeout\n");
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700869 return -ENODEV;
870 }
871
872 if (param == I8042_RET_CTL_TEST)
873 return 0;
874
Joe Perches4eb3c302010-11-29 23:33:07 -0800875 pr_err("i8042 controller selftest failed. (%#x != %#x)\n",
876 param, I8042_RET_CTL_TEST);
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700877 msleep(50);
878 } while (i++ < 5);
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500879
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700880#ifdef CONFIG_X86
881 /*
882 * On x86, we don't fail entire i8042 initialization if controller
883 * reset fails in hopes that keyboard port will still be functional
884 * and user will still get a working keyboard. This is especially
885 * important on netbooks. On other arches we trust hardware more.
886 */
Joe Perches4eb3c302010-11-29 23:33:07 -0800887 pr_info("giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500888 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700889#else
890 return -EIO;
891#endif
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500892}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894/*
895 * i8042_controller init initializes the i8042 controller, and,
896 * most importantly, sets it into non-xlated mode if that's
897 * desired.
898 */
899
900static int i8042_controller_init(void)
901{
902 unsigned long flags;
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800903 int n = 0;
904 unsigned char ctr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906/*
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800907 * Save the CTR for restore on unload / reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
909
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800910 do {
911 if (n >= 10) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800912 pr_err("Unable to get stable CTR read\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800913 return -EIO;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800916 if (n != 0)
917 udelay(50);
918
919 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800920 pr_err("Can't read CTR while initializing i8042\n");
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800921 return -EIO;
922 }
923
924 } while (n < 2 || ctr[0] != ctr[1]);
925
926 i8042_initial_ctr = i8042_ctr = ctr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928/*
929 * Disable the keyboard interface and interrupt.
930 */
931
932 i8042_ctr |= I8042_CTR_KBDDIS;
933 i8042_ctr &= ~I8042_CTR_KBDINT;
934
935/*
936 * Handle keylock.
937 */
938
939 spin_lock_irqsave(&i8042_lock, flags);
940 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
941 if (i8042_unlock)
942 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500943 else
Joe Perches4eb3c302010-11-29 23:33:07 -0800944 pr_warn("Warning: Keylock active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 spin_unlock_irqrestore(&i8042_lock, flags);
947
948/*
949 * If the chip is configured into nontranslated mode by the BIOS, don't
950 * bother enabling translating and be happy.
951 */
952
953 if (~i8042_ctr & I8042_CTR_XLATE)
Dmitry Torokhov386b3842009-09-09 19:08:16 -0700954 i8042_direct = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956/*
957 * Set nontranslated mode for the kbd interface if requested by an option.
958 * After this the kbd interface becomes a simple serial in/out, like the aux
959 * interface is. We don't do this by default, since it can confuse notebook
960 * BIOSes.
961 */
962
963 if (i8042_direct)
964 i8042_ctr &= ~I8042_CTR_XLATE;
965
966/*
967 * Write CTR back.
968 */
969
970 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -0800971 pr_err("Can't write CTR while initializing i8042\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400972 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -0800975/*
976 * Flush whatever accumulated while we were disabling keyboard port.
977 */
978
979 i8042_flush();
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 0;
982}
983
984
985/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400986 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
988
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400989static void i8042_controller_reset(void)
990{
991 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -0400994 * Disable both KBD and AUX interfaces so they don't get in the way
995 */
996
997 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
998 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
999
Dmitry Torokhovee1e82c2009-11-02 21:57:40 -08001000 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001001 pr_warn("Can't write CTR while resetting\n");
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001002
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -04001003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * Disable MUX mode if present.
1005 */
1006
1007 if (i8042_mux_present)
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001008 i8042_set_mux_mode(false, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001011 * Reset the controller if requested.
1012 */
1013
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001014 if (i8042_reset)
1015 i8042_controller_selftest();
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001016
1017/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * Restore the original control register setting.
1019 */
1020
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001021 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Joe Perches4eb3c302010-11-29 23:33:07 -08001022 pr_warn("Can't restore CTR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025
1026/*
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001027 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1028 * when kernel panics. Flashing LEDs is useful for users running X who may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * not see the console and will help distingushing panics from "real"
1030 * lockups.
1031 *
1032 * Note that DELAY has a limit of 10ms so we will not get stuck here
1033 * waiting for KBC to free up even if KBD interrupt is off
1034 */
1035
1036#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1037
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001038static long i8042_panic_blink(int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 long delay = 0;
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001041 char led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -07001043 led = (state) ? 0x01 | 0x04 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 while (i8042_read_status() & I8042_STR_IBF)
1045 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001046 dbg("%02x -> i8042 (panic blink)\n", 0xed);
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -05001047 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 i8042_write_data(0xed); /* set leds */
1049 DELAY;
1050 while (i8042_read_status() & I8042_STR_IBF)
1051 DELAY;
1052 DELAY;
Joe Perches4eb3c302010-11-29 23:33:07 -08001053 dbg("%02x -> i8042 (panic blink)\n", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 i8042_write_data(led);
1055 DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return delay;
1057}
1058
1059#undef DELAY
1060
Bruno Prémontd35895d2008-05-27 01:36:04 -04001061#ifdef CONFIG_X86
1062static void i8042_dritek_enable(void)
1063{
Christoph Fritz594d6362010-09-29 18:04:21 -07001064 unsigned char param = 0x90;
Bruno Prémontd35895d2008-05-27 01:36:04 -04001065 int error;
1066
1067 error = i8042_command(&param, 0x1059);
1068 if (error)
Joe Perches4eb3c302010-11-29 23:33:07 -08001069 pr_warn("Failed to enable DRITEK extension: %d\n", error);
Bruno Prémontd35895d2008-05-27 01:36:04 -04001070}
1071#endif
1072
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001073#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -07001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001076 * Here we try to reset everything back to a state we had
1077 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 */
1079
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001080static int i8042_controller_resume(bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001082 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001084 error = i8042_controller_check();
1085 if (error)
1086 return error;
Vojtech Pavlik2673c832005-05-28 02:11:27 -05001087
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001088 if (i8042_reset || force_reset) {
1089 error = i8042_controller_selftest();
1090 if (error)
1091 return error;
1092 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001093
1094/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001095 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001096 */
1097
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001098 i8042_ctr = i8042_initial_ctr;
1099 if (i8042_direct)
1100 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001101 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1102 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c832005-05-28 02:11:27 -05001103 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001104 pr_warn("Can't write CTR to resume, retrying...\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001105 msleep(50);
1106 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Joe Perches4eb3c302010-11-29 23:33:07 -08001107 pr_err("CTR write retry failed\n");
Jiri Kosina2f6a77d2008-06-17 11:47:27 -04001108 return -EIO;
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111
Bruno Prémontd35895d2008-05-27 01:36:04 -04001112
1113#ifdef CONFIG_X86
1114 if (i8042_dritek)
1115 i8042_dritek_enable();
1116#endif
1117
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001118 if (i8042_mux_present) {
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001119 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
Joe Perches4eb3c302010-11-29 23:33:07 -08001120 pr_warn("failed to resume active multiplexor, mouse won't work\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001121 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1122 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001124 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1125 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
David Howells7d12e782006-10-05 14:55:46 +01001127 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001131
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001132/*
1133 * Here we try to restore the original BIOS settings to avoid
1134 * upsetting it.
1135 */
1136
1137static int i8042_pm_reset(struct device *dev)
1138{
1139 i8042_controller_reset();
1140
1141 return 0;
1142}
1143
1144static int i8042_pm_resume(struct device *dev)
1145{
1146 /*
1147 * On resume from S2R we always try to reset the controller
1148 * to bring it in a sane state. (In case of S2D we expect
1149 * BIOS to reset the controller for us.)
1150 */
1151 return i8042_controller_resume(true);
1152}
1153
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001154static int i8042_pm_thaw(struct device *dev)
1155{
1156 i8042_interrupt(0, NULL);
1157
1158 return 0;
1159}
1160
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001161static int i8042_pm_restore(struct device *dev)
1162{
1163 return i8042_controller_resume(false);
1164}
1165
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001166static const struct dev_pm_ops i8042_pm_ops = {
1167 .suspend = i8042_pm_reset,
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001168 .resume = i8042_pm_resume,
Alan Jenkinsc2d1a2a2010-02-17 12:17:33 -08001169 .thaw = i8042_pm_thaw,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001170 .poweroff = i8042_pm_reset,
1171 .restore = i8042_pm_restore,
1172};
1173
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001174#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176/*
1177 * We need to reset the 8042 back to original mode on system shutdown,
1178 * because otherwise BIOSes will be confused.
1179 */
1180
Russell King3ae5eae2005-11-09 22:32:44 +00001181static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001183 i8042_controller_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001186static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct serio *serio;
1189 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1190
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001191 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001192 if (!serio)
1193 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001195 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1196 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001197 serio->start = i8042_start;
1198 serio->stop = i8042_stop;
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001199 serio->close = i8042_port_close;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001200 serio->port_data = port;
1201 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001202 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001203 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1204
1205 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001206 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001207
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001211static int __init i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001214 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1215 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001217 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001218 if (!serio)
1219 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001221 serio->id.type = SERIO_8042;
1222 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001223 serio->start = i8042_start;
1224 serio->stop = i8042_stop;
1225 serio->port_data = port;
1226 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001227 if (idx < 0) {
1228 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1229 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
Dmitry Torokhov5ddbc772009-09-09 19:08:15 -07001230 serio->close = i8042_port_close;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001231 } else {
1232 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1233 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1234 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001235
1236 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001237 port->mux = idx;
1238 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001239
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001243static void __init i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001245 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1246 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001249static void __init i8042_free_aux_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001250{
1251 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001253 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1254 kfree(i8042_ports[i].serio);
1255 i8042_ports[i].serio = NULL;
1256 }
1257}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001258
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001259static void __init i8042_register_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001260{
1261 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001262
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001263 for (i = 0; i < I8042_NUM_PORTS; i++) {
1264 if (i8042_ports[i].serio) {
1265 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1266 i8042_ports[i].serio->name,
1267 (unsigned long) I8042_DATA_REG,
1268 (unsigned long) I8042_COMMAND_REG,
1269 i8042_ports[i].irq);
1270 serio_register_port(i8042_ports[i].serio);
1271 }
1272 }
1273}
1274
Ralf Baechle7a1904c2007-09-04 23:16:31 -04001275static void __devexit i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001276{
1277 int i;
1278
1279 for (i = 0; i < I8042_NUM_PORTS; i++) {
1280 if (i8042_ports[i].serio) {
1281 serio_unregister_port(i8042_ports[i].serio);
1282 i8042_ports[i].serio = NULL;
1283 }
1284 }
1285}
1286
Dmitry Torokhov181d6832009-09-16 01:06:43 -07001287/*
1288 * Checks whether port belongs to i8042 controller.
1289 */
1290bool i8042_check_port_owner(const struct serio *port)
1291{
1292 int i;
1293
1294 for (i = 0; i < I8042_NUM_PORTS; i++)
1295 if (i8042_ports[i].serio == port)
1296 return true;
1297
1298 return false;
1299}
1300EXPORT_SYMBOL(i8042_check_port_owner);
1301
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001302static void i8042_free_irqs(void)
1303{
1304 if (i8042_aux_irq_registered)
1305 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1306 if (i8042_kbd_irq_registered)
1307 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1308
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001309 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001310}
1311
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001312static int __init i8042_setup_aux(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001313{
1314 int (*aux_enable)(void);
1315 int error;
1316 int i;
1317
1318 if (i8042_check_aux())
1319 return -ENODEV;
1320
1321 if (i8042_nomux || i8042_check_mux()) {
1322 error = i8042_create_aux_port(-1);
1323 if (error)
1324 goto err_free_ports;
1325 aux_enable = i8042_enable_aux_port;
1326 } else {
1327 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1328 error = i8042_create_aux_port(i);
1329 if (error)
1330 goto err_free_ports;
1331 }
1332 aux_enable = i8042_enable_mux_ports;
1333 }
1334
1335 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1336 "i8042", i8042_platform_device);
1337 if (error)
1338 goto err_free_ports;
1339
1340 if (aux_enable())
1341 goto err_free_irq;
1342
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001343 i8042_aux_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001344 return 0;
1345
1346 err_free_irq:
1347 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1348 err_free_ports:
1349 i8042_free_aux_ports();
1350 return error;
1351}
1352
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001353static int __init i8042_setup_kbd(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001354{
1355 int error;
1356
1357 error = i8042_create_kbd_port();
1358 if (error)
1359 return error;
1360
1361 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1362 "i8042", i8042_platform_device);
1363 if (error)
1364 goto err_free_port;
1365
1366 error = i8042_enable_kbd_port();
1367 if (error)
1368 goto err_free_irq;
1369
Dmitry Torokhov386b3842009-09-09 19:08:16 -07001370 i8042_kbd_irq_registered = true;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001371 return 0;
1372
1373 err_free_irq:
1374 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1375 err_free_port:
1376 i8042_free_kbd_port();
1377 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001380static int __init i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001382 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001384 i8042_platform_device = dev;
1385
Dmitry Torokhov1ca56e52010-07-20 20:25:34 -07001386 if (i8042_reset) {
1387 error = i8042_controller_selftest();
1388 if (error)
1389 return error;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001392 error = i8042_controller_init();
1393 if (error)
1394 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Bruno Prémontd35895d2008-05-27 01:36:04 -04001396#ifdef CONFIG_X86
1397 if (i8042_dritek)
1398 i8042_dritek_enable();
1399#endif
1400
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001401 if (!i8042_noaux) {
1402 error = i8042_setup_aux();
1403 if (error && error != -ENODEV && error != -EBUSY)
1404 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001407 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001408 error = i8042_setup_kbd();
1409 if (error)
1410 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001411 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001412/*
1413 * Ok, everything is ready, let's register all serio ports
1414 */
1415 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001418
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001419 out_fail:
1420 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1421 i8042_free_irqs();
1422 i8042_controller_reset();
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001423 i8042_platform_device = NULL;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001424
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001425 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001428static int __devexit i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001430 i8042_unregister_ports();
1431 i8042_free_irqs();
1432 i8042_controller_reset();
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001433 i8042_platform_device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001435 return 0;
1436}
1437
1438static struct platform_driver i8042_driver = {
1439 .driver = {
1440 .name = "i8042",
1441 .owner = THIS_MODULE,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001442#ifdef CONFIG_PM
1443 .pm = &i8042_pm_ops,
1444#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001445 },
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001446 .remove = __devexit_p(i8042_remove),
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001447 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001448};
1449
1450static int __init i8042_init(void)
1451{
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001452 struct platform_device *pdev;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001453 int err;
1454
1455 dbg_init();
1456
1457 err = i8042_platform_init();
1458 if (err)
1459 return err;
1460
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001461 err = i8042_controller_check();
1462 if (err)
1463 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001464
Dmitry Torokhovec62e1c2010-03-08 22:37:09 -08001465 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1466 if (IS_ERR(pdev)) {
1467 err = PTR_ERR(pdev);
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001468 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001469 }
1470
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001471 panic_blink = i8042_panic_blink;
1472
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001473 return 0;
1474
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001475 err_platform_exit:
1476 i8042_platform_exit();
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001477 return err;
1478}
1479
1480static void __exit i8042_exit(void)
1481{
Dmitry Torokhovf8113412009-09-09 19:08:17 -07001482 platform_device_unregister(i8042_platform_device);
Dmitry Torokhovaf045b82010-08-31 17:27:02 -07001483 platform_driver_unregister(&i8042_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 i8042_platform_exit();
1485
1486 panic_blink = NULL;
1487}
1488
1489module_init(i8042_init);
1490module_exit(i8042_exit);