[PATCH] w1: Use mutexes instead of semaphores.
[pandora-kernel.git] / drivers / w1 / w1_int.c
1 /*
2  *      w1_int.c
3  *
4  * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5  *
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>
25 #include <linux/kthread.h>
26
27 #include "w1.h"
28 #include "w1_log.h"
29 #include "w1_netlink.h"
30
31 static u32 w1_ids = 1;
32
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;
39 extern struct mutex w1_mlock;
40
41 extern int w1_process(void *);
42
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)
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
65         dev->owner              = THIS_MODULE;
66         dev->max_slave_count    = slave_count;
67         dev->slave_count        = 0;
68         dev->attempts           = 0;
69         dev->initialized        = 0;
70         dev->id                 = id;
71         dev->slave_ttl          = slave_ttl;
72         dev->search_count       = -1; /* continual scan */
73
74         atomic_set(&dev->refcnt, 2);
75
76         INIT_LIST_HEAD(&dev->slist);
77         mutex_init(&dev->mutex);
78
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
86         dev->seq = 1;
87
88         err = device_register(&dev->dev);
89         if (err) {
90                 printk(KERN_ERR "Failed to register master device. err=%d\n", err);
91                 memset(dev, 0, sizeof(struct w1_master));
92                 kfree(dev);
93                 dev = NULL;
94         }
95
96         return dev;
97 }
98
99 void w1_free_dev(struct w1_master *dev)
100 {
101         device_unregister(&dev->dev);
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
110         /* validate minimum functionality */
111         if (!(master->touch_bit && master->reset_bus) &&
112             !(master->write_bit && master->read_bit)) {
113                 printk(KERN_ERR "w1_add_master_device: invalid function set\n");
114                 return(-EINVAL);
115         }
116
117         dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
118         if (!dev)
119                 return -ENOMEM;
120
121         dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
122         if (IS_ERR(dev->thread)) {
123                 retval = PTR_ERR(dev->thread);
124                 dev_err(&dev->dev,
125                          "Failed to create new kernel thread. err=%d\n",
126                          retval);
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
138         mutex_lock(&w1_mlock);
139         list_add(&dev->w1_master_entry, &w1_masters);
140         mutex_unlock(&w1_mlock);
141
142         memset(&msg, 0, sizeof(msg));
143         msg.id.mst.id = dev->id;
144         msg.type = W1_MASTER_ADD;
145         w1_netlink_send(dev, &msg);
146
147         return 0;
148
149 err_out_kill_thread:
150         kthread_stop(dev->thread);
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 {
159         struct w1_netlink_msg msg;
160
161         set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
162         kthread_stop(dev->thread);
163
164         while (atomic_read(&dev->refcnt)) {
165                 dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
166                                 dev->name, atomic_read(&dev->refcnt));
167
168                 if (msleep_interruptible(1000))
169                         flush_signals(current);
170         }
171
172         memset(&msg, 0, sizeof(msg));
173         msg.id.mst.id = dev->id;
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;
183
184         list_for_each_entry(dev, &w1_masters, w1_master_entry) {
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);