i8042.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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);