i8042.c 27 KB

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