i2c-hid.c 27 KB

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