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)
52 #define I2C_HID_STARTED 0
53 #define I2C_HID_RESET_PENDING 1
54 #define I2C_HID_READ_PENDING 2
56 #define I2C_HID_PWR_ON 0x00
57 #define I2C_HID_PWR_SLEEP 0x01
61 module_param(debug, bool, 0444);
62 MODULE_PARM_DESC(debug, "print a lot of debug information");
64 #define i2c_hid_dbg(ihid, fmt, arg...) \
67 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
71 __le16 wHIDDescLength;
73 __le16 wReportDescLength;
74 __le16 wReportDescRegister;
75 __le16 wInputRegister;
76 __le16 wMaxInputLength;
77 __le16 wOutputRegister;
78 __le16 wMaxOutputLength;
79 __le16 wCommandRegister;
88 unsigned int registerIndex;
103 #define I2C_HID_CMD(opcode_) \
104 .opcode = opcode_, .length = 4, \
105 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
107 /* fetch HID descriptor */
108 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
109 /* fetch report descriptors */
110 static const struct i2c_hid_cmd hid_report_descr_cmd = {
111 .registerIndex = offsetof(struct i2c_hid_desc,
112 wReportDescRegister),
116 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
118 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
119 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
120 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
121 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
124 * These definitions are not used here, but are defined by the spec.
125 * Keeping them here for documentation purposes.
127 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
128 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
129 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
130 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
133 static DEFINE_MUTEX(i2c_hid_open_mut);
135 /* The main device structure */
137 struct i2c_client *client; /* i2c client */
138 struct hid_device *hid; /* pointer to corresponding HID dev */
140 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
141 struct i2c_hid_desc hdesc; /* the HID Descriptor */
143 __le16 wHIDDescRegister; /* location of the i2c
144 * register of the HID
146 unsigned int bufsize; /* i2c buffer size */
147 u8 *inbuf; /* Input buffer */
148 u8 *rawbuf; /* Raw Input buffer */
149 u8 *cmdbuf; /* Command buffer */
150 u8 *argsbuf; /* Command arguments buffer */
152 unsigned long flags; /* device flags */
153 unsigned long quirks; /* Various quirks */
155 wait_queue_head_t wait; /* For waiting the interrupt */
157 struct i2c_hid_platform_data pdata;
159 bool irq_wake_enabled;
160 struct mutex reset_lock;
163 static const struct i2c_hid_quirks {
167 } i2c_hid_quirks[] = {
168 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
169 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
170 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
171 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
176 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
177 * @idVendor: the 16-bit vendor ID
178 * @idProduct: the 16-bit product ID
180 * Returns: a u32 quirks value.
182 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
187 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
188 if (i2c_hid_quirks[n].idVendor == idVendor &&
189 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
190 i2c_hid_quirks[n].idProduct == idProduct))
191 quirks = i2c_hid_quirks[n].quirks;
196 static int __i2c_hid_command(struct i2c_client *client,
197 const struct i2c_hid_cmd *command, u8 reportID,
198 u8 reportType, u8 *args, int args_len,
199 unsigned char *buf_recv, int data_len)
201 struct i2c_hid *ihid = i2c_get_clientdata(client);
202 union command *cmd = (union command *)ihid->cmdbuf;
204 struct i2c_msg msg[2];
207 int length = command->length;
208 bool wait = command->wait;
209 unsigned int registerIndex = command->registerIndex;
211 /* special case for hid_descr_cmd */
212 if (command == &hid_descr_cmd) {
213 cmd->c.reg = ihid->wHIDDescRegister;
215 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
216 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
220 cmd->c.opcode = command->opcode;
221 cmd->c.reportTypeID = reportID | reportType << 4;
224 memcpy(cmd->data + length, args, args_len);
227 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
229 msg[0].addr = client->addr;
230 msg[0].flags = client->flags & I2C_M_TEN;
232 msg[0].buf = cmd->data;
234 msg[1].addr = client->addr;
235 msg[1].flags = client->flags & I2C_M_TEN;
236 msg[1].flags |= I2C_M_RD;
237 msg[1].len = data_len;
238 msg[1].buf = buf_recv;
240 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
244 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
246 ret = i2c_transfer(client->adapter, msg, msg_num);
249 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
252 return ret < 0 ? ret : -EIO;
257 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
258 if (!wait_event_timeout(ihid->wait,
259 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
260 msecs_to_jiffies(5000)))
262 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
268 static int i2c_hid_command(struct i2c_client *client,
269 const struct i2c_hid_cmd *command,
270 unsigned char *buf_recv, int data_len)
272 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
276 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
277 u8 reportID, unsigned char *buf_recv, int data_len)
279 struct i2c_hid *ihid = i2c_get_clientdata(client);
283 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
285 i2c_hid_dbg(ihid, "%s\n", __func__);
287 if (reportID >= 0x0F) {
288 args[args_len++] = reportID;
292 args[args_len++] = readRegister & 0xFF;
293 args[args_len++] = readRegister >> 8;
295 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
296 reportType, args, args_len, buf_recv, data_len);
298 dev_err(&client->dev,
299 "failed to retrieve report from device.\n");
307 * i2c_hid_set_or_send_report: forward an incoming report to the device
308 * @client: the i2c_client of the device
309 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
310 * @reportID: the report ID
311 * @buf: the actual data to transfer, without the report ID
313 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
315 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
316 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
318 struct i2c_hid *ihid = i2c_get_clientdata(client);
319 u8 *args = ihid->argsbuf;
320 const struct i2c_hid_cmd *hidcmd;
322 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
323 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
324 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
329 i2c_hid_dbg(ihid, "%s\n", __func__);
331 if (data_len > ihid->bufsize)
334 size = 2 /* size */ +
335 (reportID ? 1 : 0) /* reportID */ +
337 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
338 2 /* dataRegister */ +
341 if (!use_data && maxOutputLength == 0)
344 if (reportID >= 0x0F) {
345 args[index++] = reportID;
350 * use the data register for feature reports or if the device does not
351 * support the output register
354 args[index++] = dataRegister & 0xFF;
355 args[index++] = dataRegister >> 8;
356 hidcmd = &hid_set_report_cmd;
358 args[index++] = outputRegister & 0xFF;
359 args[index++] = outputRegister >> 8;
360 hidcmd = &hid_no_cmd;
363 args[index++] = size & 0xFF;
364 args[index++] = size >> 8;
367 args[index++] = reportID;
369 memcpy(&args[index], buf, data_len);
371 ret = __i2c_hid_command(client, hidcmd, reportID,
372 reportType, args, args_len, NULL, 0);
374 dev_err(&client->dev, "failed to set a report to device.\n");
381 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
383 struct i2c_hid *ihid = i2c_get_clientdata(client);
386 i2c_hid_dbg(ihid, "%s\n", __func__);
389 * Some devices require to send a command to wakeup before power on.
390 * The call will get a return value (EREMOTEIO) but device will be
391 * triggered and activated. After that, it goes like a normal device.
393 if (power_state == I2C_HID_PWR_ON &&
394 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
395 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
397 /* Device was already activated */
402 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
403 0, NULL, 0, NULL, 0);
406 dev_err(&client->dev, "failed to change power setting.\n");
411 * The HID over I2C specification states that if a DEVICE needs time
412 * after the PWR_ON request, it should utilise CLOCK stretching.
413 * However, it has been observered that the Windows driver provides a
414 * 1ms sleep between the PWR_ON and RESET requests.
415 * According to Goodix Windows even waits 60 ms after (other?)
416 * PWR_ON requests. Testing has confirmed that several devices
417 * will not work properly without a delay after a PWR_ON request.
419 if (!ret && power_state == I2C_HID_PWR_ON)
425 static int i2c_hid_hwreset(struct i2c_client *client)
427 struct i2c_hid *ihid = i2c_get_clientdata(client);
430 i2c_hid_dbg(ihid, "%s\n", __func__);
433 * This prevents sending feature reports while the device is
434 * being reset. Otherwise we may lose the reset complete
437 mutex_lock(&ihid->reset_lock);
439 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
443 i2c_hid_dbg(ihid, "resetting...\n");
445 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
447 dev_err(&client->dev, "failed to reset device.\n");
448 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
452 mutex_unlock(&ihid->reset_lock);
456 static void i2c_hid_get_input(struct i2c_hid *ihid)
460 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
462 if (size > ihid->bufsize)
463 size = ihid->bufsize;
465 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
470 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
471 __func__, ret, size);
475 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
478 /* host or device initiated RESET completed */
479 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
480 wake_up(&ihid->wait);
484 if ((ret_size > size) || (ret_size < 2)) {
485 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
486 __func__, size, ret_size);
490 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
492 if (test_bit(I2C_HID_STARTED, &ihid->flags))
493 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
499 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
501 struct i2c_hid *ihid = dev_id;
503 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
506 i2c_hid_get_input(ihid);
511 static int i2c_hid_get_report_length(struct hid_report *report)
513 return ((report->size - 1) >> 3) + 1 +
514 report->device->report_enum[report->type].numbered + 2;
518 * Traverse the supplied list of reports and find the longest
520 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
523 struct hid_report *report;
526 /* We should not rely on wMaxInputLength, as some devices may set it to
528 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
529 size = i2c_hid_get_report_length(report);
535 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
539 kfree(ihid->argsbuf);
544 ihid->argsbuf = NULL;
548 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
550 /* the worst case is computed from the set_report command with a
551 * reportID > 15 and the maximum report length */
552 int args_len = sizeof(__u8) + /* ReportID */
553 sizeof(__u8) + /* optional ReportID byte */
554 sizeof(__u16) + /* data register */
555 sizeof(__u16) + /* size of the report */
556 report_size; /* report */
558 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
559 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
560 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
561 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
563 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
564 i2c_hid_free_buffers(ihid);
568 ihid->bufsize = report_size;
573 static int i2c_hid_get_raw_report(struct hid_device *hid,
574 unsigned char report_number, __u8 *buf, size_t count,
575 unsigned char report_type)
577 struct i2c_client *client = hid->driver_data;
578 struct i2c_hid *ihid = i2c_get_clientdata(client);
579 size_t ret_count, ask_count;
582 if (report_type == HID_OUTPUT_REPORT)
585 /* +2 bytes to include the size of the reply in the query buffer */
586 ask_count = min(count + 2, (size_t)ihid->bufsize);
588 ret = i2c_hid_get_report(client,
589 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
590 report_number, ihid->rawbuf, ask_count);
595 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
600 ret_count = min(ret_count, ask_count);
602 /* The query buffer contains the size, dropping it in the reply */
603 count = min(count, ret_count - 2);
604 memcpy(buf, ihid->rawbuf + 2, count);
609 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
610 size_t count, unsigned char report_type, bool use_data)
612 struct i2c_client *client = hid->driver_data;
613 struct i2c_hid *ihid = i2c_get_clientdata(client);
614 int report_id = buf[0];
617 if (report_type == HID_INPUT_REPORT)
620 mutex_lock(&ihid->reset_lock);
627 ret = i2c_hid_set_or_send_report(client,
628 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
629 report_id, buf, count, use_data);
631 if (report_id && ret >= 0)
632 ret++; /* add report_id to the number of transfered bytes */
634 mutex_unlock(&ihid->reset_lock);
639 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
642 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
646 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
647 __u8 *buf, size_t len, unsigned char rtype,
651 case HID_REQ_GET_REPORT:
652 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
653 case HID_REQ_SET_REPORT:
654 if (buf[0] != reportnum)
656 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
662 static int i2c_hid_parse(struct hid_device *hid)
664 struct i2c_client *client = hid->driver_data;
665 struct i2c_hid *ihid = i2c_get_clientdata(client);
666 struct i2c_hid_desc *hdesc = &ihid->hdesc;
673 i2c_hid_dbg(ihid, "entering %s\n", __func__);
675 rsize = le16_to_cpu(hdesc->wReportDescLength);
676 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
677 dbg_hid("weird size of report descriptor (%u)\n", rsize);
682 ret = i2c_hid_hwreset(client);
685 } while (tries-- > 0 && ret);
690 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
694 rdesc = use_override;
695 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
697 rdesc = kzalloc(rsize, GFP_KERNEL);
700 dbg_hid("couldn't allocate rdesc memory\n");
704 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
706 ret = i2c_hid_command(client, &hid_report_descr_cmd,
709 hid_err(hid, "reading report descriptor failed\n");
715 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
717 ret = hid_parse_report(hid, rdesc, rsize);
722 dbg_hid("parsing report descriptor failed\n");
729 static int i2c_hid_start(struct hid_device *hid)
731 struct i2c_client *client = hid->driver_data;
732 struct i2c_hid *ihid = i2c_get_clientdata(client);
734 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
736 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
737 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
738 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
740 if (bufsize > ihid->bufsize) {
741 disable_irq(client->irq);
742 i2c_hid_free_buffers(ihid);
744 ret = i2c_hid_alloc_buffers(ihid, bufsize);
745 enable_irq(client->irq);
754 static void i2c_hid_stop(struct hid_device *hid)
759 static int i2c_hid_open(struct hid_device *hid)
761 struct i2c_client *client = hid->driver_data;
762 struct i2c_hid *ihid = i2c_get_clientdata(client);
765 ret = pm_runtime_get_sync(&client->dev);
769 set_bit(I2C_HID_STARTED, &ihid->flags);
773 static void i2c_hid_close(struct hid_device *hid)
775 struct i2c_client *client = hid->driver_data;
776 struct i2c_hid *ihid = i2c_get_clientdata(client);
778 clear_bit(I2C_HID_STARTED, &ihid->flags);
780 /* Save some power */
781 pm_runtime_put(&client->dev);
784 static int i2c_hid_power(struct hid_device *hid, int lvl)
786 struct i2c_client *client = hid->driver_data;
787 struct i2c_hid *ihid = i2c_get_clientdata(client);
789 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
793 pm_runtime_get_sync(&client->dev);
796 pm_runtime_put(&client->dev);
802 struct hid_ll_driver i2c_hid_ll_driver = {
803 .parse = i2c_hid_parse,
804 .start = i2c_hid_start,
805 .stop = i2c_hid_stop,
806 .open = i2c_hid_open,
807 .close = i2c_hid_close,
808 .power = i2c_hid_power,
809 .output_report = i2c_hid_output_report,
810 .raw_request = i2c_hid_raw_request,
812 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
814 static int i2c_hid_init_irq(struct i2c_client *client)
816 struct i2c_hid *ihid = i2c_get_clientdata(client);
817 unsigned long irqflags = 0;
820 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
822 if (!irq_get_trigger_type(client->irq))
823 irqflags = IRQF_TRIGGER_LOW;
825 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
826 irqflags | IRQF_ONESHOT, client->name, ihid);
828 dev_warn(&client->dev,
829 "Could not register for %s interrupt, irq = %d,"
831 client->name, client->irq, ret);
839 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
841 struct i2c_client *client = ihid->client;
842 struct i2c_hid_desc *hdesc = &ihid->hdesc;
846 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
847 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
848 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
850 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
852 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
853 ret = i2c_hid_command(client, &hid_descr_cmd,
855 sizeof(struct i2c_hid_desc));
857 dev_err(&client->dev, "hid_descr_cmd failed\n");
862 /* Validate the length of HID descriptor, the 4 first bytes:
863 * bytes 0-1 -> length
864 * bytes 2-3 -> bcdVersion (has to be 1.00) */
865 /* check bcdVersion == 1.0 */
866 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
867 dev_err(&client->dev,
868 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
869 le16_to_cpu(hdesc->bcdVersion));
873 /* Descriptor length should be 30 bytes as per the specification */
874 dsize = le16_to_cpu(hdesc->wHIDDescLength);
875 if (dsize != sizeof(struct i2c_hid_desc)) {
876 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
880 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
885 static int i2c_hid_acpi_pdata(struct i2c_client *client,
886 struct i2c_hid_platform_data *pdata)
888 static guid_t i2c_hid_guid =
889 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
890 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
891 union acpi_object *obj;
892 struct acpi_device *adev;
895 handle = ACPI_HANDLE(&client->dev);
896 if (!handle || acpi_bus_get_device(handle, &adev))
899 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
902 dev_err(&client->dev, "device _DSM execution failed\n");
906 pdata->hid_descriptor_address = obj->integer.value;
912 static void i2c_hid_acpi_fix_up_power(struct device *dev)
914 acpi_handle handle = ACPI_HANDLE(dev);
915 struct acpi_device *adev;
917 if (handle && acpi_bus_get_device(handle, &adev) == 0)
918 acpi_device_fix_up_power(adev);
921 static const struct acpi_device_id i2c_hid_acpi_match[] = {
926 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
928 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
929 struct i2c_hid_platform_data *pdata)
934 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
938 static int i2c_hid_of_probe(struct i2c_client *client,
939 struct i2c_hid_platform_data *pdata)
941 struct device *dev = &client->dev;
945 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
947 dev_err(&client->dev, "HID register address not provided\n");
951 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
955 pdata->hid_descriptor_address = val;
957 ret = of_property_read_u32(dev->of_node, "post-power-on-delay-ms",
960 pdata->post_power_delay_ms = val;
965 static const struct of_device_id i2c_hid_of_match[] = {
966 { .compatible = "hid-over-i2c" },
969 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
971 static inline int i2c_hid_of_probe(struct i2c_client *client,
972 struct i2c_hid_platform_data *pdata)
978 static int i2c_hid_probe(struct i2c_client *client,
979 const struct i2c_device_id *dev_id)
982 struct i2c_hid *ihid;
983 struct hid_device *hid;
985 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
987 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
990 dev_err(&client->dev,
991 "HID over i2c has not been provided an Int IRQ\n");
995 if (client->irq < 0) {
996 if (client->irq != -EPROBE_DEFER)
997 dev_err(&client->dev,
998 "HID over i2c doesn't have a valid IRQ\n");
1002 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
1006 if (client->dev.of_node) {
1007 ret = i2c_hid_of_probe(client, &ihid->pdata);
1010 } else if (!platform_data) {
1011 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1013 dev_err(&client->dev,
1014 "HID register address not provided\n");
1018 ihid->pdata = *platform_data;
1021 ihid->pdata.supply = devm_regulator_get(&client->dev, "vdd");
1022 if (IS_ERR(ihid->pdata.supply)) {
1023 ret = PTR_ERR(ihid->pdata.supply);
1024 if (ret != -EPROBE_DEFER)
1025 dev_err(&client->dev, "Failed to get regulator: %d\n",
1030 ret = regulator_enable(ihid->pdata.supply);
1032 dev_err(&client->dev, "Failed to enable regulator: %d\n",
1036 if (ihid->pdata.post_power_delay_ms)
1037 msleep(ihid->pdata.post_power_delay_ms);
1039 i2c_set_clientdata(client, ihid);
1041 ihid->client = client;
1043 hidRegister = ihid->pdata.hid_descriptor_address;
1044 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1046 init_waitqueue_head(&ihid->wait);
1047 mutex_init(&ihid->reset_lock);
1049 /* we need to allocate the command buffer without knowing the maximum
1050 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1051 * real computation later. */
1052 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1056 i2c_hid_acpi_fix_up_power(&client->dev);
1058 pm_runtime_get_noresume(&client->dev);
1059 pm_runtime_set_active(&client->dev);
1060 pm_runtime_enable(&client->dev);
1061 device_enable_async_suspend(&client->dev);
1063 /* Make sure there is something at this address */
1064 ret = i2c_smbus_read_byte(client);
1066 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1071 ret = i2c_hid_fetch_hid_descriptor(ihid);
1075 ret = i2c_hid_init_irq(client);
1079 hid = hid_allocate_device();
1087 hid->driver_data = client;
1088 hid->ll_driver = &i2c_hid_ll_driver;
1089 hid->dev.parent = &client->dev;
1091 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1092 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1093 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1095 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1096 client->name, (u16)hid->vendor, (u16)hid->product);
1097 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1099 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1101 ret = hid_add_device(hid);
1104 hid_err(client, "can't add hid device: %d\n", ret);
1108 pm_runtime_put(&client->dev);
1112 hid_destroy_device(hid);
1115 free_irq(client->irq, ihid);
1118 pm_runtime_put_noidle(&client->dev);
1119 pm_runtime_disable(&client->dev);
1122 regulator_disable(ihid->pdata.supply);
1125 i2c_hid_free_buffers(ihid);
1130 static int i2c_hid_remove(struct i2c_client *client)
1132 struct i2c_hid *ihid = i2c_get_clientdata(client);
1133 struct hid_device *hid;
1135 pm_runtime_get_sync(&client->dev);
1136 pm_runtime_disable(&client->dev);
1137 pm_runtime_set_suspended(&client->dev);
1138 pm_runtime_put_noidle(&client->dev);
1141 hid_destroy_device(hid);
1143 free_irq(client->irq, ihid);
1146 i2c_hid_free_buffers(ihid);
1148 regulator_disable(ihid->pdata.supply);
1155 static void i2c_hid_shutdown(struct i2c_client *client)
1157 struct i2c_hid *ihid = i2c_get_clientdata(client);
1159 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1160 free_irq(client->irq, ihid);
1163 #ifdef CONFIG_PM_SLEEP
1164 static int i2c_hid_suspend(struct device *dev)
1166 struct i2c_client *client = to_i2c_client(dev);
1167 struct i2c_hid *ihid = i2c_get_clientdata(client);
1168 struct hid_device *hid = ihid->hid;
1172 if (hid->driver && hid->driver->suspend) {
1174 * Wake up the device so that IO issues in
1175 * HID driver's suspend code can succeed.
1177 ret = pm_runtime_resume(dev);
1181 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1186 if (!pm_runtime_suspended(dev)) {
1187 /* Save some power */
1188 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1190 disable_irq(client->irq);
1193 if (device_may_wakeup(&client->dev)) {
1194 wake_status = enable_irq_wake(client->irq);
1196 ihid->irq_wake_enabled = true;
1198 hid_warn(hid, "Failed to enable irq wake: %d\n",
1201 ret = regulator_disable(ihid->pdata.supply);
1203 hid_warn(hid, "Failed to disable supply: %d\n", ret);
1209 static int i2c_hid_resume(struct device *dev)
1212 struct i2c_client *client = to_i2c_client(dev);
1213 struct i2c_hid *ihid = i2c_get_clientdata(client);
1214 struct hid_device *hid = ihid->hid;
1217 if (!device_may_wakeup(&client->dev)) {
1218 ret = regulator_enable(ihid->pdata.supply);
1220 hid_warn(hid, "Failed to enable supply: %d\n", ret);
1221 if (ihid->pdata.post_power_delay_ms)
1222 msleep(ihid->pdata.post_power_delay_ms);
1223 } else if (ihid->irq_wake_enabled) {
1224 wake_status = disable_irq_wake(client->irq);
1226 ihid->irq_wake_enabled = false;
1228 hid_warn(hid, "Failed to disable irq wake: %d\n",
1232 /* We'll resume to full power */
1233 pm_runtime_disable(dev);
1234 pm_runtime_set_active(dev);
1235 pm_runtime_enable(dev);
1237 enable_irq(client->irq);
1238 ret = i2c_hid_hwreset(client);
1242 if (hid->driver && hid->driver->reset_resume) {
1243 ret = hid->driver->reset_resume(hid);
1252 static int i2c_hid_runtime_suspend(struct device *dev)
1254 struct i2c_client *client = to_i2c_client(dev);
1256 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1257 disable_irq(client->irq);
1261 static int i2c_hid_runtime_resume(struct device *dev)
1263 struct i2c_client *client = to_i2c_client(dev);
1265 enable_irq(client->irq);
1266 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1271 static const struct dev_pm_ops i2c_hid_pm = {
1272 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1273 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1277 static const struct i2c_device_id i2c_hid_id_table[] = {
1279 { "hid-over-i2c", 0 },
1282 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1285 static struct i2c_driver i2c_hid_driver = {
1289 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1290 .of_match_table = of_match_ptr(i2c_hid_of_match),
1293 .probe = i2c_hid_probe,
1294 .remove = i2c_hid_remove,
1295 .shutdown = i2c_hid_shutdown,
1296 .id_table = i2c_hid_id_table,
1299 module_i2c_driver(i2c_hid_driver);
1301 MODULE_DESCRIPTION("HID over I2C core driver");
1302 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1303 MODULE_LICENSE("GPL");