i8042.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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 __init 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 __init 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 __init 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 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 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 device *dev)
  755. {
  756. i8042_controller_cleanup();
  757. }
  758. static struct device_driver i8042_driver = {
  759. .name = "i8042",
  760. .bus = &platform_bus_type,
  761. .suspend = i8042_suspend,
  762. .resume = i8042_resume,
  763. .shutdown = i8042_shutdown,
  764. };
  765. static int __init i8042_create_kbd_port(void)
  766. {
  767. struct serio *serio;
  768. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  769. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  770. if (!serio)
  771. return -ENOMEM;
  772. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  773. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  774. serio->open = i8042_open;
  775. serio->close = i8042_close;
  776. serio->start = i8042_start;
  777. serio->stop = i8042_stop;
  778. serio->port_data = port;
  779. serio->dev.parent = &i8042_platform_device->dev;
  780. strlcpy(serio->name, "i8042 Kbd Port", sizeof(serio->name));
  781. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  782. port->serio = serio;
  783. return i8042_port_register(port);
  784. }
  785. static int __init i8042_create_aux_port(void)
  786. {
  787. struct serio *serio;
  788. struct i8042_port *port = &i8042_ports[I8042_AUX_PORT_NO];
  789. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  790. if (!serio)
  791. return -ENOMEM;
  792. serio->id.type = SERIO_8042;
  793. serio->write = i8042_aux_write;
  794. serio->open = i8042_open;
  795. serio->close = i8042_close;
  796. serio->start = i8042_start;
  797. serio->stop = i8042_stop;
  798. serio->port_data = port;
  799. serio->dev.parent = &i8042_platform_device->dev;
  800. strlcpy(serio->name, "i8042 Aux Port", sizeof(serio->name));
  801. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  802. port->serio = serio;
  803. return i8042_port_register(port);
  804. }
  805. static int __init i8042_create_mux_port(int index)
  806. {
  807. struct serio *serio;
  808. struct i8042_port *port = &i8042_ports[I8042_MUX_PORT_NO + index];
  809. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  810. if (!serio)
  811. return -ENOMEM;
  812. serio->id.type = SERIO_8042;
  813. serio->write = i8042_aux_write;
  814. serio->open = i8042_open;
  815. serio->close = i8042_close;
  816. serio->start = i8042_start;
  817. serio->stop = i8042_stop;
  818. serio->port_data = port;
  819. serio->dev.parent = &i8042_platform_device->dev;
  820. snprintf(serio->name, sizeof(serio->name), "i8042 Aux-%d Port", index);
  821. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, index + 1);
  822. *port = i8042_ports[I8042_AUX_PORT_NO];
  823. port->exists = 0;
  824. snprintf(port->name, sizeof(port->name), "AUX%d", index);
  825. port->mux = index;
  826. port->serio = serio;
  827. return i8042_port_register(port);
  828. }
  829. static int __init i8042_init(void)
  830. {
  831. int i, have_ports = 0;
  832. int err;
  833. dbg_init();
  834. init_timer(&i8042_timer);
  835. i8042_timer.function = i8042_timer_func;
  836. err = i8042_platform_init();
  837. if (err)
  838. return err;
  839. i8042_ports[I8042_AUX_PORT_NO].irq = I8042_AUX_IRQ;
  840. i8042_ports[I8042_KBD_PORT_NO].irq = I8042_KBD_IRQ;
  841. if (i8042_controller_init()) {
  842. err = -ENODEV;
  843. goto err_platform_exit;
  844. }
  845. err = driver_register(&i8042_driver);
  846. if (err)
  847. goto err_controller_cleanup;
  848. i8042_platform_device = platform_device_register_simple("i8042", -1, NULL, 0);
  849. if (IS_ERR(i8042_platform_device)) {
  850. err = PTR_ERR(i8042_platform_device);
  851. goto err_unregister_driver;
  852. }
  853. if (!i8042_noaux && !i8042_check_aux()) {
  854. if (!i8042_nomux && !i8042_check_mux()) {
  855. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  856. err = i8042_create_mux_port(i);
  857. if (err)
  858. goto err_unregister_ports;
  859. }
  860. } else {
  861. err = i8042_create_aux_port();
  862. if (err)
  863. goto err_unregister_ports;
  864. }
  865. have_ports = 1;
  866. }
  867. if (!i8042_nokbd) {
  868. err = i8042_create_kbd_port();
  869. if (err)
  870. goto err_unregister_ports;
  871. have_ports = 1;
  872. }
  873. if (!have_ports) {
  874. err = -ENODEV;
  875. goto err_unregister_device;
  876. }
  877. mod_timer(&i8042_timer, jiffies + I8042_POLL_PERIOD);
  878. return 0;
  879. err_unregister_ports:
  880. for (i = 0; i < I8042_NUM_PORTS; i++)
  881. if (i8042_ports[i].serio)
  882. serio_unregister_port(i8042_ports[i].serio);
  883. err_unregister_device:
  884. platform_device_unregister(i8042_platform_device);
  885. err_unregister_driver:
  886. driver_unregister(&i8042_driver);
  887. err_controller_cleanup:
  888. i8042_controller_cleanup();
  889. err_platform_exit:
  890. i8042_platform_exit();
  891. return err;
  892. }
  893. static void __exit i8042_exit(void)
  894. {
  895. int i;
  896. i8042_controller_cleanup();
  897. for (i = 0; i < I8042_NUM_PORTS; i++)
  898. if (i8042_ports[i].exists)
  899. serio_unregister_port(i8042_ports[i].serio);
  900. del_timer_sync(&i8042_timer);
  901. platform_device_unregister(i8042_platform_device);
  902. driver_unregister(&i8042_driver);
  903. i8042_platform_exit();
  904. panic_blink = NULL;
  905. }
  906. module_init(i8042_init);
  907. module_exit(i8042_exit);