i2c-hid.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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 = le16_to_cpu(ihid->hdesc.wMaxInputLength);
  314. if (size > ihid->bufsize)
  315. size = ihid->bufsize;
  316. ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
  317. if (ret != size) {
  318. if (ret < 0)
  319. return;
  320. dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
  321. __func__, ret, size);
  322. return;
  323. }
  324. ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
  325. if (!ret_size) {
  326. /* host or device initiated RESET completed */
  327. if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
  328. wake_up(&ihid->wait);
  329. return;
  330. }
  331. if (ret_size > size) {
  332. dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
  333. __func__, size, ret_size);
  334. return;
  335. }
  336. i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
  337. if (test_bit(I2C_HID_STARTED, &ihid->flags))
  338. hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
  339. ret_size - 2, 1);
  340. return;
  341. }
  342. static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
  343. {
  344. struct i2c_hid *ihid = dev_id;
  345. if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
  346. return IRQ_HANDLED;
  347. i2c_hid_get_input(ihid);
  348. return IRQ_HANDLED;
  349. }
  350. static int i2c_hid_get_report_length(struct hid_report *report)
  351. {
  352. return ((report->size - 1) >> 3) + 1 +
  353. report->device->report_enum[report->type].numbered + 2;
  354. }
  355. static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
  356. size_t bufsize)
  357. {
  358. struct hid_device *hid = report->device;
  359. struct i2c_client *client = hid->driver_data;
  360. struct i2c_hid *ihid = i2c_get_clientdata(client);
  361. unsigned int size, ret_size;
  362. size = i2c_hid_get_report_length(report);
  363. if (i2c_hid_get_report(client,
  364. report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  365. report->id, buffer, size))
  366. return;
  367. i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer);
  368. ret_size = buffer[0] | (buffer[1] << 8);
  369. if (ret_size != size) {
  370. dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
  371. __func__, size, ret_size);
  372. return;
  373. }
  374. /* hid->driver_lock is held as we are in probe function,
  375. * we just need to setup the input fields, so using
  376. * hid_report_raw_event is safe. */
  377. hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
  378. }
  379. /*
  380. * Initialize all reports
  381. */
  382. static void i2c_hid_init_reports(struct hid_device *hid)
  383. {
  384. struct hid_report *report;
  385. struct i2c_client *client = hid->driver_data;
  386. struct i2c_hid *ihid = i2c_get_clientdata(client);
  387. u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
  388. if (!inbuf) {
  389. dev_err(&client->dev, "can not retrieve initial reports\n");
  390. return;
  391. }
  392. /*
  393. * The device must be powered on while we fetch initial reports
  394. * from it.
  395. */
  396. pm_runtime_get_sync(&client->dev);
  397. list_for_each_entry(report,
  398. &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  399. i2c_hid_init_report(report, inbuf, ihid->bufsize);
  400. pm_runtime_put(&client->dev);
  401. kfree(inbuf);
  402. }
  403. /*
  404. * Traverse the supplied list of reports and find the longest
  405. */
  406. static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
  407. unsigned int *max)
  408. {
  409. struct hid_report *report;
  410. unsigned int size;
  411. /* We should not rely on wMaxInputLength, as some devices may set it to
  412. * a wrong length. */
  413. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  414. size = i2c_hid_get_report_length(report);
  415. if (*max < size)
  416. *max = size;
  417. }
  418. }
  419. static void i2c_hid_free_buffers(struct i2c_hid *ihid)
  420. {
  421. kfree(ihid->inbuf);
  422. kfree(ihid->rawbuf);
  423. kfree(ihid->argsbuf);
  424. kfree(ihid->cmdbuf);
  425. ihid->inbuf = NULL;
  426. ihid->rawbuf = NULL;
  427. ihid->cmdbuf = NULL;
  428. ihid->argsbuf = NULL;
  429. ihid->bufsize = 0;
  430. }
  431. static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
  432. {
  433. /* the worst case is computed from the set_report command with a
  434. * reportID > 15 and the maximum report length */
  435. int args_len = sizeof(__u8) + /* optional ReportID byte */
  436. sizeof(__u16) + /* data register */
  437. sizeof(__u16) + /* size of the report */
  438. report_size; /* report */
  439. ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
  440. ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
  441. ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
  442. ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
  443. if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
  444. i2c_hid_free_buffers(ihid);
  445. return -ENOMEM;
  446. }
  447. ihid->bufsize = report_size;
  448. return 0;
  449. }
  450. static int i2c_hid_get_raw_report(struct hid_device *hid,
  451. unsigned char report_number, __u8 *buf, size_t count,
  452. unsigned char report_type)
  453. {
  454. struct i2c_client *client = hid->driver_data;
  455. struct i2c_hid *ihid = i2c_get_clientdata(client);
  456. size_t ret_count, ask_count;
  457. int ret;
  458. if (report_type == HID_OUTPUT_REPORT)
  459. return -EINVAL;
  460. /* +2 bytes to include the size of the reply in the query buffer */
  461. ask_count = min(count + 2, (size_t)ihid->bufsize);
  462. ret = i2c_hid_get_report(client,
  463. report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  464. report_number, ihid->rawbuf, ask_count);
  465. if (ret < 0)
  466. return ret;
  467. ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
  468. if (ret_count <= 2)
  469. return 0;
  470. ret_count = min(ret_count, ask_count);
  471. /* The query buffer contains the size, dropping it in the reply */
  472. count = min(count, ret_count - 2);
  473. memcpy(buf, ihid->rawbuf + 2, count);
  474. return count;
  475. }
  476. static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
  477. size_t count, unsigned char report_type, bool use_data)
  478. {
  479. struct i2c_client *client = hid->driver_data;
  480. int report_id = buf[0];
  481. int ret;
  482. if (report_type == HID_INPUT_REPORT)
  483. return -EINVAL;
  484. if (report_id) {
  485. buf++;
  486. count--;
  487. }
  488. ret = i2c_hid_set_or_send_report(client,
  489. report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
  490. report_id, buf, count, use_data);
  491. if (report_id && ret >= 0)
  492. ret++; /* add report_id to the number of transfered bytes */
  493. return ret;
  494. }
  495. static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
  496. size_t count)
  497. {
  498. return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
  499. false);
  500. }
  501. static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
  502. __u8 *buf, size_t len, unsigned char rtype,
  503. int reqtype)
  504. {
  505. switch (reqtype) {
  506. case HID_REQ_GET_REPORT:
  507. return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
  508. case HID_REQ_SET_REPORT:
  509. if (buf[0] != reportnum)
  510. return -EINVAL;
  511. return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
  512. default:
  513. return -EIO;
  514. }
  515. }
  516. static int i2c_hid_parse(struct hid_device *hid)
  517. {
  518. struct i2c_client *client = hid->driver_data;
  519. struct i2c_hid *ihid = i2c_get_clientdata(client);
  520. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  521. unsigned int rsize;
  522. char *rdesc;
  523. int ret;
  524. int tries = 3;
  525. i2c_hid_dbg(ihid, "entering %s\n", __func__);
  526. rsize = le16_to_cpu(hdesc->wReportDescLength);
  527. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  528. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  529. return -EINVAL;
  530. }
  531. do {
  532. ret = i2c_hid_hwreset(client);
  533. if (ret)
  534. msleep(1000);
  535. } while (tries-- > 0 && ret);
  536. if (ret)
  537. return ret;
  538. rdesc = kzalloc(rsize, GFP_KERNEL);
  539. if (!rdesc) {
  540. dbg_hid("couldn't allocate rdesc memory\n");
  541. return -ENOMEM;
  542. }
  543. i2c_hid_dbg(ihid, "asking HID report descriptor\n");
  544. ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
  545. if (ret) {
  546. hid_err(hid, "reading report descriptor failed\n");
  547. kfree(rdesc);
  548. return -EIO;
  549. }
  550. i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
  551. ret = hid_parse_report(hid, rdesc, rsize);
  552. kfree(rdesc);
  553. if (ret) {
  554. dbg_hid("parsing report descriptor failed\n");
  555. return ret;
  556. }
  557. return 0;
  558. }
  559. static int i2c_hid_start(struct hid_device *hid)
  560. {
  561. struct i2c_client *client = hid->driver_data;
  562. struct i2c_hid *ihid = i2c_get_clientdata(client);
  563. int ret;
  564. unsigned int bufsize = HID_MIN_BUFFER_SIZE;
  565. i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
  566. i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
  567. i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
  568. if (bufsize > ihid->bufsize) {
  569. i2c_hid_free_buffers(ihid);
  570. ret = i2c_hid_alloc_buffers(ihid, bufsize);
  571. if (ret)
  572. return ret;
  573. }
  574. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  575. i2c_hid_init_reports(hid);
  576. return 0;
  577. }
  578. static void i2c_hid_stop(struct hid_device *hid)
  579. {
  580. hid->claimed = 0;
  581. }
  582. static int i2c_hid_open(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 = 0;
  587. mutex_lock(&i2c_hid_open_mut);
  588. if (!hid->open++) {
  589. ret = pm_runtime_get_sync(&client->dev);
  590. if (ret < 0) {
  591. hid->open--;
  592. goto done;
  593. }
  594. set_bit(I2C_HID_STARTED, &ihid->flags);
  595. }
  596. done:
  597. mutex_unlock(&i2c_hid_open_mut);
  598. return ret < 0 ? ret : 0;
  599. }
  600. static void i2c_hid_close(struct hid_device *hid)
  601. {
  602. struct i2c_client *client = hid->driver_data;
  603. struct i2c_hid *ihid = i2c_get_clientdata(client);
  604. /* protecting hid->open to make sure we don't restart
  605. * data acquistion due to a resumption we no longer
  606. * care about
  607. */
  608. mutex_lock(&i2c_hid_open_mut);
  609. if (!--hid->open) {
  610. clear_bit(I2C_HID_STARTED, &ihid->flags);
  611. /* Save some power */
  612. pm_runtime_put(&client->dev);
  613. }
  614. mutex_unlock(&i2c_hid_open_mut);
  615. }
  616. static int i2c_hid_power(struct hid_device *hid, int lvl)
  617. {
  618. struct i2c_client *client = hid->driver_data;
  619. struct i2c_hid *ihid = i2c_get_clientdata(client);
  620. i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
  621. switch (lvl) {
  622. case PM_HINT_FULLON:
  623. pm_runtime_get_sync(&client->dev);
  624. break;
  625. case PM_HINT_NORMAL:
  626. pm_runtime_put(&client->dev);
  627. break;
  628. }
  629. return 0;
  630. }
  631. static struct hid_ll_driver i2c_hid_ll_driver = {
  632. .parse = i2c_hid_parse,
  633. .start = i2c_hid_start,
  634. .stop = i2c_hid_stop,
  635. .open = i2c_hid_open,
  636. .close = i2c_hid_close,
  637. .power = i2c_hid_power,
  638. .output_report = i2c_hid_output_report,
  639. .raw_request = i2c_hid_raw_request,
  640. };
  641. static int i2c_hid_init_irq(struct i2c_client *client)
  642. {
  643. struct i2c_hid *ihid = i2c_get_clientdata(client);
  644. int ret;
  645. dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
  646. ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
  647. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  648. client->name, ihid);
  649. if (ret < 0) {
  650. dev_warn(&client->dev,
  651. "Could not register for %s interrupt, irq = %d,"
  652. " ret = %d\n",
  653. client->name, client->irq, ret);
  654. return ret;
  655. }
  656. return 0;
  657. }
  658. static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
  659. {
  660. struct i2c_client *client = ihid->client;
  661. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  662. unsigned int dsize;
  663. int ret;
  664. /* i2c hid fetch using a fixed descriptor size (30 bytes) */
  665. i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
  666. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
  667. sizeof(struct i2c_hid_desc));
  668. if (ret) {
  669. dev_err(&client->dev, "hid_descr_cmd failed\n");
  670. return -ENODEV;
  671. }
  672. /* Validate the length of HID descriptor, the 4 first bytes:
  673. * bytes 0-1 -> length
  674. * bytes 2-3 -> bcdVersion (has to be 1.00) */
  675. /* check bcdVersion == 1.0 */
  676. if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
  677. dev_err(&client->dev,
  678. "unexpected HID descriptor bcdVersion (0x%04hx)\n",
  679. le16_to_cpu(hdesc->bcdVersion));
  680. return -ENODEV;
  681. }
  682. /* Descriptor length should be 30 bytes as per the specification */
  683. dsize = le16_to_cpu(hdesc->wHIDDescLength);
  684. if (dsize != sizeof(struct i2c_hid_desc)) {
  685. dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
  686. dsize);
  687. return -ENODEV;
  688. }
  689. i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
  690. return 0;
  691. }
  692. #ifdef CONFIG_ACPI
  693. static int i2c_hid_acpi_pdata(struct i2c_client *client,
  694. struct i2c_hid_platform_data *pdata)
  695. {
  696. static u8 i2c_hid_guid[] = {
  697. 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
  698. 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
  699. };
  700. union acpi_object *obj;
  701. struct acpi_device *adev;
  702. acpi_handle handle;
  703. handle = ACPI_HANDLE(&client->dev);
  704. if (!handle || acpi_bus_get_device(handle, &adev))
  705. return -ENODEV;
  706. obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL,
  707. ACPI_TYPE_INTEGER);
  708. if (!obj) {
  709. dev_err(&client->dev, "device _DSM execution failed\n");
  710. return -ENODEV;
  711. }
  712. pdata->hid_descriptor_address = obj->integer.value;
  713. ACPI_FREE(obj);
  714. return 0;
  715. }
  716. static const struct acpi_device_id i2c_hid_acpi_match[] = {
  717. {"ACPI0C50", 0 },
  718. {"PNP0C50", 0 },
  719. { },
  720. };
  721. MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
  722. #else
  723. static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
  724. struct i2c_hid_platform_data *pdata)
  725. {
  726. return -ENODEV;
  727. }
  728. #endif
  729. #ifdef CONFIG_OF
  730. static int i2c_hid_of_probe(struct i2c_client *client,
  731. struct i2c_hid_platform_data *pdata)
  732. {
  733. struct device *dev = &client->dev;
  734. u32 val;
  735. int ret;
  736. ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
  737. if (ret) {
  738. dev_err(&client->dev, "HID register address not provided\n");
  739. return -ENODEV;
  740. }
  741. if (val >> 16) {
  742. dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
  743. val);
  744. return -EINVAL;
  745. }
  746. pdata->hid_descriptor_address = val;
  747. return 0;
  748. }
  749. static const struct of_device_id i2c_hid_of_match[] = {
  750. { .compatible = "hid-over-i2c" },
  751. {},
  752. };
  753. MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
  754. #else
  755. static inline int i2c_hid_of_probe(struct i2c_client *client,
  756. struct i2c_hid_platform_data *pdata)
  757. {
  758. return -ENODEV;
  759. }
  760. #endif
  761. static int i2c_hid_probe(struct i2c_client *client,
  762. const struct i2c_device_id *dev_id)
  763. {
  764. int ret;
  765. struct i2c_hid *ihid;
  766. struct hid_device *hid;
  767. __u16 hidRegister;
  768. struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
  769. dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
  770. if (!client->irq) {
  771. dev_err(&client->dev,
  772. "HID over i2c has not been provided an Int IRQ\n");
  773. return -EINVAL;
  774. }
  775. ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
  776. if (!ihid)
  777. return -ENOMEM;
  778. if (client->dev.of_node) {
  779. ret = i2c_hid_of_probe(client, &ihid->pdata);
  780. if (ret)
  781. goto err;
  782. } else if (!platform_data) {
  783. ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
  784. if (ret) {
  785. dev_err(&client->dev,
  786. "HID register address not provided\n");
  787. goto err;
  788. }
  789. } else {
  790. ihid->pdata = *platform_data;
  791. }
  792. i2c_set_clientdata(client, ihid);
  793. ihid->client = client;
  794. hidRegister = ihid->pdata.hid_descriptor_address;
  795. ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
  796. init_waitqueue_head(&ihid->wait);
  797. /* we need to allocate the command buffer without knowing the maximum
  798. * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
  799. * real computation later. */
  800. ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
  801. if (ret < 0)
  802. goto err;
  803. pm_runtime_get_noresume(&client->dev);
  804. pm_runtime_set_active(&client->dev);
  805. pm_runtime_enable(&client->dev);
  806. ret = i2c_hid_fetch_hid_descriptor(ihid);
  807. if (ret < 0)
  808. goto err_pm;
  809. ret = i2c_hid_init_irq(client);
  810. if (ret < 0)
  811. goto err_pm;
  812. hid = hid_allocate_device();
  813. if (IS_ERR(hid)) {
  814. ret = PTR_ERR(hid);
  815. goto err_irq;
  816. }
  817. ihid->hid = hid;
  818. hid->driver_data = client;
  819. hid->ll_driver = &i2c_hid_ll_driver;
  820. hid->dev.parent = &client->dev;
  821. ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
  822. hid->bus = BUS_I2C;
  823. hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
  824. hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
  825. hid->product = le16_to_cpu(ihid->hdesc.wProductID);
  826. snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
  827. client->name, hid->vendor, hid->product);
  828. ret = hid_add_device(hid);
  829. if (ret) {
  830. if (ret != -ENODEV)
  831. hid_err(client, "can't add hid device: %d\n", ret);
  832. goto err_mem_free;
  833. }
  834. pm_runtime_put(&client->dev);
  835. return 0;
  836. err_mem_free:
  837. hid_destroy_device(hid);
  838. err_irq:
  839. free_irq(client->irq, ihid);
  840. err_pm:
  841. pm_runtime_put_noidle(&client->dev);
  842. pm_runtime_disable(&client->dev);
  843. err:
  844. i2c_hid_free_buffers(ihid);
  845. kfree(ihid);
  846. return ret;
  847. }
  848. static int i2c_hid_remove(struct i2c_client *client)
  849. {
  850. struct i2c_hid *ihid = i2c_get_clientdata(client);
  851. struct hid_device *hid;
  852. pm_runtime_get_sync(&client->dev);
  853. pm_runtime_disable(&client->dev);
  854. pm_runtime_set_suspended(&client->dev);
  855. pm_runtime_put_noidle(&client->dev);
  856. hid = ihid->hid;
  857. hid_destroy_device(hid);
  858. free_irq(client->irq, ihid);
  859. if (ihid->bufsize)
  860. i2c_hid_free_buffers(ihid);
  861. kfree(ihid);
  862. return 0;
  863. }
  864. #ifdef CONFIG_PM_SLEEP
  865. static int i2c_hid_suspend(struct device *dev)
  866. {
  867. struct i2c_client *client = to_i2c_client(dev);
  868. struct i2c_hid *ihid = i2c_get_clientdata(client);
  869. struct hid_device *hid = ihid->hid;
  870. int ret = 0;
  871. disable_irq(client->irq);
  872. if (device_may_wakeup(&client->dev))
  873. enable_irq_wake(client->irq);
  874. if (hid->driver && hid->driver->suspend)
  875. ret = hid->driver->suspend(hid, PMSG_SUSPEND);
  876. /* Save some power */
  877. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  878. return ret;
  879. }
  880. static int i2c_hid_resume(struct device *dev)
  881. {
  882. int ret;
  883. struct i2c_client *client = to_i2c_client(dev);
  884. struct i2c_hid *ihid = i2c_get_clientdata(client);
  885. struct hid_device *hid = ihid->hid;
  886. enable_irq(client->irq);
  887. ret = i2c_hid_hwreset(client);
  888. if (ret)
  889. return ret;
  890. if (device_may_wakeup(&client->dev))
  891. disable_irq_wake(client->irq);
  892. if (hid->driver && hid->driver->reset_resume) {
  893. ret = hid->driver->reset_resume(hid);
  894. return ret;
  895. }
  896. return 0;
  897. }
  898. #endif
  899. #ifdef CONFIG_PM
  900. static int i2c_hid_runtime_suspend(struct device *dev)
  901. {
  902. struct i2c_client *client = to_i2c_client(dev);
  903. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  904. disable_irq(client->irq);
  905. return 0;
  906. }
  907. static int i2c_hid_runtime_resume(struct device *dev)
  908. {
  909. struct i2c_client *client = to_i2c_client(dev);
  910. enable_irq(client->irq);
  911. i2c_hid_set_power(client, I2C_HID_PWR_ON);
  912. return 0;
  913. }
  914. #endif
  915. static const struct dev_pm_ops i2c_hid_pm = {
  916. SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
  917. SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
  918. NULL)
  919. };
  920. static const struct i2c_device_id i2c_hid_id_table[] = {
  921. { "hid", 0 },
  922. { },
  923. };
  924. MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
  925. static struct i2c_driver i2c_hid_driver = {
  926. .driver = {
  927. .name = "i2c_hid",
  928. .owner = THIS_MODULE,
  929. .pm = &i2c_hid_pm,
  930. .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
  931. .of_match_table = of_match_ptr(i2c_hid_of_match),
  932. },
  933. .probe = i2c_hid_probe,
  934. .remove = i2c_hid_remove,
  935. .id_table = i2c_hid_id_table,
  936. };
  937. module_i2c_driver(i2c_hid_driver);
  938. MODULE_DESCRIPTION("HID over I2C core driver");
  939. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  940. MODULE_LICENSE("GPL");