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>
41 #include <linux/i2c/i2c-hid.h>
44 #define I2C_HID_STARTED (1 << 0)
45 #define I2C_HID_RESET_PENDING (1 << 1)
46 #define I2C_HID_READ_PENDING (1 << 2)
48 #define I2C_HID_PWR_ON 0x00
49 #define I2C_HID_PWR_SLEEP 0x01
53 module_param(debug
, bool, 0444);
54 MODULE_PARM_DESC(debug
, "print a lot of debug information");
56 #define i2c_hid_dbg(ihid, fmt, arg...) \
59 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
63 __le16 wHIDDescLength
;
65 __le16 wReportDescLength
;
66 __le16 wReportDescRegister
;
67 __le16 wInputRegister
;
68 __le16 wMaxInputLength
;
69 __le16 wOutputRegister
;
70 __le16 wMaxOutputLength
;
71 __le16 wCommandRegister
;
80 unsigned int registerIndex
;
95 #define I2C_HID_CMD(opcode_) \
96 .opcode = opcode_, .length = 4, \
97 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
99 /* fetch HID descriptor */
100 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
101 /* fetch report descriptors */
102 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
103 .registerIndex
= offsetof(struct i2c_hid_desc
,
104 wReportDescRegister
),
108 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
110 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
111 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
112 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
113 static const struct i2c_hid_cmd hid_no_cmd
= { .length
= 0 };
116 * These definitions are not used here, but are defined by the spec.
117 * Keeping them here for documentation purposes.
119 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
120 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
121 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
122 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
125 static DEFINE_MUTEX(i2c_hid_open_mut
);
127 /* The main device structure */
129 struct i2c_client
*client
; /* i2c client */
130 struct hid_device
*hid
; /* pointer to corresponding HID dev */
132 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
133 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
135 __le16 wHIDDescRegister
; /* location of the i2c
136 * register of the HID
138 unsigned int bufsize
; /* i2c buffer size */
139 char *inbuf
; /* Input buffer */
140 char *rawbuf
; /* Raw Input buffer */
141 char *cmdbuf
; /* Command buffer */
142 char *argsbuf
; /* Command arguments buffer */
144 unsigned long flags
; /* device flags */
146 wait_queue_head_t wait
; /* For waiting the interrupt */
148 struct i2c_hid_platform_data pdata
;
151 static int __i2c_hid_command(struct i2c_client
*client
,
152 const struct i2c_hid_cmd
*command
, u8 reportID
,
153 u8 reportType
, u8
*args
, int args_len
,
154 unsigned char *buf_recv
, int data_len
)
156 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
157 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
159 struct i2c_msg msg
[2];
162 int length
= command
->length
;
163 bool wait
= command
->wait
;
164 unsigned int registerIndex
= command
->registerIndex
;
166 /* special case for hid_descr_cmd */
167 if (command
== &hid_descr_cmd
) {
168 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
170 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
171 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
175 cmd
->c
.opcode
= command
->opcode
;
176 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
179 memcpy(cmd
->data
+ length
, args
, args_len
);
182 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
184 msg
[0].addr
= client
->addr
;
185 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
187 msg
[0].buf
= cmd
->data
;
189 msg
[1].addr
= client
->addr
;
190 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
191 msg
[1].flags
|= I2C_M_RD
;
192 msg
[1].len
= data_len
;
193 msg
[1].buf
= buf_recv
;
195 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
199 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
201 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
204 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
207 return ret
< 0 ? ret
: -EIO
;
212 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
213 if (!wait_event_timeout(ihid
->wait
,
214 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
215 msecs_to_jiffies(5000)))
217 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
223 static int i2c_hid_command(struct i2c_client
*client
,
224 const struct i2c_hid_cmd
*command
,
225 unsigned char *buf_recv
, int data_len
)
227 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
231 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
232 u8 reportID
, unsigned char *buf_recv
, int data_len
)
234 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
238 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
240 i2c_hid_dbg(ihid
, "%s\n", __func__
);
242 if (reportID
>= 0x0F) {
243 args
[args_len
++] = reportID
;
247 args
[args_len
++] = readRegister
& 0xFF;
248 args
[args_len
++] = readRegister
>> 8;
250 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
251 reportType
, args
, args_len
, buf_recv
, data_len
);
253 dev_err(&client
->dev
,
254 "failed to retrieve report from device.\n");
262 * i2c_hid_set_or_send_report: forward an incoming report to the device
263 * @client: the i2c_client of the device
264 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
265 * @reportID: the report ID
266 * @buf: the actual data to transfer, without the report ID
268 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
270 static int i2c_hid_set_or_send_report(struct i2c_client
*client
, u8 reportType
,
271 u8 reportID
, unsigned char *buf
, size_t data_len
, bool use_data
)
273 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
274 u8
*args
= ihid
->argsbuf
;
275 const struct i2c_hid_cmd
*hidcmd
;
277 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
278 u16 outputRegister
= le16_to_cpu(ihid
->hdesc
.wOutputRegister
);
279 u16 maxOutputLength
= le16_to_cpu(ihid
->hdesc
.wMaxOutputLength
);
281 /* hid_hw_* already checked that data_len < HID_MAX_BUFFER_SIZE */
282 u16 size
= 2 /* size */ +
283 (reportID
? 1 : 0) /* reportID */ +
285 int args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
286 2 /* dataRegister */ +
290 i2c_hid_dbg(ihid
, "%s\n", __func__
);
292 if (!use_data
&& maxOutputLength
== 0)
295 if (reportID
>= 0x0F) {
296 args
[index
++] = reportID
;
301 * use the data register for feature reports or if the device does not
302 * support the output register
305 args
[index
++] = dataRegister
& 0xFF;
306 args
[index
++] = dataRegister
>> 8;
307 hidcmd
= &hid_set_report_cmd
;
309 args
[index
++] = outputRegister
& 0xFF;
310 args
[index
++] = outputRegister
>> 8;
311 hidcmd
= &hid_no_cmd
;
314 args
[index
++] = size
& 0xFF;
315 args
[index
++] = size
>> 8;
318 args
[index
++] = reportID
;
320 memcpy(&args
[index
], buf
, data_len
);
322 ret
= __i2c_hid_command(client
, hidcmd
, reportID
,
323 reportType
, args
, args_len
, NULL
, 0);
325 dev_err(&client
->dev
, "failed to set a report to device.\n");
332 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
334 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
337 i2c_hid_dbg(ihid
, "%s\n", __func__
);
339 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
340 0, NULL
, 0, NULL
, 0);
342 dev_err(&client
->dev
, "failed to change power setting.\n");
347 static int i2c_hid_hwreset(struct i2c_client
*client
)
349 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
352 i2c_hid_dbg(ihid
, "%s\n", __func__
);
354 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
358 i2c_hid_dbg(ihid
, "resetting...\n");
360 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
362 dev_err(&client
->dev
, "failed to reset device.\n");
363 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
370 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
373 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
375 if (size
> ihid
->bufsize
)
376 size
= ihid
->bufsize
;
378 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
383 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
384 __func__
, ret
, size
);
388 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
391 /* host or device initiated RESET completed */
392 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
393 wake_up(&ihid
->wait
);
397 if (ret_size
> size
) {
398 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
399 __func__
, size
, ret_size
);
403 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
405 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
406 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
412 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
414 struct i2c_hid
*ihid
= dev_id
;
416 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
419 i2c_hid_get_input(ihid
);
424 static int i2c_hid_get_report_length(struct hid_report
*report
)
426 return ((report
->size
- 1) >> 3) + 1 +
427 report
->device
->report_enum
[report
->type
].numbered
+ 2;
430 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
433 struct hid_device
*hid
= report
->device
;
434 struct i2c_client
*client
= hid
->driver_data
;
435 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
436 unsigned int size
, ret_size
;
438 size
= i2c_hid_get_report_length(report
);
439 if (i2c_hid_get_report(client
,
440 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
441 report
->id
, buffer
, size
))
444 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, buffer
);
446 ret_size
= buffer
[0] | (buffer
[1] << 8);
448 if (ret_size
!= size
) {
449 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
450 __func__
, size
, ret_size
);
454 /* hid->driver_lock is held as we are in probe function,
455 * we just need to setup the input fields, so using
456 * hid_report_raw_event is safe. */
457 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
461 * Initialize all reports
463 static void i2c_hid_init_reports(struct hid_device
*hid
)
465 struct hid_report
*report
;
466 struct i2c_client
*client
= hid
->driver_data
;
467 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
468 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
471 dev_err(&client
->dev
, "can not retrieve initial reports\n");
476 * The device must be powered on while we fetch initial reports
479 pm_runtime_get_sync(&client
->dev
);
481 list_for_each_entry(report
,
482 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
483 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
485 pm_runtime_put(&client
->dev
);
491 * Traverse the supplied list of reports and find the longest
493 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
496 struct hid_report
*report
;
499 /* We should not rely on wMaxInputLength, as some devices may set it to
501 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
502 size
= i2c_hid_get_report_length(report
);
508 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
512 kfree(ihid
->argsbuf
);
517 ihid
->argsbuf
= NULL
;
521 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
523 /* the worst case is computed from the set_report command with a
524 * reportID > 15 and the maximum report length */
525 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
526 sizeof(__u16
) + /* data register */
527 sizeof(__u16
) + /* size of the report */
528 report_size
; /* report */
530 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
531 ihid
->rawbuf
= kzalloc(report_size
, GFP_KERNEL
);
532 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
533 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
535 if (!ihid
->inbuf
|| !ihid
->rawbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
536 i2c_hid_free_buffers(ihid
);
540 ihid
->bufsize
= report_size
;
545 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
546 unsigned char report_number
, __u8
*buf
, size_t count
,
547 unsigned char report_type
)
549 struct i2c_client
*client
= hid
->driver_data
;
550 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
551 size_t ret_count
, ask_count
;
554 if (report_type
== HID_OUTPUT_REPORT
)
557 /* +2 bytes to include the size of the reply in the query buffer */
558 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
560 ret
= i2c_hid_get_report(client
,
561 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
562 report_number
, ihid
->rawbuf
, ask_count
);
567 ret_count
= ihid
->rawbuf
[0] | (ihid
->rawbuf
[1] << 8);
572 ret_count
= min(ret_count
, ask_count
);
574 /* The query buffer contains the size, dropping it in the reply */
575 count
= min(count
, ret_count
- 2);
576 memcpy(buf
, ihid
->rawbuf
+ 2, count
);
581 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
582 size_t count
, unsigned char report_type
, bool use_data
)
584 struct i2c_client
*client
= hid
->driver_data
;
585 int report_id
= buf
[0];
588 if (report_type
== HID_INPUT_REPORT
)
596 ret
= i2c_hid_set_or_send_report(client
,
597 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
598 report_id
, buf
, count
, use_data
);
600 if (report_id
&& ret
>= 0)
601 ret
++; /* add report_id to the number of transfered bytes */
606 static int i2c_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
609 return i2c_hid_output_raw_report(hid
, buf
, count
, HID_OUTPUT_REPORT
,
613 static int i2c_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
614 __u8
*buf
, size_t len
, unsigned char rtype
,
618 case HID_REQ_GET_REPORT
:
619 return i2c_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
620 case HID_REQ_SET_REPORT
:
621 if (buf
[0] != reportnum
)
623 return i2c_hid_output_raw_report(hid
, buf
, len
, rtype
, true);
629 static int i2c_hid_parse(struct hid_device
*hid
)
631 struct i2c_client
*client
= hid
->driver_data
;
632 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
633 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
639 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
641 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
642 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
643 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
648 ret
= i2c_hid_hwreset(client
);
651 } while (tries
-- > 0 && ret
);
656 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
659 dbg_hid("couldn't allocate rdesc memory\n");
663 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
665 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
667 hid_err(hid
, "reading report descriptor failed\n");
672 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
674 ret
= hid_parse_report(hid
, rdesc
, rsize
);
677 dbg_hid("parsing report descriptor failed\n");
684 static int i2c_hid_start(struct hid_device
*hid
)
686 struct i2c_client
*client
= hid
->driver_data
;
687 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
689 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
691 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
692 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
693 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
695 if (bufsize
> ihid
->bufsize
) {
696 i2c_hid_free_buffers(ihid
);
698 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
704 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
705 i2c_hid_init_reports(hid
);
710 static void i2c_hid_stop(struct hid_device
*hid
)
715 static int i2c_hid_open(struct hid_device
*hid
)
717 struct i2c_client
*client
= hid
->driver_data
;
718 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
721 mutex_lock(&i2c_hid_open_mut
);
723 ret
= pm_runtime_get_sync(&client
->dev
);
728 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
731 mutex_unlock(&i2c_hid_open_mut
);
732 return ret
< 0 ? ret
: 0;
735 static void i2c_hid_close(struct hid_device
*hid
)
737 struct i2c_client
*client
= hid
->driver_data
;
738 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
740 /* protecting hid->open to make sure we don't restart
741 * data acquistion due to a resumption we no longer
744 mutex_lock(&i2c_hid_open_mut
);
746 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
748 /* Save some power */
749 pm_runtime_put(&client
->dev
);
751 mutex_unlock(&i2c_hid_open_mut
);
754 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
756 struct i2c_client
*client
= hid
->driver_data
;
757 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
759 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
763 pm_runtime_get_sync(&client
->dev
);
766 pm_runtime_put(&client
->dev
);
772 static struct hid_ll_driver i2c_hid_ll_driver
= {
773 .parse
= i2c_hid_parse
,
774 .start
= i2c_hid_start
,
775 .stop
= i2c_hid_stop
,
776 .open
= i2c_hid_open
,
777 .close
= i2c_hid_close
,
778 .power
= i2c_hid_power
,
779 .output_report
= i2c_hid_output_report
,
780 .raw_request
= i2c_hid_raw_request
,
783 static int i2c_hid_init_irq(struct i2c_client
*client
)
785 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
788 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
790 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
791 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
794 dev_warn(&client
->dev
,
795 "Could not register for %s interrupt, irq = %d,"
797 client
->name
, client
->irq
, ret
);
805 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
807 struct i2c_client
*client
= ihid
->client
;
808 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
812 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
813 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
814 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
815 sizeof(struct i2c_hid_desc
));
817 dev_err(&client
->dev
, "hid_descr_cmd failed\n");
821 /* Validate the length of HID descriptor, the 4 first bytes:
822 * bytes 0-1 -> length
823 * bytes 2-3 -> bcdVersion (has to be 1.00) */
824 /* check bcdVersion == 1.0 */
825 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
826 dev_err(&client
->dev
,
827 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
828 le16_to_cpu(hdesc
->bcdVersion
));
832 /* Descriptor length should be 30 bytes as per the specification */
833 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
834 if (dsize
!= sizeof(struct i2c_hid_desc
)) {
835 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
839 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
844 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
845 struct i2c_hid_platform_data
*pdata
)
847 static u8 i2c_hid_guid
[] = {
848 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
849 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
851 union acpi_object
*obj
;
852 struct acpi_device
*adev
;
855 handle
= ACPI_HANDLE(&client
->dev
);
856 if (!handle
|| acpi_bus_get_device(handle
, &adev
))
859 obj
= acpi_evaluate_dsm_typed(handle
, i2c_hid_guid
, 1, 1, NULL
,
862 dev_err(&client
->dev
, "device _DSM execution failed\n");
866 pdata
->hid_descriptor_address
= obj
->integer
.value
;
872 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
877 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
879 static inline int i2c_hid_acpi_pdata(struct i2c_client
*client
,
880 struct i2c_hid_platform_data
*pdata
)
887 static int i2c_hid_of_probe(struct i2c_client
*client
,
888 struct i2c_hid_platform_data
*pdata
)
890 struct device
*dev
= &client
->dev
;
894 ret
= of_property_read_u32(dev
->of_node
, "hid-descr-addr", &val
);
896 dev_err(&client
->dev
, "HID register address not provided\n");
900 dev_err(&client
->dev
, "Bad HID register address: 0x%08x\n",
904 pdata
->hid_descriptor_address
= val
;
909 static const struct of_device_id i2c_hid_of_match
[] = {
910 { .compatible
= "hid-over-i2c" },
913 MODULE_DEVICE_TABLE(of
, i2c_hid_of_match
);
915 static inline int i2c_hid_of_probe(struct i2c_client
*client
,
916 struct i2c_hid_platform_data
*pdata
)
922 static int i2c_hid_probe(struct i2c_client
*client
,
923 const struct i2c_device_id
*dev_id
)
926 struct i2c_hid
*ihid
;
927 struct hid_device
*hid
;
929 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
931 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
934 dev_err(&client
->dev
,
935 "HID over i2c has not been provided an Int IRQ\n");
939 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
943 if (client
->dev
.of_node
) {
944 ret
= i2c_hid_of_probe(client
, &ihid
->pdata
);
947 } else if (!platform_data
) {
948 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
950 dev_err(&client
->dev
,
951 "HID register address not provided\n");
955 ihid
->pdata
= *platform_data
;
958 i2c_set_clientdata(client
, ihid
);
960 ihid
->client
= client
;
962 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
963 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
965 init_waitqueue_head(&ihid
->wait
);
967 /* we need to allocate the command buffer without knowing the maximum
968 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
969 * real computation later. */
970 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
974 pm_runtime_get_noresume(&client
->dev
);
975 pm_runtime_set_active(&client
->dev
);
976 pm_runtime_enable(&client
->dev
);
978 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
982 ret
= i2c_hid_init_irq(client
);
986 hid
= hid_allocate_device();
994 hid
->driver_data
= client
;
995 hid
->ll_driver
= &i2c_hid_ll_driver
;
996 hid
->dev
.parent
= &client
->dev
;
997 ACPI_COMPANION_SET(&hid
->dev
, ACPI_COMPANION(&client
->dev
));
999 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
1000 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
1001 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
1003 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
1004 client
->name
, hid
->vendor
, hid
->product
);
1006 ret
= hid_add_device(hid
);
1009 hid_err(client
, "can't add hid device: %d\n", ret
);
1013 pm_runtime_put(&client
->dev
);
1017 hid_destroy_device(hid
);
1020 free_irq(client
->irq
, ihid
);
1023 pm_runtime_put_noidle(&client
->dev
);
1024 pm_runtime_disable(&client
->dev
);
1027 i2c_hid_free_buffers(ihid
);
1032 static int i2c_hid_remove(struct i2c_client
*client
)
1034 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1035 struct hid_device
*hid
;
1037 pm_runtime_get_sync(&client
->dev
);
1038 pm_runtime_disable(&client
->dev
);
1039 pm_runtime_set_suspended(&client
->dev
);
1040 pm_runtime_put_noidle(&client
->dev
);
1043 hid_destroy_device(hid
);
1045 free_irq(client
->irq
, ihid
);
1048 i2c_hid_free_buffers(ihid
);
1055 #ifdef CONFIG_PM_SLEEP
1056 static int i2c_hid_suspend(struct device
*dev
)
1058 struct i2c_client
*client
= to_i2c_client(dev
);
1059 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1060 struct hid_device
*hid
= ihid
->hid
;
1063 disable_irq(client
->irq
);
1064 if (device_may_wakeup(&client
->dev
))
1065 enable_irq_wake(client
->irq
);
1067 if (hid
->driver
&& hid
->driver
->suspend
)
1068 ret
= hid
->driver
->suspend(hid
, PMSG_SUSPEND
);
1070 /* Save some power */
1071 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1076 static int i2c_hid_resume(struct device
*dev
)
1079 struct i2c_client
*client
= to_i2c_client(dev
);
1080 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1081 struct hid_device
*hid
= ihid
->hid
;
1083 enable_irq(client
->irq
);
1084 ret
= i2c_hid_hwreset(client
);
1088 if (device_may_wakeup(&client
->dev
))
1089 disable_irq_wake(client
->irq
);
1091 if (hid
->driver
&& hid
->driver
->reset_resume
) {
1092 ret
= hid
->driver
->reset_resume(hid
);
1101 static int i2c_hid_runtime_suspend(struct device
*dev
)
1103 struct i2c_client
*client
= to_i2c_client(dev
);
1105 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1106 disable_irq(client
->irq
);
1110 static int i2c_hid_runtime_resume(struct device
*dev
)
1112 struct i2c_client
*client
= to_i2c_client(dev
);
1114 enable_irq(client
->irq
);
1115 i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
1120 static const struct dev_pm_ops i2c_hid_pm
= {
1121 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend
, i2c_hid_resume
)
1122 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend
, i2c_hid_runtime_resume
,
1126 static const struct i2c_device_id i2c_hid_id_table
[] = {
1130 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1133 static struct i2c_driver i2c_hid_driver
= {
1136 .owner
= THIS_MODULE
,
1138 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1139 .of_match_table
= of_match_ptr(i2c_hid_of_match
),
1142 .probe
= i2c_hid_probe
,
1143 .remove
= i2c_hid_remove
,
1145 .id_table
= i2c_hid_id_table
,
1148 module_i2c_driver(i2c_hid_driver
);
1150 MODULE_DESCRIPTION("HID over I2C core driver");
1151 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1152 MODULE_LICENSE("GPL");