i8042.c 37 KB

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