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/bug.h>
36 #include <linux/hid.h>
38 #include <linux/i2c/i2c-hid.h>
41 #define I2C_HID_STARTED (1 << 0)
42 #define I2C_HID_RESET_PENDING (1 << 1)
43 #define I2C_HID_READ_PENDING (1 << 2)
45 #define I2C_HID_PWR_ON 0x00
46 #define I2C_HID_PWR_SLEEP 0x01
49 static bool debug
= false;
50 module_param(debug
, bool, 0444);
51 MODULE_PARM_DESC(debug
, "print a lot of debug information");
53 #define i2c_hid_dbg(ihid, fmt, arg...) \
55 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg)
58 __le16 wHIDDescLength
;
60 __le16 wReportDescLength
;
61 __le16 wReportDescRegister
;
62 __le16 wInputRegister
;
63 __le16 wMaxInputLength
;
64 __le16 wOutputRegister
;
65 __le16 wMaxOutputLength
;
66 __le16 wCommandRegister
;
74 unsigned int registerIndex
;
89 #define I2C_HID_CMD(opcode_) \
90 .opcode = opcode_, .length = 4, \
91 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
93 /* fetch HID descriptor */
94 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
95 /* fetch report descriptors */
96 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
97 .registerIndex
= offsetof(struct i2c_hid_desc
,
102 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
104 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
105 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
106 static const struct i2c_hid_cmd hid_get_idle_cmd
= { I2C_HID_CMD(0x04) };
107 static const struct i2c_hid_cmd hid_set_idle_cmd
= { I2C_HID_CMD(0x05) };
108 static const struct i2c_hid_cmd hid_get_protocol_cmd
= { I2C_HID_CMD(0x06) };
109 static const struct i2c_hid_cmd hid_set_protocol_cmd
= { I2C_HID_CMD(0x07) };
110 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
111 /* read/write data register */
112 static const struct i2c_hid_cmd hid_data_cmd
= {
113 .registerIndex
= offsetof(struct i2c_hid_desc
, wDataRegister
),
116 /* write output reports */
117 static const struct i2c_hid_cmd hid_out_cmd
= {
118 .registerIndex
= offsetof(struct i2c_hid_desc
,
123 /* The main device structure */
125 struct i2c_client
*client
; /* i2c client */
126 struct hid_device
*hid
; /* pointer to corresponding HID dev */
128 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
129 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
131 __le16 wHIDDescRegister
; /* location of the i2c
132 * register of the HID
134 unsigned int bufsize
; /* i2c buffer size */
135 char *inbuf
; /* Input buffer */
136 char *cmdbuf
; /* Command buffer */
137 char *argsbuf
; /* Command arguments buffer */
139 unsigned long flags
; /* device flags */
141 int irq
; /* the interrupt line irq */
143 wait_queue_head_t wait
; /* For waiting the interrupt */
146 static int __i2c_hid_command(struct i2c_client
*client
,
147 const struct i2c_hid_cmd
*command
, u8 reportID
,
148 u8 reportType
, u8
*args
, int args_len
,
149 unsigned char *buf_recv
, int data_len
)
151 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
152 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
154 struct i2c_msg msg
[2];
157 int length
= command
->length
;
158 bool wait
= command
->wait
;
159 unsigned int registerIndex
= command
->registerIndex
;
161 /* special case for hid_descr_cmd */
162 if (command
== &hid_descr_cmd
) {
163 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
165 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
166 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
170 cmd
->c
.opcode
= command
->opcode
;
171 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
174 memcpy(cmd
->data
+ length
, args
, args_len
);
177 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
179 msg
[0].addr
= client
->addr
;
180 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
182 msg
[0].buf
= cmd
->data
;
184 msg
[1].addr
= client
->addr
;
185 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
186 msg
[1].flags
|= I2C_M_RD
;
187 msg
[1].len
= data_len
;
188 msg
[1].buf
= buf_recv
;
190 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
194 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
196 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
199 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
202 return ret
< 0 ? ret
: -EIO
;
207 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
208 if (!wait_event_timeout(ihid
->wait
,
209 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
210 msecs_to_jiffies(5000)))
212 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
218 static int i2c_hid_command(struct i2c_client
*client
,
219 const struct i2c_hid_cmd
*command
,
220 unsigned char *buf_recv
, int data_len
)
222 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
226 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
227 u8 reportID
, unsigned char *buf_recv
, int data_len
)
229 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
233 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
235 i2c_hid_dbg(ihid
, "%s\n", __func__
);
237 if (reportID
>= 0x0F) {
238 args
[args_len
++] = reportID
;
242 args
[args_len
++] = readRegister
& 0xFF;
243 args
[args_len
++] = readRegister
>> 8;
245 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
246 reportType
, args
, args_len
, buf_recv
, data_len
);
248 dev_err(&client
->dev
,
249 "failed to retrieve report from device.\n");
256 static int i2c_hid_set_report(struct i2c_client
*client
, u8 reportType
,
257 u8 reportID
, unsigned char *buf
, size_t data_len
)
259 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
260 u8
*args
= ihid
->argsbuf
;
262 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
264 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
265 u16 size
= 2 /* size */ +
266 (reportID
? 1 : 0) /* reportID */ +
268 int args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
269 2 /* dataRegister */ +
273 i2c_hid_dbg(ihid
, "%s\n", __func__
);
275 if (reportID
>= 0x0F) {
276 args
[index
++] = reportID
;
280 args
[index
++] = dataRegister
& 0xFF;
281 args
[index
++] = dataRegister
>> 8;
283 args
[index
++] = size
& 0xFF;
284 args
[index
++] = size
>> 8;
287 args
[index
++] = reportID
;
289 memcpy(&args
[index
], buf
, data_len
);
291 ret
= __i2c_hid_command(client
, &hid_set_report_cmd
, reportID
,
292 reportType
, args
, args_len
, NULL
, 0);
294 dev_err(&client
->dev
, "failed to set a report to device.\n");
301 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
303 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
306 i2c_hid_dbg(ihid
, "%s\n", __func__
);
308 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
309 0, NULL
, 0, NULL
, 0);
311 dev_err(&client
->dev
, "failed to change power setting.\n");
316 static int i2c_hid_hwreset(struct i2c_client
*client
)
318 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
321 i2c_hid_dbg(ihid
, "%s\n", __func__
);
323 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
327 i2c_hid_dbg(ihid
, "resetting...\n");
329 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
331 dev_err(&client
->dev
, "failed to reset device.\n");
332 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
339 static int i2c_hid_get_input(struct i2c_hid
*ihid
)
342 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
344 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
349 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
350 __func__
, ret
, size
);
354 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
357 /* host or device initiated RESET completed */
358 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
359 wake_up(&ihid
->wait
);
363 if (ret_size
> size
) {
364 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
365 __func__
, size
, ret_size
);
369 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
371 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
372 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
378 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
380 struct i2c_hid
*ihid
= dev_id
;
382 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
385 i2c_hid_get_input(ihid
);
390 static int i2c_hid_get_report_length(struct hid_report
*report
)
392 return ((report
->size
- 1) >> 3) + 1 +
393 report
->device
->report_enum
[report
->type
].numbered
+ 2;
396 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
399 struct hid_device
*hid
= report
->device
;
400 struct i2c_client
*client
= hid
->driver_data
;
401 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
402 unsigned int size
, ret_size
;
404 size
= i2c_hid_get_report_length(report
);
405 i2c_hid_get_report(client
,
406 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
407 report
->id
, buffer
, size
);
409 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, ihid
->inbuf
);
411 ret_size
= buffer
[0] | (buffer
[1] << 8);
413 if (ret_size
!= size
) {
414 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
415 __func__
, size
, ret_size
);
419 /* hid->driver_lock is held as we are in probe function,
420 * we just need to setup the input fields, so using
421 * hid_report_raw_event is safe. */
422 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
426 * Initialize all reports
428 static void i2c_hid_init_reports(struct hid_device
*hid
)
430 struct hid_report
*report
;
431 struct i2c_client
*client
= hid
->driver_data
;
432 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
433 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
438 list_for_each_entry(report
,
439 &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
440 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
442 list_for_each_entry(report
,
443 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
444 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
450 * Traverse the supplied list of reports and find the longest
452 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
455 struct hid_report
*report
;
458 /* We should not rely on wMaxInputLength, as some devices may set it to
460 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
461 size
= i2c_hid_get_report_length(report
);
467 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
)
469 /* the worst case is computed from the set_report command with a
470 * reportID > 15 and the maximum report length */
471 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
472 sizeof(__u16
) + /* data register */
473 sizeof(__u16
) + /* size of the report */
474 ihid
->bufsize
; /* report */
476 ihid
->inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
481 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
483 if (!ihid
->argsbuf
) {
488 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
492 kfree(ihid
->argsbuf
);
494 ihid
->argsbuf
= NULL
;
501 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
504 kfree(ihid
->argsbuf
);
508 ihid
->argsbuf
= NULL
;
511 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
512 unsigned char report_number
, __u8
*buf
, size_t count
,
513 unsigned char report_type
)
515 struct i2c_client
*client
= hid
->driver_data
;
516 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
519 if (report_type
== HID_OUTPUT_REPORT
)
522 if (count
> ihid
->bufsize
)
523 count
= ihid
->bufsize
;
525 ret
= i2c_hid_get_report(client
,
526 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
527 report_number
, ihid
->inbuf
, count
);
532 count
= ihid
->inbuf
[0] | (ihid
->inbuf
[1] << 8);
534 memcpy(buf
, ihid
->inbuf
+ 2, count
);
539 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
540 size_t count
, unsigned char report_type
)
542 struct i2c_client
*client
= hid
->driver_data
;
543 int report_id
= buf
[0];
545 if (report_type
== HID_INPUT_REPORT
)
548 return i2c_hid_set_report(client
,
549 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
550 report_id
, buf
, count
);
553 static int i2c_hid_parse(struct hid_device
*hid
)
555 struct i2c_client
*client
= hid
->driver_data
;
556 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
557 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
563 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
565 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
566 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
567 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
572 ret
= i2c_hid_hwreset(client
);
575 } while (tries
-- > 0 && ret
);
580 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
583 dbg_hid("couldn't allocate rdesc memory\n");
587 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
589 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
591 hid_err(hid
, "reading report descriptor failed\n");
596 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
598 ret
= hid_parse_report(hid
, rdesc
, rsize
);
601 dbg_hid("parsing report descriptor failed\n");
608 static int i2c_hid_start(struct hid_device
*hid
)
610 struct i2c_client
*client
= hid
->driver_data
;
611 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
613 int old_bufsize
= ihid
->bufsize
;
615 ihid
->bufsize
= HID_MIN_BUFFER_SIZE
;
616 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &ihid
->bufsize
);
617 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &ihid
->bufsize
);
618 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &ihid
->bufsize
);
620 if (ihid
->bufsize
> old_bufsize
|| !ihid
->inbuf
|| !ihid
->cmdbuf
) {
621 i2c_hid_free_buffers(ihid
);
623 ret
= i2c_hid_alloc_buffers(ihid
);
626 ihid
->bufsize
= old_bufsize
;
631 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
632 i2c_hid_init_reports(hid
);
637 static void i2c_hid_stop(struct hid_device
*hid
)
639 struct i2c_client
*client
= hid
->driver_data
;
640 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
644 i2c_hid_free_buffers(ihid
);
647 static int i2c_hid_open(struct hid_device
*hid
)
649 struct i2c_client
*client
= hid
->driver_data
;
650 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
654 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
659 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
664 static void i2c_hid_close(struct hid_device
*hid
)
666 struct i2c_client
*client
= hid
->driver_data
;
667 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
669 /* protecting hid->open to make sure we don't restart
670 * data acquistion due to a resumption we no longer
674 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
676 /* Save some power */
677 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
681 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
683 struct i2c_client
*client
= hid
->driver_data
;
684 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
687 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
691 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
694 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
700 static int i2c_hid_hidinput_input_event(struct input_dev
*dev
,
701 unsigned int type
, unsigned int code
, int value
)
703 struct hid_device
*hid
= input_get_drvdata(dev
);
704 struct hid_field
*field
;
708 return input_ff_event(dev
, type
, code
, value
);
713 offset
= hidinput_find_field(hid
, type
, code
, &field
);
716 hid_warn(dev
, "event field not found\n");
720 hid_set_field(field
, offset
, value
);
725 static struct hid_ll_driver i2c_hid_ll_driver
= {
726 .parse
= i2c_hid_parse
,
727 .start
= i2c_hid_start
,
728 .stop
= i2c_hid_stop
,
729 .open
= i2c_hid_open
,
730 .close
= i2c_hid_close
,
731 .power
= i2c_hid_power
,
732 .hidinput_input_event
= i2c_hid_hidinput_input_event
,
735 static int __devinit
i2c_hid_init_irq(struct i2c_client
*client
)
737 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
740 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
742 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
743 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
746 dev_dbg(&client
->dev
,
747 "Could not register for %s interrupt, irq = %d,"
749 client
->name
, client
->irq
, ret
);
754 ihid
->irq
= client
->irq
;
759 static int __devinit
i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
761 struct i2c_client
*client
= ihid
->client
;
762 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
766 /* Fetch the length of HID description, retrieve the 4 first bytes:
767 * bytes 0-1 -> length
768 * bytes 2-3 -> bcdVersion (has to be 1.00) */
769 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
, 4);
771 i2c_hid_dbg(ihid
, "%s, ihid->hdesc_buffer: %*ph\n",
772 __func__
, 4, ihid
->hdesc_buffer
);
775 dev_err(&client
->dev
, "HID_DESCR_LENGTH_CMD Fail (ret=%d)\n",
780 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
781 if (!dsize
|| dsize
> HID_MAX_DESCRIPTOR_SIZE
) {
782 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
787 /* check bcdVersion == 1.0 */
788 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
789 dev_err(&client
->dev
,
790 "unexpected HID descriptor bcdVersion (0x%04x)\n",
791 le16_to_cpu(hdesc
->bcdVersion
));
795 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
797 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
800 dev_err(&client
->dev
, "hid_descr_cmd Fail\n");
804 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
809 static int __devinit
i2c_hid_probe(struct i2c_client
*client
,
810 const struct i2c_device_id
*dev_id
)
813 struct i2c_hid
*ihid
;
814 struct hid_device
*hid
;
816 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
818 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
820 if (!platform_data
) {
821 dev_err(&client
->dev
, "HID register address not provided\n");
826 dev_err(&client
->dev
,
827 "HID over i2c has not been provided an Int IRQ\n");
831 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
835 i2c_set_clientdata(client
, ihid
);
837 ihid
->client
= client
;
839 hidRegister
= platform_data
->hid_descriptor_address
;
840 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
842 init_waitqueue_head(&ihid
->wait
);
844 /* we need to allocate the command buffer without knowing the maximum
845 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
846 * real computation later. */
847 ihid
->bufsize
= HID_MIN_BUFFER_SIZE
;
848 i2c_hid_alloc_buffers(ihid
);
850 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
854 ret
= i2c_hid_init_irq(client
);
858 hid
= hid_allocate_device();
866 hid
->driver_data
= client
;
867 hid
->ll_driver
= &i2c_hid_ll_driver
;
868 hid
->hid_get_raw_report
= i2c_hid_get_raw_report
;
869 hid
->hid_output_raw_report
= i2c_hid_output_raw_report
;
870 hid
->dev
.parent
= &client
->dev
;
872 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
873 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
874 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
876 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04X:%04X",
877 client
->name
, hid
->vendor
, hid
->product
);
879 ret
= hid_add_device(hid
);
882 hid_err(client
, "can't add hid device: %d\n", ret
);
889 hid_destroy_device(hid
);
893 free_irq(ihid
->irq
, ihid
);
899 static int __devexit
i2c_hid_remove(struct i2c_client
*client
)
901 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
902 struct hid_device
*hid
;
908 hid_destroy_device(hid
);
910 free_irq(client
->irq
, ihid
);
917 #ifdef CONFIG_PM_SLEEP
918 static int i2c_hid_suspend(struct device
*dev
)
920 struct i2c_client
*client
= to_i2c_client(dev
);
921 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
923 if (device_may_wakeup(&client
->dev
))
924 enable_irq_wake(ihid
->irq
);
926 /* Save some power */
927 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
932 static int i2c_hid_resume(struct device
*dev
)
935 struct i2c_client
*client
= to_i2c_client(dev
);
937 ret
= i2c_hid_hwreset(client
);
941 if (device_may_wakeup(&client
->dev
))
942 disable_irq_wake(client
->irq
);
948 static SIMPLE_DEV_PM_OPS(i2c_hid_pm
, i2c_hid_suspend
, i2c_hid_resume
);
950 static const struct i2c_device_id i2c_hid_id_table
[] = {
954 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
957 static struct i2c_driver i2c_hid_driver
= {
960 .owner
= THIS_MODULE
,
964 .probe
= i2c_hid_probe
,
965 .remove
= __devexit_p(i2c_hid_remove
),
967 .id_table
= i2c_hid_id_table
,
970 module_i2c_driver(i2c_hid_driver
);
972 MODULE_DESCRIPTION("HID over I2C core driver");
973 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
974 MODULE_LICENSE("GPL");