i2c-hid.c 28 KB

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