2 * HID over I2C protocol implementation
4 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
5 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
6 * Copyright (c) 2012 Red Hat, Inc
8 * This code is partly based on "USB HID support for Linux":
10 * Copyright (c) 1999 Andreas Gal
11 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
12 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
13 * Copyright (c) 2007-2008 Oliver Neukum
14 * Copyright (c) 2006-2010 Jiri Kosina
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file COPYING in the main directory of this archive for
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/input.h>
25 #include <linux/irq.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/device.h>
31 #include <linux/wait.h>
32 #include <linux/err.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
35 #include <linux/jiffies.h>
36 #include <linux/kernel.h>
37 #include <linux/hid.h>
38 #include <linux/mutex.h>
39 #include <linux/acpi.h>
41 #include <linux/regulator/consumer.h>
43 #include <linux/platform_data/i2c-hid.h>
45 #include "../hid-ids.h"
48 /* quirks to control the device */
49 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
50 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
51 #define I2C_HID_QUIRK_NO_RUNTIME_PM BIT(2)
52 #define I2C_HID_QUIRK_DELAY_AFTER_SLEEP BIT(3)
53 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
56 #define I2C_HID_STARTED 0
57 #define I2C_HID_RESET_PENDING 1
58 #define I2C_HID_READ_PENDING 2
60 #define I2C_HID_PWR_ON 0x00
61 #define I2C_HID_PWR_SLEEP 0x01
65 module_param(debug
, bool, 0444);
66 MODULE_PARM_DESC(debug
, "print a lot of debug information");
68 #define i2c_hid_dbg(ihid, fmt, arg...) \
71 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
75 __le16 wHIDDescLength
;
77 __le16 wReportDescLength
;
78 __le16 wReportDescRegister
;
79 __le16 wInputRegister
;
80 __le16 wMaxInputLength
;
81 __le16 wOutputRegister
;
82 __le16 wMaxOutputLength
;
83 __le16 wCommandRegister
;
92 unsigned int registerIndex
;
107 #define I2C_HID_CMD(opcode_) \
108 .opcode = opcode_, .length = 4, \
109 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
111 /* fetch HID descriptor */
112 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
113 /* fetch report descriptors */
114 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
115 .registerIndex
= offsetof(struct i2c_hid_desc
,
116 wReportDescRegister
),
120 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
122 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
123 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
124 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
125 static const struct i2c_hid_cmd hid_no_cmd
= { .length
= 0 };
128 * These definitions are not used here, but are defined by the spec.
129 * Keeping them here for documentation purposes.
131 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
132 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
133 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
134 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
137 /* The main device structure */
139 struct i2c_client
*client
; /* i2c client */
140 struct hid_device
*hid
; /* pointer to corresponding HID dev */
142 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
143 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
145 __le16 wHIDDescRegister
; /* location of the i2c
146 * register of the HID
148 unsigned int bufsize
; /* i2c buffer size */
149 u8
*inbuf
; /* Input buffer */
150 u8
*rawbuf
; /* Raw Input buffer */
151 u8
*cmdbuf
; /* Command buffer */
152 u8
*argsbuf
; /* Command arguments buffer */
154 unsigned long flags
; /* device flags */
155 unsigned long quirks
; /* Various quirks */
157 wait_queue_head_t wait
; /* For waiting the interrupt */
159 struct i2c_hid_platform_data pdata
;
161 bool irq_wake_enabled
;
162 struct mutex reset_lock
;
164 unsigned long sleep_delay
;
167 static const struct i2c_hid_quirks
{
171 } i2c_hid_quirks
[] = {
172 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8752
,
173 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
174 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8755
,
175 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
176 { I2C_VENDOR_ID_HANTICK
, I2C_PRODUCT_ID_HANTICK_5288
,
177 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET
|
178 I2C_HID_QUIRK_NO_RUNTIME_PM
},
179 { I2C_VENDOR_ID_RAYDIUM
, I2C_PRODUCT_ID_RAYDIUM_4B33
,
180 I2C_HID_QUIRK_DELAY_AFTER_SLEEP
},
181 { USB_VENDOR_ID_LG
, I2C_DEVICE_ID_LG_8001
,
182 I2C_HID_QUIRK_NO_RUNTIME_PM
},
183 { I2C_VENDOR_ID_GOODIX
, I2C_DEVICE_ID_GOODIX_01F0
,
184 I2C_HID_QUIRK_NO_RUNTIME_PM
},
185 { USB_VENDOR_ID_ELAN
, HID_ANY_ID
,
186 I2C_HID_QUIRK_BOGUS_IRQ
},
191 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
192 * @idVendor: the 16-bit vendor ID
193 * @idProduct: the 16-bit product ID
195 * Returns: a u32 quirks value.
197 static u32
i2c_hid_lookup_quirk(const u16 idVendor
, const u16 idProduct
)
202 for (n
= 0; i2c_hid_quirks
[n
].idVendor
; n
++)
203 if (i2c_hid_quirks
[n
].idVendor
== idVendor
&&
204 (i2c_hid_quirks
[n
].idProduct
== (__u16
)HID_ANY_ID
||
205 i2c_hid_quirks
[n
].idProduct
== idProduct
))
206 quirks
= i2c_hid_quirks
[n
].quirks
;
211 static int __i2c_hid_command(struct i2c_client
*client
,
212 const struct i2c_hid_cmd
*command
, u8 reportID
,
213 u8 reportType
, u8
*args
, int args_len
,
214 unsigned char *buf_recv
, int data_len
)
216 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
217 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
219 struct i2c_msg msg
[2];
222 int length
= command
->length
;
223 bool wait
= command
->wait
;
224 unsigned int registerIndex
= command
->registerIndex
;
226 /* special case for hid_descr_cmd */
227 if (command
== &hid_descr_cmd
) {
228 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
230 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
231 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
235 cmd
->c
.opcode
= command
->opcode
;
236 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
239 memcpy(cmd
->data
+ length
, args
, args_len
);
242 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
244 msg
[0].addr
= client
->addr
;
245 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
247 msg
[0].buf
= cmd
->data
;
249 msg
[1].addr
= client
->addr
;
250 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
251 msg
[1].flags
|= I2C_M_RD
;
252 msg
[1].len
= data_len
;
253 msg
[1].buf
= buf_recv
;
255 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
259 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
261 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
264 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
267 return ret
< 0 ? ret
: -EIO
;
271 if (wait
&& (ihid
->quirks
& I2C_HID_QUIRK_NO_IRQ_AFTER_RESET
)) {
274 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
275 if (!wait_event_timeout(ihid
->wait
,
276 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
277 msecs_to_jiffies(5000)))
279 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
285 static int i2c_hid_command(struct i2c_client
*client
,
286 const struct i2c_hid_cmd
*command
,
287 unsigned char *buf_recv
, int data_len
)
289 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
293 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
294 u8 reportID
, unsigned char *buf_recv
, int data_len
)
296 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
300 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
302 i2c_hid_dbg(ihid
, "%s\n", __func__
);
304 if (reportID
>= 0x0F) {
305 args
[args_len
++] = reportID
;
309 args
[args_len
++] = readRegister
& 0xFF;
310 args
[args_len
++] = readRegister
>> 8;
312 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
313 reportType
, args
, args_len
, buf_recv
, data_len
);
315 dev_err(&client
->dev
,
316 "failed to retrieve report from device.\n");
324 * i2c_hid_set_or_send_report: forward an incoming report to the device
325 * @client: the i2c_client of the device
326 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
327 * @reportID: the report ID
328 * @buf: the actual data to transfer, without the report ID
330 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
332 static int i2c_hid_set_or_send_report(struct i2c_client
*client
, u8 reportType
,
333 u8 reportID
, unsigned char *buf
, size_t data_len
, bool use_data
)
335 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
336 u8
*args
= ihid
->argsbuf
;
337 const struct i2c_hid_cmd
*hidcmd
;
339 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
340 u16 outputRegister
= le16_to_cpu(ihid
->hdesc
.wOutputRegister
);
341 u16 maxOutputLength
= le16_to_cpu(ihid
->hdesc
.wMaxOutputLength
);
346 i2c_hid_dbg(ihid
, "%s\n", __func__
);
348 if (data_len
> ihid
->bufsize
)
351 size
= 2 /* size */ +
352 (reportID
? 1 : 0) /* reportID */ +
354 args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
355 2 /* dataRegister */ +
358 if (!use_data
&& maxOutputLength
== 0)
361 if (reportID
>= 0x0F) {
362 args
[index
++] = reportID
;
367 * use the data register for feature reports or if the device does not
368 * support the output register
371 args
[index
++] = dataRegister
& 0xFF;
372 args
[index
++] = dataRegister
>> 8;
373 hidcmd
= &hid_set_report_cmd
;
375 args
[index
++] = outputRegister
& 0xFF;
376 args
[index
++] = outputRegister
>> 8;
377 hidcmd
= &hid_no_cmd
;
380 args
[index
++] = size
& 0xFF;
381 args
[index
++] = size
>> 8;
384 args
[index
++] = reportID
;
386 memcpy(&args
[index
], buf
, data_len
);
388 ret
= __i2c_hid_command(client
, hidcmd
, reportID
,
389 reportType
, args
, args_len
, NULL
, 0);
391 dev_err(&client
->dev
, "failed to set a report to device.\n");
398 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
400 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
402 unsigned long now
, delay
;
404 i2c_hid_dbg(ihid
, "%s\n", __func__
);
407 * Some devices require to send a command to wakeup before power on.
408 * The call will get a return value (EREMOTEIO) but device will be
409 * triggered and activated. After that, it goes like a normal device.
411 if (power_state
== I2C_HID_PWR_ON
&&
412 ihid
->quirks
& I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
) {
413 ret
= i2c_hid_command(client
, &hid_set_power_cmd
, NULL
, 0);
415 /* Device was already activated */
420 if (ihid
->quirks
& I2C_HID_QUIRK_DELAY_AFTER_SLEEP
&&
421 power_state
== I2C_HID_PWR_ON
) {
423 if (time_after(ihid
->sleep_delay
, now
)) {
424 delay
= jiffies_to_usecs(ihid
->sleep_delay
- now
);
425 usleep_range(delay
, delay
+ 1);
429 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
430 0, NULL
, 0, NULL
, 0);
432 if (ihid
->quirks
& I2C_HID_QUIRK_DELAY_AFTER_SLEEP
&&
433 power_state
== I2C_HID_PWR_SLEEP
)
434 ihid
->sleep_delay
= jiffies
+ msecs_to_jiffies(20);
437 dev_err(&client
->dev
, "failed to change power setting.\n");
443 static int i2c_hid_hwreset(struct i2c_client
*client
)
445 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
448 i2c_hid_dbg(ihid
, "%s\n", __func__
);
451 * This prevents sending feature reports while the device is
452 * being reset. Otherwise we may lose the reset complete
455 mutex_lock(&ihid
->reset_lock
);
457 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
462 * The HID over I2C specification states that if a DEVICE needs time
463 * after the PWR_ON request, it should utilise CLOCK stretching.
464 * However, it has been observered that the Windows driver provides a
465 * 1ms sleep between the PWR_ON and RESET requests and that some devices
468 usleep_range(1000, 5000);
470 i2c_hid_dbg(ihid
, "resetting...\n");
472 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
474 dev_err(&client
->dev
, "failed to reset device.\n");
475 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
479 mutex_unlock(&ihid
->reset_lock
);
483 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
487 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
489 if (size
> ihid
->bufsize
)
490 size
= ihid
->bufsize
;
492 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
497 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
498 __func__
, ret
, size
);
502 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
505 /* host or device initiated RESET completed */
506 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
507 wake_up(&ihid
->wait
);
511 if (ihid
->quirks
& I2C_HID_QUIRK_BOGUS_IRQ
&& ret_size
== 0xffff) {
512 dev_warn_once(&ihid
->client
->dev
, "%s: IRQ triggered but "
513 "there's no data\n", __func__
);
517 if ((ret_size
> size
) || (ret_size
< 2)) {
518 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
519 __func__
, size
, ret_size
);
523 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
525 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
526 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
532 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
534 struct i2c_hid
*ihid
= dev_id
;
536 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
539 i2c_hid_get_input(ihid
);
544 static int i2c_hid_get_report_length(struct hid_report
*report
)
546 return ((report
->size
- 1) >> 3) + 1 +
547 report
->device
->report_enum
[report
->type
].numbered
+ 2;
551 * Traverse the supplied list of reports and find the longest
553 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
556 struct hid_report
*report
;
559 /* We should not rely on wMaxInputLength, as some devices may set it to
561 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
562 size
= i2c_hid_get_report_length(report
);
568 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
572 kfree(ihid
->argsbuf
);
577 ihid
->argsbuf
= NULL
;
581 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
583 /* the worst case is computed from the set_report command with a
584 * reportID > 15 and the maximum report length */
585 int args_len
= sizeof(__u8
) + /* ReportID */
586 sizeof(__u8
) + /* optional ReportID byte */
587 sizeof(__u16
) + /* data register */
588 sizeof(__u16
) + /* size of the report */
589 report_size
; /* report */
591 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
592 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
593 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
594 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
596 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
597 i2c_hid_free_buffers(ihid
);
601 ihid
->bufsize
= report_size
;
606 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
607 unsigned char report_number
, __u8
*buf
, size_t count
,
608 unsigned char report_type
)
610 struct i2c_client
*client
= hid
->driver_data
;
611 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
612 size_t ret_count
, ask_count
;
615 if (report_type
== HID_OUTPUT_REPORT
)
618 /* +2 bytes to include the size of the reply in the query buffer */
619 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
621 ret
= i2c_hid_get_report(client
,
622 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
623 report_number
, ihid
->rawbuf
, ask_count
);
628 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
633 ret_count
= min(ret_count
, ask_count
);
635 /* The query buffer contains the size, dropping it in the reply */
636 count
= min(count
, ret_count
- 2);
637 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
642 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
643 size_t count
, unsigned char report_type
, bool use_data
)
645 struct i2c_client
*client
= hid
->driver_data
;
646 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
647 int report_id
= buf
[0];
650 if (report_type
== HID_INPUT_REPORT
)
653 mutex_lock(&ihid
->reset_lock
);
660 ret
= i2c_hid_set_or_send_report(client
,
661 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
662 report_id
, buf
, count
, use_data
);
664 if (report_id
&& ret
>= 0)
665 ret
++; /* add report_id to the number of transfered bytes */
667 mutex_unlock(&ihid
->reset_lock
);
672 static int i2c_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
675 return i2c_hid_output_raw_report(hid
, buf
, count
, HID_OUTPUT_REPORT
,
679 static int i2c_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
680 __u8
*buf
, size_t len
, unsigned char rtype
,
684 case HID_REQ_GET_REPORT
:
685 return i2c_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
686 case HID_REQ_SET_REPORT
:
687 if (buf
[0] != reportnum
)
689 return i2c_hid_output_raw_report(hid
, buf
, len
, rtype
, true);
695 static int i2c_hid_parse(struct hid_device
*hid
)
697 struct i2c_client
*client
= hid
->driver_data
;
698 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
699 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
706 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
708 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
709 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
710 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
715 ret
= i2c_hid_hwreset(client
);
718 } while (tries
-- > 0 && ret
);
723 use_override
= i2c_hid_get_dmi_hid_report_desc_override(client
->name
,
727 rdesc
= use_override
;
728 i2c_hid_dbg(ihid
, "Using a HID report descriptor override\n");
730 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
733 dbg_hid("couldn't allocate rdesc memory\n");
737 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
739 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
,
742 hid_err(hid
, "reading report descriptor failed\n");
748 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
750 ret
= hid_parse_report(hid
, rdesc
, rsize
);
755 dbg_hid("parsing report descriptor failed\n");
762 static int i2c_hid_start(struct hid_device
*hid
)
764 struct i2c_client
*client
= hid
->driver_data
;
765 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
767 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
769 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
770 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
771 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
773 if (bufsize
> ihid
->bufsize
) {
774 disable_irq(client
->irq
);
775 i2c_hid_free_buffers(ihid
);
777 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
778 enable_irq(client
->irq
);
787 static void i2c_hid_stop(struct hid_device
*hid
)
792 static int i2c_hid_open(struct hid_device
*hid
)
794 struct i2c_client
*client
= hid
->driver_data
;
795 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
798 ret
= pm_runtime_get_sync(&client
->dev
);
802 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
806 static void i2c_hid_close(struct hid_device
*hid
)
808 struct i2c_client
*client
= hid
->driver_data
;
809 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
811 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
813 /* Save some power */
814 pm_runtime_put(&client
->dev
);
817 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
819 struct i2c_client
*client
= hid
->driver_data
;
820 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
822 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
826 pm_runtime_get_sync(&client
->dev
);
829 pm_runtime_put(&client
->dev
);
835 struct hid_ll_driver i2c_hid_ll_driver
= {
836 .parse
= i2c_hid_parse
,
837 .start
= i2c_hid_start
,
838 .stop
= i2c_hid_stop
,
839 .open
= i2c_hid_open
,
840 .close
= i2c_hid_close
,
841 .power
= i2c_hid_power
,
842 .output_report
= i2c_hid_output_report
,
843 .raw_request
= i2c_hid_raw_request
,
845 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver
);
847 static int i2c_hid_init_irq(struct i2c_client
*client
)
849 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
850 unsigned long irqflags
= 0;
853 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
855 if (!irq_get_trigger_type(client
->irq
))
856 irqflags
= IRQF_TRIGGER_LOW
;
858 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
859 irqflags
| IRQF_ONESHOT
, client
->name
, ihid
);
861 dev_warn(&client
->dev
,
862 "Could not register for %s interrupt, irq = %d,"
864 client
->name
, client
->irq
, ret
);
872 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
874 struct i2c_client
*client
= ihid
->client
;
875 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
879 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
880 if (i2c_hid_get_dmi_i2c_hid_desc_override(client
->name
)) {
881 i2c_hid_dbg(ihid
, "Using a HID descriptor override\n");
883 *i2c_hid_get_dmi_i2c_hid_desc_override(client
->name
);
885 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
886 ret
= i2c_hid_command(client
, &hid_descr_cmd
,
888 sizeof(struct i2c_hid_desc
));
890 dev_err(&client
->dev
, "hid_descr_cmd failed\n");
895 /* Validate the length of HID descriptor, the 4 first bytes:
896 * bytes 0-1 -> length
897 * bytes 2-3 -> bcdVersion (has to be 1.00) */
898 /* check bcdVersion == 1.0 */
899 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
900 dev_err(&client
->dev
,
901 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
902 le16_to_cpu(hdesc
->bcdVersion
));
906 /* Descriptor length should be 30 bytes as per the specification */
907 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
908 if (dsize
!= sizeof(struct i2c_hid_desc
)) {
909 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
913 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
918 static const struct acpi_device_id i2c_hid_acpi_blacklist
[] = {
920 * The CHPN0001 ACPI device, which is used to describe the Chipone
921 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
927 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
928 struct i2c_hid_platform_data
*pdata
)
930 static guid_t i2c_hid_guid
=
931 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
932 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
933 union acpi_object
*obj
;
934 struct acpi_device
*adev
;
937 handle
= ACPI_HANDLE(&client
->dev
);
938 if (!handle
|| acpi_bus_get_device(handle
, &adev
)) {
939 dev_err(&client
->dev
, "Error could not get ACPI device\n");
943 if (acpi_match_device_ids(adev
, i2c_hid_acpi_blacklist
) == 0)
946 obj
= acpi_evaluate_dsm_typed(handle
, &i2c_hid_guid
, 1, 1, NULL
,
949 dev_err(&client
->dev
, "Error _DSM call to get HID descriptor address failed\n");
953 pdata
->hid_descriptor_address
= obj
->integer
.value
;
959 static void i2c_hid_acpi_fix_up_power(struct device
*dev
)
961 struct acpi_device
*adev
;
963 adev
= ACPI_COMPANION(dev
);
965 acpi_device_fix_up_power(adev
);
968 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
973 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
975 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
976 struct i2c_hid_platform_data
*pdata
)
981 static inline void i2c_hid_acpi_fix_up_power(struct device
*dev
) {}
985 static int i2c_hid_of_probe(struct i2c_client
*client
,
986 struct i2c_hid_platform_data
*pdata
)
988 struct device
*dev
= &client
->dev
;
992 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
994 dev_err(&client
->dev
, "HID register address not provided\n");
998 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
1002 pdata
->hid_descriptor_address
= val
;
1007 static const struct of_device_id i2c_hid_of_match
[] = {
1008 { .compatible
= "hid-over-i2c" },
1011 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
1013 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
1014 struct i2c_hid_platform_data
*pdata
)
1020 static void i2c_hid_fwnode_probe(struct i2c_client
*client
,
1021 struct i2c_hid_platform_data
*pdata
)
1025 if (!device_property_read_u32(&client
->dev
, "post-power-on-delay-ms",
1027 pdata
->post_power_delay_ms
= val
;
1030 static int i2c_hid_probe(struct i2c_client
*client
,
1031 const struct i2c_device_id
*dev_id
)
1034 struct i2c_hid
*ihid
;
1035 struct hid_device
*hid
;
1037 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
1039 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
1042 dev_err(&client
->dev
,
1043 "HID over i2c has not been provided an Int IRQ\n");
1047 if (client
->irq
< 0) {
1048 if (client
->irq
!= -EPROBE_DEFER
)
1049 dev_err(&client
->dev
,
1050 "HID over i2c doesn't have a valid IRQ\n");
1054 ihid
= devm_kzalloc(&client
->dev
, sizeof(*ihid
), GFP_KERNEL
);
1058 if (client
->dev
.of_node
) {
1059 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
1062 } else if (!platform_data
) {
1063 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
1067 ihid
->pdata
= *platform_data
;
1070 /* Parse platform agnostic common properties from ACPI / device tree */
1071 i2c_hid_fwnode_probe(client
, &ihid
->pdata
);
1073 ihid
->pdata
.supplies
[0].supply
= "vdd";
1074 ihid
->pdata
.supplies
[1].supply
= "vddl";
1076 ret
= devm_regulator_bulk_get(&client
->dev
,
1077 ARRAY_SIZE(ihid
->pdata
.supplies
),
1078 ihid
->pdata
.supplies
);
1082 ret
= regulator_bulk_enable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1083 ihid
->pdata
.supplies
);
1087 if (ihid
->pdata
.post_power_delay_ms
)
1088 msleep(ihid
->pdata
.post_power_delay_ms
);
1090 i2c_set_clientdata(client
, ihid
);
1092 ihid
->client
= client
;
1094 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
1095 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
1097 init_waitqueue_head(&ihid
->wait
);
1098 mutex_init(&ihid
->reset_lock
);
1100 /* we need to allocate the command buffer without knowing the maximum
1101 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1102 * real computation later. */
1103 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
1107 i2c_hid_acpi_fix_up_power(&client
->dev
);
1109 pm_runtime_get_noresume(&client
->dev
);
1110 pm_runtime_set_active(&client
->dev
);
1111 pm_runtime_enable(&client
->dev
);
1112 device_enable_async_suspend(&client
->dev
);
1114 /* Make sure there is something at this address */
1115 ret
= i2c_smbus_read_byte(client
);
1117 dev_dbg(&client
->dev
, "nothing at this address: %d\n", ret
);
1122 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
1126 ret
= i2c_hid_init_irq(client
);
1130 hid
= hid_allocate_device();
1138 hid
->driver_data
= client
;
1139 hid
->ll_driver
= &i2c_hid_ll_driver
;
1140 hid
->dev
.parent
= &client
->dev
;
1142 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1143 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1144 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1146 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1147 client
->name
, hid
->vendor
, hid
->product
);
1148 strlcpy(hid
->phys
, dev_name(&client
->dev
), sizeof(hid
->phys
));
1150 ihid
->quirks
= i2c_hid_lookup_quirk(hid
->vendor
, hid
->product
);
1152 ret
= hid_add_device(hid
);
1155 hid_err(client
, "can't add hid device: %d\n", ret
);
1159 if (!(ihid
->quirks
& I2C_HID_QUIRK_NO_RUNTIME_PM
))
1160 pm_runtime_put(&client
->dev
);
1165 hid_destroy_device(hid
);
1168 free_irq(client
->irq
, ihid
);
1171 pm_runtime_put_noidle(&client
->dev
);
1172 pm_runtime_disable(&client
->dev
);
1175 regulator_bulk_disable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1176 ihid
->pdata
.supplies
);
1177 i2c_hid_free_buffers(ihid
);
1181 static int i2c_hid_remove(struct i2c_client
*client
)
1183 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1184 struct hid_device
*hid
;
1186 if (!(ihid
->quirks
& I2C_HID_QUIRK_NO_RUNTIME_PM
))
1187 pm_runtime_get_sync(&client
->dev
);
1188 pm_runtime_disable(&client
->dev
);
1189 pm_runtime_set_suspended(&client
->dev
);
1190 pm_runtime_put_noidle(&client
->dev
);
1193 hid_destroy_device(hid
);
1195 free_irq(client
->irq
, ihid
);
1198 i2c_hid_free_buffers(ihid
);
1200 regulator_bulk_disable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1201 ihid
->pdata
.supplies
);
1206 static void i2c_hid_shutdown(struct i2c_client
*client
)
1208 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1210 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1211 free_irq(client
->irq
, ihid
);
1214 #ifdef CONFIG_PM_SLEEP
1215 static int i2c_hid_suspend(struct device
*dev
)
1217 struct i2c_client
*client
= to_i2c_client(dev
);
1218 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1219 struct hid_device
*hid
= ihid
->hid
;
1223 if (hid
->driver
&& hid
->driver
->suspend
) {
1225 * Wake up the device so that IO issues in
1226 * HID driver's suspend code can succeed.
1228 ret
= pm_runtime_resume(dev
);
1232 ret
= hid
->driver
->suspend(hid
, PMSG_SUSPEND
);
1237 if (!pm_runtime_suspended(dev
)) {
1238 /* Save some power */
1239 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1241 disable_irq(client
->irq
);
1244 if (device_may_wakeup(&client
->dev
)) {
1245 wake_status
= enable_irq_wake(client
->irq
);
1247 ihid
->irq_wake_enabled
= true;
1249 hid_warn(hid
, "Failed to enable irq wake: %d\n",
1252 regulator_bulk_disable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1253 ihid
->pdata
.supplies
);
1259 static int i2c_hid_resume(struct device
*dev
)
1262 struct i2c_client
*client
= to_i2c_client(dev
);
1263 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1264 struct hid_device
*hid
= ihid
->hid
;
1267 if (!device_may_wakeup(&client
->dev
)) {
1268 ret
= regulator_bulk_enable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1269 ihid
->pdata
.supplies
);
1271 hid_warn(hid
, "Failed to enable supplies: %d\n", ret
);
1273 if (ihid
->pdata
.post_power_delay_ms
)
1274 msleep(ihid
->pdata
.post_power_delay_ms
);
1275 } else if (ihid
->irq_wake_enabled
) {
1276 wake_status
= disable_irq_wake(client
->irq
);
1278 ihid
->irq_wake_enabled
= false;
1280 hid_warn(hid
, "Failed to disable irq wake: %d\n",
1284 /* We'll resume to full power */
1285 pm_runtime_disable(dev
);
1286 pm_runtime_set_active(dev
);
1287 pm_runtime_enable(dev
);
1289 enable_irq(client
->irq
);
1291 /* Instead of resetting device, simply powers the device on. This
1292 * solves "incomplete reports" on Raydium devices 2386:3118 and
1293 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1294 * data after a suspend/resume.
1296 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1300 if (hid
->driver
&& hid
->driver
->reset_resume
) {
1301 ret
= hid
->driver
->reset_resume(hid
);
1310 static int i2c_hid_runtime_suspend(struct device
*dev
)
1312 struct i2c_client
*client
= to_i2c_client(dev
);
1314 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1315 disable_irq(client
->irq
);
1319 static int i2c_hid_runtime_resume(struct device
*dev
)
1321 struct i2c_client
*client
= to_i2c_client(dev
);
1323 enable_irq(client
->irq
);
1324 i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1329 static const struct dev_pm_ops i2c_hid_pm
= {
1330 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend
, i2c_hid_resume
)
1331 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend
, i2c_hid_runtime_resume
,
1335 static const struct i2c_device_id i2c_hid_id_table
[] = {
1337 { "hid-over-i2c", 0 },
1340 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1343 static struct i2c_driver i2c_hid_driver
= {
1347 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1348 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1351 .probe
= i2c_hid_probe
,
1352 .remove
= i2c_hid_remove
,
1353 .shutdown
= i2c_hid_shutdown
,
1354 .id_table
= i2c_hid_id_table
,
1357 module_i2c_driver(i2c_hid_driver
);
1359 MODULE_DESCRIPTION("HID over I2C core driver");
1360 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1361 MODULE_LICENSE("GPL");