aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/sys.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/sys.c')
-rw-r--r--drivers/base/sys.c65
1 files changed, 46 insertions, 19 deletions
diff --git a/drivers/base/sys.c b/drivers/base/sys.c
index 1667aaf4fde6..f6fb54741602 100644
--- a/drivers/base/sys.c
+++ b/drivers/base/sys.c
@@ -166,6 +166,36 @@ EXPORT_SYMBOL_GPL(sysdev_class_unregister);
166 166
167static DEFINE_MUTEX(sysdev_drivers_lock); 167static DEFINE_MUTEX(sysdev_drivers_lock);
168 168
169/*
170 * @dev != NULL means that we're unwinding because some drv->add()
171 * failed for some reason. You need to grab sysdev_drivers_lock before
172 * calling this.
173 */
174static void __sysdev_driver_remove(struct sysdev_class *cls,
175 struct sysdev_driver *drv,
176 struct sys_device *from_dev)
177{
178 struct sys_device *dev = from_dev;
179
180 list_del_init(&drv->entry);
181 if (!cls)
182 return;
183
184 if (!drv->remove)
185 goto kset_put;
186
187 if (dev)
188 list_for_each_entry_continue_reverse(dev, &cls->kset.list,
189 kobj.entry)
190 drv->remove(dev);
191 else
192 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
193 drv->remove(dev);
194
195kset_put:
196 kset_put(&cls->kset);
197}
198
169/** 199/**
170 * sysdev_driver_register - Register auxillary driver 200 * sysdev_driver_register - Register auxillary driver
171 * @cls: Device class driver belongs to. 201 * @cls: Device class driver belongs to.
@@ -175,14 +205,14 @@ static DEFINE_MUTEX(sysdev_drivers_lock);
175 * called on each operation on devices of that class. The refcount 205 * called on each operation on devices of that class. The refcount
176 * of @cls is incremented. 206 * of @cls is incremented.
177 */ 207 */
178
179int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv) 208int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)
180{ 209{
210 struct sys_device *dev = NULL;
181 int err = 0; 211 int err = 0;
182 212
183 if (!cls) { 213 if (!cls) {
184 WARN(1, KERN_WARNING "sysdev: invalid class passed to " 214 WARN(1, KERN_WARNING "sysdev: invalid class passed to %s!\n",
185 "sysdev_driver_register!\n"); 215 __func__);
186 return -EINVAL; 216 return -EINVAL;
187 } 217 }
188 218
@@ -198,19 +228,27 @@ int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)
198 228
199 /* If devices of this class already exist, tell the driver */ 229 /* If devices of this class already exist, tell the driver */
200 if (drv->add) { 230 if (drv->add) {
201 struct sys_device *dev; 231 list_for_each_entry(dev, &cls->kset.list, kobj.entry) {
202 list_for_each_entry(dev, &cls->kset.list, kobj.entry) 232 err = drv->add(dev);
203 drv->add(dev); 233 if (err)
234 goto unwind;
235 }
204 } 236 }
205 } else { 237 } else {
206 err = -EINVAL; 238 err = -EINVAL;
207 WARN(1, KERN_ERR "%s: invalid device class\n", __func__); 239 WARN(1, KERN_ERR "%s: invalid device class\n", __func__);
208 } 240 }
241
242 goto unlock;
243
244unwind:
245 __sysdev_driver_remove(cls, drv, dev);
246
247unlock:
209 mutex_unlock(&sysdev_drivers_lock); 248 mutex_unlock(&sysdev_drivers_lock);
210 return err; 249 return err;
211} 250}
212 251
213
214/** 252/**
215 * sysdev_driver_unregister - Remove an auxillary driver. 253 * sysdev_driver_unregister - Remove an auxillary driver.
216 * @cls: Class driver belongs to. 254 * @cls: Class driver belongs to.
@@ -220,23 +258,12 @@ void sysdev_driver_unregister(struct sysdev_class *cls,
220 struct sysdev_driver *drv) 258 struct sysdev_driver *drv)
221{ 259{
222 mutex_lock(&sysdev_drivers_lock); 260 mutex_lock(&sysdev_drivers_lock);
223 list_del_init(&drv->entry); 261 __sysdev_driver_remove(cls, drv, NULL);
224 if (cls) {
225 if (drv->remove) {
226 struct sys_device *dev;
227 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
228 drv->remove(dev);
229 }
230 kset_put(&cls->kset);
231 }
232 mutex_unlock(&sysdev_drivers_lock); 262 mutex_unlock(&sysdev_drivers_lock);
233} 263}
234
235EXPORT_SYMBOL_GPL(sysdev_driver_register); 264EXPORT_SYMBOL_GPL(sysdev_driver_register);
236EXPORT_SYMBOL_GPL(sysdev_driver_unregister); 265EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
237 266
238
239
240/** 267/**
241 * sysdev_register - add a system device to the tree 268 * sysdev_register - add a system device to the tree
242 * @sysdev: device in question 269 * @sysdev: device in question