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
;
156 static int __i2c_hid_command(struct i2c_client
*client
,
157 const struct i2c_hid_cmd
*command
, u8 reportID
,
158 u8 reportType
, u8
*args
, int args_len
,
159 unsigned char *buf_recv
, int data_len
)
161 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
162 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
164 struct i2c_msg msg
[2];
167 int length
= command
->length
;
168 bool wait
= command
->wait
;
169 unsigned int registerIndex
= command
->registerIndex
;
171 /* special case for hid_descr_cmd */
172 if (command
== &hid_descr_cmd
) {
173 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
175 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
176 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
180 cmd
->c
.opcode
= command
->opcode
;
181 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
184 memcpy(cmd
->data
+ length
, args
, args_len
);
187 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
189 msg
[0].addr
= client
->addr
;
190 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
192 msg
[0].buf
= cmd
->data
;
194 msg
[1].addr
= client
->addr
;
195 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
196 msg
[1].flags
|= I2C_M_RD
;
197 msg
[1].len
= data_len
;
198 msg
[1].buf
= buf_recv
;
200 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
204 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
206 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
209 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
212 return ret
< 0 ? ret
: -EIO
;
217 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
218 if (!wait_event_timeout(ihid
->wait
,
219 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
220 msecs_to_jiffies(5000)))
222 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
228 static int i2c_hid_command(struct i2c_client
*client
,
229 const struct i2c_hid_cmd
*command
,
230 unsigned char *buf_recv
, int data_len
)
232 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
236 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
237 u8 reportID
, unsigned char *buf_recv
, int data_len
)
239 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
243 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
245 i2c_hid_dbg(ihid
, "%s\n", __func__
);
247 if (reportID
>= 0x0F) {
248 args
[args_len
++] = reportID
;
252 args
[args_len
++] = readRegister
& 0xFF;
253 args
[args_len
++] = readRegister
>> 8;
255 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
256 reportType
, args
, args_len
, buf_recv
, data_len
);
258 dev_err(&client
->dev
,
259 "failed to retrieve report from device.\n");
267 * i2c_hid_set_or_send_report: forward an incoming report to the device
268 * @client: the i2c_client of the device
269 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
270 * @reportID: the report ID
271 * @buf: the actual data to transfer, without the report ID
273 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
275 static int i2c_hid_set_or_send_report(struct i2c_client
*client
, u8 reportType
,
276 u8 reportID
, unsigned char *buf
, size_t data_len
, bool use_data
)
278 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
279 u8
*args
= ihid
->argsbuf
;
280 const struct i2c_hid_cmd
*hidcmd
;
282 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
283 u16 outputRegister
= le16_to_cpu(ihid
->hdesc
.wOutputRegister
);
284 u16 maxOutputLength
= le16_to_cpu(ihid
->hdesc
.wMaxOutputLength
);
286 /* hid_hw_* already checked that data_len < HID_MAX_BUFFER_SIZE */
287 u16 size
= 2 /* size */ +
288 (reportID
? 1 : 0) /* reportID */ +
290 int args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
291 2 /* dataRegister */ +
295 i2c_hid_dbg(ihid
, "%s\n", __func__
);
297 if (!use_data
&& maxOutputLength
== 0)
300 if (reportID
>= 0x0F) {
301 args
[index
++] = reportID
;
306 * use the data register for feature reports or if the device does not
307 * support the output register
310 args
[index
++] = dataRegister
& 0xFF;
311 args
[index
++] = dataRegister
>> 8;
312 hidcmd
= &hid_set_report_cmd
;
314 args
[index
++] = outputRegister
& 0xFF;
315 args
[index
++] = outputRegister
>> 8;
316 hidcmd
= &hid_no_cmd
;
319 args
[index
++] = size
& 0xFF;
320 args
[index
++] = size
>> 8;
323 args
[index
++] = reportID
;
325 memcpy(&args
[index
], buf
, data_len
);
327 ret
= __i2c_hid_command(client
, hidcmd
, reportID
,
328 reportType
, args
, args_len
, NULL
, 0);
330 dev_err(&client
->dev
, "failed to set a report to device.\n");
337 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
339 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
342 i2c_hid_dbg(ihid
, "%s\n", __func__
);
344 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
345 0, NULL
, 0, NULL
, 0);
347 dev_err(&client
->dev
, "failed to change power setting.\n");
352 static int i2c_hid_hwreset(struct i2c_client
*client
)
354 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
357 i2c_hid_dbg(ihid
, "%s\n", __func__
);
359 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
363 i2c_hid_dbg(ihid
, "resetting...\n");
365 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
367 dev_err(&client
->dev
, "failed to reset device.\n");
368 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
375 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
378 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
380 if (size
> ihid
->bufsize
)
381 size
= ihid
->bufsize
;
383 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
388 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
389 __func__
, ret
, size
);
393 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
396 /* host or device initiated RESET completed */
397 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
398 wake_up(&ihid
->wait
);
402 if (ret_size
> size
) {
403 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
404 __func__
, size
, ret_size
);
408 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
410 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
411 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
417 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
419 struct i2c_hid
*ihid
= dev_id
;
421 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
424 i2c_hid_get_input(ihid
);
429 static int i2c_hid_get_report_length(struct hid_report
*report
)
431 return ((report
->size
- 1) >> 3) + 1 +
432 report
->device
->report_enum
[report
->type
].numbered
+ 2;
435 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
438 struct hid_device
*hid
= report
->device
;
439 struct i2c_client
*client
= hid
->driver_data
;
440 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
441 unsigned int size
, ret_size
;
443 size
= i2c_hid_get_report_length(report
);
444 if (i2c_hid_get_report(client
,
445 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
446 report
->id
, buffer
, size
))
449 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, buffer
);
451 ret_size
= buffer
[0] | (buffer
[1] << 8);
453 if (ret_size
!= size
) {
454 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
455 __func__
, size
, ret_size
);
459 /* hid->driver_lock is held as we are in probe function,
460 * we just need to setup the input fields, so using
461 * hid_report_raw_event is safe. */
462 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
466 * Initialize all reports
468 static void i2c_hid_init_reports(struct hid_device
*hid
)
470 struct hid_report
*report
;
471 struct i2c_client
*client
= hid
->driver_data
;
472 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
473 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
476 dev_err(&client
->dev
, "can not retrieve initial reports\n");
481 * The device must be powered on while we fetch initial reports
484 pm_runtime_get_sync(&client
->dev
);
486 list_for_each_entry(report
,
487 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
488 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
490 pm_runtime_put(&client
->dev
);
496 * Traverse the supplied list of reports and find the longest
498 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
501 struct hid_report
*report
;
504 /* We should not rely on wMaxInputLength, as some devices may set it to
506 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
507 size
= i2c_hid_get_report_length(report
);
513 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
517 kfree(ihid
->argsbuf
);
522 ihid
->argsbuf
= NULL
;
526 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
528 /* the worst case is computed from the set_report command with a
529 * reportID > 15 and the maximum report length */
530 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
531 sizeof(__u16
) + /* data register */
532 sizeof(__u16
) + /* size of the report */
533 report_size
; /* report */
535 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
536 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
537 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
538 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
540 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
541 i2c_hid_free_buffers(ihid
);
545 ihid
->bufsize
= report_size
;
550 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
551 unsigned char report_number
, __u8
*buf
, size_t count
,
552 unsigned char report_type
)
554 struct i2c_client
*client
= hid
->driver_data
;
555 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
556 size_t ret_count
, ask_count
;
559 if (report_type
== HID_OUTPUT_REPORT
)
562 /* +2 bytes to include the size of the reply in the query buffer */
563 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
565 ret
= i2c_hid_get_report(client
,
566 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
567 report_number
, ihid
->rawbuf
, ask_count
);
572 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
577 ret_count
= min(ret_count
, ask_count
);
579 /* The query buffer contains the size, dropping it in the reply */
580 count
= min(count
, ret_count
- 2);
581 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
586 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
587 size_t count
, unsigned char report_type
, bool use_data
)
589 struct i2c_client
*client
= hid
->driver_data
;
590 int report_id
= buf
[0];
593 if (report_type
== HID_INPUT_REPORT
)
601 ret
= i2c_hid_set_or_send_report(client
,
602 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
603 report_id
, buf
, count
, use_data
);
605 if (report_id
&& ret
>= 0)
606 ret
++; /* add report_id to the number of transfered bytes */
611 static int i2c_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
614 return i2c_hid_output_raw_report(hid
, buf
, count
, HID_OUTPUT_REPORT
,
618 static int i2c_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
619 __u8
*buf
, size_t len
, unsigned char rtype
,
623 case HID_REQ_GET_REPORT
:
624 return i2c_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
625 case HID_REQ_SET_REPORT
:
626 if (buf
[0] != reportnum
)
628 return i2c_hid_output_raw_report(hid
, buf
, len
, rtype
, true);
634 static int i2c_hid_parse(struct hid_device
*hid
)
636 struct i2c_client
*client
= hid
->driver_data
;
637 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
638 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
644 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
646 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
647 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
648 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
653 ret
= i2c_hid_hwreset(client
);
656 } while (tries
-- > 0 && ret
);
661 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
664 dbg_hid("couldn't allocate rdesc memory\n");
668 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
670 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
672 hid_err(hid
, "reading report descriptor failed\n");
677 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
679 ret
= hid_parse_report(hid
, rdesc
, rsize
);
682 dbg_hid("parsing report descriptor failed\n");
689 static int i2c_hid_start(struct hid_device
*hid
)
691 struct i2c_client
*client
= hid
->driver_data
;
692 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
694 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
696 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
697 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
698 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
700 if (bufsize
> ihid
->bufsize
) {
701 i2c_hid_free_buffers(ihid
);
703 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
709 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
710 i2c_hid_init_reports(hid
);
715 static void i2c_hid_stop(struct hid_device
*hid
)
720 static int i2c_hid_open(struct hid_device
*hid
)
722 struct i2c_client
*client
= hid
->driver_data
;
723 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
726 mutex_lock(&i2c_hid_open_mut
);
728 ret
= pm_runtime_get_sync(&client
->dev
);
733 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
736 mutex_unlock(&i2c_hid_open_mut
);
737 return ret
< 0 ? ret
: 0;
740 static void i2c_hid_close(struct hid_device
*hid
)
742 struct i2c_client
*client
= hid
->driver_data
;
743 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
745 /* protecting hid->open to make sure we don't restart
746 * data acquistion due to a resumption we no longer
749 mutex_lock(&i2c_hid_open_mut
);
751 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
753 /* Save some power */
754 pm_runtime_put(&client
->dev
);
756 mutex_unlock(&i2c_hid_open_mut
);
759 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
761 struct i2c_client
*client
= hid
->driver_data
;
762 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
764 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
768 pm_runtime_get_sync(&client
->dev
);
771 pm_runtime_put(&client
->dev
);
777 static struct hid_ll_driver i2c_hid_ll_driver
= {
778 .parse
= i2c_hid_parse
,
779 .start
= i2c_hid_start
,
780 .stop
= i2c_hid_stop
,
781 .open
= i2c_hid_open
,
782 .close
= i2c_hid_close
,
783 .power
= i2c_hid_power
,
784 .output_report
= i2c_hid_output_report
,
785 .raw_request
= i2c_hid_raw_request
,
788 static int i2c_hid_init_irq(struct i2c_client
*client
)
790 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
793 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", ihid
->irq
);
795 ret
= request_threaded_irq(ihid
->irq
, NULL
, i2c_hid_irq
,
796 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
799 dev_warn(&client
->dev
,
800 "Could not register for %s interrupt, irq = %d,"
802 client
->name
, ihid
->irq
, ret
);
810 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
812 struct i2c_client
*client
= ihid
->client
;
813 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
817 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
818 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
819 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
820 sizeof(struct i2c_hid_desc
));
822 dev_err(&client
->dev
, "hid_descr_cmd failed\n");
826 /* Validate the length of HID descriptor, the 4 first bytes:
827 * bytes 0-1 -> length
828 * bytes 2-3 -> bcdVersion (has to be 1.00) */
829 /* check bcdVersion == 1.0 */
830 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
831 dev_err(&client
->dev
,
832 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
833 le16_to_cpu(hdesc
->bcdVersion
));
837 /* Descriptor length should be 30 bytes as per the specification */
838 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
839 if (dsize
!= sizeof(struct i2c_hid_desc
)) {
840 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
844 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
850 /* Default GPIO mapping */
851 static const struct acpi_gpio_params i2c_hid_irq_gpio
= { 0, 0, true };
852 static const struct acpi_gpio_mapping i2c_hid_acpi_gpios
[] = {
853 { "gpios", &i2c_hid_irq_gpio
, 1 },
857 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
858 struct i2c_hid_platform_data
*pdata
)
860 static u8 i2c_hid_guid
[] = {
861 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
862 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
864 union acpi_object
*obj
;
865 struct acpi_device
*adev
;
869 handle
= ACPI_HANDLE(&client
->dev
);
870 if (!handle
|| acpi_bus_get_device(handle
, &adev
))
873 obj
= acpi_evaluate_dsm_typed(handle
, i2c_hid_guid
, 1, 1, NULL
,
876 dev_err(&client
->dev
, "device _DSM execution failed\n");
880 pdata
->hid_descriptor_address
= obj
->integer
.value
;
883 /* GPIOs are optional */
884 ret
= acpi_dev_add_driver_gpios(adev
, i2c_hid_acpi_gpios
);
885 return ret
< 0 && ret
!= -ENXIO
? ret
: 0;
888 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
893 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
895 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
896 struct i2c_hid_platform_data
*pdata
)
903 static int i2c_hid_of_probe(struct i2c_client
*client
,
904 struct i2c_hid_platform_data
*pdata
)
906 struct device
*dev
= &client
->dev
;
910 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
912 dev_err(&client
->dev
, "HID register address not provided\n");
916 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
920 pdata
->hid_descriptor_address
= val
;
925 static const struct of_device_id i2c_hid_of_match
[] = {
926 { .compatible
= "hid-over-i2c" },
929 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
931 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
932 struct i2c_hid_platform_data
*pdata
)
938 static int i2c_hid_probe(struct i2c_client
*client
,
939 const struct i2c_device_id
*dev_id
)
942 struct i2c_hid
*ihid
;
943 struct hid_device
*hid
;
945 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
947 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
949 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
953 if (client
->dev
.of_node
) {
954 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
957 } else if (!platform_data
) {
958 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
960 dev_err(&client
->dev
,
961 "HID register address not provided\n");
965 ihid
->pdata
= *platform_data
;
968 if (client
->irq
> 0) {
969 ihid
->irq
= client
->irq
;
970 } else if (ACPI_COMPANION(&client
->dev
)) {
971 ihid
->desc
= gpiod_get(&client
->dev
, NULL
, GPIOD_IN
);
972 if (IS_ERR(ihid
->desc
)) {
973 dev_err(&client
->dev
, "Failed to get GPIO interrupt\n");
974 return PTR_ERR(ihid
->desc
);
977 ihid
->irq
= gpiod_to_irq(ihid
->desc
);
979 gpiod_put(ihid
->desc
);
980 dev_err(&client
->dev
, "Failed to convert GPIO to IRQ\n");
985 i2c_set_clientdata(client
, ihid
);
987 ihid
->client
= client
;
989 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
990 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
992 init_waitqueue_head(&ihid
->wait
);
994 /* we need to allocate the command buffer without knowing the maximum
995 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
996 * real computation later. */
997 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
1001 pm_runtime_get_noresume(&client
->dev
);
1002 pm_runtime_set_active(&client
->dev
);
1003 pm_runtime_enable(&client
->dev
);
1005 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
1009 ret
= i2c_hid_init_irq(client
);
1013 hid
= hid_allocate_device();
1021 hid
->driver_data
= client
;
1022 hid
->ll_driver
= &i2c_hid_ll_driver
;
1023 hid
->dev
.parent
= &client
->dev
;
1025 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1026 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1027 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1029 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1030 client
->name
, hid
->vendor
, hid
->product
);
1031 strlcpy(hid
->phys
, dev_name(&client
->dev
), sizeof(hid
->phys
));
1033 ret
= hid_add_device(hid
);
1036 hid_err(client
, "can't add hid device: %d\n", ret
);
1040 pm_runtime_put(&client
->dev
);
1044 hid_destroy_device(hid
);
1047 free_irq(ihid
->irq
, ihid
);
1050 pm_runtime_put_noidle(&client
->dev
);
1051 pm_runtime_disable(&client
->dev
);
1055 gpiod_put(ihid
->desc
);
1057 i2c_hid_free_buffers(ihid
);
1062 static int i2c_hid_remove(struct i2c_client
*client
)
1064 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1065 struct hid_device
*hid
;
1067 pm_runtime_get_sync(&client
->dev
);
1068 pm_runtime_disable(&client
->dev
);
1069 pm_runtime_set_suspended(&client
->dev
);
1070 pm_runtime_put_noidle(&client
->dev
);
1073 hid_destroy_device(hid
);
1075 free_irq(ihid
->irq
, ihid
);
1078 i2c_hid_free_buffers(ihid
);
1081 gpiod_put(ihid
->desc
);
1085 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&client
->dev
));
1090 #ifdef CONFIG_PM_SLEEP
1091 static int i2c_hid_suspend(struct device
*dev
)
1093 struct i2c_client
*client
= to_i2c_client(dev
);
1094 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1095 struct hid_device
*hid
= ihid
->hid
;
1099 if (hid
->driver
&& hid
->driver
->suspend
)
1100 ret
= hid
->driver
->suspend(hid
, PMSG_SUSPEND
);
1102 disable_irq(ihid
->irq
);
1103 if (device_may_wakeup(&client
->dev
)) {
1104 wake_status
= enable_irq_wake(ihid
->irq
);
1106 ihid
->irq_wake_enabled
= true;
1108 hid_warn(hid
, "Failed to enable irq wake: %d\n",
1112 /* Save some power */
1113 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1118 static int i2c_hid_resume(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
;
1126 enable_irq(ihid
->irq
);
1127 ret
= i2c_hid_hwreset(client
);
1131 if (device_may_wakeup(&client
->dev
) && ihid
->irq_wake_enabled
) {
1132 wake_status
= disable_irq_wake(ihid
->irq
);
1134 ihid
->irq_wake_enabled
= false;
1136 hid_warn(hid
, "Failed to disable irq wake: %d\n",
1140 if (hid
->driver
&& hid
->driver
->reset_resume
) {
1141 ret
= hid
->driver
->reset_resume(hid
);
1150 static int i2c_hid_runtime_suspend(struct device
*dev
)
1152 struct i2c_client
*client
= to_i2c_client(dev
);
1153 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1155 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1156 disable_irq(ihid
->irq
);
1160 static int i2c_hid_runtime_resume(struct device
*dev
)
1162 struct i2c_client
*client
= to_i2c_client(dev
);
1163 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1165 enable_irq(ihid
->irq
);
1166 i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1171 static const struct dev_pm_ops i2c_hid_pm
= {
1172 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend
, i2c_hid_resume
)
1173 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend
, i2c_hid_runtime_resume
,
1177 static const struct i2c_device_id i2c_hid_id_table
[] = {
1181 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1184 static struct i2c_driver i2c_hid_driver
= {
1187 .owner
= THIS_MODULE
,
1189 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1190 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1193 .probe
= i2c_hid_probe
,
1194 .remove
= i2c_hid_remove
,
1196 .id_table
= i2c_hid_id_table
,
1199 module_i2c_driver(i2c_hid_driver
);
1201 MODULE_DESCRIPTION("HID over I2C core driver");
1202 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1203 MODULE_LICENSE("GPL");