2 * HID over I2C protocol implementation
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
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>
39 #include "../hid-ids.h"
42 /* quirks to control the device */
43 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
44 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
45 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
46 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
47 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
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 /* The main device structure */
134 struct i2c_client *client; /* i2c client */
135 struct hid_device *hid; /* pointer to corresponding HID dev */
137 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
138 struct i2c_hid_desc hdesc; /* the HID Descriptor */
140 __le16 wHIDDescRegister; /* location of the i2c
141 * register of the HID
143 unsigned int bufsize; /* i2c buffer size */
144 u8 *inbuf; /* Input buffer */
145 u8 *rawbuf; /* Raw Input buffer */
146 u8 *cmdbuf; /* Command buffer */
147 u8 *argsbuf; /* Command arguments buffer */
149 unsigned long flags; /* device flags */
150 unsigned long quirks; /* Various quirks */
152 wait_queue_head_t wait; /* For waiting the interrupt */
154 bool irq_wake_enabled;
155 struct mutex reset_lock;
157 struct i2chid_ops *ops;
160 static const struct i2c_hid_quirks {
164 } i2c_hid_quirks[] = {
165 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
166 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
167 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
168 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
169 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
170 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
171 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
172 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
173 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
174 I2C_HID_QUIRK_BOGUS_IRQ },
175 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
176 I2C_HID_QUIRK_RESET_ON_RESUME },
177 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
178 I2C_HID_QUIRK_RESET_ON_RESUME },
179 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
180 I2C_HID_QUIRK_BAD_INPUT_SIZE },
185 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
186 * @idVendor: the 16-bit vendor ID
187 * @idProduct: the 16-bit product ID
189 * Returns: a u32 quirks value.
191 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
196 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
197 if (i2c_hid_quirks[n].idVendor == idVendor &&
198 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
199 i2c_hid_quirks[n].idProduct == idProduct))
200 quirks = i2c_hid_quirks[n].quirks;
205 static int __i2c_hid_command(struct i2c_client *client,
206 const struct i2c_hid_cmd *command, u8 reportID,
207 u8 reportType, u8 *args, int args_len,
208 unsigned char *buf_recv, int data_len)
210 struct i2c_hid *ihid = i2c_get_clientdata(client);
211 union command *cmd = (union command *)ihid->cmdbuf;
213 struct i2c_msg msg[2];
216 int length = command->length;
217 bool wait = command->wait;
218 unsigned int registerIndex = command->registerIndex;
220 /* special case for hid_descr_cmd */
221 if (command == &hid_descr_cmd) {
222 cmd->c.reg = ihid->wHIDDescRegister;
224 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
225 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
229 cmd->c.opcode = command->opcode;
230 cmd->c.reportTypeID = reportID | reportType << 4;
233 memcpy(cmd->data + length, args, args_len);
236 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
238 msg[0].addr = client->addr;
239 msg[0].flags = client->flags & I2C_M_TEN;
241 msg[0].buf = cmd->data;
243 msg[1].addr = client->addr;
244 msg[1].flags = client->flags & I2C_M_TEN;
245 msg[1].flags |= I2C_M_RD;
246 msg[1].len = data_len;
247 msg[1].buf = buf_recv;
249 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
253 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
255 ret = i2c_transfer(client->adapter, msg, msg_num);
258 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
261 return ret < 0 ? ret : -EIO;
265 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
268 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
269 if (!wait_event_timeout(ihid->wait,
270 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
271 msecs_to_jiffies(5000)))
273 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
279 static int i2c_hid_command(struct i2c_client *client,
280 const struct i2c_hid_cmd *command,
281 unsigned char *buf_recv, int data_len)
283 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
287 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
288 u8 reportID, unsigned char *buf_recv, int data_len)
290 struct i2c_hid *ihid = i2c_get_clientdata(client);
294 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
296 i2c_hid_dbg(ihid, "%s\n", __func__);
298 if (reportID >= 0x0F) {
299 args[args_len++] = reportID;
303 args[args_len++] = readRegister & 0xFF;
304 args[args_len++] = readRegister >> 8;
306 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
307 reportType, args, args_len, buf_recv, data_len);
309 dev_err(&client->dev,
310 "failed to retrieve report from device.\n");
318 * i2c_hid_set_or_send_report: forward an incoming report to the device
319 * @client: the i2c_client of the device
320 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
321 * @reportID: the report ID
322 * @buf: the actual data to transfer, without the report ID
323 * @data_len: size of buf
324 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
326 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
327 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
329 struct i2c_hid *ihid = i2c_get_clientdata(client);
330 u8 *args = ihid->argsbuf;
331 const struct i2c_hid_cmd *hidcmd;
333 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
334 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
335 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
340 i2c_hid_dbg(ihid, "%s\n", __func__);
342 if (data_len > ihid->bufsize)
345 size = 2 /* size */ +
346 (reportID ? 1 : 0) /* reportID */ +
348 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
349 2 /* dataRegister */ +
352 if (!use_data && maxOutputLength == 0)
355 if (reportID >= 0x0F) {
356 args[index++] = reportID;
361 * use the data register for feature reports or if the device does not
362 * support the output register
365 args[index++] = dataRegister & 0xFF;
366 args[index++] = dataRegister >> 8;
367 hidcmd = &hid_set_report_cmd;
369 args[index++] = outputRegister & 0xFF;
370 args[index++] = outputRegister >> 8;
371 hidcmd = &hid_no_cmd;
374 args[index++] = size & 0xFF;
375 args[index++] = size >> 8;
378 args[index++] = reportID;
380 memcpy(&args[index], buf, data_len);
382 ret = __i2c_hid_command(client, hidcmd, reportID,
383 reportType, args, args_len, NULL, 0);
385 dev_err(&client->dev, "failed to set a report to device.\n");
392 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
394 struct i2c_hid *ihid = i2c_get_clientdata(client);
397 i2c_hid_dbg(ihid, "%s\n", __func__);
400 * Some devices require to send a command to wakeup before power on.
401 * The call will get a return value (EREMOTEIO) but device will be
402 * triggered and activated. After that, it goes like a normal device.
404 if (power_state == I2C_HID_PWR_ON &&
405 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
406 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
408 /* Device was already activated */
413 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
414 0, NULL, 0, NULL, 0);
417 dev_err(&client->dev, "failed to change power setting.\n");
422 * The HID over I2C specification states that if a DEVICE needs time
423 * after the PWR_ON request, it should utilise CLOCK stretching.
424 * However, it has been observered that the Windows driver provides a
425 * 1ms sleep between the PWR_ON and RESET requests.
426 * According to Goodix Windows even waits 60 ms after (other?)
427 * PWR_ON requests. Testing has confirmed that several devices
428 * will not work properly without a delay after a PWR_ON request.
430 if (!ret && power_state == I2C_HID_PWR_ON)
436 static int i2c_hid_hwreset(struct i2c_client *client)
438 struct i2c_hid *ihid = i2c_get_clientdata(client);
441 i2c_hid_dbg(ihid, "%s\n", __func__);
444 * This prevents sending feature reports while the device is
445 * being reset. Otherwise we may lose the reset complete
448 mutex_lock(&ihid->reset_lock);
450 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
454 i2c_hid_dbg(ihid, "resetting...\n");
456 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
458 dev_err(&client->dev, "failed to reset device.\n");
459 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
463 /* At least some SIS devices need this after reset */
464 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
467 mutex_unlock(&ihid->reset_lock);
471 static void i2c_hid_get_input(struct i2c_hid *ihid)
475 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
477 if (size > ihid->bufsize)
478 size = ihid->bufsize;
480 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
485 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
486 __func__, ret, size);
490 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
493 /* host or device initiated RESET completed */
494 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
495 wake_up(&ihid->wait);
499 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
500 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
501 "there's no data\n", __func__);
505 if ((ret_size > size) || (ret_size < 2)) {
506 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
507 ihid->inbuf[0] = size & 0xff;
508 ihid->inbuf[1] = size >> 8;
511 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
512 __func__, size, ret_size);
517 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
519 if (test_bit(I2C_HID_STARTED, &ihid->flags))
520 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
526 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
528 struct i2c_hid *ihid = dev_id;
530 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
533 i2c_hid_get_input(ihid);
538 static int i2c_hid_get_report_length(struct hid_report *report)
540 return ((report->size - 1) >> 3) + 1 +
541 report->device->report_enum[report->type].numbered + 2;
545 * Traverse the supplied list of reports and find the longest
547 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
550 struct hid_report *report;
553 /* We should not rely on wMaxInputLength, as some devices may set it to
555 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
556 size = i2c_hid_get_report_length(report);
562 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
566 kfree(ihid->argsbuf);
571 ihid->argsbuf = NULL;
575 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
577 /* the worst case is computed from the set_report command with a
578 * reportID > 15 and the maximum report length */
579 int args_len = sizeof(__u8) + /* ReportID */
580 sizeof(__u8) + /* optional ReportID byte */
581 sizeof(__u16) + /* data register */
582 sizeof(__u16) + /* size of the report */
583 report_size; /* report */
585 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
586 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
587 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
588 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
590 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
591 i2c_hid_free_buffers(ihid);
595 ihid->bufsize = report_size;
600 static int i2c_hid_get_raw_report(struct hid_device *hid,
601 unsigned char report_number, __u8 *buf, size_t count,
602 unsigned char report_type)
604 struct i2c_client *client = hid->driver_data;
605 struct i2c_hid *ihid = i2c_get_clientdata(client);
606 size_t ret_count, ask_count;
609 if (report_type == HID_OUTPUT_REPORT)
612 /* +2 bytes to include the size of the reply in the query buffer */
613 ask_count = min(count + 2, (size_t)ihid->bufsize);
615 ret = i2c_hid_get_report(client,
616 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
617 report_number, ihid->rawbuf, ask_count);
622 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
627 ret_count = min(ret_count, ask_count);
629 /* The query buffer contains the size, dropping it in the reply */
630 count = min(count, ret_count - 2);
631 memcpy(buf, ihid->rawbuf + 2, count);
636 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
637 size_t count, unsigned char report_type, bool use_data)
639 struct i2c_client *client = hid->driver_data;
640 struct i2c_hid *ihid = i2c_get_clientdata(client);
641 int report_id = buf[0];
644 if (report_type == HID_INPUT_REPORT)
647 mutex_lock(&ihid->reset_lock);
654 ret = i2c_hid_set_or_send_report(client,
655 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
656 report_id, buf, count, use_data);
658 if (report_id && ret >= 0)
659 ret++; /* add report_id to the number of transfered bytes */
661 mutex_unlock(&ihid->reset_lock);
666 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
669 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
673 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
674 __u8 *buf, size_t len, unsigned char rtype,
678 case HID_REQ_GET_REPORT:
679 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
680 case HID_REQ_SET_REPORT:
681 if (buf[0] != reportnum)
683 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
689 static int i2c_hid_parse(struct hid_device *hid)
691 struct i2c_client *client = hid->driver_data;
692 struct i2c_hid *ihid = i2c_get_clientdata(client);
693 struct i2c_hid_desc *hdesc = &ihid->hdesc;
700 i2c_hid_dbg(ihid, "entering %s\n", __func__);
702 rsize = le16_to_cpu(hdesc->wReportDescLength);
703 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
704 dbg_hid("weird size of report descriptor (%u)\n", rsize);
709 ret = i2c_hid_hwreset(client);
712 } while (tries-- > 0 && ret);
717 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
721 rdesc = use_override;
722 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
724 rdesc = kzalloc(rsize, GFP_KERNEL);
727 dbg_hid("couldn't allocate rdesc memory\n");
731 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
733 ret = i2c_hid_command(client, &hid_report_descr_cmd,
736 hid_err(hid, "reading report descriptor failed\n");
742 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
744 ret = hid_parse_report(hid, rdesc, rsize);
749 dbg_hid("parsing report descriptor failed\n");
756 static int i2c_hid_start(struct hid_device *hid)
758 struct i2c_client *client = hid->driver_data;
759 struct i2c_hid *ihid = i2c_get_clientdata(client);
761 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
763 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
764 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
765 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
767 if (bufsize > ihid->bufsize) {
768 disable_irq(client->irq);
769 i2c_hid_free_buffers(ihid);
771 ret = i2c_hid_alloc_buffers(ihid, bufsize);
772 enable_irq(client->irq);
781 static void i2c_hid_stop(struct hid_device *hid)
786 static int i2c_hid_open(struct hid_device *hid)
788 struct i2c_client *client = hid->driver_data;
789 struct i2c_hid *ihid = i2c_get_clientdata(client);
791 set_bit(I2C_HID_STARTED, &ihid->flags);
795 static void i2c_hid_close(struct hid_device *hid)
797 struct i2c_client *client = hid->driver_data;
798 struct i2c_hid *ihid = i2c_get_clientdata(client);
800 clear_bit(I2C_HID_STARTED, &ihid->flags);
803 struct hid_ll_driver i2c_hid_ll_driver = {
804 .parse = i2c_hid_parse,
805 .start = i2c_hid_start,
806 .stop = i2c_hid_stop,
807 .open = i2c_hid_open,
808 .close = i2c_hid_close,
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);
884 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
886 if (!ihid->ops->power_up)
889 return ihid->ops->power_up(ihid->ops);
892 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
894 if (!ihid->ops->power_down)
897 ihid->ops->power_down(ihid->ops);
900 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
902 if (!ihid->ops->shutdown_tail)
905 ihid->ops->shutdown_tail(ihid->ops);
908 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
909 u16 hid_descriptor_address)
912 struct i2c_hid *ihid;
913 struct hid_device *hid;
915 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
918 dev_err(&client->dev,
919 "HID over i2c has not been provided an Int IRQ\n");
923 if (client->irq < 0) {
924 if (client->irq != -EPROBE_DEFER)
925 dev_err(&client->dev,
926 "HID over i2c doesn't have a valid IRQ\n");
930 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
936 ret = i2c_hid_core_power_up(ihid);
940 i2c_set_clientdata(client, ihid);
942 ihid->client = client;
944 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
946 init_waitqueue_head(&ihid->wait);
947 mutex_init(&ihid->reset_lock);
949 /* we need to allocate the command buffer without knowing the maximum
950 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
951 * real computation later. */
952 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
956 device_enable_async_suspend(&client->dev);
958 /* Make sure there is something at this address */
959 ret = i2c_smbus_read_byte(client);
961 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
966 ret = i2c_hid_fetch_hid_descriptor(ihid);
968 dev_err(&client->dev,
969 "Failed to fetch the HID Descriptor\n");
973 ret = i2c_hid_init_irq(client);
977 hid = hid_allocate_device();
985 hid->driver_data = client;
986 hid->ll_driver = &i2c_hid_ll_driver;
987 hid->dev.parent = &client->dev;
989 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
990 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
991 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
993 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
994 client->name, hid->vendor, hid->product);
995 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
997 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
999 ret = hid_add_device(hid);
1002 hid_err(client, "can't add hid device: %d\n", ret);
1009 hid_destroy_device(hid);
1012 free_irq(client->irq, ihid);
1015 i2c_hid_core_power_down(ihid);
1016 i2c_hid_free_buffers(ihid);
1019 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1021 int i2c_hid_core_remove(struct i2c_client *client)
1023 struct i2c_hid *ihid = i2c_get_clientdata(client);
1024 struct hid_device *hid;
1027 hid_destroy_device(hid);
1029 free_irq(client->irq, ihid);
1032 i2c_hid_free_buffers(ihid);
1034 i2c_hid_core_power_down(ihid);
1038 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1040 void i2c_hid_core_shutdown(struct i2c_client *client)
1042 struct i2c_hid *ihid = i2c_get_clientdata(client);
1044 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1045 free_irq(client->irq, ihid);
1047 i2c_hid_core_shutdown_tail(ihid);
1049 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1051 #ifdef CONFIG_PM_SLEEP
1052 static int i2c_hid_core_suspend(struct device *dev)
1054 struct i2c_client *client = to_i2c_client(dev);
1055 struct i2c_hid *ihid = i2c_get_clientdata(client);
1056 struct hid_device *hid = ihid->hid;
1060 if (hid->driver && hid->driver->suspend) {
1061 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1066 /* Save some power */
1067 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1069 disable_irq(client->irq);
1071 if (device_may_wakeup(&client->dev)) {
1072 wake_status = enable_irq_wake(client->irq);
1074 ihid->irq_wake_enabled = true;
1076 hid_warn(hid, "Failed to enable irq wake: %d\n",
1079 i2c_hid_core_power_down(ihid);
1085 static int i2c_hid_core_resume(struct device *dev)
1088 struct i2c_client *client = to_i2c_client(dev);
1089 struct i2c_hid *ihid = i2c_get_clientdata(client);
1090 struct hid_device *hid = ihid->hid;
1093 if (!device_may_wakeup(&client->dev)) {
1094 i2c_hid_core_power_up(ihid);
1095 } else if (ihid->irq_wake_enabled) {
1096 wake_status = disable_irq_wake(client->irq);
1098 ihid->irq_wake_enabled = false;
1100 hid_warn(hid, "Failed to disable irq wake: %d\n",
1104 enable_irq(client->irq);
1106 /* Instead of resetting device, simply powers the device on. This
1107 * solves "incomplete reports" on Raydium devices 2386:3118 and
1108 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1109 * data after a suspend/resume.
1111 * However some ALPS touchpads generate IRQ storm without reset, so
1112 * let's still reset them here.
1114 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1115 ret = i2c_hid_hwreset(client);
1117 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1122 if (hid->driver && hid->driver->reset_resume) {
1123 ret = hid->driver->reset_resume(hid);
1131 const struct dev_pm_ops i2c_hid_core_pm = {
1132 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1134 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1136 MODULE_DESCRIPTION("HID over I2C core driver");
1138 MODULE_LICENSE("GPL");