From 4a200c3b9a40242652b5734630bdd0bcf3aca75f Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Mon, 12 Nov 2012 15:42:59 +0100 Subject: HID: i2c-hid: introduce HID over i2c specification implementation Microsoft published the protocol specification of HID over i2c: http://msdn.microsoft.com/en-us/library/windows/hardware/hh852380.aspx This patch introduces an implementation of this protocol. This implementation does not includes the ACPI part of the specification. This will come when ACPI 5.0 devices enumeration will be available. Once the ACPI part is done, OEM will not have to declare HID over I2C devices in their platform specific driver. Signed-off-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/Kconfig | 21 + drivers/hid/i2c-hid/Makefile | 5 + drivers/hid/i2c-hid/i2c-hid.c | 974 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1000 insertions(+) create mode 100644 drivers/hid/i2c-hid/Kconfig create mode 100644 drivers/hid/i2c-hid/Makefile create mode 100644 drivers/hid/i2c-hid/i2c-hid.c (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/Kconfig b/drivers/hid/i2c-hid/Kconfig new file mode 100644 index 000000000000..5b7d4d87a143 --- /dev/null +++ b/drivers/hid/i2c-hid/Kconfig @@ -0,0 +1,21 @@ +menu "I2C HID support" + depends on I2C + +config I2C_HID + tristate "HID over I2C transport layer" + default n + depends on I2C && INPUT + select HID + ---help--- + Say Y here if you want to use the HID over i2c protocol + implementation. + + If unsure, say N. + + This support is also available as a module. If so, the module + will be called i2c-hid. + +comment "Input core support is needed for HID over I2C input layer" + depends on I2C_HID && INPUT=n + +endmenu diff --git a/drivers/hid/i2c-hid/Makefile b/drivers/hid/i2c-hid/Makefile new file mode 100644 index 000000000000..832d8f9aaba2 --- /dev/null +++ b/drivers/hid/i2c-hid/Makefile @@ -0,0 +1,5 @@ +# +# Makefile for the I2C input drivers +# + +obj-$(CONFIG_I2C_HID) += i2c-hid.o diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c new file mode 100644 index 000000000000..11140bdae660 --- /dev/null +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -0,0 +1,974 @@ +/* + * HID over I2C protocol implementation + * + * Copyright (c) 2012 Benjamin Tissoires + * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France + * Copyright (c) 2012 Red Hat, Inc + * + * This code is partly based on "USB HID support for Linux": + * + * Copyright (c) 1999 Andreas Gal + * Copyright (c) 2000-2005 Vojtech Pavlik + * Copyright (c) 2005 Michael Haboustak for Concept2, Inc + * Copyright (c) 2007-2008 Oliver Neukum + * Copyright (c) 2006-2010 Jiri Kosina + * + * This file is subject to the terms and conditions of the GNU General Public + * License. See the file COPYING in the main directory of this archive for + * more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/* flags */ +#define I2C_HID_STARTED (1 << 0) +#define I2C_HID_RESET_PENDING (1 << 1) +#define I2C_HID_READ_PENDING (1 << 2) + +#define I2C_HID_PWR_ON 0x00 +#define I2C_HID_PWR_SLEEP 0x01 + +/* debug option */ +static bool debug = false; +module_param(debug, bool, 0444); +MODULE_PARM_DESC(debug, "print a lot of debug information"); + +#define i2c_hid_dbg(ihid, fmt, arg...) \ + if (debug) \ + dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg) + +struct i2c_hid_desc { + __le16 wHIDDescLength; + __le16 bcdVersion; + __le16 wReportDescLength; + __le16 wReportDescRegister; + __le16 wInputRegister; + __le16 wMaxInputLength; + __le16 wOutputRegister; + __le16 wMaxOutputLength; + __le16 wCommandRegister; + __le16 wDataRegister; + __le16 wVendorID; + __le16 wProductID; + __le16 wVersionID; +} __packed; + +struct i2c_hid_cmd { + unsigned int registerIndex; + __u8 opcode; + unsigned int length; + bool wait; +}; + +union command { + u8 data[0]; + struct cmd { + __le16 reg; + __u8 reportTypeID; + __u8 opcode; + } __packed c; +}; + +#define I2C_HID_CMD(opcode_) \ + .opcode = opcode_, .length = 4, \ + .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister) + +/* fetch HID descriptor */ +static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 }; +/* fetch report descriptors */ +static const struct i2c_hid_cmd hid_report_descr_cmd = { + .registerIndex = offsetof(struct i2c_hid_desc, + wReportDescRegister), + .opcode = 0x00, + .length = 2 }; +/* commands */ +static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01), + .wait = true }; +static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; +static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; +static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; +static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; +static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; +static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; +static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; +/* read/write data register */ +static const struct i2c_hid_cmd hid_data_cmd = { + .registerIndex = offsetof(struct i2c_hid_desc, wDataRegister), + .opcode = 0x00, + .length = 2 }; +/* write output reports */ +static const struct i2c_hid_cmd hid_out_cmd = { + .registerIndex = offsetof(struct i2c_hid_desc, + wOutputRegister), + .opcode = 0x00, + .length = 2 }; + +/* The main device structure */ +struct i2c_hid { + struct i2c_client *client; /* i2c client */ + struct hid_device *hid; /* pointer to corresponding HID dev */ + union { + __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)]; + struct i2c_hid_desc hdesc; /* the HID Descriptor */ + }; + __le16 wHIDDescRegister; /* location of the i2c + * register of the HID + * descriptor. */ + unsigned int bufsize; /* i2c buffer size */ + char *inbuf; /* Input buffer */ + char *cmdbuf; /* Command buffer */ + char *argsbuf; /* Command arguments buffer */ + + unsigned long flags; /* device flags */ + + int irq; /* the interrupt line irq */ + + wait_queue_head_t wait; /* For waiting the interrupt */ +}; + +static int __i2c_hid_command(struct i2c_client *client, + const struct i2c_hid_cmd *command, u8 reportID, + u8 reportType, u8 *args, int args_len, + unsigned char *buf_recv, int data_len) +{ + struct i2c_hid *ihid = i2c_get_clientdata(client); + union command *cmd = (union command *)ihid->cmdbuf; + int ret; + struct i2c_msg msg[2]; + int msg_num = 1; + + int length = command->length; + bool wait = command->wait; + unsigned int registerIndex = command->registerIndex; + + /* special case for hid_descr_cmd */ + if (command == &hid_descr_cmd) { + cmd->c.reg = ihid->wHIDDescRegister; + } else { + cmd->data[0] = ihid->hdesc_buffer[registerIndex]; + cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1]; + } + + if (length > 2) { + cmd->c.opcode = command->opcode; + cmd->c.reportTypeID = reportID | reportType << 4; + } + + memcpy(cmd->data + length, args, args_len); + length += args_len; + + i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data); + + msg[0].addr = client->addr; + msg[0].flags = client->flags & I2C_M_TEN; + msg[0].len = length; + msg[0].buf = cmd->data; + if (data_len > 0) { + msg[1].addr = client->addr; + msg[1].flags = client->flags & I2C_M_TEN; + msg[1].flags |= I2C_M_RD; + msg[1].len = data_len; + msg[1].buf = buf_recv; + msg_num = 2; + set_bit(I2C_HID_READ_PENDING, &ihid->flags); + } + + if (wait) + set_bit(I2C_HID_RESET_PENDING, &ihid->flags); + + ret = i2c_transfer(client->adapter, msg, msg_num); + + if (data_len > 0) + clear_bit(I2C_HID_READ_PENDING, &ihid->flags); + + if (ret != msg_num) + return ret < 0 ? ret : -EIO; + + ret = 0; + + if (wait) { + i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); + if (!wait_event_timeout(ihid->wait, + !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), + msecs_to_jiffies(5000))) + ret = -ENODATA; + i2c_hid_dbg(ihid, "%s: finished.\n", __func__); + } + + return ret; +} + +static int i2c_hid_command(struct i2c_client *client, + const struct i2c_hid_cmd *command, + unsigned char *buf_recv, int data_len) +{ + return __i2c_hid_command(client, command, 0, 0, NULL, 0, + buf_recv, data_len); +} + +static int i2c_hid_get_report(struct i2c_client *client, u8 reportType, + u8 reportID, unsigned char *buf_recv, int data_len) +{ + struct i2c_hid *ihid = i2c_get_clientdata(client); + u8 args[3]; + int ret; + int args_len = 0; + u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister); + + i2c_hid_dbg(ihid, "%s\n", __func__); + + if (reportID >= 0x0F) { + args[args_len++] = reportID; + reportID = 0x0F; + } + + args[args_len++] = readRegister & 0xFF; + args[args_len++] = readRegister >> 8; + + ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID, + reportType, args, args_len, buf_recv, data_len); + if (ret) { + dev_err(&client->dev, + "failed to retrieve report from device.\n"); + return -EINVAL; + } + + return 0; +} + +static int i2c_hid_set_report(struct i2c_client *client, u8 reportType, + u8 reportID, unsigned char *buf, size_t data_len) +{ + struct i2c_hid *ihid = i2c_get_clientdata(client); + u8 *args = ihid->argsbuf; + int ret; + u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); + + /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */ + u16 size = 2 /* size */ + + (reportID ? 1 : 0) /* reportID */ + + data_len /* buf */; + int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ + + 2 /* dataRegister */ + + size /* args */; + int index = 0; + + i2c_hid_dbg(ihid, "%s\n", __func__); + + if (reportID >= 0x0F) { + args[index++] = reportID; + reportID = 0x0F; + } + + args[index++] = dataRegister & 0xFF; + args[index++] = dataRegister >> 8; + + args[index++] = size & 0xFF; + args[index++] = size >> 8; + + if (reportID) + args[index++] = reportID; + + memcpy(&args[index], buf, data_len); + + ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID, + reportType, args, args_len, NULL, 0); + if (ret) { + dev_err(&client->dev, "failed to set a report to device.\n"); + return -EINVAL; + } + + return data_len; +} + +static int i2c_hid_set_power(struct i2c_client *client, int power_state) +{ + struct i2c_hid *ihid = i2c_get_clientdata(client); + int ret; + + i2c_hid_dbg(ihid, "%s\n", __func__); + + ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state, + 0, NULL, 0, NULL, 0); + if (ret) + dev_err(&client->dev, "failed to change power setting.\n"); + + return ret; +} + +static int i2c_hid_hwreset(struct i2c_client *client) +{ + struct i2c_hid *ihid = i2c_get_clientdata(client); + int ret; + + i2c_hid_dbg(ihid, "%s\n", __func__); + + ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); + if (ret) + return ret; + + i2c_hid_dbg(ihid, "resetting...\n"); + + ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0); + if (ret) { + dev_err(&client->dev, "failed to reset device.\n"); + i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); + return ret; + } + + return 0; +} + +static int i2c_hid_get_input(struct i2c_hid *ihid) +{ + int ret, ret_size; + int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); + + ret = i2c_master_recv(ihid->client, ihid->inbuf, size); + if (ret != size) { + if (ret < 0) + return ret; + + dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", + __func__, ret, size); + return ret; + } + + ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; + + if (!ret_size) { + /* host or device initiated RESET completed */ + if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) + wake_up(&ihid->wait); + return 0; + } + + if (ret_size > size) { + dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", + __func__, size, ret_size); + return -EIO; + } + + i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); + + if (test_bit(I2C_HID_STARTED, &ihid->flags)) + hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, + ret_size - 2, 1); + + return 0; +} + +static irqreturn_t i2c_hid_irq(int irq, void *dev_id) +{ + struct i2c_hid *ihid = dev_id; + + if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) + return IRQ_HANDLED; + + i2c_hid_get_input(ihid); + + return IRQ_HANDLED; +} + +static int i2c_hid_get_report_length(struct hid_report *report) +{ + return ((report->size - 1) >> 3) + 1 + + report->device->report_enum[report->type].numbered + 2; +} + +static void i2c_hid_init_report(struct hid_report *report, u8 *buffer, + size_t bufsize) +{ + struct hid_device *hid = report->device; + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + unsigned int size, ret_size; + + size = i2c_hid_get_report_length(report); + i2c_hid_get_report(client, + report->type == HID_FEATURE_REPORT ? 0x03 : 0x01, + report->id, buffer, size); + + i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf); + + ret_size = buffer[0] | (buffer[1] << 8); + + if (ret_size != size) { + dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n", + __func__, size, ret_size); + return; + } + + /* hid->driver_lock is held as we are in probe function, + * we just need to setup the input fields, so using + * hid_report_raw_event is safe. */ + hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1); +} + +/* + * Initialize all reports + */ +static void i2c_hid_init_reports(struct hid_device *hid) +{ + struct hid_report *report; + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL); + + if (!inbuf) + return; + + list_for_each_entry(report, + &hid->report_enum[HID_INPUT_REPORT].report_list, list) + i2c_hid_init_report(report, inbuf, ihid->bufsize); + + list_for_each_entry(report, + &hid->report_enum[HID_FEATURE_REPORT].report_list, list) + i2c_hid_init_report(report, inbuf, ihid->bufsize); + + kfree(inbuf); +} + +/* + * Traverse the supplied list of reports and find the longest + */ +static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, + unsigned int *max) +{ + struct hid_report *report; + unsigned int size; + + /* We should not rely on wMaxInputLength, as some devices may set it to + * a wrong length. */ + list_for_each_entry(report, &hid->report_enum[type].report_list, list) { + size = i2c_hid_get_report_length(report); + if (*max < size) + *max = size; + } +} + +static int i2c_hid_alloc_buffers(struct i2c_hid *ihid) +{ + /* the worst case is computed from the set_report command with a + * reportID > 15 and the maximum report length */ + int args_len = sizeof(__u8) + /* optional ReportID byte */ + sizeof(__u16) + /* data register */ + sizeof(__u16) + /* size of the report */ + ihid->bufsize; /* report */ + + ihid->inbuf = kzalloc(ihid->bufsize, GFP_KERNEL); + + if (!ihid->inbuf) + return -ENOMEM; + + ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); + + if (!ihid->argsbuf) { + kfree(ihid->inbuf); + return -ENOMEM; + } + + ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); + + if (!ihid->cmdbuf) { + kfree(ihid->inbuf); + kfree(ihid->argsbuf); + ihid->inbuf = NULL; + ihid->argsbuf = NULL; + return -ENOMEM; + } + + return 0; +} + +static void i2c_hid_free_buffers(struct i2c_hid *ihid) +{ + kfree(ihid->inbuf); + kfree(ihid->argsbuf); + kfree(ihid->cmdbuf); + ihid->inbuf = NULL; + ihid->cmdbuf = NULL; + ihid->argsbuf = NULL; +} + +static int i2c_hid_get_raw_report(struct hid_device *hid, + unsigned char report_number, __u8 *buf, size_t count, + unsigned char report_type) +{ + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + int ret; + + if (report_type == HID_OUTPUT_REPORT) + return -EINVAL; + + if (count > ihid->bufsize) + count = ihid->bufsize; + + ret = i2c_hid_get_report(client, + report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, + report_number, ihid->inbuf, count); + + if (ret < 0) + return ret; + + count = ihid->inbuf[0] | (ihid->inbuf[1] << 8); + + memcpy(buf, ihid->inbuf + 2, count); + + return count; +} + +static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf, + size_t count, unsigned char report_type) +{ + struct i2c_client *client = hid->driver_data; + int report_id = buf[0]; + + if (report_type == HID_INPUT_REPORT) + return -EINVAL; + + return i2c_hid_set_report(client, + report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, + report_id, buf, count); +} + +static int i2c_hid_parse(struct hid_device *hid) +{ + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + struct i2c_hid_desc *hdesc = &ihid->hdesc; + unsigned int rsize; + char *rdesc; + int ret; + int tries = 3; + + i2c_hid_dbg(ihid, "entering %s\n", __func__); + + rsize = le16_to_cpu(hdesc->wReportDescLength); + if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { + dbg_hid("weird size of report descriptor (%u)\n", rsize); + return -EINVAL; + } + + do { + ret = i2c_hid_hwreset(client); + if (ret) + msleep(1000); + } while (tries-- > 0 && ret); + + if (ret) + return ret; + + rdesc = kzalloc(rsize, GFP_KERNEL); + + if (!rdesc) { + dbg_hid("couldn't allocate rdesc memory\n"); + return -ENOMEM; + } + + i2c_hid_dbg(ihid, "asking HID report descriptor\n"); + + ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize); + if (ret) { + hid_err(hid, "reading report descriptor failed\n"); + kfree(rdesc); + return -EIO; + } + + i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); + + ret = hid_parse_report(hid, rdesc, rsize); + kfree(rdesc); + if (ret) { + dbg_hid("parsing report descriptor failed\n"); + return ret; + } + + return 0; +} + +static int i2c_hid_start(struct hid_device *hid) +{ + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + int ret; + int old_bufsize = ihid->bufsize; + + ihid->bufsize = HID_MIN_BUFFER_SIZE; + i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &ihid->bufsize); + i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &ihid->bufsize); + i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &ihid->bufsize); + + if (ihid->bufsize > old_bufsize || !ihid->inbuf || !ihid->cmdbuf) { + i2c_hid_free_buffers(ihid); + + ret = i2c_hid_alloc_buffers(ihid); + + if (ret) { + ihid->bufsize = old_bufsize; + return ret; + } + } + + if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS)) + i2c_hid_init_reports(hid); + + return 0; +} + +static void i2c_hid_stop(struct hid_device *hid) +{ + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + + hid->claimed = 0; + + i2c_hid_free_buffers(ihid); +} + +static int i2c_hid_open(struct hid_device *hid) +{ + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + int ret; + + if (!hid->open++) { + ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); + if (ret) { + hid->open--; + return -EIO; + } + set_bit(I2C_HID_STARTED, &ihid->flags); + } + return 0; +} + +static void i2c_hid_close(struct hid_device *hid) +{ + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + + /* protecting hid->open to make sure we don't restart + * data acquistion due to a resumption we no longer + * care about + */ + if (!--hid->open) { + clear_bit(I2C_HID_STARTED, &ihid->flags); + + /* Save some power */ + i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); + } +} + +static int i2c_hid_power(struct hid_device *hid, int lvl) +{ + struct i2c_client *client = hid->driver_data; + struct i2c_hid *ihid = i2c_get_clientdata(client); + int ret = 0; + + i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl); + + switch (lvl) { + case PM_HINT_FULLON: + ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); + break; + case PM_HINT_NORMAL: + ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); + break; + } + return ret; +} + +static int i2c_hid_hidinput_input_event(struct input_dev *dev, + unsigned int type, unsigned int code, int value) +{ + struct hid_device *hid = input_get_drvdata(dev); + struct hid_field *field; + int offset; + + if (type == EV_FF) + return input_ff_event(dev, type, code, value); + + if (type != EV_LED) + return -1; + + offset = hidinput_find_field(hid, type, code, &field); + + if (offset == -1) { + hid_warn(dev, "event field not found\n"); + return -1; + } + + hid_set_field(field, offset, value); + + return 0; +} + +static struct hid_ll_driver i2c_hid_ll_driver = { + .parse = i2c_hid_parse, + .start = i2c_hid_start, + .stop = i2c_hid_stop, + .open = i2c_hid_open, + .close = i2c_hid_close, + .power = i2c_hid_power, + .hidinput_input_event = i2c_hid_hidinput_input_event, +}; + +static int __devinit i2c_hid_init_irq(struct i2c_client *client) +{ + struct i2c_hid *ihid = i2c_get_clientdata(client); + int ret; + + dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq); + + ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, + IRQF_TRIGGER_FALLING | IRQF_ONESHOT, + client->name, ihid); + if (ret < 0) { + dev_dbg(&client->dev, + "Could not register for %s interrupt, irq = %d," + " ret = %d\n", + client->name, client->irq, ret); + + return ret; + } + + ihid->irq = client->irq; + + return 0; +} + +static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) +{ + struct i2c_client *client = ihid->client; + struct i2c_hid_desc *hdesc = &ihid->hdesc; + unsigned int dsize; + int ret; + + /* Fetch the length of HID description, retrieve the 4 first bytes: + * bytes 0-1 -> length + * bytes 2-3 -> bcdVersion (has to be 1.00) */ + ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4); + + i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n", + __func__, 4, ihid->hdesc_buffer); + + if (ret) { + dev_err(&client->dev, "HID_DESCR_LENGTH_CMD Fail (ret=%d)\n", + ret); + return -ENODEV; + } + + dsize = le16_to_cpu(hdesc->wHIDDescLength); + if (!dsize || dsize > HID_MAX_DESCRIPTOR_SIZE) { + dev_err(&client->dev, "weird size of HID descriptor (%u)\n", + dsize); + return -ENODEV; + } + + /* check bcdVersion == 1.0 */ + if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { + dev_err(&client->dev, + "unexpected HID descriptor bcdVersion (0x%04x)\n", + le16_to_cpu(hdesc->bcdVersion)); + return -ENODEV; + } + + i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); + + ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, + dsize); + if (ret) { + dev_err(&client->dev, "hid_descr_cmd Fail\n"); + return -ENODEV; + } + + i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer); + + return 0; +} + +static int __devinit i2c_hid_probe(struct i2c_client *client, + const struct i2c_device_id *dev_id) +{ + int ret; + struct i2c_hid *ihid; + struct hid_device *hid; + __u16 hidRegister; + struct i2c_hid_platform_data *platform_data = client->dev.platform_data; + + dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); + + if (!platform_data) { + dev_err(&client->dev, "HID register address not provided\n"); + return -EINVAL; + } + + if (!client->irq) { + dev_err(&client->dev, + "HID over i2c has not been provided an Int IRQ\n"); + return -EINVAL; + } + + ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL); + if (!ihid) + return -ENOMEM; + + i2c_set_clientdata(client, ihid); + + ihid->client = client; + + hidRegister = platform_data->hid_descriptor_address; + ihid->wHIDDescRegister = cpu_to_le16(hidRegister); + + init_waitqueue_head(&ihid->wait); + + /* we need to allocate the command buffer without knowing the maximum + * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the + * real computation later. */ + ihid->bufsize = HID_MIN_BUFFER_SIZE; + i2c_hid_alloc_buffers(ihid); + + ret = i2c_hid_fetch_hid_descriptor(ihid); + if (ret < 0) + goto err; + + ret = i2c_hid_init_irq(client); + if (ret < 0) + goto err; + + hid = hid_allocate_device(); + if (IS_ERR(hid)) { + ret = PTR_ERR(hid); + goto err; + } + + ihid->hid = hid; + + hid->driver_data = client; + hid->ll_driver = &i2c_hid_ll_driver; + hid->hid_get_raw_report = i2c_hid_get_raw_report; + hid->hid_output_raw_report = i2c_hid_output_raw_report; + hid->dev.parent = &client->dev; + hid->bus = BUS_I2C; + hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); + hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); + hid->product = le16_to_cpu(ihid->hdesc.wProductID); + + snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", + client->name, hid->vendor, hid->product); + + ret = hid_add_device(hid); + if (ret) { + if (ret != -ENODEV) + hid_err(client, "can't add hid device: %d\n", ret); + goto err_mem_free; + } + + return 0; + +err_mem_free: + hid_destroy_device(hid); + +err: + if (ihid->irq) + free_irq(ihid->irq, ihid); + + kfree(ihid); + return ret; +} + +static int __devexit i2c_hid_remove(struct i2c_client *client) +{ + struct i2c_hid *ihid = i2c_get_clientdata(client); + struct hid_device *hid; + + if (WARN_ON(!ihid)) + return -1; + + hid = ihid->hid; + hid_destroy_device(hid); + + free_irq(client->irq, ihid); + + kfree(ihid); + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int i2c_hid_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct i2c_hid *ihid = i2c_get_clientdata(client); + + if (device_may_wakeup(&client->dev)) + enable_irq_wake(ihid->irq); + + /* Save some power */ + i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); + + return 0; +} + +static int i2c_hid_resume(struct device *dev) +{ + int ret; + struct i2c_client *client = to_i2c_client(dev); + + ret = i2c_hid_hwreset(client); + if (ret) + return ret; + + if (device_may_wakeup(&client->dev)) + disable_irq_wake(client->irq); + + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume); + +static const struct i2c_device_id i2c_hid_id_table[] = { + { "i2c_hid", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table); + + +static struct i2c_driver i2c_hid_driver = { + .driver = { + .name = "i2c_hid", + .owner = THIS_MODULE, + .pm = &i2c_hid_pm, + }, + + .probe = i2c_hid_probe, + .remove = __devexit_p(i2c_hid_remove), + + .id_table = i2c_hid_id_table, +}; + +module_i2c_driver(i2c_hid_driver); + +MODULE_DESCRIPTION("HID over I2C core driver"); +MODULE_AUTHOR("Benjamin Tissoires "); +MODULE_LICENSE("GPL"); -- cgit v1.2.3 From 3c62602434c13744df62b3ab0ab7950cd36f24db Mon Sep 17 00:00:00 2001 From: Jiri Kosina Date: Tue, 20 Nov 2012 17:09:40 +0100 Subject: HID: i2c-hid: fix memory leak during probe In case we are returning from i2c_hid_probe() through the 'err' or 'err_mem_free' labels, there is noone freeing the buffers allocated by i2c_hid_alloc_buffers(). Reviewed-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 11140bdae660..67ab5b7b64d8 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -892,6 +892,7 @@ err: if (ihid->irq) free_irq(ihid->irq, ihid); + i2c_hid_free_buffers(ihid); kfree(ihid); return ret; } -- cgit v1.2.3 From 24ebb37e6515d999bb27f5f8de6ff30faa7479f5 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:42 +0100 Subject: HID: i2c-hid: change I2C name no I2C driver has "i2c" in its name. It makes more sense to call this i2c driver "hid". Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 67ab5b7b64d8..0fbb22906806 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -949,7 +949,7 @@ static int i2c_hid_resume(struct device *dev) static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume); static const struct i2c_device_id i2c_hid_id_table[] = { - { "i2c_hid", 0 }, + { "hid", 0 }, { }, }; MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table); -- cgit v1.2.3 From 8ee0c05aa0c8310621fa6c8dec43e9415e5542be Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:44 +0100 Subject: HID: i2c-hid: enhance Kconfig The "comment" part can never be displayed, so we can remove it. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/Kconfig | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/Kconfig b/drivers/hid/i2c-hid/Kconfig index 5b7d4d87a143..b66617a020bd 100644 --- a/drivers/hid/i2c-hid/Kconfig +++ b/drivers/hid/i2c-hid/Kconfig @@ -7,15 +7,12 @@ config I2C_HID depends on I2C && INPUT select HID ---help--- - Say Y here if you want to use the HID over i2c protocol - implementation. + Say Y here if you use a keyboard, a touchpad, a touchscreen, or any + other HID based devices which is connected to your computer via I2C. If unsure, say N. This support is also available as a module. If so, the module will be called i2c-hid. -comment "Input core support is needed for HID over I2C input layer" - depends on I2C_HID && INPUT=n - endmenu -- cgit v1.2.3 From ee8e8806348732e328d119418a9788aabeceed0a Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:45 +0100 Subject: HID: i2c-hid: fix checkpatch.pl warning We should not initialize to 0 static declarations. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 0fbb22906806..3c8fe4673705 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -46,7 +46,7 @@ #define I2C_HID_PWR_SLEEP 0x01 /* debug option */ -static bool debug = false; +static bool debug; module_param(debug, bool, 0444); MODULE_PARM_DESC(debug, "print a lot of debug information"); -- cgit v1.2.3 From fa738644e57d5fd54b8c0a4f5a7815972e65ce1a Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:46 +0100 Subject: HID: i2c-hid: fix i2c_hid_dbg macro This avoids the problematic case: if (condition) i2c_hid_dbg(ihid, "Blah blah %d\n", i); else do_something_very_important(); Which looks correct, however with the previous macro definition, this expands to the unexpected: if (condition) { if (debug) \ dev_printk(KERN_DEBUG, &ihid->client->dev, "Blah blah %d\n", i); else do_something_very_important(); } Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 3c8fe4673705..d95e8de6a920 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -50,9 +50,11 @@ static bool debug; module_param(debug, bool, 0444); MODULE_PARM_DESC(debug, "print a lot of debug information"); -#define i2c_hid_dbg(ihid, fmt, arg...) \ - if (debug) \ - dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg) +#define i2c_hid_dbg(ihid, fmt, arg...) \ +do { \ + if (debug) \ + dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \ +} while (0) struct i2c_hid_desc { __le16 wHIDDescLength; -- cgit v1.2.3 From 6bf6c8bf2ba6548c38eeaccb735992660c4bbaeb Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:47 +0100 Subject: HID: i2c-hid: remove unused static declarations These definitions are not used here, but are defined by the specification. Keeping some of them for documentation purposes. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index d95e8de6a920..be5126c281c3 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -105,22 +105,17 @@ static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01), .wait = true }; static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; -static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; -static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; -static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; -static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; -/* read/write data register */ -static const struct i2c_hid_cmd hid_data_cmd = { - .registerIndex = offsetof(struct i2c_hid_desc, wDataRegister), - .opcode = 0x00, - .length = 2 }; -/* write output reports */ -static const struct i2c_hid_cmd hid_out_cmd = { - .registerIndex = offsetof(struct i2c_hid_desc, - wOutputRegister), - .opcode = 0x00, - .length = 2 }; + +/* + * These definitions are not used here, but are defined by the spec. + * Keeping them here for documentation purposes. + * + * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; + * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; + * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; + * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; + */ /* The main device structure */ struct i2c_hid { -- cgit v1.2.3 From 317b204a131c36df564bd71214e9e18348a084c9 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:48 +0100 Subject: HID: i2c-hid: fix return paths Forwards appropriate return values. As noone use the error returned by i2c_hid_get_input, let's make it returning void. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index be5126c281c3..e0d097deb067 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -244,7 +244,7 @@ static int i2c_hid_get_report(struct i2c_client *client, u8 reportType, if (ret) { dev_err(&client->dev, "failed to retrieve report from device.\n"); - return -EINVAL; + return ret; } return 0; @@ -289,7 +289,7 @@ static int i2c_hid_set_report(struct i2c_client *client, u8 reportType, reportType, args, args_len, NULL, 0); if (ret) { dev_err(&client->dev, "failed to set a report to device.\n"); - return -EINVAL; + return ret; } return data_len; @@ -333,7 +333,7 @@ static int i2c_hid_hwreset(struct i2c_client *client) return 0; } -static int i2c_hid_get_input(struct i2c_hid *ihid) +static void i2c_hid_get_input(struct i2c_hid *ihid) { int ret, ret_size; int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); @@ -341,11 +341,11 @@ static int i2c_hid_get_input(struct i2c_hid *ihid) ret = i2c_master_recv(ihid->client, ihid->inbuf, size); if (ret != size) { if (ret < 0) - return ret; + return; dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", __func__, ret, size); - return ret; + return; } ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; @@ -354,13 +354,13 @@ static int i2c_hid_get_input(struct i2c_hid *ihid) /* host or device initiated RESET completed */ if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) wake_up(&ihid->wait); - return 0; + return; } if (ret_size > size) { dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", __func__, size, ret_size); - return -EIO; + return; } i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); @@ -369,7 +369,7 @@ static int i2c_hid_get_input(struct i2c_hid *ihid) hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, ret_size - 2, 1); - return 0; + return; } static irqreturn_t i2c_hid_irq(int irq, void *dev_id) @@ -429,8 +429,10 @@ static void i2c_hid_init_reports(struct hid_device *hid) struct i2c_hid *ihid = i2c_get_clientdata(client); u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL); - if (!inbuf) + if (!inbuf) { + dev_err(&client->dev, "can not retrieve initial reports\n"); return; + } list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) @@ -714,9 +716,7 @@ static int i2c_hid_hidinput_input_event(struct input_dev *dev, return -1; } - hid_set_field(field, offset, value); - - return 0; + return hid_set_field(field, offset, value); } static struct hid_ll_driver i2c_hid_ll_driver = { -- cgit v1.2.3 From 9972dcc29cd1fc1b550656eb04496df6dab3dc42 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:49 +0100 Subject: HID: i2c-hid: fix error messages Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index e0d097deb067..035a0cdd129e 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -740,10 +740,10 @@ static int __devinit i2c_hid_init_irq(struct i2c_client *client) IRQF_TRIGGER_FALLING | IRQF_ONESHOT, client->name, ihid); if (ret < 0) { - dev_dbg(&client->dev, + dev_warn(&client->dev, "Could not register for %s interrupt, irq = %d," " ret = %d\n", - client->name, client->irq, ret); + client->name, client->irq, ret); return ret; } @@ -769,7 +769,8 @@ static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) __func__, 4, ihid->hdesc_buffer); if (ret) { - dev_err(&client->dev, "HID_DESCR_LENGTH_CMD Fail (ret=%d)\n", + dev_err(&client->dev, + "unable to fetch the size of HID descriptor (ret=%d)\n", ret); return -ENODEV; } @@ -784,7 +785,7 @@ static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) /* check bcdVersion == 1.0 */ if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { dev_err(&client->dev, - "unexpected HID descriptor bcdVersion (0x%04x)\n", + "unexpected HID descriptor bcdVersion (0x%04hx)\n", le16_to_cpu(hdesc->bcdVersion)); return -ENODEV; } @@ -870,7 +871,7 @@ static int __devinit i2c_hid_probe(struct i2c_client *client, hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); hid->product = le16_to_cpu(ihid->hdesc.wProductID); - snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", + snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX", client->name, hid->vendor, hid->product); ret = hid_add_device(hid); -- cgit v1.2.3 From 134ebfd86b9353a3d98f9f4e93b4e79824a4b49a Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:54 +0100 Subject: HID: i2c-hid: also call i2c_hid_free_buffers in i2c_hid_remove In the case where the hid driver in charge of handling the hid part of the device (hid-generic for instance) fails at probe, neither i2c_hid_start nor i2c_hid_stop are called. Thus, the buffers allocated in i2c_hid_probe are never freed. Signed-off-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 035a0cdd129e..aab3357626c7 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -908,6 +908,9 @@ static int __devexit i2c_hid_remove(struct i2c_client *client) free_irq(client->irq, ihid); + if (ihid->bufsize) + i2c_hid_free_buffers(ihid); + kfree(ihid); return 0; -- cgit v1.2.3 From c737bcf92ea206d3b5d351890f1ade9e12ad4cbe Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:50 +0100 Subject: HID: i2c-hid: i2c_hid_get_report may fail If i2c_hid_get_report fails, exit i2c_hid_init_report. The printk log is already called by i2c_hid_get_report, so no need to add some more printks. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index aab3357626c7..c823a16f35ac 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -399,9 +399,10 @@ static void i2c_hid_init_report(struct hid_report *report, u8 *buffer, unsigned int size, ret_size; size = i2c_hid_get_report_length(report); - i2c_hid_get_report(client, + if (i2c_hid_get_report(client, report->type == HID_FEATURE_REPORT ? 0x03 : 0x01, - report->id, buffer, size); + report->id, buffer, size)) + return; i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf); -- cgit v1.2.3 From 6233362852785dbaffe1896d62ec834faf53fb57 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 4 Dec 2012 16:27:52 +0100 Subject: HID: i2c-hid: remove unneeded test in i2c_hid_remove ihid can not be null, so there are no reasons to test it. Signed-off-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index c823a16f35ac..3bbd70e839f2 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -901,9 +901,6 @@ static int __devexit i2c_hid_remove(struct i2c_client *client) struct i2c_hid *ihid = i2c_get_clientdata(client); struct hid_device *hid; - if (WARN_ON(!ihid)) - return -1; - hid = ihid->hid; hid_destroy_device(hid); -- cgit v1.2.3 From addb114dd646f9386f5845cdb059f22a497f9a9d Mon Sep 17 00:00:00 2001 From: Jiri Kosina Date: Wed, 5 Dec 2012 11:29:08 +0100 Subject: HID: i2c-hid: remove superfluous include The pointless WARN_ON() has been removed from i2c_hid_remove(), so we don't need bug.h any more. Reported-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 1 - 1 file changed, 1 deletion(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 3bbd70e839f2..7062df21da52 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -32,7 +32,6 @@ #include #include #include -#include #include #include -- cgit v1.2.3 From 27174cff8fcfcb3a60a3e7225cc627f6a4b7f827 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 5 Dec 2012 15:02:53 +0100 Subject: HID: i2c-hid: fix memory corruption due to missing hid declaration HID descriptors contains 4 bytes of reserved field. The previous implementation was overriding the next fields in struct i2c_hid. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 7062df21da52..34cca428a6a9 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -69,6 +69,7 @@ struct i2c_hid_desc { __le16 wVendorID; __le16 wProductID; __le16 wVersionID; + __le32 reserved; } __packed; struct i2c_hid_cmd { @@ -776,7 +777,13 @@ static int __devinit i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) } dsize = le16_to_cpu(hdesc->wHIDDescLength); - if (!dsize || dsize > HID_MAX_DESCRIPTOR_SIZE) { + /* + * the size of the HID descriptor should at least contain + * its size and the bcdVersion (4 bytes), and should not be greater + * than sizeof(struct i2c_hid_desc) as we directly fill this struct + * through i2c_hid_command. + */ + if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) { dev_err(&client->dev, "weird size of HID descriptor (%u)\n", dsize); return -ENODEV; -- cgit v1.2.3 From 29b45787d1f57f82a1c37ff5a47786c6a9dbedd8 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 5 Dec 2012 15:02:54 +0100 Subject: HID: i2c-hid: reorder allocation/free of buffers Simplifies i2c_hid_alloc_buffers tests, and makes this function responsible of the assignment of ihid->bufsize. The condition for the reallocation in i2c_hid_start is then simpler. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 68 ++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 40 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 34cca428a6a9..d00f1854cb82 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -464,48 +464,38 @@ static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, } } -static int i2c_hid_alloc_buffers(struct i2c_hid *ihid) +static void i2c_hid_free_buffers(struct i2c_hid *ihid) +{ + kfree(ihid->inbuf); + kfree(ihid->argsbuf); + kfree(ihid->cmdbuf); + ihid->inbuf = NULL; + ihid->cmdbuf = NULL; + ihid->argsbuf = NULL; + ihid->bufsize = 0; +} + +static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) { /* the worst case is computed from the set_report command with a * reportID > 15 and the maximum report length */ int args_len = sizeof(__u8) + /* optional ReportID byte */ sizeof(__u16) + /* data register */ sizeof(__u16) + /* size of the report */ - ihid->bufsize; /* report */ - - ihid->inbuf = kzalloc(ihid->bufsize, GFP_KERNEL); - - if (!ihid->inbuf) - return -ENOMEM; + report_size; /* report */ + ihid->inbuf = kzalloc(report_size, GFP_KERNEL); ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); - - if (!ihid->argsbuf) { - kfree(ihid->inbuf); - return -ENOMEM; - } - ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); - if (!ihid->cmdbuf) { - kfree(ihid->inbuf); - kfree(ihid->argsbuf); - ihid->inbuf = NULL; - ihid->argsbuf = NULL; + if (!ihid->inbuf || !ihid->argsbuf || !ihid->cmdbuf) { + i2c_hid_free_buffers(ihid); return -ENOMEM; } - return 0; -} + ihid->bufsize = report_size; -static void i2c_hid_free_buffers(struct i2c_hid *ihid) -{ - kfree(ihid->inbuf); - kfree(ihid->argsbuf); - kfree(ihid->cmdbuf); - ihid->inbuf = NULL; - ihid->cmdbuf = NULL; - ihid->argsbuf = NULL; + return 0; } static int i2c_hid_get_raw_report(struct hid_device *hid, @@ -610,22 +600,19 @@ static int i2c_hid_start(struct hid_device *hid) struct i2c_client *client = hid->driver_data; struct i2c_hid *ihid = i2c_get_clientdata(client); int ret; - int old_bufsize = ihid->bufsize; + unsigned int bufsize = HID_MIN_BUFFER_SIZE; - ihid->bufsize = HID_MIN_BUFFER_SIZE; - i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &ihid->bufsize); - i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &ihid->bufsize); - i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &ihid->bufsize); + i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); + i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); + i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); - if (ihid->bufsize > old_bufsize || !ihid->inbuf || !ihid->cmdbuf) { + if (bufsize > ihid->bufsize) { i2c_hid_free_buffers(ihid); - ret = i2c_hid_alloc_buffers(ihid); + ret = i2c_hid_alloc_buffers(ihid, bufsize); - if (ret) { - ihid->bufsize = old_bufsize; + if (ret) return ret; - } } if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS)) @@ -849,8 +836,9 @@ static int __devinit i2c_hid_probe(struct i2c_client *client, /* we need to allocate the command buffer without knowing the maximum * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the * real computation later. */ - ihid->bufsize = HID_MIN_BUFFER_SIZE; - i2c_hid_alloc_buffers(ihid); + ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); + if (ret < 0) + goto err; ret = i2c_hid_fetch_hid_descriptor(ihid); if (ret < 0) -- cgit v1.2.3 From 8a1bbb5319384dab6568ac2ce30d19b922413bec Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 5 Dec 2012 15:02:55 +0100 Subject: HID: i2c-hid: remove extra .irq field in struct i2c_hid There is no point in keeping the irq in i2c_hid as it's already there in client. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index d00f1854cb82..c6630d442e78 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -135,8 +135,6 @@ struct i2c_hid { unsigned long flags; /* device flags */ - int irq; /* the interrupt line irq */ - wait_queue_head_t wait; /* For waiting the interrupt */ }; @@ -736,8 +734,6 @@ static int __devinit i2c_hid_init_irq(struct i2c_client *client) return ret; } - ihid->irq = client->irq; - return 0; } @@ -851,7 +847,7 @@ static int __devinit i2c_hid_probe(struct i2c_client *client, hid = hid_allocate_device(); if (IS_ERR(hid)) { ret = PTR_ERR(hid); - goto err; + goto err_irq; } ihid->hid = hid; @@ -881,10 +877,10 @@ static int __devinit i2c_hid_probe(struct i2c_client *client, err_mem_free: hid_destroy_device(hid); -err: - if (ihid->irq) - free_irq(ihid->irq, ihid); +err_irq: + free_irq(client->irq, ihid); +err: i2c_hid_free_buffers(ihid); kfree(ihid); return ret; @@ -912,10 +908,9 @@ static int __devexit i2c_hid_remove(struct i2c_client *client) static int i2c_hid_suspend(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); - struct i2c_hid *ihid = i2c_get_clientdata(client); if (device_may_wakeup(&client->dev)) - enable_irq_wake(ihid->irq); + enable_irq_wake(client->irq); /* Save some power */ i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); -- cgit v1.2.3 From e5b50fe7bea43d0658773c89ba410ecc56867ee6 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 5 Dec 2012 15:02:56 +0100 Subject: HID: i2c-hid: fix i2c_hid_get_raw_report count mismatches The previous memcpy implementation relied on the size advertized by the device. There were no guarantees that buf was big enough. Some gymnastic is also required with the +2/-2 to take into account the first 2 bytes of the returned buffer where the total returned length is supplied by the device. Signed-off-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index c6630d442e78..ce01d5916184 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -502,23 +502,31 @@ static int i2c_hid_get_raw_report(struct hid_device *hid, { struct i2c_client *client = hid->driver_data; struct i2c_hid *ihid = i2c_get_clientdata(client); + size_t ret_count, ask_count; int ret; if (report_type == HID_OUTPUT_REPORT) return -EINVAL; - if (count > ihid->bufsize) - count = ihid->bufsize; + /* +2 bytes to include the size of the reply in the query buffer */ + ask_count = min(count + 2, (size_t)ihid->bufsize); ret = i2c_hid_get_report(client, report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, - report_number, ihid->inbuf, count); + report_number, ihid->inbuf, ask_count); if (ret < 0) return ret; - count = ihid->inbuf[0] | (ihid->inbuf[1] << 8); + ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8); + if (!ret_count) + return 0; + + ret_count = min(ret_count, ask_count); + + /* The query buffer contains the size, dropping it in the reply */ + count = min(count, ret_count - 2); memcpy(buf, ihid->inbuf + 2, count); return count; -- cgit v1.2.3 From 9afd09a1db93cc9084846c2b9bc0d1194743e5dc Mon Sep 17 00:00:00 2001 From: Jiri Kosina Date: Thu, 6 Dec 2012 10:59:28 +0100 Subject: HID: i2c-hid: fix ret_count check ret_count has to be at least 3, as we have to count the 2 bytes that are used for the size of the reply. Without this, memcpy() might be called with zero or negative count. Reported-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index ce01d5916184..6e1774c3a3f9 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -520,7 +520,7 @@ static int i2c_hid_get_raw_report(struct hid_device *hid, ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8); - if (!ret_count) + if (ret_count <= 2) return 0; ret_count = min(ret_count, ask_count); -- cgit v1.2.3 From 7a7d6d9c5fcd4b674da38e814cfc0724c67731b2 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 12 Dec 2012 18:00:02 +0100 Subject: HID: i2c-hid: add mutex protecting open/close race We should not enter close function while someone else is in open. This mutex prevents this race. There is also no need to override the ret value with -EIO in case of a failure of i2c_hid_set_power. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare Signed-off-by: Jiri Kosina --- drivers/hid/i2c-hid/i2c-hid.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'drivers/hid/i2c-hid') diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 6e1774c3a3f9..9ef222442ca0 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -117,6 +118,8 @@ static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; */ +static DEFINE_MUTEX(i2c_hid_open_mut); + /* The main device structure */ struct i2c_hid { struct i2c_client *client; /* i2c client */ @@ -641,17 +644,20 @@ static int i2c_hid_open(struct hid_device *hid) { struct i2c_client *client = hid->driver_data; struct i2c_hid *ihid = i2c_get_clientdata(client); - int ret; + int ret = 0; + mutex_lock(&i2c_hid_open_mut); if (!hid->open++) { ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); if (ret) { hid->open--; - return -EIO; + goto done; } set_bit(I2C_HID_STARTED, &ihid->flags); } - return 0; +done: + mutex_unlock(&i2c_hid_open_mut); + return ret; } static void i2c_hid_close(struct hid_device *hid) @@ -663,12 +669,14 @@ static void i2c_hid_close(struct hid_device *hid) * data acquistion due to a resumption we no longer * care about */ + mutex_lock(&i2c_hid_open_mut); if (!--hid->open) { clear_bit(I2C_HID_STARTED, &ihid->flags); /* Save some power */ i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); } + mutex_unlock(&i2c_hid_open_mut); } static int i2c_hid_power(struct hid_device *hid, int lvl) -- cgit v1.2.3