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