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"
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_RESEND_REPORT_DESCR BIT(2)
53 #define I2C_HID_STARTED 0
54 #define I2C_HID_RESET_PENDING 1
55 #define I2C_HID_READ_PENDING 2
57 #define I2C_HID_PWR_ON 0x00
58 #define I2C_HID_PWR_SLEEP 0x01
62 module_param(debug
, bool, 0444);
63 MODULE_PARM_DESC(debug
, "print a lot of debug information");
65 #define i2c_hid_dbg(ihid, fmt, arg...) \
68 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
72 __le16 wHIDDescLength
;
74 __le16 wReportDescLength
;
75 __le16 wReportDescRegister
;
76 __le16 wInputRegister
;
77 __le16 wMaxInputLength
;
78 __le16 wOutputRegister
;
79 __le16 wMaxOutputLength
;
80 __le16 wCommandRegister
;
89 unsigned int registerIndex
;
104 #define I2C_HID_CMD(opcode_) \
105 .opcode = opcode_, .length = 4, \
106 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
108 /* fetch HID descriptor */
109 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
110 /* fetch report descriptors */
111 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
112 .registerIndex
= offsetof(struct i2c_hid_desc
,
113 wReportDescRegister
),
117 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
119 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
120 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
121 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
122 static const struct i2c_hid_cmd hid_no_cmd
= { .length
= 0 };
125 * These definitions are not used here, but are defined by the spec.
126 * Keeping them here for documentation purposes.
128 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
129 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
130 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
131 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
134 static DEFINE_MUTEX(i2c_hid_open_mut
);
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
;
164 static const struct i2c_hid_quirks
{
168 } i2c_hid_quirks
[] = {
169 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8752
,
170 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
171 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8755
,
172 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
173 { I2C_VENDOR_ID_HANTICK
, I2C_PRODUCT_ID_HANTICK_5288
,
174 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET
},
175 { I2C_VENDOR_ID_RAYD
, I2C_PRODUCT_ID_RAYD_3118
,
176 I2C_HID_QUIRK_RESEND_REPORT_DESCR
},
177 { I2C_VENDOR_ID_RAYD
, I2C_PRODUCT_ID_RAYD_4B33
,
178 I2C_HID_QUIRK_RESEND_REPORT_DESCR
},
179 { USB_VENDOR_ID_SIS_TOUCH
, USB_DEVICE_ID_SIS10FB_TOUCH
,
180 I2C_HID_QUIRK_RESEND_REPORT_DESCR
},
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
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");
423 static int i2c_hid_hwreset(struct i2c_client
*client
)
425 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
428 i2c_hid_dbg(ihid
, "%s\n", __func__
);
431 * This prevents sending feature reports while the device is
432 * being reset. Otherwise we may lose the reset complete
435 mutex_lock(&ihid
->reset_lock
);
437 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
442 * The HID over I2C specification states that if a DEVICE needs time
443 * after the PWR_ON request, it should utilise CLOCK stretching.
444 * However, it has been observered that the Windows driver provides a
445 * 1ms sleep between the PWR_ON and RESET requests and that some devices
448 usleep_range(1000, 5000);
450 i2c_hid_dbg(ihid
, "resetting...\n");
452 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
454 dev_err(&client
->dev
, "failed to reset device.\n");
455 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
459 mutex_unlock(&ihid
->reset_lock
);
463 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
467 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
469 if (size
> ihid
->bufsize
)
470 size
= ihid
->bufsize
;
472 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
477 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
478 __func__
, ret
, size
);
482 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
485 /* host or device initiated RESET completed */
486 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
487 wake_up(&ihid
->wait
);
491 if ((ret_size
> size
) || (ret_size
<= 2)) {
492 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
493 __func__
, size
, ret_size
);
497 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
499 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
500 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
506 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
508 struct i2c_hid
*ihid
= dev_id
;
510 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
513 i2c_hid_get_input(ihid
);
518 static int i2c_hid_get_report_length(struct hid_report
*report
)
520 return ((report
->size
- 1) >> 3) + 1 +
521 report
->device
->report_enum
[report
->type
].numbered
+ 2;
525 * Traverse the supplied list of reports and find the longest
527 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
530 struct hid_report
*report
;
533 /* We should not rely on wMaxInputLength, as some devices may set it to
535 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
536 size
= i2c_hid_get_report_length(report
);
542 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
546 kfree(ihid
->argsbuf
);
551 ihid
->argsbuf
= NULL
;
555 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
557 /* the worst case is computed from the set_report command with a
558 * reportID > 15 and the maximum report length */
559 int args_len
= sizeof(__u8
) + /* ReportID */
560 sizeof(__u8
) + /* optional ReportID byte */
561 sizeof(__u16
) + /* data register */
562 sizeof(__u16
) + /* size of the report */
563 report_size
; /* report */
565 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
566 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
567 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
568 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
570 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
571 i2c_hid_free_buffers(ihid
);
575 ihid
->bufsize
= report_size
;
580 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
581 unsigned char report_number
, __u8
*buf
, size_t count
,
582 unsigned char report_type
)
584 struct i2c_client
*client
= hid
->driver_data
;
585 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
586 size_t ret_count
, ask_count
;
589 if (report_type
== HID_OUTPUT_REPORT
)
592 /* +2 bytes to include the size of the reply in the query buffer */
593 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
595 ret
= i2c_hid_get_report(client
,
596 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
597 report_number
, ihid
->rawbuf
, ask_count
);
602 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
607 ret_count
= min(ret_count
, ask_count
);
609 /* The query buffer contains the size, dropping it in the reply */
610 count
= min(count
, ret_count
- 2);
611 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
616 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
617 size_t count
, unsigned char report_type
, bool use_data
)
619 struct i2c_client
*client
= hid
->driver_data
;
620 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
621 int report_id
= buf
[0];
624 if (report_type
== HID_INPUT_REPORT
)
627 mutex_lock(&ihid
->reset_lock
);
634 ret
= i2c_hid_set_or_send_report(client
,
635 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
636 report_id
, buf
, count
, use_data
);
638 if (report_id
&& ret
>= 0)
639 ret
++; /* add report_id to the number of transfered bytes */
641 mutex_unlock(&ihid
->reset_lock
);
646 static int i2c_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
649 return i2c_hid_output_raw_report(hid
, buf
, count
, HID_OUTPUT_REPORT
,
653 static int i2c_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
654 __u8
*buf
, size_t len
, unsigned char rtype
,
658 case HID_REQ_GET_REPORT
:
659 return i2c_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
660 case HID_REQ_SET_REPORT
:
661 if (buf
[0] != reportnum
)
663 return i2c_hid_output_raw_report(hid
, buf
, len
, rtype
, true);
669 static int i2c_hid_parse(struct hid_device
*hid
)
671 struct i2c_client
*client
= hid
->driver_data
;
672 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
673 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
679 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
681 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
682 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
683 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
688 ret
= i2c_hid_hwreset(client
);
691 } while (tries
-- > 0 && ret
);
696 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
699 dbg_hid("couldn't allocate rdesc memory\n");
703 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
705 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
707 hid_err(hid
, "reading report descriptor failed\n");
712 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
714 ret
= hid_parse_report(hid
, rdesc
, rsize
);
717 dbg_hid("parsing report descriptor failed\n");
724 static int i2c_hid_start(struct hid_device
*hid
)
726 struct i2c_client
*client
= hid
->driver_data
;
727 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
729 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
731 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
732 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
733 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
735 if (bufsize
> ihid
->bufsize
) {
736 disable_irq(client
->irq
);
737 i2c_hid_free_buffers(ihid
);
739 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
740 enable_irq(client
->irq
);
749 static void i2c_hid_stop(struct hid_device
*hid
)
754 static int i2c_hid_open(struct hid_device
*hid
)
756 struct i2c_client
*client
= hid
->driver_data
;
757 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
760 ret
= pm_runtime_get_sync(&client
->dev
);
764 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
768 static void i2c_hid_close(struct hid_device
*hid
)
770 struct i2c_client
*client
= hid
->driver_data
;
771 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
773 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
775 /* Save some power */
776 pm_runtime_put(&client
->dev
);
779 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
781 struct i2c_client
*client
= hid
->driver_data
;
782 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
784 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
788 pm_runtime_get_sync(&client
->dev
);
791 pm_runtime_put(&client
->dev
);
797 struct hid_ll_driver i2c_hid_ll_driver
= {
798 .parse
= i2c_hid_parse
,
799 .start
= i2c_hid_start
,
800 .stop
= i2c_hid_stop
,
801 .open
= i2c_hid_open
,
802 .close
= i2c_hid_close
,
803 .power
= i2c_hid_power
,
804 .output_report
= i2c_hid_output_report
,
805 .raw_request
= i2c_hid_raw_request
,
807 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver
);
809 static int i2c_hid_init_irq(struct i2c_client
*client
)
811 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
812 unsigned long irqflags
= 0;
815 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
817 if (!irq_get_trigger_type(client
->irq
))
818 irqflags
= IRQF_TRIGGER_LOW
;
820 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
821 irqflags
| IRQF_ONESHOT
, client
->name
, ihid
);
823 dev_warn(&client
->dev
,
824 "Could not register for %s interrupt, irq = %d,"
826 client
->name
, client
->irq
, ret
);
834 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
836 struct i2c_client
*client
= ihid
->client
;
837 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
841 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
842 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
843 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
844 sizeof(struct i2c_hid_desc
));
846 dev_err(&client
->dev
, "hid_descr_cmd failed\n");
850 /* Validate the length of HID descriptor, the 4 first bytes:
851 * bytes 0-1 -> length
852 * bytes 2-3 -> bcdVersion (has to be 1.00) */
853 /* check bcdVersion == 1.0 */
854 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
855 dev_err(&client
->dev
,
856 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
857 le16_to_cpu(hdesc
->bcdVersion
));
861 /* Descriptor length should be 30 bytes as per the specification */
862 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
863 if (dsize
!= sizeof(struct i2c_hid_desc
)) {
864 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
868 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
873 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
874 struct i2c_hid_platform_data
*pdata
)
876 static guid_t i2c_hid_guid
=
877 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
878 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
879 union acpi_object
*obj
;
880 struct acpi_device
*adev
;
883 handle
= ACPI_HANDLE(&client
->dev
);
884 if (!handle
|| acpi_bus_get_device(handle
, &adev
))
887 obj
= acpi_evaluate_dsm_typed(handle
, &i2c_hid_guid
, 1, 1, NULL
,
890 dev_err(&client
->dev
, "device _DSM execution failed\n");
894 pdata
->hid_descriptor_address
= obj
->integer
.value
;
900 static void i2c_hid_acpi_fix_up_power(struct device
*dev
)
902 acpi_handle handle
= ACPI_HANDLE(dev
);
903 struct acpi_device
*adev
;
905 if (handle
&& acpi_bus_get_device(handle
, &adev
) == 0)
906 acpi_device_fix_up_power(adev
);
909 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
914 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
916 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
917 struct i2c_hid_platform_data
*pdata
)
922 static inline void i2c_hid_acpi_fix_up_power(struct device
*dev
) {}
926 static int i2c_hid_of_probe(struct i2c_client
*client
,
927 struct i2c_hid_platform_data
*pdata
)
929 struct device
*dev
= &client
->dev
;
933 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
935 dev_err(&client
->dev
, "HID register address not provided\n");
939 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
943 pdata
->hid_descriptor_address
= val
;
945 ret
= of_property_read_u32(dev
->of_node
, "post-power-on-delay-ms",
948 pdata
->post_power_delay_ms
= val
;
953 static const struct of_device_id i2c_hid_of_match
[] = {
954 { .compatible
= "hid-over-i2c" },
957 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
959 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
960 struct i2c_hid_platform_data
*pdata
)
966 static int i2c_hid_probe(struct i2c_client
*client
,
967 const struct i2c_device_id
*dev_id
)
970 struct i2c_hid
*ihid
;
971 struct hid_device
*hid
;
973 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
975 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
978 dev_err(&client
->dev
,
979 "HID over i2c has not been provided an Int IRQ\n");
983 if (client
->irq
< 0) {
984 if (client
->irq
!= -EPROBE_DEFER
)
985 dev_err(&client
->dev
,
986 "HID over i2c doesn't have a valid IRQ\n");
990 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
994 if (client
->dev
.of_node
) {
995 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
998 } else if (!platform_data
) {
999 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
1001 dev_err(&client
->dev
,
1002 "HID register address not provided\n");
1006 ihid
->pdata
= *platform_data
;
1009 ihid
->pdata
.supply
= devm_regulator_get(&client
->dev
, "vdd");
1010 if (IS_ERR(ihid
->pdata
.supply
)) {
1011 ret
= PTR_ERR(ihid
->pdata
.supply
);
1012 if (ret
!= -EPROBE_DEFER
)
1013 dev_err(&client
->dev
, "Failed to get regulator: %d\n",
1018 ret
= regulator_enable(ihid
->pdata
.supply
);
1020 dev_err(&client
->dev
, "Failed to enable regulator: %d\n",
1024 if (ihid
->pdata
.post_power_delay_ms
)
1025 msleep(ihid
->pdata
.post_power_delay_ms
);
1027 i2c_set_clientdata(client
, ihid
);
1029 ihid
->client
= client
;
1031 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
1032 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
1034 init_waitqueue_head(&ihid
->wait
);
1035 mutex_init(&ihid
->reset_lock
);
1037 /* we need to allocate the command buffer without knowing the maximum
1038 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1039 * real computation later. */
1040 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
1044 i2c_hid_acpi_fix_up_power(&client
->dev
);
1046 pm_runtime_get_noresume(&client
->dev
);
1047 pm_runtime_set_active(&client
->dev
);
1048 pm_runtime_enable(&client
->dev
);
1049 device_enable_async_suspend(&client
->dev
);
1051 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
1055 ret
= i2c_hid_init_irq(client
);
1059 hid
= hid_allocate_device();
1067 hid
->driver_data
= client
;
1068 hid
->ll_driver
= &i2c_hid_ll_driver
;
1069 hid
->dev
.parent
= &client
->dev
;
1071 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1072 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1073 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1075 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1076 client
->name
, hid
->vendor
, hid
->product
);
1077 strlcpy(hid
->phys
, dev_name(&client
->dev
), sizeof(hid
->phys
));
1079 ihid
->quirks
= i2c_hid_lookup_quirk(hid
->vendor
, hid
->product
);
1081 ret
= hid_add_device(hid
);
1084 hid_err(client
, "can't add hid device: %d\n", ret
);
1088 pm_runtime_put(&client
->dev
);
1092 hid_destroy_device(hid
);
1095 free_irq(client
->irq
, ihid
);
1098 pm_runtime_put_noidle(&client
->dev
);
1099 pm_runtime_disable(&client
->dev
);
1102 regulator_disable(ihid
->pdata
.supply
);
1105 i2c_hid_free_buffers(ihid
);
1110 static int i2c_hid_remove(struct i2c_client
*client
)
1112 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1113 struct hid_device
*hid
;
1115 pm_runtime_get_sync(&client
->dev
);
1116 pm_runtime_disable(&client
->dev
);
1117 pm_runtime_set_suspended(&client
->dev
);
1118 pm_runtime_put_noidle(&client
->dev
);
1121 hid_destroy_device(hid
);
1123 free_irq(client
->irq
, ihid
);
1126 i2c_hid_free_buffers(ihid
);
1128 regulator_disable(ihid
->pdata
.supply
);
1135 static void i2c_hid_shutdown(struct i2c_client
*client
)
1137 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1139 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1140 free_irq(client
->irq
, ihid
);
1143 #ifdef CONFIG_PM_SLEEP
1144 static int i2c_hid_suspend(struct device
*dev
)
1146 struct i2c_client
*client
= to_i2c_client(dev
);
1147 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1148 struct hid_device
*hid
= ihid
->hid
;
1152 if (hid
->driver
&& hid
->driver
->suspend
) {
1154 * Wake up the device so that IO issues in
1155 * HID driver's suspend code can succeed.
1157 ret
= pm_runtime_resume(dev
);
1161 ret
= hid
->driver
->suspend(hid
, PMSG_SUSPEND
);
1166 if (!pm_runtime_suspended(dev
)) {
1167 /* Save some power */
1168 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1170 disable_irq(client
->irq
);
1173 if (device_may_wakeup(&client
->dev
)) {
1174 wake_status
= enable_irq_wake(client
->irq
);
1176 ihid
->irq_wake_enabled
= true;
1178 hid_warn(hid
, "Failed to enable irq wake: %d\n",
1181 ret
= regulator_disable(ihid
->pdata
.supply
);
1183 hid_warn(hid
, "Failed to disable supply: %d\n", ret
);
1189 static int i2c_hid_resume(struct device
*dev
)
1192 struct i2c_client
*client
= to_i2c_client(dev
);
1193 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1194 struct hid_device
*hid
= ihid
->hid
;
1197 if (!device_may_wakeup(&client
->dev
)) {
1198 ret
= regulator_enable(ihid
->pdata
.supply
);
1200 hid_warn(hid
, "Failed to enable supply: %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 /* We'll resume to full power */
1213 pm_runtime_disable(dev
);
1214 pm_runtime_set_active(dev
);
1215 pm_runtime_enable(dev
);
1217 enable_irq(client
->irq
);
1218 ret
= i2c_hid_hwreset(client
);
1222 /* RAYDIUM device (2386:3118) need to re-send report descr cmd
1223 * after resume, after this it will be back normal.
1224 * otherwise it issues too many incomplete reports.
1226 if (ihid
->quirks
& I2C_HID_QUIRK_RESEND_REPORT_DESCR
) {
1227 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, NULL
, 0);
1232 if (hid
->driver
&& hid
->driver
->reset_resume
) {
1233 ret
= hid
->driver
->reset_resume(hid
);
1242 static int i2c_hid_runtime_suspend(struct device
*dev
)
1244 struct i2c_client
*client
= to_i2c_client(dev
);
1246 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1247 disable_irq(client
->irq
);
1251 static int i2c_hid_runtime_resume(struct device
*dev
)
1253 struct i2c_client
*client
= to_i2c_client(dev
);
1255 enable_irq(client
->irq
);
1256 i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1261 static const struct dev_pm_ops i2c_hid_pm
= {
1262 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend
, i2c_hid_resume
)
1263 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend
, i2c_hid_runtime_resume
,
1267 static const struct i2c_device_id i2c_hid_id_table
[] = {
1269 { "hid-over-i2c", 0 },
1272 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1275 static struct i2c_driver i2c_hid_driver
= {
1279 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1280 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1283 .probe
= i2c_hid_probe
,
1284 .remove
= i2c_hid_remove
,
1285 .shutdown
= i2c_hid_shutdown
,
1286 .id_table
= i2c_hid_id_table
,
1289 module_i2c_driver(i2c_hid_driver
);
1291 MODULE_DESCRIPTION("HID over I2C core driver");
1292 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1293 MODULE_LICENSE("GPL");