i8042.c 36 KB

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