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/device.h>
30 #include <linux/wait.h>
31 #include <linux/err.h>
32 #include <linux/string.h>
33 #include <linux/list.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/hid.h>
37 #include <linux/mutex.h>
38 #include <linux/acpi.h>
40 #include <linux/regulator/consumer.h>
42 #include <linux/platform_data/i2c-hid.h>
44 #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_BOGUS_IRQ BIT(4)
51 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
52 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
56 #define I2C_HID_STARTED 0
57 #define I2C_HID_RESET_PENDING 1
58 #define I2C_HID_READ_PENDING 2
60 #define I2C_HID_PWR_ON 0x00
61 #define I2C_HID_PWR_SLEEP 0x01
65 module_param(debug, bool, 0444);
66 MODULE_PARM_DESC(debug, "print a lot of debug information");
68 #define i2c_hid_dbg(ihid, fmt, arg...) \
71 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
75 __le16 wHIDDescLength;
77 __le16 wReportDescLength;
78 __le16 wReportDescRegister;
79 __le16 wInputRegister;
80 __le16 wMaxInputLength;
81 __le16 wOutputRegister;
82 __le16 wMaxOutputLength;
83 __le16 wCommandRegister;
92 unsigned int registerIndex;
107 #define I2C_HID_CMD(opcode_) \
108 .opcode = opcode_, .length = 4, \
109 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
111 /* fetch HID descriptor */
112 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
113 /* fetch report descriptors */
114 static const struct i2c_hid_cmd hid_report_descr_cmd = {
115 .registerIndex = offsetof(struct i2c_hid_desc,
116 wReportDescRegister),
120 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
122 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
123 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
124 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
125 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
128 * These definitions are not used here, but are defined by the spec.
129 * Keeping them here for documentation purposes.
131 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
132 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
133 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
134 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
137 /* The main device structure */
139 struct i2c_client *client; /* i2c client */
140 struct hid_device *hid; /* pointer to corresponding HID dev */
142 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
143 struct i2c_hid_desc hdesc; /* the HID Descriptor */
145 __le16 wHIDDescRegister; /* location of the i2c
146 * register of the HID
148 unsigned int bufsize; /* i2c buffer size */
149 u8 *inbuf; /* Input buffer */
150 u8 *rawbuf; /* Raw Input buffer */
151 u8 *cmdbuf; /* Command buffer */
152 u8 *argsbuf; /* Command arguments buffer */
154 unsigned long flags; /* device flags */
155 unsigned long quirks; /* Various quirks */
157 wait_queue_head_t wait; /* For waiting the interrupt */
159 struct i2c_hid_platform_data pdata;
161 bool irq_wake_enabled;
162 struct mutex reset_lock;
165 static const struct i2c_hid_quirks {
169 } i2c_hid_quirks[] = {
170 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
171 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
172 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
173 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
174 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
175 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
176 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
177 I2C_HID_QUIRK_BOGUS_IRQ },
178 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
179 I2C_HID_QUIRK_RESET_ON_RESUME },
180 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
181 I2C_HID_QUIRK_RESET_ON_RESUME },
182 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
183 I2C_HID_QUIRK_BAD_INPUT_SIZE },
188 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
189 * @idVendor: the 16-bit vendor ID
190 * @idProduct: the 16-bit product ID
192 * Returns: a u32 quirks value.
194 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
199 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
200 if (i2c_hid_quirks[n].idVendor == idVendor &&
201 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
202 i2c_hid_quirks[n].idProduct == idProduct))
203 quirks = i2c_hid_quirks[n].quirks;
208 static int __i2c_hid_command(struct i2c_client *client,
209 const struct i2c_hid_cmd *command, u8 reportID,
210 u8 reportType, u8 *args, int args_len,
211 unsigned char *buf_recv, int data_len)
213 struct i2c_hid *ihid = i2c_get_clientdata(client);
214 union command *cmd = (union command *)ihid->cmdbuf;
216 struct i2c_msg msg[2];
219 int length = command->length;
220 bool wait = command->wait;
221 unsigned int registerIndex = command->registerIndex;
223 /* special case for hid_descr_cmd */
224 if (command == &hid_descr_cmd) {
225 cmd->c.reg = ihid->wHIDDescRegister;
227 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
228 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
232 cmd->c.opcode = command->opcode;
233 cmd->c.reportTypeID = reportID | reportType << 4;
236 memcpy(cmd->data + length, args, args_len);
239 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
241 msg[0].addr = client->addr;
242 msg[0].flags = client->flags & I2C_M_TEN;
244 msg[0].buf = cmd->data;
246 msg[1].addr = client->addr;
247 msg[1].flags = client->flags & I2C_M_TEN;
248 msg[1].flags |= I2C_M_RD;
249 msg[1].len = data_len;
250 msg[1].buf = buf_recv;
252 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
256 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
258 ret = i2c_transfer(client->adapter, msg, msg_num);
261 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
264 return ret < 0 ? ret : -EIO;
268 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
271 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
272 if (!wait_event_timeout(ihid->wait,
273 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
274 msecs_to_jiffies(5000)))
276 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
282 static int i2c_hid_command(struct i2c_client *client,
283 const struct i2c_hid_cmd *command,
284 unsigned char *buf_recv, int data_len)
286 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
290 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
291 u8 reportID, unsigned char *buf_recv, int data_len)
293 struct i2c_hid *ihid = i2c_get_clientdata(client);
297 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
299 i2c_hid_dbg(ihid, "%s\n", __func__);
301 if (reportID >= 0x0F) {
302 args[args_len++] = reportID;
306 args[args_len++] = readRegister & 0xFF;
307 args[args_len++] = readRegister >> 8;
309 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
310 reportType, args, args_len, buf_recv, data_len);
312 dev_err(&client->dev,
313 "failed to retrieve report from device.\n");
321 * i2c_hid_set_or_send_report: forward an incoming report to the device
322 * @client: the i2c_client of the device
323 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
324 * @reportID: the report ID
325 * @buf: the actual data to transfer, without the report ID
326 * @data_len: size of buf
327 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
329 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
330 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
332 struct i2c_hid *ihid = i2c_get_clientdata(client);
333 u8 *args = ihid->argsbuf;
334 const struct i2c_hid_cmd *hidcmd;
336 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
337 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
338 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
343 i2c_hid_dbg(ihid, "%s\n", __func__);
345 if (data_len > ihid->bufsize)
348 size = 2 /* size */ +
349 (reportID ? 1 : 0) /* reportID */ +
351 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
352 2 /* dataRegister */ +
355 if (!use_data && maxOutputLength == 0)
358 if (reportID >= 0x0F) {
359 args[index++] = reportID;
364 * use the data register for feature reports or if the device does not
365 * support the output register
368 args[index++] = dataRegister & 0xFF;
369 args[index++] = dataRegister >> 8;
370 hidcmd = &hid_set_report_cmd;
372 args[index++] = outputRegister & 0xFF;
373 args[index++] = outputRegister >> 8;
374 hidcmd = &hid_no_cmd;
377 args[index++] = size & 0xFF;
378 args[index++] = size >> 8;
381 args[index++] = reportID;
383 memcpy(&args[index], buf, data_len);
385 ret = __i2c_hid_command(client, hidcmd, reportID,
386 reportType, args, args_len, NULL, 0);
388 dev_err(&client->dev, "failed to set a report to device.\n");
395 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
397 struct i2c_hid *ihid = i2c_get_clientdata(client);
400 i2c_hid_dbg(ihid, "%s\n", __func__);
403 * Some devices require to send a command to wakeup before power on.
404 * The call will get a return value (EREMOTEIO) but device will be
405 * triggered and activated. After that, it goes like a normal device.
407 if (power_state == I2C_HID_PWR_ON &&
408 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
409 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
411 /* Device was already activated */
416 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
417 0, NULL, 0, NULL, 0);
420 dev_err(&client->dev, "failed to change power setting.\n");
425 * The HID over I2C specification states that if a DEVICE needs time
426 * after the PWR_ON request, it should utilise CLOCK stretching.
427 * However, it has been observered that the Windows driver provides a
428 * 1ms sleep between the PWR_ON and RESET requests.
429 * According to Goodix Windows even waits 60 ms after (other?)
430 * PWR_ON requests. Testing has confirmed that several devices
431 * will not work properly without a delay after a PWR_ON request.
433 if (!ret && power_state == I2C_HID_PWR_ON)
439 static int i2c_hid_hwreset(struct i2c_client *client)
441 struct i2c_hid *ihid = i2c_get_clientdata(client);
444 i2c_hid_dbg(ihid, "%s\n", __func__);
447 * This prevents sending feature reports while the device is
448 * being reset. Otherwise we may lose the reset complete
451 mutex_lock(&ihid->reset_lock);
453 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
457 i2c_hid_dbg(ihid, "resetting...\n");
459 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
461 dev_err(&client->dev, "failed to reset device.\n");
462 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
466 /* At least some SIS devices need this after reset */
467 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
470 mutex_unlock(&ihid->reset_lock);
474 static void i2c_hid_get_input(struct i2c_hid *ihid)
478 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
480 if (size > ihid->bufsize)
481 size = ihid->bufsize;
483 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
488 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
489 __func__, ret, size);
493 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
496 /* host or device initiated RESET completed */
497 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
498 wake_up(&ihid->wait);
502 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
503 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
504 "there's no data\n", __func__);
508 if ((ret_size > size) || (ret_size < 2)) {
509 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
510 ihid->inbuf[0] = size & 0xff;
511 ihid->inbuf[1] = size >> 8;
514 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
515 __func__, size, ret_size);
520 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
522 if (test_bit(I2C_HID_STARTED, &ihid->flags))
523 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
529 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
531 struct i2c_hid *ihid = dev_id;
533 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
536 i2c_hid_get_input(ihid);
541 static int i2c_hid_get_report_length(struct hid_report *report)
543 return ((report->size - 1) >> 3) + 1 +
544 report->device->report_enum[report->type].numbered + 2;
548 * Traverse the supplied list of reports and find the longest
550 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
553 struct hid_report *report;
556 /* We should not rely on wMaxInputLength, as some devices may set it to
558 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
559 size = i2c_hid_get_report_length(report);
565 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
569 kfree(ihid->argsbuf);
574 ihid->argsbuf = NULL;
578 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
580 /* the worst case is computed from the set_report command with a
581 * reportID > 15 and the maximum report length */
582 int args_len = sizeof(__u8) + /* ReportID */
583 sizeof(__u8) + /* optional ReportID byte */
584 sizeof(__u16) + /* data register */
585 sizeof(__u16) + /* size of the report */
586 report_size; /* report */
588 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
589 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
590 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
591 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
593 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
594 i2c_hid_free_buffers(ihid);
598 ihid->bufsize = report_size;
603 static int i2c_hid_get_raw_report(struct hid_device *hid,
604 unsigned char report_number, __u8 *buf, size_t count,
605 unsigned char report_type)
607 struct i2c_client *client = hid->driver_data;
608 struct i2c_hid *ihid = i2c_get_clientdata(client);
609 size_t ret_count, ask_count;
612 if (report_type == HID_OUTPUT_REPORT)
615 /* +2 bytes to include the size of the reply in the query buffer */
616 ask_count = min(count + 2, (size_t)ihid->bufsize);
618 ret = i2c_hid_get_report(client,
619 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
620 report_number, ihid->rawbuf, ask_count);
625 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
630 ret_count = min(ret_count, ask_count);
632 /* The query buffer contains the size, dropping it in the reply */
633 count = min(count, ret_count - 2);
634 memcpy(buf, ihid->rawbuf + 2, count);
639 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
640 size_t count, unsigned char report_type, bool use_data)
642 struct i2c_client *client = hid->driver_data;
643 struct i2c_hid *ihid = i2c_get_clientdata(client);
644 int report_id = buf[0];
647 if (report_type == HID_INPUT_REPORT)
650 mutex_lock(&ihid->reset_lock);
657 ret = i2c_hid_set_or_send_report(client,
658 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
659 report_id, buf, count, use_data);
661 if (report_id && ret >= 0)
662 ret++; /* add report_id to the number of transfered bytes */
664 mutex_unlock(&ihid->reset_lock);
669 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
672 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
676 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
677 __u8 *buf, size_t len, unsigned char rtype,
681 case HID_REQ_GET_REPORT:
682 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
683 case HID_REQ_SET_REPORT:
684 if (buf[0] != reportnum)
686 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
692 static int i2c_hid_parse(struct hid_device *hid)
694 struct i2c_client *client = hid->driver_data;
695 struct i2c_hid *ihid = i2c_get_clientdata(client);
696 struct i2c_hid_desc *hdesc = &ihid->hdesc;
703 i2c_hid_dbg(ihid, "entering %s\n", __func__);
705 rsize = le16_to_cpu(hdesc->wReportDescLength);
706 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
707 dbg_hid("weird size of report descriptor (%u)\n", rsize);
712 ret = i2c_hid_hwreset(client);
715 } while (tries-- > 0 && ret);
720 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
724 rdesc = use_override;
725 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
727 rdesc = kzalloc(rsize, GFP_KERNEL);
730 dbg_hid("couldn't allocate rdesc memory\n");
734 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
736 ret = i2c_hid_command(client, &hid_report_descr_cmd,
739 hid_err(hid, "reading report descriptor failed\n");
745 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
747 ret = hid_parse_report(hid, rdesc, rsize);
752 dbg_hid("parsing report descriptor failed\n");
759 static int i2c_hid_start(struct hid_device *hid)
761 struct i2c_client *client = hid->driver_data;
762 struct i2c_hid *ihid = i2c_get_clientdata(client);
764 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
766 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
767 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
768 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
770 if (bufsize > ihid->bufsize) {
771 disable_irq(client->irq);
772 i2c_hid_free_buffers(ihid);
774 ret = i2c_hid_alloc_buffers(ihid, bufsize);
775 enable_irq(client->irq);
784 static void i2c_hid_stop(struct hid_device *hid)
789 static int i2c_hid_open(struct hid_device *hid)
791 struct i2c_client *client = hid->driver_data;
792 struct i2c_hid *ihid = i2c_get_clientdata(client);
794 set_bit(I2C_HID_STARTED, &ihid->flags);
798 static void i2c_hid_close(struct hid_device *hid)
800 struct i2c_client *client = hid->driver_data;
801 struct i2c_hid *ihid = i2c_get_clientdata(client);
803 clear_bit(I2C_HID_STARTED, &ihid->flags);
806 struct hid_ll_driver i2c_hid_ll_driver = {
807 .parse = i2c_hid_parse,
808 .start = i2c_hid_start,
809 .stop = i2c_hid_stop,
810 .open = i2c_hid_open,
811 .close = i2c_hid_close,
812 .output_report = i2c_hid_output_report,
813 .raw_request = i2c_hid_raw_request,
815 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
817 static int i2c_hid_init_irq(struct i2c_client *client)
819 struct i2c_hid *ihid = i2c_get_clientdata(client);
820 unsigned long irqflags = 0;
823 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
825 if (!irq_get_trigger_type(client->irq))
826 irqflags = IRQF_TRIGGER_LOW;
828 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
829 irqflags | IRQF_ONESHOT, client->name, ihid);
831 dev_warn(&client->dev,
832 "Could not register for %s interrupt, irq = %d,"
834 client->name, client->irq, ret);
842 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
844 struct i2c_client *client = ihid->client;
845 struct i2c_hid_desc *hdesc = &ihid->hdesc;
849 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
850 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
851 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
853 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
855 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
856 ret = i2c_hid_command(client, &hid_descr_cmd,
858 sizeof(struct i2c_hid_desc));
860 dev_err(&client->dev, "hid_descr_cmd failed\n");
865 /* Validate the length of HID descriptor, the 4 first bytes:
866 * bytes 0-1 -> length
867 * bytes 2-3 -> bcdVersion (has to be 1.00) */
868 /* check bcdVersion == 1.0 */
869 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
870 dev_err(&client->dev,
871 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
872 le16_to_cpu(hdesc->bcdVersion));
876 /* Descriptor length should be 30 bytes as per the specification */
877 dsize = le16_to_cpu(hdesc->wHIDDescLength);
878 if (dsize != sizeof(struct i2c_hid_desc)) {
879 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
883 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
888 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
890 * The CHPN0001 ACPI device, which is used to describe the Chipone
891 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
897 static int i2c_hid_acpi_pdata(struct i2c_client *client,
898 struct i2c_hid_platform_data *pdata)
900 static guid_t i2c_hid_guid =
901 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
902 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
903 union acpi_object *obj;
904 struct acpi_device *adev;
907 handle = ACPI_HANDLE(&client->dev);
908 if (!handle || acpi_bus_get_device(handle, &adev)) {
909 dev_err(&client->dev, "Error could not get ACPI device\n");
913 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
916 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
919 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
923 pdata->hid_descriptor_address = obj->integer.value;
929 static void i2c_hid_acpi_fix_up_power(struct device *dev)
931 struct acpi_device *adev;
933 adev = ACPI_COMPANION(dev);
935 acpi_device_fix_up_power(adev);
938 static const struct acpi_device_id i2c_hid_acpi_match[] = {
943 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
945 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
946 struct i2c_hid_platform_data *pdata)
951 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
955 static int i2c_hid_of_probe(struct i2c_client *client,
956 struct i2c_hid_platform_data *pdata)
958 struct device *dev = &client->dev;
962 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
964 dev_err(&client->dev, "HID register address not provided\n");
968 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
972 pdata->hid_descriptor_address = val;
977 static const struct of_device_id i2c_hid_of_match[] = {
978 { .compatible = "hid-over-i2c" },
981 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
983 static inline int i2c_hid_of_probe(struct i2c_client *client,
984 struct i2c_hid_platform_data *pdata)
990 static void i2c_hid_fwnode_probe(struct i2c_client *client,
991 struct i2c_hid_platform_data *pdata)
995 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
997 pdata->post_power_delay_ms = val;
1000 static int i2c_hid_probe(struct i2c_client *client,
1001 const struct i2c_device_id *dev_id)
1004 struct i2c_hid *ihid;
1005 struct hid_device *hid;
1007 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1009 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1012 dev_err(&client->dev,
1013 "HID over i2c has not been provided an Int IRQ\n");
1017 if (client->irq < 0) {
1018 if (client->irq != -EPROBE_DEFER)
1019 dev_err(&client->dev,
1020 "HID over i2c doesn't have a valid IRQ\n");
1024 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1028 if (client->dev.of_node) {
1029 ret = i2c_hid_of_probe(client, &ihid->pdata);
1032 } else if (!platform_data) {
1033 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1037 ihid->pdata = *platform_data;
1040 /* Parse platform agnostic common properties from ACPI / device tree */
1041 i2c_hid_fwnode_probe(client, &ihid->pdata);
1043 ihid->pdata.supplies[0].supply = "vdd";
1044 ihid->pdata.supplies[1].supply = "vddl";
1046 ret = devm_regulator_bulk_get(&client->dev,
1047 ARRAY_SIZE(ihid->pdata.supplies),
1048 ihid->pdata.supplies);
1052 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1053 ihid->pdata.supplies);
1057 if (ihid->pdata.post_power_delay_ms)
1058 msleep(ihid->pdata.post_power_delay_ms);
1060 i2c_set_clientdata(client, ihid);
1062 ihid->client = client;
1064 hidRegister = ihid->pdata.hid_descriptor_address;
1065 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1067 init_waitqueue_head(&ihid->wait);
1068 mutex_init(&ihid->reset_lock);
1070 /* we need to allocate the command buffer without knowing the maximum
1071 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1072 * real computation later. */
1073 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1077 i2c_hid_acpi_fix_up_power(&client->dev);
1079 device_enable_async_suspend(&client->dev);
1081 /* Make sure there is something at this address */
1082 ret = i2c_smbus_read_byte(client);
1084 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1089 ret = i2c_hid_fetch_hid_descriptor(ihid);
1093 ret = i2c_hid_init_irq(client);
1097 hid = hid_allocate_device();
1105 hid->driver_data = client;
1106 hid->ll_driver = &i2c_hid_ll_driver;
1107 hid->dev.parent = &client->dev;
1109 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1110 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1111 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1113 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1114 client->name, hid->vendor, hid->product);
1115 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1117 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1119 ret = hid_add_device(hid);
1122 hid_err(client, "can't add hid device: %d\n", ret);
1129 hid_destroy_device(hid);
1132 free_irq(client->irq, ihid);
1135 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1136 ihid->pdata.supplies);
1137 i2c_hid_free_buffers(ihid);
1141 static int i2c_hid_remove(struct i2c_client *client)
1143 struct i2c_hid *ihid = i2c_get_clientdata(client);
1144 struct hid_device *hid;
1147 hid_destroy_device(hid);
1149 free_irq(client->irq, ihid);
1152 i2c_hid_free_buffers(ihid);
1154 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1155 ihid->pdata.supplies);
1160 static void i2c_hid_shutdown(struct i2c_client *client)
1162 struct i2c_hid *ihid = i2c_get_clientdata(client);
1164 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1165 free_irq(client->irq, ihid);
1168 #ifdef CONFIG_PM_SLEEP
1169 static int i2c_hid_suspend(struct device *dev)
1171 struct i2c_client *client = to_i2c_client(dev);
1172 struct i2c_hid *ihid = i2c_get_clientdata(client);
1173 struct hid_device *hid = ihid->hid;
1177 if (hid->driver && hid->driver->suspend) {
1178 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1183 /* Save some power */
1184 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1186 disable_irq(client->irq);
1188 if (device_may_wakeup(&client->dev)) {
1189 wake_status = enable_irq_wake(client->irq);
1191 ihid->irq_wake_enabled = true;
1193 hid_warn(hid, "Failed to enable irq wake: %d\n",
1196 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1197 ihid->pdata.supplies);
1203 static int i2c_hid_resume(struct device *dev)
1206 struct i2c_client *client = to_i2c_client(dev);
1207 struct i2c_hid *ihid = i2c_get_clientdata(client);
1208 struct hid_device *hid = ihid->hid;
1211 if (!device_may_wakeup(&client->dev)) {
1212 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1213 ihid->pdata.supplies);
1215 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1217 if (ihid->pdata.post_power_delay_ms)
1218 msleep(ihid->pdata.post_power_delay_ms);
1219 } else if (ihid->irq_wake_enabled) {
1220 wake_status = disable_irq_wake(client->irq);
1222 ihid->irq_wake_enabled = false;
1224 hid_warn(hid, "Failed to disable irq wake: %d\n",
1228 enable_irq(client->irq);
1230 /* Instead of resetting device, simply powers the device on. This
1231 * solves "incomplete reports" on Raydium devices 2386:3118 and
1232 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1233 * data after a suspend/resume.
1235 * However some ALPS touchpads generate IRQ storm without reset, so
1236 * let's still reset them here.
1238 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1239 ret = i2c_hid_hwreset(client);
1241 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1246 if (hid->driver && hid->driver->reset_resume) {
1247 ret = hid->driver->reset_resume(hid);
1255 static const struct dev_pm_ops i2c_hid_pm = {
1256 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1259 static const struct i2c_device_id i2c_hid_id_table[] = {
1261 { "hid-over-i2c", 0 },
1264 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1267 static struct i2c_driver i2c_hid_driver = {
1271 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1272 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1273 .of_match_table = of_match_ptr(i2c_hid_of_match),
1276 .probe = i2c_hid_probe,
1277 .remove = i2c_hid_remove,
1278 .shutdown = i2c_hid_shutdown,
1279 .id_table = i2c_hid_id_table,
1282 module_i2c_driver(i2c_hid_driver);
1284 MODULE_DESCRIPTION("HID over I2C core driver");
1285 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1286 MODULE_LICENSE("GPL");