i2c-hid.c 23 KB

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