Commit d096d1ee authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

[PATCH] I2C: keep i2c-dev numbers in sync with i2c adapter numbers

This makes userspace tools easier to figure out which i2c-dev device is
assigned to which i2c adapter.

Yes, we can overflow the i2c dev array right now, but that would take a
lot of i2c adapter modprobe/rmmod cycles.  That will be fixed up soon.
parent 2c942813
...@@ -72,24 +72,18 @@ struct i2c_dev *i2c_dev_get_by_minor(unsigned index) ...@@ -72,24 +72,18 @@ struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
struct i2c_dev *i2c_dev_get_by_adapter(struct i2c_adapter *adap) struct i2c_dev *i2c_dev_get_by_adapter(struct i2c_adapter *adap)
{ {
struct i2c_dev *i2c_dev = NULL; struct i2c_dev *i2c_dev = NULL;
int i;
spin_lock(&i2c_dev_array_lock); spin_lock(&i2c_dev_array_lock);
for (i = 0; i < I2C_MINORS; ++i) { if ((i2c_dev_array[adap->nr]) &&
if ((i2c_dev_array[i]) && (i2c_dev_array[adap->nr]->adap == adap))
(i2c_dev_array[i]->adap == adap)) { i2c_dev = i2c_dev_array[adap->nr];
i2c_dev = i2c_dev_array[i];
break;
}
}
spin_unlock(&i2c_dev_array_lock); spin_unlock(&i2c_dev_array_lock);
return i2c_dev; return i2c_dev;
} }
static struct i2c_dev *get_free_i2c_dev(void) static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
{ {
struct i2c_dev *i2c_dev; struct i2c_dev *i2c_dev;
unsigned int i;
i2c_dev = kmalloc(sizeof(*i2c_dev), GFP_KERNEL); i2c_dev = kmalloc(sizeof(*i2c_dev), GFP_KERNEL);
if (!i2c_dev) if (!i2c_dev)
...@@ -97,15 +91,16 @@ static struct i2c_dev *get_free_i2c_dev(void) ...@@ -97,15 +91,16 @@ static struct i2c_dev *get_free_i2c_dev(void)
memset(i2c_dev, 0x00, sizeof(*i2c_dev)); memset(i2c_dev, 0x00, sizeof(*i2c_dev));
spin_lock(&i2c_dev_array_lock); spin_lock(&i2c_dev_array_lock);
for (i = 0; i < I2C_MINORS; ++i) { if (i2c_dev_array[adap->nr]) {
if (i2c_dev_array[i])
continue;
i2c_dev->minor = i;
i2c_dev_array[i] = i2c_dev;
spin_unlock(&i2c_dev_array_lock); spin_unlock(&i2c_dev_array_lock);
return i2c_dev; dev_err(&adap->dev, "i2c-dev already has a device assigned to this adapter\n");
goto error;
} }
i2c_dev->minor = adap->nr;
i2c_dev_array[adap->nr] = i2c_dev;
spin_unlock(&i2c_dev_array_lock); spin_unlock(&i2c_dev_array_lock);
return i2c_dev;
error:
kfree(i2c_dev); kfree(i2c_dev);
return ERR_PTR(-ENODEV); return ERR_PTR(-ENODEV);
} }
...@@ -446,7 +441,7 @@ static int i2cdev_attach_adapter(struct i2c_adapter *adap) ...@@ -446,7 +441,7 @@ static int i2cdev_attach_adapter(struct i2c_adapter *adap)
struct i2c_dev *i2c_dev; struct i2c_dev *i2c_dev;
int retval; int retval;
i2c_dev = get_free_i2c_dev(); i2c_dev = get_free_i2c_dev(adap);
if (IS_ERR(i2c_dev)) if (IS_ERR(i2c_dev))
return PTR_ERR(i2c_dev); return PTR_ERR(i2c_dev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment