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_NO_IRQ_AFTER_RESET BIT(0)
48 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(1)
49 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(2)
50 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(3)
51 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(4)
52 #define I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND BIT(5)
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 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
124 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
125 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
126 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
127 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
128 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
129 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
130 I2C_HID_QUIRK_RESET_ON_RESUME },
131 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
132 I2C_HID_QUIRK_RESET_ON_RESUME },
133 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
134 I2C_HID_QUIRK_BAD_INPUT_SIZE },
135 { I2C_VENDOR_ID_CIRQUE, I2C_PRODUCT_ID_CIRQUE_1063,
136 I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND },
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 ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
401 /* Device was already activated */
406 ret = i2c_hid_set_power_command(ihid, power_state);
408 dev_err(&ihid->client->dev,
409 "failed to change power setting.\n");
414 * The HID over I2C specification states that if a DEVICE needs time
415 * after the PWR_ON request, it should utilise CLOCK stretching.
416 * However, it has been observered that the Windows driver provides a
417 * 1ms sleep between the PWR_ON and RESET requests.
418 * According to Goodix Windows even waits 60 ms after (other?)
419 * PWR_ON requests. Testing has confirmed that several devices
420 * will not work properly without a delay after a PWR_ON request.
422 if (!ret && power_state == I2C_HID_PWR_ON)
428 static int i2c_hid_start_hwreset(struct i2c_hid *ihid)
433 i2c_hid_dbg(ihid, "%s\n", __func__);
436 * This prevents sending feature reports while the device is
437 * being reset. Otherwise we may lose the reset complete
440 lockdep_assert_held(&ihid->reset_lock);
442 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
446 /* Prepare reset command. Command register goes first. */
447 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
448 length += sizeof(__le16);
449 /* Next is RESET command itself */
450 length += i2c_hid_encode_command(ihid->cmdbuf + length,
451 I2C_HID_OPCODE_RESET, 0, 0);
453 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
455 ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
457 dev_err(&ihid->client->dev,
458 "failed to reset device: %d\n", ret);
459 goto err_clear_reset;
465 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
466 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
470 static int i2c_hid_finish_hwreset(struct i2c_hid *ihid)
474 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
476 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
478 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
479 } else if (!wait_event_timeout(ihid->wait,
480 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
481 msecs_to_jiffies(1000))) {
482 dev_warn(&ihid->client->dev, "device did not ack reset within 1000 ms\n");
483 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
485 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
487 /* At least some SIS devices need this after reset */
488 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
489 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
494 static void i2c_hid_get_input(struct i2c_hid *ihid)
496 u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
500 if (size > ihid->bufsize)
501 size = ihid->bufsize;
503 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
508 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
509 __func__, ret, size);
513 /* Receiving buffer is properly aligned */
514 ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
516 /* host or device initiated RESET completed */
517 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
518 wake_up(&ihid->wait);
522 if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
523 dev_warn_once(&ihid->client->dev,
524 "%s: IRQ triggered but there's no data\n",
529 if (ret_size > size || ret_size < sizeof(__le16)) {
530 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
531 *(__le16 *)ihid->inbuf = cpu_to_le16(size);
534 dev_err(&ihid->client->dev,
535 "%s: incomplete report (%d/%d)\n",
536 __func__, size, ret_size);
541 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
543 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
544 if (ihid->hid->group != HID_GROUP_RMI)
545 pm_wakeup_event(&ihid->client->dev, 0);
547 hid_input_report(ihid->hid, HID_INPUT_REPORT,
548 ihid->inbuf + sizeof(__le16),
549 ret_size - sizeof(__le16), 1);
555 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
557 struct i2c_hid *ihid = dev_id;
559 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
562 i2c_hid_get_input(ihid);
567 static int i2c_hid_get_report_length(struct hid_report *report)
569 return ((report->size - 1) >> 3) + 1 +
570 report->device->report_enum[report->type].numbered + 2;
574 * Traverse the supplied list of reports and find the longest
576 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
579 struct hid_report *report;
582 /* We should not rely on wMaxInputLength, as some devices may set it to
584 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
585 size = i2c_hid_get_report_length(report);
591 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
602 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
605 * The worst case is computed from the set_report command with a
606 * reportID > 15 and the maximum report length.
608 int cmd_len = sizeof(__le16) + /* command register */
609 sizeof(u8) + /* encoded report type/ID */
610 sizeof(u8) + /* opcode */
611 sizeof(u8) + /* optional 3rd byte report ID */
612 sizeof(__le16) + /* data register */
613 sizeof(__le16) + /* report data size */
614 sizeof(u8) + /* report ID if numbered report */
617 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
618 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
619 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
621 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
622 i2c_hid_free_buffers(ihid);
626 ihid->bufsize = report_size;
631 static int i2c_hid_get_raw_report(struct hid_device *hid,
632 u8 report_type, u8 report_id,
633 u8 *buf, size_t count)
635 struct i2c_client *client = hid->driver_data;
636 struct i2c_hid *ihid = i2c_get_clientdata(client);
639 if (report_type == HID_OUTPUT_REPORT)
643 * In case of unnumbered reports the response from the device will
644 * not have the report ID that the upper layers expect, so we need
645 * to stash it the buffer ourselves and adjust the data size.
653 ret_count = i2c_hid_get_report(ihid,
654 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
655 report_id, buf, count);
657 if (ret_count > 0 && !report_id)
663 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
664 const u8 *buf, size_t count, bool do_set)
666 struct i2c_client *client = hid->driver_data;
667 struct i2c_hid *ihid = i2c_get_clientdata(client);
668 int report_id = buf[0];
671 if (report_type == HID_INPUT_REPORT)
674 mutex_lock(&ihid->reset_lock);
677 * Note that both numbered and unnumbered reports passed here
678 * are supposed to have report ID stored in the 1st byte of the
679 * buffer, so we strip it off unconditionally before passing payload
680 * to i2c_hid_set_or_send_report which takes care of encoding
681 * everything properly.
683 ret = i2c_hid_set_or_send_report(ihid,
684 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
685 report_id, buf + 1, count - 1, do_set);
688 ret++; /* add report_id to the number of transferred bytes */
690 mutex_unlock(&ihid->reset_lock);
695 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
697 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
701 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
702 __u8 *buf, size_t len, unsigned char rtype,
706 case HID_REQ_GET_REPORT:
707 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
708 case HID_REQ_SET_REPORT:
709 if (buf[0] != reportnum)
711 return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
717 static int i2c_hid_parse(struct hid_device *hid)
719 struct i2c_client *client = hid->driver_data;
720 struct i2c_hid *ihid = i2c_get_clientdata(client);
721 struct i2c_hid_desc *hdesc = &ihid->hdesc;
722 char *rdesc = NULL, *use_override = NULL;
727 i2c_hid_dbg(ihid, "entering %s\n", __func__);
729 rsize = le16_to_cpu(hdesc->wReportDescLength);
730 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
731 dbg_hid("weird size of report descriptor (%u)\n", rsize);
735 mutex_lock(&ihid->reset_lock);
737 ret = i2c_hid_start_hwreset(ihid);
740 } while (tries-- > 0 && ret);
745 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
749 rdesc = use_override;
750 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
752 rdesc = kzalloc(rsize, GFP_KERNEL);
759 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
761 ret = i2c_hid_read_register(ihid,
762 ihid->hdesc.wReportDescRegister,
765 hid_err(hid, "reading report descriptor failed\n");
771 * Windows directly reads the report-descriptor after sending reset
772 * and then waits for resets completion afterwards. Some touchpads
773 * actually wait for the report-descriptor to be read before signalling
776 ret = i2c_hid_finish_hwreset(ihid);
778 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
779 mutex_unlock(&ihid->reset_lock);
783 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
785 ret = hid_parse_report(hid, rdesc, rsize);
787 dbg_hid("parsing report descriptor failed\n");
796 static int i2c_hid_start(struct hid_device *hid)
798 struct i2c_client *client = hid->driver_data;
799 struct i2c_hid *ihid = i2c_get_clientdata(client);
801 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
803 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
804 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
805 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
807 if (bufsize > ihid->bufsize) {
808 disable_irq(client->irq);
809 i2c_hid_free_buffers(ihid);
811 ret = i2c_hid_alloc_buffers(ihid, bufsize);
812 enable_irq(client->irq);
821 static void i2c_hid_stop(struct hid_device *hid)
826 static int i2c_hid_open(struct hid_device *hid)
828 struct i2c_client *client = hid->driver_data;
829 struct i2c_hid *ihid = i2c_get_clientdata(client);
831 set_bit(I2C_HID_STARTED, &ihid->flags);
835 static void i2c_hid_close(struct hid_device *hid)
837 struct i2c_client *client = hid->driver_data;
838 struct i2c_hid *ihid = i2c_get_clientdata(client);
840 clear_bit(I2C_HID_STARTED, &ihid->flags);
843 static const struct hid_ll_driver i2c_hid_ll_driver = {
844 .parse = i2c_hid_parse,
845 .start = i2c_hid_start,
846 .stop = i2c_hid_stop,
847 .open = i2c_hid_open,
848 .close = i2c_hid_close,
849 .output_report = i2c_hid_output_report,
850 .raw_request = i2c_hid_raw_request,
853 static int i2c_hid_init_irq(struct i2c_client *client)
855 struct i2c_hid *ihid = i2c_get_clientdata(client);
856 unsigned long irqflags = 0;
859 i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
861 if (!irq_get_trigger_type(client->irq))
862 irqflags = IRQF_TRIGGER_LOW;
864 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
865 irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN,
868 dev_warn(&client->dev,
869 "Could not register for %s interrupt, irq = %d,"
871 client->name, client->irq, ret);
879 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
881 struct i2c_client *client = ihid->client;
882 struct i2c_hid_desc *hdesc = &ihid->hdesc;
886 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
887 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
888 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
890 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
892 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
893 error = i2c_hid_read_register(ihid,
894 ihid->wHIDDescRegister,
896 sizeof(ihid->hdesc));
898 dev_err(&ihid->client->dev,
899 "failed to fetch HID descriptor: %d\n",
905 /* Validate the length of HID descriptor, the 4 first bytes:
906 * bytes 0-1 -> length
907 * bytes 2-3 -> bcdVersion (has to be 1.00) */
908 /* check bcdVersion == 1.0 */
909 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
910 dev_err(&ihid->client->dev,
911 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
912 le16_to_cpu(hdesc->bcdVersion));
916 /* Descriptor length should be 30 bytes as per the specification */
917 dsize = le16_to_cpu(hdesc->wHIDDescLength);
918 if (dsize != sizeof(struct i2c_hid_desc)) {
919 dev_err(&ihid->client->dev,
920 "weird size of HID descriptor (%u)\n", dsize);
923 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
927 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
929 if (!ihid->ops->power_up)
932 return ihid->ops->power_up(ihid->ops);
935 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
937 if (!ihid->ops->power_down)
940 ihid->ops->power_down(ihid->ops);
943 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
945 if (!ihid->ops->shutdown_tail)
948 ihid->ops->shutdown_tail(ihid->ops);
951 static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff)
953 struct i2c_client *client = ihid->client;
954 struct hid_device *hid = ihid->hid;
957 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
961 /* Save some power */
962 if (!(ihid->quirks & I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND))
963 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
965 disable_irq(client->irq);
967 if (force_poweroff || !device_may_wakeup(&client->dev))
968 i2c_hid_core_power_down(ihid);
973 static int i2c_hid_core_resume(struct i2c_hid *ihid)
975 struct i2c_client *client = ihid->client;
976 struct hid_device *hid = ihid->hid;
979 if (!device_may_wakeup(&client->dev))
980 i2c_hid_core_power_up(ihid);
982 enable_irq(client->irq);
984 /* Instead of resetting device, simply powers the device on. This
985 * solves "incomplete reports" on Raydium devices 2386:3118 and
986 * 2386:4B33 and fixes various SIS touchscreens no longer sending
987 * data after a suspend/resume.
989 * However some ALPS touchpads generate IRQ storm without reset, so
990 * let's still reset them here.
992 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) {
993 mutex_lock(&ihid->reset_lock);
994 ret = i2c_hid_start_hwreset(ihid);
996 ret = i2c_hid_finish_hwreset(ihid);
997 mutex_unlock(&ihid->reset_lock);
999 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1005 return hid_driver_reset_resume(hid);
1009 * Check that the device exists and parse the HID descriptor.
1011 static int __i2c_hid_core_probe(struct i2c_hid *ihid)
1013 struct i2c_client *client = ihid->client;
1014 struct hid_device *hid = ihid->hid;
1017 /* Make sure there is something at this address */
1018 ret = i2c_smbus_read_byte(client);
1020 i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret);
1024 ret = i2c_hid_fetch_hid_descriptor(ihid);
1026 dev_err(&client->dev,
1027 "Failed to fetch the HID Descriptor\n");
1031 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1032 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1033 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1035 hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor,
1038 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1039 client->name, (u16)hid->vendor, (u16)hid->product);
1040 strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1042 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1047 static int i2c_hid_core_register_hid(struct i2c_hid *ihid)
1049 struct i2c_client *client = ihid->client;
1050 struct hid_device *hid = ihid->hid;
1053 enable_irq(client->irq);
1055 ret = hid_add_device(hid);
1058 hid_err(client, "can't add hid device: %d\n", ret);
1059 disable_irq(client->irq);
1066 static int i2c_hid_core_probe_panel_follower(struct i2c_hid *ihid)
1070 ret = i2c_hid_core_power_up(ihid);
1074 ret = __i2c_hid_core_probe(ihid);
1076 goto err_power_down;
1078 ret = i2c_hid_core_register_hid(ihid);
1080 goto err_power_down;
1085 i2c_hid_core_power_down(ihid);
1090 static void ihid_core_panel_prepare_work(struct work_struct *work)
1092 struct i2c_hid *ihid = container_of(work, struct i2c_hid,
1093 panel_follower_prepare_work);
1094 struct hid_device *hid = ihid->hid;
1098 * hid->version is set on the first power up. If it's still zero then
1099 * this is the first power on so we should perform initial power up
1103 ret = i2c_hid_core_probe_panel_follower(ihid);
1105 ret = i2c_hid_core_resume(ihid);
1108 dev_warn(&ihid->client->dev, "Power on failed: %d\n", ret);
1110 WRITE_ONCE(ihid->prepare_work_finished, true);
1113 * The work APIs provide a number of memory ordering guarantees
1114 * including one that says that memory writes before schedule_work()
1115 * are always visible to the work function, but they don't appear to
1116 * guarantee that a write that happened in the work is visible after
1117 * cancel_work_sync(). We'll add a write memory barrier here to match
1118 * with i2c_hid_core_panel_unpreparing() to ensure that our write to
1119 * prepare_work_finished is visible there.
1124 static int i2c_hid_core_panel_prepared(struct drm_panel_follower *follower)
1126 struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1129 * Powering on a touchscreen can be a slow process. Queue the work to
1130 * the system workqueue so we don't block the panel's power up.
1132 WRITE_ONCE(ihid->prepare_work_finished, false);
1133 schedule_work(&ihid->panel_follower_prepare_work);
1138 static int i2c_hid_core_panel_unpreparing(struct drm_panel_follower *follower)
1140 struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1142 cancel_work_sync(&ihid->panel_follower_prepare_work);
1144 /* Match with ihid_core_panel_prepare_work() */
1146 if (!READ_ONCE(ihid->prepare_work_finished))
1149 return i2c_hid_core_suspend(ihid, true);
1152 static const struct drm_panel_follower_funcs i2c_hid_core_panel_follower_funcs = {
1153 .panel_prepared = i2c_hid_core_panel_prepared,
1154 .panel_unpreparing = i2c_hid_core_panel_unpreparing,
1157 static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid)
1159 struct device *dev = &ihid->client->dev;
1162 ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_funcs;
1165 * If we're not in control of our own power up/power down then we can't
1166 * do the logic to manage wakeups. Give a warning if a user thought
1167 * that was possible then force the capability off.
1169 if (device_can_wakeup(dev)) {
1170 dev_warn(dev, "Can't wakeup if following panel\n");
1171 device_set_wakeup_capable(dev, false);
1174 ret = drm_panel_add_follower(dev, &ihid->panel_follower);
1181 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
1182 u16 hid_descriptor_address, u32 quirks)
1185 struct i2c_hid *ihid;
1186 struct hid_device *hid;
1188 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1191 dev_err(&client->dev,
1192 "HID over i2c has not been provided an Int IRQ\n");
1196 if (client->irq < 0) {
1197 if (client->irq != -EPROBE_DEFER)
1198 dev_err(&client->dev,
1199 "HID over i2c doesn't have a valid IRQ\n");
1203 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1207 i2c_set_clientdata(client, ihid);
1210 ihid->client = client;
1211 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
1212 ihid->is_panel_follower = drm_is_panel_follower(&client->dev);
1214 init_waitqueue_head(&ihid->wait);
1215 mutex_init(&ihid->reset_lock);
1216 INIT_WORK(&ihid->panel_follower_prepare_work, ihid_core_panel_prepare_work);
1218 /* we need to allocate the command buffer without knowing the maximum
1219 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1220 * real computation later. */
1221 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1224 device_enable_async_suspend(&client->dev);
1226 hid = hid_allocate_device();
1229 goto err_free_buffers;
1234 hid->driver_data = client;
1235 hid->ll_driver = &i2c_hid_ll_driver;
1236 hid->dev.parent = &client->dev;
1238 hid->initial_quirks = quirks;
1240 /* Power on and probe unless device is a panel follower. */
1241 if (!ihid->is_panel_follower) {
1242 ret = i2c_hid_core_power_up(ihid);
1244 goto err_destroy_device;
1246 ret = __i2c_hid_core_probe(ihid);
1248 goto err_power_down;
1251 ret = i2c_hid_init_irq(client);
1253 goto err_power_down;
1256 * If we're a panel follower, we'll register when the panel turns on;
1257 * otherwise we do it right away.
1259 if (ihid->is_panel_follower)
1260 ret = i2c_hid_core_register_panel_follower(ihid);
1262 ret = i2c_hid_core_register_hid(ihid);
1269 free_irq(client->irq, ihid);
1271 if (!ihid->is_panel_follower)
1272 i2c_hid_core_power_down(ihid);
1274 hid_destroy_device(hid);
1276 i2c_hid_free_buffers(ihid);
1280 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1282 void i2c_hid_core_remove(struct i2c_client *client)
1284 struct i2c_hid *ihid = i2c_get_clientdata(client);
1285 struct hid_device *hid;
1288 * If we're a follower, the act of unfollowing will cause us to be
1289 * powered down. Otherwise we need to manually do it.
1291 if (ihid->is_panel_follower)
1292 drm_panel_remove_follower(&ihid->panel_follower);
1294 i2c_hid_core_suspend(ihid, true);
1297 hid_destroy_device(hid);
1299 free_irq(client->irq, ihid);
1302 i2c_hid_free_buffers(ihid);
1304 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1306 void i2c_hid_core_shutdown(struct i2c_client *client)
1308 struct i2c_hid *ihid = i2c_get_clientdata(client);
1310 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1311 free_irq(client->irq, ihid);
1313 i2c_hid_core_shutdown_tail(ihid);
1315 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1317 static int i2c_hid_core_pm_suspend(struct device *dev)
1319 struct i2c_client *client = to_i2c_client(dev);
1320 struct i2c_hid *ihid = i2c_get_clientdata(client);
1322 if (ihid->is_panel_follower)
1325 return i2c_hid_core_suspend(ihid, false);
1328 static int i2c_hid_core_pm_resume(struct device *dev)
1330 struct i2c_client *client = to_i2c_client(dev);
1331 struct i2c_hid *ihid = i2c_get_clientdata(client);
1333 if (ihid->is_panel_follower)
1336 return i2c_hid_core_resume(ihid);
1339 const struct dev_pm_ops i2c_hid_core_pm = {
1340 SYSTEM_SLEEP_PM_OPS(i2c_hid_core_pm_suspend, i2c_hid_core_pm_resume)
1342 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1344 MODULE_DESCRIPTION("HID over I2C core driver");
1345 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1346 MODULE_LICENSE("GPL");