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;
98 unsigned int registerIndex;
103 #define I2C_HID_CMD(opcode_) \
104 .opcode = opcode_, .length = 4, \
105 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
108 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01) };
109 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
110 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
113 * These definitions are not used here, but are defined by the spec.
114 * Keeping them here for documentation purposes.
116 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
117 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
118 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
119 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
122 /* The main device structure */
124 struct i2c_client *client; /* i2c client */
125 struct hid_device *hid; /* pointer to corresponding HID dev */
127 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
128 struct i2c_hid_desc hdesc; /* the HID Descriptor */
130 __le16 wHIDDescRegister; /* location of the i2c
131 * register of the HID
133 unsigned int bufsize; /* i2c buffer size */
134 u8 *inbuf; /* Input buffer */
135 u8 *rawbuf; /* Raw Input buffer */
136 u8 *cmdbuf; /* Command buffer */
138 unsigned long flags; /* device flags */
139 unsigned long quirks; /* Various quirks */
141 wait_queue_head_t wait; /* For waiting the interrupt */
143 bool irq_wake_enabled;
144 struct mutex reset_lock;
146 struct i2chid_ops *ops;
149 static const struct i2c_hid_quirks {
153 } i2c_hid_quirks[] = {
154 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
155 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
156 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
157 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
158 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
159 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
160 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
161 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
162 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
163 I2C_HID_QUIRK_RESET_ON_RESUME },
164 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
165 I2C_HID_QUIRK_RESET_ON_RESUME },
166 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
167 I2C_HID_QUIRK_BAD_INPUT_SIZE },
169 * Sending the wakeup after reset actually break ELAN touchscreen controller
171 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
172 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
173 I2C_HID_QUIRK_BOGUS_IRQ },
178 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
179 * @idVendor: the 16-bit vendor ID
180 * @idProduct: the 16-bit product ID
182 * Returns: a u32 quirks value.
184 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
189 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
190 if (i2c_hid_quirks[n].idVendor == idVendor &&
191 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
192 i2c_hid_quirks[n].idProduct == idProduct))
193 quirks = i2c_hid_quirks[n].quirks;
198 static int i2c_hid_xfer(struct i2c_hid *ihid,
199 u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
201 struct i2c_client *client = ihid->client;
202 struct i2c_msg msgs[2] = { 0 };
207 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
208 __func__, send_len, send_buf);
210 msgs[n].addr = client->addr;
211 msgs[n].flags = client->flags & I2C_M_TEN;
212 msgs[n].len = send_len;
213 msgs[n].buf = send_buf;
218 msgs[n].addr = client->addr;
219 msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_RD;
220 msgs[n].len = recv_len;
221 msgs[n].buf = recv_buf;
224 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
227 ret = i2c_transfer(client->adapter, msgs, n);
230 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
233 return ret < 0 ? ret : -EIO;
238 static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
239 void *buf, size_t len)
241 *(__le16 *)ihid->cmdbuf = reg;
243 return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
246 static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
247 int report_type, int report_id)
251 if (report_id < 0x0F) {
252 buf[length++] = report_type << 4 | report_id;
253 buf[length++] = opcode;
255 buf[length++] = report_type << 4 | 0x0F;
256 buf[length++] = opcode;
257 buf[length++] = report_id;
263 static int __i2c_hid_command(struct i2c_hid *ihid,
264 const struct i2c_hid_cmd *command, u8 reportID,
265 u8 reportType, u8 *args, int args_len,
266 unsigned char *buf_recv, int data_len)
268 int length = command->length;
269 unsigned int registerIndex = command->registerIndex;
271 ihid->cmdbuf[0] = ihid->hdesc_buffer[registerIndex];
272 ihid->cmdbuf[1] = ihid->hdesc_buffer[registerIndex + 1];
275 length = sizeof(__le16) + /* register */
276 i2c_hid_encode_command(ihid->cmdbuf + sizeof(__le16),
278 reportType, reportID);
281 memcpy(ihid->cmdbuf + length, args, args_len);
284 return i2c_hid_xfer(ihid, ihid->cmdbuf, length, buf_recv, data_len);
287 static int i2c_hid_command(struct i2c_hid *ihid,
288 const struct i2c_hid_cmd *command,
289 unsigned char *buf_recv, int data_len)
291 return __i2c_hid_command(ihid, command, 0, 0, NULL, 0,
295 static int i2c_hid_get_report(struct i2c_hid *ihid, u8 reportType,
296 u8 reportID, unsigned char *buf_recv, int data_len)
301 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
303 i2c_hid_dbg(ihid, "%s\n", __func__);
305 args[args_len++] = readRegister & 0xFF;
306 args[args_len++] = readRegister >> 8;
308 ret = __i2c_hid_command(ihid, &hid_get_report_cmd, reportID,
309 reportType, args, args_len, buf_recv, data_len);
311 dev_err(&ihid->client->dev,
312 "failed to retrieve report from device.\n");
319 static size_t i2c_hid_format_report(u8 *buf, int report_id,
320 const u8 *data, size_t size)
322 size_t length = sizeof(__le16); /* reserve space to store size */
325 buf[length++] = report_id;
327 memcpy(buf + length, data, size);
330 /* Store overall size in the beginning of the buffer */
331 put_unaligned_le16(length, buf);
337 * i2c_hid_set_or_send_report: forward an incoming report to the device
338 * @ihid: the i2c hid device
339 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
340 * @report_id: the report ID
341 * @buf: the actual data to transfer, without the report ID
342 * @data_len: size of buf
343 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
345 static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
346 u8 report_type, u8 report_id,
347 const u8 *buf, size_t data_len,
353 i2c_hid_dbg(ihid, "%s\n", __func__);
355 if (data_len > ihid->bufsize)
358 if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
362 /* Command register goes first */
363 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
364 length += sizeof(__le16);
365 /* Next is SET_REPORT command */
366 length += i2c_hid_encode_command(ihid->cmdbuf + length,
367 I2C_HID_OPCODE_SET_REPORT,
368 report_type, report_id);
370 * Report data will go into the data register. Because
371 * command can be either 2 or 3 bytes destination for
372 * the data register may be not aligned.
374 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
375 ihid->cmdbuf + length);
376 length += sizeof(__le16);
379 * With simple "send report" all data goes into the output
382 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;;
383 length += sizeof(__le16);
386 length += i2c_hid_format_report(ihid->cmdbuf + length,
387 report_id, buf, data_len);
389 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
391 dev_err(&ihid->client->dev,
392 "failed to set a report to device: %d\n", error);
399 static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
403 i2c_hid_dbg(ihid, "%s\n", __func__);
406 * Some devices require to send a command to wakeup before power on.
407 * The call will get a return value (EREMOTEIO) but device will be
408 * triggered and activated. After that, it goes like a normal device.
410 if (power_state == I2C_HID_PWR_ON &&
411 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
412 ret = i2c_hid_command(ihid, &hid_set_power_cmd, NULL, 0);
414 /* Device was already activated */
419 ret = __i2c_hid_command(ihid, &hid_set_power_cmd, power_state,
420 0, NULL, 0, NULL, 0);
422 dev_err(&ihid->client->dev,
423 "failed to change power setting.\n");
428 * The HID over I2C specification states that if a DEVICE needs time
429 * after the PWR_ON request, it should utilise CLOCK stretching.
430 * However, it has been observered that the Windows driver provides a
431 * 1ms sleep between the PWR_ON and RESET requests.
432 * According to Goodix Windows even waits 60 ms after (other?)
433 * PWR_ON requests. Testing has confirmed that several devices
434 * will not work properly without a delay after a PWR_ON request.
436 if (!ret && power_state == I2C_HID_PWR_ON)
442 static int i2c_hid_execute_reset(struct i2c_hid *ihid)
446 i2c_hid_dbg(ihid, "resetting...\n");
448 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
450 ret = i2c_hid_command(ihid, &hid_reset_cmd, NULL, 0);
452 dev_err(&ihid->client->dev, "failed to reset device.\n");
456 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
461 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
462 if (!wait_event_timeout(ihid->wait,
463 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
464 msecs_to_jiffies(5000))) {
468 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
471 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
475 static int i2c_hid_hwreset(struct i2c_hid *ihid)
479 i2c_hid_dbg(ihid, "%s\n", __func__);
482 * This prevents sending feature reports while the device is
483 * being reset. Otherwise we may lose the reset complete
486 mutex_lock(&ihid->reset_lock);
488 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
492 ret = i2c_hid_execute_reset(ihid);
494 dev_err(&ihid->client->dev,
495 "failed to reset device: %d\n", ret);
496 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
500 /* At least some SIS devices need this after reset */
501 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
502 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
505 mutex_unlock(&ihid->reset_lock);
509 static void i2c_hid_get_input(struct i2c_hid *ihid)
513 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
515 if (size > ihid->bufsize)
516 size = ihid->bufsize;
518 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
523 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
524 __func__, ret, size);
528 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
531 /* host or device initiated RESET completed */
532 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
533 wake_up(&ihid->wait);
537 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
538 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
539 "there's no data\n", __func__);
543 if ((ret_size > size) || (ret_size < 2)) {
544 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
545 ihid->inbuf[0] = size & 0xff;
546 ihid->inbuf[1] = size >> 8;
549 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
550 __func__, size, ret_size);
555 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
557 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
558 pm_wakeup_event(&ihid->client->dev, 0);
560 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
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 unsigned char report_number, __u8 *buf, size_t count,
645 unsigned char report_type)
647 struct i2c_client *client = hid->driver_data;
648 struct i2c_hid *ihid = i2c_get_clientdata(client);
649 size_t ret_count, ask_count;
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.
660 if (!report_number) {
666 /* +2 bytes to include the size of the reply in the query buffer */
667 ask_count = min(count + 2, (size_t)ihid->bufsize);
669 ret = i2c_hid_get_report(ihid,
670 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
671 report_number, ihid->rawbuf, ask_count);
676 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
681 ret_count = min(ret_count, ask_count);
683 /* The query buffer contains the size, dropping it in the reply */
684 count = min(count, ret_count - 2);
685 memcpy(buf, ihid->rawbuf + 2, count);
693 static int i2c_hid_output_raw_report(struct hid_device *hid,
694 const u8 *buf, size_t count,
695 u8 report_type, bool do_set)
697 struct i2c_client *client = hid->driver_data;
698 struct i2c_hid *ihid = i2c_get_clientdata(client);
699 int report_id = buf[0];
702 if (report_type == HID_INPUT_REPORT)
705 mutex_lock(&ihid->reset_lock);
708 * Note that both numbered and unnumbered reports passed here
709 * are supposed to have report ID stored in the 1st byte of the
710 * buffer, so we strip it off unconditionally before passing payload
711 * to i2c_hid_set_or_send_report which takes care of encoding
712 * everything properly.
714 ret = i2c_hid_set_or_send_report(ihid,
715 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
716 report_id, buf + 1, count - 1, do_set);
719 ret++; /* add report_id to the number of transferred bytes */
721 mutex_unlock(&ihid->reset_lock);
726 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
728 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
732 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
733 __u8 *buf, size_t len, unsigned char rtype,
737 case HID_REQ_GET_REPORT:
738 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
739 case HID_REQ_SET_REPORT:
740 if (buf[0] != reportnum)
742 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
748 static int i2c_hid_parse(struct hid_device *hid)
750 struct i2c_client *client = hid->driver_data;
751 struct i2c_hid *ihid = i2c_get_clientdata(client);
752 struct i2c_hid_desc *hdesc = &ihid->hdesc;
759 i2c_hid_dbg(ihid, "entering %s\n", __func__);
761 rsize = le16_to_cpu(hdesc->wReportDescLength);
762 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
763 dbg_hid("weird size of report descriptor (%u)\n", rsize);
768 ret = i2c_hid_hwreset(ihid);
771 } while (tries-- > 0 && ret);
776 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
780 rdesc = use_override;
781 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
783 rdesc = kzalloc(rsize, GFP_KERNEL);
786 dbg_hid("couldn't allocate rdesc memory\n");
790 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
792 ret = i2c_hid_read_register(ihid,
793 ihid->hdesc.wReportDescRegister,
796 hid_err(hid, "reading report descriptor failed\n");
802 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
804 ret = hid_parse_report(hid, rdesc, rsize);
809 dbg_hid("parsing report descriptor failed\n");
816 static int i2c_hid_start(struct hid_device *hid)
818 struct i2c_client *client = hid->driver_data;
819 struct i2c_hid *ihid = i2c_get_clientdata(client);
821 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
823 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
824 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
825 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
827 if (bufsize > ihid->bufsize) {
828 disable_irq(client->irq);
829 i2c_hid_free_buffers(ihid);
831 ret = i2c_hid_alloc_buffers(ihid, bufsize);
832 enable_irq(client->irq);
841 static void i2c_hid_stop(struct hid_device *hid)
846 static int i2c_hid_open(struct hid_device *hid)
848 struct i2c_client *client = hid->driver_data;
849 struct i2c_hid *ihid = i2c_get_clientdata(client);
851 set_bit(I2C_HID_STARTED, &ihid->flags);
855 static void i2c_hid_close(struct hid_device *hid)
857 struct i2c_client *client = hid->driver_data;
858 struct i2c_hid *ihid = i2c_get_clientdata(client);
860 clear_bit(I2C_HID_STARTED, &ihid->flags);
863 struct hid_ll_driver i2c_hid_ll_driver = {
864 .parse = i2c_hid_parse,
865 .start = i2c_hid_start,
866 .stop = i2c_hid_stop,
867 .open = i2c_hid_open,
868 .close = i2c_hid_close,
869 .output_report = i2c_hid_output_report,
870 .raw_request = i2c_hid_raw_request,
872 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
874 static int i2c_hid_init_irq(struct i2c_client *client)
876 struct i2c_hid *ihid = i2c_get_clientdata(client);
877 unsigned long irqflags = 0;
880 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
882 if (!irq_get_trigger_type(client->irq))
883 irqflags = IRQF_TRIGGER_LOW;
885 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
886 irqflags | IRQF_ONESHOT, client->name, ihid);
888 dev_warn(&client->dev,
889 "Could not register for %s interrupt, irq = %d,"
891 client->name, client->irq, ret);
899 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
901 struct i2c_client *client = ihid->client;
902 struct i2c_hid_desc *hdesc = &ihid->hdesc;
906 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
907 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
908 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
910 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
912 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
913 error = i2c_hid_read_register(ihid,
914 ihid->wHIDDescRegister,
916 sizeof(ihid->hdesc));
918 dev_err(&ihid->client->dev,
919 "failed to fetch HID descriptor: %d\n",
925 /* Validate the length of HID descriptor, the 4 first bytes:
926 * bytes 0-1 -> length
927 * bytes 2-3 -> bcdVersion (has to be 1.00) */
928 /* check bcdVersion == 1.0 */
929 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
930 dev_err(&ihid->client->dev,
931 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
932 le16_to_cpu(hdesc->bcdVersion));
936 /* Descriptor length should be 30 bytes as per the specification */
937 dsize = le16_to_cpu(hdesc->wHIDDescLength);
938 if (dsize != sizeof(struct i2c_hid_desc)) {
939 dev_err(&ihid->client->dev,
940 "weird size of HID descriptor (%u)\n", dsize);
943 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
947 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
949 if (!ihid->ops->power_up)
952 return ihid->ops->power_up(ihid->ops);
955 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
957 if (!ihid->ops->power_down)
960 ihid->ops->power_down(ihid->ops);
963 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
965 if (!ihid->ops->shutdown_tail)
968 ihid->ops->shutdown_tail(ihid->ops);
971 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
972 u16 hid_descriptor_address, u32 quirks)
975 struct i2c_hid *ihid;
976 struct hid_device *hid;
978 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
981 dev_err(&client->dev,
982 "HID over i2c has not been provided an Int IRQ\n");
986 if (client->irq < 0) {
987 if (client->irq != -EPROBE_DEFER)
988 dev_err(&client->dev,
989 "HID over i2c doesn't have a valid IRQ\n");
993 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
999 ret = i2c_hid_core_power_up(ihid);
1003 i2c_set_clientdata(client, ihid);
1005 ihid->client = client;
1007 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
1009 init_waitqueue_head(&ihid->wait);
1010 mutex_init(&ihid->reset_lock);
1012 /* we need to allocate the command buffer without knowing the maximum
1013 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1014 * real computation later. */
1015 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1019 device_enable_async_suspend(&client->dev);
1021 /* Make sure there is something at this address */
1022 ret = i2c_smbus_read_byte(client);
1024 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1029 ret = i2c_hid_fetch_hid_descriptor(ihid);
1031 dev_err(&client->dev,
1032 "Failed to fetch the HID Descriptor\n");
1036 ret = i2c_hid_init_irq(client);
1040 hid = hid_allocate_device();
1048 hid->driver_data = client;
1049 hid->ll_driver = &i2c_hid_ll_driver;
1050 hid->dev.parent = &client->dev;
1052 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1053 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1054 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1056 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1057 client->name, (u16)hid->vendor, (u16)hid->product);
1058 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1060 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1062 ret = hid_add_device(hid);
1065 hid_err(client, "can't add hid device: %d\n", ret);
1069 hid->quirks |= quirks;
1074 hid_destroy_device(hid);
1077 free_irq(client->irq, ihid);
1080 i2c_hid_core_power_down(ihid);
1081 i2c_hid_free_buffers(ihid);
1084 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1086 int i2c_hid_core_remove(struct i2c_client *client)
1088 struct i2c_hid *ihid = i2c_get_clientdata(client);
1089 struct hid_device *hid;
1092 hid_destroy_device(hid);
1094 free_irq(client->irq, ihid);
1097 i2c_hid_free_buffers(ihid);
1099 i2c_hid_core_power_down(ihid);
1103 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1105 void i2c_hid_core_shutdown(struct i2c_client *client)
1107 struct i2c_hid *ihid = i2c_get_clientdata(client);
1109 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1110 free_irq(client->irq, ihid);
1112 i2c_hid_core_shutdown_tail(ihid);
1114 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1116 #ifdef CONFIG_PM_SLEEP
1117 static int i2c_hid_core_suspend(struct device *dev)
1119 struct i2c_client *client = to_i2c_client(dev);
1120 struct i2c_hid *ihid = i2c_get_clientdata(client);
1121 struct hid_device *hid = ihid->hid;
1125 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1129 /* Save some power */
1130 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1132 disable_irq(client->irq);
1134 if (device_may_wakeup(&client->dev)) {
1135 wake_status = enable_irq_wake(client->irq);
1137 ihid->irq_wake_enabled = true;
1139 hid_warn(hid, "Failed to enable irq wake: %d\n",
1142 i2c_hid_core_power_down(ihid);
1148 static int i2c_hid_core_resume(struct device *dev)
1151 struct i2c_client *client = to_i2c_client(dev);
1152 struct i2c_hid *ihid = i2c_get_clientdata(client);
1153 struct hid_device *hid = ihid->hid;
1156 if (!device_may_wakeup(&client->dev)) {
1157 i2c_hid_core_power_up(ihid);
1158 } else if (ihid->irq_wake_enabled) {
1159 wake_status = disable_irq_wake(client->irq);
1161 ihid->irq_wake_enabled = false;
1163 hid_warn(hid, "Failed to disable irq wake: %d\n",
1167 enable_irq(client->irq);
1169 /* Instead of resetting device, simply powers the device on. This
1170 * solves "incomplete reports" on Raydium devices 2386:3118 and
1171 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1172 * data after a suspend/resume.
1174 * However some ALPS touchpads generate IRQ storm without reset, so
1175 * let's still reset them here.
1177 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1178 ret = i2c_hid_hwreset(ihid);
1180 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1185 return hid_driver_reset_resume(hid);
1189 const struct dev_pm_ops i2c_hid_core_pm = {
1190 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1192 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1194 MODULE_DESCRIPTION("HID over I2C core driver");
1195 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1196 MODULE_LICENSE("GPL");