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 <drm/drm_panel.h>
43 #include "../hid-ids.h"
46 /* quirks to control the device */
47 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
48 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
49 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
50 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
51 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
52 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7)
55 #define I2C_HID_OPCODE_RESET 0x01
56 #define I2C_HID_OPCODE_GET_REPORT 0x02
57 #define I2C_HID_OPCODE_SET_REPORT 0x03
58 #define I2C_HID_OPCODE_GET_IDLE 0x04
59 #define I2C_HID_OPCODE_SET_IDLE 0x05
60 #define I2C_HID_OPCODE_GET_PROTOCOL 0x06
61 #define I2C_HID_OPCODE_SET_PROTOCOL 0x07
62 #define I2C_HID_OPCODE_SET_POWER 0x08
65 #define I2C_HID_STARTED 0
66 #define I2C_HID_RESET_PENDING 1
67 #define I2C_HID_READ_PENDING 2
69 #define I2C_HID_PWR_ON 0x00
70 #define I2C_HID_PWR_SLEEP 0x01
72 #define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__)
75 __le16 wHIDDescLength;
77 __le16 wReportDescLength;
78 __le16 wReportDescRegister;
79 __le16 wInputRegister;
80 __le16 wMaxInputLength;
81 __le16 wOutputRegister;
82 __le16 wMaxOutputLength;
83 __le16 wCommandRegister;
91 /* The main device structure */
93 struct i2c_client *client; /* i2c client */
94 struct hid_device *hid; /* pointer to corresponding HID dev */
95 struct i2c_hid_desc hdesc; /* the HID Descriptor */
96 __le16 wHIDDescRegister; /* location of the i2c
99 unsigned int bufsize; /* i2c buffer size */
100 u8 *inbuf; /* Input buffer */
101 u8 *rawbuf; /* Raw Input buffer */
102 u8 *cmdbuf; /* Command buffer */
104 unsigned long flags; /* device flags */
105 unsigned long quirks; /* Various quirks */
107 wait_queue_head_t wait; /* For waiting the interrupt */
109 struct mutex reset_lock;
111 struct i2chid_ops *ops;
112 struct drm_panel_follower panel_follower;
113 struct work_struct panel_follower_prepare_work;
114 bool is_panel_follower;
115 bool prepare_work_finished;
118 static const struct i2c_hid_quirks {
122 } i2c_hid_quirks[] = {
123 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
124 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
125 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
126 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
127 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
128 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
129 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
130 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
131 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
132 I2C_HID_QUIRK_RESET_ON_RESUME },
133 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
134 I2C_HID_QUIRK_RESET_ON_RESUME },
135 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
136 I2C_HID_QUIRK_BAD_INPUT_SIZE },
138 * Sending the wakeup after reset actually break ELAN touchscreen controller
140 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
141 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
142 I2C_HID_QUIRK_BOGUS_IRQ },
147 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
148 * @idVendor: the 16-bit vendor ID
149 * @idProduct: the 16-bit product ID
151 * Returns: a u32 quirks value.
153 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
158 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
159 if (i2c_hid_quirks[n].idVendor == idVendor &&
160 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
161 i2c_hid_quirks[n].idProduct == idProduct))
162 quirks = i2c_hid_quirks[n].quirks;
167 static int i2c_hid_xfer(struct i2c_hid *ihid,
168 u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
170 struct i2c_client *client = ihid->client;
171 struct i2c_msg msgs[2] = { 0 };
176 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
177 __func__, send_len, send_buf);
179 msgs[n].addr = client->addr;
180 msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
181 msgs[n].len = send_len;
182 msgs[n].buf = send_buf;
187 msgs[n].addr = client->addr;
188 msgs[n].flags = (client->flags & I2C_M_TEN) |
189 I2C_M_RD | I2C_M_DMA_SAFE;
190 msgs[n].len = recv_len;
191 msgs[n].buf = recv_buf;
194 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
197 ret = i2c_transfer(client->adapter, msgs, n);
200 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
203 return ret < 0 ? ret : -EIO;
208 static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
209 void *buf, size_t len)
211 *(__le16 *)ihid->cmdbuf = reg;
213 return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
216 static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
217 int report_type, int report_id)
221 if (report_id < 0x0F) {
222 buf[length++] = report_type << 4 | report_id;
223 buf[length++] = opcode;
225 buf[length++] = report_type << 4 | 0x0F;
226 buf[length++] = opcode;
227 buf[length++] = report_id;
233 static int i2c_hid_get_report(struct i2c_hid *ihid,
234 u8 report_type, u8 report_id,
235 u8 *recv_buf, size_t recv_len)
241 i2c_hid_dbg(ihid, "%s\n", __func__);
243 /* Command register goes first */
244 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
245 length += sizeof(__le16);
246 /* Next is GET_REPORT command */
247 length += i2c_hid_encode_command(ihid->cmdbuf + length,
248 I2C_HID_OPCODE_GET_REPORT,
249 report_type, report_id);
251 * Device will send report data through data register. Because
252 * command can be either 2 or 3 bytes destination for the data
253 * register may be not aligned.
255 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
256 ihid->cmdbuf + length);
257 length += sizeof(__le16);
260 * In addition to report data device will supply data length
261 * in the first 2 bytes of the response, so adjust .
263 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
264 ihid->rawbuf, recv_len + sizeof(__le16));
266 dev_err(&ihid->client->dev,
267 "failed to set a report to device: %d\n", error);
271 /* The buffer is sufficiently aligned */
272 ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
274 /* Check for empty report response */
275 if (ret_count <= sizeof(__le16))
278 recv_len = min(recv_len, ret_count - sizeof(__le16));
279 memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
281 if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
282 dev_err(&ihid->client->dev,
283 "device returned incorrect report (%d vs %d expected)\n",
284 recv_buf[0], report_id);
291 static size_t i2c_hid_format_report(u8 *buf, int report_id,
292 const u8 *data, size_t size)
294 size_t length = sizeof(__le16); /* reserve space to store size */
297 buf[length++] = report_id;
299 memcpy(buf + length, data, size);
302 /* Store overall size in the beginning of the buffer */
303 put_unaligned_le16(length, buf);
309 * i2c_hid_set_or_send_report: forward an incoming report to the device
310 * @ihid: the i2c hid device
311 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
312 * @report_id: the report ID
313 * @buf: the actual data to transfer, without the report ID
314 * @data_len: size of buf
315 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
317 static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
318 u8 report_type, u8 report_id,
319 const u8 *buf, size_t data_len,
325 i2c_hid_dbg(ihid, "%s\n", __func__);
327 if (data_len > ihid->bufsize)
330 if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
334 /* Command register goes first */
335 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
336 length += sizeof(__le16);
337 /* Next is SET_REPORT command */
338 length += i2c_hid_encode_command(ihid->cmdbuf + length,
339 I2C_HID_OPCODE_SET_REPORT,
340 report_type, report_id);
342 * Report data will go into the data register. Because
343 * command can be either 2 or 3 bytes destination for
344 * the data register may be not aligned.
346 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
347 ihid->cmdbuf + length);
348 length += sizeof(__le16);
351 * With simple "send report" all data goes into the output
354 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
355 length += sizeof(__le16);
358 length += i2c_hid_format_report(ihid->cmdbuf + length,
359 report_id, buf, data_len);
361 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
363 dev_err(&ihid->client->dev,
364 "failed to set a report to device: %d\n", error);
371 static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
375 /* SET_POWER uses command register */
376 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
377 length = sizeof(__le16);
379 /* Now the command itself */
380 length += i2c_hid_encode_command(ihid->cmdbuf + length,
381 I2C_HID_OPCODE_SET_POWER,
384 return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
387 static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
391 i2c_hid_dbg(ihid, "%s\n", __func__);
394 * Some devices require to send a command to wakeup before power on.
395 * The call will get a return value (EREMOTEIO) but device will be
396 * triggered and activated. After that, it goes like a normal device.
398 if (power_state == I2C_HID_PWR_ON &&
399 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
400 ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
402 /* Device was already activated */
407 ret = i2c_hid_set_power_command(ihid, power_state);
409 dev_err(&ihid->client->dev,
410 "failed to change power setting.\n");
415 * The HID over I2C specification states that if a DEVICE needs time
416 * after the PWR_ON request, it should utilise CLOCK stretching.
417 * However, it has been observered that the Windows driver provides a
418 * 1ms sleep between the PWR_ON and RESET requests.
419 * According to Goodix Windows even waits 60 ms after (other?)
420 * PWR_ON requests. Testing has confirmed that several devices
421 * will not work properly without a delay after a PWR_ON request.
423 if (!ret && power_state == I2C_HID_PWR_ON)
429 static int i2c_hid_execute_reset(struct i2c_hid *ihid)
434 i2c_hid_dbg(ihid, "resetting...\n");
436 /* Prepare reset command. Command register goes first. */
437 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
438 length += sizeof(__le16);
439 /* Next is RESET command itself */
440 length += i2c_hid_encode_command(ihid->cmdbuf + length,
441 I2C_HID_OPCODE_RESET, 0, 0);
443 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
445 ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
447 dev_err(&ihid->client->dev, "failed to reset device.\n");
451 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
456 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
457 if (!wait_event_timeout(ihid->wait,
458 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
459 msecs_to_jiffies(5000))) {
463 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
466 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
470 static int i2c_hid_hwreset(struct i2c_hid *ihid)
474 i2c_hid_dbg(ihid, "%s\n", __func__);
477 * This prevents sending feature reports while the device is
478 * being reset. Otherwise we may lose the reset complete
481 mutex_lock(&ihid->reset_lock);
483 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
487 ret = i2c_hid_execute_reset(ihid);
489 dev_err(&ihid->client->dev,
490 "failed to reset device: %d\n", ret);
491 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
495 /* At least some SIS devices need this after reset */
496 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
497 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
500 mutex_unlock(&ihid->reset_lock);
504 static void i2c_hid_get_input(struct i2c_hid *ihid)
506 u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
510 if (size > ihid->bufsize)
511 size = ihid->bufsize;
513 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
518 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
519 __func__, ret, size);
523 /* Receiving buffer is properly aligned */
524 ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
526 /* host or device initiated RESET completed */
527 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
528 wake_up(&ihid->wait);
532 if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
533 dev_warn_once(&ihid->client->dev,
534 "%s: IRQ triggered but there's no data\n",
539 if (ret_size > size || ret_size < sizeof(__le16)) {
540 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
541 *(__le16 *)ihid->inbuf = cpu_to_le16(size);
544 dev_err(&ihid->client->dev,
545 "%s: incomplete report (%d/%d)\n",
546 __func__, size, ret_size);
551 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
553 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
554 if (ihid->hid->group != HID_GROUP_RMI)
555 pm_wakeup_event(&ihid->client->dev, 0);
557 hid_input_report(ihid->hid, HID_INPUT_REPORT,
558 ihid->inbuf + sizeof(__le16),
559 ret_size - sizeof(__le16), 1);
565 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
567 struct i2c_hid *ihid = dev_id;
569 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
572 i2c_hid_get_input(ihid);
577 static int i2c_hid_get_report_length(struct hid_report *report)
579 return ((report->size - 1) >> 3) + 1 +
580 report->device->report_enum[report->type].numbered + 2;
584 * Traverse the supplied list of reports and find the longest
586 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
589 struct hid_report *report;
592 /* We should not rely on wMaxInputLength, as some devices may set it to
594 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
595 size = i2c_hid_get_report_length(report);
601 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
612 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
615 * The worst case is computed from the set_report command with a
616 * reportID > 15 and the maximum report length.
618 int cmd_len = sizeof(__le16) + /* command register */
619 sizeof(u8) + /* encoded report type/ID */
620 sizeof(u8) + /* opcode */
621 sizeof(u8) + /* optional 3rd byte report ID */
622 sizeof(__le16) + /* data register */
623 sizeof(__le16) + /* report data size */
624 sizeof(u8) + /* report ID if numbered report */
627 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
628 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
629 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
631 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
632 i2c_hid_free_buffers(ihid);
636 ihid->bufsize = report_size;
641 static int i2c_hid_get_raw_report(struct hid_device *hid,
642 u8 report_type, u8 report_id,
643 u8 *buf, size_t count)
645 struct i2c_client *client = hid->driver_data;
646 struct i2c_hid *ihid = i2c_get_clientdata(client);
649 if (report_type == HID_OUTPUT_REPORT)
653 * In case of unnumbered reports the response from the device will
654 * not have the report ID that the upper layers expect, so we need
655 * to stash it the buffer ourselves and adjust the data size.
663 ret_count = i2c_hid_get_report(ihid,
664 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
665 report_id, buf, count);
667 if (ret_count > 0 && !report_id)
673 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
674 const u8 *buf, size_t count, bool do_set)
676 struct i2c_client *client = hid->driver_data;
677 struct i2c_hid *ihid = i2c_get_clientdata(client);
678 int report_id = buf[0];
681 if (report_type == HID_INPUT_REPORT)
684 mutex_lock(&ihid->reset_lock);
687 * Note that both numbered and unnumbered reports passed here
688 * are supposed to have report ID stored in the 1st byte of the
689 * buffer, so we strip it off unconditionally before passing payload
690 * to i2c_hid_set_or_send_report which takes care of encoding
691 * everything properly.
693 ret = i2c_hid_set_or_send_report(ihid,
694 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
695 report_id, buf + 1, count - 1, do_set);
698 ret++; /* add report_id to the number of transferred bytes */
700 mutex_unlock(&ihid->reset_lock);
705 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
707 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
711 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
712 __u8 *buf, size_t len, unsigned char rtype,
716 case HID_REQ_GET_REPORT:
717 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
718 case HID_REQ_SET_REPORT:
719 if (buf[0] != reportnum)
721 return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
727 static int i2c_hid_parse(struct hid_device *hid)
729 struct i2c_client *client = hid->driver_data;
730 struct i2c_hid *ihid = i2c_get_clientdata(client);
731 struct i2c_hid_desc *hdesc = &ihid->hdesc;
738 i2c_hid_dbg(ihid, "entering %s\n", __func__);
740 rsize = le16_to_cpu(hdesc->wReportDescLength);
741 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
742 dbg_hid("weird size of report descriptor (%u)\n", rsize);
747 ret = i2c_hid_hwreset(ihid);
750 } while (tries-- > 0 && ret);
755 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
759 rdesc = use_override;
760 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
762 rdesc = kzalloc(rsize, GFP_KERNEL);
765 dbg_hid("couldn't allocate rdesc memory\n");
769 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
771 ret = i2c_hid_read_register(ihid,
772 ihid->hdesc.wReportDescRegister,
775 hid_err(hid, "reading report descriptor failed\n");
781 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
783 ret = hid_parse_report(hid, rdesc, rsize);
788 dbg_hid("parsing report descriptor failed\n");
795 static int i2c_hid_start(struct hid_device *hid)
797 struct i2c_client *client = hid->driver_data;
798 struct i2c_hid *ihid = i2c_get_clientdata(client);
800 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
802 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
803 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
804 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
806 if (bufsize > ihid->bufsize) {
807 disable_irq(client->irq);
808 i2c_hid_free_buffers(ihid);
810 ret = i2c_hid_alloc_buffers(ihid, bufsize);
811 enable_irq(client->irq);
820 static void i2c_hid_stop(struct hid_device *hid)
825 static int i2c_hid_open(struct hid_device *hid)
827 struct i2c_client *client = hid->driver_data;
828 struct i2c_hid *ihid = i2c_get_clientdata(client);
830 set_bit(I2C_HID_STARTED, &ihid->flags);
834 static void i2c_hid_close(struct hid_device *hid)
836 struct i2c_client *client = hid->driver_data;
837 struct i2c_hid *ihid = i2c_get_clientdata(client);
839 clear_bit(I2C_HID_STARTED, &ihid->flags);
842 static const struct hid_ll_driver i2c_hid_ll_driver = {
843 .parse = i2c_hid_parse,
844 .start = i2c_hid_start,
845 .stop = i2c_hid_stop,
846 .open = i2c_hid_open,
847 .close = i2c_hid_close,
848 .output_report = i2c_hid_output_report,
849 .raw_request = i2c_hid_raw_request,
852 static int i2c_hid_init_irq(struct i2c_client *client)
854 struct i2c_hid *ihid = i2c_get_clientdata(client);
855 unsigned long irqflags = 0;
858 i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
860 if (!irq_get_trigger_type(client->irq))
861 irqflags = IRQF_TRIGGER_LOW;
863 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
864 irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN,
867 dev_warn(&client->dev,
868 "Could not register for %s interrupt, irq = %d,"
870 client->name, client->irq, ret);
878 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
880 struct i2c_client *client = ihid->client;
881 struct i2c_hid_desc *hdesc = &ihid->hdesc;
885 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
886 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
887 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
889 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
891 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
892 error = i2c_hid_read_register(ihid,
893 ihid->wHIDDescRegister,
895 sizeof(ihid->hdesc));
897 dev_err(&ihid->client->dev,
898 "failed to fetch HID descriptor: %d\n",
904 /* Validate the length of HID descriptor, the 4 first bytes:
905 * bytes 0-1 -> length
906 * bytes 2-3 -> bcdVersion (has to be 1.00) */
907 /* check bcdVersion == 1.0 */
908 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
909 dev_err(&ihid->client->dev,
910 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
911 le16_to_cpu(hdesc->bcdVersion));
915 /* Descriptor length should be 30 bytes as per the specification */
916 dsize = le16_to_cpu(hdesc->wHIDDescLength);
917 if (dsize != sizeof(struct i2c_hid_desc)) {
918 dev_err(&ihid->client->dev,
919 "weird size of HID descriptor (%u)\n", dsize);
922 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
926 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
928 if (!ihid->ops->power_up)
931 return ihid->ops->power_up(ihid->ops);
934 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
936 if (!ihid->ops->power_down)
939 ihid->ops->power_down(ihid->ops);
942 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
944 if (!ihid->ops->shutdown_tail)
947 ihid->ops->shutdown_tail(ihid->ops);
950 static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff)
952 struct i2c_client *client = ihid->client;
953 struct hid_device *hid = ihid->hid;
956 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
960 /* Save some power */
961 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
963 disable_irq(client->irq);
965 if (force_poweroff || !device_may_wakeup(&client->dev))
966 i2c_hid_core_power_down(ihid);
971 static int i2c_hid_core_resume(struct i2c_hid *ihid)
973 struct i2c_client *client = ihid->client;
974 struct hid_device *hid = ihid->hid;
977 if (!device_may_wakeup(&client->dev))
978 i2c_hid_core_power_up(ihid);
980 enable_irq(client->irq);
982 /* Instead of resetting device, simply powers the device on. This
983 * solves "incomplete reports" on Raydium devices 2386:3118 and
984 * 2386:4B33 and fixes various SIS touchscreens no longer sending
985 * data after a suspend/resume.
987 * However some ALPS touchpads generate IRQ storm without reset, so
988 * let's still reset them here.
990 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
991 ret = i2c_hid_hwreset(ihid);
993 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
998 return hid_driver_reset_resume(hid);
1002 * Check that the device exists and parse the HID descriptor.
1004 static int __i2c_hid_core_probe(struct i2c_hid *ihid)
1006 struct i2c_client *client = ihid->client;
1007 struct hid_device *hid = ihid->hid;
1010 /* Make sure there is something at this address */
1011 ret = i2c_smbus_read_byte(client);
1013 i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret);
1017 ret = i2c_hid_fetch_hid_descriptor(ihid);
1019 dev_err(&client->dev,
1020 "Failed to fetch the HID Descriptor\n");
1024 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1025 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1026 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1028 hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor,
1031 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1032 client->name, (u16)hid->vendor, (u16)hid->product);
1033 strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1035 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1040 static int i2c_hid_core_register_hid(struct i2c_hid *ihid)
1042 struct i2c_client *client = ihid->client;
1043 struct hid_device *hid = ihid->hid;
1046 enable_irq(client->irq);
1048 ret = hid_add_device(hid);
1051 hid_err(client, "can't add hid device: %d\n", ret);
1052 disable_irq(client->irq);
1059 static int i2c_hid_core_probe_panel_follower(struct i2c_hid *ihid)
1063 ret = i2c_hid_core_power_up(ihid);
1067 ret = __i2c_hid_core_probe(ihid);
1069 goto err_power_down;
1071 ret = i2c_hid_core_register_hid(ihid);
1073 goto err_power_down;
1078 i2c_hid_core_power_down(ihid);
1083 static void ihid_core_panel_prepare_work(struct work_struct *work)
1085 struct i2c_hid *ihid = container_of(work, struct i2c_hid,
1086 panel_follower_prepare_work);
1087 struct hid_device *hid = ihid->hid;
1091 * hid->version is set on the first power up. If it's still zero then
1092 * this is the first power on so we should perform initial power up
1096 ret = i2c_hid_core_probe_panel_follower(ihid);
1098 ret = i2c_hid_core_resume(ihid);
1101 dev_warn(&ihid->client->dev, "Power on failed: %d\n", ret);
1103 WRITE_ONCE(ihid->prepare_work_finished, true);
1106 * The work APIs provide a number of memory ordering guarantees
1107 * including one that says that memory writes before schedule_work()
1108 * are always visible to the work function, but they don't appear to
1109 * guarantee that a write that happened in the work is visible after
1110 * cancel_work_sync(). We'll add a write memory barrier here to match
1111 * with i2c_hid_core_panel_unpreparing() to ensure that our write to
1112 * prepare_work_finished is visible there.
1117 static int i2c_hid_core_panel_prepared(struct drm_panel_follower *follower)
1119 struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1122 * Powering on a touchscreen can be a slow process. Queue the work to
1123 * the system workqueue so we don't block the panel's power up.
1125 WRITE_ONCE(ihid->prepare_work_finished, false);
1126 schedule_work(&ihid->panel_follower_prepare_work);
1131 static int i2c_hid_core_panel_unpreparing(struct drm_panel_follower *follower)
1133 struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1135 cancel_work_sync(&ihid->panel_follower_prepare_work);
1137 /* Match with ihid_core_panel_prepare_work() */
1139 if (!READ_ONCE(ihid->prepare_work_finished))
1142 return i2c_hid_core_suspend(ihid, true);
1145 static const struct drm_panel_follower_funcs i2c_hid_core_panel_follower_funcs = {
1146 .panel_prepared = i2c_hid_core_panel_prepared,
1147 .panel_unpreparing = i2c_hid_core_panel_unpreparing,
1150 static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid)
1152 struct device *dev = &ihid->client->dev;
1155 ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_funcs;
1158 * If we're not in control of our own power up/power down then we can't
1159 * do the logic to manage wakeups. Give a warning if a user thought
1160 * that was possible then force the capability off.
1162 if (device_can_wakeup(dev)) {
1163 dev_warn(dev, "Can't wakeup if following panel\n");
1164 device_set_wakeup_capable(dev, false);
1167 ret = drm_panel_add_follower(dev, &ihid->panel_follower);
1174 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
1175 u16 hid_descriptor_address, u32 quirks)
1178 struct i2c_hid *ihid;
1179 struct hid_device *hid;
1181 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1184 dev_err(&client->dev,
1185 "HID over i2c has not been provided an Int IRQ\n");
1189 if (client->irq < 0) {
1190 if (client->irq != -EPROBE_DEFER)
1191 dev_err(&client->dev,
1192 "HID over i2c doesn't have a valid IRQ\n");
1196 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1200 i2c_set_clientdata(client, ihid);
1203 ihid->client = client;
1204 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
1205 ihid->is_panel_follower = drm_is_panel_follower(&client->dev);
1207 init_waitqueue_head(&ihid->wait);
1208 mutex_init(&ihid->reset_lock);
1209 INIT_WORK(&ihid->panel_follower_prepare_work, ihid_core_panel_prepare_work);
1211 /* we need to allocate the command buffer without knowing the maximum
1212 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1213 * real computation later. */
1214 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1217 device_enable_async_suspend(&client->dev);
1219 hid = hid_allocate_device();
1222 goto err_free_buffers;
1227 hid->driver_data = client;
1228 hid->ll_driver = &i2c_hid_ll_driver;
1229 hid->dev.parent = &client->dev;
1231 hid->initial_quirks = quirks;
1233 /* Power on and probe unless device is a panel follower. */
1234 if (!ihid->is_panel_follower) {
1235 ret = i2c_hid_core_power_up(ihid);
1237 goto err_destroy_device;
1239 ret = __i2c_hid_core_probe(ihid);
1241 goto err_power_down;
1244 ret = i2c_hid_init_irq(client);
1246 goto err_power_down;
1249 * If we're a panel follower, we'll register when the panel turns on;
1250 * otherwise we do it right away.
1252 if (ihid->is_panel_follower)
1253 ret = i2c_hid_core_register_panel_follower(ihid);
1255 ret = i2c_hid_core_register_hid(ihid);
1262 free_irq(client->irq, ihid);
1264 if (!ihid->is_panel_follower)
1265 i2c_hid_core_power_down(ihid);
1267 hid_destroy_device(hid);
1269 i2c_hid_free_buffers(ihid);
1273 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1275 void i2c_hid_core_remove(struct i2c_client *client)
1277 struct i2c_hid *ihid = i2c_get_clientdata(client);
1278 struct hid_device *hid;
1281 * If we're a follower, the act of unfollowing will cause us to be
1282 * powered down. Otherwise we need to manually do it.
1284 if (ihid->is_panel_follower)
1285 drm_panel_remove_follower(&ihid->panel_follower);
1287 i2c_hid_core_suspend(ihid, true);
1290 hid_destroy_device(hid);
1292 free_irq(client->irq, ihid);
1295 i2c_hid_free_buffers(ihid);
1297 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1299 void i2c_hid_core_shutdown(struct i2c_client *client)
1301 struct i2c_hid *ihid = i2c_get_clientdata(client);
1303 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1304 free_irq(client->irq, ihid);
1306 i2c_hid_core_shutdown_tail(ihid);
1308 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1310 static int i2c_hid_core_pm_suspend(struct device *dev)
1312 struct i2c_client *client = to_i2c_client(dev);
1313 struct i2c_hid *ihid = i2c_get_clientdata(client);
1315 if (ihid->is_panel_follower)
1318 return i2c_hid_core_suspend(ihid, false);
1321 static int i2c_hid_core_pm_resume(struct device *dev)
1323 struct i2c_client *client = to_i2c_client(dev);
1324 struct i2c_hid *ihid = i2c_get_clientdata(client);
1326 if (ihid->is_panel_follower)
1329 return i2c_hid_core_resume(ihid);
1332 const struct dev_pm_ops i2c_hid_core_pm = {
1333 SYSTEM_SLEEP_PM_OPS(i2c_hid_core_pm_suspend, i2c_hid_core_pm_resume)
1335 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1337 MODULE_DESCRIPTION("HID over I2C core driver");
1338 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1339 MODULE_LICENSE("GPL");