i8042.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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. spin_lock_irq(&i8042_lock);
  354. port->exists = true;
  355. spin_unlock_irq(&i8042_lock);
  356. return 0;
  357. }
  358. /*
  359. * i8042_stop() marks serio port as non-existing so i8042_interrupt
  360. * will not try to send data to the port that is about to go away.
  361. * The function is called by serio core as part of unregister procedure.
  362. */
  363. static void i8042_stop(struct serio *serio)
  364. {
  365. struct i8042_port *port = serio->port_data;
  366. spin_lock_irq(&i8042_lock);
  367. port->exists = false;
  368. port->serio = NULL;
  369. spin_unlock_irq(&i8042_lock);
  370. /*
  371. * We need to make sure that interrupt handler finishes using
  372. * our serio port before we return from this function.
  373. * We synchronize with both AUX and KBD IRQs because there is
  374. * a (very unlikely) chance that AUX IRQ is raised for KBD port
  375. * and vice versa.
  376. */
  377. synchronize_irq(I8042_AUX_IRQ);
  378. synchronize_irq(I8042_KBD_IRQ);
  379. }
  380. /*
  381. * i8042_filter() filters out unwanted bytes from the input data stream.
  382. * It is called from i8042_interrupt and thus is running with interrupts
  383. * off and i8042_lock held.
  384. */
  385. static bool i8042_filter(unsigned char data, unsigned char str,
  386. struct serio *serio)
  387. {
  388. if (unlikely(i8042_suppress_kbd_ack)) {
  389. if ((~str & I8042_STR_AUXDATA) &&
  390. (data == 0xfa || data == 0xfe)) {
  391. i8042_suppress_kbd_ack--;
  392. dbg("Extra keyboard ACK - filtered out\n");
  393. return true;
  394. }
  395. }
  396. if (i8042_platform_filter && i8042_platform_filter(data, str, serio)) {
  397. dbg("Filtered out by platform filter\n");
  398. return true;
  399. }
  400. return false;
  401. }
  402. /*
  403. * i8042_interrupt() is the most important function in this driver -
  404. * it handles the interrupts from the i8042, and sends incoming bytes
  405. * to the upper layers.
  406. */
  407. static irqreturn_t i8042_interrupt(int irq, void *dev_id)
  408. {
  409. struct i8042_port *port;
  410. struct serio *serio;
  411. unsigned long flags;
  412. unsigned char str, data;
  413. unsigned int dfl;
  414. unsigned int port_no;
  415. bool filtered;
  416. int ret = 1;
  417. spin_lock_irqsave(&i8042_lock, flags);
  418. str = i8042_read_status();
  419. if (unlikely(~str & I8042_STR_OBF)) {
  420. spin_unlock_irqrestore(&i8042_lock, flags);
  421. if (irq)
  422. dbg("Interrupt %d, without any data\n", irq);
  423. ret = 0;
  424. goto out;
  425. }
  426. data = i8042_read_data();
  427. if (i8042_mux_present && (str & I8042_STR_AUXDATA)) {
  428. static unsigned long last_transmit;
  429. static unsigned char last_str;
  430. dfl = 0;
  431. if (str & I8042_STR_MUXERR) {
  432. dbg("MUX error, status is %02x, data is %02x\n",
  433. str, data);
  434. /*
  435. * When MUXERR condition is signalled the data register can only contain
  436. * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately
  437. * it is not always the case. Some KBCs also report 0xfc when there is
  438. * nothing connected to the port while others sometimes get confused which
  439. * port the data came from and signal error leaving the data intact. They
  440. * _do not_ revert to legacy mode (actually I've never seen KBC reverting
  441. * to legacy mode yet, when we see one we'll add proper handling).
  442. * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the
  443. * rest assume that the data came from the same serio last byte
  444. * was transmitted (if transmission happened not too long ago).
  445. */
  446. switch (data) {
  447. default:
  448. if (time_before(jiffies, last_transmit + HZ/10)) {
  449. str = last_str;
  450. break;
  451. }
  452. /* fall through - report timeout */
  453. case 0xfc:
  454. case 0xfd:
  455. case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break;
  456. case 0xff: dfl = SERIO_PARITY; data = 0xfe; break;
  457. }
  458. }
  459. port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3);
  460. last_str = str;
  461. last_transmit = jiffies;
  462. } else {
  463. dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) |
  464. ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0);
  465. port_no = (str & I8042_STR_AUXDATA) ?
  466. I8042_AUX_PORT_NO : I8042_KBD_PORT_NO;
  467. }
  468. port = &i8042_ports[port_no];
  469. serio = port->exists ? port->serio : NULL;
  470. if (irq && serio)
  471. pm_wakeup_event(&serio->dev, 0);
  472. filter_dbg(port->driver_bound, data, "<- i8042 (interrupt, %d, %d%s%s)\n",
  473. port_no, irq,
  474. dfl & SERIO_PARITY ? ", bad parity" : "",
  475. dfl & SERIO_TIMEOUT ? ", timeout" : "");
  476. filtered = i8042_filter(data, str, serio);
  477. spin_unlock_irqrestore(&i8042_lock, flags);
  478. if (likely(serio && !filtered))
  479. serio_interrupt(serio, data, dfl);
  480. out:
  481. return IRQ_RETVAL(ret);
  482. }
  483. /*
  484. * i8042_enable_kbd_port enables keyboard port on chip
  485. */
  486. static int i8042_enable_kbd_port(void)
  487. {
  488. i8042_ctr &= ~I8042_CTR_KBDDIS;
  489. i8042_ctr |= I8042_CTR_KBDINT;
  490. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  491. i8042_ctr &= ~I8042_CTR_KBDINT;
  492. i8042_ctr |= I8042_CTR_KBDDIS;
  493. pr_err("Failed to enable KBD port\n");
  494. return -EIO;
  495. }
  496. return 0;
  497. }
  498. /*
  499. * i8042_enable_aux_port enables AUX (mouse) port on chip
  500. */
  501. static int i8042_enable_aux_port(void)
  502. {
  503. i8042_ctr &= ~I8042_CTR_AUXDIS;
  504. i8042_ctr |= I8042_CTR_AUXINT;
  505. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  506. i8042_ctr &= ~I8042_CTR_AUXINT;
  507. i8042_ctr |= I8042_CTR_AUXDIS;
  508. pr_err("Failed to enable AUX port\n");
  509. return -EIO;
  510. }
  511. return 0;
  512. }
  513. /*
  514. * i8042_enable_mux_ports enables 4 individual AUX ports after
  515. * the controller has been switched into Multiplexed mode
  516. */
  517. static int i8042_enable_mux_ports(void)
  518. {
  519. unsigned char param;
  520. int i;
  521. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  522. i8042_command(&param, I8042_CMD_MUX_PFX + i);
  523. i8042_command(&param, I8042_CMD_AUX_ENABLE);
  524. }
  525. return i8042_enable_aux_port();
  526. }
  527. /*
  528. * i8042_set_mux_mode checks whether the controller has an
  529. * active multiplexor and puts the chip into Multiplexed (true)
  530. * or Legacy (false) mode.
  531. */
  532. static int i8042_set_mux_mode(bool multiplex, unsigned char *mux_version)
  533. {
  534. unsigned char param, val;
  535. /*
  536. * Get rid of bytes in the queue.
  537. */
  538. i8042_flush();
  539. /*
  540. * Internal loopback test - send three bytes, they should come back from the
  541. * mouse interface, the last should be version.
  542. */
  543. param = val = 0xf0;
  544. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
  545. return -1;
  546. param = val = multiplex ? 0x56 : 0xf6;
  547. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param != val)
  548. return -1;
  549. param = val = multiplex ? 0xa4 : 0xa5;
  550. if (i8042_command(&param, I8042_CMD_AUX_LOOP) || param == val)
  551. return -1;
  552. /*
  553. * Workaround for interference with USB Legacy emulation
  554. * that causes a v10.12 MUX to be found.
  555. */
  556. if (param == 0xac)
  557. return -1;
  558. if (mux_version)
  559. *mux_version = param;
  560. return 0;
  561. }
  562. /*
  563. * i8042_check_mux() checks whether the controller supports the PS/2 Active
  564. * Multiplexing specification by Synaptics, Phoenix, Insyde and
  565. * LCS/Telegraphics.
  566. */
  567. static int __init i8042_check_mux(void)
  568. {
  569. unsigned char mux_version;
  570. if (i8042_set_mux_mode(true, &mux_version))
  571. return -1;
  572. pr_info("Detected active multiplexing controller, rev %d.%d\n",
  573. (mux_version >> 4) & 0xf, mux_version & 0xf);
  574. /*
  575. * Disable all muxed ports by disabling AUX.
  576. */
  577. i8042_ctr |= I8042_CTR_AUXDIS;
  578. i8042_ctr &= ~I8042_CTR_AUXINT;
  579. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  580. pr_err("Failed to disable AUX port, can't use MUX\n");
  581. return -EIO;
  582. }
  583. i8042_mux_present = true;
  584. return 0;
  585. }
  586. /*
  587. * The following is used to test AUX IRQ delivery.
  588. */
  589. static struct completion i8042_aux_irq_delivered __initdata;
  590. static bool i8042_irq_being_tested __initdata;
  591. static irqreturn_t __init i8042_aux_test_irq(int irq, void *dev_id)
  592. {
  593. unsigned long flags;
  594. unsigned char str, data;
  595. int ret = 0;
  596. spin_lock_irqsave(&i8042_lock, flags);
  597. str = i8042_read_status();
  598. if (str & I8042_STR_OBF) {
  599. data = i8042_read_data();
  600. dbg("%02x <- i8042 (aux_test_irq, %s)\n",
  601. data, str & I8042_STR_AUXDATA ? "aux" : "kbd");
  602. if (i8042_irq_being_tested &&
  603. data == 0xa5 && (str & I8042_STR_AUXDATA))
  604. complete(&i8042_aux_irq_delivered);
  605. ret = 1;
  606. }
  607. spin_unlock_irqrestore(&i8042_lock, flags);
  608. return IRQ_RETVAL(ret);
  609. }
  610. /*
  611. * i8042_toggle_aux - enables or disables AUX port on i8042 via command and
  612. * verifies success by readinng CTR. Used when testing for presence of AUX
  613. * port.
  614. */
  615. static int __init i8042_toggle_aux(bool on)
  616. {
  617. unsigned char param;
  618. int i;
  619. if (i8042_command(&param,
  620. on ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE))
  621. return -1;
  622. /* some chips need some time to set the I8042_CTR_AUXDIS bit */
  623. for (i = 0; i < 100; i++) {
  624. udelay(50);
  625. if (i8042_command(&param, I8042_CMD_CTL_RCTR))
  626. return -1;
  627. if (!(param & I8042_CTR_AUXDIS) == on)
  628. return 0;
  629. }
  630. return -1;
  631. }
  632. /*
  633. * i8042_check_aux() applies as much paranoia as it can at detecting
  634. * the presence of an AUX interface.
  635. */
  636. static int __init i8042_check_aux(void)
  637. {
  638. int retval = -1;
  639. bool irq_registered = false;
  640. bool aux_loop_broken = false;
  641. unsigned long flags;
  642. unsigned char param;
  643. /*
  644. * Get rid of bytes in the queue.
  645. */
  646. i8042_flush();
  647. /*
  648. * Internal loopback test - filters out AT-type i8042's. Unfortunately
  649. * SiS screwed up and their 5597 doesn't support the LOOP command even
  650. * though it has an AUX port.
  651. */
  652. param = 0x5a;
  653. retval = i8042_command(&param, I8042_CMD_AUX_LOOP);
  654. if (retval || param != 0x5a) {
  655. /*
  656. * External connection test - filters out AT-soldered PS/2 i8042's
  657. * 0x00 - no error, 0x01-0x03 - clock/data stuck, 0xff - general error
  658. * 0xfa - no error on some notebooks which ignore the spec
  659. * Because it's common for chipsets to return error on perfectly functioning
  660. * AUX ports, we test for this only when the LOOP command failed.
  661. */
  662. if (i8042_command(&param, I8042_CMD_AUX_TEST) ||
  663. (param && param != 0xfa && param != 0xff))
  664. return -1;
  665. /*
  666. * If AUX_LOOP completed without error but returned unexpected data
  667. * mark it as broken
  668. */
  669. if (!retval)
  670. aux_loop_broken = true;
  671. }
  672. /*
  673. * Bit assignment test - filters out PS/2 i8042's in AT mode
  674. */
  675. if (i8042_toggle_aux(false)) {
  676. pr_warn("Failed to disable AUX port, but continuing anyway... Is this a SiS?\n");
  677. pr_warn("If AUX port is really absent please use the 'i8042.noaux' option\n");
  678. }
  679. if (i8042_toggle_aux(true))
  680. return -1;
  681. /*
  682. * Reset keyboard (needed on some laptops to successfully detect
  683. * touchpad, e.g., some Gigabyte laptop models with Elantech
  684. * touchpads).
  685. */
  686. if (i8042_kbdreset) {
  687. pr_warn("Attempting to reset device connected to KBD port\n");
  688. i8042_kbd_write(NULL, (unsigned char) 0xff);
  689. }
  690. /*
  691. * Test AUX IRQ delivery to make sure BIOS did not grab the IRQ and
  692. * used it for a PCI card or somethig else.
  693. */
  694. if (i8042_noloop || i8042_bypass_aux_irq_test || aux_loop_broken) {
  695. /*
  696. * Without LOOP command we can't test AUX IRQ delivery. Assume the port
  697. * is working and hope we are right.
  698. */
  699. retval = 0;
  700. goto out;
  701. }
  702. if (request_irq(I8042_AUX_IRQ, i8042_aux_test_irq, IRQF_SHARED,
  703. "i8042", i8042_platform_device))
  704. goto out;
  705. irq_registered = true;
  706. if (i8042_enable_aux_port())
  707. goto out;
  708. spin_lock_irqsave(&i8042_lock, flags);
  709. init_completion(&i8042_aux_irq_delivered);
  710. i8042_irq_being_tested = true;
  711. param = 0xa5;
  712. retval = __i8042_command(&param, I8042_CMD_AUX_LOOP & 0xf0ff);
  713. spin_unlock_irqrestore(&i8042_lock, flags);
  714. if (retval)
  715. goto out;
  716. if (wait_for_completion_timeout(&i8042_aux_irq_delivered,
  717. msecs_to_jiffies(250)) == 0) {
  718. /*
  719. * AUX IRQ was never delivered so we need to flush the controller to
  720. * get rid of the byte we put there; otherwise keyboard may not work.
  721. */
  722. dbg(" -- i8042 (aux irq test timeout)\n");
  723. i8042_flush();
  724. retval = -1;
  725. }
  726. out:
  727. /*
  728. * Disable the interface.
  729. */
  730. i8042_ctr |= I8042_CTR_AUXDIS;
  731. i8042_ctr &= ~I8042_CTR_AUXINT;
  732. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  733. retval = -1;
  734. if (irq_registered)
  735. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  736. return retval;
  737. }
  738. static int i8042_controller_check(void)
  739. {
  740. if (i8042_flush()) {
  741. pr_info("No controller found\n");
  742. return -ENODEV;
  743. }
  744. return 0;
  745. }
  746. static int i8042_controller_selftest(void)
  747. {
  748. unsigned char param;
  749. int i = 0;
  750. /*
  751. * We try this 5 times; on some really fragile systems this does not
  752. * take the first time...
  753. */
  754. do {
  755. if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
  756. pr_err("i8042 controller selftest timeout\n");
  757. return -ENODEV;
  758. }
  759. if (param == I8042_RET_CTL_TEST)
  760. return 0;
  761. dbg("i8042 controller selftest: %#x != %#x\n",
  762. param, I8042_RET_CTL_TEST);
  763. msleep(50);
  764. } while (i++ < 5);
  765. #ifdef CONFIG_X86
  766. /*
  767. * On x86, we don't fail entire i8042 initialization if controller
  768. * reset fails in hopes that keyboard port will still be functional
  769. * and user will still get a working keyboard. This is especially
  770. * important on netbooks. On other arches we trust hardware more.
  771. */
  772. pr_info("giving up on controller selftest, continuing anyway...\n");
  773. return 0;
  774. #else
  775. pr_err("i8042 controller selftest failed\n");
  776. return -EIO;
  777. #endif
  778. }
  779. /*
  780. * i8042_controller init initializes the i8042 controller, and,
  781. * most importantly, sets it into non-xlated mode if that's
  782. * desired.
  783. */
  784. static int i8042_controller_init(void)
  785. {
  786. unsigned long flags;
  787. int n = 0;
  788. unsigned char ctr[2];
  789. /*
  790. * Save the CTR for restore on unload / reboot.
  791. */
  792. do {
  793. if (n >= 10) {
  794. pr_err("Unable to get stable CTR read\n");
  795. return -EIO;
  796. }
  797. if (n != 0)
  798. udelay(50);
  799. if (i8042_command(&ctr[n++ % 2], I8042_CMD_CTL_RCTR)) {
  800. pr_err("Can't read CTR while initializing i8042\n");
  801. return -EIO;
  802. }
  803. } while (n < 2 || ctr[0] != ctr[1]);
  804. i8042_initial_ctr = i8042_ctr = ctr[0];
  805. /*
  806. * Disable the keyboard interface and interrupt.
  807. */
  808. i8042_ctr |= I8042_CTR_KBDDIS;
  809. i8042_ctr &= ~I8042_CTR_KBDINT;
  810. /*
  811. * Handle keylock.
  812. */
  813. spin_lock_irqsave(&i8042_lock, flags);
  814. if (~i8042_read_status() & I8042_STR_KEYLOCK) {
  815. if (i8042_unlock)
  816. i8042_ctr |= I8042_CTR_IGNKEYLOCK;
  817. else
  818. pr_warn("Warning: Keylock active\n");
  819. }
  820. spin_unlock_irqrestore(&i8042_lock, flags);
  821. /*
  822. * If the chip is configured into nontranslated mode by the BIOS, don't
  823. * bother enabling translating and be happy.
  824. */
  825. if (~i8042_ctr & I8042_CTR_XLATE)
  826. i8042_direct = true;
  827. /*
  828. * Set nontranslated mode for the kbd interface if requested by an option.
  829. * After this the kbd interface becomes a simple serial in/out, like the aux
  830. * interface is. We don't do this by default, since it can confuse notebook
  831. * BIOSes.
  832. */
  833. if (i8042_direct)
  834. i8042_ctr &= ~I8042_CTR_XLATE;
  835. /*
  836. * Write CTR back.
  837. */
  838. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  839. pr_err("Can't write CTR while initializing i8042\n");
  840. return -EIO;
  841. }
  842. /*
  843. * Flush whatever accumulated while we were disabling keyboard port.
  844. */
  845. i8042_flush();
  846. return 0;
  847. }
  848. /*
  849. * Reset the controller and reset CRT to the original value set by BIOS.
  850. */
  851. static void i8042_controller_reset(bool s2r_wants_reset)
  852. {
  853. i8042_flush();
  854. /*
  855. * Disable both KBD and AUX interfaces so they don't get in the way
  856. */
  857. i8042_ctr |= I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
  858. i8042_ctr &= ~(I8042_CTR_KBDINT | I8042_CTR_AUXINT);
  859. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR))
  860. pr_warn("Can't write CTR while resetting\n");
  861. /*
  862. * Disable MUX mode if present.
  863. */
  864. if (i8042_mux_present)
  865. i8042_set_mux_mode(false, NULL);
  866. /*
  867. * Reset the controller if requested.
  868. */
  869. if (i8042_reset == I8042_RESET_ALWAYS ||
  870. (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
  871. i8042_controller_selftest();
  872. }
  873. /*
  874. * Restore the original control register setting.
  875. */
  876. if (i8042_command(&i8042_initial_ctr, I8042_CMD_CTL_WCTR))
  877. pr_warn("Can't restore CTR\n");
  878. }
  879. /*
  880. * i8042_panic_blink() will turn the keyboard LEDs on or off and is called
  881. * when kernel panics. Flashing LEDs is useful for users running X who may
  882. * not see the console and will help distinguishing panics from "real"
  883. * lockups.
  884. *
  885. * Note that DELAY has a limit of 10ms so we will not get stuck here
  886. * waiting for KBC to free up even if KBD interrupt is off
  887. */
  888. #define DELAY do { mdelay(1); if (++delay > 10) return delay; } while(0)
  889. static long i8042_panic_blink(int state)
  890. {
  891. long delay = 0;
  892. char led;
  893. led = (state) ? 0x01 | 0x04 : 0;
  894. while (i8042_read_status() & I8042_STR_IBF)
  895. DELAY;
  896. dbg("%02x -> i8042 (panic blink)\n", 0xed);
  897. i8042_suppress_kbd_ack = 2;
  898. i8042_write_data(0xed); /* set leds */
  899. DELAY;
  900. while (i8042_read_status() & I8042_STR_IBF)
  901. DELAY;
  902. DELAY;
  903. dbg("%02x -> i8042 (panic blink)\n", led);
  904. i8042_write_data(led);
  905. DELAY;
  906. return delay;
  907. }
  908. #undef DELAY
  909. #ifdef CONFIG_X86
  910. static void i8042_dritek_enable(void)
  911. {
  912. unsigned char param = 0x90;
  913. int error;
  914. error = i8042_command(&param, 0x1059);
  915. if (error)
  916. pr_warn("Failed to enable DRITEK extension: %d\n", error);
  917. }
  918. #endif
  919. #ifdef CONFIG_PM
  920. /*
  921. * Here we try to reset everything back to a state we had
  922. * before suspending.
  923. */
  924. static int i8042_controller_resume(bool s2r_wants_reset)
  925. {
  926. int error;
  927. error = i8042_controller_check();
  928. if (error)
  929. return error;
  930. if (i8042_reset == I8042_RESET_ALWAYS ||
  931. (i8042_reset == I8042_RESET_ON_S2RAM && s2r_wants_reset)) {
  932. error = i8042_controller_selftest();
  933. if (error)
  934. return error;
  935. }
  936. /*
  937. * Restore original CTR value and disable all ports
  938. */
  939. i8042_ctr = i8042_initial_ctr;
  940. if (i8042_direct)
  941. i8042_ctr &= ~I8042_CTR_XLATE;
  942. i8042_ctr |= I8042_CTR_AUXDIS | I8042_CTR_KBDDIS;
  943. i8042_ctr &= ~(I8042_CTR_AUXINT | I8042_CTR_KBDINT);
  944. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  945. pr_warn("Can't write CTR to resume, retrying...\n");
  946. msleep(50);
  947. if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
  948. pr_err("CTR write retry failed\n");
  949. return -EIO;
  950. }
  951. }
  952. #ifdef CONFIG_X86
  953. if (i8042_dritek)
  954. i8042_dritek_enable();
  955. #endif
  956. if (i8042_mux_present) {
  957. if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports())
  958. pr_warn("failed to resume active multiplexor, mouse won't work\n");
  959. } else if (i8042_ports[I8042_AUX_PORT_NO].serio)
  960. i8042_enable_aux_port();
  961. if (i8042_ports[I8042_KBD_PORT_NO].serio)
  962. i8042_enable_kbd_port();
  963. i8042_interrupt(0, NULL);
  964. return 0;
  965. }
  966. /*
  967. * Here we try to restore the original BIOS settings to avoid
  968. * upsetting it.
  969. */
  970. static int i8042_pm_suspend(struct device *dev)
  971. {
  972. int i;
  973. if (pm_suspend_via_firmware())
  974. i8042_controller_reset(true);
  975. /* Set up serio interrupts for system wakeup. */
  976. for (i = 0; i < I8042_NUM_PORTS; i++) {
  977. struct serio *serio = i8042_ports[i].serio;
  978. if (serio && device_may_wakeup(&serio->dev))
  979. enable_irq_wake(i8042_ports[i].irq);
  980. }
  981. return 0;
  982. }
  983. static int i8042_pm_resume_noirq(struct device *dev)
  984. {
  985. if (!pm_resume_via_firmware())
  986. i8042_interrupt(0, NULL);
  987. return 0;
  988. }
  989. static int i8042_pm_resume(struct device *dev)
  990. {
  991. bool want_reset;
  992. int i;
  993. for (i = 0; i < I8042_NUM_PORTS; i++) {
  994. struct serio *serio = i8042_ports[i].serio;
  995. if (serio && device_may_wakeup(&serio->dev))
  996. disable_irq_wake(i8042_ports[i].irq);
  997. }
  998. /*
  999. * If platform firmware was not going to be involved in suspend, we did
  1000. * not restore the controller state to whatever it had been at boot
  1001. * time, so we do not need to do anything.
  1002. */
  1003. if (!pm_suspend_via_firmware())
  1004. return 0;
  1005. /*
  1006. * We only need to reset the controller if we are resuming after handing
  1007. * off control to the platform firmware, otherwise we can simply restore
  1008. * the mode.
  1009. */
  1010. want_reset = pm_resume_via_firmware();
  1011. return i8042_controller_resume(want_reset);
  1012. }
  1013. static int i8042_pm_thaw(struct device *dev)
  1014. {
  1015. i8042_interrupt(0, NULL);
  1016. return 0;
  1017. }
  1018. static int i8042_pm_reset(struct device *dev)
  1019. {
  1020. i8042_controller_reset(false);
  1021. return 0;
  1022. }
  1023. static int i8042_pm_restore(struct device *dev)
  1024. {
  1025. return i8042_controller_resume(false);
  1026. }
  1027. static const struct dev_pm_ops i8042_pm_ops = {
  1028. .suspend = i8042_pm_suspend,
  1029. .resume_noirq = i8042_pm_resume_noirq,
  1030. .resume = i8042_pm_resume,
  1031. .thaw = i8042_pm_thaw,
  1032. .poweroff = i8042_pm_reset,
  1033. .restore = i8042_pm_restore,
  1034. };
  1035. #endif /* CONFIG_PM */
  1036. /*
  1037. * We need to reset the 8042 back to original mode on system shutdown,
  1038. * because otherwise BIOSes will be confused.
  1039. */
  1040. static void i8042_shutdown(struct platform_device *dev)
  1041. {
  1042. i8042_controller_reset(false);
  1043. }
  1044. static int __init i8042_create_kbd_port(void)
  1045. {
  1046. struct serio *serio;
  1047. struct i8042_port *port = &i8042_ports[I8042_KBD_PORT_NO];
  1048. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  1049. if (!serio)
  1050. return -ENOMEM;
  1051. serio->id.type = i8042_direct ? SERIO_8042 : SERIO_8042_XL;
  1052. serio->write = i8042_dumbkbd ? NULL : i8042_kbd_write;
  1053. serio->start = i8042_start;
  1054. serio->stop = i8042_stop;
  1055. serio->close = i8042_port_close;
  1056. serio->ps2_cmd_mutex = &i8042_mutex;
  1057. serio->port_data = port;
  1058. serio->dev.parent = &i8042_platform_device->dev;
  1059. strlcpy(serio->name, "i8042 KBD port", sizeof(serio->name));
  1060. strlcpy(serio->phys, I8042_KBD_PHYS_DESC, sizeof(serio->phys));
  1061. strlcpy(serio->firmware_id, i8042_kbd_firmware_id,
  1062. sizeof(serio->firmware_id));
  1063. port->serio = serio;
  1064. port->irq = I8042_KBD_IRQ;
  1065. return 0;
  1066. }
  1067. static int __init i8042_create_aux_port(int idx)
  1068. {
  1069. struct serio *serio;
  1070. int port_no = idx < 0 ? I8042_AUX_PORT_NO : I8042_MUX_PORT_NO + idx;
  1071. struct i8042_port *port = &i8042_ports[port_no];
  1072. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  1073. if (!serio)
  1074. return -ENOMEM;
  1075. serio->id.type = SERIO_8042;
  1076. serio->write = i8042_aux_write;
  1077. serio->start = i8042_start;
  1078. serio->stop = i8042_stop;
  1079. serio->ps2_cmd_mutex = &i8042_mutex;
  1080. serio->port_data = port;
  1081. serio->dev.parent = &i8042_platform_device->dev;
  1082. if (idx < 0) {
  1083. strlcpy(serio->name, "i8042 AUX port", sizeof(serio->name));
  1084. strlcpy(serio->phys, I8042_AUX_PHYS_DESC, sizeof(serio->phys));
  1085. strlcpy(serio->firmware_id, i8042_aux_firmware_id,
  1086. sizeof(serio->firmware_id));
  1087. serio->close = i8042_port_close;
  1088. } else {
  1089. snprintf(serio->name, sizeof(serio->name), "i8042 AUX%d port", idx);
  1090. snprintf(serio->phys, sizeof(serio->phys), I8042_MUX_PHYS_DESC, idx + 1);
  1091. strlcpy(serio->firmware_id, i8042_aux_firmware_id,
  1092. sizeof(serio->firmware_id));
  1093. }
  1094. port->serio = serio;
  1095. port->mux = idx;
  1096. port->irq = I8042_AUX_IRQ;
  1097. return 0;
  1098. }
  1099. static void __init i8042_free_kbd_port(void)
  1100. {
  1101. kfree(i8042_ports[I8042_KBD_PORT_NO].serio);
  1102. i8042_ports[I8042_KBD_PORT_NO].serio = NULL;
  1103. }
  1104. static void __init i8042_free_aux_ports(void)
  1105. {
  1106. int i;
  1107. for (i = I8042_AUX_PORT_NO; i < I8042_NUM_PORTS; i++) {
  1108. kfree(i8042_ports[i].serio);
  1109. i8042_ports[i].serio = NULL;
  1110. }
  1111. }
  1112. static void __init i8042_register_ports(void)
  1113. {
  1114. int i;
  1115. for (i = 0; i < I8042_NUM_PORTS; i++) {
  1116. struct serio *serio = i8042_ports[i].serio;
  1117. if (!serio)
  1118. continue;
  1119. printk(KERN_INFO "serio: %s at %#lx,%#lx irq %d\n",
  1120. serio->name,
  1121. (unsigned long) I8042_DATA_REG,
  1122. (unsigned long) I8042_COMMAND_REG,
  1123. i8042_ports[i].irq);
  1124. serio_register_port(serio);
  1125. device_set_wakeup_capable(&serio->dev, true);
  1126. /*
  1127. * On platforms using suspend-to-idle, allow the keyboard to
  1128. * wake up the system from sleep by enabling keyboard wakeups
  1129. * by default. This is consistent with keyboard wakeup
  1130. * behavior on many platforms using suspend-to-RAM (ACPI S3)
  1131. * by default.
  1132. */
  1133. if (pm_suspend_via_s2idle() && i == I8042_KBD_PORT_NO)
  1134. device_set_wakeup_enable(&serio->dev, true);
  1135. }
  1136. }
  1137. static void i8042_unregister_ports(void)
  1138. {
  1139. int i;
  1140. for (i = 0; i < I8042_NUM_PORTS; i++) {
  1141. if (i8042_ports[i].serio) {
  1142. serio_unregister_port(i8042_ports[i].serio);
  1143. i8042_ports[i].serio = NULL;
  1144. }
  1145. }
  1146. }
  1147. static void i8042_free_irqs(void)
  1148. {
  1149. if (i8042_aux_irq_registered)
  1150. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  1151. if (i8042_kbd_irq_registered)
  1152. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  1153. i8042_aux_irq_registered = i8042_kbd_irq_registered = false;
  1154. }
  1155. static int __init i8042_setup_aux(void)
  1156. {
  1157. int (*aux_enable)(void);
  1158. int error;
  1159. int i;
  1160. if (i8042_check_aux())
  1161. return -ENODEV;
  1162. if (i8042_nomux || i8042_check_mux()) {
  1163. error = i8042_create_aux_port(-1);
  1164. if (error)
  1165. goto err_free_ports;
  1166. aux_enable = i8042_enable_aux_port;
  1167. } else {
  1168. for (i = 0; i < I8042_NUM_MUX_PORTS; i++) {
  1169. error = i8042_create_aux_port(i);
  1170. if (error)
  1171. goto err_free_ports;
  1172. }
  1173. aux_enable = i8042_enable_mux_ports;
  1174. }
  1175. error = request_irq(I8042_AUX_IRQ, i8042_interrupt, IRQF_SHARED,
  1176. "i8042", i8042_platform_device);
  1177. if (error)
  1178. goto err_free_ports;
  1179. if (aux_enable())
  1180. goto err_free_irq;
  1181. i8042_aux_irq_registered = true;
  1182. return 0;
  1183. err_free_irq:
  1184. free_irq(I8042_AUX_IRQ, i8042_platform_device);
  1185. err_free_ports:
  1186. i8042_free_aux_ports();
  1187. return error;
  1188. }
  1189. static int __init i8042_setup_kbd(void)
  1190. {
  1191. int error;
  1192. error = i8042_create_kbd_port();
  1193. if (error)
  1194. return error;
  1195. error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
  1196. "i8042", i8042_platform_device);
  1197. if (error)
  1198. goto err_free_port;
  1199. error = i8042_enable_kbd_port();
  1200. if (error)
  1201. goto err_free_irq;
  1202. i8042_kbd_irq_registered = true;
  1203. return 0;
  1204. err_free_irq:
  1205. free_irq(I8042_KBD_IRQ, i8042_platform_device);
  1206. err_free_port:
  1207. i8042_free_kbd_port();
  1208. return error;
  1209. }
  1210. static int i8042_kbd_bind_notifier(struct notifier_block *nb,
  1211. unsigned long action, void *data)
  1212. {
  1213. struct device *dev = data;
  1214. struct serio *serio = to_serio_port(dev);
  1215. struct i8042_port *port = serio->port_data;
  1216. if (serio != i8042_ports[I8042_KBD_PORT_NO].serio)
  1217. return 0;
  1218. switch (action) {
  1219. case BUS_NOTIFY_BOUND_DRIVER:
  1220. port->driver_bound = true;
  1221. break;
  1222. case BUS_NOTIFY_UNBIND_DRIVER:
  1223. port->driver_bound = false;
  1224. break;
  1225. }
  1226. return 0;
  1227. }
  1228. static int __init i8042_probe(struct platform_device *dev)
  1229. {
  1230. int error;
  1231. i8042_platform_device = dev;
  1232. if (i8042_reset == I8042_RESET_ALWAYS) {
  1233. error = i8042_controller_selftest();
  1234. if (error)
  1235. return error;
  1236. }
  1237. error = i8042_controller_init();
  1238. if (error)
  1239. return error;
  1240. #ifdef CONFIG_X86
  1241. if (i8042_dritek)
  1242. i8042_dritek_enable();
  1243. #endif
  1244. if (!i8042_noaux) {
  1245. error = i8042_setup_aux();
  1246. if (error && error != -ENODEV && error != -EBUSY)
  1247. goto out_fail;
  1248. }
  1249. if (!i8042_nokbd) {
  1250. error = i8042_setup_kbd();
  1251. if (error)
  1252. goto out_fail;
  1253. }
  1254. /*
  1255. * Ok, everything is ready, let's register all serio ports
  1256. */
  1257. i8042_register_ports();
  1258. return 0;
  1259. out_fail:
  1260. i8042_free_aux_ports(); /* in case KBD failed but AUX not */
  1261. i8042_free_irqs();
  1262. i8042_controller_reset(false);
  1263. i8042_platform_device = NULL;
  1264. return error;
  1265. }
  1266. static int i8042_remove(struct platform_device *dev)
  1267. {
  1268. i8042_unregister_ports();
  1269. i8042_free_irqs();
  1270. i8042_controller_reset(false);
  1271. i8042_platform_device = NULL;
  1272. return 0;
  1273. }
  1274. static struct platform_driver i8042_driver = {
  1275. .driver = {
  1276. .name = "i8042",
  1277. #ifdef CONFIG_PM
  1278. .pm = &i8042_pm_ops,
  1279. #endif
  1280. },
  1281. .remove = i8042_remove,
  1282. .shutdown = i8042_shutdown,
  1283. };
  1284. static struct notifier_block i8042_kbd_bind_notifier_block = {
  1285. .notifier_call = i8042_kbd_bind_notifier,
  1286. };
  1287. static int __init i8042_init(void)
  1288. {
  1289. struct platform_device *pdev;
  1290. int err;
  1291. dbg_init();
  1292. err = i8042_platform_init();
  1293. if (err)
  1294. return err;
  1295. err = i8042_controller_check();
  1296. if (err)
  1297. goto err_platform_exit;
  1298. pdev = platform_create_bundle(&i8042_driver, i8042_probe, NULL, 0, NULL, 0);
  1299. if (IS_ERR(pdev)) {
  1300. err = PTR_ERR(pdev);
  1301. goto err_platform_exit;
  1302. }
  1303. bus_register_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
  1304. panic_blink = i8042_panic_blink;
  1305. return 0;
  1306. err_platform_exit:
  1307. i8042_platform_exit();
  1308. return err;
  1309. }
  1310. static void __exit i8042_exit(void)
  1311. {
  1312. platform_device_unregister(i8042_platform_device);
  1313. platform_driver_unregister(&i8042_driver);
  1314. i8042_platform_exit();
  1315. bus_unregister_notifier(&serio_bus, &i8042_kbd_bind_notifier_block);
  1316. panic_blink = NULL;
  1317. }
  1318. module_init(i8042_init);
  1319. module_exit(i8042_exit);