i8042.c 28 KB

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