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>
39 #include <linux/i2c/i2c-hid.h>
42 #define I2C_HID_STARTED (1 << 0)
43 #define I2C_HID_RESET_PENDING (1 << 1)
44 #define I2C_HID_READ_PENDING (1 << 2)
46 #define I2C_HID_PWR_ON 0x00
47 #define I2C_HID_PWR_SLEEP 0x01
51 module_param(debug
, bool, 0444);
52 MODULE_PARM_DESC(debug
, "print a lot of debug information");
54 #define i2c_hid_dbg(ihid, fmt, arg...) \
57 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
61 __le16 wHIDDescLength
;
63 __le16 wReportDescLength
;
64 __le16 wReportDescRegister
;
65 __le16 wInputRegister
;
66 __le16 wMaxInputLength
;
67 __le16 wOutputRegister
;
68 __le16 wMaxOutputLength
;
69 __le16 wCommandRegister
;
78 unsigned int registerIndex
;
93 #define I2C_HID_CMD(opcode_) \
94 .opcode = opcode_, .length = 4, \
95 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
97 /* fetch HID descriptor */
98 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
99 /* fetch report descriptors */
100 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
101 .registerIndex
= offsetof(struct i2c_hid_desc
,
102 wReportDescRegister
),
106 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
108 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
109 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
110 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
113 * These definitions are not used here, but are defined by the spec.
114 * Keeping them here for documentation purposes.
116 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
117 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
118 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
119 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
122 static DEFINE_MUTEX(i2c_hid_open_mut
);
124 /* The main device structure */
126 struct i2c_client
*client
; /* i2c client */
127 struct hid_device
*hid
; /* pointer to corresponding HID dev */
129 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
130 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
132 __le16 wHIDDescRegister
; /* location of the i2c
133 * register of the HID
135 unsigned int bufsize
; /* i2c buffer size */
136 char *inbuf
; /* Input buffer */
137 char *cmdbuf
; /* Command buffer */
138 char *argsbuf
; /* Command arguments buffer */
140 unsigned long flags
; /* device flags */
142 wait_queue_head_t wait
; /* For waiting the interrupt */
144 struct i2c_hid_platform_data pdata
;
147 static int __i2c_hid_command(struct i2c_client
*client
,
148 const struct i2c_hid_cmd
*command
, u8 reportID
,
149 u8 reportType
, u8
*args
, int args_len
,
150 unsigned char *buf_recv
, int data_len
)
152 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
153 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
155 struct i2c_msg msg
[2];
158 int length
= command
->length
;
159 bool wait
= command
->wait
;
160 unsigned int registerIndex
= command
->registerIndex
;
162 /* special case for hid_descr_cmd */
163 if (command
== &hid_descr_cmd
) {
164 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
166 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
167 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
171 cmd
->c
.opcode
= command
->opcode
;
172 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
175 memcpy(cmd
->data
+ length
, args
, args_len
);
178 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
180 msg
[0].addr
= client
->addr
;
181 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
183 msg
[0].buf
= cmd
->data
;
185 msg
[1].addr
= client
->addr
;
186 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
187 msg
[1].flags
|= I2C_M_RD
;
188 msg
[1].len
= data_len
;
189 msg
[1].buf
= buf_recv
;
191 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
195 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
197 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
200 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
203 return ret
< 0 ? ret
: -EIO
;
208 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
209 if (!wait_event_timeout(ihid
->wait
,
210 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
211 msecs_to_jiffies(5000)))
213 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
219 static int i2c_hid_command(struct i2c_client
*client
,
220 const struct i2c_hid_cmd
*command
,
221 unsigned char *buf_recv
, int data_len
)
223 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
227 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
228 u8 reportID
, unsigned char *buf_recv
, int data_len
)
230 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
234 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
236 i2c_hid_dbg(ihid
, "%s\n", __func__
);
238 if (reportID
>= 0x0F) {
239 args
[args_len
++] = reportID
;
243 args
[args_len
++] = readRegister
& 0xFF;
244 args
[args_len
++] = readRegister
>> 8;
246 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
247 reportType
, args
, args_len
, buf_recv
, data_len
);
249 dev_err(&client
->dev
,
250 "failed to retrieve report from device.\n");
257 static int i2c_hid_set_report(struct i2c_client
*client
, u8 reportType
,
258 u8 reportID
, unsigned char *buf
, size_t data_len
)
260 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
261 u8
*args
= ihid
->argsbuf
;
263 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
265 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
266 u16 size
= 2 /* size */ +
267 (reportID
? 1 : 0) /* reportID */ +
269 int args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
270 2 /* dataRegister */ +
274 i2c_hid_dbg(ihid
, "%s\n", __func__
);
276 if (reportID
>= 0x0F) {
277 args
[index
++] = reportID
;
281 args
[index
++] = dataRegister
& 0xFF;
282 args
[index
++] = dataRegister
>> 8;
284 args
[index
++] = size
& 0xFF;
285 args
[index
++] = size
>> 8;
288 args
[index
++] = reportID
;
290 memcpy(&args
[index
], buf
, data_len
);
292 ret
= __i2c_hid_command(client
, &hid_set_report_cmd
, reportID
,
293 reportType
, args
, args_len
, NULL
, 0);
295 dev_err(&client
->dev
, "failed to set a report to device.\n");
302 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
304 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
307 i2c_hid_dbg(ihid
, "%s\n", __func__
);
309 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
310 0, NULL
, 0, NULL
, 0);
312 dev_err(&client
->dev
, "failed to change power setting.\n");
317 static int i2c_hid_hwreset(struct i2c_client
*client
)
319 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
322 i2c_hid_dbg(ihid
, "%s\n", __func__
);
324 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
328 i2c_hid_dbg(ihid
, "resetting...\n");
330 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
332 dev_err(&client
->dev
, "failed to reset device.\n");
333 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
340 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
343 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
345 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
350 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
351 __func__
, ret
, size
);
355 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
358 /* host or device initiated RESET completed */
359 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
360 wake_up(&ihid
->wait
);
364 if (ret_size
> size
) {
365 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
366 __func__
, size
, ret_size
);
370 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
372 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
373 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
379 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
381 struct i2c_hid
*ihid
= dev_id
;
383 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
386 i2c_hid_get_input(ihid
);
391 static int i2c_hid_get_report_length(struct hid_report
*report
)
393 return ((report
->size
- 1) >> 3) + 1 +
394 report
->device
->report_enum
[report
->type
].numbered
+ 2;
397 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
400 struct hid_device
*hid
= report
->device
;
401 struct i2c_client
*client
= hid
->driver_data
;
402 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
403 unsigned int size
, ret_size
;
405 size
= i2c_hid_get_report_length(report
);
406 if (i2c_hid_get_report(client
,
407 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
408 report
->id
, buffer
, size
))
411 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, ihid
->inbuf
);
413 ret_size
= buffer
[0] | (buffer
[1] << 8);
415 if (ret_size
!= size
) {
416 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
417 __func__
, size
, ret_size
);
421 /* hid->driver_lock is held as we are in probe function,
422 * we just need to setup the input fields, so using
423 * hid_report_raw_event is safe. */
424 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
428 * Initialize all reports
430 static void i2c_hid_init_reports(struct hid_device
*hid
)
432 struct hid_report
*report
;
433 struct i2c_client
*client
= hid
->driver_data
;
434 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
435 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
438 dev_err(&client
->dev
, "can not retrieve initial reports\n");
442 list_for_each_entry(report
,
443 &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
444 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
446 list_for_each_entry(report
,
447 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
448 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
454 * Traverse the supplied list of reports and find the longest
456 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
459 struct hid_report
*report
;
462 /* We should not rely on wMaxInputLength, as some devices may set it to
464 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
465 size
= i2c_hid_get_report_length(report
);
471 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
474 kfree(ihid
->argsbuf
);
478 ihid
->argsbuf
= NULL
;
482 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
484 /* the worst case is computed from the set_report command with a
485 * reportID > 15 and the maximum report length */
486 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
487 sizeof(__u16
) + /* data register */
488 sizeof(__u16
) + /* size of the report */
489 report_size
; /* report */
491 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
492 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
493 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
495 if (!ihid
->inbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
496 i2c_hid_free_buffers(ihid
);
500 ihid
->bufsize
= report_size
;
505 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
506 unsigned char report_number
, __u8
*buf
, size_t count
,
507 unsigned char report_type
)
509 struct i2c_client
*client
= hid
->driver_data
;
510 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
511 size_t ret_count
, ask_count
;
514 if (report_type
== HID_OUTPUT_REPORT
)
517 /* +2 bytes to include the size of the reply in the query buffer */
518 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
520 ret
= i2c_hid_get_report(client
,
521 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
522 report_number
, ihid
->inbuf
, ask_count
);
527 ret_count
= ihid
->inbuf
[0] | (ihid
->inbuf
[1] << 8);
532 ret_count
= min(ret_count
, ask_count
);
534 /* The query buffer contains the size, dropping it in the reply */
535 count
= min(count
, ret_count
- 2);
536 memcpy(buf
, ihid
->inbuf
+ 2, count
);
541 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
542 size_t count
, unsigned char report_type
)
544 struct i2c_client
*client
= hid
->driver_data
;
545 int report_id
= buf
[0];
548 if (report_type
== HID_INPUT_REPORT
)
556 ret
= i2c_hid_set_report(client
,
557 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
558 report_id
, buf
, count
);
560 if (report_id
&& ret
>= 0)
561 ret
++; /* add report_id to the number of transfered bytes */
566 static void i2c_hid_request(struct hid_device
*hid
, struct hid_report
*rep
,
569 struct i2c_client
*client
= hid
->driver_data
;
572 int len
= i2c_hid_get_report_length(rep
) - 2;
574 buf
= kzalloc(len
, GFP_KERNEL
);
579 case HID_REQ_GET_REPORT
:
580 ret
= i2c_hid_get_raw_report(hid
, rep
->id
, buf
, len
, rep
->type
);
582 dev_err(&client
->dev
, "%s: unable to get report: %d\n",
585 hid_input_report(hid
, rep
->type
, buf
, ret
, 0);
587 case HID_REQ_SET_REPORT
:
588 hid_output_report(rep
, buf
);
589 i2c_hid_output_raw_report(hid
, buf
, len
, rep
->type
);
596 static int i2c_hid_parse(struct hid_device
*hid
)
598 struct i2c_client
*client
= hid
->driver_data
;
599 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
600 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
606 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
608 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
609 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
610 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
615 ret
= i2c_hid_hwreset(client
);
618 } while (tries
-- > 0 && ret
);
623 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
626 dbg_hid("couldn't allocate rdesc memory\n");
630 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
632 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
634 hid_err(hid
, "reading report descriptor failed\n");
639 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
641 ret
= hid_parse_report(hid
, rdesc
, rsize
);
644 dbg_hid("parsing report descriptor failed\n");
651 static int i2c_hid_start(struct hid_device
*hid
)
653 struct i2c_client
*client
= hid
->driver_data
;
654 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
656 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
658 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
659 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
660 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
662 if (bufsize
> ihid
->bufsize
) {
663 i2c_hid_free_buffers(ihid
);
665 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
671 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
672 i2c_hid_init_reports(hid
);
677 static void i2c_hid_stop(struct hid_device
*hid
)
679 struct i2c_client
*client
= hid
->driver_data
;
680 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
684 i2c_hid_free_buffers(ihid
);
687 static int i2c_hid_open(struct hid_device
*hid
)
689 struct i2c_client
*client
= hid
->driver_data
;
690 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
693 mutex_lock(&i2c_hid_open_mut
);
695 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
700 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
703 mutex_unlock(&i2c_hid_open_mut
);
707 static void i2c_hid_close(struct hid_device
*hid
)
709 struct i2c_client
*client
= hid
->driver_data
;
710 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
712 /* protecting hid->open to make sure we don't restart
713 * data acquistion due to a resumption we no longer
716 mutex_lock(&i2c_hid_open_mut
);
718 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
720 /* Save some power */
721 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
723 mutex_unlock(&i2c_hid_open_mut
);
726 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
728 struct i2c_client
*client
= hid
->driver_data
;
729 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
732 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
736 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
739 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
745 static int i2c_hid_hidinput_input_event(struct input_dev
*dev
,
746 unsigned int type
, unsigned int code
, int value
)
748 struct hid_device
*hid
= input_get_drvdata(dev
);
749 struct hid_field
*field
;
753 return input_ff_event(dev
, type
, code
, value
);
758 offset
= hidinput_find_field(hid
, type
, code
, &field
);
761 hid_warn(dev
, "event field not found\n");
765 return hid_set_field(field
, offset
, value
);
768 static struct hid_ll_driver i2c_hid_ll_driver
= {
769 .parse
= i2c_hid_parse
,
770 .start
= i2c_hid_start
,
771 .stop
= i2c_hid_stop
,
772 .open
= i2c_hid_open
,
773 .close
= i2c_hid_close
,
774 .power
= i2c_hid_power
,
775 .request
= i2c_hid_request
,
776 .hidinput_input_event
= i2c_hid_hidinput_input_event
,
779 static int i2c_hid_init_irq(struct i2c_client
*client
)
781 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
784 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
786 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
787 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
790 dev_warn(&client
->dev
,
791 "Could not register for %s interrupt, irq = %d,"
793 client
->name
, client
->irq
, ret
);
801 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
803 struct i2c_client
*client
= ihid
->client
;
804 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
808 /* Fetch the length of HID description, retrieve the 4 first bytes:
809 * bytes 0-1 -> length
810 * bytes 2-3 -> bcdVersion (has to be 1.00) */
811 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
, 4);
813 i2c_hid_dbg(ihid
, "%s, ihid->hdesc_buffer: %*ph\n",
814 __func__
, 4, ihid
->hdesc_buffer
);
817 dev_err(&client
->dev
,
818 "unable to fetch the size of HID descriptor (ret=%d)\n",
823 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
825 * the size of the HID descriptor should at least contain
826 * its size and the bcdVersion (4 bytes), and should not be greater
827 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
828 * through i2c_hid_command.
830 if (dsize
< 4 || dsize
> sizeof(struct i2c_hid_desc
)) {
831 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
836 /* check bcdVersion == 1.0 */
837 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
838 dev_err(&client
->dev
,
839 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
840 le16_to_cpu(hdesc
->bcdVersion
));
844 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
846 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
849 dev_err(&client
->dev
, "hid_descr_cmd Fail\n");
853 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
859 static int i2c_hid_acpi_pdata(struct i2c_client
*client
,
860 struct i2c_hid_platform_data
*pdata
)
862 static u8 i2c_hid_guid
[] = {
863 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
864 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
866 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
867 union acpi_object params
[4], *obj
;
868 struct acpi_object_list input
;
869 struct acpi_device
*adev
;
872 handle
= ACPI_HANDLE(&client
->dev
);
873 if (!handle
|| acpi_bus_get_device(handle
, &adev
))
876 input
.count
= ARRAY_SIZE(params
);
877 input
.pointer
= params
;
879 params
[0].type
= ACPI_TYPE_BUFFER
;
880 params
[0].buffer
.length
= sizeof(i2c_hid_guid
);
881 params
[0].buffer
.pointer
= i2c_hid_guid
;
882 params
[1].type
= ACPI_TYPE_INTEGER
;
883 params
[1].integer
.value
= 1;
884 params
[2].type
= ACPI_TYPE_INTEGER
;
885 params
[2].integer
.value
= 1; /* HID function */
886 params
[3].type
= ACPI_TYPE_INTEGER
;
887 params
[3].integer
.value
= 0;
889 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_DSM", &input
, &buf
))) {
890 dev_err(&client
->dev
, "device _DSM execution failed\n");
894 obj
= (union acpi_object
*)buf
.pointer
;
895 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
896 dev_err(&client
->dev
, "device _DSM returned invalid type: %d\n",
902 pdata
->hid_descriptor_address
= obj
->integer
.value
;
908 static const struct acpi_device_id i2c_hid_acpi_match
[] = {
913 MODULE_DEVICE_TABLE(acpi
, i2c_hid_acpi_match
);
915 static inline int i2c_hid_acpi_pdata(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 (!platform_data
) {
944 ret
= i2c_hid_acpi_pdata(client
, &ihid
->pdata
);
946 dev_err(&client
->dev
,
947 "HID register address not provided\n");
951 ihid
->pdata
= *platform_data
;
954 i2c_set_clientdata(client
, ihid
);
956 ihid
->client
= client
;
958 hidRegister
= ihid
->pdata
.hid_descriptor_address
;
959 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
961 init_waitqueue_head(&ihid
->wait
);
963 /* we need to allocate the command buffer without knowing the maximum
964 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
965 * real computation later. */
966 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
970 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
974 ret
= i2c_hid_init_irq(client
);
978 hid
= hid_allocate_device();
986 hid
->driver_data
= client
;
987 hid
->ll_driver
= &i2c_hid_ll_driver
;
988 hid
->hid_get_raw_report
= i2c_hid_get_raw_report
;
989 hid
->hid_output_raw_report
= i2c_hid_output_raw_report
;
990 hid
->dev
.parent
= &client
->dev
;
991 ACPI_HANDLE_SET(&hid
->dev
, ACPI_HANDLE(&client
->dev
));
993 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
994 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
995 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
997 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
998 client
->name
, hid
->vendor
, hid
->product
);
1000 ret
= hid_add_device(hid
);
1003 hid_err(client
, "can't add hid device: %d\n", ret
);
1010 hid_destroy_device(hid
);
1013 free_irq(client
->irq
, ihid
);
1016 i2c_hid_free_buffers(ihid
);
1021 static int i2c_hid_remove(struct i2c_client
*client
)
1023 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
1024 struct hid_device
*hid
;
1027 hid_destroy_device(hid
);
1029 free_irq(client
->irq
, ihid
);
1032 i2c_hid_free_buffers(ihid
);
1039 #ifdef CONFIG_PM_SLEEP
1040 static int i2c_hid_suspend(struct device
*dev
)
1042 struct i2c_client
*client
= to_i2c_client(dev
);
1044 if (device_may_wakeup(&client
->dev
))
1045 enable_irq_wake(client
->irq
);
1047 /* Save some power */
1048 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
1053 static int i2c_hid_resume(struct device
*dev
)
1056 struct i2c_client
*client
= to_i2c_client(dev
);
1058 ret
= i2c_hid_hwreset(client
);
1062 if (device_may_wakeup(&client
->dev
))
1063 disable_irq_wake(client
->irq
);
1069 static SIMPLE_DEV_PM_OPS(i2c_hid_pm
, i2c_hid_suspend
, i2c_hid_resume
);
1071 static const struct i2c_device_id i2c_hid_id_table
[] = {
1075 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
1078 static struct i2c_driver i2c_hid_driver
= {
1081 .owner
= THIS_MODULE
,
1083 .acpi_match_table
= ACPI_PTR(i2c_hid_acpi_match
),
1086 .probe
= i2c_hid_probe
,
1087 .remove
= i2c_hid_remove
,
1089 .id_table
= i2c_hid_id_table
,
1092 module_i2c_driver(i2c_hid_driver
);
1094 MODULE_DESCRIPTION("HID over I2C core driver");
1095 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1096 MODULE_LICENSE("GPL");