i2c-hid.c 29 KB

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