i2c-hid-core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. /*
  2. * HID over I2C protocol implementation
  3. *
  4. * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  5. * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
  6. * Copyright (c) 2012 Red Hat, Inc
  7. *
  8. * This code is partly based on "USB HID support for Linux":
  9. *
  10. * Copyright (c) 1999 Andreas Gal
  11. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  12. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  13. * Copyright (c) 2007-2008 Oliver Neukum
  14. * Copyright (c) 2006-2010 Jiri Kosina
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file COPYING in the main directory of this archive for
  18. * more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/input.h>
  24. #include <linux/irq.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/pm.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/device.h>
  30. #include <linux/wait.h>
  31. #include <linux/err.h>
  32. #include <linux/string.h>
  33. #include <linux/list.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/kernel.h>
  36. #include <linux/hid.h>
  37. #include <linux/mutex.h>
  38. #include <linux/acpi.h>
  39. #include <linux/of.h>
  40. #include <linux/regulator/consumer.h>
  41. #include <linux/platform_data/i2c-hid.h>
  42. #include "../hid-ids.h"
  43. #include "i2c-hid.h"
  44. /* quirks to control the device */
  45. #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
  46. #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
  47. #define I2C_HID_QUIRK_NO_RUNTIME_PM BIT(2)
  48. #define I2C_HID_QUIRK_DELAY_AFTER_SLEEP BIT(3)
  49. #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
  50. #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
  51. #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
  52. /* flags */
  53. #define I2C_HID_STARTED 0
  54. #define I2C_HID_RESET_PENDING 1
  55. #define I2C_HID_READ_PENDING 2
  56. #define I2C_HID_PWR_ON 0x00
  57. #define I2C_HID_PWR_SLEEP 0x01
  58. /* debug option */
  59. static bool debug;
  60. module_param(debug, bool, 0444);
  61. MODULE_PARM_DESC(debug, "print a lot of debug information");
  62. #define i2c_hid_dbg(ihid, fmt, arg...) \
  63. do { \
  64. if (debug) \
  65. dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
  66. } while (0)
  67. struct i2c_hid_desc {
  68. __le16 wHIDDescLength;
  69. __le16 bcdVersion;
  70. __le16 wReportDescLength;
  71. __le16 wReportDescRegister;
  72. __le16 wInputRegister;
  73. __le16 wMaxInputLength;
  74. __le16 wOutputRegister;
  75. __le16 wMaxOutputLength;
  76. __le16 wCommandRegister;
  77. __le16 wDataRegister;
  78. __le16 wVendorID;
  79. __le16 wProductID;
  80. __le16 wVersionID;
  81. __le32 reserved;
  82. } __packed;
  83. struct i2c_hid_cmd {
  84. unsigned int registerIndex;
  85. __u8 opcode;
  86. unsigned int length;
  87. bool wait;
  88. };
  89. union command {
  90. u8 data[0];
  91. struct cmd {
  92. __le16 reg;
  93. __u8 reportTypeID;
  94. __u8 opcode;
  95. } __packed c;
  96. };
  97. #define I2C_HID_CMD(opcode_) \
  98. .opcode = opcode_, .length = 4, \
  99. .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
  100. /* fetch HID descriptor */
  101. static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
  102. /* fetch report descriptors */
  103. static const struct i2c_hid_cmd hid_report_descr_cmd = {
  104. .registerIndex = offsetof(struct i2c_hid_desc,
  105. wReportDescRegister),
  106. .opcode = 0x00,
  107. .length = 2 };
  108. /* commands */
  109. static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
  110. .wait = true };
  111. static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
  112. static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
  113. static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
  114. static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
  115. /*
  116. * These definitions are not used here, but are defined by the spec.
  117. * Keeping them here for documentation purposes.
  118. *
  119. * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
  120. * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
  121. * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
  122. * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
  123. */
  124. /* The main device structure */
  125. struct i2c_hid {
  126. struct i2c_client *client; /* i2c client */
  127. struct hid_device *hid; /* pointer to corresponding HID dev */
  128. union {
  129. __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
  130. struct i2c_hid_desc hdesc; /* the HID Descriptor */
  131. };
  132. __le16 wHIDDescRegister; /* location of the i2c
  133. * register of the HID
  134. * descriptor. */
  135. unsigned int bufsize; /* i2c buffer size */
  136. u8 *inbuf; /* Input buffer */
  137. u8 *rawbuf; /* Raw Input buffer */
  138. u8 *cmdbuf; /* Command buffer */
  139. u8 *argsbuf; /* Command arguments buffer */
  140. unsigned long flags; /* device flags */
  141. unsigned long quirks; /* Various quirks */
  142. wait_queue_head_t wait; /* For waiting the interrupt */
  143. struct i2c_hid_platform_data pdata;
  144. bool irq_wake_enabled;
  145. struct mutex reset_lock;
  146. unsigned long sleep_delay;
  147. };
  148. static const struct i2c_hid_quirks {
  149. __u16 idVendor;
  150. __u16 idProduct;
  151. __u32 quirks;
  152. } i2c_hid_quirks[] = {
  153. { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
  154. I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
  155. { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
  156. I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
  157. { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
  158. I2C_HID_QUIRK_NO_IRQ_AFTER_RESET |
  159. I2C_HID_QUIRK_NO_RUNTIME_PM },
  160. { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_4B33,
  161. I2C_HID_QUIRK_DELAY_AFTER_SLEEP },
  162. { USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_8001,
  163. I2C_HID_QUIRK_NO_RUNTIME_PM },
  164. { USB_VENDOR_ID_ELAN, HID_ANY_ID,
  165. I2C_HID_QUIRK_BOGUS_IRQ },
  166. { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
  167. I2C_HID_QUIRK_RESET_ON_RESUME },
  168. { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
  169. I2C_HID_QUIRK_RESET_ON_RESUME },
  170. { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
  171. I2C_HID_QUIRK_BAD_INPUT_SIZE },
  172. { 0, 0 }
  173. };
  174. /*
  175. * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
  176. * @idVendor: the 16-bit vendor ID
  177. * @idProduct: the 16-bit product ID
  178. *
  179. * Returns: a u32 quirks value.
  180. */
  181. static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
  182. {
  183. u32 quirks = 0;
  184. int n;
  185. for (n = 0; i2c_hid_quirks[n].idVendor; n++)
  186. if (i2c_hid_quirks[n].idVendor == idVendor &&
  187. (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
  188. i2c_hid_quirks[n].idProduct == idProduct))
  189. quirks = i2c_hid_quirks[n].quirks;
  190. return quirks;
  191. }
  192. static int __i2c_hid_command(struct i2c_client *client,
  193. const struct i2c_hid_cmd *command, u8 reportID,
  194. u8 reportType, u8 *args, int args_len,
  195. unsigned char *buf_recv, int data_len)
  196. {
  197. struct i2c_hid *ihid = i2c_get_clientdata(client);
  198. union command *cmd = (union command *)ihid->cmdbuf;
  199. int ret;
  200. struct i2c_msg msg[2];
  201. int msg_num = 1;
  202. int length = command->length;
  203. bool wait = command->wait;
  204. unsigned int registerIndex = command->registerIndex;
  205. /* special case for hid_descr_cmd */
  206. if (command == &hid_descr_cmd) {
  207. cmd->c.reg = ihid->wHIDDescRegister;
  208. } else {
  209. cmd->data[0] = ihid->hdesc_buffer[registerIndex];
  210. cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
  211. }
  212. if (length > 2) {
  213. cmd->c.opcode = command->opcode;
  214. cmd->c.reportTypeID = reportID | reportType << 4;
  215. }
  216. memcpy(cmd->data + length, args, args_len);
  217. length += args_len;
  218. i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
  219. msg[0].addr = client->addr;
  220. msg[0].flags = client->flags & I2C_M_TEN;
  221. msg[0].len = length;
  222. msg[0].buf = cmd->data;
  223. if (data_len > 0) {
  224. msg[1].addr = client->addr;
  225. msg[1].flags = client->flags & I2C_M_TEN;
  226. msg[1].flags |= I2C_M_RD;
  227. msg[1].len = data_len;
  228. msg[1].buf = buf_recv;
  229. msg_num = 2;
  230. set_bit(I2C_HID_READ_PENDING, &ihid->flags);
  231. }
  232. if (wait)
  233. set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
  234. ret = i2c_transfer(client->adapter, msg, msg_num);
  235. if (data_len > 0)
  236. clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
  237. if (ret != msg_num)
  238. return ret < 0 ? ret : -EIO;
  239. ret = 0;
  240. if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
  241. msleep(100);
  242. } else if (wait) {
  243. i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
  244. if (!wait_event_timeout(ihid->wait,
  245. !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
  246. msecs_to_jiffies(5000)))
  247. ret = -ENODATA;
  248. i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
  249. }
  250. return ret;
  251. }
  252. static int i2c_hid_command(struct i2c_client *client,
  253. const struct i2c_hid_cmd *command,
  254. unsigned char *buf_recv, int data_len)
  255. {
  256. return __i2c_hid_command(client, command, 0, 0, NULL, 0,
  257. buf_recv, data_len);
  258. }
  259. static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
  260. u8 reportID, unsigned char *buf_recv, int data_len)
  261. {
  262. struct i2c_hid *ihid = i2c_get_clientdata(client);
  263. u8 args[3];
  264. int ret;
  265. int args_len = 0;
  266. u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
  267. i2c_hid_dbg(ihid, "%s\n", __func__);
  268. if (reportID >= 0x0F) {
  269. args[args_len++] = reportID;
  270. reportID = 0x0F;
  271. }
  272. args[args_len++] = readRegister & 0xFF;
  273. args[args_len++] = readRegister >> 8;
  274. ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
  275. reportType, args, args_len, buf_recv, data_len);
  276. if (ret) {
  277. dev_err(&client->dev,
  278. "failed to retrieve report from device.\n");
  279. return ret;
  280. }
  281. return 0;
  282. }
  283. /**
  284. * i2c_hid_set_or_send_report: forward an incoming report to the device
  285. * @client: the i2c_client of the device
  286. * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
  287. * @reportID: the report ID
  288. * @buf: the actual data to transfer, without the report ID
  289. * @len: size of buf
  290. * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
  291. */
  292. static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
  293. u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
  294. {
  295. struct i2c_hid *ihid = i2c_get_clientdata(client);
  296. u8 *args = ihid->argsbuf;
  297. const struct i2c_hid_cmd *hidcmd;
  298. int ret;
  299. u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
  300. u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
  301. u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
  302. u16 size;
  303. int args_len;
  304. int index = 0;
  305. i2c_hid_dbg(ihid, "%s\n", __func__);
  306. if (data_len > ihid->bufsize)
  307. return -EINVAL;
  308. size = 2 /* size */ +
  309. (reportID ? 1 : 0) /* reportID */ +
  310. data_len /* buf */;
  311. args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
  312. 2 /* dataRegister */ +
  313. size /* args */;
  314. if (!use_data && maxOutputLength == 0)
  315. return -ENOSYS;
  316. if (reportID >= 0x0F) {
  317. args[index++] = reportID;
  318. reportID = 0x0F;
  319. }
  320. /*
  321. * use the data register for feature reports or if the device does not
  322. * support the output register
  323. */
  324. if (use_data) {
  325. args[index++] = dataRegister & 0xFF;
  326. args[index++] = dataRegister >> 8;
  327. hidcmd = &hid_set_report_cmd;
  328. } else {
  329. args[index++] = outputRegister & 0xFF;
  330. args[index++] = outputRegister >> 8;
  331. hidcmd = &hid_no_cmd;
  332. }
  333. args[index++] = size & 0xFF;
  334. args[index++] = size >> 8;
  335. if (reportID)
  336. args[index++] = reportID;
  337. memcpy(&args[index], buf, data_len);
  338. ret = __i2c_hid_command(client, hidcmd, reportID,
  339. reportType, args, args_len, NULL, 0);
  340. if (ret) {
  341. dev_err(&client->dev, "failed to set a report to device.\n");
  342. return ret;
  343. }
  344. return data_len;
  345. }
  346. static int i2c_hid_set_power(struct i2c_client *client, int power_state)
  347. {
  348. struct i2c_hid *ihid = i2c_get_clientdata(client);
  349. int ret;
  350. unsigned long now, delay;
  351. i2c_hid_dbg(ihid, "%s\n", __func__);
  352. /*
  353. * Some devices require to send a command to wakeup before power on.
  354. * The call will get a return value (EREMOTEIO) but device will be
  355. * triggered and activated. After that, it goes like a normal device.
  356. */
  357. if (power_state == I2C_HID_PWR_ON &&
  358. ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
  359. ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
  360. /* Device was already activated */
  361. if (!ret)
  362. goto set_pwr_exit;
  363. }
  364. if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
  365. power_state == I2C_HID_PWR_ON) {
  366. now = jiffies;
  367. if (time_after(ihid->sleep_delay, now)) {
  368. delay = jiffies_to_usecs(ihid->sleep_delay - now);
  369. usleep_range(delay, delay + 1);
  370. }
  371. }
  372. ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
  373. 0, NULL, 0, NULL, 0);
  374. if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
  375. power_state == I2C_HID_PWR_SLEEP)
  376. ihid->sleep_delay = jiffies + msecs_to_jiffies(20);
  377. if (ret)
  378. dev_err(&client->dev, "failed to change power setting.\n");
  379. set_pwr_exit:
  380. /*
  381. * The HID over I2C specification states that if a DEVICE needs time
  382. * after the PWR_ON request, it should utilise CLOCK stretching.
  383. * However, it has been observered that the Windows driver provides a
  384. * 1ms sleep between the PWR_ON and RESET requests.
  385. * According to Goodix Windows even waits 60 ms after (other?)
  386. * PWR_ON requests. Testing has confirmed that several devices
  387. * will not work properly without a delay after a PWR_ON request.
  388. */
  389. if (!ret && power_state == I2C_HID_PWR_ON)
  390. msleep(60);
  391. return ret;
  392. }
  393. static int i2c_hid_hwreset(struct i2c_client *client)
  394. {
  395. struct i2c_hid *ihid = i2c_get_clientdata(client);
  396. int ret;
  397. i2c_hid_dbg(ihid, "%s\n", __func__);
  398. /*
  399. * This prevents sending feature reports while the device is
  400. * being reset. Otherwise we may lose the reset complete
  401. * interrupt.
  402. */
  403. mutex_lock(&ihid->reset_lock);
  404. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  405. if (ret)
  406. goto out_unlock;
  407. i2c_hid_dbg(ihid, "resetting...\n");
  408. ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
  409. if (ret) {
  410. dev_err(&client->dev, "failed to reset device.\n");
  411. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  412. }
  413. out_unlock:
  414. mutex_unlock(&ihid->reset_lock);
  415. return ret;
  416. }
  417. static void i2c_hid_get_input(struct i2c_hid *ihid)
  418. {
  419. int ret;
  420. u32 ret_size;
  421. int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
  422. if (size > ihid->bufsize)
  423. size = ihid->bufsize;
  424. ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
  425. if (ret != size) {
  426. if (ret < 0)
  427. return;
  428. dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
  429. __func__, ret, size);
  430. return;
  431. }
  432. ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
  433. if (!ret_size) {
  434. /* host or device initiated RESET completed */
  435. if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
  436. wake_up(&ihid->wait);
  437. return;
  438. }
  439. if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
  440. dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
  441. "there's no data\n", __func__);
  442. return;
  443. }
  444. if ((ret_size > size) || (ret_size < 2)) {
  445. if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
  446. ihid->inbuf[0] = size & 0xff;
  447. ihid->inbuf[1] = size >> 8;
  448. ret_size = size;
  449. } else {
  450. dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
  451. __func__, size, ret_size);
  452. return;
  453. }
  454. }
  455. i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
  456. if (test_bit(I2C_HID_STARTED, &ihid->flags))
  457. hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
  458. ret_size - 2, 1);
  459. return;
  460. }
  461. static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
  462. {
  463. struct i2c_hid *ihid = dev_id;
  464. if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
  465. return IRQ_HANDLED;
  466. i2c_hid_get_input(ihid);
  467. return IRQ_HANDLED;
  468. }
  469. static int i2c_hid_get_report_length(struct hid_report *report)
  470. {
  471. return ((report->size - 1) >> 3) + 1 +
  472. report->device->report_enum[report->type].numbered + 2;
  473. }
  474. /*
  475. * Traverse the supplied list of reports and find the longest
  476. */
  477. static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
  478. unsigned int *max)
  479. {
  480. struct hid_report *report;
  481. unsigned int size;
  482. /* We should not rely on wMaxInputLength, as some devices may set it to
  483. * a wrong length. */
  484. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  485. size = i2c_hid_get_report_length(report);
  486. if (*max < size)
  487. *max = size;
  488. }
  489. }
  490. static void i2c_hid_free_buffers(struct i2c_hid *ihid)
  491. {
  492. kfree(ihid->inbuf);
  493. kfree(ihid->rawbuf);
  494. kfree(ihid->argsbuf);
  495. kfree(ihid->cmdbuf);
  496. ihid->inbuf = NULL;
  497. ihid->rawbuf = NULL;
  498. ihid->cmdbuf = NULL;
  499. ihid->argsbuf = NULL;
  500. ihid->bufsize = 0;
  501. }
  502. static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
  503. {
  504. /* the worst case is computed from the set_report command with a
  505. * reportID > 15 and the maximum report length */
  506. int args_len = sizeof(__u8) + /* ReportID */
  507. sizeof(__u8) + /* optional ReportID byte */
  508. sizeof(__u16) + /* data register */
  509. sizeof(__u16) + /* size of the report */
  510. report_size; /* report */
  511. ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
  512. ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
  513. ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
  514. ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
  515. if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
  516. i2c_hid_free_buffers(ihid);
  517. return -ENOMEM;
  518. }
  519. ihid->bufsize = report_size;
  520. return 0;
  521. }
  522. static int i2c_hid_get_raw_report(struct hid_device *hid,
  523. unsigned char report_number, __u8 *buf, size_t count,
  524. unsigned char report_type)
  525. {
  526. struct i2c_client *client = hid->driver_data;
  527. struct i2c_hid *ihid = i2c_get_clientdata(client);
  528. size_t ret_count, ask_count;
  529. int ret;
  530. if (report_type == HID_OUTPUT_REPORT)
  531. return -EINVAL;
  532. /* +2 bytes to include the size of the reply in the query buffer */
  533. ask_count = min(count + 2, (size_t)ihid->bufsize);
  534. ret = i2c_hid_get_report(client,
  535. report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  536. report_number, ihid->rawbuf, ask_count);
  537. if (ret < 0)
  538. return ret;
  539. ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
  540. if (ret_count <= 2)
  541. return 0;
  542. ret_count = min(ret_count, ask_count);
  543. /* The query buffer contains the size, dropping it in the reply */
  544. count = min(count, ret_count - 2);
  545. memcpy(buf, ihid->rawbuf + 2, count);
  546. return count;
  547. }
  548. static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
  549. size_t count, unsigned char report_type, bool use_data)
  550. {
  551. struct i2c_client *client = hid->driver_data;
  552. struct i2c_hid *ihid = i2c_get_clientdata(client);
  553. int report_id = buf[0];
  554. int ret;
  555. if (report_type == HID_INPUT_REPORT)
  556. return -EINVAL;
  557. mutex_lock(&ihid->reset_lock);
  558. if (report_id) {
  559. buf++;
  560. count--;
  561. }
  562. ret = i2c_hid_set_or_send_report(client,
  563. report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
  564. report_id, buf, count, use_data);
  565. if (report_id && ret >= 0)
  566. ret++; /* add report_id to the number of transfered bytes */
  567. mutex_unlock(&ihid->reset_lock);
  568. return ret;
  569. }
  570. static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
  571. size_t count)
  572. {
  573. return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
  574. false);
  575. }
  576. static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
  577. __u8 *buf, size_t len, unsigned char rtype,
  578. int reqtype)
  579. {
  580. switch (reqtype) {
  581. case HID_REQ_GET_REPORT:
  582. return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
  583. case HID_REQ_SET_REPORT:
  584. if (buf[0] != reportnum)
  585. return -EINVAL;
  586. return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
  587. default:
  588. return -EIO;
  589. }
  590. }
  591. static int i2c_hid_parse(struct hid_device *hid)
  592. {
  593. struct i2c_client *client = hid->driver_data;
  594. struct i2c_hid *ihid = i2c_get_clientdata(client);
  595. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  596. unsigned int rsize;
  597. char *rdesc;
  598. int ret;
  599. int tries = 3;
  600. char *use_override;
  601. i2c_hid_dbg(ihid, "entering %s\n", __func__);
  602. rsize = le16_to_cpu(hdesc->wReportDescLength);
  603. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  604. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  605. return -EINVAL;
  606. }
  607. do {
  608. ret = i2c_hid_hwreset(client);
  609. if (ret)
  610. msleep(1000);
  611. } while (tries-- > 0 && ret);
  612. if (ret)
  613. return ret;
  614. use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
  615. &rsize);
  616. if (use_override) {
  617. rdesc = use_override;
  618. i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
  619. } else {
  620. rdesc = kzalloc(rsize, GFP_KERNEL);
  621. if (!rdesc) {
  622. dbg_hid("couldn't allocate rdesc memory\n");
  623. return -ENOMEM;
  624. }
  625. i2c_hid_dbg(ihid, "asking HID report descriptor\n");
  626. ret = i2c_hid_command(client, &hid_report_descr_cmd,
  627. rdesc, rsize);
  628. if (ret) {
  629. hid_err(hid, "reading report descriptor failed\n");
  630. kfree(rdesc);
  631. return -EIO;
  632. }
  633. }
  634. i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
  635. ret = hid_parse_report(hid, rdesc, rsize);
  636. if (!use_override)
  637. kfree(rdesc);
  638. if (ret) {
  639. dbg_hid("parsing report descriptor failed\n");
  640. return ret;
  641. }
  642. return 0;
  643. }
  644. static int i2c_hid_start(struct hid_device *hid)
  645. {
  646. struct i2c_client *client = hid->driver_data;
  647. struct i2c_hid *ihid = i2c_get_clientdata(client);
  648. int ret;
  649. unsigned int bufsize = HID_MIN_BUFFER_SIZE;
  650. i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
  651. i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
  652. i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
  653. if (bufsize > ihid->bufsize) {
  654. disable_irq(client->irq);
  655. i2c_hid_free_buffers(ihid);
  656. ret = i2c_hid_alloc_buffers(ihid, bufsize);
  657. enable_irq(client->irq);
  658. if (ret)
  659. return ret;
  660. }
  661. return 0;
  662. }
  663. static void i2c_hid_stop(struct hid_device *hid)
  664. {
  665. hid->claimed = 0;
  666. }
  667. static int i2c_hid_open(struct hid_device *hid)
  668. {
  669. struct i2c_client *client = hid->driver_data;
  670. struct i2c_hid *ihid = i2c_get_clientdata(client);
  671. int ret = 0;
  672. ret = pm_runtime_get_sync(&client->dev);
  673. if (ret < 0)
  674. return ret;
  675. set_bit(I2C_HID_STARTED, &ihid->flags);
  676. return 0;
  677. }
  678. static void i2c_hid_close(struct hid_device *hid)
  679. {
  680. struct i2c_client *client = hid->driver_data;
  681. struct i2c_hid *ihid = i2c_get_clientdata(client);
  682. clear_bit(I2C_HID_STARTED, &ihid->flags);
  683. /* Save some power */
  684. pm_runtime_put(&client->dev);
  685. }
  686. static int i2c_hid_power(struct hid_device *hid, int lvl)
  687. {
  688. struct i2c_client *client = hid->driver_data;
  689. struct i2c_hid *ihid = i2c_get_clientdata(client);
  690. i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
  691. switch (lvl) {
  692. case PM_HINT_FULLON:
  693. pm_runtime_get_sync(&client->dev);
  694. break;
  695. case PM_HINT_NORMAL:
  696. pm_runtime_put(&client->dev);
  697. break;
  698. }
  699. return 0;
  700. }
  701. struct hid_ll_driver i2c_hid_ll_driver = {
  702. .parse = i2c_hid_parse,
  703. .start = i2c_hid_start,
  704. .stop = i2c_hid_stop,
  705. .open = i2c_hid_open,
  706. .close = i2c_hid_close,
  707. .power = i2c_hid_power,
  708. .output_report = i2c_hid_output_report,
  709. .raw_request = i2c_hid_raw_request,
  710. };
  711. EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
  712. static int i2c_hid_init_irq(struct i2c_client *client)
  713. {
  714. struct i2c_hid *ihid = i2c_get_clientdata(client);
  715. unsigned long irqflags = 0;
  716. int ret;
  717. dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
  718. if (!irq_get_trigger_type(client->irq))
  719. irqflags = IRQF_TRIGGER_LOW;
  720. ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
  721. irqflags | IRQF_ONESHOT, client->name, ihid);
  722. if (ret < 0) {
  723. dev_warn(&client->dev,
  724. "Could not register for %s interrupt, irq = %d,"
  725. " ret = %d\n",
  726. client->name, client->irq, ret);
  727. return ret;
  728. }
  729. return 0;
  730. }
  731. static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
  732. {
  733. struct i2c_client *client = ihid->client;
  734. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  735. unsigned int dsize;
  736. int ret;
  737. /* i2c hid fetch using a fixed descriptor size (30 bytes) */
  738. if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
  739. i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
  740. ihid->hdesc =
  741. *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
  742. } else {
  743. i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
  744. ret = i2c_hid_command(client, &hid_descr_cmd,
  745. ihid->hdesc_buffer,
  746. sizeof(struct i2c_hid_desc));
  747. if (ret) {
  748. dev_err(&client->dev, "hid_descr_cmd failed\n");
  749. return -ENODEV;
  750. }
  751. }
  752. /* Validate the length of HID descriptor, the 4 first bytes:
  753. * bytes 0-1 -> length
  754. * bytes 2-3 -> bcdVersion (has to be 1.00) */
  755. /* check bcdVersion == 1.0 */
  756. if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
  757. dev_err(&client->dev,
  758. "unexpected HID descriptor bcdVersion (0x%04hx)\n",
  759. le16_to_cpu(hdesc->bcdVersion));
  760. return -ENODEV;
  761. }
  762. /* Descriptor length should be 30 bytes as per the specification */
  763. dsize = le16_to_cpu(hdesc->wHIDDescLength);
  764. if (dsize != sizeof(struct i2c_hid_desc)) {
  765. dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
  766. dsize);
  767. return -ENODEV;
  768. }
  769. i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
  770. return 0;
  771. }
  772. #ifdef CONFIG_ACPI
  773. static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
  774. /*
  775. * The CHPN0001 ACPI device, which is used to describe the Chipone
  776. * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
  777. */
  778. {"CHPN0001", 0 },
  779. { },
  780. };
  781. static int i2c_hid_acpi_pdata(struct i2c_client *client,
  782. struct i2c_hid_platform_data *pdata)
  783. {
  784. static guid_t i2c_hid_guid =
  785. GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
  786. 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
  787. union acpi_object *obj;
  788. struct acpi_device *adev;
  789. acpi_handle handle;
  790. handle = ACPI_HANDLE(&client->dev);
  791. if (!handle || acpi_bus_get_device(handle, &adev)) {
  792. dev_err(&client->dev, "Error could not get ACPI device\n");
  793. return -ENODEV;
  794. }
  795. if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
  796. return -ENODEV;
  797. obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
  798. ACPI_TYPE_INTEGER);
  799. if (!obj) {
  800. dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
  801. return -ENODEV;
  802. }
  803. pdata->hid_descriptor_address = obj->integer.value;
  804. ACPI_FREE(obj);
  805. return 0;
  806. }
  807. static void i2c_hid_acpi_fix_up_power(struct device *dev)
  808. {
  809. struct acpi_device *adev;
  810. adev = ACPI_COMPANION(dev);
  811. if (adev)
  812. acpi_device_fix_up_power(adev);
  813. }
  814. static const struct acpi_device_id i2c_hid_acpi_match[] = {
  815. {"ACPI0C50", 0 },
  816. {"PNP0C50", 0 },
  817. { },
  818. };
  819. MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
  820. #else
  821. static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
  822. struct i2c_hid_platform_data *pdata)
  823. {
  824. return -ENODEV;
  825. }
  826. static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
  827. #endif
  828. #ifdef CONFIG_OF
  829. static int i2c_hid_of_probe(struct i2c_client *client,
  830. struct i2c_hid_platform_data *pdata)
  831. {
  832. struct device *dev = &client->dev;
  833. u32 val;
  834. int ret;
  835. ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
  836. if (ret) {
  837. dev_err(&client->dev, "HID register address not provided\n");
  838. return -ENODEV;
  839. }
  840. if (val >> 16) {
  841. dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
  842. val);
  843. return -EINVAL;
  844. }
  845. pdata->hid_descriptor_address = val;
  846. return 0;
  847. }
  848. static const struct of_device_id i2c_hid_of_match[] = {
  849. { .compatible = "hid-over-i2c" },
  850. {},
  851. };
  852. MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
  853. #else
  854. static inline int i2c_hid_of_probe(struct i2c_client *client,
  855. struct i2c_hid_platform_data *pdata)
  856. {
  857. return -ENODEV;
  858. }
  859. #endif
  860. static void i2c_hid_fwnode_probe(struct i2c_client *client,
  861. struct i2c_hid_platform_data *pdata)
  862. {
  863. u32 val;
  864. if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
  865. &val))
  866. pdata->post_power_delay_ms = val;
  867. }
  868. static int i2c_hid_probe(struct i2c_client *client,
  869. const struct i2c_device_id *dev_id)
  870. {
  871. int ret;
  872. struct i2c_hid *ihid;
  873. struct hid_device *hid;
  874. __u16 hidRegister;
  875. struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
  876. dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
  877. if (!client->irq) {
  878. dev_err(&client->dev,
  879. "HID over i2c has not been provided an Int IRQ\n");
  880. return -EINVAL;
  881. }
  882. if (client->irq < 0) {
  883. if (client->irq != -EPROBE_DEFER)
  884. dev_err(&client->dev,
  885. "HID over i2c doesn't have a valid IRQ\n");
  886. return client->irq;
  887. }
  888. ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
  889. if (!ihid)
  890. return -ENOMEM;
  891. if (client->dev.of_node) {
  892. ret = i2c_hid_of_probe(client, &ihid->pdata);
  893. if (ret)
  894. return ret;
  895. } else if (!platform_data) {
  896. ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
  897. if (ret)
  898. return ret;
  899. } else {
  900. ihid->pdata = *platform_data;
  901. }
  902. /* Parse platform agnostic common properties from ACPI / device tree */
  903. i2c_hid_fwnode_probe(client, &ihid->pdata);
  904. ihid->pdata.supplies[0].supply = "vdd";
  905. ihid->pdata.supplies[1].supply = "vddl";
  906. ret = devm_regulator_bulk_get(&client->dev,
  907. ARRAY_SIZE(ihid->pdata.supplies),
  908. ihid->pdata.supplies);
  909. if (ret)
  910. return ret;
  911. ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
  912. ihid->pdata.supplies);
  913. if (ret < 0)
  914. return ret;
  915. if (ihid->pdata.post_power_delay_ms)
  916. msleep(ihid->pdata.post_power_delay_ms);
  917. i2c_set_clientdata(client, ihid);
  918. ihid->client = client;
  919. hidRegister = ihid->pdata.hid_descriptor_address;
  920. ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
  921. init_waitqueue_head(&ihid->wait);
  922. mutex_init(&ihid->reset_lock);
  923. /* we need to allocate the command buffer without knowing the maximum
  924. * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
  925. * real computation later. */
  926. ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
  927. if (ret < 0)
  928. goto err_regulator;
  929. i2c_hid_acpi_fix_up_power(&client->dev);
  930. pm_runtime_get_noresume(&client->dev);
  931. pm_runtime_set_active(&client->dev);
  932. pm_runtime_enable(&client->dev);
  933. device_enable_async_suspend(&client->dev);
  934. /* Make sure there is something at this address */
  935. ret = i2c_smbus_read_byte(client);
  936. if (ret < 0) {
  937. dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
  938. ret = -ENXIO;
  939. goto err_pm;
  940. }
  941. ret = i2c_hid_fetch_hid_descriptor(ihid);
  942. if (ret < 0)
  943. goto err_pm;
  944. ret = i2c_hid_init_irq(client);
  945. if (ret < 0)
  946. goto err_pm;
  947. hid = hid_allocate_device();
  948. if (IS_ERR(hid)) {
  949. ret = PTR_ERR(hid);
  950. goto err_irq;
  951. }
  952. ihid->hid = hid;
  953. hid->driver_data = client;
  954. hid->ll_driver = &i2c_hid_ll_driver;
  955. hid->dev.parent = &client->dev;
  956. hid->bus = BUS_I2C;
  957. hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
  958. hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
  959. hid->product = le16_to_cpu(ihid->hdesc.wProductID);
  960. snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
  961. client->name, hid->vendor, hid->product);
  962. strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
  963. ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
  964. ret = hid_add_device(hid);
  965. if (ret) {
  966. if (ret != -ENODEV)
  967. hid_err(client, "can't add hid device: %d\n", ret);
  968. goto err_mem_free;
  969. }
  970. if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
  971. pm_runtime_put(&client->dev);
  972. return 0;
  973. err_mem_free:
  974. hid_destroy_device(hid);
  975. err_irq:
  976. free_irq(client->irq, ihid);
  977. err_pm:
  978. pm_runtime_put_noidle(&client->dev);
  979. pm_runtime_disable(&client->dev);
  980. err_regulator:
  981. regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
  982. ihid->pdata.supplies);
  983. i2c_hid_free_buffers(ihid);
  984. return ret;
  985. }
  986. static int i2c_hid_remove(struct i2c_client *client)
  987. {
  988. struct i2c_hid *ihid = i2c_get_clientdata(client);
  989. struct hid_device *hid;
  990. if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
  991. pm_runtime_get_sync(&client->dev);
  992. pm_runtime_disable(&client->dev);
  993. pm_runtime_set_suspended(&client->dev);
  994. pm_runtime_put_noidle(&client->dev);
  995. hid = ihid->hid;
  996. hid_destroy_device(hid);
  997. free_irq(client->irq, ihid);
  998. if (ihid->bufsize)
  999. i2c_hid_free_buffers(ihid);
  1000. regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
  1001. ihid->pdata.supplies);
  1002. return 0;
  1003. }
  1004. static void i2c_hid_shutdown(struct i2c_client *client)
  1005. {
  1006. struct i2c_hid *ihid = i2c_get_clientdata(client);
  1007. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  1008. free_irq(client->irq, ihid);
  1009. }
  1010. #ifdef CONFIG_PM_SLEEP
  1011. static int i2c_hid_suspend(struct device *dev)
  1012. {
  1013. struct i2c_client *client = to_i2c_client(dev);
  1014. struct i2c_hid *ihid = i2c_get_clientdata(client);
  1015. struct hid_device *hid = ihid->hid;
  1016. int ret;
  1017. int wake_status;
  1018. if (hid->driver && hid->driver->suspend) {
  1019. /*
  1020. * Wake up the device so that IO issues in
  1021. * HID driver's suspend code can succeed.
  1022. */
  1023. ret = pm_runtime_resume(dev);
  1024. if (ret < 0)
  1025. return ret;
  1026. ret = hid->driver->suspend(hid, PMSG_SUSPEND);
  1027. if (ret < 0)
  1028. return ret;
  1029. }
  1030. if (!pm_runtime_suspended(dev)) {
  1031. /* Save some power */
  1032. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  1033. disable_irq(client->irq);
  1034. }
  1035. if (device_may_wakeup(&client->dev)) {
  1036. wake_status = enable_irq_wake(client->irq);
  1037. if (!wake_status)
  1038. ihid->irq_wake_enabled = true;
  1039. else
  1040. hid_warn(hid, "Failed to enable irq wake: %d\n",
  1041. wake_status);
  1042. } else {
  1043. regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
  1044. ihid->pdata.supplies);
  1045. }
  1046. return 0;
  1047. }
  1048. static int i2c_hid_resume(struct device *dev)
  1049. {
  1050. int ret;
  1051. struct i2c_client *client = to_i2c_client(dev);
  1052. struct i2c_hid *ihid = i2c_get_clientdata(client);
  1053. struct hid_device *hid = ihid->hid;
  1054. int wake_status;
  1055. if (!device_may_wakeup(&client->dev)) {
  1056. ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
  1057. ihid->pdata.supplies);
  1058. if (ret)
  1059. hid_warn(hid, "Failed to enable supplies: %d\n", ret);
  1060. if (ihid->pdata.post_power_delay_ms)
  1061. msleep(ihid->pdata.post_power_delay_ms);
  1062. } else if (ihid->irq_wake_enabled) {
  1063. wake_status = disable_irq_wake(client->irq);
  1064. if (!wake_status)
  1065. ihid->irq_wake_enabled = false;
  1066. else
  1067. hid_warn(hid, "Failed to disable irq wake: %d\n",
  1068. wake_status);
  1069. }
  1070. /* We'll resume to full power */
  1071. pm_runtime_disable(dev);
  1072. pm_runtime_set_active(dev);
  1073. pm_runtime_enable(dev);
  1074. enable_irq(client->irq);
  1075. /* Instead of resetting device, simply powers the device on. This
  1076. * solves "incomplete reports" on Raydium devices 2386:3118 and
  1077. * 2386:4B33 and fixes various SIS touchscreens no longer sending
  1078. * data after a suspend/resume.
  1079. *
  1080. * However some ALPS touchpads generate IRQ storm without reset, so
  1081. * let's still reset them here.
  1082. */
  1083. if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
  1084. ret = i2c_hid_hwreset(client);
  1085. else
  1086. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  1087. if (ret)
  1088. return ret;
  1089. if (hid->driver && hid->driver->reset_resume) {
  1090. ret = hid->driver->reset_resume(hid);
  1091. return ret;
  1092. }
  1093. return 0;
  1094. }
  1095. #endif
  1096. #ifdef CONFIG_PM
  1097. static int i2c_hid_runtime_suspend(struct device *dev)
  1098. {
  1099. struct i2c_client *client = to_i2c_client(dev);
  1100. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  1101. disable_irq(client->irq);
  1102. return 0;
  1103. }
  1104. static int i2c_hid_runtime_resume(struct device *dev)
  1105. {
  1106. struct i2c_client *client = to_i2c_client(dev);
  1107. enable_irq(client->irq);
  1108. i2c_hid_set_power(client, I2C_HID_PWR_ON);
  1109. return 0;
  1110. }
  1111. #endif
  1112. static const struct dev_pm_ops i2c_hid_pm = {
  1113. SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
  1114. SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
  1115. NULL)
  1116. };
  1117. static const struct i2c_device_id i2c_hid_id_table[] = {
  1118. { "hid", 0 },
  1119. { "hid-over-i2c", 0 },
  1120. { },
  1121. };
  1122. MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
  1123. static struct i2c_driver i2c_hid_driver = {
  1124. .driver = {
  1125. .name = "i2c_hid",
  1126. .pm = &i2c_hid_pm,
  1127. .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
  1128. .of_match_table = of_match_ptr(i2c_hid_of_match),
  1129. },
  1130. .probe = i2c_hid_probe,
  1131. .remove = i2c_hid_remove,
  1132. .shutdown = i2c_hid_shutdown,
  1133. .id_table = i2c_hid_id_table,
  1134. };
  1135. module_i2c_driver(i2c_hid_driver);
  1136. MODULE_DESCRIPTION("HID over I2C core driver");
  1137. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  1138. MODULE_LICENSE("GPL");