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/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/regulator/consumer.h>
42 #include <linux/platform_data/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)
49 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
50 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
51 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
54 #define I2C_HID_STARTED 0
55 #define I2C_HID_RESET_PENDING 1
56 #define I2C_HID_READ_PENDING 2
58 #define I2C_HID_PWR_ON 0x00
59 #define I2C_HID_PWR_SLEEP 0x01
63 module_param(debug, bool, 0444);
64 MODULE_PARM_DESC(debug, "print a lot of debug information");
66 #define i2c_hid_dbg(ihid, fmt, arg...) \
69 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
73 __le16 wHIDDescLength;
75 __le16 wReportDescLength;
76 __le16 wReportDescRegister;
77 __le16 wInputRegister;
78 __le16 wMaxInputLength;
79 __le16 wOutputRegister;
80 __le16 wMaxOutputLength;
81 __le16 wCommandRegister;
90 unsigned int registerIndex;
105 #define I2C_HID_CMD(opcode_) \
106 .opcode = opcode_, .length = 4, \
107 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
109 /* fetch HID descriptor */
110 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
111 /* fetch report descriptors */
112 static const struct i2c_hid_cmd hid_report_descr_cmd = {
113 .registerIndex = offsetof(struct i2c_hid_desc,
114 wReportDescRegister),
118 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
120 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
121 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
122 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
123 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
126 * These definitions are not used here, but are defined by the spec.
127 * Keeping them here for documentation purposes.
129 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
130 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
131 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
132 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
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, HID_ANY_ID,
169 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
170 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
171 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
172 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
173 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
174 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
175 I2C_HID_QUIRK_BOGUS_IRQ },
176 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
177 I2C_HID_QUIRK_RESET_ON_RESUME },
182 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
183 * @idVendor: the 16-bit vendor ID
184 * @idProduct: the 16-bit product ID
186 * Returns: a u32 quirks value.
188 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
193 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
194 if (i2c_hid_quirks[n].idVendor == idVendor &&
195 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
196 i2c_hid_quirks[n].idProduct == idProduct))
197 quirks = i2c_hid_quirks[n].quirks;
202 static int __i2c_hid_command(struct i2c_client *client,
203 const struct i2c_hid_cmd *command, u8 reportID,
204 u8 reportType, u8 *args, int args_len,
205 unsigned char *buf_recv, int data_len)
207 struct i2c_hid *ihid = i2c_get_clientdata(client);
208 union command *cmd = (union command *)ihid->cmdbuf;
210 struct i2c_msg msg[2];
213 int length = command->length;
214 bool wait = command->wait;
215 unsigned int registerIndex = command->registerIndex;
217 /* special case for hid_descr_cmd */
218 if (command == &hid_descr_cmd) {
219 cmd->c.reg = ihid->wHIDDescRegister;
221 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
222 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
226 cmd->c.opcode = command->opcode;
227 cmd->c.reportTypeID = reportID | reportType << 4;
230 memcpy(cmd->data + length, args, args_len);
233 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
235 msg[0].addr = client->addr;
236 msg[0].flags = client->flags & I2C_M_TEN;
238 msg[0].buf = cmd->data;
240 msg[1].addr = client->addr;
241 msg[1].flags = client->flags & I2C_M_TEN;
242 msg[1].flags |= I2C_M_RD;
243 msg[1].len = data_len;
244 msg[1].buf = buf_recv;
246 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
250 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
252 ret = i2c_transfer(client->adapter, msg, msg_num);
255 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
258 return ret < 0 ? ret : -EIO;
262 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
265 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
266 if (!wait_event_timeout(ihid->wait,
267 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
268 msecs_to_jiffies(5000)))
270 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
276 static int i2c_hid_command(struct i2c_client *client,
277 const struct i2c_hid_cmd *command,
278 unsigned char *buf_recv, int data_len)
280 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
284 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
285 u8 reportID, unsigned char *buf_recv, int data_len)
287 struct i2c_hid *ihid = i2c_get_clientdata(client);
291 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
293 i2c_hid_dbg(ihid, "%s\n", __func__);
295 if (reportID >= 0x0F) {
296 args[args_len++] = reportID;
300 args[args_len++] = readRegister & 0xFF;
301 args[args_len++] = readRegister >> 8;
303 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
304 reportType, args, args_len, buf_recv, data_len);
306 dev_err(&client->dev,
307 "failed to retrieve report from device.\n");
315 * i2c_hid_set_or_send_report: forward an incoming report to the device
316 * @client: the i2c_client of the device
317 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
318 * @reportID: the report ID
319 * @buf: the actual data to transfer, without the report ID
321 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
323 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
324 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
326 struct i2c_hid *ihid = i2c_get_clientdata(client);
327 u8 *args = ihid->argsbuf;
328 const struct i2c_hid_cmd *hidcmd;
330 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
331 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
332 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
337 i2c_hid_dbg(ihid, "%s\n", __func__);
339 if (data_len > ihid->bufsize)
342 size = 2 /* size */ +
343 (reportID ? 1 : 0) /* reportID */ +
345 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
346 2 /* dataRegister */ +
349 if (!use_data && maxOutputLength == 0)
352 if (reportID >= 0x0F) {
353 args[index++] = reportID;
358 * use the data register for feature reports or if the device does not
359 * support the output register
362 args[index++] = dataRegister & 0xFF;
363 args[index++] = dataRegister >> 8;
364 hidcmd = &hid_set_report_cmd;
366 args[index++] = outputRegister & 0xFF;
367 args[index++] = outputRegister >> 8;
368 hidcmd = &hid_no_cmd;
371 args[index++] = size & 0xFF;
372 args[index++] = size >> 8;
375 args[index++] = reportID;
377 memcpy(&args[index], buf, data_len);
379 ret = __i2c_hid_command(client, hidcmd, reportID,
380 reportType, args, args_len, NULL, 0);
382 dev_err(&client->dev, "failed to set a report to device.\n");
389 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
391 struct i2c_hid *ihid = i2c_get_clientdata(client);
394 i2c_hid_dbg(ihid, "%s\n", __func__);
397 * Some devices require to send a command to wakeup before power on.
398 * The call will get a return value (EREMOTEIO) but device will be
399 * triggered and activated. After that, it goes like a normal device.
401 if (power_state == I2C_HID_PWR_ON &&
402 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
403 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
405 /* Device was already activated */
410 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
411 0, NULL, 0, NULL, 0);
414 dev_err(&client->dev, "failed to change power setting.\n");
420 static int i2c_hid_hwreset(struct i2c_client *client)
422 struct i2c_hid *ihid = i2c_get_clientdata(client);
425 i2c_hid_dbg(ihid, "%s\n", __func__);
428 * This prevents sending feature reports while the device is
429 * being reset. Otherwise we may lose the reset complete
432 mutex_lock(&ihid->reset_lock);
434 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
439 * The HID over I2C specification states that if a DEVICE needs time
440 * after the PWR_ON request, it should utilise CLOCK stretching.
441 * However, it has been observered that the Windows driver provides a
442 * 1ms sleep between the PWR_ON and RESET requests and that some devices
445 usleep_range(1000, 5000);
447 i2c_hid_dbg(ihid, "resetting...\n");
449 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
451 dev_err(&client->dev, "failed to reset device.\n");
452 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
456 /* At least some SIS devices need this after reset */
457 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
460 mutex_unlock(&ihid->reset_lock);
464 static void i2c_hid_get_input(struct i2c_hid *ihid)
468 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
470 if (size > ihid->bufsize)
471 size = ihid->bufsize;
473 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
478 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
479 __func__, ret, size);
483 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
486 /* host or device initiated RESET completed */
487 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
488 wake_up(&ihid->wait);
492 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
493 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
494 "there's no data\n", __func__);
498 if ((ret_size > size) || (ret_size < 2)) {
499 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
500 __func__, size, ret_size);
504 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
506 if (test_bit(I2C_HID_STARTED, &ihid->flags))
507 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
513 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
515 struct i2c_hid *ihid = dev_id;
517 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
520 i2c_hid_get_input(ihid);
525 static int i2c_hid_get_report_length(struct hid_report *report)
527 return ((report->size - 1) >> 3) + 1 +
528 report->device->report_enum[report->type].numbered + 2;
532 * Traverse the supplied list of reports and find the longest
534 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
537 struct hid_report *report;
540 /* We should not rely on wMaxInputLength, as some devices may set it to
542 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
543 size = i2c_hid_get_report_length(report);
549 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
553 kfree(ihid->argsbuf);
558 ihid->argsbuf = NULL;
562 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
564 /* the worst case is computed from the set_report command with a
565 * reportID > 15 and the maximum report length */
566 int args_len = sizeof(__u8) + /* ReportID */
567 sizeof(__u8) + /* optional ReportID byte */
568 sizeof(__u16) + /* data register */
569 sizeof(__u16) + /* size of the report */
570 report_size; /* report */
572 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
573 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
574 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
575 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
577 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
578 i2c_hid_free_buffers(ihid);
582 ihid->bufsize = report_size;
587 static int i2c_hid_get_raw_report(struct hid_device *hid,
588 unsigned char report_number, __u8 *buf, size_t count,
589 unsigned char report_type)
591 struct i2c_client *client = hid->driver_data;
592 struct i2c_hid *ihid = i2c_get_clientdata(client);
593 size_t ret_count, ask_count;
596 if (report_type == HID_OUTPUT_REPORT)
599 /* +2 bytes to include the size of the reply in the query buffer */
600 ask_count = min(count + 2, (size_t)ihid->bufsize);
602 ret = i2c_hid_get_report(client,
603 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
604 report_number, ihid->rawbuf, ask_count);
609 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
614 ret_count = min(ret_count, ask_count);
616 /* The query buffer contains the size, dropping it in the reply */
617 count = min(count, ret_count - 2);
618 memcpy(buf, ihid->rawbuf + 2, count);
623 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
624 size_t count, unsigned char report_type, bool use_data)
626 struct i2c_client *client = hid->driver_data;
627 struct i2c_hid *ihid = i2c_get_clientdata(client);
628 int report_id = buf[0];
631 if (report_type == HID_INPUT_REPORT)
634 mutex_lock(&ihid->reset_lock);
641 ret = i2c_hid_set_or_send_report(client,
642 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
643 report_id, buf, count, use_data);
645 if (report_id && ret >= 0)
646 ret++; /* add report_id to the number of transfered bytes */
648 mutex_unlock(&ihid->reset_lock);
653 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
656 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
660 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
661 __u8 *buf, size_t len, unsigned char rtype,
665 case HID_REQ_GET_REPORT:
666 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
667 case HID_REQ_SET_REPORT:
668 if (buf[0] != reportnum)
670 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
676 static int i2c_hid_parse(struct hid_device *hid)
678 struct i2c_client *client = hid->driver_data;
679 struct i2c_hid *ihid = i2c_get_clientdata(client);
680 struct i2c_hid_desc *hdesc = &ihid->hdesc;
687 i2c_hid_dbg(ihid, "entering %s\n", __func__);
689 rsize = le16_to_cpu(hdesc->wReportDescLength);
690 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
691 dbg_hid("weird size of report descriptor (%u)\n", rsize);
696 ret = i2c_hid_hwreset(client);
699 } while (tries-- > 0 && ret);
704 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
708 rdesc = use_override;
709 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
711 rdesc = kzalloc(rsize, GFP_KERNEL);
714 dbg_hid("couldn't allocate rdesc memory\n");
718 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
720 ret = i2c_hid_command(client, &hid_report_descr_cmd,
723 hid_err(hid, "reading report descriptor failed\n");
729 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
731 ret = hid_parse_report(hid, rdesc, rsize);
736 dbg_hid("parsing report descriptor failed\n");
743 static int i2c_hid_start(struct hid_device *hid)
745 struct i2c_client *client = hid->driver_data;
746 struct i2c_hid *ihid = i2c_get_clientdata(client);
748 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
750 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
751 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
752 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
754 if (bufsize > ihid->bufsize) {
755 disable_irq(client->irq);
756 i2c_hid_free_buffers(ihid);
758 ret = i2c_hid_alloc_buffers(ihid, bufsize);
759 enable_irq(client->irq);
768 static void i2c_hid_stop(struct hid_device *hid)
773 static int i2c_hid_open(struct hid_device *hid)
775 struct i2c_client *client = hid->driver_data;
776 struct i2c_hid *ihid = i2c_get_clientdata(client);
778 set_bit(I2C_HID_STARTED, &ihid->flags);
782 static void i2c_hid_close(struct hid_device *hid)
784 struct i2c_client *client = hid->driver_data;
785 struct i2c_hid *ihid = i2c_get_clientdata(client);
787 clear_bit(I2C_HID_STARTED, &ihid->flags);
790 struct hid_ll_driver i2c_hid_ll_driver = {
791 .parse = i2c_hid_parse,
792 .start = i2c_hid_start,
793 .stop = i2c_hid_stop,
794 .open = i2c_hid_open,
795 .close = i2c_hid_close,
796 .output_report = i2c_hid_output_report,
797 .raw_request = i2c_hid_raw_request,
799 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
801 static int i2c_hid_init_irq(struct i2c_client *client)
803 struct i2c_hid *ihid = i2c_get_clientdata(client);
804 unsigned long irqflags = 0;
807 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
809 if (!irq_get_trigger_type(client->irq))
810 irqflags = IRQF_TRIGGER_LOW;
812 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
813 irqflags | IRQF_ONESHOT, client->name, ihid);
815 dev_warn(&client->dev,
816 "Could not register for %s interrupt, irq = %d,"
818 client->name, client->irq, ret);
826 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
828 struct i2c_client *client = ihid->client;
829 struct i2c_hid_desc *hdesc = &ihid->hdesc;
833 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
834 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
835 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
837 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
839 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
840 ret = i2c_hid_command(client, &hid_descr_cmd,
842 sizeof(struct i2c_hid_desc));
844 dev_err(&client->dev, "hid_descr_cmd failed\n");
849 /* Validate the length of HID descriptor, the 4 first bytes:
850 * bytes 0-1 -> length
851 * bytes 2-3 -> bcdVersion (has to be 1.00) */
852 /* check bcdVersion == 1.0 */
853 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
854 dev_err(&client->dev,
855 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
856 le16_to_cpu(hdesc->bcdVersion));
860 /* Descriptor length should be 30 bytes as per the specification */
861 dsize = le16_to_cpu(hdesc->wHIDDescLength);
862 if (dsize != sizeof(struct i2c_hid_desc)) {
863 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
867 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
872 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
874 * The CHPN0001 ACPI device, which is used to describe the Chipone
875 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
881 static int i2c_hid_acpi_pdata(struct i2c_client *client,
882 struct i2c_hid_platform_data *pdata)
884 static guid_t i2c_hid_guid =
885 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
886 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
887 union acpi_object *obj;
888 struct acpi_device *adev;
891 handle = ACPI_HANDLE(&client->dev);
892 if (!handle || acpi_bus_get_device(handle, &adev)) {
893 dev_err(&client->dev, "Error could not get ACPI device\n");
897 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
900 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
903 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
907 pdata->hid_descriptor_address = obj->integer.value;
913 static void i2c_hid_acpi_fix_up_power(struct device *dev)
915 struct acpi_device *adev;
917 adev = ACPI_COMPANION(dev);
919 acpi_device_fix_up_power(adev);
922 static const struct acpi_device_id i2c_hid_acpi_match[] = {
927 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
929 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
930 struct i2c_hid_platform_data *pdata)
935 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
939 static int i2c_hid_of_probe(struct i2c_client *client,
940 struct i2c_hid_platform_data *pdata)
942 struct device *dev = &client->dev;
946 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
948 dev_err(&client->dev, "HID register address not provided\n");
952 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
956 pdata->hid_descriptor_address = val;
961 static const struct of_device_id i2c_hid_of_match[] = {
962 { .compatible = "hid-over-i2c" },
965 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
967 static inline int i2c_hid_of_probe(struct i2c_client *client,
968 struct i2c_hid_platform_data *pdata)
974 static void i2c_hid_fwnode_probe(struct i2c_client *client,
975 struct i2c_hid_platform_data *pdata)
979 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
981 pdata->post_power_delay_ms = val;
984 static int i2c_hid_probe(struct i2c_client *client,
985 const struct i2c_device_id *dev_id)
988 struct i2c_hid *ihid;
989 struct hid_device *hid;
991 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
993 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
996 dev_err(&client->dev,
997 "HID over i2c has not been provided an Int IRQ\n");
1001 if (client->irq < 0) {
1002 if (client->irq != -EPROBE_DEFER)
1003 dev_err(&client->dev,
1004 "HID over i2c doesn't have a valid IRQ\n");
1008 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1012 if (client->dev.of_node) {
1013 ret = i2c_hid_of_probe(client, &ihid->pdata);
1016 } else if (!platform_data) {
1017 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1021 ihid->pdata = *platform_data;
1024 /* Parse platform agnostic common properties from ACPI / device tree */
1025 i2c_hid_fwnode_probe(client, &ihid->pdata);
1027 ihid->pdata.supplies[0].supply = "vdd";
1028 ihid->pdata.supplies[1].supply = "vddl";
1030 ret = devm_regulator_bulk_get(&client->dev,
1031 ARRAY_SIZE(ihid->pdata.supplies),
1032 ihid->pdata.supplies);
1036 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1037 ihid->pdata.supplies);
1041 if (ihid->pdata.post_power_delay_ms)
1042 msleep(ihid->pdata.post_power_delay_ms);
1044 i2c_set_clientdata(client, ihid);
1046 ihid->client = client;
1048 hidRegister = ihid->pdata.hid_descriptor_address;
1049 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1051 init_waitqueue_head(&ihid->wait);
1052 mutex_init(&ihid->reset_lock);
1054 /* we need to allocate the command buffer without knowing the maximum
1055 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1056 * real computation later. */
1057 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1061 i2c_hid_acpi_fix_up_power(&client->dev);
1063 device_enable_async_suspend(&client->dev);
1065 /* Make sure there is something at this address */
1066 ret = i2c_smbus_read_byte(client);
1068 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1073 ret = i2c_hid_fetch_hid_descriptor(ihid);
1077 ret = i2c_hid_init_irq(client);
1081 hid = hid_allocate_device();
1089 hid->driver_data = client;
1090 hid->ll_driver = &i2c_hid_ll_driver;
1091 hid->dev.parent = &client->dev;
1093 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1094 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1095 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1097 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1098 client->name, hid->vendor, hid->product);
1099 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1101 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1103 ret = hid_add_device(hid);
1106 hid_err(client, "can't add hid device: %d\n", ret);
1113 hid_destroy_device(hid);
1116 free_irq(client->irq, ihid);
1119 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1120 ihid->pdata.supplies);
1121 i2c_hid_free_buffers(ihid);
1125 static int i2c_hid_remove(struct i2c_client *client)
1127 struct i2c_hid *ihid = i2c_get_clientdata(client);
1128 struct hid_device *hid;
1131 hid_destroy_device(hid);
1133 free_irq(client->irq, ihid);
1136 i2c_hid_free_buffers(ihid);
1138 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1139 ihid->pdata.supplies);
1144 static void i2c_hid_shutdown(struct i2c_client *client)
1146 struct i2c_hid *ihid = i2c_get_clientdata(client);
1148 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1149 free_irq(client->irq, ihid);
1152 #ifdef CONFIG_PM_SLEEP
1153 static int i2c_hid_suspend(struct device *dev)
1155 struct i2c_client *client = to_i2c_client(dev);
1156 struct i2c_hid *ihid = i2c_get_clientdata(client);
1157 struct hid_device *hid = ihid->hid;
1161 if (hid->driver && hid->driver->suspend) {
1162 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1167 /* Save some power */
1168 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1170 disable_irq(client->irq);
1172 if (device_may_wakeup(&client->dev)) {
1173 wake_status = enable_irq_wake(client->irq);
1175 ihid->irq_wake_enabled = true;
1177 hid_warn(hid, "Failed to enable irq wake: %d\n",
1180 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1181 ihid->pdata.supplies);
1187 static int i2c_hid_resume(struct device *dev)
1190 struct i2c_client *client = to_i2c_client(dev);
1191 struct i2c_hid *ihid = i2c_get_clientdata(client);
1192 struct hid_device *hid = ihid->hid;
1195 if (!device_may_wakeup(&client->dev)) {
1196 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1197 ihid->pdata.supplies);
1199 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1201 if (ihid->pdata.post_power_delay_ms)
1202 msleep(ihid->pdata.post_power_delay_ms);
1203 } else if (ihid->irq_wake_enabled) {
1204 wake_status = disable_irq_wake(client->irq);
1206 ihid->irq_wake_enabled = false;
1208 hid_warn(hid, "Failed to disable irq wake: %d\n",
1212 enable_irq(client->irq);
1214 /* Instead of resetting device, simply powers the device on. This
1215 * solves "incomplete reports" on Raydium devices 2386:3118 and
1216 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1217 * data after a suspend/resume.
1219 * However some ALPS touchpads generate IRQ storm without reset, so
1220 * let's still reset them here.
1222 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1223 ret = i2c_hid_hwreset(client);
1225 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1230 if (hid->driver && hid->driver->reset_resume) {
1231 ret = hid->driver->reset_resume(hid);
1239 static const struct dev_pm_ops i2c_hid_pm = {
1240 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1243 static const struct i2c_device_id i2c_hid_id_table[] = {
1245 { "hid-over-i2c", 0 },
1248 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1251 static struct i2c_driver i2c_hid_driver = {
1255 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1256 .of_match_table = of_match_ptr(i2c_hid_of_match),
1259 .probe = i2c_hid_probe,
1260 .remove = i2c_hid_remove,
1261 .shutdown = i2c_hid_shutdown,
1262 .id_table = i2c_hid_id_table,
1265 module_i2c_driver(i2c_hid_driver);
1267 MODULE_DESCRIPTION("HID over I2C core driver");
1268 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1269 MODULE_LICENSE("GPL");