From 9455e4c9abf76fa02170743859b2ddbb484e7fdf Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sat, 1 Jul 2006 17:16:57 +0200 Subject: i2c-dev: Cleanups i2c-dev: Cleanups * We no more need to include platform_device.h. * Delete the to_i2c_dev macro, which is no more used (and no more valid either.) * Drop i2c_dev.minor. Now that the minor number always matches the i2c adapter number, this field is redundant with i2c_dev.adap->nr. * Delete i2c_dev_get_by_adapter() which is now redundant with i2c_dev_get_by_minor() for the same reason. * Drop the local variable dev in i2cdev_attach_adapter(), we can easily do without it. Signed-off-by: Jean Delvare Signed-off-by: Greg Kroah-Hartman --- drivers/i2c/i2c-dev.c | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) (limited to 'drivers/i2c/i2c-dev.c') diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 58ccddd5c237..6e90dec02567 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -34,17 +34,14 @@ #include #include #include -#include #include static struct i2c_client i2cdev_client_template; struct i2c_dev { - int minor; struct i2c_adapter *adap; struct class_device *class_dev; }; -#define to_i2c_dev(d) container_of(d, struct i2c_dev, class_dev) #define I2C_MINORS 256 static struct i2c_dev *i2c_dev_array[I2C_MINORS]; @@ -60,18 +57,6 @@ static struct i2c_dev *i2c_dev_get_by_minor(unsigned index) return i2c_dev; } -static struct i2c_dev *i2c_dev_get_by_adapter(struct i2c_adapter *adap) -{ - struct i2c_dev *i2c_dev = NULL; - - spin_lock(&i2c_dev_array_lock); - if ((i2c_dev_array[adap->nr]) && - (i2c_dev_array[adap->nr]->adap == adap)) - i2c_dev = i2c_dev_array[adap->nr]; - spin_unlock(&i2c_dev_array_lock); - return i2c_dev; -} - static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; @@ -86,7 +71,7 @@ static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap) dev_err(&adap->dev, "i2c-dev already has a device assigned to this adapter\n"); goto error; } - i2c_dev->minor = adap->nr; + i2c_dev->adap = adap; i2c_dev_array[adap->nr] = i2c_dev; spin_unlock(&i2c_dev_array_lock); return i2c_dev; @@ -98,7 +83,7 @@ error: static void return_i2c_dev(struct i2c_dev *i2c_dev) { spin_lock(&i2c_dev_array_lock); - i2c_dev_array[i2c_dev->minor] = NULL; + i2c_dev_array[i2c_dev->adap->nr] = NULL; spin_unlock(&i2c_dev_array_lock); } @@ -415,21 +400,19 @@ static struct class *i2c_dev_class; static int i2cdev_attach_adapter(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; - struct device *dev; i2c_dev = get_free_i2c_dev(adap); if (IS_ERR(i2c_dev)) return PTR_ERR(i2c_dev); pr_debug("i2c-dev: adapter [%s] registered as minor %d\n", - adap->name, i2c_dev->minor); + adap->name, adap->nr); /* register this i2c device with the driver core */ - i2c_dev->adap = adap; - dev = &adap->dev; i2c_dev->class_dev = class_device_create(i2c_dev_class, NULL, - MKDEV(I2C_MAJOR, i2c_dev->minor), - dev, "i2c-%d", i2c_dev->minor); + MKDEV(I2C_MAJOR, adap->nr), + &adap->dev, "i2c-%d", + adap->nr); if (!i2c_dev->class_dev) goto error; class_device_create_file(i2c_dev->class_dev, &class_device_attr_name); @@ -444,12 +427,12 @@ static int i2cdev_detach_adapter(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; - i2c_dev = i2c_dev_get_by_adapter(adap); + i2c_dev = i2c_dev_get_by_minor(adap->nr); if (!i2c_dev) return -ENODEV; return_i2c_dev(i2c_dev); - class_device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, i2c_dev->minor)); + class_device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); kfree(i2c_dev); pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name); -- cgit From f3b3aadbbd66d8a020550b01b37d9b1ea559f2c3 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sat, 1 Jul 2006 17:17:38 +0200 Subject: i2c-dev: Use a list for data storage i2c-dev: Use a list for data storage Use a list instead of a static array for storing the i2c-dev data. Given that most systems have less than 10 i2c busses, most of the space was wasted, so this saves around 1 kB of memory (2 kB on 64-bit archs.) The drawback is that lookup was in O(1) and is now in O(N), but given that the values of N are always small, I don't think this is a problem. Signed-off-by: Jean Delvare Signed-off-by: Greg Kroah-Hartman --- drivers/i2c/i2c-dev.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'drivers/i2c/i2c-dev.c') diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 6e90dec02567..206e8052f90f 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -39,21 +40,27 @@ static struct i2c_client i2cdev_client_template; struct i2c_dev { + struct list_head list; struct i2c_adapter *adap; struct class_device *class_dev; }; #define I2C_MINORS 256 -static struct i2c_dev *i2c_dev_array[I2C_MINORS]; -static DEFINE_SPINLOCK(i2c_dev_array_lock); +static LIST_HEAD(i2c_dev_list); +static DEFINE_SPINLOCK(i2c_dev_list_lock); static struct i2c_dev *i2c_dev_get_by_minor(unsigned index) { struct i2c_dev *i2c_dev; - spin_lock(&i2c_dev_array_lock); - i2c_dev = i2c_dev_array[index]; - spin_unlock(&i2c_dev_array_lock); + spin_lock(&i2c_dev_list_lock); + list_for_each_entry(i2c_dev, &i2c_dev_list, list) { + if (i2c_dev->adap->nr == index) + goto found; + } + i2c_dev = NULL; +found: + spin_unlock(&i2c_dev_list_lock); return i2c_dev; } @@ -61,30 +68,28 @@ static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; + if (adap->nr >= I2C_MINORS) { + printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n", + adap->nr); + return ERR_PTR(-ENODEV); + } + i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL); if (!i2c_dev) return ERR_PTR(-ENOMEM); - - spin_lock(&i2c_dev_array_lock); - if (i2c_dev_array[adap->nr]) { - spin_unlock(&i2c_dev_array_lock); - dev_err(&adap->dev, "i2c-dev already has a device assigned to this adapter\n"); - goto error; - } i2c_dev->adap = adap; - i2c_dev_array[adap->nr] = i2c_dev; - spin_unlock(&i2c_dev_array_lock); + + spin_lock(&i2c_dev_list_lock); + list_add_tail(&i2c_dev->list, &i2c_dev_list); + spin_unlock(&i2c_dev_list_lock); return i2c_dev; -error: - kfree(i2c_dev); - return ERR_PTR(-ENODEV); } static void return_i2c_dev(struct i2c_dev *i2c_dev) { - spin_lock(&i2c_dev_array_lock); - i2c_dev_array[i2c_dev->adap->nr] = NULL; - spin_unlock(&i2c_dev_array_lock); + spin_lock(&i2c_dev_list_lock); + list_del(&i2c_dev->list); + spin_unlock(&i2c_dev_list_lock); } static ssize_t show_adapter_name(struct class_device *class_dev, char *buf) -- cgit From 22f76e744dc41096987c6df8270b5c249511cde5 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sat, 1 Jul 2006 17:20:57 +0200 Subject: i2c-dev: Drop the client template i2c-dev: Drop the client template Drop the i2c-dev client template. This saves about 360 bytes of memory. I got the idea from a similar cleanup Hans-Frieder Vogt made to i2c-nforce2 recently. Signed-off-by: Jean Delvare Signed-off-by: Greg Kroah-Hartman --- drivers/i2c/i2c-dev.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'drivers/i2c/i2c-dev.c') diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 206e8052f90f..2ce083391292 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -37,7 +37,7 @@ #include #include -static struct i2c_client i2cdev_client_template; +static struct i2c_driver i2cdev_driver; struct i2c_dev { struct list_head list; @@ -365,12 +365,13 @@ static int i2cdev_open(struct inode *inode, struct file *file) if (!adap) return -ENODEV; - client = kmalloc(sizeof(*client), GFP_KERNEL); + client = kzalloc(sizeof(*client), GFP_KERNEL); if (!client) { i2c_put_adapter(adap); return -ENOMEM; } - memcpy(client, &i2cdev_client_template, sizeof(*client)); + snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr); + client->driver = &i2cdev_driver; /* registered with adapter, passed as client to user */ client->adapter = adap; @@ -459,12 +460,6 @@ static struct i2c_driver i2cdev_driver = { .detach_client = i2cdev_detach_client, }; -static struct i2c_client i2cdev_client_template = { - .name = "I2C /dev entry", - .addr = -1, - .driver = &i2cdev_driver, -}; - static int __init i2c_dev_init(void) { int res; -- cgit From defcb46ed4666095677c8f52904b9e328587a20d Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Tue, 15 Aug 2006 18:30:24 +0200 Subject: i2c: __must_check fixes, i2c-dev i2c: __must_check fixes (i2c-dev) Check for error on sysfs file creation. Check for error on device creation. Delete sysfs file on device destruction. I couldn't test this one beyond compilation, as it applies on top of another patch in Greg's tree [1] which breaks all my systems when I apply it (my udev isn't recent enough.) Anyone with bleeding edge udev is welcome to test and report. [1] http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/i2c/i2c-dev-device.patch Signed-off-by: Jean Delvare Signed-off-by: Greg Kroah-Hartman --- drivers/i2c/i2c-dev.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'drivers/i2c/i2c-dev.c') diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 2ce083391292..567fb05aeccc 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -406,6 +406,7 @@ static struct class *i2c_dev_class; static int i2cdev_attach_adapter(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; + int res; i2c_dev = get_free_i2c_dev(adap); if (IS_ERR(i2c_dev)) @@ -419,14 +420,20 @@ static int i2cdev_attach_adapter(struct i2c_adapter *adap) MKDEV(I2C_MAJOR, adap->nr), &adap->dev, "i2c-%d", adap->nr); - if (!i2c_dev->class_dev) + if (!i2c_dev->class_dev) { + res = -ENODEV; goto error; - class_device_create_file(i2c_dev->class_dev, &class_device_attr_name); + } + res = class_device_create_file(i2c_dev->class_dev, &class_device_attr_name); + if (res) + goto error_destroy; return 0; +error_destroy: + class_device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); error: return_i2c_dev(i2c_dev); kfree(i2c_dev); - return -ENODEV; + return res; } static int i2cdev_detach_adapter(struct i2c_adapter *adap) @@ -437,6 +444,7 @@ static int i2cdev_detach_adapter(struct i2c_adapter *adap) if (!i2c_dev) return -ENODEV; + class_device_remove_file(i2c_dev->class_dev, &class_device_attr_name); return_i2c_dev(i2c_dev); class_device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); kfree(i2c_dev); -- cgit From b32d20dc8b187e03605f091dbde9a78676a2a642 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 3 Sep 2006 22:19:25 +0200 Subject: i2c-dev: attach/detach_adapter cleanups i2c-dev: attach/detach_adapter cleanups * Only print that an adapter was attached when it succeeds. * i2c_dev == NULL on detach simply means that the attach failed before, this isn't an error per se. Signed-off-by: Jean Delvare Signed-off-by: Greg Kroah-Hartman --- drivers/i2c/i2c-dev.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'drivers/i2c/i2c-dev.c') diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 567fb05aeccc..3f869033ed70 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -412,9 +412,6 @@ static int i2cdev_attach_adapter(struct i2c_adapter *adap) if (IS_ERR(i2c_dev)) return PTR_ERR(i2c_dev); - pr_debug("i2c-dev: adapter [%s] registered as minor %d\n", - adap->name, adap->nr); - /* register this i2c device with the driver core */ i2c_dev->class_dev = class_device_create(i2c_dev_class, NULL, MKDEV(I2C_MAJOR, adap->nr), @@ -427,6 +424,9 @@ static int i2cdev_attach_adapter(struct i2c_adapter *adap) res = class_device_create_file(i2c_dev->class_dev, &class_device_attr_name); if (res) goto error_destroy; + + pr_debug("i2c-dev: adapter [%s] registered as minor %d\n", + adap->name, adap->nr); return 0; error_destroy: class_device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); @@ -441,8 +441,8 @@ static int i2cdev_detach_adapter(struct i2c_adapter *adap) struct i2c_dev *i2c_dev; i2c_dev = i2c_dev_get_by_minor(adap->nr); - if (!i2c_dev) - return -ENODEV; + if (!i2c_dev) /* attach_adapter must have failed */ + return 0; class_device_remove_file(i2c_dev->class_dev, &class_device_attr_name); return_i2c_dev(i2c_dev); -- cgit