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];
544 if (report_type == HID_INPUT_REPORT)
547 return i2c_hid_set_report(client,
548 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
549 report_id, buf, count);
552 static int i2c_hid_parse(struct hid_device *hid)
554 struct i2c_client *client = hid->driver_data;
555 struct i2c_hid *ihid = i2c_get_clientdata(client);
556 struct i2c_hid_desc *hdesc = &ihid->hdesc;
562 i2c_hid_dbg(ihid, "entering %s\n", __func__);
564 rsize = le16_to_cpu(hdesc->wReportDescLength);
565 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
566 dbg_hid("weird size of report descriptor (%u)\n", rsize);
571 ret = i2c_hid_hwreset(client);
574 } while (tries-- > 0 && ret);
579 rdesc = kzalloc(rsize, GFP_KERNEL);
582 dbg_hid("couldn't allocate rdesc memory\n");
586 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
588 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
590 hid_err(hid, "reading report descriptor failed\n");
595 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
597 ret = hid_parse_report(hid, rdesc, rsize);
600 dbg_hid("parsing report descriptor failed\n");
607 static int i2c_hid_start(struct hid_device *hid)
609 struct i2c_client *client = hid->driver_data;
610 struct i2c_hid *ihid = i2c_get_clientdata(client);
612 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
614 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
615 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
616 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
618 if (bufsize > ihid->bufsize) {
619 i2c_hid_free_buffers(ihid);
621 ret = i2c_hid_alloc_buffers(ihid, bufsize);
627 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
628 i2c_hid_init_reports(hid);
633 static void i2c_hid_stop(struct hid_device *hid)
635 struct i2c_client *client = hid->driver_data;
636 struct i2c_hid *ihid = i2c_get_clientdata(client);
640 i2c_hid_free_buffers(ihid);
643 static int i2c_hid_open(struct hid_device *hid)
645 struct i2c_client *client = hid->driver_data;
646 struct i2c_hid *ihid = i2c_get_clientdata(client);
649 mutex_lock(&i2c_hid_open_mut);
651 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
656 set_bit(I2C_HID_STARTED, &ihid->flags);
659 mutex_unlock(&i2c_hid_open_mut);
663 static void i2c_hid_close(struct hid_device *hid)
665 struct i2c_client *client = hid->driver_data;
666 struct i2c_hid *ihid = i2c_get_clientdata(client);
668 /* protecting hid->open to make sure we don't restart
669 * data acquistion due to a resumption we no longer
672 mutex_lock(&i2c_hid_open_mut);
674 clear_bit(I2C_HID_STARTED, &ihid->flags);
676 /* Save some power */
677 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
679 mutex_unlock(&i2c_hid_open_mut);
682 static int i2c_hid_power(struct hid_device *hid, int lvl)
684 struct i2c_client *client = hid->driver_data;
685 struct i2c_hid *ihid = i2c_get_clientdata(client);
688 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
692 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
695 ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
701 static int i2c_hid_hidinput_input_event(struct input_dev *dev,
702 unsigned int type, unsigned int code, int value)
704 struct hid_device *hid = input_get_drvdata(dev);
705 struct hid_field *field;
709 return input_ff_event(dev, type, code, value);
714 offset = hidinput_find_field(hid, type, code, &field);
717 hid_warn(dev, "event field not found\n");
721 return hid_set_field(field, offset, value);
724 static struct hid_ll_driver i2c_hid_ll_driver = {
725 .parse = i2c_hid_parse,
726 .start = i2c_hid_start,
727 .stop = i2c_hid_stop,
728 .open = i2c_hid_open,
729 .close = i2c_hid_close,
730 .power = i2c_hid_power,
731 .hidinput_input_event = i2c_hid_hidinput_input_event,
734 static int __devinit i2c_hid_init_irq(struct i2c_client *client)
736 struct i2c_hid *ihid = i2c_get_clientdata(client);
739 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
741 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
742 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
745 dev_warn(&client->dev,
746 "Could not register for %s interrupt, irq = %d,"
748 client->name, client->irq, ret);
756 static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
758 struct i2c_client *client = ihid->client;
759 struct i2c_hid_desc *hdesc = &ihid->hdesc;
763 /* Fetch the length of HID description, retrieve the 4 first bytes:
764 * bytes 0-1 -> length
765 * bytes 2-3 -> bcdVersion (has to be 1.00) */
766 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
768 i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
769 __func__, 4, ihid->hdesc_buffer);
772 dev_err(&client->dev,
773 "unable to fetch the size of HID descriptor (ret=%d)\n",
778 dsize = le16_to_cpu(hdesc->wHIDDescLength);
780 * the size of the HID descriptor should at least contain
781 * its size and the bcdVersion (4 bytes), and should not be greater
782 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
783 * through i2c_hid_command.
785 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
786 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
791 /* check bcdVersion == 1.0 */
792 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
793 dev_err(&client->dev,
794 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
795 le16_to_cpu(hdesc->bcdVersion));
799 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
801 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
804 dev_err(&client->dev, "hid_descr_cmd Fail\n");
808 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
813 static int __devinit i2c_hid_probe(struct i2c_client *client,
814 const struct i2c_device_id *dev_id)
817 struct i2c_hid *ihid;
818 struct hid_device *hid;
820 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
822 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
824 if (!platform_data) {
825 dev_err(&client->dev, "HID register address not provided\n");
830 dev_err(&client->dev,
831 "HID over i2c has not been provided an Int IRQ\n");
835 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
839 i2c_set_clientdata(client, ihid);
841 ihid->client = client;
843 hidRegister = platform_data->hid_descriptor_address;
844 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
846 init_waitqueue_head(&ihid->wait);
848 /* we need to allocate the command buffer without knowing the maximum
849 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
850 * real computation later. */
851 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
855 ret = i2c_hid_fetch_hid_descriptor(ihid);
859 ret = i2c_hid_init_irq(client);
863 hid = hid_allocate_device();
871 hid->driver_data = client;
872 hid->ll_driver = &i2c_hid_ll_driver;
873 hid->hid_get_raw_report = i2c_hid_get_raw_report;
874 hid->hid_output_raw_report = i2c_hid_output_raw_report;
875 hid->dev.parent = &client->dev;
877 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
878 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
879 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
881 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
882 client->name, hid->vendor, hid->product);
884 ret = hid_add_device(hid);
887 hid_err(client, "can't add hid device: %d\n", ret);
894 hid_destroy_device(hid);
897 free_irq(client->irq, ihid);
900 i2c_hid_free_buffers(ihid);
905 static int __devexit i2c_hid_remove(struct i2c_client *client)
907 struct i2c_hid *ihid = i2c_get_clientdata(client);
908 struct hid_device *hid;
911 hid_destroy_device(hid);
913 free_irq(client->irq, ihid);
916 i2c_hid_free_buffers(ihid);
923 #ifdef CONFIG_PM_SLEEP
924 static int i2c_hid_suspend(struct device *dev)
926 struct i2c_client *client = to_i2c_client(dev);
928 if (device_may_wakeup(&client->dev))
929 enable_irq_wake(client->irq);
931 /* Save some power */
932 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
937 static int i2c_hid_resume(struct device *dev)
940 struct i2c_client *client = to_i2c_client(dev);
942 ret = i2c_hid_hwreset(client);
946 if (device_may_wakeup(&client->dev))
947 disable_irq_wake(client->irq);
953 static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
955 static const struct i2c_device_id i2c_hid_id_table[] = {
959 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
962 static struct i2c_driver i2c_hid_driver = {
965 .owner = THIS_MODULE,
969 .probe = i2c_hid_probe,
970 .remove = __devexit_p(i2c_hid_remove),
972 .id_table = i2c_hid_id_table,
975 module_i2c_driver(i2c_hid_driver);
977 MODULE_DESCRIPTION("HID over I2C core driver");
978 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
979 MODULE_LICENSE("GPL");