i2c-hid.c 23 KB

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