i8042.c 27 KB

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