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)
55 #define I2C_HID_STARTED 0
56 #define I2C_HID_RESET_PENDING 1
57 #define I2C_HID_READ_PENDING 2
59 #define I2C_HID_PWR_ON 0x00
60 #define I2C_HID_PWR_SLEEP 0x01
64 module_param(debug
, bool, 0444);
65 MODULE_PARM_DESC(debug
, "print a lot of debug information");
67 #define i2c_hid_dbg(ihid, fmt, arg...) \
70 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
74 __le16 wHIDDescLength
;
76 __le16 wReportDescLength
;
77 __le16 wReportDescRegister
;
78 __le16 wInputRegister
;
79 __le16 wMaxInputLength
;
80 __le16 wOutputRegister
;
81 __le16 wMaxOutputLength
;
82 __le16 wCommandRegister
;
91 unsigned int registerIndex
;
106 #define I2C_HID_CMD(opcode_) \
107 .opcode = opcode_, .length = 4, \
108 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
110 /* fetch HID descriptor */
111 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
112 /* fetch report descriptors */
113 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
114 .registerIndex
= offsetof(struct i2c_hid_desc
,
115 wReportDescRegister
),
119 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
121 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
122 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
123 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
124 static const struct i2c_hid_cmd hid_no_cmd
= { .length
= 0 };
127 * These definitions are not used here, but are defined by the spec.
128 * Keeping them here for documentation purposes.
130 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
131 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
132 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
133 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
136 /* The main device structure */
138 struct i2c_client
*client
; /* i2c client */
139 struct hid_device
*hid
; /* pointer to corresponding HID dev */
141 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
142 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
144 __le16 wHIDDescRegister
; /* location of the i2c
145 * register of the HID
147 unsigned int bufsize
; /* i2c buffer size */
148 u8
*inbuf
; /* Input buffer */
149 u8
*rawbuf
; /* Raw Input buffer */
150 u8
*cmdbuf
; /* Command buffer */
151 u8
*argsbuf
; /* Command arguments buffer */
153 unsigned long flags
; /* device flags */
154 unsigned long quirks
; /* Various quirks */
156 wait_queue_head_t wait
; /* For waiting the interrupt */
158 struct i2c_hid_platform_data pdata
;
160 bool irq_wake_enabled
;
161 struct mutex reset_lock
;
163 unsigned long sleep_delay
;
166 static const struct i2c_hid_quirks
{
170 } i2c_hid_quirks
[] = {
171 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8752
,
172 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
173 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8755
,
174 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
175 { I2C_VENDOR_ID_HANTICK
, I2C_PRODUCT_ID_HANTICK_5288
,
176 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET
|
177 I2C_HID_QUIRK_NO_RUNTIME_PM
},
178 { I2C_VENDOR_ID_RAYDIUM
, I2C_PRODUCT_ID_RAYDIUM_4B33
,
179 I2C_HID_QUIRK_DELAY_AFTER_SLEEP
},
180 { USB_VENDOR_ID_LG
, I2C_DEVICE_ID_LG_8001
,
181 I2C_HID_QUIRK_NO_RUNTIME_PM
},
182 { I2C_VENDOR_ID_GOODIX
, I2C_DEVICE_ID_GOODIX_01F0
,
183 I2C_HID_QUIRK_NO_RUNTIME_PM
},
188 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
189 * @idVendor: the 16-bit vendor ID
190 * @idProduct: the 16-bit product ID
192 * Returns: a u32 quirks value.
194 static u32
i2c_hid_lookup_quirk(const u16 idVendor
, const u16 idProduct
)
199 for (n
= 0; i2c_hid_quirks
[n
].idVendor
; n
++)
200 if (i2c_hid_quirks
[n
].idVendor
== idVendor
&&
201 (i2c_hid_quirks
[n
].idProduct
== (__u16
)HID_ANY_ID
||
202 i2c_hid_quirks
[n
].idProduct
== idProduct
))
203 quirks
= i2c_hid_quirks
[n
].quirks
;
208 static int __i2c_hid_command(struct i2c_client
*client
,
209 const struct i2c_hid_cmd
*command
, u8 reportID
,
210 u8 reportType
, u8
*args
, int args_len
,
211 unsigned char *buf_recv
, int data_len
)
213 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
214 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
216 struct i2c_msg msg
[2];
219 int length
= command
->length
;
220 bool wait
= command
->wait
;
221 unsigned int registerIndex
= command
->registerIndex
;
223 /* special case for hid_descr_cmd */
224 if (command
== &hid_descr_cmd
) {
225 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
227 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
228 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
232 cmd
->c
.opcode
= command
->opcode
;
233 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
236 memcpy(cmd
->data
+ length
, args
, args_len
);
239 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
241 msg
[0].addr
= client
->addr
;
242 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
244 msg
[0].buf
= cmd
->data
;
246 msg
[1].addr
= client
->addr
;
247 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
248 msg
[1].flags
|= I2C_M_RD
;
249 msg
[1].len
= data_len
;
250 msg
[1].buf
= buf_recv
;
252 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
256 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
258 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
261 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
264 return ret
< 0 ? ret
: -EIO
;
268 if (wait
&& (ihid
->quirks
& I2C_HID_QUIRK_NO_IRQ_AFTER_RESET
)) {
271 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
272 if (!wait_event_timeout(ihid
->wait
,
273 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
274 msecs_to_jiffies(5000)))
276 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
282 static int i2c_hid_command(struct i2c_client
*client
,
283 const struct i2c_hid_cmd
*command
,
284 unsigned char *buf_recv
, int data_len
)
286 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
290 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
291 u8 reportID
, unsigned char *buf_recv
, int data_len
)
293 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
297 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
299 i2c_hid_dbg(ihid
, "%s\n", __func__
);
301 if (reportID
>= 0x0F) {
302 args
[args_len
++] = reportID
;
306 args
[args_len
++] = readRegister
& 0xFF;
307 args
[args_len
++] = readRegister
>> 8;
309 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
310 reportType
, args
, args_len
, buf_recv
, data_len
);
312 dev_err(&client
->dev
,
313 "failed to retrieve report from device.\n");
321 * i2c_hid_set_or_send_report: forward an incoming report to the device
322 * @client: the i2c_client of the device
323 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
324 * @reportID: the report ID
325 * @buf: the actual data to transfer, without the report ID
327 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
329 static int i2c_hid_set_or_send_report(struct i2c_client
*client
, u8 reportType
,
330 u8 reportID
, unsigned char *buf
, size_t data_len
, bool use_data
)
332 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
333 u8
*args
= ihid
->argsbuf
;
334 const struct i2c_hid_cmd
*hidcmd
;
336 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
337 u16 outputRegister
= le16_to_cpu(ihid
->hdesc
.wOutputRegister
);
338 u16 maxOutputLength
= le16_to_cpu(ihid
->hdesc
.wMaxOutputLength
);
343 i2c_hid_dbg(ihid
, "%s\n", __func__
);
345 if (data_len
> ihid
->bufsize
)
348 size
= 2 /* size */ +
349 (reportID
? 1 : 0) /* reportID */ +
351 args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
352 2 /* dataRegister */ +
355 if (!use_data
&& maxOutputLength
== 0)
358 if (reportID
>= 0x0F) {
359 args
[index
++] = reportID
;
364 * use the data register for feature reports or if the device does not
365 * support the output register
368 args
[index
++] = dataRegister
& 0xFF;
369 args
[index
++] = dataRegister
>> 8;
370 hidcmd
= &hid_set_report_cmd
;
372 args
[index
++] = outputRegister
& 0xFF;
373 args
[index
++] = outputRegister
>> 8;
374 hidcmd
= &hid_no_cmd
;
377 args
[index
++] = size
& 0xFF;
378 args
[index
++] = size
>> 8;
381 args
[index
++] = reportID
;
383 memcpy(&args
[index
], buf
, data_len
);
385 ret
= __i2c_hid_command(client
, hidcmd
, reportID
,
386 reportType
, args
, args_len
, NULL
, 0);
388 dev_err(&client
->dev
, "failed to set a report to device.\n");
395 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
397 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
399 unsigned long now
, delay
;
401 i2c_hid_dbg(ihid
, "%s\n", __func__
);
404 * Some devices require to send a command to wakeup before power on.
405 * The call will get a return value (EREMOTEIO) but device will be
406 * triggered and activated. After that, it goes like a normal device.
408 if (power_state
== I2C_HID_PWR_ON
&&
409 ihid
->quirks
& I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
) {
410 ret
= i2c_hid_command(client
, &hid_set_power_cmd
, NULL
, 0);
412 /* Device was already activated */
417 if (ihid
->quirks
& I2C_HID_QUIRK_DELAY_AFTER_SLEEP
&&
418 power_state
== I2C_HID_PWR_ON
) {
420 if (time_after(ihid
->sleep_delay
, now
)) {
421 delay
= jiffies_to_usecs(ihid
->sleep_delay
- now
);
422 usleep_range(delay
, delay
+ 1);
426 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
427 0, NULL
, 0, NULL
, 0);
429 if (ihid
->quirks
& I2C_HID_QUIRK_DELAY_AFTER_SLEEP
&&
430 power_state
== I2C_HID_PWR_SLEEP
)
431 ihid
->sleep_delay
= jiffies
+ msecs_to_jiffies(20);
434 dev_err(&client
->dev
, "failed to change power setting.\n");
440 static int i2c_hid_hwreset(struct i2c_client
*client
)
442 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
445 i2c_hid_dbg(ihid
, "%s\n", __func__
);
448 * This prevents sending feature reports while the device is
449 * being reset. Otherwise we may lose the reset complete
452 mutex_lock(&ihid
->reset_lock
);
454 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
459 * The HID over I2C specification states that if a DEVICE needs time
460 * after the PWR_ON request, it should utilise CLOCK stretching.
461 * However, it has been observered that the Windows driver provides a
462 * 1ms sleep between the PWR_ON and RESET requests and that some devices
465 usleep_range(1000, 5000);
467 i2c_hid_dbg(ihid
, "resetting...\n");
469 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
471 dev_err(&client
->dev
, "failed to reset device.\n");
472 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
476 mutex_unlock(&ihid
->reset_lock
);
480 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
484 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
486 if (size
> ihid
->bufsize
)
487 size
= ihid
->bufsize
;
489 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
494 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
495 __func__
, ret
, size
);
499 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
502 /* host or device initiated RESET completed */
503 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
504 wake_up(&ihid
->wait
);
508 if ((ret_size
> size
) || (ret_size
< 2)) {
509 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
510 __func__
, size
, ret_size
);
514 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
516 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
517 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
523 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
525 struct i2c_hid
*ihid
= dev_id
;
527 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
530 i2c_hid_get_input(ihid
);
535 static int i2c_hid_get_report_length(struct hid_report
*report
)
537 return ((report
->size
- 1) >> 3) + 1 +
538 report
->device
->report_enum
[report
->type
].numbered
+ 2;
542 * Traverse the supplied list of reports and find the longest
544 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
547 struct hid_report
*report
;
550 /* We should not rely on wMaxInputLength, as some devices may set it to
552 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
553 size
= i2c_hid_get_report_length(report
);
559 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
563 kfree(ihid
->argsbuf
);
568 ihid
->argsbuf
= NULL
;
572 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
574 /* the worst case is computed from the set_report command with a
575 * reportID > 15 and the maximum report length */
576 int args_len
= sizeof(__u8
) + /* ReportID */
577 sizeof(__u8
) + /* optional ReportID byte */
578 sizeof(__u16
) + /* data register */
579 sizeof(__u16
) + /* size of the report */
580 report_size
; /* report */
582 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
583 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
584 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
585 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
587 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
588 i2c_hid_free_buffers(ihid
);
592 ihid
->bufsize
= report_size
;
597 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
598 unsigned char report_number
, __u8
*buf
, size_t count
,
599 unsigned char report_type
)
601 struct i2c_client
*client
= hid
->driver_data
;
602 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
603 size_t ret_count
, ask_count
;
606 if (report_type
== HID_OUTPUT_REPORT
)
609 /* +2 bytes to include the size of the reply in the query buffer */
610 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
612 ret
= i2c_hid_get_report(client
,
613 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
614 report_number
, ihid
->rawbuf
, ask_count
);
619 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
624 ret_count
= min(ret_count
, ask_count
);
626 /* The query buffer contains the size, dropping it in the reply */
627 count
= min(count
, ret_count
- 2);
628 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
633 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
634 size_t count
, unsigned char report_type
, bool use_data
)
636 struct i2c_client
*client
= hid
->driver_data
;
637 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
638 int report_id
= buf
[0];
641 if (report_type
== HID_INPUT_REPORT
)
644 mutex_lock(&ihid
->reset_lock
);
651 ret
= i2c_hid_set_or_send_report(client
,
652 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
653 report_id
, buf
, count
, use_data
);
655 if (report_id
&& ret
>= 0)
656 ret
++; /* add report_id to the number of transfered bytes */
658 mutex_unlock(&ihid
->reset_lock
);
663 static int i2c_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
666 return i2c_hid_output_raw_report(hid
, buf
, count
, HID_OUTPUT_REPORT
,
670 static int i2c_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
671 __u8
*buf
, size_t len
, unsigned char rtype
,
675 case HID_REQ_GET_REPORT
:
676 return i2c_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
677 case HID_REQ_SET_REPORT
:
678 if (buf
[0] != reportnum
)
680 return i2c_hid_output_raw_report(hid
, buf
, len
, rtype
, true);
686 static int i2c_hid_parse(struct hid_device
*hid
)
688 struct i2c_client
*client
= hid
->driver_data
;
689 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
690 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
697 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
699 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
700 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
701 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
706 ret
= i2c_hid_hwreset(client
);
709 } while (tries
-- > 0 && ret
);
714 use_override
= i2c_hid_get_dmi_hid_report_desc_override(client
->name
,
718 rdesc
= use_override
;
719 i2c_hid_dbg(ihid
, "Using a HID report descriptor override\n");
721 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
724 dbg_hid("couldn't allocate rdesc memory\n");
728 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
730 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
,
733 hid_err(hid
, "reading report descriptor failed\n");
739 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
741 ret
= hid_parse_report(hid
, rdesc
, rsize
);
746 dbg_hid("parsing report descriptor failed\n");
753 static int i2c_hid_start(struct hid_device
*hid
)
755 struct i2c_client
*client
= hid
->driver_data
;
756 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
758 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
760 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
761 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
762 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
764 if (bufsize
> ihid
->bufsize
) {
765 disable_irq(client
->irq
);
766 i2c_hid_free_buffers(ihid
);
768 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
769 enable_irq(client
->irq
);
778 static void i2c_hid_stop(struct hid_device
*hid
)
783 static int i2c_hid_open(struct hid_device
*hid
)
785 struct i2c_client
*client
= hid
->driver_data
;
786 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
789 ret
= pm_runtime_get_sync(&client
->dev
);
793 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
797 static void i2c_hid_close(struct hid_device
*hid
)
799 struct i2c_client
*client
= hid
->driver_data
;
800 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
802 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
804 /* Save some power */
805 pm_runtime_put(&client
->dev
);
808 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
810 struct i2c_client
*client
= hid
->driver_data
;
811 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
813 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
817 pm_runtime_get_sync(&client
->dev
);
820 pm_runtime_put(&client
->dev
);
826 struct hid_ll_driver i2c_hid_ll_driver
= {
827 .parse
= i2c_hid_parse
,
828 .start
= i2c_hid_start
,
829 .stop
= i2c_hid_stop
,
830 .open
= i2c_hid_open
,
831 .close
= i2c_hid_close
,
832 .power
= i2c_hid_power
,
833 .output_report
= i2c_hid_output_report
,
834 .raw_request
= i2c_hid_raw_request
,
836 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver
);
838 static int i2c_hid_init_irq(struct i2c_client
*client
)
840 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
841 unsigned long irqflags
= 0;
844 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
846 if (!irq_get_trigger_type(client
->irq
))
847 irqflags
= IRQF_TRIGGER_LOW
;
849 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
850 irqflags
| IRQF_ONESHOT
, client
->name
, ihid
);
852 dev_warn(&client
->dev
,
853 "Could not register for %s interrupt, irq = %d,"
855 client
->name
, client
->irq
, ret
);
863 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
865 struct i2c_client
*client
= ihid
->client
;
866 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
870 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
871 if (i2c_hid_get_dmi_i2c_hid_desc_override(client
->name
)) {
872 i2c_hid_dbg(ihid
, "Using a HID descriptor override\n");
874 *i2c_hid_get_dmi_i2c_hid_desc_override(client
->name
);
876 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
877 ret
= i2c_hid_command(client
, &hid_descr_cmd
,
879 sizeof(struct i2c_hid_desc
));
881 dev_err(&client
->dev
, "hid_descr_cmd failed\n");
886 /* Validate the length of HID descriptor, the 4 first bytes:
887 * bytes 0-1 -> length
888 * bytes 2-3 -> bcdVersion (has to be 1.00) */
889 /* check bcdVersion == 1.0 */
890 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
891 dev_err(&client
->dev
,
892 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
893 le16_to_cpu(hdesc
->bcdVersion
));
897 /* Descriptor length should be 30 bytes as per the specification */
898 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
899 if (dsize
!= sizeof(struct i2c_hid_desc
)) {
900 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
904 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
909 static const struct acpi_device_id i2c_hid_acpi_blacklist
[] = {
911 * The CHPN0001 ACPI device, which is used to describe the Chipone
912 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
918 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
919 struct i2c_hid_platform_data
*pdata
)
921 static guid_t i2c_hid_guid
=
922 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
923 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
924 union acpi_object
*obj
;
925 struct acpi_device
*adev
;
928 handle
= ACPI_HANDLE(&client
->dev
);
929 if (!handle
|| acpi_bus_get_device(handle
, &adev
)) {
930 dev_err(&client
->dev
, "Error could not get ACPI device\n");
934 if (acpi_match_device_ids(adev
, i2c_hid_acpi_blacklist
) == 0)
937 obj
= acpi_evaluate_dsm_typed(handle
, &i2c_hid_guid
, 1, 1, NULL
,
940 dev_err(&client
->dev
, "Error _DSM call to get HID descriptor address failed\n");
944 pdata
->hid_descriptor_address
= obj
->integer
.value
;
950 static void i2c_hid_acpi_fix_up_power(struct device
*dev
)
952 struct acpi_device
*adev
;
954 adev
= ACPI_COMPANION(dev
);
956 acpi_device_fix_up_power(adev
);
959 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
964 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
966 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
967 struct i2c_hid_platform_data
*pdata
)
972 static inline void i2c_hid_acpi_fix_up_power(struct device
*dev
) {}
976 static int i2c_hid_of_probe(struct i2c_client
*client
,
977 struct i2c_hid_platform_data
*pdata
)
979 struct device
*dev
= &client
->dev
;
983 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
985 dev_err(&client
->dev
, "HID register address not provided\n");
989 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
993 pdata
->hid_descriptor_address
= val
;
998 static const struct of_device_id i2c_hid_of_match
[] = {
999 { .compatible
= "hid-over-i2c" },
1002 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
1004 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
1005 struct i2c_hid_platform_data
*pdata
)
1011 static void i2c_hid_fwnode_probe(struct i2c_client
*client
,
1012 struct i2c_hid_platform_data
*pdata
)
1016 if (!device_property_read_u32(&client
->dev
, "post-power-on-delay-ms",
1018 pdata
->post_power_delay_ms
= val
;
1021 static int i2c_hid_probe(struct i2c_client
*client
,
1022 const struct i2c_device_id
*dev_id
)
1025 struct i2c_hid
*ihid
;
1026 struct hid_device
*hid
;
1028 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
1030 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
1033 dev_err(&client
->dev
,
1034 "HID over i2c has not been provided an Int IRQ\n");
1038 if (client
->irq
< 0) {
1039 if (client
->irq
!= -EPROBE_DEFER
)
1040 dev_err(&client
->dev
,
1041 "HID over i2c doesn't have a valid IRQ\n");
1045 ihid
= devm_kzalloc(&client
->dev
, sizeof(*ihid
), GFP_KERNEL
);
1049 if (client
->dev
.of_node
) {
1050 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
1053 } else if (!platform_data
) {
1054 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
1058 ihid
->pdata
= *platform_data
;
1061 /* Parse platform agnostic common properties from ACPI / device tree */
1062 i2c_hid_fwnode_probe(client
, &ihid
->pdata
);
1064 ihid
->pdata
.supplies
[0].supply
= "vdd";
1065 ihid
->pdata
.supplies
[1].supply
= "vddl";
1067 ret
= devm_regulator_bulk_get(&client
->dev
,
1068 ARRAY_SIZE(ihid
->pdata
.supplies
),
1069 ihid
->pdata
.supplies
);
1073 ret
= regulator_bulk_enable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1074 ihid
->pdata
.supplies
);
1078 if (ihid
->pdata
.post_power_delay_ms
)
1079 msleep(ihid
->pdata
.post_power_delay_ms
);
1081 i2c_set_clientdata(client
, ihid
);
1083 ihid
->client
= client
;
1085 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
1086 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
1088 init_waitqueue_head(&ihid
->wait
);
1089 mutex_init(&ihid
->reset_lock
);
1091 /* we need to allocate the command buffer without knowing the maximum
1092 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1093 * real computation later. */
1094 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
1098 i2c_hid_acpi_fix_up_power(&client
->dev
);
1100 pm_runtime_get_noresume(&client
->dev
);
1101 pm_runtime_set_active(&client
->dev
);
1102 pm_runtime_enable(&client
->dev
);
1103 device_enable_async_suspend(&client
->dev
);
1105 /* Make sure there is something at this address */
1106 ret
= i2c_smbus_read_byte(client
);
1108 dev_dbg(&client
->dev
, "nothing at this address: %d\n", ret
);
1113 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
1117 ret
= i2c_hid_init_irq(client
);
1121 hid
= hid_allocate_device();
1129 hid
->driver_data
= client
;
1130 hid
->ll_driver
= &i2c_hid_ll_driver
;
1131 hid
->dev
.parent
= &client
->dev
;
1133 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1134 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1135 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1137 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1138 client
->name
, hid
->vendor
, hid
->product
);
1139 strlcpy(hid
->phys
, dev_name(&client
->dev
), sizeof(hid
->phys
));
1141 ihid
->quirks
= i2c_hid_lookup_quirk(hid
->vendor
, hid
->product
);
1143 ret
= hid_add_device(hid
);
1146 hid_err(client
, "can't add hid device: %d\n", ret
);
1150 if (!(ihid
->quirks
& I2C_HID_QUIRK_NO_RUNTIME_PM
))
1151 pm_runtime_put(&client
->dev
);
1156 hid_destroy_device(hid
);
1159 free_irq(client
->irq
, ihid
);
1162 pm_runtime_put_noidle(&client
->dev
);
1163 pm_runtime_disable(&client
->dev
);
1166 regulator_bulk_disable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1167 ihid
->pdata
.supplies
);
1168 i2c_hid_free_buffers(ihid
);
1172 static int i2c_hid_remove(struct i2c_client
*client
)
1174 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1175 struct hid_device
*hid
;
1177 if (!(ihid
->quirks
& I2C_HID_QUIRK_NO_RUNTIME_PM
))
1178 pm_runtime_get_sync(&client
->dev
);
1179 pm_runtime_disable(&client
->dev
);
1180 pm_runtime_set_suspended(&client
->dev
);
1181 pm_runtime_put_noidle(&client
->dev
);
1184 hid_destroy_device(hid
);
1186 free_irq(client
->irq
, ihid
);
1189 i2c_hid_free_buffers(ihid
);
1191 regulator_bulk_disable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1192 ihid
->pdata
.supplies
);
1197 static void i2c_hid_shutdown(struct i2c_client
*client
)
1199 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1201 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1202 free_irq(client
->irq
, ihid
);
1205 #ifdef CONFIG_PM_SLEEP
1206 static int i2c_hid_suspend(struct device
*dev
)
1208 struct i2c_client
*client
= to_i2c_client(dev
);
1209 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1210 struct hid_device
*hid
= ihid
->hid
;
1214 if (hid
->driver
&& hid
->driver
->suspend
) {
1216 * Wake up the device so that IO issues in
1217 * HID driver's suspend code can succeed.
1219 ret
= pm_runtime_resume(dev
);
1223 ret
= hid
->driver
->suspend(hid
, PMSG_SUSPEND
);
1228 if (!pm_runtime_suspended(dev
)) {
1229 /* Save some power */
1230 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1232 disable_irq(client
->irq
);
1235 if (device_may_wakeup(&client
->dev
)) {
1236 wake_status
= enable_irq_wake(client
->irq
);
1238 ihid
->irq_wake_enabled
= true;
1240 hid_warn(hid
, "Failed to enable irq wake: %d\n",
1243 regulator_bulk_disable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1244 ihid
->pdata
.supplies
);
1250 static int i2c_hid_resume(struct device
*dev
)
1253 struct i2c_client
*client
= to_i2c_client(dev
);
1254 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1255 struct hid_device
*hid
= ihid
->hid
;
1258 if (!device_may_wakeup(&client
->dev
)) {
1259 ret
= regulator_bulk_enable(ARRAY_SIZE(ihid
->pdata
.supplies
),
1260 ihid
->pdata
.supplies
);
1262 hid_warn(hid
, "Failed to enable supplies: %d\n", ret
);
1264 if (ihid
->pdata
.post_power_delay_ms
)
1265 msleep(ihid
->pdata
.post_power_delay_ms
);
1266 } else if (ihid
->irq_wake_enabled
) {
1267 wake_status
= disable_irq_wake(client
->irq
);
1269 ihid
->irq_wake_enabled
= false;
1271 hid_warn(hid
, "Failed to disable irq wake: %d\n",
1275 /* We'll resume to full power */
1276 pm_runtime_disable(dev
);
1277 pm_runtime_set_active(dev
);
1278 pm_runtime_enable(dev
);
1280 enable_irq(client
->irq
);
1282 /* Instead of resetting device, simply powers the device on. This
1283 * solves "incomplete reports" on Raydium devices 2386:3118 and
1284 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1285 * data after a suspend/resume.
1287 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1291 if (hid
->driver
&& hid
->driver
->reset_resume
) {
1292 ret
= hid
->driver
->reset_resume(hid
);
1301 static int i2c_hid_runtime_suspend(struct device
*dev
)
1303 struct i2c_client
*client
= to_i2c_client(dev
);
1305 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1306 disable_irq(client
->irq
);
1310 static int i2c_hid_runtime_resume(struct device
*dev
)
1312 struct i2c_client
*client
= to_i2c_client(dev
);
1314 enable_irq(client
->irq
);
1315 i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1320 static const struct dev_pm_ops i2c_hid_pm
= {
1321 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend
, i2c_hid_resume
)
1322 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend
, i2c_hid_runtime_resume
,
1326 static const struct i2c_device_id i2c_hid_id_table
[] = {
1328 { "hid-over-i2c", 0 },
1331 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1334 static struct i2c_driver i2c_hid_driver
= {
1338 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1339 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1342 .probe
= i2c_hid_probe
,
1343 .remove
= i2c_hid_remove
,
1344 .shutdown
= i2c_hid_shutdown
,
1345 .id_table
= i2c_hid_id_table
,
1348 module_i2c_driver(i2c_hid_driver
);
1350 MODULE_DESCRIPTION("HID over I2C core driver");
1351 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1352 MODULE_LICENSE("GPL");