Add parameters to sysfs added i2c devices
Subsystems (3)
cover Add parameters to sysfs added i2c devices
When I was testing with qemu, I needed a way to pass in the irq to the smbus alert handler, and I needed a way to set some IPMI SSIF parameters. These patches add a way to pass in parameters to sysfs, show how the IPMI SSIF driver can use this, and then adds handling of irq setup for the i2c-smbus alert handler.
-corey
PATCH 1/3 i2c: Add parameters to sysfs-added i2c devices
From: Corey Minyard <[email protected]>
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.
--- drivers/i2c/i2c-core.c | 46 ++++++++++++++++++++++++++++++++++------------ include/linux/i2c.h | 3 +++ 2 files changed, 37 insertions(+), 12 deletions(-)
drivers/i2c/i2c-core.c +34 -12
@@ -785,7 +785,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
@@ -1052,6 +1055,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));
@@ -1201,31 +1211,43 @@ 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 || !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 && end !='\n' ) { +if (isspace (end)) { +/* Extra parms, skip the address and space. */ +while (!isspace (*pos)) + pos++; +while (isspace (*pos)) + pos++; + info.parms = pos; + }else { +dev_err (dev,"%s: Extra parameters\n" ,"new_device" ); +return -EINVAL ; + } }if ((info.addr &I2C_ADDR_OFFSET_TEN_BIT ) ==I2C_ADDR_OFFSET_TEN_BIT ) {
include/linux/i2c.h +3 -0
@@ -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; };
Signature · 1 line
2.5.0
PATCH 2/3 ipmi: Handle I2C parms in the SSIF driver.
In reply to [email protected]From: Corey Minyard <[email protected]>
--- drivers/char/ipmi/ipmi_ssif.c | 62 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-)
drivers/char/ipmi/ipmi_ssif.c +61 -1
@@ -200,7 +200,7 @@ struct ssif_info {struct ipmi_smi_msg *waiting_msg;struct ipmi_smi_msg *curr_msg;enum ssif_intf_state ssif_state; -unsigned long ssif_debug; +unsigned int ssif_debug;struct ipmi_smi_handlers handlers;
@@ -1386,6 +1386,60 @@ restart:return found; } +static int ssif_parse_parms (struct ssif_info *ssif_info, +const char *parms,u8 *slave_addr) +{ +int rv; +char end; + +if (!parms) +return 0 ; + +while (*parms) { +const char *next = parms; +const char *val; +int parmlen; + +while (*next && !isspace (*next) && *next !='=' ) + next++; + +if (*next !='=' ) { +pr_err ("IPMI SSIF invalid parm starting at %s\n" , + parms); +return -EINVAL ; + } + + parmlen = next - parms; + next++; + val = next; +while (*next && !isspace (*next)) + next++; + +if (strncmp (parms,"ipmb" , parmlen) ==0 ) { + rv =sscanf (val,"%hhx%c" , slave_addr, &end); +if ((rv <1 ) || ((rv >1 ) && !isspace (end))) { +pr_err ("Invalid ipmb address: %s\n" , val); +return -EINVAL ; + } + }else if (strncmp (parms,"debug" , parmlen) ==0 ) { + rv =sscanf (val,"%i%c" , &ssif_info->ssif_debug, &end); +if ((rv <1 ) || ((rv >1 ) && !isspace (end))) { +pr_err ("Invalid debug value: %s\n" , val); +return -EINVAL ; + } + }else { +pr_err ("Invalid IPMI SSIF parameter: %s\n" , parms); +return -EINVAL ; + } + +while (*next &&isspace (*next)) + next++; + parms = next; + } + +return 0 ; +} +static bool check_acpi (struct ssif_info *ssif_info,struct device *dev) {#ifdef CONFIG_ACPI
@@ -1435,6 +1489,12 @@ static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)if (!addr_info) {/* Must have come in through sysfs. */ ssif_info->addr_source =SI_HOTMOD ; + rv =ssif_parse_parms (ssif_info, client->parms, + &slave_addr); +if (rv) { +pr_err (PFX "Unable to parse parms from i2c\n" ); + goto out; + } }else { ssif_info->addr_source = addr_info->addr_src; ssif_info->ssif_debug = addr_info->debug;
Signature · 1 line
2.5.0
PATCH 3/3 i2c-smbus: Allow parms to be passed in from sysfs new_device
In reply to [email protected]From: Corey Minyard <[email protected]>
This allows "irq=<num>" and "edge" to be passed in to the i2c new_device after the name and i2c address. This way the alert can be configured without having to have platform data.
--- drivers/i2c/i2c-smbus.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)
drivers/i2c/i2c-smbus.c +72 -0
@@ -23,6 +23,7 @@#include <linux/i2c.h> #include <linux/i2c-smbus.h> #include <linux/slab.h> +#include <linux/ctype.h> struct i2c_smbus_alert {unsigned int alert_edge_triggered:1 ;
@@ -123,15 +124,86 @@ static irqreturn_t smbalert_irq(int irq, void *d)return IRQ_HANDLED ; } +static struct i2c_smbus_alert_setup *smbalert_parse_parms (struct device *dev, +const char *parms, +struct i2c_smbus_alert_setup *data) +{ +int rv; +char end; + +if (!parms) +return NULL ; + + data->alert_edge_triggered = false; +while (*parms) { +const char *next = parms; +const char *val; +int parmlen; + +while (*next && !isspace (*next) && *next !='=' ) + next++; + + parmlen = next - parms; + +if (*next =='=' ) { + next++; + val = next; +while (*next && !isspace (*next)) + next++; + }else { + val =NULL ; + } + +if (strncmp (parms,"irq" , parmlen) ==0 ) { +if (!val) { +dev_err (dev,"no irq parm value given\n" ); +return NULL ; + } + rv =sscanf (val,"%d%c" , &data->irq, &end); +if ((rv <1 ) || ((rv >1 ) && !isspace (end))) { +dev_err (dev,"Invalid irq parm: %s\n" , val); +return NULL ; + } + }else if (strncmp (parms,"edge" , parmlen) ==0 ) { +if (val) { +dev_err (dev,"Value given with edge parm: %s\n" , + val); +return NULL ; + } + data->alert_edge_triggered = true; + }else { +dev_err (dev,"Invalid parameter: %s\n" , parms); +return NULL ; + } + +while (*next &&isspace (*next)) + next++; + parms = next; + } + +return data; +} +/* Setup SMBALERT# infrastructure */ static int smbalert_probe (struct i2c_client *ara,const struct i2c_device_id *id) {struct i2c_smbus_alert_setup *setup =dev_get_platdata (&ara->dev); +struct i2c_smbus_alert_setup dummy_setup;struct i2c_smbus_alert *alert;struct i2c_adapter *adapter = ara->adapter;int res; +if (ara->parms) +/* Came in through sysfs. */ + setup =smbalert_parse_parms (&ara->dev, ara->parms, + &dummy_setup); + +if (!setup) { +dev_err (&ara->dev,"SMBus alert without setup\n" ); +return -ENODEV ; + } + alert =devm_kzalloc (&ara->dev,sizeof (struct i2c_smbus_alert ),GFP_KERNEL );if (!alert)
Signature · 1 line
2.5.0