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 int irq; /* the interrupt line irq */
140 wait_queue_head_t wait; /* For waiting the interrupt */
143 static int __i2c_hid_command(struct i2c_client *client,
144 const struct i2c_hid_cmd *command, u8 reportID,
145 u8 reportType, u8 *args, int args_len,
146 unsigned char *buf_recv, int data_len)
148 struct i2c_hid *ihid = i2c_get_clientdata(client);
149 union command *cmd = (union command *)ihid->cmdbuf;
151 struct i2c_msg msg[2];
154 int length = command->length;
155 bool wait = command->wait;
156 unsigned int registerIndex = command->registerIndex;
158 /* special case for hid_descr_cmd */
159 if (command == &hid_descr_cmd) {
160 cmd->c.reg = ihid->wHIDDescRegister;
162 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
163 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
167 cmd->c.opcode = command->opcode;
168 cmd->c.reportTypeID = reportID | reportType << 4;
171 memcpy(cmd->data + length, args, args_len);
174 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
176 msg[0].addr = client->addr;
177 msg[0].flags = client->flags & I2C_M_TEN;
179 msg[0].buf = cmd->data;
181 msg[1].addr = client->addr;
182 msg[1].flags = client->flags & I2C_M_TEN;
183 msg[1].flags |= I2C_M_RD;
184 msg[1].len = data_len;
185 msg[1].buf = buf_recv;
187 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
191 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
193 ret = i2c_transfer(client->adapter, msg, msg_num);
196 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
199 return ret < 0 ? ret : -EIO;
204 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
205 if (!wait_event_timeout(ihid->wait,
206 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
207 msecs_to_jiffies(5000)))
209 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
215 static int i2c_hid_command(struct i2c_client *client,
216 const struct i2c_hid_cmd *command,
217 unsigned char *buf_recv, int data_len)
219 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
223 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
224 u8 reportID, unsigned char *buf_recv, int data_len)
226 struct i2c_hid *ihid = i2c_get_clientdata(client);
230 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
232 i2c_hid_dbg(ihid, "%s\n", __func__);
234 if (reportID >= 0x0F) {
235 args[args_len++] = reportID;
239 args[args_len++] = readRegister & 0xFF;
240 args[args_len++] = readRegister >> 8;
242 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
243 reportType, args, args_len, buf_recv, data_len);
245 dev_err(&client->dev,
246 "failed to retrieve report from device.\n");
253 static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
254 u8 reportID, unsigned char *buf, size_t data_len)
256 struct i2c_hid *ihid = i2c_get_clientdata(client);
257 u8 *args = ihid->argsbuf;
259 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
261 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
262 u16 size = 2 /* size */ +
263 (reportID ? 1 : 0) /* reportID */ +
265 int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
266 2 /* dataRegister */ +
270 i2c_hid_dbg(ihid, "%s\n", __func__);
272 if (reportID >= 0x0F) {
273 args[index++] = reportID;
277 args[index++] = dataRegister & 0xFF;
278 args[index++] = dataRegister >> 8;
280 args[index++] = size & 0xFF;
281 args[index++] = size >> 8;
284 args[index++] = reportID;
286 memcpy(&args[index], buf, data_len);
288 ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
289 reportType, args, args_len, NULL, 0);
291 dev_err(&client->dev, "failed to set a report to device.\n");
298 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
300 struct i2c_hid *ihid = i2c_get_clientdata(client);
303 i2c_hid_dbg(ihid, "%s\n", __func__);
305 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
306 0, NULL, 0, NULL, 0);
308 dev_err(&client->dev, "failed to change power setting.\n");
313 static int i2c_hid_hwreset(struct i2c_client *client)
315 struct i2c_hid *ihid = i2c_get_clientdata(client);
318 i2c_hid_dbg(ihid, "%s\n", __func__);
320 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
324 i2c_hid_dbg(ihid, "resetting...\n");
326 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
328 dev_err(&client->dev, "failed to reset device.\n");
329 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
336 static void i2c_hid_get_input(struct i2c_hid *ihid)
339 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
341 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
346 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
347 __func__, ret, size);
351 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
354 /* host or device initiated RESET completed */
355 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
356 wake_up(&ihid->wait);
360 if (ret_size > size) {
361 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
362 __func__, size, ret_size);
366 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
368 if (test_bit(I2C_HID_STARTED, &ihid->flags))
369 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
375 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
377 struct i2c_hid *ihid = dev_id;
379 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
382 i2c_hid_get_input(ihid);
387 static int i2c_hid_get_report_length(struct hid_report *report)
389 return ((report->size - 1) >> 3) + 1 +
390 report->device->report_enum[report->type].numbered + 2;
393 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
396 struct hid_device *hid = report->device;
397 struct i2c_client *client = hid->driver_data;
398 struct i2c_hid *ihid = i2c_get_clientdata(client);
399 unsigned int size, ret_size;
401 size = i2c_hid_get_report_length(report);
402 if (i2c_hid_get_report(client,
403 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
404 report->id, buffer, size))
407 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
409 ret_size = buffer[0] | (buffer[1] << 8);
411 if (ret_size != size) {
412 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
413 __func__, size, ret_size);
417 /* hid->driver_lock is held as we are in probe function,
418 * we just need to setup the input fields, so using
419 * hid_report_raw_event is safe. */
420 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
424 * Initialize all reports
426 static void i2c_hid_init_reports(struct hid_device *hid)
428 struct hid_report *report;
429 struct i2c_client *client = hid->driver_data;
430 struct i2c_hid *ihid = i2c_get_clientdata(client);
431 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
434 dev_err(&client->dev, "can not retrieve initial reports\n");
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 return hid_set_field(field, offset, value);
723 static struct hid_ll_driver i2c_hid_ll_driver = {
724 .parse = i2c_hid_parse,
725 .start = i2c_hid_start,
726 .stop = i2c_hid_stop,
727 .open = i2c_hid_open,
728 .close = i2c_hid_close,
729 .power = i2c_hid_power,
730 .hidinput_input_event = i2c_hid_hidinput_input_event,
733 static int __devinit i2c_hid_init_irq(struct i2c_client *client)
735 struct i2c_hid *ihid = i2c_get_clientdata(client);
738 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
740 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
741 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
744 dev_warn(&client->dev,
745 "Could not register for %s interrupt, irq = %d,"
747 client->name, client->irq, ret);
752 ihid->irq = client->irq;
757 static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
759 struct i2c_client *client = ihid->client;
760 struct i2c_hid_desc *hdesc = &ihid->hdesc;
764 /* Fetch the length of HID description, retrieve the 4 first bytes:
765 * bytes 0-1 -> length
766 * bytes 2-3 -> bcdVersion (has to be 1.00) */
767 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
769 i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
770 __func__, 4, ihid->hdesc_buffer);
773 dev_err(&client->dev,
774 "unable to fetch the size of HID descriptor (ret=%d)\n",
779 dsize = le16_to_cpu(hdesc->wHIDDescLength);
781 * the size of the HID descriptor should at least contain
782 * its size and the bcdVersion (4 bytes), and should not be greater
783 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
784 * through i2c_hid_command.
786 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
787 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
792 /* check bcdVersion == 1.0 */
793 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
794 dev_err(&client->dev,
795 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
796 le16_to_cpu(hdesc->bcdVersion));
800 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
802 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
805 dev_err(&client->dev, "hid_descr_cmd Fail\n");
809 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
814 static int __devinit i2c_hid_probe(struct i2c_client *client,
815 const struct i2c_device_id *dev_id)
818 struct i2c_hid *ihid;
819 struct hid_device *hid;
821 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
823 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
825 if (!platform_data) {
826 dev_err(&client->dev, "HID register address not provided\n");
831 dev_err(&client->dev,
832 "HID over i2c has not been provided an Int IRQ\n");
836 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
840 i2c_set_clientdata(client, ihid);
842 ihid->client = client;
844 hidRegister = platform_data->hid_descriptor_address;
845 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
847 init_waitqueue_head(&ihid->wait);
849 /* we need to allocate the command buffer without knowing the maximum
850 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
851 * real computation later. */
852 ihid->bufsize = HID_MIN_BUFFER_SIZE;
853 i2c_hid_alloc_buffers(ihid);
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);
898 free_irq(ihid->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);
927 struct i2c_hid *ihid = i2c_get_clientdata(client);
929 if (device_may_wakeup(&client->dev))
930 enable_irq_wake(ihid->irq);
932 /* Save some power */
933 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
938 static int i2c_hid_resume(struct device *dev)
941 struct i2c_client *client = to_i2c_client(dev);
943 ret = i2c_hid_hwreset(client);
947 if (device_may_wakeup(&client->dev))
948 disable_irq_wake(client->irq);
954 static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
956 static const struct i2c_device_id i2c_hid_id_table[] = {
960 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
963 static struct i2c_driver i2c_hid_driver = {
966 .owner = THIS_MODULE,
970 .probe = i2c_hid_probe,
971 .remove = __devexit_p(i2c_hid_remove),
973 .id_table = i2c_hid_id_table,
976 module_i2c_driver(i2c_hid_driver);
978 MODULE_DESCRIPTION("HID over I2C core driver");
979 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
980 MODULE_LICENSE("GPL");