i2c-hid.c 27 KB

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