i2c-hid.c 27 KB

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