i2c-hid.c 30 KB

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