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>
37 #include <linux/acpi.h>
39 #include <linux/i2c/i2c-hid.h>
42 #define I2C_HID_STARTED (1 << 0)
43 #define I2C_HID_RESET_PENDING (1 << 1)
44 #define I2C_HID_READ_PENDING (1 << 2)
46 #define I2C_HID_PWR_ON 0x00
47 #define I2C_HID_PWR_SLEEP 0x01
51 module_param(debug, bool, 0444);
52 MODULE_PARM_DESC(debug, "print a lot of debug information");
54 #define i2c_hid_dbg(ihid, fmt, arg...) \
57 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
61 __le16 wHIDDescLength;
63 __le16 wReportDescLength;
64 __le16 wReportDescRegister;
65 __le16 wInputRegister;
66 __le16 wMaxInputLength;
67 __le16 wOutputRegister;
68 __le16 wMaxOutputLength;
69 __le16 wCommandRegister;
78 unsigned int registerIndex;
93 #define I2C_HID_CMD(opcode_) \
94 .opcode = opcode_, .length = 4, \
95 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
97 /* fetch HID descriptor */
98 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
99 /* fetch report descriptors */
100 static const struct i2c_hid_cmd hid_report_descr_cmd = {
101 .registerIndex = offsetof(struct i2c_hid_desc,
102 wReportDescRegister),
106 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
108 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
109 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
110 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
113 * These definitions are not used here, but are defined by the spec.
114 * Keeping them here for documentation purposes.
116 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
117 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
118 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
119 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
122 static DEFINE_MUTEX(i2c_hid_open_mut);
124 /* The main device structure */
126 struct i2c_client *client; /* i2c client */
127 struct hid_device *hid; /* pointer to corresponding HID dev */
129 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
130 struct i2c_hid_desc hdesc; /* the HID Descriptor */
132 __le16 wHIDDescRegister; /* location of the i2c
133 * register of the HID
135 unsigned int bufsize; /* i2c buffer size */
136 char *inbuf; /* Input buffer */
137 char *rawbuf; /* Raw Input buffer */
138 char *cmdbuf; /* Command buffer */
139 char *argsbuf; /* Command arguments buffer */
141 unsigned long flags; /* device flags */
143 wait_queue_head_t wait; /* For waiting the interrupt */
145 struct i2c_hid_platform_data pdata;
148 static int __i2c_hid_command(struct i2c_client *client,
149 const struct i2c_hid_cmd *command, u8 reportID,
150 u8 reportType, u8 *args, int args_len,
151 unsigned char *buf_recv, int data_len)
153 struct i2c_hid *ihid = i2c_get_clientdata(client);
154 union command *cmd = (union command *)ihid->cmdbuf;
156 struct i2c_msg msg[2];
159 int length = command->length;
160 bool wait = command->wait;
161 unsigned int registerIndex = command->registerIndex;
163 /* special case for hid_descr_cmd */
164 if (command == &hid_descr_cmd) {
165 cmd->c.reg = ihid->wHIDDescRegister;
167 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
168 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
172 cmd->c.opcode = command->opcode;
173 cmd->c.reportTypeID = reportID | reportType << 4;
176 memcpy(cmd->data + length, args, args_len);
179 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
181 msg[0].addr = client->addr;
182 msg[0].flags = client->flags & I2C_M_TEN;
184 msg[0].buf = cmd->data;
186 msg[1].addr = client->addr;
187 msg[1].flags = client->flags & I2C_M_TEN;
188 msg[1].flags |= I2C_M_RD;
189 msg[1].len = data_len;
190 msg[1].buf = buf_recv;
192 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
196 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
198 ret = i2c_transfer(client->adapter, msg, msg_num);
201 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
204 return ret < 0 ? ret : -EIO;
209 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
210 if (!wait_event_timeout(ihid->wait,
211 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
212 msecs_to_jiffies(5000)))
214 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
220 static int i2c_hid_command(struct i2c_client *client,
221 const struct i2c_hid_cmd *command,
222 unsigned char *buf_recv, int data_len)
224 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
228 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
229 u8 reportID, unsigned char *buf_recv, int data_len)
231 struct i2c_hid *ihid = i2c_get_clientdata(client);
235 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
237 i2c_hid_dbg(ihid, "%s\n", __func__);
239 if (reportID >= 0x0F) {
240 args[args_len++] = reportID;
244 args[args_len++] = readRegister & 0xFF;
245 args[args_len++] = readRegister >> 8;
247 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
248 reportType, args, args_len, buf_recv, data_len);
250 dev_err(&client->dev,
251 "failed to retrieve report from device.\n");
258 static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
259 u8 reportID, unsigned char *buf, size_t data_len)
261 struct i2c_hid *ihid = i2c_get_clientdata(client);
262 u8 *args = ihid->argsbuf;
264 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
266 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
267 u16 size = 2 /* size */ +
268 (reportID ? 1 : 0) /* reportID */ +
270 int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
271 2 /* dataRegister */ +
275 i2c_hid_dbg(ihid, "%s\n", __func__);
277 if (reportID >= 0x0F) {
278 args[index++] = reportID;
282 args[index++] = dataRegister & 0xFF;
283 args[index++] = dataRegister >> 8;
285 args[index++] = size & 0xFF;
286 args[index++] = size >> 8;
289 args[index++] = reportID;
291 memcpy(&args[index], buf, data_len);
293 ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
294 reportType, args, args_len, NULL, 0);
296 dev_err(&client->dev, "failed to set a report to device.\n");
303 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
305 struct i2c_hid *ihid = i2c_get_clientdata(client);
308 i2c_hid_dbg(ihid, "%s\n", __func__);
310 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
311 0, NULL, 0, NULL, 0);
313 dev_err(&client->dev, "failed to change power setting.\n");
318 static int i2c_hid_hwreset(struct i2c_client *client)
320 struct i2c_hid *ihid = i2c_get_clientdata(client);
323 i2c_hid_dbg(ihid, "%s\n", __func__);
325 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
329 i2c_hid_dbg(ihid, "resetting...\n");
331 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
333 dev_err(&client->dev, "failed to reset device.\n");
334 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
341 static void i2c_hid_get_input(struct i2c_hid *ihid)
344 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
346 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
351 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
352 __func__, ret, size);
356 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
359 /* host or device initiated RESET completed */
360 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
361 wake_up(&ihid->wait);
365 if (ret_size > size) {
366 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
367 __func__, size, ret_size);
371 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
373 if (test_bit(I2C_HID_STARTED, &ihid->flags))
374 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
380 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
382 struct i2c_hid *ihid = dev_id;
384 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
387 i2c_hid_get_input(ihid);
392 static int i2c_hid_get_report_length(struct hid_report *report)
394 return ((report->size - 1) >> 3) + 1 +
395 report->device->report_enum[report->type].numbered + 2;
398 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
401 struct hid_device *hid = report->device;
402 struct i2c_client *client = hid->driver_data;
403 struct i2c_hid *ihid = i2c_get_clientdata(client);
404 unsigned int size, ret_size;
406 size = i2c_hid_get_report_length(report);
407 if (i2c_hid_get_report(client,
408 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
409 report->id, buffer, size))
412 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
414 ret_size = buffer[0] | (buffer[1] << 8);
416 if (ret_size != size) {
417 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
418 __func__, size, ret_size);
422 /* hid->driver_lock is held as we are in probe function,
423 * we just need to setup the input fields, so using
424 * hid_report_raw_event is safe. */
425 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
429 * Initialize all reports
431 static void i2c_hid_init_reports(struct hid_device *hid)
433 struct hid_report *report;
434 struct i2c_client *client = hid->driver_data;
435 struct i2c_hid *ihid = i2c_get_clientdata(client);
436 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
439 dev_err(&client->dev, "can not retrieve initial reports\n");
443 list_for_each_entry(report,
444 &hid->report_enum[HID_INPUT_REPORT].report_list, list)
445 i2c_hid_init_report(report, inbuf, ihid->bufsize);
447 list_for_each_entry(report,
448 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
449 i2c_hid_init_report(report, inbuf, ihid->bufsize);
455 * Traverse the supplied list of reports and find the longest
457 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
460 struct hid_report *report;
463 /* We should not rely on wMaxInputLength, as some devices may set it to
465 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
466 size = i2c_hid_get_report_length(report);
472 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
476 kfree(ihid->argsbuf);
481 ihid->argsbuf = NULL;
485 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
487 /* the worst case is computed from the set_report command with a
488 * reportID > 15 and the maximum report length */
489 int args_len = sizeof(__u8) + /* optional ReportID byte */
490 sizeof(__u16) + /* data register */
491 sizeof(__u16) + /* size of the report */
492 report_size; /* report */
494 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
495 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
496 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
497 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
499 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
500 i2c_hid_free_buffers(ihid);
504 ihid->bufsize = report_size;
509 static int i2c_hid_get_raw_report(struct hid_device *hid,
510 unsigned char report_number, __u8 *buf, size_t count,
511 unsigned char report_type)
513 struct i2c_client *client = hid->driver_data;
514 struct i2c_hid *ihid = i2c_get_clientdata(client);
515 size_t ret_count, ask_count;
518 if (report_type == HID_OUTPUT_REPORT)
521 /* +2 bytes to include the size of the reply in the query buffer */
522 ask_count = min(count + 2, (size_t)ihid->bufsize);
524 ret = i2c_hid_get_report(client,
525 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
526 report_number, ihid->rawbuf, ask_count);
531 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
536 ret_count = min(ret_count, ask_count);
538 /* The query buffer contains the size, dropping it in the reply */
539 count = min(count, ret_count - 2);
540 memcpy(buf, ihid->rawbuf + 2, count);
545 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
546 size_t count, unsigned char report_type)
548 struct i2c_client *client = hid->driver_data;
549 int report_id = buf[0];
552 if (report_type == HID_INPUT_REPORT)
560 ret = i2c_hid_set_report(client,
561 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
562 report_id, buf, count);
564 if (report_id && ret >= 0)
565 ret++; /* add report_id to the number of transfered bytes */
570 static void i2c_hid_request(struct hid_device *hid, struct hid_report *rep,
573 struct i2c_client *client = hid->driver_data;
576 int len = i2c_hid_get_report_length(rep) - 2;
578 buf = kzalloc(len, GFP_KERNEL);
583 case HID_REQ_GET_REPORT:
584 ret = i2c_hid_get_raw_report(hid, rep->id, buf, len, rep->type);
586 dev_err(&client->dev, "%s: unable to get report: %d\n",
589 hid_input_report(hid, rep->type, buf, ret, 0);
591 case HID_REQ_SET_REPORT:
592 hid_output_report(rep, buf);
593 i2c_hid_output_raw_report(hid, buf, len, rep->type);
600 static int i2c_hid_parse(struct hid_device *hid)
602 struct i2c_client *client = hid->driver_data;
603 struct i2c_hid *ihid = i2c_get_clientdata(client);
604 struct i2c_hid_desc *hdesc = &ihid->hdesc;
610 i2c_hid_dbg(ihid, "entering %s\n", __func__);
612 rsize = le16_to_cpu(hdesc->wReportDescLength);
613 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
614 dbg_hid("weird size of report descriptor (%u)\n", rsize);
619 ret = i2c_hid_hwreset(client);
622 } while (tries-- > 0 && ret);
627 rdesc = kzalloc(rsize, GFP_KERNEL);
630 dbg_hid("couldn't allocate rdesc memory\n");
634 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
636 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
638 hid_err(hid, "reading report descriptor failed\n");
643 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
645 ret = hid_parse_report(hid, rdesc, rsize);
648 dbg_hid("parsing report descriptor failed\n");
655 static int i2c_hid_start(struct hid_device *hid)
657 struct i2c_client *client = hid->driver_data;
658 struct i2c_hid *ihid = i2c_get_clientdata(client);
660 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
662 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
663 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
664 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
666 if (bufsize > ihid->bufsize) {
667 i2c_hid_free_buffers(ihid);
669 ret = i2c_hid_alloc_buffers(ihid, bufsize);
675 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
676 i2c_hid_init_reports(hid);
681 static void i2c_hid_stop(struct hid_device *hid)
683 struct i2c_client *client = hid->driver_data;
684 struct i2c_hid *ihid = i2c_get_clientdata(client);
688 i2c_hid_free_buffers(ihid);
691 static int i2c_hid_open(struct hid_device *hid)
693 struct i2c_client *client = hid->driver_data;
694 struct i2c_hid *ihid = i2c_get_clientdata(client);
697 mutex_lock(&i2c_hid_open_mut);
699 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
704 set_bit(I2C_HID_STARTED, &ihid->flags);
707 mutex_unlock(&i2c_hid_open_mut);
711 static void i2c_hid_close(struct hid_device *hid)
713 struct i2c_client *client = hid->driver_data;
714 struct i2c_hid *ihid = i2c_get_clientdata(client);
716 /* protecting hid->open to make sure we don't restart
717 * data acquistion due to a resumption we no longer
720 mutex_lock(&i2c_hid_open_mut);
722 clear_bit(I2C_HID_STARTED, &ihid->flags);
724 /* Save some power */
725 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
727 mutex_unlock(&i2c_hid_open_mut);
730 static int i2c_hid_power(struct hid_device *hid, int lvl)
732 struct i2c_client *client = hid->driver_data;
733 struct i2c_hid *ihid = i2c_get_clientdata(client);
736 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
740 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
743 ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
749 static int i2c_hid_hidinput_input_event(struct input_dev *dev,
750 unsigned int type, unsigned int code, int value)
752 struct hid_device *hid = input_get_drvdata(dev);
753 struct hid_field *field;
757 return input_ff_event(dev, type, code, value);
762 offset = hidinput_find_field(hid, type, code, &field);
765 hid_warn(dev, "event field not found\n");
769 return hid_set_field(field, offset, value);
772 static struct hid_ll_driver i2c_hid_ll_driver = {
773 .parse = i2c_hid_parse,
774 .start = i2c_hid_start,
775 .stop = i2c_hid_stop,
776 .open = i2c_hid_open,
777 .close = i2c_hid_close,
778 .power = i2c_hid_power,
779 .request = i2c_hid_request,
780 .hidinput_input_event = i2c_hid_hidinput_input_event,
783 static int i2c_hid_init_irq(struct i2c_client *client)
785 struct i2c_hid *ihid = i2c_get_clientdata(client);
788 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
790 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
791 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
794 dev_warn(&client->dev,
795 "Could not register for %s interrupt, irq = %d,"
797 client->name, client->irq, ret);
805 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
807 struct i2c_client *client = ihid->client;
808 struct i2c_hid_desc *hdesc = &ihid->hdesc;
812 /* Fetch the length of HID description, retrieve the 4 first bytes:
813 * bytes 0-1 -> length
814 * bytes 2-3 -> bcdVersion (has to be 1.00) */
815 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
817 i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
818 __func__, 4, ihid->hdesc_buffer);
821 dev_err(&client->dev,
822 "unable to fetch the size of HID descriptor (ret=%d)\n",
827 dsize = le16_to_cpu(hdesc->wHIDDescLength);
829 * the size of the HID descriptor should at least contain
830 * its size and the bcdVersion (4 bytes), and should not be greater
831 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
832 * through i2c_hid_command.
834 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
835 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
840 /* check bcdVersion == 1.0 */
841 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
842 dev_err(&client->dev,
843 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
844 le16_to_cpu(hdesc->bcdVersion));
848 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
850 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
853 dev_err(&client->dev, "hid_descr_cmd Fail\n");
857 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
863 static int i2c_hid_acpi_pdata(struct i2c_client *client,
864 struct i2c_hid_platform_data *pdata)
866 static u8 i2c_hid_guid[] = {
867 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
868 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
870 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
871 union acpi_object params[4], *obj;
872 struct acpi_object_list input;
873 struct acpi_device *adev;
876 handle = ACPI_HANDLE(&client->dev);
877 if (!handle || acpi_bus_get_device(handle, &adev))
880 input.count = ARRAY_SIZE(params);
881 input.pointer = params;
883 params[0].type = ACPI_TYPE_BUFFER;
884 params[0].buffer.length = sizeof(i2c_hid_guid);
885 params[0].buffer.pointer = i2c_hid_guid;
886 params[1].type = ACPI_TYPE_INTEGER;
887 params[1].integer.value = 1;
888 params[2].type = ACPI_TYPE_INTEGER;
889 params[2].integer.value = 1; /* HID function */
890 params[3].type = ACPI_TYPE_INTEGER;
891 params[3].integer.value = 0;
893 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DSM", &input, &buf))) {
894 dev_err(&client->dev, "device _DSM execution failed\n");
898 obj = (union acpi_object *)buf.pointer;
899 if (obj->type != ACPI_TYPE_INTEGER) {
900 dev_err(&client->dev, "device _DSM returned invalid type: %d\n",
906 pdata->hid_descriptor_address = obj->integer.value;
912 static const struct acpi_device_id i2c_hid_acpi_match[] = {
917 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
919 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
920 struct i2c_hid_platform_data *pdata)
926 static int i2c_hid_probe(struct i2c_client *client,
927 const struct i2c_device_id *dev_id)
930 struct i2c_hid *ihid;
931 struct hid_device *hid;
933 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
935 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
938 dev_err(&client->dev,
939 "HID over i2c has not been provided an Int IRQ\n");
943 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
947 if (!platform_data) {
948 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
950 dev_err(&client->dev,
951 "HID register address not provided\n");
955 ihid->pdata = *platform_data;
958 i2c_set_clientdata(client, ihid);
960 ihid->client = client;
962 hidRegister = ihid->pdata.hid_descriptor_address;
963 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
965 init_waitqueue_head(&ihid->wait);
967 /* we need to allocate the command buffer without knowing the maximum
968 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
969 * real computation later. */
970 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
974 ret = i2c_hid_fetch_hid_descriptor(ihid);
978 ret = i2c_hid_init_irq(client);
982 hid = hid_allocate_device();
990 hid->driver_data = client;
991 hid->ll_driver = &i2c_hid_ll_driver;
992 hid->hid_get_raw_report = i2c_hid_get_raw_report;
993 hid->hid_output_raw_report = i2c_hid_output_raw_report;
994 hid->dev.parent = &client->dev;
995 ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
997 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
998 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
999 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1001 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1002 client->name, hid->vendor, hid->product);
1004 ret = hid_add_device(hid);
1007 hid_err(client, "can't add hid device: %d\n", ret);
1014 hid_destroy_device(hid);
1017 free_irq(client->irq, ihid);
1020 i2c_hid_free_buffers(ihid);
1025 static int i2c_hid_remove(struct i2c_client *client)
1027 struct i2c_hid *ihid = i2c_get_clientdata(client);
1028 struct hid_device *hid;
1031 hid_destroy_device(hid);
1033 free_irq(client->irq, ihid);
1036 i2c_hid_free_buffers(ihid);
1043 #ifdef CONFIG_PM_SLEEP
1044 static int i2c_hid_suspend(struct device *dev)
1046 struct i2c_client *client = to_i2c_client(dev);
1048 if (device_may_wakeup(&client->dev))
1049 enable_irq_wake(client->irq);
1051 /* Save some power */
1052 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1057 static int i2c_hid_resume(struct device *dev)
1060 struct i2c_client *client = to_i2c_client(dev);
1062 ret = i2c_hid_hwreset(client);
1066 if (device_may_wakeup(&client->dev))
1067 disable_irq_wake(client->irq);
1073 static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
1075 static const struct i2c_device_id i2c_hid_id_table[] = {
1079 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1082 static struct i2c_driver i2c_hid_driver = {
1085 .owner = THIS_MODULE,
1087 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1090 .probe = i2c_hid_probe,
1091 .remove = i2c_hid_remove,
1093 .id_table = i2c_hid_id_table,
1096 module_i2c_driver(i2c_hid_driver);
1098 MODULE_DESCRIPTION("HID over I2C core driver");
1099 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1100 MODULE_LICENSE("GPL");