MODULE_AUTHOR("Vojtech Pavlik ");
MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
MODULE_LICENSE("GPL");
staticbool i8042_nokbd;
module_param_named(nokbd, i8042_nokbd, bool, 0);
MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
staticbool i8042_noaux;
module_param_named(noaux, i8042_noaux, bool, 0);
MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
staticbool i8042_nomux;
module_param_named(nomux, i8042_nomux, bool, 0);
MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
staticbool i8042_direct;
module_param_named(direct, i8042_direct, bool, 0);
MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
staticbool i8042_dumbkbd;
module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
staticbool i8042_noloop;
module_param_named(noloop, i8042_noloop, bool, 0);
MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
/* * i8042_lock protects serialization between i8042_command and * the interrupt handler.
*/ static DEFINE_SPINLOCK(i8042_lock);
/* * Writers to AUX and KBD ports as well as users issuing i8042_command * directly should acquire i8042_mutex (by means of calling * i8042_lock_chip() and i8042_unlock_chip() helpers) to ensure that * they do not disturb each other (unfortunately in many i8042 * implementations write to one of the ports will immediately abort * command that is being processed by another port).
*/ static DEFINE_MUTEX(i8042_mutex);
/* * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to * be ready for reading values from it / writing values to it. * Called always with i8042_lock held.
*/
staticint i8042_wait_read(void)
{ int i = 0;
while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
udelay(50);
i++;
} return -(i == I8042_CTL_TIMEOUT);
}
staticint i8042_wait_write(void)
{ int i = 0;
while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
udelay(50);
i++;
} return -(i == I8042_CTL_TIMEOUT);
}
/* * i8042_flush() flushes all data that may be in the keyboard and mouse buffers * of the i8042 down the toilet.
*/
staticint i8042_flush(void)
{ unsignedchar data, str; int count = 0;
guard(spinlock_irqsave)(&i8042_lock);
while ((str = i8042_read_status()) & I8042_STR_OBF) { if (count++ >= I8042_BUFFER_SIZE) return -EIO;
/* * i8042_command() executes a command on the i8042. It also sends the input * parameter(s) of the commands to it, and receives the output value(s). The * parameters are to be stored in the param array, and the output is placed * into the same array. The number of the parameters and output values is * encoded in bits 8-11 of the command number.
*/
staticint __i8042_command(unsignedchar *param, int command)
{ int i, error;
if (i8042_noloop && command == I8042_CMD_AUX_LOOP) return -1;
error = i8042_wait_write(); if (error) return error;
/* * See if there is any data appeared while we were messing with * port state.
*/
i8042_handle_data(0);
}
/* * i8042_start() is called by serio core when port is about to finish * registering. It will mark port as existing so i8042_interrupt can * start sending data through it.
*/ staticint i8042_start(struct serio *serio)
{ struct i8042_port *port = serio->port_data;
device_set_wakeup_capable(&serio->dev, true);
/* * On platforms using suspend-to-idle, allow the keyboard to * wake up the system from sleep by enabling keyboard wakeups * by default. This is consistent with keyboard wakeup * behavior on many platforms using suspend-to-RAM (ACPI S3) * by default.
*/ if (pm_suspend_default_s2idle() &&
serio == i8042_ports[I8042_KBD_PORT_NO].serio) {
device_set_wakeup_enable(&serio->dev, true);
}
/* * i8042_stop() marks serio port as non-existing so i8042_interrupt * will not try to send data to the port that is about to go away. * The function is called by serio core as part of unregister procedure.
*/ staticvoid i8042_stop(struct serio *serio)
{ struct i8042_port *port = serio->port_data;
/* * We need to make sure that interrupt handler finishes using * our serio port before we return from this function. * We synchronize with both AUX and KBD IRQs because there is * a (very unlikely) chance that AUX IRQ is raised for KBD port * and vice versa.
*/
synchronize_irq(I8042_AUX_IRQ);
synchronize_irq(I8042_KBD_IRQ);
}
/* * i8042_filter() filters out unwanted bytes from the input data stream. * It is called from i8042_interrupt and thus is running with interrupts * off and i8042_lock held.
*/ staticbool i8042_filter(unsignedchar data, unsignedchar str, struct serio *serio)
{ if (unlikely(i8042_suppress_kbd_ack)) { if ((~str & I8042_STR_AUXDATA) &&
(data == 0xfa || data == 0xfe)) {
i8042_suppress_kbd_ack--;
dbg("Extra keyboard ACK - filtered out\n"); returntrue;
}
}
if (!i8042_platform_filter) returnfalse;
if (i8042_platform_filter(data, str, serio, i8042_platform_filter_context)) {
dbg("Filtered out by platform filter\n"); returntrue;
}
returnfalse;
}
/* * i8042_handle_mux() handles case when data is coming from one of * the multiplexed ports. It would be simple if not for quirks with * handling errors: * * When MUXERR condition is signalled the data register can only contain * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately * it is not always the case. Some KBCs also report 0xfc when there is * nothing connected to the port while others sometimes get confused which * port the data came from and signal error leaving the data intact. They * _do not_ revert to legacy mode (actually I've never seen KBC reverting * to legacy mode yet, when we see one we'll add proper handling). * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the * rest assume that the data came from the same serio last byte * was transmitted (if transmission happened not too long ago).
*/ staticint i8042_handle_mux(u8 str, u8 *data, unsignedint *dfl)
{ staticunsignedlong last_transmit; staticunsignedlong last_port; unsignedint mux_port;
mux_port = (str >> 6) & 3;
*dfl = 0;
if (str & I8042_STR_MUXERR) {
dbg("MUX error, status is %02x, data is %02x\n",
str, *data);
switch (*data) { default: if (time_before(jiffies, last_transmit + HZ/10)) {
mux_port = last_port; break;
}
fallthrough; /* report timeout */ case 0xfc: case 0xfd: case 0xfe:
*dfl = SERIO_TIMEOUT;
*data = 0xfe; break; case 0xff:
*dfl = SERIO_PARITY;
*data = 0xfe; break;
}
}
last_port = mux_port;
last_transmit = jiffies;
return I8042_MUX_PORT_NO + mux_port;
}
/* * i8042_handle_data() is the most important function in this driver - * it reads the data from the i8042, determines its destination serio * port, and sends received byte to the upper layers. * * Returns true if there was data waiting, false otherwise.
*/ staticbool i8042_handle_data(int irq)
{ struct i8042_port *port; struct serio *serio; unsignedchar str, data; unsignedint dfl; unsignedint port_no; bool filtered;
if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
i8042_ctr &= ~I8042_CTR_AUXINT;
i8042_ctr |= I8042_CTR_AUXDIS;
pr_err("Failed to enable AUX port\n"); return -EIO;
}
return 0;
}
/* * i8042_enable_mux_ports enables 4 individual AUX ports after * the controller has been switched into Multiplexed mode
*/
staticint i8042_enable_mux_ports(void)
{ unsignedchar param; int i;
for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
i8042_command(¶m, I8042_CMD_MUX_PFX + i);
i8042_command(¶m, I8042_CMD_AUX_ENABLE);
}
return i8042_enable_aux_port();
}
/* * i8042_set_mux_mode checks whether the controller has an * active multiplexor and puts the chip into Multiplexed (true) * or Legacy (false) mode.
*/
unsignedchar param, val; /* * Get rid of bytes in the queue.
*/
i8042_flush();
/* * Internal loopback test - send three bytes, they should come back from the * mouse interface, the last should be version.
*/
param = val = 0xf0; if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val) return -1;
param = val = multiplex ? 0x56 : 0xf6; if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param != val) return -1;
param = val = multiplex ? 0xa4 : 0xa5; if (i8042_command(¶m, I8042_CMD_AUX_LOOP) || param == val) return -1;
/* * Workaround for interference with USB Legacy emulation * that causes a v10.12 MUX to be found.
*/ if (param == 0xac) return -1;
if (mux_version)
*mux_version = param;
return 0;
}
/* * i8042_check_mux() checks whether the controller supports the PS/2 Active * Multiplexing specification by Synaptics, Phoenix, Insyde and * LCS/Telegraphics.
*/
if (i8042_irq_being_tested && data == 0xa5 && (str & I8042_STR_AUXDATA))
complete(&i8042_aux_irq_delivered);
return IRQ_HANDLED;
}
/* * i8042_toggle_aux - enables or disables AUX port on i8042 via command and * verifies success by readinng CTR. Used when testing for presence of AUX * port.
*/ staticint i8042_toggle_aux(bool on)
{ unsignedchar param; int i;
if (i8042_command(¶m,
on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE)) return -1;
/* some chips need some time to set the I8042_CTR_AUXDIS bit */ for (i = 0; i < 100; i++) {
udelay(50);
if (i8042_command(¶m, I8042_CMD_CTL_RCTR)) return -1;
if (!(param & I8042_CTR_AUXDIS) == on) return 0;
}
return -1;
}
/* * i8042_check_aux() applies as much paranoia as it can at detecting * the presence of an AUX interface.
*/
/* * Internal loopback test - filters out AT-type i8042's. Unfortunately * SiS screwed up and their 5597 doesn't support the LOOP command even * though it has an AUX port.
*/
/* * External connection test - filters out AT-soldered PS/2 i8042's * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error * 0xfa - no error on some notebooks which ignore the spec * Because it's common for chipsets to return error on perfectly functioning * AUX ports, we test for this only when the LOOP command failed.
*/
/* * If AUX_LOOP completed without error but returned unexpected data * mark it as broken
*/ if (!retval)
aux_loop_broken = true;
}
/* * Bit assignment test - filters out PS/2 i8042's in AT mode
*/
if (i8042_toggle_aux(false)) {
pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
}
if (i8042_toggle_aux(true)) return -1;
/* * Reset keyboard (needed on some laptops to successfully detect * touchpad, e.g., some Gigabyte laptop models with Elantech * touchpads).
*/ if (i8042_kbdreset) {
pr_warn("Attempting to reset device connected to KBD port\n");
i8042_kbd_write(NULL, (unsignedchar) 0xff);
}
/* * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and * used it for a PCI card or somethig else.
*/
if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) { /* * Without LOOP command we can't test AUX IRQ delivery. Assume the port * is working and hope we are right.
*/
retval = 0; goto out;
}
if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED, "i8042", i8042_platform_device)) goto out;
if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
msecs_to_jiffies(250)) == 0) { /* * AUX IRQ was never delivered so we need to flush the controller to * get rid of the byte we put there; otherwise keyboard may not work.
*/
dbg(" -- i8042 (aux irq test timeout)\n");
i8042_flush();
retval = -1;
}
#ifdef CONFIG_X86 /* * On x86, we don't fail entire i8042 initialization if controller * reset fails in hopes that keyboard port will still be functional * and user will still get a working keyboard. This is especially * important on netbooks. On other arches we trust hardware more.
*/
pr_info("giving up on controller selftest, continuing anyway...\n"); return 0; #else
pr_err("i8042 controller selftest failed\n"); return -EIO; #endif
}
/* * i8042_controller_init initializes the i8042 controller, and, * most importantly, sets it into non-xlated mode if that's * desired.
*/
staticint i8042_controller_init(void)
{ int n = 0; unsignedchar ctr[2];
/* * Save the CTR for restore on unload / reboot.
*/
do { if (n >= 10) {
pr_err("Unable to get stable CTR read\n"); return -EIO;
}
if (n != 0)
udelay(50);
if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
pr_err("Can't read CTR while initializing i8042\n"); return i8042_probe_defer ? -EPROBE_DEFER : -EIO;
}
} while (n < 2 || ctr[0] != ctr[1]);
i8042_initial_ctr = i8042_ctr = ctr[0];
/* * Disable the keyboard interface and interrupt.
*/
scoped_guard(spinlock_irqsave, &i8042_lock) { if (~i8042_read_status() & I8042_STR_KEYLOCK) { if (i8042_unlock)
i8042_ctr |= I8042_CTR_IGNKEYLOCK; else
pr_warn("Warning: Keylock active\n");
}
}
/* * If the chip is configured into nontranslated mode by the BIOS, don't * bother enabling translating and be happy.
*/
if (~i8042_ctr & I8042_CTR_XLATE)
i8042_direct = true;
/* * Set nontranslated mode for the kbd interface if requested by an option. * After this the kbd interface becomes a simple serial in/out, like the aux * interface is. We don't do this by default, since it can confuse notebook * BIOSes.
*/
if (i8042_direct)
i8042_ctr &= ~I8042_CTR_XLATE;
/* * Write CTR back.
*/
if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
pr_err("Can't write CTR while initializing i8042\n"); return -EIO;
}
/* * Flush whatever accumulated while we were disabling keyboard port.
*/
i8042_flush();
return 0;
}
/* * Reset the controller and reset CRT to the original value set by BIOS.
*/
/* * Restore the original control register setting.
*/
if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
pr_warn("Can't restore CTR\n");
}
/* * i8042_panic_blink() will turn the keyboard LEDs on or off and is called * when kernel panics. Flashing LEDs is useful for users running X who may * not see the console and will help distinguishing panics from "real" * lockups. * * Note that DELAY has a limit of 10ms so we will not get stuck here * waiting for KBC to free up even if KBD interrupt is off
*/
#define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
staticlong i8042_panic_blink(int state)
{ long delay = 0; char led;
#ifdef CONFIG_X86 if (i8042_dritek)
i8042_dritek_enable(); #endif
if (i8042_mux_present) { if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
pr_warn("failed to resume active multiplexor, mouse won't work\n");
} elseif (i8042_ports[I8042_AUX_PORT_NO].serio) {
i8042_enable_aux_port();
}
if (i8042_ports[I8042_KBD_PORT_NO].serio)
i8042_enable_kbd_port();
i8042_handle_data(0);
return 0;
}
/* * Here we try to restore the original BIOS settings to avoid * upsetting it.
*/
staticint i8042_pm_suspend(struct device *dev)
{ int i;
if (!i8042_forcenorestore && pm_suspend_via_firmware())
i8042_controller_reset(true);
/* Set up serio interrupts for system wakeup. */ for (i = 0; i < I8042_NUM_PORTS; i++) { struct serio *serio = i8042_ports[i].serio;
if (serio && device_may_wakeup(&serio->dev))
enable_irq_wake(i8042_ports[i].irq);
}
return 0;
}
staticint i8042_pm_resume_noirq(struct device *dev)
{ if (i8042_forcenorestore || !pm_resume_via_firmware())
i8042_handle_data(0);
return 0;
}
staticint i8042_pm_resume(struct device *dev)
{ bool want_reset; int i;
for (i = 0; i < I8042_NUM_PORTS; i++) { struct serio *serio = i8042_ports[i].serio;
if (serio && device_may_wakeup(&serio->dev))
disable_irq_wake(i8042_ports[i].irq);
}
/* * If platform firmware was not going to be involved in suspend, we did * not restore the controller state to whatever it had been at boot * time, so we do not need to do anything.
*/ if (i8042_forcenorestore || !pm_suspend_via_firmware()) return 0;
/* * We only need to reset the controller if we are resuming after handing * off control to the platform firmware, otherwise we can simply restore * the mode.
*/
want_reset = pm_resume_via_firmware();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.