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/pm_wakeirq.h>
30 #include <linux/device.h>
31 #include <linux/wait.h>
32 #include <linux/err.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
35 #include <linux/jiffies.h>
36 #include <linux/kernel.h>
37 #include <linux/hid.h>
38 #include <linux/mutex.h>
39 #include <asm/unaligned.h>
41 #include "../hid-ids.h"
44 /* quirks to control the device */
45 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
46 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
47 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
48 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
49 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
50 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7)
53 #define I2C_HID_OPCODE_RESET 0x01
54 #define I2C_HID_OPCODE_GET_REPORT 0x02
55 #define I2C_HID_OPCODE_SET_REPORT 0x03
56 #define I2C_HID_OPCODE_GET_IDLE 0x04
57 #define I2C_HID_OPCODE_SET_IDLE 0x05
58 #define I2C_HID_OPCODE_GET_PROTOCOL 0x06
59 #define I2C_HID_OPCODE_SET_PROTOCOL 0x07
60 #define I2C_HID_OPCODE_SET_POWER 0x08
63 #define I2C_HID_STARTED 0
64 #define I2C_HID_RESET_PENDING 1
65 #define I2C_HID_READ_PENDING 2
67 #define I2C_HID_PWR_ON 0x00
68 #define I2C_HID_PWR_SLEEP 0x01
72 module_param(debug, bool, 0444);
73 MODULE_PARM_DESC(debug, "print a lot of debug information");
75 #define i2c_hid_dbg(ihid, fmt, arg...) \
78 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
82 __le16 wHIDDescLength;
84 __le16 wReportDescLength;
85 __le16 wReportDescRegister;
86 __le16 wInputRegister;
87 __le16 wMaxInputLength;
88 __le16 wOutputRegister;
89 __le16 wMaxOutputLength;
90 __le16 wCommandRegister;
98 /* The main device structure */
100 struct i2c_client *client; /* i2c client */
101 struct hid_device *hid; /* pointer to corresponding HID dev */
102 struct i2c_hid_desc hdesc; /* the HID Descriptor */
103 __le16 wHIDDescRegister; /* location of the i2c
104 * register of the HID
106 unsigned int bufsize; /* i2c buffer size */
107 u8 *inbuf; /* Input buffer */
108 u8 *rawbuf; /* Raw Input buffer */
109 u8 *cmdbuf; /* Command buffer */
111 unsigned long flags; /* device flags */
112 unsigned long quirks; /* Various quirks */
114 wait_queue_head_t wait; /* For waiting the interrupt */
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) | I2C_M_DMA_SAFE;
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) |
192 I2C_M_RD | I2C_M_DMA_SAFE;
193 msgs[n].len = recv_len;
194 msgs[n].buf = recv_buf;
197 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
200 ret = i2c_transfer(client->adapter, msgs, n);
203 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
206 return ret < 0 ? ret : -EIO;
211 static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
212 void *buf, size_t len)
214 *(__le16 *)ihid->cmdbuf = reg;
216 return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
219 static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
220 int report_type, int report_id)
224 if (report_id < 0x0F) {
225 buf[length++] = report_type << 4 | report_id;
226 buf[length++] = opcode;
228 buf[length++] = report_type << 4 | 0x0F;
229 buf[length++] = opcode;
230 buf[length++] = report_id;
236 static int i2c_hid_get_report(struct i2c_hid *ihid,
237 u8 report_type, u8 report_id,
238 u8 *recv_buf, size_t recv_len)
244 i2c_hid_dbg(ihid, "%s\n", __func__);
246 /* Command register goes first */
247 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
248 length += sizeof(__le16);
249 /* Next is GET_REPORT command */
250 length += i2c_hid_encode_command(ihid->cmdbuf + length,
251 I2C_HID_OPCODE_GET_REPORT,
252 report_type, report_id);
254 * Device will send report data through data register. Because
255 * command can be either 2 or 3 bytes destination for the data
256 * register may be not aligned.
258 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
259 ihid->cmdbuf + length);
260 length += sizeof(__le16);
263 * In addition to report data device will supply data length
264 * in the first 2 bytes of the response, so adjust .
266 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
267 ihid->rawbuf, recv_len + sizeof(__le16));
269 dev_err(&ihid->client->dev,
270 "failed to set a report to device: %d\n", error);
274 /* The buffer is sufficiently aligned */
275 ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
277 /* Check for empty report response */
278 if (ret_count <= sizeof(__le16))
281 recv_len = min(recv_len, ret_count - sizeof(__le16));
282 memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
284 if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
285 dev_err(&ihid->client->dev,
286 "device returned incorrect report (%d vs %d expected)\n",
287 recv_buf[0], report_id);
294 static size_t i2c_hid_format_report(u8 *buf, int report_id,
295 const u8 *data, size_t size)
297 size_t length = sizeof(__le16); /* reserve space to store size */
300 buf[length++] = report_id;
302 memcpy(buf + length, data, size);
305 /* Store overall size in the beginning of the buffer */
306 put_unaligned_le16(length, buf);
312 * i2c_hid_set_or_send_report: forward an incoming report to the device
313 * @ihid: the i2c hid device
314 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
315 * @report_id: the report ID
316 * @buf: the actual data to transfer, without the report ID
317 * @data_len: size of buf
318 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
320 static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
321 u8 report_type, u8 report_id,
322 const u8 *buf, size_t data_len,
328 i2c_hid_dbg(ihid, "%s\n", __func__);
330 if (data_len > ihid->bufsize)
333 if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
337 /* Command register goes first */
338 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
339 length += sizeof(__le16);
340 /* Next is SET_REPORT command */
341 length += i2c_hid_encode_command(ihid->cmdbuf + length,
342 I2C_HID_OPCODE_SET_REPORT,
343 report_type, report_id);
345 * Report data will go into the data register. Because
346 * command can be either 2 or 3 bytes destination for
347 * the data register may be not aligned.
349 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
350 ihid->cmdbuf + length);
351 length += sizeof(__le16);
354 * With simple "send report" all data goes into the output
357 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
358 length += sizeof(__le16);
361 length += i2c_hid_format_report(ihid->cmdbuf + length,
362 report_id, buf, data_len);
364 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
366 dev_err(&ihid->client->dev,
367 "failed to set a report to device: %d\n", error);
374 static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
378 /* SET_POWER uses command register */
379 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
380 length = sizeof(__le16);
382 /* Now the command itself */
383 length += i2c_hid_encode_command(ihid->cmdbuf + length,
384 I2C_HID_OPCODE_SET_POWER,
387 return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
390 static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
394 i2c_hid_dbg(ihid, "%s\n", __func__);
397 * Some devices require to send a command to wakeup before power on.
398 * The call will get a return value (EREMOTEIO) but device will be
399 * triggered and activated. After that, it goes like a normal device.
401 if (power_state == I2C_HID_PWR_ON &&
402 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
403 ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
405 /* Device was already activated */
410 ret = i2c_hid_set_power_command(ihid, power_state);
412 dev_err(&ihid->client->dev,
413 "failed to change power setting.\n");
418 * The HID over I2C specification states that if a DEVICE needs time
419 * after the PWR_ON request, it should utilise CLOCK stretching.
420 * However, it has been observered that the Windows driver provides a
421 * 1ms sleep between the PWR_ON and RESET requests.
422 * According to Goodix Windows even waits 60 ms after (other?)
423 * PWR_ON requests. Testing has confirmed that several devices
424 * will not work properly without a delay after a PWR_ON request.
426 if (!ret && power_state == I2C_HID_PWR_ON)
432 static int i2c_hid_execute_reset(struct i2c_hid *ihid)
437 i2c_hid_dbg(ihid, "resetting...\n");
439 /* Prepare reset command. Command register goes first. */
440 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
441 length += sizeof(__le16);
442 /* Next is RESET command itself */
443 length += i2c_hid_encode_command(ihid->cmdbuf + length,
444 I2C_HID_OPCODE_RESET, 0, 0);
446 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
448 ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
450 dev_err(&ihid->client->dev, "failed to reset device.\n");
454 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
459 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
460 if (!wait_event_timeout(ihid->wait,
461 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
462 msecs_to_jiffies(5000))) {
466 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
469 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
473 static int i2c_hid_hwreset(struct i2c_hid *ihid)
477 i2c_hid_dbg(ihid, "%s\n", __func__);
480 * This prevents sending feature reports while the device is
481 * being reset. Otherwise we may lose the reset complete
484 mutex_lock(&ihid->reset_lock);
486 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
490 ret = i2c_hid_execute_reset(ihid);
492 dev_err(&ihid->client->dev,
493 "failed to reset device: %d\n", ret);
494 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
498 /* At least some SIS devices need this after reset */
499 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
500 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
503 mutex_unlock(&ihid->reset_lock);
507 static void i2c_hid_get_input(struct i2c_hid *ihid)
509 u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
513 if (size > ihid->bufsize)
514 size = ihid->bufsize;
516 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
521 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
522 __func__, ret, size);
526 /* Receiving buffer is properly aligned */
527 ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
529 /* host or device initiated RESET completed */
530 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
531 wake_up(&ihid->wait);
535 if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
536 dev_warn_once(&ihid->client->dev,
537 "%s: IRQ triggered but there's no data\n",
542 if (ret_size > size || ret_size < sizeof(__le16)) {
543 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
544 *(__le16 *)ihid->inbuf = cpu_to_le16(size);
547 dev_err(&ihid->client->dev,
548 "%s: incomplete report (%d/%d)\n",
549 __func__, size, ret_size);
554 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
556 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
557 if (ihid->hid->group != HID_GROUP_RMI)
558 pm_wakeup_event(&ihid->client->dev, 0);
560 hid_input_report(ihid->hid, HID_INPUT_REPORT,
561 ihid->inbuf + sizeof(__le16),
562 ret_size - sizeof(__le16), 1);
568 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
570 struct i2c_hid *ihid = dev_id;
572 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
575 i2c_hid_get_input(ihid);
580 static int i2c_hid_get_report_length(struct hid_report *report)
582 return ((report->size - 1) >> 3) + 1 +
583 report->device->report_enum[report->type].numbered + 2;
587 * Traverse the supplied list of reports and find the longest
589 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
592 struct hid_report *report;
595 /* We should not rely on wMaxInputLength, as some devices may set it to
597 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
598 size = i2c_hid_get_report_length(report);
604 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
615 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
618 * The worst case is computed from the set_report command with a
619 * reportID > 15 and the maximum report length.
621 int cmd_len = sizeof(__le16) + /* command register */
622 sizeof(u8) + /* encoded report type/ID */
623 sizeof(u8) + /* opcode */
624 sizeof(u8) + /* optional 3rd byte report ID */
625 sizeof(__le16) + /* data register */
626 sizeof(__le16) + /* report data size */
627 sizeof(u8) + /* report ID if numbered report */
630 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
631 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
632 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
634 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
635 i2c_hid_free_buffers(ihid);
639 ihid->bufsize = report_size;
644 static int i2c_hid_get_raw_report(struct hid_device *hid,
645 u8 report_type, u8 report_id,
646 u8 *buf, size_t count)
648 struct i2c_client *client = hid->driver_data;
649 struct i2c_hid *ihid = i2c_get_clientdata(client);
652 if (report_type == HID_OUTPUT_REPORT)
656 * In case of unnumbered reports the response from the device will
657 * not have the report ID that the upper layers expect, so we need
658 * to stash it the buffer ourselves and adjust the data size.
666 ret_count = i2c_hid_get_report(ihid,
667 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
668 report_id, buf, count);
670 if (ret_count > 0 && !report_id)
676 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
677 const u8 *buf, size_t count, bool do_set)
679 struct i2c_client *client = hid->driver_data;
680 struct i2c_hid *ihid = i2c_get_clientdata(client);
681 int report_id = buf[0];
684 if (report_type == HID_INPUT_REPORT)
687 mutex_lock(&ihid->reset_lock);
690 * Note that both numbered and unnumbered reports passed here
691 * are supposed to have report ID stored in the 1st byte of the
692 * buffer, so we strip it off unconditionally before passing payload
693 * to i2c_hid_set_or_send_report which takes care of encoding
694 * everything properly.
696 ret = i2c_hid_set_or_send_report(ihid,
697 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
698 report_id, buf + 1, count - 1, do_set);
701 ret++; /* add report_id to the number of transferred bytes */
703 mutex_unlock(&ihid->reset_lock);
708 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
710 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
714 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
715 __u8 *buf, size_t len, unsigned char rtype,
719 case HID_REQ_GET_REPORT:
720 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
721 case HID_REQ_SET_REPORT:
722 if (buf[0] != reportnum)
724 return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
730 static int i2c_hid_parse(struct hid_device *hid)
732 struct i2c_client *client = hid->driver_data;
733 struct i2c_hid *ihid = i2c_get_clientdata(client);
734 struct i2c_hid_desc *hdesc = &ihid->hdesc;
741 i2c_hid_dbg(ihid, "entering %s\n", __func__);
743 rsize = le16_to_cpu(hdesc->wReportDescLength);
744 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
745 dbg_hid("weird size of report descriptor (%u)\n", rsize);
750 ret = i2c_hid_hwreset(ihid);
753 } while (tries-- > 0 && ret);
758 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
762 rdesc = use_override;
763 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
765 rdesc = kzalloc(rsize, GFP_KERNEL);
768 dbg_hid("couldn't allocate rdesc memory\n");
772 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
774 ret = i2c_hid_read_register(ihid,
775 ihid->hdesc.wReportDescRegister,
778 hid_err(hid, "reading report descriptor failed\n");
784 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
786 ret = hid_parse_report(hid, rdesc, rsize);
791 dbg_hid("parsing report descriptor failed\n");
798 static int i2c_hid_start(struct hid_device *hid)
800 struct i2c_client *client = hid->driver_data;
801 struct i2c_hid *ihid = i2c_get_clientdata(client);
803 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
805 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
806 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
807 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
809 if (bufsize > ihid->bufsize) {
810 disable_irq(client->irq);
811 i2c_hid_free_buffers(ihid);
813 ret = i2c_hid_alloc_buffers(ihid, bufsize);
814 enable_irq(client->irq);
823 static void i2c_hid_stop(struct hid_device *hid)
828 static int i2c_hid_open(struct hid_device *hid)
830 struct i2c_client *client = hid->driver_data;
831 struct i2c_hid *ihid = i2c_get_clientdata(client);
833 set_bit(I2C_HID_STARTED, &ihid->flags);
837 static void i2c_hid_close(struct hid_device *hid)
839 struct i2c_client *client = hid->driver_data;
840 struct i2c_hid *ihid = i2c_get_clientdata(client);
842 clear_bit(I2C_HID_STARTED, &ihid->flags);
845 struct hid_ll_driver i2c_hid_ll_driver = {
846 .parse = i2c_hid_parse,
847 .start = i2c_hid_start,
848 .stop = i2c_hid_stop,
849 .open = i2c_hid_open,
850 .close = i2c_hid_close,
851 .output_report = i2c_hid_output_report,
852 .raw_request = i2c_hid_raw_request,
854 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
856 static int i2c_hid_init_irq(struct i2c_client *client)
858 struct i2c_hid *ihid = i2c_get_clientdata(client);
859 unsigned long irqflags = 0;
862 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
864 if (!irq_get_trigger_type(client->irq))
865 irqflags = IRQF_TRIGGER_LOW;
867 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
868 irqflags | IRQF_ONESHOT, client->name, ihid);
870 dev_warn(&client->dev,
871 "Could not register for %s interrupt, irq = %d,"
873 client->name, client->irq, ret);
881 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
883 struct i2c_client *client = ihid->client;
884 struct i2c_hid_desc *hdesc = &ihid->hdesc;
888 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
889 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
890 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
892 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
894 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
895 error = i2c_hid_read_register(ihid,
896 ihid->wHIDDescRegister,
898 sizeof(ihid->hdesc));
900 dev_err(&ihid->client->dev,
901 "failed to fetch HID descriptor: %d\n",
907 /* Validate the length of HID descriptor, the 4 first bytes:
908 * bytes 0-1 -> length
909 * bytes 2-3 -> bcdVersion (has to be 1.00) */
910 /* check bcdVersion == 1.0 */
911 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
912 dev_err(&ihid->client->dev,
913 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
914 le16_to_cpu(hdesc->bcdVersion));
918 /* Descriptor length should be 30 bytes as per the specification */
919 dsize = le16_to_cpu(hdesc->wHIDDescLength);
920 if (dsize != sizeof(struct i2c_hid_desc)) {
921 dev_err(&ihid->client->dev,
922 "weird size of HID descriptor (%u)\n", dsize);
925 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
929 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
931 if (!ihid->ops->power_up)
934 return ihid->ops->power_up(ihid->ops);
937 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
939 if (!ihid->ops->power_down)
942 ihid->ops->power_down(ihid->ops);
945 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
947 if (!ihid->ops->shutdown_tail)
950 ihid->ops->shutdown_tail(ihid->ops);
953 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
954 u16 hid_descriptor_address, u32 quirks)
957 struct i2c_hid *ihid;
958 struct hid_device *hid;
960 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
963 dev_err(&client->dev,
964 "HID over i2c has not been provided an Int IRQ\n");
968 if (client->irq < 0) {
969 if (client->irq != -EPROBE_DEFER)
970 dev_err(&client->dev,
971 "HID over i2c doesn't have a valid IRQ\n");
975 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
981 ret = i2c_hid_core_power_up(ihid);
985 i2c_set_clientdata(client, ihid);
987 ihid->client = client;
989 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
991 init_waitqueue_head(&ihid->wait);
992 mutex_init(&ihid->reset_lock);
994 /* we need to allocate the command buffer without knowing the maximum
995 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
996 * real computation later. */
997 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1001 device_enable_async_suspend(&client->dev);
1003 /* Make sure there is something at this address */
1004 ret = i2c_smbus_read_byte(client);
1006 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1011 ret = i2c_hid_fetch_hid_descriptor(ihid);
1013 dev_err(&client->dev,
1014 "Failed to fetch the HID Descriptor\n");
1018 ret = i2c_hid_init_irq(client);
1022 hid = hid_allocate_device();
1030 hid->driver_data = client;
1031 hid->ll_driver = &i2c_hid_ll_driver;
1032 hid->dev.parent = &client->dev;
1034 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1035 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1036 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1038 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1039 client->name, (u16)hid->vendor, (u16)hid->product);
1040 strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1042 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1044 ret = hid_add_device(hid);
1047 hid_err(client, "can't add hid device: %d\n", ret);
1051 hid->quirks |= quirks;
1056 hid_destroy_device(hid);
1059 free_irq(client->irq, ihid);
1062 i2c_hid_core_power_down(ihid);
1063 i2c_hid_free_buffers(ihid);
1066 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1068 void i2c_hid_core_remove(struct i2c_client *client)
1070 struct i2c_hid *ihid = i2c_get_clientdata(client);
1071 struct hid_device *hid;
1074 hid_destroy_device(hid);
1076 free_irq(client->irq, ihid);
1079 i2c_hid_free_buffers(ihid);
1081 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;
1104 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1108 /* Save some power */
1109 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1111 disable_irq(client->irq);
1113 if (!device_may_wakeup(&client->dev))
1114 i2c_hid_core_power_down(ihid);
1119 static int i2c_hid_core_resume(struct device *dev)
1122 struct i2c_client *client = to_i2c_client(dev);
1123 struct i2c_hid *ihid = i2c_get_clientdata(client);
1124 struct hid_device *hid = ihid->hid;
1126 if (!device_may_wakeup(&client->dev))
1127 i2c_hid_core_power_up(ihid);
1129 enable_irq(client->irq);
1131 /* Instead of resetting device, simply powers the device on. This
1132 * solves "incomplete reports" on Raydium devices 2386:3118 and
1133 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1134 * data after a suspend/resume.
1136 * However some ALPS touchpads generate IRQ storm without reset, so
1137 * let's still reset them here.
1139 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1140 ret = i2c_hid_hwreset(ihid);
1142 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1147 return hid_driver_reset_resume(hid);
1151 const struct dev_pm_ops i2c_hid_core_pm = {
1152 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1154 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1156 MODULE_DESCRIPTION("HID over I2C core driver");
1157 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1158 MODULE_LICENSE("GPL");