i2c-hid.c 31 KB

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