i8042.c 38 KB

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