i2c-hid.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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/mutex.h>
  36. #include <linux/acpi.h>
  37. #include <linux/i2c/i2c-hid.h>
  38. /* flags */
  39. #define I2C_HID_STARTED (1 << 0)
  40. #define I2C_HID_RESET_PENDING (1 << 1)
  41. #define I2C_HID_READ_PENDING (1 << 2)
  42. #define I2C_HID_PWR_ON 0x00
  43. #define I2C_HID_PWR_SLEEP 0x01
  44. /* debug option */
  45. static bool debug;
  46. module_param(debug, bool, 0444);
  47. MODULE_PARM_DESC(debug, "print a lot of debug information");
  48. #define i2c_hid_dbg(ihid, fmt, arg...) \
  49. do { \
  50. if (debug) \
  51. dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
  52. } while (0)
  53. struct i2c_hid_desc {
  54. __le16 wHIDDescLength;
  55. __le16 bcdVersion;
  56. __le16 wReportDescLength;
  57. __le16 wReportDescRegister;
  58. __le16 wInputRegister;
  59. __le16 wMaxInputLength;
  60. __le16 wOutputRegister;
  61. __le16 wMaxOutputLength;
  62. __le16 wCommandRegister;
  63. __le16 wDataRegister;
  64. __le16 wVendorID;
  65. __le16 wProductID;
  66. __le16 wVersionID;
  67. __le32 reserved;
  68. } __packed;
  69. struct i2c_hid_cmd {
  70. unsigned int registerIndex;
  71. __u8 opcode;
  72. unsigned int length;
  73. bool wait;
  74. };
  75. union command {
  76. u8 data[0];
  77. struct cmd {
  78. __le16 reg;
  79. __u8 reportTypeID;
  80. __u8 opcode;
  81. } __packed c;
  82. };
  83. #define I2C_HID_CMD(opcode_) \
  84. .opcode = opcode_, .length = 4, \
  85. .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
  86. /* fetch HID descriptor */
  87. static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
  88. /* fetch report descriptors */
  89. static const struct i2c_hid_cmd hid_report_descr_cmd = {
  90. .registerIndex = offsetof(struct i2c_hid_desc,
  91. wReportDescRegister),
  92. .opcode = 0x00,
  93. .length = 2 };
  94. /* commands */
  95. static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
  96. .wait = true };
  97. static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
  98. static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
  99. static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
  100. /*
  101. * These definitions are not used here, but are defined by the spec.
  102. * Keeping them here for documentation purposes.
  103. *
  104. * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
  105. * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
  106. * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
  107. * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
  108. */
  109. static DEFINE_MUTEX(i2c_hid_open_mut);
  110. /* The main device structure */
  111. struct i2c_hid {
  112. struct i2c_client *client; /* i2c client */
  113. struct hid_device *hid; /* pointer to corresponding HID dev */
  114. union {
  115. __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
  116. struct i2c_hid_desc hdesc; /* the HID Descriptor */
  117. };
  118. __le16 wHIDDescRegister; /* location of the i2c
  119. * register of the HID
  120. * descriptor. */
  121. unsigned int bufsize; /* i2c buffer size */
  122. char *inbuf; /* Input buffer */
  123. char *cmdbuf; /* Command buffer */
  124. char *argsbuf; /* Command arguments buffer */
  125. unsigned long flags; /* device flags */
  126. wait_queue_head_t wait; /* For waiting the interrupt */
  127. struct i2c_hid_platform_data pdata;
  128. };
  129. static int __i2c_hid_command(struct i2c_client *client,
  130. const struct i2c_hid_cmd *command, u8 reportID,
  131. u8 reportType, u8 *args, int args_len,
  132. unsigned char *buf_recv, int data_len)
  133. {
  134. struct i2c_hid *ihid = i2c_get_clientdata(client);
  135. union command *cmd = (union command *)ihid->cmdbuf;
  136. int ret;
  137. struct i2c_msg msg[2];
  138. int msg_num = 1;
  139. int length = command->length;
  140. bool wait = command->wait;
  141. unsigned int registerIndex = command->registerIndex;
  142. /* special case for hid_descr_cmd */
  143. if (command == &hid_descr_cmd) {
  144. cmd->c.reg = ihid->wHIDDescRegister;
  145. } else {
  146. cmd->data[0] = ihid->hdesc_buffer[registerIndex];
  147. cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
  148. }
  149. if (length > 2) {
  150. cmd->c.opcode = command->opcode;
  151. cmd->c.reportTypeID = reportID | reportType << 4;
  152. }
  153. memcpy(cmd->data + length, args, args_len);
  154. length += args_len;
  155. i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
  156. msg[0].addr = client->addr;
  157. msg[0].flags = client->flags & I2C_M_TEN;
  158. msg[0].len = length;
  159. msg[0].buf = cmd->data;
  160. if (data_len > 0) {
  161. msg[1].addr = client->addr;
  162. msg[1].flags = client->flags & I2C_M_TEN;
  163. msg[1].flags |= I2C_M_RD;
  164. msg[1].len = data_len;
  165. msg[1].buf = buf_recv;
  166. msg_num = 2;
  167. set_bit(I2C_HID_READ_PENDING, &ihid->flags);
  168. }
  169. if (wait)
  170. set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
  171. ret = i2c_transfer(client->adapter, msg, msg_num);
  172. if (data_len > 0)
  173. clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
  174. if (ret != msg_num)
  175. return ret < 0 ? ret : -EIO;
  176. ret = 0;
  177. if (wait) {
  178. i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
  179. if (!wait_event_timeout(ihid->wait,
  180. !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
  181. msecs_to_jiffies(5000)))
  182. ret = -ENODATA;
  183. i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
  184. }
  185. return ret;
  186. }
  187. static int i2c_hid_command(struct i2c_client *client,
  188. const struct i2c_hid_cmd *command,
  189. unsigned char *buf_recv, int data_len)
  190. {
  191. return __i2c_hid_command(client, command, 0, 0, NULL, 0,
  192. buf_recv, data_len);
  193. }
  194. static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
  195. u8 reportID, unsigned char *buf_recv, int data_len)
  196. {
  197. struct i2c_hid *ihid = i2c_get_clientdata(client);
  198. u8 args[3];
  199. int ret;
  200. int args_len = 0;
  201. u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
  202. i2c_hid_dbg(ihid, "%s\n", __func__);
  203. if (reportID >= 0x0F) {
  204. args[args_len++] = reportID;
  205. reportID = 0x0F;
  206. }
  207. args[args_len++] = readRegister & 0xFF;
  208. args[args_len++] = readRegister >> 8;
  209. ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
  210. reportType, args, args_len, buf_recv, data_len);
  211. if (ret) {
  212. dev_err(&client->dev,
  213. "failed to retrieve report from device.\n");
  214. return ret;
  215. }
  216. return 0;
  217. }
  218. static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
  219. u8 reportID, unsigned char *buf, size_t data_len)
  220. {
  221. struct i2c_hid *ihid = i2c_get_clientdata(client);
  222. u8 *args = ihid->argsbuf;
  223. int ret;
  224. u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
  225. /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
  226. u16 size = 2 /* size */ +
  227. (reportID ? 1 : 0) /* reportID */ +
  228. data_len /* buf */;
  229. int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
  230. 2 /* dataRegister */ +
  231. size /* args */;
  232. int index = 0;
  233. i2c_hid_dbg(ihid, "%s\n", __func__);
  234. if (reportID >= 0x0F) {
  235. args[index++] = reportID;
  236. reportID = 0x0F;
  237. }
  238. args[index++] = dataRegister & 0xFF;
  239. args[index++] = dataRegister >> 8;
  240. args[index++] = size & 0xFF;
  241. args[index++] = size >> 8;
  242. if (reportID)
  243. args[index++] = reportID;
  244. memcpy(&args[index], buf, data_len);
  245. ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
  246. reportType, args, args_len, NULL, 0);
  247. if (ret) {
  248. dev_err(&client->dev, "failed to set a report to device.\n");
  249. return ret;
  250. }
  251. return data_len;
  252. }
  253. static int i2c_hid_set_power(struct i2c_client *client, int power_state)
  254. {
  255. struct i2c_hid *ihid = i2c_get_clientdata(client);
  256. int ret;
  257. i2c_hid_dbg(ihid, "%s\n", __func__);
  258. ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
  259. 0, NULL, 0, NULL, 0);
  260. if (ret)
  261. dev_err(&client->dev, "failed to change power setting.\n");
  262. return ret;
  263. }
  264. static int i2c_hid_hwreset(struct i2c_client *client)
  265. {
  266. struct i2c_hid *ihid = i2c_get_clientdata(client);
  267. int ret;
  268. i2c_hid_dbg(ihid, "%s\n", __func__);
  269. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  270. if (ret)
  271. return ret;
  272. i2c_hid_dbg(ihid, "resetting...\n");
  273. ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
  274. if (ret) {
  275. dev_err(&client->dev, "failed to reset device.\n");
  276. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  277. return ret;
  278. }
  279. return 0;
  280. }
  281. static void i2c_hid_get_input(struct i2c_hid *ihid)
  282. {
  283. int ret, ret_size;
  284. int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
  285. ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
  286. if (ret != size) {
  287. if (ret < 0)
  288. return;
  289. dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
  290. __func__, ret, size);
  291. return;
  292. }
  293. ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
  294. if (!ret_size) {
  295. /* host or device initiated RESET completed */
  296. if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
  297. wake_up(&ihid->wait);
  298. return;
  299. }
  300. if (ret_size > size) {
  301. dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
  302. __func__, size, ret_size);
  303. return;
  304. }
  305. i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
  306. if (test_bit(I2C_HID_STARTED, &ihid->flags))
  307. hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
  308. ret_size - 2, 1);
  309. return;
  310. }
  311. static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
  312. {
  313. struct i2c_hid *ihid = dev_id;
  314. if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
  315. return IRQ_HANDLED;
  316. i2c_hid_get_input(ihid);
  317. return IRQ_HANDLED;
  318. }
  319. static int i2c_hid_get_report_length(struct hid_report *report)
  320. {
  321. return ((report->size - 1) >> 3) + 1 +
  322. report->device->report_enum[report->type].numbered + 2;
  323. }
  324. static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
  325. size_t bufsize)
  326. {
  327. struct hid_device *hid = report->device;
  328. struct i2c_client *client = hid->driver_data;
  329. struct i2c_hid *ihid = i2c_get_clientdata(client);
  330. unsigned int size, ret_size;
  331. size = i2c_hid_get_report_length(report);
  332. if (i2c_hid_get_report(client,
  333. report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  334. report->id, buffer, size))
  335. return;
  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. dev_err(&client->dev, "can not retrieve initial reports\n");
  359. return;
  360. }
  361. list_for_each_entry(report,
  362. &hid->report_enum[HID_INPUT_REPORT].report_list, list)
  363. i2c_hid_init_report(report, inbuf, ihid->bufsize);
  364. list_for_each_entry(report,
  365. &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
  366. i2c_hid_init_report(report, inbuf, ihid->bufsize);
  367. kfree(inbuf);
  368. }
  369. /*
  370. * Traverse the supplied list of reports and find the longest
  371. */
  372. static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
  373. unsigned int *max)
  374. {
  375. struct hid_report *report;
  376. unsigned int size;
  377. /* We should not rely on wMaxInputLength, as some devices may set it to
  378. * a wrong length. */
  379. list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
  380. size = i2c_hid_get_report_length(report);
  381. if (*max < size)
  382. *max = size;
  383. }
  384. }
  385. static void i2c_hid_free_buffers(struct i2c_hid *ihid)
  386. {
  387. kfree(ihid->inbuf);
  388. kfree(ihid->argsbuf);
  389. kfree(ihid->cmdbuf);
  390. ihid->inbuf = NULL;
  391. ihid->cmdbuf = NULL;
  392. ihid->argsbuf = NULL;
  393. ihid->bufsize = 0;
  394. }
  395. static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
  396. {
  397. /* the worst case is computed from the set_report command with a
  398. * reportID > 15 and the maximum report length */
  399. int args_len = sizeof(__u8) + /* optional ReportID byte */
  400. sizeof(__u16) + /* data register */
  401. sizeof(__u16) + /* size of the report */
  402. report_size; /* report */
  403. ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
  404. ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
  405. ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
  406. if (!ihid->inbuf || !ihid->argsbuf || !ihid->cmdbuf) {
  407. i2c_hid_free_buffers(ihid);
  408. return -ENOMEM;
  409. }
  410. ihid->bufsize = report_size;
  411. return 0;
  412. }
  413. static int i2c_hid_get_raw_report(struct hid_device *hid,
  414. unsigned char report_number, __u8 *buf, size_t count,
  415. unsigned char report_type)
  416. {
  417. struct i2c_client *client = hid->driver_data;
  418. struct i2c_hid *ihid = i2c_get_clientdata(client);
  419. size_t ret_count, ask_count;
  420. int ret;
  421. if (report_type == HID_OUTPUT_REPORT)
  422. return -EINVAL;
  423. /* +2 bytes to include the size of the reply in the query buffer */
  424. ask_count = min(count + 2, (size_t)ihid->bufsize);
  425. ret = i2c_hid_get_report(client,
  426. report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
  427. report_number, ihid->inbuf, ask_count);
  428. if (ret < 0)
  429. return ret;
  430. ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
  431. if (ret_count <= 2)
  432. return 0;
  433. ret_count = min(ret_count, ask_count);
  434. /* The query buffer contains the size, dropping it in the reply */
  435. count = min(count, ret_count - 2);
  436. memcpy(buf, ihid->inbuf + 2, count);
  437. return count;
  438. }
  439. static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
  440. size_t count, unsigned char report_type)
  441. {
  442. struct i2c_client *client = hid->driver_data;
  443. int report_id = buf[0];
  444. int ret;
  445. if (report_type == HID_INPUT_REPORT)
  446. return -EINVAL;
  447. if (report_id) {
  448. buf++;
  449. count--;
  450. }
  451. ret = i2c_hid_set_report(client,
  452. report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
  453. report_id, buf, count);
  454. if (report_id && ret >= 0)
  455. ret++; /* add report_id to the number of transfered bytes */
  456. return ret;
  457. }
  458. static int i2c_hid_parse(struct hid_device *hid)
  459. {
  460. struct i2c_client *client = hid->driver_data;
  461. struct i2c_hid *ihid = i2c_get_clientdata(client);
  462. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  463. unsigned int rsize;
  464. char *rdesc;
  465. int ret;
  466. int tries = 3;
  467. i2c_hid_dbg(ihid, "entering %s\n", __func__);
  468. rsize = le16_to_cpu(hdesc->wReportDescLength);
  469. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  470. dbg_hid("weird size of report descriptor (%u)\n", rsize);
  471. return -EINVAL;
  472. }
  473. do {
  474. ret = i2c_hid_hwreset(client);
  475. if (ret)
  476. msleep(1000);
  477. } while (tries-- > 0 && ret);
  478. if (ret)
  479. return ret;
  480. rdesc = kzalloc(rsize, GFP_KERNEL);
  481. if (!rdesc) {
  482. dbg_hid("couldn't allocate rdesc memory\n");
  483. return -ENOMEM;
  484. }
  485. i2c_hid_dbg(ihid, "asking HID report descriptor\n");
  486. ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
  487. if (ret) {
  488. hid_err(hid, "reading report descriptor failed\n");
  489. kfree(rdesc);
  490. return -EIO;
  491. }
  492. i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
  493. ret = hid_parse_report(hid, rdesc, rsize);
  494. kfree(rdesc);
  495. if (ret) {
  496. dbg_hid("parsing report descriptor failed\n");
  497. return ret;
  498. }
  499. return 0;
  500. }
  501. static int i2c_hid_start(struct hid_device *hid)
  502. {
  503. struct i2c_client *client = hid->driver_data;
  504. struct i2c_hid *ihid = i2c_get_clientdata(client);
  505. int ret;
  506. unsigned int bufsize = HID_MIN_BUFFER_SIZE;
  507. i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
  508. i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
  509. i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
  510. if (bufsize > ihid->bufsize) {
  511. i2c_hid_free_buffers(ihid);
  512. ret = i2c_hid_alloc_buffers(ihid, bufsize);
  513. if (ret)
  514. return ret;
  515. }
  516. if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
  517. i2c_hid_init_reports(hid);
  518. return 0;
  519. }
  520. static void i2c_hid_stop(struct hid_device *hid)
  521. {
  522. struct i2c_client *client = hid->driver_data;
  523. struct i2c_hid *ihid = i2c_get_clientdata(client);
  524. hid->claimed = 0;
  525. i2c_hid_free_buffers(ihid);
  526. }
  527. static int i2c_hid_open(struct hid_device *hid)
  528. {
  529. struct i2c_client *client = hid->driver_data;
  530. struct i2c_hid *ihid = i2c_get_clientdata(client);
  531. int ret = 0;
  532. mutex_lock(&i2c_hid_open_mut);
  533. if (!hid->open++) {
  534. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  535. if (ret) {
  536. hid->open--;
  537. goto done;
  538. }
  539. set_bit(I2C_HID_STARTED, &ihid->flags);
  540. }
  541. done:
  542. mutex_unlock(&i2c_hid_open_mut);
  543. return ret;
  544. }
  545. static void i2c_hid_close(struct hid_device *hid)
  546. {
  547. struct i2c_client *client = hid->driver_data;
  548. struct i2c_hid *ihid = i2c_get_clientdata(client);
  549. /* protecting hid->open to make sure we don't restart
  550. * data acquistion due to a resumption we no longer
  551. * care about
  552. */
  553. mutex_lock(&i2c_hid_open_mut);
  554. if (!--hid->open) {
  555. clear_bit(I2C_HID_STARTED, &ihid->flags);
  556. /* Save some power */
  557. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  558. }
  559. mutex_unlock(&i2c_hid_open_mut);
  560. }
  561. static int i2c_hid_power(struct hid_device *hid, int lvl)
  562. {
  563. struct i2c_client *client = hid->driver_data;
  564. struct i2c_hid *ihid = i2c_get_clientdata(client);
  565. int ret = 0;
  566. i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
  567. switch (lvl) {
  568. case PM_HINT_FULLON:
  569. ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
  570. break;
  571. case PM_HINT_NORMAL:
  572. ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  573. break;
  574. }
  575. return ret;
  576. }
  577. static int i2c_hid_hidinput_input_event(struct input_dev *dev,
  578. unsigned int type, unsigned int code, int value)
  579. {
  580. struct hid_device *hid = input_get_drvdata(dev);
  581. struct hid_field *field;
  582. int offset;
  583. if (type == EV_FF)
  584. return input_ff_event(dev, type, code, value);
  585. if (type != EV_LED)
  586. return -1;
  587. offset = hidinput_find_field(hid, type, code, &field);
  588. if (offset == -1) {
  589. hid_warn(dev, "event field not found\n");
  590. return -1;
  591. }
  592. return hid_set_field(field, offset, value);
  593. }
  594. static struct hid_ll_driver i2c_hid_ll_driver = {
  595. .parse = i2c_hid_parse,
  596. .start = i2c_hid_start,
  597. .stop = i2c_hid_stop,
  598. .open = i2c_hid_open,
  599. .close = i2c_hid_close,
  600. .power = i2c_hid_power,
  601. .hidinput_input_event = i2c_hid_hidinput_input_event,
  602. };
  603. static int i2c_hid_init_irq(struct i2c_client *client)
  604. {
  605. struct i2c_hid *ihid = i2c_get_clientdata(client);
  606. int ret;
  607. dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
  608. ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
  609. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  610. client->name, ihid);
  611. if (ret < 0) {
  612. dev_warn(&client->dev,
  613. "Could not register for %s interrupt, irq = %d,"
  614. " ret = %d\n",
  615. client->name, client->irq, ret);
  616. return ret;
  617. }
  618. return 0;
  619. }
  620. static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
  621. {
  622. struct i2c_client *client = ihid->client;
  623. struct i2c_hid_desc *hdesc = &ihid->hdesc;
  624. unsigned int dsize;
  625. int ret;
  626. /* Fetch the length of HID description, retrieve the 4 first bytes:
  627. * bytes 0-1 -> length
  628. * bytes 2-3 -> bcdVersion (has to be 1.00) */
  629. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
  630. i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
  631. __func__, 4, ihid->hdesc_buffer);
  632. if (ret) {
  633. dev_err(&client->dev,
  634. "unable to fetch the size of HID descriptor (ret=%d)\n",
  635. ret);
  636. return -ENODEV;
  637. }
  638. dsize = le16_to_cpu(hdesc->wHIDDescLength);
  639. /*
  640. * the size of the HID descriptor should at least contain
  641. * its size and the bcdVersion (4 bytes), and should not be greater
  642. * than sizeof(struct i2c_hid_desc) as we directly fill this struct
  643. * through i2c_hid_command.
  644. */
  645. if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
  646. dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
  647. dsize);
  648. return -ENODEV;
  649. }
  650. /* check bcdVersion == 1.0 */
  651. if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
  652. dev_err(&client->dev,
  653. "unexpected HID descriptor bcdVersion (0x%04hx)\n",
  654. le16_to_cpu(hdesc->bcdVersion));
  655. return -ENODEV;
  656. }
  657. i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
  658. ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
  659. dsize);
  660. if (ret) {
  661. dev_err(&client->dev, "hid_descr_cmd Fail\n");
  662. return -ENODEV;
  663. }
  664. i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
  665. return 0;
  666. }
  667. #ifdef CONFIG_ACPI
  668. static int i2c_hid_acpi_pdata(struct i2c_client *client,
  669. struct i2c_hid_platform_data *pdata)
  670. {
  671. static u8 i2c_hid_guid[] = {
  672. 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
  673. 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
  674. };
  675. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  676. union acpi_object params[4], *obj;
  677. struct acpi_object_list input;
  678. struct acpi_device *adev;
  679. acpi_handle handle;
  680. handle = ACPI_HANDLE(&client->dev);
  681. if (!handle || acpi_bus_get_device(handle, &adev))
  682. return -ENODEV;
  683. input.count = ARRAY_SIZE(params);
  684. input.pointer = params;
  685. params[0].type = ACPI_TYPE_BUFFER;
  686. params[0].buffer.length = sizeof(i2c_hid_guid);
  687. params[0].buffer.pointer = i2c_hid_guid;
  688. params[1].type = ACPI_TYPE_INTEGER;
  689. params[1].integer.value = 1;
  690. params[2].type = ACPI_TYPE_INTEGER;
  691. params[2].integer.value = 1; /* HID function */
  692. params[3].type = ACPI_TYPE_INTEGER;
  693. params[3].integer.value = 0;
  694. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DSM", &input, &buf))) {
  695. dev_err(&client->dev, "device _DSM execution failed\n");
  696. return -ENODEV;
  697. }
  698. obj = (union acpi_object *)buf.pointer;
  699. if (obj->type != ACPI_TYPE_INTEGER) {
  700. dev_err(&client->dev, "device _DSM returned invalid type: %d\n",
  701. obj->type);
  702. kfree(buf.pointer);
  703. return -EINVAL;
  704. }
  705. pdata->hid_descriptor_address = obj->integer.value;
  706. kfree(buf.pointer);
  707. return 0;
  708. }
  709. static const struct acpi_device_id i2c_hid_acpi_match[] = {
  710. {"ACPI0C50", 0 },
  711. {"PNP0C50", 0 },
  712. { },
  713. };
  714. MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
  715. #else
  716. static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
  717. struct i2c_hid_platform_data *pdata)
  718. {
  719. return -ENODEV;
  720. }
  721. #endif
  722. static int i2c_hid_probe(struct i2c_client *client,
  723. const struct i2c_device_id *dev_id)
  724. {
  725. int ret;
  726. struct i2c_hid *ihid;
  727. struct hid_device *hid;
  728. __u16 hidRegister;
  729. struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
  730. dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
  731. if (!client->irq) {
  732. dev_err(&client->dev,
  733. "HID over i2c has not been provided an Int IRQ\n");
  734. return -EINVAL;
  735. }
  736. ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
  737. if (!ihid)
  738. return -ENOMEM;
  739. if (!platform_data) {
  740. ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
  741. if (ret) {
  742. dev_err(&client->dev,
  743. "HID register address not provided\n");
  744. goto err;
  745. }
  746. } else {
  747. ihid->pdata = *platform_data;
  748. }
  749. i2c_set_clientdata(client, ihid);
  750. ihid->client = client;
  751. hidRegister = ihid->pdata.hid_descriptor_address;
  752. ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
  753. init_waitqueue_head(&ihid->wait);
  754. /* we need to allocate the command buffer without knowing the maximum
  755. * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
  756. * real computation later. */
  757. ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
  758. if (ret < 0)
  759. goto err;
  760. ret = i2c_hid_fetch_hid_descriptor(ihid);
  761. if (ret < 0)
  762. goto err;
  763. ret = i2c_hid_init_irq(client);
  764. if (ret < 0)
  765. goto err;
  766. hid = hid_allocate_device();
  767. if (IS_ERR(hid)) {
  768. ret = PTR_ERR(hid);
  769. goto err_irq;
  770. }
  771. ihid->hid = hid;
  772. hid->driver_data = client;
  773. hid->ll_driver = &i2c_hid_ll_driver;
  774. hid->hid_get_raw_report = i2c_hid_get_raw_report;
  775. hid->hid_output_raw_report = i2c_hid_output_raw_report;
  776. hid->dev.parent = &client->dev;
  777. ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
  778. hid->bus = BUS_I2C;
  779. hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
  780. hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
  781. hid->product = le16_to_cpu(ihid->hdesc.wProductID);
  782. snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
  783. client->name, hid->vendor, hid->product);
  784. ret = hid_add_device(hid);
  785. if (ret) {
  786. if (ret != -ENODEV)
  787. hid_err(client, "can't add hid device: %d\n", ret);
  788. goto err_mem_free;
  789. }
  790. return 0;
  791. err_mem_free:
  792. hid_destroy_device(hid);
  793. err_irq:
  794. free_irq(client->irq, ihid);
  795. err:
  796. i2c_hid_free_buffers(ihid);
  797. kfree(ihid);
  798. return ret;
  799. }
  800. static int i2c_hid_remove(struct i2c_client *client)
  801. {
  802. struct i2c_hid *ihid = i2c_get_clientdata(client);
  803. struct hid_device *hid;
  804. hid = ihid->hid;
  805. hid_destroy_device(hid);
  806. free_irq(client->irq, ihid);
  807. if (ihid->bufsize)
  808. i2c_hid_free_buffers(ihid);
  809. kfree(ihid);
  810. return 0;
  811. }
  812. #ifdef CONFIG_PM_SLEEP
  813. static int i2c_hid_suspend(struct device *dev)
  814. {
  815. struct i2c_client *client = to_i2c_client(dev);
  816. if (device_may_wakeup(&client->dev))
  817. enable_irq_wake(client->irq);
  818. /* Save some power */
  819. i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
  820. return 0;
  821. }
  822. static int i2c_hid_resume(struct device *dev)
  823. {
  824. int ret;
  825. struct i2c_client *client = to_i2c_client(dev);
  826. ret = i2c_hid_hwreset(client);
  827. if (ret)
  828. return ret;
  829. if (device_may_wakeup(&client->dev))
  830. disable_irq_wake(client->irq);
  831. return 0;
  832. }
  833. #endif
  834. static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
  835. static const struct i2c_device_id i2c_hid_id_table[] = {
  836. { "hid", 0 },
  837. { },
  838. };
  839. MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
  840. static struct i2c_driver i2c_hid_driver = {
  841. .driver = {
  842. .name = "i2c_hid",
  843. .owner = THIS_MODULE,
  844. .pm = &i2c_hid_pm,
  845. .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
  846. },
  847. .probe = i2c_hid_probe,
  848. .remove = i2c_hid_remove,
  849. .id_table = i2c_hid_id_table,
  850. };
  851. module_i2c_driver(i2c_hid_driver);
  852. MODULE_DESCRIPTION("HID over I2C core driver");
  853. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  854. MODULE_LICENSE("GPL");