i2c-hid.c 29 KB

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