i2c-hid.c 32 KB

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