i8042.c 30 KB

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