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>
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;
104 #define I2C_HID_CMD(opcode_) \
105 .opcode = opcode_, .length = 4, \
106 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
108 /* fetch HID descriptor */
109 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
110 /* fetch report descriptors */
111 static const struct i2c_hid_cmd hid_report_descr_cmd = {
112 .registerIndex = offsetof(struct i2c_hid_desc,
113 wReportDescRegister),
117 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
119 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
120 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
121 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
122 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
125 * These definitions are not used here, but are defined by the spec.
126 * Keeping them here for documentation purposes.
128 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
129 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
130 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
131 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
134 /* The main device structure */
136 struct i2c_client *client; /* i2c client */
137 struct hid_device *hid; /* pointer to corresponding HID dev */
139 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
140 struct i2c_hid_desc hdesc; /* the HID Descriptor */
142 __le16 wHIDDescRegister; /* location of the i2c
143 * register of the HID
145 unsigned int bufsize; /* i2c buffer size */
146 u8 *inbuf; /* Input buffer */
147 u8 *rawbuf; /* Raw Input buffer */
148 u8 *cmdbuf; /* Command buffer */
149 u8 *argsbuf; /* Command arguments buffer */
151 unsigned long flags; /* device flags */
152 unsigned long quirks; /* Various quirks */
154 wait_queue_head_t wait; /* For waiting the interrupt */
156 bool irq_wake_enabled;
157 struct mutex reset_lock;
159 struct i2chid_ops *ops;
162 static const struct i2c_hid_quirks {
166 } i2c_hid_quirks[] = {
167 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
168 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
169 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
170 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
171 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
172 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
173 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
174 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
175 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
176 I2C_HID_QUIRK_RESET_ON_RESUME },
177 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
178 I2C_HID_QUIRK_RESET_ON_RESUME },
179 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
180 I2C_HID_QUIRK_BAD_INPUT_SIZE },
182 * Sending the wakeup after reset actually break ELAN touchscreen controller
184 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
185 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
186 I2C_HID_QUIRK_BOGUS_IRQ },
191 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
192 * @idVendor: the 16-bit vendor ID
193 * @idProduct: the 16-bit product ID
195 * Returns: a u32 quirks value.
197 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
202 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
203 if (i2c_hid_quirks[n].idVendor == idVendor &&
204 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
205 i2c_hid_quirks[n].idProduct == idProduct))
206 quirks = i2c_hid_quirks[n].quirks;
211 static int __i2c_hid_command(struct i2c_client *client,
212 const struct i2c_hid_cmd *command, u8 reportID,
213 u8 reportType, u8 *args, int args_len,
214 unsigned char *buf_recv, int data_len)
216 struct i2c_hid *ihid = i2c_get_clientdata(client);
217 union command *cmd = (union command *)ihid->cmdbuf;
219 struct i2c_msg msg[2];
222 int length = command->length;
223 bool wait = command->wait;
224 unsigned int registerIndex = command->registerIndex;
226 /* special case for hid_descr_cmd */
227 if (command == &hid_descr_cmd) {
228 cmd->c.reg = ihid->wHIDDescRegister;
230 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
231 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
235 cmd->c.opcode = command->opcode;
236 if (reportID < 0x0F) {
237 cmd->c.reportTypeID = reportType << 4 | reportID;
239 cmd->c.reportTypeID = reportType << 4 | 0x0F;
240 cmd->c.reportID = reportID;
245 memcpy(cmd->data + length, args, args_len);
248 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
250 msg[0].addr = client->addr;
251 msg[0].flags = client->flags & I2C_M_TEN;
253 msg[0].buf = cmd->data;
255 msg[1].addr = client->addr;
256 msg[1].flags = client->flags & I2C_M_TEN;
257 msg[1].flags |= I2C_M_RD;
258 msg[1].len = data_len;
259 msg[1].buf = buf_recv;
261 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
265 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
267 ret = i2c_transfer(client->adapter, msg, msg_num);
270 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
273 return ret < 0 ? ret : -EIO;
277 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
280 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
281 if (!wait_event_timeout(ihid->wait,
282 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
283 msecs_to_jiffies(5000)))
285 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
291 static int i2c_hid_command(struct i2c_client *client,
292 const struct i2c_hid_cmd *command,
293 unsigned char *buf_recv, int data_len)
295 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
299 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
300 u8 reportID, unsigned char *buf_recv, int data_len)
302 struct i2c_hid *ihid = i2c_get_clientdata(client);
306 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
308 i2c_hid_dbg(ihid, "%s\n", __func__);
310 args[args_len++] = readRegister & 0xFF;
311 args[args_len++] = readRegister >> 8;
313 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
314 reportType, args, args_len, buf_recv, data_len);
316 dev_err(&client->dev,
317 "failed to retrieve report from device.\n");
325 * i2c_hid_set_or_send_report: forward an incoming report to the device
326 * @client: the i2c_client of the device
327 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
328 * @reportID: the report ID
329 * @buf: the actual data to transfer, without the report ID
330 * @data_len: size of buf
331 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
333 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
334 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
336 struct i2c_hid *ihid = i2c_get_clientdata(client);
337 u8 *args = ihid->argsbuf;
338 const struct i2c_hid_cmd *hidcmd;
340 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
341 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
342 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
347 i2c_hid_dbg(ihid, "%s\n", __func__);
349 if (data_len > ihid->bufsize)
352 size = 2 /* size */ +
353 (reportID ? 1 : 0) /* reportID */ +
355 args_len = 2 /* dataRegister */ +
358 if (!use_data && maxOutputLength == 0)
362 * use the data register for feature reports or if the device does not
363 * support the output register
366 args[index++] = dataRegister & 0xFF;
367 args[index++] = dataRegister >> 8;
368 hidcmd = &hid_set_report_cmd;
370 args[index++] = outputRegister & 0xFF;
371 args[index++] = outputRegister >> 8;
372 hidcmd = &hid_no_cmd;
375 args[index++] = size & 0xFF;
376 args[index++] = size >> 8;
379 args[index++] = reportID;
381 memcpy(&args[index], buf, data_len);
383 ret = __i2c_hid_command(client, hidcmd, reportID,
384 reportType, args, args_len, NULL, 0);
386 dev_err(&client->dev, "failed to set a report to device.\n");
393 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
395 struct i2c_hid *ihid = i2c_get_clientdata(client);
398 i2c_hid_dbg(ihid, "%s\n", __func__);
401 * Some devices require to send a command to wakeup before power on.
402 * The call will get a return value (EREMOTEIO) but device will be
403 * triggered and activated. After that, it goes like a normal device.
405 if (power_state == I2C_HID_PWR_ON &&
406 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
407 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
409 /* Device was already activated */
414 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
415 0, NULL, 0, NULL, 0);
418 dev_err(&client->dev, "failed to change power setting.\n");
423 * The HID over I2C specification states that if a DEVICE needs time
424 * after the PWR_ON request, it should utilise CLOCK stretching.
425 * However, it has been observered that the Windows driver provides a
426 * 1ms sleep between the PWR_ON and RESET requests.
427 * According to Goodix Windows even waits 60 ms after (other?)
428 * PWR_ON requests. Testing has confirmed that several devices
429 * will not work properly without a delay after a PWR_ON request.
431 if (!ret && power_state == I2C_HID_PWR_ON)
437 static int i2c_hid_hwreset(struct i2c_client *client)
439 struct i2c_hid *ihid = i2c_get_clientdata(client);
442 i2c_hid_dbg(ihid, "%s\n", __func__);
445 * This prevents sending feature reports while the device is
446 * being reset. Otherwise we may lose the reset complete
449 mutex_lock(&ihid->reset_lock);
451 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
455 i2c_hid_dbg(ihid, "resetting...\n");
457 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
459 dev_err(&client->dev, "failed to reset device.\n");
460 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
464 /* At least some SIS devices need this after reset */
465 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
466 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
469 mutex_unlock(&ihid->reset_lock);
473 static void i2c_hid_get_input(struct i2c_hid *ihid)
477 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
479 if (size > ihid->bufsize)
480 size = ihid->bufsize;
482 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
487 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
488 __func__, ret, size);
492 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
495 /* host or device initiated RESET completed */
496 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
497 wake_up(&ihid->wait);
501 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
502 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
503 "there's no data\n", __func__);
507 if ((ret_size > size) || (ret_size < 2)) {
508 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
509 ihid->inbuf[0] = size & 0xff;
510 ihid->inbuf[1] = size >> 8;
513 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
514 __func__, size, ret_size);
519 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
521 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
522 pm_wakeup_event(&ihid->client->dev, 0);
524 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
531 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
533 struct i2c_hid *ihid = dev_id;
535 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
538 i2c_hid_get_input(ihid);
543 static int i2c_hid_get_report_length(struct hid_report *report)
545 return ((report->size - 1) >> 3) + 1 +
546 report->device->report_enum[report->type].numbered + 2;
550 * Traverse the supplied list of reports and find the longest
552 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
555 struct hid_report *report;
558 /* We should not rely on wMaxInputLength, as some devices may set it to
560 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
561 size = i2c_hid_get_report_length(report);
567 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
571 kfree(ihid->argsbuf);
576 ihid->argsbuf = NULL;
580 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
582 /* the worst case is computed from the set_report command with a
583 * reportID > 15 and the maximum report length */
584 int args_len = sizeof(__u8) + /* ReportID */
585 sizeof(__u8) + /* optional ReportID byte */
586 sizeof(__u16) + /* data register */
587 sizeof(__u16) + /* size of the report */
588 report_size; /* report */
590 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
591 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
592 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
593 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
595 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
596 i2c_hid_free_buffers(ihid);
600 ihid->bufsize = report_size;
605 static int i2c_hid_get_raw_report(struct hid_device *hid,
606 unsigned char report_number, __u8 *buf, size_t count,
607 unsigned char report_type)
609 struct i2c_client *client = hid->driver_data;
610 struct i2c_hid *ihid = i2c_get_clientdata(client);
611 size_t ret_count, ask_count;
614 if (report_type == HID_OUTPUT_REPORT)
618 * In case of unnumbered reports the response from the device will
619 * not have the report ID that the upper layers expect, so we need
620 * to stash it the buffer ourselves and adjust the data size.
622 if (!report_number) {
628 /* +2 bytes to include the size of the reply in the query buffer */
629 ask_count = min(count + 2, (size_t)ihid->bufsize);
631 ret = i2c_hid_get_report(client,
632 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
633 report_number, ihid->rawbuf, ask_count);
638 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
643 ret_count = min(ret_count, ask_count);
645 /* The query buffer contains the size, dropping it in the reply */
646 count = min(count, ret_count - 2);
647 memcpy(buf, ihid->rawbuf + 2, count);
655 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
656 size_t count, unsigned char report_type, bool use_data)
658 struct i2c_client *client = hid->driver_data;
659 struct i2c_hid *ihid = i2c_get_clientdata(client);
660 int report_id = buf[0];
663 if (report_type == HID_INPUT_REPORT)
666 mutex_lock(&ihid->reset_lock);
669 * Note that both numbered and unnumbered reports passed here
670 * are supposed to have report ID stored in the 1st byte of the
671 * buffer, so we strip it off unconditionally before passing payload
672 * to i2c_hid_set_or_send_report which takes care of encoding
673 * everything properly.
675 ret = i2c_hid_set_or_send_report(client,
676 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
677 report_id, buf + 1, count - 1, use_data);
680 ret++; /* add report_id to the number of transferred bytes */
682 mutex_unlock(&ihid->reset_lock);
687 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
690 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
694 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
695 __u8 *buf, size_t len, unsigned char rtype,
699 case HID_REQ_GET_REPORT:
700 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
701 case HID_REQ_SET_REPORT:
702 if (buf[0] != reportnum)
704 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
710 static int i2c_hid_parse(struct hid_device *hid)
712 struct i2c_client *client = hid->driver_data;
713 struct i2c_hid *ihid = i2c_get_clientdata(client);
714 struct i2c_hid_desc *hdesc = &ihid->hdesc;
721 i2c_hid_dbg(ihid, "entering %s\n", __func__);
723 rsize = le16_to_cpu(hdesc->wReportDescLength);
724 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
725 dbg_hid("weird size of report descriptor (%u)\n", rsize);
730 ret = i2c_hid_hwreset(client);
733 } while (tries-- > 0 && ret);
738 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
742 rdesc = use_override;
743 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
745 rdesc = kzalloc(rsize, GFP_KERNEL);
748 dbg_hid("couldn't allocate rdesc memory\n");
752 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
754 ret = i2c_hid_command(client, &hid_report_descr_cmd,
757 hid_err(hid, "reading report descriptor failed\n");
763 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
765 ret = hid_parse_report(hid, rdesc, rsize);
770 dbg_hid("parsing report descriptor failed\n");
777 static int i2c_hid_start(struct hid_device *hid)
779 struct i2c_client *client = hid->driver_data;
780 struct i2c_hid *ihid = i2c_get_clientdata(client);
782 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
784 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
785 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
786 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
788 if (bufsize > ihid->bufsize) {
789 disable_irq(client->irq);
790 i2c_hid_free_buffers(ihid);
792 ret = i2c_hid_alloc_buffers(ihid, bufsize);
793 enable_irq(client->irq);
802 static void i2c_hid_stop(struct hid_device *hid)
807 static int i2c_hid_open(struct hid_device *hid)
809 struct i2c_client *client = hid->driver_data;
810 struct i2c_hid *ihid = i2c_get_clientdata(client);
812 set_bit(I2C_HID_STARTED, &ihid->flags);
816 static void i2c_hid_close(struct hid_device *hid)
818 struct i2c_client *client = hid->driver_data;
819 struct i2c_hid *ihid = i2c_get_clientdata(client);
821 clear_bit(I2C_HID_STARTED, &ihid->flags);
824 struct hid_ll_driver i2c_hid_ll_driver = {
825 .parse = i2c_hid_parse,
826 .start = i2c_hid_start,
827 .stop = i2c_hid_stop,
828 .open = i2c_hid_open,
829 .close = i2c_hid_close,
830 .output_report = i2c_hid_output_report,
831 .raw_request = i2c_hid_raw_request,
833 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
835 static int i2c_hid_init_irq(struct i2c_client *client)
837 struct i2c_hid *ihid = i2c_get_clientdata(client);
838 unsigned long irqflags = 0;
841 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
843 if (!irq_get_trigger_type(client->irq))
844 irqflags = IRQF_TRIGGER_LOW;
846 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
847 irqflags | IRQF_ONESHOT, client->name, ihid);
849 dev_warn(&client->dev,
850 "Could not register for %s interrupt, irq = %d,"
852 client->name, client->irq, ret);
860 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
862 struct i2c_client *client = ihid->client;
863 struct i2c_hid_desc *hdesc = &ihid->hdesc;
867 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
868 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
869 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
871 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
873 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
874 ret = i2c_hid_command(client, &hid_descr_cmd,
876 sizeof(struct i2c_hid_desc));
878 dev_err(&client->dev, "hid_descr_cmd failed\n");
883 /* Validate the length of HID descriptor, the 4 first bytes:
884 * bytes 0-1 -> length
885 * bytes 2-3 -> bcdVersion (has to be 1.00) */
886 /* check bcdVersion == 1.0 */
887 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
888 dev_err(&client->dev,
889 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
890 le16_to_cpu(hdesc->bcdVersion));
894 /* Descriptor length should be 30 bytes as per the specification */
895 dsize = le16_to_cpu(hdesc->wHIDDescLength);
896 if (dsize != sizeof(struct i2c_hid_desc)) {
897 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
901 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
905 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
907 if (!ihid->ops->power_up)
910 return ihid->ops->power_up(ihid->ops);
913 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
915 if (!ihid->ops->power_down)
918 ihid->ops->power_down(ihid->ops);
921 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
923 if (!ihid->ops->shutdown_tail)
926 ihid->ops->shutdown_tail(ihid->ops);
929 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
930 u16 hid_descriptor_address, u32 quirks)
933 struct i2c_hid *ihid;
934 struct hid_device *hid;
936 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
939 dev_err(&client->dev,
940 "HID over i2c has not been provided an Int IRQ\n");
944 if (client->irq < 0) {
945 if (client->irq != -EPROBE_DEFER)
946 dev_err(&client->dev,
947 "HID over i2c doesn't have a valid IRQ\n");
951 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
957 ret = i2c_hid_core_power_up(ihid);
961 i2c_set_clientdata(client, ihid);
963 ihid->client = client;
965 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
967 init_waitqueue_head(&ihid->wait);
968 mutex_init(&ihid->reset_lock);
970 /* we need to allocate the command buffer without knowing the maximum
971 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
972 * real computation later. */
973 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
977 device_enable_async_suspend(&client->dev);
979 /* Make sure there is something at this address */
980 ret = i2c_smbus_read_byte(client);
982 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
987 ret = i2c_hid_fetch_hid_descriptor(ihid);
989 dev_err(&client->dev,
990 "Failed to fetch the HID Descriptor\n");
994 ret = i2c_hid_init_irq(client);
998 hid = hid_allocate_device();
1006 hid->driver_data = client;
1007 hid->ll_driver = &i2c_hid_ll_driver;
1008 hid->dev.parent = &client->dev;
1010 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1011 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1012 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1014 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1015 client->name, (u16)hid->vendor, (u16)hid->product);
1016 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1018 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1020 ret = hid_add_device(hid);
1023 hid_err(client, "can't add hid device: %d\n", ret);
1027 hid->quirks |= quirks;
1032 hid_destroy_device(hid);
1035 free_irq(client->irq, ihid);
1038 i2c_hid_core_power_down(ihid);
1039 i2c_hid_free_buffers(ihid);
1042 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1044 int i2c_hid_core_remove(struct i2c_client *client)
1046 struct i2c_hid *ihid = i2c_get_clientdata(client);
1047 struct hid_device *hid;
1050 hid_destroy_device(hid);
1052 free_irq(client->irq, ihid);
1055 i2c_hid_free_buffers(ihid);
1057 i2c_hid_core_power_down(ihid);
1061 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1063 void i2c_hid_core_shutdown(struct i2c_client *client)
1065 struct i2c_hid *ihid = i2c_get_clientdata(client);
1067 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1068 free_irq(client->irq, ihid);
1070 i2c_hid_core_shutdown_tail(ihid);
1072 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1074 #ifdef CONFIG_PM_SLEEP
1075 static int i2c_hid_core_suspend(struct device *dev)
1077 struct i2c_client *client = to_i2c_client(dev);
1078 struct i2c_hid *ihid = i2c_get_clientdata(client);
1079 struct hid_device *hid = ihid->hid;
1083 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1087 /* Save some power */
1088 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1090 disable_irq(client->irq);
1092 if (device_may_wakeup(&client->dev)) {
1093 wake_status = enable_irq_wake(client->irq);
1095 ihid->irq_wake_enabled = true;
1097 hid_warn(hid, "Failed to enable irq wake: %d\n",
1100 i2c_hid_core_power_down(ihid);
1106 static int i2c_hid_core_resume(struct device *dev)
1109 struct i2c_client *client = to_i2c_client(dev);
1110 struct i2c_hid *ihid = i2c_get_clientdata(client);
1111 struct hid_device *hid = ihid->hid;
1114 if (!device_may_wakeup(&client->dev)) {
1115 i2c_hid_core_power_up(ihid);
1116 } else if (ihid->irq_wake_enabled) {
1117 wake_status = disable_irq_wake(client->irq);
1119 ihid->irq_wake_enabled = false;
1121 hid_warn(hid, "Failed to disable irq wake: %d\n",
1125 enable_irq(client->irq);
1127 /* Instead of resetting device, simply powers the device on. This
1128 * solves "incomplete reports" on Raydium devices 2386:3118 and
1129 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1130 * data after a suspend/resume.
1132 * However some ALPS touchpads generate IRQ storm without reset, so
1133 * let's still reset them here.
1135 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1136 ret = i2c_hid_hwreset(client);
1138 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1143 return hid_driver_reset_resume(hid);
1147 const struct dev_pm_ops i2c_hid_core_pm = {
1148 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1150 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1152 MODULE_DESCRIPTION("HID over I2C core driver");
1153 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1154 MODULE_LICENSE("GPL");