blob: 24e7c102fa75d7137a54f84e5a8e7537eb167b06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakov77859252005-05-20 22:33:25 +04002 * w1_int.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Polyakov674a396c2006-02-20 11:15:37 +030025#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "w1.h"
28#include "w1_log.h"
29#include "w1_netlink.h"
30
31static u32 w1_ids = 1;
32
Evgeniy Polyakov12003372006-03-23 19:11:58 +030033extern struct device_driver w1_master_driver;
34extern struct bus_type w1_bus_type;
35extern struct device w1_master_device;
36extern int w1_max_slave_count;
37extern int w1_max_slave_ttl;
38extern struct list_head w1_masters;
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +040039extern struct mutex w1_mlock;
Evgeniy Polyakov12003372006-03-23 19:11:58 +030040
41extern int w1_process(void *);
42
Evgeniy Polyakov77859252005-05-20 22:33:25 +040043static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
44 struct device_driver *driver,
45 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
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 Polyakov77859252005-05-20 22:33:25 +040065 dev->owner = THIS_MODULE;
66 dev->max_slave_count = slave_count;
67 dev->slave_count = 0;
68 dev->attempts = 0;
Evgeniy Polyakov77859252005-05-20 22:33:25 +040069 dev->initialized = 0;
70 dev->id = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 dev->slave_ttl = slave_ttl;
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +040072 dev->search_count = -1; /* continual scan */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 atomic_set(&dev->refcnt, 2);
75
76 INIT_LIST_HEAD(&dev->slist);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +040077 mutex_init(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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 Torvalds1da177e2005-04-16 15:20:36 -070086 dev->seq = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 err = device_register(&dev->dev);
89 if (err) {
90 printk(KERN_ERR "Failed to register master device. err=%d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 memset(dev, 0, sizeof(struct w1_master));
92 kfree(dev);
93 dev = NULL;
94 }
95
96 return dev;
97}
98
Evgeniy Polyakov12003372006-03-23 19:11:58 +030099void w1_free_dev(struct w1_master *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 device_unregister(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104int 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 Polyakovbe57ce22005-06-04 01:21:46 +0400110 /* validate minimum functionality */
111 if (!(master->touch_bit && master->reset_bus) &&
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400112 !(master->write_bit && master->read_bit)) {
113 printk(KERN_ERR "w1_add_master_device: invalid function set\n");
114 return(-EINVAL);
Evgeniy Polyakovbe57ce22005-06-04 01:21:46 +0400115 }
116
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400117 dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (!dev)
119 return -ENOMEM;
120
Evgeniy Polyakov674a396c2006-02-20 11:15:37 +0300121 dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
122 if (IS_ERR(dev->thread)) {
123 retval = PTR_ERR(dev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 dev_err(&dev->dev,
125 "Failed to create new kernel thread. err=%d\n",
Evgeniy Polyakov674a396c2006-02-20 11:15:37 +0300126 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 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 Polyakovabd52a12006-04-03 12:04:27 +0400138 mutex_lock(&w1_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 list_add(&dev->w1_master_entry, &w1_masters);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400140 mutex_unlock(&w1_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300142 memset(&msg, 0, sizeof(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 msg.id.mst.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 msg.type = W1_MASTER_ADD;
145 w1_netlink_send(dev, &msg);
146
147 return 0;
148
149err_out_kill_thread:
Evgeniy Polyakov674a396c2006-02-20 11:15:37 +0300150 kthread_stop(dev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151err_out_free_dev:
152 w1_free_dev(dev);
153
154 return retval;
155}
156
157void __w1_remove_master_device(struct w1_master *dev)
158{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct w1_netlink_msg msg;
160
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400161 set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
Evgeniy Polyakov674a396c2006-02-20 11:15:37 +0300162 kthread_stop(dev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 while (atomic_read(&dev->refcnt)) {
Evgeniy Polyakov674a396c2006-02-20 11:15:37 +0300165 dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dev->name, atomic_read(&dev->refcnt));
167
168 if (msleep_interruptible(1000))
169 flush_signals(current);
170 }
171
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300172 memset(&msg, 0, sizeof(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 msg.id.mst.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 msg.type = W1_MASTER_REMOVE;
175 w1_netlink_send(dev, &msg);
176
177 w1_free_dev(dev);
178}
179
180void w1_remove_master_device(struct w1_bus_master *bm)
181{
182 struct w1_master *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400184 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 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
200EXPORT_SYMBOL(w1_add_master_device);
201EXPORT_SYMBOL(w1_remove_master_device);