i2c-hid.c 29 KB

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