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)
53 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7)
57 #define I2C_HID_STARTED 0
58 #define I2C_HID_RESET_PENDING 1
59 #define I2C_HID_READ_PENDING 2
61 #define I2C_HID_PWR_ON 0x00
62 #define I2C_HID_PWR_SLEEP 0x01
66 module_param(debug, bool, 0444);
67 MODULE_PARM_DESC(debug, "print a lot of debug information");
69 #define i2c_hid_dbg(ihid, fmt, arg...) \
72 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
76 __le16 wHIDDescLength;
78 __le16 wReportDescLength;
79 __le16 wReportDescRegister;
80 __le16 wInputRegister;
81 __le16 wMaxInputLength;
82 __le16 wOutputRegister;
83 __le16 wMaxOutputLength;
84 __le16 wCommandRegister;
93 unsigned int registerIndex;
108 #define I2C_HID_CMD(opcode_) \
109 .opcode = opcode_, .length = 4, \
110 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
112 /* fetch HID descriptor */
113 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
114 /* fetch report descriptors */
115 static const struct i2c_hid_cmd hid_report_descr_cmd = {
116 .registerIndex = offsetof(struct i2c_hid_desc,
117 wReportDescRegister),
121 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
123 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
124 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
125 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
126 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
129 * These definitions are not used here, but are defined by the spec.
130 * Keeping them here for documentation purposes.
132 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
133 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
134 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
135 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
138 /* The main device structure */
140 struct i2c_client *client; /* i2c client */
141 struct hid_device *hid; /* pointer to corresponding HID dev */
143 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
144 struct i2c_hid_desc hdesc; /* the HID Descriptor */
146 __le16 wHIDDescRegister; /* location of the i2c
147 * register of the HID
149 unsigned int bufsize; /* i2c buffer size */
150 u8 *inbuf; /* Input buffer */
151 u8 *rawbuf; /* Raw Input buffer */
152 u8 *cmdbuf; /* Command buffer */
153 u8 *argsbuf; /* Command arguments buffer */
155 unsigned long flags; /* device flags */
156 unsigned long quirks; /* Various quirks */
158 wait_queue_head_t wait; /* For waiting the interrupt */
160 struct i2c_hid_platform_data pdata;
162 bool irq_wake_enabled;
163 struct mutex reset_lock;
166 static const struct i2c_hid_quirks {
170 } i2c_hid_quirks[] = {
171 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
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_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
176 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
177 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
178 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
179 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
180 I2C_HID_QUIRK_RESET_ON_RESUME },
181 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
182 I2C_HID_QUIRK_RESET_ON_RESUME },
183 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
184 I2C_HID_QUIRK_BAD_INPUT_SIZE },
186 * Sending the wakeup after reset actually break ELAN touchscreen controller
188 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
189 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
190 I2C_HID_QUIRK_BOGUS_IRQ },
195 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
196 * @idVendor: the 16-bit vendor ID
197 * @idProduct: the 16-bit product ID
199 * Returns: a u32 quirks value.
201 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
206 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
207 if (i2c_hid_quirks[n].idVendor == idVendor &&
208 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
209 i2c_hid_quirks[n].idProduct == idProduct))
210 quirks = i2c_hid_quirks[n].quirks;
215 static int __i2c_hid_command(struct i2c_client *client,
216 const struct i2c_hid_cmd *command, u8 reportID,
217 u8 reportType, u8 *args, int args_len,
218 unsigned char *buf_recv, int data_len)
220 struct i2c_hid *ihid = i2c_get_clientdata(client);
221 union command *cmd = (union command *)ihid->cmdbuf;
223 struct i2c_msg msg[2];
226 int length = command->length;
227 bool wait = command->wait;
228 unsigned int registerIndex = command->registerIndex;
230 /* special case for hid_descr_cmd */
231 if (command == &hid_descr_cmd) {
232 cmd->c.reg = ihid->wHIDDescRegister;
234 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
235 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
239 cmd->c.opcode = command->opcode;
240 cmd->c.reportTypeID = reportID | reportType << 4;
243 memcpy(cmd->data + length, args, args_len);
246 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
248 msg[0].addr = client->addr;
249 msg[0].flags = client->flags & I2C_M_TEN;
251 msg[0].buf = cmd->data;
253 msg[1].addr = client->addr;
254 msg[1].flags = client->flags & I2C_M_TEN;
255 msg[1].flags |= I2C_M_RD;
256 msg[1].len = data_len;
257 msg[1].buf = buf_recv;
259 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
263 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
265 ret = i2c_transfer(client->adapter, msg, msg_num);
268 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
271 return ret < 0 ? ret : -EIO;
275 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
278 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
279 if (!wait_event_timeout(ihid->wait,
280 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
281 msecs_to_jiffies(5000)))
283 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
289 static int i2c_hid_command(struct i2c_client *client,
290 const struct i2c_hid_cmd *command,
291 unsigned char *buf_recv, int data_len)
293 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
297 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
298 u8 reportID, unsigned char *buf_recv, int data_len)
300 struct i2c_hid *ihid = i2c_get_clientdata(client);
304 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
306 i2c_hid_dbg(ihid, "%s\n", __func__);
308 if (reportID >= 0x0F) {
309 args[args_len++] = reportID;
313 args[args_len++] = readRegister & 0xFF;
314 args[args_len++] = readRegister >> 8;
316 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
317 reportType, args, args_len, buf_recv, data_len);
319 dev_err(&client->dev,
320 "failed to retrieve report from device.\n");
328 * i2c_hid_set_or_send_report: forward an incoming report to the device
329 * @client: the i2c_client of the device
330 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
331 * @reportID: the report ID
332 * @buf: the actual data to transfer, without the report ID
333 * @data_len: size of buf
334 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
336 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
337 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
339 struct i2c_hid *ihid = i2c_get_clientdata(client);
340 u8 *args = ihid->argsbuf;
341 const struct i2c_hid_cmd *hidcmd;
343 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
344 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
345 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
350 i2c_hid_dbg(ihid, "%s\n", __func__);
352 if (data_len > ihid->bufsize)
355 size = 2 /* size */ +
356 (reportID ? 1 : 0) /* reportID */ +
358 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
359 2 /* dataRegister */ +
362 if (!use_data && maxOutputLength == 0)
365 if (reportID >= 0x0F) {
366 args[index++] = reportID;
371 * use the data register for feature reports or if the device does not
372 * support the output register
375 args[index++] = dataRegister & 0xFF;
376 args[index++] = dataRegister >> 8;
377 hidcmd = &hid_set_report_cmd;
379 args[index++] = outputRegister & 0xFF;
380 args[index++] = outputRegister >> 8;
381 hidcmd = &hid_no_cmd;
384 args[index++] = size & 0xFF;
385 args[index++] = size >> 8;
388 args[index++] = reportID;
390 memcpy(&args[index], buf, data_len);
392 ret = __i2c_hid_command(client, hidcmd, reportID,
393 reportType, args, args_len, NULL, 0);
395 dev_err(&client->dev, "failed to set a report to device.\n");
402 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
404 struct i2c_hid *ihid = i2c_get_clientdata(client);
407 i2c_hid_dbg(ihid, "%s\n", __func__);
410 * Some devices require to send a command to wakeup before power on.
411 * The call will get a return value (EREMOTEIO) but device will be
412 * triggered and activated. After that, it goes like a normal device.
414 if (power_state == I2C_HID_PWR_ON &&
415 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
416 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
418 /* Device was already activated */
423 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
424 0, NULL, 0, NULL, 0);
427 dev_err(&client->dev, "failed to change power setting.\n");
432 * The HID over I2C specification states that if a DEVICE needs time
433 * after the PWR_ON request, it should utilise CLOCK stretching.
434 * However, it has been observered that the Windows driver provides a
435 * 1ms sleep between the PWR_ON and RESET requests.
436 * According to Goodix Windows even waits 60 ms after (other?)
437 * PWR_ON requests. Testing has confirmed that several devices
438 * will not work properly without a delay after a PWR_ON request.
440 if (!ret && power_state == I2C_HID_PWR_ON)
446 static int i2c_hid_hwreset(struct i2c_client *client)
448 struct i2c_hid *ihid = i2c_get_clientdata(client);
451 i2c_hid_dbg(ihid, "%s\n", __func__);
454 * This prevents sending feature reports while the device is
455 * being reset. Otherwise we may lose the reset complete
458 mutex_lock(&ihid->reset_lock);
460 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
464 i2c_hid_dbg(ihid, "resetting...\n");
466 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
468 dev_err(&client->dev, "failed to reset device.\n");
469 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
473 /* At least some SIS devices need this after reset */
474 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
475 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
478 mutex_unlock(&ihid->reset_lock);
482 static void i2c_hid_get_input(struct i2c_hid *ihid)
486 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
488 if (size > ihid->bufsize)
489 size = ihid->bufsize;
491 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
496 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
497 __func__, ret, size);
501 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
504 /* host or device initiated RESET completed */
505 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
506 wake_up(&ihid->wait);
510 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
511 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
512 "there's no data\n", __func__);
516 if ((ret_size > size) || (ret_size < 2)) {
517 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
518 ihid->inbuf[0] = size & 0xff;
519 ihid->inbuf[1] = size >> 8;
522 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
523 __func__, size, ret_size);
528 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
530 if (test_bit(I2C_HID_STARTED, &ihid->flags))
531 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
537 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
539 struct i2c_hid *ihid = dev_id;
541 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
544 i2c_hid_get_input(ihid);
549 static int i2c_hid_get_report_length(struct hid_report *report)
551 return ((report->size - 1) >> 3) + 1 +
552 report->device->report_enum[report->type].numbered + 2;
556 * Traverse the supplied list of reports and find the longest
558 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
561 struct hid_report *report;
564 /* We should not rely on wMaxInputLength, as some devices may set it to
566 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
567 size = i2c_hid_get_report_length(report);
573 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
577 kfree(ihid->argsbuf);
582 ihid->argsbuf = NULL;
586 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
588 /* the worst case is computed from the set_report command with a
589 * reportID > 15 and the maximum report length */
590 int args_len = sizeof(__u8) + /* ReportID */
591 sizeof(__u8) + /* optional ReportID byte */
592 sizeof(__u16) + /* data register */
593 sizeof(__u16) + /* size of the report */
594 report_size; /* report */
596 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
597 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
598 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
599 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
601 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
602 i2c_hid_free_buffers(ihid);
606 ihid->bufsize = report_size;
611 static int i2c_hid_get_raw_report(struct hid_device *hid,
612 unsigned char report_number, __u8 *buf, size_t count,
613 unsigned char report_type)
615 struct i2c_client *client = hid->driver_data;
616 struct i2c_hid *ihid = i2c_get_clientdata(client);
617 size_t ret_count, ask_count;
620 if (report_type == HID_OUTPUT_REPORT)
624 * In case of unnumbered reports the response from the device will
625 * not have the report ID that the upper layers expect, so we need
626 * to stash it the buffer ourselves and adjust the data size.
628 if (!report_number) {
634 /* +2 bytes to include the size of the reply in the query buffer */
635 ask_count = min(count + 2, (size_t)ihid->bufsize);
637 ret = i2c_hid_get_report(client,
638 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
639 report_number, ihid->rawbuf, ask_count);
644 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
649 ret_count = min(ret_count, ask_count);
651 /* The query buffer contains the size, dropping it in the reply */
652 count = min(count, ret_count - 2);
653 memcpy(buf, ihid->rawbuf + 2, count);
661 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
662 size_t count, unsigned char report_type, bool use_data)
664 struct i2c_client *client = hid->driver_data;
665 struct i2c_hid *ihid = i2c_get_clientdata(client);
666 int report_id = buf[0];
669 if (report_type == HID_INPUT_REPORT)
672 mutex_lock(&ihid->reset_lock);
675 * Note that both numbered and unnumbered reports passed here
676 * are supposed to have report ID stored in the 1st byte of the
677 * buffer, so we strip it off unconditionally before passing payload
678 * to i2c_hid_set_or_send_report which takes care of encoding
679 * everything properly.
681 ret = i2c_hid_set_or_send_report(client,
682 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
683 report_id, buf + 1, count - 1, use_data);
686 ret++; /* add report_id to the number of transferred bytes */
688 mutex_unlock(&ihid->reset_lock);
693 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
696 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
700 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
701 __u8 *buf, size_t len, unsigned char rtype,
705 case HID_REQ_GET_REPORT:
706 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
707 case HID_REQ_SET_REPORT:
708 if (buf[0] != reportnum)
710 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
716 static int i2c_hid_parse(struct hid_device *hid)
718 struct i2c_client *client = hid->driver_data;
719 struct i2c_hid *ihid = i2c_get_clientdata(client);
720 struct i2c_hid_desc *hdesc = &ihid->hdesc;
727 i2c_hid_dbg(ihid, "entering %s\n", __func__);
729 rsize = le16_to_cpu(hdesc->wReportDescLength);
730 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
731 dbg_hid("weird size of report descriptor (%u)\n", rsize);
736 ret = i2c_hid_hwreset(client);
739 } while (tries-- > 0 && ret);
744 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
748 rdesc = use_override;
749 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
751 rdesc = kzalloc(rsize, GFP_KERNEL);
754 dbg_hid("couldn't allocate rdesc memory\n");
758 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
760 ret = i2c_hid_command(client, &hid_report_descr_cmd,
763 hid_err(hid, "reading report descriptor failed\n");
769 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
771 ret = hid_parse_report(hid, rdesc, rsize);
776 dbg_hid("parsing report descriptor failed\n");
783 static int i2c_hid_start(struct hid_device *hid)
785 struct i2c_client *client = hid->driver_data;
786 struct i2c_hid *ihid = i2c_get_clientdata(client);
788 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
790 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
791 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
792 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
794 if (bufsize > ihid->bufsize) {
795 disable_irq(client->irq);
796 i2c_hid_free_buffers(ihid);
798 ret = i2c_hid_alloc_buffers(ihid, bufsize);
799 enable_irq(client->irq);
808 static void i2c_hid_stop(struct hid_device *hid)
813 static int i2c_hid_open(struct hid_device *hid)
815 struct i2c_client *client = hid->driver_data;
816 struct i2c_hid *ihid = i2c_get_clientdata(client);
818 set_bit(I2C_HID_STARTED, &ihid->flags);
822 static void i2c_hid_close(struct hid_device *hid)
824 struct i2c_client *client = hid->driver_data;
825 struct i2c_hid *ihid = i2c_get_clientdata(client);
827 clear_bit(I2C_HID_STARTED, &ihid->flags);
830 struct hid_ll_driver i2c_hid_ll_driver = {
831 .parse = i2c_hid_parse,
832 .start = i2c_hid_start,
833 .stop = i2c_hid_stop,
834 .open = i2c_hid_open,
835 .close = i2c_hid_close,
836 .output_report = i2c_hid_output_report,
837 .raw_request = i2c_hid_raw_request,
839 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
841 static int i2c_hid_init_irq(struct i2c_client *client)
843 struct i2c_hid *ihid = i2c_get_clientdata(client);
844 unsigned long irqflags = 0;
847 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
849 if (!irq_get_trigger_type(client->irq))
850 irqflags = IRQF_TRIGGER_LOW;
852 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
853 irqflags | IRQF_ONESHOT, client->name, ihid);
855 dev_warn(&client->dev,
856 "Could not register for %s interrupt, irq = %d,"
858 client->name, client->irq, ret);
866 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
868 struct i2c_client *client = ihid->client;
869 struct i2c_hid_desc *hdesc = &ihid->hdesc;
873 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
874 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
875 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
877 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
879 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
880 ret = i2c_hid_command(client, &hid_descr_cmd,
882 sizeof(struct i2c_hid_desc));
884 dev_err(&client->dev, "hid_descr_cmd failed\n");
889 /* Validate the length of HID descriptor, the 4 first bytes:
890 * bytes 0-1 -> length
891 * bytes 2-3 -> bcdVersion (has to be 1.00) */
892 /* check bcdVersion == 1.0 */
893 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
894 dev_err(&client->dev,
895 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
896 le16_to_cpu(hdesc->bcdVersion));
900 /* Descriptor length should be 30 bytes as per the specification */
901 dsize = le16_to_cpu(hdesc->wHIDDescLength);
902 if (dsize != sizeof(struct i2c_hid_desc)) {
903 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
907 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
912 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
914 * The CHPN0001 ACPI device, which is used to describe the Chipone
915 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
921 static int i2c_hid_acpi_pdata(struct i2c_client *client,
922 struct i2c_hid_platform_data *pdata)
924 static guid_t i2c_hid_guid =
925 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
926 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
927 union acpi_object *obj;
928 struct acpi_device *adev;
931 handle = ACPI_HANDLE(&client->dev);
932 if (!handle || acpi_bus_get_device(handle, &adev)) {
933 dev_err(&client->dev, "Error could not get ACPI device\n");
937 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
940 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
943 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
947 pdata->hid_descriptor_address = obj->integer.value;
953 static void i2c_hid_acpi_fix_up_power(struct device *dev)
955 struct acpi_device *adev;
957 adev = ACPI_COMPANION(dev);
959 acpi_device_fix_up_power(adev);
962 static void i2c_hid_acpi_enable_wakeup(struct device *dev)
964 if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) {
965 device_set_wakeup_capable(dev, true);
966 device_set_wakeup_enable(dev, false);
970 static void i2c_hid_acpi_shutdown(struct device *dev)
972 acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D3_COLD);
975 static const struct acpi_device_id i2c_hid_acpi_match[] = {
980 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
982 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
983 struct i2c_hid_platform_data *pdata)
988 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
990 static inline void i2c_hid_acpi_enable_wakeup(struct device *dev) {}
992 static inline void i2c_hid_acpi_shutdown(struct device *dev) {}
996 static int i2c_hid_of_probe(struct i2c_client *client,
997 struct i2c_hid_platform_data *pdata)
999 struct device *dev = &client->dev;
1003 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
1005 dev_err(&client->dev, "HID register address not provided\n");
1009 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
1013 pdata->hid_descriptor_address = val;
1018 static const struct of_device_id i2c_hid_of_match[] = {
1019 { .compatible = "hid-over-i2c" },
1022 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1024 static inline int i2c_hid_of_probe(struct i2c_client *client,
1025 struct i2c_hid_platform_data *pdata)
1031 static void i2c_hid_fwnode_probe(struct i2c_client *client,
1032 struct i2c_hid_platform_data *pdata)
1036 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
1038 pdata->post_power_delay_ms = val;
1041 static int i2c_hid_probe(struct i2c_client *client,
1042 const struct i2c_device_id *dev_id)
1045 struct i2c_hid *ihid;
1046 struct hid_device *hid;
1048 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1050 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1053 dev_err(&client->dev,
1054 "HID over i2c has not been provided an Int IRQ\n");
1058 if (client->irq < 0) {
1059 if (client->irq != -EPROBE_DEFER)
1060 dev_err(&client->dev,
1061 "HID over i2c doesn't have a valid IRQ\n");
1065 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1069 if (client->dev.of_node) {
1070 ret = i2c_hid_of_probe(client, &ihid->pdata);
1073 } else if (!platform_data) {
1074 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1078 ihid->pdata = *platform_data;
1081 /* Parse platform agnostic common properties from ACPI / device tree */
1082 i2c_hid_fwnode_probe(client, &ihid->pdata);
1084 ihid->pdata.supplies[0].supply = "vdd";
1085 ihid->pdata.supplies[1].supply = "vddl";
1087 ret = devm_regulator_bulk_get(&client->dev,
1088 ARRAY_SIZE(ihid->pdata.supplies),
1089 ihid->pdata.supplies);
1093 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1094 ihid->pdata.supplies);
1098 if (ihid->pdata.post_power_delay_ms)
1099 msleep(ihid->pdata.post_power_delay_ms);
1101 i2c_set_clientdata(client, ihid);
1103 ihid->client = client;
1105 hidRegister = ihid->pdata.hid_descriptor_address;
1106 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1108 init_waitqueue_head(&ihid->wait);
1109 mutex_init(&ihid->reset_lock);
1111 /* we need to allocate the command buffer without knowing the maximum
1112 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1113 * real computation later. */
1114 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1118 i2c_hid_acpi_fix_up_power(&client->dev);
1120 i2c_hid_acpi_enable_wakeup(&client->dev);
1122 device_enable_async_suspend(&client->dev);
1124 /* Make sure there is something at this address */
1125 ret = i2c_smbus_read_byte(client);
1127 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1132 ret = i2c_hid_fetch_hid_descriptor(ihid);
1136 ret = i2c_hid_init_irq(client);
1140 hid = hid_allocate_device();
1148 hid->driver_data = client;
1149 hid->ll_driver = &i2c_hid_ll_driver;
1150 hid->dev.parent = &client->dev;
1152 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1153 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1154 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1156 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1157 client->name, (u16)hid->vendor, (u16)hid->product);
1158 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1160 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1162 ret = hid_add_device(hid);
1165 hid_err(client, "can't add hid device: %d\n", ret);
1172 hid_destroy_device(hid);
1175 free_irq(client->irq, ihid);
1178 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1179 ihid->pdata.supplies);
1180 i2c_hid_free_buffers(ihid);
1184 static int i2c_hid_remove(struct i2c_client *client)
1186 struct i2c_hid *ihid = i2c_get_clientdata(client);
1187 struct hid_device *hid;
1190 hid_destroy_device(hid);
1192 free_irq(client->irq, ihid);
1195 i2c_hid_free_buffers(ihid);
1197 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1198 ihid->pdata.supplies);
1203 static void i2c_hid_shutdown(struct i2c_client *client)
1205 struct i2c_hid *ihid = i2c_get_clientdata(client);
1207 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1208 free_irq(client->irq, ihid);
1210 i2c_hid_acpi_shutdown(&client->dev);
1213 #ifdef CONFIG_PM_SLEEP
1214 static int i2c_hid_suspend(struct device *dev)
1216 struct i2c_client *client = to_i2c_client(dev);
1217 struct i2c_hid *ihid = i2c_get_clientdata(client);
1218 struct hid_device *hid = ihid->hid;
1222 if (hid->driver && hid->driver->suspend) {
1223 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1228 /* Save some power */
1229 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1231 disable_irq(client->irq);
1233 if (device_may_wakeup(&client->dev)) {
1234 wake_status = enable_irq_wake(client->irq);
1236 ihid->irq_wake_enabled = true;
1238 hid_warn(hid, "Failed to enable irq wake: %d\n",
1241 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1242 ihid->pdata.supplies);
1248 static int i2c_hid_resume(struct device *dev)
1251 struct i2c_client *client = to_i2c_client(dev);
1252 struct i2c_hid *ihid = i2c_get_clientdata(client);
1253 struct hid_device *hid = ihid->hid;
1256 if (!device_may_wakeup(&client->dev)) {
1257 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1258 ihid->pdata.supplies);
1260 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1262 if (ihid->pdata.post_power_delay_ms)
1263 msleep(ihid->pdata.post_power_delay_ms);
1264 } else if (ihid->irq_wake_enabled) {
1265 wake_status = disable_irq_wake(client->irq);
1267 ihid->irq_wake_enabled = false;
1269 hid_warn(hid, "Failed to disable irq wake: %d\n",
1273 enable_irq(client->irq);
1275 /* Instead of resetting device, simply powers the device on. This
1276 * solves "incomplete reports" on Raydium devices 2386:3118 and
1277 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1278 * data after a suspend/resume.
1280 * However some ALPS touchpads generate IRQ storm without reset, so
1281 * let's still reset them here.
1283 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1284 ret = i2c_hid_hwreset(client);
1286 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1291 if (hid->driver && hid->driver->reset_resume) {
1292 ret = hid->driver->reset_resume(hid);
1300 static const struct dev_pm_ops i2c_hid_pm = {
1301 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1304 static const struct i2c_device_id i2c_hid_id_table[] = {
1306 { "hid-over-i2c", 0 },
1309 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1312 static struct i2c_driver i2c_hid_driver = {
1316 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1317 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1318 .of_match_table = of_match_ptr(i2c_hid_of_match),
1321 .probe = i2c_hid_probe,
1322 .remove = i2c_hid_remove,
1323 .shutdown = i2c_hid_shutdown,
1324 .id_table = i2c_hid_id_table,
1327 module_i2c_driver(i2c_hid_driver);
1329 MODULE_DESCRIPTION("HID over I2C core driver");
1330 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1331 MODULE_LICENSE("GPL");