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>
37 #include <linux/i2c/i2c-hid.h>
40 #define I2C_HID_STARTED (1 << 0)
41 #define I2C_HID_RESET_PENDING (1 << 1)
42 #define I2C_HID_READ_PENDING (1 << 2)
44 #define I2C_HID_PWR_ON 0x00
45 #define I2C_HID_PWR_SLEEP 0x01
49 module_param(debug
, bool, 0444);
50 MODULE_PARM_DESC(debug
, "print a lot of debug information");
52 #define i2c_hid_dbg(ihid, fmt, arg...) \
55 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
59 __le16 wHIDDescLength
;
61 __le16 wReportDescLength
;
62 __le16 wReportDescRegister
;
63 __le16 wInputRegister
;
64 __le16 wMaxInputLength
;
65 __le16 wOutputRegister
;
66 __le16 wMaxOutputLength
;
67 __le16 wCommandRegister
;
76 unsigned int registerIndex
;
91 #define I2C_HID_CMD(opcode_) \
92 .opcode = opcode_, .length = 4, \
93 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
95 /* fetch HID descriptor */
96 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
97 /* fetch report descriptors */
98 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
99 .registerIndex
= offsetof(struct i2c_hid_desc
,
100 wReportDescRegister
),
104 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
106 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
107 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
108 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
111 * These definitions are not used here, but are defined by the spec.
112 * Keeping them here for documentation purposes.
114 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
115 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
116 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
117 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
120 /* The main device structure */
122 struct i2c_client
*client
; /* i2c client */
123 struct hid_device
*hid
; /* pointer to corresponding HID dev */
125 __u8 hdesc_buffer
[sizeof(struct i2c_hid_desc
)];
126 struct i2c_hid_desc hdesc
; /* the HID Descriptor */
128 __le16 wHIDDescRegister
; /* location of the i2c
129 * register of the HID
131 unsigned int bufsize
; /* i2c buffer size */
132 char *inbuf
; /* Input buffer */
133 char *cmdbuf
; /* Command buffer */
134 char *argsbuf
; /* Command arguments buffer */
136 unsigned long flags
; /* device flags */
138 wait_queue_head_t wait
; /* For waiting the interrupt */
141 static int __i2c_hid_command(struct i2c_client
*client
,
142 const struct i2c_hid_cmd
*command
, u8 reportID
,
143 u8 reportType
, u8
*args
, int args_len
,
144 unsigned char *buf_recv
, int data_len
)
146 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
147 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
149 struct i2c_msg msg
[2];
152 int length
= command
->length
;
153 bool wait
= command
->wait
;
154 unsigned int registerIndex
= command
->registerIndex
;
156 /* special case for hid_descr_cmd */
157 if (command
== &hid_descr_cmd
) {
158 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
160 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
161 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
165 cmd
->c
.opcode
= command
->opcode
;
166 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
169 memcpy(cmd
->data
+ length
, args
, args_len
);
172 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
174 msg
[0].addr
= client
->addr
;
175 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
177 msg
[0].buf
= cmd
->data
;
179 msg
[1].addr
= client
->addr
;
180 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
181 msg
[1].flags
|= I2C_M_RD
;
182 msg
[1].len
= data_len
;
183 msg
[1].buf
= buf_recv
;
185 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
189 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
191 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
194 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
197 return ret
< 0 ? ret
: -EIO
;
202 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
203 if (!wait_event_timeout(ihid
->wait
,
204 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
205 msecs_to_jiffies(5000)))
207 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
213 static int i2c_hid_command(struct i2c_client
*client
,
214 const struct i2c_hid_cmd
*command
,
215 unsigned char *buf_recv
, int data_len
)
217 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
221 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
222 u8 reportID
, unsigned char *buf_recv
, int data_len
)
224 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
228 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
230 i2c_hid_dbg(ihid
, "%s\n", __func__
);
232 if (reportID
>= 0x0F) {
233 args
[args_len
++] = reportID
;
237 args
[args_len
++] = readRegister
& 0xFF;
238 args
[args_len
++] = readRegister
>> 8;
240 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
241 reportType
, args
, args_len
, buf_recv
, data_len
);
243 dev_err(&client
->dev
,
244 "failed to retrieve report from device.\n");
251 static int i2c_hid_set_report(struct i2c_client
*client
, u8 reportType
,
252 u8 reportID
, unsigned char *buf
, size_t data_len
)
254 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
255 u8
*args
= ihid
->argsbuf
;
257 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
259 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
260 u16 size
= 2 /* size */ +
261 (reportID
? 1 : 0) /* reportID */ +
263 int args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
264 2 /* dataRegister */ +
268 i2c_hid_dbg(ihid
, "%s\n", __func__
);
270 if (reportID
>= 0x0F) {
271 args
[index
++] = reportID
;
275 args
[index
++] = dataRegister
& 0xFF;
276 args
[index
++] = dataRegister
>> 8;
278 args
[index
++] = size
& 0xFF;
279 args
[index
++] = size
>> 8;
282 args
[index
++] = reportID
;
284 memcpy(&args
[index
], buf
, data_len
);
286 ret
= __i2c_hid_command(client
, &hid_set_report_cmd
, reportID
,
287 reportType
, args
, args_len
, NULL
, 0);
289 dev_err(&client
->dev
, "failed to set a report to device.\n");
296 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
298 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
301 i2c_hid_dbg(ihid
, "%s\n", __func__
);
303 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
304 0, NULL
, 0, NULL
, 0);
306 dev_err(&client
->dev
, "failed to change power setting.\n");
311 static int i2c_hid_hwreset(struct i2c_client
*client
)
313 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
316 i2c_hid_dbg(ihid
, "%s\n", __func__
);
318 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
322 i2c_hid_dbg(ihid
, "resetting...\n");
324 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
326 dev_err(&client
->dev
, "failed to reset device.\n");
327 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
334 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
337 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
339 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
344 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
345 __func__
, ret
, size
);
349 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
352 /* host or device initiated RESET completed */
353 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
354 wake_up(&ihid
->wait
);
358 if (ret_size
> size
) {
359 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
360 __func__
, size
, ret_size
);
364 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
366 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
367 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
373 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
375 struct i2c_hid
*ihid
= dev_id
;
377 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
380 i2c_hid_get_input(ihid
);
385 static int i2c_hid_get_report_length(struct hid_report
*report
)
387 return ((report
->size
- 1) >> 3) + 1 +
388 report
->device
->report_enum
[report
->type
].numbered
+ 2;
391 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
394 struct hid_device
*hid
= report
->device
;
395 struct i2c_client
*client
= hid
->driver_data
;
396 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
397 unsigned int size
, ret_size
;
399 size
= i2c_hid_get_report_length(report
);
400 if (i2c_hid_get_report(client
,
401 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
402 report
->id
, buffer
, size
))
405 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, ihid
->inbuf
);
407 ret_size
= buffer
[0] | (buffer
[1] << 8);
409 if (ret_size
!= size
) {
410 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
411 __func__
, size
, ret_size
);
415 /* hid->driver_lock is held as we are in probe function,
416 * we just need to setup the input fields, so using
417 * hid_report_raw_event is safe. */
418 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
422 * Initialize all reports
424 static void i2c_hid_init_reports(struct hid_device
*hid
)
426 struct hid_report
*report
;
427 struct i2c_client
*client
= hid
->driver_data
;
428 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
429 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
432 dev_err(&client
->dev
, "can not retrieve initial reports\n");
436 list_for_each_entry(report
,
437 &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
438 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
440 list_for_each_entry(report
,
441 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
442 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
448 * Traverse the supplied list of reports and find the longest
450 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
453 struct hid_report
*report
;
456 /* We should not rely on wMaxInputLength, as some devices may set it to
458 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
459 size
= i2c_hid_get_report_length(report
);
465 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
468 kfree(ihid
->argsbuf
);
472 ihid
->argsbuf
= NULL
;
476 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
478 /* the worst case is computed from the set_report command with a
479 * reportID > 15 and the maximum report length */
480 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
481 sizeof(__u16
) + /* data register */
482 sizeof(__u16
) + /* size of the report */
483 report_size
; /* report */
485 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
486 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
487 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
489 if (!ihid
->inbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
490 i2c_hid_free_buffers(ihid
);
494 ihid
->bufsize
= report_size
;
499 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
500 unsigned char report_number
, __u8
*buf
, size_t count
,
501 unsigned char report_type
)
503 struct i2c_client
*client
= hid
->driver_data
;
504 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
507 if (report_type
== HID_OUTPUT_REPORT
)
510 if (count
> ihid
->bufsize
)
511 count
= ihid
->bufsize
;
513 ret
= i2c_hid_get_report(client
,
514 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
515 report_number
, ihid
->inbuf
, count
);
520 count
= ihid
->inbuf
[0] | (ihid
->inbuf
[1] << 8);
522 memcpy(buf
, ihid
->inbuf
+ 2, count
);
527 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
528 size_t count
, unsigned char report_type
)
530 struct i2c_client
*client
= hid
->driver_data
;
531 int report_id
= buf
[0];
533 if (report_type
== HID_INPUT_REPORT
)
536 return i2c_hid_set_report(client
,
537 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
538 report_id
, buf
, count
);
541 static int i2c_hid_parse(struct hid_device
*hid
)
543 struct i2c_client
*client
= hid
->driver_data
;
544 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
545 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
551 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
553 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
554 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
555 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
560 ret
= i2c_hid_hwreset(client
);
563 } while (tries
-- > 0 && ret
);
568 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
571 dbg_hid("couldn't allocate rdesc memory\n");
575 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
577 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
579 hid_err(hid
, "reading report descriptor failed\n");
584 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
586 ret
= hid_parse_report(hid
, rdesc
, rsize
);
589 dbg_hid("parsing report descriptor failed\n");
596 static int i2c_hid_start(struct hid_device
*hid
)
598 struct i2c_client
*client
= hid
->driver_data
;
599 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
601 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
603 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
604 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
605 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
607 if (bufsize
> ihid
->bufsize
) {
608 i2c_hid_free_buffers(ihid
);
610 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
616 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
617 i2c_hid_init_reports(hid
);
622 static void i2c_hid_stop(struct hid_device
*hid
)
624 struct i2c_client
*client
= hid
->driver_data
;
625 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
629 i2c_hid_free_buffers(ihid
);
632 static int i2c_hid_open(struct hid_device
*hid
)
634 struct i2c_client
*client
= hid
->driver_data
;
635 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
639 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
644 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
649 static void i2c_hid_close(struct hid_device
*hid
)
651 struct i2c_client
*client
= hid
->driver_data
;
652 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
654 /* protecting hid->open to make sure we don't restart
655 * data acquistion due to a resumption we no longer
659 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
661 /* Save some power */
662 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
666 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
668 struct i2c_client
*client
= hid
->driver_data
;
669 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
672 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
676 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
679 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
685 static int i2c_hid_hidinput_input_event(struct input_dev
*dev
,
686 unsigned int type
, unsigned int code
, int value
)
688 struct hid_device
*hid
= input_get_drvdata(dev
);
689 struct hid_field
*field
;
693 return input_ff_event(dev
, type
, code
, value
);
698 offset
= hidinput_find_field(hid
, type
, code
, &field
);
701 hid_warn(dev
, "event field not found\n");
705 return hid_set_field(field
, offset
, value
);
708 static struct hid_ll_driver i2c_hid_ll_driver
= {
709 .parse
= i2c_hid_parse
,
710 .start
= i2c_hid_start
,
711 .stop
= i2c_hid_stop
,
712 .open
= i2c_hid_open
,
713 .close
= i2c_hid_close
,
714 .power
= i2c_hid_power
,
715 .hidinput_input_event
= i2c_hid_hidinput_input_event
,
718 static int __devinit
i2c_hid_init_irq(struct i2c_client
*client
)
720 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
723 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
725 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
726 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
729 dev_warn(&client
->dev
,
730 "Could not register for %s interrupt, irq = %d,"
732 client
->name
, client
->irq
, ret
);
740 static int __devinit
i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
742 struct i2c_client
*client
= ihid
->client
;
743 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
747 /* Fetch the length of HID description, retrieve the 4 first bytes:
748 * bytes 0-1 -> length
749 * bytes 2-3 -> bcdVersion (has to be 1.00) */
750 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
, 4);
752 i2c_hid_dbg(ihid
, "%s, ihid->hdesc_buffer: %*ph\n",
753 __func__
, 4, ihid
->hdesc_buffer
);
756 dev_err(&client
->dev
,
757 "unable to fetch the size of HID descriptor (ret=%d)\n",
762 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
764 * the size of the HID descriptor should at least contain
765 * its size and the bcdVersion (4 bytes), and should not be greater
766 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
767 * through i2c_hid_command.
769 if (dsize
< 4 || dsize
> sizeof(struct i2c_hid_desc
)) {
770 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
775 /* check bcdVersion == 1.0 */
776 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
777 dev_err(&client
->dev
,
778 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
779 le16_to_cpu(hdesc
->bcdVersion
));
783 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
785 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
788 dev_err(&client
->dev
, "hid_descr_cmd Fail\n");
792 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
797 static int __devinit
i2c_hid_probe(struct i2c_client
*client
,
798 const struct i2c_device_id
*dev_id
)
801 struct i2c_hid
*ihid
;
802 struct hid_device
*hid
;
804 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
806 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
808 if (!platform_data
) {
809 dev_err(&client
->dev
, "HID register address not provided\n");
814 dev_err(&client
->dev
,
815 "HID over i2c has not been provided an Int IRQ\n");
819 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
823 i2c_set_clientdata(client
, ihid
);
825 ihid
->client
= client
;
827 hidRegister
= platform_data
->hid_descriptor_address
;
828 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
830 init_waitqueue_head(&ihid
->wait
);
832 /* we need to allocate the command buffer without knowing the maximum
833 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
834 * real computation later. */
835 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
839 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
843 ret
= i2c_hid_init_irq(client
);
847 hid
= hid_allocate_device();
855 hid
->driver_data
= client
;
856 hid
->ll_driver
= &i2c_hid_ll_driver
;
857 hid
->hid_get_raw_report
= i2c_hid_get_raw_report
;
858 hid
->hid_output_raw_report
= i2c_hid_output_raw_report
;
859 hid
->dev
.parent
= &client
->dev
;
861 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
862 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
863 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
865 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
866 client
->name
, hid
->vendor
, hid
->product
);
868 ret
= hid_add_device(hid
);
871 hid_err(client
, "can't add hid device: %d\n", ret
);
878 hid_destroy_device(hid
);
881 free_irq(client
->irq
, ihid
);
884 i2c_hid_free_buffers(ihid
);
889 static int __devexit
i2c_hid_remove(struct i2c_client
*client
)
891 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
892 struct hid_device
*hid
;
895 hid_destroy_device(hid
);
897 free_irq(client
->irq
, ihid
);
900 i2c_hid_free_buffers(ihid
);
907 #ifdef CONFIG_PM_SLEEP
908 static int i2c_hid_suspend(struct device
*dev
)
910 struct i2c_client
*client
= to_i2c_client(dev
);
912 if (device_may_wakeup(&client
->dev
))
913 enable_irq_wake(client
->irq
);
915 /* Save some power */
916 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
921 static int i2c_hid_resume(struct device
*dev
)
924 struct i2c_client
*client
= to_i2c_client(dev
);
926 ret
= i2c_hid_hwreset(client
);
930 if (device_may_wakeup(&client
->dev
))
931 disable_irq_wake(client
->irq
);
937 static SIMPLE_DEV_PM_OPS(i2c_hid_pm
, i2c_hid_suspend
, i2c_hid_resume
);
939 static const struct i2c_device_id i2c_hid_id_table
[] = {
943 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
946 static struct i2c_driver i2c_hid_driver
= {
949 .owner
= THIS_MODULE
,
953 .probe
= i2c_hid_probe
,
954 .remove
= __devexit_p(i2c_hid_remove
),
956 .id_table
= i2c_hid_id_table
,
959 module_i2c_driver(i2c_hid_driver
);
961 MODULE_DESCRIPTION("HID over I2C core driver");
962 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
963 MODULE_LICENSE("GPL");