Commit fe9f8bbe authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Linus Torvalds

[PATCH] i2c-dev cleanup

(based on lm_sensors CVS)
parent 47bc91ff
...@@ -77,6 +77,7 @@ static struct file_operations i2cdev_fops = { ...@@ -77,6 +77,7 @@ static struct file_operations i2cdev_fops = {
static struct i2c_adapter *i2cdev_adaps[I2CDEV_ADAPS_MAX]; static struct i2c_adapter *i2cdev_adaps[I2CDEV_ADAPS_MAX];
static struct i2c_driver i2cdev_driver = { static struct i2c_driver i2cdev_driver = {
.owner = THIS_MODULE,
.name = "i2c-dev dummy driver", .name = "i2c-dev dummy driver",
.id = I2C_DRIVERID_I2CDEV, .id = I2C_DRIVERID_I2CDEV,
.flags = I2C_DF_DUMMY, .flags = I2C_DF_DUMMY,
...@@ -340,47 +341,42 @@ int i2cdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, ...@@ -340,47 +341,42 @@ int i2cdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
return 0; return 0;
} }
int i2cdev_open (struct inode *inode, struct file *file) static int i2cdev_open(struct inode *inode, struct file *file)
{ {
unsigned int minor = minor(inode->i_rdev); unsigned int minor = minor(inode->i_rdev);
struct i2c_client *client; struct i2c_client *client;
if ((minor >= I2CDEV_ADAPS_MAX) || ! (i2cdev_adaps[minor])) { if ((minor >= I2CDEV_ADAPS_MAX) || !(i2cdev_adaps[minor]))
#ifdef DEBUG
printk(KERN_DEBUG "i2c-dev.o: Trying to open unattached adapter i2c-%d\n",
minor);
#endif
return -ENODEV; return -ENODEV;
}
/* Note that we here allocate a client for later use, but we will *not* client = kmalloc(sizeof(*client), GFP_KERNEL);
register this client! Yes, this is safe. No, it is not very clean. */ if (!client)
if(! (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
return -ENOMEM; return -ENOMEM;
memcpy(client,&i2cdev_client_template,sizeof(struct i2c_client)); memcpy(client, &i2cdev_client_template, sizeof(*client));
/* registered with adapter, passed as client to user */
client->adapter = i2cdev_adaps[minor]; client->adapter = i2cdev_adaps[minor];
file->private_data = client; file->private_data = client;
if (!try_module_get(i2cdev_adaps[minor]->owner)) { /* use adapter module, i2c-dev handled with fops */
kfree(client); if (!try_module_get(client->adapter->owner))
return -ENODEV; goto out_kfree;
}
#ifdef DEBUG
printk(KERN_DEBUG "i2c-dev.o: opened i2c-%d\n",minor);
#endif
return 0; return 0;
out_kfree:
kfree(client);
return -ENODEV;
} }
static int i2cdev_release (struct inode *inode, struct file *file) static int i2cdev_release(struct inode *inode, struct file *file)
{ {
unsigned int minor = minor(inode->i_rdev); struct i2c_client *client = file->private_data;
kfree(file->private_data);
file->private_data=NULL; module_put(client->adapter->owner);
#ifdef DEBUG kfree(client);
printk(KERN_DEBUG "i2c-dev.o: Closed: i2c-%d\n", minor); file->private_data = NULL;
#endif
module_put(i2cdev_adaps[minor]->owner);
return 0; return 0;
} }
......
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