i2c-hid.c 26 KB

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