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>
40 #include <linux/gpio/consumer.h>
42 #include <linux/notifier.h>
43 #include <linux/rk_keys.h>
45 #include <linux/i2c/i2c-hid.h>
48 #define I2C_HID_STARTED 0
49 #define I2C_HID_RESET_PENDING 1
50 #define I2C_HID_READ_PENDING 2
52 #define I2C_HID_PWR_ON 0x00
53 #define I2C_HID_PWR_SLEEP 0x01
57 module_param(debug, bool, 0444);
58 MODULE_PARM_DESC(debug, "print a lot of debug information");
60 #define i2c_hid_dbg(ihid, fmt, arg...) \
63 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
67 __le16 wHIDDescLength;
69 __le16 wReportDescLength;
70 __le16 wReportDescRegister;
71 __le16 wInputRegister;
72 __le16 wMaxInputLength;
73 __le16 wOutputRegister;
74 __le16 wMaxOutputLength;
75 __le16 wCommandRegister;
84 unsigned int registerIndex;
99 #define I2C_HID_CMD(opcode_) \
100 .opcode = opcode_, .length = 4, \
101 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
103 /* fetch HID descriptor */
104 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
105 /* fetch report descriptors */
106 static const struct i2c_hid_cmd hid_report_descr_cmd = {
107 .registerIndex = offsetof(struct i2c_hid_desc,
108 wReportDescRegister),
112 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
114 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
115 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
116 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
117 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
120 * These definitions are not used here, but are defined by the spec.
121 * Keeping them here for documentation purposes.
123 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
124 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
125 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
126 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
129 static DEFINE_MUTEX(i2c_hid_open_mut);
131 /* The main device structure */
133 struct i2c_client *client; /* i2c client */
134 struct hid_device *hid; /* pointer to corresponding HID dev */
136 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
137 struct i2c_hid_desc hdesc; /* the HID Descriptor */
139 __le16 wHIDDescRegister; /* location of the i2c
140 * register of the HID
142 unsigned int bufsize; /* i2c buffer size */
143 char *inbuf; /* Input buffer */
144 char *rawbuf; /* Raw Input buffer */
145 char *cmdbuf; /* Command buffer */
146 char *argsbuf; /* Command arguments buffer */
148 unsigned long flags; /* device flags */
150 wait_queue_head_t wait; /* For waiting the interrupt */
151 struct gpio_desc *desc;
154 struct i2c_hid_platform_data pdata;
156 bool irq_wake_enabled;
158 struct notifier_block fb_notif;
162 static int ihid_fb_notifier_callback(struct notifier_block *self,
163 unsigned long action, void *data)
165 struct i2c_hid *ihid;
166 struct fb_event *event = data;
168 ihid = container_of(self, struct i2c_hid, fb_notif);
170 if (action == FB_EARLY_EVENT_BLANK) {
171 switch (*((int *)event->data)) {
172 case FB_BLANK_UNBLANK:
175 ihid->is_suspend = 1;
178 } else if (action == FB_EVENT_BLANK) {
179 switch (*((int *)event->data)) {
180 case FB_BLANK_UNBLANK:
181 ihid->is_suspend = 0;
191 static int __i2c_hid_command(struct i2c_client *client,
192 const struct i2c_hid_cmd *command, u8 reportID,
193 u8 reportType, u8 *args, int args_len,
194 unsigned char *buf_recv, int data_len)
196 struct i2c_hid *ihid = i2c_get_clientdata(client);
197 union command *cmd = (union command *)ihid->cmdbuf;
199 struct i2c_msg msg[2];
202 int length = command->length;
203 bool wait = command->wait;
204 unsigned int registerIndex = command->registerIndex;
206 /* special case for hid_descr_cmd */
207 if (command == &hid_descr_cmd) {
208 cmd->c.reg = ihid->wHIDDescRegister;
210 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
211 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
215 cmd->c.opcode = command->opcode;
216 cmd->c.reportTypeID = reportID | reportType << 4;
219 memcpy(cmd->data + length, args, args_len);
222 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
224 msg[0].addr = client->addr;
225 msg[0].flags = client->flags & I2C_M_TEN;
227 msg[0].buf = cmd->data;
229 msg[1].addr = client->addr;
230 msg[1].flags = client->flags & I2C_M_TEN;
231 msg[1].flags |= I2C_M_RD;
232 msg[1].len = data_len;
233 msg[1].buf = buf_recv;
235 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
239 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
241 ret = i2c_transfer(client->adapter, msg, msg_num);
244 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
247 return ret < 0 ? ret : -EIO;
252 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
253 if (!wait_event_timeout(ihid->wait,
254 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
255 msecs_to_jiffies(5000)))
257 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
263 static int i2c_hid_command(struct i2c_client *client,
264 const struct i2c_hid_cmd *command,
265 unsigned char *buf_recv, int data_len)
267 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
271 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
272 u8 reportID, unsigned char *buf_recv, int data_len)
274 struct i2c_hid *ihid = i2c_get_clientdata(client);
278 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
280 i2c_hid_dbg(ihid, "%s\n", __func__);
282 if (reportID >= 0x0F) {
283 args[args_len++] = reportID;
287 args[args_len++] = readRegister & 0xFF;
288 args[args_len++] = readRegister >> 8;
290 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
291 reportType, args, args_len, buf_recv, data_len);
293 dev_err(&client->dev,
294 "failed to retrieve report from device.\n");
302 * i2c_hid_set_or_send_report: forward an incoming report to the device
303 * @client: the i2c_client of the device
304 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
305 * @reportID: the report ID
306 * @buf: the actual data to transfer, without the report ID
308 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
310 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
311 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
313 struct i2c_hid *ihid = i2c_get_clientdata(client);
314 u8 *args = ihid->argsbuf;
315 const struct i2c_hid_cmd *hidcmd;
317 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
318 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
319 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
324 i2c_hid_dbg(ihid, "%s\n", __func__);
326 if (data_len > ihid->bufsize)
329 size = 2 /* size */ +
330 (reportID ? 1 : 0) /* reportID */ +
332 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
333 2 /* dataRegister */ +
336 if (!use_data && maxOutputLength == 0)
339 if (reportID >= 0x0F) {
340 args[index++] = reportID;
345 * use the data register for feature reports or if the device does not
346 * support the output register
349 args[index++] = dataRegister & 0xFF;
350 args[index++] = dataRegister >> 8;
351 hidcmd = &hid_set_report_cmd;
353 args[index++] = outputRegister & 0xFF;
354 args[index++] = outputRegister >> 8;
355 hidcmd = &hid_no_cmd;
358 args[index++] = size & 0xFF;
359 args[index++] = size >> 8;
362 args[index++] = reportID;
364 memcpy(&args[index], buf, data_len);
366 ret = __i2c_hid_command(client, hidcmd, reportID,
367 reportType, args, args_len, NULL, 0);
369 dev_err(&client->dev, "failed to set a report to device.\n");
376 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
378 struct i2c_hid *ihid = i2c_get_clientdata(client);
381 i2c_hid_dbg(ihid, "%s\n", __func__);
383 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
384 0, NULL, 0, NULL, 0);
386 dev_err(&client->dev, "failed to change power setting.\n");
391 static int i2c_hid_hwreset(struct i2c_client *client)
393 struct i2c_hid *ihid = i2c_get_clientdata(client);
396 i2c_hid_dbg(ihid, "%s\n", __func__);
398 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
402 i2c_hid_dbg(ihid, "resetting...\n");
404 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
406 dev_err(&client->dev, "failed to reset device.\n");
407 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
414 static void i2c_hid_get_input(struct i2c_hid *ihid)
417 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
419 if (size > ihid->bufsize)
420 size = ihid->bufsize;
422 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
427 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
428 __func__, ret, size);
432 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
435 /* host or device initiated RESET completed */
436 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
437 wake_up(&ihid->wait);
441 if (ret_size > size) {
442 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
443 __func__, size, ret_size);
447 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
449 if (test_bit(I2C_HID_STARTED, &ihid->flags))
450 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
456 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
458 struct i2c_hid *ihid = dev_id;
460 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
463 i2c_hid_get_input(ihid);
465 if (device_may_wakeup(&ihid->client->dev) && ihid->is_suspend == 1)
466 rk_send_wakeup_key();
471 static int i2c_hid_get_report_length(struct hid_report *report)
473 return ((report->size - 1) >> 3) + 1 +
474 report->device->report_enum[report->type].numbered + 2;
477 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
480 struct hid_device *hid = report->device;
481 struct i2c_client *client = hid->driver_data;
482 struct i2c_hid *ihid = i2c_get_clientdata(client);
483 unsigned int size, ret_size;
485 size = i2c_hid_get_report_length(report);
486 if (i2c_hid_get_report(client,
487 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
488 report->id, buffer, size))
491 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer);
493 ret_size = buffer[0] | (buffer[1] << 8);
495 if (ret_size != size) {
496 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
497 __func__, size, ret_size);
501 /* hid->driver_lock is held as we are in probe function,
502 * we just need to setup the input fields, so using
503 * hid_report_raw_event is safe. */
504 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
508 * Initialize all reports
510 static void i2c_hid_init_reports(struct hid_device *hid)
512 struct hid_report *report;
513 struct i2c_client *client = hid->driver_data;
514 struct i2c_hid *ihid = i2c_get_clientdata(client);
515 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
518 dev_err(&client->dev, "can not retrieve initial reports\n");
523 * The device must be powered on while we fetch initial reports
526 pm_runtime_get_sync(&client->dev);
528 list_for_each_entry(report,
529 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
530 i2c_hid_init_report(report, inbuf, ihid->bufsize);
532 pm_runtime_put(&client->dev);
538 * Traverse the supplied list of reports and find the longest
540 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
543 struct hid_report *report;
546 /* We should not rely on wMaxInputLength, as some devices may set it to
548 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
549 size = i2c_hid_get_report_length(report);
555 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
559 kfree(ihid->argsbuf);
564 ihid->argsbuf = NULL;
568 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
570 /* the worst case is computed from the set_report command with a
571 * reportID > 15 and the maximum report length */
572 int args_len = sizeof(__u8) + /* optional ReportID byte */
573 sizeof(__u16) + /* data register */
574 sizeof(__u16) + /* size of the report */
575 report_size; /* report */
577 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
578 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
579 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
580 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
582 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
583 i2c_hid_free_buffers(ihid);
587 ihid->bufsize = report_size;
592 static int i2c_hid_get_raw_report(struct hid_device *hid,
593 unsigned char report_number, __u8 *buf, size_t count,
594 unsigned char report_type)
596 struct i2c_client *client = hid->driver_data;
597 struct i2c_hid *ihid = i2c_get_clientdata(client);
598 size_t ret_count, ask_count;
601 if (report_type == HID_OUTPUT_REPORT)
604 /* +2 bytes to include the size of the reply in the query buffer */
605 ask_count = min(count + 2, (size_t)ihid->bufsize);
607 ret = i2c_hid_get_report(client,
608 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
609 report_number, ihid->rawbuf, ask_count);
614 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
619 ret_count = min(ret_count, ask_count);
621 /* The query buffer contains the size, dropping it in the reply */
622 count = min(count, ret_count - 2);
623 memcpy(buf, ihid->rawbuf + 2, count);
628 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
629 size_t count, unsigned char report_type, bool use_data)
631 struct i2c_client *client = hid->driver_data;
632 int report_id = buf[0];
635 if (report_type == HID_INPUT_REPORT)
643 ret = i2c_hid_set_or_send_report(client,
644 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
645 report_id, buf, count, use_data);
647 if (report_id && ret >= 0)
648 ret++; /* add report_id to the number of transfered bytes */
653 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
656 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
660 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
661 __u8 *buf, size_t len, unsigned char rtype,
665 case HID_REQ_GET_REPORT:
666 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
667 case HID_REQ_SET_REPORT:
668 if (buf[0] != reportnum)
670 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
676 static int i2c_hid_parse(struct hid_device *hid)
678 struct i2c_client *client = hid->driver_data;
679 struct i2c_hid *ihid = i2c_get_clientdata(client);
680 struct i2c_hid_desc *hdesc = &ihid->hdesc;
686 i2c_hid_dbg(ihid, "entering %s\n", __func__);
688 rsize = le16_to_cpu(hdesc->wReportDescLength);
689 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
690 dbg_hid("weird size of report descriptor (%u)\n", rsize);
695 ret = i2c_hid_hwreset(client);
698 } while (tries-- > 0 && ret);
703 rdesc = kzalloc(rsize, GFP_KERNEL);
706 dbg_hid("couldn't allocate rdesc memory\n");
710 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
712 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
714 hid_err(hid, "reading report descriptor failed\n");
719 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
721 ret = hid_parse_report(hid, rdesc, rsize);
724 dbg_hid("parsing report descriptor failed\n");
731 static int i2c_hid_start(struct hid_device *hid)
733 struct i2c_client *client = hid->driver_data;
734 struct i2c_hid *ihid = i2c_get_clientdata(client);
736 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
738 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
739 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
740 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
742 if (bufsize > ihid->bufsize) {
743 i2c_hid_free_buffers(ihid);
745 ret = i2c_hid_alloc_buffers(ihid, bufsize);
751 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
752 i2c_hid_init_reports(hid);
757 static void i2c_hid_stop(struct hid_device *hid)
762 static int i2c_hid_open(struct hid_device *hid)
764 struct i2c_client *client = hid->driver_data;
765 struct i2c_hid *ihid = i2c_get_clientdata(client);
768 mutex_lock(&i2c_hid_open_mut);
770 ret = pm_runtime_get_sync(&client->dev);
775 set_bit(I2C_HID_STARTED, &ihid->flags);
778 mutex_unlock(&i2c_hid_open_mut);
779 return ret < 0 ? ret : 0;
782 static void i2c_hid_close(struct hid_device *hid)
784 struct i2c_client *client = hid->driver_data;
785 struct i2c_hid *ihid = i2c_get_clientdata(client);
787 /* protecting hid->open to make sure we don't restart
788 * data acquistion due to a resumption we no longer
791 mutex_lock(&i2c_hid_open_mut);
793 clear_bit(I2C_HID_STARTED, &ihid->flags);
795 /* Save some power */
796 pm_runtime_put(&client->dev);
798 mutex_unlock(&i2c_hid_open_mut);
801 static int i2c_hid_power(struct hid_device *hid, int lvl)
803 struct i2c_client *client = hid->driver_data;
804 struct i2c_hid *ihid = i2c_get_clientdata(client);
806 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
810 pm_runtime_get_sync(&client->dev);
813 pm_runtime_put(&client->dev);
819 static struct hid_ll_driver i2c_hid_ll_driver = {
820 .parse = i2c_hid_parse,
821 .start = i2c_hid_start,
822 .stop = i2c_hid_stop,
823 .open = i2c_hid_open,
824 .close = i2c_hid_close,
825 .power = i2c_hid_power,
826 .output_report = i2c_hid_output_report,
827 .raw_request = i2c_hid_raw_request,
830 static int i2c_hid_init_irq(struct i2c_client *client)
832 struct i2c_hid *ihid = i2c_get_clientdata(client);
835 dev_dbg(&client->dev, "Requesting IRQ: %d\n", ihid->irq);
837 ret = request_threaded_irq(ihid->irq, NULL, i2c_hid_irq,
838 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
841 dev_warn(&client->dev,
842 "Could not register for %s interrupt, irq = %d,"
844 client->name, ihid->irq, ret);
852 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
854 struct i2c_client *client = ihid->client;
855 struct i2c_hid_desc *hdesc = &ihid->hdesc;
859 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
860 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
861 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
862 sizeof(struct i2c_hid_desc));
864 dev_err(&client->dev, "hid_descr_cmd failed\n");
868 /* Validate the length of HID descriptor, the 4 first bytes:
869 * bytes 0-1 -> length
870 * bytes 2-3 -> bcdVersion (has to be 1.00) */
871 /* check bcdVersion == 1.0 */
872 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
873 dev_err(&client->dev,
874 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
875 le16_to_cpu(hdesc->bcdVersion));
879 /* Descriptor length should be 30 bytes as per the specification */
880 dsize = le16_to_cpu(hdesc->wHIDDescLength);
881 if (dsize != sizeof(struct i2c_hid_desc)) {
882 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
886 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
892 /* Default GPIO mapping */
893 static const struct acpi_gpio_params i2c_hid_irq_gpio = { 0, 0, true };
894 static const struct acpi_gpio_mapping i2c_hid_acpi_gpios[] = {
895 { "gpios", &i2c_hid_irq_gpio, 1 },
899 static int i2c_hid_acpi_pdata(struct i2c_client *client,
900 struct i2c_hid_platform_data *pdata)
902 static u8 i2c_hid_guid[] = {
903 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
904 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
906 union acpi_object *obj;
907 struct acpi_device *adev;
911 handle = ACPI_HANDLE(&client->dev);
912 if (!handle || acpi_bus_get_device(handle, &adev))
915 obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL,
918 dev_err(&client->dev, "device _DSM execution failed\n");
922 pdata->hid_descriptor_address = obj->integer.value;
925 /* GPIOs are optional */
926 ret = acpi_dev_add_driver_gpios(adev, i2c_hid_acpi_gpios);
927 return ret < 0 && ret != -ENXIO ? ret : 0;
930 static const struct acpi_device_id i2c_hid_acpi_match[] = {
935 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
937 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
938 struct i2c_hid_platform_data *pdata)
945 static int i2c_hid_of_probe(struct i2c_client *client,
946 struct i2c_hid_platform_data *pdata)
948 struct device *dev = &client->dev;
952 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
954 dev_err(&client->dev, "HID register address not provided\n");
958 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
962 pdata->hid_descriptor_address = val;
967 static const struct of_device_id i2c_hid_of_match[] = {
968 { .compatible = "hid-over-i2c" },
971 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
973 static inline int i2c_hid_of_probe(struct i2c_client *client,
974 struct i2c_hid_platform_data *pdata)
980 static int i2c_hid_probe(struct i2c_client *client,
981 const struct i2c_device_id *dev_id)
984 struct i2c_hid *ihid;
985 struct hid_device *hid;
987 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
989 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
991 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
995 if (client->dev.of_node) {
996 ret = i2c_hid_of_probe(client, &ihid->pdata);
999 } else if (!platform_data) {
1000 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1002 dev_err(&client->dev,
1003 "HID register address not provided\n");
1007 ihid->pdata = *platform_data;
1010 if (client->irq > 0) {
1011 ihid->irq = client->irq;
1012 } else if (ACPI_COMPANION(&client->dev)) {
1013 ihid->desc = gpiod_get(&client->dev, NULL, GPIOD_IN);
1014 if (IS_ERR(ihid->desc)) {
1015 dev_err(&client->dev, "Failed to get GPIO interrupt\n");
1016 return PTR_ERR(ihid->desc);
1019 ihid->irq = gpiod_to_irq(ihid->desc);
1020 if (ihid->irq < 0) {
1021 gpiod_put(ihid->desc);
1022 dev_err(&client->dev, "Failed to convert GPIO to IRQ\n");
1027 i2c_set_clientdata(client, ihid);
1029 ihid->client = client;
1031 hidRegister = ihid->pdata.hid_descriptor_address;
1032 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1034 init_waitqueue_head(&ihid->wait);
1036 /* we need to allocate the command buffer without knowing the maximum
1037 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1038 * real computation later. */
1039 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1043 pm_runtime_get_noresume(&client->dev);
1044 pm_runtime_set_active(&client->dev);
1045 pm_runtime_enable(&client->dev);
1047 ret = i2c_hid_fetch_hid_descriptor(ihid);
1051 ret = i2c_hid_init_irq(client);
1055 if (client->dev.of_node) {
1056 ret = of_property_read_bool(client->dev.of_node, "hid-support-wakeup");
1058 device_init_wakeup(&client->dev, true);
1059 ihid->is_suspend = 0;
1060 ihid->fb_notif.notifier_call = ihid_fb_notifier_callback;
1061 fb_register_client(&ihid->fb_notif);
1065 hid = hid_allocate_device();
1073 hid->driver_data = client;
1074 hid->ll_driver = &i2c_hid_ll_driver;
1075 hid->dev.parent = &client->dev;
1077 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1078 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1079 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1081 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1082 client->name, hid->vendor, hid->product);
1083 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1085 ret = hid_add_device(hid);
1088 hid_err(client, "can't add hid device: %d\n", ret);
1092 pm_runtime_put(&client->dev);
1096 hid_destroy_device(hid);
1099 free_irq(ihid->irq, ihid);
1102 pm_runtime_put_noidle(&client->dev);
1103 pm_runtime_disable(&client->dev);
1107 gpiod_put(ihid->desc);
1109 i2c_hid_free_buffers(ihid);
1114 static int i2c_hid_remove(struct i2c_client *client)
1116 struct i2c_hid *ihid = i2c_get_clientdata(client);
1117 struct hid_device *hid;
1119 pm_runtime_get_sync(&client->dev);
1120 pm_runtime_disable(&client->dev);
1121 pm_runtime_set_suspended(&client->dev);
1122 pm_runtime_put_noidle(&client->dev);
1125 hid_destroy_device(hid);
1127 free_irq(ihid->irq, ihid);
1130 i2c_hid_free_buffers(ihid);
1133 gpiod_put(ihid->desc);
1137 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&client->dev));
1142 #ifdef CONFIG_PM_SLEEP
1143 static int i2c_hid_suspend(struct device *dev)
1145 struct i2c_client *client = to_i2c_client(dev);
1146 struct i2c_hid *ihid = i2c_get_clientdata(client);
1147 struct hid_device *hid = ihid->hid;
1151 if (hid->driver && hid->driver->suspend)
1152 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1154 disable_irq(ihid->irq);
1155 if (device_may_wakeup(&client->dev)) {
1156 wake_status = enable_irq_wake(ihid->irq);
1158 ihid->irq_wake_enabled = true;
1160 hid_warn(hid, "Failed to enable irq wake: %d\n",
1164 /* Save some power */
1165 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1170 static int i2c_hid_resume(struct device *dev)
1173 struct i2c_client *client = to_i2c_client(dev);
1174 struct i2c_hid *ihid = i2c_get_clientdata(client);
1175 struct hid_device *hid = ihid->hid;
1178 enable_irq(ihid->irq);
1179 if (!device_may_wakeup(&client->dev)) {
1180 ret = i2c_hid_hwreset(client);
1185 if (device_may_wakeup(&client->dev) && ihid->irq_wake_enabled) {
1186 wake_status = disable_irq_wake(ihid->irq);
1188 ihid->irq_wake_enabled = false;
1190 hid_warn(hid, "Failed to disable irq wake: %d\n",
1194 if (hid->driver && hid->driver->reset_resume) {
1195 ret = hid->driver->reset_resume(hid);
1204 static int i2c_hid_runtime_suspend(struct device *dev)
1206 struct i2c_client *client = to_i2c_client(dev);
1207 struct i2c_hid *ihid = i2c_get_clientdata(client);
1209 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1210 disable_irq(ihid->irq);
1214 static int i2c_hid_runtime_resume(struct device *dev)
1216 struct i2c_client *client = to_i2c_client(dev);
1217 struct i2c_hid *ihid = i2c_get_clientdata(client);
1219 enable_irq(ihid->irq);
1220 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1225 static const struct dev_pm_ops i2c_hid_pm = {
1226 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1227 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1231 static const struct i2c_device_id i2c_hid_id_table[] = {
1235 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1238 static struct i2c_driver i2c_hid_driver = {
1241 .owner = THIS_MODULE,
1243 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1244 .of_match_table = of_match_ptr(i2c_hid_of_match),
1247 .probe = i2c_hid_probe,
1248 .remove = i2c_hid_remove,
1250 .id_table = i2c_hid_id_table,
1253 module_i2c_driver(i2c_hid_driver);
1255 MODULE_DESCRIPTION("HID over I2C core driver");
1256 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1257 MODULE_LICENSE("GPL");