Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / w1 / w1.c
blobfd630cec0e7915bcdbc620d49b098002b18bbc0c
1 /*
2 * w1.c
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5 *
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 <linux/delay.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/list.h>
27 #include <linux/interrupt.h>
28 #include <linux/spinlock.h>
29 #include <linux/timer.h>
30 #include <linux/device.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
34 #include <asm/atomic.h>
36 #include "w1.h"
37 #include "w1_io.h"
38 #include "w1_log.h"
39 #include "w1_int.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);
62 static int w1_master_match(struct device *dev, struct device_driver *drv)
64 return 1;
67 static int w1_master_probe(struct device *dev)
69 return -ENODEV;
72 static int w1_master_remove(struct device *dev)
74 return 0;
77 static void w1_master_release(struct device *dev)
79 struct w1_master *md = container_of(dev, struct w1_master, dev);
81 complete(&md->dev_released);
84 static void w1_slave_release(struct device *dev)
86 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
88 complete(&sl->dev_released);
91 static ssize_t w1_default_read_name(struct device *dev, char *buf)
93 return sprintf(buf, "No family registered.\n");
96 static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
97 size_t count)
99 return sprintf(buf, "No family registered.\n");
102 static struct bus_type w1_bus_type = {
103 .name = "w1",
104 .match = w1_master_match,
107 struct device_driver w1_driver = {
108 .name = "w1_driver",
109 .bus = &w1_bus_type,
110 .probe = w1_master_probe,
111 .remove = w1_master_remove,
114 struct device w1_device = {
115 .parent = NULL,
116 .bus = &w1_bus_type,
117 .bus_id = "w1 bus master",
118 .driver = &w1_driver,
119 .release = &w1_master_release
122 static struct device_attribute w1_slave_attribute = {
123 .attr = {
124 .name = "name",
125 .mode = S_IRUGO,
126 .owner = THIS_MODULE
128 .show = &w1_default_read_name,
131 static struct device_attribute w1_slave_attribute_val = {
132 .attr = {
133 .name = "value",
134 .mode = S_IRUGO,
135 .owner = THIS_MODULE
137 .show = &w1_default_read_name,
140 static ssize_t w1_master_attribute_show_name(struct device *dev, char *buf)
142 struct w1_master *md = container_of (dev, struct w1_master, dev);
143 ssize_t count;
145 if (down_interruptible (&md->mutex))
146 return -EBUSY;
148 count = sprintf(buf, "%s\n", md->name);
150 up(&md->mutex);
152 return count;
155 static ssize_t w1_master_attribute_show_pointer(struct device *dev, char *buf)
157 struct w1_master *md = container_of(dev, struct w1_master, dev);
158 ssize_t count;
160 if (down_interruptible(&md->mutex))
161 return -EBUSY;
163 count = sprintf(buf, "0x%p\n", md->bus_master);
165 up(&md->mutex);
166 return count;
169 static ssize_t w1_master_attribute_show_timeout(struct device *dev, char *buf)
171 ssize_t count;
172 count = sprintf(buf, "%d\n", w1_timeout);
173 return count;
176 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, char *buf)
178 struct w1_master *md = container_of(dev, struct w1_master, dev);
179 ssize_t count;
181 if (down_interruptible(&md->mutex))
182 return -EBUSY;
184 count = sprintf(buf, "%d\n", md->max_slave_count);
186 up(&md->mutex);
187 return count;
190 static ssize_t w1_master_attribute_show_attempts(struct device *dev, char *buf)
192 struct w1_master *md = container_of(dev, struct w1_master, dev);
193 ssize_t count;
195 if (down_interruptible(&md->mutex))
196 return -EBUSY;
198 count = sprintf(buf, "%lu\n", md->attempts);
200 up(&md->mutex);
201 return count;
204 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, char *buf)
206 struct w1_master *md = container_of(dev, struct w1_master, dev);
207 ssize_t count;
209 if (down_interruptible(&md->mutex))
210 return -EBUSY;
212 count = sprintf(buf, "%d\n", md->slave_count);
214 up(&md->mutex);
215 return count;
218 static ssize_t w1_master_attribute_show_slaves(struct device *dev, char *buf)
221 struct w1_master *md = container_of(dev, struct w1_master, dev);
222 int c = PAGE_SIZE;
224 if (down_interruptible(&md->mutex))
225 return -EBUSY;
227 if (md->slave_count == 0)
228 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
229 else {
230 struct list_head *ent, *n;
231 struct w1_slave *sl;
233 list_for_each_safe(ent, n, &md->slist) {
234 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
236 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
240 up(&md->mutex);
242 return PAGE_SIZE - c;
245 static struct device_attribute w1_master_attribute_slaves = {
246 .attr = {
247 .name = "w1_master_slaves",
248 .mode = S_IRUGO,
249 .owner = THIS_MODULE,
251 .show = &w1_master_attribute_show_slaves,
253 static struct device_attribute w1_master_attribute_slave_count = {
254 .attr = {
255 .name = "w1_master_slave_count",
256 .mode = S_IRUGO,
257 .owner = THIS_MODULE
259 .show = &w1_master_attribute_show_slave_count,
261 static struct device_attribute w1_master_attribute_attempts = {
262 .attr = {
263 .name = "w1_master_attempts",
264 .mode = S_IRUGO,
265 .owner = THIS_MODULE
267 .show = &w1_master_attribute_show_attempts,
269 static struct device_attribute w1_master_attribute_max_slave_count = {
270 .attr = {
271 .name = "w1_master_max_slave_count",
272 .mode = S_IRUGO,
273 .owner = THIS_MODULE
275 .show = &w1_master_attribute_show_max_slave_count,
277 static struct device_attribute w1_master_attribute_timeout = {
278 .attr = {
279 .name = "w1_master_timeout",
280 .mode = S_IRUGO,
281 .owner = THIS_MODULE
283 .show = &w1_master_attribute_show_timeout,
285 static struct device_attribute w1_master_attribute_pointer = {
286 .attr = {
287 .name = "w1_master_pointer",
288 .mode = S_IRUGO,
289 .owner = THIS_MODULE
291 .show = &w1_master_attribute_show_pointer,
293 static struct device_attribute w1_master_attribute_name = {
294 .attr = {
295 .name = "w1_master_name",
296 .mode = S_IRUGO,
297 .owner = THIS_MODULE
299 .show = &w1_master_attribute_show_name,
302 static struct bin_attribute w1_slave_bin_attribute = {
303 .attr = {
304 .name = "w1_slave",
305 .mode = S_IRUGO,
306 .owner = THIS_MODULE,
308 .size = W1_SLAVE_DATA_SIZE,
309 .read = &w1_default_read_bin,
312 static int __w1_attach_slave_device(struct w1_slave *sl)
314 int err;
316 sl->dev.parent = &sl->master->dev;
317 sl->dev.driver = sl->master->driver;
318 sl->dev.bus = &w1_bus_type;
319 sl->dev.release = &w1_slave_release;
321 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
322 "%02x-%012llx",
323 (unsigned int) sl->reg_num.family,
324 (unsigned long long) sl->reg_num.id);
325 snprintf (&sl->name[0], sizeof(sl->name),
326 "%02x-%012llx",
327 (unsigned int) sl->reg_num.family,
328 (unsigned long long) sl->reg_num.id);
330 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
331 &sl->dev.bus_id[0]);
333 err = device_register(&sl->dev);
334 if (err < 0) {
335 dev_err(&sl->dev,
336 "Device registration [%s] failed. err=%d\n",
337 sl->dev.bus_id, err);
338 return err;
341 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
342 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
343 memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val));
345 sl->attr_bin.read = sl->family->fops->rbin;
346 sl->attr_name.show = sl->family->fops->rname;
347 sl->attr_val.show = sl->family->fops->rval;
348 sl->attr_val.attr.name = sl->family->fops->rvalname;
350 err = device_create_file(&sl->dev, &sl->attr_name);
351 if (err < 0) {
352 dev_err(&sl->dev,
353 "sysfs file creation for [%s] failed. err=%d\n",
354 sl->dev.bus_id, err);
355 device_unregister(&sl->dev);
356 return err;
359 err = device_create_file(&sl->dev, &sl->attr_val);
360 if (err < 0) {
361 dev_err(&sl->dev,
362 "sysfs file creation for [%s] failed. err=%d\n",
363 sl->dev.bus_id, err);
364 device_remove_file(&sl->dev, &sl->attr_name);
365 device_unregister(&sl->dev);
366 return err;
369 err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
370 if (err < 0) {
371 dev_err(&sl->dev,
372 "sysfs file creation for [%s] failed. err=%d\n",
373 sl->dev.bus_id, err);
374 device_remove_file(&sl->dev, &sl->attr_name);
375 device_remove_file(&sl->dev, &sl->attr_val);
376 device_unregister(&sl->dev);
377 return err;
380 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
382 return 0;
385 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
387 struct w1_slave *sl;
388 struct w1_family *f;
389 int err;
390 struct w1_netlink_msg msg;
392 sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
393 if (!sl) {
394 dev_err(&dev->dev,
395 "%s: failed to allocate new slave device.\n",
396 __func__);
397 return -ENOMEM;
400 memset(sl, 0, sizeof(*sl));
402 sl->owner = THIS_MODULE;
403 sl->master = dev;
404 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
406 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
407 atomic_set(&sl->refcnt, 0);
408 init_completion(&sl->dev_released);
410 spin_lock(&w1_flock);
411 f = w1_family_registered(rn->family);
412 if (!f) {
413 spin_unlock(&w1_flock);
414 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
415 rn->family, rn->family,
416 (unsigned long long)rn->id, rn->crc);
417 kfree(sl);
418 return -ENODEV;
420 __w1_family_get(f);
421 spin_unlock(&w1_flock);
423 sl->family = f;
426 err = __w1_attach_slave_device(sl);
427 if (err < 0) {
428 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
429 sl->name);
430 w1_family_put(sl->family);
431 kfree(sl);
432 return err;
435 sl->ttl = dev->slave_ttl;
436 dev->slave_count++;
438 memcpy(&msg.id.id, rn, sizeof(msg.id.id));
439 msg.type = W1_SLAVE_ADD;
440 w1_netlink_send(dev, &msg);
442 return 0;
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;
473 int found = 0;
475 spin_lock_irq(&w1_mlock);
476 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
477 if (dev->bus_master->data == data) {
478 found = 1;
479 atomic_inc(&dev->refcnt);
480 break;
483 spin_unlock_irq(&w1_mlock);
485 return (found)?dev:NULL;
488 void w1_slave_found(unsigned long data, u64 rn)
490 int slave_count;
491 struct w1_slave *sl;
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);
498 if (!dev) {
499 printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
500 data);
501 return;
504 tmp = (struct w1_reg_num *) &rn;
506 slave_count = 0;
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);
515 break;
517 else if (sl->reg_num.family == tmp->family) {
518 family_found = 1;
519 break;
522 slave_count++;
525 if (slave_count == dev->slave_count && rn ) {
526 tmp = cpu_to_le64(rn);
527 if(((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&tmp, 7))
528 w1_attach_slave_device(dev, (struct w1_reg_num *) &rn);
531 atomic_dec(&dev->refcnt);
534 void w1_search(struct w1_master *dev)
536 u64 last, rn, tmp;
537 int i, count = 0;
538 int last_family_desc, last_zero, last_device;
539 int search_bit, id_bit, comp_bit, desc_bit;
541 search_bit = id_bit = comp_bit = 0;
542 rn = tmp = last = 0;
543 last_device = last_zero = last_family_desc = 0;
545 desc_bit = 64;
547 while (!(id_bit && comp_bit) && !last_device
548 && count++ < dev->max_slave_count) {
549 last = rn;
550 rn = 0;
552 last_family_desc = 0;
555 * Reset bus and all 1-wire device state machines
556 * so they can respond to our requests.
558 * Return 0 - device(s) present, 1 - no devices present.
560 if (w1_reset_bus(dev)) {
561 dev_info(&dev->dev, "No devices present on the wire.\n");
562 break;
565 #if 1
566 w1_write_8(dev, W1_SEARCH);
567 for (i = 0; i < 64; ++i) {
569 * Read 2 bits from bus.
570 * All who don't sleep must send ID bit and COMPLEMENT ID bit.
571 * They actually are ANDed between all senders.
573 id_bit = w1_touch_bit(dev, 1);
574 comp_bit = w1_touch_bit(dev, 1);
576 if (id_bit && comp_bit)
577 break;
579 if (id_bit == 0 && comp_bit == 0) {
580 if (i == desc_bit)
581 search_bit = 1;
582 else if (i > desc_bit)
583 search_bit = 0;
584 else
585 search_bit = ((last >> i) & 0x1);
587 if (search_bit == 0) {
588 last_zero = i;
589 if (last_zero < 9)
590 last_family_desc = last_zero;
594 else
595 search_bit = id_bit;
597 tmp = search_bit;
598 rn |= (tmp << i);
601 * Write 1 bit to bus
602 * and make all who don't have "search_bit" in "i"'th position
603 * in it's registration number sleep.
605 if (dev->bus_master->touch_bit)
606 w1_touch_bit(dev, search_bit);
607 else
608 w1_write_bit(dev, search_bit);
611 #endif
613 if (desc_bit == last_zero)
614 last_device = 1;
616 desc_bit = last_zero;
618 w1_slave_found(dev->bus_master->data, rn);
622 int w1_create_master_attributes(struct w1_master *dev)
624 if ( device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 ||
625 device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 ||
626 device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 ||
627 device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 ||
628 device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0||
629 device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0||
630 device_create_file(&dev->dev, &w1_master_attribute_name) < 0)
631 return -EINVAL;
633 return 0;
636 void w1_destroy_master_attributes(struct w1_master *dev)
638 device_remove_file(&dev->dev, &w1_master_attribute_slaves);
639 device_remove_file(&dev->dev, &w1_master_attribute_slave_count);
640 device_remove_file(&dev->dev, &w1_master_attribute_attempts);
641 device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count);
642 device_remove_file(&dev->dev, &w1_master_attribute_timeout);
643 device_remove_file(&dev->dev, &w1_master_attribute_pointer);
644 device_remove_file(&dev->dev, &w1_master_attribute_name);
648 int w1_control(void *data)
650 struct w1_slave *sl;
651 struct w1_master *dev;
652 struct list_head *ent, *ment, *n, *mn;
653 int err, have_to_wait = 0;
655 daemonize("w1_control");
656 allow_signal(SIGTERM);
658 while (!control_needs_exit || have_to_wait) {
659 have_to_wait = 0;
661 try_to_freeze(PF_FREEZE);
662 msleep_interruptible(w1_timeout * 1000);
664 if (signal_pending(current))
665 flush_signals(current);
667 list_for_each_safe(ment, mn, &w1_masters) {
668 dev = list_entry(ment, struct w1_master, w1_master_entry);
670 if (!control_needs_exit && !dev->need_exit)
671 continue;
673 * Little race: we can create thread but not set the flag.
674 * Get a chance for external process to set flag up.
676 if (!dev->initialized) {
677 have_to_wait = 1;
678 continue;
681 spin_lock(&w1_mlock);
682 list_del(&dev->w1_master_entry);
683 spin_unlock(&w1_mlock);
685 if (control_needs_exit) {
686 dev->need_exit = 1;
688 err = kill_proc(dev->kpid, SIGTERM, 1);
689 if (err)
690 dev_err(&dev->dev,
691 "Failed to send signal to w1 kernel thread %d.\n",
692 dev->kpid);
695 wait_for_completion(&dev->dev_exited);
697 list_for_each_safe(ent, n, &dev->slist) {
698 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
700 if (!sl)
701 dev_warn(&dev->dev,
702 "%s: slave entry is NULL.\n",
703 __func__);
704 else {
705 list_del(&sl->w1_slave_entry);
707 w1_slave_detach(sl);
708 kfree(sl);
711 w1_destroy_master_attributes(dev);
712 atomic_dec(&dev->refcnt);
716 complete_and_exit(&w1_control_complete, 0);
719 int w1_process(void *data)
721 struct w1_master *dev = (struct w1_master *) data;
722 struct list_head *ent, *n;
723 struct w1_slave *sl;
725 daemonize("%s", dev->name);
726 allow_signal(SIGTERM);
728 while (!dev->need_exit) {
729 try_to_freeze(PF_FREEZE);
730 msleep_interruptible(w1_timeout * 1000);
732 if (signal_pending(current))
733 flush_signals(current);
735 if (dev->need_exit)
736 break;
738 if (!dev->initialized)
739 continue;
741 if (down_interruptible(&dev->mutex))
742 continue;
744 list_for_each_safe(ent, n, &dev->slist) {
745 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
747 if (sl)
748 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
751 w1_search_devices(dev, w1_slave_found);
753 list_for_each_safe(ent, n, &dev->slist) {
754 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
756 if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
757 list_del (&sl->w1_slave_entry);
759 w1_slave_detach (sl);
760 kfree (sl);
762 dev->slave_count--;
764 else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
765 sl->ttl = dev->slave_ttl;
767 up(&dev->mutex);
770 atomic_dec(&dev->refcnt);
771 complete_and_exit(&dev->dev_exited, 0);
773 return 0;
776 int w1_init(void)
778 int retval;
780 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
782 retval = bus_register(&w1_bus_type);
783 if (retval) {
784 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
785 goto err_out_exit_init;
788 retval = driver_register(&w1_driver);
789 if (retval) {
790 printk(KERN_ERR
791 "Failed to register master driver. err=%d.\n",
792 retval);
793 goto err_out_bus_unregister;
796 control_thread = kernel_thread(&w1_control, NULL, 0);
797 if (control_thread < 0) {
798 printk(KERN_ERR "Failed to create control thread. err=%d\n",
799 control_thread);
800 retval = control_thread;
801 goto err_out_driver_unregister;
804 return 0;
806 err_out_driver_unregister:
807 driver_unregister(&w1_driver);
809 err_out_bus_unregister:
810 bus_unregister(&w1_bus_type);
812 err_out_exit_init:
813 return retval;
816 void w1_fini(void)
818 struct w1_master *dev;
819 struct list_head *ent, *n;
821 list_for_each_safe(ent, n, &w1_masters) {
822 dev = list_entry(ent, struct w1_master, w1_master_entry);
823 __w1_remove_master_device(dev);
826 control_needs_exit = 1;
828 wait_for_completion(&w1_control_complete);
830 driver_unregister(&w1_driver);
831 bus_unregister(&w1_bus_type);
834 module_init(w1_init);
835 module_exit(w1_fini);