sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / hid / i2c-hid / i2c-hid.c
blob78fb32a7b103446136000c8ee9ac64cf7fba7054
1 /*
2 * HID over I2C protocol implementation
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
8 * This code is partly based on "USB HID support for Linux":
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
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.
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/input.h>
25 #include <linux/irq.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/pm.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/device.h>
31 #include <linux/wait.h>
32 #include <linux/err.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
35 #include <linux/jiffies.h>
36 #include <linux/kernel.h>
37 #include <linux/hid.h>
38 #include <linux/mutex.h>
39 #include <linux/acpi.h>
40 #include <linux/of.h>
41 #include <linux/regulator/consumer.h>
43 #include <linux/i2c/i2c-hid.h>
45 #include "../hid-ids.h"
47 /* quirks to control the device */
48 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
50 /* flags */
51 #define I2C_HID_STARTED 0
52 #define I2C_HID_RESET_PENDING 1
53 #define I2C_HID_READ_PENDING 2
55 #define I2C_HID_PWR_ON 0x00
56 #define I2C_HID_PWR_SLEEP 0x01
58 /* debug option */
59 static bool debug;
60 module_param(debug, bool, 0444);
61 MODULE_PARM_DESC(debug, "print a lot of debug information");
63 #define i2c_hid_dbg(ihid, fmt, arg...) \
64 do { \
65 if (debug) \
66 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
67 } while (0)
69 struct i2c_hid_desc {
70 __le16 wHIDDescLength;
71 __le16 bcdVersion;
72 __le16 wReportDescLength;
73 __le16 wReportDescRegister;
74 __le16 wInputRegister;
75 __le16 wMaxInputLength;
76 __le16 wOutputRegister;
77 __le16 wMaxOutputLength;
78 __le16 wCommandRegister;
79 __le16 wDataRegister;
80 __le16 wVendorID;
81 __le16 wProductID;
82 __le16 wVersionID;
83 __le32 reserved;
84 } __packed;
86 struct i2c_hid_cmd {
87 unsigned int registerIndex;
88 __u8 opcode;
89 unsigned int length;
90 bool wait;
93 union command {
94 u8 data[0];
95 struct cmd {
96 __le16 reg;
97 __u8 reportTypeID;
98 __u8 opcode;
99 } __packed c;
102 #define I2C_HID_CMD(opcode_) \
103 .opcode = opcode_, .length = 4, \
104 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
106 /* fetch HID descriptor */
107 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
108 /* fetch report descriptors */
109 static const struct i2c_hid_cmd hid_report_descr_cmd = {
110 .registerIndex = offsetof(struct i2c_hid_desc,
111 wReportDescRegister),
112 .opcode = 0x00,
113 .length = 2 };
114 /* commands */
115 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
116 .wait = true };
117 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
118 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
119 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
120 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
123 * These definitions are not used here, but are defined by the spec.
124 * Keeping them here for documentation purposes.
126 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
127 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
128 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
129 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
132 static DEFINE_MUTEX(i2c_hid_open_mut);
134 /* The main device structure */
135 struct i2c_hid {
136 struct i2c_client *client; /* i2c client */
137 struct hid_device *hid; /* pointer to corresponding HID dev */
138 union {
139 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
140 struct i2c_hid_desc hdesc; /* the HID Descriptor */
142 __le16 wHIDDescRegister; /* location of the i2c
143 * register of the HID
144 * descriptor. */
145 unsigned int bufsize; /* i2c buffer size */
146 char *inbuf; /* Input buffer */
147 char *rawbuf; /* Raw Input buffer */
148 char *cmdbuf; /* Command buffer */
149 char *argsbuf; /* Command arguments buffer */
151 unsigned long flags; /* device flags */
152 unsigned long quirks; /* Various quirks */
154 wait_queue_head_t wait; /* For waiting the interrupt */
156 struct i2c_hid_platform_data pdata;
158 bool irq_wake_enabled;
159 struct mutex reset_lock;
162 static const struct i2c_hid_quirks {
163 __u16 idVendor;
164 __u16 idProduct;
165 __u32 quirks;
166 } i2c_hid_quirks[] = {
167 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
168 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
169 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
170 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
171 { 0, 0 }
175 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
176 * @idVendor: the 16-bit vendor ID
177 * @idProduct: the 16-bit product ID
179 * Returns: a u32 quirks value.
181 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
183 u32 quirks = 0;
184 int n;
186 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
187 if (i2c_hid_quirks[n].idVendor == idVendor &&
188 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
189 i2c_hid_quirks[n].idProduct == idProduct))
190 quirks = i2c_hid_quirks[n].quirks;
192 return quirks;
195 static int __i2c_hid_command(struct i2c_client *client,
196 const struct i2c_hid_cmd *command, u8 reportID,
197 u8 reportType, u8 *args, int args_len,
198 unsigned char *buf_recv, int data_len)
200 struct i2c_hid *ihid = i2c_get_clientdata(client);
201 union command *cmd = (union command *)ihid->cmdbuf;
202 int ret;
203 struct i2c_msg msg[2];
204 int msg_num = 1;
206 int length = command->length;
207 bool wait = command->wait;
208 unsigned int registerIndex = command->registerIndex;
210 /* special case for hid_descr_cmd */
211 if (command == &hid_descr_cmd) {
212 cmd->c.reg = ihid->wHIDDescRegister;
213 } else {
214 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
215 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
218 if (length > 2) {
219 cmd->c.opcode = command->opcode;
220 cmd->c.reportTypeID = reportID | reportType << 4;
223 memcpy(cmd->data + length, args, args_len);
224 length += args_len;
226 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
228 msg[0].addr = client->addr;
229 msg[0].flags = client->flags & I2C_M_TEN;
230 msg[0].len = length;
231 msg[0].buf = cmd->data;
232 if (data_len > 0) {
233 msg[1].addr = client->addr;
234 msg[1].flags = client->flags & I2C_M_TEN;
235 msg[1].flags |= I2C_M_RD;
236 msg[1].len = data_len;
237 msg[1].buf = buf_recv;
238 msg_num = 2;
239 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
242 if (wait)
243 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
245 ret = i2c_transfer(client->adapter, msg, msg_num);
247 if (data_len > 0)
248 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
250 if (ret != msg_num)
251 return ret < 0 ? ret : -EIO;
253 ret = 0;
255 if (wait) {
256 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
257 if (!wait_event_timeout(ihid->wait,
258 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
259 msecs_to_jiffies(5000)))
260 ret = -ENODATA;
261 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
264 return ret;
267 static int i2c_hid_command(struct i2c_client *client,
268 const struct i2c_hid_cmd *command,
269 unsigned char *buf_recv, int data_len)
271 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
272 buf_recv, data_len);
275 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
276 u8 reportID, unsigned char *buf_recv, int data_len)
278 struct i2c_hid *ihid = i2c_get_clientdata(client);
279 u8 args[3];
280 int ret;
281 int args_len = 0;
282 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
284 i2c_hid_dbg(ihid, "%s\n", __func__);
286 if (reportID >= 0x0F) {
287 args[args_len++] = reportID;
288 reportID = 0x0F;
291 args[args_len++] = readRegister & 0xFF;
292 args[args_len++] = readRegister >> 8;
294 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
295 reportType, args, args_len, buf_recv, data_len);
296 if (ret) {
297 dev_err(&client->dev,
298 "failed to retrieve report from device.\n");
299 return ret;
302 return 0;
306 * i2c_hid_set_or_send_report: forward an incoming report to the device
307 * @client: the i2c_client of the device
308 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
309 * @reportID: the report ID
310 * @buf: the actual data to transfer, without the report ID
311 * @len: size of buf
312 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
314 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
315 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
317 struct i2c_hid *ihid = i2c_get_clientdata(client);
318 u8 *args = ihid->argsbuf;
319 const struct i2c_hid_cmd *hidcmd;
320 int ret;
321 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
322 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
323 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
324 u16 size;
325 int args_len;
326 int index = 0;
328 i2c_hid_dbg(ihid, "%s\n", __func__);
330 if (data_len > ihid->bufsize)
331 return -EINVAL;
333 size = 2 /* size */ +
334 (reportID ? 1 : 0) /* reportID */ +
335 data_len /* buf */;
336 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
337 2 /* dataRegister */ +
338 size /* args */;
340 if (!use_data && maxOutputLength == 0)
341 return -ENOSYS;
343 if (reportID >= 0x0F) {
344 args[index++] = reportID;
345 reportID = 0x0F;
349 * use the data register for feature reports or if the device does not
350 * support the output register
352 if (use_data) {
353 args[index++] = dataRegister & 0xFF;
354 args[index++] = dataRegister >> 8;
355 hidcmd = &hid_set_report_cmd;
356 } else {
357 args[index++] = outputRegister & 0xFF;
358 args[index++] = outputRegister >> 8;
359 hidcmd = &hid_no_cmd;
362 args[index++] = size & 0xFF;
363 args[index++] = size >> 8;
365 if (reportID)
366 args[index++] = reportID;
368 memcpy(&args[index], buf, data_len);
370 ret = __i2c_hid_command(client, hidcmd, reportID,
371 reportType, args, args_len, NULL, 0);
372 if (ret) {
373 dev_err(&client->dev, "failed to set a report to device.\n");
374 return ret;
377 return data_len;
380 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
382 struct i2c_hid *ihid = i2c_get_clientdata(client);
383 int ret;
385 i2c_hid_dbg(ihid, "%s\n", __func__);
388 * Some devices require to send a command to wakeup before power on.
389 * The call will get a return value (EREMOTEIO) but device will be
390 * triggered and activated. After that, it goes like a normal device.
392 if (power_state == I2C_HID_PWR_ON &&
393 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
394 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
396 /* Device was already activated */
397 if (!ret)
398 goto set_pwr_exit;
401 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
402 0, NULL, 0, NULL, 0);
404 if (ret)
405 dev_err(&client->dev, "failed to change power setting.\n");
407 set_pwr_exit:
408 return ret;
411 static int i2c_hid_hwreset(struct i2c_client *client)
413 struct i2c_hid *ihid = i2c_get_clientdata(client);
414 int ret;
416 i2c_hid_dbg(ihid, "%s\n", __func__);
419 * This prevents sending feature reports while the device is
420 * being reset. Otherwise we may lose the reset complete
421 * interrupt.
423 mutex_lock(&ihid->reset_lock);
425 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
426 if (ret)
427 goto out_unlock;
429 i2c_hid_dbg(ihid, "resetting...\n");
431 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
432 if (ret) {
433 dev_err(&client->dev, "failed to reset device.\n");
434 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
437 out_unlock:
438 mutex_unlock(&ihid->reset_lock);
439 return ret;
442 static void i2c_hid_get_input(struct i2c_hid *ihid)
444 int ret, ret_size;
445 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
447 if (size > ihid->bufsize)
448 size = ihid->bufsize;
450 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
451 if (ret != size) {
452 if (ret < 0)
453 return;
455 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
456 __func__, ret, size);
457 return;
460 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
462 if (!ret_size) {
463 /* host or device initiated RESET completed */
464 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
465 wake_up(&ihid->wait);
466 return;
469 if (ret_size > size) {
470 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
471 __func__, size, ret_size);
472 return;
475 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
477 if (test_bit(I2C_HID_STARTED, &ihid->flags))
478 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
479 ret_size - 2, 1);
481 return;
484 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
486 struct i2c_hid *ihid = dev_id;
488 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
489 return IRQ_HANDLED;
491 i2c_hid_get_input(ihid);
493 return IRQ_HANDLED;
496 static int i2c_hid_get_report_length(struct hid_report *report)
498 return ((report->size - 1) >> 3) + 1 +
499 report->device->report_enum[report->type].numbered + 2;
502 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
503 size_t bufsize)
505 struct hid_device *hid = report->device;
506 struct i2c_client *client = hid->driver_data;
507 struct i2c_hid *ihid = i2c_get_clientdata(client);
508 unsigned int size, ret_size;
510 size = i2c_hid_get_report_length(report);
511 if (i2c_hid_get_report(client,
512 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
513 report->id, buffer, size))
514 return;
516 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer);
518 ret_size = buffer[0] | (buffer[1] << 8);
520 if (ret_size != size) {
521 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
522 __func__, size, ret_size);
523 return;
526 /* hid->driver_lock is held as we are in probe function,
527 * we just need to setup the input fields, so using
528 * hid_report_raw_event is safe. */
529 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
533 * Initialize all reports
535 static void i2c_hid_init_reports(struct hid_device *hid)
537 struct hid_report *report;
538 struct i2c_client *client = hid->driver_data;
539 struct i2c_hid *ihid = i2c_get_clientdata(client);
540 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
542 if (!inbuf) {
543 dev_err(&client->dev, "can not retrieve initial reports\n");
544 return;
548 * The device must be powered on while we fetch initial reports
549 * from it.
551 pm_runtime_get_sync(&client->dev);
553 list_for_each_entry(report,
554 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
555 i2c_hid_init_report(report, inbuf, ihid->bufsize);
557 pm_runtime_put(&client->dev);
559 kfree(inbuf);
563 * Traverse the supplied list of reports and find the longest
565 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
566 unsigned int *max)
568 struct hid_report *report;
569 unsigned int size;
571 /* We should not rely on wMaxInputLength, as some devices may set it to
572 * a wrong length. */
573 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
574 size = i2c_hid_get_report_length(report);
575 if (*max < size)
576 *max = size;
580 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
582 kfree(ihid->inbuf);
583 kfree(ihid->rawbuf);
584 kfree(ihid->argsbuf);
585 kfree(ihid->cmdbuf);
586 ihid->inbuf = NULL;
587 ihid->rawbuf = NULL;
588 ihid->cmdbuf = NULL;
589 ihid->argsbuf = NULL;
590 ihid->bufsize = 0;
593 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
595 /* the worst case is computed from the set_report command with a
596 * reportID > 15 and the maximum report length */
597 int args_len = sizeof(__u8) + /* optional ReportID byte */
598 sizeof(__u16) + /* data register */
599 sizeof(__u16) + /* size of the report */
600 report_size; /* report */
602 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
603 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
604 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
605 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
607 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
608 i2c_hid_free_buffers(ihid);
609 return -ENOMEM;
612 ihid->bufsize = report_size;
614 return 0;
617 static int i2c_hid_get_raw_report(struct hid_device *hid,
618 unsigned char report_number, __u8 *buf, size_t count,
619 unsigned char report_type)
621 struct i2c_client *client = hid->driver_data;
622 struct i2c_hid *ihid = i2c_get_clientdata(client);
623 size_t ret_count, ask_count;
624 int ret;
626 if (report_type == HID_OUTPUT_REPORT)
627 return -EINVAL;
629 /* +2 bytes to include the size of the reply in the query buffer */
630 ask_count = min(count + 2, (size_t)ihid->bufsize);
632 ret = i2c_hid_get_report(client,
633 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
634 report_number, ihid->rawbuf, ask_count);
636 if (ret < 0)
637 return ret;
639 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
641 if (ret_count <= 2)
642 return 0;
644 ret_count = min(ret_count, ask_count);
646 /* The query buffer contains the size, dropping it in the reply */
647 count = min(count, ret_count - 2);
648 memcpy(buf, ihid->rawbuf + 2, count);
650 return count;
653 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
654 size_t count, unsigned char report_type, bool use_data)
656 struct i2c_client *client = hid->driver_data;
657 struct i2c_hid *ihid = i2c_get_clientdata(client);
658 int report_id = buf[0];
659 int ret;
661 if (report_type == HID_INPUT_REPORT)
662 return -EINVAL;
664 mutex_lock(&ihid->reset_lock);
666 if (report_id) {
667 buf++;
668 count--;
671 ret = i2c_hid_set_or_send_report(client,
672 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
673 report_id, buf, count, use_data);
675 if (report_id && ret >= 0)
676 ret++; /* add report_id to the number of transfered bytes */
678 mutex_unlock(&ihid->reset_lock);
680 return ret;
683 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
684 size_t count)
686 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
687 false);
690 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
691 __u8 *buf, size_t len, unsigned char rtype,
692 int reqtype)
694 switch (reqtype) {
695 case HID_REQ_GET_REPORT:
696 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
697 case HID_REQ_SET_REPORT:
698 if (buf[0] != reportnum)
699 return -EINVAL;
700 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
701 default:
702 return -EIO;
706 static int i2c_hid_parse(struct hid_device *hid)
708 struct i2c_client *client = hid->driver_data;
709 struct i2c_hid *ihid = i2c_get_clientdata(client);
710 struct i2c_hid_desc *hdesc = &ihid->hdesc;
711 unsigned int rsize;
712 char *rdesc;
713 int ret;
714 int tries = 3;
716 i2c_hid_dbg(ihid, "entering %s\n", __func__);
718 rsize = le16_to_cpu(hdesc->wReportDescLength);
719 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
720 dbg_hid("weird size of report descriptor (%u)\n", rsize);
721 return -EINVAL;
724 do {
725 ret = i2c_hid_hwreset(client);
726 if (ret)
727 msleep(1000);
728 } while (tries-- > 0 && ret);
730 if (ret)
731 return ret;
733 rdesc = kzalloc(rsize, GFP_KERNEL);
735 if (!rdesc) {
736 dbg_hid("couldn't allocate rdesc memory\n");
737 return -ENOMEM;
740 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
742 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
743 if (ret) {
744 hid_err(hid, "reading report descriptor failed\n");
745 kfree(rdesc);
746 return -EIO;
749 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
751 ret = hid_parse_report(hid, rdesc, rsize);
752 kfree(rdesc);
753 if (ret) {
754 dbg_hid("parsing report descriptor failed\n");
755 return ret;
758 return 0;
761 static int i2c_hid_start(struct hid_device *hid)
763 struct i2c_client *client = hid->driver_data;
764 struct i2c_hid *ihid = i2c_get_clientdata(client);
765 int ret;
766 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
768 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
769 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
770 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
772 if (bufsize > ihid->bufsize) {
773 disable_irq(client->irq);
774 i2c_hid_free_buffers(ihid);
776 ret = i2c_hid_alloc_buffers(ihid, bufsize);
777 enable_irq(client->irq);
779 if (ret)
780 return ret;
783 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
784 i2c_hid_init_reports(hid);
786 return 0;
789 static void i2c_hid_stop(struct hid_device *hid)
791 hid->claimed = 0;
794 static int i2c_hid_open(struct hid_device *hid)
796 struct i2c_client *client = hid->driver_data;
797 struct i2c_hid *ihid = i2c_get_clientdata(client);
798 int ret = 0;
800 mutex_lock(&i2c_hid_open_mut);
801 if (!hid->open++) {
802 ret = pm_runtime_get_sync(&client->dev);
803 if (ret < 0) {
804 hid->open--;
805 goto done;
807 set_bit(I2C_HID_STARTED, &ihid->flags);
809 done:
810 mutex_unlock(&i2c_hid_open_mut);
811 return ret < 0 ? ret : 0;
814 static void i2c_hid_close(struct hid_device *hid)
816 struct i2c_client *client = hid->driver_data;
817 struct i2c_hid *ihid = i2c_get_clientdata(client);
819 /* protecting hid->open to make sure we don't restart
820 * data acquistion due to a resumption we no longer
821 * care about
823 mutex_lock(&i2c_hid_open_mut);
824 if (!--hid->open) {
825 clear_bit(I2C_HID_STARTED, &ihid->flags);
827 /* Save some power */
828 pm_runtime_put(&client->dev);
830 mutex_unlock(&i2c_hid_open_mut);
833 static int i2c_hid_power(struct hid_device *hid, int lvl)
835 struct i2c_client *client = hid->driver_data;
836 struct i2c_hid *ihid = i2c_get_clientdata(client);
838 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
840 switch (lvl) {
841 case PM_HINT_FULLON:
842 pm_runtime_get_sync(&client->dev);
843 break;
844 case PM_HINT_NORMAL:
845 pm_runtime_put(&client->dev);
846 break;
848 return 0;
851 static struct hid_ll_driver i2c_hid_ll_driver = {
852 .parse = i2c_hid_parse,
853 .start = i2c_hid_start,
854 .stop = i2c_hid_stop,
855 .open = i2c_hid_open,
856 .close = i2c_hid_close,
857 .power = i2c_hid_power,
858 .output_report = i2c_hid_output_report,
859 .raw_request = i2c_hid_raw_request,
862 static int i2c_hid_init_irq(struct i2c_client *client)
864 struct i2c_hid *ihid = i2c_get_clientdata(client);
865 unsigned long irqflags = 0;
866 int ret;
868 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
870 if (!irq_get_trigger_type(client->irq))
871 irqflags = IRQF_TRIGGER_LOW;
873 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
874 irqflags | IRQF_ONESHOT, client->name, ihid);
875 if (ret < 0) {
876 dev_warn(&client->dev,
877 "Could not register for %s interrupt, irq = %d,"
878 " ret = %d\n",
879 client->name, client->irq, ret);
881 return ret;
884 return 0;
887 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
889 struct i2c_client *client = ihid->client;
890 struct i2c_hid_desc *hdesc = &ihid->hdesc;
891 unsigned int dsize;
892 int ret;
894 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
895 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
896 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
897 sizeof(struct i2c_hid_desc));
898 if (ret) {
899 dev_err(&client->dev, "hid_descr_cmd failed\n");
900 return -ENODEV;
903 /* Validate the length of HID descriptor, the 4 first bytes:
904 * bytes 0-1 -> length
905 * bytes 2-3 -> bcdVersion (has to be 1.00) */
906 /* check bcdVersion == 1.0 */
907 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
908 dev_err(&client->dev,
909 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
910 le16_to_cpu(hdesc->bcdVersion));
911 return -ENODEV;
914 /* Descriptor length should be 30 bytes as per the specification */
915 dsize = le16_to_cpu(hdesc->wHIDDescLength);
916 if (dsize != sizeof(struct i2c_hid_desc)) {
917 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
918 dsize);
919 return -ENODEV;
921 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
922 return 0;
925 #ifdef CONFIG_ACPI
926 static int i2c_hid_acpi_pdata(struct i2c_client *client,
927 struct i2c_hid_platform_data *pdata)
929 static u8 i2c_hid_guid[] = {
930 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
931 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
933 union acpi_object *obj;
934 struct acpi_device *adev;
935 acpi_handle handle;
937 handle = ACPI_HANDLE(&client->dev);
938 if (!handle || acpi_bus_get_device(handle, &adev))
939 return -ENODEV;
941 obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL,
942 ACPI_TYPE_INTEGER);
943 if (!obj) {
944 dev_err(&client->dev, "device _DSM execution failed\n");
945 return -ENODEV;
948 pdata->hid_descriptor_address = obj->integer.value;
949 ACPI_FREE(obj);
951 return 0;
954 static const struct acpi_device_id i2c_hid_acpi_match[] = {
955 {"ACPI0C50", 0 },
956 {"PNP0C50", 0 },
957 { },
959 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
960 #else
961 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
962 struct i2c_hid_platform_data *pdata)
964 return -ENODEV;
966 #endif
968 #ifdef CONFIG_OF
969 static int i2c_hid_of_probe(struct i2c_client *client,
970 struct i2c_hid_platform_data *pdata)
972 struct device *dev = &client->dev;
973 u32 val;
974 int ret;
976 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
977 if (ret) {
978 dev_err(&client->dev, "HID register address not provided\n");
979 return -ENODEV;
981 if (val >> 16) {
982 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
983 val);
984 return -EINVAL;
986 pdata->hid_descriptor_address = val;
988 return 0;
991 static const struct of_device_id i2c_hid_of_match[] = {
992 { .compatible = "hid-over-i2c" },
995 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
996 #else
997 static inline int i2c_hid_of_probe(struct i2c_client *client,
998 struct i2c_hid_platform_data *pdata)
1000 return -ENODEV;
1002 #endif
1004 static int i2c_hid_probe(struct i2c_client *client,
1005 const struct i2c_device_id *dev_id)
1007 int ret;
1008 struct i2c_hid *ihid;
1009 struct hid_device *hid;
1010 __u16 hidRegister;
1011 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1013 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1015 if (!client->irq) {
1016 dev_err(&client->dev,
1017 "HID over i2c has not been provided an Int IRQ\n");
1018 return -EINVAL;
1021 if (client->irq < 0) {
1022 if (client->irq != -EPROBE_DEFER)
1023 dev_err(&client->dev,
1024 "HID over i2c doesn't have a valid IRQ\n");
1025 return client->irq;
1028 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
1029 if (!ihid)
1030 return -ENOMEM;
1032 if (client->dev.of_node) {
1033 ret = i2c_hid_of_probe(client, &ihid->pdata);
1034 if (ret)
1035 goto err;
1036 } else if (!platform_data) {
1037 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1038 if (ret) {
1039 dev_err(&client->dev,
1040 "HID register address not provided\n");
1041 goto err;
1043 } else {
1044 ihid->pdata = *platform_data;
1047 i2c_set_clientdata(client, ihid);
1049 ihid->client = client;
1051 hidRegister = ihid->pdata.hid_descriptor_address;
1052 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1054 init_waitqueue_head(&ihid->wait);
1055 mutex_init(&ihid->reset_lock);
1057 /* we need to allocate the command buffer without knowing the maximum
1058 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1059 * real computation later. */
1060 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1061 if (ret < 0)
1062 goto err;
1064 pm_runtime_get_noresume(&client->dev);
1065 pm_runtime_set_active(&client->dev);
1066 pm_runtime_enable(&client->dev);
1067 device_enable_async_suspend(&client->dev);
1069 ret = i2c_hid_fetch_hid_descriptor(ihid);
1070 if (ret < 0)
1071 goto err_pm;
1073 ret = i2c_hid_init_irq(client);
1074 if (ret < 0)
1075 goto err_pm;
1077 hid = hid_allocate_device();
1078 if (IS_ERR(hid)) {
1079 ret = PTR_ERR(hid);
1080 goto err_irq;
1083 ihid->hid = hid;
1085 hid->driver_data = client;
1086 hid->ll_driver = &i2c_hid_ll_driver;
1087 hid->dev.parent = &client->dev;
1088 hid->bus = BUS_I2C;
1089 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1090 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1091 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1093 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1094 client->name, hid->vendor, hid->product);
1095 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1097 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1099 ret = hid_add_device(hid);
1100 if (ret) {
1101 if (ret != -ENODEV)
1102 hid_err(client, "can't add hid device: %d\n", ret);
1103 goto err_mem_free;
1106 pm_runtime_put(&client->dev);
1107 return 0;
1109 err_mem_free:
1110 hid_destroy_device(hid);
1112 err_irq:
1113 free_irq(client->irq, ihid);
1115 err_pm:
1116 pm_runtime_put_noidle(&client->dev);
1117 pm_runtime_disable(&client->dev);
1119 err:
1120 i2c_hid_free_buffers(ihid);
1121 kfree(ihid);
1122 return ret;
1125 static int i2c_hid_remove(struct i2c_client *client)
1127 struct i2c_hid *ihid = i2c_get_clientdata(client);
1128 struct hid_device *hid;
1130 pm_runtime_get_sync(&client->dev);
1131 pm_runtime_disable(&client->dev);
1132 pm_runtime_set_suspended(&client->dev);
1133 pm_runtime_put_noidle(&client->dev);
1135 hid = ihid->hid;
1136 hid_destroy_device(hid);
1138 free_irq(client->irq, ihid);
1140 if (ihid->bufsize)
1141 i2c_hid_free_buffers(ihid);
1143 kfree(ihid);
1145 return 0;
1148 static void i2c_hid_shutdown(struct i2c_client *client)
1150 struct i2c_hid *ihid = i2c_get_clientdata(client);
1152 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1153 free_irq(client->irq, ihid);
1156 #ifdef CONFIG_PM_SLEEP
1157 static int i2c_hid_suspend(struct device *dev)
1159 struct i2c_client *client = to_i2c_client(dev);
1160 struct i2c_hid *ihid = i2c_get_clientdata(client);
1161 struct hid_device *hid = ihid->hid;
1162 int ret;
1163 int wake_status;
1165 if (hid->driver && hid->driver->suspend) {
1167 * Wake up the device so that IO issues in
1168 * HID driver's suspend code can succeed.
1170 ret = pm_runtime_resume(dev);
1171 if (ret < 0)
1172 return ret;
1174 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1175 if (ret < 0)
1176 return ret;
1179 if (!pm_runtime_suspended(dev)) {
1180 /* Save some power */
1181 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1183 disable_irq(client->irq);
1186 if (device_may_wakeup(&client->dev)) {
1187 wake_status = enable_irq_wake(client->irq);
1188 if (!wake_status)
1189 ihid->irq_wake_enabled = true;
1190 else
1191 hid_warn(hid, "Failed to enable irq wake: %d\n",
1192 wake_status);
1195 return 0;
1198 static int i2c_hid_resume(struct device *dev)
1200 int ret;
1201 struct i2c_client *client = to_i2c_client(dev);
1202 struct i2c_hid *ihid = i2c_get_clientdata(client);
1203 struct hid_device *hid = ihid->hid;
1204 int wake_status;
1206 if (device_may_wakeup(&client->dev) && ihid->irq_wake_enabled) {
1207 wake_status = disable_irq_wake(client->irq);
1208 if (!wake_status)
1209 ihid->irq_wake_enabled = false;
1210 else
1211 hid_warn(hid, "Failed to disable irq wake: %d\n",
1212 wake_status);
1215 /* We'll resume to full power */
1216 pm_runtime_disable(dev);
1217 pm_runtime_set_active(dev);
1218 pm_runtime_enable(dev);
1220 enable_irq(client->irq);
1221 ret = i2c_hid_hwreset(client);
1222 if (ret)
1223 return ret;
1225 if (hid->driver && hid->driver->reset_resume) {
1226 ret = hid->driver->reset_resume(hid);
1227 return ret;
1230 return 0;
1232 #endif
1234 #ifdef CONFIG_PM
1235 static int i2c_hid_runtime_suspend(struct device *dev)
1237 struct i2c_client *client = to_i2c_client(dev);
1239 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1240 disable_irq(client->irq);
1241 return 0;
1244 static int i2c_hid_runtime_resume(struct device *dev)
1246 struct i2c_client *client = to_i2c_client(dev);
1248 enable_irq(client->irq);
1249 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1250 return 0;
1252 #endif
1254 static const struct dev_pm_ops i2c_hid_pm = {
1255 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1256 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1257 NULL)
1260 static const struct i2c_device_id i2c_hid_id_table[] = {
1261 { "hid", 0 },
1262 { "hid-over-i2c", 0 },
1263 { },
1265 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1268 static struct i2c_driver i2c_hid_driver = {
1269 .driver = {
1270 .name = "i2c_hid",
1271 .pm = &i2c_hid_pm,
1272 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1273 .of_match_table = of_match_ptr(i2c_hid_of_match),
1276 .probe = i2c_hid_probe,
1277 .remove = i2c_hid_remove,
1278 .shutdown = i2c_hid_shutdown,
1279 .id_table = i2c_hid_id_table,
1282 module_i2c_driver(i2c_hid_driver);
1284 MODULE_DESCRIPTION("HID over I2C core driver");
1285 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1286 MODULE_LICENSE("GPL");