i8042.c 26 KB

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