i8042.c 37 KB

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