blob: 2a76d08c4d3e55f306bc704d398e611887e54612 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * i8042 keyboard and mouse controller driver for Linux
3 *
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 */
12
13#include <linux/delay.h>
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/interrupt.h>
17#include <linux/ioport.h>
18#include <linux/config.h>
19#include <linux/init.h>
20#include <linux/serio.h>
21#include <linux/err.h>
22#include <linux/rcupdate.h>
23
24#include <asm/io.h>
25
26MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
28MODULE_LICENSE("GPL");
29
30static unsigned int i8042_noaux;
31module_param_named(noaux, i8042_noaux, bool, 0);
32MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
33
34static unsigned int i8042_nomux;
35module_param_named(nomux, i8042_nomux, bool, 0);
36MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing conrtoller is present.");
37
38static unsigned int i8042_unlock;
39module_param_named(unlock, i8042_unlock, bool, 0);
40MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
41
42static unsigned int i8042_reset;
43module_param_named(reset, i8042_reset, bool, 0);
44MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
45
46static unsigned int i8042_direct;
47module_param_named(direct, i8042_direct, bool, 0);
48MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
49
50static unsigned int i8042_dumbkbd;
51module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
52MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
53
54static unsigned int i8042_noloop;
55module_param_named(noloop, i8042_noloop, bool, 0);
56MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
57
58static unsigned int i8042_blink_frequency = 500;
59module_param_named(panicblink, i8042_blink_frequency, uint, 0600);
60MODULE_PARM_DESC(panicblink, "Frequency with which keyboard LEDs should blink when kernel panics");
61
62#ifdef CONFIG_PNP
63static int i8042_nopnp;
64module_param_named(nopnp, i8042_nopnp, bool, 0);
65MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
66#endif
67
68#define DEBUG
69#ifdef DEBUG
70static int i8042_debug;
71module_param_named(debug, i8042_debug, bool, 0600);
72MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
73#endif
74
75__obsolete_setup("i8042_noaux");
76__obsolete_setup("i8042_nomux");
77__obsolete_setup("i8042_unlock");
78__obsolete_setup("i8042_reset");
79__obsolete_setup("i8042_direct");
80__obsolete_setup("i8042_dumbkbd");
81
82#include "i8042.h"
83
84static DEFINE_SPINLOCK(i8042_lock);
85
86struct i8042_port {
87 struct serio *serio;
88 int irq;
89 unsigned char disable;
90 unsigned char irqen;
91 unsigned char exists;
92 signed char mux;
93 char name[8];
94};
95
96#define I8042_KBD_PORT_NO 0
97#define I8042_AUX_PORT_NO 1
98#define I8042_MUX_PORT_NO 2
99#define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
100static struct i8042_port i8042_ports[I8042_NUM_PORTS] = {
101 {
102 .disable = I8042_CTR_KBDDIS,
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500103 .irqen = I8042_CTR_KBDINT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .mux = -1,
105 .name = "KBD",
106 },
107 {
108 .disable = I8042_CTR_AUXDIS,
109 .irqen = I8042_CTR_AUXINT,
110 .mux = -1,
111 .name = "AUX",
112 }
113};
114
115static unsigned char i8042_initial_ctr;
116static unsigned char i8042_ctr;
117static unsigned char i8042_mux_open;
118static unsigned char i8042_mux_present;
119static struct timer_list i8042_timer;
120static struct platform_device *i8042_platform_device;
121
122
123/*
124 * Shared IRQ's require a device pointer, but this driver doesn't support
125 * multiple devices
126 */
127#define i8042_request_irq_cookie (&i8042_timer)
128
129static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs);
130
131/*
132 * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
133 * be ready for reading values from it / writing values to it.
134 * Called always with i8042_lock held.
135 */
136
137static int i8042_wait_read(void)
138{
139 int i = 0;
140 while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
141 udelay(50);
142 i++;
143 }
144 return -(i == I8042_CTL_TIMEOUT);
145}
146
147static int i8042_wait_write(void)
148{
149 int i = 0;
150 while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
151 udelay(50);
152 i++;
153 }
154 return -(i == I8042_CTL_TIMEOUT);
155}
156
157/*
158 * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
159 * of the i8042 down the toilet.
160 */
161
162static int i8042_flush(void)
163{
164 unsigned long flags;
165 unsigned char data, str;
166 int i = 0;
167
168 spin_lock_irqsave(&i8042_lock, flags);
169
170 while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
171 udelay(50);
172 data = i8042_read_data();
173 i++;
174 dbg("%02x <- i8042 (flush, %s)", data,
175 str & I8042_STR_AUXDATA ? "aux" : "kbd");
176 }
177
178 spin_unlock_irqrestore(&i8042_lock, flags);
179
180 return i;
181}
182
183/*
184 * i8042_command() executes a command on the i8042. It also sends the input
185 * parameter(s) of the commands to it, and receives the output value(s). The
186 * parameters are to be stored in the param array, and the output is placed
187 * into the same array. The number of the parameters and output values is
188 * encoded in bits 8-11 of the command number.
189 */
190
191static int i8042_command(unsigned char *param, int command)
192{
193 unsigned long flags;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500194 int i, retval, auxerr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
197 return -1;
198
199 spin_lock_irqsave(&i8042_lock, flags);
200
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500201 if ((retval = i8042_wait_write()))
202 goto out;
203
204 dbg("%02x -> i8042 (command)", command & 0xff);
205 i8042_write_command(command & 0xff);
206
207 for (i = 0; i < ((command >> 12) & 0xf); i++) {
208 if ((retval = i8042_wait_write()))
209 goto out;
210 dbg("%02x -> i8042 (parameter)", param[i]);
211 i8042_write_data(param[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500214 for (i = 0; i < ((command >> 8) & 0xf); i++) {
215 if ((retval = i8042_wait_read()))
216 goto out;
217
218 if (command == I8042_CMD_AUX_LOOP &&
219 !(i8042_read_status() & I8042_STR_AUXDATA)) {
220 retval = auxerr = -1;
221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500224 param[i] = i8042_read_data();
225 dbg("%02x <- i8042 (return)", param[i]);
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (retval)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500229 dbg(" -- i8042 (%s)", auxerr ? "auxerr" : "timeout");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500231 out:
232 spin_unlock_irqrestore(&i8042_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return retval;
234}
235
236/*
237 * i8042_kbd_write() sends a byte out through the keyboard interface.
238 */
239
240static int i8042_kbd_write(struct serio *port, unsigned char c)
241{
242 unsigned long flags;
243 int retval = 0;
244
245 spin_lock_irqsave(&i8042_lock, flags);
246
247 if(!(retval = i8042_wait_write())) {
248 dbg("%02x -> i8042 (kbd-data)", c);
249 i8042_write_data(c);
250 }
251
252 spin_unlock_irqrestore(&i8042_lock, flags);
253
254 return retval;
255}
256
257/*
258 * i8042_aux_write() sends a byte out through the aux interface.
259 */
260
261static int i8042_aux_write(struct serio *serio, unsigned char c)
262{
263 struct i8042_port *port = serio->port_data;
264 int retval;
265
266/*
267 * Send the byte out.
268 */
269
270 if (port->mux == -1)
271 retval = i8042_command(&c, I8042_CMD_AUX_SEND);
272 else
273 retval = i8042_command(&c, I8042_CMD_MUX_SEND + port->mux);
274
275/*
276 * Make sure the interrupt happens and the character is received even
277 * in the case the IRQ isn't wired, so that we can receive further
278 * characters later.
279 */
280
281 i8042_interrupt(0, NULL, NULL);
282 return retval;
283}
284
285/*
286 * i8042_activate_port() enables port on a chip.
287 */
288
289static int i8042_activate_port(struct i8042_port *port)
290{
291 if (!port->serio)
292 return -1;
293
294 i8042_flush();
295
296 /*
297 * Enable port again here because it is disabled if we are
298 * resuming (normally it is enabled already).
299 */
300 i8042_ctr &= ~port->disable;
301
302 i8042_ctr |= port->irqen;
303
304 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
305 i8042_ctr &= ~port->irqen;
306 return -1;
307 }
308
309 return 0;
310}
311
312
313/*
314 * i8042_open() is called when a port is open by the higher layer.
315 * It allocates the interrupt and calls i8042_enable_port.
316 */
317
318static int i8042_open(struct serio *serio)
319{
320 struct i8042_port *port = serio->port_data;
321
322 if (port->mux != -1)
323 if (i8042_mux_open++)
324 return 0;
325
326 if (request_irq(port->irq, i8042_interrupt,
327 SA_SHIRQ, "i8042", i8042_request_irq_cookie)) {
328 printk(KERN_ERR "i8042.c: Can't get irq %d for %s, unregistering the port.\n", port->irq, port->name);
329 goto irq_fail;
330 }
331
332 if (i8042_activate_port(port)) {
333 printk(KERN_ERR "i8042.c: Can't activate %s, unregistering the port\n", port->name);
334 goto activate_fail;
335 }
336
337 i8042_interrupt(0, NULL, NULL);
338
339 return 0;
340
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500341 activate_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 free_irq(port->irq, i8042_request_irq_cookie);
343
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500344 irq_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 serio_unregister_port_delayed(serio);
346
347 return -1;
348}
349
350/*
351 * i8042_close() frees the interrupt, so that it can possibly be used
352 * by another driver. We never know - if the user doesn't have a mouse,
353 * the BIOS could have used the AUX interrupt for PCI.
354 */
355
356static void i8042_close(struct serio *serio)
357{
358 struct i8042_port *port = serio->port_data;
359
360 if (port->mux != -1)
361 if (--i8042_mux_open)
362 return;
363
364 i8042_ctr &= ~port->irqen;
365
366 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
367 printk(KERN_WARNING "i8042.c: Can't write CTR while closing %s.\n", port->name);
368/*
369 * We still want to continue and free IRQ so if more data keeps coming in
370 * kernel will just ignore the irq.
371 */
372 }
373
374 free_irq(port->irq, i8042_request_irq_cookie);
375
376 i8042_flush();
377}
378
379/*
380 * i8042_start() is called by serio core when port is about to finish
381 * registering. It will mark port as existing so i8042_interrupt can
382 * start sending data through it.
383 */
384static int i8042_start(struct serio *serio)
385{
386 struct i8042_port *port = serio->port_data;
387
388 port->exists = 1;
389 mb();
390 return 0;
391}
392
393/*
394 * i8042_stop() marks serio port as non-existing so i8042_interrupt
395 * will not try to send data to the port that is about to go away.
396 * The function is called by serio core as part of unregister procedure.
397 */
398static void i8042_stop(struct serio *serio)
399{
400 struct i8042_port *port = serio->port_data;
401
402 port->exists = 0;
Paul E. McKenneyb2b18662005-06-25 14:55:38 -0700403 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 port->serio = NULL;
405}
406
407/*
408 * i8042_interrupt() is the most important function in this driver -
409 * it handles the interrupts from the i8042, and sends incoming bytes
410 * to the upper layers.
411 */
412
413static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs)
414{
415 struct i8042_port *port;
416 unsigned long flags;
417 unsigned char str, data;
418 unsigned int dfl;
419 unsigned int port_no;
420 int ret;
421
422 mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
423
424 spin_lock_irqsave(&i8042_lock, flags);
425 str = i8042_read_status();
426 if (unlikely(~str & I8042_STR_OBF)) {
427 spin_unlock_irqrestore(&i8042_lock, flags);
428 if (irq) dbg("Interrupt %d, without any data", irq);
429 ret = 0;
430 goto out;
431 }
432 data = i8042_read_data();
433 spin_unlock_irqrestore(&i8042_lock, flags);
434
435 if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
436 static unsigned long last_transmit;
437 static unsigned char last_str;
438
439 dfl = 0;
440 if (str & I8042_STR_MUXERR) {
441 dbg("MUX error, status is %02x, data is %02x", str, data);
442 switch (data) {
443 default:
444/*
445 * When MUXERR condition is signalled the data register can only contain
446 * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
447 * it is not always the case. Some KBC just get confused which port the
448 * data came from and signal error leaving the data intact. They _do not_
449 * revert to legacy mode (actually I've never seen KBC reverting to legacy
450 * mode yet, when we see one we'll add proper handling).
451 * Anyway, we will assume that the data came from the same serio last byte
452 * was transmitted (if transmission happened not too long ago).
453 */
454 if (time_before(jiffies, last_transmit + HZ/10)) {
455 str = last_str;
456 break;
457 }
458 /* fall through - report timeout */
459 case 0xfd:
460 case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
461 case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
462 }
463 }
464
465 port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
466 last_str = str;
467 last_transmit = jiffies;
468 } else {
469
470 dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
471 ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
472
473 port_no = (str & I8042_STR_AUXDATA) ?
474 I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
475 }
476
477 port = &i8042_ports[port_no];
478
479 dbg("%02x <- i8042 (interrupt, %s, %d%s%s)",
480 data, port->name, irq,
481 dfl & SERIO_PARITY ? ", bad parity" : "",
482 dfl & SERIO_TIMEOUT ? ", timeout" : "");
483
484 if (likely(port->exists))
485 serio_interrupt(port->serio, data, dfl, regs);
486
487 ret = 1;
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500488 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return IRQ_RETVAL(ret);
490}
491
492/*
493 * i8042_set_mux_mode checks whether the controller has an active
494 * multiplexor and puts the chip into Multiplexed (1) or Legacy (0) mode.
495 */
496
497static int i8042_set_mux_mode(unsigned int mode, unsigned char *mux_version)
498{
499
500 unsigned char param;
501/*
502 * Get rid of bytes in the queue.
503 */
504
505 i8042_flush();
506
507/*
508 * Internal loopback test - send three bytes, they should come back from the
509 * mouse interface, the last should be version. Note that we negate mouseport
510 * command responses for the i8042_check_aux() routine.
511 */
512
513 param = 0xf0;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500514 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -1;
516 param = mode ? 0x56 : 0xf6;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500517 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != (mode ? 0x56 : 0xf6))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -1;
519 param = mode ? 0xa4 : 0xa5;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500520 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == (mode ? 0xa4 : 0xa5))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -1;
522
523 if (mux_version)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500524 *mux_version = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 return 0;
527}
528
529
530/*
531 * i8042_enable_mux_ports enables 4 individual AUX ports after
532 * the controller has been switched into Multiplexed mode
533 */
534
535static int i8042_enable_mux_ports(void)
536{
537 unsigned char param;
538 int i;
539/*
540 * Disable all muxed ports by disabling AUX.
541 */
542
543 i8042_ctr |= I8042_CTR_AUXDIS;
544 i8042_ctr &= ~I8042_CTR_AUXINT;
545
546 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
547 printk(KERN_ERR "i8042.c: Failed to disable AUX port, can't use MUX.\n");
548 return -1;
549 }
550
551/*
552 * Enable all muxed ports.
553 */
554
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500555 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 i8042_command(&param, I8042_CMD_MUX_PFX + i);
557 i8042_command(&param, I8042_CMD_AUX_ENABLE);
558 }
559
560 return 0;
561}
562
563
564/*
565 * i8042_check_mux() checks whether the controller supports the PS/2 Active
566 * Multiplexing specification by Synaptics, Phoenix, Insyde and
567 * LCS/Telegraphics.
568 */
569
570static int __init i8042_check_mux(void)
571{
572 unsigned char mux_version;
573
574 if (i8042_set_mux_mode(1, &mux_version))
575 return -1;
576
577 /* Workaround for interference with USB Legacy emulation */
578 /* that causes a v10.12 MUX to be found. */
579 if (mux_version == 0xAC)
580 return -1;
581
582 printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
583 (mux_version >> 4) & 0xf, mux_version & 0xf);
584
585 if (i8042_enable_mux_ports())
586 return -1;
587
588 i8042_mux_present = 1;
589 return 0;
590}
591
592
593/*
594 * i8042_check_aux() applies as much paranoia as it can at detecting
595 * the presence of an AUX interface.
596 */
597
598static int __init i8042_check_aux(void)
599{
600 unsigned char param;
601 static int i8042_check_aux_cookie;
602
603/*
604 * Check if AUX irq is available. If it isn't, then there is no point
605 * in trying to detect AUX presence.
606 */
607
608 if (request_irq(i8042_ports[I8042_AUX_PORT_NO].irq, i8042_interrupt,
609 SA_SHIRQ, "i8042", &i8042_check_aux_cookie))
610 return -1;
611 free_irq(i8042_ports[I8042_AUX_PORT_NO].irq, &i8042_check_aux_cookie);
612
613/*
614 * Get rid of bytes in the queue.
615 */
616
617 i8042_flush();
618
619/*
620 * Internal loopback test - filters out AT-type i8042's. Unfortunately
621 * SiS screwed up and their 5597 doesn't support the LOOP command even
622 * though it has an AUX port.
623 */
624
625 param = 0x5a;
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500626 if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0x5a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628/*
629 * External connection test - filters out AT-soldered PS/2 i8042's
630 * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
631 * 0xfa - no error on some notebooks which ignore the spec
632 * Because it's common for chipsets to return error on perfectly functioning
633 * AUX ports, we test for this only when the LOOP command failed.
634 */
635
636 if (i8042_command(&param, I8042_CMD_AUX_TEST)
Dmitry Torokhov463a4f72005-07-15 01:51:56 -0500637 || (param && param != 0xfa && param != 0xff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -1;
639 }
640
641/*
642 * Bit assignment test - filters out PS/2 i8042's in AT mode
643 */
644
645 if (i8042_command(&param, I8042_CMD_AUX_DISABLE))
646 return -1;
647 if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (~param & I8042_CTR_AUXDIS)) {
648 printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
649 printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
650 }
651
652 if (i8042_command(&param, I8042_CMD_AUX_ENABLE))
653 return -1;
654 if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (param & I8042_CTR_AUXDIS))
655 return -1;
656
657/*
658 * Disable the interface.
659 */
660
661 i8042_ctr |= I8042_CTR_AUXDIS;
662 i8042_ctr &= ~I8042_CTR_AUXINT;
663
664 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
665 return -1;
666
667 return 0;
668}
669
670
671/*
672 * i8042_port_register() marks the device as existing,
673 * registers it, and reports to the user.
674 */
675
676static int __init i8042_port_register(struct i8042_port *port)
677{
678 i8042_ctr &= ~port->disable;
679
680 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
681 printk(KERN_WARNING "i8042.c: Can't write CTR while registering.\n");
682 kfree(port->serio);
683 port->serio = NULL;
684 i8042_ctr |= port->disable;
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500685 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 printk(KERN_INFO "serio: i8042 %s port at %#lx,%#lx irq %d\n",
689 port->name,
690 (unsigned long) I8042_DATA_REG,
691 (unsigned long) I8042_COMMAND_REG,
692 port->irq);
693
694 serio_register_port(port->serio);
695
696 return 0;
697}
698
699
700static void i8042_timer_func(unsigned long data)
701{
702 i8042_interrupt(0, NULL, NULL);
703}
704
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500705static int i8042_ctl_test(void)
706{
707 unsigned char param;
708
709 if (!i8042_reset)
710 return 0;
711
712 if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
713 printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
714 return -1;
715 }
716
717 if (param != I8042_RET_CTL_TEST) {
718 printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
719 param, I8042_RET_CTL_TEST);
720 return -1;
721 }
722
723 return 0;
724}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726/*
727 * i8042_controller init initializes the i8042 controller, and,
728 * most importantly, sets it into non-xlated mode if that's
729 * desired.
730 */
731
732static int i8042_controller_init(void)
733{
734 unsigned long flags;
735
736/*
737 * Test the i8042. We need to know if it thinks it's working correctly
738 * before doing anything else.
739 */
740
741 if (i8042_flush() == I8042_BUFFER_SIZE) {
742 printk(KERN_ERR "i8042.c: No controller found.\n");
743 return -1;
744 }
745
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500746 if (i8042_ctl_test())
747 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/*
750 * Save the CTR for restoral on unload / reboot.
751 */
752
753 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
754 printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
755 return -1;
756 }
757
758 i8042_initial_ctr = i8042_ctr;
759
760/*
761 * Disable the keyboard interface and interrupt.
762 */
763
764 i8042_ctr |= I8042_CTR_KBDDIS;
765 i8042_ctr &= ~I8042_CTR_KBDINT;
766
767/*
768 * Handle keylock.
769 */
770
771 spin_lock_irqsave(&i8042_lock, flags);
772 if (~i8042_read_status() & I8042_STR_KEYLOCK) {
773 if (i8042_unlock)
774 i8042_ctr |= I8042_CTR_IGNKEYLOCK;
775 else
776 printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
777 }
778 spin_unlock_irqrestore(&i8042_lock, flags);
779
780/*
781 * If the chip is configured into nontranslated mode by the BIOS, don't
782 * bother enabling translating and be happy.
783 */
784
785 if (~i8042_ctr & I8042_CTR_XLATE)
786 i8042_direct = 1;
787
788/*
789 * Set nontranslated mode for the kbd interface if requested by an option.
790 * After this the kbd interface becomes a simple serial in/out, like the aux
791 * interface is. We don't do this by default, since it can confuse notebook
792 * BIOSes.
793 */
794
795 if (i8042_direct)
796 i8042_ctr &= ~I8042_CTR_XLATE;
797
798/*
799 * Write CTR back.
800 */
801
802 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
803 printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
804 return -1;
805 }
806
807 return 0;
808}
809
810
811/*
812 * Reset the controller.
813 */
814static void i8042_controller_reset(void)
815{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816/*
817 * Reset the controller if requested.
818 */
819
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500820 i8042_ctl_test();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822/*
823 * Disable MUX mode if present.
824 */
825
826 if (i8042_mux_present)
827 i8042_set_mux_mode(0, NULL);
828
829/*
830 * Restore the original control register setting.
831 */
832
833 i8042_ctr = i8042_initial_ctr;
834
835 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
836 printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
837}
838
839
840/*
841 * Here we try to reset everything back to a state in which the BIOS will be
842 * able to talk to the hardware when rebooting.
843 */
844
845static void i8042_controller_cleanup(void)
846{
847 int i;
848
849 i8042_flush();
850
851/*
852 * Reset anything that is connected to the ports.
853 */
854
855 for (i = 0; i < I8042_NUM_PORTS; i++)
856 if (i8042_ports[i].exists)
857 serio_cleanup(i8042_ports[i].serio);
858
859 i8042_controller_reset();
860}
861
862
863/*
864 * i8042_panic_blink() will flash the keyboard LEDs and is called when
865 * kernel panics. Flashing LEDs is useful for users running X who may
866 * not see the console and will help distingushing panics from "real"
867 * lockups.
868 *
869 * Note that DELAY has a limit of 10ms so we will not get stuck here
870 * waiting for KBC to free up even if KBD interrupt is off
871 */
872
873#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
874
875static long i8042_panic_blink(long count)
876{
877 long delay = 0;
878 static long last_blink;
879 static char led;
880
881 /*
882 * We expect frequency to be about 1/2s. KDB uses about 1s.
883 * Make sure they are different.
884 */
885 if (!i8042_blink_frequency)
886 return 0;
887 if (count - last_blink < i8042_blink_frequency)
888 return 0;
889
890 led ^= 0x01 | 0x04;
891 while (i8042_read_status() & I8042_STR_IBF)
892 DELAY;
893 i8042_write_data(0xed); /* set leds */
894 DELAY;
895 while (i8042_read_status() & I8042_STR_IBF)
896 DELAY;
897 DELAY;
898 i8042_write_data(led);
899 DELAY;
900 last_blink = count;
901 return delay;
902}
903
904#undef DELAY
905
906/*
907 * Here we try to restore the original BIOS settings
908 */
909
910static int i8042_suspend(struct device *dev, pm_message_t state, u32 level)
911{
912 if (level == SUSPEND_DISABLE) {
913 del_timer_sync(&i8042_timer);
914 i8042_controller_reset();
915 }
916
917 return 0;
918}
919
920
921/*
922 * Here we try to reset everything back to a state in which suspended
923 */
924
925static int i8042_resume(struct device *dev, u32 level)
926{
927 int i;
928
929 if (level != RESUME_ENABLE)
930 return 0;
931
Vojtech Pavlik2673c832005-05-28 02:11:27 -0500932 if (i8042_ctl_test())
933 return -1;
934
935 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
936 printk(KERN_ERR "i8042: Can't write CTR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -1;
938 }
939
940 if (i8042_mux_present)
941 if (i8042_set_mux_mode(1, NULL) || i8042_enable_mux_ports())
942 printk(KERN_WARNING "i8042: failed to resume active multiplexor, mouse won't work.\n");
943
944/*
945 * Activate all ports.
946 */
947
948 for (i = 0; i < I8042_NUM_PORTS; i++)
949 i8042_activate_port(&i8042_ports[i]);
950
951/*
952 * Restart timer (for polling "stuck" data)
953 */
954 mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
955
956 panic_blink = i8042_panic_blink;
957
958 return 0;
959
960}
961
962/*
963 * We need to reset the 8042 back to original mode on system shutdown,
964 * because otherwise BIOSes will be confused.
965 */
966
967static void i8042_shutdown(struct device *dev)
968{
969 i8042_controller_cleanup();
970}
971
972static struct device_driver i8042_driver = {
973 .name = "i8042",
974 .bus = &platform_bus_type,
975 .suspend = i8042_suspend,
976 .resume = i8042_resume,
977 .shutdown = i8042_shutdown,
978};
979
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500980static int __init i8042_create_kbd_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 struct serio *serio;
983 struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
984
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500985 serio = kcalloc(1, sizeof(struct serio), GFP_KERNEL);
986 if (!serio)
987 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dmitry Torokhov0854e522005-09-04 01:41:27 -0500989 serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
990 serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
991 serio->open = i8042_open;
992 serio->close = i8042_close;
993 serio->start = i8042_start;
994 serio->stop = i8042_stop;
995 serio->port_data = port;
996 serio->dev.parent = &i8042_platform_device->dev;
997 strlcpy(serio->name, "i8042 Kbd Port", sizeof(serio->name));
998 strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
999
1000 port->serio = serio;
1001
1002 return i8042_port_register(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001005static int __init i8042_create_aux_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct serio *serio;
1008 struct i8042_port *port = &i8042_ports[I8042_AUX_PORT_NO];
1009
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001010 serio = kcalloc(1, sizeof(struct serio), GFP_KERNEL);
1011 if (!serio)
1012 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001014 serio->id.type = SERIO_8042;
1015 serio->write = i8042_aux_write;
1016 serio->open = i8042_open;
1017 serio->close = i8042_close;
1018 serio->start = i8042_start;
1019 serio->stop = i8042_stop;
1020 serio->port_data = port;
1021 serio->dev.parent = &i8042_platform_device->dev;
1022 strlcpy(serio->name, "i8042 Aux Port", sizeof(serio->name));
1023 strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
1024
1025 port->serio = serio;
1026
1027 return i8042_port_register(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001030static int __init i8042_create_mux_port(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct serio *serio;
1033 struct i8042_port *port = &i8042_ports[I8042_MUX_PORT_NO + index];
1034
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001035 serio = kcalloc(1, sizeof(struct serio), GFP_KERNEL);
1036 if (!serio)
1037 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001039 serio->id.type = SERIO_8042;
1040 serio->write = i8042_aux_write;
1041 serio->open = i8042_open;
1042 serio->close = i8042_close;
1043 serio->start = i8042_start;
1044 serio->stop = i8042_stop;
1045 serio->port_data = port;
1046 serio->dev.parent = &i8042_platform_device->dev;
1047 snprintf(serio->name, sizeof(serio->name), "i8042 Aux-%d Port", index);
1048 snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, index + 1);
1049
1050 *port = i8042_ports[I8042_AUX_PORT_NO];
1051 port->exists = 0;
1052 snprintf(port->name, sizeof(port->name), "AUX%d", index);
1053 port->mux = index;
1054 port->serio = serio;
1055
1056 return i8042_port_register(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059static int __init i8042_init(void)
1060{
1061 int i;
1062 int err;
1063
1064 dbg_init();
1065
1066 init_timer(&i8042_timer);
1067 i8042_timer.function = i8042_timer_func;
1068
Dmitry Torokhov8d5987a2005-09-04 01:41:38 -05001069 err = i8042_platform_init();
1070 if (err)
1071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 i8042_ports[I8042_AUX_PORT_NO].irq = I8042_AUX_IRQ;
1074 i8042_ports[I8042_KBD_PORT_NO].irq = I8042_KBD_IRQ;
1075
1076 if (i8042_controller_init()) {
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001077 err = -ENODEV;
1078 goto err_platform_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
1081 err = driver_register(&i8042_driver);
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001082 if (err)
1083 goto err_controller_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 i8042_platform_device = platform_device_register_simple("i8042", -1, NULL, 0);
1086 if (IS_ERR(i8042_platform_device)) {
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001087 err = PTR_ERR(i8042_platform_device);
1088 goto err_unregister_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
1091 if (!i8042_noaux && !i8042_check_aux()) {
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001092 if (!i8042_nomux && !i8042_check_mux()) {
1093 for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
1094 err = i8042_create_mux_port(i);
1095 if (err)
1096 goto err_unregister_ports;
1097 }
1098 } else {
1099 err = i8042_create_aux_port();
1100 if (err)
1101 goto err_unregister_ports;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001105 err = i8042_create_kbd_port();
1106 if (err)
1107 goto err_unregister_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
1110
1111 return 0;
Dmitry Torokhov0854e522005-09-04 01:41:27 -05001112
1113 err_unregister_ports:
1114 for (i = 0; i < I8042_NUM_PORTS; i++)
1115 if (i8042_ports[i].serio)
1116 serio_unregister_port(i8042_ports[i].serio);
1117 platform_device_unregister(i8042_platform_device);
1118 err_unregister_driver:
1119 driver_unregister(&i8042_driver);
1120 err_controller_cleanup:
1121 i8042_controller_cleanup();
1122 err_platform_exit:
1123 i8042_platform_exit();
1124
1125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128static void __exit i8042_exit(void)
1129{
1130 int i;
1131
1132 i8042_controller_cleanup();
1133
1134 for (i = 0; i < I8042_NUM_PORTS; i++)
1135 if (i8042_ports[i].exists)
1136 serio_unregister_port(i8042_ports[i].serio);
1137
1138 del_timer_sync(&i8042_timer);
1139
1140 platform_device_unregister(i8042_platform_device);
1141 driver_unregister(&i8042_driver);
1142
1143 i8042_platform_exit();
1144
1145 panic_blink = NULL;
1146}
1147
1148module_init(i8042_init);
1149module_exit(i8042_exit);