i8042.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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/init.h>
  17. #include <linux/serio.h>
  18. #include <linux/err.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/platform_device.h>
  21. #include <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. IRQF_SHARED, "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 __devinit 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 __devinit 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. IRQF_SHARED, "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 __devinit 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 platform_device *dev, pm_message_t state)
  717. {
  718. del_timer_sync(&i8042_timer);
  719. i8042_controller_reset();
  720. return 0;
  721. }
  722. /*
  723. * Here we try to reset everything back to a state in which suspended
  724. */
  725. static int i8042_resume(struct platform_device *dev)
  726. {
  727. int i;
  728. if (i8042_ctl_test())
  729. return -1;
  730. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  731. printk(KERN_ERR "i8042: Can't write CTR\n");
  732. return -1;
  733. }
  734. if (i8042_mux_present)
  735. if (i8042_set_mux_mode(1, NULL) || i8042_enable_mux_ports())
  736. printk(KERN_WARNING "i8042: failed to resume active multiplexor, mouse won't work.\n");
  737. /*
  738. * Activate all ports.
  739. */
  740. for (i = 0; i < I8042_NUM_PORTS; i++)
  741. i8042_activate_port(&i8042_ports[i]);
  742. /*
  743. * Restart timer (for polling "stuck" data)
  744. */
  745. mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
  746. panic_blink = i8042_panic_blink;
  747. return 0;
  748. }
  749. /*
  750. * We need to reset the 8042 back to original mode on system shutdown,
  751. * because otherwise BIOSes will be confused.
  752. */
  753. static void i8042_shutdown(struct platform_device *dev)
  754. {
  755. i8042_controller_cleanup();
  756. }
  757. static int __devinit i8042_create_kbd_port(void)
  758. {
  759. struct serio *serio;
  760. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  761. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  762. if (!serio)
  763. return -ENOMEM;
  764. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  765. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  766. serio->open = i8042_open;
  767. serio->close = i8042_close;
  768. serio->start = i8042_start;
  769. serio->stop = i8042_stop;
  770. serio->port_data = port;
  771. serio->dev.parent = &i8042_platform_device->dev;
  772. strlcpy(serio->name, "i8042 Kbd Port", sizeof(serio->name));
  773. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  774. port->serio = serio;
  775. return i8042_port_register(port);
  776. }
  777. static int __devinit i8042_create_aux_port(void)
  778. {
  779. struct serio *serio;
  780. struct i8042_port *port = &i8042_ports[I8042_AUX_PORT_NO];
  781. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  782. if (!serio)
  783. return -ENOMEM;
  784. serio->id.type = SERIO_8042;
  785. serio->write = i8042_aux_write;
  786. serio->open = i8042_open;
  787. serio->close = i8042_close;
  788. serio->start = i8042_start;
  789. serio->stop = i8042_stop;
  790. serio->port_data = port;
  791. serio->dev.parent = &i8042_platform_device->dev;
  792. strlcpy(serio->name, "i8042 Aux Port", sizeof(serio->name));
  793. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  794. port->serio = serio;
  795. return i8042_port_register(port);
  796. }
  797. static int __devinit i8042_create_mux_port(int index)
  798. {
  799. struct serio *serio;
  800. struct i8042_port *port = &i8042_ports[I8042_MUX_PORT_NO + index];
  801. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  802. if (!serio)
  803. return -ENOMEM;
  804. serio->id.type = SERIO_8042;
  805. serio->write = i8042_aux_write;
  806. serio->open = i8042_open;
  807. serio->close = i8042_close;
  808. serio->start = i8042_start;
  809. serio->stop = i8042_stop;
  810. serio->port_data = port;
  811. serio->dev.parent = &i8042_platform_device->dev;
  812. snprintf(serio->name, sizeof(serio->name), "i8042 Aux-%d Port", index);
  813. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, index + 1);
  814. *port = i8042_ports[I8042_AUX_PORT_NO];
  815. port->exists = 0;
  816. snprintf(port->name, sizeof(port->name), "AUX%d", index);
  817. port->mux = index;
  818. port->serio = serio;
  819. return i8042_port_register(port);
  820. }
  821. static int __devinit i8042_probe(struct platform_device *dev)
  822. {
  823. int i, have_ports = 0;
  824. int err;
  825. init_timer(&i8042_timer);
  826. i8042_timer.function = i8042_timer_func;
  827. if (i8042_controller_init())
  828. return -ENODEV;
  829. if (!i8042_noaux && !i8042_check_aux()) {
  830. if (!i8042_nomux && !i8042_check_mux()) {
  831. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  832. err = i8042_create_mux_port(i);
  833. if (err)
  834. goto err_unregister_ports;
  835. }
  836. } else {
  837. err = i8042_create_aux_port();
  838. if (err)
  839. goto err_unregister_ports;
  840. }
  841. have_ports = 1;
  842. }
  843. if (!i8042_nokbd) {
  844. err = i8042_create_kbd_port();
  845. if (err)
  846. goto err_unregister_ports;
  847. have_ports = 1;
  848. }
  849. if (!have_ports) {
  850. err = -ENODEV;
  851. goto err_controller_cleanup;
  852. }
  853. mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
  854. return 0;
  855. err_unregister_ports:
  856. for (i = 0; i < I8042_NUM_PORTS; i++)
  857. if (i8042_ports[i].serio)
  858. serio_unregister_port(i8042_ports[i].serio);
  859. err_controller_cleanup:
  860. i8042_controller_cleanup();
  861. return err;
  862. }
  863. static int __devexit i8042_remove(struct platform_device *dev)
  864. {
  865. int i;
  866. i8042_controller_cleanup();
  867. for (i = 0; i < I8042_NUM_PORTS; i++)
  868. if (i8042_ports[i].exists)
  869. serio_unregister_port(i8042_ports[i].serio);
  870. del_timer_sync(&i8042_timer);
  871. return 0;
  872. }
  873. static struct platform_driver i8042_driver = {
  874. .driver = {
  875. .name = "i8042",
  876. .owner = THIS_MODULE,
  877. },
  878. .probe = i8042_probe,
  879. .remove = __devexit_p(i8042_remove),
  880. .suspend = i8042_suspend,
  881. .resume = i8042_resume,
  882. .shutdown = i8042_shutdown,
  883. };
  884. static int __init i8042_init(void)
  885. {
  886. int err;
  887. dbg_init();
  888. err = i8042_platform_init();
  889. if (err)
  890. return err;
  891. i8042_ports[I8042_AUX_PORT_NO].irq = I8042_AUX_IRQ;
  892. i8042_ports[I8042_KBD_PORT_NO].irq = I8042_KBD_IRQ;
  893. err = platform_driver_register(&i8042_driver);
  894. if (err)
  895. goto err_platform_exit;
  896. i8042_platform_device = platform_device_alloc("i8042", -1);
  897. if (!i8042_platform_device) {
  898. err = -ENOMEM;
  899. goto err_unregister_driver;
  900. }
  901. err = platform_device_add(i8042_platform_device);
  902. if (err)
  903. goto err_free_device;
  904. return 0;
  905. err_free_device:
  906. platform_device_put(i8042_platform_device);
  907. err_unregister_driver:
  908. platform_driver_unregister(&i8042_driver);
  909. err_platform_exit:
  910. i8042_platform_exit();
  911. return err;
  912. }
  913. static void __exit i8042_exit(void)
  914. {
  915. platform_device_unregister(i8042_platform_device);
  916. platform_driver_unregister(&i8042_driver);
  917. i8042_platform_exit();
  918. panic_blink = NULL;
  919. }
  920. module_init(i8042_init);
  921. module_exit(i8042_exit);