blob: b53a015bf8a596704144412d1e496edc9769e62c [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
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070013#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/delay.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/interrupt.h>
17#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/serio.h>
20#include <linux/err.h>
21#include <linux/rcupdate.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Márton Németh553a05b2007-10-22 00:56:52 -040023#include <linux/i8042.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/io.h>
26
27MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
29MODULE_LICENSE("GPL");
30
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -050031static unsigned int i8042_nokbd;
32module_param_named(nokbd, i8042_nokbd, bool, 0);
33MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static unsigned int i8042_noaux;
36module_param_named(noaux, i8042_noaux, bool, 0);
37MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
38
39static unsigned int i8042_nomux;
40module_param_named(nomux, i8042_nomux, bool, 0);
41MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing conrtoller is present.");
42
43static unsigned int i8042_unlock;
44module_param_named(unlock, i8042_unlock, bool, 0);
45MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
46
47static unsigned int i8042_reset;
48module_param_named(reset, i8042_reset, bool, 0);
49MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
50
51static unsigned int i8042_direct;
52module_param_named(direct, i8042_direct, bool, 0);
53MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
54
55static unsigned int i8042_dumbkbd;
56module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
57MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
58
59static unsigned int i8042_noloop;
60module_param_named(noloop, i8042_noloop, bool, 0);
61MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
62
63static unsigned int i8042_blink_frequency = 500;
64module_param_named(panicblink, i8042_blink_frequency, uint, 0600);
65MODULE_PARM_DESC(panicblink, "Frequency with which keyboard LEDs should blink when kernel panics");
66
Carlos Corbacho8987fec2008-01-21 01:04:40 -050067#ifdef CONFIG_X86
68static unsigned int i8042_dritek;
69module_param_named(dritek, i8042_dritek, bool, 0);
70MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
71#endif
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef CONFIG_PNP
74static int i8042_nopnp;
75module_param_named(nopnp, i8042_nopnp, bool, 0);
76MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
77#endif
78
79#define DEBUG
80#ifdef DEBUG
81static int i8042_debug;
82module_param_named(debug, i8042_debug, bool, 0600);
83MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
84#endif
85
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -070086static bool i8042_bypass_aux_irq_test;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include "i8042.h"
89
90static DEFINE_SPINLOCK(i8042_lock);
91
92struct i8042_port {
93 struct serio *serio;
94 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unsigned char exists;
96 signed char mux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
99#define I8042_KBD_PORT_NO 0
100#define I8042_AUX_PORT_NO 1
101#define I8042_MUX_PORT_NO 2
102#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400103
104static struct i8042_port i8042_ports[I8042_NUM_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static unsigned char i8042_initial_ctr;
107static unsigned char i8042_ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static unsigned char i8042_mux_present;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400109static unsigned char i8042_kbd_irq_registered;
110static unsigned char i8042_aux_irq_registered;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400111static unsigned char i8042_suppress_kbd_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct platform_device *i8042_platform_device;
113
David Howells7d12e782006-10-05 14:55:46 +0100114static irqreturn_t i8042_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
118 * be ready for reading values from it / writing values to it.
119 * Called always with i8042_lock held.
120 */
121
122static int i8042_wait_read(void)
123{
124 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
127 udelay(50);
128 i++;
129 }
130 return -(i == I8042_CTL_TIMEOUT);
131}
132
133static int i8042_wait_write(void)
134{
135 int i = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
138 udelay(50);
139 i++;
140 }
141 return -(i == I8042_CTL_TIMEOUT);
142}
143
144/*
145 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
146 * of the i8042 down the toilet.
147 */
148
149static int i8042_flush(void)
150{
151 unsigned long flags;
152 unsigned char data, str;
153 int i = 0;
154
155 spin_lock_irqsave(&i8042_lock, flags);
156
157 while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
158 udelay(50);
159 data = i8042_read_data();
160 i++;
161 dbg("%02x <- i8042 (flush, %s)", data,
162 str & I8042_STR_AUXDATA ? "aux" : "kbd");
163 }
164
165 spin_unlock_irqrestore(&i8042_lock, flags);
166
167 return i;
168}
169
170/*
171 * i8042_command() executes a command on the i8042. It also sends the input
172 * parameter(s) of the commands to it, and receives the output value(s). The
173 * parameters are to be stored in the param array, and the output is placed
174 * into the same array. The number of the parameters and output values is
175 * encoded in bits 8-11 of the command number.
176 */
177
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400178static int __i8042_command(unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400180 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
183 return -1;
184
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400185 error = i8042_wait_write();
186 if (error)
187 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500188
189 dbg("%02x -> i8042 (command)", command & 0xff);
190 i8042_write_command(command & 0xff);
191
192 for (i = 0; i < ((command >> 12) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400193 error = i8042_wait_write();
194 if (error)
195 return error;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500196 dbg("%02x -> i8042 (parameter)", param[i]);
197 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500200 for (i = 0; i < ((command >> 8) & 0xf); i++) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400201 error = i8042_wait_read();
202 if (error) {
203 dbg(" -- i8042 (timeout)");
204 return error;
205 }
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500206
207 if (command == I8042_CMD_AUX_LOOP &&
208 !(i8042_read_status() & I8042_STR_AUXDATA)) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400209 dbg(" -- i8042 (auxerr)");
210 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500213 param[i] = i8042_read_data();
214 dbg("%02x <- i8042 (return)", param[i]);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400217 return 0;
218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Márton Németh553a05b2007-10-22 00:56:52 -0400220int i8042_command(unsigned char *param, int command)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400221{
222 unsigned long flags;
223 int retval;
224
225 spin_lock_irqsave(&i8042_lock, flags);
226 retval = __i8042_command(param, command);
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500227 spin_unlock_irqrestore(&i8042_lock, flags);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return retval;
230}
Márton Németh553a05b2007-10-22 00:56:52 -0400231EXPORT_SYMBOL(i8042_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
234 * i8042_kbd_write() sends a byte out through the keyboard interface.
235 */
236
237static int i8042_kbd_write(struct serio *port, unsigned char c)
238{
239 unsigned long flags;
240 int retval = 0;
241
242 spin_lock_irqsave(&i8042_lock, flags);
243
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400244 if (!(retval = i8042_wait_write())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dbg("%02x -> i8042 (kbd-data)", c);
246 i8042_write_data(c);
247 }
248
249 spin_unlock_irqrestore(&i8042_lock, flags);
250
251 return retval;
252}
253
254/*
255 * i8042_aux_write() sends a byte out through the aux interface.
256 */
257
258static int i8042_aux_write(struct serio *serio, unsigned char c)
259{
260 struct i8042_port *port = serio->port_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dmitry Torokhovf4e3c712006-11-02 23:27:49 -0500262 return i8042_command(&c, port->mux == -1 ?
263 I8042_CMD_AUX_SEND :
264 I8042_CMD_MUX_SEND + port->mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * i8042_start() is called by serio core when port is about to finish
269 * registering. It will mark port as existing so i8042_interrupt can
270 * start sending data through it.
271 */
272static int i8042_start(struct serio *serio)
273{
274 struct i8042_port *port = serio->port_data;
275
276 port->exists = 1;
277 mb();
278 return 0;
279}
280
281/*
282 * i8042_stop() marks serio port as non-existing so i8042_interrupt
283 * will not try to send data to the port that is about to go away.
284 * The function is called by serio core as part of unregister procedure.
285 */
286static void i8042_stop(struct serio *serio)
287{
288 struct i8042_port *port = serio->port_data;
289
290 port->exists = 0;
Dmitry Torokhova8399c52007-11-04 00:44:31 -0400291
292 /*
293 * We synchronize with both AUX and KBD IRQs because there is
294 * a (very unlikely) chance that AUX IRQ is raised for KBD port
295 * and vice versa.
296 */
297 synchronize_irq(I8042_AUX_IRQ);
298 synchronize_irq(I8042_KBD_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 port->serio = NULL;
300}
301
302/*
303 * i8042_interrupt() is the most important function in this driver -
304 * it handles the interrupts from the i8042, and sends incoming bytes
305 * to the upper layers.
306 */
307
David Howells7d12e782006-10-05 14:55:46 +0100308static irqreturn_t i8042_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct i8042_port *port;
311 unsigned long flags;
312 unsigned char str, data;
313 unsigned int dfl;
314 unsigned int port_no;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400315 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_lock_irqsave(&i8042_lock, flags);
318 str = i8042_read_status();
319 if (unlikely(~str & I8042_STR_OBF)) {
320 spin_unlock_irqrestore(&i8042_lock, flags);
321 if (irq) dbg("Interrupt %d, without any data", irq);
322 ret = 0;
323 goto out;
324 }
325 data = i8042_read_data();
326 spin_unlock_irqrestore(&i8042_lock, flags);
327
328 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
329 static unsigned long last_transmit;
330 static unsigned char last_str;
331
332 dfl = 0;
333 if (str & I8042_STR_MUXERR) {
334 dbg("MUX error, status is %02x, data is %02x", str, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
336 * When MUXERR condition is signalled the data register can only contain
337 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500338 * it is not always the case. Some KBCs also report 0xfc when there is
339 * nothing connected to the port while others sometimes get confused which
340 * port the data came from and signal error leaving the data intact. They
341 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
342 * to legacy mode yet, when we see one we'll add proper handling).
343 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
344 * rest assume that the data came from the same serio last byte
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * was transmitted (if transmission happened not too long ago).
346 */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500347
348 switch (data) {
349 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (time_before(jiffies, last_transmit + HZ/10)) {
351 str = last_str;
352 break;
353 }
354 /* fall through - report timeout */
Dmitry Torokhova216a4b2006-11-17 01:07:06 -0500355 case 0xfc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 case 0xfd:
357 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
358 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
359 }
360 }
361
362 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
363 last_str = str;
364 last_transmit = jiffies;
365 } else {
366
367 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
368 ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
369
370 port_no = (str & I8042_STR_AUXDATA) ?
371 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
372 }
373
374 port = &i8042_ports[port_no];
375
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400376 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)",
377 data, port_no, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dfl & SERIO_PARITY ? ", bad parity" : "",
379 dfl & SERIO_TIMEOUT ? ", timeout" : "");
380
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400381 if (unlikely(i8042_suppress_kbd_ack))
382 if (port_no == I8042_KBD_PORT_NO &&
383 (data == 0xfa || data == 0xfe)) {
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -0500384 i8042_suppress_kbd_ack--;
Dmitry Torokhov817e6ba2006-10-11 01:44:28 -0400385 goto out;
386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (likely(port->exists))
David Howells7d12e782006-10-05 14:55:46 +0100389 serio_interrupt(port->serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500391 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return IRQ_RETVAL(ret);
393}
394
395/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400396 * i8042_enable_kbd_port enables keybaord port on chip
397 */
398
399static int i8042_enable_kbd_port(void)
400{
401 i8042_ctr &= ~I8042_CTR_KBDDIS;
402 i8042_ctr |= I8042_CTR_KBDINT;
403
404 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400405 i8042_ctr &= ~I8042_CTR_KBDINT;
406 i8042_ctr |= I8042_CTR_KBDDIS;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400407 printk(KERN_ERR "i8042.c: Failed to enable KBD port.\n");
408 return -EIO;
409 }
410
411 return 0;
412}
413
414/*
415 * i8042_enable_aux_port enables AUX (mouse) port on chip
416 */
417
418static int i8042_enable_aux_port(void)
419{
420 i8042_ctr &= ~I8042_CTR_AUXDIS;
421 i8042_ctr |= I8042_CTR_AUXINT;
422
423 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Markus Armbruster018db6b2007-07-18 01:20:41 -0400424 i8042_ctr &= ~I8042_CTR_AUXINT;
425 i8042_ctr |= I8042_CTR_AUXDIS;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400426 printk(KERN_ERR "i8042.c: Failed to enable AUX port.\n");
427 return -EIO;
428 }
429
430 return 0;
431}
432
433/*
434 * i8042_enable_mux_ports enables 4 individual AUX ports after
435 * the controller has been switched into Multiplexed mode
436 */
437
438static int i8042_enable_mux_ports(void)
439{
440 unsigned char param;
441 int i;
442
443 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
444 i8042_command(&param, I8042_CMD_MUX_PFX + i);
445 i8042_command(&param, I8042_CMD_AUX_ENABLE);
446 }
447
448 return i8042_enable_aux_port();
449}
450
451/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * i8042_set_mux_mode checks whether the controller has an active
453 * multiplexor and puts the chip into Multiplexed (1) or Legacy (0) mode.
454 */
455
456static int i8042_set_mux_mode(unsigned int mode, unsigned char *mux_version)
457{
458
459 unsigned char param;
460/*
461 * Get rid of bytes in the queue.
462 */
463
464 i8042_flush();
465
466/*
467 * Internal loopback test - send three bytes, they should come back from the
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400468 * mouse interface, the last should be version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470
471 param = 0xf0;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500472 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return -1;
474 param = mode ? 0x56 : 0xf6;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500475 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != (mode ? 0x56 : 0xf6))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -1;
477 param = mode ? 0xa4 : 0xa5;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500478 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == (mode ? 0xa4 : 0xa5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -1;
480
481 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500482 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return 0;
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * i8042_check_mux() checks whether the controller supports the PS/2 Active
489 * Multiplexing specification by Synaptics, Phoenix, Insyde and
490 * LCS/Telegraphics.
491 */
492
Dmitry Torokhov87fd6312005-12-28 01:25:11 -0500493static int __devinit i8042_check_mux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 unsigned char mux_version;
496
497 if (i8042_set_mux_mode(1, &mux_version))
498 return -1;
499
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400500/*
501 * Workaround for interference with USB Legacy emulation
502 * that causes a v10.12 MUX to be found.
503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (mux_version == 0xAC)
505 return -1;
506
507 printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
508 (mux_version >> 4) & 0xf, mux_version & 0xf);
509
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400510/*
511 * Disable all muxed ports by disabling AUX.
512 */
513 i8042_ctr |= I8042_CTR_AUXDIS;
514 i8042_ctr &= ~I8042_CTR_AUXINT;
515
516 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
517 printk(KERN_ERR "i8042.c: Failed to disable AUX port, can't use MUX.\n");
518 return -EIO;
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 i8042_mux_present = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return 0;
524}
525
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400526/*
527 * The following is used to test AUX IRQ delivery.
528 */
529static struct completion i8042_aux_irq_delivered __devinitdata;
530static int i8042_irq_being_tested __devinitdata;
531
David Howells7d12e782006-10-05 14:55:46 +0100532static irqreturn_t __devinit i8042_aux_test_irq(int irq, void *dev_id)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400533{
534 unsigned long flags;
535 unsigned char str, data;
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400536 int ret = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400537
538 spin_lock_irqsave(&i8042_lock, flags);
539 str = i8042_read_status();
540 if (str & I8042_STR_OBF) {
541 data = i8042_read_data();
542 if (i8042_irq_being_tested &&
543 data == 0xa5 && (str & I8042_STR_AUXDATA))
544 complete(&i8042_aux_irq_delivered);
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400545 ret = 1;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400546 }
547 spin_unlock_irqrestore(&i8042_lock, flags);
548
Fernando Luis Vázquez Caoe3758b22007-08-30 00:04:15 -0400549 return IRQ_RETVAL(ret);
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400550}
551
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400552/*
553 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
554 * verifies success by readinng CTR. Used when testing for presence of AUX
555 * port.
556 */
557static int __devinit i8042_toggle_aux(int on)
558{
559 unsigned char param;
560 int i;
561
562 if (i8042_command(&param,
563 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
564 return -1;
565
566 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
567 for (i = 0; i < 100; i++) {
568 udelay(50);
569
570 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
571 return -1;
572
573 if (!(param & I8042_CTR_AUXDIS) == on)
574 return 0;
575 }
576
577 return -1;
578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/*
581 * i8042_check_aux() applies as much paranoia as it can at detecting
582 * the presence of an AUX interface.
583 */
584
Dmitry Torokhov87fd6312005-12-28 01:25:11 -0500585static int __devinit i8042_check_aux(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400587 int retval = -1;
588 int irq_registered = 0;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500589 int aux_loop_broken = 0;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400590 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 unsigned char param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593/*
594 * Get rid of bytes in the queue.
595 */
596
597 i8042_flush();
598
599/*
600 * Internal loopback test - filters out AT-type i8042's. Unfortunately
601 * SiS screwed up and their 5597 doesn't support the LOOP command even
602 * though it has an AUX port.
603 */
604
605 param = 0x5a;
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500606 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
607 if (retval || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609/*
610 * External connection test - filters out AT-soldered PS/2 i8042's
611 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
612 * 0xfa - no error on some notebooks which ignore the spec
613 * Because it's common for chipsets to return error on perfectly functioning
614 * AUX ports, we test for this only when the LOOP command failed.
615 */
616
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400617 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
618 (param && param != 0xfa && param != 0xff))
619 return -1;
Dmitry Torokhov1e4865f2007-02-10 01:29:53 -0500620
Dmitry Torokhov3ca5de62007-03-07 23:20:55 -0500621/*
622 * If AUX_LOOP completed without error but returned unexpected data
623 * mark it as broken
624 */
625 if (!retval)
626 aux_loop_broken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
629/*
630 * Bit assignment test - filters out PS/2 i8042's in AT mode
631 */
632
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400633 if (i8042_toggle_aux(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
635 printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
636 }
637
Roland Scheideggerd2ada552007-05-08 01:31:40 -0400638 if (i8042_toggle_aux(1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -1;
640
641/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400642 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
643 * used it for a PCI card or somethig else.
644 */
645
Dmitry Torokhov1c7827a2009-09-03 21:45:34 -0700646 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400647/*
648 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
649 * is working and hope we are right.
650 */
651 retval = 0;
652 goto out;
653 }
654
655 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
656 "i8042", i8042_platform_device))
657 goto out;
658
659 irq_registered = 1;
660
661 if (i8042_enable_aux_port())
662 goto out;
663
664 spin_lock_irqsave(&i8042_lock, flags);
665
666 init_completion(&i8042_aux_irq_delivered);
667 i8042_irq_being_tested = 1;
668
669 param = 0xa5;
670 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
671
672 spin_unlock_irqrestore(&i8042_lock, flags);
673
674 if (retval)
675 goto out;
676
677 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
678 msecs_to_jiffies(250)) == 0) {
679/*
680 * AUX IRQ was never delivered so we need to flush the controller to
681 * get rid of the byte we put there; otherwise keyboard may not work.
682 */
683 i8042_flush();
684 retval = -1;
685 }
686
687 out:
688
689/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * Disable the interface.
691 */
692
693 i8042_ctr |= I8042_CTR_AUXDIS;
694 i8042_ctr &= ~I8042_CTR_AUXINT;
695
696 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400697 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400699 if (irq_registered)
700 free_irq(I8042_AUX_IRQ, i8042_platform_device);
701
702 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400705static int i8042_controller_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400707 if (i8042_flush() == I8042_BUFFER_SIZE) {
708 printk(KERN_ERR "i8042.c: No controller found.\n");
709 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713}
714
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400715static int i8042_controller_selftest(void)
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500716{
717 unsigned char param;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700718 int i = 0;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500719
720 if (!i8042_reset)
721 return 0;
722
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700723 /*
724 * We try this 5 times; on some really fragile systems this does not
725 * take the first time...
726 */
727 do {
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500728
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700729 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
730 printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
731 return -ENODEV;
732 }
733
734 if (param == I8042_RET_CTL_TEST)
735 return 0;
736
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500737 printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700738 param, I8042_RET_CTL_TEST);
739 msleep(50);
740 } while (i++ < 5);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500741
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700742#ifdef CONFIG_X86
743 /*
744 * On x86, we don't fail entire i8042 initialization if controller
745 * reset fails in hopes that keyboard port will still be functional
746 * and user will still get a working keyboard. This is especially
747 * important on netbooks. On other arches we trust hardware more.
748 */
749 printk(KERN_INFO
750 "i8042: giving up on controller selftest, continuing anyway...\n");
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500751 return 0;
Arjan van de Ven5ea2fc62009-04-09 11:36:50 -0700752#else
753 return -EIO;
754#endif
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500755}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757/*
758 * i8042_controller init initializes the i8042 controller, and,
759 * most importantly, sets it into non-xlated mode if that's
760 * desired.
761 */
762
763static int i8042_controller_init(void)
764{
765 unsigned long flags;
766
767/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * Save the CTR for restoral on unload / reboot.
769 */
770
771 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
772 printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400773 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
776 i8042_initial_ctr = i8042_ctr;
777
778/*
779 * Disable the keyboard interface and interrupt.
780 */
781
782 i8042_ctr |= I8042_CTR_KBDDIS;
783 i8042_ctr &= ~I8042_CTR_KBDINT;
784
785/*
786 * Handle keylock.
787 */
788
789 spin_lock_irqsave(&i8042_lock, flags);
790 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
791 if (i8042_unlock)
792 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500793 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
795 }
796 spin_unlock_irqrestore(&i8042_lock, flags);
797
798/*
799 * If the chip is configured into nontranslated mode by the BIOS, don't
800 * bother enabling translating and be happy.
801 */
802
803 if (~i8042_ctr & I8042_CTR_XLATE)
804 i8042_direct = 1;
805
806/*
807 * Set nontranslated mode for the kbd interface if requested by an option.
808 * After this the kbd interface becomes a simple serial in/out, like the aux
809 * interface is. We don't do this by default, since it can confuse notebook
810 * BIOSes.
811 */
812
813 if (i8042_direct)
814 i8042_ctr &= ~I8042_CTR_XLATE;
815
816/*
817 * Write CTR back.
818 */
819
820 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
821 printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400822 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 return 0;
826}
827
828
829/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400830 * Reset the controller and reset CRT to the original value set by BIOS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
832
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400833static void i8042_controller_reset(void)
834{
835 i8042_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837/*
Dmitry Torokhov8d04ddb2007-04-12 01:32:09 -0400838 * Disable both KBD and AUX interfaces so they don't get in the way
839 */
840
841 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
842 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
843
844/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * Disable MUX mode if present.
846 */
847
848 if (i8042_mux_present)
849 i8042_set_mux_mode(0, NULL);
850
851/*
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400852 * Reset the controller if requested.
853 */
854
855 i8042_controller_selftest();
856
857/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * Restore the original control register setting.
859 */
860
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400861 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
863}
864
865
866/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 * i8042_panic_blink() will flash the keyboard LEDs and is called when
868 * kernel panics. Flashing LEDs is useful for users running X who may
869 * not see the console and will help distingushing panics from "real"
870 * lockups.
871 *
872 * Note that DELAY has a limit of 10ms so we will not get stuck here
873 * waiting for KBC to free up even if KBD interrupt is off
874 */
875
876#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
877
878static long i8042_panic_blink(long count)
879{
880 long delay = 0;
881 static long last_blink;
882 static char led;
883
884 /*
885 * We expect frequency to be about 1/2s. KDB uses about 1s.
886 * Make sure they are different.
887 */
888 if (!i8042_blink_frequency)
889 return 0;
890 if (count - last_blink < i8042_blink_frequency)
891 return 0;
892
893 led ^= 0x01 | 0x04;
894 while (i8042_read_status() & I8042_STR_IBF)
895 DELAY;
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -0500896 dbg("%02x -> i8042 (panic blink)", 0xed);
897 i8042_suppress_kbd_ack = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 i8042_write_data(0xed); /* set leds */
899 DELAY;
900 while (i8042_read_status() & I8042_STR_IBF)
901 DELAY;
902 DELAY;
Dmitry Torokhov19f3c3e2007-01-18 00:42:31 -0500903 dbg("%02x -> i8042 (panic blink)", led);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 i8042_write_data(led);
905 DELAY;
906 last_blink = count;
907 return delay;
908}
909
910#undef DELAY
911
Bruno Prémontd35895d2008-05-27 01:36:04 -0400912#ifdef CONFIG_X86
913static void i8042_dritek_enable(void)
914{
915 char param = 0x90;
916 int error;
917
918 error = i8042_command(&param, 0x1059);
919 if (error)
920 printk(KERN_WARNING
921 "Failed to enable DRITEK extension: %d\n",
922 error);
923}
924#endif
925
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500926#ifdef CONFIG_PM
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -0700929 * Here we try to restore the original BIOS settings to avoid
930 * upsetting it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
932
Dmitry Torokhovebd77682009-07-22 21:51:32 -0700933static int i8042_pm_reset(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Dmitry Torokhovebd77682009-07-22 21:51:32 -0700935 i8042_controller_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 return 0;
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*
Dmitry Torokhovebd77682009-07-22 21:51:32 -0700941 * Here we try to reset everything back to a state we had
942 * before suspending.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
944
Dmitry Torokhovebd77682009-07-22 21:51:32 -0700945static int i8042_pm_restore(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400947 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400949 error = i8042_controller_check();
950 if (error)
951 return error;
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500952
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400953 error = i8042_controller_selftest();
954 if (error)
955 return error;
956
957/*
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500958 * Restore original CTR value and disable all ports
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400959 */
960
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500961 i8042_ctr = i8042_initial_ctr;
962 if (i8042_direct)
963 i8042_ctr &= ~I8042_CTR_XLATE;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400964 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
965 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
Vojtech Pavlik2673c8362005-05-28 02:11:27 -0500966 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
Jiri Kosina2f6a77d2008-06-17 11:47:27 -0400967 printk(KERN_WARNING "i8042: Can't write CTR to resume, retrying...\n");
968 msleep(50);
969 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
970 printk(KERN_ERR "i8042: CTR write retry failed\n");
971 return -EIO;
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Bruno Prémontd35895d2008-05-27 01:36:04 -0400975
976#ifdef CONFIG_X86
977 if (i8042_dritek)
978 i8042_dritek_enable();
979#endif
980
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400981 if (i8042_mux_present) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (i8042_set_mux_mode(1, NULL) || i8042_enable_mux_ports())
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400983 printk(KERN_WARNING
984 "i8042: failed to resume active multiplexor, "
985 "mouse won't work.\n");
986 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
987 i8042_enable_aux_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dmitry Torokhovde9ce702006-09-10 21:57:21 -0400989 if (i8042_ports[I8042_KBD_PORT_NO].serio)
990 i8042_enable_kbd_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
David Howells7d12e782006-10-05 14:55:46 +0100992 i8042_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
Dmitry Torokhovebd77682009-07-22 21:51:32 -0700996
997static const struct dev_pm_ops i8042_pm_ops = {
998 .suspend = i8042_pm_reset,
999 .resume = i8042_pm_restore,
1000 .poweroff = i8042_pm_reset,
1001 .restore = i8042_pm_restore,
1002};
1003
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001004#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006/*
1007 * We need to reset the 8042 back to original mode on system shutdown,
1008 * because otherwise BIOSes will be confused.
1009 */
1010
Russell King3ae5eae2005-11-09 22:32:44 +00001011static void i8042_shutdown(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001013 i8042_controller_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001016static int __devinit i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 struct serio *serio;
1019 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1020
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001021 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001022 if (!serio)
1023 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001025 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1026 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001027 serio->start = i8042_start;
1028 serio->stop = i8042_stop;
1029 serio->port_data = port;
1030 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001031 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001032 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1033
1034 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001035 port->irq = I8042_KBD_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001036
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001040static int __devinit i8042_create_aux_port(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct serio *serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001043 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1044 struct i8042_port *port = &i8042_ports[port_no];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Dmitry Torokhovd39969d2005-09-10 12:04:42 -05001046 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001047 if (!serio)
1048 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001050 serio->id.type = SERIO_8042;
1051 serio->write = i8042_aux_write;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001052 serio->start = i8042_start;
1053 serio->stop = i8042_stop;
1054 serio->port_data = port;
1055 serio->dev.parent = &i8042_platform_device->dev;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001056 if (idx < 0) {
1057 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1058 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1059 } else {
1060 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1061 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1062 }
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001063
1064 port->serio = serio;
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001065 port->mux = idx;
1066 port->irq = I8042_AUX_IRQ;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001067
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001071static void __devinit i8042_free_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001073 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1074 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001077static void __devinit i8042_free_aux_ports(void)
1078{
1079 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001081 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1082 kfree(i8042_ports[i].serio);
1083 i8042_ports[i].serio = NULL;
1084 }
1085}
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001086
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001087static void __devinit i8042_register_ports(void)
1088{
1089 int i;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001090
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001091 for (i = 0; i < I8042_NUM_PORTS; i++) {
1092 if (i8042_ports[i].serio) {
1093 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1094 i8042_ports[i].serio->name,
1095 (unsigned long) I8042_DATA_REG,
1096 (unsigned long) I8042_COMMAND_REG,
1097 i8042_ports[i].irq);
1098 serio_register_port(i8042_ports[i].serio);
1099 }
1100 }
1101}
1102
Ralf Baechle7a1904c2007-09-04 23:16:31 -04001103static void __devexit i8042_unregister_ports(void)
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001104{
1105 int i;
1106
1107 for (i = 0; i < I8042_NUM_PORTS; i++) {
1108 if (i8042_ports[i].serio) {
1109 serio_unregister_port(i8042_ports[i].serio);
1110 i8042_ports[i].serio = NULL;
1111 }
1112 }
1113}
1114
1115static void i8042_free_irqs(void)
1116{
1117 if (i8042_aux_irq_registered)
1118 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1119 if (i8042_kbd_irq_registered)
1120 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1121
1122 i8042_aux_irq_registered = i8042_kbd_irq_registered = 0;
1123}
1124
1125static int __devinit i8042_setup_aux(void)
1126{
1127 int (*aux_enable)(void);
1128 int error;
1129 int i;
1130
1131 if (i8042_check_aux())
1132 return -ENODEV;
1133
1134 if (i8042_nomux || i8042_check_mux()) {
1135 error = i8042_create_aux_port(-1);
1136 if (error)
1137 goto err_free_ports;
1138 aux_enable = i8042_enable_aux_port;
1139 } else {
1140 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1141 error = i8042_create_aux_port(i);
1142 if (error)
1143 goto err_free_ports;
1144 }
1145 aux_enable = i8042_enable_mux_ports;
1146 }
1147
1148 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1149 "i8042", i8042_platform_device);
1150 if (error)
1151 goto err_free_ports;
1152
1153 if (aux_enable())
1154 goto err_free_irq;
1155
1156 i8042_aux_irq_registered = 1;
1157 return 0;
1158
1159 err_free_irq:
1160 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1161 err_free_ports:
1162 i8042_free_aux_ports();
1163 return error;
1164}
1165
1166static int __devinit i8042_setup_kbd(void)
1167{
1168 int error;
1169
1170 error = i8042_create_kbd_port();
1171 if (error)
1172 return error;
1173
1174 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1175 "i8042", i8042_platform_device);
1176 if (error)
1177 goto err_free_port;
1178
1179 error = i8042_enable_kbd_port();
1180 if (error)
1181 goto err_free_irq;
1182
1183 i8042_kbd_irq_registered = 1;
1184 return 0;
1185
1186 err_free_irq:
1187 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1188 err_free_port:
1189 i8042_free_kbd_port();
1190 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001193static int __devinit i8042_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001195 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001197 error = i8042_controller_selftest();
1198 if (error)
1199 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001201 error = i8042_controller_init();
1202 if (error)
1203 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Bruno Prémontd35895d2008-05-27 01:36:04 -04001205#ifdef CONFIG_X86
1206 if (i8042_dritek)
1207 i8042_dritek_enable();
1208#endif
1209
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001210 if (!i8042_noaux) {
1211 error = i8042_setup_aux();
1212 if (error && error != -ENODEV && error != -EBUSY)
1213 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001216 if (!i8042_nokbd) {
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001217 error = i8042_setup_kbd();
1218 if (error)
1219 goto out_fail;
Dmitry Torokhov945ef0d2005-09-04 01:42:00 -05001220 }
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001221/*
1222 * Ok, everything is ready, let's register all serio ports
1223 */
1224 i8042_register_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001227
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001228 out_fail:
1229 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1230 i8042_free_irqs();
1231 i8042_controller_reset();
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001232
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001233 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001236static int __devexit i8042_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001238 i8042_unregister_ports();
1239 i8042_free_irqs();
1240 i8042_controller_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001242 return 0;
1243}
1244
1245static struct platform_driver i8042_driver = {
1246 .driver = {
1247 .name = "i8042",
1248 .owner = THIS_MODULE,
Dmitry Torokhovebd77682009-07-22 21:51:32 -07001249#ifdef CONFIG_PM
1250 .pm = &i8042_pm_ops,
1251#endif
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001252 },
1253 .probe = i8042_probe,
1254 .remove = __devexit_p(i8042_remove),
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001255 .shutdown = i8042_shutdown,
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001256};
1257
1258static int __init i8042_init(void)
1259{
1260 int err;
1261
1262 dbg_init();
1263
1264 err = i8042_platform_init();
1265 if (err)
1266 return err;
1267
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001268 err = i8042_controller_check();
1269 if (err)
1270 goto err_platform_exit;
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001271
1272 err = platform_driver_register(&i8042_driver);
1273 if (err)
1274 goto err_platform_exit;
1275
1276 i8042_platform_device = platform_device_alloc("i8042", -1);
1277 if (!i8042_platform_device) {
1278 err = -ENOMEM;
1279 goto err_unregister_driver;
1280 }
1281
1282 err = platform_device_add(i8042_platform_device);
1283 if (err)
1284 goto err_free_device;
1285
Dmitry Torokhovde9ce702006-09-10 21:57:21 -04001286 panic_blink = i8042_panic_blink;
1287
Dmitry Torokhov87fd6312005-12-28 01:25:11 -05001288 return 0;
1289
1290 err_free_device:
1291 platform_device_put(i8042_platform_device);
1292 err_unregister_driver:
1293 platform_driver_unregister(&i8042_driver);
1294 err_platform_exit:
1295 i8042_platform_exit();
1296
1297 return err;
1298}
1299
1300static void __exit i8042_exit(void)
1301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 platform_device_unregister(i8042_platform_device);
Russell King3ae5eae2005-11-09 22:32:44 +00001303 platform_driver_unregister(&i8042_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 i8042_platform_exit();
1305
1306 panic_blink = NULL;
1307}
1308
1309module_init(i8042_init);
1310module_exit(i8042_exit);