i8042.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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/delay.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.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 <asm/io.h>
  22. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  23. MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
  24. MODULE_LICENSE("GPL");
  25. static unsigned int i8042_nokbd;
  26. module_param_named(nokbd, i8042_nokbd, bool, 0);
  27. MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
  28. static unsigned int i8042_noaux;
  29. module_param_named(noaux, i8042_noaux, bool, 0);
  30. MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
  31. static unsigned int i8042_nomux;
  32. module_param_named(nomux, i8042_nomux, bool, 0);
  33. MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing conrtoller is present.");
  34. static unsigned int i8042_unlock;
  35. module_param_named(unlock, i8042_unlock, bool, 0);
  36. MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
  37. static unsigned int i8042_reset;
  38. module_param_named(reset, i8042_reset, bool, 0);
  39. MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
  40. static unsigned int i8042_direct;
  41. module_param_named(direct, i8042_direct, bool, 0);
  42. MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
  43. static unsigned int i8042_dumbkbd;
  44. module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
  45. MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
  46. static unsigned int i8042_noloop;
  47. module_param_named(noloop, i8042_noloop, bool, 0);
  48. MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
  49. static unsigned int i8042_blink_frequency = 500;
  50. module_param_named(panicblink, i8042_blink_frequency, uint, 0600);
  51. MODULE_PARM_DESC(panicblink, "Frequency with which keyboard LEDs should blink when kernel panics");
  52. #ifdef CONFIG_PNP
  53. static int i8042_nopnp;
  54. module_param_named(nopnp, i8042_nopnp, bool, 0);
  55. MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
  56. #endif
  57. #define DEBUG
  58. #ifdef DEBUG
  59. static int i8042_debug;
  60. module_param_named(debug, i8042_debug, bool, 0600);
  61. MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
  62. #endif
  63. __obsolete_setup("i8042_noaux");
  64. __obsolete_setup("i8042_nomux");
  65. __obsolete_setup("i8042_unlock");
  66. __obsolete_setup("i8042_reset");
  67. __obsolete_setup("i8042_direct");
  68. __obsolete_setup("i8042_dumbkbd");
  69. #include "i8042.h"
  70. static DEFINE_SPINLOCK(i8042_lock);
  71. struct i8042_port {
  72. struct serio *serio;
  73. int irq;
  74. unsigned char exists;
  75. signed char mux;
  76. };
  77. #define I8042_KBD_PORT_NO 0
  78. #define I8042_AUX_PORT_NO 1
  79. #define I8042_MUX_PORT_NO 2
  80. #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
  81. static struct i8042_port i8042_ports[I8042_NUM_PORTS];
  82. static unsigned char i8042_initial_ctr;
  83. static unsigned char i8042_ctr;
  84. static unsigned char i8042_mux_present;
  85. static unsigned char i8042_kbd_irq_registered;
  86. static unsigned char i8042_aux_irq_registered;
  87. static unsigned char i8042_suppress_kbd_ack;
  88. static struct platform_device *i8042_platform_device;
  89. static irqreturn_t i8042_interrupt(int irq, void *dev_id);
  90. /*
  91. * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
  92. * be ready for reading values from it / writing values to it.
  93. * Called always with i8042_lock held.
  94. */
  95. static int i8042_wait_read(void)
  96. {
  97. int i = 0;
  98. while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
  99. udelay(50);
  100. i++;
  101. }
  102. return -(i == I8042_CTL_TIMEOUT);
  103. }
  104. static int i8042_wait_write(void)
  105. {
  106. int i = 0;
  107. while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
  108. udelay(50);
  109. i++;
  110. }
  111. return -(i == I8042_CTL_TIMEOUT);
  112. }
  113. /*
  114. * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
  115. * of the i8042 down the toilet.
  116. */
  117. static int i8042_flush(void)
  118. {
  119. unsigned long flags;
  120. unsigned char data, str;
  121. int i = 0;
  122. spin_lock_irqsave(&i8042_lock, flags);
  123. while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
  124. udelay(50);
  125. data = i8042_read_data();
  126. i++;
  127. dbg("%02x <- i8042 (flush, %s)", data,
  128. str & I8042_STR_AUXDATA ? "aux" : "kbd");
  129. }
  130. spin_unlock_irqrestore(&i8042_lock, flags);
  131. return i;
  132. }
  133. /*
  134. * i8042_command() executes a command on the i8042. It also sends the input
  135. * parameter(s) of the commands to it, and receives the output value(s). The
  136. * parameters are to be stored in the param array, and the output is placed
  137. * into the same array. The number of the parameters and output values is
  138. * encoded in bits 8-11 of the command number.
  139. */
  140. static int __i8042_command(unsigned char *param, int command)
  141. {
  142. int i, error;
  143. if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
  144. return -1;
  145. error = i8042_wait_write();
  146. if (error)
  147. return error;
  148. dbg("%02x -> i8042 (command)", command & 0xff);
  149. i8042_write_command(command & 0xff);
  150. for (i = 0; i < ((command >> 12) & 0xf); i++) {
  151. error = i8042_wait_write();
  152. if (error)
  153. return error;
  154. dbg("%02x -> i8042 (parameter)", param[i]);
  155. i8042_write_data(param[i]);
  156. }
  157. for (i = 0; i < ((command >> 8) & 0xf); i++) {
  158. error = i8042_wait_read();
  159. if (error) {
  160. dbg(" -- i8042 (timeout)");
  161. return error;
  162. }
  163. if (command == I8042_CMD_AUX_LOOP &&
  164. !(i8042_read_status() & I8042_STR_AUXDATA)) {
  165. dbg(" -- i8042 (auxerr)");
  166. return -1;
  167. }
  168. param[i] = i8042_read_data();
  169. dbg("%02x <- i8042 (return)", param[i]);
  170. }
  171. return 0;
  172. }
  173. static int i8042_command(unsigned char *param, int command)
  174. {
  175. unsigned long flags;
  176. int retval;
  177. spin_lock_irqsave(&i8042_lock, flags);
  178. retval = __i8042_command(param, command);
  179. spin_unlock_irqrestore(&i8042_lock, flags);
  180. return retval;
  181. }
  182. /*
  183. * i8042_kbd_write() sends a byte out through the keyboard interface.
  184. */
  185. static int i8042_kbd_write(struct serio *port, unsigned char c)
  186. {
  187. unsigned long flags;
  188. int retval = 0;
  189. spin_lock_irqsave(&i8042_lock, flags);
  190. if (!(retval = i8042_wait_write())) {
  191. dbg("%02x -> i8042 (kbd-data)", c);
  192. i8042_write_data(c);
  193. }
  194. spin_unlock_irqrestore(&i8042_lock, flags);
  195. return retval;
  196. }
  197. /*
  198. * i8042_aux_write() sends a byte out through the aux interface.
  199. */
  200. static int i8042_aux_write(struct serio *serio, unsigned char c)
  201. {
  202. struct i8042_port *port = serio->port_data;
  203. return i8042_command(&c, port->mux == -1 ?
  204. I8042_CMD_AUX_SEND :
  205. I8042_CMD_MUX_SEND + port->mux);
  206. }
  207. /*
  208. * i8042_start() is called by serio core when port is about to finish
  209. * registering. It will mark port as existing so i8042_interrupt can
  210. * start sending data through it.
  211. */
  212. static int i8042_start(struct serio *serio)
  213. {
  214. struct i8042_port *port = serio->port_data;
  215. port->exists = 1;
  216. mb();
  217. return 0;
  218. }
  219. /*
  220. * i8042_stop() marks serio port as non-existing so i8042_interrupt
  221. * will not try to send data to the port that is about to go away.
  222. * The function is called by serio core as part of unregister procedure.
  223. */
  224. static void i8042_stop(struct serio *serio)
  225. {
  226. struct i8042_port *port = serio->port_data;
  227. port->exists = 0;
  228. synchronize_sched();
  229. port->serio = NULL;
  230. }
  231. /*
  232. * i8042_interrupt() is the most important function in this driver -
  233. * it handles the interrupts from the i8042, and sends incoming bytes
  234. * to the upper layers.
  235. */
  236. static irqreturn_t i8042_interrupt(int irq, void *dev_id)
  237. {
  238. struct i8042_port *port;
  239. unsigned long flags;
  240. unsigned char str, data;
  241. unsigned int dfl;
  242. unsigned int port_no;
  243. int ret = 1;
  244. spin_lock_irqsave(&i8042_lock, flags);
  245. str = i8042_read_status();
  246. if (unlikely(~str & I8042_STR_OBF)) {
  247. spin_unlock_irqrestore(&i8042_lock, flags);
  248. if (irq) dbg("Interrupt %d, without any data", irq);
  249. ret = 0;
  250. goto out;
  251. }
  252. data = i8042_read_data();
  253. spin_unlock_irqrestore(&i8042_lock, flags);
  254. if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
  255. static unsigned long last_transmit;
  256. static unsigned char last_str;
  257. dfl = 0;
  258. if (str & I8042_STR_MUXERR) {
  259. dbg("MUX error, status is %02x, data is %02x", str, data);
  260. /*
  261. * When MUXERR condition is signalled the data register can only contain
  262. * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
  263. * it is not always the case. Some KBCs also report 0xfc when there is
  264. * nothing connected to the port while others sometimes get confused which
  265. * port the data came from and signal error leaving the data intact. They
  266. * _do not_ revert to legacy mode (actually I've never seen KBC reverting
  267. * to legacy mode yet, when we see one we'll add proper handling).
  268. * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
  269. * rest assume that the data came from the same serio last byte
  270. * was transmitted (if transmission happened not too long ago).
  271. */
  272. switch (data) {
  273. default:
  274. if (time_before(jiffies, last_transmit + HZ/10)) {
  275. str = last_str;
  276. break;
  277. }
  278. /* fall through - report timeout */
  279. case 0xfc:
  280. case 0xfd:
  281. case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
  282. case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
  283. }
  284. }
  285. port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
  286. last_str = str;
  287. last_transmit = jiffies;
  288. } else {
  289. dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
  290. ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
  291. port_no = (str & I8042_STR_AUXDATA) ?
  292. I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
  293. }
  294. port = &i8042_ports[port_no];
  295. dbg("%02x <- i8042 (interrupt, %d, %d%s%s)",
  296. data, port_no, irq,
  297. dfl & SERIO_PARITY ? ", bad parity" : "",
  298. dfl & SERIO_TIMEOUT ? ", timeout" : "");
  299. if (unlikely(i8042_suppress_kbd_ack))
  300. if (port_no == I8042_KBD_PORT_NO &&
  301. (data == 0xfa || data == 0xfe)) {
  302. i8042_suppress_kbd_ack--;
  303. goto out;
  304. }
  305. if (likely(port->exists))
  306. serio_interrupt(port->serio, data, dfl);
  307. out:
  308. return IRQ_RETVAL(ret);
  309. }
  310. /*
  311. * i8042_enable_kbd_port enables keybaord port on chip
  312. */
  313. static int i8042_enable_kbd_port(void)
  314. {
  315. i8042_ctr &= ~I8042_CTR_KBDDIS;
  316. i8042_ctr |= I8042_CTR_KBDINT;
  317. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  318. printk(KERN_ERR "i8042.c: Failed to enable KBD port.\n");
  319. return -EIO;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * i8042_enable_aux_port enables AUX (mouse) port on chip
  325. */
  326. static int i8042_enable_aux_port(void)
  327. {
  328. i8042_ctr &= ~I8042_CTR_AUXDIS;
  329. i8042_ctr |= I8042_CTR_AUXINT;
  330. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  331. printk(KERN_ERR "i8042.c: Failed to enable AUX port.\n");
  332. return -EIO;
  333. }
  334. return 0;
  335. }
  336. /*
  337. * i8042_enable_mux_ports enables 4 individual AUX ports after
  338. * the controller has been switched into Multiplexed mode
  339. */
  340. static int i8042_enable_mux_ports(void)
  341. {
  342. unsigned char param;
  343. int i;
  344. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  345. i8042_command(&param, I8042_CMD_MUX_PFX + i);
  346. i8042_command(&param, I8042_CMD_AUX_ENABLE);
  347. }
  348. return i8042_enable_aux_port();
  349. }
  350. /*
  351. * i8042_set_mux_mode checks whether the controller has an active
  352. * multiplexor and puts the chip into Multiplexed (1) or Legacy (0) mode.
  353. */
  354. static int i8042_set_mux_mode(unsigned int mode, unsigned char *mux_version)
  355. {
  356. unsigned char param;
  357. /*
  358. * Get rid of bytes in the queue.
  359. */
  360. i8042_flush();
  361. /*
  362. * Internal loopback test - send three bytes, they should come back from the
  363. * mouse interface, the last should be version.
  364. */
  365. param = 0xf0;
  366. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xf0)
  367. return -1;
  368. param = mode ? 0x56 : 0xf6;
  369. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != (mode ? 0x56 : 0xf6))
  370. return -1;
  371. param = mode ? 0xa4 : 0xa5;
  372. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == (mode ? 0xa4 : 0xa5))
  373. return -1;
  374. if (mux_version)
  375. *mux_version = param;
  376. return 0;
  377. }
  378. /*
  379. * i8042_check_mux() checks whether the controller supports the PS/2 Active
  380. * Multiplexing specification by Synaptics, Phoenix, Insyde and
  381. * LCS/Telegraphics.
  382. */
  383. static int __devinit i8042_check_mux(void)
  384. {
  385. unsigned char mux_version;
  386. if (i8042_set_mux_mode(1, &mux_version))
  387. return -1;
  388. /*
  389. * Workaround for interference with USB Legacy emulation
  390. * that causes a v10.12 MUX to be found.
  391. */
  392. if (mux_version == 0xAC)
  393. return -1;
  394. printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
  395. (mux_version >> 4) & 0xf, mux_version & 0xf);
  396. /*
  397. * Disable all muxed ports by disabling AUX.
  398. */
  399. i8042_ctr |= I8042_CTR_AUXDIS;
  400. i8042_ctr &= ~I8042_CTR_AUXINT;
  401. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  402. printk(KERN_ERR "i8042.c: Failed to disable AUX port, can't use MUX.\n");
  403. return -EIO;
  404. }
  405. i8042_mux_present = 1;
  406. return 0;
  407. }
  408. /*
  409. * The following is used to test AUX IRQ delivery.
  410. */
  411. static struct completion i8042_aux_irq_delivered __devinitdata;
  412. static int i8042_irq_being_tested __devinitdata;
  413. static irqreturn_t __devinit i8042_aux_test_irq(int irq, void *dev_id)
  414. {
  415. unsigned long flags;
  416. unsigned char str, data;
  417. spin_lock_irqsave(&i8042_lock, flags);
  418. str = i8042_read_status();
  419. if (str & I8042_STR_OBF) {
  420. data = i8042_read_data();
  421. if (i8042_irq_being_tested &&
  422. data == 0xa5 && (str & I8042_STR_AUXDATA))
  423. complete(&i8042_aux_irq_delivered);
  424. }
  425. spin_unlock_irqrestore(&i8042_lock, flags);
  426. return IRQ_HANDLED;
  427. }
  428. /*
  429. * i8042_check_aux() applies as much paranoia as it can at detecting
  430. * the presence of an AUX interface.
  431. */
  432. static int __devinit i8042_check_aux(void)
  433. {
  434. int retval = -1;
  435. int irq_registered = 0;
  436. int aux_loop_broken = 0;
  437. unsigned long flags;
  438. unsigned char param;
  439. /*
  440. * Get rid of bytes in the queue.
  441. */
  442. i8042_flush();
  443. /*
  444. * Internal loopback test - filters out AT-type i8042's. Unfortunately
  445. * SiS screwed up and their 5597 doesn't support the LOOP command even
  446. * though it has an AUX port.
  447. */
  448. param = 0x5a;
  449. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0x5a) {
  450. /*
  451. * External connection test - filters out AT-soldered PS/2 i8042's
  452. * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
  453. * 0xfa - no error on some notebooks which ignore the spec
  454. * Because it's common for chipsets to return error on perfectly functioning
  455. * AUX ports, we test for this only when the LOOP command failed.
  456. */
  457. if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
  458. (param && param != 0xfa && param != 0xff))
  459. return -1;
  460. aux_loop_broken = 1;
  461. }
  462. /*
  463. * Bit assignment test - filters out PS/2 i8042's in AT mode
  464. */
  465. if (i8042_command(&param, I8042_CMD_AUX_DISABLE))
  466. return -1;
  467. if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (~param & I8042_CTR_AUXDIS)) {
  468. printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
  469. printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
  470. }
  471. if (i8042_command(&param, I8042_CMD_AUX_ENABLE))
  472. return -1;
  473. if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (param & I8042_CTR_AUXDIS))
  474. return -1;
  475. /*
  476. * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
  477. * used it for a PCI card or somethig else.
  478. */
  479. if (i8042_noloop || aux_loop_broken) {
  480. /*
  481. * Without LOOP command we can't test AUX IRQ delivery. Assume the port
  482. * is working and hope we are right.
  483. */
  484. retval = 0;
  485. goto out;
  486. }
  487. if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
  488. "i8042", i8042_platform_device))
  489. goto out;
  490. irq_registered = 1;
  491. if (i8042_enable_aux_port())
  492. goto out;
  493. spin_lock_irqsave(&i8042_lock, flags);
  494. init_completion(&i8042_aux_irq_delivered);
  495. i8042_irq_being_tested = 1;
  496. param = 0xa5;
  497. retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
  498. spin_unlock_irqrestore(&i8042_lock, flags);
  499. if (retval)
  500. goto out;
  501. if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
  502. msecs_to_jiffies(250)) == 0) {
  503. /*
  504. * AUX IRQ was never delivered so we need to flush the controller to
  505. * get rid of the byte we put there; otherwise keyboard may not work.
  506. */
  507. i8042_flush();
  508. retval = -1;
  509. }
  510. out:
  511. /*
  512. * Disable the interface.
  513. */
  514. i8042_ctr |= I8042_CTR_AUXDIS;
  515. i8042_ctr &= ~I8042_CTR_AUXINT;
  516. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  517. retval = -1;
  518. if (irq_registered)
  519. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  520. return retval;
  521. }
  522. static int i8042_controller_check(void)
  523. {
  524. if (i8042_flush() == I8042_BUFFER_SIZE) {
  525. printk(KERN_ERR "i8042.c: No controller found.\n");
  526. return -ENODEV;
  527. }
  528. return 0;
  529. }
  530. static int i8042_controller_selftest(void)
  531. {
  532. unsigned char param;
  533. if (!i8042_reset)
  534. return 0;
  535. if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
  536. printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
  537. return -ENODEV;
  538. }
  539. if (param != I8042_RET_CTL_TEST) {
  540. printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
  541. param, I8042_RET_CTL_TEST);
  542. return -EIO;
  543. }
  544. return 0;
  545. }
  546. /*
  547. * i8042_controller init initializes the i8042 controller, and,
  548. * most importantly, sets it into non-xlated mode if that's
  549. * desired.
  550. */
  551. static int i8042_controller_init(void)
  552. {
  553. unsigned long flags;
  554. /*
  555. * Save the CTR for restoral on unload / reboot.
  556. */
  557. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
  558. printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
  559. return -EIO;
  560. }
  561. i8042_initial_ctr = i8042_ctr;
  562. /*
  563. * Disable the keyboard interface and interrupt.
  564. */
  565. i8042_ctr |= I8042_CTR_KBDDIS;
  566. i8042_ctr &= ~I8042_CTR_KBDINT;
  567. /*
  568. * Handle keylock.
  569. */
  570. spin_lock_irqsave(&i8042_lock, flags);
  571. if (~i8042_read_status() & I8042_STR_KEYLOCK) {
  572. if (i8042_unlock)
  573. i8042_ctr |= I8042_CTR_IGNKEYLOCK;
  574. else
  575. printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
  576. }
  577. spin_unlock_irqrestore(&i8042_lock, flags);
  578. /*
  579. * If the chip is configured into nontranslated mode by the BIOS, don't
  580. * bother enabling translating and be happy.
  581. */
  582. if (~i8042_ctr & I8042_CTR_XLATE)
  583. i8042_direct = 1;
  584. /*
  585. * Set nontranslated mode for the kbd interface if requested by an option.
  586. * After this the kbd interface becomes a simple serial in/out, like the aux
  587. * interface is. We don't do this by default, since it can confuse notebook
  588. * BIOSes.
  589. */
  590. if (i8042_direct)
  591. i8042_ctr &= ~I8042_CTR_XLATE;
  592. /*
  593. * Write CTR back.
  594. */
  595. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  596. printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
  597. return -EIO;
  598. }
  599. return 0;
  600. }
  601. /*
  602. * Reset the controller and reset CRT to the original value set by BIOS.
  603. */
  604. static void i8042_controller_reset(void)
  605. {
  606. i8042_flush();
  607. /*
  608. * Disable MUX mode if present.
  609. */
  610. if (i8042_mux_present)
  611. i8042_set_mux_mode(0, NULL);
  612. /*
  613. * Reset the controller if requested.
  614. */
  615. i8042_controller_selftest();
  616. /*
  617. * Restore the original control register setting.
  618. */
  619. if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
  620. printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
  621. }
  622. /*
  623. * i8042_panic_blink() will flash the keyboard LEDs and is called when
  624. * kernel panics. Flashing LEDs is useful for users running X who may
  625. * not see the console and will help distingushing panics from "real"
  626. * lockups.
  627. *
  628. * Note that DELAY has a limit of 10ms so we will not get stuck here
  629. * waiting for KBC to free up even if KBD interrupt is off
  630. */
  631. #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
  632. static long i8042_panic_blink(long count)
  633. {
  634. long delay = 0;
  635. static long last_blink;
  636. static char led;
  637. /*
  638. * We expect frequency to be about 1/2s. KDB uses about 1s.
  639. * Make sure they are different.
  640. */
  641. if (!i8042_blink_frequency)
  642. return 0;
  643. if (count - last_blink < i8042_blink_frequency)
  644. return 0;
  645. led ^= 0x01 | 0x04;
  646. while (i8042_read_status() & I8042_STR_IBF)
  647. DELAY;
  648. dbg("%02x -> i8042 (panic blink)", 0xed);
  649. i8042_suppress_kbd_ack = 2;
  650. i8042_write_data(0xed); /* set leds */
  651. DELAY;
  652. while (i8042_read_status() & I8042_STR_IBF)
  653. DELAY;
  654. DELAY;
  655. dbg("%02x -> i8042 (panic blink)", led);
  656. i8042_write_data(led);
  657. DELAY;
  658. last_blink = count;
  659. return delay;
  660. }
  661. #undef DELAY
  662. #ifdef CONFIG_PM
  663. /*
  664. * Here we try to restore the original BIOS settings. We only want to
  665. * do that once, when we really suspend, not when we taking memory
  666. * snapshot for swsusp (in this case we'll perform required cleanup
  667. * as part of shutdown process).
  668. */
  669. static int i8042_suspend(struct platform_device *dev, pm_message_t state)
  670. {
  671. if (dev->dev.power.power_state.event != state.event) {
  672. if (state.event == PM_EVENT_SUSPEND)
  673. i8042_controller_reset();
  674. dev->dev.power.power_state = state;
  675. }
  676. return 0;
  677. }
  678. /*
  679. * Here we try to reset everything back to a state in which suspended
  680. */
  681. static int i8042_resume(struct platform_device *dev)
  682. {
  683. int error;
  684. /*
  685. * Do not bother with restoring state if we haven't suspened yet
  686. */
  687. if (dev->dev.power.power_state.event == PM_EVENT_ON)
  688. return 0;
  689. error = i8042_controller_check();
  690. if (error)
  691. return error;
  692. error = i8042_controller_selftest();
  693. if (error)
  694. return error;
  695. /*
  696. * Restore original CTR value and disable all ports
  697. */
  698. i8042_ctr = i8042_initial_ctr;
  699. if (i8042_direct)
  700. i8042_ctr &= ~I8042_CTR_XLATE;
  701. i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
  702. i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
  703. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  704. printk(KERN_ERR "i8042: Can't write CTR to resume\n");
  705. return -EIO;
  706. }
  707. if (i8042_mux_present) {
  708. if (i8042_set_mux_mode(1, NULL) || i8042_enable_mux_ports())
  709. printk(KERN_WARNING
  710. "i8042: failed to resume active multiplexor, "
  711. "mouse won't work.\n");
  712. } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
  713. i8042_enable_aux_port();
  714. if (i8042_ports[I8042_KBD_PORT_NO].serio)
  715. i8042_enable_kbd_port();
  716. i8042_interrupt(0, NULL);
  717. dev->dev.power.power_state = PMSG_ON;
  718. return 0;
  719. }
  720. #endif /* CONFIG_PM */
  721. /*
  722. * We need to reset the 8042 back to original mode on system shutdown,
  723. * because otherwise BIOSes will be confused.
  724. */
  725. static void i8042_shutdown(struct platform_device *dev)
  726. {
  727. i8042_controller_reset();
  728. }
  729. static int __devinit i8042_create_kbd_port(void)
  730. {
  731. struct serio *serio;
  732. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  733. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  734. if (!serio)
  735. return -ENOMEM;
  736. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  737. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  738. serio->start = i8042_start;
  739. serio->stop = i8042_stop;
  740. serio->port_data = port;
  741. serio->dev.parent = &i8042_platform_device->dev;
  742. strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
  743. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  744. port->serio = serio;
  745. port->irq = I8042_KBD_IRQ;
  746. return 0;
  747. }
  748. static int __devinit i8042_create_aux_port(int idx)
  749. {
  750. struct serio *serio;
  751. int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
  752. struct i8042_port *port = &i8042_ports[port_no];
  753. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  754. if (!serio)
  755. return -ENOMEM;
  756. serio->id.type = SERIO_8042;
  757. serio->write = i8042_aux_write;
  758. serio->start = i8042_start;
  759. serio->stop = i8042_stop;
  760. serio->port_data = port;
  761. serio->dev.parent = &i8042_platform_device->dev;
  762. if (idx < 0) {
  763. strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
  764. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  765. } else {
  766. snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
  767. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
  768. }
  769. port->serio = serio;
  770. port->mux = idx;
  771. port->irq = I8042_AUX_IRQ;
  772. return 0;
  773. }
  774. static void __devinit i8042_free_kbd_port(void)
  775. {
  776. kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
  777. i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
  778. }
  779. static void __devinit i8042_free_aux_ports(void)
  780. {
  781. int i;
  782. for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
  783. kfree(i8042_ports[i].serio);
  784. i8042_ports[i].serio = NULL;
  785. }
  786. }
  787. static void __devinit i8042_register_ports(void)
  788. {
  789. int i;
  790. for (i = 0; i < I8042_NUM_PORTS; i++) {
  791. if (i8042_ports[i].serio) {
  792. printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
  793. i8042_ports[i].serio->name,
  794. (unsigned long) I8042_DATA_REG,
  795. (unsigned long) I8042_COMMAND_REG,
  796. i8042_ports[i].irq);
  797. serio_register_port(i8042_ports[i].serio);
  798. }
  799. }
  800. }
  801. static void __devinit i8042_unregister_ports(void)
  802. {
  803. int i;
  804. for (i = 0; i < I8042_NUM_PORTS; i++) {
  805. if (i8042_ports[i].serio) {
  806. serio_unregister_port(i8042_ports[i].serio);
  807. i8042_ports[i].serio = NULL;
  808. }
  809. }
  810. }
  811. static void i8042_free_irqs(void)
  812. {
  813. if (i8042_aux_irq_registered)
  814. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  815. if (i8042_kbd_irq_registered)
  816. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  817. i8042_aux_irq_registered = i8042_kbd_irq_registered = 0;
  818. }
  819. static int __devinit i8042_setup_aux(void)
  820. {
  821. int (*aux_enable)(void);
  822. int error;
  823. int i;
  824. if (i8042_check_aux())
  825. return -ENODEV;
  826. if (i8042_nomux || i8042_check_mux()) {
  827. error = i8042_create_aux_port(-1);
  828. if (error)
  829. goto err_free_ports;
  830. aux_enable = i8042_enable_aux_port;
  831. } else {
  832. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  833. error = i8042_create_aux_port(i);
  834. if (error)
  835. goto err_free_ports;
  836. }
  837. aux_enable = i8042_enable_mux_ports;
  838. }
  839. error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
  840. "i8042", i8042_platform_device);
  841. if (error)
  842. goto err_free_ports;
  843. if (aux_enable())
  844. goto err_free_irq;
  845. i8042_aux_irq_registered = 1;
  846. return 0;
  847. err_free_irq:
  848. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  849. err_free_ports:
  850. i8042_free_aux_ports();
  851. return error;
  852. }
  853. static int __devinit i8042_setup_kbd(void)
  854. {
  855. int error;
  856. error = i8042_create_kbd_port();
  857. if (error)
  858. return error;
  859. error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
  860. "i8042", i8042_platform_device);
  861. if (error)
  862. goto err_free_port;
  863. error = i8042_enable_kbd_port();
  864. if (error)
  865. goto err_free_irq;
  866. i8042_kbd_irq_registered = 1;
  867. return 0;
  868. err_free_irq:
  869. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  870. err_free_port:
  871. i8042_free_kbd_port();
  872. return error;
  873. }
  874. static int __devinit i8042_probe(struct platform_device *dev)
  875. {
  876. int error;
  877. error = i8042_controller_selftest();
  878. if (error)
  879. return error;
  880. error = i8042_controller_init();
  881. if (error)
  882. return error;
  883. if (!i8042_noaux) {
  884. error = i8042_setup_aux();
  885. if (error && error != -ENODEV && error != -EBUSY)
  886. goto out_fail;
  887. }
  888. if (!i8042_nokbd) {
  889. error = i8042_setup_kbd();
  890. if (error)
  891. goto out_fail;
  892. }
  893. /*
  894. * Ok, everything is ready, let's register all serio ports
  895. */
  896. i8042_register_ports();
  897. return 0;
  898. out_fail:
  899. i8042_free_aux_ports(); /* in case KBD failed but AUX not */
  900. i8042_free_irqs();
  901. i8042_controller_reset();
  902. return error;
  903. }
  904. static int __devexit i8042_remove(struct platform_device *dev)
  905. {
  906. i8042_unregister_ports();
  907. i8042_free_irqs();
  908. i8042_controller_reset();
  909. return 0;
  910. }
  911. static struct platform_driver i8042_driver = {
  912. .driver = {
  913. .name = "i8042",
  914. .owner = THIS_MODULE,
  915. },
  916. .probe = i8042_probe,
  917. .remove = __devexit_p(i8042_remove),
  918. .shutdown = i8042_shutdown,
  919. #ifdef CONFIG_PM
  920. .suspend = i8042_suspend,
  921. .resume = i8042_resume,
  922. #endif
  923. };
  924. static int __init i8042_init(void)
  925. {
  926. int err;
  927. dbg_init();
  928. err = i8042_platform_init();
  929. if (err)
  930. return err;
  931. err = i8042_controller_check();
  932. if (err)
  933. goto err_platform_exit;
  934. err = platform_driver_register(&i8042_driver);
  935. if (err)
  936. goto err_platform_exit;
  937. i8042_platform_device = platform_device_alloc("i8042", -1);
  938. if (!i8042_platform_device) {
  939. err = -ENOMEM;
  940. goto err_unregister_driver;
  941. }
  942. err = platform_device_add(i8042_platform_device);
  943. if (err)
  944. goto err_free_device;
  945. panic_blink = i8042_panic_blink;
  946. return 0;
  947. err_free_device:
  948. platform_device_put(i8042_platform_device);
  949. err_unregister_driver:
  950. platform_driver_unregister(&i8042_driver);
  951. err_platform_exit:
  952. i8042_platform_exit();
  953. return err;
  954. }
  955. static void __exit i8042_exit(void)
  956. {
  957. platform_device_unregister(i8042_platform_device);
  958. platform_driver_unregister(&i8042_driver);
  959. i8042_platform_exit();
  960. panic_blink = NULL;
  961. }
  962. module_init(i8042_init);
  963. module_exit(i8042_exit);