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/i2c/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)
51 #define I2C_HID_STARTED 0
52 #define I2C_HID_RESET_PENDING 1
53 #define I2C_HID_READ_PENDING 2
55 #define I2C_HID_PWR_ON 0x00
56 #define I2C_HID_PWR_SLEEP 0x01
60 module_param(debug
, bool, 0444);
61 MODULE_PARM_DESC(debug
, "print a lot of debug information");
63 #define i2c_hid_dbg(ihid, fmt, arg...) \
66 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
70 __le16 wHIDDescLength
;
72 __le16 wReportDescLength
;
73 __le16 wReportDescRegister
;
74 __le16 wInputRegister
;
75 __le16 wMaxInputLength
;
76 __le16 wOutputRegister
;
77 __le16 wMaxOutputLength
;
78 __le16 wCommandRegister
;
87 unsigned int registerIndex
;
102 #define I2C_HID_CMD(opcode_) \
103 .opcode = opcode_, .length = 4, \
104 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
106 /* fetch HID descriptor */
107 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
108 /* fetch report descriptors */
109 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
110 .registerIndex
= offsetof(struct i2c_hid_desc
,
111 wReportDescRegister
),
115 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
117 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
118 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
119 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
120 static const struct i2c_hid_cmd hid_no_cmd
= { .length
= 0 };
123 * These definitions are not used here, but are defined by the spec.
124 * Keeping them here for documentation purposes.
126 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
127 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
128 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
129 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
132 static DEFINE_MUTEX(i2c_hid_open_mut
);
134 /* The main device structure */
136 struct i2c_client
*client
; /* i2c client */
137 struct hid_device
*hid
; /* pointer to corresponding HID dev */
139 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
140 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
142 __le16 wHIDDescRegister
; /* location of the i2c
143 * register of the HID
145 unsigned int bufsize
; /* i2c buffer size */
146 char *inbuf
; /* Input buffer */
147 char *rawbuf
; /* Raw Input buffer */
148 char *cmdbuf
; /* Command buffer */
149 char *argsbuf
; /* Command arguments buffer */
151 unsigned long flags
; /* device flags */
152 unsigned long quirks
; /* Various quirks */
154 wait_queue_head_t wait
; /* For waiting the interrupt */
156 struct i2c_hid_platform_data pdata
;
158 bool irq_wake_enabled
;
159 struct mutex reset_lock
;
162 static const struct i2c_hid_quirks
{
166 } i2c_hid_quirks
[] = {
167 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8752
,
168 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
169 { USB_VENDOR_ID_WEIDA
, USB_DEVICE_ID_WEIDA_8755
,
170 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
},
175 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
176 * @idVendor: the 16-bit vendor ID
177 * @idProduct: the 16-bit product ID
179 * Returns: a u32 quirks value.
181 static u32
i2c_hid_lookup_quirk(const u16 idVendor
, const u16 idProduct
)
186 for (n
= 0; i2c_hid_quirks
[n
].idVendor
; n
++)
187 if (i2c_hid_quirks
[n
].idVendor
== idVendor
&&
188 (i2c_hid_quirks
[n
].idProduct
== (__u16
)HID_ANY_ID
||
189 i2c_hid_quirks
[n
].idProduct
== idProduct
))
190 quirks
= i2c_hid_quirks
[n
].quirks
;
195 static int __i2c_hid_command(struct i2c_client
*client
,
196 const struct i2c_hid_cmd
*command
, u8 reportID
,
197 u8 reportType
, u8
*args
, int args_len
,
198 unsigned char *buf_recv
, int data_len
)
200 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
201 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
203 struct i2c_msg msg
[2];
206 int length
= command
->length
;
207 bool wait
= command
->wait
;
208 unsigned int registerIndex
= command
->registerIndex
;
210 /* special case for hid_descr_cmd */
211 if (command
== &hid_descr_cmd
) {
212 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
214 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
215 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
219 cmd
->c
.opcode
= command
->opcode
;
220 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
223 memcpy(cmd
->data
+ length
, args
, args_len
);
226 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
228 msg
[0].addr
= client
->addr
;
229 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
231 msg
[0].buf
= cmd
->data
;
233 msg
[1].addr
= client
->addr
;
234 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
235 msg
[1].flags
|= I2C_M_RD
;
236 msg
[1].len
= data_len
;
237 msg
[1].buf
= buf_recv
;
239 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
243 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
245 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
248 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
251 return ret
< 0 ? ret
: -EIO
;
256 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
257 if (!wait_event_timeout(ihid
->wait
,
258 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
259 msecs_to_jiffies(5000)))
261 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
267 static int i2c_hid_command(struct i2c_client
*client
,
268 const struct i2c_hid_cmd
*command
,
269 unsigned char *buf_recv
, int data_len
)
271 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
275 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
276 u8 reportID
, unsigned char *buf_recv
, int data_len
)
278 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
282 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
284 i2c_hid_dbg(ihid
, "%s\n", __func__
);
286 if (reportID
>= 0x0F) {
287 args
[args_len
++] = reportID
;
291 args
[args_len
++] = readRegister
& 0xFF;
292 args
[args_len
++] = readRegister
>> 8;
294 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
295 reportType
, args
, args_len
, buf_recv
, data_len
);
297 dev_err(&client
->dev
,
298 "failed to retrieve report from device.\n");
306 * i2c_hid_set_or_send_report: forward an incoming report to the device
307 * @client: the i2c_client of the device
308 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
309 * @reportID: the report ID
310 * @buf: the actual data to transfer, without the report ID
312 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
314 static int i2c_hid_set_or_send_report(struct i2c_client
*client
, u8 reportType
,
315 u8 reportID
, unsigned char *buf
, size_t data_len
, bool use_data
)
317 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
318 u8
*args
= ihid
->argsbuf
;
319 const struct i2c_hid_cmd
*hidcmd
;
321 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
322 u16 outputRegister
= le16_to_cpu(ihid
->hdesc
.wOutputRegister
);
323 u16 maxOutputLength
= le16_to_cpu(ihid
->hdesc
.wMaxOutputLength
);
328 i2c_hid_dbg(ihid
, "%s\n", __func__
);
330 if (data_len
> ihid
->bufsize
)
333 size
= 2 /* size */ +
334 (reportID
? 1 : 0) /* reportID */ +
336 args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
337 2 /* dataRegister */ +
340 if (!use_data
&& maxOutputLength
== 0)
343 if (reportID
>= 0x0F) {
344 args
[index
++] = reportID
;
349 * use the data register for feature reports or if the device does not
350 * support the output register
353 args
[index
++] = dataRegister
& 0xFF;
354 args
[index
++] = dataRegister
>> 8;
355 hidcmd
= &hid_set_report_cmd
;
357 args
[index
++] = outputRegister
& 0xFF;
358 args
[index
++] = outputRegister
>> 8;
359 hidcmd
= &hid_no_cmd
;
362 args
[index
++] = size
& 0xFF;
363 args
[index
++] = size
>> 8;
366 args
[index
++] = reportID
;
368 memcpy(&args
[index
], buf
, data_len
);
370 ret
= __i2c_hid_command(client
, hidcmd
, reportID
,
371 reportType
, args
, args_len
, NULL
, 0);
373 dev_err(&client
->dev
, "failed to set a report to device.\n");
380 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
382 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
385 i2c_hid_dbg(ihid
, "%s\n", __func__
);
388 * Some devices require to send a command to wakeup before power on.
389 * The call will get a return value (EREMOTEIO) but device will be
390 * triggered and activated. After that, it goes like a normal device.
392 if (power_state
== I2C_HID_PWR_ON
&&
393 ihid
->quirks
& I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV
) {
394 ret
= i2c_hid_command(client
, &hid_set_power_cmd
, NULL
, 0);
396 /* Device was already activated */
401 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
402 0, NULL
, 0, NULL
, 0);
405 dev_err(&client
->dev
, "failed to change power setting.\n");
411 static int i2c_hid_hwreset(struct i2c_client
*client
)
413 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
416 i2c_hid_dbg(ihid
, "%s\n", __func__
);
419 * This prevents sending feature reports while the device is
420 * being reset. Otherwise we may lose the reset complete
423 mutex_lock(&ihid
->reset_lock
);
425 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
430 * The HID over I2C specification states that if a DEVICE needs time
431 * after the PWR_ON request, it should utilise CLOCK stretching.
432 * However, it has been observered that the Windows driver provides a
433 * 1ms sleep between the PWR_ON and RESET requests and that some devices
436 usleep_range(1000, 5000);
438 i2c_hid_dbg(ihid
, "resetting...\n");
440 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
442 dev_err(&client
->dev
, "failed to reset device.\n");
443 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
447 mutex_unlock(&ihid
->reset_lock
);
451 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
454 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
456 if (size
> ihid
->bufsize
)
457 size
= ihid
->bufsize
;
459 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
464 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
465 __func__
, ret
, size
);
469 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
472 /* host or device initiated RESET completed */
473 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
474 wake_up(&ihid
->wait
);
478 if (ret_size
> size
) {
479 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
480 __func__
, size
, ret_size
);
484 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
486 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
487 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
493 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
495 struct i2c_hid
*ihid
= dev_id
;
497 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
500 i2c_hid_get_input(ihid
);
505 static int i2c_hid_get_report_length(struct hid_report
*report
)
507 return ((report
->size
- 1) >> 3) + 1 +
508 report
->device
->report_enum
[report
->type
].numbered
+ 2;
511 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
514 struct hid_device
*hid
= report
->device
;
515 struct i2c_client
*client
= hid
->driver_data
;
516 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
517 unsigned int size
, ret_size
;
519 size
= i2c_hid_get_report_length(report
);
520 if (i2c_hid_get_report(client
,
521 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
522 report
->id
, buffer
, size
))
525 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, buffer
);
527 ret_size
= buffer
[0] | (buffer
[1] << 8);
529 if (ret_size
!= size
) {
530 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
531 __func__
, size
, ret_size
);
535 /* hid->driver_lock is held as we are in probe function,
536 * we just need to setup the input fields, so using
537 * hid_report_raw_event is safe. */
538 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
542 * Initialize all reports
544 static void i2c_hid_init_reports(struct hid_device
*hid
)
546 struct hid_report
*report
;
547 struct i2c_client
*client
= hid
->driver_data
;
548 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
549 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
552 dev_err(&client
->dev
, "can not retrieve initial reports\n");
557 * The device must be powered on while we fetch initial reports
560 pm_runtime_get_sync(&client
->dev
);
562 list_for_each_entry(report
,
563 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
564 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
566 pm_runtime_put(&client
->dev
);
572 * Traverse the supplied list of reports and find the longest
574 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
577 struct hid_report
*report
;
580 /* We should not rely on wMaxInputLength, as some devices may set it to
582 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
583 size
= i2c_hid_get_report_length(report
);
589 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
593 kfree(ihid
->argsbuf
);
598 ihid
->argsbuf
= NULL
;
602 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
604 /* the worst case is computed from the set_report command with a
605 * reportID > 15 and the maximum report length */
606 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
607 sizeof(__u16
) + /* data register */
608 sizeof(__u16
) + /* size of the report */
609 report_size
; /* report */
611 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
612 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
613 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
614 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
616 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
617 i2c_hid_free_buffers(ihid
);
621 ihid
->bufsize
= report_size
;
626 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
627 unsigned char report_number
, __u8
*buf
, size_t count
,
628 unsigned char report_type
)
630 struct i2c_client
*client
= hid
->driver_data
;
631 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
632 size_t ret_count
, ask_count
;
635 if (report_type
== HID_OUTPUT_REPORT
)
638 /* +2 bytes to include the size of the reply in the query buffer */
639 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
641 ret
= i2c_hid_get_report(client
,
642 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
643 report_number
, ihid
->rawbuf
, ask_count
);
648 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
653 ret_count
= min(ret_count
, ask_count
);
655 /* The query buffer contains the size, dropping it in the reply */
656 count
= min(count
, ret_count
- 2);
657 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
662 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
663 size_t count
, unsigned char report_type
, bool use_data
)
665 struct i2c_client
*client
= hid
->driver_data
;
666 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
667 int report_id
= buf
[0];
670 if (report_type
== HID_INPUT_REPORT
)
673 mutex_lock(&ihid
->reset_lock
);
680 ret
= i2c_hid_set_or_send_report(client
,
681 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
682 report_id
, buf
, count
, use_data
);
684 if (report_id
&& ret
>= 0)
685 ret
++; /* add report_id to the number of transfered bytes */
687 mutex_unlock(&ihid
->reset_lock
);
692 static int i2c_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
695 return i2c_hid_output_raw_report(hid
, buf
, count
, HID_OUTPUT_REPORT
,
699 static int i2c_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
700 __u8
*buf
, size_t len
, unsigned char rtype
,
704 case HID_REQ_GET_REPORT
:
705 return i2c_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
706 case HID_REQ_SET_REPORT
:
707 if (buf
[0] != reportnum
)
709 return i2c_hid_output_raw_report(hid
, buf
, len
, rtype
, true);
715 static int i2c_hid_parse(struct hid_device
*hid
)
717 struct i2c_client
*client
= hid
->driver_data
;
718 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
719 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
725 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
727 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
728 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
729 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
734 ret
= i2c_hid_hwreset(client
);
737 } while (tries
-- > 0 && ret
);
742 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
745 dbg_hid("couldn't allocate rdesc memory\n");
749 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
751 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
753 hid_err(hid
, "reading report descriptor failed\n");
758 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
760 ret
= hid_parse_report(hid
, rdesc
, rsize
);
763 dbg_hid("parsing report descriptor failed\n");
770 static int i2c_hid_start(struct hid_device
*hid
)
772 struct i2c_client
*client
= hid
->driver_data
;
773 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
775 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
777 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
778 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
779 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
781 if (bufsize
> ihid
->bufsize
) {
782 disable_irq(client
->irq
);
783 i2c_hid_free_buffers(ihid
);
785 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
786 enable_irq(client
->irq
);
792 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
793 i2c_hid_init_reports(hid
);
798 static void i2c_hid_stop(struct hid_device
*hid
)
803 static int i2c_hid_open(struct hid_device
*hid
)
805 struct i2c_client
*client
= hid
->driver_data
;
806 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
809 mutex_lock(&i2c_hid_open_mut
);
811 ret
= pm_runtime_get_sync(&client
->dev
);
816 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
819 mutex_unlock(&i2c_hid_open_mut
);
820 return ret
< 0 ? ret
: 0;
823 static void i2c_hid_close(struct hid_device
*hid
)
825 struct i2c_client
*client
= hid
->driver_data
;
826 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
828 /* protecting hid->open to make sure we don't restart
829 * data acquistion due to a resumption we no longer
832 mutex_lock(&i2c_hid_open_mut
);
834 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
836 /* Save some power */
837 pm_runtime_put(&client
->dev
);
839 mutex_unlock(&i2c_hid_open_mut
);
842 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
844 struct i2c_client
*client
= hid
->driver_data
;
845 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
847 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
851 pm_runtime_get_sync(&client
->dev
);
854 pm_runtime_put(&client
->dev
);
860 static struct hid_ll_driver i2c_hid_ll_driver
= {
861 .parse
= i2c_hid_parse
,
862 .start
= i2c_hid_start
,
863 .stop
= i2c_hid_stop
,
864 .open
= i2c_hid_open
,
865 .close
= i2c_hid_close
,
866 .power
= i2c_hid_power
,
867 .output_report
= i2c_hid_output_report
,
868 .raw_request
= i2c_hid_raw_request
,
871 static int i2c_hid_init_irq(struct i2c_client
*client
)
873 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
874 unsigned long irqflags
= 0;
877 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
879 if (!irq_get_trigger_type(client
->irq
))
880 irqflags
= IRQF_TRIGGER_LOW
;
882 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
883 irqflags
| IRQF_ONESHOT
, client
->name
, ihid
);
885 dev_warn(&client
->dev
,
886 "Could not register for %s interrupt, irq = %d,"
888 client
->name
, client
->irq
, ret
);
896 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
898 struct i2c_client
*client
= ihid
->client
;
899 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
903 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
904 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
905 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
906 sizeof(struct i2c_hid_desc
));
908 dev_err(&client
->dev
, "hid_descr_cmd failed\n");
912 /* Validate the length of HID descriptor, the 4 first bytes:
913 * bytes 0-1 -> length
914 * bytes 2-3 -> bcdVersion (has to be 1.00) */
915 /* check bcdVersion == 1.0 */
916 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
917 dev_err(&client
->dev
,
918 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
919 le16_to_cpu(hdesc
->bcdVersion
));
923 /* Descriptor length should be 30 bytes as per the specification */
924 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
925 if (dsize
!= sizeof(struct i2c_hid_desc
)) {
926 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
930 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
935 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
936 struct i2c_hid_platform_data
*pdata
)
938 static u8 i2c_hid_guid
[] = {
939 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
940 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
942 union acpi_object
*obj
;
943 struct acpi_device
*adev
;
946 handle
= ACPI_HANDLE(&client
->dev
);
947 if (!handle
|| acpi_bus_get_device(handle
, &adev
))
950 obj
= acpi_evaluate_dsm_typed(handle
, i2c_hid_guid
, 1, 1, NULL
,
953 dev_err(&client
->dev
, "device _DSM execution failed\n");
957 pdata
->hid_descriptor_address
= obj
->integer
.value
;
963 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
968 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
970 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
971 struct i2c_hid_platform_data
*pdata
)
978 static int i2c_hid_of_probe(struct i2c_client
*client
,
979 struct i2c_hid_platform_data
*pdata
)
981 struct device
*dev
= &client
->dev
;
985 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
987 dev_err(&client
->dev
, "HID register address not provided\n");
991 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
995 pdata
->hid_descriptor_address
= val
;
1000 static const struct of_device_id i2c_hid_of_match
[] = {
1001 { .compatible
= "hid-over-i2c" },
1004 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
1006 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
1007 struct i2c_hid_platform_data
*pdata
)
1013 static int i2c_hid_probe(struct i2c_client
*client
,
1014 const struct i2c_device_id
*dev_id
)
1017 struct i2c_hid
*ihid
;
1018 struct hid_device
*hid
;
1020 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
1022 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
1025 dev_err(&client
->dev
,
1026 "HID over i2c has not been provided an Int IRQ\n");
1030 if (client
->irq
< 0) {
1031 if (client
->irq
!= -EPROBE_DEFER
)
1032 dev_err(&client
->dev
,
1033 "HID over i2c doesn't have a valid IRQ\n");
1037 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
1041 if (client
->dev
.of_node
) {
1042 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
1045 } else if (!platform_data
) {
1046 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
1048 dev_err(&client
->dev
,
1049 "HID register address not provided\n");
1053 ihid
->pdata
= *platform_data
;
1056 i2c_set_clientdata(client
, ihid
);
1058 ihid
->client
= client
;
1060 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
1061 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
1063 init_waitqueue_head(&ihid
->wait
);
1064 mutex_init(&ihid
->reset_lock
);
1066 /* we need to allocate the command buffer without knowing the maximum
1067 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1068 * real computation later. */
1069 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
1073 pm_runtime_get_noresume(&client
->dev
);
1074 pm_runtime_set_active(&client
->dev
);
1075 pm_runtime_enable(&client
->dev
);
1076 device_enable_async_suspend(&client
->dev
);
1078 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
1082 ret
= i2c_hid_init_irq(client
);
1086 hid
= hid_allocate_device();
1094 hid
->driver_data
= client
;
1095 hid
->ll_driver
= &i2c_hid_ll_driver
;
1096 hid
->dev
.parent
= &client
->dev
;
1098 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1099 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1100 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1102 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1103 client
->name
, hid
->vendor
, hid
->product
);
1104 strlcpy(hid
->phys
, dev_name(&client
->dev
), sizeof(hid
->phys
));
1106 ihid
->quirks
= i2c_hid_lookup_quirk(hid
->vendor
, hid
->product
);
1108 ret
= hid_add_device(hid
);
1111 hid_err(client
, "can't add hid device: %d\n", ret
);
1115 pm_runtime_put(&client
->dev
);
1119 hid_destroy_device(hid
);
1122 free_irq(client
->irq
, ihid
);
1125 pm_runtime_put_noidle(&client
->dev
);
1126 pm_runtime_disable(&client
->dev
);
1129 i2c_hid_free_buffers(ihid
);
1134 static int i2c_hid_remove(struct i2c_client
*client
)
1136 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1137 struct hid_device
*hid
;
1139 pm_runtime_get_sync(&client
->dev
);
1140 pm_runtime_disable(&client
->dev
);
1141 pm_runtime_set_suspended(&client
->dev
);
1142 pm_runtime_put_noidle(&client
->dev
);
1145 hid_destroy_device(hid
);
1147 free_irq(client
->irq
, ihid
);
1150 i2c_hid_free_buffers(ihid
);
1157 static void i2c_hid_shutdown(struct i2c_client
*client
)
1159 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1161 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1162 free_irq(client
->irq
, ihid
);
1165 #ifdef CONFIG_PM_SLEEP
1166 static int i2c_hid_suspend(struct device
*dev
)
1168 struct i2c_client
*client
= to_i2c_client(dev
);
1169 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1170 struct hid_device
*hid
= ihid
->hid
;
1174 if (hid
->driver
&& hid
->driver
->suspend
) {
1176 * Wake up the device so that IO issues in
1177 * HID driver's suspend code can succeed.
1179 ret
= pm_runtime_resume(dev
);
1183 ret
= hid
->driver
->suspend(hid
, PMSG_SUSPEND
);
1188 if (!pm_runtime_suspended(dev
)) {
1189 /* Save some power */
1190 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1192 disable_irq(client
->irq
);
1195 if (device_may_wakeup(&client
->dev
)) {
1196 wake_status
= enable_irq_wake(client
->irq
);
1198 ihid
->irq_wake_enabled
= true;
1200 hid_warn(hid
, "Failed to enable irq wake: %d\n",
1207 static int i2c_hid_resume(struct device
*dev
)
1210 struct i2c_client
*client
= to_i2c_client(dev
);
1211 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1212 struct hid_device
*hid
= ihid
->hid
;
1215 if (device_may_wakeup(&client
->dev
) && ihid
->irq_wake_enabled
) {
1216 wake_status
= disable_irq_wake(client
->irq
);
1218 ihid
->irq_wake_enabled
= false;
1220 hid_warn(hid
, "Failed to disable irq wake: %d\n",
1224 /* We'll resume to full power */
1225 pm_runtime_disable(dev
);
1226 pm_runtime_set_active(dev
);
1227 pm_runtime_enable(dev
);
1229 enable_irq(client
->irq
);
1230 ret
= i2c_hid_hwreset(client
);
1234 if (hid
->driver
&& hid
->driver
->reset_resume
) {
1235 ret
= hid
->driver
->reset_resume(hid
);
1244 static int i2c_hid_runtime_suspend(struct device
*dev
)
1246 struct i2c_client
*client
= to_i2c_client(dev
);
1248 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1249 disable_irq(client
->irq
);
1253 static int i2c_hid_runtime_resume(struct device
*dev
)
1255 struct i2c_client
*client
= to_i2c_client(dev
);
1257 enable_irq(client
->irq
);
1258 i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1263 static const struct dev_pm_ops i2c_hid_pm
= {
1264 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend
, i2c_hid_resume
)
1265 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend
, i2c_hid_runtime_resume
,
1269 static const struct i2c_device_id i2c_hid_id_table
[] = {
1271 { "hid-over-i2c", 0 },
1274 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1277 static struct i2c_driver i2c_hid_driver
= {
1281 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1282 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1285 .probe
= i2c_hid_probe
,
1286 .remove
= i2c_hid_remove
,
1287 .shutdown
= i2c_hid_shutdown
,
1288 .id_table
= i2c_hid_id_table
,
1291 module_i2c_driver(i2c_hid_driver
);
1293 MODULE_DESCRIPTION("HID over I2C core driver");
1294 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1295 MODULE_LICENSE("GPL");