i8042.c 34 KB

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