i2c-hid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * HID over I2C protocol implementation
  3. *
  4. * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  5. * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
  6. * Copyright (c) 2012 Red Hat, Inc
  7. *
  8. * This code is partly based on "USB HID support for Linux":
  9. *
  10. * Copyright (c) 1999 Andreas Gal
  11. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  12. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  13. * Copyright (c) 2007-2008 Oliver Neukum
  14. * Copyright (c) 2006-2010 Jiri Kosina
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file COPYING in the main directory of this archive for
  18. * more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/input.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/pm.h>
  27. #include <linux/device.h>
  28. #include <linux/wait.h>
  29. #include <linux/err.h>
  30. #include <linux/string.h>
  31. #include <linux/list.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/kernel.h>
  34. #include <linux/hid.h>
  35. #include <linux/i2c/i2c-hid.h>
  36. /* flags */
  37. #define I2C_HID_STARTED (1 << 0)
  38. #define I2C_HID_RESET_PENDING (1 << 1)
  39. #define I2C_HID_READ_PENDING (1 << 2)
  40. #define I2C_HID_PWR_ON 0x00
  41. #define I2C_HID_PWR_SLEEP 0x01
  42. /* debug option */
  43. static bool debug;
  44. module_param(debug, bool, 0444);
  45. MODULE_PARM_DESC(debug, "print a lot of debug information");
  46. #define i2c_hid_dbg(ihid, fmt, arg...) \
  47. do { \
  48. if (debug) \
  49. dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
  50. } while (0)
  51. struct i2c_hid_desc {
  52. __le16 wHIDDescLength;
  53. __le16 bcdVersion;
  54. __le16 wReportDescLength;
  55. __le16 wReportDescRegister;
  56. __le16 wInputRegister;
  57. __le16 wMaxInputLength;
  58. __le16 wOutputRegister;
  59. __le16 wMaxOutputLength;
  60. __le16 wCommandRegister;
  61. __le16 wDataRegister;
  62. __le16 wVendorID;
  63. __le16 wProductID;
  64. __le16 wVersionID;
  65. __le32 reserved;
  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. wait_queue_head_t wait; /* For waiting the interrupt */
  124. };
  125. static int __i2c_hid_command(struct i2c_client *client,
  126. const struct i2c_hid_cmd *command, u8 reportID,
  127. u8 reportType, u8 *args, int args_len,
  128. unsigned char *buf_recv, int data_len)
  129. {
  130. struct i2c_hid *ihid = i2c_get_clientdata(client);
  131. union command *cmd = (union command *)ihid->cmdbuf;
  132. int ret;
  133. struct i2c_msg msg[2];
  134. int msg_num = 1;
  135. int length = command->length;
  136. bool wait = command->wait;
  137. unsigned int registerIndex = command->registerIndex;
  138. /* special case for hid_descr_cmd */
  139. if (command == &hid_descr_cmd) {
  140. cmd->c.reg = ihid->wHIDDescRegister;
  141. } else {
  142. cmd->data[0] = ihid->hdesc_buffer[registerIndex];
  143. cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
  144. }
  145. if (length > 2) {
  146. cmd->c.opcode = command->opcode;
  147. cmd->c.reportTypeID = reportID | reportType << 4;
  148. }
  149. memcpy(cmd->data + length, args, args_len);
  150. length += args_len;
  151. i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
  152. msg[0].addr = client->addr;
  153. msg[0].flags = client->flags & I2C_M_TEN;
  154. msg[0].len = length;
  155. msg[0].buf = cmd->data;
  156. if (data_len > 0) {
  157. msg[1].addr = client->addr;
  158. msg[1].flags = client->flags & I2C_M_TEN;
  159. msg[1].flags |= I2C_M_RD;
  160. msg[1].len = data_len;
  161. msg[1].buf = buf_recv;
  162. msg_num = 2;
  163. set_bit(I2C_HID_READ_PENDING, &ihid->flags);
  164. }
  165. if (wait)
  166. set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
  167. ret = i2c_transfer(client->adapter, msg, msg_num);
  168. if (data_len > 0)
  169. clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
  170. if (ret != msg_num)
  171. return ret < 0 ? ret : -EIO;
  172. ret = 0;
  173. if (wait) {
  174. i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
  175. if (!wait_event_timeout(ihid->wait,
  176. !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
  177. msecs_to_jiffies(5000)))
  178. ret = -ENODATA;
  179. i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
  180. }
  181. return ret;
  182. }
  183. static int i2c_hid_command(struct i2c_client *client,
  184. const struct i2c_hid_cmd *command,
  185. unsigned char *buf_recv, int data_len)
  186. {
  187. return __i2c_hid_command(client, command, 0, 0, NULL, 0,
  188. buf_recv, data_len);
  189. }
  190. static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
  191. u8 reportID, unsigned char *buf_recv, int data_len)
  192. {
  193. struct i2c_hid *ihid = i2c_get_clientdata(client);
  194. u8 args[3];
  195. int ret;
  196. int args_len = 0;
  197. u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
  198. i2c_hid_dbg(ihid, "%s\n", __func__);
  199. if (reportID >= 0x0F) {
  200. args[args_len++] = reportID;
  201. reportID = 0x0F;
  202. }
  203. args[args_len++] = readRegister & 0xFF;
  204. args[args_len++] = readRegister >> 8;
  205. ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
  206. reportType, args, args_len, buf_recv, data_len);
  207. if (ret) {
  208. dev_err(&client->dev,
  209. "failed to retrieve report from device.\n");
  210. return ret;
  211. }
  212. return 0;
  213. }
  214. static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
  215. u8 reportID, unsigned char *buf, size_t data_len)
  216. {
  217. struct i2c_hid *ihid = i2c_get_clientdata(client);
  218. u8 *args = ihid->argsbuf;
  219. int ret;
  220. u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
  221. /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
  222. u16 size = 2 /* size */ +
  223. (reportID ? 1 : 0) /* reportID */ +
  224. data_len /* buf */;
  225. int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
  226. 2 /* dataRegister */ +
  227. size /* args */;
  228. int index = 0;
  229. i2c_hid_dbg(ihid, "%s\n", __func__);
  230. if (reportID >= 0x0F) {
  231. args[index++] = reportID;
  232. reportID = 0x0F;
  233. }
  234. args[index++] = dataRegister & 0xFF;
  235. args[index++] = dataRegister >> 8;
  236. args[index++] = size & 0xFF;
  237. args[index++] = size >> 8;
  238. if (reportID)
  239. args[index++] = reportID;
  240. memcpy(&args[index], buf, data_len);
  241. ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
  242. reportType, args, args_len, NULL, 0);
  243. if (ret) {
  244. dev_err(&client->dev, "failed to set a report to device.\n");
  245. return ret;
  246. }
  247. return data_len;
  248. }
  249. static int i2c_hid_set_power(struct i2c_client *client, int power_state)
  250. {
  251. struct i2c_hid *ihid = i2c_get_clientdata(client);
  252. int ret;
  253. i2c_hid_dbg(ihid, "%s\n", __func__);
  254. ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
  255. 0, NULL, 0, NULL, 0);
  256. if (ret)
  257. dev_err(&client->dev, "failed to change power setting.\n");
  258. return ret;
  259. }
  260. static int i2c_hid_hwreset(struct i2c_client *client)
  261. {
  262. struct i2c_hid *ihid = i2c_get_clientdata(client);
  263. int ret;
  264. i2c_hid_dbg(ihid, "%s\n", __func__);
  265. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  266. if (ret)
  267. return ret;
  268. i2c_hid_dbg(ihid, "resetting...\n");
  269. ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
  270. if (ret) {
  271. dev_err(&client->dev, "failed to reset device.\n");
  272. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. static void i2c_hid_get_input(struct i2c_hid *ihid)
  278. {
  279. int ret, ret_size;
  280. int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
  281. ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
  282. if (ret != size) {
  283. if (ret < 0)
  284. return;
  285. dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
  286. __func__, ret, size);
  287. return;
  288. }
  289. ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
  290. if (!ret_size) {
  291. /* host or device initiated RESET completed */
  292. if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
  293. wake_up(&ihid->wait);
  294. return;
  295. }
  296. if (ret_size > size) {
  297. dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
  298. __func__, size, ret_size);
  299. return;
  300. }
  301. i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
  302. if (test_bit(I2C_HID_STARTED, &ihid->flags))
  303. hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
  304. ret_size - 2, 1);
  305. return;
  306. }
  307. static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
  308. {
  309. struct i2c_hid *ihid = dev_id;
  310. if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
  311. return IRQ_HANDLED;
  312. i2c_hid_get_input(ihid);
  313. return IRQ_HANDLED;
  314. }
  315. static int i2c_hid_get_report_length(struct hid_report *report)
  316. {
  317. return ((report->size - 1) >> 3) + 1 +
  318. report->device->report_enum[report->type].numbered + 2;
  319. }
  320. static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
  321. size_t bufsize)
  322. {
  323. struct hid_device *hid = report->device;
  324. struct i2c_client *client = hid->driver_data;
  325. struct i2c_hid *ihid = i2c_get_clientdata(client);
  326. unsigned int size, ret_size;
  327. size = i2c_hid_get_report_length(report);
  328. if (i2c_hid_get_report(client,
  329. report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  330. report->id, buffer, size))
  331. return;
  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 void i2c_hid_free_buffers(struct i2c_hid *ihid)
  382. {
  383. kfree(ihid->inbuf);
  384. kfree(ihid->argsbuf);
  385. kfree(ihid->cmdbuf);
  386. ihid->inbuf = NULL;
  387. ihid->cmdbuf = NULL;
  388. ihid->argsbuf = NULL;
  389. ihid->bufsize = 0;
  390. }
  391. static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
  392. {
  393. /* the worst case is computed from the set_report command with a
  394. * reportID > 15 and the maximum report length */
  395. int args_len = sizeof(__u8) + /* optional ReportID byte */
  396. sizeof(__u16) + /* data register */
  397. sizeof(__u16) + /* size of the report */
  398. report_size; /* report */
  399. ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
  400. ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
  401. ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
  402. if (!ihid->inbuf || !ihid->argsbuf || !ihid->cmdbuf) {
  403. i2c_hid_free_buffers(ihid);
  404. return -ENOMEM;
  405. }
  406. ihid->bufsize = report_size;
  407. return 0;
  408. }
  409. static int i2c_hid_get_raw_report(struct hid_device *hid,
  410. unsigned char report_number, __u8 *buf, size_t count,
  411. unsigned char report_type)
  412. {
  413. struct i2c_client *client = hid->driver_data;
  414. struct i2c_hid *ihid = i2c_get_clientdata(client);
  415. size_t ret_count, ask_count;
  416. int ret;
  417. if (report_type == HID_OUTPUT_REPORT)
  418. return -EINVAL;
  419. /* +2 bytes to include the size of the reply in the query buffer */
  420. ask_count = min(count + 2, (size_t)ihid->bufsize);
  421. ret = i2c_hid_get_report(client,
  422. report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  423. report_number, ihid->inbuf, ask_count);
  424. if (ret < 0)
  425. return ret;
  426. ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
  427. if (!ret_count)
  428. return 0;
  429. ret_count = min(ret_count, ask_count);
  430. /* The query buffer contains the size, dropping it in the reply */
  431. count = min(count, ret_count - 2);
  432. memcpy(buf, ihid->inbuf + 2, count);
  433. return count;
  434. }
  435. static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
  436. size_t count, unsigned char report_type)
  437. {
  438. struct i2c_client *client = hid->driver_data;
  439. int report_id = buf[0];
  440. if (report_type == HID_INPUT_REPORT)
  441. return -EINVAL;
  442. return i2c_hid_set_report(client,
  443. report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
  444. report_id, buf, count);
  445. }
  446. static int i2c_hid_parse(struct hid_device *hid)
  447. {
  448. struct i2c_client *client = hid->driver_data;
  449. struct i2c_hid *ihid = i2c_get_clientdata(client);
  450. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  451. unsigned int rsize;
  452. char *rdesc;
  453. int ret;
  454. int tries = 3;
  455. i2c_hid_dbg(ihid, "entering %s\n", __func__);
  456. rsize = le16_to_cpu(hdesc->wReportDescLength);
  457. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  458. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  459. return -EINVAL;
  460. }
  461. do {
  462. ret = i2c_hid_hwreset(client);
  463. if (ret)
  464. msleep(1000);
  465. } while (tries-- > 0 && ret);
  466. if (ret)
  467. return ret;
  468. rdesc = kzalloc(rsize, GFP_KERNEL);
  469. if (!rdesc) {
  470. dbg_hid("couldn't allocate rdesc memory\n");
  471. return -ENOMEM;
  472. }
  473. i2c_hid_dbg(ihid, "asking HID report descriptor\n");
  474. ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
  475. if (ret) {
  476. hid_err(hid, "reading report descriptor failed\n");
  477. kfree(rdesc);
  478. return -EIO;
  479. }
  480. i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
  481. ret = hid_parse_report(hid, rdesc, rsize);
  482. kfree(rdesc);
  483. if (ret) {
  484. dbg_hid("parsing report descriptor failed\n");
  485. return ret;
  486. }
  487. return 0;
  488. }
  489. static int i2c_hid_start(struct hid_device *hid)
  490. {
  491. struct i2c_client *client = hid->driver_data;
  492. struct i2c_hid *ihid = i2c_get_clientdata(client);
  493. int ret;
  494. unsigned int bufsize = HID_MIN_BUFFER_SIZE;
  495. i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
  496. i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
  497. i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
  498. if (bufsize > ihid->bufsize) {
  499. i2c_hid_free_buffers(ihid);
  500. ret = i2c_hid_alloc_buffers(ihid, bufsize);
  501. if (ret)
  502. return ret;
  503. }
  504. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  505. i2c_hid_init_reports(hid);
  506. return 0;
  507. }
  508. static void i2c_hid_stop(struct hid_device *hid)
  509. {
  510. struct i2c_client *client = hid->driver_data;
  511. struct i2c_hid *ihid = i2c_get_clientdata(client);
  512. hid->claimed = 0;
  513. i2c_hid_free_buffers(ihid);
  514. }
  515. static int i2c_hid_open(struct hid_device *hid)
  516. {
  517. struct i2c_client *client = hid->driver_data;
  518. struct i2c_hid *ihid = i2c_get_clientdata(client);
  519. int ret;
  520. if (!hid->open++) {
  521. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  522. if (ret) {
  523. hid->open--;
  524. return -EIO;
  525. }
  526. set_bit(I2C_HID_STARTED, &ihid->flags);
  527. }
  528. return 0;
  529. }
  530. static void i2c_hid_close(struct hid_device *hid)
  531. {
  532. struct i2c_client *client = hid->driver_data;
  533. struct i2c_hid *ihid = i2c_get_clientdata(client);
  534. /* protecting hid->open to make sure we don't restart
  535. * data acquistion due to a resumption we no longer
  536. * care about
  537. */
  538. if (!--hid->open) {
  539. clear_bit(I2C_HID_STARTED, &ihid->flags);
  540. /* Save some power */
  541. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  542. }
  543. }
  544. static int i2c_hid_power(struct hid_device *hid, int lvl)
  545. {
  546. struct i2c_client *client = hid->driver_data;
  547. struct i2c_hid *ihid = i2c_get_clientdata(client);
  548. int ret = 0;
  549. i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
  550. switch (lvl) {
  551. case PM_HINT_FULLON:
  552. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  553. break;
  554. case PM_HINT_NORMAL:
  555. ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  556. break;
  557. }
  558. return ret;
  559. }
  560. static int i2c_hid_hidinput_input_event(struct input_dev *dev,
  561. unsigned int type, unsigned int code, int value)
  562. {
  563. struct hid_device *hid = input_get_drvdata(dev);
  564. struct hid_field *field;
  565. int offset;
  566. if (type == EV_FF)
  567. return input_ff_event(dev, type, code, value);
  568. if (type != EV_LED)
  569. return -1;
  570. offset = hidinput_find_field(hid, type, code, &field);
  571. if (offset == -1) {
  572. hid_warn(dev, "event field not found\n");
  573. return -1;
  574. }
  575. return hid_set_field(field, offset, value);
  576. }
  577. static struct hid_ll_driver i2c_hid_ll_driver = {
  578. .parse = i2c_hid_parse,
  579. .start = i2c_hid_start,
  580. .stop = i2c_hid_stop,
  581. .open = i2c_hid_open,
  582. .close = i2c_hid_close,
  583. .power = i2c_hid_power,
  584. .hidinput_input_event = i2c_hid_hidinput_input_event,
  585. };
  586. static int __devinit i2c_hid_init_irq(struct i2c_client *client)
  587. {
  588. struct i2c_hid *ihid = i2c_get_clientdata(client);
  589. int ret;
  590. dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
  591. ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
  592. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  593. client->name, ihid);
  594. if (ret < 0) {
  595. dev_warn(&client->dev,
  596. "Could not register for %s interrupt, irq = %d,"
  597. " ret = %d\n",
  598. client->name, client->irq, ret);
  599. return ret;
  600. }
  601. return 0;
  602. }
  603. static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
  604. {
  605. struct i2c_client *client = ihid->client;
  606. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  607. unsigned int dsize;
  608. int ret;
  609. /* Fetch the length of HID description, retrieve the 4 first bytes:
  610. * bytes 0-1 -> length
  611. * bytes 2-3 -> bcdVersion (has to be 1.00) */
  612. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
  613. i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
  614. __func__, 4, ihid->hdesc_buffer);
  615. if (ret) {
  616. dev_err(&client->dev,
  617. "unable to fetch the size of HID descriptor (ret=%d)\n",
  618. ret);
  619. return -ENODEV;
  620. }
  621. dsize = le16_to_cpu(hdesc->wHIDDescLength);
  622. /*
  623. * the size of the HID descriptor should at least contain
  624. * its size and the bcdVersion (4 bytes), and should not be greater
  625. * than sizeof(struct i2c_hid_desc) as we directly fill this struct
  626. * through i2c_hid_command.
  627. */
  628. if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
  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. ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
  680. if (ret < 0)
  681. goto err;
  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_irq;
  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 %04hX:%04hX",
  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_irq:
  715. free_irq(client->irq, ihid);
  716. err:
  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. hid = ihid->hid;
  726. hid_destroy_device(hid);
  727. free_irq(client->irq, ihid);
  728. if (ihid->bufsize)
  729. i2c_hid_free_buffers(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. if (device_may_wakeup(&client->dev))
  738. enable_irq_wake(client->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");