i2c-hid.c 30 KB

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