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_runtime.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 <linux/acpi.h>
41 #include <linux/regulator/consumer.h>
43 #include <linux/platform_data/i2c-hid.h>
45 #include "../hid-ids.h"
48 /* quirks to control the device */
49 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
50 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
51 #define I2C_HID_QUIRK_NO_RUNTIME_PM BIT(2)
52 #define I2C_HID_QUIRK_DELAY_AFTER_SLEEP BIT(3)
53 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
56 #define I2C_HID_STARTED 0
57 #define I2C_HID_RESET_PENDING 1
58 #define I2C_HID_READ_PENDING 2
60 #define I2C_HID_PWR_ON 0x00
61 #define I2C_HID_PWR_SLEEP 0x01
65 module_param(debug, bool, 0444);
66 MODULE_PARM_DESC(debug, "print a lot of debug information");
68 #define i2c_hid_dbg(ihid, fmt, arg...) \
71 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
75 __le16 wHIDDescLength;
77 __le16 wReportDescLength;
78 __le16 wReportDescRegister;
79 __le16 wInputRegister;
80 __le16 wMaxInputLength;
81 __le16 wOutputRegister;
82 __le16 wMaxOutputLength;
83 __le16 wCommandRegister;
92 unsigned int registerIndex;
107 #define I2C_HID_CMD(opcode_) \
108 .opcode = opcode_, .length = 4, \
109 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
111 /* fetch HID descriptor */
112 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
113 /* fetch report descriptors */
114 static const struct i2c_hid_cmd hid_report_descr_cmd = {
115 .registerIndex = offsetof(struct i2c_hid_desc,
116 wReportDescRegister),
120 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
122 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
123 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
124 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
125 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
128 * These definitions are not used here, but are defined by the spec.
129 * Keeping them here for documentation purposes.
131 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
132 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
133 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
134 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
137 /* The main device structure */
139 struct i2c_client *client; /* i2c client */
140 struct hid_device *hid; /* pointer to corresponding HID dev */
142 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
143 struct i2c_hid_desc hdesc; /* the HID Descriptor */
145 __le16 wHIDDescRegister; /* location of the i2c
146 * register of the HID
148 unsigned int bufsize; /* i2c buffer size */
149 u8 *inbuf; /* Input buffer */
150 u8 *rawbuf; /* Raw Input buffer */
151 u8 *cmdbuf; /* Command buffer */
152 u8 *argsbuf; /* Command arguments buffer */
154 unsigned long flags; /* device flags */
155 unsigned long quirks; /* Various quirks */
157 wait_queue_head_t wait; /* For waiting the interrupt */
159 struct i2c_hid_platform_data pdata;
161 bool irq_wake_enabled;
162 struct mutex reset_lock;
164 unsigned long sleep_delay;
167 static const struct i2c_hid_quirks {
171 } i2c_hid_quirks[] = {
172 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
173 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
174 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
175 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
176 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
177 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET |
178 I2C_HID_QUIRK_NO_RUNTIME_PM },
179 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_4B33,
180 I2C_HID_QUIRK_DELAY_AFTER_SLEEP },
181 { USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_8001,
182 I2C_HID_QUIRK_NO_RUNTIME_PM },
183 { I2C_VENDOR_ID_GOODIX, I2C_DEVICE_ID_GOODIX_01F0,
184 I2C_HID_QUIRK_NO_RUNTIME_PM },
185 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
186 I2C_HID_QUIRK_BOGUS_IRQ },
187 { USB_VENDOR_ID_SYNAPTICS, I2C_DEVICE_ID_SYNAPTICS_7E7E,
188 I2C_HID_QUIRK_NO_RUNTIME_PM },
193 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
194 * @idVendor: the 16-bit vendor ID
195 * @idProduct: the 16-bit product ID
197 * Returns: a u32 quirks value.
199 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
204 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
205 if (i2c_hid_quirks[n].idVendor == idVendor &&
206 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
207 i2c_hid_quirks[n].idProduct == idProduct))
208 quirks = i2c_hid_quirks[n].quirks;
213 static int __i2c_hid_command(struct i2c_client *client,
214 const struct i2c_hid_cmd *command, u8 reportID,
215 u8 reportType, u8 *args, int args_len,
216 unsigned char *buf_recv, int data_len)
218 struct i2c_hid *ihid = i2c_get_clientdata(client);
219 union command *cmd = (union command *)ihid->cmdbuf;
221 struct i2c_msg msg[2];
224 int length = command->length;
225 bool wait = command->wait;
226 unsigned int registerIndex = command->registerIndex;
228 /* special case for hid_descr_cmd */
229 if (command == &hid_descr_cmd) {
230 cmd->c.reg = ihid->wHIDDescRegister;
232 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
233 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
237 cmd->c.opcode = command->opcode;
238 cmd->c.reportTypeID = reportID | reportType << 4;
241 memcpy(cmd->data + length, args, args_len);
244 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
246 msg[0].addr = client->addr;
247 msg[0].flags = client->flags & I2C_M_TEN;
249 msg[0].buf = cmd->data;
251 msg[1].addr = client->addr;
252 msg[1].flags = client->flags & I2C_M_TEN;
253 msg[1].flags |= I2C_M_RD;
254 msg[1].len = data_len;
255 msg[1].buf = buf_recv;
257 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
261 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
263 ret = i2c_transfer(client->adapter, msg, msg_num);
266 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
269 return ret < 0 ? ret : -EIO;
273 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
276 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
277 if (!wait_event_timeout(ihid->wait,
278 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
279 msecs_to_jiffies(5000)))
281 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
287 static int i2c_hid_command(struct i2c_client *client,
288 const struct i2c_hid_cmd *command,
289 unsigned char *buf_recv, int data_len)
291 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
295 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
296 u8 reportID, unsigned char *buf_recv, int data_len)
298 struct i2c_hid *ihid = i2c_get_clientdata(client);
302 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
304 i2c_hid_dbg(ihid, "%s\n", __func__);
306 if (reportID >= 0x0F) {
307 args[args_len++] = reportID;
311 args[args_len++] = readRegister & 0xFF;
312 args[args_len++] = readRegister >> 8;
314 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
315 reportType, args, args_len, buf_recv, data_len);
317 dev_err(&client->dev,
318 "failed to retrieve report from device.\n");
326 * i2c_hid_set_or_send_report: forward an incoming report to the device
327 * @client: the i2c_client of the device
328 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
329 * @reportID: the report ID
330 * @buf: the actual data to transfer, without the report ID
332 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
334 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
335 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
337 struct i2c_hid *ihid = i2c_get_clientdata(client);
338 u8 *args = ihid->argsbuf;
339 const struct i2c_hid_cmd *hidcmd;
341 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
342 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
343 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
348 i2c_hid_dbg(ihid, "%s\n", __func__);
350 if (data_len > ihid->bufsize)
353 size = 2 /* size */ +
354 (reportID ? 1 : 0) /* reportID */ +
356 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
357 2 /* dataRegister */ +
360 if (!use_data && maxOutputLength == 0)
363 if (reportID >= 0x0F) {
364 args[index++] = reportID;
369 * use the data register for feature reports or if the device does not
370 * support the output register
373 args[index++] = dataRegister & 0xFF;
374 args[index++] = dataRegister >> 8;
375 hidcmd = &hid_set_report_cmd;
377 args[index++] = outputRegister & 0xFF;
378 args[index++] = outputRegister >> 8;
379 hidcmd = &hid_no_cmd;
382 args[index++] = size & 0xFF;
383 args[index++] = size >> 8;
386 args[index++] = reportID;
388 memcpy(&args[index], buf, data_len);
390 ret = __i2c_hid_command(client, hidcmd, reportID,
391 reportType, args, args_len, NULL, 0);
393 dev_err(&client->dev, "failed to set a report to device.\n");
400 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
402 struct i2c_hid *ihid = i2c_get_clientdata(client);
404 unsigned long now, delay;
406 i2c_hid_dbg(ihid, "%s\n", __func__);
409 * Some devices require to send a command to wakeup before power on.
410 * The call will get a return value (EREMOTEIO) but device will be
411 * triggered and activated. After that, it goes like a normal device.
413 if (power_state == I2C_HID_PWR_ON &&
414 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
415 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
417 /* Device was already activated */
422 if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
423 power_state == I2C_HID_PWR_ON) {
425 if (time_after(ihid->sleep_delay, now)) {
426 delay = jiffies_to_usecs(ihid->sleep_delay - now);
427 usleep_range(delay, delay + 1);
431 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
432 0, NULL, 0, NULL, 0);
434 if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
435 power_state == I2C_HID_PWR_SLEEP)
436 ihid->sleep_delay = jiffies + msecs_to_jiffies(20);
439 dev_err(&client->dev, "failed to change power setting.\n");
445 static int i2c_hid_hwreset(struct i2c_client *client)
447 struct i2c_hid *ihid = i2c_get_clientdata(client);
450 i2c_hid_dbg(ihid, "%s\n", __func__);
453 * This prevents sending feature reports while the device is
454 * being reset. Otherwise we may lose the reset complete
457 mutex_lock(&ihid->reset_lock);
459 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
464 * The HID over I2C specification states that if a DEVICE needs time
465 * after the PWR_ON request, it should utilise CLOCK stretching.
466 * However, it has been observered that the Windows driver provides a
467 * 1ms sleep between the PWR_ON and RESET requests and that some devices
470 usleep_range(1000, 5000);
472 i2c_hid_dbg(ihid, "resetting...\n");
474 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
476 dev_err(&client->dev, "failed to reset device.\n");
477 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
481 mutex_unlock(&ihid->reset_lock);
485 static void i2c_hid_get_input(struct i2c_hid *ihid)
489 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
491 if (size > ihid->bufsize)
492 size = ihid->bufsize;
494 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
499 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
500 __func__, ret, size);
504 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
507 /* host or device initiated RESET completed */
508 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
509 wake_up(&ihid->wait);
513 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
514 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
515 "there's no data\n", __func__);
519 if ((ret_size > size) || (ret_size < 2)) {
520 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
521 __func__, size, ret_size);
525 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
527 if (test_bit(I2C_HID_STARTED, &ihid->flags))
528 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
534 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
536 struct i2c_hid *ihid = dev_id;
538 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
541 i2c_hid_get_input(ihid);
546 static int i2c_hid_get_report_length(struct hid_report *report)
548 return ((report->size - 1) >> 3) + 1 +
549 report->device->report_enum[report->type].numbered + 2;
553 * Traverse the supplied list of reports and find the longest
555 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
558 struct hid_report *report;
561 /* We should not rely on wMaxInputLength, as some devices may set it to
563 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
564 size = i2c_hid_get_report_length(report);
570 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
574 kfree(ihid->argsbuf);
579 ihid->argsbuf = NULL;
583 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
585 /* the worst case is computed from the set_report command with a
586 * reportID > 15 and the maximum report length */
587 int args_len = sizeof(__u8) + /* ReportID */
588 sizeof(__u8) + /* optional ReportID byte */
589 sizeof(__u16) + /* data register */
590 sizeof(__u16) + /* size of the report */
591 report_size; /* report */
593 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
594 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
595 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
596 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
598 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
599 i2c_hid_free_buffers(ihid);
603 ihid->bufsize = report_size;
608 static int i2c_hid_get_raw_report(struct hid_device *hid,
609 unsigned char report_number, __u8 *buf, size_t count,
610 unsigned char report_type)
612 struct i2c_client *client = hid->driver_data;
613 struct i2c_hid *ihid = i2c_get_clientdata(client);
614 size_t ret_count, ask_count;
617 if (report_type == HID_OUTPUT_REPORT)
620 /* +2 bytes to include the size of the reply in the query buffer */
621 ask_count = min(count + 2, (size_t)ihid->bufsize);
623 ret = i2c_hid_get_report(client,
624 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
625 report_number, ihid->rawbuf, ask_count);
630 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
635 ret_count = min(ret_count, ask_count);
637 /* The query buffer contains the size, dropping it in the reply */
638 count = min(count, ret_count - 2);
639 memcpy(buf, ihid->rawbuf + 2, count);
644 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
645 size_t count, unsigned char report_type, bool use_data)
647 struct i2c_client *client = hid->driver_data;
648 struct i2c_hid *ihid = i2c_get_clientdata(client);
649 int report_id = buf[0];
652 if (report_type == HID_INPUT_REPORT)
655 mutex_lock(&ihid->reset_lock);
662 ret = i2c_hid_set_or_send_report(client,
663 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
664 report_id, buf, count, use_data);
666 if (report_id && ret >= 0)
667 ret++; /* add report_id to the number of transfered bytes */
669 mutex_unlock(&ihid->reset_lock);
674 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
677 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
681 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
682 __u8 *buf, size_t len, unsigned char rtype,
686 case HID_REQ_GET_REPORT:
687 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
688 case HID_REQ_SET_REPORT:
689 if (buf[0] != reportnum)
691 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
697 static int i2c_hid_parse(struct hid_device *hid)
699 struct i2c_client *client = hid->driver_data;
700 struct i2c_hid *ihid = i2c_get_clientdata(client);
701 struct i2c_hid_desc *hdesc = &ihid->hdesc;
708 i2c_hid_dbg(ihid, "entering %s\n", __func__);
710 rsize = le16_to_cpu(hdesc->wReportDescLength);
711 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
712 dbg_hid("weird size of report descriptor (%u)\n", rsize);
717 ret = i2c_hid_hwreset(client);
720 } while (tries-- > 0 && ret);
725 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
729 rdesc = use_override;
730 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
732 rdesc = kzalloc(rsize, GFP_KERNEL);
735 dbg_hid("couldn't allocate rdesc memory\n");
739 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
741 ret = i2c_hid_command(client, &hid_report_descr_cmd,
744 hid_err(hid, "reading report descriptor failed\n");
750 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
752 ret = hid_parse_report(hid, rdesc, rsize);
757 dbg_hid("parsing report descriptor failed\n");
764 static int i2c_hid_start(struct hid_device *hid)
766 struct i2c_client *client = hid->driver_data;
767 struct i2c_hid *ihid = i2c_get_clientdata(client);
769 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
771 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
772 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
773 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
775 if (bufsize > ihid->bufsize) {
776 disable_irq(client->irq);
777 i2c_hid_free_buffers(ihid);
779 ret = i2c_hid_alloc_buffers(ihid, bufsize);
780 enable_irq(client->irq);
789 static void i2c_hid_stop(struct hid_device *hid)
794 static int i2c_hid_open(struct hid_device *hid)
796 struct i2c_client *client = hid->driver_data;
797 struct i2c_hid *ihid = i2c_get_clientdata(client);
800 ret = pm_runtime_get_sync(&client->dev);
804 set_bit(I2C_HID_STARTED, &ihid->flags);
808 static void i2c_hid_close(struct hid_device *hid)
810 struct i2c_client *client = hid->driver_data;
811 struct i2c_hid *ihid = i2c_get_clientdata(client);
813 clear_bit(I2C_HID_STARTED, &ihid->flags);
815 /* Save some power */
816 pm_runtime_put(&client->dev);
819 static int i2c_hid_power(struct hid_device *hid, int lvl)
821 struct i2c_client *client = hid->driver_data;
822 struct i2c_hid *ihid = i2c_get_clientdata(client);
824 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
828 pm_runtime_get_sync(&client->dev);
831 pm_runtime_put(&client->dev);
837 struct hid_ll_driver i2c_hid_ll_driver = {
838 .parse = i2c_hid_parse,
839 .start = i2c_hid_start,
840 .stop = i2c_hid_stop,
841 .open = i2c_hid_open,
842 .close = i2c_hid_close,
843 .power = i2c_hid_power,
844 .output_report = i2c_hid_output_report,
845 .raw_request = i2c_hid_raw_request,
847 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
849 static int i2c_hid_init_irq(struct i2c_client *client)
851 struct i2c_hid *ihid = i2c_get_clientdata(client);
852 unsigned long irqflags = 0;
855 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
857 if (!irq_get_trigger_type(client->irq))
858 irqflags = IRQF_TRIGGER_LOW;
860 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
861 irqflags | IRQF_ONESHOT, client->name, ihid);
863 dev_warn(&client->dev,
864 "Could not register for %s interrupt, irq = %d,"
866 client->name, client->irq, ret);
874 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
876 struct i2c_client *client = ihid->client;
877 struct i2c_hid_desc *hdesc = &ihid->hdesc;
881 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
882 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
883 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
885 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
887 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
888 ret = i2c_hid_command(client, &hid_descr_cmd,
890 sizeof(struct i2c_hid_desc));
892 dev_err(&client->dev, "hid_descr_cmd failed\n");
897 /* Validate the length of HID descriptor, the 4 first bytes:
898 * bytes 0-1 -> length
899 * bytes 2-3 -> bcdVersion (has to be 1.00) */
900 /* check bcdVersion == 1.0 */
901 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
902 dev_err(&client->dev,
903 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
904 le16_to_cpu(hdesc->bcdVersion));
908 /* Descriptor length should be 30 bytes as per the specification */
909 dsize = le16_to_cpu(hdesc->wHIDDescLength);
910 if (dsize != sizeof(struct i2c_hid_desc)) {
911 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
915 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
920 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
922 * The CHPN0001 ACPI device, which is used to describe the Chipone
923 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
929 static int i2c_hid_acpi_pdata(struct i2c_client *client,
930 struct i2c_hid_platform_data *pdata)
932 static guid_t i2c_hid_guid =
933 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
934 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
935 union acpi_object *obj;
936 struct acpi_device *adev;
939 handle = ACPI_HANDLE(&client->dev);
940 if (!handle || acpi_bus_get_device(handle, &adev)) {
941 dev_err(&client->dev, "Error could not get ACPI device\n");
945 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
948 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
951 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
955 pdata->hid_descriptor_address = obj->integer.value;
961 static void i2c_hid_acpi_fix_up_power(struct device *dev)
963 struct acpi_device *adev;
965 adev = ACPI_COMPANION(dev);
967 acpi_device_fix_up_power(adev);
970 static const struct acpi_device_id i2c_hid_acpi_match[] = {
975 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
977 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
978 struct i2c_hid_platform_data *pdata)
983 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
987 static int i2c_hid_of_probe(struct i2c_client *client,
988 struct i2c_hid_platform_data *pdata)
990 struct device *dev = &client->dev;
994 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
996 dev_err(&client->dev, "HID register address not provided\n");
1000 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
1004 pdata->hid_descriptor_address = val;
1009 static const struct of_device_id i2c_hid_of_match[] = {
1010 { .compatible = "hid-over-i2c" },
1013 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1015 static inline int i2c_hid_of_probe(struct i2c_client *client,
1016 struct i2c_hid_platform_data *pdata)
1022 static void i2c_hid_fwnode_probe(struct i2c_client *client,
1023 struct i2c_hid_platform_data *pdata)
1027 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
1029 pdata->post_power_delay_ms = val;
1032 static int i2c_hid_probe(struct i2c_client *client,
1033 const struct i2c_device_id *dev_id)
1036 struct i2c_hid *ihid;
1037 struct hid_device *hid;
1039 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1041 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1044 dev_err(&client->dev,
1045 "HID over i2c has not been provided an Int IRQ\n");
1049 if (client->irq < 0) {
1050 if (client->irq != -EPROBE_DEFER)
1051 dev_err(&client->dev,
1052 "HID over i2c doesn't have a valid IRQ\n");
1056 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1060 if (client->dev.of_node) {
1061 ret = i2c_hid_of_probe(client, &ihid->pdata);
1064 } else if (!platform_data) {
1065 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1069 ihid->pdata = *platform_data;
1072 /* Parse platform agnostic common properties from ACPI / device tree */
1073 i2c_hid_fwnode_probe(client, &ihid->pdata);
1075 ihid->pdata.supplies[0].supply = "vdd";
1076 ihid->pdata.supplies[1].supply = "vddl";
1078 ret = devm_regulator_bulk_get(&client->dev,
1079 ARRAY_SIZE(ihid->pdata.supplies),
1080 ihid->pdata.supplies);
1084 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1085 ihid->pdata.supplies);
1089 if (ihid->pdata.post_power_delay_ms)
1090 msleep(ihid->pdata.post_power_delay_ms);
1092 i2c_set_clientdata(client, ihid);
1094 ihid->client = client;
1096 hidRegister = ihid->pdata.hid_descriptor_address;
1097 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1099 init_waitqueue_head(&ihid->wait);
1100 mutex_init(&ihid->reset_lock);
1102 /* we need to allocate the command buffer without knowing the maximum
1103 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1104 * real computation later. */
1105 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1109 i2c_hid_acpi_fix_up_power(&client->dev);
1111 pm_runtime_get_noresume(&client->dev);
1112 pm_runtime_set_active(&client->dev);
1113 pm_runtime_enable(&client->dev);
1114 device_enable_async_suspend(&client->dev);
1116 /* Make sure there is something at this address */
1117 ret = i2c_smbus_read_byte(client);
1119 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1124 ret = i2c_hid_fetch_hid_descriptor(ihid);
1128 ret = i2c_hid_init_irq(client);
1132 hid = hid_allocate_device();
1140 hid->driver_data = client;
1141 hid->ll_driver = &i2c_hid_ll_driver;
1142 hid->dev.parent = &client->dev;
1144 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1145 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1146 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1148 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1149 client->name, hid->vendor, hid->product);
1150 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1152 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1154 ret = hid_add_device(hid);
1157 hid_err(client, "can't add hid device: %d\n", ret);
1161 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1162 pm_runtime_put(&client->dev);
1167 hid_destroy_device(hid);
1170 free_irq(client->irq, ihid);
1173 pm_runtime_put_noidle(&client->dev);
1174 pm_runtime_disable(&client->dev);
1177 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1178 ihid->pdata.supplies);
1179 i2c_hid_free_buffers(ihid);
1183 static int i2c_hid_remove(struct i2c_client *client)
1185 struct i2c_hid *ihid = i2c_get_clientdata(client);
1186 struct hid_device *hid;
1188 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1189 pm_runtime_get_sync(&client->dev);
1190 pm_runtime_disable(&client->dev);
1191 pm_runtime_set_suspended(&client->dev);
1192 pm_runtime_put_noidle(&client->dev);
1195 hid_destroy_device(hid);
1197 free_irq(client->irq, ihid);
1200 i2c_hid_free_buffers(ihid);
1202 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1203 ihid->pdata.supplies);
1208 static void i2c_hid_shutdown(struct i2c_client *client)
1210 struct i2c_hid *ihid = i2c_get_clientdata(client);
1212 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1213 free_irq(client->irq, ihid);
1216 #ifdef CONFIG_PM_SLEEP
1217 static int i2c_hid_suspend(struct device *dev)
1219 struct i2c_client *client = to_i2c_client(dev);
1220 struct i2c_hid *ihid = i2c_get_clientdata(client);
1221 struct hid_device *hid = ihid->hid;
1225 if (hid->driver && hid->driver->suspend) {
1227 * Wake up the device so that IO issues in
1228 * HID driver's suspend code can succeed.
1230 ret = pm_runtime_resume(dev);
1234 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1239 if (!pm_runtime_suspended(dev)) {
1240 /* Save some power */
1241 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1243 disable_irq(client->irq);
1246 if (device_may_wakeup(&client->dev)) {
1247 wake_status = enable_irq_wake(client->irq);
1249 ihid->irq_wake_enabled = true;
1251 hid_warn(hid, "Failed to enable irq wake: %d\n",
1254 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1255 ihid->pdata.supplies);
1261 static int i2c_hid_resume(struct device *dev)
1264 struct i2c_client *client = to_i2c_client(dev);
1265 struct i2c_hid *ihid = i2c_get_clientdata(client);
1266 struct hid_device *hid = ihid->hid;
1269 if (!device_may_wakeup(&client->dev)) {
1270 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1271 ihid->pdata.supplies);
1273 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1275 if (ihid->pdata.post_power_delay_ms)
1276 msleep(ihid->pdata.post_power_delay_ms);
1277 } else if (ihid->irq_wake_enabled) {
1278 wake_status = disable_irq_wake(client->irq);
1280 ihid->irq_wake_enabled = false;
1282 hid_warn(hid, "Failed to disable irq wake: %d\n",
1286 /* We'll resume to full power */
1287 pm_runtime_disable(dev);
1288 pm_runtime_set_active(dev);
1289 pm_runtime_enable(dev);
1291 enable_irq(client->irq);
1293 /* Instead of resetting device, simply powers the device on. This
1294 * solves "incomplete reports" on Raydium devices 2386:3118 and
1295 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1296 * data after a suspend/resume.
1298 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1302 if (hid->driver && hid->driver->reset_resume) {
1303 ret = hid->driver->reset_resume(hid);
1312 static int i2c_hid_runtime_suspend(struct device *dev)
1314 struct i2c_client *client = to_i2c_client(dev);
1316 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1317 disable_irq(client->irq);
1321 static int i2c_hid_runtime_resume(struct device *dev)
1323 struct i2c_client *client = to_i2c_client(dev);
1325 enable_irq(client->irq);
1326 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1331 static const struct dev_pm_ops i2c_hid_pm = {
1332 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1333 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1337 static const struct i2c_device_id i2c_hid_id_table[] = {
1339 { "hid-over-i2c", 0 },
1342 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1345 static struct i2c_driver i2c_hid_driver = {
1349 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1350 .of_match_table = of_match_ptr(i2c_hid_of_match),
1353 .probe = i2c_hid_probe,
1354 .remove = i2c_hid_remove,
1355 .shutdown = i2c_hid_shutdown,
1356 .id_table = i2c_hid_id_table,
1359 module_i2c_driver(i2c_hid_driver);
1361 MODULE_DESCRIPTION("HID over I2C core driver");
1362 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1363 MODULE_LICENSE("GPL");