i8042.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. if (!i8042_reset)
  570. return 0;
  571. if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
  572. printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
  573. return -ENODEV;
  574. }
  575. if (param != I8042_RET_CTL_TEST) {
  576. printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
  577. param, I8042_RET_CTL_TEST);
  578. return -EIO;
  579. }
  580. return 0;
  581. }
  582. /*
  583. * i8042_controller init initializes the i8042 controller, and,
  584. * most importantly, sets it into non-xlated mode if that's
  585. * desired.
  586. */
  587. static int i8042_controller_init(void)
  588. {
  589. unsigned long flags;
  590. /*
  591. * Save the CTR for restoral on unload / reboot.
  592. */
  593. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
  594. printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
  595. return -EIO;
  596. }
  597. i8042_initial_ctr = i8042_ctr;
  598. /*
  599. * Disable the keyboard interface and interrupt.
  600. */
  601. i8042_ctr |= I8042_CTR_KBDDIS;
  602. i8042_ctr &= ~I8042_CTR_KBDINT;
  603. /*
  604. * Handle keylock.
  605. */
  606. spin_lock_irqsave(&i8042_lock, flags);
  607. if (~i8042_read_status() & I8042_STR_KEYLOCK) {
  608. if (i8042_unlock)
  609. i8042_ctr |= I8042_CTR_IGNKEYLOCK;
  610. else
  611. printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
  612. }
  613. spin_unlock_irqrestore(&i8042_lock, flags);
  614. /*
  615. * If the chip is configured into nontranslated mode by the BIOS, don't
  616. * bother enabling translating and be happy.
  617. */
  618. if (~i8042_ctr & I8042_CTR_XLATE)
  619. i8042_direct = 1;
  620. /*
  621. * Set nontranslated mode for the kbd interface if requested by an option.
  622. * After this the kbd interface becomes a simple serial in/out, like the aux
  623. * interface is. We don't do this by default, since it can confuse notebook
  624. * BIOSes.
  625. */
  626. if (i8042_direct)
  627. i8042_ctr &= ~I8042_CTR_XLATE;
  628. /*
  629. * Write CTR back.
  630. */
  631. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  632. printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
  633. return -EIO;
  634. }
  635. return 0;
  636. }
  637. /*
  638. * Reset the controller and reset CRT to the original value set by BIOS.
  639. */
  640. static void i8042_controller_reset(void)
  641. {
  642. i8042_flush();
  643. /*
  644. * Disable both KBD and AUX interfaces so they don't get in the way
  645. */
  646. i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
  647. i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
  648. /*
  649. * Disable MUX mode if present.
  650. */
  651. if (i8042_mux_present)
  652. i8042_set_mux_mode(0, NULL);
  653. /*
  654. * Reset the controller if requested.
  655. */
  656. i8042_controller_selftest();
  657. /*
  658. * Restore the original control register setting.
  659. */
  660. if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
  661. printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
  662. }
  663. /*
  664. * i8042_panic_blink() will flash the keyboard LEDs and is called when
  665. * kernel panics. Flashing LEDs is useful for users running X who may
  666. * not see the console and will help distingushing panics from "real"
  667. * lockups.
  668. *
  669. * Note that DELAY has a limit of 10ms so we will not get stuck here
  670. * waiting for KBC to free up even if KBD interrupt is off
  671. */
  672. #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
  673. static long i8042_panic_blink(long count)
  674. {
  675. long delay = 0;
  676. static long last_blink;
  677. static char led;
  678. /*
  679. * We expect frequency to be about 1/2s. KDB uses about 1s.
  680. * Make sure they are different.
  681. */
  682. if (!i8042_blink_frequency)
  683. return 0;
  684. if (count - last_blink < i8042_blink_frequency)
  685. return 0;
  686. led ^= 0x01 | 0x04;
  687. while (i8042_read_status() & I8042_STR_IBF)
  688. DELAY;
  689. dbg("%02x -> i8042 (panic blink)", 0xed);
  690. i8042_suppress_kbd_ack = 2;
  691. i8042_write_data(0xed); /* set leds */
  692. DELAY;
  693. while (i8042_read_status() & I8042_STR_IBF)
  694. DELAY;
  695. DELAY;
  696. dbg("%02x -> i8042 (panic blink)", led);
  697. i8042_write_data(led);
  698. DELAY;
  699. last_blink = count;
  700. return delay;
  701. }
  702. #undef DELAY
  703. #ifdef CONFIG_X86
  704. static void i8042_dritek_enable(void)
  705. {
  706. char param = 0x90;
  707. int error;
  708. error = i8042_command(&param, 0x1059);
  709. if (error)
  710. printk(KERN_WARNING
  711. "Failed to enable DRITEK extension: %d\n",
  712. error);
  713. }
  714. #endif
  715. #ifdef CONFIG_PM
  716. /*
  717. * Here we try to restore the original BIOS settings. We only want to
  718. * do that once, when we really suspend, not when we taking memory
  719. * snapshot for swsusp (in this case we'll perform required cleanup
  720. * as part of shutdown process).
  721. */
  722. static int i8042_suspend(struct platform_device *dev, pm_message_t state)
  723. {
  724. if (dev->dev.power.power_state.event != state.event) {
  725. if (state.event == PM_EVENT_SUSPEND)
  726. i8042_controller_reset();
  727. dev->dev.power.power_state = state;
  728. }
  729. return 0;
  730. }
  731. /*
  732. * Here we try to reset everything back to a state in which suspended
  733. */
  734. static int i8042_resume(struct platform_device *dev)
  735. {
  736. int error;
  737. /*
  738. * Do not bother with restoring state if we haven't suspened yet
  739. */
  740. if (dev->dev.power.power_state.event == PM_EVENT_ON)
  741. return 0;
  742. error = i8042_controller_check();
  743. if (error)
  744. return error;
  745. error = i8042_controller_selftest();
  746. if (error)
  747. return error;
  748. /*
  749. * Restore original CTR value and disable all ports
  750. */
  751. i8042_ctr = i8042_initial_ctr;
  752. if (i8042_direct)
  753. i8042_ctr &= ~I8042_CTR_XLATE;
  754. i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
  755. i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
  756. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  757. printk(KERN_ERR "i8042: Can't write CTR to resume\n");
  758. return -EIO;
  759. }
  760. #ifdef CONFIG_X86
  761. if (i8042_dritek)
  762. i8042_dritek_enable();
  763. #endif
  764. if (i8042_mux_present) {
  765. if (i8042_set_mux_mode(1, NULL) || i8042_enable_mux_ports())
  766. printk(KERN_WARNING
  767. "i8042: failed to resume active multiplexor, "
  768. "mouse won't work.\n");
  769. } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
  770. i8042_enable_aux_port();
  771. if (i8042_ports[I8042_KBD_PORT_NO].serio)
  772. i8042_enable_kbd_port();
  773. i8042_interrupt(0, NULL);
  774. dev->dev.power.power_state = PMSG_ON;
  775. return 0;
  776. }
  777. #endif /* CONFIG_PM */
  778. /*
  779. * We need to reset the 8042 back to original mode on system shutdown,
  780. * because otherwise BIOSes will be confused.
  781. */
  782. static void i8042_shutdown(struct platform_device *dev)
  783. {
  784. i8042_controller_reset();
  785. }
  786. static int __devinit i8042_create_kbd_port(void)
  787. {
  788. struct serio *serio;
  789. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  790. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  791. if (!serio)
  792. return -ENOMEM;
  793. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  794. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  795. serio->start = i8042_start;
  796. serio->stop = i8042_stop;
  797. serio->port_data = port;
  798. serio->dev.parent = &i8042_platform_device->dev;
  799. strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
  800. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  801. port->serio = serio;
  802. port->irq = I8042_KBD_IRQ;
  803. return 0;
  804. }
  805. static int __devinit i8042_create_aux_port(int idx)
  806. {
  807. struct serio *serio;
  808. int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
  809. struct i8042_port *port = &i8042_ports[port_no];
  810. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  811. if (!serio)
  812. return -ENOMEM;
  813. serio->id.type = SERIO_8042;
  814. serio->write = i8042_aux_write;
  815. serio->start = i8042_start;
  816. serio->stop = i8042_stop;
  817. serio->port_data = port;
  818. serio->dev.parent = &i8042_platform_device->dev;
  819. if (idx < 0) {
  820. strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
  821. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  822. } else {
  823. snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
  824. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
  825. }
  826. port->serio = serio;
  827. port->mux = idx;
  828. port->irq = I8042_AUX_IRQ;
  829. return 0;
  830. }
  831. static void __devinit i8042_free_kbd_port(void)
  832. {
  833. kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
  834. i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
  835. }
  836. static void __devinit i8042_free_aux_ports(void)
  837. {
  838. int i;
  839. for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
  840. kfree(i8042_ports[i].serio);
  841. i8042_ports[i].serio = NULL;
  842. }
  843. }
  844. static void __devinit i8042_register_ports(void)
  845. {
  846. int i;
  847. for (i = 0; i < I8042_NUM_PORTS; i++) {
  848. if (i8042_ports[i].serio) {
  849. printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
  850. i8042_ports[i].serio->name,
  851. (unsigned long) I8042_DATA_REG,
  852. (unsigned long) I8042_COMMAND_REG,
  853. i8042_ports[i].irq);
  854. serio_register_port(i8042_ports[i].serio);
  855. }
  856. }
  857. }
  858. static void __devexit i8042_unregister_ports(void)
  859. {
  860. int i;
  861. for (i = 0; i < I8042_NUM_PORTS; i++) {
  862. if (i8042_ports[i].serio) {
  863. serio_unregister_port(i8042_ports[i].serio);
  864. i8042_ports[i].serio = NULL;
  865. }
  866. }
  867. }
  868. static void i8042_free_irqs(void)
  869. {
  870. if (i8042_aux_irq_registered)
  871. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  872. if (i8042_kbd_irq_registered)
  873. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  874. i8042_aux_irq_registered = i8042_kbd_irq_registered = 0;
  875. }
  876. static int __devinit i8042_setup_aux(void)
  877. {
  878. int (*aux_enable)(void);
  879. int error;
  880. int i;
  881. if (i8042_check_aux())
  882. return -ENODEV;
  883. if (i8042_nomux || i8042_check_mux()) {
  884. error = i8042_create_aux_port(-1);
  885. if (error)
  886. goto err_free_ports;
  887. aux_enable = i8042_enable_aux_port;
  888. } else {
  889. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  890. error = i8042_create_aux_port(i);
  891. if (error)
  892. goto err_free_ports;
  893. }
  894. aux_enable = i8042_enable_mux_ports;
  895. }
  896. error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
  897. "i8042", i8042_platform_device);
  898. if (error)
  899. goto err_free_ports;
  900. if (aux_enable())
  901. goto err_free_irq;
  902. i8042_aux_irq_registered = 1;
  903. return 0;
  904. err_free_irq:
  905. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  906. err_free_ports:
  907. i8042_free_aux_ports();
  908. return error;
  909. }
  910. static int __devinit i8042_setup_kbd(void)
  911. {
  912. int error;
  913. error = i8042_create_kbd_port();
  914. if (error)
  915. return error;
  916. error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
  917. "i8042", i8042_platform_device);
  918. if (error)
  919. goto err_free_port;
  920. error = i8042_enable_kbd_port();
  921. if (error)
  922. goto err_free_irq;
  923. i8042_kbd_irq_registered = 1;
  924. return 0;
  925. err_free_irq:
  926. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  927. err_free_port:
  928. i8042_free_kbd_port();
  929. return error;
  930. }
  931. static int __devinit i8042_probe(struct platform_device *dev)
  932. {
  933. int error;
  934. error = i8042_controller_selftest();
  935. if (error)
  936. return error;
  937. error = i8042_controller_init();
  938. if (error)
  939. return error;
  940. #ifdef CONFIG_X86
  941. if (i8042_dritek)
  942. i8042_dritek_enable();
  943. #endif
  944. if (!i8042_noaux) {
  945. error = i8042_setup_aux();
  946. if (error && error != -ENODEV && error != -EBUSY)
  947. goto out_fail;
  948. }
  949. if (!i8042_nokbd) {
  950. error = i8042_setup_kbd();
  951. if (error)
  952. goto out_fail;
  953. }
  954. /*
  955. * Ok, everything is ready, let's register all serio ports
  956. */
  957. i8042_register_ports();
  958. return 0;
  959. out_fail:
  960. i8042_free_aux_ports(); /* in case KBD failed but AUX not */
  961. i8042_free_irqs();
  962. i8042_controller_reset();
  963. return error;
  964. }
  965. static int __devexit i8042_remove(struct platform_device *dev)
  966. {
  967. i8042_unregister_ports();
  968. i8042_free_irqs();
  969. i8042_controller_reset();
  970. return 0;
  971. }
  972. static struct platform_driver i8042_driver = {
  973. .driver = {
  974. .name = "i8042",
  975. .owner = THIS_MODULE,
  976. },
  977. .probe = i8042_probe,
  978. .remove = __devexit_p(i8042_remove),
  979. .shutdown = i8042_shutdown,
  980. #ifdef CONFIG_PM
  981. .suspend = i8042_suspend,
  982. .resume = i8042_resume,
  983. #endif
  984. };
  985. static int __init i8042_init(void)
  986. {
  987. int err;
  988. dbg_init();
  989. err = i8042_platform_init();
  990. if (err)
  991. return err;
  992. err = i8042_controller_check();
  993. if (err)
  994. goto err_platform_exit;
  995. err = platform_driver_register(&i8042_driver);
  996. if (err)
  997. goto err_platform_exit;
  998. i8042_platform_device = platform_device_alloc("i8042", -1);
  999. if (!i8042_platform_device) {
  1000. err = -ENOMEM;
  1001. goto err_unregister_driver;
  1002. }
  1003. err = platform_device_add(i8042_platform_device);
  1004. if (err)
  1005. goto err_free_device;
  1006. panic_blink = i8042_panic_blink;
  1007. return 0;
  1008. err_free_device:
  1009. platform_device_put(i8042_platform_device);
  1010. err_unregister_driver:
  1011. platform_driver_unregister(&i8042_driver);
  1012. err_platform_exit:
  1013. i8042_platform_exit();
  1014. return err;
  1015. }
  1016. static void __exit i8042_exit(void)
  1017. {
  1018. platform_device_unregister(i8042_platform_device);
  1019. platform_driver_unregister(&i8042_driver);
  1020. i8042_platform_exit();
  1021. panic_blink = NULL;
  1022. }
  1023. module_init(i8042_init);
  1024. module_exit(i8042_exit);