aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormochel@digitalimplant.org <mochel@digitalimplant.org>2005-03-21 13:41:04 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2005-06-20 18:15:12 -0400
commitaf70316af182f4716cc5eec7e0d27fc731d164bd (patch)
tree22fa4732c8270db8fd3f681355cd83e4b8088847
parenteb51b65005737b777e0709683b061d5f82aefd97 (diff)
[PATCH] Add a semaphore to struct device to synchronize calls to its driver.
This adds a per-device semaphore that is taken before every call from the core to a driver method. This prevents e.g. simultaneous calls to the ->suspend() or ->resume() and ->probe() or ->release(), potentially saving a whole lot of headaches. It also moves us a step closer to removing the bus rwsem, since it protects the fields in struct device that are modified by the core. Signed-off-by: Patrick Mochel <mochel@digitalimplant.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r--drivers/base/bus.c14
-rw-r--r--drivers/base/core.c1
-rw-r--r--drivers/base/power/resume.c8
-rw-r--r--drivers/base/power/suspend.c3
-rw-r--r--include/linux/device.h4
5 files changed, 24 insertions, 6 deletions
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 80ce88de56fa..aa27f76d28cd 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -283,18 +283,22 @@ void device_bind_driver(struct device * dev)
283 */ 283 */
284int driver_probe_device(struct device_driver * drv, struct device * dev) 284int driver_probe_device(struct device_driver * drv, struct device * dev)
285{ 285{
286 int error = 0;
287
286 if (drv->bus->match && !drv->bus->match(dev, drv)) 288 if (drv->bus->match && !drv->bus->match(dev, drv))
287 return -ENODEV; 289 return -ENODEV;
288 290
291 down(&dev->sem);
289 dev->driver = drv; 292 dev->driver = drv;
290 if (drv->probe) { 293 if (drv->probe) {
291 int error = drv->probe(dev); 294 error = drv->probe(dev);
292 if (error) { 295 if (error) {
293 dev->driver = NULL; 296 dev->driver = NULL;
297 up(&dev->sem);
294 return error; 298 return error;
295 } 299 }
296 } 300 }
297 301 up(&dev->sem);
298 device_bind_driver(dev); 302 device_bind_driver(dev);
299 return 0; 303 return 0;
300} 304}
@@ -385,7 +389,10 @@ void driver_attach(struct device_driver * drv)
385 389
386void device_release_driver(struct device * dev) 390void device_release_driver(struct device * dev)
387{ 391{
388 struct device_driver * drv = dev->driver; 392 struct device_driver * drv;
393
394 down(&dev->sem);
395 drv = dev->driver;
389 if (drv) { 396 if (drv) {
390 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj)); 397 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
391 sysfs_remove_link(&dev->kobj, "driver"); 398 sysfs_remove_link(&dev->kobj, "driver");
@@ -394,6 +401,7 @@ void device_release_driver(struct device * dev)
394 drv->remove(dev); 401 drv->remove(dev);
395 dev->driver = NULL; 402 dev->driver = NULL;
396 } 403 }
404 up(&dev->sem);
397} 405}
398 406
399 407
diff --git a/drivers/base/core.c b/drivers/base/core.c
index a293a788abd4..93440824b800 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -212,6 +212,7 @@ void device_initialize(struct device *dev)
212 INIT_LIST_HEAD(&dev->driver_list); 212 INIT_LIST_HEAD(&dev->driver_list);
213 INIT_LIST_HEAD(&dev->bus_list); 213 INIT_LIST_HEAD(&dev->bus_list);
214 INIT_LIST_HEAD(&dev->dma_pools); 214 INIT_LIST_HEAD(&dev->dma_pools);
215 init_MUTEX(&dev->sem);
215} 216}
216 217
217/** 218/**
diff --git a/drivers/base/power/resume.c b/drivers/base/power/resume.c
index 26468971ef5a..bdd96b03b885 100644
--- a/drivers/base/power/resume.c
+++ b/drivers/base/power/resume.c
@@ -22,6 +22,9 @@ extern int sysdev_resume(void);
22 22
23int resume_device(struct device * dev) 23int resume_device(struct device * dev)
24{ 24{
25 int error = 0;
26
27 down(&dev->sem);
25 if (dev->power.pm_parent 28 if (dev->power.pm_parent
26 && dev->power.pm_parent->power.power_state) { 29 && dev->power.pm_parent->power.power_state) {
27 dev_err(dev, "PM: resume from %d, parent %s still %d\n", 30 dev_err(dev, "PM: resume from %d, parent %s still %d\n",
@@ -31,9 +34,10 @@ int resume_device(struct device * dev)
31 } 34 }
32 if (dev->bus && dev->bus->resume) { 35 if (dev->bus && dev->bus->resume) {
33 dev_dbg(dev,"resuming\n"); 36 dev_dbg(dev,"resuming\n");
34 return dev->bus->resume(dev); 37 error = dev->bus->resume(dev);
35 } 38 }
36 return 0; 39 up(&dev->sem);
40 return error;
37} 41}
38 42
39 43
diff --git a/drivers/base/power/suspend.c b/drivers/base/power/suspend.c
index 0ec44ef840be..807e13fb205b 100644
--- a/drivers/base/power/suspend.c
+++ b/drivers/base/power/suspend.c
@@ -39,6 +39,7 @@ int suspend_device(struct device * dev, pm_message_t state)
39{ 39{
40 int error = 0; 40 int error = 0;
41 41
42 down(&dev->sem);
42 if (dev->power.power_state) { 43 if (dev->power.power_state) {
43 dev_dbg(dev, "PM: suspend %d-->%d\n", 44 dev_dbg(dev, "PM: suspend %d-->%d\n",
44 dev->power.power_state, state); 45 dev->power.power_state, state);
@@ -58,7 +59,7 @@ int suspend_device(struct device * dev, pm_message_t state)
58 dev_dbg(dev, "suspending\n"); 59 dev_dbg(dev, "suspending\n");
59 error = dev->bus->suspend(dev, state); 60 error = dev->bus->suspend(dev, state);
60 } 61 }
61 62 up(&dev->sem);
62 return error; 63 return error;
63} 64}
64 65
diff --git a/include/linux/device.h b/include/linux/device.h
index 68a95358013d..195b327f3e19 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -264,6 +264,10 @@ struct device {
264 struct kobject kobj; 264 struct kobject kobj;
265 char bus_id[BUS_ID_SIZE]; /* position on parent bus */ 265 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
266 266
267 struct semaphore sem; /* semaphore to synchronize calls to
268 * its driver.
269 */
270
267 struct bus_type * bus; /* type of bus device is on */ 271 struct bus_type * bus; /* type of bus device is on */
268 struct device_driver *driver; /* which driver has allocated this 272 struct device_driver *driver; /* which driver has allocated this
269 device */ 273 device */