2 * HID over I2C protocol implementation
5 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
6 * Copyright (c) 2012 Red Hat, Inc
8 * This code is partly based on "USB HID support for Linux":
10 * Copyright (c) 1999 Andreas Gal
13 * Copyright (c) 2007-2008 Oliver Neukum
14 * Copyright (c) 2006-2010 Jiri Kosina
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file COPYING in the main directory of this archive for
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/input.h>
25 #include <linux/irq.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
29 #include <linux/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>
39 #include "../hid-ids.h"
42 /* quirks to control the device */
43 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
44 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
45 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
46 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
47 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
48 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7)
52 #define I2C_HID_STARTED 0
53 #define I2C_HID_RESET_PENDING 1
54 #define I2C_HID_READ_PENDING 2
56 #define I2C_HID_PWR_ON 0x00
57 #define I2C_HID_PWR_SLEEP 0x01
61 module_param(debug, bool, 0444);
62 MODULE_PARM_DESC(debug, "print a lot of debug information");
64 #define i2c_hid_dbg(ihid, fmt, arg...) \
67 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
71 __le16 wHIDDescLength;
73 __le16 wReportDescLength;
74 __le16 wReportDescRegister;
75 __le16 wInputRegister;
76 __le16 wMaxInputLength;
77 __le16 wOutputRegister;
78 __le16 wMaxOutputLength;
79 __le16 wCommandRegister;
88 unsigned int registerIndex;
103 #define I2C_HID_CMD(opcode_) \
104 .opcode = opcode_, .length = 4, \
105 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
107 /* fetch HID descriptor */
108 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
109 /* fetch report descriptors */
110 static const struct i2c_hid_cmd hid_report_descr_cmd = {
111 .registerIndex = offsetof(struct i2c_hid_desc,
112 wReportDescRegister),
116 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
118 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
119 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
120 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
121 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
124 * These definitions are not used here, but are defined by the spec.
125 * Keeping them here for documentation purposes.
127 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
128 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
129 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
130 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
133 /* The main device structure */
135 struct i2c_client *client; /* i2c client */
136 struct hid_device *hid; /* pointer to corresponding HID dev */
138 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
139 struct i2c_hid_desc hdesc; /* the HID Descriptor */
141 __le16 wHIDDescRegister; /* location of the i2c
142 * register of the HID
144 unsigned int bufsize; /* i2c buffer size */
145 u8 *inbuf; /* Input buffer */
146 u8 *rawbuf; /* Raw Input buffer */
147 u8 *cmdbuf; /* Command buffer */
148 u8 *argsbuf; /* Command arguments buffer */
150 unsigned long flags; /* device flags */
151 unsigned long quirks; /* Various quirks */
153 wait_queue_head_t wait; /* For waiting the interrupt */
155 bool irq_wake_enabled;
156 struct mutex reset_lock;
158 struct i2chid_ops *ops;
161 static const struct i2c_hid_quirks {
165 } i2c_hid_quirks[] = {
166 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
167 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
168 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
169 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
170 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
171 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
172 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
173 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
174 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
175 I2C_HID_QUIRK_RESET_ON_RESUME },
176 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
177 I2C_HID_QUIRK_RESET_ON_RESUME },
178 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
179 I2C_HID_QUIRK_BAD_INPUT_SIZE },
181 * Sending the wakeup after reset actually break ELAN touchscreen controller
183 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
184 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
185 I2C_HID_QUIRK_BOGUS_IRQ },
190 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
191 * @idVendor: the 16-bit vendor ID
192 * @idProduct: the 16-bit product ID
194 * Returns: a u32 quirks value.
196 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
201 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
202 if (i2c_hid_quirks[n].idVendor == idVendor &&
203 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
204 i2c_hid_quirks[n].idProduct == idProduct))
205 quirks = i2c_hid_quirks[n].quirks;
210 static int __i2c_hid_command(struct i2c_client *client,
211 const struct i2c_hid_cmd *command, u8 reportID,
212 u8 reportType, u8 *args, int args_len,
213 unsigned char *buf_recv, int data_len)
215 struct i2c_hid *ihid = i2c_get_clientdata(client);
216 union command *cmd = (union command *)ihid->cmdbuf;
218 struct i2c_msg msg[2];
221 int length = command->length;
222 bool wait = command->wait;
223 unsigned int registerIndex = command->registerIndex;
225 /* special case for hid_descr_cmd */
226 if (command == &hid_descr_cmd) {
227 cmd->c.reg = ihid->wHIDDescRegister;
229 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
230 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
234 cmd->c.opcode = command->opcode;
235 cmd->c.reportTypeID = reportID | reportType << 4;
238 memcpy(cmd->data + length, args, args_len);
241 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
243 msg[0].addr = client->addr;
244 msg[0].flags = client->flags & I2C_M_TEN;
246 msg[0].buf = cmd->data;
248 msg[1].addr = client->addr;
249 msg[1].flags = client->flags & I2C_M_TEN;
250 msg[1].flags |= I2C_M_RD;
251 msg[1].len = data_len;
252 msg[1].buf = buf_recv;
254 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
258 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
260 ret = i2c_transfer(client->adapter, msg, msg_num);
263 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
266 return ret < 0 ? ret : -EIO;
270 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
273 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
274 if (!wait_event_timeout(ihid->wait,
275 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
276 msecs_to_jiffies(5000)))
278 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
284 static int i2c_hid_command(struct i2c_client *client,
285 const struct i2c_hid_cmd *command,
286 unsigned char *buf_recv, int data_len)
288 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
292 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
293 u8 reportID, unsigned char *buf_recv, int data_len)
295 struct i2c_hid *ihid = i2c_get_clientdata(client);
299 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
301 i2c_hid_dbg(ihid, "%s\n", __func__);
303 if (reportID >= 0x0F) {
304 args[args_len++] = reportID;
308 args[args_len++] = readRegister & 0xFF;
309 args[args_len++] = readRegister >> 8;
311 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
312 reportType, args, args_len, buf_recv, data_len);
314 dev_err(&client->dev,
315 "failed to retrieve report from device.\n");
323 * i2c_hid_set_or_send_report: forward an incoming report to the device
324 * @client: the i2c_client of the device
325 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
326 * @reportID: the report ID
327 * @buf: the actual data to transfer, without the report ID
328 * @data_len: size of buf
329 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
331 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
332 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
334 struct i2c_hid *ihid = i2c_get_clientdata(client);
335 u8 *args = ihid->argsbuf;
336 const struct i2c_hid_cmd *hidcmd;
338 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
339 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
340 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
345 i2c_hid_dbg(ihid, "%s\n", __func__);
347 if (data_len > ihid->bufsize)
350 size = 2 /* size */ +
351 (reportID ? 1 : 0) /* reportID */ +
353 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
354 2 /* dataRegister */ +
357 if (!use_data && maxOutputLength == 0)
360 if (reportID >= 0x0F) {
361 args[index++] = reportID;
366 * use the data register for feature reports or if the device does not
367 * support the output register
370 args[index++] = dataRegister & 0xFF;
371 args[index++] = dataRegister >> 8;
372 hidcmd = &hid_set_report_cmd;
374 args[index++] = outputRegister & 0xFF;
375 args[index++] = outputRegister >> 8;
376 hidcmd = &hid_no_cmd;
379 args[index++] = size & 0xFF;
380 args[index++] = size >> 8;
383 args[index++] = reportID;
385 memcpy(&args[index], buf, data_len);
387 ret = __i2c_hid_command(client, hidcmd, reportID,
388 reportType, args, args_len, NULL, 0);
390 dev_err(&client->dev, "failed to set a report to device.\n");
397 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
399 struct i2c_hid *ihid = i2c_get_clientdata(client);
402 i2c_hid_dbg(ihid, "%s\n", __func__);
405 * Some devices require to send a command to wakeup before power on.
406 * The call will get a return value (EREMOTEIO) but device will be
407 * triggered and activated. After that, it goes like a normal device.
409 if (power_state == I2C_HID_PWR_ON &&
410 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
411 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
413 /* Device was already activated */
418 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
419 0, NULL, 0, NULL, 0);
422 dev_err(&client->dev, "failed to change power setting.\n");
427 * The HID over I2C specification states that if a DEVICE needs time
428 * after the PWR_ON request, it should utilise CLOCK stretching.
429 * However, it has been observered that the Windows driver provides a
430 * 1ms sleep between the PWR_ON and RESET requests.
431 * According to Goodix Windows even waits 60 ms after (other?)
432 * PWR_ON requests. Testing has confirmed that several devices
433 * will not work properly without a delay after a PWR_ON request.
435 if (!ret && power_state == I2C_HID_PWR_ON)
441 static int i2c_hid_hwreset(struct i2c_client *client)
443 struct i2c_hid *ihid = i2c_get_clientdata(client);
446 i2c_hid_dbg(ihid, "%s\n", __func__);
449 * This prevents sending feature reports while the device is
450 * being reset. Otherwise we may lose the reset complete
453 mutex_lock(&ihid->reset_lock);
455 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
459 i2c_hid_dbg(ihid, "resetting...\n");
461 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
463 dev_err(&client->dev, "failed to reset device.\n");
464 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
468 /* At least some SIS devices need this after reset */
469 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
470 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
473 mutex_unlock(&ihid->reset_lock);
477 static void i2c_hid_get_input(struct i2c_hid *ihid)
481 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
483 if (size > ihid->bufsize)
484 size = ihid->bufsize;
486 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
491 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
492 __func__, ret, size);
496 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
499 /* host or device initiated RESET completed */
500 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
501 wake_up(&ihid->wait);
505 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
506 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
507 "there's no data\n", __func__);
511 if ((ret_size > size) || (ret_size < 2)) {
512 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
513 ihid->inbuf[0] = size & 0xff;
514 ihid->inbuf[1] = size >> 8;
517 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
518 __func__, size, ret_size);
523 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
525 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
526 pm_wakeup_event(&ihid->client->dev, 0);
528 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
535 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
537 struct i2c_hid *ihid = dev_id;
539 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
542 i2c_hid_get_input(ihid);
547 static int i2c_hid_get_report_length(struct hid_report *report)
549 return ((report->size - 1) >> 3) + 1 +
550 report->device->report_enum[report->type].numbered + 2;
554 * Traverse the supplied list of reports and find the longest
556 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
559 struct hid_report *report;
562 /* We should not rely on wMaxInputLength, as some devices may set it to
564 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
565 size = i2c_hid_get_report_length(report);
571 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
575 kfree(ihid->argsbuf);
580 ihid->argsbuf = NULL;
584 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
586 /* the worst case is computed from the set_report command with a
587 * reportID > 15 and the maximum report length */
588 int args_len = sizeof(__u8) + /* ReportID */
589 sizeof(__u8) + /* optional ReportID byte */
590 sizeof(__u16) + /* data register */
591 sizeof(__u16) + /* size of the report */
592 report_size; /* report */
594 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
595 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
596 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
597 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
599 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
600 i2c_hid_free_buffers(ihid);
604 ihid->bufsize = report_size;
609 static int i2c_hid_get_raw_report(struct hid_device *hid,
610 unsigned char report_number, __u8 *buf, size_t count,
611 unsigned char report_type)
613 struct i2c_client *client = hid->driver_data;
614 struct i2c_hid *ihid = i2c_get_clientdata(client);
615 size_t ret_count, ask_count;
618 if (report_type == HID_OUTPUT_REPORT)
621 /* +2 bytes to include the size of the reply in the query buffer */
622 ask_count = min(count + 2, (size_t)ihid->bufsize);
624 ret = i2c_hid_get_report(client,
625 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
626 report_number, ihid->rawbuf, ask_count);
631 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
636 ret_count = min(ret_count, ask_count);
638 /* The query buffer contains the size, dropping it in the reply */
639 count = min(count, ret_count - 2);
640 memcpy(buf, ihid->rawbuf + 2, count);
645 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
646 size_t count, unsigned char report_type, bool use_data)
648 struct i2c_client *client = hid->driver_data;
649 struct i2c_hid *ihid = i2c_get_clientdata(client);
650 int report_id = buf[0];
653 if (report_type == HID_INPUT_REPORT)
656 mutex_lock(&ihid->reset_lock);
663 ret = i2c_hid_set_or_send_report(client,
664 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
665 report_id, buf, count, use_data);
667 if (report_id && ret >= 0)
668 ret++; /* add report_id to the number of transfered bytes */
670 mutex_unlock(&ihid->reset_lock);
675 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
678 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
682 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
683 __u8 *buf, size_t len, unsigned char rtype,
687 case HID_REQ_GET_REPORT:
688 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
689 case HID_REQ_SET_REPORT:
690 if (buf[0] != reportnum)
692 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
698 static int i2c_hid_parse(struct hid_device *hid)
700 struct i2c_client *client = hid->driver_data;
701 struct i2c_hid *ihid = i2c_get_clientdata(client);
702 struct i2c_hid_desc *hdesc = &ihid->hdesc;
709 i2c_hid_dbg(ihid, "entering %s\n", __func__);
711 rsize = le16_to_cpu(hdesc->wReportDescLength);
712 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
713 dbg_hid("weird size of report descriptor (%u)\n", rsize);
718 ret = i2c_hid_hwreset(client);
721 } while (tries-- > 0 && ret);
726 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
730 rdesc = use_override;
731 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
733 rdesc = kzalloc(rsize, GFP_KERNEL);
736 dbg_hid("couldn't allocate rdesc memory\n");
740 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
742 ret = i2c_hid_command(client, &hid_report_descr_cmd,
745 hid_err(hid, "reading report descriptor failed\n");
751 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
753 ret = hid_parse_report(hid, rdesc, rsize);
758 dbg_hid("parsing report descriptor failed\n");
765 static int i2c_hid_start(struct hid_device *hid)
767 struct i2c_client *client = hid->driver_data;
768 struct i2c_hid *ihid = i2c_get_clientdata(client);
770 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
772 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
773 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
774 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
776 if (bufsize > ihid->bufsize) {
777 disable_irq(client->irq);
778 i2c_hid_free_buffers(ihid);
780 ret = i2c_hid_alloc_buffers(ihid, bufsize);
781 enable_irq(client->irq);
790 static void i2c_hid_stop(struct hid_device *hid)
795 static int i2c_hid_open(struct hid_device *hid)
797 struct i2c_client *client = hid->driver_data;
798 struct i2c_hid *ihid = i2c_get_clientdata(client);
800 set_bit(I2C_HID_STARTED, &ihid->flags);
804 static void i2c_hid_close(struct hid_device *hid)
806 struct i2c_client *client = hid->driver_data;
807 struct i2c_hid *ihid = i2c_get_clientdata(client);
809 clear_bit(I2C_HID_STARTED, &ihid->flags);
812 struct hid_ll_driver i2c_hid_ll_driver = {
813 .parse = i2c_hid_parse,
814 .start = i2c_hid_start,
815 .stop = i2c_hid_stop,
816 .open = i2c_hid_open,
817 .close = i2c_hid_close,
818 .output_report = i2c_hid_output_report,
819 .raw_request = i2c_hid_raw_request,
821 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
823 static int i2c_hid_init_irq(struct i2c_client *client)
825 struct i2c_hid *ihid = i2c_get_clientdata(client);
826 unsigned long irqflags = 0;
829 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
831 if (!irq_get_trigger_type(client->irq))
832 irqflags = IRQF_TRIGGER_LOW;
834 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
835 irqflags | IRQF_ONESHOT, client->name, ihid);
837 dev_warn(&client->dev,
838 "Could not register for %s interrupt, irq = %d,"
840 client->name, client->irq, ret);
848 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
850 struct i2c_client *client = ihid->client;
851 struct i2c_hid_desc *hdesc = &ihid->hdesc;
855 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
856 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
857 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
859 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
861 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
862 ret = i2c_hid_command(client, &hid_descr_cmd,
864 sizeof(struct i2c_hid_desc));
866 dev_err(&client->dev, "hid_descr_cmd failed\n");
871 /* Validate the length of HID descriptor, the 4 first bytes:
872 * bytes 0-1 -> length
873 * bytes 2-3 -> bcdVersion (has to be 1.00) */
874 /* check bcdVersion == 1.0 */
875 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
876 dev_err(&client->dev,
877 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
878 le16_to_cpu(hdesc->bcdVersion));
882 /* Descriptor length should be 30 bytes as per the specification */
883 dsize = le16_to_cpu(hdesc->wHIDDescLength);
884 if (dsize != sizeof(struct i2c_hid_desc)) {
885 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
889 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
893 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
895 if (!ihid->ops->power_up)
898 return ihid->ops->power_up(ihid->ops);
901 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
903 if (!ihid->ops->power_down)
906 ihid->ops->power_down(ihid->ops);
909 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
911 if (!ihid->ops->shutdown_tail)
914 ihid->ops->shutdown_tail(ihid->ops);
917 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
918 u16 hid_descriptor_address, u32 quirks)
921 struct i2c_hid *ihid;
922 struct hid_device *hid;
924 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
927 dev_err(&client->dev,
928 "HID over i2c has not been provided an Int IRQ\n");
932 if (client->irq < 0) {
933 if (client->irq != -EPROBE_DEFER)
934 dev_err(&client->dev,
935 "HID over i2c doesn't have a valid IRQ\n");
939 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
945 ret = i2c_hid_core_power_up(ihid);
949 i2c_set_clientdata(client, ihid);
951 ihid->client = client;
953 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
955 init_waitqueue_head(&ihid->wait);
956 mutex_init(&ihid->reset_lock);
958 /* we need to allocate the command buffer without knowing the maximum
959 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
960 * real computation later. */
961 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
965 device_enable_async_suspend(&client->dev);
967 /* Make sure there is something at this address */
968 ret = i2c_smbus_read_byte(client);
970 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
975 ret = i2c_hid_fetch_hid_descriptor(ihid);
977 dev_err(&client->dev,
978 "Failed to fetch the HID Descriptor\n");
982 ret = i2c_hid_init_irq(client);
986 hid = hid_allocate_device();
994 hid->driver_data = client;
995 hid->ll_driver = &i2c_hid_ll_driver;
996 hid->dev.parent = &client->dev;
998 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
999 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1000 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1002 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1003 client->name, (u16)hid->vendor, (u16)hid->product);
1004 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1006 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1008 ret = hid_add_device(hid);
1011 hid_err(client, "can't add hid device: %d\n", ret);
1015 hid->quirks |= quirks;
1020 hid_destroy_device(hid);
1023 free_irq(client->irq, ihid);
1026 i2c_hid_core_power_down(ihid);
1027 i2c_hid_free_buffers(ihid);
1030 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1032 int i2c_hid_core_remove(struct i2c_client *client)
1034 struct i2c_hid *ihid = i2c_get_clientdata(client);
1035 struct hid_device *hid;
1038 hid_destroy_device(hid);
1040 free_irq(client->irq, ihid);
1043 i2c_hid_free_buffers(ihid);
1045 i2c_hid_core_power_down(ihid);
1049 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1051 void i2c_hid_core_shutdown(struct i2c_client *client)
1053 struct i2c_hid *ihid = i2c_get_clientdata(client);
1055 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1056 free_irq(client->irq, ihid);
1058 i2c_hid_core_shutdown_tail(ihid);
1060 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1062 #ifdef CONFIG_PM_SLEEP
1063 static int i2c_hid_core_suspend(struct device *dev)
1065 struct i2c_client *client = to_i2c_client(dev);
1066 struct i2c_hid *ihid = i2c_get_clientdata(client);
1067 struct hid_device *hid = ihid->hid;
1071 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1075 /* Save some power */
1076 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1078 disable_irq(client->irq);
1080 if (device_may_wakeup(&client->dev)) {
1081 wake_status = enable_irq_wake(client->irq);
1083 ihid->irq_wake_enabled = true;
1085 hid_warn(hid, "Failed to enable irq wake: %d\n",
1088 i2c_hid_core_power_down(ihid);
1094 static int i2c_hid_core_resume(struct device *dev)
1097 struct i2c_client *client = to_i2c_client(dev);
1098 struct i2c_hid *ihid = i2c_get_clientdata(client);
1099 struct hid_device *hid = ihid->hid;
1102 if (!device_may_wakeup(&client->dev)) {
1103 i2c_hid_core_power_up(ihid);
1104 } else if (ihid->irq_wake_enabled) {
1105 wake_status = disable_irq_wake(client->irq);
1107 ihid->irq_wake_enabled = false;
1109 hid_warn(hid, "Failed to disable irq wake: %d\n",
1113 enable_irq(client->irq);
1115 /* Instead of resetting device, simply powers the device on. This
1116 * solves "incomplete reports" on Raydium devices 2386:3118 and
1117 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1118 * data after a suspend/resume.
1120 * However some ALPS touchpads generate IRQ storm without reset, so
1121 * let's still reset them here.
1123 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1124 ret = i2c_hid_hwreset(client);
1126 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1131 return hid_driver_reset_resume(hid);
1135 const struct dev_pm_ops i2c_hid_core_pm = {
1136 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1138 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1140 MODULE_DESCRIPTION("HID over I2C core driver");
1142 MODULE_LICENSE("GPL");