i2c-hid.c 27 KB

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