i8042.c 30 KB

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