i2c-hid.c 28 KB

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