i8042.c 34 KB

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