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
70 #define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__)
73 __le16 wHIDDescLength;
75 __le16 wReportDescLength;
76 __le16 wReportDescRegister;
77 __le16 wInputRegister;
78 __le16 wMaxInputLength;
79 __le16 wOutputRegister;
80 __le16 wMaxOutputLength;
81 __le16 wCommandRegister;
89 /* The main device structure */
91 struct i2c_client *client; /* i2c client */
92 struct hid_device *hid; /* pointer to corresponding HID dev */
93 struct i2c_hid_desc hdesc; /* the HID Descriptor */
94 __le16 wHIDDescRegister; /* location of the i2c
97 unsigned int bufsize; /* i2c buffer size */
98 u8 *inbuf; /* Input buffer */
99 u8 *rawbuf; /* Raw Input buffer */
100 u8 *cmdbuf; /* Command buffer */
102 unsigned long flags; /* device flags */
103 unsigned long quirks; /* Various quirks */
105 wait_queue_head_t wait; /* For waiting the interrupt */
107 struct mutex reset_lock;
109 struct i2chid_ops *ops;
112 static const struct i2c_hid_quirks {
116 } i2c_hid_quirks[] = {
117 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
118 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
119 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
120 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
121 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
122 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
123 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
124 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
125 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
126 I2C_HID_QUIRK_RESET_ON_RESUME },
127 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
128 I2C_HID_QUIRK_RESET_ON_RESUME },
129 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
130 I2C_HID_QUIRK_BAD_INPUT_SIZE },
132 * Sending the wakeup after reset actually break ELAN touchscreen controller
134 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
135 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
136 I2C_HID_QUIRK_BOGUS_IRQ },
141 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
142 * @idVendor: the 16-bit vendor ID
143 * @idProduct: the 16-bit product ID
145 * Returns: a u32 quirks value.
147 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
152 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
153 if (i2c_hid_quirks[n].idVendor == idVendor &&
154 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
155 i2c_hid_quirks[n].idProduct == idProduct))
156 quirks = i2c_hid_quirks[n].quirks;
161 static int i2c_hid_xfer(struct i2c_hid *ihid,
162 u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
164 struct i2c_client *client = ihid->client;
165 struct i2c_msg msgs[2] = { 0 };
170 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
171 __func__, send_len, send_buf);
173 msgs[n].addr = client->addr;
174 msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
175 msgs[n].len = send_len;
176 msgs[n].buf = send_buf;
181 msgs[n].addr = client->addr;
182 msgs[n].flags = (client->flags & I2C_M_TEN) |
183 I2C_M_RD | I2C_M_DMA_SAFE;
184 msgs[n].len = recv_len;
185 msgs[n].buf = recv_buf;
188 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
191 ret = i2c_transfer(client->adapter, msgs, n);
194 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
197 return ret < 0 ? ret : -EIO;
202 static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
203 void *buf, size_t len)
205 *(__le16 *)ihid->cmdbuf = reg;
207 return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
210 static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
211 int report_type, int report_id)
215 if (report_id < 0x0F) {
216 buf[length++] = report_type << 4 | report_id;
217 buf[length++] = opcode;
219 buf[length++] = report_type << 4 | 0x0F;
220 buf[length++] = opcode;
221 buf[length++] = report_id;
227 static int i2c_hid_get_report(struct i2c_hid *ihid,
228 u8 report_type, u8 report_id,
229 u8 *recv_buf, size_t recv_len)
235 i2c_hid_dbg(ihid, "%s\n", __func__);
237 /* Command register goes first */
238 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
239 length += sizeof(__le16);
240 /* Next is GET_REPORT command */
241 length += i2c_hid_encode_command(ihid->cmdbuf + length,
242 I2C_HID_OPCODE_GET_REPORT,
243 report_type, report_id);
245 * Device will send report data through data register. Because
246 * command can be either 2 or 3 bytes destination for the data
247 * register may be not aligned.
249 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
250 ihid->cmdbuf + length);
251 length += sizeof(__le16);
254 * In addition to report data device will supply data length
255 * in the first 2 bytes of the response, so adjust .
257 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
258 ihid->rawbuf, recv_len + sizeof(__le16));
260 dev_err(&ihid->client->dev,
261 "failed to set a report to device: %d\n", error);
265 /* The buffer is sufficiently aligned */
266 ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
268 /* Check for empty report response */
269 if (ret_count <= sizeof(__le16))
272 recv_len = min(recv_len, ret_count - sizeof(__le16));
273 memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
275 if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
276 dev_err(&ihid->client->dev,
277 "device returned incorrect report (%d vs %d expected)\n",
278 recv_buf[0], report_id);
285 static size_t i2c_hid_format_report(u8 *buf, int report_id,
286 const u8 *data, size_t size)
288 size_t length = sizeof(__le16); /* reserve space to store size */
291 buf[length++] = report_id;
293 memcpy(buf + length, data, size);
296 /* Store overall size in the beginning of the buffer */
297 put_unaligned_le16(length, buf);
303 * i2c_hid_set_or_send_report: forward an incoming report to the device
304 * @ihid: the i2c hid device
305 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
306 * @report_id: the report ID
307 * @buf: the actual data to transfer, without the report ID
308 * @data_len: size of buf
309 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
311 static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
312 u8 report_type, u8 report_id,
313 const u8 *buf, size_t data_len,
319 i2c_hid_dbg(ihid, "%s\n", __func__);
321 if (data_len > ihid->bufsize)
324 if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
328 /* Command register goes first */
329 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
330 length += sizeof(__le16);
331 /* Next is SET_REPORT command */
332 length += i2c_hid_encode_command(ihid->cmdbuf + length,
333 I2C_HID_OPCODE_SET_REPORT,
334 report_type, report_id);
336 * Report data will go into the data register. Because
337 * command can be either 2 or 3 bytes destination for
338 * the data register may be not aligned.
340 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
341 ihid->cmdbuf + length);
342 length += sizeof(__le16);
345 * With simple "send report" all data goes into the output
348 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
349 length += sizeof(__le16);
352 length += i2c_hid_format_report(ihid->cmdbuf + length,
353 report_id, buf, data_len);
355 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
357 dev_err(&ihid->client->dev,
358 "failed to set a report to device: %d\n", error);
365 static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
369 /* SET_POWER uses command register */
370 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
371 length = sizeof(__le16);
373 /* Now the command itself */
374 length += i2c_hid_encode_command(ihid->cmdbuf + length,
375 I2C_HID_OPCODE_SET_POWER,
378 return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
381 static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
385 i2c_hid_dbg(ihid, "%s\n", __func__);
388 * Some devices require to send a command to wakeup before power on.
389 * The call will get a return value (EREMOTEIO) but device will be
390 * triggered and activated. After that, it goes like a normal device.
392 if (power_state == I2C_HID_PWR_ON &&
393 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
394 ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
396 /* Device was already activated */
401 ret = i2c_hid_set_power_command(ihid, power_state);
403 dev_err(&ihid->client->dev,
404 "failed to change power setting.\n");
409 * The HID over I2C specification states that if a DEVICE needs time
410 * after the PWR_ON request, it should utilise CLOCK stretching.
411 * However, it has been observered that the Windows driver provides a
412 * 1ms sleep between the PWR_ON and RESET requests.
413 * According to Goodix Windows even waits 60 ms after (other?)
414 * PWR_ON requests. Testing has confirmed that several devices
415 * will not work properly without a delay after a PWR_ON request.
417 if (!ret && power_state == I2C_HID_PWR_ON)
423 static int i2c_hid_execute_reset(struct i2c_hid *ihid)
428 i2c_hid_dbg(ihid, "resetting...\n");
430 /* Prepare reset command. Command register goes first. */
431 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
432 length += sizeof(__le16);
433 /* Next is RESET command itself */
434 length += i2c_hid_encode_command(ihid->cmdbuf + length,
435 I2C_HID_OPCODE_RESET, 0, 0);
437 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
439 ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
441 dev_err(&ihid->client->dev, "failed to reset device.\n");
445 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
450 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
451 if (!wait_event_timeout(ihid->wait,
452 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
453 msecs_to_jiffies(5000))) {
457 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
460 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
464 static int i2c_hid_hwreset(struct i2c_hid *ihid)
468 i2c_hid_dbg(ihid, "%s\n", __func__);
471 * This prevents sending feature reports while the device is
472 * being reset. Otherwise we may lose the reset complete
475 mutex_lock(&ihid->reset_lock);
477 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
481 ret = i2c_hid_execute_reset(ihid);
483 dev_err(&ihid->client->dev,
484 "failed to reset device: %d\n", ret);
485 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
489 /* At least some SIS devices need this after reset */
490 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
491 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
494 mutex_unlock(&ihid->reset_lock);
498 static void i2c_hid_get_input(struct i2c_hid *ihid)
500 u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
504 if (size > ihid->bufsize)
505 size = ihid->bufsize;
507 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
512 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
513 __func__, ret, size);
517 /* Receiving buffer is properly aligned */
518 ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
520 /* host or device initiated RESET completed */
521 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
522 wake_up(&ihid->wait);
526 if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
527 dev_warn_once(&ihid->client->dev,
528 "%s: IRQ triggered but there's no data\n",
533 if (ret_size > size || ret_size < sizeof(__le16)) {
534 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
535 *(__le16 *)ihid->inbuf = cpu_to_le16(size);
538 dev_err(&ihid->client->dev,
539 "%s: incomplete report (%d/%d)\n",
540 __func__, size, ret_size);
545 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
547 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
548 if (ihid->hid->group != HID_GROUP_RMI)
549 pm_wakeup_event(&ihid->client->dev, 0);
551 hid_input_report(ihid->hid, HID_INPUT_REPORT,
552 ihid->inbuf + sizeof(__le16),
553 ret_size - sizeof(__le16), 1);
559 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
561 struct i2c_hid *ihid = dev_id;
563 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
566 i2c_hid_get_input(ihid);
571 static int i2c_hid_get_report_length(struct hid_report *report)
573 return ((report->size - 1) >> 3) + 1 +
574 report->device->report_enum[report->type].numbered + 2;
578 * Traverse the supplied list of reports and find the longest
580 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
583 struct hid_report *report;
586 /* We should not rely on wMaxInputLength, as some devices may set it to
588 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
589 size = i2c_hid_get_report_length(report);
595 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
606 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
609 * The worst case is computed from the set_report command with a
610 * reportID > 15 and the maximum report length.
612 int cmd_len = sizeof(__le16) + /* command register */
613 sizeof(u8) + /* encoded report type/ID */
614 sizeof(u8) + /* opcode */
615 sizeof(u8) + /* optional 3rd byte report ID */
616 sizeof(__le16) + /* data register */
617 sizeof(__le16) + /* report data size */
618 sizeof(u8) + /* report ID if numbered report */
621 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
622 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
623 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
625 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
626 i2c_hid_free_buffers(ihid);
630 ihid->bufsize = report_size;
635 static int i2c_hid_get_raw_report(struct hid_device *hid,
636 u8 report_type, u8 report_id,
637 u8 *buf, size_t count)
639 struct i2c_client *client = hid->driver_data;
640 struct i2c_hid *ihid = i2c_get_clientdata(client);
643 if (report_type == HID_OUTPUT_REPORT)
647 * In case of unnumbered reports the response from the device will
648 * not have the report ID that the upper layers expect, so we need
649 * to stash it the buffer ourselves and adjust the data size.
657 ret_count = i2c_hid_get_report(ihid,
658 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
659 report_id, buf, count);
661 if (ret_count > 0 && !report_id)
667 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
668 const u8 *buf, size_t count, bool do_set)
670 struct i2c_client *client = hid->driver_data;
671 struct i2c_hid *ihid = i2c_get_clientdata(client);
672 int report_id = buf[0];
675 if (report_type == HID_INPUT_REPORT)
678 mutex_lock(&ihid->reset_lock);
681 * Note that both numbered and unnumbered reports passed here
682 * are supposed to have report ID stored in the 1st byte of the
683 * buffer, so we strip it off unconditionally before passing payload
684 * to i2c_hid_set_or_send_report which takes care of encoding
685 * everything properly.
687 ret = i2c_hid_set_or_send_report(ihid,
688 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
689 report_id, buf + 1, count - 1, do_set);
692 ret++; /* add report_id to the number of transferred bytes */
694 mutex_unlock(&ihid->reset_lock);
699 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
701 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
705 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
706 __u8 *buf, size_t len, unsigned char rtype,
710 case HID_REQ_GET_REPORT:
711 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
712 case HID_REQ_SET_REPORT:
713 if (buf[0] != reportnum)
715 return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
721 static int i2c_hid_parse(struct hid_device *hid)
723 struct i2c_client *client = hid->driver_data;
724 struct i2c_hid *ihid = i2c_get_clientdata(client);
725 struct i2c_hid_desc *hdesc = &ihid->hdesc;
732 i2c_hid_dbg(ihid, "entering %s\n", __func__);
734 rsize = le16_to_cpu(hdesc->wReportDescLength);
735 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
736 dbg_hid("weird size of report descriptor (%u)\n", rsize);
741 ret = i2c_hid_hwreset(ihid);
744 } while (tries-- > 0 && ret);
749 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
753 rdesc = use_override;
754 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
756 rdesc = kzalloc(rsize, GFP_KERNEL);
759 dbg_hid("couldn't allocate rdesc memory\n");
763 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
765 ret = i2c_hid_read_register(ihid,
766 ihid->hdesc.wReportDescRegister,
769 hid_err(hid, "reading report descriptor failed\n");
775 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
777 ret = hid_parse_report(hid, rdesc, rsize);
782 dbg_hid("parsing report descriptor failed\n");
789 static int i2c_hid_start(struct hid_device *hid)
791 struct i2c_client *client = hid->driver_data;
792 struct i2c_hid *ihid = i2c_get_clientdata(client);
794 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
796 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
797 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
798 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
800 if (bufsize > ihid->bufsize) {
801 disable_irq(client->irq);
802 i2c_hid_free_buffers(ihid);
804 ret = i2c_hid_alloc_buffers(ihid, bufsize);
805 enable_irq(client->irq);
814 static void i2c_hid_stop(struct hid_device *hid)
819 static int i2c_hid_open(struct hid_device *hid)
821 struct i2c_client *client = hid->driver_data;
822 struct i2c_hid *ihid = i2c_get_clientdata(client);
824 set_bit(I2C_HID_STARTED, &ihid->flags);
828 static void i2c_hid_close(struct hid_device *hid)
830 struct i2c_client *client = hid->driver_data;
831 struct i2c_hid *ihid = i2c_get_clientdata(client);
833 clear_bit(I2C_HID_STARTED, &ihid->flags);
836 struct hid_ll_driver i2c_hid_ll_driver = {
837 .parse = i2c_hid_parse,
838 .start = i2c_hid_start,
839 .stop = i2c_hid_stop,
840 .open = i2c_hid_open,
841 .close = i2c_hid_close,
842 .output_report = i2c_hid_output_report,
843 .raw_request = i2c_hid_raw_request,
845 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
847 static int i2c_hid_init_irq(struct i2c_client *client)
849 struct i2c_hid *ihid = i2c_get_clientdata(client);
850 unsigned long irqflags = 0;
853 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
855 if (!irq_get_trigger_type(client->irq))
856 irqflags = IRQF_TRIGGER_LOW;
858 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
859 irqflags | IRQF_ONESHOT, client->name, ihid);
861 dev_warn(&client->dev,
862 "Could not register for %s interrupt, irq = %d,"
864 client->name, client->irq, ret);
872 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
874 struct i2c_client *client = ihid->client;
875 struct i2c_hid_desc *hdesc = &ihid->hdesc;
879 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
880 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
881 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
883 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
885 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
886 error = i2c_hid_read_register(ihid,
887 ihid->wHIDDescRegister,
889 sizeof(ihid->hdesc));
891 dev_err(&ihid->client->dev,
892 "failed to fetch HID descriptor: %d\n",
898 /* Validate the length of HID descriptor, the 4 first bytes:
899 * bytes 0-1 -> length
900 * bytes 2-3 -> bcdVersion (has to be 1.00) */
901 /* check bcdVersion == 1.0 */
902 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
903 dev_err(&ihid->client->dev,
904 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
905 le16_to_cpu(hdesc->bcdVersion));
909 /* Descriptor length should be 30 bytes as per the specification */
910 dsize = le16_to_cpu(hdesc->wHIDDescLength);
911 if (dsize != sizeof(struct i2c_hid_desc)) {
912 dev_err(&ihid->client->dev,
913 "weird size of HID descriptor (%u)\n", dsize);
916 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
920 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
922 if (!ihid->ops->power_up)
925 return ihid->ops->power_up(ihid->ops);
928 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
930 if (!ihid->ops->power_down)
933 ihid->ops->power_down(ihid->ops);
936 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
938 if (!ihid->ops->shutdown_tail)
941 ihid->ops->shutdown_tail(ihid->ops);
944 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
945 u16 hid_descriptor_address, u32 quirks)
948 struct i2c_hid *ihid;
949 struct hid_device *hid;
951 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
954 dev_err(&client->dev,
955 "HID over i2c has not been provided an Int IRQ\n");
959 if (client->irq < 0) {
960 if (client->irq != -EPROBE_DEFER)
961 dev_err(&client->dev,
962 "HID over i2c doesn't have a valid IRQ\n");
966 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
972 ret = i2c_hid_core_power_up(ihid);
976 i2c_set_clientdata(client, ihid);
978 ihid->client = client;
980 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
982 init_waitqueue_head(&ihid->wait);
983 mutex_init(&ihid->reset_lock);
985 /* we need to allocate the command buffer without knowing the maximum
986 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
987 * real computation later. */
988 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
992 device_enable_async_suspend(&client->dev);
994 /* Make sure there is something at this address */
995 ret = i2c_smbus_read_byte(client);
997 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1002 ret = i2c_hid_fetch_hid_descriptor(ihid);
1004 dev_err(&client->dev,
1005 "Failed to fetch the HID Descriptor\n");
1009 ret = i2c_hid_init_irq(client);
1013 hid = hid_allocate_device();
1021 hid->driver_data = client;
1022 hid->ll_driver = &i2c_hid_ll_driver;
1023 hid->dev.parent = &client->dev;
1025 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1026 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1027 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1029 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1030 client->name, (u16)hid->vendor, (u16)hid->product);
1031 strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1033 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1035 ret = hid_add_device(hid);
1038 hid_err(client, "can't add hid device: %d\n", ret);
1042 hid->quirks |= quirks;
1047 hid_destroy_device(hid);
1050 free_irq(client->irq, ihid);
1053 i2c_hid_core_power_down(ihid);
1054 i2c_hid_free_buffers(ihid);
1057 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1059 void i2c_hid_core_remove(struct i2c_client *client)
1061 struct i2c_hid *ihid = i2c_get_clientdata(client);
1062 struct hid_device *hid;
1065 hid_destroy_device(hid);
1067 free_irq(client->irq, ihid);
1070 i2c_hid_free_buffers(ihid);
1072 i2c_hid_core_power_down(ihid);
1074 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1076 void i2c_hid_core_shutdown(struct i2c_client *client)
1078 struct i2c_hid *ihid = i2c_get_clientdata(client);
1080 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1081 free_irq(client->irq, ihid);
1083 i2c_hid_core_shutdown_tail(ihid);
1085 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1087 #ifdef CONFIG_PM_SLEEP
1088 static int i2c_hid_core_suspend(struct device *dev)
1090 struct i2c_client *client = to_i2c_client(dev);
1091 struct i2c_hid *ihid = i2c_get_clientdata(client);
1092 struct hid_device *hid = ihid->hid;
1095 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1099 /* Save some power */
1100 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1102 disable_irq(client->irq);
1104 if (!device_may_wakeup(&client->dev))
1105 i2c_hid_core_power_down(ihid);
1110 static int i2c_hid_core_resume(struct device *dev)
1113 struct i2c_client *client = to_i2c_client(dev);
1114 struct i2c_hid *ihid = i2c_get_clientdata(client);
1115 struct hid_device *hid = ihid->hid;
1117 if (!device_may_wakeup(&client->dev))
1118 i2c_hid_core_power_up(ihid);
1120 enable_irq(client->irq);
1122 /* Instead of resetting device, simply powers the device on. This
1123 * solves "incomplete reports" on Raydium devices 2386:3118 and
1124 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1125 * data after a suspend/resume.
1127 * However some ALPS touchpads generate IRQ storm without reset, so
1128 * let's still reset them here.
1130 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1131 ret = i2c_hid_hwreset(ihid);
1133 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1138 return hid_driver_reset_resume(hid);
1142 const struct dev_pm_ops i2c_hid_core_pm = {
1143 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1145 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1147 MODULE_DESCRIPTION("HID over I2C core driver");
1148 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1149 MODULE_LICENSE("GPL");