w1_int.c 5.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
E
Evgeniy Polyakov 已提交
2
 *	w1_int.c
L
Linus Torvalds 已提交
3 4
 *
 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
E
Evgeniy Polyakov 已提交
5
 *
L
Linus Torvalds 已提交
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>
E
Evgeniy Polyakov 已提交
25
#include <linux/kthread.h>
L
Linus Torvalds 已提交
26 27 28 29

#include "w1.h"
#include "w1_log.h"
#include "w1_netlink.h"
E
Evgeniy Polyakov 已提交
30
#include "w1_int.h"
L
Linus Torvalds 已提交
31 32

static u32 w1_ids = 1;
33 34
static int w1_search_count = -1; /* Default is continual scan */
module_param_named(search_count, w1_search_count, int, 0);
L
Linus Torvalds 已提交
35

36 37 38
static int w1_enable_pullup = 1;
module_param_named(enable_pullup, w1_enable_pullup, int, 0);

E
Evgeniy Polyakov 已提交
39 40 41
static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
				       struct device_driver *driver,
				       struct device *device)
L
Linus Torvalds 已提交
42 43 44 45 46 47 48
{
	struct w1_master *dev;
	int err;

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


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

E
Evgeniy Polyakov 已提交
60 61 62 63 64 65
	dev->owner		= THIS_MODULE;
	dev->max_slave_count	= slave_count;
	dev->slave_count	= 0;
	dev->attempts		= 0;
	dev->initialized	= 0;
	dev->id			= id;
L
Linus Torvalds 已提交
66
	dev->slave_ttl		= slave_ttl;
67
	dev->search_count	= w1_search_count;
68
	dev->enable_pullup	= w1_enable_pullup;
L
Linus Torvalds 已提交
69

D
David Fries 已提交
70 71 72
	/* 1 for w1_process to decrement
	 * 1 for __w1_remove_master_device to decrement
	 */
L
Linus Torvalds 已提交
73 74 75
	atomic_set(&dev->refcnt, 2);

	INIT_LIST_HEAD(&dev->slist);
76
	mutex_init(&dev->mutex);
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

	memcpy(&dev->dev, device, sizeof(struct device));
	snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
		  "w1_bus_master%u", dev->id);
	snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);

	dev->driver = driver;

	dev->seq = 1;

	err = device_register(&dev->dev);
	if (err) {
		printk(KERN_ERR "Failed to register master device. err=%d\n", err);
		memset(dev, 0, sizeof(struct w1_master));
		kfree(dev);
		dev = NULL;
	}

	return dev;
}

E
Evgeniy Polyakov 已提交
98
static void w1_free_dev(struct w1_master *dev)
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108
{
	device_unregister(&dev->dev);
}

int w1_add_master_device(struct w1_bus_master *master)
{
	struct w1_master *dev;
	int retval = 0;
	struct w1_netlink_msg msg;

109 110
        /* validate minimum functionality */
        if (!(master->touch_bit && master->reset_bus) &&
111 112
            !(master->write_bit && master->read_bit) &&
	    !(master->write_byte && master->read_byte && master->reset_bus)) {
113 114
		printk(KERN_ERR "w1_add_master_device: invalid function set\n");
		return(-EINVAL);
115
        }
116 117 118 119 120 121 122 123 124 125 126 127
	/* While it would be electrically possible to make a device that
	 * generated a strong pullup in bit bang mode, only hardare that
	 * controls 1-wire time frames are even expected to support a strong
	 * pullup.  w1_io.c would need to support calling set_pullup before
	 * the last write_bit operation of a w1_write_8 which it currently
	 * doesn't.
	 */
	if (!master->write_byte && !master->touch_bit && master->set_pullup) {
		printk(KERN_ERR "w1_add_master_device: set_pullup requires "
			"write_byte or touch_bit, disabling\n");
		master->set_pullup = NULL;
	}
128

E
Evgeniy Polyakov 已提交
129
	dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
L
Linus Torvalds 已提交
130 131 132
	if (!dev)
		return -ENOMEM;

D
David Fries 已提交
133 134 135 136 137 138 139 140
	retval =  w1_create_master_attributes(dev);
	if (retval)
		goto err_out_free_dev;

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

	dev->initialized = 1;

E
Evgeniy Polyakov 已提交
141 142 143
	dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
	if (IS_ERR(dev->thread)) {
		retval = PTR_ERR(dev->thread);
L
Linus Torvalds 已提交
144 145
		dev_err(&dev->dev,
			 "Failed to create new kernel thread. err=%d\n",
E
Evgeniy Polyakov 已提交
146
			 retval);
D
David Fries 已提交
147
		goto err_out_rm_attr;
L
Linus Torvalds 已提交
148 149
	}

150
	mutex_lock(&w1_mlock);
L
Linus Torvalds 已提交
151
	list_add(&dev->w1_master_entry, &w1_masters);
152
	mutex_unlock(&w1_mlock);
L
Linus Torvalds 已提交
153

154
	memset(&msg, 0, sizeof(msg));
L
Linus Torvalds 已提交
155 156 157 158 159 160
	msg.id.mst.id = dev->id;
	msg.type = W1_MASTER_ADD;
	w1_netlink_send(dev, &msg);

	return 0;

D
David Fries 已提交
161
#if 0 /* Thread cleanup code, not required currently. */
L
Linus Torvalds 已提交
162
err_out_kill_thread:
E
Evgeniy Polyakov 已提交
163
	kthread_stop(dev->thread);
D
David Fries 已提交
164 165 166
#endif
err_out_rm_attr:
	w1_destroy_master_attributes(dev);
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175
err_out_free_dev:
	w1_free_dev(dev);

	return retval;
}

void __w1_remove_master_device(struct w1_master *dev)
{
	struct w1_netlink_msg msg;
176
	struct w1_slave *sl, *sln;
L
Linus Torvalds 已提交
177

E
Evgeniy Polyakov 已提交
178
	kthread_stop(dev->thread);
L
Linus Torvalds 已提交
179

180 181 182 183 184 185 186 187 188 189 190
	mutex_lock(&w1_mlock);
	list_del(&dev->w1_master_entry);
	mutex_unlock(&w1_mlock);

	mutex_lock(&dev->mutex);
	list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry)
		w1_slave_detach(sl);
	w1_destroy_master_attributes(dev);
	mutex_unlock(&dev->mutex);
	atomic_dec(&dev->refcnt);

L
Linus Torvalds 已提交
191
	while (atomic_read(&dev->refcnt)) {
E
Evgeniy Polyakov 已提交
192
		dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
L
Linus Torvalds 已提交
193 194 195 196 197 198
				dev->name, atomic_read(&dev->refcnt));

		if (msleep_interruptible(1000))
			flush_signals(current);
	}

199
	memset(&msg, 0, sizeof(msg));
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208
	msg.id.mst.id = dev->id;
	msg.type = W1_MASTER_REMOVE;
	w1_netlink_send(dev, &msg);

	w1_free_dev(dev);
}

void w1_remove_master_device(struct w1_bus_master *bm)
{
209
	struct w1_master *dev, *found = NULL;
L
Linus Torvalds 已提交
210

E
Evgeniy Polyakov 已提交
211
	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
L
Linus Torvalds 已提交
212 213 214
		if (!dev->initialized)
			continue;

215 216
		if (dev->bus_master->data == bm->data) {
			found = dev;
L
Linus Torvalds 已提交
217
			break;
218
		}
L
Linus Torvalds 已提交
219 220
	}

221
	if (!found) {
L
Linus Torvalds 已提交
222 223 224 225
		printk(KERN_ERR "Device doesn't exist.\n");
		return;
	}

226
	__w1_remove_master_device(found);
L
Linus Torvalds 已提交
227 228 229 230
}

EXPORT_SYMBOL(w1_add_master_device);
EXPORT_SYMBOL(w1_remove_master_device);