diff options
Diffstat (limited to 'drivers/w1/w1.c')
-rw-r--r-- | drivers/w1/w1.c | 263 |
1 files changed, 176 insertions, 87 deletions
diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c index a698b517e86..de3e9791f80 100644 --- a/drivers/w1/w1.c +++ b/drivers/w1/w1.c @@ -35,7 +35,6 @@ #include <asm/atomic.h> #include "w1.h" -#include "w1_io.h" #include "w1_log.h" #include "w1_int.h" #include "w1_family.h" @@ -55,7 +54,7 @@ module_param_named(control_timeout, w1_control_timeout, int, 0); module_param_named(max_slave_count, w1_max_slave_count, int, 0); module_param_named(slave_ttl, w1_max_slave_ttl, int, 0); -DEFINE_SPINLOCK(w1_mlock); +DEFINE_MUTEX(w1_mlock); LIST_HEAD(w1_masters); static struct task_struct *w1_control_thread; @@ -75,8 +74,6 @@ static void w1_master_release(struct device *dev) struct w1_master *md = dev_to_w1_master(dev); dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name); - - dev_fini_netlink(md); memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master)); kfree(md); } @@ -85,10 +82,10 @@ static void w1_slave_release(struct device *dev) { struct w1_slave *sl = dev_to_w1_slave(dev); - dev_dbg(dev, "%s: Releasing %s.\n", __func__, sl->name); + printk("%s: Releasing %s.\n", __func__, sl->name); while (atomic_read(&sl->refcnt)) { - dev_dbg(dev, "Waiting for %s to become free: refcnt=%d.\n", + printk("Waiting for %s to become free: refcnt=%d.\n", sl->name, atomic_read(&sl->refcnt)); if (msleep_interruptible(1000)) flush_signals(current); @@ -111,7 +108,6 @@ static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, siz { struct w1_slave *sl = kobj_to_w1_slave(kobj); - atomic_inc(&sl->refcnt); if (off > 8) { count = 0; } else { @@ -120,7 +116,6 @@ static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, siz memcpy(buf, (u8 *)&sl->reg_num, count); } - atomic_dec(&sl->refcnt); return count; } @@ -139,7 +134,63 @@ static struct bin_attribute w1_slave_attr_bin_id = { }; /* Default family */ -static struct w1_family w1_default_family; + +static ssize_t w1_default_write(struct kobject *kobj, char *buf, loff_t off, size_t count) +{ + struct w1_slave *sl = kobj_to_w1_slave(kobj); + + mutex_lock(&sl->master->mutex); + if (w1_reset_select_slave(sl)) { + count = 0; + goto out_up; + } + + w1_write_block(sl->master, buf, count); + +out_up: + mutex_unlock(&sl->master->mutex); + return count; +} + +static ssize_t w1_default_read(struct kobject *kobj, char *buf, loff_t off, size_t count) +{ + struct w1_slave *sl = kobj_to_w1_slave(kobj); + + mutex_lock(&sl->master->mutex); + w1_read_block(sl->master, buf, count); + mutex_unlock(&sl->master->mutex); + return count; +} + +static struct bin_attribute w1_default_attr = { + .attr = { + .name = "rw", + .mode = S_IRUGO | S_IWUSR, + .owner = THIS_MODULE, + }, + .size = PAGE_SIZE, + .read = w1_default_read, + .write = w1_default_write, +}; + +static int w1_default_add_slave(struct w1_slave *sl) +{ + return sysfs_create_bin_file(&sl->dev.kobj, &w1_default_attr); +} + +static void w1_default_remove_slave(struct w1_slave *sl) +{ + sysfs_remove_bin_file(&sl->dev.kobj, &w1_default_attr); +} + +static struct w1_family_ops w1_default_fops = { + .add_slave = w1_default_add_slave, + .remove_slave = w1_default_remove_slave, +}; + +static struct w1_family w1_default_family = { + .fops = &w1_default_fops, +}; static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size); @@ -183,12 +234,9 @@ static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_a struct w1_master *md = dev_to_w1_master(dev); ssize_t count; - if (down_interruptible (&md->mutex)) - return -EBUSY; - + mutex_lock(&md->mutex); count = sprintf(buf, "%s\n", md->name); - - up(&md->mutex); + mutex_unlock(&md->mutex); return count; } @@ -199,12 +247,9 @@ static ssize_t w1_master_attribute_store_search(struct device * dev, { struct w1_master *md = dev_to_w1_master(dev); - if (down_interruptible (&md->mutex)) - return -EBUSY; - + mutex_lock(&md->mutex); md->search_count = simple_strtol(buf, NULL, 0); - - up(&md->mutex); + mutex_unlock(&md->mutex); return count; } @@ -216,12 +261,9 @@ static ssize_t w1_master_attribute_show_search(struct device *dev, struct w1_master *md = dev_to_w1_master(dev); ssize_t count; - if (down_interruptible (&md->mutex)) - return -EBUSY; - + mutex_lock(&md->mutex); count = sprintf(buf, "%d\n", md->search_count); - - up(&md->mutex); + mutex_unlock(&md->mutex); return count; } @@ -231,12 +273,9 @@ static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct devic struct w1_master *md = dev_to_w1_master(dev); ssize_t count; - if (down_interruptible(&md->mutex)) - return -EBUSY; - + mutex_lock(&md->mutex); count = sprintf(buf, "0x%p\n", md->bus_master); - - up(&md->mutex); + mutex_unlock(&md->mutex); return count; } @@ -252,12 +291,9 @@ static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, stru struct w1_master *md = dev_to_w1_master(dev); ssize_t count; - if (down_interruptible(&md->mutex)) - return -EBUSY; - + mutex_lock(&md->mutex); count = sprintf(buf, "%d\n", md->max_slave_count); - - up(&md->mutex); + mutex_unlock(&md->mutex); return count; } @@ -266,12 +302,9 @@ static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct devi struct w1_master *md = dev_to_w1_master(dev); ssize_t count; - if (down_interruptible(&md->mutex)) - return -EBUSY; - + mutex_lock(&md->mutex); count = sprintf(buf, "%lu\n", md->attempts); - - up(&md->mutex); + mutex_unlock(&md->mutex); return count; } @@ -280,12 +313,9 @@ static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct d struct w1_master *md = dev_to_w1_master(dev); ssize_t count; - if (down_interruptible(&md->mutex)) - return -EBUSY; - + mutex_lock(&md->mutex); count = sprintf(buf, "%d\n", md->slave_count); - - up(&md->mutex); + mutex_unlock(&md->mutex); return count; } @@ -294,8 +324,7 @@ static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device struct w1_master *md = dev_to_w1_master(dev); int c = PAGE_SIZE; - if (down_interruptible(&md->mutex)) - return -EBUSY; + mutex_lock(&md->mutex); if (md->slave_count == 0) c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n"); @@ -310,7 +339,7 @@ static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device } } - up(&md->mutex); + mutex_unlock(&md->mutex); return PAGE_SIZE - c; } @@ -362,7 +391,8 @@ static void w1_destroy_master_attributes(struct w1_master *master) } #ifdef CONFIG_HOTPLUG -static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) +static int w1_uevent(struct device *dev, char **envp, int num_envp, + char *buffer, int buffer_size) { struct w1_master *md = NULL; struct w1_slave *sl = NULL; @@ -382,7 +412,8 @@ static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer return -EINVAL; } - dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", event_owner, name, dev->bus_id); + dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", + event_owner, name, dev->bus_id); if (dev->driver != &w1_slave_driver || !sl) return 0; @@ -401,7 +432,8 @@ static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer return 0; }; #else -static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) +static int w1_uevent(struct device *dev, char **envp, int num_envp, + char *buffer, int buffer_size) { return 0; } @@ -425,7 +457,8 @@ static int __w1_attach_slave_device(struct w1_slave *sl) (unsigned int) sl->reg_num.family, (unsigned long long) sl->reg_num.id); - dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, &sl->dev.bus_id[0]); + dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, + &sl->dev.bus_id[0]); err = device_register(&sl->dev); if (err < 0) { @@ -496,6 +529,7 @@ static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn) sl->master = dev; set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); + memset(&msg, 0, sizeof(msg)); memcpy(&sl->reg_num, rn, sizeof(sl->reg_num)); atomic_set(&sl->refcnt, 0); init_completion(&sl->released); @@ -526,7 +560,7 @@ static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn) sl->ttl = dev->slave_ttl; dev->slave_count++; - memcpy(&msg.id.id, rn, sizeof(msg.id.id)); + memcpy(msg.id.id, rn, sizeof(msg.id)); msg.type = W1_SLAVE_ADD; w1_netlink_send(dev, &msg); @@ -544,7 +578,8 @@ static void w1_slave_detach(struct w1_slave *sl) if (sl->family->fops && sl->family->fops->remove_slave) sl->family->fops->remove_slave(sl); - memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id)); + memset(&msg, 0, sizeof(msg)); + memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id)); msg.type = W1_SLAVE_REMOVE; w1_netlink_send(sl->master, &msg); @@ -561,7 +596,7 @@ static struct w1_master *w1_search_master(void *data) struct w1_master *dev; int found = 0; - spin_lock_bh(&w1_mlock); + mutex_lock(&w1_mlock); list_for_each_entry(dev, &w1_masters, w1_master_entry) { if (dev->bus_master->data == data) { found = 1; @@ -569,22 +604,69 @@ static struct w1_master *w1_search_master(void *data) break; } } - spin_unlock_bh(&w1_mlock); + mutex_unlock(&w1_mlock); + + return (found)?dev:NULL; +} + +struct w1_master *w1_search_master_id(u32 id) +{ + struct w1_master *dev; + int found = 0; + + mutex_lock(&w1_mlock); + list_for_each_entry(dev, &w1_masters, w1_master_entry) { + if (dev->id == id) { + found = 1; + atomic_inc(&dev->refcnt); + break; + } + } + mutex_unlock(&w1_mlock); return (found)?dev:NULL; } +struct w1_slave *w1_search_slave(struct w1_reg_num *id) +{ + struct w1_master *dev; + struct w1_slave *sl = NULL; + int found = 0; + + mutex_lock(&w1_mlock); + list_for_each_entry(dev, &w1_masters, w1_master_entry) { + mutex_lock(&dev->mutex); + list_for_each_entry(sl, &dev->slist, w1_slave_entry) { + if (sl->reg_num.family == id->family && + sl->reg_num.id == id->id && + sl->reg_num.crc == id->crc) { + found = 1; + atomic_inc(&dev->refcnt); + atomic_inc(&sl->refcnt); + break; + } + } + mutex_unlock(&dev->mutex); + + if (found) + break; + } + mutex_unlock(&w1_mlock); + + return (found)?sl:NULL; +} + void w1_reconnect_slaves(struct w1_family *f) { struct w1_master *dev; - spin_lock_bh(&w1_mlock); + mutex_lock(&w1_mlock); list_for_each_entry(dev, &w1_masters, w1_master_entry) { dev_dbg(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n", dev->name, f->fid); set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags); } - spin_unlock_bh(&w1_mlock); + mutex_unlock(&w1_mlock); } static void w1_slave_found(void *data, u64 rn) @@ -646,7 +728,7 @@ static void w1_slave_found(void *data, u64 rn) * @dev The master device to search * @cb Function to call when a device is found */ -void w1_search(struct w1_master *dev, w1_slave_found_callback cb) +void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb) { u64 last_rn, rn, tmp64; int i, slave_count = 0; @@ -677,7 +759,7 @@ void w1_search(struct w1_master *dev, w1_slave_found_callback cb) } /* Start the search */ - w1_write_8(dev, W1_SEARCH); + w1_write_8(dev, search_type); for (i = 0; i < 64; ++i) { /* Determine the direction/search bit */ if (i == desc_bit) @@ -739,23 +821,23 @@ static int w1_control(void *data) if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) { set_bit(W1_MASTER_NEED_EXIT, &dev->flags); - spin_lock(&w1_mlock); + mutex_lock(&w1_mlock); list_del(&dev->w1_master_entry); - spin_unlock(&w1_mlock); + mutex_unlock(&w1_mlock); - down(&dev->mutex); + mutex_lock(&dev->mutex); list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { w1_slave_detach(sl); } w1_destroy_master_attributes(dev); - up(&dev->mutex); + mutex_unlock(&dev->mutex); atomic_dec(&dev->refcnt); continue; } if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) { dev_dbg(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name); - down(&dev->mutex); + mutex_lock(&dev->mutex); list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { if (sl->family->fid == W1_FAMILY_DEFAULT) { struct w1_reg_num rn; @@ -768,7 +850,7 @@ static int w1_control(void *data) } dev_dbg(&dev->dev, "Reconnecting slaves in device %s has been finished.\n", dev->name); clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags); - up(&dev->mutex); + mutex_unlock(&dev->mutex); } } } @@ -776,10 +858,31 @@ static int w1_control(void *data) return 0; } +void w1_search_process(struct w1_master *dev, u8 search_type) +{ + struct w1_slave *sl, *sln; + + list_for_each_entry(sl, &dev->slist, w1_slave_entry) + clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); + + w1_search_devices(dev, search_type, w1_slave_found); + + list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { + if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) { + w1_slave_detach(sl); + + dev->slave_count--; + } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags)) + sl->ttl = dev->slave_ttl; + } + + if (dev->search_count > 0) + dev->search_count--; +} + int w1_process(void *data) { struct w1_master *dev = (struct w1_master *) data; - struct w1_slave *sl, *sln; while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) { try_to_freeze(); @@ -794,27 +897,9 @@ int w1_process(void *data) if (dev->search_count == 0) continue; - if (down_interruptible(&dev->mutex)) - continue; - - list_for_each_entry(sl, &dev->slist, w1_slave_entry) - clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); - - w1_search_devices(dev, w1_slave_found); - - list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { - if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) { - w1_slave_detach(sl); - - dev->slave_count--; - } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags)) - sl->ttl = dev->slave_ttl; - } - - if (dev->search_count > 0) - dev->search_count--; - - up(&dev->mutex); + mutex_lock(&dev->mutex); + w1_search_process(dev, W1_SEARCH); + mutex_unlock(&dev->mutex); } atomic_dec(&dev->refcnt); @@ -828,6 +913,8 @@ static int w1_init(void) printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n"); + w1_init_netlink(); + retval = bus_register(&w1_bus_type); if (retval) { printk(KERN_ERR "Failed to register bus. err=%d.\n", retval); @@ -880,6 +967,8 @@ static void w1_fini(void) list_for_each_entry(dev, &w1_masters, w1_master_entry) __w1_remove_master_device(dev); + w1_fini_netlink(); + kthread_stop(w1_control_thread); driver_unregister(&w1_slave_driver); |