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
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>
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>
41 #include <linux/regulator/consumer.h>
43 #include <linux/platform_data/i2c-hid.h>
45 #include "../hid-ids.h"
48 /* quirks to control the device */
49 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
50 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
51 #define I2C_HID_QUIRK_NO_RUNTIME_PM BIT(2)
52 #define I2C_HID_QUIRK_DELAY_AFTER_SLEEP BIT(3)
53 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
54 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
55 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
59 #define I2C_HID_STARTED 0
60 #define I2C_HID_RESET_PENDING 1
61 #define I2C_HID_READ_PENDING 2
63 #define I2C_HID_PWR_ON 0x00
64 #define I2C_HID_PWR_SLEEP 0x01
68 module_param(debug, bool, 0444);
69 MODULE_PARM_DESC(debug, "print a lot of debug information");
71 #define i2c_hid_dbg(ihid, fmt, arg...) \
74 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
78 __le16 wHIDDescLength;
80 __le16 wReportDescLength;
81 __le16 wReportDescRegister;
82 __le16 wInputRegister;
83 __le16 wMaxInputLength;
84 __le16 wOutputRegister;
85 __le16 wMaxOutputLength;
86 __le16 wCommandRegister;
95 unsigned int registerIndex;
110 #define I2C_HID_CMD(opcode_) \
111 .opcode = opcode_, .length = 4, \
112 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
114 /* fetch HID descriptor */
115 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
116 /* fetch report descriptors */
117 static const struct i2c_hid_cmd hid_report_descr_cmd = {
118 .registerIndex = offsetof(struct i2c_hid_desc,
119 wReportDescRegister),
123 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
125 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
126 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
127 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
128 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
131 * These definitions are not used here, but are defined by the spec.
132 * Keeping them here for documentation purposes.
134 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
135 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
136 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
137 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
140 /* The main device structure */
142 struct i2c_client *client; /* i2c client */
143 struct hid_device *hid; /* pointer to corresponding HID dev */
145 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
146 struct i2c_hid_desc hdesc; /* the HID Descriptor */
148 __le16 wHIDDescRegister; /* location of the i2c
149 * register of the HID
151 unsigned int bufsize; /* i2c buffer size */
152 u8 *inbuf; /* Input buffer */
153 u8 *rawbuf; /* Raw Input buffer */
154 u8 *cmdbuf; /* Command buffer */
155 u8 *argsbuf; /* Command arguments buffer */
157 unsigned long flags; /* device flags */
158 unsigned long quirks; /* Various quirks */
160 wait_queue_head_t wait; /* For waiting the interrupt */
162 struct i2c_hid_platform_data pdata;
164 bool irq_wake_enabled;
165 struct mutex reset_lock;
167 unsigned long sleep_delay;
170 static const struct i2c_hid_quirks {
174 } i2c_hid_quirks[] = {
175 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
176 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
177 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
178 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
179 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
180 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET |
181 I2C_HID_QUIRK_NO_RUNTIME_PM },
182 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_4B33,
183 I2C_HID_QUIRK_DELAY_AFTER_SLEEP },
184 { USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_8001,
185 I2C_HID_QUIRK_NO_RUNTIME_PM },
186 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
187 I2C_HID_QUIRK_BOGUS_IRQ },
188 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
189 I2C_HID_QUIRK_RESET_ON_RESUME },
190 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
191 I2C_HID_QUIRK_RESET_ON_RESUME },
192 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
193 I2C_HID_QUIRK_BAD_INPUT_SIZE },
198 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
199 * @idVendor: the 16-bit vendor ID
200 * @idProduct: the 16-bit product ID
202 * Returns: a u32 quirks value.
204 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
209 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
210 if (i2c_hid_quirks[n].idVendor == idVendor &&
211 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
212 i2c_hid_quirks[n].idProduct == idProduct))
213 quirks = i2c_hid_quirks[n].quirks;
218 static int __i2c_hid_command(struct i2c_client *client,
219 const struct i2c_hid_cmd *command, u8 reportID,
220 u8 reportType, u8 *args, int args_len,
221 unsigned char *buf_recv, int data_len)
223 struct i2c_hid *ihid = i2c_get_clientdata(client);
224 union command *cmd = (union command *)ihid->cmdbuf;
226 struct i2c_msg msg[2];
229 int length = command->length;
230 bool wait = command->wait;
231 unsigned int registerIndex = command->registerIndex;
233 /* special case for hid_descr_cmd */
234 if (command == &hid_descr_cmd) {
235 cmd->c.reg = ihid->wHIDDescRegister;
237 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
238 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
242 cmd->c.opcode = command->opcode;
243 cmd->c.reportTypeID = reportID | reportType << 4;
246 memcpy(cmd->data + length, args, args_len);
249 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
251 msg[0].addr = client->addr;
252 msg[0].flags = client->flags & I2C_M_TEN;
254 msg[0].buf = cmd->data;
256 msg[1].addr = client->addr;
257 msg[1].flags = client->flags & I2C_M_TEN;
258 msg[1].flags |= I2C_M_RD;
259 msg[1].len = data_len;
260 msg[1].buf = buf_recv;
262 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
266 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
268 ret = i2c_transfer(client->adapter, msg, msg_num);
271 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
274 return ret < 0 ? ret : -EIO;
278 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
281 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
282 if (!wait_event_timeout(ihid->wait,
283 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
284 msecs_to_jiffies(5000)))
286 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
292 static int i2c_hid_command(struct i2c_client *client,
293 const struct i2c_hid_cmd *command,
294 unsigned char *buf_recv, int data_len)
296 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
300 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
301 u8 reportID, unsigned char *buf_recv, int data_len)
303 struct i2c_hid *ihid = i2c_get_clientdata(client);
307 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
309 i2c_hid_dbg(ihid, "%s\n", __func__);
311 if (reportID >= 0x0F) {
312 args[args_len++] = reportID;
316 args[args_len++] = readRegister & 0xFF;
317 args[args_len++] = readRegister >> 8;
319 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
320 reportType, args, args_len, buf_recv, data_len);
322 dev_err(&client->dev,
323 "failed to retrieve report from device.\n");
331 * i2c_hid_set_or_send_report: forward an incoming report to the device
332 * @client: the i2c_client of the device
333 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
334 * @reportID: the report ID
335 * @buf: the actual data to transfer, without the report ID
337 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
339 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
340 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
342 struct i2c_hid *ihid = i2c_get_clientdata(client);
343 u8 *args = ihid->argsbuf;
344 const struct i2c_hid_cmd *hidcmd;
346 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
347 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
348 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
353 i2c_hid_dbg(ihid, "%s\n", __func__);
355 if (data_len > ihid->bufsize)
358 size = 2 /* size */ +
359 (reportID ? 1 : 0) /* reportID */ +
361 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
362 2 /* dataRegister */ +
365 if (!use_data && maxOutputLength == 0)
368 if (reportID >= 0x0F) {
369 args[index++] = reportID;
374 * use the data register for feature reports or if the device does not
375 * support the output register
378 args[index++] = dataRegister & 0xFF;
379 args[index++] = dataRegister >> 8;
380 hidcmd = &hid_set_report_cmd;
382 args[index++] = outputRegister & 0xFF;
383 args[index++] = outputRegister >> 8;
384 hidcmd = &hid_no_cmd;
387 args[index++] = size & 0xFF;
388 args[index++] = size >> 8;
391 args[index++] = reportID;
393 memcpy(&args[index], buf, data_len);
395 ret = __i2c_hid_command(client, hidcmd, reportID,
396 reportType, args, args_len, NULL, 0);
398 dev_err(&client->dev, "failed to set a report to device.\n");
405 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
407 struct i2c_hid *ihid = i2c_get_clientdata(client);
409 unsigned long now, delay;
411 i2c_hid_dbg(ihid, "%s\n", __func__);
414 * Some devices require to send a command to wakeup before power on.
415 * The call will get a return value (EREMOTEIO) but device will be
416 * triggered and activated. After that, it goes like a normal device.
418 if (power_state == I2C_HID_PWR_ON &&
419 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
420 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
422 /* Device was already activated */
427 if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
428 power_state == I2C_HID_PWR_ON) {
430 if (time_after(ihid->sleep_delay, now)) {
431 delay = jiffies_to_usecs(ihid->sleep_delay - now);
432 usleep_range(delay, delay + 1);
436 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
437 0, NULL, 0, NULL, 0);
439 if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
440 power_state == I2C_HID_PWR_SLEEP)
441 ihid->sleep_delay = jiffies + msecs_to_jiffies(20);
444 dev_err(&client->dev, "failed to change power setting.\n");
449 * The HID over I2C specification states that if a DEVICE needs time
450 * after the PWR_ON request, it should utilise CLOCK stretching.
451 * However, it has been observered that the Windows driver provides a
452 * 1ms sleep between the PWR_ON and RESET requests.
453 * According to Goodix Windows even waits 60 ms after (other?)
454 * PWR_ON requests. Testing has confirmed that several devices
455 * will not work properly without a delay after a PWR_ON request.
457 if (!ret && power_state == I2C_HID_PWR_ON)
463 static int i2c_hid_hwreset(struct i2c_client *client)
465 struct i2c_hid *ihid = i2c_get_clientdata(client);
468 i2c_hid_dbg(ihid, "%s\n", __func__);
471 * This prevents sending feature reports while the device is
472 * being reset. Otherwise we may lose the reset complete
475 mutex_lock(&ihid->reset_lock);
477 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
481 i2c_hid_dbg(ihid, "resetting...\n");
483 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
485 dev_err(&client->dev, "failed to reset device.\n");
486 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
490 mutex_unlock(&ihid->reset_lock);
494 static void i2c_hid_get_input(struct i2c_hid *ihid)
498 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
500 if (size > ihid->bufsize)
501 size = ihid->bufsize;
503 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
508 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
509 __func__, ret, size);
513 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
516 /* host or device initiated RESET completed */
517 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
518 wake_up(&ihid->wait);
522 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
523 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
524 "there's no data\n", __func__);
528 if ((ret_size > size) || (ret_size < 2)) {
529 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
530 ihid->inbuf[0] = size & 0xff;
531 ihid->inbuf[1] = size >> 8;
534 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
535 __func__, size, ret_size);
540 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
542 if (test_bit(I2C_HID_STARTED, &ihid->flags))
543 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
549 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
551 struct i2c_hid *ihid = dev_id;
553 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
556 i2c_hid_get_input(ihid);
561 static int i2c_hid_get_report_length(struct hid_report *report)
563 return ((report->size - 1) >> 3) + 1 +
564 report->device->report_enum[report->type].numbered + 2;
568 * Traverse the supplied list of reports and find the longest
570 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
573 struct hid_report *report;
576 /* We should not rely on wMaxInputLength, as some devices may set it to
578 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
579 size = i2c_hid_get_report_length(report);
585 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
589 kfree(ihid->argsbuf);
594 ihid->argsbuf = NULL;
598 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
600 /* the worst case is computed from the set_report command with a
601 * reportID > 15 and the maximum report length */
602 int args_len = sizeof(__u8) + /* ReportID */
603 sizeof(__u8) + /* optional ReportID byte */
604 sizeof(__u16) + /* data register */
605 sizeof(__u16) + /* size of the report */
606 report_size; /* report */
608 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
609 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
610 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
611 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
613 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
614 i2c_hid_free_buffers(ihid);
618 ihid->bufsize = report_size;
623 static int i2c_hid_get_raw_report(struct hid_device *hid,
624 unsigned char report_number, __u8 *buf, size_t count,
625 unsigned char report_type)
627 struct i2c_client *client = hid->driver_data;
628 struct i2c_hid *ihid = i2c_get_clientdata(client);
629 size_t ret_count, ask_count;
632 if (report_type == HID_OUTPUT_REPORT)
636 * In case of unnumbered reports the response from the device will
637 * not have the report ID that the upper layers expect, so we need
638 * to stash it the buffer ourselves and adjust the data size.
640 if (!report_number) {
646 /* +2 bytes to include the size of the reply in the query buffer */
647 ask_count = min(count + 2, (size_t)ihid->bufsize);
649 ret = i2c_hid_get_report(client,
650 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
651 report_number, ihid->rawbuf, ask_count);
656 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
661 ret_count = min(ret_count, ask_count);
663 /* The query buffer contains the size, dropping it in the reply */
664 count = min(count, ret_count - 2);
665 memcpy(buf, ihid->rawbuf + 2, count);
673 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
674 size_t count, unsigned char report_type, bool use_data)
676 struct i2c_client *client = hid->driver_data;
677 struct i2c_hid *ihid = i2c_get_clientdata(client);
678 int report_id = buf[0];
681 if (report_type == HID_INPUT_REPORT)
684 mutex_lock(&ihid->reset_lock);
687 * Note that both numbered and unnumbered reports passed here
688 * are supposed to have report ID stored in the 1st byte of the
689 * buffer, so we strip it off unconditionally before passing payload
690 * to i2c_hid_set_or_send_report which takes care of encoding
691 * everything properly.
693 ret = i2c_hid_set_or_send_report(client,
694 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
695 report_id, buf + 1, count - 1, use_data);
698 ret++; /* add report_id to the number of transferred bytes */
700 mutex_unlock(&ihid->reset_lock);
705 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
708 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
712 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
713 __u8 *buf, size_t len, unsigned char rtype,
717 case HID_REQ_GET_REPORT:
718 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
719 case HID_REQ_SET_REPORT:
720 if (buf[0] != reportnum)
722 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
728 static int i2c_hid_parse(struct hid_device *hid)
730 struct i2c_client *client = hid->driver_data;
731 struct i2c_hid *ihid = i2c_get_clientdata(client);
732 struct i2c_hid_desc *hdesc = &ihid->hdesc;
739 i2c_hid_dbg(ihid, "entering %s\n", __func__);
741 rsize = le16_to_cpu(hdesc->wReportDescLength);
742 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
743 dbg_hid("weird size of report descriptor (%u)\n", rsize);
748 ret = i2c_hid_hwreset(client);
751 } while (tries-- > 0 && ret);
756 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
760 rdesc = use_override;
761 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
763 rdesc = kzalloc(rsize, GFP_KERNEL);
766 dbg_hid("couldn't allocate rdesc memory\n");
770 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
772 ret = i2c_hid_command(client, &hid_report_descr_cmd,
775 hid_err(hid, "reading report descriptor failed\n");
781 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
783 ret = hid_parse_report(hid, rdesc, rsize);
788 dbg_hid("parsing report descriptor failed\n");
795 static int i2c_hid_start(struct hid_device *hid)
797 struct i2c_client *client = hid->driver_data;
798 struct i2c_hid *ihid = i2c_get_clientdata(client);
800 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
802 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
803 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
804 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
806 if (bufsize > ihid->bufsize) {
807 disable_irq(client->irq);
808 i2c_hid_free_buffers(ihid);
810 ret = i2c_hid_alloc_buffers(ihid, bufsize);
811 enable_irq(client->irq);
820 static void i2c_hid_stop(struct hid_device *hid)
825 static int i2c_hid_open(struct hid_device *hid)
827 struct i2c_client *client = hid->driver_data;
828 struct i2c_hid *ihid = i2c_get_clientdata(client);
831 ret = pm_runtime_get_sync(&client->dev);
835 set_bit(I2C_HID_STARTED, &ihid->flags);
839 static void i2c_hid_close(struct hid_device *hid)
841 struct i2c_client *client = hid->driver_data;
842 struct i2c_hid *ihid = i2c_get_clientdata(client);
844 clear_bit(I2C_HID_STARTED, &ihid->flags);
846 /* Save some power */
847 pm_runtime_put(&client->dev);
850 static int i2c_hid_power(struct hid_device *hid, int lvl)
852 struct i2c_client *client = hid->driver_data;
853 struct i2c_hid *ihid = i2c_get_clientdata(client);
855 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
859 pm_runtime_get_sync(&client->dev);
862 pm_runtime_put(&client->dev);
868 struct hid_ll_driver i2c_hid_ll_driver = {
869 .parse = i2c_hid_parse,
870 .start = i2c_hid_start,
871 .stop = i2c_hid_stop,
872 .open = i2c_hid_open,
873 .close = i2c_hid_close,
874 .power = i2c_hid_power,
875 .output_report = i2c_hid_output_report,
876 .raw_request = i2c_hid_raw_request,
878 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
880 static int i2c_hid_init_irq(struct i2c_client *client)
882 struct i2c_hid *ihid = i2c_get_clientdata(client);
883 unsigned long irqflags = 0;
886 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
888 if (!irq_get_trigger_type(client->irq))
889 irqflags = IRQF_TRIGGER_LOW;
891 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
892 irqflags | IRQF_ONESHOT, client->name, ihid);
894 dev_warn(&client->dev,
895 "Could not register for %s interrupt, irq = %d,"
897 client->name, client->irq, ret);
905 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
907 struct i2c_client *client = ihid->client;
908 struct i2c_hid_desc *hdesc = &ihid->hdesc;
912 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
913 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
914 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
916 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
918 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
919 ret = i2c_hid_command(client, &hid_descr_cmd,
921 sizeof(struct i2c_hid_desc));
923 dev_err(&client->dev, "hid_descr_cmd failed\n");
928 /* Validate the length of HID descriptor, the 4 first bytes:
929 * bytes 0-1 -> length
930 * bytes 2-3 -> bcdVersion (has to be 1.00) */
931 /* check bcdVersion == 1.0 */
932 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
933 dev_err(&client->dev,
934 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
935 le16_to_cpu(hdesc->bcdVersion));
939 /* Descriptor length should be 30 bytes as per the specification */
940 dsize = le16_to_cpu(hdesc->wHIDDescLength);
941 if (dsize != sizeof(struct i2c_hid_desc)) {
942 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
946 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
951 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
953 * The CHPN0001 ACPI device, which is used to describe the Chipone
954 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
960 static int i2c_hid_acpi_pdata(struct i2c_client *client,
961 struct i2c_hid_platform_data *pdata)
963 static guid_t i2c_hid_guid =
964 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
965 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
966 union acpi_object *obj;
967 struct acpi_device *adev;
970 handle = ACPI_HANDLE(&client->dev);
971 if (!handle || acpi_bus_get_device(handle, &adev)) {
972 dev_err(&client->dev, "Error could not get ACPI device\n");
976 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
979 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
982 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
986 pdata->hid_descriptor_address = obj->integer.value;
992 static void i2c_hid_acpi_fix_up_power(struct device *dev)
994 struct acpi_device *adev;
996 adev = ACPI_COMPANION(dev);
998 acpi_device_fix_up_power(adev);
1001 static const struct acpi_device_id i2c_hid_acpi_match[] = {
1006 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
1008 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
1009 struct i2c_hid_platform_data *pdata)
1014 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
1018 static int i2c_hid_of_probe(struct i2c_client *client,
1019 struct i2c_hid_platform_data *pdata)
1021 struct device *dev = &client->dev;
1025 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
1027 dev_err(&client->dev, "HID register address not provided\n");
1031 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
1035 pdata->hid_descriptor_address = val;
1040 static const struct of_device_id i2c_hid_of_match[] = {
1041 { .compatible = "hid-over-i2c" },
1044 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1046 static inline int i2c_hid_of_probe(struct i2c_client *client,
1047 struct i2c_hid_platform_data *pdata)
1053 static void i2c_hid_fwnode_probe(struct i2c_client *client,
1054 struct i2c_hid_platform_data *pdata)
1058 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
1060 pdata->post_power_delay_ms = val;
1063 static int i2c_hid_probe(struct i2c_client *client,
1064 const struct i2c_device_id *dev_id)
1067 struct i2c_hid *ihid;
1068 struct hid_device *hid;
1070 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1072 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1075 dev_err(&client->dev,
1076 "HID over i2c has not been provided an Int IRQ\n");
1080 if (client->irq < 0) {
1081 if (client->irq != -EPROBE_DEFER)
1082 dev_err(&client->dev,
1083 "HID over i2c doesn't have a valid IRQ\n");
1087 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1091 if (client->dev.of_node) {
1092 ret = i2c_hid_of_probe(client, &ihid->pdata);
1095 } else if (!platform_data) {
1096 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1100 ihid->pdata = *platform_data;
1103 /* Parse platform agnostic common properties from ACPI / device tree */
1104 i2c_hid_fwnode_probe(client, &ihid->pdata);
1106 ihid->pdata.supplies[0].supply = "vdd";
1107 ihid->pdata.supplies[1].supply = "vddl";
1109 ret = devm_regulator_bulk_get(&client->dev,
1110 ARRAY_SIZE(ihid->pdata.supplies),
1111 ihid->pdata.supplies);
1115 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1116 ihid->pdata.supplies);
1120 if (ihid->pdata.post_power_delay_ms)
1121 msleep(ihid->pdata.post_power_delay_ms);
1123 i2c_set_clientdata(client, ihid);
1125 ihid->client = client;
1127 hidRegister = ihid->pdata.hid_descriptor_address;
1128 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1130 init_waitqueue_head(&ihid->wait);
1131 mutex_init(&ihid->reset_lock);
1133 /* we need to allocate the command buffer without knowing the maximum
1134 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1135 * real computation later. */
1136 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1140 i2c_hid_acpi_fix_up_power(&client->dev);
1142 pm_runtime_get_noresume(&client->dev);
1143 pm_runtime_set_active(&client->dev);
1144 pm_runtime_enable(&client->dev);
1145 device_enable_async_suspend(&client->dev);
1147 /* Make sure there is something at this address */
1148 ret = i2c_smbus_read_byte(client);
1150 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1155 ret = i2c_hid_fetch_hid_descriptor(ihid);
1159 ret = i2c_hid_init_irq(client);
1163 hid = hid_allocate_device();
1171 hid->driver_data = client;
1172 hid->ll_driver = &i2c_hid_ll_driver;
1173 hid->dev.parent = &client->dev;
1175 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1176 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1177 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1179 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1180 client->name, (u16)hid->vendor, (u16)hid->product);
1181 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1183 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1185 ret = hid_add_device(hid);
1188 hid_err(client, "can't add hid device: %d\n", ret);
1192 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1193 pm_runtime_put(&client->dev);
1198 hid_destroy_device(hid);
1201 free_irq(client->irq, ihid);
1204 pm_runtime_put_noidle(&client->dev);
1205 pm_runtime_disable(&client->dev);
1208 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1209 ihid->pdata.supplies);
1210 i2c_hid_free_buffers(ihid);
1214 static int i2c_hid_remove(struct i2c_client *client)
1216 struct i2c_hid *ihid = i2c_get_clientdata(client);
1217 struct hid_device *hid;
1219 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1220 pm_runtime_get_sync(&client->dev);
1221 pm_runtime_disable(&client->dev);
1222 pm_runtime_set_suspended(&client->dev);
1223 pm_runtime_put_noidle(&client->dev);
1226 hid_destroy_device(hid);
1228 free_irq(client->irq, ihid);
1231 i2c_hid_free_buffers(ihid);
1233 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1234 ihid->pdata.supplies);
1239 static void i2c_hid_shutdown(struct i2c_client *client)
1241 struct i2c_hid *ihid = i2c_get_clientdata(client);
1243 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1244 free_irq(client->irq, ihid);
1247 #ifdef CONFIG_PM_SLEEP
1248 static int i2c_hid_suspend(struct device *dev)
1250 struct i2c_client *client = to_i2c_client(dev);
1251 struct i2c_hid *ihid = i2c_get_clientdata(client);
1252 struct hid_device *hid = ihid->hid;
1256 if (hid->driver && hid->driver->suspend) {
1258 * Wake up the device so that IO issues in
1259 * HID driver's suspend code can succeed.
1261 ret = pm_runtime_resume(dev);
1265 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1270 if (!pm_runtime_suspended(dev)) {
1271 /* Save some power */
1272 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1274 disable_irq(client->irq);
1277 if (device_may_wakeup(&client->dev)) {
1278 wake_status = enable_irq_wake(client->irq);
1280 ihid->irq_wake_enabled = true;
1282 hid_warn(hid, "Failed to enable irq wake: %d\n",
1285 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1286 ihid->pdata.supplies);
1292 static int i2c_hid_resume(struct device *dev)
1295 struct i2c_client *client = to_i2c_client(dev);
1296 struct i2c_hid *ihid = i2c_get_clientdata(client);
1297 struct hid_device *hid = ihid->hid;
1300 if (!device_may_wakeup(&client->dev)) {
1301 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1302 ihid->pdata.supplies);
1304 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1306 if (ihid->pdata.post_power_delay_ms)
1307 msleep(ihid->pdata.post_power_delay_ms);
1308 } else if (ihid->irq_wake_enabled) {
1309 wake_status = disable_irq_wake(client->irq);
1311 ihid->irq_wake_enabled = false;
1313 hid_warn(hid, "Failed to disable irq wake: %d\n",
1317 /* We'll resume to full power */
1318 pm_runtime_disable(dev);
1319 pm_runtime_set_active(dev);
1320 pm_runtime_enable(dev);
1322 enable_irq(client->irq);
1324 /* Instead of resetting device, simply powers the device on. This
1325 * solves "incomplete reports" on Raydium devices 2386:3118 and
1326 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1327 * data after a suspend/resume.
1329 * However some ALPS touchpads generate IRQ storm without reset, so
1330 * let's still reset them here.
1332 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1333 ret = i2c_hid_hwreset(client);
1335 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1340 if (hid->driver && hid->driver->reset_resume) {
1341 ret = hid->driver->reset_resume(hid);
1350 static int i2c_hid_runtime_suspend(struct device *dev)
1352 struct i2c_client *client = to_i2c_client(dev);
1354 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1355 disable_irq(client->irq);
1359 static int i2c_hid_runtime_resume(struct device *dev)
1361 struct i2c_client *client = to_i2c_client(dev);
1363 enable_irq(client->irq);
1364 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1369 static const struct dev_pm_ops i2c_hid_pm = {
1370 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1371 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1375 static const struct i2c_device_id i2c_hid_id_table[] = {
1377 { "hid-over-i2c", 0 },
1380 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1383 static struct i2c_driver i2c_hid_driver = {
1387 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1388 .of_match_table = of_match_ptr(i2c_hid_of_match),
1391 .probe = i2c_hid_probe,
1392 .remove = i2c_hid_remove,
1393 .shutdown = i2c_hid_shutdown,
1394 .id_table = i2c_hid_id_table,
1397 module_i2c_driver(i2c_hid_driver);
1399 MODULE_DESCRIPTION("HID over I2C core driver");
1400 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1401 MODULE_LICENSE("GPL");