i8042.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. if (i8042_irq_being_tested &&
  494. data == 0xa5 && (str & I8042_STR_AUXDATA))
  495. complete(&i8042_aux_irq_delivered);
  496. ret = 1;
  497. }
  498. spin_unlock_irqrestore(&i8042_lock, flags);
  499. return IRQ_RETVAL(ret);
  500. }
  501. /*
  502. * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
  503. * verifies success by readinng CTR. Used when testing for presence of AUX
  504. * port.
  505. */
  506. static int __init i8042_toggle_aux(bool on)
  507. {
  508. unsigned char param;
  509. int i;
  510. if (i8042_command(&param,
  511. on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
  512. return -1;
  513. /* some chips need some time to set the I8042_CTR_AUXDIS bit */
  514. for (i = 0; i < 100; i++) {
  515. udelay(50);
  516. if (i8042_command(&param, I8042_CMD_CTL_RCTR))
  517. return -1;
  518. if (!(param & I8042_CTR_AUXDIS) == on)
  519. return 0;
  520. }
  521. return -1;
  522. }
  523. /*
  524. * i8042_check_aux() applies as much paranoia as it can at detecting
  525. * the presence of an AUX interface.
  526. */
  527. static int __init i8042_check_aux(void)
  528. {
  529. int retval = -1;
  530. bool irq_registered = false;
  531. bool aux_loop_broken = false;
  532. unsigned long flags;
  533. unsigned char param;
  534. /*
  535. * Get rid of bytes in the queue.
  536. */
  537. i8042_flush();
  538. /*
  539. * Internal loopback test - filters out AT-type i8042's. Unfortunately
  540. * SiS screwed up and their 5597 doesn't support the LOOP command even
  541. * though it has an AUX port.
  542. */
  543. param = 0x5a;
  544. retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
  545. if (retval || param != 0x5a) {
  546. /*
  547. * External connection test - filters out AT-soldered PS/2 i8042's
  548. * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
  549. * 0xfa - no error on some notebooks which ignore the spec
  550. * Because it's common for chipsets to return error on perfectly functioning
  551. * AUX ports, we test for this only when the LOOP command failed.
  552. */
  553. if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
  554. (param && param != 0xfa && param != 0xff))
  555. return -1;
  556. /*
  557. * If AUX_LOOP completed without error but returned unexpected data
  558. * mark it as broken
  559. */
  560. if (!retval)
  561. aux_loop_broken = true;
  562. }
  563. /*
  564. * Bit assignment test - filters out PS/2 i8042's in AT mode
  565. */
  566. if (i8042_toggle_aux(false)) {
  567. printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
  568. printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
  569. }
  570. if (i8042_toggle_aux(true))
  571. return -1;
  572. /*
  573. * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
  574. * used it for a PCI card or somethig else.
  575. */
  576. if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
  577. /*
  578. * Without LOOP command we can't test AUX IRQ delivery. Assume the port
  579. * is working and hope we are right.
  580. */
  581. retval = 0;
  582. goto out;
  583. }
  584. if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
  585. "i8042", i8042_platform_device))
  586. goto out;
  587. irq_registered = true;
  588. if (i8042_enable_aux_port())
  589. goto out;
  590. spin_lock_irqsave(&i8042_lock, flags);
  591. init_completion(&i8042_aux_irq_delivered);
  592. i8042_irq_being_tested = true;
  593. param = 0xa5;
  594. retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
  595. spin_unlock_irqrestore(&i8042_lock, flags);
  596. if (retval)
  597. goto out;
  598. if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
  599. msecs_to_jiffies(250)) == 0) {
  600. /*
  601. * AUX IRQ was never delivered so we need to flush the controller to
  602. * get rid of the byte we put there; otherwise keyboard may not work.
  603. */
  604. i8042_flush();
  605. retval = -1;
  606. }
  607. out:
  608. /*
  609. * Disable the interface.
  610. */
  611. i8042_ctr |= I8042_CTR_AUXDIS;
  612. i8042_ctr &= ~I8042_CTR_AUXINT;
  613. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  614. retval = -1;
  615. if (irq_registered)
  616. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  617. return retval;
  618. }
  619. static int i8042_controller_check(void)
  620. {
  621. if (i8042_flush() == I8042_BUFFER_SIZE) {
  622. printk(KERN_ERR "i8042.c: No controller found.\n");
  623. return -ENODEV;
  624. }
  625. return 0;
  626. }
  627. static int i8042_controller_selftest(void)
  628. {
  629. unsigned char param;
  630. int i = 0;
  631. if (!i8042_reset)
  632. return 0;
  633. /*
  634. * We try this 5 times; on some really fragile systems this does not
  635. * take the first time...
  636. */
  637. do {
  638. if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
  639. printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
  640. return -ENODEV;
  641. }
  642. if (param == I8042_RET_CTL_TEST)
  643. return 0;
  644. printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
  645. param, I8042_RET_CTL_TEST);
  646. msleep(50);
  647. } while (i++ < 5);
  648. #ifdef CONFIG_X86
  649. /*
  650. * On x86, we don't fail entire i8042 initialization if controller
  651. * reset fails in hopes that keyboard port will still be functional
  652. * and user will still get a working keyboard. This is especially
  653. * important on netbooks. On other arches we trust hardware more.
  654. */
  655. printk(KERN_INFO
  656. "i8042: giving up on controller selftest, continuing anyway...\n");
  657. return 0;
  658. #else
  659. return -EIO;
  660. #endif
  661. }
  662. /*
  663. * i8042_controller init initializes the i8042 controller, and,
  664. * most importantly, sets it into non-xlated mode if that's
  665. * desired.
  666. */
  667. static int i8042_controller_init(void)
  668. {
  669. unsigned long flags;
  670. /*
  671. * Save the CTR for restoral on unload / reboot.
  672. */
  673. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
  674. printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
  675. return -EIO;
  676. }
  677. i8042_initial_ctr = i8042_ctr;
  678. /*
  679. * Disable the keyboard interface and interrupt.
  680. */
  681. i8042_ctr |= I8042_CTR_KBDDIS;
  682. i8042_ctr &= ~I8042_CTR_KBDINT;
  683. /*
  684. * Handle keylock.
  685. */
  686. spin_lock_irqsave(&i8042_lock, flags);
  687. if (~i8042_read_status() & I8042_STR_KEYLOCK) {
  688. if (i8042_unlock)
  689. i8042_ctr |= I8042_CTR_IGNKEYLOCK;
  690. else
  691. printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
  692. }
  693. spin_unlock_irqrestore(&i8042_lock, flags);
  694. /*
  695. * If the chip is configured into nontranslated mode by the BIOS, don't
  696. * bother enabling translating and be happy.
  697. */
  698. if (~i8042_ctr & I8042_CTR_XLATE)
  699. i8042_direct = true;
  700. /*
  701. * Set nontranslated mode for the kbd interface if requested by an option.
  702. * After this the kbd interface becomes a simple serial in/out, like the aux
  703. * interface is. We don't do this by default, since it can confuse notebook
  704. * BIOSes.
  705. */
  706. if (i8042_direct)
  707. i8042_ctr &= ~I8042_CTR_XLATE;
  708. /*
  709. * Write CTR back.
  710. */
  711. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  712. printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
  713. return -EIO;
  714. }
  715. return 0;
  716. }
  717. /*
  718. * Reset the controller and reset CRT to the original value set by BIOS.
  719. */
  720. static void i8042_controller_reset(void)
  721. {
  722. i8042_flush();
  723. /*
  724. * Disable both KBD and AUX interfaces so they don't get in the way
  725. */
  726. i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
  727. i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
  728. if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
  729. printk(KERN_WARNING "i8042.c: Can't write CTR while resetting.\n");
  730. /*
  731. * Disable MUX mode if present.
  732. */
  733. if (i8042_mux_present)
  734. i8042_set_mux_mode(false, NULL);
  735. /*
  736. * Reset the controller if requested.
  737. */
  738. i8042_controller_selftest();
  739. /*
  740. * Restore the original control register setting.
  741. */
  742. if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
  743. printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
  744. }
  745. /*
  746. * i8042_panic_blink() will flash the keyboard LEDs and is called when
  747. * kernel panics. Flashing LEDs is useful for users running X who may
  748. * not see the console and will help distingushing panics from "real"
  749. * lockups.
  750. *
  751. * Note that DELAY has a limit of 10ms so we will not get stuck here
  752. * waiting for KBC to free up even if KBD interrupt is off
  753. */
  754. #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
  755. static long i8042_panic_blink(long count)
  756. {
  757. long delay = 0;
  758. static long last_blink;
  759. static char led;
  760. /*
  761. * We expect frequency to be about 1/2s. KDB uses about 1s.
  762. * Make sure they are different.
  763. */
  764. if (!i8042_blink_frequency)
  765. return 0;
  766. if (count - last_blink < i8042_blink_frequency)
  767. return 0;
  768. led ^= 0x01 | 0x04;
  769. while (i8042_read_status() & I8042_STR_IBF)
  770. DELAY;
  771. dbg("%02x -> i8042 (panic blink)", 0xed);
  772. i8042_suppress_kbd_ack = 2;
  773. i8042_write_data(0xed); /* set leds */
  774. DELAY;
  775. while (i8042_read_status() & I8042_STR_IBF)
  776. DELAY;
  777. DELAY;
  778. dbg("%02x -> i8042 (panic blink)", led);
  779. i8042_write_data(led);
  780. DELAY;
  781. last_blink = count;
  782. return delay;
  783. }
  784. #undef DELAY
  785. #ifdef CONFIG_X86
  786. static void i8042_dritek_enable(void)
  787. {
  788. char param = 0x90;
  789. int error;
  790. error = i8042_command(&param, 0x1059);
  791. if (error)
  792. printk(KERN_WARNING
  793. "Failed to enable DRITEK extension: %d\n",
  794. error);
  795. }
  796. #endif
  797. #ifdef CONFIG_PM
  798. /*
  799. * Here we try to restore the original BIOS settings to avoid
  800. * upsetting it.
  801. */
  802. static int i8042_pm_reset(struct device *dev)
  803. {
  804. i8042_controller_reset();
  805. return 0;
  806. }
  807. /*
  808. * Here we try to reset everything back to a state we had
  809. * before suspending.
  810. */
  811. static int i8042_pm_restore(struct device *dev)
  812. {
  813. int error;
  814. error = i8042_controller_check();
  815. if (error)
  816. return error;
  817. error = i8042_controller_selftest();
  818. if (error)
  819. return error;
  820. /*
  821. * Restore original CTR value and disable all ports
  822. */
  823. i8042_ctr = i8042_initial_ctr;
  824. if (i8042_direct)
  825. i8042_ctr &= ~I8042_CTR_XLATE;
  826. i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
  827. i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
  828. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  829. printk(KERN_WARNING "i8042: Can't write CTR to resume, retrying...\n");
  830. msleep(50);
  831. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  832. printk(KERN_ERR "i8042: CTR write retry failed\n");
  833. return -EIO;
  834. }
  835. }
  836. #ifdef CONFIG_X86
  837. if (i8042_dritek)
  838. i8042_dritek_enable();
  839. #endif
  840. if (i8042_mux_present) {
  841. if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
  842. printk(KERN_WARNING
  843. "i8042: failed to resume active multiplexor, "
  844. "mouse won't work.\n");
  845. } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
  846. i8042_enable_aux_port();
  847. if (i8042_ports[I8042_KBD_PORT_NO].serio)
  848. i8042_enable_kbd_port();
  849. i8042_interrupt(0, NULL);
  850. return 0;
  851. }
  852. static const struct dev_pm_ops i8042_pm_ops = {
  853. .suspend = i8042_pm_reset,
  854. .resume = i8042_pm_restore,
  855. .poweroff = i8042_pm_reset,
  856. .restore = i8042_pm_restore,
  857. };
  858. #endif /* CONFIG_PM */
  859. /*
  860. * We need to reset the 8042 back to original mode on system shutdown,
  861. * because otherwise BIOSes will be confused.
  862. */
  863. static void i8042_shutdown(struct platform_device *dev)
  864. {
  865. i8042_controller_reset();
  866. }
  867. static int __init i8042_create_kbd_port(void)
  868. {
  869. struct serio *serio;
  870. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  871. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  872. if (!serio)
  873. return -ENOMEM;
  874. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  875. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  876. serio->start = i8042_start;
  877. serio->stop = i8042_stop;
  878. serio->close = i8042_port_close;
  879. serio->port_data = port;
  880. serio->dev.parent = &i8042_platform_device->dev;
  881. strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
  882. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  883. port->serio = serio;
  884. port->irq = I8042_KBD_IRQ;
  885. return 0;
  886. }
  887. static int __init i8042_create_aux_port(int idx)
  888. {
  889. struct serio *serio;
  890. int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
  891. struct i8042_port *port = &i8042_ports[port_no];
  892. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  893. if (!serio)
  894. return -ENOMEM;
  895. serio->id.type = SERIO_8042;
  896. serio->write = i8042_aux_write;
  897. serio->start = i8042_start;
  898. serio->stop = i8042_stop;
  899. serio->port_data = port;
  900. serio->dev.parent = &i8042_platform_device->dev;
  901. if (idx < 0) {
  902. strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
  903. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  904. serio->close = i8042_port_close;
  905. } else {
  906. snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
  907. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
  908. }
  909. port->serio = serio;
  910. port->mux = idx;
  911. port->irq = I8042_AUX_IRQ;
  912. return 0;
  913. }
  914. static void __init i8042_free_kbd_port(void)
  915. {
  916. kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
  917. i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
  918. }
  919. static void __init i8042_free_aux_ports(void)
  920. {
  921. int i;
  922. for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
  923. kfree(i8042_ports[i].serio);
  924. i8042_ports[i].serio = NULL;
  925. }
  926. }
  927. static void __init i8042_register_ports(void)
  928. {
  929. int i;
  930. for (i = 0; i < I8042_NUM_PORTS; i++) {
  931. if (i8042_ports[i].serio) {
  932. printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
  933. i8042_ports[i].serio->name,
  934. (unsigned long) I8042_DATA_REG,
  935. (unsigned long) I8042_COMMAND_REG,
  936. i8042_ports[i].irq);
  937. serio_register_port(i8042_ports[i].serio);
  938. }
  939. }
  940. }
  941. static void __devexit i8042_unregister_ports(void)
  942. {
  943. int i;
  944. for (i = 0; i < I8042_NUM_PORTS; i++) {
  945. if (i8042_ports[i].serio) {
  946. serio_unregister_port(i8042_ports[i].serio);
  947. i8042_ports[i].serio = NULL;
  948. }
  949. }
  950. }
  951. /*
  952. * Checks whether port belongs to i8042 controller.
  953. */
  954. bool i8042_check_port_owner(const struct serio *port)
  955. {
  956. int i;
  957. for (i = 0; i < I8042_NUM_PORTS; i++)
  958. if (i8042_ports[i].serio == port)
  959. return true;
  960. return false;
  961. }
  962. EXPORT_SYMBOL(i8042_check_port_owner);
  963. static void i8042_free_irqs(void)
  964. {
  965. if (i8042_aux_irq_registered)
  966. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  967. if (i8042_kbd_irq_registered)
  968. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  969. i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
  970. }
  971. static int __init i8042_setup_aux(void)
  972. {
  973. int (*aux_enable)(void);
  974. int error;
  975. int i;
  976. if (i8042_check_aux())
  977. return -ENODEV;
  978. if (i8042_nomux || i8042_check_mux()) {
  979. error = i8042_create_aux_port(-1);
  980. if (error)
  981. goto err_free_ports;
  982. aux_enable = i8042_enable_aux_port;
  983. } else {
  984. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  985. error = i8042_create_aux_port(i);
  986. if (error)
  987. goto err_free_ports;
  988. }
  989. aux_enable = i8042_enable_mux_ports;
  990. }
  991. error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
  992. "i8042", i8042_platform_device);
  993. if (error)
  994. goto err_free_ports;
  995. if (aux_enable())
  996. goto err_free_irq;
  997. i8042_aux_irq_registered = true;
  998. return 0;
  999. err_free_irq:
  1000. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  1001. err_free_ports:
  1002. i8042_free_aux_ports();
  1003. return error;
  1004. }
  1005. static int __init i8042_setup_kbd(void)
  1006. {
  1007. int error;
  1008. error = i8042_create_kbd_port();
  1009. if (error)
  1010. return error;
  1011. error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
  1012. "i8042", i8042_platform_device);
  1013. if (error)
  1014. goto err_free_port;
  1015. error = i8042_enable_kbd_port();
  1016. if (error)
  1017. goto err_free_irq;
  1018. i8042_kbd_irq_registered = true;
  1019. return 0;
  1020. err_free_irq:
  1021. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  1022. err_free_port:
  1023. i8042_free_kbd_port();
  1024. return error;
  1025. }
  1026. static int __init i8042_probe(struct platform_device *dev)
  1027. {
  1028. int error;
  1029. error = i8042_controller_selftest();
  1030. if (error)
  1031. return error;
  1032. error = i8042_controller_init();
  1033. if (error)
  1034. return error;
  1035. #ifdef CONFIG_X86
  1036. if (i8042_dritek)
  1037. i8042_dritek_enable();
  1038. #endif
  1039. if (!i8042_noaux) {
  1040. error = i8042_setup_aux();
  1041. if (error && error != -ENODEV && error != -EBUSY)
  1042. goto out_fail;
  1043. }
  1044. if (!i8042_nokbd) {
  1045. error = i8042_setup_kbd();
  1046. if (error)
  1047. goto out_fail;
  1048. }
  1049. /*
  1050. * Ok, everything is ready, let's register all serio ports
  1051. */
  1052. i8042_register_ports();
  1053. return 0;
  1054. out_fail:
  1055. i8042_free_aux_ports(); /* in case KBD failed but AUX not */
  1056. i8042_free_irqs();
  1057. i8042_controller_reset();
  1058. return error;
  1059. }
  1060. static int __devexit i8042_remove(struct platform_device *dev)
  1061. {
  1062. i8042_unregister_ports();
  1063. i8042_free_irqs();
  1064. i8042_controller_reset();
  1065. return 0;
  1066. }
  1067. static struct platform_driver i8042_driver = {
  1068. .driver = {
  1069. .name = "i8042",
  1070. .owner = THIS_MODULE,
  1071. #ifdef CONFIG_PM
  1072. .pm = &i8042_pm_ops,
  1073. #endif
  1074. },
  1075. .remove = __devexit_p(i8042_remove),
  1076. .shutdown = i8042_shutdown,
  1077. };
  1078. static int __init i8042_init(void)
  1079. {
  1080. int err;
  1081. dbg_init();
  1082. err = i8042_platform_init();
  1083. if (err)
  1084. return err;
  1085. err = i8042_controller_check();
  1086. if (err)
  1087. goto err_platform_exit;
  1088. i8042_platform_device = platform_device_alloc("i8042", -1);
  1089. if (!i8042_platform_device) {
  1090. err = -ENOMEM;
  1091. goto err_platform_exit;
  1092. }
  1093. err = platform_device_add(i8042_platform_device);
  1094. if (err)
  1095. goto err_free_device;
  1096. err = platform_driver_probe(&i8042_driver, i8042_probe);
  1097. if (err)
  1098. goto err_del_device;
  1099. panic_blink = i8042_panic_blink;
  1100. return 0;
  1101. err_del_device:
  1102. platform_device_del(i8042_platform_device);
  1103. err_free_device:
  1104. platform_device_put(i8042_platform_device);
  1105. err_platform_exit:
  1106. i8042_platform_exit();
  1107. return err;
  1108. }
  1109. static void __exit i8042_exit(void)
  1110. {
  1111. platform_driver_unregister(&i8042_driver);
  1112. platform_device_unregister(i8042_platform_device);
  1113. i8042_platform_exit();
  1114. panic_blink = NULL;
  1115. }
  1116. module_init(i8042_init);
  1117. module_exit(i8042_exit);