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/pm_runtime.h>
29 #include <linux/device.h>
30 #include <linux/wait.h>
31 #include <linux/err.h>
32 #include <linux/string.h>
33 #include <linux/list.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/hid.h>
37 #include <linux/mutex.h>
38 #include <linux/acpi.h>
41 #include <linux/i2c/i2c-hid.h>
44 #define I2C_HID_STARTED (1 << 0)
45 #define I2C_HID_RESET_PENDING (1 << 1)
46 #define I2C_HID_READ_PENDING (1 << 2)
48 #define I2C_HID_PWR_ON 0x00
49 #define I2C_HID_PWR_SLEEP 0x01
53 module_param(debug, bool, 0444);
54 MODULE_PARM_DESC(debug, "print a lot of debug information");
56 #define i2c_hid_dbg(ihid, fmt, arg...) \
59 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
63 __le16 wHIDDescLength;
65 __le16 wReportDescLength;
66 __le16 wReportDescRegister;
67 __le16 wInputRegister;
68 __le16 wMaxInputLength;
69 __le16 wOutputRegister;
70 __le16 wMaxOutputLength;
71 __le16 wCommandRegister;
80 unsigned int registerIndex;
95 #define I2C_HID_CMD(opcode_) \
96 .opcode = opcode_, .length = 4, \
97 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
99 /* fetch HID descriptor */
100 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
101 /* fetch report descriptors */
102 static const struct i2c_hid_cmd hid_report_descr_cmd = {
103 .registerIndex = offsetof(struct i2c_hid_desc,
104 wReportDescRegister),
108 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
110 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
111 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
112 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
113 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
116 * These definitions are not used here, but are defined by the spec.
117 * Keeping them here for documentation purposes.
119 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
120 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
121 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
122 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
125 static DEFINE_MUTEX(i2c_hid_open_mut);
127 /* The main device structure */
129 struct i2c_client *client; /* i2c client */
130 struct hid_device *hid; /* pointer to corresponding HID dev */
132 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
133 struct i2c_hid_desc hdesc; /* the HID Descriptor */
135 __le16 wHIDDescRegister; /* location of the i2c
136 * register of the HID
138 unsigned int bufsize; /* i2c buffer size */
139 char *inbuf; /* Input buffer */
140 char *cmdbuf; /* Command buffer */
141 char *argsbuf; /* Command arguments buffer */
143 unsigned long flags; /* device flags */
145 wait_queue_head_t wait; /* For waiting the interrupt */
147 struct i2c_hid_platform_data pdata;
150 static int __i2c_hid_command(struct i2c_client *client,
151 const struct i2c_hid_cmd *command, u8 reportID,
152 u8 reportType, u8 *args, int args_len,
153 unsigned char *buf_recv, int data_len)
155 struct i2c_hid *ihid = i2c_get_clientdata(client);
156 union command *cmd = (union command *)ihid->cmdbuf;
158 struct i2c_msg msg[2];
161 int length = command->length;
162 bool wait = command->wait;
163 unsigned int registerIndex = command->registerIndex;
165 /* special case for hid_descr_cmd */
166 if (command == &hid_descr_cmd) {
167 cmd->c.reg = ihid->wHIDDescRegister;
169 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
170 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
174 cmd->c.opcode = command->opcode;
175 cmd->c.reportTypeID = reportID | reportType << 4;
178 memcpy(cmd->data + length, args, args_len);
181 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
183 msg[0].addr = client->addr;
184 msg[0].flags = client->flags & I2C_M_TEN;
186 msg[0].buf = cmd->data;
188 msg[1].addr = client->addr;
189 msg[1].flags = client->flags & I2C_M_TEN;
190 msg[1].flags |= I2C_M_RD;
191 msg[1].len = data_len;
192 msg[1].buf = buf_recv;
194 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
198 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
200 ret = i2c_transfer(client->adapter, msg, msg_num);
203 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
206 return ret < 0 ? ret : -EIO;
211 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
212 if (!wait_event_timeout(ihid->wait,
213 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
214 msecs_to_jiffies(5000)))
216 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
222 static int i2c_hid_command(struct i2c_client *client,
223 const struct i2c_hid_cmd *command,
224 unsigned char *buf_recv, int data_len)
226 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
230 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
231 u8 reportID, unsigned char *buf_recv, int data_len)
233 struct i2c_hid *ihid = i2c_get_clientdata(client);
237 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
239 i2c_hid_dbg(ihid, "%s\n", __func__);
241 if (reportID >= 0x0F) {
242 args[args_len++] = reportID;
246 args[args_len++] = readRegister & 0xFF;
247 args[args_len++] = readRegister >> 8;
249 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
250 reportType, args, args_len, buf_recv, data_len);
252 dev_err(&client->dev,
253 "failed to retrieve report from device.\n");
261 * i2c_hid_set_or_send_report: forward an incoming report to the device
262 * @client: the i2c_client of the device
263 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
264 * @reportID: the report ID
265 * @buf: the actual data to transfer, without the report ID
267 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
269 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
270 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
272 struct i2c_hid *ihid = i2c_get_clientdata(client);
273 u8 *args = ihid->argsbuf;
274 const struct i2c_hid_cmd *hidcmd;
276 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
277 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
278 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
280 /* hid_hw_* already checked that data_len < HID_MAX_BUFFER_SIZE */
281 u16 size = 2 /* size */ +
282 (reportID ? 1 : 0) /* reportID */ +
284 int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
285 2 /* dataRegister */ +
289 i2c_hid_dbg(ihid, "%s\n", __func__);
291 if (!use_data && maxOutputLength == 0)
294 if (reportID >= 0x0F) {
295 args[index++] = reportID;
300 * use the data register for feature reports or if the device does not
301 * support the output register
304 args[index++] = dataRegister & 0xFF;
305 args[index++] = dataRegister >> 8;
306 hidcmd = &hid_set_report_cmd;
308 args[index++] = outputRegister & 0xFF;
309 args[index++] = outputRegister >> 8;
310 hidcmd = &hid_no_cmd;
313 args[index++] = size & 0xFF;
314 args[index++] = size >> 8;
317 args[index++] = reportID;
319 memcpy(&args[index], buf, data_len);
321 ret = __i2c_hid_command(client, hidcmd, reportID,
322 reportType, args, args_len, NULL, 0);
324 dev_err(&client->dev, "failed to set a report to device.\n");
331 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
333 struct i2c_hid *ihid = i2c_get_clientdata(client);
336 i2c_hid_dbg(ihid, "%s\n", __func__);
338 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
339 0, NULL, 0, NULL, 0);
341 dev_err(&client->dev, "failed to change power setting.\n");
346 static int i2c_hid_hwreset(struct i2c_client *client)
348 struct i2c_hid *ihid = i2c_get_clientdata(client);
351 i2c_hid_dbg(ihid, "%s\n", __func__);
353 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
357 i2c_hid_dbg(ihid, "resetting...\n");
359 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
361 dev_err(&client->dev, "failed to reset device.\n");
362 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
369 static void i2c_hid_get_input(struct i2c_hid *ihid)
372 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
374 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
379 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
380 __func__, ret, size);
384 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
387 /* host or device initiated RESET completed */
388 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
389 wake_up(&ihid->wait);
393 if (ret_size > size) {
394 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
395 __func__, size, ret_size);
399 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
401 if (test_bit(I2C_HID_STARTED, &ihid->flags))
402 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
408 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
410 struct i2c_hid *ihid = dev_id;
412 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
415 i2c_hid_get_input(ihid);
420 static int i2c_hid_get_report_length(struct hid_report *report)
422 return ((report->size - 1) >> 3) + 1 +
423 report->device->report_enum[report->type].numbered + 2;
426 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
429 struct hid_device *hid = report->device;
430 struct i2c_client *client = hid->driver_data;
431 struct i2c_hid *ihid = i2c_get_clientdata(client);
432 unsigned int size, ret_size;
434 size = i2c_hid_get_report_length(report);
435 if (i2c_hid_get_report(client,
436 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
437 report->id, buffer, size))
440 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
442 ret_size = buffer[0] | (buffer[1] << 8);
444 if (ret_size != size) {
445 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
446 __func__, size, ret_size);
450 /* hid->driver_lock is held as we are in probe function,
451 * we just need to setup the input fields, so using
452 * hid_report_raw_event is safe. */
453 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
457 * Initialize all reports
459 static void i2c_hid_init_reports(struct hid_device *hid)
461 struct hid_report *report;
462 struct i2c_client *client = hid->driver_data;
463 struct i2c_hid *ihid = i2c_get_clientdata(client);
464 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
467 dev_err(&client->dev, "can not retrieve initial reports\n");
472 * The device must be powered on while we fetch initial reports
475 pm_runtime_get_sync(&client->dev);
477 list_for_each_entry(report,
478 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
479 i2c_hid_init_report(report, inbuf, ihid->bufsize);
481 pm_runtime_put(&client->dev);
487 * Traverse the supplied list of reports and find the longest
489 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
492 struct hid_report *report;
495 /* We should not rely on wMaxInputLength, as some devices may set it to
497 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
498 size = i2c_hid_get_report_length(report);
504 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
507 kfree(ihid->argsbuf);
511 ihid->argsbuf = NULL;
515 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
517 /* the worst case is computed from the set_report command with a
518 * reportID > 15 and the maximum report length */
519 int args_len = sizeof(__u8) + /* optional ReportID byte */
520 sizeof(__u16) + /* data register */
521 sizeof(__u16) + /* size of the report */
522 report_size; /* report */
524 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
525 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
526 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
528 if (!ihid->inbuf || !ihid->argsbuf || !ihid->cmdbuf) {
529 i2c_hid_free_buffers(ihid);
533 ihid->bufsize = report_size;
538 static int i2c_hid_get_raw_report(struct hid_device *hid,
539 unsigned char report_number, __u8 *buf, size_t count,
540 unsigned char report_type)
542 struct i2c_client *client = hid->driver_data;
543 struct i2c_hid *ihid = i2c_get_clientdata(client);
544 size_t ret_count, ask_count;
547 if (report_type == HID_OUTPUT_REPORT)
550 /* +2 bytes to include the size of the reply in the query buffer */
551 ask_count = min(count + 2, (size_t)ihid->bufsize);
553 ret = i2c_hid_get_report(client,
554 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
555 report_number, ihid->inbuf, ask_count);
560 ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
565 ret_count = min(ret_count, ask_count);
567 /* The query buffer contains the size, dropping it in the reply */
568 count = min(count, ret_count - 2);
569 memcpy(buf, ihid->inbuf + 2, count);
574 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
575 size_t count, unsigned char report_type, bool use_data)
577 struct i2c_client *client = hid->driver_data;
578 int report_id = buf[0];
581 if (report_type == HID_INPUT_REPORT)
589 ret = i2c_hid_set_or_send_report(client,
590 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
591 report_id, buf, count, use_data);
593 if (report_id && ret >= 0)
594 ret++; /* add report_id to the number of transfered bytes */
599 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
602 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
606 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
607 __u8 *buf, size_t len, unsigned char rtype,
611 case HID_REQ_GET_REPORT:
612 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
613 case HID_REQ_SET_REPORT:
614 if (buf[0] != reportnum)
616 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
622 static int i2c_hid_parse(struct hid_device *hid)
624 struct i2c_client *client = hid->driver_data;
625 struct i2c_hid *ihid = i2c_get_clientdata(client);
626 struct i2c_hid_desc *hdesc = &ihid->hdesc;
632 i2c_hid_dbg(ihid, "entering %s\n", __func__);
634 rsize = le16_to_cpu(hdesc->wReportDescLength);
635 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
636 dbg_hid("weird size of report descriptor (%u)\n", rsize);
641 ret = i2c_hid_hwreset(client);
644 } while (tries-- > 0 && ret);
649 rdesc = kzalloc(rsize, GFP_KERNEL);
652 dbg_hid("couldn't allocate rdesc memory\n");
656 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
658 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
660 hid_err(hid, "reading report descriptor failed\n");
665 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
667 ret = hid_parse_report(hid, rdesc, rsize);
670 dbg_hid("parsing report descriptor failed\n");
677 static int i2c_hid_start(struct hid_device *hid)
679 struct i2c_client *client = hid->driver_data;
680 struct i2c_hid *ihid = i2c_get_clientdata(client);
682 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
684 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
685 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
686 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
688 if (bufsize > ihid->bufsize) {
689 i2c_hid_free_buffers(ihid);
691 ret = i2c_hid_alloc_buffers(ihid, bufsize);
697 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
698 i2c_hid_init_reports(hid);
703 static void i2c_hid_stop(struct hid_device *hid)
705 struct i2c_client *client = hid->driver_data;
706 struct i2c_hid *ihid = i2c_get_clientdata(client);
710 i2c_hid_free_buffers(ihid);
713 static int i2c_hid_open(struct hid_device *hid)
715 struct i2c_client *client = hid->driver_data;
716 struct i2c_hid *ihid = i2c_get_clientdata(client);
719 mutex_lock(&i2c_hid_open_mut);
721 ret = pm_runtime_get_sync(&client->dev);
726 set_bit(I2C_HID_STARTED, &ihid->flags);
729 mutex_unlock(&i2c_hid_open_mut);
730 return ret < 0 ? ret : 0;
733 static void i2c_hid_close(struct hid_device *hid)
735 struct i2c_client *client = hid->driver_data;
736 struct i2c_hid *ihid = i2c_get_clientdata(client);
738 /* protecting hid->open to make sure we don't restart
739 * data acquistion due to a resumption we no longer
742 mutex_lock(&i2c_hid_open_mut);
744 clear_bit(I2C_HID_STARTED, &ihid->flags);
746 /* Save some power */
747 pm_runtime_put(&client->dev);
749 mutex_unlock(&i2c_hid_open_mut);
752 static int i2c_hid_power(struct hid_device *hid, int lvl)
754 struct i2c_client *client = hid->driver_data;
755 struct i2c_hid *ihid = i2c_get_clientdata(client);
757 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
761 pm_runtime_get_sync(&client->dev);
764 pm_runtime_put(&client->dev);
770 static struct hid_ll_driver i2c_hid_ll_driver = {
771 .parse = i2c_hid_parse,
772 .start = i2c_hid_start,
773 .stop = i2c_hid_stop,
774 .open = i2c_hid_open,
775 .close = i2c_hid_close,
776 .power = i2c_hid_power,
777 .output_report = i2c_hid_output_report,
778 .raw_request = i2c_hid_raw_request,
781 static int i2c_hid_init_irq(struct i2c_client *client)
783 struct i2c_hid *ihid = i2c_get_clientdata(client);
786 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
788 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
789 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
792 dev_warn(&client->dev,
793 "Could not register for %s interrupt, irq = %d,"
795 client->name, client->irq, ret);
803 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
805 struct i2c_client *client = ihid->client;
806 struct i2c_hid_desc *hdesc = &ihid->hdesc;
810 /* Fetch the length of HID description, retrieve the 4 first bytes:
811 * bytes 0-1 -> length
812 * bytes 2-3 -> bcdVersion (has to be 1.00) */
813 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
815 i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %4ph\n", __func__,
819 dev_err(&client->dev,
820 "unable to fetch the size of HID descriptor (ret=%d)\n",
825 dsize = le16_to_cpu(hdesc->wHIDDescLength);
827 * the size of the HID descriptor should at least contain
828 * its size and the bcdVersion (4 bytes), and should not be greater
829 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
830 * through i2c_hid_command.
832 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
833 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
838 /* check bcdVersion == 1.0 */
839 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
840 dev_err(&client->dev,
841 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
842 le16_to_cpu(hdesc->bcdVersion));
846 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
848 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
851 dev_err(&client->dev, "hid_descr_cmd Fail\n");
855 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
861 static int i2c_hid_acpi_pdata(struct i2c_client *client,
862 struct i2c_hid_platform_data *pdata)
864 static u8 i2c_hid_guid[] = {
865 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
866 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
868 union acpi_object *obj;
869 struct acpi_device *adev;
872 handle = ACPI_HANDLE(&client->dev);
873 if (!handle || acpi_bus_get_device(handle, &adev))
876 obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL,
879 dev_err(&client->dev, "device _DSM execution failed\n");
883 pdata->hid_descriptor_address = obj->integer.value;
889 static const struct acpi_device_id i2c_hid_acpi_match[] = {
894 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
896 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
897 struct i2c_hid_platform_data *pdata)
904 static int i2c_hid_of_probe(struct i2c_client *client,
905 struct i2c_hid_platform_data *pdata)
907 struct device *dev = &client->dev;
911 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
913 dev_err(&client->dev, "HID register address not provided\n");
917 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
921 pdata->hid_descriptor_address = val;
926 static const struct of_device_id i2c_hid_of_match[] = {
927 { .compatible = "hid-over-i2c" },
930 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
932 static inline int i2c_hid_of_probe(struct i2c_client *client,
933 struct i2c_hid_platform_data *pdata)
939 static int i2c_hid_probe(struct i2c_client *client,
940 const struct i2c_device_id *dev_id)
943 struct i2c_hid *ihid;
944 struct hid_device *hid;
946 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
948 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
951 dev_err(&client->dev,
952 "HID over i2c has not been provided an Int IRQ\n");
956 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
960 if (client->dev.of_node) {
961 ret = i2c_hid_of_probe(client, &ihid->pdata);
964 } else if (!platform_data) {
965 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
967 dev_err(&client->dev,
968 "HID register address not provided\n");
972 ihid->pdata = *platform_data;
975 i2c_set_clientdata(client, ihid);
977 ihid->client = client;
979 hidRegister = ihid->pdata.hid_descriptor_address;
980 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
982 init_waitqueue_head(&ihid->wait);
984 /* we need to allocate the command buffer without knowing the maximum
985 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
986 * real computation later. */
987 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
991 pm_runtime_get_noresume(&client->dev);
992 pm_runtime_set_active(&client->dev);
993 pm_runtime_enable(&client->dev);
995 ret = i2c_hid_fetch_hid_descriptor(ihid);
999 ret = i2c_hid_init_irq(client);
1003 hid = hid_allocate_device();
1011 hid->driver_data = client;
1012 hid->ll_driver = &i2c_hid_ll_driver;
1013 hid->dev.parent = &client->dev;
1014 ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
1016 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1017 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1018 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1020 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1021 client->name, hid->vendor, hid->product);
1023 ret = hid_add_device(hid);
1026 hid_err(client, "can't add hid device: %d\n", ret);
1030 pm_runtime_put(&client->dev);
1034 hid_destroy_device(hid);
1037 free_irq(client->irq, ihid);
1040 pm_runtime_put_noidle(&client->dev);
1041 pm_runtime_disable(&client->dev);
1044 i2c_hid_free_buffers(ihid);
1049 static int i2c_hid_remove(struct i2c_client *client)
1051 struct i2c_hid *ihid = i2c_get_clientdata(client);
1052 struct hid_device *hid;
1054 pm_runtime_get_sync(&client->dev);
1055 pm_runtime_disable(&client->dev);
1056 pm_runtime_set_suspended(&client->dev);
1057 pm_runtime_put_noidle(&client->dev);
1060 hid_destroy_device(hid);
1062 free_irq(client->irq, ihid);
1065 i2c_hid_free_buffers(ihid);
1072 #ifdef CONFIG_PM_SLEEP
1073 static int i2c_hid_suspend(struct device *dev)
1075 struct i2c_client *client = to_i2c_client(dev);
1077 disable_irq(client->irq);
1078 if (device_may_wakeup(&client->dev))
1079 enable_irq_wake(client->irq);
1081 /* Save some power */
1082 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1087 static int i2c_hid_resume(struct device *dev)
1090 struct i2c_client *client = to_i2c_client(dev);
1092 enable_irq(client->irq);
1093 ret = i2c_hid_hwreset(client);
1097 if (device_may_wakeup(&client->dev))
1098 disable_irq_wake(client->irq);
1104 #ifdef CONFIG_PM_RUNTIME
1105 static int i2c_hid_runtime_suspend(struct device *dev)
1107 struct i2c_client *client = to_i2c_client(dev);
1109 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1110 disable_irq(client->irq);
1114 static int i2c_hid_runtime_resume(struct device *dev)
1116 struct i2c_client *client = to_i2c_client(dev);
1118 enable_irq(client->irq);
1119 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1124 static const struct dev_pm_ops i2c_hid_pm = {
1125 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1126 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1130 static const struct i2c_device_id i2c_hid_id_table[] = {
1134 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1137 static struct i2c_driver i2c_hid_driver = {
1140 .owner = THIS_MODULE,
1142 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1143 .of_match_table = of_match_ptr(i2c_hid_of_match),
1146 .probe = i2c_hid_probe,
1147 .remove = i2c_hid_remove,
1149 .id_table = i2c_hid_id_table,
1152 module_i2c_driver(i2c_hid_driver);
1154 MODULE_DESCRIPTION("HID over I2C core driver");
1155 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1156 MODULE_LICENSE("GPL");