i8042.c 38 KB

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