i8042.c 32 KB

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