i8042.c 34 KB

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