i8042.c 33 KB

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