i8042.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. * i8042 keyboard and mouse controller driver for Linux
  3. *
  4. * Copyright (c) 1999-2004 Vojtech Pavlik
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/config.h>
  17. #include <linux/init.h>
  18. #include <linux/serio.h>
  19. #include <linux/err.h>
  20. #include <linux/rcupdate.h>
  21. #include <asm/io.h>
  22. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  23. MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
  24. MODULE_LICENSE("GPL");
  25. static unsigned int i8042_nokbd;
  26. module_param_named(nokbd, i8042_nokbd, bool, 0);
  27. MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
  28. static unsigned int i8042_noaux;
  29. module_param_named(noaux, i8042_noaux, bool, 0);
  30. MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
  31. static unsigned int i8042_nomux;
  32. module_param_named(nomux, i8042_nomux, bool, 0);
  33. MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing conrtoller is present.");
  34. static unsigned int i8042_unlock;
  35. module_param_named(unlock, i8042_unlock, bool, 0);
  36. MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
  37. static unsigned int i8042_reset;
  38. module_param_named(reset, i8042_reset, bool, 0);
  39. MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
  40. static unsigned int i8042_direct;
  41. module_param_named(direct, i8042_direct, bool, 0);
  42. MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
  43. static unsigned int i8042_dumbkbd;
  44. module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
  45. MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
  46. static unsigned int i8042_noloop;
  47. module_param_named(noloop, i8042_noloop, bool, 0);
  48. MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
  49. static unsigned int i8042_blink_frequency = 500;
  50. module_param_named(panicblink, i8042_blink_frequency, uint, 0600);
  51. MODULE_PARM_DESC(panicblink, "Frequency with which keyboard LEDs should blink when kernel panics");
  52. #ifdef CONFIG_PNP
  53. static int i8042_nopnp;
  54. module_param_named(nopnp, i8042_nopnp, bool, 0);
  55. MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
  56. #endif
  57. #define DEBUG
  58. #ifdef DEBUG
  59. static int i8042_debug;
  60. module_param_named(debug, i8042_debug, bool, 0600);
  61. MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
  62. #endif
  63. __obsolete_setup("i8042_noaux");
  64. __obsolete_setup("i8042_nomux");
  65. __obsolete_setup("i8042_unlock");
  66. __obsolete_setup("i8042_reset");
  67. __obsolete_setup("i8042_direct");
  68. __obsolete_setup("i8042_dumbkbd");
  69. #include "i8042.h"
  70. static DEFINE_SPINLOCK(i8042_lock);
  71. struct i8042_port {
  72. struct serio *serio;
  73. int irq;
  74. unsigned char disable;
  75. unsigned char irqen;
  76. unsigned char exists;
  77. signed char mux;
  78. char name[8];
  79. };
  80. #define I8042_KBD_PORT_NO 0
  81. #define I8042_AUX_PORT_NO 1
  82. #define I8042_MUX_PORT_NO 2
  83. #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
  84. static struct i8042_port i8042_ports[I8042_NUM_PORTS] = {
  85. {
  86. .disable = I8042_CTR_KBDDIS,
  87. .irqen = I8042_CTR_KBDINT,
  88. .mux = -1,
  89. .name = "KBD",
  90. },
  91. {
  92. .disable = I8042_CTR_AUXDIS,
  93. .irqen = I8042_CTR_AUXINT,
  94. .mux = -1,
  95. .name = "AUX",
  96. }
  97. };
  98. static unsigned char i8042_initial_ctr;
  99. static unsigned char i8042_ctr;
  100. static unsigned char i8042_mux_open;
  101. static unsigned char i8042_mux_present;
  102. static struct timer_list i8042_timer;
  103. static struct platform_device *i8042_platform_device;
  104. /*
  105. * Shared IRQ's require a device pointer, but this driver doesn't support
  106. * multiple devices
  107. */
  108. #define i8042_request_irq_cookie (&i8042_timer)
  109. static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  110. /*
  111. * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
  112. * be ready for reading values from it / writing values to it.
  113. * Called always with i8042_lock held.
  114. */
  115. static int i8042_wait_read(void)
  116. {
  117. int i = 0;
  118. while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
  119. udelay(50);
  120. i++;
  121. }
  122. return -(i == I8042_CTL_TIMEOUT);
  123. }
  124. static int i8042_wait_write(void)
  125. {
  126. int i = 0;
  127. while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
  128. udelay(50);
  129. i++;
  130. }
  131. return -(i == I8042_CTL_TIMEOUT);
  132. }
  133. /*
  134. * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
  135. * of the i8042 down the toilet.
  136. */
  137. static int i8042_flush(void)
  138. {
  139. unsigned long flags;
  140. unsigned char data, str;
  141. int i = 0;
  142. spin_lock_irqsave(&i8042_lock, flags);
  143. while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
  144. udelay(50);
  145. data = i8042_read_data();
  146. i++;
  147. dbg("%02x <- i8042 (flush, %s)", data,
  148. str & I8042_STR_AUXDATA ? "aux" : "kbd");
  149. }
  150. spin_unlock_irqrestore(&i8042_lock, flags);
  151. return i;
  152. }
  153. /*
  154. * i8042_command() executes a command on the i8042. It also sends the input
  155. * parameter(s) of the commands to it, and receives the output value(s). The
  156. * parameters are to be stored in the param array, and the output is placed
  157. * into the same array. The number of the parameters and output values is
  158. * encoded in bits 8-11 of the command number.
  159. */
  160. static int i8042_command(unsigned char *param, int command)
  161. {
  162. unsigned long flags;
  163. int i, retval, auxerr = 0;
  164. if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
  165. return -1;
  166. spin_lock_irqsave(&i8042_lock, flags);
  167. if ((retval = i8042_wait_write()))
  168. goto out;
  169. dbg("%02x -> i8042 (command)", command & 0xff);
  170. i8042_write_command(command & 0xff);
  171. for (i = 0; i < ((command >> 12) & 0xf); i++) {
  172. if ((retval = i8042_wait_write()))
  173. goto out;
  174. dbg("%02x -> i8042 (parameter)", param[i]);
  175. i8042_write_data(param[i]);
  176. }
  177. for (i = 0; i < ((command >> 8) & 0xf); i++) {
  178. if ((retval = i8042_wait_read()))
  179. goto out;
  180. if (command == I8042_CMD_AUX_LOOP &&
  181. !(i8042_read_status() & I8042_STR_AUXDATA)) {
  182. retval = auxerr = -1;
  183. goto out;
  184. }
  185. param[i] = i8042_read_data();
  186. dbg("%02x <- i8042 (return)", param[i]);
  187. }
  188. if (retval)
  189. dbg(" -- i8042 (%s)", auxerr ? "auxerr" : "timeout");
  190. out:
  191. spin_unlock_irqrestore(&i8042_lock, flags);
  192. return retval;
  193. }
  194. /*
  195. * i8042_kbd_write() sends a byte out through the keyboard interface.
  196. */
  197. static int i8042_kbd_write(struct serio *port, unsigned char c)
  198. {
  199. unsigned long flags;
  200. int retval = 0;
  201. spin_lock_irqsave(&i8042_lock, flags);
  202. if(!(retval = i8042_wait_write())) {
  203. dbg("%02x -> i8042 (kbd-data)", c);
  204. i8042_write_data(c);
  205. }
  206. spin_unlock_irqrestore(&i8042_lock, flags);
  207. return retval;
  208. }
  209. /*
  210. * i8042_aux_write() sends a byte out through the aux interface.
  211. */
  212. static int i8042_aux_write(struct serio *serio, unsigned char c)
  213. {
  214. struct i8042_port *port = serio->port_data;
  215. int retval;
  216. /*
  217. * Send the byte out.
  218. */
  219. if (port->mux == -1)
  220. retval = i8042_command(&c, I8042_CMD_AUX_SEND);
  221. else
  222. retval = i8042_command(&c, I8042_CMD_MUX_SEND + port->mux);
  223. /*
  224. * Make sure the interrupt happens and the character is received even
  225. * in the case the IRQ isn't wired, so that we can receive further
  226. * characters later.
  227. */
  228. i8042_interrupt(0, NULL, NULL);
  229. return retval;
  230. }
  231. /*
  232. * i8042_activate_port() enables port on a chip.
  233. */
  234. static int i8042_activate_port(struct i8042_port *port)
  235. {
  236. if (!port->serio)
  237. return -1;
  238. i8042_flush();
  239. /*
  240. * Enable port again here because it is disabled if we are
  241. * resuming (normally it is enabled already).
  242. */
  243. i8042_ctr &= ~port->disable;
  244. i8042_ctr |= port->irqen;
  245. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  246. i8042_ctr &= ~port->irqen;
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. /*
  252. * i8042_open() is called when a port is open by the higher layer.
  253. * It allocates the interrupt and calls i8042_enable_port.
  254. */
  255. static int i8042_open(struct serio *serio)
  256. {
  257. struct i8042_port *port = serio->port_data;
  258. if (port->mux != -1)
  259. if (i8042_mux_open++)
  260. return 0;
  261. if (request_irq(port->irq, i8042_interrupt,
  262. SA_SHIRQ, "i8042", i8042_request_irq_cookie)) {
  263. printk(KERN_ERR "i8042.c: Can't get irq %d for %s, unregistering the port.\n", port->irq, port->name);
  264. goto irq_fail;
  265. }
  266. if (i8042_activate_port(port)) {
  267. printk(KERN_ERR "i8042.c: Can't activate %s, unregistering the port\n", port->name);
  268. goto activate_fail;
  269. }
  270. i8042_interrupt(0, NULL, NULL);
  271. return 0;
  272. activate_fail:
  273. free_irq(port->irq, i8042_request_irq_cookie);
  274. irq_fail:
  275. serio_unregister_port_delayed(serio);
  276. return -1;
  277. }
  278. /*
  279. * i8042_close() frees the interrupt, so that it can possibly be used
  280. * by another driver. We never know - if the user doesn't have a mouse,
  281. * the BIOS could have used the AUX interrupt for PCI.
  282. */
  283. static void i8042_close(struct serio *serio)
  284. {
  285. struct i8042_port *port = serio->port_data;
  286. if (port->mux != -1)
  287. if (--i8042_mux_open)
  288. return;
  289. i8042_ctr &= ~port->irqen;
  290. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  291. printk(KERN_WARNING "i8042.c: Can't write CTR while closing %s.\n", port->name);
  292. /*
  293. * We still want to continue and free IRQ so if more data keeps coming in
  294. * kernel will just ignore the irq.
  295. */
  296. }
  297. free_irq(port->irq, i8042_request_irq_cookie);
  298. i8042_flush();
  299. }
  300. /*
  301. * i8042_start() is called by serio core when port is about to finish
  302. * registering. It will mark port as existing so i8042_interrupt can
  303. * start sending data through it.
  304. */
  305. static int i8042_start(struct serio *serio)
  306. {
  307. struct i8042_port *port = serio->port_data;
  308. port->exists = 1;
  309. mb();
  310. return 0;
  311. }
  312. /*
  313. * i8042_stop() marks serio port as non-existing so i8042_interrupt
  314. * will not try to send data to the port that is about to go away.
  315. * The function is called by serio core as part of unregister procedure.
  316. */
  317. static void i8042_stop(struct serio *serio)
  318. {
  319. struct i8042_port *port = serio->port_data;
  320. port->exists = 0;
  321. synchronize_sched();
  322. port->serio = NULL;
  323. }
  324. /*
  325. * i8042_interrupt() is the most important function in this driver -
  326. * it handles the interrupts from the i8042, and sends incoming bytes
  327. * to the upper layers.
  328. */
  329. static irqreturn_t i8042_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  330. {
  331. struct i8042_port *port;
  332. unsigned long flags;
  333. unsigned char str, data;
  334. unsigned int dfl;
  335. unsigned int port_no;
  336. int ret;
  337. mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
  338. spin_lock_irqsave(&i8042_lock, flags);
  339. str = i8042_read_status();
  340. if (unlikely(~str & I8042_STR_OBF)) {
  341. spin_unlock_irqrestore(&i8042_lock, flags);
  342. if (irq) dbg("Interrupt %d, without any data", irq);
  343. ret = 0;
  344. goto out;
  345. }
  346. data = i8042_read_data();
  347. spin_unlock_irqrestore(&i8042_lock, flags);
  348. if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
  349. static unsigned long last_transmit;
  350. static unsigned char last_str;
  351. dfl = 0;
  352. if (str & I8042_STR_MUXERR) {
  353. dbg("MUX error, status is %02x, data is %02x", str, data);
  354. switch (data) {
  355. default:
  356. /*
  357. * When MUXERR condition is signalled the data register can only contain
  358. * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
  359. * it is not always the case. Some KBC just get confused which port the
  360. * data came from and signal error leaving the data intact. They _do not_
  361. * revert to legacy mode (actually I've never seen KBC reverting to legacy
  362. * mode yet, when we see one we'll add proper handling).
  363. * Anyway, we will assume that the data came from the same serio last byte
  364. * was transmitted (if transmission happened not too long ago).
  365. */
  366. if (time_before(jiffies, last_transmit + HZ/10)) {
  367. str = last_str;
  368. break;
  369. }
  370. /* fall through - report timeout */
  371. case 0xfd:
  372. case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
  373. case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
  374. }
  375. }
  376. port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
  377. last_str = str;
  378. last_transmit = jiffies;
  379. } else {
  380. dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
  381. ((str & I8042_STR_TIMEOUT) ? SERIO_TIMEOUT : 0);
  382. port_no = (str & I8042_STR_AUXDATA) ?
  383. I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
  384. }
  385. port = &i8042_ports[port_no];
  386. dbg("%02x <- i8042 (interrupt, %s, %d%s%s)",
  387. data, port->name, irq,
  388. dfl & SERIO_PARITY ? ", bad parity" : "",
  389. dfl & SERIO_TIMEOUT ? ", timeout" : "");
  390. if (likely(port->exists))
  391. serio_interrupt(port->serio, data, dfl, regs);
  392. ret = 1;
  393. out:
  394. return IRQ_RETVAL(ret);
  395. }
  396. /*
  397. * i8042_set_mux_mode checks whether the controller has an active
  398. * multiplexor and puts the chip into Multiplexed (1) or Legacy (0) mode.
  399. */
  400. static int i8042_set_mux_mode(unsigned int mode, unsigned char *mux_version)
  401. {
  402. unsigned char param;
  403. /*
  404. * Get rid of bytes in the queue.
  405. */
  406. i8042_flush();
  407. /*
  408. * Internal loopback test - send three bytes, they should come back from the
  409. * mouse interface, the last should be version. Note that we negate mouseport
  410. * command responses for the i8042_check_aux() routine.
  411. */
  412. param = 0xf0;
  413. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0xf0)
  414. return -1;
  415. param = mode ? 0x56 : 0xf6;
  416. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != (mode ? 0x56 : 0xf6))
  417. return -1;
  418. param = mode ? 0xa4 : 0xa5;
  419. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == (mode ? 0xa4 : 0xa5))
  420. return -1;
  421. if (mux_version)
  422. *mux_version = param;
  423. return 0;
  424. }
  425. /*
  426. * i8042_enable_mux_ports enables 4 individual AUX ports after
  427. * the controller has been switched into Multiplexed mode
  428. */
  429. static int i8042_enable_mux_ports(void)
  430. {
  431. unsigned char param;
  432. int i;
  433. /*
  434. * Disable all muxed ports by disabling AUX.
  435. */
  436. i8042_ctr |= I8042_CTR_AUXDIS;
  437. i8042_ctr &= ~I8042_CTR_AUXINT;
  438. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  439. printk(KERN_ERR "i8042.c: Failed to disable AUX port, can't use MUX.\n");
  440. return -1;
  441. }
  442. /*
  443. * Enable all muxed ports.
  444. */
  445. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  446. i8042_command(&param, I8042_CMD_MUX_PFX + i);
  447. i8042_command(&param, I8042_CMD_AUX_ENABLE);
  448. }
  449. return 0;
  450. }
  451. /*
  452. * i8042_check_mux() checks whether the controller supports the PS/2 Active
  453. * Multiplexing specification by Synaptics, Phoenix, Insyde and
  454. * LCS/Telegraphics.
  455. */
  456. static int __init i8042_check_mux(void)
  457. {
  458. unsigned char mux_version;
  459. if (i8042_set_mux_mode(1, &mux_version))
  460. return -1;
  461. /* Workaround for interference with USB Legacy emulation */
  462. /* that causes a v10.12 MUX to be found. */
  463. if (mux_version == 0xAC)
  464. return -1;
  465. printk(KERN_INFO "i8042.c: Detected active multiplexing controller, rev %d.%d.\n",
  466. (mux_version >> 4) & 0xf, mux_version & 0xf);
  467. if (i8042_enable_mux_ports())
  468. return -1;
  469. i8042_mux_present = 1;
  470. return 0;
  471. }
  472. /*
  473. * i8042_check_aux() applies as much paranoia as it can at detecting
  474. * the presence of an AUX interface.
  475. */
  476. static int __init i8042_check_aux(void)
  477. {
  478. unsigned char param;
  479. static int i8042_check_aux_cookie;
  480. /*
  481. * Check if AUX irq is available. If it isn't, then there is no point
  482. * in trying to detect AUX presence.
  483. */
  484. if (request_irq(i8042_ports[I8042_AUX_PORT_NO].irq, i8042_interrupt,
  485. SA_SHIRQ, "i8042", &i8042_check_aux_cookie))
  486. return -1;
  487. free_irq(i8042_ports[I8042_AUX_PORT_NO].irq, &i8042_check_aux_cookie);
  488. /*
  489. * Get rid of bytes in the queue.
  490. */
  491. i8042_flush();
  492. /*
  493. * Internal loopback test - filters out AT-type i8042's. Unfortunately
  494. * SiS screwed up and their 5597 doesn't support the LOOP command even
  495. * though it has an AUX port.
  496. */
  497. param = 0x5a;
  498. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != 0x5a) {
  499. /*
  500. * External connection test - filters out AT-soldered PS/2 i8042's
  501. * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
  502. * 0xfa - no error on some notebooks which ignore the spec
  503. * Because it's common for chipsets to return error on perfectly functioning
  504. * AUX ports, we test for this only when the LOOP command failed.
  505. */
  506. if (i8042_command(&param, I8042_CMD_AUX_TEST)
  507. || (param && param != 0xfa && param != 0xff))
  508. return -1;
  509. }
  510. /*
  511. * Bit assignment test - filters out PS/2 i8042's in AT mode
  512. */
  513. if (i8042_command(&param, I8042_CMD_AUX_DISABLE))
  514. return -1;
  515. if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (~param & I8042_CTR_AUXDIS)) {
  516. printk(KERN_WARNING "Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
  517. printk(KERN_WARNING "If AUX port is really absent please use the 'i8042.noaux' option.\n");
  518. }
  519. if (i8042_command(&param, I8042_CMD_AUX_ENABLE))
  520. return -1;
  521. if (i8042_command(&param, I8042_CMD_CTL_RCTR) || (param & I8042_CTR_AUXDIS))
  522. return -1;
  523. /*
  524. * Disable the interface.
  525. */
  526. i8042_ctr |= I8042_CTR_AUXDIS;
  527. i8042_ctr &= ~I8042_CTR_AUXINT;
  528. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  529. return -1;
  530. return 0;
  531. }
  532. /*
  533. * i8042_port_register() marks the device as existing,
  534. * registers it, and reports to the user.
  535. */
  536. static int __init i8042_port_register(struct i8042_port *port)
  537. {
  538. i8042_ctr &= ~port->disable;
  539. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  540. printk(KERN_WARNING "i8042.c: Can't write CTR while registering.\n");
  541. kfree(port->serio);
  542. port->serio = NULL;
  543. i8042_ctr |= port->disable;
  544. return -EIO;
  545. }
  546. printk(KERN_INFO "serio: i8042 %s port at %#lx,%#lx irq %d\n",
  547. port->name,
  548. (unsigned long) I8042_DATA_REG,
  549. (unsigned long) I8042_COMMAND_REG,
  550. port->irq);
  551. serio_register_port(port->serio);
  552. return 0;
  553. }
  554. static void i8042_timer_func(unsigned long data)
  555. {
  556. i8042_interrupt(0, NULL, NULL);
  557. }
  558. static int i8042_ctl_test(void)
  559. {
  560. unsigned char param;
  561. if (!i8042_reset)
  562. return 0;
  563. if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
  564. printk(KERN_ERR "i8042.c: i8042 controller self test timeout.\n");
  565. return -1;
  566. }
  567. if (param != I8042_RET_CTL_TEST) {
  568. printk(KERN_ERR "i8042.c: i8042 controller selftest failed. (%#x != %#x)\n",
  569. param, I8042_RET_CTL_TEST);
  570. return -1;
  571. }
  572. return 0;
  573. }
  574. /*
  575. * i8042_controller init initializes the i8042 controller, and,
  576. * most importantly, sets it into non-xlated mode if that's
  577. * desired.
  578. */
  579. static int i8042_controller_init(void)
  580. {
  581. unsigned long flags;
  582. /*
  583. * Test the i8042. We need to know if it thinks it's working correctly
  584. * before doing anything else.
  585. */
  586. if (i8042_flush() == I8042_BUFFER_SIZE) {
  587. printk(KERN_ERR "i8042.c: No controller found.\n");
  588. return -1;
  589. }
  590. if (i8042_ctl_test())
  591. return -1;
  592. /*
  593. * Save the CTR for restoral on unload / reboot.
  594. */
  595. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
  596. printk(KERN_ERR "i8042.c: Can't read CTR while initializing i8042.\n");
  597. return -1;
  598. }
  599. i8042_initial_ctr = i8042_ctr;
  600. /*
  601. * Disable the keyboard interface and interrupt.
  602. */
  603. i8042_ctr |= I8042_CTR_KBDDIS;
  604. i8042_ctr &= ~I8042_CTR_KBDINT;
  605. /*
  606. * Handle keylock.
  607. */
  608. spin_lock_irqsave(&i8042_lock, flags);
  609. if (~i8042_read_status() & I8042_STR_KEYLOCK) {
  610. if (i8042_unlock)
  611. i8042_ctr |= I8042_CTR_IGNKEYLOCK;
  612. else
  613. printk(KERN_WARNING "i8042.c: Warning: Keylock active.\n");
  614. }
  615. spin_unlock_irqrestore(&i8042_lock, flags);
  616. /*
  617. * If the chip is configured into nontranslated mode by the BIOS, don't
  618. * bother enabling translating and be happy.
  619. */
  620. if (~i8042_ctr & I8042_CTR_XLATE)
  621. i8042_direct = 1;
  622. /*
  623. * Set nontranslated mode for the kbd interface if requested by an option.
  624. * After this the kbd interface becomes a simple serial in/out, like the aux
  625. * interface is. We don't do this by default, since it can confuse notebook
  626. * BIOSes.
  627. */
  628. if (i8042_direct)
  629. i8042_ctr &= ~I8042_CTR_XLATE;
  630. /*
  631. * Write CTR back.
  632. */
  633. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  634. printk(KERN_ERR "i8042.c: Can't write CTR while initializing i8042.\n");
  635. return -1;
  636. }
  637. return 0;
  638. }
  639. /*
  640. * Reset the controller.
  641. */
  642. static void i8042_controller_reset(void)
  643. {
  644. /*
  645. * Reset the controller if requested.
  646. */
  647. i8042_ctl_test();
  648. /*
  649. * Disable MUX mode if present.
  650. */
  651. if (i8042_mux_present)
  652. i8042_set_mux_mode(0, NULL);
  653. /*
  654. * Restore the original control register setting.
  655. */
  656. i8042_ctr = i8042_initial_ctr;
  657. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  658. printk(KERN_WARNING "i8042.c: Can't restore CTR.\n");
  659. }
  660. /*
  661. * Here we try to reset everything back to a state in which the BIOS will be
  662. * able to talk to the hardware when rebooting.
  663. */
  664. static void i8042_controller_cleanup(void)
  665. {
  666. int i;
  667. i8042_flush();
  668. /*
  669. * Reset anything that is connected to the ports.
  670. */
  671. for (i = 0; i < I8042_NUM_PORTS; i++)
  672. if (i8042_ports[i].exists)
  673. serio_cleanup(i8042_ports[i].serio);
  674. i8042_controller_reset();
  675. }
  676. /*
  677. * i8042_panic_blink() will flash the keyboard LEDs and is called when
  678. * kernel panics. Flashing LEDs is useful for users running X who may
  679. * not see the console and will help distingushing panics from "real"
  680. * lockups.
  681. *
  682. * Note that DELAY has a limit of 10ms so we will not get stuck here
  683. * waiting for KBC to free up even if KBD interrupt is off
  684. */
  685. #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
  686. static long i8042_panic_blink(long count)
  687. {
  688. long delay = 0;
  689. static long last_blink;
  690. static char led;
  691. /*
  692. * We expect frequency to be about 1/2s. KDB uses about 1s.
  693. * Make sure they are different.
  694. */
  695. if (!i8042_blink_frequency)
  696. return 0;
  697. if (count - last_blink < i8042_blink_frequency)
  698. return 0;
  699. led ^= 0x01 | 0x04;
  700. while (i8042_read_status() & I8042_STR_IBF)
  701. DELAY;
  702. i8042_write_data(0xed); /* set leds */
  703. DELAY;
  704. while (i8042_read_status() & I8042_STR_IBF)
  705. DELAY;
  706. DELAY;
  707. i8042_write_data(led);
  708. DELAY;
  709. last_blink = count;
  710. return delay;
  711. }
  712. #undef DELAY
  713. /*
  714. * Here we try to restore the original BIOS settings
  715. */
  716. static int i8042_suspend(struct device *dev, pm_message_t state, u32 level)
  717. {
  718. if (level == SUSPEND_DISABLE) {
  719. del_timer_sync(&i8042_timer);
  720. i8042_controller_reset();
  721. }
  722. return 0;
  723. }
  724. /*
  725. * Here we try to reset everything back to a state in which suspended
  726. */
  727. static int i8042_resume(struct device *dev, u32 level)
  728. {
  729. int i;
  730. if (level != RESUME_ENABLE)
  731. return 0;
  732. if (i8042_ctl_test())
  733. return -1;
  734. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  735. printk(KERN_ERR "i8042: Can't write CTR\n");
  736. return -1;
  737. }
  738. if (i8042_mux_present)
  739. if (i8042_set_mux_mode(1, NULL) || i8042_enable_mux_ports())
  740. printk(KERN_WARNING "i8042: failed to resume active multiplexor, mouse won't work.\n");
  741. /*
  742. * Activate all ports.
  743. */
  744. for (i = 0; i < I8042_NUM_PORTS; i++)
  745. i8042_activate_port(&i8042_ports[i]);
  746. /*
  747. * Restart timer (for polling "stuck" data)
  748. */
  749. mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
  750. panic_blink = i8042_panic_blink;
  751. return 0;
  752. }
  753. /*
  754. * We need to reset the 8042 back to original mode on system shutdown,
  755. * because otherwise BIOSes will be confused.
  756. */
  757. static void i8042_shutdown(struct device *dev)
  758. {
  759. i8042_controller_cleanup();
  760. }
  761. static struct device_driver i8042_driver = {
  762. .name = "i8042",
  763. .bus = &platform_bus_type,
  764. .suspend = i8042_suspend,
  765. .resume = i8042_resume,
  766. .shutdown = i8042_shutdown,
  767. };
  768. static int __init i8042_create_kbd_port(void)
  769. {
  770. struct serio *serio;
  771. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  772. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  773. if (!serio)
  774. return -ENOMEM;
  775. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  776. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  777. serio->open = i8042_open;
  778. serio->close = i8042_close;
  779. serio->start = i8042_start;
  780. serio->stop = i8042_stop;
  781. serio->port_data = port;
  782. serio->dev.parent = &i8042_platform_device->dev;
  783. strlcpy(serio->name, "i8042 Kbd Port", sizeof(serio->name));
  784. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  785. port->serio = serio;
  786. return i8042_port_register(port);
  787. }
  788. static int __init i8042_create_aux_port(void)
  789. {
  790. struct serio *serio;
  791. struct i8042_port *port = &i8042_ports[I8042_AUX_PORT_NO];
  792. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  793. if (!serio)
  794. return -ENOMEM;
  795. serio->id.type = SERIO_8042;
  796. serio->write = i8042_aux_write;
  797. serio->open = i8042_open;
  798. serio->close = i8042_close;
  799. serio->start = i8042_start;
  800. serio->stop = i8042_stop;
  801. serio->port_data = port;
  802. serio->dev.parent = &i8042_platform_device->dev;
  803. strlcpy(serio->name, "i8042 Aux Port", sizeof(serio->name));
  804. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  805. port->serio = serio;
  806. return i8042_port_register(port);
  807. }
  808. static int __init i8042_create_mux_port(int index)
  809. {
  810. struct serio *serio;
  811. struct i8042_port *port = &i8042_ports[I8042_MUX_PORT_NO + index];
  812. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  813. if (!serio)
  814. return -ENOMEM;
  815. serio->id.type = SERIO_8042;
  816. serio->write = i8042_aux_write;
  817. serio->open = i8042_open;
  818. serio->close = i8042_close;
  819. serio->start = i8042_start;
  820. serio->stop = i8042_stop;
  821. serio->port_data = port;
  822. serio->dev.parent = &i8042_platform_device->dev;
  823. snprintf(serio->name, sizeof(serio->name), "i8042 Aux-%d Port", index);
  824. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, index + 1);
  825. *port = i8042_ports[I8042_AUX_PORT_NO];
  826. port->exists = 0;
  827. snprintf(port->name, sizeof(port->name), "AUX%d", index);
  828. port->mux = index;
  829. port->serio = serio;
  830. return i8042_port_register(port);
  831. }
  832. static int __init i8042_init(void)
  833. {
  834. int i, have_ports = 0;
  835. int err;
  836. dbg_init();
  837. init_timer(&i8042_timer);
  838. i8042_timer.function = i8042_timer_func;
  839. err = i8042_platform_init();
  840. if (err)
  841. return err;
  842. i8042_ports[I8042_AUX_PORT_NO].irq = I8042_AUX_IRQ;
  843. i8042_ports[I8042_KBD_PORT_NO].irq = I8042_KBD_IRQ;
  844. if (i8042_controller_init()) {
  845. err = -ENODEV;
  846. goto err_platform_exit;
  847. }
  848. err = driver_register(&i8042_driver);
  849. if (err)
  850. goto err_controller_cleanup;
  851. i8042_platform_device = platform_device_register_simple("i8042", -1, NULL, 0);
  852. if (IS_ERR(i8042_platform_device)) {
  853. err = PTR_ERR(i8042_platform_device);
  854. goto err_unregister_driver;
  855. }
  856. if (!i8042_noaux && !i8042_check_aux()) {
  857. if (!i8042_nomux && !i8042_check_mux()) {
  858. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  859. err = i8042_create_mux_port(i);
  860. if (err)
  861. goto err_unregister_ports;
  862. }
  863. } else {
  864. err = i8042_create_aux_port();
  865. if (err)
  866. goto err_unregister_ports;
  867. }
  868. have_ports = 1;
  869. }
  870. if (!i8042_nokbd) {
  871. err = i8042_create_kbd_port();
  872. if (err)
  873. goto err_unregister_ports;
  874. have_ports = 1;
  875. }
  876. if (!have_ports) {
  877. err = -ENODEV;
  878. goto err_unregister_device;
  879. }
  880. mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
  881. return 0;
  882. err_unregister_ports:
  883. for (i = 0; i < I8042_NUM_PORTS; i++)
  884. if (i8042_ports[i].serio)
  885. serio_unregister_port(i8042_ports[i].serio);
  886. err_unregister_device:
  887. platform_device_unregister(i8042_platform_device);
  888. err_unregister_driver:
  889. driver_unregister(&i8042_driver);
  890. err_controller_cleanup:
  891. i8042_controller_cleanup();
  892. err_platform_exit:
  893. i8042_platform_exit();
  894. return err;
  895. }
  896. static void __exit i8042_exit(void)
  897. {
  898. int i;
  899. i8042_controller_cleanup();
  900. for (i = 0; i < I8042_NUM_PORTS; i++)
  901. if (i8042_ports[i].exists)
  902. serio_unregister_port(i8042_ports[i].serio);
  903. del_timer_sync(&i8042_timer);
  904. platform_device_unregister(i8042_platform_device);
  905. driver_unregister(&i8042_driver);
  906. i8042_platform_exit();
  907. panic_blink = NULL;
  908. }
  909. module_init(i8042_init);
  910. module_exit(i8042_exit);