i8042.c 33 KB

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