/drivers/input/serio/i8042.c

http://github.com/mirrors/linux · C · 1645 lines · 1038 code · 330 blank · 277 comment · 196 complexity · c69f72866b1a58177740af2f88a49fdf MD5 · raw file

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