phytium-i8042.c 37 KB

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