Commit d2a4ef6a authored by Evgeniy Polyakov's avatar Evgeniy Polyakov Committed by Greg Kroah-Hartman

[PATCH] w1: Added add/remove slave callbacks.

Patch is based on work from Ben Gardner <bgardner@wabtec.com>
Signed-off-by: default avatarEvgeniy Polyakov <johnpol@2ka.mipt.ru>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent ea7d8f65
...@@ -59,19 +59,6 @@ static pid_t control_thread; ...@@ -59,19 +59,6 @@ static pid_t control_thread;
static int control_needs_exit; static int control_needs_exit;
static DECLARE_COMPLETION(w1_control_complete); static DECLARE_COMPLETION(w1_control_complete);
/* stuff for the default family */
static ssize_t w1_famdefault_read_name(struct device *dev, struct device_attribute *attr, char *buf)
{
struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
return(sprintf(buf, "%s\n", sl->name));
}
static struct w1_family_ops w1_default_fops = {
.rname = &w1_famdefault_read_name,
};
static struct w1_family w1_default_family = {
.fops = &w1_default_fops,
};
static int w1_master_match(struct device *dev, struct device_driver *drv) static int w1_master_match(struct device *dev, struct device_driver *drv)
{ {
return 1; return 1;
...@@ -99,30 +86,47 @@ static void w1_slave_release(struct device *dev) ...@@ -99,30 +86,47 @@ static void w1_slave_release(struct device *dev)
complete(&sl->dev_released); complete(&sl->dev_released);
} }
static ssize_t w1_default_read_name(struct device *dev, struct device_attribute *attr, char *buf) static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
{ {
return sprintf(buf, "No family registered.\n"); struct w1_slave *sl = dev_to_w1_slave(dev);
return sprintf(buf, "%s\n", sl->name);
} }
static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off, static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, size_t count)
size_t count)
{ {
return sprintf(buf, "No family registered.\n"); struct w1_slave *sl = kobj_to_w1_slave(kobj);
}
static struct device_attribute w1_slave_attribute = atomic_inc(&sl->refcnt);
__ATTR(name, S_IRUGO, w1_default_read_name, NULL); if (off > 8) {
count = 0;
} else {
if (off + count > 8)
count = 8 - off;
static struct bin_attribute w1_slave_bin_attribute = { memcpy(buf, (u8 *)&sl->reg_num, count);
}
atomic_dec(&sl->refcnt);
return count;
}
static struct device_attribute w1_slave_attr_name =
__ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
static struct bin_attribute w1_slave_attr_bin_id = {
.attr = { .attr = {
.name = "w1_slave", .name = "id",
.mode = S_IRUGO, .mode = S_IRUGO,
.owner = THIS_MODULE, .owner = THIS_MODULE,
}, },
.size = W1_SLAVE_DATA_SIZE, .size = 8,
.read = &w1_default_read_bin, .read = w1_slave_read_id,
}; };
/* Default family */
static struct w1_family w1_default_family;
static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size); static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
static struct bus_type w1_bus_type = { static struct bus_type w1_bus_type = {
...@@ -413,36 +417,44 @@ static int __w1_attach_slave_device(struct w1_slave *sl) ...@@ -413,36 +417,44 @@ static int __w1_attach_slave_device(struct w1_slave *sl)
return err; return err;
} }
memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin)); /* Create "name" entry */
memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name)); err = device_create_file(&sl->dev, &w1_slave_attr_name);
sl->attr_bin.read = sl->family->fops->rbin;
sl->attr_name.show = sl->family->fops->rname;
err = device_create_file(&sl->dev, &sl->attr_name);
if (err < 0) { if (err < 0) {
dev_err(&sl->dev, dev_err(&sl->dev,
"sysfs file creation for [%s] failed. err=%d\n", "sysfs file creation for [%s] failed. err=%d\n",
sl->dev.bus_id, err); sl->dev.bus_id, err);
device_unregister(&sl->dev); goto out_unreg;
return err;
} }
if ( sl->attr_bin.read ) { /* Create "id" entry */
err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin); err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
if (err < 0) { if (err < 0) {
dev_err(&sl->dev, dev_err(&sl->dev,
"sysfs file creation for [%s] failed. err=%d\n", "sysfs file creation for [%s] failed. err=%d\n",
sl->dev.bus_id, err); sl->dev.bus_id, err);
device_remove_file(&sl->dev, &sl->attr_name); goto out_rem1;
device_unregister(&sl->dev);
return err;
} }
/* if the family driver needs to initialize something... */
if (sl->family->fops && sl->family->fops->add_slave &&
((err = sl->family->fops->add_slave(sl)) < 0)) {
dev_err(&sl->dev,
"sysfs file creation for [%s] failed. err=%d\n",
sl->dev.bus_id, err);
goto out_rem2;
} }
list_add_tail(&sl->w1_slave_entry, &sl->master->slist); list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
return 0; return 0;
out_rem2:
sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
out_rem1:
device_remove_file(&sl->dev, &w1_slave_attr_name);
out_unreg:
device_unregister(&sl->dev);
return err;
} }
static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn) static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
...@@ -517,10 +529,11 @@ static void w1_slave_detach(struct w1_slave *sl) ...@@ -517,10 +529,11 @@ static void w1_slave_detach(struct w1_slave *sl)
flush_signals(current); flush_signals(current);
} }
if ( sl->attr_bin.read ) { if (sl->family->fops && sl->family->fops->remove_slave)
sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin); sl->family->fops->remove_slave(sl);
}
device_remove_file(&sl->dev, &sl->attr_name); sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
device_remove_file(&sl->dev, &w1_slave_attr_name);
device_unregister(&sl->dev); device_unregister(&sl->dev);
w1_family_put(sl->family); w1_family_put(sl->family);
...@@ -805,8 +818,8 @@ int w1_process(void *data) ...@@ -805,8 +818,8 @@ int w1_process(void *data)
if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) { if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
list_del (&sl->w1_slave_entry); list_del (&sl->w1_slave_entry);
w1_slave_detach (sl); w1_slave_detach(sl);
kfree (sl); kfree(sl);
dev->slave_count--; dev->slave_count--;
} else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags)) } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
......
...@@ -77,9 +77,6 @@ struct w1_slave ...@@ -77,9 +77,6 @@ struct w1_slave
struct w1_family *family; struct w1_family *family;
struct device dev; struct device dev;
struct completion dev_released; struct completion dev_released;
struct bin_attribute attr_bin;
struct device_attribute attr_name;
}; };
typedef void (* w1_slave_found_callback)(unsigned long, u64); typedef void (* w1_slave_found_callback)(unsigned long, u64);
......
...@@ -29,23 +29,12 @@ DEFINE_SPINLOCK(w1_flock); ...@@ -29,23 +29,12 @@ DEFINE_SPINLOCK(w1_flock);
static LIST_HEAD(w1_families); static LIST_HEAD(w1_families);
extern void w1_reconnect_slaves(struct w1_family *f); extern void w1_reconnect_slaves(struct w1_family *f);
static int w1_check_family(struct w1_family *f)
{
if (!f->fops->rname || !f->fops->rbin)
return -EINVAL;
return 0;
}
int w1_register_family(struct w1_family *newf) int w1_register_family(struct w1_family *newf)
{ {
struct list_head *ent, *n; struct list_head *ent, *n;
struct w1_family *f; struct w1_family *f;
int ret = 0; int ret = 0;
if (w1_check_family(newf))
return -EINVAL;
spin_lock(&w1_flock); spin_lock(&w1_flock);
list_for_each_safe(ent, n, &w1_families) { list_for_each_safe(ent, n, &w1_families) {
f = list_entry(ent, struct w1_family, family_entry); f = list_entry(ent, struct w1_family, family_entry);
......
...@@ -35,10 +35,12 @@ ...@@ -35,10 +35,12 @@
#define MAXNAMELEN 32 #define MAXNAMELEN 32
struct w1_slave;
struct w1_family_ops struct w1_family_ops
{ {
ssize_t (* rname)(struct device *, struct device_attribute *, char *); int (* add_slave)(struct w1_slave *);
ssize_t (* rbin)(struct kobject *, char *, loff_t, size_t); void (* remove_slave)(struct w1_slave *);
}; };
struct w1_family struct w1_family
......
...@@ -36,59 +36,12 @@ MODULE_LICENSE("GPL"); ...@@ -36,59 +36,12 @@ MODULE_LICENSE("GPL");
MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>"); MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family."); MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family.");
static ssize_t w1_smem_read_name(struct device *, struct device_attribute *attr, char *);
static ssize_t w1_smem_read_bin(struct kobject *, char *, loff_t, size_t);
static struct w1_family_ops w1_smem_fops = {
.rname = &w1_smem_read_name,
.rbin = &w1_smem_read_bin,
};
static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf)
{
struct w1_slave *sl = dev_to_w1_slave(dev);
return sprintf(buf, "%s\n", sl->name);
}
static ssize_t w1_smem_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
{
struct w1_slave *sl = kobj_to_w1_slave(kobj);
int i;
atomic_inc(&sl->refcnt);
if (down_interruptible(&sl->master->mutex)) {
count = 0;
goto out_dec;
}
if (off > W1_SLAVE_DATA_SIZE) {
count = 0;
goto out;
}
if (off + count > W1_SLAVE_DATA_SIZE) {
count = 0;
goto out;
}
for (i = 0; i < 8; ++i)
count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]);
count += sprintf(buf + count, "\n");
out:
up(&sl->master->mutex);
out_dec:
atomic_dec(&sl->refcnt);
return count;
}
static struct w1_family w1_smem_family_01 = { static struct w1_family w1_smem_family_01 = {
.fid = W1_FAMILY_SMEM_01, .fid = W1_FAMILY_SMEM_01,
.fops = &w1_smem_fops,
}; };
static struct w1_family w1_smem_family_81 = { static struct w1_family w1_smem_family_81 = {
.fid = W1_FAMILY_SMEM_81, .fid = W1_FAMILY_SMEM_81,
.fops = &w1_smem_fops,
}; };
static int __init w1_smem_init(void) static int __init w1_smem_init(void)
......
...@@ -42,12 +42,31 @@ static u8 bad_roms[][9] = { ...@@ -42,12 +42,31 @@ static u8 bad_roms[][9] = {
{} {}
}; };
static ssize_t w1_therm_read_name(struct device *, struct device_attribute *attr, char *);
static ssize_t w1_therm_read_bin(struct kobject *, char *, loff_t, size_t); static ssize_t w1_therm_read_bin(struct kobject *, char *, loff_t, size_t);
static struct bin_attribute w1_therm_bin_attr = {
.attr = {
.name = "w1_slave",
.mode = S_IRUGO,
.owner = THIS_MODULE,
},
.size = W1_SLAVE_DATA_SIZE,
.read = w1_therm_read_bin,
};
static int w1_therm_add_slave(struct w1_slave *sl)
{
return sysfs_create_bin_file(&sl->dev.kobj, &w1_therm_bin_attr);
}
static void w1_therm_remove_slave(struct w1_slave *sl)
{
sysfs_remove_bin_file(&sl->dev.kobj, &w1_therm_bin_attr);
}
static struct w1_family_ops w1_therm_fops = { static struct w1_family_ops w1_therm_fops = {
.rname = &w1_therm_read_name, .add_slave = w1_therm_add_slave,
.rbin = &w1_therm_read_bin, .remove_slave = w1_therm_remove_slave,
}; };
static struct w1_family w1_therm_family_DS18S20 = { static struct w1_family w1_therm_family_DS18S20 = {
...@@ -59,6 +78,7 @@ static struct w1_family w1_therm_family_DS18B20 = { ...@@ -59,6 +78,7 @@ static struct w1_family w1_therm_family_DS18B20 = {
.fid = W1_THERM_DS18B20, .fid = W1_THERM_DS18B20,
.fops = &w1_therm_fops, .fops = &w1_therm_fops,
}; };
static struct w1_family w1_therm_family_DS1822 = { static struct w1_family w1_therm_family_DS1822 = {
.fid = W1_THERM_DS1822, .fid = W1_THERM_DS1822,
.fops = &w1_therm_fops, .fops = &w1_therm_fops,
...@@ -90,12 +110,6 @@ static struct w1_therm_family_converter w1_therm_families[] = { ...@@ -90,12 +110,6 @@ static struct w1_therm_family_converter w1_therm_families[] = {
}, },
}; };
static ssize_t w1_therm_read_name(struct device *dev, struct device_attribute *attr, char *buf)
{
struct w1_slave *sl = dev_to_w1_slave(dev);
return sprintf(buf, "%s\n", sl->name);
}
static inline int w1_DS18B20_convert_temp(u8 rom[9]) static inline int w1_DS18B20_convert_temp(u8 rom[9])
{ {
int t = (rom[1] << 8) | rom[0]; int t = (rom[1] << 8) | rom[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