i8042.c 34 KB

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