i8042.c 33 KB

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