i2c-hid.c 27 KB

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