i8042.c 27 KB

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