i8042.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
  444. if (retval || param != 0x5a) {
  445. /*
  446. * External connection test - filters out AT-soldered PS/2 i8042's
  447. * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
  448. * 0xfa - no error on some notebooks which ignore the spec
  449. * Because it's common for chipsets to return error on perfectly functioning
  450. * AUX ports, we test for this only when the LOOP command failed.
  451. */
  452. if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
  453. (param && param != 0xfa && param != 0xff))
  454. return -1;
  455. /*
  456. * If AUX_LOOP completed without error but returned unexpected data
  457. * mark it as broken
  458. */
  459. if (!retval)
  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);