23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/list.h>
30 #include <linux/device.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
48 static int w1_timeout = 10;
59 static int w1_attach_slave_device(
struct w1_master *
dev,
struct w1_reg_num *rn);
66 static int w1_master_probe(
struct device *
dev)
71 static void w1_master_release(
struct device *
dev)
73 struct w1_master *
md = dev_to_w1_master(dev);
75 dev_dbg(dev,
"%s: Releasing %s.\n", __func__, md->
name);
76 memset(md, 0,
sizeof(
struct w1_master) +
sizeof(
struct w1_bus_master));
80 static void w1_slave_release(
struct device *dev)
82 struct w1_slave *
sl = dev_to_w1_slave(dev);
84 dev_dbg(dev,
"%s: Releasing %s.\n", __func__, sl->
name);
87 dev_dbg(dev,
"Waiting for %s to become free: refcnt=%d.\n",
94 sl->master->slave_count--;
101 struct w1_slave *sl = dev_to_w1_slave(dev);
103 return sprintf(buf,
"%s\n", sl->name);
109 struct w1_slave *sl = dev_to_w1_slave(dev);
112 memcpy(buf, (
u8 *)&sl->reg_num, count);
125 char *buf, loff_t off,
size_t count)
127 struct w1_slave *sl = kobj_to_w1_slave(kobj);
144 char *buf, loff_t off,
size_t count)
146 struct w1_slave *sl = kobj_to_w1_slave(kobj);
160 .read = w1_default_read,
161 .write = w1_default_write,
164 static int w1_default_add_slave(
struct w1_slave *sl)
169 static void w1_default_remove_slave(
struct w1_slave *sl)
175 .add_slave = w1_default_add_slave,
176 .remove_slave = w1_default_remove_slave,
179 static struct w1_family w1_default_family = {
180 .fops = &w1_default_fops,
185 static struct bus_type w1_bus_type = {
187 .match = w1_master_match,
192 .name =
"w1_master_driver",
194 .
probe = w1_master_probe,
200 .init_name =
"w1 bus master",
202 .release = &w1_master_release
206 .name =
"w1_slave_driver",
211 struct device w1_slave_device = {
214 .init_name =
"w1 bus slave",
215 .driver = &w1_slave_driver,
216 .release = &w1_slave_release
222 struct w1_master *md = dev_to_w1_master(dev);
226 count =
sprintf(buf,
"%s\n", md->name);
232 static ssize_t w1_master_attribute_store_search(
struct device * dev,
234 const char * buf,
size_t count)
237 struct w1_master *md = dev_to_w1_master(dev);
243 md->search_count =
tmp;
250 static ssize_t w1_master_attribute_show_search(
struct device *dev,
254 struct w1_master *md = dev_to_w1_master(dev);
258 count =
sprintf(buf,
"%d\n", md->search_count);
264 static ssize_t w1_master_attribute_store_pullup(
struct device *dev,
266 const char *buf,
size_t count)
269 struct w1_master *md = dev_to_w1_master(dev);
275 md->enable_pullup =
tmp;
282 static ssize_t w1_master_attribute_show_pullup(
struct device *dev,
286 struct w1_master *md = dev_to_w1_master(dev);
290 count =
sprintf(buf,
"%d\n", md->enable_pullup);
298 struct w1_master *md = dev_to_w1_master(dev);
302 count =
sprintf(buf,
"0x%p\n", md->bus_master);
310 count =
sprintf(buf,
"%d\n", w1_timeout);
316 struct w1_master *md = dev_to_w1_master(dev);
320 count =
sprintf(buf,
"%d\n", md->max_slave_count);
327 struct w1_master *md = dev_to_w1_master(dev);
331 count =
sprintf(buf,
"%lu\n", md->attempts);
338 struct w1_master *md = dev_to_w1_master(dev);
342 count =
sprintf(buf,
"%d\n", md->slave_count);
347 static ssize_t w1_master_attribute_show_slaves(
struct device *dev,
350 struct w1_master *md = dev_to_w1_master(dev);
355 if (md->slave_count == 0)
362 sl =
list_entry(ent,
struct w1_slave, w1_slave_entry);
373 static ssize_t w1_master_attribute_show_add(
struct device *dev,
378 "write device id xx-xxxxxxxxxxxx to add slave\n");
382 static int w1_atoreg_num(
struct device *dev,
const char *buf,
size_t count,
386 unsigned long long id;
395 const char *error_msg =
"bad slave string format, expecting "
402 i =
sscanf(buf,
"%02x-%012llx", &family, &
id);
414 dev_info(dev,
"With CRC device is %02x.%012llx.%02x.\n",
415 rn->family, (
unsigned long long)rn->id, rn->crc);
424 static struct w1_slave *w1_slave_search_device(
struct w1_master *dev,
429 if (sl->reg_num.family == rn->family &&
430 sl->reg_num.id == rn->id &&
431 sl->reg_num.crc == rn->crc) {
438 static ssize_t w1_master_attribute_store_add(
struct device *dev,
440 const char *buf,
size_t count)
442 struct w1_master *md = dev_to_w1_master(dev);
447 if (w1_atoreg_num(dev, buf, count, &rn))
451 sl = w1_slave_search_device(md, &rn);
460 w1_attach_slave_device(md, &rn);
467 static ssize_t w1_master_attribute_show_remove(
struct device *dev,
472 "write device id xx-xxxxxxxxxxxx to remove slave\n");
476 static ssize_t w1_master_attribute_store_remove(
struct device *dev,
478 const char *buf,
size_t count)
480 struct w1_master *md = dev_to_w1_master(dev);
485 if (w1_atoreg_num(dev, buf, count, &rn))
489 sl = w1_slave_search_device(md, &rn);
493 dev_info(dev,
"Device %02x-%012llx doesn't exists\n", rn.family,
494 (
unsigned long long)rn.id);
502 #define W1_MASTER_ATTR_RO(_name, _mode) \
503 struct device_attribute w1_master_attribute_##_name = \
504 __ATTR(w1_master_##_name, _mode, \
505 w1_master_attribute_show_##_name, NULL)
507 #define W1_MASTER_ATTR_RW(_name, _mode) \
508 struct device_attribute w1_master_attribute_##_name = \
509 __ATTR(w1_master_##_name, _mode, \
510 w1_master_attribute_show_##_name, \
511 w1_master_attribute_store_##_name)
525 static struct attribute *w1_master_default_attrs[] = {
526 &w1_master_attribute_name.attr,
527 &w1_master_attribute_slaves.attr,
528 &w1_master_attribute_slave_count.attr,
529 &w1_master_attribute_max_slave_count.attr,
530 &w1_master_attribute_attempts.attr,
531 &w1_master_attribute_timeout.attr,
532 &w1_master_attribute_pointer.attr,
533 &w1_master_attribute_search.attr,
534 &w1_master_attribute_pullup.attr,
535 &w1_master_attribute_add.attr,
536 &w1_master_attribute_remove.attr,
541 .attrs = w1_master_default_attrs,
554 #ifdef CONFIG_HOTPLUG
557 struct w1_master *md =
NULL;
558 struct w1_slave *sl =
NULL;
559 char *event_owner, *
name;
562 if (dev->
driver == &w1_master_driver) {
564 event_owner =
"master";
566 }
else if (dev->
driver == &w1_slave_driver) {
568 event_owner =
"slave";
571 dev_dbg(dev,
"Unknown event.\n");
575 dev_dbg(dev,
"Hotplug event for %s %s, bus_id=%s.\n",
576 event_owner, name, dev_name(dev));
578 if (dev->
driver != &w1_slave_driver || !sl)
586 (
unsigned long long)sl->reg_num.id);
597 static int __w1_attach_slave_device(
struct w1_slave *sl)
601 sl->dev.parent = &sl->master->dev;
602 sl->dev.driver = &w1_slave_driver;
603 sl->dev.
bus = &w1_bus_type;
604 sl->dev.release = &w1_slave_release;
607 (
unsigned int) sl->reg_num.family,
608 (
unsigned long long) sl->reg_num.id);
609 snprintf(&sl->name[0],
sizeof(sl->name),
611 (
unsigned int) sl->reg_num.family,
612 (
unsigned long long) sl->reg_num.id);
614 dev_dbg(&sl->dev,
"%s: registering %s as %p.\n", __func__,
615 dev_name(&sl->dev), sl);
620 "Device registration [%s] failed. err=%d\n",
621 dev_name(&sl->dev), err);
629 "sysfs file creation for [%s] failed. err=%d\n",
630 dev_name(&sl->dev), err);
638 "sysfs file creation for [%s] failed. err=%d\n",
639 dev_name(&sl->dev), err);
644 if (sl->family->fops && sl->family->fops->add_slave &&
645 ((err = sl->family->fops->add_slave(sl)) < 0)) {
647 "sysfs file creation for [%s] failed. err=%d\n",
648 dev_name(&sl->dev), err);
665 static int w1_attach_slave_device(
struct w1_master *dev,
struct w1_reg_num *rn)
672 sl = kzalloc(
sizeof(
struct w1_slave),
GFP_KERNEL);
675 "%s: failed to allocate new slave device.\n",
683 set_bit(W1_SLAVE_ACTIVE, (
long *)&sl->flags);
686 memcpy(&sl->reg_num, rn,
sizeof(sl->reg_num));
688 init_completion(&sl->released);
690 spin_lock(&w1_flock);
693 f= &w1_default_family;
694 dev_info(&dev->dev,
"Family %x for %02x.%012llx.%02x is not registered.\n",
695 rn->family, rn->family,
696 (
unsigned long long)rn->id, rn->crc);
699 spin_unlock(&w1_flock);
704 err = __w1_attach_slave_device(sl);
706 dev_err(&dev->dev,
"%s: Attaching %s failed.\n", __func__,
713 sl->ttl = dev->slave_ttl;
727 dev_dbg(&sl->dev,
"%s: detaching %s [%p].\n", __func__, sl->name, sl);
731 if (sl->family->fops && sl->family->fops->remove_slave)
732 sl->family->fops->remove_slave(sl);
734 memset(&msg, 0,
sizeof(msg));
735 memcpy(msg.
id.id, &sl->reg_num,
sizeof(msg.
id));
749 struct w1_master *
dev;
762 return (found)?dev:
NULL;
767 struct w1_master *
dev;
768 struct w1_slave *sl =
NULL;
775 if (sl->reg_num.family == id->family &&
776 sl->reg_num.id == id->id &&
777 sl->reg_num.crc == id->crc) {
791 return (found)?sl:
NULL;
796 struct w1_slave *
sl, *sln;
797 struct w1_master *
dev;
801 dev_dbg(&dev->dev,
"Reconnecting slaves in device %s "
802 "for family %02x.\n", dev->name, f->
fid);
811 && sl->reg_num.family == f->
fid) ||
812 (!attach && sl->family->fid == f->
fid)) {
815 memcpy(&rn, &sl->reg_num,
sizeof(rn));
818 w1_attach_slave_device(dev, &rn);
821 dev_dbg(&dev->dev,
"Reconnecting slaves in device %s "
822 "has been finished.\n", dev->name);
838 sl = w1_slave_search_device(dev, tmp);
840 set_bit(W1_SLAVE_ACTIVE, (
long *)&sl->flags);
843 w1_attach_slave_device(dev, tmp);
863 void w1_search(
struct w1_master *dev,
u8 search_type, w1_slave_found_callback
cb)
865 u64 last_rn, rn, tmp64;
866 int i, slave_count = 0;
867 int last_zero, last_device;
868 int search_bit, desc_bit;
878 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
891 dev_dbg(&dev->dev,
"No devices present on the wire.\n");
896 if (dev->max_slave_count == 1) {
910 for (i = 0; i < 64; ++
i) {
914 else if (i > desc_bit)
917 search_bit = ((last_rn >>
i) & 0x1);
923 if ( (triplet_ret & 0x03) == 0x03 )
927 if (triplet_ret == 0)
931 tmp64 = (triplet_ret >> 2);
936 dev_dbg(&dev->dev,
"Abort w1_search\n");
942 if ( (triplet_ret & 0x03) != 0x03 ) {
943 if ( (desc_bit == last_zero) || (last_zero < 0))
945 desc_bit = last_zero;
952 w1_slave_found_callback
cb)
954 struct w1_slave *
sl, *sln;
957 clear_bit(W1_SLAVE_ACTIVE, (
long *)&sl->flags);
962 if (!
test_bit(W1_SLAVE_ACTIVE, (
unsigned long *)&sl->flags) && !--sl->ttl)
964 else if (
test_bit(W1_SLAVE_ACTIVE, (
unsigned long *)&sl->flags))
965 sl->ttl = dev->slave_ttl;
968 if (dev->search_count > 0)
972 static void w1_search_process(
struct w1_master *dev,
u8 search_type)
979 struct w1_master *dev = (
struct w1_master *) data;
986 if (dev->search_count) {
988 w1_search_process(dev, W1_SEARCH);
999 if (dev->search_count)
1010 static int __init w1_init(
void)
1021 goto err_out_exit_init;
1027 "Failed to register master driver. err=%d.\n",
1029 goto err_out_bus_unregister;
1035 "Failed to register slave driver. err=%d.\n",
1037 goto err_out_master_unregister;
1044 err_out_slave_unregister:
1048 err_out_master_unregister:
1051 err_out_bus_unregister:
1058 static void __exit w1_fini(
void)
1060 struct w1_master *
dev;