i8042.c 27 KB

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