i8042.c 26 KB

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