From patchwork Tue May 10 16:11:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Minyard X-Patchwork-Id: 67468 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp2212203qge; Tue, 10 May 2016 09:11:56 -0700 (PDT) X-Received: by 10.98.4.195 with SMTP id 186mr29377827pfe.154.1462896710536; Tue, 10 May 2016 09:11:50 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id yx4si3568837pac.109.2016.05.10.09.11.50; Tue, 10 May 2016 09:11:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-i2c-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752018AbcEJQLt (ORCPT + 1 other); Tue, 10 May 2016 12:11:49 -0400 Received: from mail-pa0-f66.google.com ([209.85.220.66]:34810 "EHLO mail-pa0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751782AbcEJQLs (ORCPT ); Tue, 10 May 2016 12:11:48 -0400 Received: by mail-pa0-f66.google.com with SMTP id yl2so1200716pac.1 for ; Tue, 10 May 2016 09:11:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=RW3XtPXO0aAv0USD5sx9v8qj6MeOjwcV/+xZV4wSQxA=; b=JD3x1aNIAH7VW6IpMVZexvN/d48Ti5cw7osf9RBL57s2xhh3NqG/k3fMuUvK3pqhOz 4dnX33SQcPRW9SrgrutssAAbHEnlRJ1d05teoPbLZL56Wk6ozXYLDxKwepSdtx6UZbgw kTigr49zctpUVMHfhpe0VgDtuTpxhHBjuekM5uH1Do36kxMYCBVg7VMhcJVbgAi1wZ59 pM5ibrjbKr7os4hf9voZWcbjFkAjTvc4MPxezivX/X43dpEj3QWQgZvcLbkj5DDk3lL2 6inAInbzNGIc4jIYKgzR72Ol4gDmegSvD8duEABelowAioXdCn353pGhi2DpMXobHxAD uhtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=RW3XtPXO0aAv0USD5sx9v8qj6MeOjwcV/+xZV4wSQxA=; b=dR8LkZvcaWtPSYrMPD6TbckdNfTOhdDDUpJlxyNcg23N6SuNdQMeGPcFsfnrSiyhUp x2DfhTrhjwHQbjjiF1sNeHvvFdmJ5baUKxrCKuleMNq9nnMdmfT0u/HnIHM2MScGsLK7 onS9k0dizVuO8bQ1poIR18k7zn9FwcWLjoGHm3uxib16WNqvW1EFtaEzFi/6OAplPuLf z6AHlWdkLQqf3u/UoE2LBb5QES+dAPUGYxXRrPKfm7jSAWShEoOlJ4e8BmTGVPmzXqWm j14rDrQRSc3x31lJPT83Gq20ixflmMzVFOAh/lgPhqUgYxyFKetxrpDynSiQmnTtZKz2 Q5gA== X-Gm-Message-State: AOPr4FVKDa6FwY9zEdJBDv+M4wxeuurxA5f7t5zfZLp5Gt33SKWXI/EBiRPFqQNthxutEw== X-Received: by 10.66.55.101 with SMTP id r5mr59765955pap.146.1462896707297; Tue, 10 May 2016 09:11:47 -0700 (PDT) Received: from serve.minyard.net ([108.19.215.157]) by smtp.gmail.com with ESMTPSA id u63sm5607718pfu.18.2016.05.10.09.11.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 May 2016 09:11:45 -0700 (PDT) Received: from t430.minyard.net (unknown [IPv6:2001:470:b8f6:1b:3ce3:7e2:9ad4:1f9]) by serve.minyard.net (Postfix) with ESMTPA id 8A9381984; Tue, 10 May 2016 11:11:43 -0500 (CDT) Received: by t430.minyard.net (Postfix, from userid 1000) id 0C74E300548; Tue, 10 May 2016 11:11:41 -0500 (CDT) From: minyard@acm.org To: Wolfram Sang , linux-i2c@vger.kernel.org, Jean Delvare Cc: minyard@acm.org, Corey Minyard Subject: [PATCH v2 1/3] i2c: Add parameters to sysfs-added i2c devices Date: Tue, 10 May 2016 11:11:37 -0500 Message-Id: <1462896699-1627-2-git-send-email-minyard@acm.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1462896699-1627-1-git-send-email-minyard@acm.org> References: <1462896699-1627-1-git-send-email-minyard@acm.org> Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org From: Corey Minyard Some devices might need parameters to control their operation, add the ability to pass these parameters to the client. This also makes the parsing of sysfs-added I2C devices a little more flexible, allowing tabs and arbitrary numbers of spaces. Signed-off-by: Corey Minyard --- drivers/i2c/i2c-core.c | 43 +++++++++++++++++++++++++++++++------------ include/linux/i2c.h | 3 +++ 2 files changed, 34 insertions(+), 12 deletions(-) -- 2.7.4 -- To unsubscribe from this list: send the line "unsubscribe linux-i2c" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index e584d88..069c385 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -780,7 +781,10 @@ static void i2c_device_shutdown(struct device *dev) static void i2c_client_dev_release(struct device *dev) { - kfree(to_i2c_client(dev)); + struct i2c_client *client = to_i2c_client(dev); + + kfree(client->parms); + kfree(client); } static ssize_t @@ -1047,6 +1051,13 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) client->flags = info->flags; client->addr = info->addr; client->irq = info->irq; + if (info->parms) { + client->parms = kstrdup(info->parms, GFP_KERNEL); + if (!client->parms) { + dev_err(&adap->dev, "Out of memory allocating parms\n"); + goto out_err_silent; + } + } strlcpy(client->name, info->type, sizeof(client->name)); @@ -1196,31 +1207,39 @@ i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, struct i2c_adapter *adap = to_i2c_adapter(dev); struct i2c_board_info info; struct i2c_client *client; - char *blank, end; + char *pos, end; int res; memset(&info, 0, sizeof(struct i2c_board_info)); - blank = strchr(buf, ' '); - if (!blank) { + pos = strpbrk(buf, " \t"); + if (!pos) { dev_err(dev, "%s: Missing parameters\n", "new_device"); return -EINVAL; } - if (blank - buf > I2C_NAME_SIZE - 1) { + if (pos - buf > I2C_NAME_SIZE - 1) { dev_err(dev, "%s: Invalid device name\n", "new_device"); return -EINVAL; } - memcpy(info.type, buf, blank - buf); + memcpy(info.type, buf, pos - buf); - /* Parse remaining parameters, reject extra parameters */ - res = sscanf(++blank, "%hi%c", &info.addr, &end); - if (res < 1) { + while (isspace(*pos)) + pos++; + + /* Parse address, saving remaining parameters. */ + res = sscanf(pos, "%hi%c", &info.addr, &end); + if (res < 1 || (res > 1 && !isspace(end))) { dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); return -EINVAL; } - if (res > 1 && end != '\n') { - dev_err(dev, "%s: Extra parameters\n", "new_device"); - return -EINVAL; + if (res > 1) { + /* Extra parms, skip the address and space. */ + while (!isspace(*pos)) + pos++; + while (isspace(*pos)) + pos++; + if (*pos) + info.parms = pos; } if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) { diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 200cf13b..35db0dd 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -225,6 +225,7 @@ struct i2c_client { char name[I2C_NAME_SIZE]; struct i2c_adapter *adapter; /* the adapter we sit on */ struct device dev; /* the device structure */ + char *parms; /* sysfs extra parms */ int irq; /* irq issued by device */ struct list_head detected; #if IS_ENABLED(CONFIG_I2C_SLAVE) @@ -282,6 +283,7 @@ static inline int i2c_slave_event(struct i2c_client *client, * @archdata: copied into i2c_client.dev.archdata * @of_node: pointer to OpenFirmware device node * @fwnode: device node supplied by the platform firmware + * @parms: Parameters supplied on the sysfs command line * @irq: stored in i2c_client.irq * * I2C doesn't actually support hardware probing, although controllers and @@ -303,6 +305,7 @@ struct i2c_board_info { struct dev_archdata *archdata; struct device_node *of_node; struct fwnode_handle *fwnode; + char *parms; int irq; };