i8042.c 38 KB

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