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 <asm/unaligned.h>
40 #include "../hid-ids.h"
43 /* quirks to control the device */
44 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
45 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
46 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
47 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
48 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
49 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7)
52 #define I2C_HID_OPCODE_RESET 0x01
53 #define I2C_HID_OPCODE_GET_REPORT 0x02
54 #define I2C_HID_OPCODE_SET_REPORT 0x03
55 #define I2C_HID_OPCODE_GET_IDLE 0x04
56 #define I2C_HID_OPCODE_SET_IDLE 0x05
57 #define I2C_HID_OPCODE_GET_PROTOCOL 0x06
58 #define I2C_HID_OPCODE_SET_PROTOCOL 0x07
59 #define I2C_HID_OPCODE_SET_POWER 0x08
62 #define I2C_HID_STARTED 0
63 #define I2C_HID_RESET_PENDING 1
64 #define I2C_HID_READ_PENDING 2
66 #define I2C_HID_PWR_ON 0x00
67 #define I2C_HID_PWR_SLEEP 0x01
71 module_param(debug, bool, 0444);
72 MODULE_PARM_DESC(debug, "print a lot of debug information");
74 #define i2c_hid_dbg(ihid, fmt, arg...) \
77 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
81 __le16 wHIDDescLength;
83 __le16 wReportDescLength;
84 __le16 wReportDescRegister;
85 __le16 wInputRegister;
86 __le16 wMaxInputLength;
87 __le16 wOutputRegister;
88 __le16 wMaxOutputLength;
89 __le16 wCommandRegister;
97 /* The main device structure */
99 struct i2c_client *client; /* i2c client */
100 struct hid_device *hid; /* pointer to corresponding HID dev */
101 struct i2c_hid_desc hdesc; /* the HID Descriptor */
102 __le16 wHIDDescRegister; /* location of the i2c
103 * register of the HID
105 unsigned int bufsize; /* i2c buffer size */
106 u8 *inbuf; /* Input buffer */
107 u8 *rawbuf; /* Raw Input buffer */
108 u8 *cmdbuf; /* Command buffer */
110 unsigned long flags; /* device flags */
111 unsigned long quirks; /* Various quirks */
113 wait_queue_head_t wait; /* For waiting the interrupt */
115 bool irq_wake_enabled;
116 struct mutex reset_lock;
118 struct i2chid_ops *ops;
121 static const struct i2c_hid_quirks {
125 } i2c_hid_quirks[] = {
126 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
127 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
128 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
129 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
130 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
131 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
132 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
133 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
134 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
135 I2C_HID_QUIRK_RESET_ON_RESUME },
136 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
137 I2C_HID_QUIRK_RESET_ON_RESUME },
138 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
139 I2C_HID_QUIRK_BAD_INPUT_SIZE },
141 * Sending the wakeup after reset actually break ELAN touchscreen controller
143 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
144 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
145 I2C_HID_QUIRK_BOGUS_IRQ },
150 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
151 * @idVendor: the 16-bit vendor ID
152 * @idProduct: the 16-bit product ID
154 * Returns: a u32 quirks value.
156 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
161 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
162 if (i2c_hid_quirks[n].idVendor == idVendor &&
163 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
164 i2c_hid_quirks[n].idProduct == idProduct))
165 quirks = i2c_hid_quirks[n].quirks;
170 static int i2c_hid_xfer(struct i2c_hid *ihid,
171 u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
173 struct i2c_client *client = ihid->client;
174 struct i2c_msg msgs[2] = { 0 };
179 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
180 __func__, send_len, send_buf);
182 msgs[n].addr = client->addr;
183 msgs[n].flags = client->flags & I2C_M_TEN;
184 msgs[n].len = send_len;
185 msgs[n].buf = send_buf;
190 msgs[n].addr = client->addr;
191 msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_RD;
192 msgs[n].len = recv_len;
193 msgs[n].buf = recv_buf;
196 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
199 ret = i2c_transfer(client->adapter, msgs, n);
202 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
205 return ret < 0 ? ret : -EIO;
210 static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
211 void *buf, size_t len)
213 *(__le16 *)ihid->cmdbuf = reg;
215 return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
218 static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
219 int report_type, int report_id)
223 if (report_id < 0x0F) {
224 buf[length++] = report_type << 4 | report_id;
225 buf[length++] = opcode;
227 buf[length++] = report_type << 4 | 0x0F;
228 buf[length++] = opcode;
229 buf[length++] = report_id;
235 static int i2c_hid_get_report(struct i2c_hid *ihid,
236 u8 report_type, u8 report_id,
237 u8 *recv_buf, size_t recv_len)
243 i2c_hid_dbg(ihid, "%s\n", __func__);
245 /* Command register goes first */
246 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
247 length += sizeof(__le16);
248 /* Next is GET_REPORT command */
249 length += i2c_hid_encode_command(ihid->cmdbuf + length,
250 I2C_HID_OPCODE_GET_REPORT,
251 report_type, report_id);
253 * Device will send report data through data register. Because
254 * command can be either 2 or 3 bytes destination for the data
255 * register may be not aligned.
257 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
258 ihid->cmdbuf + length);
259 length += sizeof(__le16);
262 * In addition to report data device will supply data length
263 * in the first 2 bytes of the response, so adjust .
265 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
266 ihid->rawbuf, recv_len + sizeof(__le16));
268 dev_err(&ihid->client->dev,
269 "failed to set a report to device: %d\n", error);
273 /* The buffer is sufficiently aligned */
274 ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
276 /* Check for empty report response */
277 if (ret_count <= sizeof(__le16))
280 recv_len = min(recv_len, ret_count - sizeof(__le16));
281 memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
283 if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
284 dev_err(&ihid->client->dev,
285 "device returned incorrect report (%d vs %d expected)\n",
286 recv_buf[0], report_id);
293 static size_t i2c_hid_format_report(u8 *buf, int report_id,
294 const u8 *data, size_t size)
296 size_t length = sizeof(__le16); /* reserve space to store size */
299 buf[length++] = report_id;
301 memcpy(buf + length, data, size);
304 /* Store overall size in the beginning of the buffer */
305 put_unaligned_le16(length, buf);
311 * i2c_hid_set_or_send_report: forward an incoming report to the device
312 * @ihid: the i2c hid device
313 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
314 * @report_id: the report ID
315 * @buf: the actual data to transfer, without the report ID
316 * @data_len: size of buf
317 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
319 static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
320 u8 report_type, u8 report_id,
321 const u8 *buf, size_t data_len,
327 i2c_hid_dbg(ihid, "%s\n", __func__);
329 if (data_len > ihid->bufsize)
332 if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
336 /* Command register goes first */
337 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
338 length += sizeof(__le16);
339 /* Next is SET_REPORT command */
340 length += i2c_hid_encode_command(ihid->cmdbuf + length,
341 I2C_HID_OPCODE_SET_REPORT,
342 report_type, report_id);
344 * Report data will go into the data register. Because
345 * command can be either 2 or 3 bytes destination for
346 * the data register may be not aligned.
348 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
349 ihid->cmdbuf + length);
350 length += sizeof(__le16);
353 * With simple "send report" all data goes into the output
356 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;;
357 length += sizeof(__le16);
360 length += i2c_hid_format_report(ihid->cmdbuf + length,
361 report_id, buf, data_len);
363 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
365 dev_err(&ihid->client->dev,
366 "failed to set a report to device: %d\n", error);
373 static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
377 /* SET_POWER uses command register */
378 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
379 length = sizeof(__le16);
381 /* Now the command itself */
382 length += i2c_hid_encode_command(ihid->cmdbuf + length,
383 I2C_HID_OPCODE_SET_POWER,
386 return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
389 static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
393 i2c_hid_dbg(ihid, "%s\n", __func__);
396 * Some devices require to send a command to wakeup before power on.
397 * The call will get a return value (EREMOTEIO) but device will be
398 * triggered and activated. After that, it goes like a normal device.
400 if (power_state == I2C_HID_PWR_ON &&
401 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
402 ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
404 /* Device was already activated */
409 ret = i2c_hid_set_power_command(ihid, power_state);
411 dev_err(&ihid->client->dev,
412 "failed to change power setting.\n");
417 * The HID over I2C specification states that if a DEVICE needs time
418 * after the PWR_ON request, it should utilise CLOCK stretching.
419 * However, it has been observered that the Windows driver provides a
420 * 1ms sleep between the PWR_ON and RESET requests.
421 * According to Goodix Windows even waits 60 ms after (other?)
422 * PWR_ON requests. Testing has confirmed that several devices
423 * will not work properly without a delay after a PWR_ON request.
425 if (!ret && power_state == I2C_HID_PWR_ON)
431 static int i2c_hid_execute_reset(struct i2c_hid *ihid)
436 i2c_hid_dbg(ihid, "resetting...\n");
438 /* Prepare reset command. Command register goes first. */
439 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
440 length += sizeof(__le16);
441 /* Next is RESET command itself */
442 length += i2c_hid_encode_command(ihid->cmdbuf + length,
443 I2C_HID_OPCODE_RESET, 0, 0);
445 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
447 ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
449 dev_err(&ihid->client->dev, "failed to reset device.\n");
453 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
458 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
459 if (!wait_event_timeout(ihid->wait,
460 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
461 msecs_to_jiffies(5000))) {
465 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
468 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
472 static int i2c_hid_hwreset(struct i2c_hid *ihid)
476 i2c_hid_dbg(ihid, "%s\n", __func__);
479 * This prevents sending feature reports while the device is
480 * being reset. Otherwise we may lose the reset complete
483 mutex_lock(&ihid->reset_lock);
485 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
489 ret = i2c_hid_execute_reset(ihid);
491 dev_err(&ihid->client->dev,
492 "failed to reset device: %d\n", ret);
493 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
497 /* At least some SIS devices need this after reset */
498 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
499 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
502 mutex_unlock(&ihid->reset_lock);
506 static void i2c_hid_get_input(struct i2c_hid *ihid)
508 u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
512 if (size > ihid->bufsize)
513 size = ihid->bufsize;
515 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
520 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
521 __func__, ret, size);
525 /* Receiving buffer is properly aligned */
526 ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
528 /* host or device initiated RESET completed */
529 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
530 wake_up(&ihid->wait);
534 if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
535 dev_warn_once(&ihid->client->dev,
536 "%s: IRQ triggered but there's no data\n",
541 if (ret_size > size || ret_size < sizeof(__le16)) {
542 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
543 *(__le16 *)ihid->inbuf = cpu_to_le16(size);
546 dev_err(&ihid->client->dev,
547 "%s: incomplete report (%d/%d)\n",
548 __func__, size, ret_size);
553 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
555 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
556 pm_wakeup_event(&ihid->client->dev, 0);
558 hid_input_report(ihid->hid, HID_INPUT_REPORT,
559 ihid->inbuf + sizeof(__le16),
560 ret_size - sizeof(__le16), 1);
566 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
568 struct i2c_hid *ihid = dev_id;
570 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
573 i2c_hid_get_input(ihid);
578 static int i2c_hid_get_report_length(struct hid_report *report)
580 return ((report->size - 1) >> 3) + 1 +
581 report->device->report_enum[report->type].numbered + 2;
585 * Traverse the supplied list of reports and find the longest
587 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
590 struct hid_report *report;
593 /* We should not rely on wMaxInputLength, as some devices may set it to
595 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
596 size = i2c_hid_get_report_length(report);
602 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
613 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
616 * The worst case is computed from the set_report command with a
617 * reportID > 15 and the maximum report length.
619 int cmd_len = sizeof(__le16) + /* command register */
620 sizeof(u8) + /* encoded report type/ID */
621 sizeof(u8) + /* opcode */
622 sizeof(u8) + /* optional 3rd byte report ID */
623 sizeof(__le16) + /* data register */
624 sizeof(__le16) + /* report data size */
625 sizeof(u8) + /* report ID if numbered report */
628 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
629 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
630 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
632 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
633 i2c_hid_free_buffers(ihid);
637 ihid->bufsize = report_size;
642 static int i2c_hid_get_raw_report(struct hid_device *hid,
643 u8 report_type, u8 report_id,
644 u8 *buf, size_t count)
646 struct i2c_client *client = hid->driver_data;
647 struct i2c_hid *ihid = i2c_get_clientdata(client);
650 if (report_type == HID_OUTPUT_REPORT)
654 * In case of unnumbered reports the response from the device will
655 * not have the report ID that the upper layers expect, so we need
656 * to stash it the buffer ourselves and adjust the data size.
664 ret_count = i2c_hid_get_report(ihid,
665 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
666 report_id, buf, count);
668 if (ret_count > 0 && !report_id)
674 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
675 const u8 *buf, size_t count, bool do_set)
677 struct i2c_client *client = hid->driver_data;
678 struct i2c_hid *ihid = i2c_get_clientdata(client);
679 int report_id = buf[0];
682 if (report_type == HID_INPUT_REPORT)
685 mutex_lock(&ihid->reset_lock);
688 * Note that both numbered and unnumbered reports passed here
689 * are supposed to have report ID stored in the 1st byte of the
690 * buffer, so we strip it off unconditionally before passing payload
691 * to i2c_hid_set_or_send_report which takes care of encoding
692 * everything properly.
694 ret = i2c_hid_set_or_send_report(ihid,
695 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
696 report_id, buf + 1, count - 1, do_set);
699 ret++; /* add report_id to the number of transferred bytes */
701 mutex_unlock(&ihid->reset_lock);
706 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
708 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
712 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
713 __u8 *buf, size_t len, unsigned char rtype,
717 case HID_REQ_GET_REPORT:
718 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
719 case HID_REQ_SET_REPORT:
720 if (buf[0] != reportnum)
722 return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
728 static int i2c_hid_parse(struct hid_device *hid)
730 struct i2c_client *client = hid->driver_data;
731 struct i2c_hid *ihid = i2c_get_clientdata(client);
732 struct i2c_hid_desc *hdesc = &ihid->hdesc;
739 i2c_hid_dbg(ihid, "entering %s\n", __func__);
741 rsize = le16_to_cpu(hdesc->wReportDescLength);
742 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
743 dbg_hid("weird size of report descriptor (%u)\n", rsize);
748 ret = i2c_hid_hwreset(ihid);
751 } while (tries-- > 0 && ret);
756 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
760 rdesc = use_override;
761 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
763 rdesc = kzalloc(rsize, GFP_KERNEL);
766 dbg_hid("couldn't allocate rdesc memory\n");
770 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
772 ret = i2c_hid_read_register(ihid,
773 ihid->hdesc.wReportDescRegister,
776 hid_err(hid, "reading report descriptor failed\n");
782 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
784 ret = hid_parse_report(hid, rdesc, rsize);
789 dbg_hid("parsing report descriptor failed\n");
796 static int i2c_hid_start(struct hid_device *hid)
798 struct i2c_client *client = hid->driver_data;
799 struct i2c_hid *ihid = i2c_get_clientdata(client);
801 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
803 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
804 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
805 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
807 if (bufsize > ihid->bufsize) {
808 disable_irq(client->irq);
809 i2c_hid_free_buffers(ihid);
811 ret = i2c_hid_alloc_buffers(ihid, bufsize);
812 enable_irq(client->irq);
821 static void i2c_hid_stop(struct hid_device *hid)
826 static int i2c_hid_open(struct hid_device *hid)
828 struct i2c_client *client = hid->driver_data;
829 struct i2c_hid *ihid = i2c_get_clientdata(client);
831 set_bit(I2C_HID_STARTED, &ihid->flags);
835 static void i2c_hid_close(struct hid_device *hid)
837 struct i2c_client *client = hid->driver_data;
838 struct i2c_hid *ihid = i2c_get_clientdata(client);
840 clear_bit(I2C_HID_STARTED, &ihid->flags);
843 struct hid_ll_driver i2c_hid_ll_driver = {
844 .parse = i2c_hid_parse,
845 .start = i2c_hid_start,
846 .stop = i2c_hid_stop,
847 .open = i2c_hid_open,
848 .close = i2c_hid_close,
849 .output_report = i2c_hid_output_report,
850 .raw_request = i2c_hid_raw_request,
852 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
854 static int i2c_hid_init_irq(struct i2c_client *client)
856 struct i2c_hid *ihid = i2c_get_clientdata(client);
857 unsigned long irqflags = 0;
860 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
862 if (!irq_get_trigger_type(client->irq))
863 irqflags = IRQF_TRIGGER_LOW;
865 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
866 irqflags | IRQF_ONESHOT, client->name, ihid);
868 dev_warn(&client->dev,
869 "Could not register for %s interrupt, irq = %d,"
871 client->name, client->irq, ret);
879 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
881 struct i2c_client *client = ihid->client;
882 struct i2c_hid_desc *hdesc = &ihid->hdesc;
886 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
887 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
888 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
890 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
892 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
893 error = i2c_hid_read_register(ihid,
894 ihid->wHIDDescRegister,
896 sizeof(ihid->hdesc));
898 dev_err(&ihid->client->dev,
899 "failed to fetch HID descriptor: %d\n",
905 /* Validate the length of HID descriptor, the 4 first bytes:
906 * bytes 0-1 -> length
907 * bytes 2-3 -> bcdVersion (has to be 1.00) */
908 /* check bcdVersion == 1.0 */
909 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
910 dev_err(&ihid->client->dev,
911 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
912 le16_to_cpu(hdesc->bcdVersion));
916 /* Descriptor length should be 30 bytes as per the specification */
917 dsize = le16_to_cpu(hdesc->wHIDDescLength);
918 if (dsize != sizeof(struct i2c_hid_desc)) {
919 dev_err(&ihid->client->dev,
920 "weird size of HID descriptor (%u)\n", dsize);
923 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
927 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
929 if (!ihid->ops->power_up)
932 return ihid->ops->power_up(ihid->ops);
935 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
937 if (!ihid->ops->power_down)
940 ihid->ops->power_down(ihid->ops);
943 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
945 if (!ihid->ops->shutdown_tail)
948 ihid->ops->shutdown_tail(ihid->ops);
951 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
952 u16 hid_descriptor_address, u32 quirks)
955 struct i2c_hid *ihid;
956 struct hid_device *hid;
958 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
961 dev_err(&client->dev,
962 "HID over i2c has not been provided an Int IRQ\n");
966 if (client->irq < 0) {
967 if (client->irq != -EPROBE_DEFER)
968 dev_err(&client->dev,
969 "HID over i2c doesn't have a valid IRQ\n");
973 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
979 ret = i2c_hid_core_power_up(ihid);
983 i2c_set_clientdata(client, ihid);
985 ihid->client = client;
987 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
989 init_waitqueue_head(&ihid->wait);
990 mutex_init(&ihid->reset_lock);
992 /* we need to allocate the command buffer without knowing the maximum
993 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
994 * real computation later. */
995 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
999 device_enable_async_suspend(&client->dev);
1001 /* Make sure there is something at this address */
1002 ret = i2c_smbus_read_byte(client);
1004 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1009 ret = i2c_hid_fetch_hid_descriptor(ihid);
1011 dev_err(&client->dev,
1012 "Failed to fetch the HID Descriptor\n");
1016 ret = i2c_hid_init_irq(client);
1020 hid = hid_allocate_device();
1028 hid->driver_data = client;
1029 hid->ll_driver = &i2c_hid_ll_driver;
1030 hid->dev.parent = &client->dev;
1032 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1033 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1034 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1036 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1037 client->name, (u16)hid->vendor, (u16)hid->product);
1038 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1040 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1042 ret = hid_add_device(hid);
1045 hid_err(client, "can't add hid device: %d\n", ret);
1049 hid->quirks |= quirks;
1054 hid_destroy_device(hid);
1057 free_irq(client->irq, ihid);
1060 i2c_hid_core_power_down(ihid);
1061 i2c_hid_free_buffers(ihid);
1064 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1066 int i2c_hid_core_remove(struct i2c_client *client)
1068 struct i2c_hid *ihid = i2c_get_clientdata(client);
1069 struct hid_device *hid;
1072 hid_destroy_device(hid);
1074 free_irq(client->irq, ihid);
1077 i2c_hid_free_buffers(ihid);
1079 i2c_hid_core_power_down(ihid);
1083 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1085 void i2c_hid_core_shutdown(struct i2c_client *client)
1087 struct i2c_hid *ihid = i2c_get_clientdata(client);
1089 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1090 free_irq(client->irq, ihid);
1092 i2c_hid_core_shutdown_tail(ihid);
1094 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1096 #ifdef CONFIG_PM_SLEEP
1097 static int i2c_hid_core_suspend(struct device *dev)
1099 struct i2c_client *client = to_i2c_client(dev);
1100 struct i2c_hid *ihid = i2c_get_clientdata(client);
1101 struct hid_device *hid = ihid->hid;
1105 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1109 /* Save some power */
1110 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1112 disable_irq(client->irq);
1114 if (device_may_wakeup(&client->dev)) {
1115 wake_status = enable_irq_wake(client->irq);
1117 ihid->irq_wake_enabled = true;
1119 hid_warn(hid, "Failed to enable irq wake: %d\n",
1122 i2c_hid_core_power_down(ihid);
1128 static int i2c_hid_core_resume(struct device *dev)
1131 struct i2c_client *client = to_i2c_client(dev);
1132 struct i2c_hid *ihid = i2c_get_clientdata(client);
1133 struct hid_device *hid = ihid->hid;
1136 if (!device_may_wakeup(&client->dev)) {
1137 i2c_hid_core_power_up(ihid);
1138 } else if (ihid->irq_wake_enabled) {
1139 wake_status = disable_irq_wake(client->irq);
1141 ihid->irq_wake_enabled = false;
1143 hid_warn(hid, "Failed to disable irq wake: %d\n",
1147 enable_irq(client->irq);
1149 /* Instead of resetting device, simply powers the device on. This
1150 * solves "incomplete reports" on Raydium devices 2386:3118 and
1151 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1152 * data after a suspend/resume.
1154 * However some ALPS touchpads generate IRQ storm without reset, so
1155 * let's still reset them here.
1157 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1158 ret = i2c_hid_hwreset(ihid);
1160 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1165 return hid_driver_reset_resume(hid);
1169 const struct dev_pm_ops i2c_hid_core_pm = {
1170 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1172 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1174 MODULE_DESCRIPTION("HID over I2C core driver");
1175 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1176 MODULE_LICENSE("GPL");