i8042.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/types.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ioport.h>
  17. #include <linux/init.h>
  18. #include <linux/serio.h>
  19. #include <linux/err.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/i8042.h>
  23. #include <linux/slab.h>
  24. #include <asm/io.h>
  25. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  26. MODULE_DESCRIPTION("i8042 keyboard and mouse controller driver");
  27. MODULE_LICENSE("GPL");
  28. static bool i8042_nokbd;
  29. module_param_named(nokbd, i8042_nokbd, bool, 0);
  30. MODULE_PARM_DESC(nokbd, "Do not probe or use KBD port.");
  31. static bool i8042_noaux;
  32. module_param_named(noaux, i8042_noaux, bool, 0);
  33. MODULE_PARM_DESC(noaux, "Do not probe or use AUX (mouse) port.");
  34. static bool i8042_nomux;
  35. module_param_named(nomux, i8042_nomux, bool, 0);
  36. MODULE_PARM_DESC(nomux, "Do not check whether an active multiplexing controller is present.");
  37. static bool i8042_unlock;
  38. module_param_named(unlock, i8042_unlock, bool, 0);
  39. MODULE_PARM_DESC(unlock, "Ignore keyboard lock.");
  40. static bool i8042_reset;
  41. module_param_named(reset, i8042_reset, bool, 0);
  42. MODULE_PARM_DESC(reset, "Reset controller during init and cleanup.");
  43. static bool i8042_direct;
  44. module_param_named(direct, i8042_direct, bool, 0);
  45. MODULE_PARM_DESC(direct, "Put keyboard port into non-translated mode.");
  46. static bool i8042_dumbkbd;
  47. module_param_named(dumbkbd, i8042_dumbkbd, bool, 0);
  48. MODULE_PARM_DESC(dumbkbd, "Pretend that controller can only read data from keyboard");
  49. static bool i8042_noloop;
  50. module_param_named(noloop, i8042_noloop, bool, 0);
  51. MODULE_PARM_DESC(noloop, "Disable the AUX Loopback command while probing for the AUX port");
  52. static bool i8042_notimeout;
  53. module_param_named(notimeout, i8042_notimeout, bool, 0);
  54. MODULE_PARM_DESC(notimeout, "Ignore timeouts signalled by i8042");
  55. #ifdef CONFIG_X86
  56. static bool i8042_dritek;
  57. module_param_named(dritek, i8042_dritek, bool, 0);
  58. MODULE_PARM_DESC(dritek, "Force enable the Dritek keyboard extension");
  59. #endif
  60. #ifdef CONFIG_PNP
  61. static bool i8042_nopnp;
  62. module_param_named(nopnp, i8042_nopnp, bool, 0);
  63. MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings");
  64. #endif
  65. #define DEBUG
  66. #ifdef DEBUG
  67. static bool i8042_debug;
  68. module_param_named(debug, i8042_debug, bool, 0600);
  69. MODULE_PARM_DESC(debug, "Turn i8042 debugging mode on and off");
  70. #endif
  71. static bool i8042_bypass_aux_irq_test;
  72. #include "i8042.h"
  73. /*
  74. * i8042_lock protects serialization between i8042_command and
  75. * the interrupt handler.
  76. */
  77. static DEFINE_SPINLOCK(i8042_lock);
  78. /*
  79. * Writers to AUX and KBD ports as well as users issuing i8042_command
  80. * directly should acquire i8042_mutex (by means of calling
  81. * i8042_lock_chip() and i8042_unlock_ship() helpers) to ensure that
  82. * they do not disturb each other (unfortunately in many i8042
  83. * implementations write to one of the ports will immediately abort
  84. * command that is being processed by another port).
  85. */
  86. static DEFINE_MUTEX(i8042_mutex);
  87. struct i8042_port {
  88. struct serio *serio;
  89. int irq;
  90. bool exists;
  91. signed char mux;
  92. };
  93. #define I8042_KBD_PORT_NO 0
  94. #define I8042_AUX_PORT_NO 1
  95. #define I8042_MUX_PORT_NO 2
  96. #define I8042_NUM_PORTS (I8042_NUM_MUX_PORTS + 2)
  97. static struct i8042_port i8042_ports[I8042_NUM_PORTS];
  98. static unsigned char i8042_initial_ctr;
  99. static unsigned char i8042_ctr;
  100. static bool i8042_mux_present;
  101. static bool i8042_kbd_irq_registered;
  102. static bool i8042_aux_irq_registered;
  103. static unsigned char i8042_suppress_kbd_ack;
  104. static struct platform_device *i8042_platform_device;
  105. static irqreturn_t i8042_interrupt(int irq, void *dev_id);
  106. static bool (*i8042_platform_filter)(unsigned char data, unsigned char str,
  107. struct serio *serio);
  108. void i8042_lock_chip(void)
  109. {
  110. mutex_lock(&i8042_mutex);
  111. }
  112. EXPORT_SYMBOL(i8042_lock_chip);
  113. void i8042_unlock_chip(void)
  114. {
  115. mutex_unlock(&i8042_mutex);
  116. }
  117. EXPORT_SYMBOL(i8042_unlock_chip);
  118. int i8042_install_filter(bool (*filter)(unsigned char data, unsigned char str,
  119. struct serio *serio))
  120. {
  121. unsigned long flags;
  122. int ret = 0;
  123. spin_lock_irqsave(&i8042_lock, flags);
  124. if (i8042_platform_filter) {
  125. ret = -EBUSY;
  126. goto out;
  127. }
  128. i8042_platform_filter = filter;
  129. out:
  130. spin_unlock_irqrestore(&i8042_lock, flags);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL(i8042_install_filter);
  134. int i8042_remove_filter(bool (*filter)(unsigned char data, unsigned char str,
  135. struct serio *port))
  136. {
  137. unsigned long flags;
  138. int ret = 0;
  139. spin_lock_irqsave(&i8042_lock, flags);
  140. if (i8042_platform_filter != filter) {
  141. ret = -EINVAL;
  142. goto out;
  143. }
  144. i8042_platform_filter = NULL;
  145. out:
  146. spin_unlock_irqrestore(&i8042_lock, flags);
  147. return ret;
  148. }
  149. EXPORT_SYMBOL(i8042_remove_filter);
  150. /*
  151. * The i8042_wait_read() and i8042_wait_write functions wait for the i8042 to
  152. * be ready for reading values from it / writing values to it.
  153. * Called always with i8042_lock held.
  154. */
  155. static int i8042_wait_read(void)
  156. {
  157. int i = 0;
  158. while ((~i8042_read_status() & I8042_STR_OBF) && (i < I8042_CTL_TIMEOUT)) {
  159. udelay(50);
  160. i++;
  161. }
  162. return -(i == I8042_CTL_TIMEOUT);
  163. }
  164. static int i8042_wait_write(void)
  165. {
  166. int i = 0;
  167. while ((i8042_read_status() & I8042_STR_IBF) && (i < I8042_CTL_TIMEOUT)) {
  168. udelay(50);
  169. i++;
  170. }
  171. return -(i == I8042_CTL_TIMEOUT);
  172. }
  173. /*
  174. * i8042_flush() flushes all data that may be in the keyboard and mouse buffers
  175. * of the i8042 down the toilet.
  176. */
  177. static int i8042_flush(void)
  178. {
  179. unsigned long flags;
  180. unsigned char data, str;
  181. int i = 0;
  182. spin_lock_irqsave(&i8042_lock, flags);
  183. while (((str = i8042_read_status()) & I8042_STR_OBF) && (i < I8042_BUFFER_SIZE)) {
  184. udelay(50);
  185. data = i8042_read_data();
  186. i++;
  187. dbg("%02x <- i8042 (flush, %s)\n",
  188. data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
  189. }
  190. spin_unlock_irqrestore(&i8042_lock, flags);
  191. return i;
  192. }
  193. /*
  194. * i8042_command() executes a command on the i8042. It also sends the input
  195. * parameter(s) of the commands to it, and receives the output value(s). The
  196. * parameters are to be stored in the param array, and the output is placed
  197. * into the same array. The number of the parameters and output values is
  198. * encoded in bits 8-11 of the command number.
  199. */
  200. static int __i8042_command(unsigned char *param, int command)
  201. {
  202. int i, error;
  203. if (i8042_noloop && command == I8042_CMD_AUX_LOOP)
  204. return -1;
  205. error = i8042_wait_write();
  206. if (error)
  207. return error;
  208. dbg("%02x -> i8042 (command)\n", command & 0xff);
  209. i8042_write_command(command & 0xff);
  210. for (i = 0; i < ((command >> 12) & 0xf); i++) {
  211. error = i8042_wait_write();
  212. if (error)
  213. return error;
  214. dbg("%02x -> i8042 (parameter)\n", param[i]);
  215. i8042_write_data(param[i]);
  216. }
  217. for (i = 0; i < ((command >> 8) & 0xf); i++) {
  218. error = i8042_wait_read();
  219. if (error) {
  220. dbg(" -- i8042 (timeout)\n");
  221. return error;
  222. }
  223. if (command == I8042_CMD_AUX_LOOP &&
  224. !(i8042_read_status() & I8042_STR_AUXDATA)) {
  225. dbg(" -- i8042 (auxerr)\n");
  226. return -1;
  227. }
  228. param[i] = i8042_read_data();
  229. dbg("%02x <- i8042 (return)\n", param[i]);
  230. }
  231. return 0;
  232. }
  233. int i8042_command(unsigned char *param, int command)
  234. {
  235. unsigned long flags;
  236. int retval;
  237. spin_lock_irqsave(&i8042_lock, flags);
  238. retval = __i8042_command(param, command);
  239. spin_unlock_irqrestore(&i8042_lock, flags);
  240. return retval;
  241. }
  242. EXPORT_SYMBOL(i8042_command);
  243. /*
  244. * i8042_kbd_write() sends a byte out through the keyboard interface.
  245. */
  246. static int i8042_kbd_write(struct serio *port, unsigned char c)
  247. {
  248. unsigned long flags;
  249. int retval = 0;
  250. spin_lock_irqsave(&i8042_lock, flags);
  251. if (!(retval = i8042_wait_write())) {
  252. dbg("%02x -> i8042 (kbd-data)\n", c);
  253. i8042_write_data(c);
  254. }
  255. spin_unlock_irqrestore(&i8042_lock, flags);
  256. return retval;
  257. }
  258. /*
  259. * i8042_aux_write() sends a byte out through the aux interface.
  260. */
  261. static int i8042_aux_write(struct serio *serio, unsigned char c)
  262. {
  263. struct i8042_port *port = serio->port_data;
  264. return i8042_command(&c, port->mux == -1 ?
  265. I8042_CMD_AUX_SEND :
  266. I8042_CMD_MUX_SEND + port->mux);
  267. }
  268. /*
  269. * i8042_aux_close attempts to clear AUX or KBD port state by disabling
  270. * and then re-enabling it.
  271. */
  272. static void i8042_port_close(struct serio *serio)
  273. {
  274. int irq_bit;
  275. int disable_bit;
  276. const char *port_name;
  277. if (serio == i8042_ports[I8042_AUX_PORT_NO].serio) {
  278. irq_bit = I8042_CTR_AUXINT;
  279. disable_bit = I8042_CTR_AUXDIS;
  280. port_name = "AUX";
  281. } else {
  282. irq_bit = I8042_CTR_KBDINT;
  283. disable_bit = I8042_CTR_KBDDIS;
  284. port_name = "KBD";
  285. }
  286. i8042_ctr &= ~irq_bit;
  287. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  288. pr_warn("Can't write CTR while closing %s port\n", port_name);
  289. udelay(50);
  290. i8042_ctr &= ~disable_bit;
  291. i8042_ctr |= irq_bit;
  292. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  293. pr_err("Can't reactivate %s port\n", port_name);
  294. /*
  295. * See if there is any data appeared while we were messing with
  296. * port state.
  297. */
  298. i8042_interrupt(0, NULL);
  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 = true;
  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 = false;
  321. /*
  322. * We synchronize with both AUX and KBD IRQs because there is
  323. * a (very unlikely) chance that AUX IRQ is raised for KBD port
  324. * and vice versa.
  325. */
  326. synchronize_irq(I8042_AUX_IRQ);
  327. synchronize_irq(I8042_KBD_IRQ);
  328. port->serio = NULL;
  329. }
  330. /*
  331. * i8042_filter() filters out unwanted bytes from the input data stream.
  332. * It is called from i8042_interrupt and thus is running with interrupts
  333. * off and i8042_lock held.
  334. */
  335. static bool i8042_filter(unsigned char data, unsigned char str,
  336. struct serio *serio)
  337. {
  338. if (unlikely(i8042_suppress_kbd_ack)) {
  339. if ((~str & I8042_STR_AUXDATA) &&
  340. (data == 0xfa || data == 0xfe)) {
  341. i8042_suppress_kbd_ack--;
  342. dbg("Extra keyboard ACK - filtered out\n");
  343. return true;
  344. }
  345. }
  346. if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
  347. dbg("Filtered out by platform filter\n");
  348. return true;
  349. }
  350. return false;
  351. }
  352. /*
  353. * i8042_interrupt() is the most important function in this driver -
  354. * it handles the interrupts from the i8042, and sends incoming bytes
  355. * to the upper layers.
  356. */
  357. static irqreturn_t i8042_interrupt(int irq, void *dev_id)
  358. {
  359. struct i8042_port *port;
  360. struct serio *serio;
  361. unsigned long flags;
  362. unsigned char str, data;
  363. unsigned int dfl;
  364. unsigned int port_no;
  365. bool filtered;
  366. int ret = 1;
  367. spin_lock_irqsave(&i8042_lock, flags);
  368. str = i8042_read_status();
  369. if (unlikely(~str & I8042_STR_OBF)) {
  370. spin_unlock_irqrestore(&i8042_lock, flags);
  371. if (irq)
  372. dbg("Interrupt %d, without any data\n", irq);
  373. ret = 0;
  374. goto out;
  375. }
  376. data = i8042_read_data();
  377. if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
  378. static unsigned long last_transmit;
  379. static unsigned char last_str;
  380. dfl = 0;
  381. if (str & I8042_STR_MUXERR) {
  382. dbg("MUX error, status is %02x, data is %02x\n",
  383. str, data);
  384. /*
  385. * When MUXERR condition is signalled the data register can only contain
  386. * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
  387. * it is not always the case. Some KBCs also report 0xfc when there is
  388. * nothing connected to the port while others sometimes get confused which
  389. * port the data came from and signal error leaving the data intact. They
  390. * _do not_ revert to legacy mode (actually I've never seen KBC reverting
  391. * to legacy mode yet, when we see one we'll add proper handling).
  392. * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
  393. * rest assume that the data came from the same serio last byte
  394. * was transmitted (if transmission happened not too long ago).
  395. */
  396. switch (data) {
  397. default:
  398. if (time_before(jiffies, last_transmit + HZ/10)) {
  399. str = last_str;
  400. break;
  401. }
  402. /* fall through - report timeout */
  403. case 0xfc:
  404. case 0xfd:
  405. case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
  406. case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
  407. }
  408. }
  409. port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
  410. last_str = str;
  411. last_transmit = jiffies;
  412. } else {
  413. dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
  414. ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
  415. port_no = (str & I8042_STR_AUXDATA) ?
  416. I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
  417. }
  418. port = &i8042_ports[port_no];
  419. serio = port->exists ? port->serio : NULL;
  420. dbg("%02x <- i8042 (interrupt, %d, %d%s%s)\n",
  421. data, port_no, irq,
  422. dfl & SERIO_PARITY ? ", bad parity" : "",
  423. dfl & SERIO_TIMEOUT ? ", timeout" : "");
  424. filtered = i8042_filter(data, str, serio);
  425. spin_unlock_irqrestore(&i8042_lock, flags);
  426. if (likely(port->exists && !filtered))
  427. serio_interrupt(serio, data, dfl);
  428. out:
  429. return IRQ_RETVAL(ret);
  430. }
  431. /*
  432. * i8042_enable_kbd_port enables keyboard port on chip
  433. */
  434. static int i8042_enable_kbd_port(void)
  435. {
  436. i8042_ctr &= ~I8042_CTR_KBDDIS;
  437. i8042_ctr |= I8042_CTR_KBDINT;
  438. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  439. i8042_ctr &= ~I8042_CTR_KBDINT;
  440. i8042_ctr |= I8042_CTR_KBDDIS;
  441. pr_err("Failed to enable KBD port\n");
  442. return -EIO;
  443. }
  444. return 0;
  445. }
  446. /*
  447. * i8042_enable_aux_port enables AUX (mouse) port on chip
  448. */
  449. static int i8042_enable_aux_port(void)
  450. {
  451. i8042_ctr &= ~I8042_CTR_AUXDIS;
  452. i8042_ctr |= I8042_CTR_AUXINT;
  453. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  454. i8042_ctr &= ~I8042_CTR_AUXINT;
  455. i8042_ctr |= I8042_CTR_AUXDIS;
  456. pr_err("Failed to enable AUX port\n");
  457. return -EIO;
  458. }
  459. return 0;
  460. }
  461. /*
  462. * i8042_enable_mux_ports enables 4 individual AUX ports after
  463. * the controller has been switched into Multiplexed mode
  464. */
  465. static int i8042_enable_mux_ports(void)
  466. {
  467. unsigned char param;
  468. int i;
  469. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  470. i8042_command(&param, I8042_CMD_MUX_PFX + i);
  471. i8042_command(&param, I8042_CMD_AUX_ENABLE);
  472. }
  473. return i8042_enable_aux_port();
  474. }
  475. /*
  476. * i8042_set_mux_mode checks whether the controller has an
  477. * active multiplexor and puts the chip into Multiplexed (true)
  478. * or Legacy (false) mode.
  479. */
  480. static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
  481. {
  482. unsigned char param, val;
  483. /*
  484. * Get rid of bytes in the queue.
  485. */
  486. i8042_flush();
  487. /*
  488. * Internal loopback test - send three bytes, they should come back from the
  489. * mouse interface, the last should be version.
  490. */
  491. param = val = 0xf0;
  492. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
  493. return -1;
  494. param = val = multiplex ? 0x56 : 0xf6;
  495. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
  496. return -1;
  497. param = val = multiplex ? 0xa4 : 0xa5;
  498. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
  499. return -1;
  500. /*
  501. * Workaround for interference with USB Legacy emulation
  502. * that causes a v10.12 MUX to be found.
  503. */
  504. if (param == 0xac)
  505. return -1;
  506. if (mux_version)
  507. *mux_version = param;
  508. return 0;
  509. }
  510. /*
  511. * i8042_check_mux() checks whether the controller supports the PS/2 Active
  512. * Multiplexing specification by Synaptics, Phoenix, Insyde and
  513. * LCS/Telegraphics.
  514. */
  515. static int __init i8042_check_mux(void)
  516. {
  517. unsigned char mux_version;
  518. if (i8042_set_mux_mode(true, &mux_version))
  519. return -1;
  520. pr_info("Detected active multiplexing controller, rev %d.%d\n",
  521. (mux_version >> 4) & 0xf, mux_version & 0xf);
  522. /*
  523. * Disable all muxed ports by disabling AUX.
  524. */
  525. i8042_ctr |= I8042_CTR_AUXDIS;
  526. i8042_ctr &= ~I8042_CTR_AUXINT;
  527. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  528. pr_err("Failed to disable AUX port, can't use MUX\n");
  529. return -EIO;
  530. }
  531. i8042_mux_present = true;
  532. return 0;
  533. }
  534. /*
  535. * The following is used to test AUX IRQ delivery.
  536. */
  537. static struct completion i8042_aux_irq_delivered __initdata;
  538. static bool i8042_irq_being_tested __initdata;
  539. static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
  540. {
  541. unsigned long flags;
  542. unsigned char str, data;
  543. int ret = 0;
  544. spin_lock_irqsave(&i8042_lock, flags);
  545. str = i8042_read_status();
  546. if (str & I8042_STR_OBF) {
  547. data = i8042_read_data();
  548. dbg("%02x <- i8042 (aux_test_irq, %s)\n",
  549. data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
  550. if (i8042_irq_being_tested &&
  551. data == 0xa5 && (str & I8042_STR_AUXDATA))
  552. complete(&i8042_aux_irq_delivered);
  553. ret = 1;
  554. }
  555. spin_unlock_irqrestore(&i8042_lock, flags);
  556. return IRQ_RETVAL(ret);
  557. }
  558. /*
  559. * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
  560. * verifies success by readinng CTR. Used when testing for presence of AUX
  561. * port.
  562. */
  563. static int __init i8042_toggle_aux(bool on)
  564. {
  565. unsigned char param;
  566. int i;
  567. if (i8042_command(&param,
  568. on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
  569. return -1;
  570. /* some chips need some time to set the I8042_CTR_AUXDIS bit */
  571. for (i = 0; i < 100; i++) {
  572. udelay(50);
  573. if (i8042_command(&param, I8042_CMD_CTL_RCTR))
  574. return -1;
  575. if (!(param & I8042_CTR_AUXDIS) == on)
  576. return 0;
  577. }
  578. return -1;
  579. }
  580. /*
  581. * i8042_check_aux() applies as much paranoia as it can at detecting
  582. * the presence of an AUX interface.
  583. */
  584. static int __init i8042_check_aux(void)
  585. {
  586. int retval = -1;
  587. bool irq_registered = false;
  588. bool aux_loop_broken = false;
  589. unsigned long flags;
  590. unsigned char param;
  591. /*
  592. * Get rid of bytes in the queue.
  593. */
  594. i8042_flush();
  595. /*
  596. * Internal loopback test - filters out AT-type i8042's. Unfortunately
  597. * SiS screwed up and their 5597 doesn't support the LOOP command even
  598. * though it has an AUX port.
  599. */
  600. param = 0x5a;
  601. retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
  602. if (retval || param != 0x5a) {
  603. /*
  604. * External connection test - filters out AT-soldered PS/2 i8042's
  605. * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
  606. * 0xfa - no error on some notebooks which ignore the spec
  607. * Because it's common for chipsets to return error on perfectly functioning
  608. * AUX ports, we test for this only when the LOOP command failed.
  609. */
  610. if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
  611. (param && param != 0xfa && param != 0xff))
  612. return -1;
  613. /*
  614. * If AUX_LOOP completed without error but returned unexpected data
  615. * mark it as broken
  616. */
  617. if (!retval)
  618. aux_loop_broken = true;
  619. }
  620. /*
  621. * Bit assignment test - filters out PS/2 i8042's in AT mode
  622. */
  623. if (i8042_toggle_aux(false)) {
  624. pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
  625. pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
  626. }
  627. if (i8042_toggle_aux(true))
  628. return -1;
  629. /*
  630. * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
  631. * used it for a PCI card or somethig else.
  632. */
  633. if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
  634. /*
  635. * Without LOOP command we can't test AUX IRQ delivery. Assume the port
  636. * is working and hope we are right.
  637. */
  638. retval = 0;
  639. goto out;
  640. }
  641. if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
  642. "i8042", i8042_platform_device))
  643. goto out;
  644. irq_registered = true;
  645. if (i8042_enable_aux_port())
  646. goto out;
  647. spin_lock_irqsave(&i8042_lock, flags);
  648. init_completion(&i8042_aux_irq_delivered);
  649. i8042_irq_being_tested = true;
  650. param = 0xa5;
  651. retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
  652. spin_unlock_irqrestore(&i8042_lock, flags);
  653. if (retval)
  654. goto out;
  655. if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
  656. msecs_to_jiffies(250)) == 0) {
  657. /*
  658. * AUX IRQ was never delivered so we need to flush the controller to
  659. * get rid of the byte we put there; otherwise keyboard may not work.
  660. */
  661. dbg(" -- i8042 (aux irq test timeout)\n");
  662. i8042_flush();
  663. retval = -1;
  664. }
  665. out:
  666. /*
  667. * Disable the interface.
  668. */
  669. i8042_ctr |= I8042_CTR_AUXDIS;
  670. i8042_ctr &= ~I8042_CTR_AUXINT;
  671. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  672. retval = -1;
  673. if (irq_registered)
  674. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  675. return retval;
  676. }
  677. static int i8042_controller_check(void)
  678. {
  679. if (i8042_flush() == I8042_BUFFER_SIZE) {
  680. pr_err("No controller found\n");
  681. return -ENODEV;
  682. }
  683. return 0;
  684. }
  685. static int i8042_controller_selftest(void)
  686. {
  687. unsigned char param;
  688. int i = 0;
  689. /*
  690. * We try this 5 times; on some really fragile systems this does not
  691. * take the first time...
  692. */
  693. do {
  694. if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
  695. pr_err("i8042 controller self test timeout\n");
  696. return -ENODEV;
  697. }
  698. if (param == I8042_RET_CTL_TEST)
  699. return 0;
  700. pr_err("i8042 controller selftest failed. (%#x != %#x)\n",
  701. param, I8042_RET_CTL_TEST);
  702. msleep(50);
  703. } while (i++ < 5);
  704. #ifdef CONFIG_X86
  705. /*
  706. * On x86, we don't fail entire i8042 initialization if controller
  707. * reset fails in hopes that keyboard port will still be functional
  708. * and user will still get a working keyboard. This is especially
  709. * important on netbooks. On other arches we trust hardware more.
  710. */
  711. pr_info("giving up on controller selftest, continuing anyway...\n");
  712. return 0;
  713. #else
  714. return -EIO;
  715. #endif
  716. }
  717. /*
  718. * i8042_controller init initializes the i8042 controller, and,
  719. * most importantly, sets it into non-xlated mode if that's
  720. * desired.
  721. */
  722. static int i8042_controller_init(void)
  723. {
  724. unsigned long flags;
  725. int n = 0;
  726. unsigned char ctr[2];
  727. /*
  728. * Save the CTR for restore on unload / reboot.
  729. */
  730. do {
  731. if (n >= 10) {
  732. pr_err("Unable to get stable CTR read\n");
  733. return -EIO;
  734. }
  735. if (n != 0)
  736. udelay(50);
  737. if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
  738. pr_err("Can't read CTR while initializing i8042\n");
  739. return -EIO;
  740. }
  741. } while (n < 2 || ctr[0] != ctr[1]);
  742. i8042_initial_ctr = i8042_ctr = ctr[0];
  743. /*
  744. * Disable the keyboard interface and interrupt.
  745. */
  746. i8042_ctr |= I8042_CTR_KBDDIS;
  747. i8042_ctr &= ~I8042_CTR_KBDINT;
  748. /*
  749. * Handle keylock.
  750. */
  751. spin_lock_irqsave(&i8042_lock, flags);
  752. if (~i8042_read_status() & I8042_STR_KEYLOCK) {
  753. if (i8042_unlock)
  754. i8042_ctr |= I8042_CTR_IGNKEYLOCK;
  755. else
  756. pr_warn("Warning: Keylock active\n");
  757. }
  758. spin_unlock_irqrestore(&i8042_lock, flags);
  759. /*
  760. * If the chip is configured into nontranslated mode by the BIOS, don't
  761. * bother enabling translating and be happy.
  762. */
  763. if (~i8042_ctr & I8042_CTR_XLATE)
  764. i8042_direct = true;
  765. /*
  766. * Set nontranslated mode for the kbd interface if requested by an option.
  767. * After this the kbd interface becomes a simple serial in/out, like the aux
  768. * interface is. We don't do this by default, since it can confuse notebook
  769. * BIOSes.
  770. */
  771. if (i8042_direct)
  772. i8042_ctr &= ~I8042_CTR_XLATE;
  773. /*
  774. * Write CTR back.
  775. */
  776. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  777. pr_err("Can't write CTR while initializing i8042\n");
  778. return -EIO;
  779. }
  780. /*
  781. * Flush whatever accumulated while we were disabling keyboard port.
  782. */
  783. i8042_flush();
  784. return 0;
  785. }
  786. /*
  787. * Reset the controller and reset CRT to the original value set by BIOS.
  788. */
  789. static void i8042_controller_reset(void)
  790. {
  791. i8042_flush();
  792. /*
  793. * Disable both KBD and AUX interfaces so they don't get in the way
  794. */
  795. i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
  796. i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
  797. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  798. pr_warn("Can't write CTR while resetting\n");
  799. /*
  800. * Disable MUX mode if present.
  801. */
  802. if (i8042_mux_present)
  803. i8042_set_mux_mode(false, NULL);
  804. /*
  805. * Reset the controller if requested.
  806. */
  807. if (i8042_reset)
  808. i8042_controller_selftest();
  809. /*
  810. * Restore the original control register setting.
  811. */
  812. if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
  813. pr_warn("Can't restore CTR\n");
  814. }
  815. /*
  816. * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
  817. * when kernel panics. Flashing LEDs is useful for users running X who may
  818. * not see the console and will help distingushing panics from "real"
  819. * lockups.
  820. *
  821. * Note that DELAY has a limit of 10ms so we will not get stuck here
  822. * waiting for KBC to free up even if KBD interrupt is off
  823. */
  824. #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
  825. static long i8042_panic_blink(int state)
  826. {
  827. long delay = 0;
  828. char led;
  829. led = (state) ? 0x01 | 0x04 : 0;
  830. while (i8042_read_status() & I8042_STR_IBF)
  831. DELAY;
  832. dbg("%02x -> i8042 (panic blink)\n", 0xed);
  833. i8042_suppress_kbd_ack = 2;
  834. i8042_write_data(0xed); /* set leds */
  835. DELAY;
  836. while (i8042_read_status() & I8042_STR_IBF)
  837. DELAY;
  838. DELAY;
  839. dbg("%02x -> i8042 (panic blink)\n", led);
  840. i8042_write_data(led);
  841. DELAY;
  842. return delay;
  843. }
  844. #undef DELAY
  845. #ifdef CONFIG_X86
  846. static void i8042_dritek_enable(void)
  847. {
  848. unsigned char param = 0x90;
  849. int error;
  850. error = i8042_command(&param, 0x1059);
  851. if (error)
  852. pr_warn("Failed to enable DRITEK extension: %d\n", error);
  853. }
  854. #endif
  855. #ifdef CONFIG_PM
  856. /*
  857. * Here we try to reset everything back to a state we had
  858. * before suspending.
  859. */
  860. static int i8042_controller_resume(bool force_reset)
  861. {
  862. int error;
  863. error = i8042_controller_check();
  864. if (error)
  865. return error;
  866. if (i8042_reset || force_reset) {
  867. error = i8042_controller_selftest();
  868. if (error)
  869. return error;
  870. }
  871. /*
  872. * Restore original CTR value and disable all ports
  873. */
  874. i8042_ctr = i8042_initial_ctr;
  875. if (i8042_direct)
  876. i8042_ctr &= ~I8042_CTR_XLATE;
  877. i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
  878. i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
  879. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  880. pr_warn("Can't write CTR to resume, retrying...\n");
  881. msleep(50);
  882. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  883. pr_err("CTR write retry failed\n");
  884. return -EIO;
  885. }
  886. }
  887. #ifdef CONFIG_X86
  888. if (i8042_dritek)
  889. i8042_dritek_enable();
  890. #endif
  891. if (i8042_mux_present) {
  892. if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
  893. pr_warn("failed to resume active multiplexor, mouse won't work\n");
  894. } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
  895. i8042_enable_aux_port();
  896. if (i8042_ports[I8042_KBD_PORT_NO].serio)
  897. i8042_enable_kbd_port();
  898. i8042_interrupt(0, NULL);
  899. return 0;
  900. }
  901. /*
  902. * Here we try to restore the original BIOS settings to avoid
  903. * upsetting it.
  904. */
  905. static int i8042_pm_reset(struct device *dev)
  906. {
  907. i8042_controller_reset();
  908. return 0;
  909. }
  910. static int i8042_pm_resume(struct device *dev)
  911. {
  912. /*
  913. * On resume from S2R we always try to reset the controller
  914. * to bring it in a sane state. (In case of S2D we expect
  915. * BIOS to reset the controller for us.)
  916. */
  917. return i8042_controller_resume(true);
  918. }
  919. static int i8042_pm_thaw(struct device *dev)
  920. {
  921. i8042_interrupt(0, NULL);
  922. return 0;
  923. }
  924. static int i8042_pm_restore(struct device *dev)
  925. {
  926. return i8042_controller_resume(false);
  927. }
  928. static const struct dev_pm_ops i8042_pm_ops = {
  929. .suspend = i8042_pm_reset,
  930. .resume = i8042_pm_resume,
  931. .thaw = i8042_pm_thaw,
  932. .poweroff = i8042_pm_reset,
  933. .restore = i8042_pm_restore,
  934. };
  935. #endif /* CONFIG_PM */
  936. /*
  937. * We need to reset the 8042 back to original mode on system shutdown,
  938. * because otherwise BIOSes will be confused.
  939. */
  940. static void i8042_shutdown(struct platform_device *dev)
  941. {
  942. i8042_controller_reset();
  943. }
  944. static int __init i8042_create_kbd_port(void)
  945. {
  946. struct serio *serio;
  947. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  948. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  949. if (!serio)
  950. return -ENOMEM;
  951. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  952. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  953. serio->start = i8042_start;
  954. serio->stop = i8042_stop;
  955. serio->close = i8042_port_close;
  956. serio->port_data = port;
  957. serio->dev.parent = &i8042_platform_device->dev;
  958. strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
  959. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  960. port->serio = serio;
  961. port->irq = I8042_KBD_IRQ;
  962. return 0;
  963. }
  964. static int __init i8042_create_aux_port(int idx)
  965. {
  966. struct serio *serio;
  967. int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
  968. struct i8042_port *port = &i8042_ports[port_no];
  969. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  970. if (!serio)
  971. return -ENOMEM;
  972. serio->id.type = SERIO_8042;
  973. serio->write = i8042_aux_write;
  974. serio->start = i8042_start;
  975. serio->stop = i8042_stop;
  976. serio->port_data = port;
  977. serio->dev.parent = &i8042_platform_device->dev;
  978. if (idx < 0) {
  979. strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
  980. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  981. serio->close = i8042_port_close;
  982. } else {
  983. snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
  984. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
  985. }
  986. port->serio = serio;
  987. port->mux = idx;
  988. port->irq = I8042_AUX_IRQ;
  989. return 0;
  990. }
  991. static void __init i8042_free_kbd_port(void)
  992. {
  993. kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
  994. i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
  995. }
  996. static void __init i8042_free_aux_ports(void)
  997. {
  998. int i;
  999. for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
  1000. kfree(i8042_ports[i].serio);
  1001. i8042_ports[i].serio = NULL;
  1002. }
  1003. }
  1004. static void __init i8042_register_ports(void)
  1005. {
  1006. int i;
  1007. for (i = 0; i < I8042_NUM_PORTS; i++) {
  1008. if (i8042_ports[i].serio) {
  1009. printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
  1010. i8042_ports[i].serio->name,
  1011. (unsigned long) I8042_DATA_REG,
  1012. (unsigned long) I8042_COMMAND_REG,
  1013. i8042_ports[i].irq);
  1014. serio_register_port(i8042_ports[i].serio);
  1015. }
  1016. }
  1017. }
  1018. static void __devexit i8042_unregister_ports(void)
  1019. {
  1020. int i;
  1021. for (i = 0; i < I8042_NUM_PORTS; i++) {
  1022. if (i8042_ports[i].serio) {
  1023. serio_unregister_port(i8042_ports[i].serio);
  1024. i8042_ports[i].serio = NULL;
  1025. }
  1026. }
  1027. }
  1028. /*
  1029. * Checks whether port belongs to i8042 controller.
  1030. */
  1031. bool i8042_check_port_owner(const struct serio *port)
  1032. {
  1033. int i;
  1034. for (i = 0; i < I8042_NUM_PORTS; i++)
  1035. if (i8042_ports[i].serio == port)
  1036. return true;
  1037. return false;
  1038. }
  1039. EXPORT_SYMBOL(i8042_check_port_owner);
  1040. static void i8042_free_irqs(void)
  1041. {
  1042. if (i8042_aux_irq_registered)
  1043. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  1044. if (i8042_kbd_irq_registered)
  1045. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  1046. i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
  1047. }
  1048. static int __init i8042_setup_aux(void)
  1049. {
  1050. int (*aux_enable)(void);
  1051. int error;
  1052. int i;
  1053. if (i8042_check_aux())
  1054. return -ENODEV;
  1055. if (i8042_nomux || i8042_check_mux()) {
  1056. error = i8042_create_aux_port(-1);
  1057. if (error)
  1058. goto err_free_ports;
  1059. aux_enable = i8042_enable_aux_port;
  1060. } else {
  1061. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  1062. error = i8042_create_aux_port(i);
  1063. if (error)
  1064. goto err_free_ports;
  1065. }
  1066. aux_enable = i8042_enable_mux_ports;
  1067. }
  1068. error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
  1069. "i8042", i8042_platform_device);
  1070. if (error)
  1071. goto err_free_ports;
  1072. if (aux_enable())
  1073. goto err_free_irq;
  1074. i8042_aux_irq_registered = true;
  1075. return 0;
  1076. err_free_irq:
  1077. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  1078. err_free_ports:
  1079. i8042_free_aux_ports();
  1080. return error;
  1081. }
  1082. static int __init i8042_setup_kbd(void)
  1083. {
  1084. int error;
  1085. error = i8042_create_kbd_port();
  1086. if (error)
  1087. return error;
  1088. error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
  1089. "i8042", i8042_platform_device);
  1090. if (error)
  1091. goto err_free_port;
  1092. error = i8042_enable_kbd_port();
  1093. if (error)
  1094. goto err_free_irq;
  1095. i8042_kbd_irq_registered = true;
  1096. return 0;
  1097. err_free_irq:
  1098. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  1099. err_free_port:
  1100. i8042_free_kbd_port();
  1101. return error;
  1102. }
  1103. static int __init i8042_probe(struct platform_device *dev)
  1104. {
  1105. int error;
  1106. i8042_platform_device = dev;
  1107. if (i8042_reset) {
  1108. error = i8042_controller_selftest();
  1109. if (error)
  1110. return error;
  1111. }
  1112. error = i8042_controller_init();
  1113. if (error)
  1114. return error;
  1115. #ifdef CONFIG_X86
  1116. if (i8042_dritek)
  1117. i8042_dritek_enable();
  1118. #endif
  1119. if (!i8042_noaux) {
  1120. error = i8042_setup_aux();
  1121. if (error && error != -ENODEV && error != -EBUSY)
  1122. goto out_fail;
  1123. }
  1124. if (!i8042_nokbd) {
  1125. error = i8042_setup_kbd();
  1126. if (error)
  1127. goto out_fail;
  1128. }
  1129. /*
  1130. * Ok, everything is ready, let's register all serio ports
  1131. */
  1132. i8042_register_ports();
  1133. return 0;
  1134. out_fail:
  1135. i8042_free_aux_ports(); /* in case KBD failed but AUX not */
  1136. i8042_free_irqs();
  1137. i8042_controller_reset();
  1138. i8042_platform_device = NULL;
  1139. return error;
  1140. }
  1141. static int __devexit i8042_remove(struct platform_device *dev)
  1142. {
  1143. i8042_unregister_ports();
  1144. i8042_free_irqs();
  1145. i8042_controller_reset();
  1146. i8042_platform_device = NULL;
  1147. return 0;
  1148. }
  1149. static struct platform_driver i8042_driver = {
  1150. .driver = {
  1151. .name = "i8042",
  1152. .owner = THIS_MODULE,
  1153. #ifdef CONFIG_PM
  1154. .pm = &i8042_pm_ops,
  1155. #endif
  1156. },
  1157. .remove = __devexit_p(i8042_remove),
  1158. .shutdown = i8042_shutdown,
  1159. };
  1160. static int __init i8042_init(void)
  1161. {
  1162. struct platform_device *pdev;
  1163. int err;
  1164. dbg_init();
  1165. err = i8042_platform_init();
  1166. if (err)
  1167. return err;
  1168. err = i8042_controller_check();
  1169. if (err)
  1170. goto err_platform_exit;
  1171. pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
  1172. if (IS_ERR(pdev)) {
  1173. err = PTR_ERR(pdev);
  1174. goto err_platform_exit;
  1175. }
  1176. panic_blink = i8042_panic_blink;
  1177. return 0;
  1178. err_platform_exit:
  1179. i8042_platform_exit();
  1180. return err;
  1181. }
  1182. static void __exit i8042_exit(void)
  1183. {
  1184. platform_device_unregister(i8042_platform_device);
  1185. platform_driver_unregister(&i8042_driver);
  1186. i8042_platform_exit();
  1187. panic_blink = NULL;
  1188. }
  1189. module_init(i8042_init);
  1190. module_exit(i8042_exit);