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 static DEFINE_MUTEX(i2c_hid_open_mut);
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;
164 static const struct i2c_hid_quirks {
168 } i2c_hid_quirks[] = {
169 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
170 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
171 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
172 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
173 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
174 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
175 { I2C_VENDOR_ID_RAYD, I2C_PRODUCT_ID_RAYD_3118,
176 I2C_HID_QUIRK_RESEND_REPORT_DESCR },
177 { USB_VENDOR_ID_SIS_TOUCH, USB_DEVICE_ID_SIS10FB_TOUCH,
178 I2C_HID_QUIRK_RESEND_REPORT_DESCR },
183 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
184 * @idVendor: the 16-bit vendor ID
185 * @idProduct: the 16-bit product ID
187 * Returns: a u32 quirks value.
189 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
194 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
195 if (i2c_hid_quirks[n].idVendor == idVendor &&
196 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
197 i2c_hid_quirks[n].idProduct == idProduct))
198 quirks = i2c_hid_quirks[n].quirks;
203 static int __i2c_hid_command(struct i2c_client *client,
204 const struct i2c_hid_cmd *command, u8 reportID,
205 u8 reportType, u8 *args, int args_len,
206 unsigned char *buf_recv, int data_len)
208 struct i2c_hid *ihid = i2c_get_clientdata(client);
209 union command *cmd = (union command *)ihid->cmdbuf;
211 struct i2c_msg msg[2];
214 int length = command->length;
215 bool wait = command->wait;
216 unsigned int registerIndex = command->registerIndex;
218 /* special case for hid_descr_cmd */
219 if (command == &hid_descr_cmd) {
220 cmd->c.reg = ihid->wHIDDescRegister;
222 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
223 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
227 cmd->c.opcode = command->opcode;
228 cmd->c.reportTypeID = reportID | reportType << 4;
231 memcpy(cmd->data + length, args, args_len);
234 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
236 msg[0].addr = client->addr;
237 msg[0].flags = client->flags & I2C_M_TEN;
239 msg[0].buf = cmd->data;
241 msg[1].addr = client->addr;
242 msg[1].flags = client->flags & I2C_M_TEN;
243 msg[1].flags |= I2C_M_RD;
244 msg[1].len = data_len;
245 msg[1].buf = buf_recv;
247 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
251 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
253 ret = i2c_transfer(client->adapter, msg, msg_num);
256 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
259 return ret < 0 ? ret : -EIO;
263 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
266 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
267 if (!wait_event_timeout(ihid->wait,
268 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
269 msecs_to_jiffies(5000)))
271 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
277 static int i2c_hid_command(struct i2c_client *client,
278 const struct i2c_hid_cmd *command,
279 unsigned char *buf_recv, int data_len)
281 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
285 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
286 u8 reportID, unsigned char *buf_recv, int data_len)
288 struct i2c_hid *ihid = i2c_get_clientdata(client);
292 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
294 i2c_hid_dbg(ihid, "%s\n", __func__);
296 if (reportID >= 0x0F) {
297 args[args_len++] = reportID;
301 args[args_len++] = readRegister & 0xFF;
302 args[args_len++] = readRegister >> 8;
304 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
305 reportType, args, args_len, buf_recv, data_len);
307 dev_err(&client->dev,
308 "failed to retrieve report from device.\n");
316 * i2c_hid_set_or_send_report: forward an incoming report to the device
317 * @client: the i2c_client of the device
318 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
319 * @reportID: the report ID
320 * @buf: the actual data to transfer, without the report ID
322 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
324 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
325 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
327 struct i2c_hid *ihid = i2c_get_clientdata(client);
328 u8 *args = ihid->argsbuf;
329 const struct i2c_hid_cmd *hidcmd;
331 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
332 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
333 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
338 i2c_hid_dbg(ihid, "%s\n", __func__);
340 if (data_len > ihid->bufsize)
343 size = 2 /* size */ +
344 (reportID ? 1 : 0) /* reportID */ +
346 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
347 2 /* dataRegister */ +
350 if (!use_data && maxOutputLength == 0)
353 if (reportID >= 0x0F) {
354 args[index++] = reportID;
359 * use the data register for feature reports or if the device does not
360 * support the output register
363 args[index++] = dataRegister & 0xFF;
364 args[index++] = dataRegister >> 8;
365 hidcmd = &hid_set_report_cmd;
367 args[index++] = outputRegister & 0xFF;
368 args[index++] = outputRegister >> 8;
369 hidcmd = &hid_no_cmd;
372 args[index++] = size & 0xFF;
373 args[index++] = size >> 8;
376 args[index++] = reportID;
378 memcpy(&args[index], buf, data_len);
380 ret = __i2c_hid_command(client, hidcmd, reportID,
381 reportType, args, args_len, NULL, 0);
383 dev_err(&client->dev, "failed to set a report to device.\n");
390 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
392 struct i2c_hid *ihid = i2c_get_clientdata(client);
395 i2c_hid_dbg(ihid, "%s\n", __func__);
398 * Some devices require to send a command to wakeup before power on.
399 * The call will get a return value (EREMOTEIO) but device will be
400 * triggered and activated. After that, it goes like a normal device.
402 if (power_state == I2C_HID_PWR_ON &&
403 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
404 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
406 /* Device was already activated */
411 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
412 0, NULL, 0, NULL, 0);
415 dev_err(&client->dev, "failed to change power setting.\n");
421 static int i2c_hid_hwreset(struct i2c_client *client)
423 struct i2c_hid *ihid = i2c_get_clientdata(client);
426 i2c_hid_dbg(ihid, "%s\n", __func__);
429 * This prevents sending feature reports while the device is
430 * being reset. Otherwise we may lose the reset complete
433 mutex_lock(&ihid->reset_lock);
435 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
440 * The HID over I2C specification states that if a DEVICE needs time
441 * after the PWR_ON request, it should utilise CLOCK stretching.
442 * However, it has been observered that the Windows driver provides a
443 * 1ms sleep between the PWR_ON and RESET requests and that some devices
446 usleep_range(1000, 5000);
448 i2c_hid_dbg(ihid, "resetting...\n");
450 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
452 dev_err(&client->dev, "failed to reset device.\n");
453 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
457 mutex_unlock(&ihid->reset_lock);
461 static void i2c_hid_get_input(struct i2c_hid *ihid)
465 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
467 if (size > ihid->bufsize)
468 size = ihid->bufsize;
470 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
475 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
476 __func__, ret, size);
480 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
483 /* host or device initiated RESET completed */
484 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
485 wake_up(&ihid->wait);
489 if ((ret_size > size) || (ret_size <= 2)) {
490 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
491 __func__, size, ret_size);
495 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
497 if (test_bit(I2C_HID_STARTED, &ihid->flags))
498 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
504 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
506 struct i2c_hid *ihid = dev_id;
508 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
511 i2c_hid_get_input(ihid);
516 static int i2c_hid_get_report_length(struct hid_report *report)
518 return ((report->size - 1) >> 3) + 1 +
519 report->device->report_enum[report->type].numbered + 2;
523 * Traverse the supplied list of reports and find the longest
525 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
528 struct hid_report *report;
531 /* We should not rely on wMaxInputLength, as some devices may set it to
533 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
534 size = i2c_hid_get_report_length(report);
540 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
544 kfree(ihid->argsbuf);
549 ihid->argsbuf = NULL;
553 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
555 /* the worst case is computed from the set_report command with a
556 * reportID > 15 and the maximum report length */
557 int args_len = sizeof(__u8) + /* ReportID */
558 sizeof(__u8) + /* optional ReportID byte */
559 sizeof(__u16) + /* data register */
560 sizeof(__u16) + /* size of the report */
561 report_size; /* report */
563 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
564 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
565 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
566 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
568 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
569 i2c_hid_free_buffers(ihid);
573 ihid->bufsize = report_size;
578 static int i2c_hid_get_raw_report(struct hid_device *hid,
579 unsigned char report_number, __u8 *buf, size_t count,
580 unsigned char report_type)
582 struct i2c_client *client = hid->driver_data;
583 struct i2c_hid *ihid = i2c_get_clientdata(client);
584 size_t ret_count, ask_count;
587 if (report_type == HID_OUTPUT_REPORT)
590 /* +2 bytes to include the size of the reply in the query buffer */
591 ask_count = min(count + 2, (size_t)ihid->bufsize);
593 ret = i2c_hid_get_report(client,
594 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
595 report_number, ihid->rawbuf, ask_count);
600 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
605 ret_count = min(ret_count, ask_count);
607 /* The query buffer contains the size, dropping it in the reply */
608 count = min(count, ret_count - 2);
609 memcpy(buf, ihid->rawbuf + 2, count);
614 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
615 size_t count, unsigned char report_type, bool use_data)
617 struct i2c_client *client = hid->driver_data;
618 struct i2c_hid *ihid = i2c_get_clientdata(client);
619 int report_id = buf[0];
622 if (report_type == HID_INPUT_REPORT)
625 mutex_lock(&ihid->reset_lock);
632 ret = i2c_hid_set_or_send_report(client,
633 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
634 report_id, buf, count, use_data);
636 if (report_id && ret >= 0)
637 ret++; /* add report_id to the number of transfered bytes */
639 mutex_unlock(&ihid->reset_lock);
644 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
647 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
651 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
652 __u8 *buf, size_t len, unsigned char rtype,
656 case HID_REQ_GET_REPORT:
657 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
658 case HID_REQ_SET_REPORT:
659 if (buf[0] != reportnum)
661 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
667 static int i2c_hid_parse(struct hid_device *hid)
669 struct i2c_client *client = hid->driver_data;
670 struct i2c_hid *ihid = i2c_get_clientdata(client);
671 struct i2c_hid_desc *hdesc = &ihid->hdesc;
677 i2c_hid_dbg(ihid, "entering %s\n", __func__);
679 rsize = le16_to_cpu(hdesc->wReportDescLength);
680 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
681 dbg_hid("weird size of report descriptor (%u)\n", rsize);
686 ret = i2c_hid_hwreset(client);
689 } while (tries-- > 0 && ret);
694 rdesc = kzalloc(rsize, GFP_KERNEL);
697 dbg_hid("couldn't allocate rdesc memory\n");
701 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
703 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
705 hid_err(hid, "reading report descriptor failed\n");
710 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
712 ret = hid_parse_report(hid, rdesc, rsize);
715 dbg_hid("parsing report descriptor failed\n");
722 static int i2c_hid_start(struct hid_device *hid)
724 struct i2c_client *client = hid->driver_data;
725 struct i2c_hid *ihid = i2c_get_clientdata(client);
727 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
729 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
730 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
731 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
733 if (bufsize > ihid->bufsize) {
734 disable_irq(client->irq);
735 i2c_hid_free_buffers(ihid);
737 ret = i2c_hid_alloc_buffers(ihid, bufsize);
738 enable_irq(client->irq);
747 static void i2c_hid_stop(struct hid_device *hid)
752 static int i2c_hid_open(struct hid_device *hid)
754 struct i2c_client *client = hid->driver_data;
755 struct i2c_hid *ihid = i2c_get_clientdata(client);
758 ret = pm_runtime_get_sync(&client->dev);
762 set_bit(I2C_HID_STARTED, &ihid->flags);
766 static void i2c_hid_close(struct hid_device *hid)
768 struct i2c_client *client = hid->driver_data;
769 struct i2c_hid *ihid = i2c_get_clientdata(client);
771 clear_bit(I2C_HID_STARTED, &ihid->flags);
773 /* Save some power */
774 pm_runtime_put(&client->dev);
777 static int i2c_hid_power(struct hid_device *hid, int lvl)
779 struct i2c_client *client = hid->driver_data;
780 struct i2c_hid *ihid = i2c_get_clientdata(client);
782 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
786 pm_runtime_get_sync(&client->dev);
789 pm_runtime_put(&client->dev);
795 struct hid_ll_driver i2c_hid_ll_driver = {
796 .parse = i2c_hid_parse,
797 .start = i2c_hid_start,
798 .stop = i2c_hid_stop,
799 .open = i2c_hid_open,
800 .close = i2c_hid_close,
801 .power = i2c_hid_power,
802 .output_report = i2c_hid_output_report,
803 .raw_request = i2c_hid_raw_request,
805 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
807 static int i2c_hid_init_irq(struct i2c_client *client)
809 struct i2c_hid *ihid = i2c_get_clientdata(client);
810 unsigned long irqflags = 0;
813 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
815 if (!irq_get_trigger_type(client->irq))
816 irqflags = IRQF_TRIGGER_LOW;
818 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
819 irqflags | IRQF_ONESHOT, client->name, ihid);
821 dev_warn(&client->dev,
822 "Could not register for %s interrupt, irq = %d,"
824 client->name, client->irq, ret);
832 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
834 struct i2c_client *client = ihid->client;
835 struct i2c_hid_desc *hdesc = &ihid->hdesc;
839 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
840 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
841 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
842 sizeof(struct i2c_hid_desc));
844 dev_err(&client->dev, "hid_descr_cmd failed\n");
848 /* Validate the length of HID descriptor, the 4 first bytes:
849 * bytes 0-1 -> length
850 * bytes 2-3 -> bcdVersion (has to be 1.00) */
851 /* check bcdVersion == 1.0 */
852 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
853 dev_err(&client->dev,
854 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
855 le16_to_cpu(hdesc->bcdVersion));
859 /* Descriptor length should be 30 bytes as per the specification */
860 dsize = le16_to_cpu(hdesc->wHIDDescLength);
861 if (dsize != sizeof(struct i2c_hid_desc)) {
862 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
866 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
871 static int i2c_hid_acpi_pdata(struct i2c_client *client,
872 struct i2c_hid_platform_data *pdata)
874 static guid_t i2c_hid_guid =
875 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
876 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
877 union acpi_object *obj;
878 struct acpi_device *adev;
881 handle = ACPI_HANDLE(&client->dev);
882 if (!handle || acpi_bus_get_device(handle, &adev))
885 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
888 dev_err(&client->dev, "device _DSM execution failed\n");
892 pdata->hid_descriptor_address = obj->integer.value;
898 static void i2c_hid_acpi_fix_up_power(struct device *dev)
900 struct acpi_device *adev;
902 adev = ACPI_COMPANION(dev);
904 acpi_device_fix_up_power(adev);
907 static const struct acpi_device_id i2c_hid_acpi_match[] = {
912 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
914 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
915 struct i2c_hid_platform_data *pdata)
920 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
924 static int i2c_hid_of_probe(struct i2c_client *client,
925 struct i2c_hid_platform_data *pdata)
927 struct device *dev = &client->dev;
931 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
933 dev_err(&client->dev, "HID register address not provided\n");
937 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
941 pdata->hid_descriptor_address = val;
946 static const struct of_device_id i2c_hid_of_match[] = {
947 { .compatible = "hid-over-i2c" },
950 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
952 static inline int i2c_hid_of_probe(struct i2c_client *client,
953 struct i2c_hid_platform_data *pdata)
959 static void i2c_hid_fwnode_probe(struct i2c_client *client,
960 struct i2c_hid_platform_data *pdata)
964 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
966 pdata->post_power_delay_ms = val;
969 static int i2c_hid_probe(struct i2c_client *client,
970 const struct i2c_device_id *dev_id)
973 struct i2c_hid *ihid;
974 struct hid_device *hid;
976 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
978 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
981 dev_err(&client->dev,
982 "HID over i2c has not been provided an Int IRQ\n");
986 if (client->irq < 0) {
987 if (client->irq != -EPROBE_DEFER)
988 dev_err(&client->dev,
989 "HID over i2c doesn't have a valid IRQ\n");
993 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
997 if (client->dev.of_node) {
998 ret = i2c_hid_of_probe(client, &ihid->pdata);
1001 } else if (!platform_data) {
1002 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1004 dev_err(&client->dev,
1005 "HID register address not provided\n");
1009 ihid->pdata = *platform_data;
1012 /* Parse platform agnostic common properties from ACPI / device tree */
1013 i2c_hid_fwnode_probe(client, &ihid->pdata);
1015 ihid->pdata.supply = devm_regulator_get(&client->dev, "vdd");
1016 if (IS_ERR(ihid->pdata.supply)) {
1017 ret = PTR_ERR(ihid->pdata.supply);
1018 if (ret != -EPROBE_DEFER)
1019 dev_err(&client->dev, "Failed to get regulator: %d\n",
1024 ret = regulator_enable(ihid->pdata.supply);
1026 dev_err(&client->dev, "Failed to enable regulator: %d\n",
1030 if (ihid->pdata.post_power_delay_ms)
1031 msleep(ihid->pdata.post_power_delay_ms);
1033 i2c_set_clientdata(client, ihid);
1035 ihid->client = client;
1037 hidRegister = ihid->pdata.hid_descriptor_address;
1038 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1040 init_waitqueue_head(&ihid->wait);
1041 mutex_init(&ihid->reset_lock);
1043 /* we need to allocate the command buffer without knowing the maximum
1044 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1045 * real computation later. */
1046 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1050 i2c_hid_acpi_fix_up_power(&client->dev);
1052 pm_runtime_get_noresume(&client->dev);
1053 pm_runtime_set_active(&client->dev);
1054 pm_runtime_enable(&client->dev);
1055 device_enable_async_suspend(&client->dev);
1057 ret = i2c_hid_fetch_hid_descriptor(ihid);
1061 ret = i2c_hid_init_irq(client);
1065 hid = hid_allocate_device();
1073 hid->driver_data = client;
1074 hid->ll_driver = &i2c_hid_ll_driver;
1075 hid->dev.parent = &client->dev;
1077 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1078 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1079 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1081 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1082 client->name, hid->vendor, hid->product);
1083 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1085 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1087 ret = hid_add_device(hid);
1090 hid_err(client, "can't add hid device: %d\n", ret);
1094 pm_runtime_put(&client->dev);
1098 hid_destroy_device(hid);
1101 free_irq(client->irq, ihid);
1104 pm_runtime_put_noidle(&client->dev);
1105 pm_runtime_disable(&client->dev);
1108 regulator_disable(ihid->pdata.supply);
1111 i2c_hid_free_buffers(ihid);
1116 static int i2c_hid_remove(struct i2c_client *client)
1118 struct i2c_hid *ihid = i2c_get_clientdata(client);
1119 struct hid_device *hid;
1121 pm_runtime_get_sync(&client->dev);
1122 pm_runtime_disable(&client->dev);
1123 pm_runtime_set_suspended(&client->dev);
1124 pm_runtime_put_noidle(&client->dev);
1127 hid_destroy_device(hid);
1129 free_irq(client->irq, ihid);
1132 i2c_hid_free_buffers(ihid);
1134 regulator_disable(ihid->pdata.supply);
1141 static void i2c_hid_shutdown(struct i2c_client *client)
1143 struct i2c_hid *ihid = i2c_get_clientdata(client);
1145 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1146 free_irq(client->irq, ihid);
1149 #ifdef CONFIG_PM_SLEEP
1150 static int i2c_hid_suspend(struct device *dev)
1152 struct i2c_client *client = to_i2c_client(dev);
1153 struct i2c_hid *ihid = i2c_get_clientdata(client);
1154 struct hid_device *hid = ihid->hid;
1158 if (hid->driver && hid->driver->suspend) {
1160 * Wake up the device so that IO issues in
1161 * HID driver's suspend code can succeed.
1163 ret = pm_runtime_resume(dev);
1167 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1172 if (!pm_runtime_suspended(dev)) {
1173 /* Save some power */
1174 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1176 disable_irq(client->irq);
1179 if (device_may_wakeup(&client->dev)) {
1180 wake_status = enable_irq_wake(client->irq);
1182 ihid->irq_wake_enabled = true;
1184 hid_warn(hid, "Failed to enable irq wake: %d\n",
1187 ret = regulator_disable(ihid->pdata.supply);
1189 hid_warn(hid, "Failed to disable supply: %d\n", ret);
1195 static int i2c_hid_resume(struct device *dev)
1198 struct i2c_client *client = to_i2c_client(dev);
1199 struct i2c_hid *ihid = i2c_get_clientdata(client);
1200 struct hid_device *hid = ihid->hid;
1203 if (!device_may_wakeup(&client->dev)) {
1204 ret = regulator_enable(ihid->pdata.supply);
1206 hid_warn(hid, "Failed to enable supply: %d\n", ret);
1207 if (ihid->pdata.post_power_delay_ms)
1208 msleep(ihid->pdata.post_power_delay_ms);
1209 } else if (ihid->irq_wake_enabled) {
1210 wake_status = disable_irq_wake(client->irq);
1212 ihid->irq_wake_enabled = false;
1214 hid_warn(hid, "Failed to disable irq wake: %d\n",
1218 /* We'll resume to full power */
1219 pm_runtime_disable(dev);
1220 pm_runtime_set_active(dev);
1221 pm_runtime_enable(dev);
1223 enable_irq(client->irq);
1224 ret = i2c_hid_hwreset(client);
1228 /* RAYDIUM device (2386:3118) need to re-send report descr cmd
1229 * after resume, after this it will be back normal.
1230 * otherwise it issues too many incomplete reports.
1232 if (ihid->quirks & I2C_HID_QUIRK_RESEND_REPORT_DESCR) {
1233 ret = i2c_hid_command(client, &hid_report_descr_cmd, NULL, 0);
1238 if (hid->driver && hid->driver->reset_resume) {
1239 ret = hid->driver->reset_resume(hid);
1248 static int i2c_hid_runtime_suspend(struct device *dev)
1250 struct i2c_client *client = to_i2c_client(dev);
1252 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1253 disable_irq(client->irq);
1257 static int i2c_hid_runtime_resume(struct device *dev)
1259 struct i2c_client *client = to_i2c_client(dev);
1261 enable_irq(client->irq);
1262 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1267 static const struct dev_pm_ops i2c_hid_pm = {
1268 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1269 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1273 static const struct i2c_device_id i2c_hid_id_table[] = {
1275 { "hid-over-i2c", 0 },
1278 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1281 static struct i2c_driver i2c_hid_driver = {
1285 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1286 .of_match_table = of_match_ptr(i2c_hid_of_match),
1289 .probe = i2c_hid_probe,
1290 .remove = i2c_hid_remove,
1291 .shutdown = i2c_hid_shutdown,
1292 .id_table = i2c_hid_id_table,
1295 module_i2c_driver(i2c_hid_driver);
1297 MODULE_DESCRIPTION("HID over I2C core driver");
1298 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1299 MODULE_LICENSE("GPL");