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>
35 #include <linux/suspend.h>
41 #include "w1_family.h"
42 #include "w1_netlink.h"
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
46 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
48 static int w1_timeout = 10;
49 int w1_max_slave_count = 10;
50 int w1_max_slave_ttl = 10;
52 module_param_named(timeout, w1_timeout, int, 0);
53 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
54 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
56 spinlock_t w1_mlock = SPIN_LOCK_UNLOCKED;
57 LIST_HEAD(w1_masters);
59 static pid_t control_thread;
60 static int control_needs_exit;
61 static DECLARE_COMPLETION(w1_control_complete);
62 static DECLARE_WAIT_QUEUE_HEAD(w1_control_wait);
64 static int w1_master_match(struct device *dev, struct device_driver *drv)
69 static int w1_master_probe(struct device *dev)
74 static int w1_master_remove(struct device *dev)
79 static void w1_master_release(struct device *dev)
81 struct w1_master *md = container_of(dev, struct w1_master, dev);
83 complete(&md->dev_released);
86 static void w1_slave_release(struct device *dev)
88 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
90 complete(&sl->dev_released);
93 static ssize_t w1_default_read_name(struct device *dev, char *buf)
95 return sprintf(buf, "No family registered.\n");
98 static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
101 return sprintf(buf, "No family registered.\n");
104 struct bus_type w1_bus_type = {
106 .match = w1_master_match,
109 struct device_driver w1_driver = {
112 .probe = w1_master_probe,
113 .remove = w1_master_remove,
116 struct device w1_device = {
119 .bus_id = "w1 bus master",
120 .driver = &w1_driver,
121 .release = &w1_master_release
124 static struct device_attribute w1_slave_attribute = {
130 .show = &w1_default_read_name,
133 static struct device_attribute w1_slave_attribute_val = {
139 .show = &w1_default_read_name,
142 ssize_t w1_master_attribute_show_name(struct device *dev, char *buf)
144 struct w1_master *md = container_of (dev, struct w1_master, dev);
147 if (down_interruptible (&md->mutex))
150 count = sprintf(buf, "%s\n", md->name);
157 ssize_t w1_master_attribute_show_pointer(struct device *dev, char *buf)
159 struct w1_master *md = container_of(dev, struct w1_master, dev);
162 if (down_interruptible(&md->mutex))
165 count = sprintf(buf, "0x%p\n", md->bus_master);
171 ssize_t w1_master_attribute_show_timeout(struct device *dev, char *buf)
174 count = sprintf(buf, "%d\n", w1_timeout);
178 ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, char *buf)
180 struct w1_master *md = container_of(dev, struct w1_master, dev);
183 if (down_interruptible(&md->mutex))
186 count = sprintf(buf, "%d\n", md->max_slave_count);
192 ssize_t w1_master_attribute_show_attempts(struct device *dev, char *buf)
194 struct w1_master *md = container_of(dev, struct w1_master, dev);
197 if (down_interruptible(&md->mutex))
200 count = sprintf(buf, "%lu\n", md->attempts);
206 ssize_t w1_master_attribute_show_slave_count(struct device *dev, char *buf)
208 struct w1_master *md = container_of(dev, struct w1_master, dev);
211 if (down_interruptible(&md->mutex))
214 count = sprintf(buf, "%d\n", md->slave_count);
220 ssize_t w1_master_attribute_show_slaves(struct device *dev, char *buf)
223 struct w1_master *md = container_of(dev, struct w1_master, dev);
226 if (down_interruptible(&md->mutex))
229 if (md->slave_count == 0)
230 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
232 struct list_head *ent, *n;
235 list_for_each_safe(ent, n, &md->slist) {
236 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
238 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
244 return PAGE_SIZE - c;
247 static struct device_attribute w1_master_attribute_slaves = {
249 .name = "w1_master_slaves",
251 .owner = THIS_MODULE,
253 .show = &w1_master_attribute_show_slaves,
255 static struct device_attribute w1_master_attribute_slave_count = {
257 .name = "w1_master_slave_count",
261 .show = &w1_master_attribute_show_slave_count,
263 static struct device_attribute w1_master_attribute_attempts = {
265 .name = "w1_master_attempts",
269 .show = &w1_master_attribute_show_attempts,
271 static struct device_attribute w1_master_attribute_max_slave_count = {
273 .name = "w1_master_max_slave_count",
277 .show = &w1_master_attribute_show_max_slave_count,
279 static struct device_attribute w1_master_attribute_timeout = {
281 .name = "w1_master_timeout",
285 .show = &w1_master_attribute_show_timeout,
287 static struct device_attribute w1_master_attribute_pointer = {
289 .name = "w1_master_pointer",
293 .show = &w1_master_attribute_show_pointer,
295 static struct device_attribute w1_master_attribute_name = {
297 .name = "w1_master_name",
301 .show = &w1_master_attribute_show_name,
304 static struct bin_attribute w1_slave_bin_attribute = {
308 .owner = THIS_MODULE,
310 .size = W1_SLAVE_DATA_SIZE,
311 .read = &w1_default_read_bin,
314 static int __w1_attach_slave_device(struct w1_slave *sl)
318 sl->dev.parent = &sl->master->dev;
319 sl->dev.driver = sl->master->driver;
320 sl->dev.bus = &w1_bus_type;
321 sl->dev.release = &w1_slave_release;
323 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
325 (unsigned int) sl->reg_num.family,
326 (unsigned long long) sl->reg_num.id);
327 snprintf (&sl->name[0], sizeof(sl->name),
329 (unsigned int) sl->reg_num.family,
330 (unsigned long long) sl->reg_num.id);
332 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
335 err = device_register(&sl->dev);
338 "Device registration [%s] failed. err=%d\n",
339 sl->dev.bus_id, err);
343 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
344 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
345 memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val));
347 sl->attr_bin.read = sl->family->fops->rbin;
348 sl->attr_name.show = sl->family->fops->rname;
349 sl->attr_val.show = sl->family->fops->rval;
350 sl->attr_val.attr.name = sl->family->fops->rvalname;
352 err = device_create_file(&sl->dev, &sl->attr_name);
355 "sysfs file creation for [%s] failed. err=%d\n",
356 sl->dev.bus_id, err);
357 device_unregister(&sl->dev);
361 err = device_create_file(&sl->dev, &sl->attr_val);
364 "sysfs file creation for [%s] failed. err=%d\n",
365 sl->dev.bus_id, err);
366 device_remove_file(&sl->dev, &sl->attr_name);
367 device_unregister(&sl->dev);
371 err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
374 "sysfs file creation for [%s] failed. err=%d\n",
375 sl->dev.bus_id, err);
376 device_remove_file(&sl->dev, &sl->attr_name);
377 device_remove_file(&sl->dev, &sl->attr_val);
378 device_unregister(&sl->dev);
382 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
387 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
392 struct w1_netlink_msg msg;
394 sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
397 "%s: failed to allocate new slave device.\n",
402 memset(sl, 0, sizeof(*sl));
404 sl->owner = THIS_MODULE;
406 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
408 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
409 atomic_set(&sl->refcnt, 0);
410 init_completion(&sl->dev_released);
412 spin_lock(&w1_flock);
413 f = w1_family_registered(rn->family);
415 spin_unlock(&w1_flock);
416 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
417 rn->family, rn->family, rn->id, rn->crc);
422 spin_unlock(&w1_flock);
427 err = __w1_attach_slave_device(sl);
429 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
431 w1_family_put(sl->family);
436 sl->ttl = dev->slave_ttl;
439 memcpy(&msg.id.id, rn, sizeof(msg.id.id));
440 msg.type = W1_SLAVE_ADD;
441 w1_netlink_send(dev, &msg);
446 static void w1_slave_detach(struct w1_slave *sl)
448 struct w1_netlink_msg msg;
450 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
452 while (atomic_read(&sl->refcnt)) {
453 printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
454 sl->name, atomic_read(&sl->refcnt));
456 if (msleep_interruptible(1000))
457 flush_signals(current);
460 sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
461 device_remove_file(&sl->dev, &sl->attr_name);
462 device_remove_file(&sl->dev, &sl->attr_val);
463 device_unregister(&sl->dev);
464 w1_family_put(sl->family);
466 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
467 msg.type = W1_SLAVE_REMOVE;
468 w1_netlink_send(sl->master, &msg);
471 static void w1_search(struct w1_master *dev)
474 int i, count = 0, slave_count;
475 int last_family_desc, last_zero, last_device;
476 int search_bit, id_bit, comp_bit, desc_bit;
477 struct list_head *ent;
479 int family_found = 0;
483 search_bit = id_bit = comp_bit = 0;
485 last_device = last_zero = last_family_desc = 0;
489 while (!(id_bit && comp_bit) && !last_device
490 && count++ < dev->max_slave_count) {
494 last_family_desc = 0;
497 * Reset bus and all 1-wire device state machines
498 * so they can respond to our requests.
500 * Return 0 - device(s) present, 1 - no devices present.
502 if (w1_reset_bus(dev)) {
503 dev_info(&dev->dev, "No devices present on the wire.\n");
508 w1_write_8(dev, W1_SEARCH);
509 for (i = 0; i < 64; ++i) {
511 * Read 2 bits from bus.
512 * All who don't sleep must send ID bit and COMPLEMENT ID bit.
513 * They actually are ANDed between all senders.
515 id_bit = w1_touch_bit(dev, 1);
516 comp_bit = w1_touch_bit(dev, 1);
518 if (id_bit && comp_bit)
521 if (id_bit == 0 && comp_bit == 0) {
524 else if (i > desc_bit)
527 search_bit = ((last >> i) & 0x1);
529 if (search_bit == 0) {
532 last_family_desc = last_zero;
544 * and make all who don't have "search_bit" in "i"'th position
545 * in it's registration number sleep.
547 if (dev->bus_master->touch_bit)
548 w1_touch_bit(dev, search_bit);
550 w1_write_bit(dev, search_bit);
555 if (desc_bit == last_zero)
558 desc_bit = last_zero;
561 list_for_each(ent, &dev->slist) {
562 struct w1_reg_num *tmp;
564 tmp = (struct w1_reg_num *) &rn;
566 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
568 if (sl->reg_num.family == tmp->family &&
569 sl->reg_num.id == tmp->id &&
570 sl->reg_num.crc == tmp->crc) {
571 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
574 else if (sl->reg_num.family == tmp->family) {
582 if (slave_count == dev->slave_count &&
583 rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) {
584 w1_attach_slave_device(dev, (struct w1_reg_num *) &rn);
589 int w1_create_master_attributes(struct w1_master *dev)
591 if ( device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 ||
592 device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 ||
593 device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 ||
594 device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 ||
595 device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0||
596 device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0||
597 device_create_file(&dev->dev, &w1_master_attribute_name) < 0)
603 void w1_destroy_master_attributes(struct w1_master *dev)
605 device_remove_file(&dev->dev, &w1_master_attribute_slaves);
606 device_remove_file(&dev->dev, &w1_master_attribute_slave_count);
607 device_remove_file(&dev->dev, &w1_master_attribute_attempts);
608 device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count);
609 device_remove_file(&dev->dev, &w1_master_attribute_timeout);
610 device_remove_file(&dev->dev, &w1_master_attribute_pointer);
611 device_remove_file(&dev->dev, &w1_master_attribute_name);
615 int w1_control(void *data)
618 struct w1_master *dev;
619 struct list_head *ent, *ment, *n, *mn;
620 int err, have_to_wait = 0, timeout;
622 daemonize("w1_control");
623 allow_signal(SIGTERM);
625 while (!control_needs_exit || have_to_wait) {
628 timeout = w1_timeout*HZ;
630 timeout = interruptible_sleep_on_timeout(&w1_control_wait, timeout);
631 if (current->flags & PF_FREEZE)
632 refrigerator(PF_FREEZE);
633 } while (!signal_pending(current) && (timeout > 0));
635 if (signal_pending(current))
636 flush_signals(current);
638 list_for_each_safe(ment, mn, &w1_masters) {
639 dev = list_entry(ment, struct w1_master, w1_master_entry);
641 if (!control_needs_exit && !dev->need_exit)
644 * Little race: we can create thread but not set the flag.
645 * Get a chance for external process to set flag up.
647 if (!dev->initialized) {
652 spin_lock(&w1_mlock);
653 list_del(&dev->w1_master_entry);
654 spin_unlock(&w1_mlock);
656 if (control_needs_exit) {
659 err = kill_proc(dev->kpid, SIGTERM, 1);
662 "Failed to send signal to w1 kernel thread %d.\n",
666 wait_for_completion(&dev->dev_exited);
668 list_for_each_safe(ent, n, &dev->slist) {
669 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
673 "%s: slave entry is NULL.\n",
676 list_del(&sl->w1_slave_entry);
682 w1_destroy_master_attributes(dev);
683 atomic_dec(&dev->refcnt);
687 complete_and_exit(&w1_control_complete, 0);
690 int w1_process(void *data)
692 struct w1_master *dev = (struct w1_master *) data;
693 unsigned long timeout;
694 struct list_head *ent, *n;
697 daemonize("%s", dev->name);
698 allow_signal(SIGTERM);
700 while (!dev->need_exit) {
701 timeout = w1_timeout*HZ;
703 timeout = interruptible_sleep_on_timeout(&dev->kwait, timeout);
704 if (current->flags & PF_FREEZE)
705 refrigerator(PF_FREEZE);
706 } while (!signal_pending(current) && (timeout > 0));
708 if (signal_pending(current))
709 flush_signals(current);
714 if (!dev->initialized)
717 if (down_interruptible(&dev->mutex))
720 list_for_each_safe(ent, n, &dev->slist) {
721 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
724 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
729 list_for_each_safe(ent, n, &dev->slist) {
730 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
732 if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
733 list_del (&sl->w1_slave_entry);
735 w1_slave_detach (sl);
740 else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
741 sl->ttl = dev->slave_ttl;
746 atomic_dec(&dev->refcnt);
747 complete_and_exit(&dev->dev_exited, 0);
756 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
758 retval = bus_register(&w1_bus_type);
760 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
761 goto err_out_exit_init;
764 retval = driver_register(&w1_driver);
767 "Failed to register master driver. err=%d.\n",
769 goto err_out_bus_unregister;
772 control_thread = kernel_thread(&w1_control, NULL, 0);
773 if (control_thread < 0) {
774 printk(KERN_ERR "Failed to create control thread. err=%d\n",
776 retval = control_thread;
777 goto err_out_driver_unregister;
782 err_out_driver_unregister:
783 driver_unregister(&w1_driver);
785 err_out_bus_unregister:
786 bus_unregister(&w1_bus_type);
794 struct w1_master *dev;
795 struct list_head *ent, *n;
797 list_for_each_safe(ent, n, &w1_masters) {
798 dev = list_entry(ent, struct w1_master, w1_master_entry);
799 __w1_remove_master_device(dev);
802 control_needs_exit = 1;
804 wait_for_completion(&w1_control_complete);
806 driver_unregister(&w1_driver);
807 bus_unregister(&w1_bus_type);
810 module_init(w1_init);
811 module_exit(w1_fini);
813 EXPORT_SYMBOL(w1_create_master_attributes);
814 EXPORT_SYMBOL(w1_destroy_master_attributes);