4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
30 #include <linux/spinlock.h>
31 #include <linux/timer.h>
32 #include <linux/device.h>
33 #include <linux/slab.h>
34 #include <linux/sched.h>
40 #include "w1_family.h"
41 #include "w1_netlink.h"
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
45 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
47 static int w1_timeout = 10;
48 int w1_max_slave_count = 10;
49 int w1_max_slave_ttl = 10;
51 module_param_named(timeout, w1_timeout, int, 0);
52 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
53 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
55 DEFINE_SPINLOCK(w1_mlock);
56 LIST_HEAD(w1_masters);
58 static pid_t control_thread;
59 static int control_needs_exit;
60 static DECLARE_COMPLETION(w1_control_complete);
61 static DECLARE_WAIT_QUEUE_HEAD(w1_control_wait);
63 static int w1_master_match(struct device *dev, struct device_driver *drv)
68 static int w1_master_probe(struct device *dev)
73 static int w1_master_remove(struct device *dev)
78 static void w1_master_release(struct device *dev)
80 struct w1_master *md = container_of(dev, struct w1_master, dev);
82 complete(&md->dev_released);
85 static void w1_slave_release(struct device *dev)
87 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
89 complete(&sl->dev_released);
92 static ssize_t w1_default_read_name(struct device *dev, char *buf)
94 return sprintf(buf, "No family registered.\n");
97 static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
100 return sprintf(buf, "No family registered.\n");
103 struct bus_type w1_bus_type = {
105 .match = w1_master_match,
108 struct device_driver w1_driver = {
111 .probe = w1_master_probe,
112 .remove = w1_master_remove,
115 struct device w1_device = {
118 .bus_id = "w1 bus master",
119 .driver = &w1_driver,
120 .release = &w1_master_release
123 static struct device_attribute w1_slave_attribute = {
129 .show = &w1_default_read_name,
132 static struct device_attribute w1_slave_attribute_val = {
138 .show = &w1_default_read_name,
141 ssize_t w1_master_attribute_show_name(struct device *dev, char *buf)
143 struct w1_master *md = container_of (dev, struct w1_master, dev);
146 if (down_interruptible (&md->mutex))
149 count = sprintf(buf, "%s\n", md->name);
156 ssize_t w1_master_attribute_show_pointer(struct device *dev, char *buf)
158 struct w1_master *md = container_of(dev, struct w1_master, dev);
161 if (down_interruptible(&md->mutex))
164 count = sprintf(buf, "0x%p\n", md->bus_master);
170 ssize_t w1_master_attribute_show_timeout(struct device *dev, char *buf)
173 count = sprintf(buf, "%d\n", w1_timeout);
177 ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, char *buf)
179 struct w1_master *md = container_of(dev, struct w1_master, dev);
182 if (down_interruptible(&md->mutex))
185 count = sprintf(buf, "%d\n", md->max_slave_count);
191 ssize_t w1_master_attribute_show_attempts(struct device *dev, char *buf)
193 struct w1_master *md = container_of(dev, struct w1_master, dev);
196 if (down_interruptible(&md->mutex))
199 count = sprintf(buf, "%lu\n", md->attempts);
205 ssize_t w1_master_attribute_show_slave_count(struct device *dev, char *buf)
207 struct w1_master *md = container_of(dev, struct w1_master, dev);
210 if (down_interruptible(&md->mutex))
213 count = sprintf(buf, "%d\n", md->slave_count);
219 ssize_t w1_master_attribute_show_slaves(struct device *dev, char *buf)
222 struct w1_master *md = container_of(dev, struct w1_master, dev);
225 if (down_interruptible(&md->mutex))
228 if (md->slave_count == 0)
229 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
231 struct list_head *ent, *n;
234 list_for_each_safe(ent, n, &md->slist) {
235 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
237 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
243 return PAGE_SIZE - c;
246 static struct device_attribute w1_master_attribute_slaves = {
248 .name = "w1_master_slaves",
250 .owner = THIS_MODULE,
252 .show = &w1_master_attribute_show_slaves,
254 static struct device_attribute w1_master_attribute_slave_count = {
256 .name = "w1_master_slave_count",
260 .show = &w1_master_attribute_show_slave_count,
262 static struct device_attribute w1_master_attribute_attempts = {
264 .name = "w1_master_attempts",
268 .show = &w1_master_attribute_show_attempts,
270 static struct device_attribute w1_master_attribute_max_slave_count = {
272 .name = "w1_master_max_slave_count",
276 .show = &w1_master_attribute_show_max_slave_count,
278 static struct device_attribute w1_master_attribute_timeout = {
280 .name = "w1_master_timeout",
284 .show = &w1_master_attribute_show_timeout,
286 static struct device_attribute w1_master_attribute_pointer = {
288 .name = "w1_master_pointer",
292 .show = &w1_master_attribute_show_pointer,
294 static struct device_attribute w1_master_attribute_name = {
296 .name = "w1_master_name",
300 .show = &w1_master_attribute_show_name,
303 static struct bin_attribute w1_slave_bin_attribute = {
307 .owner = THIS_MODULE,
309 .size = W1_SLAVE_DATA_SIZE,
310 .read = &w1_default_read_bin,
313 static int __w1_attach_slave_device(struct w1_slave *sl)
317 sl->dev.parent = &sl->master->dev;
318 sl->dev.driver = sl->master->driver;
319 sl->dev.bus = &w1_bus_type;
320 sl->dev.release = &w1_slave_release;
322 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
324 (unsigned int) sl->reg_num.family,
325 (unsigned long long) sl->reg_num.id);
326 snprintf (&sl->name[0], sizeof(sl->name),
328 (unsigned int) sl->reg_num.family,
329 (unsigned long long) sl->reg_num.id);
331 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
334 err = device_register(&sl->dev);
337 "Device registration [%s] failed. err=%d\n",
338 sl->dev.bus_id, err);
342 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
343 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
344 memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val));
346 sl->attr_bin.read = sl->family->fops->rbin;
347 sl->attr_name.show = sl->family->fops->rname;
348 sl->attr_val.show = sl->family->fops->rval;
349 sl->attr_val.attr.name = sl->family->fops->rvalname;
351 err = device_create_file(&sl->dev, &sl->attr_name);
354 "sysfs file creation for [%s] failed. err=%d\n",
355 sl->dev.bus_id, err);
356 device_unregister(&sl->dev);
360 err = device_create_file(&sl->dev, &sl->attr_val);
363 "sysfs file creation for [%s] failed. err=%d\n",
364 sl->dev.bus_id, err);
365 device_remove_file(&sl->dev, &sl->attr_name);
366 device_unregister(&sl->dev);
370 err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
373 "sysfs file creation for [%s] failed. err=%d\n",
374 sl->dev.bus_id, err);
375 device_remove_file(&sl->dev, &sl->attr_name);
376 device_remove_file(&sl->dev, &sl->attr_val);
377 device_unregister(&sl->dev);
381 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
386 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
391 struct w1_netlink_msg msg;
393 sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
396 "%s: failed to allocate new slave device.\n",
401 memset(sl, 0, sizeof(*sl));
403 sl->owner = THIS_MODULE;
405 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
407 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
408 atomic_set(&sl->refcnt, 0);
409 init_completion(&sl->dev_released);
411 spin_lock(&w1_flock);
412 f = w1_family_registered(rn->family);
414 spin_unlock(&w1_flock);
415 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
416 rn->family, rn->family, rn->id, rn->crc);
421 spin_unlock(&w1_flock);
426 err = __w1_attach_slave_device(sl);
428 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
430 w1_family_put(sl->family);
435 sl->ttl = dev->slave_ttl;
438 memcpy(&msg.id.id, rn, sizeof(msg.id.id));
439 msg.type = W1_SLAVE_ADD;
440 w1_netlink_send(dev, &msg);
445 static void w1_slave_detach(struct w1_slave *sl)
447 struct w1_netlink_msg msg;
449 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
451 while (atomic_read(&sl->refcnt)) {
452 printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
453 sl->name, atomic_read(&sl->refcnt));
455 if (msleep_interruptible(1000))
456 flush_signals(current);
459 sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
460 device_remove_file(&sl->dev, &sl->attr_name);
461 device_remove_file(&sl->dev, &sl->attr_val);
462 device_unregister(&sl->dev);
463 w1_family_put(sl->family);
465 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
466 msg.type = W1_SLAVE_REMOVE;
467 w1_netlink_send(sl->master, &msg);
470 static struct w1_master *w1_search_master(unsigned long data)
472 struct w1_master *dev;
475 spin_lock_irq(&w1_mlock);
476 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
477 if (dev->bus_master->data == data) {
479 atomic_inc(&dev->refcnt);
483 spin_unlock_irq(&w1_mlock);
485 return (found)?dev:NULL;
488 void w1_slave_found(unsigned long data, u64 rn)
492 struct list_head *ent;
493 struct w1_reg_num *tmp;
494 int family_found = 0;
495 struct w1_master *dev;
497 dev = w1_search_master(data);
499 printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
504 tmp = (struct w1_reg_num *) &rn;
507 list_for_each(ent, &dev->slist) {
509 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
511 if (sl->reg_num.family == tmp->family &&
512 sl->reg_num.id == tmp->id &&
513 sl->reg_num.crc == tmp->crc) {
514 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
517 else if (sl->reg_num.family == tmp->family) {
525 if (slave_count == dev->slave_count &&
526 rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) {
527 w1_attach_slave_device(dev, (struct w1_reg_num *) &rn);
530 atomic_dec(&dev->refcnt);
533 void w1_search(struct w1_master *dev)
537 int last_family_desc, last_zero, last_device;
538 int search_bit, id_bit, comp_bit, desc_bit;
540 search_bit = id_bit = comp_bit = 0;
542 last_device = last_zero = last_family_desc = 0;
546 while (!(id_bit && comp_bit) && !last_device
547 && count++ < dev->max_slave_count) {
551 last_family_desc = 0;
554 * Reset bus and all 1-wire device state machines
555 * so they can respond to our requests.
557 * Return 0 - device(s) present, 1 - no devices present.
559 if (w1_reset_bus(dev)) {
560 dev_info(&dev->dev, "No devices present on the wire.\n");
565 w1_write_8(dev, W1_SEARCH);
566 for (i = 0; i < 64; ++i) {
568 * Read 2 bits from bus.
569 * All who don't sleep must send ID bit and COMPLEMENT ID bit.
570 * They actually are ANDed between all senders.
572 id_bit = w1_touch_bit(dev, 1);
573 comp_bit = w1_touch_bit(dev, 1);
575 if (id_bit && comp_bit)
578 if (id_bit == 0 && comp_bit == 0) {
581 else if (i > desc_bit)
584 search_bit = ((last >> i) & 0x1);
586 if (search_bit == 0) {
589 last_family_desc = last_zero;
601 * and make all who don't have "search_bit" in "i"'th position
602 * in it's registration number sleep.
604 if (dev->bus_master->touch_bit)
605 w1_touch_bit(dev, search_bit);
607 w1_write_bit(dev, search_bit);
612 if (desc_bit == last_zero)
615 desc_bit = last_zero;
617 w1_slave_found(dev->bus_master->data, rn);
621 int w1_create_master_attributes(struct w1_master *dev)
623 if ( device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 ||
624 device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 ||
625 device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 ||
626 device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 ||
627 device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0||
628 device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0||
629 device_create_file(&dev->dev, &w1_master_attribute_name) < 0)
635 void w1_destroy_master_attributes(struct w1_master *dev)
637 device_remove_file(&dev->dev, &w1_master_attribute_slaves);
638 device_remove_file(&dev->dev, &w1_master_attribute_slave_count);
639 device_remove_file(&dev->dev, &w1_master_attribute_attempts);
640 device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count);
641 device_remove_file(&dev->dev, &w1_master_attribute_timeout);
642 device_remove_file(&dev->dev, &w1_master_attribute_pointer);
643 device_remove_file(&dev->dev, &w1_master_attribute_name);
647 int w1_control(void *data)
650 struct w1_master *dev;
651 struct list_head *ent, *ment, *n, *mn;
652 int err, have_to_wait = 0, timeout;
654 daemonize("w1_control");
655 allow_signal(SIGTERM);
657 while (!control_needs_exit || have_to_wait) {
660 timeout = w1_timeout*HZ;
662 timeout = interruptible_sleep_on_timeout(&w1_control_wait, timeout);
663 try_to_freeze(PF_FREEZE);
664 } while (!signal_pending(current) && (timeout > 0));
666 if (signal_pending(current))
667 flush_signals(current);
669 list_for_each_safe(ment, mn, &w1_masters) {
670 dev = list_entry(ment, struct w1_master, w1_master_entry);
672 if (!control_needs_exit && !dev->need_exit)
675 * Little race: we can create thread but not set the flag.
676 * Get a chance for external process to set flag up.
678 if (!dev->initialized) {
683 spin_lock(&w1_mlock);
684 list_del(&dev->w1_master_entry);
685 spin_unlock(&w1_mlock);
687 if (control_needs_exit) {
690 err = kill_proc(dev->kpid, SIGTERM, 1);
693 "Failed to send signal to w1 kernel thread %d.\n",
697 wait_for_completion(&dev->dev_exited);
699 list_for_each_safe(ent, n, &dev->slist) {
700 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
704 "%s: slave entry is NULL.\n",
707 list_del(&sl->w1_slave_entry);
713 w1_destroy_master_attributes(dev);
714 atomic_dec(&dev->refcnt);
718 complete_and_exit(&w1_control_complete, 0);
721 int w1_process(void *data)
723 struct w1_master *dev = (struct w1_master *) data;
724 unsigned long timeout;
725 struct list_head *ent, *n;
728 daemonize("%s", dev->name);
729 allow_signal(SIGTERM);
731 while (!dev->need_exit) {
732 timeout = w1_timeout*HZ;
734 timeout = interruptible_sleep_on_timeout(&dev->kwait, timeout);
735 try_to_freeze(PF_FREEZE);
736 } while (!signal_pending(current) && (timeout > 0));
738 if (signal_pending(current))
739 flush_signals(current);
744 if (!dev->initialized)
747 if (down_interruptible(&dev->mutex))
750 list_for_each_safe(ent, n, &dev->slist) {
751 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
754 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
757 w1_search_devices(dev, w1_slave_found);
759 list_for_each_safe(ent, n, &dev->slist) {
760 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
762 if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
763 list_del (&sl->w1_slave_entry);
765 w1_slave_detach (sl);
770 else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
771 sl->ttl = dev->slave_ttl;
776 atomic_dec(&dev->refcnt);
777 complete_and_exit(&dev->dev_exited, 0);
786 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
788 retval = bus_register(&w1_bus_type);
790 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
791 goto err_out_exit_init;
794 retval = driver_register(&w1_driver);
797 "Failed to register master driver. err=%d.\n",
799 goto err_out_bus_unregister;
802 control_thread = kernel_thread(&w1_control, NULL, 0);
803 if (control_thread < 0) {
804 printk(KERN_ERR "Failed to create control thread. err=%d\n",
806 retval = control_thread;
807 goto err_out_driver_unregister;
812 err_out_driver_unregister:
813 driver_unregister(&w1_driver);
815 err_out_bus_unregister:
816 bus_unregister(&w1_bus_type);
824 struct w1_master *dev;
825 struct list_head *ent, *n;
827 list_for_each_safe(ent, n, &w1_masters) {
828 dev = list_entry(ent, struct w1_master, w1_master_entry);
829 __w1_remove_master_device(dev);
832 control_needs_exit = 1;
834 wait_for_completion(&w1_control_complete);
836 driver_unregister(&w1_driver);
837 bus_unregister(&w1_bus_type);
840 module_init(w1_init);
841 module_exit(w1_fini);
843 EXPORT_SYMBOL(w1_create_master_attributes);
844 EXPORT_SYMBOL(w1_destroy_master_attributes);