2 * HID over I2C protocol implementation
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
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)
55 #define I2C_HID_STARTED 0
56 #define I2C_HID_RESET_PENDING 1
57 #define I2C_HID_READ_PENDING 2
59 #define I2C_HID_PWR_ON 0x00
60 #define I2C_HID_PWR_SLEEP 0x01
64 module_param(debug, bool, 0444);
65 MODULE_PARM_DESC(debug, "print a lot of debug information");
67 #define i2c_hid_dbg(ihid, fmt, arg...) \
70 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
74 __le16 wHIDDescLength;
76 __le16 wReportDescLength;
77 __le16 wReportDescRegister;
78 __le16 wInputRegister;
79 __le16 wMaxInputLength;
80 __le16 wOutputRegister;
81 __le16 wMaxOutputLength;
82 __le16 wCommandRegister;
91 unsigned int registerIndex;
106 #define I2C_HID_CMD(opcode_) \
107 .opcode = opcode_, .length = 4, \
108 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
110 /* fetch HID descriptor */
111 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
112 /* fetch report descriptors */
113 static const struct i2c_hid_cmd hid_report_descr_cmd = {
114 .registerIndex = offsetof(struct i2c_hid_desc,
115 wReportDescRegister),
119 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
121 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
122 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
123 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
124 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
127 * These definitions are not used here, but are defined by the spec.
128 * Keeping them here for documentation purposes.
130 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
131 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
132 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
133 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
136 /* The main device structure */
138 struct i2c_client *client; /* i2c client */
139 struct hid_device *hid; /* pointer to corresponding HID dev */
141 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
142 struct i2c_hid_desc hdesc; /* the HID Descriptor */
144 __le16 wHIDDescRegister; /* location of the i2c
145 * register of the HID
147 unsigned int bufsize; /* i2c buffer size */
148 u8 *inbuf; /* Input buffer */
149 u8 *rawbuf; /* Raw Input buffer */
150 u8 *cmdbuf; /* Command buffer */
151 u8 *argsbuf; /* Command arguments buffer */
153 unsigned long flags; /* device flags */
154 unsigned long quirks; /* Various quirks */
156 wait_queue_head_t wait; /* For waiting the interrupt */
158 struct i2c_hid_platform_data pdata;
160 bool irq_wake_enabled;
161 struct mutex reset_lock;
163 unsigned long sleep_delay;
166 static const struct i2c_hid_quirks {
170 } i2c_hid_quirks[] = {
171 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
172 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
173 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
174 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
175 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
176 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET |
177 I2C_HID_QUIRK_NO_RUNTIME_PM },
178 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_4B33,
179 I2C_HID_QUIRK_DELAY_AFTER_SLEEP },
184 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
185 * @idVendor: the 16-bit vendor ID
186 * @idProduct: the 16-bit product ID
188 * Returns: a u32 quirks value.
190 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
195 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
196 if (i2c_hid_quirks[n].idVendor == idVendor &&
197 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
198 i2c_hid_quirks[n].idProduct == idProduct))
199 quirks = i2c_hid_quirks[n].quirks;
204 static int __i2c_hid_command(struct i2c_client *client,
205 const struct i2c_hid_cmd *command, u8 reportID,
206 u8 reportType, u8 *args, int args_len,
207 unsigned char *buf_recv, int data_len)
209 struct i2c_hid *ihid = i2c_get_clientdata(client);
210 union command *cmd = (union command *)ihid->cmdbuf;
212 struct i2c_msg msg[2];
215 int length = command->length;
216 bool wait = command->wait;
217 unsigned int registerIndex = command->registerIndex;
219 /* special case for hid_descr_cmd */
220 if (command == &hid_descr_cmd) {
221 cmd->c.reg = ihid->wHIDDescRegister;
223 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
224 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
228 cmd->c.opcode = command->opcode;
229 cmd->c.reportTypeID = reportID | reportType << 4;
232 memcpy(cmd->data + length, args, args_len);
235 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
237 msg[0].addr = client->addr;
238 msg[0].flags = client->flags & I2C_M_TEN;
240 msg[0].buf = cmd->data;
242 msg[1].addr = client->addr;
243 msg[1].flags = client->flags & I2C_M_TEN;
244 msg[1].flags |= I2C_M_RD;
245 msg[1].len = data_len;
246 msg[1].buf = buf_recv;
248 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
252 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
254 ret = i2c_transfer(client->adapter, msg, msg_num);
257 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
260 return ret < 0 ? ret : -EIO;
264 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
267 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
268 if (!wait_event_timeout(ihid->wait,
269 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
270 msecs_to_jiffies(5000)))
272 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
278 static int i2c_hid_command(struct i2c_client *client,
279 const struct i2c_hid_cmd *command,
280 unsigned char *buf_recv, int data_len)
282 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
286 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
287 u8 reportID, unsigned char *buf_recv, int data_len)
289 struct i2c_hid *ihid = i2c_get_clientdata(client);
293 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
295 i2c_hid_dbg(ihid, "%s\n", __func__);
297 if (reportID >= 0x0F) {
298 args[args_len++] = reportID;
302 args[args_len++] = readRegister & 0xFF;
303 args[args_len++] = readRegister >> 8;
305 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
306 reportType, args, args_len, buf_recv, data_len);
308 dev_err(&client->dev,
309 "failed to retrieve report from device.\n");
317 * i2c_hid_set_or_send_report: forward an incoming report to the device
318 * @client: the i2c_client of the device
319 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
320 * @reportID: the report ID
321 * @buf: the actual data to transfer, without the report ID
323 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
325 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
326 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
328 struct i2c_hid *ihid = i2c_get_clientdata(client);
329 u8 *args = ihid->argsbuf;
330 const struct i2c_hid_cmd *hidcmd;
332 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
333 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
334 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
339 i2c_hid_dbg(ihid, "%s\n", __func__);
341 if (data_len > ihid->bufsize)
344 size = 2 /* size */ +
345 (reportID ? 1 : 0) /* reportID */ +
347 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
348 2 /* dataRegister */ +
351 if (!use_data && maxOutputLength == 0)
354 if (reportID >= 0x0F) {
355 args[index++] = reportID;
360 * use the data register for feature reports or if the device does not
361 * support the output register
364 args[index++] = dataRegister & 0xFF;
365 args[index++] = dataRegister >> 8;
366 hidcmd = &hid_set_report_cmd;
368 args[index++] = outputRegister & 0xFF;
369 args[index++] = outputRegister >> 8;
370 hidcmd = &hid_no_cmd;
373 args[index++] = size & 0xFF;
374 args[index++] = size >> 8;
377 args[index++] = reportID;
379 memcpy(&args[index], buf, data_len);
381 ret = __i2c_hid_command(client, hidcmd, reportID,
382 reportType, args, args_len, NULL, 0);
384 dev_err(&client->dev, "failed to set a report to device.\n");
391 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
393 struct i2c_hid *ihid = i2c_get_clientdata(client);
395 unsigned long now, delay;
397 i2c_hid_dbg(ihid, "%s\n", __func__);
400 * Some devices require to send a command to wakeup before power on.
401 * The call will get a return value (EREMOTEIO) but device will be
402 * triggered and activated. After that, it goes like a normal device.
404 if (power_state == I2C_HID_PWR_ON &&
405 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
406 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
408 /* Device was already activated */
413 if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
414 power_state == I2C_HID_PWR_ON) {
416 if (time_after(ihid->sleep_delay, now)) {
417 delay = jiffies_to_usecs(ihid->sleep_delay - now);
418 usleep_range(delay, delay + 1);
422 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
423 0, NULL, 0, NULL, 0);
425 if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
426 power_state == I2C_HID_PWR_SLEEP)
427 ihid->sleep_delay = jiffies + msecs_to_jiffies(20);
430 dev_err(&client->dev, "failed to change power setting.\n");
436 static int i2c_hid_hwreset(struct i2c_client *client)
438 struct i2c_hid *ihid = i2c_get_clientdata(client);
441 i2c_hid_dbg(ihid, "%s\n", __func__);
444 * This prevents sending feature reports while the device is
445 * being reset. Otherwise we may lose the reset complete
448 mutex_lock(&ihid->reset_lock);
450 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
455 * The HID over I2C specification states that if a DEVICE needs time
456 * after the PWR_ON request, it should utilise CLOCK stretching.
457 * However, it has been observered that the Windows driver provides a
458 * 1ms sleep between the PWR_ON and RESET requests and that some devices
461 usleep_range(1000, 5000);
463 i2c_hid_dbg(ihid, "resetting...\n");
465 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
467 dev_err(&client->dev, "failed to reset device.\n");
468 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
472 mutex_unlock(&ihid->reset_lock);
476 static void i2c_hid_get_input(struct i2c_hid *ihid)
480 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
482 if (size > ihid->bufsize)
483 size = ihid->bufsize;
485 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
490 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
491 __func__, ret, size);
495 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
498 /* host or device initiated RESET completed */
499 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
500 wake_up(&ihid->wait);
504 if ((ret_size > size) || (ret_size < 2)) {
505 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
506 __func__, size, ret_size);
510 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
512 if (test_bit(I2C_HID_STARTED, &ihid->flags))
513 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
519 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
521 struct i2c_hid *ihid = dev_id;
523 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
526 i2c_hid_get_input(ihid);
531 static int i2c_hid_get_report_length(struct hid_report *report)
533 return ((report->size - 1) >> 3) + 1 +
534 report->device->report_enum[report->type].numbered + 2;
538 * Traverse the supplied list of reports and find the longest
540 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
543 struct hid_report *report;
546 /* We should not rely on wMaxInputLength, as some devices may set it to
548 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
549 size = i2c_hid_get_report_length(report);
555 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
559 kfree(ihid->argsbuf);
564 ihid->argsbuf = NULL;
568 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
570 /* the worst case is computed from the set_report command with a
571 * reportID > 15 and the maximum report length */
572 int args_len = sizeof(__u8) + /* ReportID */
573 sizeof(__u8) + /* optional ReportID byte */
574 sizeof(__u16) + /* data register */
575 sizeof(__u16) + /* size of the report */
576 report_size; /* report */
578 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
579 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
580 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
581 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
583 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
584 i2c_hid_free_buffers(ihid);
588 ihid->bufsize = report_size;
593 static int i2c_hid_get_raw_report(struct hid_device *hid,
594 unsigned char report_number, __u8 *buf, size_t count,
595 unsigned char report_type)
597 struct i2c_client *client = hid->driver_data;
598 struct i2c_hid *ihid = i2c_get_clientdata(client);
599 size_t ret_count, ask_count;
602 if (report_type == HID_OUTPUT_REPORT)
605 /* +2 bytes to include the size of the reply in the query buffer */
606 ask_count = min(count + 2, (size_t)ihid->bufsize);
608 ret = i2c_hid_get_report(client,
609 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
610 report_number, ihid->rawbuf, ask_count);
615 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
620 ret_count = min(ret_count, ask_count);
622 /* The query buffer contains the size, dropping it in the reply */
623 count = min(count, ret_count - 2);
624 memcpy(buf, ihid->rawbuf + 2, count);
629 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
630 size_t count, unsigned char report_type, bool use_data)
632 struct i2c_client *client = hid->driver_data;
633 struct i2c_hid *ihid = i2c_get_clientdata(client);
634 int report_id = buf[0];
637 if (report_type == HID_INPUT_REPORT)
640 mutex_lock(&ihid->reset_lock);
647 ret = i2c_hid_set_or_send_report(client,
648 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
649 report_id, buf, count, use_data);
651 if (report_id && ret >= 0)
652 ret++; /* add report_id to the number of transfered bytes */
654 mutex_unlock(&ihid->reset_lock);
659 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
662 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
666 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
667 __u8 *buf, size_t len, unsigned char rtype,
671 case HID_REQ_GET_REPORT:
672 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
673 case HID_REQ_SET_REPORT:
674 if (buf[0] != reportnum)
676 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
682 static int i2c_hid_parse(struct hid_device *hid)
684 struct i2c_client *client = hid->driver_data;
685 struct i2c_hid *ihid = i2c_get_clientdata(client);
686 struct i2c_hid_desc *hdesc = &ihid->hdesc;
693 i2c_hid_dbg(ihid, "entering %s\n", __func__);
695 rsize = le16_to_cpu(hdesc->wReportDescLength);
696 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
697 dbg_hid("weird size of report descriptor (%u)\n", rsize);
702 ret = i2c_hid_hwreset(client);
705 } while (tries-- > 0 && ret);
710 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
714 rdesc = use_override;
715 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
717 rdesc = kzalloc(rsize, GFP_KERNEL);
720 dbg_hid("couldn't allocate rdesc memory\n");
724 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
726 ret = i2c_hid_command(client, &hid_report_descr_cmd,
729 hid_err(hid, "reading report descriptor failed\n");
735 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
737 ret = hid_parse_report(hid, rdesc, rsize);
742 dbg_hid("parsing report descriptor failed\n");
749 static int i2c_hid_start(struct hid_device *hid)
751 struct i2c_client *client = hid->driver_data;
752 struct i2c_hid *ihid = i2c_get_clientdata(client);
754 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
756 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
757 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
758 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
760 if (bufsize > ihid->bufsize) {
761 disable_irq(client->irq);
762 i2c_hid_free_buffers(ihid);
764 ret = i2c_hid_alloc_buffers(ihid, bufsize);
765 enable_irq(client->irq);
774 static void i2c_hid_stop(struct hid_device *hid)
779 static int i2c_hid_open(struct hid_device *hid)
781 struct i2c_client *client = hid->driver_data;
782 struct i2c_hid *ihid = i2c_get_clientdata(client);
785 ret = pm_runtime_get_sync(&client->dev);
789 set_bit(I2C_HID_STARTED, &ihid->flags);
793 static void i2c_hid_close(struct hid_device *hid)
795 struct i2c_client *client = hid->driver_data;
796 struct i2c_hid *ihid = i2c_get_clientdata(client);
798 clear_bit(I2C_HID_STARTED, &ihid->flags);
800 /* Save some power */
801 pm_runtime_put(&client->dev);
804 static int i2c_hid_power(struct hid_device *hid, int lvl)
806 struct i2c_client *client = hid->driver_data;
807 struct i2c_hid *ihid = i2c_get_clientdata(client);
809 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
813 pm_runtime_get_sync(&client->dev);
816 pm_runtime_put(&client->dev);
822 struct hid_ll_driver i2c_hid_ll_driver = {
823 .parse = i2c_hid_parse,
824 .start = i2c_hid_start,
825 .stop = i2c_hid_stop,
826 .open = i2c_hid_open,
827 .close = i2c_hid_close,
828 .power = i2c_hid_power,
829 .output_report = i2c_hid_output_report,
830 .raw_request = i2c_hid_raw_request,
832 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
834 static int i2c_hid_init_irq(struct i2c_client *client)
836 struct i2c_hid *ihid = i2c_get_clientdata(client);
837 unsigned long irqflags = 0;
840 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
842 if (!irq_get_trigger_type(client->irq))
843 irqflags = IRQF_TRIGGER_LOW;
845 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
846 irqflags | IRQF_ONESHOT, client->name, ihid);
848 dev_warn(&client->dev,
849 "Could not register for %s interrupt, irq = %d,"
851 client->name, client->irq, ret);
859 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
861 struct i2c_client *client = ihid->client;
862 struct i2c_hid_desc *hdesc = &ihid->hdesc;
866 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
867 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
868 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
870 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
872 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
873 ret = i2c_hid_command(client, &hid_descr_cmd,
875 sizeof(struct i2c_hid_desc));
877 dev_err(&client->dev, "hid_descr_cmd failed\n");
882 /* Validate the length of HID descriptor, the 4 first bytes:
883 * bytes 0-1 -> length
884 * bytes 2-3 -> bcdVersion (has to be 1.00) */
885 /* check bcdVersion == 1.0 */
886 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
887 dev_err(&client->dev,
888 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
889 le16_to_cpu(hdesc->bcdVersion));
893 /* Descriptor length should be 30 bytes as per the specification */
894 dsize = le16_to_cpu(hdesc->wHIDDescLength);
895 if (dsize != sizeof(struct i2c_hid_desc)) {
896 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
900 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
905 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
907 * The CHPN0001 ACPI device, which is used to describe the Chipone
908 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
914 static int i2c_hid_acpi_pdata(struct i2c_client *client,
915 struct i2c_hid_platform_data *pdata)
917 static guid_t i2c_hid_guid =
918 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
919 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
920 union acpi_object *obj;
921 struct acpi_device *adev;
924 handle = ACPI_HANDLE(&client->dev);
925 if (!handle || acpi_bus_get_device(handle, &adev)) {
926 dev_err(&client->dev, "Error could not get ACPI device\n");
930 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
933 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
936 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
940 pdata->hid_descriptor_address = obj->integer.value;
946 static void i2c_hid_acpi_fix_up_power(struct device *dev)
948 struct acpi_device *adev;
950 adev = ACPI_COMPANION(dev);
952 acpi_device_fix_up_power(adev);
955 static const struct acpi_device_id i2c_hid_acpi_match[] = {
960 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
962 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
963 struct i2c_hid_platform_data *pdata)
968 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
972 static int i2c_hid_of_probe(struct i2c_client *client,
973 struct i2c_hid_platform_data *pdata)
975 struct device *dev = &client->dev;
979 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
981 dev_err(&client->dev, "HID register address not provided\n");
985 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
989 pdata->hid_descriptor_address = val;
994 static const struct of_device_id i2c_hid_of_match[] = {
995 { .compatible = "hid-over-i2c" },
998 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1000 static inline int i2c_hid_of_probe(struct i2c_client *client,
1001 struct i2c_hid_platform_data *pdata)
1007 static void i2c_hid_fwnode_probe(struct i2c_client *client,
1008 struct i2c_hid_platform_data *pdata)
1012 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
1014 pdata->post_power_delay_ms = val;
1017 static int i2c_hid_probe(struct i2c_client *client,
1018 const struct i2c_device_id *dev_id)
1021 struct i2c_hid *ihid;
1022 struct hid_device *hid;
1024 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1026 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1029 dev_err(&client->dev,
1030 "HID over i2c has not been provided an Int IRQ\n");
1034 if (client->irq < 0) {
1035 if (client->irq != -EPROBE_DEFER)
1036 dev_err(&client->dev,
1037 "HID over i2c doesn't have a valid IRQ\n");
1041 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1045 if (client->dev.of_node) {
1046 ret = i2c_hid_of_probe(client, &ihid->pdata);
1049 } else if (!platform_data) {
1050 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1054 ihid->pdata = *platform_data;
1057 /* Parse platform agnostic common properties from ACPI / device tree */
1058 i2c_hid_fwnode_probe(client, &ihid->pdata);
1060 ihid->pdata.supplies[0].supply = "vdd";
1061 ihid->pdata.supplies[1].supply = "vddl";
1063 ret = devm_regulator_bulk_get(&client->dev,
1064 ARRAY_SIZE(ihid->pdata.supplies),
1065 ihid->pdata.supplies);
1069 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1070 ihid->pdata.supplies);
1074 if (ihid->pdata.post_power_delay_ms)
1075 msleep(ihid->pdata.post_power_delay_ms);
1077 i2c_set_clientdata(client, ihid);
1079 ihid->client = client;
1081 hidRegister = ihid->pdata.hid_descriptor_address;
1082 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1084 init_waitqueue_head(&ihid->wait);
1085 mutex_init(&ihid->reset_lock);
1087 /* we need to allocate the command buffer without knowing the maximum
1088 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1089 * real computation later. */
1090 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1094 i2c_hid_acpi_fix_up_power(&client->dev);
1096 pm_runtime_get_noresume(&client->dev);
1097 pm_runtime_set_active(&client->dev);
1098 pm_runtime_enable(&client->dev);
1099 device_enable_async_suspend(&client->dev);
1101 /* Make sure there is something at this address */
1102 ret = i2c_smbus_read_byte(client);
1104 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1109 ret = i2c_hid_fetch_hid_descriptor(ihid);
1113 ret = i2c_hid_init_irq(client);
1117 hid = hid_allocate_device();
1125 hid->driver_data = client;
1126 hid->ll_driver = &i2c_hid_ll_driver;
1127 hid->dev.parent = &client->dev;
1129 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1130 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1131 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1133 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1134 client->name, hid->vendor, hid->product);
1135 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1137 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1139 ret = hid_add_device(hid);
1142 hid_err(client, "can't add hid device: %d\n", ret);
1146 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1147 pm_runtime_put(&client->dev);
1152 hid_destroy_device(hid);
1155 free_irq(client->irq, ihid);
1158 pm_runtime_put_noidle(&client->dev);
1159 pm_runtime_disable(&client->dev);
1162 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1163 ihid->pdata.supplies);
1164 i2c_hid_free_buffers(ihid);
1168 static int i2c_hid_remove(struct i2c_client *client)
1170 struct i2c_hid *ihid = i2c_get_clientdata(client);
1171 struct hid_device *hid;
1173 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1174 pm_runtime_get_sync(&client->dev);
1175 pm_runtime_disable(&client->dev);
1176 pm_runtime_set_suspended(&client->dev);
1177 pm_runtime_put_noidle(&client->dev);
1180 hid_destroy_device(hid);
1182 free_irq(client->irq, ihid);
1185 i2c_hid_free_buffers(ihid);
1187 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1188 ihid->pdata.supplies);
1193 static void i2c_hid_shutdown(struct i2c_client *client)
1195 struct i2c_hid *ihid = i2c_get_clientdata(client);
1197 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1198 free_irq(client->irq, ihid);
1201 #ifdef CONFIG_PM_SLEEP
1202 static int i2c_hid_suspend(struct device *dev)
1204 struct i2c_client *client = to_i2c_client(dev);
1205 struct i2c_hid *ihid = i2c_get_clientdata(client);
1206 struct hid_device *hid = ihid->hid;
1210 if (hid->driver && hid->driver->suspend) {
1212 * Wake up the device so that IO issues in
1213 * HID driver's suspend code can succeed.
1215 ret = pm_runtime_resume(dev);
1219 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1224 if (!pm_runtime_suspended(dev)) {
1225 /* Save some power */
1226 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1228 disable_irq(client->irq);
1231 if (device_may_wakeup(&client->dev)) {
1232 wake_status = enable_irq_wake(client->irq);
1234 ihid->irq_wake_enabled = true;
1236 hid_warn(hid, "Failed to enable irq wake: %d\n",
1239 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1240 ihid->pdata.supplies);
1246 static int i2c_hid_resume(struct device *dev)
1249 struct i2c_client *client = to_i2c_client(dev);
1250 struct i2c_hid *ihid = i2c_get_clientdata(client);
1251 struct hid_device *hid = ihid->hid;
1254 if (!device_may_wakeup(&client->dev)) {
1255 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1256 ihid->pdata.supplies);
1258 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1260 if (ihid->pdata.post_power_delay_ms)
1261 msleep(ihid->pdata.post_power_delay_ms);
1262 } else if (ihid->irq_wake_enabled) {
1263 wake_status = disable_irq_wake(client->irq);
1265 ihid->irq_wake_enabled = false;
1267 hid_warn(hid, "Failed to disable irq wake: %d\n",
1271 /* We'll resume to full power */
1272 pm_runtime_disable(dev);
1273 pm_runtime_set_active(dev);
1274 pm_runtime_enable(dev);
1276 enable_irq(client->irq);
1278 /* Instead of resetting device, simply powers the device on. This
1279 * solves "incomplete reports" on Raydium devices 2386:3118 and
1280 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1281 * data after a suspend/resume.
1283 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1287 if (hid->driver && hid->driver->reset_resume) {
1288 ret = hid->driver->reset_resume(hid);
1297 static int i2c_hid_runtime_suspend(struct device *dev)
1299 struct i2c_client *client = to_i2c_client(dev);
1301 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1302 disable_irq(client->irq);
1306 static int i2c_hid_runtime_resume(struct device *dev)
1308 struct i2c_client *client = to_i2c_client(dev);
1310 enable_irq(client->irq);
1311 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1316 static const struct dev_pm_ops i2c_hid_pm = {
1317 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1318 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1322 static const struct i2c_device_id i2c_hid_id_table[] = {
1324 { "hid-over-i2c", 0 },
1327 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1330 static struct i2c_driver i2c_hid_driver = {
1334 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1335 .of_match_table = of_match_ptr(i2c_hid_of_match),
1338 .probe = i2c_hid_probe,
1339 .remove = i2c_hid_remove,
1340 .shutdown = i2c_hid_shutdown,
1341 .id_table = i2c_hid_id_table,
1344 module_i2c_driver(i2c_hid_driver);
1346 MODULE_DESCRIPTION("HID over I2C core driver");
1348 MODULE_LICENSE("GPL");