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>
23 #include <asm/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 = 5 * HZ;
49 int w1_max_slave_count = 10;
51 module_param_named(timeout, w1_timeout, int, 0);
52 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
54 spinlock_t w1_mlock = SPIN_LOCK_UNLOCKED;
55 LIST_HEAD(w1_masters);
57 static pid_t control_thread;
58 static int control_needs_exit;
59 static DECLARE_COMPLETION(w1_control_complete);
60 static DECLARE_WAIT_QUEUE_HEAD(w1_control_wait);
62 static int w1_master_match(struct device *dev, struct device_driver *drv)
67 static int w1_master_probe(struct device *dev)
72 static int w1_master_remove(struct device *dev)
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,
99 return sprintf(buf, "No family registered.\n");
102 struct bus_type w1_bus_type = {
104 .match = w1_master_match,
107 struct device_driver w1_driver = {
110 .probe = w1_master_probe,
111 .remove = w1_master_remove,
114 struct device w1_device = {
117 .bus_id = "w1 bus master",
118 .driver = &w1_driver,
119 .release = &w1_master_release
122 static struct device_attribute w1_slave_attribute = {
128 .show = &w1_default_read_name,
131 static struct device_attribute w1_slave_attribute_val = {
137 .show = &w1_default_read_name,
140 static ssize_t w1_master_attribute_show(struct device *dev, char *buf)
142 return sprintf(buf, "please fix me\n");
144 struct w1_master *md = container_of(dev, struct w1_master, dev);
147 if (down_interruptible(&md->mutex))
150 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", md->name);
151 c -= snprintf(buf + PAGE_SIZE - c, c,
152 "bus_master=0x%p, timeout=%d, max_slave_count=%d, attempts=%lu\n",
153 md->bus_master, w1_timeout, md->max_slave_count,
155 c -= snprintf(buf + PAGE_SIZE - c, c, "%d slaves: ",
157 if (md->slave_count == 0)
158 c -= snprintf(buf + PAGE_SIZE - c, c, "no.\n");
160 struct list_head *ent, *n;
163 list_for_each_safe(ent, n, &md->slist) {
164 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
166 c -= snprintf(buf + PAGE_SIZE - c, c, "%s[%p] ",
169 c -= snprintf(buf + PAGE_SIZE - c, c, "\n");
174 return PAGE_SIZE - c;
178 struct device_attribute w1_master_attribute = {
180 .name = "w1_master_stats",
182 .owner = THIS_MODULE,
184 .show = &w1_master_attribute_show,
187 static struct bin_attribute w1_slave_bin_attribute = {
191 .owner = THIS_MODULE,
193 .size = W1_SLAVE_DATA_SIZE,
194 .read = &w1_default_read_bin,
197 static int __w1_attach_slave_device(struct w1_slave *sl)
201 sl->dev.parent = &sl->master->dev;
202 sl->dev.driver = sl->master->driver;
203 sl->dev.bus = &w1_bus_type;
204 sl->dev.release = &w1_slave_release;
206 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
208 (unsigned int) sl->reg_num.family,
209 (unsigned long long) sl->reg_num.id);
210 snprintf (&sl->name[0], sizeof(sl->name),
212 (unsigned int) sl->reg_num.family,
213 (unsigned long long) sl->reg_num.id);
215 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
218 err = device_register(&sl->dev);
221 "Device registration [%s] failed. err=%d\n",
222 sl->dev.bus_id, err);
226 w1_slave_bin_attribute.read = sl->family->fops->rbin;
227 w1_slave_attribute.show = sl->family->fops->rname;
228 w1_slave_attribute_val.show = sl->family->fops->rval;
229 w1_slave_attribute_val.attr.name = sl->family->fops->rvalname;
231 err = device_create_file(&sl->dev, &w1_slave_attribute);
234 "sysfs file creation for [%s] failed. err=%d\n",
235 sl->dev.bus_id, err);
236 device_unregister(&sl->dev);
240 err = device_create_file(&sl->dev, &w1_slave_attribute_val);
243 "sysfs file creation for [%s] failed. err=%d\n",
244 sl->dev.bus_id, err);
245 device_remove_file(&sl->dev, &w1_slave_attribute);
246 device_unregister(&sl->dev);
250 err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_bin_attribute);
253 "sysfs file creation for [%s] failed. err=%d\n",
254 sl->dev.bus_id, err);
255 device_remove_file(&sl->dev, &w1_slave_attribute);
256 device_remove_file(&sl->dev, &w1_slave_attribute_val);
257 device_unregister(&sl->dev);
261 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
266 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
272 sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
275 "%s: failed to allocate new slave device.\n",
280 memset(sl, 0, sizeof(*sl));
282 sl->owner = THIS_MODULE;
285 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
286 atomic_set(&sl->refcnt, 0);
287 init_completion(&sl->dev_released);
289 spin_lock(&w1_flock);
290 f = w1_family_registered(rn->family);
292 spin_unlock(&w1_flock);
293 dev_info(&dev->dev, "Family %x is not registered.\n",
299 spin_unlock(&w1_flock);
304 err = __w1_attach_slave_device(sl);
306 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
308 w1_family_put(sl->family);
318 static void w1_slave_detach(struct w1_slave *sl)
320 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
322 while (atomic_read(&sl->refcnt))
323 schedule_timeout(10);
325 sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_bin_attribute);
326 device_remove_file(&sl->dev, &w1_slave_attribute);
327 device_unregister(&sl->dev);
328 w1_family_put(sl->family);
331 static void w1_search(struct w1_master *dev)
334 int i, count = 0, slave_count;
335 int last_family_desc, last_zero, last_device;
336 int search_bit, id_bit, comp_bit, desc_bit;
337 struct list_head *ent;
339 int family_found = 0;
340 struct w1_netlink_msg msg;
344 memset(&msg, 0, sizeof(msg));
346 search_bit = id_bit = comp_bit = 0;
348 last_device = last_zero = last_family_desc = 0;
352 while (!(id_bit && comp_bit) && !last_device
353 && count++ < dev->max_slave_count) {
357 last_family_desc = 0;
360 * Reset bus and all 1-wire device state machines
361 * so they can respond to our requests.
363 * Return 0 - device(s) present, 1 - no devices present.
365 if (w1_reset_bus(dev)) {
366 dev_info(&dev->dev, "No devices present on the wire.\n");
371 memset(&msg, 0, sizeof(msg));
373 w1_write_8(dev, W1_SEARCH);
374 for (i = 0; i < 64; ++i) {
376 * Read 2 bits from bus.
377 * All who don't sleep must send ID bit and COMPLEMENT ID bit.
378 * They actually are ANDed between all senders.
380 id_bit = w1_read_bit(dev);
381 comp_bit = w1_read_bit(dev);
383 if (id_bit && comp_bit)
386 if (id_bit == 0 && comp_bit == 0) {
389 else if (i > desc_bit)
392 search_bit = ((last >> i) & 0x1);
394 if (search_bit == 0) {
397 last_family_desc = last_zero;
409 * and make all who don't have "search_bit" in "i"'th position
410 * in it's registration number sleep.
412 w1_write_bit(dev, search_bit);
417 msg.val = w1_calc_crc8((u8 *) & rn, 7);
418 w1_netlink_send(dev, &msg);
420 if (desc_bit == last_zero)
423 desc_bit = last_zero;
426 list_for_each(ent, &dev->slist) {
427 struct w1_reg_num *tmp;
429 tmp = (struct w1_reg_num *) &rn;
431 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
433 if (sl->reg_num.family == tmp->family &&
434 sl->reg_num.id == tmp->id &&
435 sl->reg_num.crc == tmp->crc)
437 else if (sl->reg_num.family == tmp->family) {
445 if (slave_count == dev->slave_count &&
446 msg.val && (*((__u8 *) & msg.val) == msg.id.id.crc)) {
447 w1_attach_slave_device(dev, (struct w1_reg_num *) &rn);
452 int w1_control(void *data)
455 struct w1_master *dev;
456 struct list_head *ent, *ment, *n, *mn;
457 int err, have_to_wait = 0, timeout;
459 daemonize("w1_control");
460 allow_signal(SIGTERM);
462 while (!control_needs_exit || have_to_wait) {
465 timeout = w1_timeout;
467 timeout = interruptible_sleep_on_timeout(&w1_control_wait, timeout);
468 if (current->flags & PF_FREEZE)
469 refrigerator(PF_FREEZE);
470 } while (!signal_pending(current) && (timeout > 0));
472 if (signal_pending(current))
473 flush_signals(current);
475 list_for_each_safe(ment, mn, &w1_masters) {
476 dev = list_entry(ment, struct w1_master, w1_master_entry);
478 if (!control_needs_exit && !dev->need_exit)
481 * Little race: we can create thread but not set the flag.
482 * Get a chance for external process to set flag up.
484 if (!dev->initialized) {
489 spin_lock(&w1_mlock);
490 list_del(&dev->w1_master_entry);
491 spin_unlock(&w1_mlock);
493 if (control_needs_exit) {
496 err = kill_proc(dev->kpid, SIGTERM, 1);
499 "Failed to send signal to w1 kernel thread %d.\n",
503 wait_for_completion(&dev->dev_exited);
505 list_for_each_safe(ent, n, &dev->slist) {
506 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
510 "%s: slave entry is NULL.\n",
513 list_del(&sl->w1_slave_entry);
519 device_remove_file(&dev->dev, &w1_master_attribute);
520 atomic_dec(&dev->refcnt);
524 complete_and_exit(&w1_control_complete, 0);
527 int w1_process(void *data)
529 struct w1_master *dev = (struct w1_master *) data;
530 unsigned long timeout;
532 daemonize("%s", dev->name);
533 allow_signal(SIGTERM);
535 while (!dev->need_exit) {
536 timeout = w1_timeout;
538 timeout = interruptible_sleep_on_timeout(&dev->kwait, timeout);
539 if (current->flags & PF_FREEZE)
540 refrigerator(PF_FREEZE);
541 } while (!signal_pending(current) && (timeout > 0));
543 if (signal_pending(current))
544 flush_signals(current);
549 if (!dev->initialized)
552 if (down_interruptible(&dev->mutex))
558 atomic_dec(&dev->refcnt);
559 complete_and_exit(&dev->dev_exited, 0);
568 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
570 retval = bus_register(&w1_bus_type);
572 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
573 goto err_out_exit_init;
576 retval = driver_register(&w1_driver);
579 "Failed to register master driver. err=%d.\n",
581 goto err_out_bus_unregister;
584 control_thread = kernel_thread(&w1_control, NULL, 0);
585 if (control_thread < 0) {
586 printk(KERN_ERR "Failed to create control thread. err=%d\n",
588 retval = control_thread;
589 goto err_out_driver_unregister;
594 err_out_driver_unregister:
595 driver_unregister(&w1_driver);
597 err_out_bus_unregister:
598 bus_unregister(&w1_bus_type);
606 struct w1_master *dev;
607 struct list_head *ent, *n;
609 list_for_each_safe(ent, n, &w1_masters) {
610 dev = list_entry(ent, struct w1_master, w1_master_entry);
611 __w1_remove_master_device(dev);
614 control_needs_exit = 1;
616 wait_for_completion(&w1_control_complete);
618 driver_unregister(&w1_driver);
619 bus_unregister(&w1_bus_type);
622 module_init(w1_init);
623 module_exit(w1_fini);