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/delay.h>
26 #include <linux/slab.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/device.h>
30 #include <linux/wait.h>
31 #include <linux/err.h>
32 #include <linux/string.h>
33 #include <linux/list.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/hid.h>
37 #include <linux/mutex.h>
38 #include <linux/acpi.h>
40 #include <linux/gpio/consumer.h>
42 #include <linux/i2c/i2c-hid.h>
44 #include "../hid-ids.h"
47 /* quirks to control the device */
48 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
51 #define I2C_HID_STARTED 0
52 #define I2C_HID_RESET_PENDING 1
53 #define I2C_HID_READ_PENDING 2
55 #define I2C_HID_PWR_ON 0x00
56 #define I2C_HID_PWR_SLEEP 0x01
60 module_param(debug, bool, 0444);
61 MODULE_PARM_DESC(debug, "print a lot of debug information");
63 #define i2c_hid_dbg(ihid, fmt, arg...) \
66 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
70 __le16 wHIDDescLength;
72 __le16 wReportDescLength;
73 __le16 wReportDescRegister;
74 __le16 wInputRegister;
75 __le16 wMaxInputLength;
76 __le16 wOutputRegister;
77 __le16 wMaxOutputLength;
78 __le16 wCommandRegister;
87 unsigned int registerIndex;
102 #define I2C_HID_CMD(opcode_) \
103 .opcode = opcode_, .length = 4, \
104 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
106 /* fetch HID descriptor */
107 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
108 /* fetch report descriptors */
109 static const struct i2c_hid_cmd hid_report_descr_cmd = {
110 .registerIndex = offsetof(struct i2c_hid_desc,
111 wReportDescRegister),
115 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
117 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
118 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
119 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
120 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
123 * These definitions are not used here, but are defined by the spec.
124 * Keeping them here for documentation purposes.
126 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
127 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
128 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
129 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
132 static DEFINE_MUTEX(i2c_hid_open_mut);
134 /* The main device structure */
136 struct i2c_client *client; /* i2c client */
137 struct hid_device *hid; /* pointer to corresponding HID dev */
139 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
140 struct i2c_hid_desc hdesc; /* the HID Descriptor */
142 __le16 wHIDDescRegister; /* location of the i2c
143 * register of the HID
145 unsigned int bufsize; /* i2c buffer size */
146 u8 *inbuf; /* Input buffer */
147 u8 *rawbuf; /* Raw Input buffer */
148 u8 *cmdbuf; /* Command buffer */
149 u8 *argsbuf; /* Command arguments buffer */
151 unsigned long flags; /* device flags */
152 unsigned long quirks; /* Various quirks */
154 wait_queue_head_t wait; /* For waiting the interrupt */
155 struct gpio_desc *desc;
158 struct i2c_hid_platform_data pdata;
160 bool irq_wake_enabled;
161 struct mutex reset_lock;
164 static const struct i2c_hid_quirks {
168 } i2c_hid_quirks[] = {
169 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
170 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
171 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
172 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
177 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
178 * @idVendor: the 16-bit vendor ID
179 * @idProduct: the 16-bit product ID
181 * Returns: a u32 quirks value.
183 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
188 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
189 if (i2c_hid_quirks[n].idVendor == idVendor &&
190 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
191 i2c_hid_quirks[n].idProduct == idProduct))
192 quirks = i2c_hid_quirks[n].quirks;
197 static int __i2c_hid_command(struct i2c_client *client,
198 const struct i2c_hid_cmd *command, u8 reportID,
199 u8 reportType, u8 *args, int args_len,
200 unsigned char *buf_recv, int data_len)
202 struct i2c_hid *ihid = i2c_get_clientdata(client);
203 union command *cmd = (union command *)ihid->cmdbuf;
205 struct i2c_msg msg[2];
208 int length = command->length;
209 bool wait = command->wait;
210 unsigned int registerIndex = command->registerIndex;
212 /* special case for hid_descr_cmd */
213 if (command == &hid_descr_cmd) {
214 cmd->c.reg = ihid->wHIDDescRegister;
216 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
217 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
221 cmd->c.opcode = command->opcode;
222 cmd->c.reportTypeID = reportID | reportType << 4;
225 memcpy(cmd->data + length, args, args_len);
228 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
230 msg[0].addr = client->addr;
231 msg[0].flags = client->flags & I2C_M_TEN;
233 msg[0].buf = cmd->data;
235 msg[1].addr = client->addr;
236 msg[1].flags = client->flags & I2C_M_TEN;
237 msg[1].flags |= I2C_M_RD;
238 msg[1].len = data_len;
239 msg[1].buf = buf_recv;
241 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
245 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
247 ret = i2c_transfer(client->adapter, msg, msg_num);
250 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
253 return ret < 0 ? ret : -EIO;
258 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
259 if (!wait_event_timeout(ihid->wait,
260 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
261 msecs_to_jiffies(5000)))
263 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
269 static int i2c_hid_command(struct i2c_client *client,
270 const struct i2c_hid_cmd *command,
271 unsigned char *buf_recv, int data_len)
273 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
277 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
278 u8 reportID, unsigned char *buf_recv, int data_len)
280 struct i2c_hid *ihid = i2c_get_clientdata(client);
284 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
286 i2c_hid_dbg(ihid, "%s\n", __func__);
288 if (reportID >= 0x0F) {
289 args[args_len++] = reportID;
293 args[args_len++] = readRegister & 0xFF;
294 args[args_len++] = readRegister >> 8;
296 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
297 reportType, args, args_len, buf_recv, data_len);
299 dev_err(&client->dev,
300 "failed to retrieve report from device.\n");
308 * i2c_hid_set_or_send_report: forward an incoming report to the device
309 * @client: the i2c_client of the device
310 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
311 * @reportID: the report ID
312 * @buf: the actual data to transfer, without the report ID
314 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
316 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
317 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
319 struct i2c_hid *ihid = i2c_get_clientdata(client);
320 u8 *args = ihid->argsbuf;
321 const struct i2c_hid_cmd *hidcmd;
323 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
324 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
325 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
330 i2c_hid_dbg(ihid, "%s\n", __func__);
332 if (data_len > ihid->bufsize)
335 size = 2 /* size */ +
336 (reportID ? 1 : 0) /* reportID */ +
338 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
339 2 /* dataRegister */ +
342 if (!use_data && maxOutputLength == 0)
345 if (reportID >= 0x0F) {
346 args[index++] = reportID;
351 * use the data register for feature reports or if the device does not
352 * support the output register
355 args[index++] = dataRegister & 0xFF;
356 args[index++] = dataRegister >> 8;
357 hidcmd = &hid_set_report_cmd;
359 args[index++] = outputRegister & 0xFF;
360 args[index++] = outputRegister >> 8;
361 hidcmd = &hid_no_cmd;
364 args[index++] = size & 0xFF;
365 args[index++] = size >> 8;
368 args[index++] = reportID;
370 memcpy(&args[index], buf, data_len);
372 ret = __i2c_hid_command(client, hidcmd, reportID,
373 reportType, args, args_len, NULL, 0);
375 dev_err(&client->dev, "failed to set a report to device.\n");
382 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
384 struct i2c_hid *ihid = i2c_get_clientdata(client);
387 i2c_hid_dbg(ihid, "%s\n", __func__);
390 * Some devices require to send a command to wakeup before power on.
391 * The call will get a return value (EREMOTEIO) but device will be
392 * triggered and activated. After that, it goes like a normal device.
394 if (power_state == I2C_HID_PWR_ON &&
395 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
396 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
398 /* Device was already activated */
403 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
404 0, NULL, 0, NULL, 0);
407 dev_err(&client->dev, "failed to change power setting.\n");
412 * The HID over I2C specification states that if a DEVICE needs time
413 * after the PWR_ON request, it should utilise CLOCK stretching.
414 * However, it has been observered that the Windows driver provides a
415 * 1ms sleep between the PWR_ON and RESET requests.
416 * According to Goodix Windows even waits 60 ms after (other?)
417 * PWR_ON requests. Testing has confirmed that several devices
418 * will not work properly without a delay after a PWR_ON request.
420 if (!ret && power_state == I2C_HID_PWR_ON)
426 static int i2c_hid_hwreset(struct i2c_client *client)
428 struct i2c_hid *ihid = i2c_get_clientdata(client);
431 i2c_hid_dbg(ihid, "%s\n", __func__);
434 * This prevents sending feature reports while the device is
435 * being reset. Otherwise we may lose the reset complete
438 mutex_lock(&ihid->reset_lock);
440 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
444 i2c_hid_dbg(ihid, "resetting...\n");
446 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
448 dev_err(&client->dev, "failed to reset device.\n");
449 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
453 mutex_unlock(&ihid->reset_lock);
457 static void i2c_hid_get_input(struct i2c_hid *ihid)
461 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
463 if (size > ihid->bufsize)
464 size = ihid->bufsize;
466 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
471 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
472 __func__, ret, size);
476 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
479 /* host or device initiated RESET completed */
480 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
481 wake_up(&ihid->wait);
485 if ((ret_size > size) || (ret_size < 2)) {
486 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
487 __func__, size, ret_size);
491 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
493 if (test_bit(I2C_HID_STARTED, &ihid->flags))
494 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
500 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
502 struct i2c_hid *ihid = dev_id;
504 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
507 i2c_hid_get_input(ihid);
512 static int i2c_hid_get_report_length(struct hid_report *report)
514 return ((report->size - 1) >> 3) + 1 +
515 report->device->report_enum[report->type].numbered + 2;
518 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
521 struct hid_device *hid = report->device;
522 struct i2c_client *client = hid->driver_data;
523 struct i2c_hid *ihid = i2c_get_clientdata(client);
524 unsigned int size, ret_size;
526 size = i2c_hid_get_report_length(report);
527 if (i2c_hid_get_report(client,
528 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
529 report->id, buffer, size))
532 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer);
534 ret_size = buffer[0] | (buffer[1] << 8);
536 if (ret_size != size) {
537 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
538 __func__, size, ret_size);
542 /* hid->driver_lock is held as we are in probe function,
543 * we just need to setup the input fields, so using
544 * hid_report_raw_event is safe. */
545 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
549 * Initialize all reports
551 static void i2c_hid_init_reports(struct hid_device *hid)
553 struct hid_report *report;
554 struct i2c_client *client = hid->driver_data;
555 struct i2c_hid *ihid = i2c_get_clientdata(client);
556 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
559 dev_err(&client->dev, "can not retrieve initial reports\n");
564 * The device must be powered on while we fetch initial reports
567 pm_runtime_get_sync(&client->dev);
569 list_for_each_entry(report,
570 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
571 i2c_hid_init_report(report, inbuf, ihid->bufsize);
573 pm_runtime_put(&client->dev);
579 * Traverse the supplied list of reports and find the longest
581 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
584 struct hid_report *report;
587 /* We should not rely on wMaxInputLength, as some devices may set it to
589 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
590 size = i2c_hid_get_report_length(report);
596 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
600 kfree(ihid->argsbuf);
605 ihid->argsbuf = NULL;
609 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
611 /* the worst case is computed from the set_report command with a
612 * reportID > 15 and the maximum report length */
613 int args_len = sizeof(__u8) + /* ReportID */
614 sizeof(__u8) + /* optional ReportID byte */
615 sizeof(__u16) + /* data register */
616 sizeof(__u16) + /* size of the report */
617 report_size; /* report */
619 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
620 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
621 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
622 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
624 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
625 i2c_hid_free_buffers(ihid);
629 ihid->bufsize = report_size;
634 static int i2c_hid_get_raw_report(struct hid_device *hid,
635 unsigned char report_number, __u8 *buf, size_t count,
636 unsigned char report_type)
638 struct i2c_client *client = hid->driver_data;
639 struct i2c_hid *ihid = i2c_get_clientdata(client);
640 size_t ret_count, ask_count;
643 if (report_type == HID_OUTPUT_REPORT)
646 /* +2 bytes to include the size of the reply in the query buffer */
647 ask_count = min(count + 2, (size_t)ihid->bufsize);
649 ret = i2c_hid_get_report(client,
650 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
651 report_number, ihid->rawbuf, ask_count);
656 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
661 ret_count = min(ret_count, ask_count);
663 /* The query buffer contains the size, dropping it in the reply */
664 count = min(count, ret_count - 2);
665 memcpy(buf, ihid->rawbuf + 2, count);
670 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
671 size_t count, unsigned char report_type, bool use_data)
673 struct i2c_client *client = hid->driver_data;
674 struct i2c_hid *ihid = i2c_get_clientdata(client);
675 int report_id = buf[0];
678 if (report_type == HID_INPUT_REPORT)
681 mutex_lock(&ihid->reset_lock);
688 ret = i2c_hid_set_or_send_report(client,
689 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
690 report_id, buf, count, use_data);
692 if (report_id && ret >= 0)
693 ret++; /* add report_id to the number of transfered bytes */
695 mutex_unlock(&ihid->reset_lock);
700 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
703 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
707 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
708 __u8 *buf, size_t len, unsigned char rtype,
712 case HID_REQ_GET_REPORT:
713 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
714 case HID_REQ_SET_REPORT:
715 if (buf[0] != reportnum)
717 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
723 static int i2c_hid_parse(struct hid_device *hid)
725 struct i2c_client *client = hid->driver_data;
726 struct i2c_hid *ihid = i2c_get_clientdata(client);
727 struct i2c_hid_desc *hdesc = &ihid->hdesc;
734 i2c_hid_dbg(ihid, "entering %s\n", __func__);
736 rsize = le16_to_cpu(hdesc->wReportDescLength);
737 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
738 dbg_hid("weird size of report descriptor (%u)\n", rsize);
743 ret = i2c_hid_hwreset(client);
746 } while (tries-- > 0 && ret);
751 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
755 rdesc = use_override;
756 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
758 rdesc = kzalloc(rsize, GFP_KERNEL);
761 dbg_hid("couldn't allocate rdesc memory\n");
765 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
767 ret = i2c_hid_command(client, &hid_report_descr_cmd,
770 hid_err(hid, "reading report descriptor failed\n");
776 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
778 ret = hid_parse_report(hid, rdesc, rsize);
783 dbg_hid("parsing report descriptor failed\n");
790 static int i2c_hid_start(struct hid_device *hid)
792 struct i2c_client *client = hid->driver_data;
793 struct i2c_hid *ihid = i2c_get_clientdata(client);
795 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
797 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
798 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
799 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
801 if (bufsize > ihid->bufsize) {
802 i2c_hid_free_buffers(ihid);
804 ret = i2c_hid_alloc_buffers(ihid, bufsize);
810 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
811 i2c_hid_init_reports(hid);
816 static void i2c_hid_stop(struct hid_device *hid)
821 static int i2c_hid_open(struct hid_device *hid)
823 struct i2c_client *client = hid->driver_data;
824 struct i2c_hid *ihid = i2c_get_clientdata(client);
827 mutex_lock(&i2c_hid_open_mut);
829 ret = pm_runtime_get_sync(&client->dev);
834 set_bit(I2C_HID_STARTED, &ihid->flags);
837 mutex_unlock(&i2c_hid_open_mut);
838 return ret < 0 ? ret : 0;
841 static void i2c_hid_close(struct hid_device *hid)
843 struct i2c_client *client = hid->driver_data;
844 struct i2c_hid *ihid = i2c_get_clientdata(client);
846 /* protecting hid->open to make sure we don't restart
847 * data acquistion due to a resumption we no longer
850 mutex_lock(&i2c_hid_open_mut);
852 clear_bit(I2C_HID_STARTED, &ihid->flags);
854 /* Save some power */
855 pm_runtime_put(&client->dev);
857 mutex_unlock(&i2c_hid_open_mut);
860 static int i2c_hid_power(struct hid_device *hid, int lvl)
862 struct i2c_client *client = hid->driver_data;
863 struct i2c_hid *ihid = i2c_get_clientdata(client);
865 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
869 pm_runtime_get_sync(&client->dev);
872 pm_runtime_put(&client->dev);
878 struct hid_ll_driver i2c_hid_ll_driver = {
879 .parse = i2c_hid_parse,
880 .start = i2c_hid_start,
881 .stop = i2c_hid_stop,
882 .open = i2c_hid_open,
883 .close = i2c_hid_close,
884 .power = i2c_hid_power,
885 .output_report = i2c_hid_output_report,
886 .raw_request = i2c_hid_raw_request,
888 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
890 static int i2c_hid_init_irq(struct i2c_client *client)
892 struct i2c_hid *ihid = i2c_get_clientdata(client);
895 dev_dbg(&client->dev, "Requesting IRQ: %d\n", ihid->irq);
897 ret = request_threaded_irq(ihid->irq, NULL, i2c_hid_irq,
898 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
901 dev_warn(&client->dev,
902 "Could not register for %s interrupt, irq = %d,"
904 client->name, ihid->irq, ret);
912 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
914 struct i2c_client *client = ihid->client;
915 struct i2c_hid_desc *hdesc = &ihid->hdesc;
919 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
920 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
921 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
923 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
925 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
926 ret = i2c_hid_command(client, &hid_descr_cmd,
928 sizeof(struct i2c_hid_desc));
930 dev_err(&client->dev, "hid_descr_cmd failed\n");
935 /* Validate the length of HID descriptor, the 4 first bytes:
936 * bytes 0-1 -> length
937 * bytes 2-3 -> bcdVersion (has to be 1.00) */
938 /* check bcdVersion == 1.0 */
939 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
940 dev_err(&client->dev,
941 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
942 le16_to_cpu(hdesc->bcdVersion));
946 /* Descriptor length should be 30 bytes as per the specification */
947 dsize = le16_to_cpu(hdesc->wHIDDescLength);
948 if (dsize != sizeof(struct i2c_hid_desc)) {
949 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
953 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
959 /* Default GPIO mapping */
960 static const struct acpi_gpio_params i2c_hid_irq_gpio = { 0, 0, true };
961 static const struct acpi_gpio_mapping i2c_hid_acpi_gpios[] = {
962 { "gpios", &i2c_hid_irq_gpio, 1 },
966 static int i2c_hid_acpi_pdata(struct i2c_client *client,
967 struct i2c_hid_platform_data *pdata)
969 static u8 i2c_hid_guid[] = {
970 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
971 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
973 union acpi_object *obj;
974 struct acpi_device *adev;
978 handle = ACPI_HANDLE(&client->dev);
979 if (!handle || acpi_bus_get_device(handle, &adev))
982 obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL,
985 dev_err(&client->dev, "device _DSM execution failed\n");
989 pdata->hid_descriptor_address = obj->integer.value;
992 /* GPIOs are optional */
993 ret = acpi_dev_add_driver_gpios(adev, i2c_hid_acpi_gpios);
994 return ret < 0 && ret != -ENXIO ? ret : 0;
997 static void i2c_hid_acpi_fix_up_power(struct device *dev)
999 acpi_handle handle = ACPI_HANDLE(dev);
1000 struct acpi_device *adev;
1002 if (handle && acpi_bus_get_device(handle, &adev) == 0)
1003 acpi_device_fix_up_power(adev);
1006 static const struct acpi_device_id i2c_hid_acpi_match[] = {
1011 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
1013 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
1014 struct i2c_hid_platform_data *pdata)
1019 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
1023 static int i2c_hid_of_probe(struct i2c_client *client,
1024 struct i2c_hid_platform_data *pdata)
1026 struct device *dev = &client->dev;
1030 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
1032 dev_err(&client->dev, "HID register address not provided\n");
1036 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
1040 pdata->hid_descriptor_address = val;
1045 static const struct of_device_id i2c_hid_of_match[] = {
1046 { .compatible = "hid-over-i2c" },
1049 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1051 static inline int i2c_hid_of_probe(struct i2c_client *client,
1052 struct i2c_hid_platform_data *pdata)
1058 static int i2c_hid_probe(struct i2c_client *client,
1059 const struct i2c_device_id *dev_id)
1062 struct i2c_hid *ihid;
1063 struct hid_device *hid;
1065 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1067 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1069 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
1073 if (client->dev.of_node) {
1074 ret = i2c_hid_of_probe(client, &ihid->pdata);
1077 } else if (!platform_data) {
1078 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1080 dev_err(&client->dev,
1081 "HID register address not provided\n");
1085 ihid->pdata = *platform_data;
1088 if (client->irq > 0) {
1089 ihid->irq = client->irq;
1090 } else if (ACPI_COMPANION(&client->dev)) {
1091 ihid->desc = gpiod_get(&client->dev, NULL, GPIOD_IN);
1092 if (IS_ERR(ihid->desc)) {
1093 dev_err(&client->dev, "Failed to get GPIO interrupt\n");
1094 return PTR_ERR(ihid->desc);
1097 ihid->irq = gpiod_to_irq(ihid->desc);
1098 if (ihid->irq < 0) {
1099 gpiod_put(ihid->desc);
1100 dev_err(&client->dev, "Failed to convert GPIO to IRQ\n");
1105 i2c_set_clientdata(client, ihid);
1107 ihid->client = client;
1109 hidRegister = ihid->pdata.hid_descriptor_address;
1110 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1112 init_waitqueue_head(&ihid->wait);
1113 mutex_init(&ihid->reset_lock);
1115 /* we need to allocate the command buffer without knowing the maximum
1116 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1117 * real computation later. */
1118 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1122 i2c_hid_acpi_fix_up_power(&client->dev);
1124 pm_runtime_get_noresume(&client->dev);
1125 pm_runtime_set_active(&client->dev);
1126 pm_runtime_enable(&client->dev);
1127 device_enable_async_suspend(&client->dev);
1129 /* Make sure there is something at this address */
1130 ret = i2c_smbus_read_byte(client);
1132 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1137 ret = i2c_hid_fetch_hid_descriptor(ihid);
1141 ret = i2c_hid_init_irq(client);
1145 hid = hid_allocate_device();
1153 hid->driver_data = client;
1154 hid->ll_driver = &i2c_hid_ll_driver;
1155 hid->dev.parent = &client->dev;
1157 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1158 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1159 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1161 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1162 client->name, (u16)hid->vendor, (u16)hid->product);
1163 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1165 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1167 ret = hid_add_device(hid);
1170 hid_err(client, "can't add hid device: %d\n", ret);
1174 pm_runtime_put(&client->dev);
1178 hid_destroy_device(hid);
1181 free_irq(ihid->irq, ihid);
1184 pm_runtime_put_noidle(&client->dev);
1185 pm_runtime_disable(&client->dev);
1189 gpiod_put(ihid->desc);
1191 i2c_hid_free_buffers(ihid);
1196 static int i2c_hid_remove(struct i2c_client *client)
1198 struct i2c_hid *ihid = i2c_get_clientdata(client);
1199 struct hid_device *hid;
1201 pm_runtime_get_sync(&client->dev);
1202 pm_runtime_disable(&client->dev);
1203 pm_runtime_set_suspended(&client->dev);
1204 pm_runtime_put_noidle(&client->dev);
1207 hid_destroy_device(hid);
1209 free_irq(ihid->irq, ihid);
1212 i2c_hid_free_buffers(ihid);
1215 gpiod_put(ihid->desc);
1219 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&client->dev));
1224 static void i2c_hid_shutdown(struct i2c_client *client)
1226 struct i2c_hid *ihid = i2c_get_clientdata(client);
1228 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1229 free_irq(client->irq, ihid);
1232 #ifdef CONFIG_PM_SLEEP
1233 static int i2c_hid_suspend(struct device *dev)
1235 struct i2c_client *client = to_i2c_client(dev);
1236 struct i2c_hid *ihid = i2c_get_clientdata(client);
1237 struct hid_device *hid = ihid->hid;
1241 if (hid->driver && hid->driver->suspend) {
1243 * Wake up the device so that IO issues in
1244 * HID driver's suspend code can succeed.
1246 ret = pm_runtime_resume(dev);
1250 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1255 if (!pm_runtime_suspended(dev)) {
1256 /* Save some power */
1257 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1259 disable_irq(ihid->irq);
1262 if (device_may_wakeup(&client->dev)) {
1263 wake_status = enable_irq_wake(ihid->irq);
1265 ihid->irq_wake_enabled = true;
1267 hid_warn(hid, "Failed to enable irq wake: %d\n",
1274 static int i2c_hid_resume(struct device *dev)
1277 struct i2c_client *client = to_i2c_client(dev);
1278 struct i2c_hid *ihid = i2c_get_clientdata(client);
1279 struct hid_device *hid = ihid->hid;
1282 if (device_may_wakeup(&client->dev) && ihid->irq_wake_enabled) {
1283 wake_status = disable_irq_wake(ihid->irq);
1285 ihid->irq_wake_enabled = false;
1287 hid_warn(hid, "Failed to disable irq wake: %d\n",
1291 /* We'll resume to full power */
1292 pm_runtime_disable(dev);
1293 pm_runtime_set_active(dev);
1294 pm_runtime_enable(dev);
1296 enable_irq(ihid->irq);
1297 ret = i2c_hid_hwreset(client);
1301 if (hid->driver && hid->driver->reset_resume) {
1302 ret = hid->driver->reset_resume(hid);
1311 static int i2c_hid_runtime_suspend(struct device *dev)
1313 struct i2c_client *client = to_i2c_client(dev);
1314 struct i2c_hid *ihid = i2c_get_clientdata(client);
1316 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1317 disable_irq(ihid->irq);
1321 static int i2c_hid_runtime_resume(struct device *dev)
1323 struct i2c_client *client = to_i2c_client(dev);
1324 struct i2c_hid *ihid = i2c_get_clientdata(client);
1326 enable_irq(ihid->irq);
1327 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1332 static const struct dev_pm_ops i2c_hid_pm = {
1333 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1334 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1338 static const struct i2c_device_id i2c_hid_id_table[] = {
1340 { "hid-over-i2c", 0 },
1343 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1346 static struct i2c_driver i2c_hid_driver = {
1350 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1351 .of_match_table = of_match_ptr(i2c_hid_of_match),
1354 .probe = i2c_hid_probe,
1355 .remove = i2c_hid_remove,
1356 .shutdown = i2c_hid_shutdown,
1357 .id_table = i2c_hid_id_table,
1360 module_i2c_driver(i2c_hid_driver);
1362 MODULE_DESCRIPTION("HID over I2C core driver");
1363 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1364 MODULE_LICENSE("GPL");