i8042.c 30 KB

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