i2c-hid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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/bug.h>
  35. #include <linux/hid.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. } __packed;
  67. struct i2c_hid_cmd {
  68. unsigned int registerIndex;
  69. __u8 opcode;
  70. unsigned int length;
  71. bool wait;
  72. };
  73. union command {
  74. u8 data[0];
  75. struct cmd {
  76. __le16 reg;
  77. __u8 reportTypeID;
  78. __u8 opcode;
  79. } __packed c;
  80. };
  81. #define I2C_HID_CMD(opcode_) \
  82. .opcode = opcode_, .length = 4, \
  83. .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
  84. /* fetch HID descriptor */
  85. static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
  86. /* fetch report descriptors */
  87. static const struct i2c_hid_cmd hid_report_descr_cmd = {
  88. .registerIndex = offsetof(struct i2c_hid_desc,
  89. wReportDescRegister),
  90. .opcode = 0x00,
  91. .length = 2 };
  92. /* commands */
  93. static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
  94. .wait = true };
  95. static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
  96. static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
  97. static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
  98. static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
  99. static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
  100. static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
  101. static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
  102. /* read/write data register */
  103. static const struct i2c_hid_cmd hid_data_cmd = {
  104. .registerIndex = offsetof(struct i2c_hid_desc, wDataRegister),
  105. .opcode = 0x00,
  106. .length = 2 };
  107. /* write output reports */
  108. static const struct i2c_hid_cmd hid_out_cmd = {
  109. .registerIndex = offsetof(struct i2c_hid_desc,
  110. wOutputRegister),
  111. .opcode = 0x00,
  112. .length = 2 };
  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. int irq; /* the interrupt line irq */
  130. wait_queue_head_t wait; /* For waiting the interrupt */
  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 -EINVAL;
  218. }
  219. return 0;
  220. }
  221. static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
  222. u8 reportID, unsigned char *buf, size_t data_len)
  223. {
  224. struct i2c_hid *ihid = i2c_get_clientdata(client);
  225. u8 *args = ihid->argsbuf;
  226. int ret;
  227. u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
  228. /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
  229. u16 size = 2 /* size */ +
  230. (reportID ? 1 : 0) /* reportID */ +
  231. data_len /* buf */;
  232. int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
  233. 2 /* dataRegister */ +
  234. size /* args */;
  235. int index = 0;
  236. i2c_hid_dbg(ihid, "%s\n", __func__);
  237. if (reportID >= 0x0F) {
  238. args[index++] = reportID;
  239. reportID = 0x0F;
  240. }
  241. args[index++] = dataRegister & 0xFF;
  242. args[index++] = dataRegister >> 8;
  243. args[index++] = size & 0xFF;
  244. args[index++] = size >> 8;
  245. if (reportID)
  246. args[index++] = reportID;
  247. memcpy(&args[index], buf, data_len);
  248. ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
  249. reportType, args, args_len, NULL, 0);
  250. if (ret) {
  251. dev_err(&client->dev, "failed to set a report to device.\n");
  252. return -EINVAL;
  253. }
  254. return data_len;
  255. }
  256. static int i2c_hid_set_power(struct i2c_client *client, int power_state)
  257. {
  258. struct i2c_hid *ihid = i2c_get_clientdata(client);
  259. int ret;
  260. i2c_hid_dbg(ihid, "%s\n", __func__);
  261. ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
  262. 0, NULL, 0, NULL, 0);
  263. if (ret)
  264. dev_err(&client->dev, "failed to change power setting.\n");
  265. return ret;
  266. }
  267. static int i2c_hid_hwreset(struct i2c_client *client)
  268. {
  269. struct i2c_hid *ihid = i2c_get_clientdata(client);
  270. int ret;
  271. i2c_hid_dbg(ihid, "%s\n", __func__);
  272. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  273. if (ret)
  274. return ret;
  275. i2c_hid_dbg(ihid, "resetting...\n");
  276. ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
  277. if (ret) {
  278. dev_err(&client->dev, "failed to reset device.\n");
  279. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  280. return ret;
  281. }
  282. return 0;
  283. }
  284. static int i2c_hid_get_input(struct i2c_hid *ihid)
  285. {
  286. int ret, ret_size;
  287. int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
  288. ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
  289. if (ret != size) {
  290. if (ret < 0)
  291. return ret;
  292. dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
  293. __func__, ret, size);
  294. return ret;
  295. }
  296. ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
  297. if (!ret_size) {
  298. /* host or device initiated RESET completed */
  299. if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
  300. wake_up(&ihid->wait);
  301. return 0;
  302. }
  303. if (ret_size > size) {
  304. dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
  305. __func__, size, ret_size);
  306. return -EIO;
  307. }
  308. i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
  309. if (test_bit(I2C_HID_STARTED, &ihid->flags))
  310. hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
  311. ret_size - 2, 1);
  312. return 0;
  313. }
  314. static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
  315. {
  316. struct i2c_hid *ihid = dev_id;
  317. if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
  318. return IRQ_HANDLED;
  319. i2c_hid_get_input(ihid);
  320. return IRQ_HANDLED;
  321. }
  322. static int i2c_hid_get_report_length(struct hid_report *report)
  323. {
  324. return ((report->size - 1) >> 3) + 1 +
  325. report->device->report_enum[report->type].numbered + 2;
  326. }
  327. static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
  328. size_t bufsize)
  329. {
  330. struct hid_device *hid = report->device;
  331. struct i2c_client *client = hid->driver_data;
  332. struct i2c_hid *ihid = i2c_get_clientdata(client);
  333. unsigned int size, ret_size;
  334. size = i2c_hid_get_report_length(report);
  335. i2c_hid_get_report(client,
  336. report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  337. report->id, buffer, size);
  338. i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
  339. ret_size = buffer[0] | (buffer[1] << 8);
  340. if (ret_size != size) {
  341. dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
  342. __func__, size, ret_size);
  343. return;
  344. }
  345. /* hid->driver_lock is held as we are in probe function,
  346. * we just need to setup the input fields, so using
  347. * hid_report_raw_event is safe. */
  348. hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
  349. }
  350. /*
  351. * Initialize all reports
  352. */
  353. static void i2c_hid_init_reports(struct hid_device *hid)
  354. {
  355. struct hid_report *report;
  356. struct i2c_client *client = hid->driver_data;
  357. struct i2c_hid *ihid = i2c_get_clientdata(client);
  358. u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
  359. if (!inbuf)
  360. return;
  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 int i2c_hid_alloc_buffers(struct i2c_hid *ihid)
  386. {
  387. /* the worst case is computed from the set_report command with a
  388. * reportID > 15 and the maximum report length */
  389. int args_len = sizeof(__u8) + /* optional ReportID byte */
  390. sizeof(__u16) + /* data register */
  391. sizeof(__u16) + /* size of the report */
  392. ihid->bufsize; /* report */
  393. ihid->inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
  394. if (!ihid->inbuf)
  395. return -ENOMEM;
  396. ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
  397. if (!ihid->argsbuf) {
  398. kfree(ihid->inbuf);
  399. return -ENOMEM;
  400. }
  401. ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
  402. if (!ihid->cmdbuf) {
  403. kfree(ihid->inbuf);
  404. kfree(ihid->argsbuf);
  405. ihid->inbuf = NULL;
  406. ihid->argsbuf = NULL;
  407. return -ENOMEM;
  408. }
  409. return 0;
  410. }
  411. static void i2c_hid_free_buffers(struct i2c_hid *ihid)
  412. {
  413. kfree(ihid->inbuf);
  414. kfree(ihid->argsbuf);
  415. kfree(ihid->cmdbuf);
  416. ihid->inbuf = NULL;
  417. ihid->cmdbuf = NULL;
  418. ihid->argsbuf = NULL;
  419. }
  420. static int i2c_hid_get_raw_report(struct hid_device *hid,
  421. unsigned char report_number, __u8 *buf, size_t count,
  422. unsigned char report_type)
  423. {
  424. struct i2c_client *client = hid->driver_data;
  425. struct i2c_hid *ihid = i2c_get_clientdata(client);
  426. int ret;
  427. if (report_type == HID_OUTPUT_REPORT)
  428. return -EINVAL;
  429. if (count > ihid->bufsize)
  430. count = ihid->bufsize;
  431. ret = i2c_hid_get_report(client,
  432. report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  433. report_number, ihid->inbuf, count);
  434. if (ret < 0)
  435. return ret;
  436. count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
  437. memcpy(buf, ihid->inbuf + 2, count);
  438. return count;
  439. }
  440. static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
  441. size_t count, unsigned char report_type)
  442. {
  443. struct i2c_client *client = hid->driver_data;
  444. int report_id = buf[0];
  445. if (report_type == HID_INPUT_REPORT)
  446. return -EINVAL;
  447. return i2c_hid_set_report(client,
  448. report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
  449. report_id, buf, count);
  450. }
  451. static int i2c_hid_parse(struct hid_device *hid)
  452. {
  453. struct i2c_client *client = hid->driver_data;
  454. struct i2c_hid *ihid = i2c_get_clientdata(client);
  455. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  456. unsigned int rsize;
  457. char *rdesc;
  458. int ret;
  459. int tries = 3;
  460. i2c_hid_dbg(ihid, "entering %s\n", __func__);
  461. rsize = le16_to_cpu(hdesc->wReportDescLength);
  462. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  463. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  464. return -EINVAL;
  465. }
  466. do {
  467. ret = i2c_hid_hwreset(client);
  468. if (ret)
  469. msleep(1000);
  470. } while (tries-- > 0 && ret);
  471. if (ret)
  472. return ret;
  473. rdesc = kzalloc(rsize, GFP_KERNEL);
  474. if (!rdesc) {
  475. dbg_hid("couldn't allocate rdesc memory\n");
  476. return -ENOMEM;
  477. }
  478. i2c_hid_dbg(ihid, "asking HID report descriptor\n");
  479. ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
  480. if (ret) {
  481. hid_err(hid, "reading report descriptor failed\n");
  482. kfree(rdesc);
  483. return -EIO;
  484. }
  485. i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
  486. ret = hid_parse_report(hid, rdesc, rsize);
  487. kfree(rdesc);
  488. if (ret) {
  489. dbg_hid("parsing report descriptor failed\n");
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. static int i2c_hid_start(struct hid_device *hid)
  495. {
  496. struct i2c_client *client = hid->driver_data;
  497. struct i2c_hid *ihid = i2c_get_clientdata(client);
  498. int ret;
  499. int old_bufsize = ihid->bufsize;
  500. ihid->bufsize = HID_MIN_BUFFER_SIZE;
  501. i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &ihid->bufsize);
  502. i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &ihid->bufsize);
  503. i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &ihid->bufsize);
  504. if (ihid->bufsize > old_bufsize || !ihid->inbuf || !ihid->cmdbuf) {
  505. i2c_hid_free_buffers(ihid);
  506. ret = i2c_hid_alloc_buffers(ihid);
  507. if (ret) {
  508. ihid->bufsize = old_bufsize;
  509. return ret;
  510. }
  511. }
  512. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  513. i2c_hid_init_reports(hid);
  514. return 0;
  515. }
  516. static void i2c_hid_stop(struct hid_device *hid)
  517. {
  518. struct i2c_client *client = hid->driver_data;
  519. struct i2c_hid *ihid = i2c_get_clientdata(client);
  520. hid->claimed = 0;
  521. i2c_hid_free_buffers(ihid);
  522. }
  523. static int i2c_hid_open(struct hid_device *hid)
  524. {
  525. struct i2c_client *client = hid->driver_data;
  526. struct i2c_hid *ihid = i2c_get_clientdata(client);
  527. int ret;
  528. if (!hid->open++) {
  529. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  530. if (ret) {
  531. hid->open--;
  532. return -EIO;
  533. }
  534. set_bit(I2C_HID_STARTED, &ihid->flags);
  535. }
  536. return 0;
  537. }
  538. static void i2c_hid_close(struct hid_device *hid)
  539. {
  540. struct i2c_client *client = hid->driver_data;
  541. struct i2c_hid *ihid = i2c_get_clientdata(client);
  542. /* protecting hid->open to make sure we don't restart
  543. * data acquistion due to a resumption we no longer
  544. * care about
  545. */
  546. if (!--hid->open) {
  547. clear_bit(I2C_HID_STARTED, &ihid->flags);
  548. /* Save some power */
  549. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  550. }
  551. }
  552. static int i2c_hid_power(struct hid_device *hid, int lvl)
  553. {
  554. struct i2c_client *client = hid->driver_data;
  555. struct i2c_hid *ihid = i2c_get_clientdata(client);
  556. int ret = 0;
  557. i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
  558. switch (lvl) {
  559. case PM_HINT_FULLON:
  560. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  561. break;
  562. case PM_HINT_NORMAL:
  563. ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  564. break;
  565. }
  566. return ret;
  567. }
  568. static int i2c_hid_hidinput_input_event(struct input_dev *dev,
  569. unsigned int type, unsigned int code, int value)
  570. {
  571. struct hid_device *hid = input_get_drvdata(dev);
  572. struct hid_field *field;
  573. int offset;
  574. if (type == EV_FF)
  575. return input_ff_event(dev, type, code, value);
  576. if (type != EV_LED)
  577. return -1;
  578. offset = hidinput_find_field(hid, type, code, &field);
  579. if (offset == -1) {
  580. hid_warn(dev, "event field not found\n");
  581. return -1;
  582. }
  583. hid_set_field(field, offset, value);
  584. return 0;
  585. }
  586. static struct hid_ll_driver i2c_hid_ll_driver = {
  587. .parse = i2c_hid_parse,
  588. .start = i2c_hid_start,
  589. .stop = i2c_hid_stop,
  590. .open = i2c_hid_open,
  591. .close = i2c_hid_close,
  592. .power = i2c_hid_power,
  593. .hidinput_input_event = i2c_hid_hidinput_input_event,
  594. };
  595. static int __devinit i2c_hid_init_irq(struct i2c_client *client)
  596. {
  597. struct i2c_hid *ihid = i2c_get_clientdata(client);
  598. int ret;
  599. dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
  600. ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
  601. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  602. client->name, ihid);
  603. if (ret < 0) {
  604. dev_dbg(&client->dev,
  605. "Could not register for %s interrupt, irq = %d,"
  606. " ret = %d\n",
  607. client->name, client->irq, ret);
  608. return ret;
  609. }
  610. ihid->irq = client->irq;
  611. return 0;
  612. }
  613. static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
  614. {
  615. struct i2c_client *client = ihid->client;
  616. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  617. unsigned int dsize;
  618. int ret;
  619. /* Fetch the length of HID description, retrieve the 4 first bytes:
  620. * bytes 0-1 -> length
  621. * bytes 2-3 -> bcdVersion (has to be 1.00) */
  622. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
  623. i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
  624. __func__, 4, ihid->hdesc_buffer);
  625. if (ret) {
  626. dev_err(&client->dev, "HID_DESCR_LENGTH_CMD Fail (ret=%d)\n",
  627. ret);
  628. return -ENODEV;
  629. }
  630. dsize = le16_to_cpu(hdesc->wHIDDescLength);
  631. if (!dsize || dsize > HID_MAX_DESCRIPTOR_SIZE) {
  632. dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
  633. dsize);
  634. return -ENODEV;
  635. }
  636. /* check bcdVersion == 1.0 */
  637. if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
  638. dev_err(&client->dev,
  639. "unexpected HID descriptor bcdVersion (0x%04x)\n",
  640. le16_to_cpu(hdesc->bcdVersion));
  641. return -ENODEV;
  642. }
  643. i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
  644. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
  645. dsize);
  646. if (ret) {
  647. dev_err(&client->dev, "hid_descr_cmd Fail\n");
  648. return -ENODEV;
  649. }
  650. i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
  651. return 0;
  652. }
  653. static int __devinit i2c_hid_probe(struct i2c_client *client,
  654. const struct i2c_device_id *dev_id)
  655. {
  656. int ret;
  657. struct i2c_hid *ihid;
  658. struct hid_device *hid;
  659. __u16 hidRegister;
  660. struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
  661. dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
  662. if (!platform_data) {
  663. dev_err(&client->dev, "HID register address not provided\n");
  664. return -EINVAL;
  665. }
  666. if (!client->irq) {
  667. dev_err(&client->dev,
  668. "HID over i2c has not been provided an Int IRQ\n");
  669. return -EINVAL;
  670. }
  671. ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
  672. if (!ihid)
  673. return -ENOMEM;
  674. i2c_set_clientdata(client, ihid);
  675. ihid->client = client;
  676. hidRegister = platform_data->hid_descriptor_address;
  677. ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
  678. init_waitqueue_head(&ihid->wait);
  679. /* we need to allocate the command buffer without knowing the maximum
  680. * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
  681. * real computation later. */
  682. ihid->bufsize = HID_MIN_BUFFER_SIZE;
  683. i2c_hid_alloc_buffers(ihid);
  684. ret = i2c_hid_fetch_hid_descriptor(ihid);
  685. if (ret < 0)
  686. goto err;
  687. ret = i2c_hid_init_irq(client);
  688. if (ret < 0)
  689. goto err;
  690. hid = hid_allocate_device();
  691. if (IS_ERR(hid)) {
  692. ret = PTR_ERR(hid);
  693. goto err;
  694. }
  695. ihid->hid = hid;
  696. hid->driver_data = client;
  697. hid->ll_driver = &i2c_hid_ll_driver;
  698. hid->hid_get_raw_report = i2c_hid_get_raw_report;
  699. hid->hid_output_raw_report = i2c_hid_output_raw_report;
  700. hid->dev.parent = &client->dev;
  701. hid->bus = BUS_I2C;
  702. hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
  703. hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
  704. hid->product = le16_to_cpu(ihid->hdesc.wProductID);
  705. snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
  706. client->name, hid->vendor, hid->product);
  707. ret = hid_add_device(hid);
  708. if (ret) {
  709. if (ret != -ENODEV)
  710. hid_err(client, "can't add hid device: %d\n", ret);
  711. goto err_mem_free;
  712. }
  713. return 0;
  714. err_mem_free:
  715. hid_destroy_device(hid);
  716. err:
  717. if (ihid->irq)
  718. free_irq(ihid->irq, ihid);
  719. i2c_hid_free_buffers(ihid);
  720. kfree(ihid);
  721. return ret;
  722. }
  723. static int __devexit i2c_hid_remove(struct i2c_client *client)
  724. {
  725. struct i2c_hid *ihid = i2c_get_clientdata(client);
  726. struct hid_device *hid;
  727. if (WARN_ON(!ihid))
  728. return -1;
  729. hid = ihid->hid;
  730. hid_destroy_device(hid);
  731. free_irq(client->irq, ihid);
  732. kfree(ihid);
  733. return 0;
  734. }
  735. #ifdef CONFIG_PM_SLEEP
  736. static int i2c_hid_suspend(struct device *dev)
  737. {
  738. struct i2c_client *client = to_i2c_client(dev);
  739. struct i2c_hid *ihid = i2c_get_clientdata(client);
  740. if (device_may_wakeup(&client->dev))
  741. enable_irq_wake(ihid->irq);
  742. /* Save some power */
  743. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  744. return 0;
  745. }
  746. static int i2c_hid_resume(struct device *dev)
  747. {
  748. int ret;
  749. struct i2c_client *client = to_i2c_client(dev);
  750. ret = i2c_hid_hwreset(client);
  751. if (ret)
  752. return ret;
  753. if (device_may_wakeup(&client->dev))
  754. disable_irq_wake(client->irq);
  755. return 0;
  756. }
  757. #endif
  758. static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
  759. static const struct i2c_device_id i2c_hid_id_table[] = {
  760. { "hid", 0 },
  761. { },
  762. };
  763. MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
  764. static struct i2c_driver i2c_hid_driver = {
  765. .driver = {
  766. .name = "i2c_hid",
  767. .owner = THIS_MODULE,
  768. .pm = &i2c_hid_pm,
  769. },
  770. .probe = i2c_hid_probe,
  771. .remove = __devexit_p(i2c_hid_remove),
  772. .id_table = i2c_hid_id_table,
  773. };
  774. module_i2c_driver(i2c_hid_driver);
  775. MODULE_DESCRIPTION("HID over I2C core driver");
  776. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  777. MODULE_LICENSE("GPL");