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/device.h>
29 #include <linux/wait.h>
30 #include <linux/err.h>
31 #include <linux/string.h>
32 #include <linux/list.h>
33 #include <linux/jiffies.h>
34 #include <linux/kernel.h>
35 #include <linux/hid.h>
36 #include <linux/mutex.h>
37 #include <linux/acpi.h>
40 #include <linux/i2c/i2c-hid.h>
43 #define I2C_HID_STARTED (1 << 0)
44 #define I2C_HID_RESET_PENDING (1 << 1)
45 #define I2C_HID_READ_PENDING (1 << 2)
47 #define I2C_HID_PWR_ON 0x00
48 #define I2C_HID_PWR_SLEEP 0x01
52 module_param(debug
, bool, 0444);
53 MODULE_PARM_DESC(debug
, "print a lot of debug information");
55 #define i2c_hid_dbg(ihid, fmt, arg...) \
58 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
62 __le16 wHIDDescLength
;
64 __le16 wReportDescLength
;
65 __le16 wReportDescRegister
;
66 __le16 wInputRegister
;
67 __le16 wMaxInputLength
;
68 __le16 wOutputRegister
;
69 __le16 wMaxOutputLength
;
70 __le16 wCommandRegister
;
79 unsigned int registerIndex
;
94 #define I2C_HID_CMD(opcode_) \
95 .opcode = opcode_, .length = 4, \
96 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
98 /* fetch HID descriptor */
99 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
100 /* fetch report descriptors */
101 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
102 .registerIndex
= offsetof(struct i2c_hid_desc
,
103 wReportDescRegister
),
107 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
109 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
110 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
111 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
112 static const struct i2c_hid_cmd hid_no_cmd
= { .length
= 0 };
115 * These definitions are not used here, but are defined by the spec.
116 * Keeping them here for documentation purposes.
118 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
119 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
120 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
121 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
124 static DEFINE_MUTEX(i2c_hid_open_mut
);
126 /* The main device structure */
128 struct i2c_client
*client
; /* i2c client */
129 struct hid_device
*hid
; /* pointer to corresponding HID dev */
131 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
132 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
134 __le16 wHIDDescRegister
; /* location of the i2c
135 * register of the HID
137 unsigned int bufsize
; /* i2c buffer size */
138 char *inbuf
; /* Input buffer */
139 char *rawbuf
; /* Raw Input buffer */
140 char *cmdbuf
; /* Command buffer */
141 char *argsbuf
; /* Command arguments buffer */
143 unsigned long flags
; /* device flags */
145 wait_queue_head_t wait
; /* For waiting the interrupt */
147 struct i2c_hid_platform_data pdata
;
150 static int __i2c_hid_command(struct i2c_client
*client
,
151 const struct i2c_hid_cmd
*command
, u8 reportID
,
152 u8 reportType
, u8
*args
, int args_len
,
153 unsigned char *buf_recv
, int data_len
)
155 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
156 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
158 struct i2c_msg msg
[2];
161 int length
= command
->length
;
162 bool wait
= command
->wait
;
163 unsigned int registerIndex
= command
->registerIndex
;
165 /* special case for hid_descr_cmd */
166 if (command
== &hid_descr_cmd
) {
167 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
169 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
170 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
174 cmd
->c
.opcode
= command
->opcode
;
175 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
178 memcpy(cmd
->data
+ length
, args
, args_len
);
181 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
183 msg
[0].addr
= client
->addr
;
184 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
186 msg
[0].buf
= cmd
->data
;
188 msg
[1].addr
= client
->addr
;
189 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
190 msg
[1].flags
|= I2C_M_RD
;
191 msg
[1].len
= data_len
;
192 msg
[1].buf
= buf_recv
;
194 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
198 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
200 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
203 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
206 return ret
< 0 ? ret
: -EIO
;
211 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
212 if (!wait_event_timeout(ihid
->wait
,
213 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
214 msecs_to_jiffies(5000)))
216 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
222 static int i2c_hid_command(struct i2c_client
*client
,
223 const struct i2c_hid_cmd
*command
,
224 unsigned char *buf_recv
, int data_len
)
226 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
230 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
231 u8 reportID
, unsigned char *buf_recv
, int data_len
)
233 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
237 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
239 i2c_hid_dbg(ihid
, "%s\n", __func__
);
241 if (reportID
>= 0x0F) {
242 args
[args_len
++] = reportID
;
246 args
[args_len
++] = readRegister
& 0xFF;
247 args
[args_len
++] = readRegister
>> 8;
249 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
250 reportType
, args
, args_len
, buf_recv
, data_len
);
252 dev_err(&client
->dev
,
253 "failed to retrieve report from device.\n");
260 static int i2c_hid_set_report(struct i2c_client
*client
, u8 reportType
,
261 u8 reportID
, unsigned char *buf
, size_t data_len
)
263 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
264 u8
*args
= ihid
->argsbuf
;
265 const struct i2c_hid_cmd
* hidcmd
= &hid_set_report_cmd
;
267 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
268 u16 outputRegister
= le16_to_cpu(ihid
->hdesc
.wOutputRegister
);
269 u16 maxOutputLength
= le16_to_cpu(ihid
->hdesc
.wMaxOutputLength
);
271 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
272 u16 size
= 2 /* size */ +
273 (reportID
? 1 : 0) /* reportID */ +
275 int args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
276 2 /* dataRegister */ +
280 i2c_hid_dbg(ihid
, "%s\n", __func__
);
282 if (reportID
>= 0x0F) {
283 args
[index
++] = reportID
;
288 * use the data register for feature reports or if the device does not
289 * support the output register
291 if (reportType
== 0x03 || maxOutputLength
== 0) {
292 args
[index
++] = dataRegister
& 0xFF;
293 args
[index
++] = dataRegister
>> 8;
295 args
[index
++] = outputRegister
& 0xFF;
296 args
[index
++] = outputRegister
>> 8;
297 hidcmd
= &hid_no_cmd
;
300 args
[index
++] = size
& 0xFF;
301 args
[index
++] = size
>> 8;
304 args
[index
++] = reportID
;
306 memcpy(&args
[index
], buf
, data_len
);
308 ret
= __i2c_hid_command(client
, hidcmd
, reportID
,
309 reportType
, args
, args_len
, NULL
, 0);
311 dev_err(&client
->dev
, "failed to set a report to device.\n");
318 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
320 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
323 i2c_hid_dbg(ihid
, "%s\n", __func__
);
325 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
326 0, NULL
, 0, NULL
, 0);
328 dev_err(&client
->dev
, "failed to change power setting.\n");
333 static int i2c_hid_hwreset(struct i2c_client
*client
)
335 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
338 i2c_hid_dbg(ihid
, "%s\n", __func__
);
340 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
344 i2c_hid_dbg(ihid
, "resetting...\n");
346 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
348 dev_err(&client
->dev
, "failed to reset device.\n");
349 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
356 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
359 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
361 if (size
> ihid
->bufsize
)
362 size
= ihid
->bufsize
;
364 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
369 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
370 __func__
, ret
, size
);
374 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
377 /* host or device initiated RESET completed */
378 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
379 wake_up(&ihid
->wait
);
383 if (ret_size
> size
) {
384 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
385 __func__
, size
, ret_size
);
389 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
391 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
392 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
398 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
400 struct i2c_hid
*ihid
= dev_id
;
402 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
405 i2c_hid_get_input(ihid
);
410 static int i2c_hid_get_report_length(struct hid_report
*report
)
412 return ((report
->size
- 1) >> 3) + 1 +
413 report
->device
->report_enum
[report
->type
].numbered
+ 2;
416 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
419 struct hid_device
*hid
= report
->device
;
420 struct i2c_client
*client
= hid
->driver_data
;
421 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
422 unsigned int size
, ret_size
;
424 size
= i2c_hid_get_report_length(report
);
425 if (i2c_hid_get_report(client
,
426 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
427 report
->id
, buffer
, size
))
430 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, ihid
->inbuf
);
432 ret_size
= buffer
[0] | (buffer
[1] << 8);
434 if (ret_size
!= size
) {
435 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
436 __func__
, size
, ret_size
);
440 /* hid->driver_lock is held as we are in probe function,
441 * we just need to setup the input fields, so using
442 * hid_report_raw_event is safe. */
443 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
447 * Initialize all reports
449 static void i2c_hid_init_reports(struct hid_device
*hid
)
451 struct hid_report
*report
;
452 struct i2c_client
*client
= hid
->driver_data
;
453 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
454 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
457 dev_err(&client
->dev
, "can not retrieve initial reports\n");
461 list_for_each_entry(report
,
462 &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
463 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
465 list_for_each_entry(report
,
466 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
467 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
473 * Traverse the supplied list of reports and find the longest
475 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
478 struct hid_report
*report
;
481 /* We should not rely on wMaxInputLength, as some devices may set it to
483 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
484 size
= i2c_hid_get_report_length(report
);
490 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
494 kfree(ihid
->argsbuf
);
499 ihid
->argsbuf
= NULL
;
503 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
505 /* the worst case is computed from the set_report command with a
506 * reportID > 15 and the maximum report length */
507 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
508 sizeof(__u16
) + /* data register */
509 sizeof(__u16
) + /* size of the report */
510 report_size
; /* report */
512 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
513 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
514 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
515 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
517 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
518 i2c_hid_free_buffers(ihid
);
522 ihid
->bufsize
= report_size
;
527 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
528 unsigned char report_number
, __u8
*buf
, size_t count
,
529 unsigned char report_type
)
531 struct i2c_client
*client
= hid
->driver_data
;
532 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
533 size_t ret_count
, ask_count
;
536 if (report_type
== HID_OUTPUT_REPORT
)
539 /* +2 bytes to include the size of the reply in the query buffer */
540 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
542 ret
= i2c_hid_get_report(client
,
543 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
544 report_number
, ihid
->rawbuf
, ask_count
);
549 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
554 ret_count
= min(ret_count
, ask_count
);
556 /* The query buffer contains the size, dropping it in the reply */
557 count
= min(count
, ret_count
- 2);
558 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
563 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
564 size_t count
, unsigned char report_type
)
566 struct i2c_client
*client
= hid
->driver_data
;
567 int report_id
= buf
[0];
570 if (report_type
== HID_INPUT_REPORT
)
578 ret
= i2c_hid_set_report(client
,
579 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
580 report_id
, buf
, count
);
582 if (report_id
&& ret
>= 0)
583 ret
++; /* add report_id to the number of transfered bytes */
588 static void i2c_hid_request(struct hid_device
*hid
, struct hid_report
*rep
,
591 struct i2c_client
*client
= hid
->driver_data
;
594 int len
= i2c_hid_get_report_length(rep
) - 2;
596 buf
= kzalloc(len
, GFP_KERNEL
);
601 case HID_REQ_GET_REPORT
:
602 ret
= i2c_hid_get_raw_report(hid
, rep
->id
, buf
, len
, rep
->type
);
604 dev_err(&client
->dev
, "%s: unable to get report: %d\n",
607 hid_input_report(hid
, rep
->type
, buf
, ret
, 0);
609 case HID_REQ_SET_REPORT
:
610 hid_output_report(rep
, buf
);
611 i2c_hid_output_raw_report(hid
, buf
, len
, rep
->type
);
618 static int i2c_hid_parse(struct hid_device
*hid
)
620 struct i2c_client
*client
= hid
->driver_data
;
621 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
622 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
628 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
630 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
631 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
632 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
637 ret
= i2c_hid_hwreset(client
);
640 } while (tries
-- > 0 && ret
);
645 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
648 dbg_hid("couldn't allocate rdesc memory\n");
652 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
654 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
656 hid_err(hid
, "reading report descriptor failed\n");
661 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
663 ret
= hid_parse_report(hid
, rdesc
, rsize
);
666 dbg_hid("parsing report descriptor failed\n");
673 static int i2c_hid_start(struct hid_device
*hid
)
675 struct i2c_client
*client
= hid
->driver_data
;
676 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
678 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
680 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
681 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
682 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
684 if (bufsize
> ihid
->bufsize
) {
685 i2c_hid_free_buffers(ihid
);
687 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
693 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
694 i2c_hid_init_reports(hid
);
699 static void i2c_hid_stop(struct hid_device
*hid
)
701 struct i2c_client
*client
= hid
->driver_data
;
702 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
706 i2c_hid_free_buffers(ihid
);
709 static int i2c_hid_open(struct hid_device
*hid
)
711 struct i2c_client
*client
= hid
->driver_data
;
712 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
715 mutex_lock(&i2c_hid_open_mut
);
717 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
722 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
725 mutex_unlock(&i2c_hid_open_mut
);
729 static void i2c_hid_close(struct hid_device
*hid
)
731 struct i2c_client
*client
= hid
->driver_data
;
732 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
734 /* protecting hid->open to make sure we don't restart
735 * data acquistion due to a resumption we no longer
738 mutex_lock(&i2c_hid_open_mut
);
740 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
742 /* Save some power */
743 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
745 mutex_unlock(&i2c_hid_open_mut
);
748 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
750 struct i2c_client
*client
= hid
->driver_data
;
751 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
754 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
758 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
761 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
767 static struct hid_ll_driver i2c_hid_ll_driver
= {
768 .parse
= i2c_hid_parse
,
769 .start
= i2c_hid_start
,
770 .stop
= i2c_hid_stop
,
771 .open
= i2c_hid_open
,
772 .close
= i2c_hid_close
,
773 .power
= i2c_hid_power
,
774 .request
= i2c_hid_request
,
777 static int i2c_hid_init_irq(struct i2c_client
*client
)
779 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
782 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
784 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
785 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
788 dev_warn(&client
->dev
,
789 "Could not register for %s interrupt, irq = %d,"
791 client
->name
, client
->irq
, ret
);
799 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
801 struct i2c_client
*client
= ihid
->client
;
802 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
806 /* Fetch the length of HID description, retrieve the 4 first bytes:
807 * bytes 0-1 -> length
808 * bytes 2-3 -> bcdVersion (has to be 1.00) */
809 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
, 4);
811 i2c_hid_dbg(ihid
, "%s, ihid->hdesc_buffer: %4ph\n", __func__
,
815 dev_err(&client
->dev
,
816 "unable to fetch the size of HID descriptor (ret=%d)\n",
821 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
823 * the size of the HID descriptor should at least contain
824 * its size and the bcdVersion (4 bytes), and should not be greater
825 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
826 * through i2c_hid_command.
828 if (dsize
< 4 || dsize
> sizeof(struct i2c_hid_desc
)) {
829 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
834 /* check bcdVersion == 1.0 */
835 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
836 dev_err(&client
->dev
,
837 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
838 le16_to_cpu(hdesc
->bcdVersion
));
842 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
844 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
847 dev_err(&client
->dev
, "hid_descr_cmd Fail\n");
851 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
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 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
865 union acpi_object params
[4], *obj
;
866 struct acpi_object_list input
;
867 struct acpi_device
*adev
;
870 handle
= ACPI_HANDLE(&client
->dev
);
871 if (!handle
|| acpi_bus_get_device(handle
, &adev
))
874 input
.count
= ARRAY_SIZE(params
);
875 input
.pointer
= params
;
877 params
[0].type
= ACPI_TYPE_BUFFER
;
878 params
[0].buffer
.length
= sizeof(i2c_hid_guid
);
879 params
[0].buffer
.pointer
= i2c_hid_guid
;
880 params
[1].type
= ACPI_TYPE_INTEGER
;
881 params
[1].integer
.value
= 1;
882 params
[2].type
= ACPI_TYPE_INTEGER
;
883 params
[2].integer
.value
= 1; /* HID function */
884 params
[3].type
= ACPI_TYPE_PACKAGE
;
885 params
[3].package
.count
= 0;
886 params
[3].package
.elements
= NULL
;
888 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_DSM", &input
, &buf
))) {
889 dev_err(&client
->dev
, "device _DSM execution failed\n");
893 obj
= (union acpi_object
*)buf
.pointer
;
894 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
895 dev_err(&client
->dev
, "device _DSM returned invalid type: %d\n",
901 pdata
->hid_descriptor_address
= obj
->integer
.value
;
907 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
912 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
914 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
915 struct i2c_hid_platform_data
*pdata
)
922 static int i2c_hid_of_probe(struct i2c_client
*client
,
923 struct i2c_hid_platform_data
*pdata
)
925 struct device
*dev
= &client
->dev
;
929 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
931 dev_err(&client
->dev
, "HID register address not provided\n");
935 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
939 pdata
->hid_descriptor_address
= val
;
944 static const struct of_device_id i2c_hid_of_match
[] = {
945 { .compatible
= "hid-over-i2c" },
948 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
950 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
951 struct i2c_hid_platform_data
*pdata
)
957 static int i2c_hid_probe(struct i2c_client
*client
,
958 const struct i2c_device_id
*dev_id
)
961 struct i2c_hid
*ihid
;
962 struct hid_device
*hid
;
964 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
966 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
969 dev_err(&client
->dev
,
970 "HID over i2c has not been provided an Int IRQ\n");
974 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
978 if (client
->dev
.of_node
) {
979 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
982 } else if (!platform_data
) {
983 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
985 dev_err(&client
->dev
,
986 "HID register address not provided\n");
990 ihid
->pdata
= *platform_data
;
993 i2c_set_clientdata(client
, ihid
);
995 ihid
->client
= client
;
997 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
998 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
1000 init_waitqueue_head(&ihid
->wait
);
1002 /* we need to allocate the command buffer without knowing the maximum
1003 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1004 * real computation later. */
1005 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
1009 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
1013 ret
= i2c_hid_init_irq(client
);
1017 hid
= hid_allocate_device();
1025 hid
->driver_data
= client
;
1026 hid
->ll_driver
= &i2c_hid_ll_driver
;
1027 hid
->hid_get_raw_report
= i2c_hid_get_raw_report
;
1028 hid
->hid_output_raw_report
= i2c_hid_output_raw_report
;
1029 hid
->dev
.parent
= &client
->dev
;
1030 ACPI_HANDLE_SET(&hid
->dev
, ACPI_HANDLE(&client
->dev
));
1032 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1033 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1034 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1036 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1037 client
->name
, hid
->vendor
, hid
->product
);
1039 ret
= hid_add_device(hid
);
1042 hid_err(client
, "can't add hid device: %d\n", ret
);
1049 hid_destroy_device(hid
);
1052 free_irq(client
->irq
, ihid
);
1055 i2c_hid_free_buffers(ihid
);
1060 static int i2c_hid_remove(struct i2c_client
*client
)
1062 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1063 struct hid_device
*hid
;
1066 hid_destroy_device(hid
);
1068 free_irq(client
->irq
, ihid
);
1071 i2c_hid_free_buffers(ihid
);
1078 static void i2c_hid_shutdown(struct i2c_client
*client
)
1080 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1082 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1083 free_irq(client
->irq
, ihid
);
1086 #ifdef CONFIG_PM_SLEEP
1087 static int i2c_hid_suspend(struct device
*dev
)
1089 struct i2c_client
*client
= to_i2c_client(dev
);
1091 if (device_may_wakeup(&client
->dev
))
1092 enable_irq_wake(client
->irq
);
1094 /* Save some power */
1095 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1100 static int i2c_hid_resume(struct device
*dev
)
1103 struct i2c_client
*client
= to_i2c_client(dev
);
1105 ret
= i2c_hid_hwreset(client
);
1109 if (device_may_wakeup(&client
->dev
))
1110 disable_irq_wake(client
->irq
);
1116 static SIMPLE_DEV_PM_OPS(i2c_hid_pm
, i2c_hid_suspend
, i2c_hid_resume
);
1118 static const struct i2c_device_id i2c_hid_id_table
[] = {
1122 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1125 static struct i2c_driver i2c_hid_driver
= {
1128 .owner
= THIS_MODULE
,
1130 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1131 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1134 .probe
= i2c_hid_probe
,
1135 .remove
= i2c_hid_remove
,
1136 .shutdown
= i2c_hid_shutdown
,
1137 .id_table
= i2c_hid_id_table
,
1140 module_i2c_driver(i2c_hid_driver
);
1142 MODULE_DESCRIPTION("HID over I2C core driver");
1143 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1144 MODULE_LICENSE("GPL");