i2c-hid.c 23 KB

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