Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 2 | * w1_int.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/delay.h> |
Evgeniy Polyakov | 674a396c | 2006-02-20 11:15:37 +0300 | [diff] [blame] | 25 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include "w1.h" |
| 28 | #include "w1_log.h" |
| 29 | #include "w1_netlink.h" |
| 30 | |
| 31 | static u32 w1_ids = 1; |
| 32 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 33 | extern struct device_driver w1_master_driver; |
| 34 | extern struct bus_type w1_bus_type; |
| 35 | extern struct device w1_master_device; |
| 36 | extern int w1_max_slave_count; |
| 37 | extern int w1_max_slave_ttl; |
| 38 | extern struct list_head w1_masters; |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame^] | 39 | extern struct mutex w1_mlock; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 40 | |
| 41 | extern int w1_process(void *); |
| 42 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 43 | static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl, |
| 44 | struct device_driver *driver, |
| 45 | struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | struct w1_master *dev; |
| 48 | int err; |
| 49 | |
| 50 | /* |
| 51 | * We are in process context(kernel thread), so can sleep. |
| 52 | */ |
| 53 | dev = kmalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL); |
| 54 | if (!dev) { |
| 55 | printk(KERN_ERR |
| 56 | "Failed to allocate %zd bytes for new w1 device.\n", |
| 57 | sizeof(struct w1_master)); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | memset(dev, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master)); |
| 62 | |
| 63 | dev->bus_master = (struct w1_bus_master *)(dev + 1); |
| 64 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 65 | dev->owner = THIS_MODULE; |
| 66 | dev->max_slave_count = slave_count; |
| 67 | dev->slave_count = 0; |
| 68 | dev->attempts = 0; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 69 | dev->initialized = 0; |
| 70 | dev->id = id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | dev->slave_ttl = slave_ttl; |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 72 | dev->search_count = -1; /* continual scan */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | atomic_set(&dev->refcnt, 2); |
| 75 | |
| 76 | INIT_LIST_HEAD(&dev->slist); |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame^] | 77 | mutex_init(&dev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | memcpy(&dev->dev, device, sizeof(struct device)); |
| 80 | snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id), |
| 81 | "w1_bus_master%u", dev->id); |
| 82 | snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id); |
| 83 | |
| 84 | dev->driver = driver; |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | dev->seq = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | err = device_register(&dev->dev); |
| 89 | if (err) { |
| 90 | printk(KERN_ERR "Failed to register master device. err=%d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | memset(dev, 0, sizeof(struct w1_master)); |
| 92 | kfree(dev); |
| 93 | dev = NULL; |
| 94 | } |
| 95 | |
| 96 | return dev; |
| 97 | } |
| 98 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 99 | void w1_free_dev(struct w1_master *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | device_unregister(&dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int w1_add_master_device(struct w1_bus_master *master) |
| 105 | { |
| 106 | struct w1_master *dev; |
| 107 | int retval = 0; |
| 108 | struct w1_netlink_msg msg; |
| 109 | |
Evgeniy Polyakov | be57ce2 | 2005-06-04 01:21:46 +0400 | [diff] [blame] | 110 | /* validate minimum functionality */ |
| 111 | if (!(master->touch_bit && master->reset_bus) && |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 112 | !(master->write_bit && master->read_bit)) { |
| 113 | printk(KERN_ERR "w1_add_master_device: invalid function set\n"); |
| 114 | return(-EINVAL); |
Evgeniy Polyakov | be57ce2 | 2005-06-04 01:21:46 +0400 | [diff] [blame] | 115 | } |
| 116 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 117 | dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | if (!dev) |
| 119 | return -ENOMEM; |
| 120 | |
Evgeniy Polyakov | 674a396c | 2006-02-20 11:15:37 +0300 | [diff] [blame] | 121 | dev->thread = kthread_run(&w1_process, dev, "%s", dev->name); |
| 122 | if (IS_ERR(dev->thread)) { |
| 123 | retval = PTR_ERR(dev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | dev_err(&dev->dev, |
| 125 | "Failed to create new kernel thread. err=%d\n", |
Evgeniy Polyakov | 674a396c | 2006-02-20 11:15:37 +0300 | [diff] [blame] | 126 | retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | goto err_out_free_dev; |
| 128 | } |
| 129 | |
| 130 | retval = w1_create_master_attributes(dev); |
| 131 | if (retval) |
| 132 | goto err_out_kill_thread; |
| 133 | |
| 134 | memcpy(dev->bus_master, master, sizeof(struct w1_bus_master)); |
| 135 | |
| 136 | dev->initialized = 1; |
| 137 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame^] | 138 | mutex_lock(&w1_mlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | list_add(&dev->w1_master_entry, &w1_masters); |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame^] | 140 | mutex_unlock(&w1_mlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 142 | memset(&msg, 0, sizeof(msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | msg.id.mst.id = dev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | msg.type = W1_MASTER_ADD; |
| 145 | w1_netlink_send(dev, &msg); |
| 146 | |
| 147 | return 0; |
| 148 | |
| 149 | err_out_kill_thread: |
Evgeniy Polyakov | 674a396c | 2006-02-20 11:15:37 +0300 | [diff] [blame] | 150 | kthread_stop(dev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | err_out_free_dev: |
| 152 | w1_free_dev(dev); |
| 153 | |
| 154 | return retval; |
| 155 | } |
| 156 | |
| 157 | void __w1_remove_master_device(struct w1_master *dev) |
| 158 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | struct w1_netlink_msg msg; |
| 160 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 161 | set_bit(W1_MASTER_NEED_EXIT, &dev->flags); |
Evgeniy Polyakov | 674a396c | 2006-02-20 11:15:37 +0300 | [diff] [blame] | 162 | kthread_stop(dev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | while (atomic_read(&dev->refcnt)) { |
Evgeniy Polyakov | 674a396c | 2006-02-20 11:15:37 +0300 | [diff] [blame] | 165 | dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | dev->name, atomic_read(&dev->refcnt)); |
| 167 | |
| 168 | if (msleep_interruptible(1000)) |
| 169 | flush_signals(current); |
| 170 | } |
| 171 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 172 | memset(&msg, 0, sizeof(msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | msg.id.mst.id = dev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | msg.type = W1_MASTER_REMOVE; |
| 175 | w1_netlink_send(dev, &msg); |
| 176 | |
| 177 | w1_free_dev(dev); |
| 178 | } |
| 179 | |
| 180 | void w1_remove_master_device(struct w1_bus_master *bm) |
| 181 | { |
| 182 | struct w1_master *dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 184 | list_for_each_entry(dev, &w1_masters, w1_master_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (!dev->initialized) |
| 186 | continue; |
| 187 | |
| 188 | if (dev->bus_master->data == bm->data) |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | if (!dev) { |
| 193 | printk(KERN_ERR "Device doesn't exist.\n"); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | __w1_remove_master_device(dev); |
| 198 | } |
| 199 | |
| 200 | EXPORT_SYMBOL(w1_add_master_device); |
| 201 | EXPORT_SYMBOL(w1_remove_master_device); |