Input: synaptics - fix crash in synaptics_module_init()
[deliverable/linux.git] / drivers / input / serio / i8042.c
CommitLineData
1da177e4
LT
1/*
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
4eb3c30b
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
7e044e05 15#include <linux/types.h>
1da177e4
LT
16#include <linux/delay.h>
17#include <linux/module.h>
1da177e4
LT
18#include <linux/interrupt.h>
19#include <linux/ioport.h>
1da177e4
LT
20#include <linux/init.h>
21#include <linux/serio.h>
22#include <linux/err.h>
23#include <linux/rcupdate.h>
d052d1be 24#include <linux/platform_device.h>
553a05b8 25#include <linux/i8042.h>
5a0e3ad6 26#include <linux/slab.h>
1da177e4
LT
27
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
386b3849 34static bool i8042_nokbd;
945ef0d4
DT
35module_param_named(nokbd, i8042_nokbd, bool, 0);
36MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
37
386b3849 38static bool i8042_noaux;
1da177e4
LT
39module_param_named(noaux, i8042_noaux, bool, 0);
40MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
41
386b3849 42static bool i8042_nomux;
1da177e4 43module_param_named(nomux, i8042_nomux, bool, 0);
2c860a11 44MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
1da177e4 45
386b3849 46static bool i8042_unlock;
1da177e4
LT
47module_param_named(unlock, i8042_unlock, bool, 0);
48MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
49
386b3849 50static bool i8042_reset;
1da177e4
LT
51module_param_named(reset, i8042_reset, bool, 0);
52MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
53
386b3849 54static bool i8042_direct;
1da177e4
LT
55module_param_named(direct, i8042_direct, bool, 0);
56MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
57
386b3849 58static bool i8042_dumbkbd;
1da177e4
LT
59module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
60MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
61
386b3849 62static bool i8042_noloop;
1da177e4
LT
63module_param_named(noloop, i8042_noloop, bool, 0);
64MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
65
f8313ef1
JK
66static bool i8042_notimeout;
67module_param_named(notimeout, i8042_notimeout, bool, 0);
68MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
69
8987fec0 70#ifdef CONFIG_X86
386b3849 71static bool i8042_dritek;
8987fec0
CC
72module_param_named(dritek, i8042_dritek, bool, 0);
73MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
74#endif
75
1da177e4 76#ifdef CONFIG_PNP
386b3849 77static bool i8042_nopnp;
1da177e4
LT
78module_param_named(nopnp, i8042_nopnp, bool, 0);
79MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
80#endif
81
82#define DEBUG
83#ifdef DEBUG
386b3849 84static bool i8042_debug;
1da177e4
LT
85module_param_named(debug, i8042_debug, bool, 0600);
86MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
87#endif
88
1c7827ae
DT
89static bool i8042_bypass_aux_irq_test;
90
1da177e4
LT
91#include "i8042.h"
92
181d683d
DT
93/*
94 * i8042_lock protects serialization between i8042_command and
95 * the interrupt handler.
96 */
1da177e4
LT
97static DEFINE_SPINLOCK(i8042_lock);
98
181d683d
DT
99/*
100 * Writers to AUX and KBD ports as well as users issuing i8042_command
101 * directly should acquire i8042_mutex (by means of calling
102 * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
103 * they do not disturb each other (unfortunately in many i8042
104 * implementations write to one of the ports will immediately abort
105 * command that is being processed by another port).
106 */
107static DEFINE_MUTEX(i8042_mutex);
108
1da177e4
LT
109struct i8042_port {
110 struct serio *serio;
111 int irq;
386b3849 112 bool exists;
1da177e4 113 signed char mux;
1da177e4
LT
114};
115
116#define I8042_KBD_PORT_NO 0
117#define I8042_AUX_PORT_NO 1
118#define I8042_MUX_PORT_NO 2
119#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
de9ce703
DT
120
121static struct i8042_port i8042_ports[I8042_NUM_PORTS];
1da177e4
LT
122
123static unsigned char i8042_initial_ctr;
124static unsigned char i8042_ctr;
386b3849
DT
125static bool i8042_mux_present;
126static bool i8042_kbd_irq_registered;
127static bool i8042_aux_irq_registered;
817e6ba3 128static unsigned char i8042_suppress_kbd_ack;
1da177e4
LT
129static struct platform_device *i8042_platform_device;
130
7d12e780 131static irqreturn_t i8042_interrupt(int irq, void *dev_id);
967c9ef9
MG
132static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
133 struct serio *serio);
1da177e4 134
181d683d
DT
135void i8042_lock_chip(void)
136{
137 mutex_lock(&i8042_mutex);
138}
139EXPORT_SYMBOL(i8042_lock_chip);
140
141void i8042_unlock_chip(void)
142{
143 mutex_unlock(&i8042_mutex);
144}
145EXPORT_SYMBOL(i8042_unlock_chip);
146
967c9ef9
MG
147int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
148 struct serio *serio))
149{
150 unsigned long flags;
151 int ret = 0;
152
153 spin_lock_irqsave(&i8042_lock, flags);
154
155 if (i8042_platform_filter) {
156 ret = -EBUSY;
157 goto out;
158 }
159
160 i8042_platform_filter = filter;
161
162out:
163 spin_unlock_irqrestore(&i8042_lock, flags);
164 return ret;
165}
166EXPORT_SYMBOL(i8042_install_filter);
167
168int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
169 struct serio *port))
170{
171 unsigned long flags;
172 int ret = 0;
173
174 spin_lock_irqsave(&i8042_lock, flags);
175
176 if (i8042_platform_filter != filter) {
177 ret = -EINVAL;
178 goto out;
179 }
180
181 i8042_platform_filter = NULL;
182
183out:
184 spin_unlock_irqrestore(&i8042_lock, flags);
185 return ret;
186}
187EXPORT_SYMBOL(i8042_remove_filter);
188
1da177e4
LT
189/*
190 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
191 * be ready for reading values from it / writing values to it.
192 * Called always with i8042_lock held.
193 */
194
195static int i8042_wait_read(void)
196{
197 int i = 0;
de9ce703 198
1da177e4
LT
199 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
200 udelay(50);
201 i++;
202 }
203 return -(i == I8042_CTL_TIMEOUT);
204}
205
206static int i8042_wait_write(void)
207{
208 int i = 0;
de9ce703 209
1da177e4
LT
210 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
211 udelay(50);
212 i++;
213 }
214 return -(i == I8042_CTL_TIMEOUT);
215}
216
217/*
218 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
219 * of the i8042 down the toilet.
220 */
221
222static int i8042_flush(void)
223{
224 unsigned long flags;
225 unsigned char data, str;
226 int i = 0;
227
228 spin_lock_irqsave(&i8042_lock, flags);
229
230 while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
231 udelay(50);
232 data = i8042_read_data();
233 i++;
4eb3c30b
JP
234 dbg("%02x <- i8042 (flush, %s)\n",
235 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
1da177e4
LT
236 }
237
238 spin_unlock_irqrestore(&i8042_lock, flags);
239
240 return i;
241}
242
243/*
244 * i8042_command() executes a command on the i8042. It also sends the input
245 * parameter(s) of the commands to it, and receives the output value(s). The
246 * parameters are to be stored in the param array, and the output is placed
247 * into the same array. The number of the parameters and output values is
248 * encoded in bits 8-11 of the command number.
249 */
250
de9ce703 251static int __i8042_command(unsigned char *param, int command)
1da177e4 252{
de9ce703 253 int i, error;
1da177e4
LT
254
255 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
256 return -1;
257
de9ce703
DT
258 error = i8042_wait_write();
259 if (error)
260 return error;
463a4f76 261
4eb3c30b 262 dbg("%02x -> i8042 (command)\n", command & 0xff);
463a4f76
DT
263 i8042_write_command(command & 0xff);
264
265 for (i = 0; i < ((command >> 12) & 0xf); i++) {
de9ce703
DT
266 error = i8042_wait_write();
267 if (error)
268 return error;
4eb3c30b 269 dbg("%02x -> i8042 (parameter)\n", param[i]);
463a4f76 270 i8042_write_data(param[i]);
1da177e4
LT
271 }
272
463a4f76 273 for (i = 0; i < ((command >> 8) & 0xf); i++) {
de9ce703
DT
274 error = i8042_wait_read();
275 if (error) {
4eb3c30b 276 dbg(" -- i8042 (timeout)\n");
de9ce703
DT
277 return error;
278 }
1da177e4 279
463a4f76
DT
280 if (command == I8042_CMD_AUX_LOOP &&
281 !(i8042_read_status() & I8042_STR_AUXDATA)) {
4eb3c30b 282 dbg(" -- i8042 (auxerr)\n");
de9ce703 283 return -1;
1da177e4
LT
284 }
285
463a4f76 286 param[i] = i8042_read_data();
4eb3c30b 287 dbg("%02x <- i8042 (return)\n", param[i]);
463a4f76 288 }
1da177e4 289
de9ce703
DT
290 return 0;
291}
1da177e4 292
553a05b8 293int i8042_command(unsigned char *param, int command)
de9ce703
DT
294{
295 unsigned long flags;
296 int retval;
297
298 spin_lock_irqsave(&i8042_lock, flags);
299 retval = __i8042_command(param, command);
463a4f76 300 spin_unlock_irqrestore(&i8042_lock, flags);
de9ce703 301
1da177e4
LT
302 return retval;
303}
553a05b8 304EXPORT_SYMBOL(i8042_command);
1da177e4
LT
305
306/*
307 * i8042_kbd_write() sends a byte out through the keyboard interface.
308 */
309
310static int i8042_kbd_write(struct serio *port, unsigned char c)
311{
312 unsigned long flags;
313 int retval = 0;
314
315 spin_lock_irqsave(&i8042_lock, flags);
316
de9ce703 317 if (!(retval = i8042_wait_write())) {
4eb3c30b 318 dbg("%02x -> i8042 (kbd-data)\n", c);
1da177e4
LT
319 i8042_write_data(c);
320 }
321
322 spin_unlock_irqrestore(&i8042_lock, flags);
323
324 return retval;
325}
326
327/*
328 * i8042_aux_write() sends a byte out through the aux interface.
329 */
330
331static int i8042_aux_write(struct serio *serio, unsigned char c)
332{
333 struct i8042_port *port = serio->port_data;
1da177e4 334
f4e3c711
DT
335 return i8042_command(&c, port->mux == -1 ?
336 I8042_CMD_AUX_SEND :
337 I8042_CMD_MUX_SEND + port->mux);
1da177e4
LT
338}
339
5ddbc77c
DT
340
341/*
342 * i8042_aux_close attempts to clear AUX or KBD port state by disabling
343 * and then re-enabling it.
344 */
345
346static void i8042_port_close(struct serio *serio)
347{
348 int irq_bit;
349 int disable_bit;
350 const char *port_name;
351
352 if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
353 irq_bit = I8042_CTR_AUXINT;
354 disable_bit = I8042_CTR_AUXDIS;
355 port_name = "AUX";
356 } else {
357 irq_bit = I8042_CTR_KBDINT;
358 disable_bit = I8042_CTR_KBDDIS;
359 port_name = "KBD";
360 }
361
362 i8042_ctr &= ~irq_bit;
363 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
4eb3c30b 364 pr_warn("Can't write CTR while closing %s port\n", port_name);
5ddbc77c
DT
365
366 udelay(50);
367
368 i8042_ctr &= ~disable_bit;
369 i8042_ctr |= irq_bit;
370 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
4eb3c30b 371 pr_err("Can't reactivate %s port\n", port_name);
5ddbc77c
DT
372
373 /*
374 * See if there is any data appeared while we were messing with
375 * port state.
376 */
377 i8042_interrupt(0, NULL);
378}
379
1da177e4
LT
380/*
381 * i8042_start() is called by serio core when port is about to finish
382 * registering. It will mark port as existing so i8042_interrupt can
383 * start sending data through it.
384 */
385static int i8042_start(struct serio *serio)
386{
387 struct i8042_port *port = serio->port_data;
388
386b3849 389 port->exists = true;
1da177e4
LT
390 mb();
391 return 0;
392}
393
394/*
395 * i8042_stop() marks serio port as non-existing so i8042_interrupt
396 * will not try to send data to the port that is about to go away.
397 * The function is called by serio core as part of unregister procedure.
398 */
399static void i8042_stop(struct serio *serio)
400{
401 struct i8042_port *port = serio->port_data;
402
386b3849 403 port->exists = false;
a8399c51
DT
404
405 /*
406 * We synchronize with both AUX and KBD IRQs because there is
407 * a (very unlikely) chance that AUX IRQ is raised for KBD port
408 * and vice versa.
409 */
410 synchronize_irq(I8042_AUX_IRQ);
411 synchronize_irq(I8042_KBD_IRQ);
1da177e4
LT
412 port->serio = NULL;
413}
414
4e8d340d
DT
415/*
416 * i8042_filter() filters out unwanted bytes from the input data stream.
417 * It is called from i8042_interrupt and thus is running with interrupts
418 * off and i8042_lock held.
419 */
967c9ef9
MG
420static bool i8042_filter(unsigned char data, unsigned char str,
421 struct serio *serio)
4e8d340d
DT
422{
423 if (unlikely(i8042_suppress_kbd_ack)) {
424 if ((~str & I8042_STR_AUXDATA) &&
425 (data == 0xfa || data == 0xfe)) {
426 i8042_suppress_kbd_ack--;
427 dbg("Extra keyboard ACK - filtered out\n");
428 return true;
429 }
430 }
431
967c9ef9 432 if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
0747e3bc 433 dbg("Filtered out by platform filter\n");
967c9ef9
MG
434 return true;
435 }
436
4e8d340d
DT
437 return false;
438}
439
1da177e4
LT
440/*
441 * i8042_interrupt() is the most important function in this driver -
442 * it handles the interrupts from the i8042, and sends incoming bytes
443 * to the upper layers.
444 */
445
7d12e780 446static irqreturn_t i8042_interrupt(int irq, void *dev_id)
1da177e4
LT
447{
448 struct i8042_port *port;
967c9ef9 449 struct serio *serio;
1da177e4
LT
450 unsigned long flags;
451 unsigned char str, data;
452 unsigned int dfl;
453 unsigned int port_no;
4e8d340d 454 bool filtered;
817e6ba3 455 int ret = 1;
1da177e4 456
1da177e4 457 spin_lock_irqsave(&i8042_lock, flags);
4e8d340d 458
1da177e4
LT
459 str = i8042_read_status();
460 if (unlikely(~str & I8042_STR_OBF)) {
461 spin_unlock_irqrestore(&i8042_lock, flags);
4eb3c30b
JP
462 if (irq)
463 dbg("Interrupt %d, without any data\n", irq);
1da177e4
LT
464 ret = 0;
465 goto out;
466 }
4e8d340d 467
1da177e4 468 data = i8042_read_data();
1da177e4
LT
469
470 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
471 static unsigned long last_transmit;
472 static unsigned char last_str;
473
474 dfl = 0;
475 if (str & I8042_STR_MUXERR) {
4eb3c30b
JP
476 dbg("MUX error, status is %02x, data is %02x\n",
477 str, data);
1da177e4
LT
478/*
479 * When MUXERR condition is signalled the data register can only contain
480 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
a216a4b6
DT
481 * it is not always the case. Some KBCs also report 0xfc when there is
482 * nothing connected to the port while others sometimes get confused which
483 * port the data came from and signal error leaving the data intact. They
484 * _do not_ revert to legacy mode (actually I've never seen KBC reverting
485 * to legacy mode yet, when we see one we'll add proper handling).
486 * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
487 * rest assume that the data came from the same serio last byte
1da177e4
LT
488 * was transmitted (if transmission happened not too long ago).
489 */
a216a4b6
DT
490
491 switch (data) {
492 default:
1da177e4
LT
493 if (time_before(jiffies, last_transmit + HZ/10)) {
494 str = last_str;
495 break;
496 }
497 /* fall through - report timeout */
a216a4b6 498 case 0xfc:
1da177e4
LT
499 case 0xfd:
500 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
501 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
502 }
503 }
504
505 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
506 last_str = str;
507 last_transmit = jiffies;
508 } else {
509
510 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
f8313ef1 511 ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
1da177e4
LT
512
513 port_no = (str & I8042_STR_AUXDATA) ?
514 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
515 }
516
517 port = &i8042_ports[port_no];
967c9ef9 518 serio = port->exists ? port->serio : NULL;
1da177e4 519
4eb3c30b 520 dbg("%02x <- i8042 (interrupt, %d, %d%s%s)\n",
de9ce703 521 data, port_no, irq,
1da177e4
LT
522 dfl & SERIO_PARITY ? ", bad parity" : "",
523 dfl & SERIO_TIMEOUT ? ", timeout" : "");
524
967c9ef9 525 filtered = i8042_filter(data, str, serio);
4e8d340d
DT
526
527 spin_unlock_irqrestore(&i8042_lock, flags);
817e6ba3 528
4e8d340d 529 if (likely(port->exists && !filtered))
967c9ef9 530 serio_interrupt(serio, data, dfl);
1da177e4 531
0854e52d 532 out:
1da177e4
LT
533 return IRQ_RETVAL(ret);
534}
535
de9ce703 536/*
5ddbc77c 537 * i8042_enable_kbd_port enables keyboard port on chip
de9ce703
DT
538 */
539
540static int i8042_enable_kbd_port(void)
541{
542 i8042_ctr &= ~I8042_CTR_KBDDIS;
543 i8042_ctr |= I8042_CTR_KBDINT;
544
545 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
018db6bb
MA
546 i8042_ctr &= ~I8042_CTR_KBDINT;
547 i8042_ctr |= I8042_CTR_KBDDIS;
4eb3c30b 548 pr_err("Failed to enable KBD port\n");
de9ce703
DT
549 return -EIO;
550 }
551
552 return 0;
553}
554
555/*
556 * i8042_enable_aux_port enables AUX (mouse) port on chip
557 */
558
559static int i8042_enable_aux_port(void)
560{
561 i8042_ctr &= ~I8042_CTR_AUXDIS;
562 i8042_ctr |= I8042_CTR_AUXINT;
563
564 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
018db6bb
MA
565 i8042_ctr &= ~I8042_CTR_AUXINT;
566 i8042_ctr |= I8042_CTR_AUXDIS;
4eb3c30b 567 pr_err("Failed to enable AUX port\n");
de9ce703
DT
568 return -EIO;
569 }
570
571 return 0;
572}
573
574/*
575 * i8042_enable_mux_ports enables 4 individual AUX ports after
576 * the controller has been switched into Multiplexed mode
577 */
578
579static int i8042_enable_mux_ports(void)
580{
581 unsigned char param;
582 int i;
583
584 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
585 i8042_command(&param, I8042_CMD_MUX_PFX + i);
586 i8042_command(&param, I8042_CMD_AUX_ENABLE);
587 }
588
589 return i8042_enable_aux_port();
590}
591
1da177e4 592/*
386b3849
DT
593 * i8042_set_mux_mode checks whether the controller has an
594 * active multiplexor and puts the chip into Multiplexed (true)
595 * or Legacy (false) mode.
1da177e4
LT
596 */
597
386b3849 598static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
1da177e4
LT
599{
600
386b3849 601 unsigned char param, val;
1da177e4
LT
602/*
603 * Get rid of bytes in the queue.
604 */
605
606 i8042_flush();
607
608/*
609 * Internal loopback test - send three bytes, they should come back from the
de9ce703 610 * mouse interface, the last should be version.
1da177e4
LT
611 */
612
386b3849
DT
613 param = val = 0xf0;
614 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
615 return -1;
616 param = val = multiplex ? 0x56 : 0xf6;
617 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
1da177e4 618 return -1;
386b3849
DT
619 param = val = multiplex ? 0xa4 : 0xa5;
620 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
1da177e4 621 return -1;
386b3849
DT
622
623/*
624 * Workaround for interference with USB Legacy emulation
625 * that causes a v10.12 MUX to be found.
626 */
627 if (param == 0xac)
1da177e4
LT
628 return -1;
629
630 if (mux_version)
463a4f76 631 *mux_version = param;
1da177e4
LT
632
633 return 0;
634}
635
1da177e4 636/*
de9ce703
DT
637 * i8042_check_mux() checks whether the controller supports the PS/2 Active
638 * Multiplexing specification by Synaptics, Phoenix, Insyde and
639 * LCS/Telegraphics.
1da177e4
LT
640 */
641
f8113416 642static int __init i8042_check_mux(void)
1da177e4 643{
de9ce703
DT
644 unsigned char mux_version;
645
386b3849 646 if (i8042_set_mux_mode(true, &mux_version))
de9ce703
DT
647 return -1;
648
4eb3c30b 649 pr_info("Detected active multiplexing controller, rev %d.%d\n",
de9ce703 650 (mux_version >> 4) & 0xf, mux_version & 0xf);
1da177e4 651
de9ce703
DT
652/*
653 * Disable all muxed ports by disabling AUX.
654 */
1da177e4
LT
655 i8042_ctr |= I8042_CTR_AUXDIS;
656 i8042_ctr &= ~I8042_CTR_AUXINT;
657
658 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
4eb3c30b 659 pr_err("Failed to disable AUX port, can't use MUX\n");
de9ce703 660 return -EIO;
1da177e4
LT
661 }
662
386b3849 663 i8042_mux_present = true;
1da177e4
LT
664
665 return 0;
666}
667
1da177e4 668/*
de9ce703 669 * The following is used to test AUX IRQ delivery.
1da177e4 670 */
f8113416
DT
671static struct completion i8042_aux_irq_delivered __initdata;
672static bool i8042_irq_being_tested __initdata;
1da177e4 673
f8113416 674static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
1da177e4 675{
de9ce703
DT
676 unsigned long flags;
677 unsigned char str, data;
e3758b2a 678 int ret = 0;
1da177e4 679
de9ce703
DT
680 spin_lock_irqsave(&i8042_lock, flags);
681 str = i8042_read_status();
682 if (str & I8042_STR_OBF) {
683 data = i8042_read_data();
4eb3c30b
JP
684 dbg("%02x <- i8042 (aux_test_irq, %s)\n",
685 data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
de9ce703
DT
686 if (i8042_irq_being_tested &&
687 data == 0xa5 && (str & I8042_STR_AUXDATA))
688 complete(&i8042_aux_irq_delivered);
e3758b2a 689 ret = 1;
de9ce703
DT
690 }
691 spin_unlock_irqrestore(&i8042_lock, flags);
1da177e4 692
e3758b2a 693 return IRQ_RETVAL(ret);
1da177e4
LT
694}
695
d2ada559
RS
696/*
697 * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
698 * verifies success by readinng CTR. Used when testing for presence of AUX
699 * port.
700 */
f8113416 701static int __init i8042_toggle_aux(bool on)
d2ada559
RS
702{
703 unsigned char param;
704 int i;
705
706 if (i8042_command(&param,
707 on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
708 return -1;
709
710 /* some chips need some time to set the I8042_CTR_AUXDIS bit */
711 for (i = 0; i < 100; i++) {
712 udelay(50);
713
714 if (i8042_command(&param, I8042_CMD_CTL_RCTR))
715 return -1;
716
717 if (!(param & I8042_CTR_AUXDIS) == on)
718 return 0;
719 }
720
721 return -1;
722}
1da177e4
LT
723
724/*
725 * i8042_check_aux() applies as much paranoia as it can at detecting
726 * the presence of an AUX interface.
727 */
728
f8113416 729static int __init i8042_check_aux(void)
1da177e4 730{
de9ce703 731 int retval = -1;
386b3849
DT
732 bool irq_registered = false;
733 bool aux_loop_broken = false;
de9ce703 734 unsigned long flags;
1da177e4 735 unsigned char param;
1da177e4
LT
736
737/*
738 * Get rid of bytes in the queue.
739 */
740
741 i8042_flush();
742
743/*
744 * Internal loopback test - filters out AT-type i8042's. Unfortunately
745 * SiS screwed up and their 5597 doesn't support the LOOP command even
746 * though it has an AUX port.
747 */
748
749 param = 0x5a;
3ca5de6d
DT
750 retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
751 if (retval || param != 0x5a) {
1da177e4
LT
752
753/*
754 * External connection test - filters out AT-soldered PS/2 i8042's
755 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
756 * 0xfa - no error on some notebooks which ignore the spec
757 * Because it's common for chipsets to return error on perfectly functioning
758 * AUX ports, we test for this only when the LOOP command failed.
759 */
760
de9ce703
DT
761 if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
762 (param && param != 0xfa && param != 0xff))
763 return -1;
1e4865f8 764
3ca5de6d
DT
765/*
766 * If AUX_LOOP completed without error but returned unexpected data
767 * mark it as broken
768 */
769 if (!retval)
386b3849 770 aux_loop_broken = true;
1da177e4
LT
771 }
772
773/*
774 * Bit assignment test - filters out PS/2 i8042's in AT mode
775 */
776
386b3849 777 if (i8042_toggle_aux(false)) {
4eb3c30b
JP
778 pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
779 pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
1da177e4
LT
780 }
781
386b3849 782 if (i8042_toggle_aux(true))
1da177e4
LT
783 return -1;
784
785/*
de9ce703
DT
786 * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
787 * used it for a PCI card or somethig else.
1da177e4
LT
788 */
789
1c7827ae 790 if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
de9ce703
DT
791/*
792 * Without LOOP command we can't test AUX IRQ delivery. Assume the port
793 * is working and hope we are right.
794 */
795 retval = 0;
796 goto out;
797 }
1da177e4 798
de9ce703
DT
799 if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
800 "i8042", i8042_platform_device))
801 goto out;
1da177e4 802
386b3849 803 irq_registered = true;
de9ce703
DT
804
805 if (i8042_enable_aux_port())
806 goto out;
807
808 spin_lock_irqsave(&i8042_lock, flags);
1da177e4 809
de9ce703 810 init_completion(&i8042_aux_irq_delivered);
386b3849 811 i8042_irq_being_tested = true;
de9ce703
DT
812
813 param = 0xa5;
814 retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
815
816 spin_unlock_irqrestore(&i8042_lock, flags);
817
818 if (retval)
819 goto out;
1da177e4 820
de9ce703
DT
821 if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
822 msecs_to_jiffies(250)) == 0) {
1da177e4 823/*
de9ce703
DT
824 * AUX IRQ was never delivered so we need to flush the controller to
825 * get rid of the byte we put there; otherwise keyboard may not work.
1da177e4 826 */
4eb3c30b 827 dbg(" -- i8042 (aux irq test timeout)\n");
de9ce703
DT
828 i8042_flush();
829 retval = -1;
830 }
1da177e4 831
de9ce703 832 out:
1da177e4 833
de9ce703
DT
834/*
835 * Disable the interface.
836 */
1da177e4 837
de9ce703
DT
838 i8042_ctr |= I8042_CTR_AUXDIS;
839 i8042_ctr &= ~I8042_CTR_AUXINT;
1da177e4 840
de9ce703
DT
841 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
842 retval = -1;
1da177e4 843
de9ce703
DT
844 if (irq_registered)
845 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1da177e4 846
de9ce703
DT
847 return retval;
848}
1da177e4 849
de9ce703 850static int i8042_controller_check(void)
1da177e4 851{
de9ce703 852 if (i8042_flush() == I8042_BUFFER_SIZE) {
4eb3c30b 853 pr_err("No controller found\n");
de9ce703
DT
854 return -ENODEV;
855 }
856
857 return 0;
1da177e4
LT
858}
859
de9ce703 860static int i8042_controller_selftest(void)
2673c836
VP
861{
862 unsigned char param;
5ea2fc64 863 int i = 0;
2673c836 864
5ea2fc64
AV
865 /*
866 * We try this 5 times; on some really fragile systems this does not
867 * take the first time...
868 */
869 do {
870
871 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
4eb3c30b 872 pr_err("i8042 controller self test timeout\n");
5ea2fc64
AV
873 return -ENODEV;
874 }
875
876 if (param == I8042_RET_CTL_TEST)
877 return 0;
2673c836 878
4eb3c30b
JP
879 pr_err("i8042 controller selftest failed. (%#x != %#x)\n",
880 param, I8042_RET_CTL_TEST);
5ea2fc64
AV
881 msleep(50);
882 } while (i++ < 5);
2673c836 883
5ea2fc64
AV
884#ifdef CONFIG_X86
885 /*
886 * On x86, we don't fail entire i8042 initialization if controller
887 * reset fails in hopes that keyboard port will still be functional
888 * and user will still get a working keyboard. This is especially
889 * important on netbooks. On other arches we trust hardware more.
890 */
4eb3c30b 891 pr_info("giving up on controller selftest, continuing anyway...\n");
2673c836 892 return 0;
5ea2fc64
AV
893#else
894 return -EIO;
895#endif
2673c836 896}
1da177e4
LT
897
898/*
899 * i8042_controller init initializes the i8042 controller, and,
900 * most importantly, sets it into non-xlated mode if that's
901 * desired.
902 */
903
904static int i8042_controller_init(void)
905{
906 unsigned long flags;
ee1e82ce
DT
907 int n = 0;
908 unsigned char ctr[2];
1da177e4 909
1da177e4 910/*
ee1e82ce 911 * Save the CTR for restore on unload / reboot.
1da177e4
LT
912 */
913
ee1e82ce
DT
914 do {
915 if (n >= 10) {
4eb3c30b 916 pr_err("Unable to get stable CTR read\n");
ee1e82ce
DT
917 return -EIO;
918 }
919
920 if (n != 0)
921 udelay(50);
922
923 if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
4eb3c30b 924 pr_err("Can't read CTR while initializing i8042\n");
ee1e82ce
DT
925 return -EIO;
926 }
927
928 } while (n < 2 || ctr[0] != ctr[1]);
1da177e4 929
ee1e82ce 930 i8042_initial_ctr = i8042_ctr = ctr[0];
1da177e4
LT
931
932/*
933 * Disable the keyboard interface and interrupt.
934 */
935
936 i8042_ctr |= I8042_CTR_KBDDIS;
937 i8042_ctr &= ~I8042_CTR_KBDINT;
938
939/*
940 * Handle keylock.
941 */
942
943 spin_lock_irqsave(&i8042_lock, flags);
944 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
945 if (i8042_unlock)
946 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
82dd9eff 947 else
4eb3c30b 948 pr_warn("Warning: Keylock active\n");
1da177e4
LT
949 }
950 spin_unlock_irqrestore(&i8042_lock, flags);
951
952/*
953 * If the chip is configured into nontranslated mode by the BIOS, don't
954 * bother enabling translating and be happy.
955 */
956
957 if (~i8042_ctr & I8042_CTR_XLATE)
386b3849 958 i8042_direct = true;
1da177e4
LT
959
960/*
961 * Set nontranslated mode for the kbd interface if requested by an option.
962 * After this the kbd interface becomes a simple serial in/out, like the aux
963 * interface is. We don't do this by default, since it can confuse notebook
964 * BIOSes.
965 */
966
967 if (i8042_direct)
968 i8042_ctr &= ~I8042_CTR_XLATE;
969
970/*
971 * Write CTR back.
972 */
973
974 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
4eb3c30b 975 pr_err("Can't write CTR while initializing i8042\n");
de9ce703 976 return -EIO;
1da177e4
LT
977 }
978
ee1e82ce
DT
979/*
980 * Flush whatever accumulated while we were disabling keyboard port.
981 */
982
983 i8042_flush();
984
1da177e4
LT
985 return 0;
986}
987
988
989/*
de9ce703 990 * Reset the controller and reset CRT to the original value set by BIOS.
1da177e4 991 */
de9ce703 992
1da177e4
LT
993static void i8042_controller_reset(void)
994{
de9ce703 995 i8042_flush();
1da177e4 996
8d04ddb6
DT
997/*
998 * Disable both KBD and AUX interfaces so they don't get in the way
999 */
1000
1001 i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
1002 i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
1003
ee1e82ce 1004 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
4eb3c30b 1005 pr_warn("Can't write CTR while resetting\n");
5ddbc77c 1006
1da177e4
LT
1007/*
1008 * Disable MUX mode if present.
1009 */
1010
1011 if (i8042_mux_present)
386b3849 1012 i8042_set_mux_mode(false, NULL);
1da177e4
LT
1013
1014/*
de9ce703 1015 * Reset the controller if requested.
1da177e4
LT
1016 */
1017
1ca56e51
DT
1018 if (i8042_reset)
1019 i8042_controller_selftest();
1da177e4 1020
de9ce703
DT
1021/*
1022 * Restore the original control register setting.
1023 */
1024
1025 if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
4eb3c30b 1026 pr_warn("Can't restore CTR\n");
1da177e4
LT
1027}
1028
1029
1da177e4 1030/*
c7ff0d9c
TS
1031 * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
1032 * when kernel panics. Flashing LEDs is useful for users running X who may
1da177e4
LT
1033 * not see the console and will help distingushing panics from "real"
1034 * lockups.
1035 *
1036 * Note that DELAY has a limit of 10ms so we will not get stuck here
1037 * waiting for KBC to free up even if KBD interrupt is off
1038 */
1039
1040#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
1041
c7ff0d9c 1042static long i8042_panic_blink(int state)
1da177e4
LT
1043{
1044 long delay = 0;
c7ff0d9c 1045 char led;
1da177e4 1046
c7ff0d9c 1047 led = (state) ? 0x01 | 0x04 : 0;
1da177e4
LT
1048 while (i8042_read_status() & I8042_STR_IBF)
1049 DELAY;
4eb3c30b 1050 dbg("%02x -> i8042 (panic blink)\n", 0xed);
19f3c3e3 1051 i8042_suppress_kbd_ack = 2;
1da177e4
LT
1052 i8042_write_data(0xed); /* set leds */
1053 DELAY;
1054 while (i8042_read_status() & I8042_STR_IBF)
1055 DELAY;
1056 DELAY;
4eb3c30b 1057 dbg("%02x -> i8042 (panic blink)\n", led);
1da177e4
LT
1058 i8042_write_data(led);
1059 DELAY;
1da177e4
LT
1060 return delay;
1061}
1062
1063#undef DELAY
1064
d35895db
BP
1065#ifdef CONFIG_X86
1066static void i8042_dritek_enable(void)
1067{
594d6363 1068 unsigned char param = 0x90;
d35895db
BP
1069 int error;
1070
1071 error = i8042_command(&param, 0x1059);
1072 if (error)
4eb3c30b 1073 pr_warn("Failed to enable DRITEK extension: %d\n", error);
d35895db
BP
1074}
1075#endif
1076
82dd9eff 1077#ifdef CONFIG_PM
7e044e05 1078
1da177e4 1079/*
ebd7768d
DT
1080 * Here we try to reset everything back to a state we had
1081 * before suspending.
1da177e4
LT
1082 */
1083
1ca56e51 1084static int i8042_controller_resume(bool force_reset)
1da177e4 1085{
de9ce703 1086 int error;
1da177e4 1087
de9ce703
DT
1088 error = i8042_controller_check();
1089 if (error)
1090 return error;
2673c836 1091
1ca56e51
DT
1092 if (i8042_reset || force_reset) {
1093 error = i8042_controller_selftest();
1094 if (error)
1095 return error;
1096 }
1da177e4
LT
1097
1098/*
82dd9eff 1099 * Restore original CTR value and disable all ports
1da177e4
LT
1100 */
1101
82dd9eff
DT
1102 i8042_ctr = i8042_initial_ctr;
1103 if (i8042_direct)
1104 i8042_ctr &= ~I8042_CTR_XLATE;
de9ce703
DT
1105 i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
1106 i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
1107 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
4eb3c30b 1108 pr_warn("Can't write CTR to resume, retrying...\n");
2f6a77d5
JK
1109 msleep(50);
1110 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
4eb3c30b 1111 pr_err("CTR write retry failed\n");
2f6a77d5
JK
1112 return -EIO;
1113 }
de9ce703 1114 }
1da177e4 1115
d35895db
BP
1116
1117#ifdef CONFIG_X86
1118 if (i8042_dritek)
1119 i8042_dritek_enable();
1120#endif
1121
de9ce703 1122 if (i8042_mux_present) {
386b3849 1123 if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
4eb3c30b 1124 pr_warn("failed to resume active multiplexor, mouse won't work\n");
de9ce703
DT
1125 } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
1126 i8042_enable_aux_port();
1da177e4 1127
de9ce703
DT
1128 if (i8042_ports[I8042_KBD_PORT_NO].serio)
1129 i8042_enable_kbd_port();
1130
7d12e780 1131 i8042_interrupt(0, NULL);
1da177e4
LT
1132
1133 return 0;
1da177e4 1134}
ebd7768d 1135
1ca56e51
DT
1136/*
1137 * Here we try to restore the original BIOS settings to avoid
1138 * upsetting it.
1139 */
1140
1141static int i8042_pm_reset(struct device *dev)
1142{
1143 i8042_controller_reset();
1144
1145 return 0;
1146}
1147
1148static int i8042_pm_resume(struct device *dev)
1149{
1150 /*
1151 * On resume from S2R we always try to reset the controller
1152 * to bring it in a sane state. (In case of S2D we expect
1153 * BIOS to reset the controller for us.)
1154 */
1155 return i8042_controller_resume(true);
1156}
1157
c2d1a2a1
AJ
1158static int i8042_pm_thaw(struct device *dev)
1159{
1160 i8042_interrupt(0, NULL);
1161
1162 return 0;
1163}
1164
1ca56e51
DT
1165static int i8042_pm_restore(struct device *dev)
1166{
1167 return i8042_controller_resume(false);
1168}
1169
ebd7768d
DT
1170static const struct dev_pm_ops i8042_pm_ops = {
1171 .suspend = i8042_pm_reset,
1ca56e51 1172 .resume = i8042_pm_resume,
c2d1a2a1 1173 .thaw = i8042_pm_thaw,
ebd7768d
DT
1174 .poweroff = i8042_pm_reset,
1175 .restore = i8042_pm_restore,
1176};
1177
82dd9eff 1178#endif /* CONFIG_PM */
1da177e4
LT
1179
1180/*
1181 * We need to reset the 8042 back to original mode on system shutdown,
1182 * because otherwise BIOSes will be confused.
1183 */
1184
3ae5eaec 1185static void i8042_shutdown(struct platform_device *dev)
1da177e4 1186{
82dd9eff 1187 i8042_controller_reset();
1da177e4
LT
1188}
1189
f8113416 1190static int __init i8042_create_kbd_port(void)
1da177e4
LT
1191{
1192 struct serio *serio;
1193 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
1194
d39969de 1195 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
0854e52d
DT
1196 if (!serio)
1197 return -ENOMEM;
1198
1199 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
1200 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
0854e52d
DT
1201 serio->start = i8042_start;
1202 serio->stop = i8042_stop;
5ddbc77c 1203 serio->close = i8042_port_close;
0854e52d
DT
1204 serio->port_data = port;
1205 serio->dev.parent = &i8042_platform_device->dev;
de9ce703 1206 strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
0854e52d
DT
1207 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
1208
1209 port->serio = serio;
de9ce703 1210 port->irq = I8042_KBD_IRQ;
0854e52d 1211
de9ce703 1212 return 0;
1da177e4
LT
1213}
1214
f8113416 1215static int __init i8042_create_aux_port(int idx)
1da177e4
LT
1216{
1217 struct serio *serio;
de9ce703
DT
1218 int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
1219 struct i8042_port *port = &i8042_ports[port_no];
1da177e4 1220
d39969de 1221 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
0854e52d
DT
1222 if (!serio)
1223 return -ENOMEM;
1224
1225 serio->id.type = SERIO_8042;
1226 serio->write = i8042_aux_write;
0854e52d
DT
1227 serio->start = i8042_start;
1228 serio->stop = i8042_stop;
1229 serio->port_data = port;
1230 serio->dev.parent = &i8042_platform_device->dev;
de9ce703
DT
1231 if (idx < 0) {
1232 strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
1233 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
5ddbc77c 1234 serio->close = i8042_port_close;
de9ce703
DT
1235 } else {
1236 snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
1237 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
1238 }
0854e52d
DT
1239
1240 port->serio = serio;
de9ce703
DT
1241 port->mux = idx;
1242 port->irq = I8042_AUX_IRQ;
0854e52d 1243
de9ce703 1244 return 0;
1da177e4
LT
1245}
1246
f8113416 1247static void __init i8042_free_kbd_port(void)
1da177e4 1248{
de9ce703
DT
1249 kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
1250 i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
1251}
1da177e4 1252
f8113416 1253static void __init i8042_free_aux_ports(void)
de9ce703
DT
1254{
1255 int i;
0854e52d 1256
de9ce703
DT
1257 for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
1258 kfree(i8042_ports[i].serio);
1259 i8042_ports[i].serio = NULL;
1260 }
1261}
0854e52d 1262
f8113416 1263static void __init i8042_register_ports(void)
de9ce703
DT
1264{
1265 int i;
0854e52d 1266
de9ce703
DT
1267 for (i = 0; i < I8042_NUM_PORTS; i++) {
1268 if (i8042_ports[i].serio) {
1269 printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
1270 i8042_ports[i].serio->name,
1271 (unsigned long) I8042_DATA_REG,
1272 (unsigned long) I8042_COMMAND_REG,
1273 i8042_ports[i].irq);
1274 serio_register_port(i8042_ports[i].serio);
1275 }
1276 }
1da177e4
LT
1277}
1278
7a1904c3 1279static void __devexit i8042_unregister_ports(void)
1da177e4 1280{
de9ce703 1281 int i;
1da177e4 1282
de9ce703
DT
1283 for (i = 0; i < I8042_NUM_PORTS; i++) {
1284 if (i8042_ports[i].serio) {
1285 serio_unregister_port(i8042_ports[i].serio);
1286 i8042_ports[i].serio = NULL;
1287 }
1288 }
1289}
1290
181d683d
DT
1291/*
1292 * Checks whether port belongs to i8042 controller.
1293 */
1294bool i8042_check_port_owner(const struct serio *port)
1295{
1296 int i;
1297
1298 for (i = 0; i < I8042_NUM_PORTS; i++)
1299 if (i8042_ports[i].serio == port)
1300 return true;
1301
1302 return false;
1303}
1304EXPORT_SYMBOL(i8042_check_port_owner);
1305
de9ce703
DT
1306static void i8042_free_irqs(void)
1307{
1308 if (i8042_aux_irq_registered)
1309 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1310 if (i8042_kbd_irq_registered)
1311 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1312
386b3849 1313 i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
de9ce703
DT
1314}
1315
f8113416 1316static int __init i8042_setup_aux(void)
de9ce703
DT
1317{
1318 int (*aux_enable)(void);
1319 int error;
1320 int i;
1da177e4 1321
de9ce703 1322 if (i8042_check_aux())
87fd6318 1323 return -ENODEV;
1da177e4 1324
de9ce703
DT
1325 if (i8042_nomux || i8042_check_mux()) {
1326 error = i8042_create_aux_port(-1);
1327 if (error)
1328 goto err_free_ports;
1329 aux_enable = i8042_enable_aux_port;
1330 } else {
1331 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1332 error = i8042_create_aux_port(i);
1333 if (error)
1334 goto err_free_ports;
0854e52d 1335 }
de9ce703 1336 aux_enable = i8042_enable_mux_ports;
1da177e4
LT
1337 }
1338
de9ce703
DT
1339 error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
1340 "i8042", i8042_platform_device);
1341 if (error)
1342 goto err_free_ports;
945ef0d4 1343
de9ce703
DT
1344 if (aux_enable())
1345 goto err_free_irq;
1da177e4 1346
386b3849 1347 i8042_aux_irq_registered = true;
1da177e4 1348 return 0;
0854e52d 1349
de9ce703
DT
1350 err_free_irq:
1351 free_irq(I8042_AUX_IRQ, i8042_platform_device);
1352 err_free_ports:
1353 i8042_free_aux_ports();
1354 return error;
1355}
0854e52d 1356
f8113416 1357static int __init i8042_setup_kbd(void)
de9ce703
DT
1358{
1359 int error;
1360
1361 error = i8042_create_kbd_port();
1362 if (error)
1363 return error;
1364
1365 error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
1366 "i8042", i8042_platform_device);
1367 if (error)
1368 goto err_free_port;
1369
1370 error = i8042_enable_kbd_port();
1371 if (error)
1372 goto err_free_irq;
1373
386b3849 1374 i8042_kbd_irq_registered = true;
de9ce703
DT
1375 return 0;
1376
1377 err_free_irq:
1378 free_irq(I8042_KBD_IRQ, i8042_platform_device);
1379 err_free_port:
1380 i8042_free_kbd_port();
1381 return error;
1da177e4
LT
1382}
1383
f8113416 1384static int __init i8042_probe(struct platform_device *dev)
1da177e4 1385{
de9ce703 1386 int error;
1da177e4 1387
ec62e1c8
DT
1388 i8042_platform_device = dev;
1389
1ca56e51
DT
1390 if (i8042_reset) {
1391 error = i8042_controller_selftest();
1392 if (error)
1393 return error;
1394 }
1da177e4 1395
de9ce703
DT
1396 error = i8042_controller_init();
1397 if (error)
1398 return error;
1399
d35895db
BP
1400#ifdef CONFIG_X86
1401 if (i8042_dritek)
1402 i8042_dritek_enable();
1403#endif
1404
de9ce703
DT
1405 if (!i8042_noaux) {
1406 error = i8042_setup_aux();
1407 if (error && error != -ENODEV && error != -EBUSY)
1408 goto out_fail;
1409 }
1410
1411 if (!i8042_nokbd) {
1412 error = i8042_setup_kbd();
1413 if (error)
1414 goto out_fail;
1415 }
de9ce703
DT
1416/*
1417 * Ok, everything is ready, let's register all serio ports
1418 */
1419 i8042_register_ports();
1420
1421 return 0;
1422
1423 out_fail:
1424 i8042_free_aux_ports(); /* in case KBD failed but AUX not */
1425 i8042_free_irqs();
1426 i8042_controller_reset();
ec62e1c8 1427 i8042_platform_device = NULL;
de9ce703
DT
1428
1429 return error;
1430}
1431
1432static int __devexit i8042_remove(struct platform_device *dev)
1433{
1434 i8042_unregister_ports();
1435 i8042_free_irqs();
1436 i8042_controller_reset();
ec62e1c8 1437 i8042_platform_device = NULL;
1da177e4 1438
87fd6318
DT
1439 return 0;
1440}
1441
1442static struct platform_driver i8042_driver = {
1443 .driver = {
1444 .name = "i8042",
1445 .owner = THIS_MODULE,
ebd7768d
DT
1446#ifdef CONFIG_PM
1447 .pm = &i8042_pm_ops,
1448#endif
87fd6318 1449 },
87fd6318 1450 .remove = __devexit_p(i8042_remove),
82dd9eff 1451 .shutdown = i8042_shutdown,
87fd6318
DT
1452};
1453
1454static int __init i8042_init(void)
1455{
ec62e1c8 1456 struct platform_device *pdev;
87fd6318
DT
1457 int err;
1458
1459 dbg_init();
1460
1461 err = i8042_platform_init();
1462 if (err)
1463 return err;
1464
de9ce703
DT
1465 err = i8042_controller_check();
1466 if (err)
1467 goto err_platform_exit;
87fd6318 1468
ec62e1c8
DT
1469 pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
1470 if (IS_ERR(pdev)) {
1471 err = PTR_ERR(pdev);
f8113416 1472 goto err_platform_exit;
87fd6318
DT
1473 }
1474
de9ce703
DT
1475 panic_blink = i8042_panic_blink;
1476
87fd6318
DT
1477 return 0;
1478
87fd6318
DT
1479 err_platform_exit:
1480 i8042_platform_exit();
87fd6318
DT
1481 return err;
1482}
1483
1484static void __exit i8042_exit(void)
1485{
f8113416 1486 platform_device_unregister(i8042_platform_device);
af045b86 1487 platform_driver_unregister(&i8042_driver);
1da177e4
LT
1488 i8042_platform_exit();
1489
1490 panic_blink = NULL;
1491}
1492
1493module_init(i8042_init);
1494module_exit(i8042_exit);
This page took 1.018633 seconds and 5 git commands to generate.