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>
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
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...) \
56 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
60 __le16 wHIDDescLength
;
62 __le16 wReportDescLength
;
63 __le16 wReportDescRegister
;
64 __le16 wInputRegister
;
65 __le16 wMaxInputLength
;
66 __le16 wOutputRegister
;
67 __le16 wMaxOutputLength
;
68 __le16 wCommandRegister
;
77 unsigned int registerIndex
;
92 #define I2C_HID_CMD(opcode_) \
93 .opcode = opcode_, .length = 4, \
94 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
96 /* fetch HID descriptor */
97 static const struct i2c_hid_cmd hid_descr_cmd
= { .length
= 2 };
98 /* fetch report descriptors */
99 static const struct i2c_hid_cmd hid_report_descr_cmd
= {
100 .registerIndex
= offsetof(struct i2c_hid_desc
,
101 wReportDescRegister
),
105 static const struct i2c_hid_cmd hid_reset_cmd
= { I2C_HID_CMD(0x01),
107 static const struct i2c_hid_cmd hid_get_report_cmd
= { I2C_HID_CMD(0x02) };
108 static const struct i2c_hid_cmd hid_set_report_cmd
= { I2C_HID_CMD(0x03) };
109 static const struct i2c_hid_cmd hid_set_power_cmd
= { I2C_HID_CMD(0x08) };
112 * These definitions are not used here, but are defined by the spec.
113 * Keeping them here for documentation purposes.
115 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
116 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
117 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
118 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
121 static DEFINE_MUTEX(i2c_hid_open_mut
);
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 wait_queue_head_t wait
; /* For waiting the interrupt */
144 static int __i2c_hid_command(struct i2c_client
*client
,
145 const struct i2c_hid_cmd
*command
, u8 reportID
,
146 u8 reportType
, u8
*args
, int args_len
,
147 unsigned char *buf_recv
, int data_len
)
149 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
150 union command
*cmd
= (union command
*)ihid
->cmdbuf
;
152 struct i2c_msg msg
[2];
155 int length
= command
->length
;
156 bool wait
= command
->wait
;
157 unsigned int registerIndex
= command
->registerIndex
;
159 /* special case for hid_descr_cmd */
160 if (command
== &hid_descr_cmd
) {
161 cmd
->c
.reg
= ihid
->wHIDDescRegister
;
163 cmd
->data
[0] = ihid
->hdesc_buffer
[registerIndex
];
164 cmd
->data
[1] = ihid
->hdesc_buffer
[registerIndex
+ 1];
168 cmd
->c
.opcode
= command
->opcode
;
169 cmd
->c
.reportTypeID
= reportID
| reportType
<< 4;
172 memcpy(cmd
->data
+ length
, args
, args_len
);
175 i2c_hid_dbg(ihid
, "%s: cmd=%*ph\n", __func__
, length
, cmd
->data
);
177 msg
[0].addr
= client
->addr
;
178 msg
[0].flags
= client
->flags
& I2C_M_TEN
;
180 msg
[0].buf
= cmd
->data
;
182 msg
[1].addr
= client
->addr
;
183 msg
[1].flags
= client
->flags
& I2C_M_TEN
;
184 msg
[1].flags
|= I2C_M_RD
;
185 msg
[1].len
= data_len
;
186 msg
[1].buf
= buf_recv
;
188 set_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
192 set_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
);
194 ret
= i2c_transfer(client
->adapter
, msg
, msg_num
);
197 clear_bit(I2C_HID_READ_PENDING
, &ihid
->flags
);
200 return ret
< 0 ? ret
: -EIO
;
205 i2c_hid_dbg(ihid
, "%s: waiting...\n", __func__
);
206 if (!wait_event_timeout(ihid
->wait
,
207 !test_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
),
208 msecs_to_jiffies(5000)))
210 i2c_hid_dbg(ihid
, "%s: finished.\n", __func__
);
216 static int i2c_hid_command(struct i2c_client
*client
,
217 const struct i2c_hid_cmd
*command
,
218 unsigned char *buf_recv
, int data_len
)
220 return __i2c_hid_command(client
, command
, 0, 0, NULL
, 0,
224 static int i2c_hid_get_report(struct i2c_client
*client
, u8 reportType
,
225 u8 reportID
, unsigned char *buf_recv
, int data_len
)
227 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
231 u16 readRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
233 i2c_hid_dbg(ihid
, "%s\n", __func__
);
235 if (reportID
>= 0x0F) {
236 args
[args_len
++] = reportID
;
240 args
[args_len
++] = readRegister
& 0xFF;
241 args
[args_len
++] = readRegister
>> 8;
243 ret
= __i2c_hid_command(client
, &hid_get_report_cmd
, reportID
,
244 reportType
, args
, args_len
, buf_recv
, data_len
);
246 dev_err(&client
->dev
,
247 "failed to retrieve report from device.\n");
254 static int i2c_hid_set_report(struct i2c_client
*client
, u8 reportType
,
255 u8 reportID
, unsigned char *buf
, size_t data_len
)
257 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
258 u8
*args
= ihid
->argsbuf
;
260 u16 dataRegister
= le16_to_cpu(ihid
->hdesc
.wDataRegister
);
262 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
263 u16 size
= 2 /* size */ +
264 (reportID
? 1 : 0) /* reportID */ +
266 int args_len
= (reportID
>= 0x0F ? 1 : 0) /* optional third byte */ +
267 2 /* dataRegister */ +
271 i2c_hid_dbg(ihid
, "%s\n", __func__
);
273 if (reportID
>= 0x0F) {
274 args
[index
++] = reportID
;
278 args
[index
++] = dataRegister
& 0xFF;
279 args
[index
++] = dataRegister
>> 8;
281 args
[index
++] = size
& 0xFF;
282 args
[index
++] = size
>> 8;
285 args
[index
++] = reportID
;
287 memcpy(&args
[index
], buf
, data_len
);
289 ret
= __i2c_hid_command(client
, &hid_set_report_cmd
, reportID
,
290 reportType
, args
, args_len
, NULL
, 0);
292 dev_err(&client
->dev
, "failed to set a report to device.\n");
299 static int i2c_hid_set_power(struct i2c_client
*client
, int power_state
)
301 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
304 i2c_hid_dbg(ihid
, "%s\n", __func__
);
306 ret
= __i2c_hid_command(client
, &hid_set_power_cmd
, power_state
,
307 0, NULL
, 0, NULL
, 0);
309 dev_err(&client
->dev
, "failed to change power setting.\n");
314 static int i2c_hid_hwreset(struct i2c_client
*client
)
316 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
319 i2c_hid_dbg(ihid
, "%s\n", __func__
);
321 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
325 i2c_hid_dbg(ihid
, "resetting...\n");
327 ret
= i2c_hid_command(client
, &hid_reset_cmd
, NULL
, 0);
329 dev_err(&client
->dev
, "failed to reset device.\n");
330 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
337 static void i2c_hid_get_input(struct i2c_hid
*ihid
)
340 int size
= le16_to_cpu(ihid
->hdesc
.wMaxInputLength
);
342 ret
= i2c_master_recv(ihid
->client
, ihid
->inbuf
, size
);
347 dev_err(&ihid
->client
->dev
, "%s: got %d data instead of %d\n",
348 __func__
, ret
, size
);
352 ret_size
= ihid
->inbuf
[0] | ihid
->inbuf
[1] << 8;
355 /* host or device initiated RESET completed */
356 if (test_and_clear_bit(I2C_HID_RESET_PENDING
, &ihid
->flags
))
357 wake_up(&ihid
->wait
);
361 if (ret_size
> size
) {
362 dev_err(&ihid
->client
->dev
, "%s: incomplete report (%d/%d)\n",
363 __func__
, size
, ret_size
);
367 i2c_hid_dbg(ihid
, "input: %*ph\n", ret_size
, ihid
->inbuf
);
369 if (test_bit(I2C_HID_STARTED
, &ihid
->flags
))
370 hid_input_report(ihid
->hid
, HID_INPUT_REPORT
, ihid
->inbuf
+ 2,
376 static irqreturn_t
i2c_hid_irq(int irq
, void *dev_id
)
378 struct i2c_hid
*ihid
= dev_id
;
380 if (test_bit(I2C_HID_READ_PENDING
, &ihid
->flags
))
383 i2c_hid_get_input(ihid
);
388 static int i2c_hid_get_report_length(struct hid_report
*report
)
390 return ((report
->size
- 1) >> 3) + 1 +
391 report
->device
->report_enum
[report
->type
].numbered
+ 2;
394 static void i2c_hid_init_report(struct hid_report
*report
, u8
*buffer
,
397 struct hid_device
*hid
= report
->device
;
398 struct i2c_client
*client
= hid
->driver_data
;
399 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
400 unsigned int size
, ret_size
;
402 size
= i2c_hid_get_report_length(report
);
403 if (i2c_hid_get_report(client
,
404 report
->type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
405 report
->id
, buffer
, size
))
408 i2c_hid_dbg(ihid
, "report (len=%d): %*ph\n", size
, size
, ihid
->inbuf
);
410 ret_size
= buffer
[0] | (buffer
[1] << 8);
412 if (ret_size
!= size
) {
413 dev_err(&client
->dev
, "error in %s size:%d / ret_size:%d\n",
414 __func__
, size
, ret_size
);
418 /* hid->driver_lock is held as we are in probe function,
419 * we just need to setup the input fields, so using
420 * hid_report_raw_event is safe. */
421 hid_report_raw_event(hid
, report
->type
, buffer
+ 2, size
- 2, 1);
425 * Initialize all reports
427 static void i2c_hid_init_reports(struct hid_device
*hid
)
429 struct hid_report
*report
;
430 struct i2c_client
*client
= hid
->driver_data
;
431 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
432 u8
*inbuf
= kzalloc(ihid
->bufsize
, GFP_KERNEL
);
435 dev_err(&client
->dev
, "can not retrieve initial reports\n");
439 list_for_each_entry(report
,
440 &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
441 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
443 list_for_each_entry(report
,
444 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
445 i2c_hid_init_report(report
, inbuf
, ihid
->bufsize
);
451 * Traverse the supplied list of reports and find the longest
453 static void i2c_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
456 struct hid_report
*report
;
459 /* We should not rely on wMaxInputLength, as some devices may set it to
461 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
462 size
= i2c_hid_get_report_length(report
);
468 static void i2c_hid_free_buffers(struct i2c_hid
*ihid
)
471 kfree(ihid
->argsbuf
);
475 ihid
->argsbuf
= NULL
;
479 static int i2c_hid_alloc_buffers(struct i2c_hid
*ihid
, size_t report_size
)
481 /* the worst case is computed from the set_report command with a
482 * reportID > 15 and the maximum report length */
483 int args_len
= sizeof(__u8
) + /* optional ReportID byte */
484 sizeof(__u16
) + /* data register */
485 sizeof(__u16
) + /* size of the report */
486 report_size
; /* report */
488 ihid
->inbuf
= kzalloc(report_size
, GFP_KERNEL
);
489 ihid
->argsbuf
= kzalloc(args_len
, GFP_KERNEL
);
490 ihid
->cmdbuf
= kzalloc(sizeof(union command
) + args_len
, GFP_KERNEL
);
492 if (!ihid
->inbuf
|| !ihid
->argsbuf
|| !ihid
->cmdbuf
) {
493 i2c_hid_free_buffers(ihid
);
497 ihid
->bufsize
= report_size
;
502 static int i2c_hid_get_raw_report(struct hid_device
*hid
,
503 unsigned char report_number
, __u8
*buf
, size_t count
,
504 unsigned char report_type
)
506 struct i2c_client
*client
= hid
->driver_data
;
507 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
508 size_t ret_count
, ask_count
;
511 if (report_type
== HID_OUTPUT_REPORT
)
514 /* +2 bytes to include the size of the reply in the query buffer */
515 ask_count
= min(count
+ 2, (size_t)ihid
->bufsize
);
517 ret
= i2c_hid_get_report(client
,
518 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x01,
519 report_number
, ihid
->inbuf
, ask_count
);
524 ret_count
= ihid
->inbuf
[0] | (ihid
->inbuf
[1] << 8);
529 ret_count
= min(ret_count
, ask_count
);
531 /* The query buffer contains the size, dropping it in the reply */
532 count
= min(count
, ret_count
- 2);
533 memcpy(buf
, ihid
->inbuf
+ 2, count
);
538 static int i2c_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
539 size_t count
, unsigned char report_type
)
541 struct i2c_client
*client
= hid
->driver_data
;
542 int report_id
= buf
[0];
545 if (report_type
== HID_INPUT_REPORT
)
553 ret
= i2c_hid_set_report(client
,
554 report_type
== HID_FEATURE_REPORT
? 0x03 : 0x02,
555 report_id
, buf
, count
);
557 if (report_id
&& ret
>= 0)
558 ret
++; /* add report_id to the number of transfered bytes */
563 static int i2c_hid_parse(struct hid_device
*hid
)
565 struct i2c_client
*client
= hid
->driver_data
;
566 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
567 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
573 i2c_hid_dbg(ihid
, "entering %s\n", __func__
);
575 rsize
= le16_to_cpu(hdesc
->wReportDescLength
);
576 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
577 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
582 ret
= i2c_hid_hwreset(client
);
585 } while (tries
-- > 0 && ret
);
590 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
593 dbg_hid("couldn't allocate rdesc memory\n");
597 i2c_hid_dbg(ihid
, "asking HID report descriptor\n");
599 ret
= i2c_hid_command(client
, &hid_report_descr_cmd
, rdesc
, rsize
);
601 hid_err(hid
, "reading report descriptor failed\n");
606 i2c_hid_dbg(ihid
, "Report Descriptor: %*ph\n", rsize
, rdesc
);
608 ret
= hid_parse_report(hid
, rdesc
, rsize
);
611 dbg_hid("parsing report descriptor failed\n");
618 static int i2c_hid_start(struct hid_device
*hid
)
620 struct i2c_client
*client
= hid
->driver_data
;
621 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
623 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
625 i2c_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
626 i2c_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
627 i2c_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
629 if (bufsize
> ihid
->bufsize
) {
630 i2c_hid_free_buffers(ihid
);
632 ret
= i2c_hid_alloc_buffers(ihid
, bufsize
);
638 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
639 i2c_hid_init_reports(hid
);
644 static void i2c_hid_stop(struct hid_device
*hid
)
646 struct i2c_client
*client
= hid
->driver_data
;
647 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
651 i2c_hid_free_buffers(ihid
);
654 static int i2c_hid_open(struct hid_device
*hid
)
656 struct i2c_client
*client
= hid
->driver_data
;
657 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
660 mutex_lock(&i2c_hid_open_mut
);
662 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
667 set_bit(I2C_HID_STARTED
, &ihid
->flags
);
670 mutex_unlock(&i2c_hid_open_mut
);
674 static void i2c_hid_close(struct hid_device
*hid
)
676 struct i2c_client
*client
= hid
->driver_data
;
677 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
679 /* protecting hid->open to make sure we don't restart
680 * data acquistion due to a resumption we no longer
683 mutex_lock(&i2c_hid_open_mut
);
685 clear_bit(I2C_HID_STARTED
, &ihid
->flags
);
687 /* Save some power */
688 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
690 mutex_unlock(&i2c_hid_open_mut
);
693 static int i2c_hid_power(struct hid_device
*hid
, int lvl
)
695 struct i2c_client
*client
= hid
->driver_data
;
696 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
699 i2c_hid_dbg(ihid
, "%s lvl:%d\n", __func__
, lvl
);
703 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_ON
);
706 ret
= i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
712 static int i2c_hid_hidinput_input_event(struct input_dev
*dev
,
713 unsigned int type
, unsigned int code
, int value
)
715 struct hid_device
*hid
= input_get_drvdata(dev
);
716 struct hid_field
*field
;
720 return input_ff_event(dev
, type
, code
, value
);
725 offset
= hidinput_find_field(hid
, type
, code
, &field
);
728 hid_warn(dev
, "event field not found\n");
732 return hid_set_field(field
, offset
, value
);
735 static struct hid_ll_driver i2c_hid_ll_driver
= {
736 .parse
= i2c_hid_parse
,
737 .start
= i2c_hid_start
,
738 .stop
= i2c_hid_stop
,
739 .open
= i2c_hid_open
,
740 .close
= i2c_hid_close
,
741 .power
= i2c_hid_power
,
742 .hidinput_input_event
= i2c_hid_hidinput_input_event
,
745 static int __devinit
i2c_hid_init_irq(struct i2c_client
*client
)
747 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
750 dev_dbg(&client
->dev
, "Requesting IRQ: %d\n", client
->irq
);
752 ret
= request_threaded_irq(client
->irq
, NULL
, i2c_hid_irq
,
753 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
756 dev_warn(&client
->dev
,
757 "Could not register for %s interrupt, irq = %d,"
759 client
->name
, client
->irq
, ret
);
767 static int __devinit
i2c_hid_fetch_hid_descriptor(struct i2c_hid
*ihid
)
769 struct i2c_client
*client
= ihid
->client
;
770 struct i2c_hid_desc
*hdesc
= &ihid
->hdesc
;
774 /* Fetch the length of HID description, retrieve the 4 first bytes:
775 * bytes 0-1 -> length
776 * bytes 2-3 -> bcdVersion (has to be 1.00) */
777 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
, 4);
779 i2c_hid_dbg(ihid
, "%s, ihid->hdesc_buffer: %*ph\n",
780 __func__
, 4, ihid
->hdesc_buffer
);
783 dev_err(&client
->dev
,
784 "unable to fetch the size of HID descriptor (ret=%d)\n",
789 dsize
= le16_to_cpu(hdesc
->wHIDDescLength
);
791 * the size of the HID descriptor should at least contain
792 * its size and the bcdVersion (4 bytes), and should not be greater
793 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
794 * through i2c_hid_command.
796 if (dsize
< 4 || dsize
> sizeof(struct i2c_hid_desc
)) {
797 dev_err(&client
->dev
, "weird size of HID descriptor (%u)\n",
802 /* check bcdVersion == 1.0 */
803 if (le16_to_cpu(hdesc
->bcdVersion
) != 0x0100) {
804 dev_err(&client
->dev
,
805 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
806 le16_to_cpu(hdesc
->bcdVersion
));
810 i2c_hid_dbg(ihid
, "Fetching the HID descriptor\n");
812 ret
= i2c_hid_command(client
, &hid_descr_cmd
, ihid
->hdesc_buffer
,
815 dev_err(&client
->dev
, "hid_descr_cmd Fail\n");
819 i2c_hid_dbg(ihid
, "HID Descriptor: %*ph\n", dsize
, ihid
->hdesc_buffer
);
824 static int __devinit
i2c_hid_probe(struct i2c_client
*client
,
825 const struct i2c_device_id
*dev_id
)
828 struct i2c_hid
*ihid
;
829 struct hid_device
*hid
;
831 struct i2c_hid_platform_data
*platform_data
= client
->dev
.platform_data
;
833 dbg_hid("HID probe called for i2c 0x%02x\n", client
->addr
);
835 if (!platform_data
) {
836 dev_err(&client
->dev
, "HID register address not provided\n");
841 dev_err(&client
->dev
,
842 "HID over i2c has not been provided an Int IRQ\n");
846 ihid
= kzalloc(sizeof(struct i2c_hid
), GFP_KERNEL
);
850 i2c_set_clientdata(client
, ihid
);
852 ihid
->client
= client
;
854 hidRegister
= platform_data
->hid_descriptor_address
;
855 ihid
->wHIDDescRegister
= cpu_to_le16(hidRegister
);
857 init_waitqueue_head(&ihid
->wait
);
859 /* we need to allocate the command buffer without knowing the maximum
860 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
861 * real computation later. */
862 ret
= i2c_hid_alloc_buffers(ihid
, HID_MIN_BUFFER_SIZE
);
866 ret
= i2c_hid_fetch_hid_descriptor(ihid
);
870 ret
= i2c_hid_init_irq(client
);
874 hid
= hid_allocate_device();
882 hid
->driver_data
= client
;
883 hid
->ll_driver
= &i2c_hid_ll_driver
;
884 hid
->hid_get_raw_report
= i2c_hid_get_raw_report
;
885 hid
->hid_output_raw_report
= i2c_hid_output_raw_report
;
886 hid
->dev
.parent
= &client
->dev
;
888 hid
->version
= le16_to_cpu(ihid
->hdesc
.bcdVersion
);
889 hid
->vendor
= le16_to_cpu(ihid
->hdesc
.wVendorID
);
890 hid
->product
= le16_to_cpu(ihid
->hdesc
.wProductID
);
892 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04hX:%04hX",
893 client
->name
, hid
->vendor
, hid
->product
);
895 ret
= hid_add_device(hid
);
898 hid_err(client
, "can't add hid device: %d\n", ret
);
905 hid_destroy_device(hid
);
908 free_irq(client
->irq
, ihid
);
911 i2c_hid_free_buffers(ihid
);
916 static int __devexit
i2c_hid_remove(struct i2c_client
*client
)
918 struct i2c_hid
*ihid
= i2c_get_clientdata(client
);
919 struct hid_device
*hid
;
922 hid_destroy_device(hid
);
924 free_irq(client
->irq
, ihid
);
927 i2c_hid_free_buffers(ihid
);
934 #ifdef CONFIG_PM_SLEEP
935 static int i2c_hid_suspend(struct device
*dev
)
937 struct i2c_client
*client
= to_i2c_client(dev
);
939 if (device_may_wakeup(&client
->dev
))
940 enable_irq_wake(client
->irq
);
942 /* Save some power */
943 i2c_hid_set_power(client
, I2C_HID_PWR_SLEEP
);
948 static int i2c_hid_resume(struct device
*dev
)
951 struct i2c_client
*client
= to_i2c_client(dev
);
953 ret
= i2c_hid_hwreset(client
);
957 if (device_may_wakeup(&client
->dev
))
958 disable_irq_wake(client
->irq
);
964 static SIMPLE_DEV_PM_OPS(i2c_hid_pm
, i2c_hid_suspend
, i2c_hid_resume
);
966 static const struct i2c_device_id i2c_hid_id_table
[] = {
970 MODULE_DEVICE_TABLE(i2c
, i2c_hid_id_table
);
973 static struct i2c_driver i2c_hid_driver
= {
976 .owner
= THIS_MODULE
,
980 .probe
= i2c_hid_probe
,
981 .remove
= __devexit_p(i2c_hid_remove
),
983 .id_table
= i2c_hid_id_table
,
986 module_i2c_driver(i2c_hid_driver
);
988 MODULE_DESCRIPTION("HID over I2C core driver");
989 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
990 MODULE_LICENSE("GPL");