w1_int.c 6.35 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Evgeniy Polyakov's avatar
Evgeniy Polyakov committed
2
 *	w1_int.c
Linus Torvalds's avatar
Linus Torvalds committed
3
 *
4
 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
Evgeniy Polyakov's avatar
Evgeniy Polyakov committed
5
 *
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/delay.h>
25
#include <linux/kthread.h>
26
#include <linux/slab.h>
27
#include <linux/export.h>
28
#include <linux/moduleparam.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32

#include "w1.h"
#include "w1_log.h"
#include "w1_netlink.h"
33
#include "w1_int.h"
Linus Torvalds's avatar
Linus Torvalds committed
34

35 36
static int w1_search_count = -1; /* Default is continual scan */
module_param_named(search_count, w1_search_count, int, 0);
Linus Torvalds's avatar
Linus Torvalds committed
37

38 39 40
static int w1_enable_pullup = 1;
module_param_named(enable_pullup, w1_enable_pullup, int, 0);

41
static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
Evgeniy Polyakov's avatar
Evgeniy Polyakov committed
42 43
				       struct device_driver *driver,
				       struct device *device)
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49 50
{
	struct w1_master *dev;
	int err;

	/*
	 * We are in process context(kernel thread), so can sleep.
	 */
51
	dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
52
	if (!dev) {
53
		pr_err("Failed to allocate %zd bytes for new w1 device.\n",
Linus Torvalds's avatar
Linus Torvalds committed
54 55 56 57 58 59 60
			sizeof(struct w1_master));
		return NULL;
	}


	dev->bus_master = (struct w1_bus_master *)(dev + 1);

Evgeniy Polyakov's avatar
Evgeniy Polyakov committed
61 62 63 64 65 66
	dev->owner		= THIS_MODULE;
	dev->max_slave_count	= slave_count;
	dev->slave_count	= 0;
	dev->attempts		= 0;
	dev->initialized	= 0;
	dev->id			= id;
Linus Torvalds's avatar
Linus Torvalds committed
67
	dev->slave_ttl		= slave_ttl;
68
	dev->search_count	= w1_search_count;
69
	dev->enable_pullup	= w1_enable_pullup;
Linus Torvalds's avatar
Linus Torvalds committed
70

David Fries's avatar
David Fries committed
71 72 73
	/* 1 for w1_process to decrement
	 * 1 for __w1_remove_master_device to decrement
	 */
Linus Torvalds's avatar
Linus Torvalds committed
74 75 76
	atomic_set(&dev->refcnt, 2);

	INIT_LIST_HEAD(&dev->slist);
77
	INIT_LIST_HEAD(&dev->async_list);
78
	mutex_init(&dev->mutex);
79
	mutex_init(&dev->bus_mutex);
80
	mutex_init(&dev->list_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
81 82

	memcpy(&dev->dev, device, sizeof(struct device));
83
	dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
Linus Torvalds's avatar
Linus Torvalds committed
84
	snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
85
	dev->dev.init_name = dev->name;
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89 90 91 92

	dev->driver = driver;

	dev->seq = 1;

	err = device_register(&dev->dev);
	if (err) {
93
		pr_err("Failed to register master device. err=%d\n", err);
94
		put_device(&dev->dev);
Linus Torvalds's avatar
Linus Torvalds committed
95 96 97 98 99 100
		dev = NULL;
	}

	return dev;
}

Evgeniy Polyakov's avatar
Evgeniy Polyakov committed
101
static void w1_free_dev(struct w1_master *dev)
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105
{
	device_unregister(&dev->dev);
}

106 107 108 109
/**
 * w1_add_master_device() - registers a new master device
 * @master:	master bus device to register
 */
Linus Torvalds's avatar
Linus Torvalds committed
110 111
int w1_add_master_device(struct w1_bus_master *master)
{
112
	struct w1_master *dev, *entry;
Linus Torvalds's avatar
Linus Torvalds committed
113 114
	int retval = 0;
	struct w1_netlink_msg msg;
115
	int id, found;
Linus Torvalds's avatar
Linus Torvalds committed
116

117 118 119
	/* validate minimum functionality */
	if (!(master->touch_bit && master->reset_bus) &&
	    !(master->write_bit && master->read_bit) &&
120
	    !(master->write_byte && master->read_byte && master->reset_bus)) {
121
		pr_err("w1_add_master_device: invalid function set\n");
122
		return(-EINVAL);
123
	}
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	/* Lock until the device is added (or not) to w1_masters. */
	mutex_lock(&w1_mlock);
	/* Search for the first available id (starting at 1). */
	id = 0;
	do {
		++id;
		found = 0;
		list_for_each_entry(entry, &w1_masters, w1_master_entry) {
			if (entry->id == id) {
				found = 1;
				break;
			}
		}
	} while (found);

	dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
		&w1_master_driver, &w1_master_device);
	if (!dev) {
		mutex_unlock(&w1_mlock);
Linus Torvalds's avatar
Linus Torvalds committed
144
		return -ENOMEM;
145
	}
Linus Torvalds's avatar
Linus Torvalds committed
146

David Fries's avatar
David Fries committed
147
	retval =  w1_create_master_attributes(dev);
148 149
	if (retval) {
		mutex_unlock(&w1_mlock);
David Fries's avatar
David Fries committed
150
		goto err_out_free_dev;
151
	}
David Fries's avatar
David Fries committed
152 153 154 155 156

	memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));

	dev->initialized = 1;

157 158 159
	dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
	if (IS_ERR(dev->thread)) {
		retval = PTR_ERR(dev->thread);
Linus Torvalds's avatar
Linus Torvalds committed
160 161
		dev_err(&dev->dev,
			 "Failed to create new kernel thread. err=%d\n",
162
			 retval);
163
		mutex_unlock(&w1_mlock);
David Fries's avatar
David Fries committed
164
		goto err_out_rm_attr;
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167
	}

	list_add(&dev->w1_master_entry, &w1_masters);
168
	mutex_unlock(&w1_mlock);
Linus Torvalds's avatar
Linus Torvalds committed
169

170
	memset(&msg, 0, sizeof(msg));
Linus Torvalds's avatar
Linus Torvalds committed
171 172 173 174 175 176
	msg.id.mst.id = dev->id;
	msg.type = W1_MASTER_ADD;
	w1_netlink_send(dev, &msg);

	return 0;

David Fries's avatar
David Fries committed
177
#if 0 /* Thread cleanup code, not required currently. */
Linus Torvalds's avatar
Linus Torvalds committed
178
err_out_kill_thread:
179
	set_bit(W1_ABORT_SEARCH, &dev->flags);
180
	kthread_stop(dev->thread);
David Fries's avatar
David Fries committed
181 182 183
#endif
err_out_rm_attr:
	w1_destroy_master_attributes(dev);
Linus Torvalds's avatar
Linus Torvalds committed
184 185 186 187 188 189 190 191 192
err_out_free_dev:
	w1_free_dev(dev);

	return retval;
}

void __w1_remove_master_device(struct w1_master *dev)
{
	struct w1_netlink_msg msg;
193
	struct w1_slave *sl, *sln;
Linus Torvalds's avatar
Linus Torvalds committed
194

195 196 197 198
	mutex_lock(&w1_mlock);
	list_del(&dev->w1_master_entry);
	mutex_unlock(&w1_mlock);

199 200 201
	set_bit(W1_ABORT_SEARCH, &dev->flags);
	kthread_stop(dev->thread);

202
	mutex_lock(&dev->mutex);
203 204 205
	mutex_lock(&dev->list_mutex);
	list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
		mutex_unlock(&dev->list_mutex);
206
		w1_slave_detach(sl);
207 208
		mutex_lock(&dev->list_mutex);
	}
209
	w1_destroy_master_attributes(dev);
210
	mutex_unlock(&dev->list_mutex);
211 212 213
	mutex_unlock(&dev->mutex);
	atomic_dec(&dev->refcnt);

Linus Torvalds's avatar
Linus Torvalds committed
214
	while (atomic_read(&dev->refcnt)) {
215
		dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219
				dev->name, atomic_read(&dev->refcnt));

		if (msleep_interruptible(1000))
			flush_signals(current);
220
		mutex_lock(&dev->list_mutex);
221
		w1_process_callbacks(dev);
222
		mutex_unlock(&dev->list_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
223
	}
224
	mutex_lock(&dev->list_mutex);
225
	w1_process_callbacks(dev);
226
	mutex_unlock(&dev->list_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
227

228
	memset(&msg, 0, sizeof(msg));
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231 232 233 234 235
	msg.id.mst.id = dev->id;
	msg.type = W1_MASTER_REMOVE;
	w1_netlink_send(dev, &msg);

	w1_free_dev(dev);
}

236 237 238 239
/**
 * w1_remove_master_device() - unregister a master device
 * @bm:	master bus device to remove
 */
Linus Torvalds's avatar
Linus Torvalds committed
240 241
void w1_remove_master_device(struct w1_bus_master *bm)
{
242
	struct w1_master *dev, *found = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
243

Evgeniy Polyakov's avatar
Evgeniy Polyakov committed
244
	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247
		if (!dev->initialized)
			continue;

248 249
		if (dev->bus_master->data == bm->data) {
			found = dev;
Linus Torvalds's avatar
Linus Torvalds committed
250
			break;
251
		}
Linus Torvalds's avatar
Linus Torvalds committed
252 253
	}

254
	if (!found) {
255
		pr_err("Device doesn't exist.\n");
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258
		return;
	}

259
	__w1_remove_master_device(found);
Linus Torvalds's avatar
Linus Torvalds committed
260 261 262 263
}

EXPORT_SYMBOL(w1_add_master_device);
EXPORT_SYMBOL(w1_remove_master_device);