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>
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) | 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 pm_wakeup_event(&ihid->client->dev, 0);
559 hid_input_report(ihid->hid, HID_INPUT_REPORT,
560 ihid->inbuf + sizeof(__le16),
561 ret_size - sizeof(__le16), 1);
567 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
569 struct i2c_hid *ihid = dev_id;
571 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
574 i2c_hid_get_input(ihid);
579 static int i2c_hid_get_report_length(struct hid_report *report)
581 return ((report->size - 1) >> 3) + 1 +
582 report->device->report_enum[report->type].numbered + 2;
586 * Traverse the supplied list of reports and find the longest
588 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
591 struct hid_report *report;
594 /* We should not rely on wMaxInputLength, as some devices may set it to
596 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
597 size = i2c_hid_get_report_length(report);
603 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
614 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
617 * The worst case is computed from the set_report command with a
618 * reportID > 15 and the maximum report length.
620 int cmd_len = sizeof(__le16) + /* command register */
621 sizeof(u8) + /* encoded report type/ID */
622 sizeof(u8) + /* opcode */
623 sizeof(u8) + /* optional 3rd byte report ID */
624 sizeof(__le16) + /* data register */
625 sizeof(__le16) + /* report data size */
626 sizeof(u8) + /* report ID if numbered report */
629 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
630 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
631 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
633 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
634 i2c_hid_free_buffers(ihid);
638 ihid->bufsize = report_size;
643 static int i2c_hid_get_raw_report(struct hid_device *hid,
644 u8 report_type, u8 report_id,
645 u8 *buf, size_t count)
647 struct i2c_client *client = hid->driver_data;
648 struct i2c_hid *ihid = i2c_get_clientdata(client);
651 if (report_type == HID_OUTPUT_REPORT)
655 * In case of unnumbered reports the response from the device will
656 * not have the report ID that the upper layers expect, so we need
657 * to stash it the buffer ourselves and adjust the data size.
665 ret_count = i2c_hid_get_report(ihid,
666 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
667 report_id, buf, count);
669 if (ret_count > 0 && !report_id)
675 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
676 const u8 *buf, size_t count, bool do_set)
678 struct i2c_client *client = hid->driver_data;
679 struct i2c_hid *ihid = i2c_get_clientdata(client);
680 int report_id = buf[0];
683 if (report_type == HID_INPUT_REPORT)
686 mutex_lock(&ihid->reset_lock);
689 * Note that both numbered and unnumbered reports passed here
690 * are supposed to have report ID stored in the 1st byte of the
691 * buffer, so we strip it off unconditionally before passing payload
692 * to i2c_hid_set_or_send_report which takes care of encoding
693 * everything properly.
695 ret = i2c_hid_set_or_send_report(ihid,
696 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
697 report_id, buf + 1, count - 1, do_set);
700 ret++; /* add report_id to the number of transferred bytes */
702 mutex_unlock(&ihid->reset_lock);
707 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
709 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
713 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
714 __u8 *buf, size_t len, unsigned char rtype,
718 case HID_REQ_GET_REPORT:
719 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
720 case HID_REQ_SET_REPORT:
721 if (buf[0] != reportnum)
723 return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
729 static int i2c_hid_parse(struct hid_device *hid)
731 struct i2c_client *client = hid->driver_data;
732 struct i2c_hid *ihid = i2c_get_clientdata(client);
733 struct i2c_hid_desc *hdesc = &ihid->hdesc;
740 i2c_hid_dbg(ihid, "entering %s\n", __func__);
742 rsize = le16_to_cpu(hdesc->wReportDescLength);
743 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
744 dbg_hid("weird size of report descriptor (%u)\n", rsize);
749 ret = i2c_hid_hwreset(ihid);
752 } while (tries-- > 0 && ret);
757 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
761 rdesc = use_override;
762 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
764 rdesc = kzalloc(rsize, GFP_KERNEL);
767 dbg_hid("couldn't allocate rdesc memory\n");
771 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
773 ret = i2c_hid_read_register(ihid,
774 ihid->hdesc.wReportDescRegister,
777 hid_err(hid, "reading report descriptor failed\n");
783 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
785 ret = hid_parse_report(hid, rdesc, rsize);
790 dbg_hid("parsing report descriptor failed\n");
797 static int i2c_hid_start(struct hid_device *hid)
799 struct i2c_client *client = hid->driver_data;
800 struct i2c_hid *ihid = i2c_get_clientdata(client);
802 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
804 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
805 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
806 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
808 if (bufsize > ihid->bufsize) {
809 disable_irq(client->irq);
810 i2c_hid_free_buffers(ihid);
812 ret = i2c_hid_alloc_buffers(ihid, bufsize);
813 enable_irq(client->irq);
822 static void i2c_hid_stop(struct hid_device *hid)
827 static int i2c_hid_open(struct hid_device *hid)
829 struct i2c_client *client = hid->driver_data;
830 struct i2c_hid *ihid = i2c_get_clientdata(client);
832 set_bit(I2C_HID_STARTED, &ihid->flags);
836 static void i2c_hid_close(struct hid_device *hid)
838 struct i2c_client *client = hid->driver_data;
839 struct i2c_hid *ihid = i2c_get_clientdata(client);
841 clear_bit(I2C_HID_STARTED, &ihid->flags);
844 struct hid_ll_driver i2c_hid_ll_driver = {
845 .parse = i2c_hid_parse,
846 .start = i2c_hid_start,
847 .stop = i2c_hid_stop,
848 .open = i2c_hid_open,
849 .close = i2c_hid_close,
850 .output_report = i2c_hid_output_report,
851 .raw_request = i2c_hid_raw_request,
853 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
855 static int i2c_hid_init_irq(struct i2c_client *client)
857 struct i2c_hid *ihid = i2c_get_clientdata(client);
858 unsigned long irqflags = 0;
861 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
863 if (!irq_get_trigger_type(client->irq))
864 irqflags = IRQF_TRIGGER_LOW;
866 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
867 irqflags | IRQF_ONESHOT, client->name, ihid);
869 dev_warn(&client->dev,
870 "Could not register for %s interrupt, irq = %d,"
872 client->name, client->irq, ret);
880 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
882 struct i2c_client *client = ihid->client;
883 struct i2c_hid_desc *hdesc = &ihid->hdesc;
887 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
888 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
889 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
891 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
893 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
894 error = i2c_hid_read_register(ihid,
895 ihid->wHIDDescRegister,
897 sizeof(ihid->hdesc));
899 dev_err(&ihid->client->dev,
900 "failed to fetch HID descriptor: %d\n",
906 /* Validate the length of HID descriptor, the 4 first bytes:
907 * bytes 0-1 -> length
908 * bytes 2-3 -> bcdVersion (has to be 1.00) */
909 /* check bcdVersion == 1.0 */
910 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
911 dev_err(&ihid->client->dev,
912 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
913 le16_to_cpu(hdesc->bcdVersion));
917 /* Descriptor length should be 30 bytes as per the specification */
918 dsize = le16_to_cpu(hdesc->wHIDDescLength);
919 if (dsize != sizeof(struct i2c_hid_desc)) {
920 dev_err(&ihid->client->dev,
921 "weird size of HID descriptor (%u)\n", dsize);
924 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
928 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
930 if (!ihid->ops->power_up)
933 return ihid->ops->power_up(ihid->ops);
936 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
938 if (!ihid->ops->power_down)
941 ihid->ops->power_down(ihid->ops);
944 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
946 if (!ihid->ops->shutdown_tail)
949 ihid->ops->shutdown_tail(ihid->ops);
952 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
953 u16 hid_descriptor_address, u32 quirks)
956 struct i2c_hid *ihid;
957 struct hid_device *hid;
959 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
962 dev_err(&client->dev,
963 "HID over i2c has not been provided an Int IRQ\n");
967 if (client->irq < 0) {
968 if (client->irq != -EPROBE_DEFER)
969 dev_err(&client->dev,
970 "HID over i2c doesn't have a valid IRQ\n");
974 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
980 ret = i2c_hid_core_power_up(ihid);
984 i2c_set_clientdata(client, ihid);
986 ihid->client = client;
988 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
990 init_waitqueue_head(&ihid->wait);
991 mutex_init(&ihid->reset_lock);
993 /* we need to allocate the command buffer without knowing the maximum
994 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
995 * real computation later. */
996 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1000 device_enable_async_suspend(&client->dev);
1002 /* Make sure there is something at this address */
1003 ret = i2c_smbus_read_byte(client);
1005 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1010 ret = i2c_hid_fetch_hid_descriptor(ihid);
1012 dev_err(&client->dev,
1013 "Failed to fetch the HID Descriptor\n");
1017 ret = i2c_hid_init_irq(client);
1021 hid = hid_allocate_device();
1029 hid->driver_data = client;
1030 hid->ll_driver = &i2c_hid_ll_driver;
1031 hid->dev.parent = &client->dev;
1033 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1034 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1035 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1037 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1038 client->name, (u16)hid->vendor, (u16)hid->product);
1039 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1041 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1043 ret = hid_add_device(hid);
1046 hid_err(client, "can't add hid device: %d\n", ret);
1050 hid->quirks |= quirks;
1055 hid_destroy_device(hid);
1058 free_irq(client->irq, ihid);
1061 i2c_hid_core_power_down(ihid);
1062 i2c_hid_free_buffers(ihid);
1065 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1067 int i2c_hid_core_remove(struct i2c_client *client)
1069 struct i2c_hid *ihid = i2c_get_clientdata(client);
1070 struct hid_device *hid;
1073 hid_destroy_device(hid);
1075 free_irq(client->irq, ihid);
1078 i2c_hid_free_buffers(ihid);
1080 i2c_hid_core_power_down(ihid);
1084 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1086 void i2c_hid_core_shutdown(struct i2c_client *client)
1088 struct i2c_hid *ihid = i2c_get_clientdata(client);
1090 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1091 free_irq(client->irq, ihid);
1093 i2c_hid_core_shutdown_tail(ihid);
1095 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1097 #ifdef CONFIG_PM_SLEEP
1098 static int i2c_hid_core_suspend(struct device *dev)
1100 struct i2c_client *client = to_i2c_client(dev);
1101 struct i2c_hid *ihid = i2c_get_clientdata(client);
1102 struct hid_device *hid = ihid->hid;
1106 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1110 /* Save some power */
1111 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1113 disable_irq(client->irq);
1115 if (device_may_wakeup(&client->dev)) {
1116 wake_status = enable_irq_wake(client->irq);
1118 ihid->irq_wake_enabled = true;
1120 hid_warn(hid, "Failed to enable irq wake: %d\n",
1123 i2c_hid_core_power_down(ihid);
1129 static int i2c_hid_core_resume(struct device *dev)
1132 struct i2c_client *client = to_i2c_client(dev);
1133 struct i2c_hid *ihid = i2c_get_clientdata(client);
1134 struct hid_device *hid = ihid->hid;
1137 if (!device_may_wakeup(&client->dev)) {
1138 i2c_hid_core_power_up(ihid);
1139 } else if (ihid->irq_wake_enabled) {
1140 wake_status = disable_irq_wake(client->irq);
1142 ihid->irq_wake_enabled = false;
1144 hid_warn(hid, "Failed to disable irq wake: %d\n",
1148 enable_irq(client->irq);
1150 /* Instead of resetting device, simply powers the device on. This
1151 * solves "incomplete reports" on Raydium devices 2386:3118 and
1152 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1153 * data after a suspend/resume.
1155 * However some ALPS touchpads generate IRQ storm without reset, so
1156 * let's still reset them here.
1158 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1159 ret = i2c_hid_hwreset(ihid);
1161 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1166 return hid_driver_reset_resume(hid);
1170 const struct dev_pm_ops i2c_hid_core_pm = {
1171 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1173 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1175 MODULE_DESCRIPTION("HID over I2C core driver");
1177 MODULE_LICENSE("GPL");