i2c-hid.c 27 KB

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