i8042.c 37 KB

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