i2c-hid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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. i2c_hid_get_report(client,
  330. report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  331. report->id, buffer, size);
  332. i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
  333. ret_size = buffer[0] | (buffer[1] << 8);
  334. if (ret_size != size) {
  335. dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
  336. __func__, size, ret_size);
  337. return;
  338. }
  339. /* hid->driver_lock is held as we are in probe function,
  340. * we just need to setup the input fields, so using
  341. * hid_report_raw_event is safe. */
  342. hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
  343. }
  344. /*
  345. * Initialize all reports
  346. */
  347. static void i2c_hid_init_reports(struct hid_device *hid)
  348. {
  349. struct hid_report *report;
  350. struct i2c_client *client = hid->driver_data;
  351. struct i2c_hid *ihid = i2c_get_clientdata(client);
  352. u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
  353. if (!inbuf) {
  354. dev_err(&client->dev, "can not retrieve initial reports\n");
  355. return;
  356. }
  357. list_for_each_entry(report,
  358. &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  359. i2c_hid_init_report(report, inbuf, ihid->bufsize);
  360. list_for_each_entry(report,
  361. &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  362. i2c_hid_init_report(report, inbuf, ihid->bufsize);
  363. kfree(inbuf);
  364. }
  365. /*
  366. * Traverse the supplied list of reports and find the longest
  367. */
  368. static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
  369. unsigned int *max)
  370. {
  371. struct hid_report *report;
  372. unsigned int size;
  373. /* We should not rely on wMaxInputLength, as some devices may set it to
  374. * a wrong length. */
  375. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  376. size = i2c_hid_get_report_length(report);
  377. if (*max < size)
  378. *max = size;
  379. }
  380. }
  381. static int i2c_hid_alloc_buffers(struct i2c_hid *ihid)
  382. {
  383. /* the worst case is computed from the set_report command with a
  384. * reportID > 15 and the maximum report length */
  385. int args_len = sizeof(__u8) + /* optional ReportID byte */
  386. sizeof(__u16) + /* data register */
  387. sizeof(__u16) + /* size of the report */
  388. ihid->bufsize; /* report */
  389. ihid->inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
  390. if (!ihid->inbuf)
  391. return -ENOMEM;
  392. ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
  393. if (!ihid->argsbuf) {
  394. kfree(ihid->inbuf);
  395. return -ENOMEM;
  396. }
  397. ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
  398. if (!ihid->cmdbuf) {
  399. kfree(ihid->inbuf);
  400. kfree(ihid->argsbuf);
  401. ihid->inbuf = NULL;
  402. ihid->argsbuf = NULL;
  403. return -ENOMEM;
  404. }
  405. return 0;
  406. }
  407. static void i2c_hid_free_buffers(struct i2c_hid *ihid)
  408. {
  409. kfree(ihid->inbuf);
  410. kfree(ihid->argsbuf);
  411. kfree(ihid->cmdbuf);
  412. ihid->inbuf = NULL;
  413. ihid->cmdbuf = NULL;
  414. ihid->argsbuf = NULL;
  415. }
  416. static int i2c_hid_get_raw_report(struct hid_device *hid,
  417. unsigned char report_number, __u8 *buf, size_t count,
  418. unsigned char report_type)
  419. {
  420. struct i2c_client *client = hid->driver_data;
  421. struct i2c_hid *ihid = i2c_get_clientdata(client);
  422. int ret;
  423. if (report_type == HID_OUTPUT_REPORT)
  424. return -EINVAL;
  425. if (count > ihid->bufsize)
  426. count = ihid->bufsize;
  427. ret = i2c_hid_get_report(client,
  428. report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  429. report_number, ihid->inbuf, count);
  430. if (ret < 0)
  431. return ret;
  432. count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
  433. memcpy(buf, ihid->inbuf + 2, count);
  434. return count;
  435. }
  436. static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
  437. size_t count, unsigned char report_type)
  438. {
  439. struct i2c_client *client = hid->driver_data;
  440. int report_id = buf[0];
  441. if (report_type == HID_INPUT_REPORT)
  442. return -EINVAL;
  443. return i2c_hid_set_report(client,
  444. report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
  445. report_id, buf, count);
  446. }
  447. static int i2c_hid_parse(struct hid_device *hid)
  448. {
  449. struct i2c_client *client = hid->driver_data;
  450. struct i2c_hid *ihid = i2c_get_clientdata(client);
  451. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  452. unsigned int rsize;
  453. char *rdesc;
  454. int ret;
  455. int tries = 3;
  456. i2c_hid_dbg(ihid, "entering %s\n", __func__);
  457. rsize = le16_to_cpu(hdesc->wReportDescLength);
  458. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  459. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  460. return -EINVAL;
  461. }
  462. do {
  463. ret = i2c_hid_hwreset(client);
  464. if (ret)
  465. msleep(1000);
  466. } while (tries-- > 0 && ret);
  467. if (ret)
  468. return ret;
  469. rdesc = kzalloc(rsize, GFP_KERNEL);
  470. if (!rdesc) {
  471. dbg_hid("couldn't allocate rdesc memory\n");
  472. return -ENOMEM;
  473. }
  474. i2c_hid_dbg(ihid, "asking HID report descriptor\n");
  475. ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
  476. if (ret) {
  477. hid_err(hid, "reading report descriptor failed\n");
  478. kfree(rdesc);
  479. return -EIO;
  480. }
  481. i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
  482. ret = hid_parse_report(hid, rdesc, rsize);
  483. kfree(rdesc);
  484. if (ret) {
  485. dbg_hid("parsing report descriptor failed\n");
  486. return ret;
  487. }
  488. return 0;
  489. }
  490. static int i2c_hid_start(struct hid_device *hid)
  491. {
  492. struct i2c_client *client = hid->driver_data;
  493. struct i2c_hid *ihid = i2c_get_clientdata(client);
  494. int ret;
  495. int old_bufsize = ihid->bufsize;
  496. ihid->bufsize = HID_MIN_BUFFER_SIZE;
  497. i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &ihid->bufsize);
  498. i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &ihid->bufsize);
  499. i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &ihid->bufsize);
  500. if (ihid->bufsize > old_bufsize || !ihid->inbuf || !ihid->cmdbuf) {
  501. i2c_hid_free_buffers(ihid);
  502. ret = i2c_hid_alloc_buffers(ihid);
  503. if (ret) {
  504. ihid->bufsize = old_bufsize;
  505. return ret;
  506. }
  507. }
  508. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  509. i2c_hid_init_reports(hid);
  510. return 0;
  511. }
  512. static void i2c_hid_stop(struct hid_device *hid)
  513. {
  514. struct i2c_client *client = hid->driver_data;
  515. struct i2c_hid *ihid = i2c_get_clientdata(client);
  516. hid->claimed = 0;
  517. i2c_hid_free_buffers(ihid);
  518. }
  519. static int i2c_hid_open(struct hid_device *hid)
  520. {
  521. struct i2c_client *client = hid->driver_data;
  522. struct i2c_hid *ihid = i2c_get_clientdata(client);
  523. int ret;
  524. if (!hid->open++) {
  525. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  526. if (ret) {
  527. hid->open--;
  528. return -EIO;
  529. }
  530. set_bit(I2C_HID_STARTED, &ihid->flags);
  531. }
  532. return 0;
  533. }
  534. static void i2c_hid_close(struct hid_device *hid)
  535. {
  536. struct i2c_client *client = hid->driver_data;
  537. struct i2c_hid *ihid = i2c_get_clientdata(client);
  538. /* protecting hid->open to make sure we don't restart
  539. * data acquistion due to a resumption we no longer
  540. * care about
  541. */
  542. if (!--hid->open) {
  543. clear_bit(I2C_HID_STARTED, &ihid->flags);
  544. /* Save some power */
  545. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  546. }
  547. }
  548. static int i2c_hid_power(struct hid_device *hid, int lvl)
  549. {
  550. struct i2c_client *client = hid->driver_data;
  551. struct i2c_hid *ihid = i2c_get_clientdata(client);
  552. int ret = 0;
  553. i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
  554. switch (lvl) {
  555. case PM_HINT_FULLON:
  556. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  557. break;
  558. case PM_HINT_NORMAL:
  559. ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  560. break;
  561. }
  562. return ret;
  563. }
  564. static int i2c_hid_hidinput_input_event(struct input_dev *dev,
  565. unsigned int type, unsigned int code, int value)
  566. {
  567. struct hid_device *hid = input_get_drvdata(dev);
  568. struct hid_field *field;
  569. int offset;
  570. if (type == EV_FF)
  571. return input_ff_event(dev, type, code, value);
  572. if (type != EV_LED)
  573. return -1;
  574. offset = hidinput_find_field(hid, type, code, &field);
  575. if (offset == -1) {
  576. hid_warn(dev, "event field not found\n");
  577. return -1;
  578. }
  579. return hid_set_field(field, offset, value);
  580. }
  581. static struct hid_ll_driver i2c_hid_ll_driver = {
  582. .parse = i2c_hid_parse,
  583. .start = i2c_hid_start,
  584. .stop = i2c_hid_stop,
  585. .open = i2c_hid_open,
  586. .close = i2c_hid_close,
  587. .power = i2c_hid_power,
  588. .hidinput_input_event = i2c_hid_hidinput_input_event,
  589. };
  590. static int __devinit i2c_hid_init_irq(struct i2c_client *client)
  591. {
  592. struct i2c_hid *ihid = i2c_get_clientdata(client);
  593. int ret;
  594. dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
  595. ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
  596. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  597. client->name, ihid);
  598. if (ret < 0) {
  599. dev_dbg(&client->dev,
  600. "Could not register for %s interrupt, irq = %d,"
  601. " ret = %d\n",
  602. client->name, client->irq, ret);
  603. return ret;
  604. }
  605. ihid->irq = client->irq;
  606. return 0;
  607. }
  608. static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
  609. {
  610. struct i2c_client *client = ihid->client;
  611. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  612. unsigned int dsize;
  613. int ret;
  614. /* Fetch the length of HID description, retrieve the 4 first bytes:
  615. * bytes 0-1 -> length
  616. * bytes 2-3 -> bcdVersion (has to be 1.00) */
  617. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
  618. i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
  619. __func__, 4, ihid->hdesc_buffer);
  620. if (ret) {
  621. dev_err(&client->dev, "HID_DESCR_LENGTH_CMD Fail (ret=%d)\n",
  622. ret);
  623. return -ENODEV;
  624. }
  625. dsize = le16_to_cpu(hdesc->wHIDDescLength);
  626. if (!dsize || dsize > HID_MAX_DESCRIPTOR_SIZE) {
  627. dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
  628. dsize);
  629. return -ENODEV;
  630. }
  631. /* check bcdVersion == 1.0 */
  632. if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
  633. dev_err(&client->dev,
  634. "unexpected HID descriptor bcdVersion (0x%04x)\n",
  635. le16_to_cpu(hdesc->bcdVersion));
  636. return -ENODEV;
  637. }
  638. i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
  639. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
  640. dsize);
  641. if (ret) {
  642. dev_err(&client->dev, "hid_descr_cmd Fail\n");
  643. return -ENODEV;
  644. }
  645. i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
  646. return 0;
  647. }
  648. static int __devinit i2c_hid_probe(struct i2c_client *client,
  649. const struct i2c_device_id *dev_id)
  650. {
  651. int ret;
  652. struct i2c_hid *ihid;
  653. struct hid_device *hid;
  654. __u16 hidRegister;
  655. struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
  656. dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
  657. if (!platform_data) {
  658. dev_err(&client->dev, "HID register address not provided\n");
  659. return -EINVAL;
  660. }
  661. if (!client->irq) {
  662. dev_err(&client->dev,
  663. "HID over i2c has not been provided an Int IRQ\n");
  664. return -EINVAL;
  665. }
  666. ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
  667. if (!ihid)
  668. return -ENOMEM;
  669. i2c_set_clientdata(client, ihid);
  670. ihid->client = client;
  671. hidRegister = platform_data->hid_descriptor_address;
  672. ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
  673. init_waitqueue_head(&ihid->wait);
  674. /* we need to allocate the command buffer without knowing the maximum
  675. * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
  676. * real computation later. */
  677. ihid->bufsize = HID_MIN_BUFFER_SIZE;
  678. i2c_hid_alloc_buffers(ihid);
  679. ret = i2c_hid_fetch_hid_descriptor(ihid);
  680. if (ret < 0)
  681. goto err;
  682. ret = i2c_hid_init_irq(client);
  683. if (ret < 0)
  684. goto err;
  685. hid = hid_allocate_device();
  686. if (IS_ERR(hid)) {
  687. ret = PTR_ERR(hid);
  688. goto err;
  689. }
  690. ihid->hid = hid;
  691. hid->driver_data = client;
  692. hid->ll_driver = &i2c_hid_ll_driver;
  693. hid->hid_get_raw_report = i2c_hid_get_raw_report;
  694. hid->hid_output_raw_report = i2c_hid_output_raw_report;
  695. hid->dev.parent = &client->dev;
  696. hid->bus = BUS_I2C;
  697. hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
  698. hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
  699. hid->product = le16_to_cpu(ihid->hdesc.wProductID);
  700. snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
  701. client->name, hid->vendor, hid->product);
  702. ret = hid_add_device(hid);
  703. if (ret) {
  704. if (ret != -ENODEV)
  705. hid_err(client, "can't add hid device: %d\n", ret);
  706. goto err_mem_free;
  707. }
  708. return 0;
  709. err_mem_free:
  710. hid_destroy_device(hid);
  711. err:
  712. if (ihid->irq)
  713. free_irq(ihid->irq, ihid);
  714. i2c_hid_free_buffers(ihid);
  715. kfree(ihid);
  716. return ret;
  717. }
  718. static int __devexit i2c_hid_remove(struct i2c_client *client)
  719. {
  720. struct i2c_hid *ihid = i2c_get_clientdata(client);
  721. struct hid_device *hid;
  722. if (WARN_ON(!ihid))
  723. return -1;
  724. hid = ihid->hid;
  725. hid_destroy_device(hid);
  726. free_irq(client->irq, ihid);
  727. kfree(ihid);
  728. return 0;
  729. }
  730. #ifdef CONFIG_PM_SLEEP
  731. static int i2c_hid_suspend(struct device *dev)
  732. {
  733. struct i2c_client *client = to_i2c_client(dev);
  734. struct i2c_hid *ihid = i2c_get_clientdata(client);
  735. if (device_may_wakeup(&client->dev))
  736. enable_irq_wake(ihid->irq);
  737. /* Save some power */
  738. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  739. return 0;
  740. }
  741. static int i2c_hid_resume(struct device *dev)
  742. {
  743. int ret;
  744. struct i2c_client *client = to_i2c_client(dev);
  745. ret = i2c_hid_hwreset(client);
  746. if (ret)
  747. return ret;
  748. if (device_may_wakeup(&client->dev))
  749. disable_irq_wake(client->irq);
  750. return 0;
  751. }
  752. #endif
  753. static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
  754. static const struct i2c_device_id i2c_hid_id_table[] = {
  755. { "hid", 0 },
  756. { },
  757. };
  758. MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
  759. static struct i2c_driver i2c_hid_driver = {
  760. .driver = {
  761. .name = "i2c_hid",
  762. .owner = THIS_MODULE,
  763. .pm = &i2c_hid_pm,
  764. },
  765. .probe = i2c_hid_probe,
  766. .remove = __devexit_p(i2c_hid_remove),
  767. .id_table = i2c_hid_id_table,
  768. };
  769. module_i2c_driver(i2c_hid_driver);
  770. MODULE_DESCRIPTION("HID over I2C core driver");
  771. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  772. MODULE_LICENSE("GPL");