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