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