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/delay.h>
26 #include <linux/slab.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/device.h>
30 #include <linux/wait.h>
31 #include <linux/err.h>
32 #include <linux/string.h>
33 #include <linux/list.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/hid.h>
37 #include <linux/mutex.h>
38 #include <linux/acpi.h>
40 #include <linux/gpio/consumer.h>
42 #include <linux/i2c/i2c-hid.h>
45 #define I2C_HID_STARTED 0
46 #define I2C_HID_RESET_PENDING 1
47 #define I2C_HID_READ_PENDING 2
49 #define I2C_HID_PWR_ON 0x00
50 #define I2C_HID_PWR_SLEEP 0x01
54 module_param(debug
, bool, 0444);
55 MODULE_PARM_DESC(debug
, "print a lot of debug information");
57 #define i2c_hid_dbg(ihid, fmt, arg...) \
60 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
64 __le16 wHIDDescLength
;
66 __le16 wReportDescLength
;
67 __le16 wReportDescRegister
;
68 __le16 wInputRegister
;
69 __le16 wMaxInputLength
;
70 __le16 wOutputRegister
;
71 __le16 wMaxOutputLength
;
72 __le16 wCommandRegister
;
81 unsigned int registerIndex
;
96 #define I2C_HID_CMD(opcode_) \
97 .opcode = opcode_, .length = 4, \
98 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
100 /* fetch HID descriptor */
101 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
102 /* fetch report descriptors */
103 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
104 .registerIndex
= offsetof(struct i2c_hid_desc
,
105 wReportDescRegister
),
109 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
111 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
112 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
113 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
114 static const struct i2c_hid_cmd hid_no_cmd
= { .length
= 0 };
117 * These definitions are not used here, but are defined by the spec.
118 * Keeping them here for documentation purposes.
120 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
121 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
122 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
123 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
126 static DEFINE_MUTEX(i2c_hid_open_mut
);
128 /* The main device structure */
130 struct i2c_client
*client
; /* i2c client */
131 struct hid_device
*hid
; /* pointer to corresponding HID dev */
133 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
134 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
136 __le16 wHIDDescRegister
; /* location of the i2c
137 * register of the HID
139 unsigned int bufsize
; /* i2c buffer size */
140 char *inbuf
; /* Input buffer */
141 char *rawbuf
; /* Raw Input buffer */
142 char *cmdbuf
; /* Command buffer */
143 char *argsbuf
; /* Command arguments buffer */
145 unsigned long flags
; /* device flags */
147 wait_queue_head_t wait
; /* For waiting the interrupt */
148 struct gpio_desc
*desc
;
151 struct i2c_hid_platform_data pdata
;
153 bool irq_wake_enabled
;
154 struct mutex reset_lock
;
157 static int __i2c_hid_command(struct i2c_client
*client
,
158 const struct i2c_hid_cmd
*command
, u8 reportID
,
159 u8 reportType
, u8
*args
, int args_len
,
160 unsigned char *buf_recv
, int data_len
)
162 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
163 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
165 struct i2c_msg msg
[2];
168 int length
= command
->length
;
169 bool wait
= command
->wait
;
170 unsigned int registerIndex
= command
->registerIndex
;
172 /* special case for hid_descr_cmd */
173 if (command
== &hid_descr_cmd
) {
174 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
176 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
177 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
181 cmd
->c
.opcode
= command
->opcode
;
182 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
185 memcpy(cmd
->data
+ length
, args
, args_len
);
188 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
190 msg
[0].addr
= client
->addr
;
191 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
193 msg
[0].buf
= cmd
->data
;
195 msg
[1].addr
= client
->addr
;
196 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
197 msg
[1].flags
|= I2C_M_RD
;
198 msg
[1].len
= data_len
;
199 msg
[1].buf
= buf_recv
;
201 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
205 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
207 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
210 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
213 return ret
< 0 ? ret
: -EIO
;
218 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
219 if (!wait_event_timeout(ihid
->wait
,
220 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
221 msecs_to_jiffies(5000)))
223 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
229 static int i2c_hid_command(struct i2c_client
*client
,
230 const struct i2c_hid_cmd
*command
,
231 unsigned char *buf_recv
, int data_len
)
233 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
237 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
238 u8 reportID
, unsigned char *buf_recv
, int data_len
)
240 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
244 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
246 i2c_hid_dbg(ihid
, "%s\n", __func__
);
248 if (reportID
>= 0x0F) {
249 args
[args_len
++] = reportID
;
253 args
[args_len
++] = readRegister
& 0xFF;
254 args
[args_len
++] = readRegister
>> 8;
256 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
257 reportType
, args
, args_len
, buf_recv
, data_len
);
259 dev_err(&client
->dev
,
260 "failed to retrieve report from device.\n");
268 * i2c_hid_set_or_send_report: forward an incoming report to the device
269 * @client: the i2c_client of the device
270 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
271 * @reportID: the report ID
272 * @buf: the actual data to transfer, without the report ID
274 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
276 static int i2c_hid_set_or_send_report(struct i2c_client
*client
, u8 reportType
,
277 u8 reportID
, unsigned char *buf
, size_t data_len
, bool use_data
)
279 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
280 u8
*args
= ihid
->argsbuf
;
281 const struct i2c_hid_cmd
*hidcmd
;
283 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
284 u16 outputRegister
= le16_to_cpu(ihid
->hdesc
.wOutputRegister
);
285 u16 maxOutputLength
= le16_to_cpu(ihid
->hdesc
.wMaxOutputLength
);
290 i2c_hid_dbg(ihid
, "%s\n", __func__
);
292 if (data_len
> ihid
->bufsize
)
295 size
= 2 /* size */ +
296 (reportID
? 1 : 0) /* reportID */ +
298 args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
299 2 /* dataRegister */ +
302 if (!use_data
&& maxOutputLength
== 0)
305 if (reportID
>= 0x0F) {
306 args
[index
++] = reportID
;
311 * use the data register for feature reports or if the device does not
312 * support the output register
315 args
[index
++] = dataRegister
& 0xFF;
316 args
[index
++] = dataRegister
>> 8;
317 hidcmd
= &hid_set_report_cmd
;
319 args
[index
++] = outputRegister
& 0xFF;
320 args
[index
++] = outputRegister
>> 8;
321 hidcmd
= &hid_no_cmd
;
324 args
[index
++] = size
& 0xFF;
325 args
[index
++] = size
>> 8;
328 args
[index
++] = reportID
;
330 memcpy(&args
[index
], buf
, data_len
);
332 ret
= __i2c_hid_command(client
, hidcmd
, reportID
,
333 reportType
, args
, args_len
, NULL
, 0);
335 dev_err(&client
->dev
, "failed to set a report to device.\n");
342 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
344 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
347 i2c_hid_dbg(ihid
, "%s\n", __func__
);
349 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
350 0, NULL
, 0, NULL
, 0);
352 dev_err(&client
->dev
, "failed to change power setting.\n");
357 static int i2c_hid_hwreset(struct i2c_client
*client
)
359 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
362 i2c_hid_dbg(ihid
, "%s\n", __func__
);
365 * This prevents sending feature reports while the device is
366 * being reset. Otherwise we may lose the reset complete
369 mutex_lock(&ihid
->reset_lock
);
371 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
375 i2c_hid_dbg(ihid
, "resetting...\n");
377 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
379 dev_err(&client
->dev
, "failed to reset device.\n");
380 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
384 mutex_unlock(&ihid
->reset_lock
);
388 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
391 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
393 if (size
> ihid
->bufsize
)
394 size
= ihid
->bufsize
;
396 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
401 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
402 __func__
, ret
, size
);
406 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
409 /* host or device initiated RESET completed */
410 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
411 wake_up(&ihid
->wait
);
415 if (ret_size
> size
) {
416 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
417 __func__
, size
, ret_size
);
421 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
423 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
424 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
430 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
432 struct i2c_hid
*ihid
= dev_id
;
434 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
437 i2c_hid_get_input(ihid
);
442 static int i2c_hid_get_report_length(struct hid_report
*report
)
444 return ((report
->size
- 1) >> 3) + 1 +
445 report
->device
->report_enum
[report
->type
].numbered
+ 2;
448 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
451 struct hid_device
*hid
= report
->device
;
452 struct i2c_client
*client
= hid
->driver_data
;
453 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
454 unsigned int size
, ret_size
;
456 size
= i2c_hid_get_report_length(report
);
457 if (i2c_hid_get_report(client
,
458 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
459 report
->id
, buffer
, size
))
462 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, buffer
);
464 ret_size
= buffer
[0] | (buffer
[1] << 8);
466 if (ret_size
!= size
) {
467 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
468 __func__
, size
, ret_size
);
472 /* hid->driver_lock is held as we are in probe function,
473 * we just need to setup the input fields, so using
474 * hid_report_raw_event is safe. */
475 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
479 * Initialize all reports
481 static void i2c_hid_init_reports(struct hid_device
*hid
)
483 struct hid_report
*report
;
484 struct i2c_client
*client
= hid
->driver_data
;
485 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
486 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
489 dev_err(&client
->dev
, "can not retrieve initial reports\n");
494 * The device must be powered on while we fetch initial reports
497 pm_runtime_get_sync(&client
->dev
);
499 list_for_each_entry(report
,
500 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
501 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
503 pm_runtime_put(&client
->dev
);
509 * Traverse the supplied list of reports and find the longest
511 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
514 struct hid_report
*report
;
517 /* We should not rely on wMaxInputLength, as some devices may set it to
519 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
520 size
= i2c_hid_get_report_length(report
);
526 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
530 kfree(ihid
->argsbuf
);
535 ihid
->argsbuf
= NULL
;
539 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
541 /* the worst case is computed from the set_report command with a
542 * reportID > 15 and the maximum report length */
543 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
544 sizeof(__u16
) + /* data register */
545 sizeof(__u16
) + /* size of the report */
546 report_size
; /* report */
548 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
549 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
550 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
551 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
553 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
554 i2c_hid_free_buffers(ihid
);
558 ihid
->bufsize
= report_size
;
563 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
564 unsigned char report_number
, __u8
*buf
, size_t count
,
565 unsigned char report_type
)
567 struct i2c_client
*client
= hid
->driver_data
;
568 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
569 size_t ret_count
, ask_count
;
572 if (report_type
== HID_OUTPUT_REPORT
)
575 /* +2 bytes to include the size of the reply in the query buffer */
576 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
578 ret
= i2c_hid_get_report(client
,
579 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
580 report_number
, ihid
->rawbuf
, ask_count
);
585 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
590 ret_count
= min(ret_count
, ask_count
);
592 /* The query buffer contains the size, dropping it in the reply */
593 count
= min(count
, ret_count
- 2);
594 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
599 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
600 size_t count
, unsigned char report_type
, bool use_data
)
602 struct i2c_client
*client
= hid
->driver_data
;
603 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
604 int report_id
= buf
[0];
607 if (report_type
== HID_INPUT_REPORT
)
610 mutex_lock(&ihid
->reset_lock
);
617 ret
= i2c_hid_set_or_send_report(client
,
618 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
619 report_id
, buf
, count
, use_data
);
621 if (report_id
&& ret
>= 0)
622 ret
++; /* add report_id to the number of transfered bytes */
624 mutex_unlock(&ihid
->reset_lock
);
629 static int i2c_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
632 return i2c_hid_output_raw_report(hid
, buf
, count
, HID_OUTPUT_REPORT
,
636 static int i2c_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
637 __u8
*buf
, size_t len
, unsigned char rtype
,
641 case HID_REQ_GET_REPORT
:
642 return i2c_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
643 case HID_REQ_SET_REPORT
:
644 if (buf
[0] != reportnum
)
646 return i2c_hid_output_raw_report(hid
, buf
, len
, rtype
, true);
652 static int i2c_hid_parse(struct hid_device
*hid
)
654 struct i2c_client
*client
= hid
->driver_data
;
655 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
656 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
662 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
664 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
665 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
666 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
671 ret
= i2c_hid_hwreset(client
);
674 } while (tries
-- > 0 && ret
);
679 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
682 dbg_hid("couldn't allocate rdesc memory\n");
686 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
688 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
690 hid_err(hid
, "reading report descriptor failed\n");
695 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
697 ret
= hid_parse_report(hid
, rdesc
, rsize
);
700 dbg_hid("parsing report descriptor failed\n");
707 static int i2c_hid_start(struct hid_device
*hid
)
709 struct i2c_client
*client
= hid
->driver_data
;
710 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
712 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
714 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
715 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
716 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
718 if (bufsize
> ihid
->bufsize
) {
719 i2c_hid_free_buffers(ihid
);
721 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
727 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
728 i2c_hid_init_reports(hid
);
733 static void i2c_hid_stop(struct hid_device
*hid
)
738 static int i2c_hid_open(struct hid_device
*hid
)
740 struct i2c_client
*client
= hid
->driver_data
;
741 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
744 mutex_lock(&i2c_hid_open_mut
);
746 ret
= pm_runtime_get_sync(&client
->dev
);
751 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
754 mutex_unlock(&i2c_hid_open_mut
);
755 return ret
< 0 ? ret
: 0;
758 static void i2c_hid_close(struct hid_device
*hid
)
760 struct i2c_client
*client
= hid
->driver_data
;
761 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
763 /* protecting hid->open to make sure we don't restart
764 * data acquistion due to a resumption we no longer
767 mutex_lock(&i2c_hid_open_mut
);
769 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
771 /* Save some power */
772 pm_runtime_put(&client
->dev
);
774 mutex_unlock(&i2c_hid_open_mut
);
777 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
779 struct i2c_client
*client
= hid
->driver_data
;
780 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
782 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
786 pm_runtime_get_sync(&client
->dev
);
789 pm_runtime_put(&client
->dev
);
795 static struct hid_ll_driver i2c_hid_ll_driver
= {
796 .parse
= i2c_hid_parse
,
797 .start
= i2c_hid_start
,
798 .stop
= i2c_hid_stop
,
799 .open
= i2c_hid_open
,
800 .close
= i2c_hid_close
,
801 .power
= i2c_hid_power
,
802 .output_report
= i2c_hid_output_report
,
803 .raw_request
= i2c_hid_raw_request
,
806 static int i2c_hid_init_irq(struct i2c_client
*client
)
808 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
811 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", ihid
->irq
);
813 ret
= request_threaded_irq(ihid
->irq
, NULL
, i2c_hid_irq
,
814 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
817 dev_warn(&client
->dev
,
818 "Could not register for %s interrupt, irq = %d,"
820 client
->name
, ihid
->irq
, ret
);
828 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
830 struct i2c_client
*client
= ihid
->client
;
831 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
835 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
836 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
837 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
838 sizeof(struct i2c_hid_desc
));
840 dev_err(&client
->dev
, "hid_descr_cmd failed\n");
844 /* Validate the length of HID descriptor, the 4 first bytes:
845 * bytes 0-1 -> length
846 * bytes 2-3 -> bcdVersion (has to be 1.00) */
847 /* check bcdVersion == 1.0 */
848 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
849 dev_err(&client
->dev
,
850 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
851 le16_to_cpu(hdesc
->bcdVersion
));
855 /* Descriptor length should be 30 bytes as per the specification */
856 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
857 if (dsize
!= sizeof(struct i2c_hid_desc
)) {
858 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
862 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
868 /* Default GPIO mapping */
869 static const struct acpi_gpio_params i2c_hid_irq_gpio
= { 0, 0, true };
870 static const struct acpi_gpio_mapping i2c_hid_acpi_gpios
[] = {
871 { "gpios", &i2c_hid_irq_gpio
, 1 },
875 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
876 struct i2c_hid_platform_data
*pdata
)
878 static u8 i2c_hid_guid
[] = {
879 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
880 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
882 union acpi_object
*obj
;
883 struct acpi_device
*adev
;
887 handle
= ACPI_HANDLE(&client
->dev
);
888 if (!handle
|| acpi_bus_get_device(handle
, &adev
))
891 obj
= acpi_evaluate_dsm_typed(handle
, i2c_hid_guid
, 1, 1, NULL
,
894 dev_err(&client
->dev
, "device _DSM execution failed\n");
898 pdata
->hid_descriptor_address
= obj
->integer
.value
;
901 /* GPIOs are optional */
902 ret
= acpi_dev_add_driver_gpios(adev
, i2c_hid_acpi_gpios
);
903 return ret
< 0 && ret
!= -ENXIO
? ret
: 0;
906 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
911 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
913 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
914 struct i2c_hid_platform_data
*pdata
)
921 static int i2c_hid_of_probe(struct i2c_client
*client
,
922 struct i2c_hid_platform_data
*pdata
)
924 struct device
*dev
= &client
->dev
;
928 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
930 dev_err(&client
->dev
, "HID register address not provided\n");
934 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
938 pdata
->hid_descriptor_address
= val
;
943 static const struct of_device_id i2c_hid_of_match
[] = {
944 { .compatible
= "hid-over-i2c" },
947 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
949 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
950 struct i2c_hid_platform_data
*pdata
)
956 static int i2c_hid_probe(struct i2c_client
*client
,
957 const struct i2c_device_id
*dev_id
)
960 struct i2c_hid
*ihid
;
961 struct hid_device
*hid
;
963 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
965 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
967 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
971 if (client
->dev
.of_node
) {
972 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
975 } else if (!platform_data
) {
976 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
978 dev_err(&client
->dev
,
979 "HID register address not provided\n");
983 ihid
->pdata
= *platform_data
;
986 if (client
->irq
> 0) {
987 ihid
->irq
= client
->irq
;
988 } else if (ACPI_COMPANION(&client
->dev
)) {
989 ihid
->desc
= gpiod_get(&client
->dev
, NULL
, GPIOD_IN
);
990 if (IS_ERR(ihid
->desc
)) {
991 dev_err(&client
->dev
, "Failed to get GPIO interrupt\n");
992 return PTR_ERR(ihid
->desc
);
995 ihid
->irq
= gpiod_to_irq(ihid
->desc
);
997 gpiod_put(ihid
->desc
);
998 dev_err(&client
->dev
, "Failed to convert GPIO to IRQ\n");
1003 i2c_set_clientdata(client
, ihid
);
1005 ihid
->client
= client
;
1007 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
1008 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
1010 init_waitqueue_head(&ihid
->wait
);
1011 mutex_init(&ihid
->reset_lock
);
1013 /* we need to allocate the command buffer without knowing the maximum
1014 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1015 * real computation later. */
1016 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
1020 pm_runtime_get_noresume(&client
->dev
);
1021 pm_runtime_set_active(&client
->dev
);
1022 pm_runtime_enable(&client
->dev
);
1023 device_enable_async_suspend(&client
->dev
);
1025 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
1029 ret
= i2c_hid_init_irq(client
);
1033 hid
= hid_allocate_device();
1041 hid
->driver_data
= client
;
1042 hid
->ll_driver
= &i2c_hid_ll_driver
;
1043 hid
->dev
.parent
= &client
->dev
;
1045 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1046 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1047 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1049 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1050 client
->name
, hid
->vendor
, hid
->product
);
1051 strlcpy(hid
->phys
, dev_name(&client
->dev
), sizeof(hid
->phys
));
1053 ret
= hid_add_device(hid
);
1056 hid_err(client
, "can't add hid device: %d\n", ret
);
1060 pm_runtime_put(&client
->dev
);
1064 hid_destroy_device(hid
);
1067 free_irq(ihid
->irq
, ihid
);
1070 pm_runtime_put_noidle(&client
->dev
);
1071 pm_runtime_disable(&client
->dev
);
1075 gpiod_put(ihid
->desc
);
1077 i2c_hid_free_buffers(ihid
);
1082 static int i2c_hid_remove(struct i2c_client
*client
)
1084 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1085 struct hid_device
*hid
;
1087 pm_runtime_get_sync(&client
->dev
);
1088 pm_runtime_disable(&client
->dev
);
1089 pm_runtime_set_suspended(&client
->dev
);
1090 pm_runtime_put_noidle(&client
->dev
);
1093 hid_destroy_device(hid
);
1095 free_irq(ihid
->irq
, ihid
);
1098 i2c_hid_free_buffers(ihid
);
1101 gpiod_put(ihid
->desc
);
1105 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&client
->dev
));
1110 static void i2c_hid_shutdown(struct i2c_client
*client
)
1112 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1114 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1115 free_irq(client
->irq
, ihid
);
1118 #ifdef CONFIG_PM_SLEEP
1119 static int i2c_hid_suspend(struct device
*dev
)
1121 struct i2c_client
*client
= to_i2c_client(dev
);
1122 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1123 struct hid_device
*hid
= ihid
->hid
;
1127 if (hid
->driver
&& hid
->driver
->suspend
) {
1129 * Wake up the device so that IO issues in
1130 * HID driver's suspend code can succeed.
1132 ret
= pm_runtime_resume(dev
);
1136 ret
= hid
->driver
->suspend(hid
, PMSG_SUSPEND
);
1141 if (!pm_runtime_suspended(dev
)) {
1142 /* Save some power */
1143 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1145 disable_irq(ihid
->irq
);
1148 if (device_may_wakeup(&client
->dev
)) {
1149 wake_status
= enable_irq_wake(ihid
->irq
);
1151 ihid
->irq_wake_enabled
= true;
1153 hid_warn(hid
, "Failed to enable irq wake: %d\n",
1160 static int i2c_hid_resume(struct device
*dev
)
1163 struct i2c_client
*client
= to_i2c_client(dev
);
1164 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1165 struct hid_device
*hid
= ihid
->hid
;
1168 if (device_may_wakeup(&client
->dev
) && ihid
->irq_wake_enabled
) {
1169 wake_status
= disable_irq_wake(ihid
->irq
);
1171 ihid
->irq_wake_enabled
= false;
1173 hid_warn(hid
, "Failed to disable irq wake: %d\n",
1177 /* We'll resume to full power */
1178 pm_runtime_disable(dev
);
1179 pm_runtime_set_active(dev
);
1180 pm_runtime_enable(dev
);
1182 enable_irq(ihid
->irq
);
1183 ret
= i2c_hid_hwreset(client
);
1187 if (hid
->driver
&& hid
->driver
->reset_resume
) {
1188 ret
= hid
->driver
->reset_resume(hid
);
1197 static int i2c_hid_runtime_suspend(struct device
*dev
)
1199 struct i2c_client
*client
= to_i2c_client(dev
);
1200 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1202 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1203 disable_irq(ihid
->irq
);
1207 static int i2c_hid_runtime_resume(struct device
*dev
)
1209 struct i2c_client
*client
= to_i2c_client(dev
);
1210 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1212 enable_irq(ihid
->irq
);
1213 i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1218 static const struct dev_pm_ops i2c_hid_pm
= {
1219 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend
, i2c_hid_resume
)
1220 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend
, i2c_hid_runtime_resume
,
1224 static const struct i2c_device_id i2c_hid_id_table
[] = {
1226 { "hid-over-i2c", 0 },
1229 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1232 static struct i2c_driver i2c_hid_driver
= {
1236 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1237 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1240 .probe
= i2c_hid_probe
,
1241 .remove
= i2c_hid_remove
,
1242 .shutdown
= i2c_hid_shutdown
,
1243 .id_table
= i2c_hid_id_table
,
1246 module_i2c_driver(i2c_hid_driver
);
1248 MODULE_DESCRIPTION("HID over I2C core driver");
1249 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1250 MODULE_LICENSE("GPL");