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"
47 /* quirks to control the device */
48 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
49 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
50 #define I2C_HID_QUIRK_RESEND_REPORT_DESCR BIT(2)
53 #define I2C_HID_STARTED 0
54 #define I2C_HID_RESET_PENDING 1
55 #define I2C_HID_READ_PENDING 2
57 #define I2C_HID_PWR_ON 0x00
58 #define I2C_HID_PWR_SLEEP 0x01
62 module_param(debug, bool, 0444);
63 MODULE_PARM_DESC(debug, "print a lot of debug information");
65 #define i2c_hid_dbg(ihid, fmt, arg...) \
68 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
72 __le16 wHIDDescLength;
74 __le16 wReportDescLength;
75 __le16 wReportDescRegister;
76 __le16 wInputRegister;
77 __le16 wMaxInputLength;
78 __le16 wOutputRegister;
79 __le16 wMaxOutputLength;
80 __le16 wCommandRegister;
89 unsigned int registerIndex;
104 #define I2C_HID_CMD(opcode_) \
105 .opcode = opcode_, .length = 4, \
106 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
108 /* fetch HID descriptor */
109 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
110 /* fetch report descriptors */
111 static const struct i2c_hid_cmd hid_report_descr_cmd = {
112 .registerIndex = offsetof(struct i2c_hid_desc,
113 wReportDescRegister),
117 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
119 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
120 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
121 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
122 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
125 * These definitions are not used here, but are defined by the spec.
126 * Keeping them here for documentation purposes.
128 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
129 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
130 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
131 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
134 /* The main device structure */
136 struct i2c_client *client; /* i2c client */
137 struct hid_device *hid; /* pointer to corresponding HID dev */
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
145 unsigned int bufsize; /* i2c buffer size */
146 u8 *inbuf; /* Input buffer */
147 u8 *rawbuf; /* Raw Input buffer */
148 u8 *cmdbuf; /* Command buffer */
149 u8 *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 {
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 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
172 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
173 { USB_VENDOR_ID_SIS_TOUCH, USB_DEVICE_ID_SIS10FB_TOUCH,
174 I2C_HID_QUIRK_RESEND_REPORT_DESCR },
179 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
180 * @idVendor: the 16-bit vendor ID
181 * @idProduct: the 16-bit product ID
183 * Returns: a u32 quirks value.
185 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
190 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
191 if (i2c_hid_quirks[n].idVendor == idVendor &&
192 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
193 i2c_hid_quirks[n].idProduct == idProduct))
194 quirks = i2c_hid_quirks[n].quirks;
199 static int __i2c_hid_command(struct i2c_client *client,
200 const struct i2c_hid_cmd *command, u8 reportID,
201 u8 reportType, u8 *args, int args_len,
202 unsigned char *buf_recv, int data_len)
204 struct i2c_hid *ihid = i2c_get_clientdata(client);
205 union command *cmd = (union command *)ihid->cmdbuf;
207 struct i2c_msg msg[2];
210 int length = command->length;
211 bool wait = command->wait;
212 unsigned int registerIndex = command->registerIndex;
214 /* special case for hid_descr_cmd */
215 if (command == &hid_descr_cmd) {
216 cmd->c.reg = ihid->wHIDDescRegister;
218 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
219 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
223 cmd->c.opcode = command->opcode;
224 cmd->c.reportTypeID = reportID | reportType << 4;
227 memcpy(cmd->data + length, args, args_len);
230 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
232 msg[0].addr = client->addr;
233 msg[0].flags = client->flags & I2C_M_TEN;
235 msg[0].buf = cmd->data;
237 msg[1].addr = client->addr;
238 msg[1].flags = client->flags & I2C_M_TEN;
239 msg[1].flags |= I2C_M_RD;
240 msg[1].len = data_len;
241 msg[1].buf = buf_recv;
243 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
247 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
249 ret = i2c_transfer(client->adapter, msg, msg_num);
252 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
255 return ret < 0 ? ret : -EIO;
259 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
262 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
263 if (!wait_event_timeout(ihid->wait,
264 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
265 msecs_to_jiffies(5000)))
267 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
273 static int i2c_hid_command(struct i2c_client *client,
274 const struct i2c_hid_cmd *command,
275 unsigned char *buf_recv, int data_len)
277 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
281 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
282 u8 reportID, unsigned char *buf_recv, int data_len)
284 struct i2c_hid *ihid = i2c_get_clientdata(client);
288 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
290 i2c_hid_dbg(ihid, "%s\n", __func__);
292 if (reportID >= 0x0F) {
293 args[args_len++] = reportID;
297 args[args_len++] = readRegister & 0xFF;
298 args[args_len++] = readRegister >> 8;
300 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
301 reportType, args, args_len, buf_recv, data_len);
303 dev_err(&client->dev,
304 "failed to retrieve report from device.\n");
312 * i2c_hid_set_or_send_report: forward an incoming report to the device
313 * @client: the i2c_client of the device
314 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
315 * @reportID: the report ID
316 * @buf: the actual data to transfer, without the report ID
318 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
320 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
321 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
323 struct i2c_hid *ihid = i2c_get_clientdata(client);
324 u8 *args = ihid->argsbuf;
325 const struct i2c_hid_cmd *hidcmd;
327 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
328 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
329 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
334 i2c_hid_dbg(ihid, "%s\n", __func__);
336 if (data_len > ihid->bufsize)
339 size = 2 /* size */ +
340 (reportID ? 1 : 0) /* reportID */ +
342 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
343 2 /* dataRegister */ +
346 if (!use_data && maxOutputLength == 0)
349 if (reportID >= 0x0F) {
350 args[index++] = reportID;
355 * use the data register for feature reports or if the device does not
356 * support the output register
359 args[index++] = dataRegister & 0xFF;
360 args[index++] = dataRegister >> 8;
361 hidcmd = &hid_set_report_cmd;
363 args[index++] = outputRegister & 0xFF;
364 args[index++] = outputRegister >> 8;
365 hidcmd = &hid_no_cmd;
368 args[index++] = size & 0xFF;
369 args[index++] = size >> 8;
372 args[index++] = reportID;
374 memcpy(&args[index], buf, data_len);
376 ret = __i2c_hid_command(client, hidcmd, reportID,
377 reportType, args, args_len, NULL, 0);
379 dev_err(&client->dev, "failed to set a report to device.\n");
386 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
388 struct i2c_hid *ihid = i2c_get_clientdata(client);
391 i2c_hid_dbg(ihid, "%s\n", __func__);
394 * Some devices require to send a command to wakeup before power on.
395 * The call will get a return value (EREMOTEIO) but device will be
396 * triggered and activated. After that, it goes like a normal device.
398 if (power_state == I2C_HID_PWR_ON &&
399 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
400 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
402 /* Device was already activated */
407 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
408 0, NULL, 0, NULL, 0);
411 dev_err(&client->dev, "failed to change power setting.\n");
417 static int i2c_hid_hwreset(struct i2c_client *client)
419 struct i2c_hid *ihid = i2c_get_clientdata(client);
422 i2c_hid_dbg(ihid, "%s\n", __func__);
425 * This prevents sending feature reports while the device is
426 * being reset. Otherwise we may lose the reset complete
429 mutex_lock(&ihid->reset_lock);
431 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
436 * The HID over I2C specification states that if a DEVICE needs time
437 * after the PWR_ON request, it should utilise CLOCK stretching.
438 * However, it has been observered that the Windows driver provides a
439 * 1ms sleep between the PWR_ON and RESET requests and that some devices
442 usleep_range(1000, 5000);
444 i2c_hid_dbg(ihid, "resetting...\n");
446 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
448 dev_err(&client->dev, "failed to reset device.\n");
449 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
453 mutex_unlock(&ihid->reset_lock);
457 static void i2c_hid_get_input(struct i2c_hid *ihid)
461 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
463 if (size > ihid->bufsize)
464 size = ihid->bufsize;
466 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
471 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
472 __func__, ret, size);
476 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
479 /* host or device initiated RESET completed */
480 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
481 wake_up(&ihid->wait);
485 if ((ret_size > size) || (ret_size < 2)) {
486 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
487 __func__, size, ret_size);
491 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
493 if (test_bit(I2C_HID_STARTED, &ihid->flags))
494 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
500 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
502 struct i2c_hid *ihid = dev_id;
504 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
507 i2c_hid_get_input(ihid);
512 static int i2c_hid_get_report_length(struct hid_report *report)
514 return ((report->size - 1) >> 3) + 1 +
515 report->device->report_enum[report->type].numbered + 2;
519 * Traverse the supplied list of reports and find the longest
521 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
524 struct hid_report *report;
527 /* We should not rely on wMaxInputLength, as some devices may set it to
529 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
530 size = i2c_hid_get_report_length(report);
536 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
540 kfree(ihid->argsbuf);
545 ihid->argsbuf = NULL;
549 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
551 /* the worst case is computed from the set_report command with a
552 * reportID > 15 and the maximum report length */
553 int args_len = sizeof(__u8) + /* ReportID */
554 sizeof(__u8) + /* optional ReportID byte */
555 sizeof(__u16) + /* data register */
556 sizeof(__u16) + /* size of the report */
557 report_size; /* report */
559 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
560 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
561 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
562 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
564 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
565 i2c_hid_free_buffers(ihid);
569 ihid->bufsize = report_size;
574 static int i2c_hid_get_raw_report(struct hid_device *hid,
575 unsigned char report_number, __u8 *buf, size_t count,
576 unsigned char report_type)
578 struct i2c_client *client = hid->driver_data;
579 struct i2c_hid *ihid = i2c_get_clientdata(client);
580 size_t ret_count, ask_count;
583 if (report_type == HID_OUTPUT_REPORT)
586 /* +2 bytes to include the size of the reply in the query buffer */
587 ask_count = min(count + 2, (size_t)ihid->bufsize);
589 ret = i2c_hid_get_report(client,
590 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
591 report_number, ihid->rawbuf, ask_count);
596 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
601 ret_count = min(ret_count, ask_count);
603 /* The query buffer contains the size, dropping it in the reply */
604 count = min(count, ret_count - 2);
605 memcpy(buf, ihid->rawbuf + 2, count);
610 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
611 size_t count, unsigned char report_type, bool use_data)
613 struct i2c_client *client = hid->driver_data;
614 struct i2c_hid *ihid = i2c_get_clientdata(client);
615 int report_id = buf[0];
618 if (report_type == HID_INPUT_REPORT)
621 mutex_lock(&ihid->reset_lock);
628 ret = i2c_hid_set_or_send_report(client,
629 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
630 report_id, buf, count, use_data);
632 if (report_id && ret >= 0)
633 ret++; /* add report_id to the number of transfered bytes */
635 mutex_unlock(&ihid->reset_lock);
640 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
643 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
647 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
648 __u8 *buf, size_t len, unsigned char rtype,
652 case HID_REQ_GET_REPORT:
653 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
654 case HID_REQ_SET_REPORT:
655 if (buf[0] != reportnum)
657 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
663 static int i2c_hid_parse(struct hid_device *hid)
665 struct i2c_client *client = hid->driver_data;
666 struct i2c_hid *ihid = i2c_get_clientdata(client);
667 struct i2c_hid_desc *hdesc = &ihid->hdesc;
673 i2c_hid_dbg(ihid, "entering %s\n", __func__);
675 rsize = le16_to_cpu(hdesc->wReportDescLength);
676 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
677 dbg_hid("weird size of report descriptor (%u)\n", rsize);
682 ret = i2c_hid_hwreset(client);
685 } while (tries-- > 0 && ret);
690 rdesc = kzalloc(rsize, GFP_KERNEL);
693 dbg_hid("couldn't allocate rdesc memory\n");
697 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
699 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
701 hid_err(hid, "reading report descriptor failed\n");
706 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
708 ret = hid_parse_report(hid, rdesc, rsize);
711 dbg_hid("parsing report descriptor failed\n");
718 static int i2c_hid_start(struct hid_device *hid)
720 struct i2c_client *client = hid->driver_data;
721 struct i2c_hid *ihid = i2c_get_clientdata(client);
723 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
725 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
726 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
727 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
729 if (bufsize > ihid->bufsize) {
730 disable_irq(client->irq);
731 i2c_hid_free_buffers(ihid);
733 ret = i2c_hid_alloc_buffers(ihid, bufsize);
734 enable_irq(client->irq);
743 static void i2c_hid_stop(struct hid_device *hid)
748 static int i2c_hid_open(struct hid_device *hid)
750 struct i2c_client *client = hid->driver_data;
751 struct i2c_hid *ihid = i2c_get_clientdata(client);
754 ret = pm_runtime_get_sync(&client->dev);
758 set_bit(I2C_HID_STARTED, &ihid->flags);
762 static void i2c_hid_close(struct hid_device *hid)
764 struct i2c_client *client = hid->driver_data;
765 struct i2c_hid *ihid = i2c_get_clientdata(client);
767 clear_bit(I2C_HID_STARTED, &ihid->flags);
769 /* Save some power */
770 pm_runtime_put(&client->dev);
773 static int i2c_hid_power(struct hid_device *hid, int lvl)
775 struct i2c_client *client = hid->driver_data;
776 struct i2c_hid *ihid = i2c_get_clientdata(client);
778 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
782 pm_runtime_get_sync(&client->dev);
785 pm_runtime_put(&client->dev);
791 struct hid_ll_driver i2c_hid_ll_driver = {
792 .parse = i2c_hid_parse,
793 .start = i2c_hid_start,
794 .stop = i2c_hid_stop,
795 .open = i2c_hid_open,
796 .close = i2c_hid_close,
797 .power = i2c_hid_power,
798 .output_report = i2c_hid_output_report,
799 .raw_request = i2c_hid_raw_request,
801 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
803 static int i2c_hid_init_irq(struct i2c_client *client)
805 struct i2c_hid *ihid = i2c_get_clientdata(client);
806 unsigned long irqflags = 0;
809 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
811 if (!irq_get_trigger_type(client->irq))
812 irqflags = IRQF_TRIGGER_LOW;
814 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
815 irqflags | IRQF_ONESHOT, client->name, ihid);
817 dev_warn(&client->dev,
818 "Could not register for %s interrupt, irq = %d,"
820 client->name, client->irq, ret);
828 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
830 struct i2c_client *client = ihid->client;
831 struct i2c_hid_desc *hdesc = &ihid->hdesc;
835 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
836 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
837 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
838 sizeof(struct i2c_hid_desc));
840 dev_err(&client->dev, "hid_descr_cmd failed\n");
844 /* Validate the length of HID descriptor, the 4 first bytes:
845 * bytes 0-1 -> length
846 * bytes 2-3 -> bcdVersion (has to be 1.00) */
847 /* check bcdVersion == 1.0 */
848 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
849 dev_err(&client->dev,
850 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
851 le16_to_cpu(hdesc->bcdVersion));
855 /* Descriptor length should be 30 bytes as per the specification */
856 dsize = le16_to_cpu(hdesc->wHIDDescLength);
857 if (dsize != sizeof(struct i2c_hid_desc)) {
858 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
862 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
867 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
869 * The CHPN0001 ACPI device, which is used to describe the Chipone
870 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
876 static int i2c_hid_acpi_pdata(struct i2c_client *client,
877 struct i2c_hid_platform_data *pdata)
879 static guid_t i2c_hid_guid =
880 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
881 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
882 union acpi_object *obj;
883 struct acpi_device *adev;
886 handle = ACPI_HANDLE(&client->dev);
887 if (!handle || acpi_bus_get_device(handle, &adev)) {
888 dev_err(&client->dev, "Error could not get ACPI device\n");
892 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
895 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
898 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
902 pdata->hid_descriptor_address = obj->integer.value;
908 static void i2c_hid_acpi_fix_up_power(struct device *dev)
910 struct acpi_device *adev;
912 adev = ACPI_COMPANION(dev);
914 acpi_device_fix_up_power(adev);
917 static const struct acpi_device_id i2c_hid_acpi_match[] = {
922 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
924 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
925 struct i2c_hid_platform_data *pdata)
930 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
934 static int i2c_hid_of_probe(struct i2c_client *client,
935 struct i2c_hid_platform_data *pdata)
937 struct device *dev = &client->dev;
941 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
943 dev_err(&client->dev, "HID register address not provided\n");
947 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
951 pdata->hid_descriptor_address = val;
956 static const struct of_device_id i2c_hid_of_match[] = {
957 { .compatible = "hid-over-i2c" },
960 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
962 static inline int i2c_hid_of_probe(struct i2c_client *client,
963 struct i2c_hid_platform_data *pdata)
969 static void i2c_hid_fwnode_probe(struct i2c_client *client,
970 struct i2c_hid_platform_data *pdata)
974 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
976 pdata->post_power_delay_ms = val;
979 static int i2c_hid_probe(struct i2c_client *client,
980 const struct i2c_device_id *dev_id)
983 struct i2c_hid *ihid;
984 struct hid_device *hid;
986 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
988 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
991 dev_err(&client->dev,
992 "HID over i2c has not been provided an Int IRQ\n");
996 if (client->irq < 0) {
997 if (client->irq != -EPROBE_DEFER)
998 dev_err(&client->dev,
999 "HID over i2c doesn't have a valid IRQ\n");
1003 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1007 if (client->dev.of_node) {
1008 ret = i2c_hid_of_probe(client, &ihid->pdata);
1011 } else if (!platform_data) {
1012 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1016 ihid->pdata = *platform_data;
1019 /* Parse platform agnostic common properties from ACPI / device tree */
1020 i2c_hid_fwnode_probe(client, &ihid->pdata);
1022 ihid->pdata.supplies[0].supply = "vdd";
1023 ihid->pdata.supplies[1].supply = "vddl";
1025 ret = devm_regulator_bulk_get(&client->dev,
1026 ARRAY_SIZE(ihid->pdata.supplies),
1027 ihid->pdata.supplies);
1031 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1032 ihid->pdata.supplies);
1036 if (ihid->pdata.post_power_delay_ms)
1037 msleep(ihid->pdata.post_power_delay_ms);
1039 i2c_set_clientdata(client, ihid);
1041 ihid->client = client;
1043 hidRegister = ihid->pdata.hid_descriptor_address;
1044 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1046 init_waitqueue_head(&ihid->wait);
1047 mutex_init(&ihid->reset_lock);
1049 /* we need to allocate the command buffer without knowing the maximum
1050 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1051 * real computation later. */
1052 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1056 i2c_hid_acpi_fix_up_power(&client->dev);
1058 pm_runtime_get_noresume(&client->dev);
1059 pm_runtime_set_active(&client->dev);
1060 pm_runtime_enable(&client->dev);
1061 device_enable_async_suspend(&client->dev);
1063 /* Make sure there is something at this address */
1064 ret = i2c_smbus_read_byte(client);
1066 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1071 ret = i2c_hid_fetch_hid_descriptor(ihid);
1075 ret = i2c_hid_init_irq(client);
1079 hid = hid_allocate_device();
1087 hid->driver_data = client;
1088 hid->ll_driver = &i2c_hid_ll_driver;
1089 hid->dev.parent = &client->dev;
1091 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1092 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1093 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1095 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1096 client->name, hid->vendor, hid->product);
1097 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1099 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1101 ret = hid_add_device(hid);
1104 hid_err(client, "can't add hid device: %d\n", ret);
1108 pm_runtime_put(&client->dev);
1112 hid_destroy_device(hid);
1115 free_irq(client->irq, ihid);
1118 pm_runtime_put_noidle(&client->dev);
1119 pm_runtime_disable(&client->dev);
1122 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1123 ihid->pdata.supplies);
1124 i2c_hid_free_buffers(ihid);
1128 static int i2c_hid_remove(struct i2c_client *client)
1130 struct i2c_hid *ihid = i2c_get_clientdata(client);
1131 struct hid_device *hid;
1133 pm_runtime_get_sync(&client->dev);
1134 pm_runtime_disable(&client->dev);
1135 pm_runtime_set_suspended(&client->dev);
1136 pm_runtime_put_noidle(&client->dev);
1139 hid_destroy_device(hid);
1141 free_irq(client->irq, ihid);
1144 i2c_hid_free_buffers(ihid);
1146 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1147 ihid->pdata.supplies);
1152 static void i2c_hid_shutdown(struct i2c_client *client)
1154 struct i2c_hid *ihid = i2c_get_clientdata(client);
1156 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1157 free_irq(client->irq, ihid);
1160 #ifdef CONFIG_PM_SLEEP
1161 static int i2c_hid_suspend(struct device *dev)
1163 struct i2c_client *client = to_i2c_client(dev);
1164 struct i2c_hid *ihid = i2c_get_clientdata(client);
1165 struct hid_device *hid = ihid->hid;
1169 if (hid->driver && hid->driver->suspend) {
1171 * Wake up the device so that IO issues in
1172 * HID driver's suspend code can succeed.
1174 ret = pm_runtime_resume(dev);
1178 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1183 if (!pm_runtime_suspended(dev)) {
1184 /* Save some power */
1185 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1187 disable_irq(client->irq);
1190 if (device_may_wakeup(&client->dev)) {
1191 wake_status = enable_irq_wake(client->irq);
1193 ihid->irq_wake_enabled = true;
1195 hid_warn(hid, "Failed to enable irq wake: %d\n",
1198 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1199 ihid->pdata.supplies);
1205 static int i2c_hid_resume(struct device *dev)
1208 struct i2c_client *client = to_i2c_client(dev);
1209 struct i2c_hid *ihid = i2c_get_clientdata(client);
1210 struct hid_device *hid = ihid->hid;
1213 if (!device_may_wakeup(&client->dev)) {
1214 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1215 ihid->pdata.supplies);
1217 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1219 if (ihid->pdata.post_power_delay_ms)
1220 msleep(ihid->pdata.post_power_delay_ms);
1221 } else if (ihid->irq_wake_enabled) {
1222 wake_status = disable_irq_wake(client->irq);
1224 ihid->irq_wake_enabled = false;
1226 hid_warn(hid, "Failed to disable irq wake: %d\n",
1230 /* We'll resume to full power */
1231 pm_runtime_disable(dev);
1232 pm_runtime_set_active(dev);
1233 pm_runtime_enable(dev);
1235 enable_irq(client->irq);
1237 /* Instead of resetting device, simply powers the device on. This
1238 * solves "incomplete reports" on Raydium devices 2386:3118 and
1241 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1245 /* Some devices need to re-send report descr cmd
1246 * after resume, after this it will be back normal.
1247 * otherwise it issues too many incomplete reports.
1249 if (ihid->quirks & I2C_HID_QUIRK_RESEND_REPORT_DESCR) {
1250 ret = i2c_hid_command(client, &hid_report_descr_cmd, NULL, 0);
1255 if (hid->driver && hid->driver->reset_resume) {
1256 ret = hid->driver->reset_resume(hid);
1265 static int i2c_hid_runtime_suspend(struct device *dev)
1267 struct i2c_client *client = to_i2c_client(dev);
1269 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1270 disable_irq(client->irq);
1274 static int i2c_hid_runtime_resume(struct device *dev)
1276 struct i2c_client *client = to_i2c_client(dev);
1278 enable_irq(client->irq);
1279 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1284 static const struct dev_pm_ops i2c_hid_pm = {
1285 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1286 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1290 static const struct i2c_device_id i2c_hid_id_table[] = {
1292 { "hid-over-i2c", 0 },
1295 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1298 static struct i2c_driver i2c_hid_driver = {
1302 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1303 .of_match_table = of_match_ptr(i2c_hid_of_match),
1306 .probe = i2c_hid_probe,
1307 .remove = i2c_hid_remove,
1308 .shutdown = i2c_hid_shutdown,
1309 .id_table = i2c_hid_id_table,
1312 module_i2c_driver(i2c_hid_driver);
1314 MODULE_DESCRIPTION("HID over I2C core driver");
1315 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1316 MODULE_LICENSE("GPL");